Debian Codegraph @

src/videofilters/h26x/h264-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 "mediastreamer2/bits_rw.h"
22 #include "mediastreamer2/rfc3984.h"
23
24 #include "h264-nal-packer.h"
25 #include "h264-nal-unpacker.h"
26 #include "h264-utils.h"
27
28 using namespace mediastreamer;
29 using namespace std;
30
31 extern "C" {
32
33 void ms_h264_bitstream_to_nalus(const uint8_t *bitstream, size_t size, MSQueue *nalus) {
34 H26xUtils::byteStreamToNalus(bitstream, size, nalus);
35 }
36
37 uint8_t ms_h264_nalu_get_nri(const mblk_t *nalu) {
38 return ((*nalu->b_rptr) >> 5) & 0x3;
39 }
40
41 MSH264NaluType ms_h264_nalu_get_type(const mblk_t *nalu) {
42 return (*nalu->b_rptr) & ((1 << 5) - 1);
43 }
44
45 unsigned int _ms_h264_get_id(const mblk_t *parameter_set, size_t offset, const char *symbol_name) {
46 unsigned int id;
47 MSBitsReader reader;
48 const uint8_t *buffer = parameter_set->b_rptr + offset;
49 size_t bufsize = parameter_set->b_wptr - buffer;
50 ms_bits_reader_init(&reader, buffer, bufsize);
51 return ms_bits_reader_ue(&reader, &id, symbol_name) == 0 ? id : 0;
52 }
53
54 unsigned int ms_h264_sps_get_id(const mblk_t *sps) {
55 return _ms_h264_get_id(sps, 4, "seq_parameter_set_id");
56 }
57
58 unsigned int ms_h264_pps_get_id(const mblk_t *pps) {
59 return _ms_h264_get_id(pps, 1, "pic_parameter_set_id");
60 }
61
62 void ms_h264_stream_to_nalus(const uint8_t *frame, size_t size, MSQueue *nalus, int *idr_count) {
63 const uint8_t *ptr = frame;
64
65 if (idr_count) *idr_count = 0;
66
67 while (ptr < frame + size) {
68 uint32_t nalu_size;
69 mblk_t *nalu;
70 MSH264NaluType nalu_type;
71
72 memcpy(&nalu_size, ptr, 4);
73 nalu_size = ntohl(nalu_size);
74
75 nalu = allocb(nalu_size, 0);
76 memcpy(nalu->b_wptr, ptr + 4, nalu_size);
77 ptr += nalu_size + 4;
78 nalu->b_wptr += nalu_size;
79
80 nalu_type = ms_h264_nalu_get_type(nalu);
81 if (idr_count && nalu_type == MSH264NaluTypeIDR) (*idr_count)++;
82
83 ms_queue_put(nalus, nalu);
84 }
85 }
86
87 MSVideoSize ms_h264_sps_get_video_size(const mblk_t *sps) {
88 MSVideoSize video_size;
89 MSBitsReader reader;
90 unsigned int profile_idc;
91 unsigned int pic_order_cnt_type;
92 unsigned int pic_width_in_mbs_minus1;
93 unsigned int pic_height_in_map_units_minus1;
94 unsigned int frame_mbs_only_flag;
95 unsigned int frame_cropping_flag;
96 /* init reader, but skip 1 byte (nal_unit_type) */
97 ms_bits_reader_init(&reader, sps->b_rptr + 1, sps->b_wptr - sps->b_rptr - 1);
98
99 ms_bits_reader_n_bits(&reader, 8, &profile_idc, "profile_idc");
100 ms_bits_reader_n_bits(&reader, 1, NULL, "constraint_set0_flag");
101 ms_bits_reader_n_bits(&reader, 1, NULL, "constraint_set1_flag");
102 ms_bits_reader_n_bits(&reader, 1, NULL, "constraint_set2_flag");
103 ms_bits_reader_n_bits(&reader, 5, NULL, "reserved_zero_5bits");
104 ms_bits_reader_n_bits(&reader, 8, NULL, "level_idc");
105 ms_bits_reader_ue(&reader, NULL, "seq_parameter_set_id");
106
107 if (profile_idc == 100) {
108 { ms_bits_reader_ue(&reader, NULL, "chroma_format_idc"); }
109 ms_bits_reader_ue(&reader, NULL, "bit_depth_luma_minus8");
110 ms_bits_reader_ue(&reader, NULL, "bit_depth_chroma_minus8");
111 ms_bits_reader_n_bits(&reader, 1, NULL, "qpprime_y_zero_transform_bypass_flag");
112 ms_bits_reader_n_bits(&reader, 1, NULL, "seq_scaling_matrix_present_flag");
113 }
114 ms_bits_reader_ue(&reader, NULL, "log2_max_frame_num_minus4");
115
116 ms_bits_reader_ue(&reader, &pic_order_cnt_type, "pic_order_cnt_type");
117
118 if (pic_order_cnt_type == 0) {
119 ms_bits_reader_ue(&reader, NULL, "log2_max_pic_order_cnt_lsb_minus4");
120 } else if (pic_order_cnt_type == 1) {
121 int i;
122 { ms_bits_reader_n_bits(&reader, 1, NULL, "delta_pic_order_always_zero_flag"); }
123 ms_bits_reader_se(&reader, NULL, "offset_for_non_ref_pic");
124 ms_bits_reader_se(&reader, NULL, "offset_for_top_to_bottom_field");
125 {
126 unsigned int num_ref_frames_in_pic_order_cnt_cycle;
127 ms_bits_reader_ue(&reader, &num_ref_frames_in_pic_order_cnt_cycle, "num_ref_frames_in_pic_order_cnt_cycle");
128 for (i = 0; i < (int)num_ref_frames_in_pic_order_cnt_cycle; i++) {
129 ms_bits_reader_se(&reader, NULL, "offset_for_ref_frame[ i ]");
130 }
131 }
132 }
133
134 ms_bits_reader_ue(&reader, NULL, "num_ref_frames");
135 ms_bits_reader_n_bits(&reader, 1, NULL, "gaps_in_frame_num_value_allowed_flag");
136
137 ms_bits_reader_ue(&reader, &pic_width_in_mbs_minus1, "pic_width_in_mbs_minus1");
138
139 ms_bits_reader_ue(&reader, &pic_height_in_map_units_minus1, "pic_height_in_map_units_minus1");
140
141 ms_bits_reader_n_bits(&reader, 1, &frame_mbs_only_flag, "frame_mbs_only_flag");
142
143 if (!frame_mbs_only_flag) {
144 ms_bits_reader_n_bits(&reader, 1, NULL, "mb_adaptive_frame_field_flag");
145 }
146
147 ms_bits_reader_n_bits(&reader, 1, NULL, "direct_8x8_inference_flag");
148
149 ms_bits_reader_n_bits(&reader, 1, &frame_cropping_flag, "frame_cropping_flag");
150
151 if (frame_cropping_flag) {
152 unsigned int frame_crop_left_offset;
153 unsigned int frame_crop_right_offset;
154 unsigned int frame_crop_top_offset;
155 unsigned int frame_crop_bottom_offset;
156 ms_bits_reader_ue(&reader, &frame_crop_left_offset, "frame_crop_left_offset");
157 ms_bits_reader_ue(&reader, &frame_crop_right_offset, "frame_crop_right_offset");
158 video_size.width =
159 ((pic_width_in_mbs_minus1 + 1) * 16) - frame_crop_left_offset * 2 - frame_crop_right_offset * 2;
160 ms_bits_reader_ue(&reader, &frame_crop_top_offset, "frame_crop_top_offset");
161 ms_bits_reader_ue(&reader, &frame_crop_bottom_offset, "frame_crop_bottom_offset");
162 video_size.height = ((2 - frame_mbs_only_flag) * (pic_height_in_map_units_minus1 + 1) * 16) -
163 (frame_crop_top_offset * 2) - (frame_crop_bottom_offset * 2);
164 } else {
165 video_size.width = (pic_width_in_mbs_minus1 + 1) * 16;
166 video_size.height = (2 - frame_mbs_only_flag) * (pic_height_in_map_units_minus1 + 1) * 16;
167 }
168
169 ms_bits_reader_n_bits(&reader, 1, NULL, "vui_parameters_present_flag");
170 return video_size;
171 }
172
173 } // extern "C"
174
175 namespace mediastreamer {
176
177 H264NaluType::H264NaluType(uint8_t value) {
178 if (value & 0xe0) throw out_of_range("H264 NALu type higher than 31");
179 _value = value;
180 }
181
182 const H264NaluType H264NaluType::DataPartA = 2;
183 const H264NaluType H264NaluType::DataPartB = 3;
184 const H264NaluType H264NaluType::DataPartC = 4;
185 const H264NaluType H264NaluType::Idr = 5;
186 const H264NaluType H264NaluType::Sei = 6;
187 const H264NaluType H264NaluType::Sps = 7;
188 const H264NaluType H264NaluType::Pps = 8;
189 const H264NaluType H264NaluType::StapA = 24;
190 const H264NaluType H264NaluType::FuA = 28;
191
192 void H264NaluHeader::setNri(uint8_t nri) {
193 if (nri > 3) throw out_of_range("H264 NALu NRI higher than 3");
194 _nri = nri;
195 }
196
197 bool H264NaluHeader::operator==(const H264NaluHeader &h2) const {
198 return _fBit == h2._fBit && _type == h2._type && _nri == h2._nri;
199 }
200
201 void H264NaluHeader::parse(const uint8_t *header) {
202 uint8_t h = *header;
203 _type = H264NaluType(h & 0x1f);
204 h >>= 5;
205 _nri = (h & 0x3);
206 h >>= 2;
207 _fBit = (h != 0);
208 }
209
210 mblk_t *H264NaluHeader::forge() const {
211 uint8_t h = _fBit ? 1 : 0;
212 h <<= 2;
213 h |= _nri;
214 h <<= 5;
215 h |= uint8_t(_type);
216
217 mblk_t *m = allocb(1, 0);
218 *m->b_wptr++ = h;
219 return m;
220 }
221
222 unsigned int H264FrameAnalyser::Info::toUInt() const {
223 unsigned int res = 0;
224 if (this->hasIdr) res |= Rfc3984HasIDR;
225 if (this->hasSps) res |= Rfc3984HasSPS;
226 if (this->hasPps) res |= Rfc3984HasPPS;
227 if (this->newSps) res |= Rfc3984NewSPS;
228 if (this->newPps) res |= Rfc3984NewPPS;
229 if (this->hasIdr && this->hasSps && this->hasPps) res |= Rfc3984IsKeyFrame;
230 return res;
231 }
232
233 H264FrameAnalyser::~H264FrameAnalyser() {
234 if (_lastSps) freemsg(_lastSps);
235 if (_lastPps) freemsg(_lastPps);
236 }
237
238 H264FrameAnalyser::Info H264FrameAnalyser::analyse(const MSQueue *frame) {
239 Info info;
240 for (const mblk_t *nalu = qbegin(&frame->q); !qend(&frame->q, nalu); nalu = qnext(&frame->q, nalu)) {
241 MSH264NaluType type = ms_h264_nalu_get_type(nalu);
242 if (type == MSH264NaluTypeIDR) {
243 info.hasIdr = true;
244 } else if (type == MSH264NaluTypeSPS) {
245 info.hasSps = true;
246 info.newSps = updateParameterSet(nalu);
247 } else if (type == MSH264NaluTypePPS) {
248 info.hasPps = true;
249 info.newPps = updateParameterSet(nalu);
250 }
251 }
252 return info;
253 }
254
255 bool H264FrameAnalyser::updateParameterSet(const mblk_t *new_parameter_set) {
256 mblk_t *&last_parameter_set = ms_h264_nalu_get_type(new_parameter_set) == MSH264NaluTypePPS ? _lastPps : _lastSps;
257 if (last_parameter_set != nullptr) {
258 size_t last_size = last_parameter_set->b_wptr - last_parameter_set->b_rptr;
259 size_t new_size = new_parameter_set->b_wptr - new_parameter_set->b_rptr;
260 if (last_size != new_size || memcmp(last_parameter_set->b_rptr, new_parameter_set->b_rptr, new_size) != 0) {
261 freemsg(last_parameter_set);
262 last_parameter_set = copyb(new_parameter_set);
263 return true;
264 } else {
265 return false;
266 }
267 } else {
268 last_parameter_set = copyb(new_parameter_set);
269 return true;
270 }
271 }
272
273 mblk_t *H264Tools::prependFuIndicatorAndHeader(mblk_t *m, uint8_t indicator, bool_t start, bool_t end, uint8_t type) {
274 mblk_t *h = allocb(2, 0);
275 h->b_wptr[0] = indicator;
276 h->b_wptr[1] = ((start & 0x1) << 7) | ((end & 0x1) << 6) | type;
277 h->b_wptr += 2;
278 h->b_cont = m;
279 if (start) m->b_rptr++; /*skip original nalu header */
280 return h;
281 }
282
283 H264ParameterSetsInserter::~H264ParameterSetsInserter() {
284 flush();
285 }
286
287 void H264ParameterSetsInserter::process(MSQueue *in, MSQueue *out) {
288 H264NaluHeader header;
289 bool isKeyFrame = false;
290 while (mblk_t *m = ms_queue_get(in)) {
291 header.parse(m->b_rptr);
292 if (header.getType() == H264NaluType::Sps) {
293 replaceParameterSet(_sps, m);
294 } else if (header.getType() == H264NaluType::Pps) {
295 replaceParameterSet(_pps, m);
296 } else {
297 if (header.getType().isKeyFramePart()) {
298 ms_message("H264ParameterSetsInserter: I-frame detected");
299 isKeyFrame = true;
300 }
301 ms_queue_put(out, m);
302 }
303 }
304 if (isKeyFrame) {
305 mblk_t *insPoint = ms_queue_peek_first(out);
306 ms_queue_insert(out, insPoint, dupmsg(_sps));
307 ms_queue_insert(out, insPoint, dupmsg(_pps));
308 ms_message("H264ParameterSetsInserter: parameter sets inserted");
309 }
310 }
311
312 void H264ParameterSetsInserter::flush() {
313 replaceParameterSet(_sps, nullptr);
314 replaceParameterSet(_pps, nullptr);
315 }
316
317 H26xNaluHeader *H264ToolFactory::createNaluHeader() const {
318 return new H264NaluHeader();
319 }
320
321 NalPacker *H264ToolFactory::createNalPacker(size_t maxPayloadSize) const {
322 return new H264NalPacker(maxPayloadSize);
323 }
324
325 NalUnpacker *H264ToolFactory::createNalUnpacker() const {
326 return new H264NalUnpacker();
327 }
328
329 H26xParameterSetsInserter *H264ToolFactory::createParameterSetsInserter() const {
330 return new H264ParameterSetsInserter();
331 }
332
333 H26xParameterSetsStore *H264ToolFactory::createParameterSetsStore() const {
334 return new H264ParameterSetsStore();
335 }
336
337 } // namespace mediastreamer