Debian Codegraph @

src/videofilters/h26x/nal-unpacker.cpp

raw
1 /*
2 * Copyright (c) 2010-2022 Belledonne Communications SARL.
3 *
4 * This file is part of mediastreamer2
5 * (see https://gitlab.linphone.org/BC/public/mediastreamer2).
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <bitset>
22
23 #include "nal-unpacker.h"
24
25 using namespace std;
26
27 namespace mediastreamer {
28
29 NalUnpacker::Status &NalUnpacker::Status::operator|=(const Status &s2) {
30 this->frameAvailable = (this->frameAvailable || s2.frameAvailable);
31 this->frameCorrupted = (this->frameCorrupted || s2.frameCorrupted);
32 this->isKeyFrame = (this->isKeyFrame || s2.isKeyFrame);
33 return *this;
34 }
35
36 unsigned int NalUnpacker::Status::toUInt() const {
37 bitset<3> flags;
38 if (frameAvailable) flags.set(0);
39 if (frameCorrupted) flags.set(1);
40 if (isKeyFrame) flags.set(2);
41 return flags.to_ulong();
42 }
43
44 NalUnpacker::NalUnpacker(FuAggregatorInterface *aggregator, ApSpliterInterface *spliter)
45 : _fuAggregator(aggregator), _apSpliter(spliter) {
46 ms_queue_init(&_q);
47 }
48
49 NalUnpacker::Status NalUnpacker::unpack(mblk_t *im, MSQueue *out) {
50 PacketType type = getNaluType(im);
51 int marker = mblk_get_marker_info(im);
52 uint32_t ts = mblk_get_timestamp_info(im);
53 uint16_t cseq = mblk_get_cseq(im);
54 Status ret;
55
56 if (_lastTs != ts) {
57 /*a new frame is arriving, in case the marker bit was not set in previous frame, output it now,
58 * unless it is a FU-A packet (workaround for buggy implementations).
59 * The marker bit may be absent because of a packet loss (then the frame is corrupted),
60 * or because the remote implementation is not compliant, for example when sending SPS and PPS with
61 * timestamp different than the following IDR frame.
62 */
63 _lastTs = ts;
64 if (!_fuAggregator->isAggregating() && !ms_queue_empty(&_q)) {
65 Status status;
66 status.frameAvailable = true;
67 status.frameCorrupted |= (cseq != _refCSeq + 1);
68 ret = outputFrame(out, status);
69 ms_warning("Incomplete H264 frame (missing marker bit after seq number %u, frame corrupted: %s)",
70 mblk_get_cseq(ms_queue_peek_last(out)), status.frameCorrupted ? "yes" : "no");
71 }
72 }
73
74 if (im->b_cont) msgpullup(im, -1);
75
76 if (!_initializedRefCSeq) {
77 _initializedRefCSeq = TRUE;
78 _refCSeq = cseq;
79 } else {
80 _refCSeq++;
81 if (_refCSeq != cseq) {
82 ms_message("sequence inconsistency detected (diff=%i)", (int)(cseq - _refCSeq));
83 _refCSeq = cseq;
84 _status.frameCorrupted = true;
85 }
86 }
87
88 switch (type) {
89 case PacketType::SingleNalUnit:
90 _fuAggregator->reset();
91 /*single nal unit*/
92 ms_debug("Receiving single NAL");
93 storeNal(im);
94 break;
95 case PacketType::FragmentationUnit: {
96 try {
97 ms_debug("Receiving FU-A");
98 mblk_t *o = _fuAggregator->feed(im);
99 if (o) storeNal(o);
100 } catch (const invalid_argument &e) {
101 ms_error("%s", e.what());
102 _fuAggregator->reset();
103 _status.frameCorrupted = true;
104 }
105 break;
106 }
107 case PacketType::AggregationPacket:
108 ms_debug("Receiving STAP-A");
109 _apSpliter->feed(im);
110 while ((im = ms_queue_get(_apSpliter->getNalus()))) {
111 storeNal(im);
112 }
113 break;
114 }
115
116 if (marker) {
117 _lastTs = ts;
118 ms_debug("Marker bit set");
119 Status status;
120 status.frameAvailable = true;
121 ret = outputFrame(out, status);
122 }
123
124 return ret;
125 }
126
127 void NalUnpacker::reset() {
128 ms_queue_flush(&_q);
129 _status = Status();
130 _initializedRefCSeq = false;
131 _fuAggregator->reset();
132 }
133
134 NalUnpacker::Status NalUnpacker::outputFrame(MSQueue *out, const Status &flags) {
135 Status res = _status;
136 if (!ms_queue_empty(out)) {
137 ms_warning("rfc3984_unpack: output_frame invoked several times in a row, this should not happen");
138 }
139 res |= flags;
140 while (!ms_queue_empty(&_q)) {
141 ms_queue_put(out, ms_queue_get(&_q));
142 }
143 _status = Status();
144 return res;
145 }
146
147 void NalUnpacker::storeNal(mblk_t *nal) {
148 ms_queue_put(&_q, nal);
149 }
150
151 } // namespace mediastreamer