Debian Codegraph @

src/videofilters/h26x/h26x-utils.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 <stdexcept>
22
23 #include "h264-utils.h"
24 #include "h265-utils.h"
25
26 #include "h26x-utils.h"
27
28 using namespace std;
29
30 namespace mediastreamer {
31
32 void H26xUtils::naluStreamToNalus(const std::vector<uint8_t> &byteStream, MSQueue *out) {
33 H26xUtils::naluStreamToNalus(byteStream.data(), byteStream.size(), out);
34 }
35
36 void H26xUtils::naluStreamToNalus(const uint8_t *bytestream, size_t size, MSQueue *out) {
37 const uint8_t *ptr = bytestream;
38 while (ptr < bytestream + size) {
39 uint32_t nalu_size;
40 memcpy(&nalu_size, ptr, 4);
41 nalu_size = ntohl(nalu_size);
42
43 mblk_t *nalu = allocb(nalu_size, 0);
44 memcpy(nalu->b_wptr, ptr + 4, nalu_size);
45 ptr += nalu_size + 4;
46 nalu->b_wptr += nalu_size;
47
48 ms_queue_put(out, nalu);
49 }
50 }
51
52 void H26xUtils::byteStreamToNalus(const std::vector<uint8_t> &byteStream, MSQueue *out, bool removePreventionBytes) {
53 H26xUtils::byteStreamToNalus(byteStream.data(), byteStream.size(), out, removePreventionBytes);
54 }
55
56 static bool isPictureStartCode(const uint8_t *bytestream, size_t size) {
57 if (size <= 4) return false;
58 if (bytestream[0] == 0 && bytestream[1] == 0 && bytestream[2] == 0 && bytestream[3] == 1) return true;
59 return false;
60 }
61
62 mblk_t *H26xUtils::makeNalu(const uint8_t *byteStream,
63 size_t naluSize,
64 bool removePreventionBytes,
65 int *preventionBytesRemoved) {
66 mblk_t *nalu = allocb(naluSize, 0);
67 const uint8_t *it;
68 const uint8_t *end = byteStream + naluSize;
69 for (it = byteStream; it < end;) {
70 if (removePreventionBytes && it[0] == 0 && it + 3 < end && it[1] == 0 && it[2] == 3 && it[3] == 1) {
71 /* Found 0x00000301, replace by 0x000001*/
72 it += 3;
73 *nalu->b_wptr++ = 0;
74 *nalu->b_wptr++ = 0;
75 *nalu->b_wptr++ = 1;
76 (*preventionBytesRemoved)++;
77 } else {
78 *nalu->b_wptr++ = *it++;
79 }
80 }
81 return nalu;
82 }
83
84 void H26xUtils::byteStreamToNalus(const uint8_t *byteStream, size_t size, MSQueue *out, bool removePreventionBytes) {
85 int preventionBytesRemoved = 0;
86 size_t i;
87 size_t begin, end;
88 size_t naluSize;
89
90 if (!isPictureStartCode(byteStream, size)) {
91 ms_error("no picture start code found in H26x byte stream");
92 throw invalid_argument("no picutre start code found in H26x byte stream");
93 return;
94 }
95 begin = 4;
96 for (i = begin; i + 3 < size; ++i) {
97 if (byteStream[i] == 0 && byteStream[i + 1] == 0 && byteStream[i + 2] == 1) {
98 end = i;
99 naluSize = end - begin;
100 ms_queue_put(out, makeNalu(byteStream + begin, naluSize, removePreventionBytes, &preventionBytesRemoved));
101 i += 3;
102 begin = i;
103 }
104 }
105 naluSize = size - begin;
106 ms_queue_put(out, makeNalu(byteStream + begin, naluSize, removePreventionBytes, &preventionBytesRemoved));
107
108 if (preventionBytesRemoved > 0) {
109 ms_message("Removed %i start code prevention bytes", preventionBytesRemoved);
110 }
111 }
112
113 size_t H26xUtils::nalusToByteStream(MSQueue *nalus, uint8_t *byteStream, size_t size) {
114 bool startPicture = true;
115 uint8_t *byteStreamEnd = byteStream + size;
116 uint8_t *it = byteStream;
117
118 if (size < 4) throw invalid_argument("Insufficient buffer size");
119
120 while (mblk_t *im = ms_queue_get(nalus)) {
121 if (startPicture) {
122 // starting picture extra zero byte
123 *it++ = 0;
124 startPicture = false;
125 }
126
127 // starting NALu marker
128 *it++ = 0;
129 *it++ = 0;
130 *it++ = 1;
131
132 // copy NALu content
133 for (const uint8_t *src = im->b_rptr; src < im->b_wptr && it < byteStreamEnd;) {
134 if (src[0] == 0 && src + 2 < im->b_wptr && src[1] == 0 && (/*src[2] == 0 ||*/ src[2] == 1)) {
135 if (it + 3 < byteStreamEnd) {
136 *it++ = 0;
137 *it++ = 0;
138 *it++ = 3; // emulation prevention three byte
139 src += 2;
140 } else throw invalid_argument("Insufficient buffer size");
141 } else {
142 *it++ = *src++;
143 }
144 }
145 freemsg(im);
146 if (it == byteStreamEnd) throw invalid_argument("Insufficient buffer size");
147 }
148 return it - byteStream;
149 }
150
151 void H26xParameterSetsInserter::replaceParameterSet(mblk_t *&ps, mblk_t *newPs) {
152 if (ps) freemsg(ps);
153 ps = newPs;
154 }
155
156 H26xParameterSetsStore::H26xParameterSetsStore(const std::string &mime, const std::initializer_list<int> &psCodes) {
157 _naluHeader.reset(H26xToolFactory::get(mime).createNaluHeader());
158 for (int psCode : psCodes) {
159 _ps[psCode] = nullptr;
160 }
161 }
162
163 H26xParameterSetsStore::~H26xParameterSetsStore() {
164 for (auto it = _ps.begin(); it != _ps.end(); it++) {
165 if (it->second) freemsg(it->second);
166 }
167 }
168
169 bool H26xParameterSetsStore::psGatheringCompleted() const {
170 for (const auto &item : _ps) {
171 if (item.second == nullptr) return false;
172 }
173 return true;
174 }
175
176 void H26xParameterSetsStore::extractAllPs(MSQueue *frame) {
177 for (mblk_t *nalu = ms_queue_peek_first(frame); !ms_queue_end(frame, nalu);) {
178 _naluHeader->parse(nalu->b_rptr);
179 int type = _naluHeader->getAbsType();
180 if (_ps.find(type) != _ps.end()) {
181 mblk_t *ps = nalu;
182 nalu = ms_queue_next(frame, nalu);
183 ms_queue_remove(frame, ps);
184 addPs(type, ps);
185 continue;
186 }
187 nalu = ms_queue_next(frame, nalu);
188 }
189 }
190
191 void H26xParameterSetsStore::fetchAllPs(MSQueue *outq) const {
192 MSQueue q;
193 ms_queue_init(&q);
194 for (const auto &item : _ps) {
195 if (item.second) {
196 ms_queue_put(outq, dupmsg(item.second));
197 }
198 }
199 }
200
201 void H26xParameterSetsStore::addPs(int naluType, mblk_t *nalu) {
202 bool replaceParam = false;
203 mblk_t *lastPs = _ps[naluType];
204
205 if (lastPs == nullptr || nalu == nullptr) {
206 replaceParam = true;
207 } else {
208 ssize_t naluSize = (ssize_t)(nalu->b_wptr - nalu->b_rptr);
209 ssize_t lastPsSize = (ssize_t)(lastPs->b_wptr - lastPs->b_rptr);
210 if (naluSize != lastPsSize || memcmp(nalu->b_rptr, lastPs->b_rptr, naluSize) != 0) {
211 replaceParam = true;
212 }
213 }
214
215 if (replaceParam) {
216 if (lastPs) freemsg(lastPs);
217 _ps[naluType] = nalu ? dupmsg(nalu) : nullptr;
218 _newParameters = true;
219 }
220 }
221
222 const H26xToolFactory &H26xToolFactory::get(const std::string &mime) {
223 unique_ptr<H26xToolFactory> &instance = _instances[mime];
224 if (instance == nullptr) {
225 if (mime == "video/avc") instance.reset(new H264ToolFactory());
226 else if (mime == "video/hevc") instance.reset(new H265ToolFactory());
227 else throw invalid_argument("no H26xToolFactory class associated to '" + mime + "' mime");
228 }
229 return *instance;
230 }
231
232 std::unordered_map<std::string, std::unique_ptr<H26xToolFactory>> H26xToolFactory::_instances;
233
234 } // namespace mediastreamer