1 /*
2 * H.266 / VVC parser
3 *
4 * Copyright (C) 2021 Nuo Mi <nuomi2021@gmail.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
28
29 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
30 #define IS_IDR(nut) (nut == VVC_IDR_W_RADL || nut == VVC_IDR_N_LP)
31 #define IS_H266_SLICE(nut) (nut <= VVC_RASL_NUT || (nut >= VVC_IDR_W_RADL && nut <= VVC_GDR_NUT))
32
40
46
50
52
55
57
60
64 };
65
69 };
70
72 {
73 switch (
sps->sps_bitdepth_minus8) {
74 case 0:
76 case 2:
78 }
80 }
81
82 /**
83 * Find the end of the current frame in the bitstream.
84 * @return the position of the first byte of the next frame, or END_NOT_FOUND
85 */
87 int buf_size)
88 {
92
93 for (
i = 0;
i < buf_size;
i++) {
94 int nut, code_len;
95
97
99 continue;
100
101 code_len = ((pc->
state64 >> 3 * 8) & 0xFFFFFFFF) == 0x01 ? 4 : 3;
102
103 nut = (pc->
state64 >> (8 + 3)) & 0x1F;
104 // 7.4.2.4.3 and 7.4.2.4.4
112 return i - (code_len + 2);
113 }
115 int sh_picture_header_in_slice_header_flag = buf[
i] >> 7;
116
117 if (nut ==
VVC_PH_NUT || sh_picture_header_in_slice_header_flag) {
120 } else { // First slice of next frame found
122 return i - (code_len + 2);
123 }
124 }
125 }
126 }
128 }
129
131 {
132 int has_p = 0;
140 }
142 has_p = 1;
143 }
144 }
145 }
147 }
148
151 {
152 static const uint8_t h266_sub_width_c[] = {
153 1, 2, 2, 1
154 };
155 static const uint8_t h266_sub_height_c[] = {
156 1, 2, 1, 1
157 };
161
165
170
171 s->coded_width =
pps->pps_pic_width_in_luma_samples;
172 s->coded_height =
pps->pps_pic_height_in_luma_samples;
173 s->width =
pps->pps_pic_width_in_luma_samples -
174 (
pps->pps_conf_win_left_offset +
pps->pps_conf_win_right_offset) *
175 h266_sub_width_c[
sps->sps_chroma_format_idc];
176 s->height =
pps->pps_pic_height_in_luma_samples -
177 (
pps->pps_conf_win_top_offset +
pps->pps_conf_win_bottom_offset) *
178 h266_sub_height_c[
sps->sps_chroma_format_idc];
179
180 avctx->
profile =
sps->profile_tier_level.general_profile_idc;
181 avctx->
level =
sps->profile_tier_level.general_level_idc;
182
188
189 if (
sps->sps_ptl_dpb_hrd_params_present_flag &&
190 sps->sps_timing_hrd_params_present_flag) {
191 uint32_t num =
sps->sps_general_timing_hrd_parameters.num_units_in_tick;
192 uint32_t den =
sps->sps_general_timing_hrd_parameters.time_scale;
193
194 if (num != 0 && den != 0)
196 num, den, 1 << 30);
197 }
198 }
199
200 //8.3.1 Decoding process for picture order count.
201 //VTM did not follow the spec, and it's much simpler than spec.
202 //We follow the VTM.
207 {
208 int poc_msb, max_poc_lsb, poc_lsb;
210 max_poc_lsb = 1 << (
sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4);
211 poc_lsb =
ph->ph_pic_order_cnt_lsb;
213 if (
ph->ph_poc_msb_cycle_present_flag)
214 poc_msb =
ph->ph_poc_msb_cycle_val * max_poc_lsb;
215 else
216 poc_msb = 0;
217 } else {
219 int prev_poc_lsb = prev_poc & (max_poc_lsb - 1);
220 int prev_poc_msb = prev_poc - prev_poc_lsb;
221 if (
ph->ph_poc_msb_cycle_present_flag) {
222 poc_msb =
ph->ph_poc_msb_cycle_val * max_poc_lsb;
223 } else {
224 if ((poc_lsb < prev_poc_lsb) && ((prev_poc_lsb - poc_lsb) >=
225 (max_poc_lsb / 2)))
226 poc_msb = prev_poc_msb + (unsigned)max_poc_lsb;
227 else if ((poc_lsb > prev_poc_lsb) && ((poc_lsb - prev_poc_lsb) >
228 (max_poc_lsb / 2)))
229 poc_msb = prev_poc_msb - (unsigned)max_poc_lsb;
230 else
231 poc_msb = prev_poc_msb;
232 }
233 }
234
235 *poc = poc_msb + poc_lsb;
236 }
237
239 {
243 }
244
246 {
247 //7.4.2.4.3
254
256
258
266 }
268 }
269
272 {
275
279 if (!nal)
280 continue;
283 info->ph = &
ph->ph_picture_header;
286 if (
info->slice->header.sh_picture_header_in_slice_header_flag)
287 info->ph = &
info->slice->header.sh_picture_header;
290 "can't find picture header in picture unit.\n");
293 }
294 break;
295 }
296 }
301 }
302 info->pps = h266->
pps[
info->ph->ph_pic_parameter_set_id];
305 info->ph->ph_pic_parameter_set_id);
308 }
309 info->sps = h266->
sps[
info->pps->pps_seq_parameter_set_id];
312 info->pps->pps_seq_parameter_set_id);
315 }
317 return 0;
321 }
322
324 {
328 goto end;
330 end:
332 }
333
334 /**
335 * Parse NAL units of found picture and decode some basic information.
336 *
337 * @param s parser context.
338 * @param avctx codec context.
339 * @param buf buffer with field/frame data.
340 * @param buf_size size of the buffer.
341 * @return < 0 for error, == 0 for a complete au, > 0 is not a completed au.
342 */
345 {
348
352
353 if (!buf_size) {
356 return 0;
357 }
358 return 1;
359 }
360
361 if ((
ret = ff_cbs_read(
ctx->cbc, pu,
NULL, buf, buf_size)) < 0) {
363 goto end;
364 }
366 goto end;
369 goto end;
370 }
374 } else {
375 ret = 1;
//not a completed au
376 }
377 end:
378 ff_cbs_fragment_reset(pu);
380 }
381
382 /**
383 * Combine PU to AU
384 *
385 * @param s parser context.
386 * @param avctx codec context.
387 * @param buf buffer to a PU.
388 * @param buf_size size of the buffer.
389 * @return < 0 for error, == 0 a complete au, > 0 not a completed au.
390 */
392 const uint8_t **buf, int *buf_size)
393 {
396
397 ctx->cbc->log_ctx = avctx;
398
402 if (
ctx->last_au.size) {
403 *buf =
ctx->last_au.data;
404 *buf_size =
ctx->last_au.size;
405 } else {
407 }
408 }
411 }
412
414 const uint8_t **poutbuf, int *poutbuf_size,
415 const uint8_t *buf, int buf_size)
416 {
421
422 int is_dummy_buf = !buf_size;
423 int flush = !buf_size;
424 const uint8_t *dummy_buf = buf;
425
427 *poutbuf_size = 0;
428
430 ctx->parsed_extradata = 1;
431
432 ret = ff_cbs_read_extradata_from_codec(
ctx->cbc, pu, avctx);
435
436 ff_cbs_fragment_reset(pu);
437 }
438
440 next = buf_size;
441 } else {
444 return buf_size;
445 }
446
447 is_dummy_buf &= (dummy_buf == buf);
448
449 if (!is_dummy_buf) {
452 buf_size = 0;
454 }
456 return next;
457 }
458
459 *poutbuf = buf;
460 *poutbuf_size = buf_size;
461
462 return next;
463 }
464
479 };
480
482 {
485
490
493
495 }
496
498 {
500
503 ff_cbs_fragment_free(&
ctx->picture_unit);
504
505 ff_cbs_close(&
ctx->cbc);
507 }
508
515 };