2 * HEVC Parameter Set decoding
4 * Copyright (C) 2012 - 2103 Guillaume Martres
5 * Copyright (C) 2012 - 2103 Mickael Raulet
6 * Copyright (C) 2012 - 2013 Gildas Cocherel
7 * Copyright (C) 2013 Vittorio Giovara
9 * This file is part of FFmpeg.
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/imgutils.h"
28 #include "hevc_data.h"
31 static const uint8_t default_scaling_list_intra[] = {
32 16, 16, 16, 16, 17, 18, 21, 24,
33 16, 16, 16, 16, 17, 19, 22, 25,
34 16, 16, 17, 18, 20, 22, 25, 29,
35 16, 16, 18, 21, 24, 27, 31, 36,
36 17, 17, 20, 24, 30, 35, 41, 47,
37 18, 19, 22, 27, 35, 44, 54, 65,
38 21, 22, 25, 31, 41, 54, 70, 88,
39 24, 25, 29, 36, 47, 65, 88, 115
42 static const uint8_t default_scaling_list_inter[] = {
43 16, 16, 16, 16, 17, 18, 20, 24,
44 16, 16, 16, 17, 18, 20, 24, 25,
45 16, 16, 17, 18, 20, 24, 25, 28,
46 16, 17, 18, 20, 24, 25, 28, 33,
47 17, 18, 20, 24, 25, 28, 33, 41,
48 18, 20, 24, 25, 28, 33, 41, 54,
49 20, 24, 25, 28, 33, 41, 54, 71,
50 24, 25, 28, 33, 41, 54, 71, 91
53 static const AVRational vui_sar[] = {
73 static const uint8_t hevc_sub_width_c[] = {
77 static const uint8_t hevc_sub_height_c[] = {
81 static void remove_pps(HEVCParamSets *s, int id)
83 if (s->pps_list[id] && s->pps == (const HEVCPPS*)s->pps_list[id]->data)
85 av_buffer_unref(&s->pps_list[id]);
88 static void remove_sps(HEVCParamSets *s, int id)
91 if (s->sps_list[id]) {
92 if (s->sps == (const HEVCSPS*)s->sps_list[id]->data)
95 /* drop all PPS that depend on this SPS */
96 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
97 if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == id)
100 av_assert0(!(s->sps_list[id] && s->sps == (HEVCSPS*)s->sps_list[id]->data));
102 av_buffer_unref(&s->sps_list[id]);
105 static void remove_vps(HEVCParamSets *s, int id)
108 if (s->vps_list[id]) {
109 if (s->vps == (const HEVCVPS*)s->vps_list[id]->data)
112 for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
113 if (s->sps_list[i] && ((HEVCSPS*)s->sps_list[i]->data)->vps_id == id)
116 av_buffer_unref(&s->vps_list[id]);
119 int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
120 ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
122 uint8_t rps_predict = 0;
129 if (rps != sps->st_rps && sps->nb_st_rps)
130 rps_predict = get_bits1(gb);
133 const ShortTermRPS *rps_ridx;
135 unsigned abs_delta_rps;
136 uint8_t use_delta_flag = 0;
137 uint8_t delta_rps_sign;
139 if (is_slice_header) {
140 unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
141 if (delta_idx > sps->nb_st_rps) {
142 av_log(avctx, AV_LOG_ERROR,
143 "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
144 delta_idx, sps->nb_st_rps);
145 return AVERROR_INVALIDDATA;
147 rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
148 rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
150 rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
152 delta_rps_sign = get_bits1(gb);
153 abs_delta_rps = get_ue_golomb_long(gb) + 1;
154 if (abs_delta_rps < 1 || abs_delta_rps > 32768) {
155 av_log(avctx, AV_LOG_ERROR,
156 "Invalid value of abs_delta_rps: %d\n",
158 return AVERROR_INVALIDDATA;
160 delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
161 for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
162 int used = rps->used[k] = get_bits1(gb);
165 use_delta_flag = get_bits1(gb);
167 if (used || use_delta_flag) {
168 if (i < rps_ridx->num_delta_pocs)
169 delta_poc = delta_rps + rps_ridx->delta_poc[i];
171 delta_poc = delta_rps;
172 rps->delta_poc[k] = delta_poc;
181 if (k >= FF_ARRAY_ELEMS(rps->used)) {
182 av_log(avctx, AV_LOG_ERROR,
183 "Invalid num_delta_pocs: %d\n", k);
184 return AVERROR_INVALIDDATA;
187 rps->num_delta_pocs = k;
188 rps->num_negative_pics = k0;
189 // sort in increasing order (smallest first)
190 if (rps->num_delta_pocs != 0) {
192 for (i = 1; i < rps->num_delta_pocs; i++) {
193 delta_poc = rps->delta_poc[i];
195 for (k = i - 1; k >= 0; k--) {
196 tmp = rps->delta_poc[k];
197 if (delta_poc < tmp) {
198 rps->delta_poc[k + 1] = tmp;
199 rps->used[k + 1] = rps->used[k];
200 rps->delta_poc[k] = delta_poc;
206 if ((rps->num_negative_pics >> 1) != 0) {
208 k = rps->num_negative_pics - 1;
209 // flip the negative values to largest first
210 for (i = 0; i < rps->num_negative_pics >> 1; i++) {
211 delta_poc = rps->delta_poc[i];
213 rps->delta_poc[i] = rps->delta_poc[k];
214 rps->used[i] = rps->used[k];
215 rps->delta_poc[k] = delta_poc;
221 unsigned int prev, nb_positive_pics;
222 rps->num_negative_pics = get_ue_golomb_long(gb);
223 nb_positive_pics = get_ue_golomb_long(gb);
225 if (rps->num_negative_pics >= HEVC_MAX_REFS ||
226 nb_positive_pics >= HEVC_MAX_REFS) {
227 av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
228 return AVERROR_INVALIDDATA;
231 rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
232 if (rps->num_delta_pocs) {
234 for (i = 0; i < rps->num_negative_pics; i++) {
235 delta_poc = get_ue_golomb_long(gb) + 1;
236 if (delta_poc < 1 || delta_poc > 32768) {
237 av_log(avctx, AV_LOG_ERROR,
238 "Invalid value of delta_poc: %d\n",
240 return AVERROR_INVALIDDATA;
243 rps->delta_poc[i] = prev;
244 rps->used[i] = get_bits1(gb);
247 for (i = 0; i < nb_positive_pics; i++) {
248 delta_poc = get_ue_golomb_long(gb) + 1;
249 if (delta_poc < 1 || delta_poc > 32768) {
250 av_log(avctx, AV_LOG_ERROR,
251 "Invalid value of delta_poc: %d\n",
253 return AVERROR_INVALIDDATA;
256 rps->delta_poc[rps->num_negative_pics + i] = prev;
257 rps->used[rps->num_negative_pics + i] = get_bits1(gb);
265 static int decode_profile_tier_level(GetBitContext *gb, AVCodecContext *avctx,
270 if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 43 + 1)
273 ptl->profile_space = get_bits(gb, 2);
274 ptl->tier_flag = get_bits1(gb);
275 ptl->profile_idc = get_bits(gb, 5);
276 if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
277 av_log(avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
278 else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
279 av_log(avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
280 else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_STILL_PICTURE)
281 av_log(avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
282 else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT)
283 av_log(avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n");
285 av_log(avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
287 for (i = 0; i < 32; i++) {
288 ptl->profile_compatibility_flag[i] = get_bits1(gb);
290 if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
291 ptl->profile_idc = i;
293 ptl->progressive_source_flag = get_bits1(gb);
294 ptl->interlaced_source_flag = get_bits1(gb);
295 ptl->non_packed_constraint_flag = get_bits1(gb);
296 ptl->frame_only_constraint_flag = get_bits1(gb);
298 #define check_profile_idc(idc) \
299 ptl->profile_idc == idc || ptl->profile_compatibility_flag[idc]
301 if (check_profile_idc(4) || check_profile_idc(5) || check_profile_idc(6) ||
302 check_profile_idc(7) || check_profile_idc(8) || check_profile_idc(9) ||
303 check_profile_idc(10)) {
305 ptl->max_12bit_constraint_flag = get_bits1(gb);
306 ptl->max_10bit_constraint_flag = get_bits1(gb);
307 ptl->max_8bit_constraint_flag = get_bits1(gb);
308 ptl->max_422chroma_constraint_flag = get_bits1(gb);
309 ptl->max_420chroma_constraint_flag = get_bits1(gb);
310 ptl->max_monochrome_constraint_flag = get_bits1(gb);
311 ptl->intra_constraint_flag = get_bits1(gb);
312 ptl->one_picture_only_constraint_flag = get_bits1(gb);
313 ptl->lower_bit_rate_constraint_flag = get_bits1(gb);
315 if (check_profile_idc(5) || check_profile_idc(9) || check_profile_idc(10)) {
316 ptl->max_14bit_constraint_flag = get_bits1(gb);
317 skip_bits_long(gb, 33); // XXX_reserved_zero_33bits[0..32]
319 skip_bits_long(gb, 34); // XXX_reserved_zero_34bits[0..33]
321 } else if (check_profile_idc(2)) {
323 ptl->one_picture_only_constraint_flag = get_bits1(gb);
324 skip_bits_long(gb, 35); // XXX_reserved_zero_35bits[0..34]
326 skip_bits_long(gb, 43); // XXX_reserved_zero_43bits[0..42]
329 if (check_profile_idc(1) || check_profile_idc(2) || check_profile_idc(3) ||
330 check_profile_idc(4) || check_profile_idc(5) || check_profile_idc(9))
331 ptl->inbld_flag = get_bits1(gb);
334 #undef check_profile_idc
339 static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
340 PTL *ptl, int max_num_sub_layers)
343 if (decode_profile_tier_level(gb, avctx, &ptl->general_ptl) < 0 ||
344 get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
345 av_log(avctx, AV_LOG_ERROR, "PTL information too short\n");
349 ptl->general_ptl.level_idc = get_bits(gb, 8);
351 for (i = 0; i < max_num_sub_layers - 1; i++) {
352 ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
353 ptl->sub_layer_level_present_flag[i] = get_bits1(gb);
356 if (max_num_sub_layers - 1> 0)
357 for (i = max_num_sub_layers - 1; i < 8; i++)
358 skip_bits(gb, 2); // reserved_zero_2bits[i]
359 for (i = 0; i < max_num_sub_layers - 1; i++) {
360 if (ptl->sub_layer_profile_present_flag[i] &&
361 decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]) < 0) {
362 av_log(avctx, AV_LOG_ERROR,
363 "PTL information for sublayer %i too short\n", i);
366 if (ptl->sub_layer_level_present_flag[i]) {
367 if (get_bits_left(gb) < 8) {
368 av_log(avctx, AV_LOG_ERROR,
369 "Not enough data for sublayer %i level_idc\n", i);
372 ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
379 static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
380 int subpic_params_present)
384 for (i = 0; i < nb_cpb; i++) {
385 get_ue_golomb_long(gb); // bit_rate_value_minus1
386 get_ue_golomb_long(gb); // cpb_size_value_minus1
388 if (subpic_params_present) {
389 get_ue_golomb_long(gb); // cpb_size_du_value_minus1
390 get_ue_golomb_long(gb); // bit_rate_du_value_minus1
392 skip_bits1(gb); // cbr_flag
396 static int decode_hrd(GetBitContext *gb, int common_inf_present,
399 int nal_params_present = 0, vcl_params_present = 0;
400 int subpic_params_present = 0;
403 if (common_inf_present) {
404 nal_params_present = get_bits1(gb);
405 vcl_params_present = get_bits1(gb);
407 if (nal_params_present || vcl_params_present) {
408 subpic_params_present = get_bits1(gb);
410 if (subpic_params_present) {
411 skip_bits(gb, 8); // tick_divisor_minus2
412 skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
413 skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
414 skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
417 skip_bits(gb, 4); // bit_rate_scale
418 skip_bits(gb, 4); // cpb_size_scale
420 if (subpic_params_present)
421 skip_bits(gb, 4); // cpb_size_du_scale
423 skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
424 skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
425 skip_bits(gb, 5); // dpb_output_delay_length_minus1
429 for (i = 0; i < max_sublayers; i++) {
431 unsigned int nb_cpb = 1;
432 int fixed_rate = get_bits1(gb);
435 fixed_rate = get_bits1(gb);
438 get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
440 low_delay = get_bits1(gb);
443 nb_cpb = get_ue_golomb_long(gb) + 1;
444 if (nb_cpb < 1 || nb_cpb > 32) {
445 av_log(NULL, AV_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb);
446 return AVERROR_INVALIDDATA;
450 if (nal_params_present)
451 decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
452 if (vcl_params_present)
453 decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
458 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
465 AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
468 return AVERROR(ENOMEM);
469 vps = (HEVCVPS*)vps_buf->data;
471 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
473 nal_size = gb->buffer_end - gb->buffer;
474 if (nal_size > sizeof(vps->data)) {
475 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "
476 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
477 nal_size, sizeof(vps->data));
478 vps->data_size = sizeof(vps->data);
480 vps->data_size = nal_size;
482 memcpy(vps->data, gb->buffer, vps->data_size);
484 vps_id = get_bits(gb, 4);
486 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
487 av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
491 vps->vps_max_layers = get_bits(gb, 6) + 1;
492 vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
493 vps->vps_temporal_id_nesting_flag = get_bits1(gb);
495 if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
496 av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
500 if (vps->vps_max_sub_layers > HEVC_MAX_SUB_LAYERS) {
501 av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
502 vps->vps_max_sub_layers);
506 if (parse_ptl(gb, avctx, &vps->ptl, vps->vps_max_sub_layers) < 0)
509 vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
511 i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
512 for (; i < vps->vps_max_sub_layers; i++) {
513 vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
514 vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb);
515 vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1;
517 if (vps->vps_max_dec_pic_buffering[i] > HEVC_MAX_DPB_SIZE || !vps->vps_max_dec_pic_buffering[i]) {
518 av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
519 vps->vps_max_dec_pic_buffering[i] - 1);
522 if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
523 av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
524 vps->vps_num_reorder_pics[i]);
525 if (avctx->err_recognition & AV_EF_EXPLODE)
530 vps->vps_max_layer_id = get_bits(gb, 6);
531 vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
532 if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
533 (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
534 av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
538 for (i = 1; i < vps->vps_num_layer_sets; i++)
539 for (j = 0; j <= vps->vps_max_layer_id; j++)
540 skip_bits(gb, 1); // layer_id_included_flag[i][j]
542 vps->vps_timing_info_present_flag = get_bits1(gb);
543 if (vps->vps_timing_info_present_flag) {
544 vps->vps_num_units_in_tick = get_bits_long(gb, 32);
545 vps->vps_time_scale = get_bits_long(gb, 32);
546 vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
547 if (vps->vps_poc_proportional_to_timing_flag)
548 vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
549 vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
550 if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
551 av_log(avctx, AV_LOG_ERROR,
552 "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
555 for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
556 int common_inf_present = 1;
558 get_ue_golomb_long(gb); // hrd_layer_set_idx
560 common_inf_present = get_bits1(gb);
561 decode_hrd(gb, common_inf_present, vps->vps_max_sub_layers);
564 get_bits1(gb); /* vps_extension_flag */
566 if (get_bits_left(gb) < 0) {
567 av_log(avctx, AV_LOG_ERROR,
568 "Overread VPS by %d bits\n", -get_bits_left(gb));
569 if (ps->vps_list[vps_id])
573 if (ps->vps_list[vps_id] &&
574 !memcmp(ps->vps_list[vps_id]->data, vps_buf->data, vps_buf->size)) {
575 av_buffer_unref(&vps_buf);
577 remove_vps(ps, vps_id);
578 ps->vps_list[vps_id] = vps_buf;
584 av_buffer_unref(&vps_buf);
585 return AVERROR_INVALIDDATA;
588 static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
589 int apply_defdispwin, HEVCSPS *sps)
591 VUI backup_vui, *vui = &sps->vui;
592 GetBitContext backup;
593 int sar_present, alt = 0;
595 av_log(avctx, AV_LOG_DEBUG, "Decoding VUI\n");
597 sar_present = get_bits1(gb);
599 uint8_t sar_idx = get_bits(gb, 8);
600 if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
601 vui->sar = vui_sar[sar_idx];
602 else if (sar_idx == 255) {
603 vui->sar.num = get_bits(gb, 16);
604 vui->sar.den = get_bits(gb, 16);
606 av_log(avctx, AV_LOG_WARNING,
607 "Unknown SAR index: %u.\n", sar_idx);
610 vui->overscan_info_present_flag = get_bits1(gb);
611 if (vui->overscan_info_present_flag)
612 vui->overscan_appropriate_flag = get_bits1(gb);
614 vui->video_signal_type_present_flag = get_bits1(gb);
615 if (vui->video_signal_type_present_flag) {
616 vui->video_format = get_bits(gb, 3);
617 vui->video_full_range_flag = get_bits1(gb);
618 vui->colour_description_present_flag = get_bits1(gb);
619 if (vui->video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
620 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
621 if (vui->colour_description_present_flag) {
622 vui->colour_primaries = get_bits(gb, 8);
623 vui->transfer_characteristic = get_bits(gb, 8);
624 vui->matrix_coeffs = get_bits(gb, 8);
626 // Set invalid values to "unspecified"
627 if (!av_color_primaries_name(vui->colour_primaries))
628 vui->colour_primaries = AVCOL_PRI_UNSPECIFIED;
629 if (!av_color_transfer_name(vui->transfer_characteristic))
630 vui->transfer_characteristic = AVCOL_TRC_UNSPECIFIED;
631 if (!av_color_space_name(vui->matrix_coeffs))
632 vui->matrix_coeffs = AVCOL_SPC_UNSPECIFIED;
633 if (vui->matrix_coeffs == AVCOL_SPC_RGB) {
634 switch (sps->pix_fmt) {
635 case AV_PIX_FMT_YUV444P:
636 sps->pix_fmt = AV_PIX_FMT_GBRP;
638 case AV_PIX_FMT_YUV444P10:
639 sps->pix_fmt = AV_PIX_FMT_GBRP10;
641 case AV_PIX_FMT_YUV444P12:
642 sps->pix_fmt = AV_PIX_FMT_GBRP12;
649 vui->chroma_loc_info_present_flag = get_bits1(gb);
650 if (vui->chroma_loc_info_present_flag) {
651 vui->chroma_sample_loc_type_top_field = get_ue_golomb_long(gb);
652 vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_long(gb);
655 vui->neutra_chroma_indication_flag = get_bits1(gb);
656 vui->field_seq_flag = get_bits1(gb);
657 vui->frame_field_info_present_flag = get_bits1(gb);
659 // Backup context in case an alternate header is detected
660 memcpy(&backup, gb, sizeof(backup));
661 memcpy(&backup_vui, vui, sizeof(backup_vui));
662 if (get_bits_left(gb) >= 68 && show_bits_long(gb, 21) == 0x100000) {
663 vui->default_display_window_flag = 0;
664 av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
666 vui->default_display_window_flag = get_bits1(gb);
668 if (vui->default_display_window_flag) {
669 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
670 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
671 vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
672 vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
673 vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
674 vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
676 if (apply_defdispwin &&
677 avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
678 av_log(avctx, AV_LOG_DEBUG,
679 "discarding vui default display window, "
680 "original values are l:%u r:%u t:%u b:%u\n",
681 vui->def_disp_win.left_offset,
682 vui->def_disp_win.right_offset,
683 vui->def_disp_win.top_offset,
684 vui->def_disp_win.bottom_offset);
686 vui->def_disp_win.left_offset =
687 vui->def_disp_win.right_offset =
688 vui->def_disp_win.top_offset =
689 vui->def_disp_win.bottom_offset = 0;
694 vui->vui_timing_info_present_flag = get_bits1(gb);
696 if (vui->vui_timing_info_present_flag) {
697 if( get_bits_left(gb) < 66 && !alt) {
698 // The alternate syntax seem to have timing info located
699 // at where def_disp_win is normally located
700 av_log(avctx, AV_LOG_WARNING,
701 "Strange VUI timing information, retrying...\n");
702 memcpy(vui, &backup_vui, sizeof(backup_vui));
703 memcpy(gb, &backup, sizeof(backup));
707 vui->vui_num_units_in_tick = get_bits_long(gb, 32);
708 vui->vui_time_scale = get_bits_long(gb, 32);
710 av_log(avctx, AV_LOG_INFO, "Retry got %"PRIu32"/%"PRIu32" fps\n",
711 vui->vui_time_scale, vui->vui_num_units_in_tick);
713 vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
714 if (vui->vui_poc_proportional_to_timing_flag)
715 vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
716 vui->vui_hrd_parameters_present_flag = get_bits1(gb);
717 if (vui->vui_hrd_parameters_present_flag)
718 decode_hrd(gb, 1, sps->max_sub_layers);
721 vui->bitstream_restriction_flag = get_bits1(gb);
722 if (vui->bitstream_restriction_flag) {
723 if (get_bits_left(gb) < 8 && !alt) {
724 av_log(avctx, AV_LOG_WARNING,
725 "Strange VUI bitstream restriction information, retrying"
726 " from timing information...\n");
727 memcpy(vui, &backup_vui, sizeof(backup_vui));
728 memcpy(gb, &backup, sizeof(backup));
732 vui->tiles_fixed_structure_flag = get_bits1(gb);
733 vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
734 vui->restricted_ref_pic_lists_flag = get_bits1(gb);
735 vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb);
736 vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb);
737 vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb);
738 vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb);
739 vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb);
742 if (get_bits_left(gb) < 1 && !alt) {
743 // XXX: Alternate syntax when sps_range_extension_flag != 0?
744 av_log(avctx, AV_LOG_WARNING,
745 "Overread in VUI, retrying from timing information...\n");
746 memcpy(vui, &backup_vui, sizeof(backup_vui));
747 memcpy(gb, &backup, sizeof(backup));
753 static void set_default_scaling_list_data(ScalingList *sl)
757 for (matrixId = 0; matrixId < 6; matrixId++) {
759 memset(sl->sl[0][matrixId], 16, 16);
760 sl->sl_dc[0][matrixId] = 16; // default for 16x16
761 sl->sl_dc[1][matrixId] = 16; // default for 32x32
763 memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
764 memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
765 memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
766 memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
767 memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
768 memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
769 memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
770 memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
771 memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
772 memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
773 memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
774 memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
775 memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
776 memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
777 memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
778 memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
779 memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
780 memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
783 static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx, ScalingList *sl, HEVCSPS *sps)
785 uint8_t scaling_list_pred_mode_flag;
786 int32_t scaling_list_dc_coef[2][6];
787 int size_id, matrix_id, pos;
790 for (size_id = 0; size_id < 4; size_id++)
791 for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
792 scaling_list_pred_mode_flag = get_bits1(gb);
793 if (!scaling_list_pred_mode_flag) {
794 unsigned int delta = get_ue_golomb_long(gb);
795 /* Only need to handle non-zero delta. Zero means default,
796 * which should already be in the arrays. */
798 // Copy from previous array.
799 delta *= (size_id == 3) ? 3 : 1;
800 if (matrix_id < delta) {
801 av_log(avctx, AV_LOG_ERROR,
802 "Invalid delta in scaling list data: %d.\n", delta);
803 return AVERROR_INVALIDDATA;
806 memcpy(sl->sl[size_id][matrix_id],
807 sl->sl[size_id][matrix_id - delta],
808 size_id > 0 ? 64 : 16);
810 sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
813 int next_coef, coef_num;
814 int32_t scaling_list_delta_coef;
817 coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
819 scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
820 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
821 sl->sl_dc[size_id - 2][matrix_id] = next_coef;
823 for (i = 0; i < coef_num; i++) {
825 pos = 4 * ff_hevc_diag_scan4x4_y[i] +
826 ff_hevc_diag_scan4x4_x[i];
828 pos = 8 * ff_hevc_diag_scan8x8_y[i] +
829 ff_hevc_diag_scan8x8_x[i];
831 scaling_list_delta_coef = get_se_golomb(gb);
832 next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
833 sl->sl[size_id][matrix_id][pos] = next_coef;
838 if (sps->chroma_format_idc == 3) {
839 for (i = 0; i < 64; i++) {
840 sl->sl[3][1][i] = sl->sl[2][1][i];
841 sl->sl[3][2][i] = sl->sl[2][2][i];
842 sl->sl[3][4][i] = sl->sl[2][4][i];
843 sl->sl[3][5][i] = sl->sl[2][5][i];
845 sl->sl_dc[1][1] = sl->sl_dc[0][1];
846 sl->sl_dc[1][2] = sl->sl_dc[0][2];
847 sl->sl_dc[1][4] = sl->sl_dc[0][4];
848 sl->sl_dc[1][5] = sl->sl_dc[0][5];
855 static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
857 const AVPixFmtDescriptor *desc;
858 switch (sps->bit_depth) {
860 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
861 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
862 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
863 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
866 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY9;
867 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
868 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
869 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
872 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
873 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
874 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
875 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
878 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
879 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
880 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
881 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
884 av_log(avctx, AV_LOG_ERROR,
885 "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
886 "chroma_format_idc is %d, depth is %d\n",
887 sps->chroma_format_idc, sps->bit_depth);
888 return AVERROR_INVALIDDATA;
891 desc = av_pix_fmt_desc_get(sps->pix_fmt);
893 return AVERROR(EINVAL);
895 sps->hshift[0] = sps->vshift[0] = 0;
896 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
897 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
899 sps->pixel_shift = sps->bit_depth > 8;
904 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
905 int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
909 int log2_diff_max_min_transform_block_size;
910 int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
915 sps->vps_id = get_bits(gb, 4);
917 if (vps_list && !vps_list[sps->vps_id]) {
918 av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
920 return AVERROR_INVALIDDATA;
923 sps->max_sub_layers = get_bits(gb, 3) + 1;
924 if (sps->max_sub_layers > HEVC_MAX_SUB_LAYERS) {
925 av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
926 sps->max_sub_layers);
927 return AVERROR_INVALIDDATA;
930 sps->temporal_id_nesting_flag = get_bits(gb, 1);
932 if ((ret = parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers)) < 0)
935 *sps_id = get_ue_golomb_long(gb);
936 if (*sps_id >= HEVC_MAX_SPS_COUNT) {
937 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
938 return AVERROR_INVALIDDATA;
941 sps->chroma_format_idc = get_ue_golomb_long(gb);
942 if (sps->chroma_format_idc > 3U) {
943 av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
944 return AVERROR_INVALIDDATA;
947 if (sps->chroma_format_idc == 3)
948 sps->separate_colour_plane_flag = get_bits1(gb);
950 if (sps->separate_colour_plane_flag)
951 sps->chroma_format_idc = 0;
953 sps->width = get_ue_golomb_long(gb);
954 sps->height = get_ue_golomb_long(gb);
955 if ((ret = av_image_check_size(sps->width,
956 sps->height, 0, avctx)) < 0)
959 if (get_bits1(gb)) { // pic_conformance_flag
960 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
961 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
962 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
963 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
964 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
965 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
967 if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
968 av_log(avctx, AV_LOG_DEBUG,
969 "discarding sps conformance window, "
970 "original values are l:%u r:%u t:%u b:%u\n",
971 sps->pic_conf_win.left_offset,
972 sps->pic_conf_win.right_offset,
973 sps->pic_conf_win.top_offset,
974 sps->pic_conf_win.bottom_offset);
976 sps->pic_conf_win.left_offset =
977 sps->pic_conf_win.right_offset =
978 sps->pic_conf_win.top_offset =
979 sps->pic_conf_win.bottom_offset = 0;
981 sps->output_window = sps->pic_conf_win;
984 sps->bit_depth = get_ue_golomb_long(gb) + 8;
985 bit_depth_chroma = get_ue_golomb_long(gb) + 8;
986 if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
987 av_log(avctx, AV_LOG_ERROR,
988 "Luma bit depth (%d) is different from chroma bit depth (%d), "
989 "this is unsupported.\n",
990 sps->bit_depth, bit_depth_chroma);
991 return AVERROR_INVALIDDATA;
993 sps->bit_depth_chroma = bit_depth_chroma;
995 ret = map_pixel_format(avctx, sps);
999 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
1000 if (sps->log2_max_poc_lsb > 16) {
1001 av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
1002 sps->log2_max_poc_lsb - 4);
1003 return AVERROR_INVALIDDATA;
1006 sublayer_ordering_info = get_bits1(gb);
1007 start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
1008 for (i = start; i < sps->max_sub_layers; i++) {
1009 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
1010 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
1011 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
1012 if (sps->temporal_layer[i].max_dec_pic_buffering > (unsigned)HEVC_MAX_DPB_SIZE) {
1013 av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
1014 sps->temporal_layer[i].max_dec_pic_buffering - 1U);
1015 return AVERROR_INVALIDDATA;
1017 if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
1018 av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
1019 sps->temporal_layer[i].num_reorder_pics);
1020 if (avctx->err_recognition & AV_EF_EXPLODE ||
1021 sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
1022 return AVERROR_INVALIDDATA;
1024 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
1028 if (!sublayer_ordering_info) {
1029 for (i = 0; i < start; i++) {
1030 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
1031 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
1032 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
1036 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
1037 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
1038 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
1039 log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
1040 sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
1041 sps->log2_min_tb_size;
1043 if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
1044 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
1045 return AVERROR_INVALIDDATA;
1048 if (sps->log2_diff_max_min_coding_block_size > 30) {
1049 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_coding_block_size", sps->log2_diff_max_min_coding_block_size);
1050 return AVERROR_INVALIDDATA;
1053 if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
1054 av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
1055 return AVERROR_INVALIDDATA;
1058 if (log2_diff_max_min_transform_block_size < 0 || log2_diff_max_min_transform_block_size > 30) {
1059 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size", log2_diff_max_min_transform_block_size);
1060 return AVERROR_INVALIDDATA;
1063 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
1064 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
1066 sps->scaling_list_enable_flag = get_bits1(gb);
1067 if (sps->scaling_list_enable_flag) {
1068 set_default_scaling_list_data(&sps->scaling_list);
1070 if (get_bits1(gb)) {
1071 ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
1077 sps->amp_enabled_flag = get_bits1(gb);
1078 sps->sao_enabled = get_bits1(gb);
1080 sps->pcm_enabled_flag = get_bits1(gb);
1081 if (sps->pcm_enabled_flag) {
1082 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
1083 sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
1084 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
1085 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
1086 get_ue_golomb_long(gb);
1087 if (FFMAX(sps->pcm.bit_depth, sps->pcm.bit_depth_chroma) > sps->bit_depth) {
1088 av_log(avctx, AV_LOG_ERROR,
1089 "PCM bit depth (%d, %d) is greater than normal bit depth (%d)\n",
1090 sps->pcm.bit_depth, sps->pcm.bit_depth_chroma, sps->bit_depth);
1091 return AVERROR_INVALIDDATA;
1094 sps->pcm.loop_filter_disable_flag = get_bits1(gb);
1097 sps->nb_st_rps = get_ue_golomb_long(gb);
1098 if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) {
1099 av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
1101 return AVERROR_INVALIDDATA;
1103 for (i = 0; i < sps->nb_st_rps; i++) {
1104 if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
1109 sps->long_term_ref_pics_present_flag = get_bits1(gb);
1110 if (sps->long_term_ref_pics_present_flag) {
1111 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
1112 if (sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PICS) {
1113 av_log(avctx, AV_LOG_ERROR, "Too many long term ref pics: %d.\n",
1114 sps->num_long_term_ref_pics_sps);
1115 return AVERROR_INVALIDDATA;
1117 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
1118 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
1119 sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
1123 sps->sps_temporal_mvp_enabled_flag = get_bits1(gb);
1124 sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
1125 sps->vui.sar = (AVRational){0, 1};
1126 vui_present = get_bits1(gb);
1128 decode_vui(gb, avctx, apply_defdispwin, sps);
1130 if (get_bits1(gb)) { // sps_extension_flag
1131 sps->sps_range_extension_flag = get_bits1(gb);
1132 skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7);
1133 if (sps->sps_range_extension_flag) {
1134 sps->transform_skip_rotation_enabled_flag = get_bits1(gb);
1135 sps->transform_skip_context_enabled_flag = get_bits1(gb);
1136 sps->implicit_rdpcm_enabled_flag = get_bits1(gb);
1138 sps->explicit_rdpcm_enabled_flag = get_bits1(gb);
1140 sps->extended_precision_processing_flag = get_bits1(gb);
1141 if (sps->extended_precision_processing_flag)
1142 av_log(avctx, AV_LOG_WARNING,
1143 "extended_precision_processing_flag not yet implemented\n");
1145 sps->intra_smoothing_disabled_flag = get_bits1(gb);
1146 sps->high_precision_offsets_enabled_flag = get_bits1(gb);
1147 if (sps->high_precision_offsets_enabled_flag)
1148 av_log(avctx, AV_LOG_WARNING,
1149 "high_precision_offsets_enabled_flag not yet implemented\n");
1151 sps->persistent_rice_adaptation_enabled_flag = get_bits1(gb);
1153 sps->cabac_bypass_alignment_enabled_flag = get_bits1(gb);
1154 if (sps->cabac_bypass_alignment_enabled_flag)
1155 av_log(avctx, AV_LOG_WARNING,
1156 "cabac_bypass_alignment_enabled_flag not yet implemented\n");
1159 if (apply_defdispwin) {
1160 sps->output_window.left_offset += sps->vui.def_disp_win.left_offset;
1161 sps->output_window.right_offset += sps->vui.def_disp_win.right_offset;
1162 sps->output_window.top_offset += sps->vui.def_disp_win.top_offset;
1163 sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
1166 ow = &sps->output_window;
1167 if (ow->left_offset >= INT_MAX - ow->right_offset ||
1168 ow->top_offset >= INT_MAX - ow->bottom_offset ||
1169 ow->left_offset + ow->right_offset >= sps->width ||
1170 ow->top_offset + ow->bottom_offset >= sps->height) {
1171 av_log(avctx, AV_LOG_WARNING, "Invalid cropping offsets: %u/%u/%u/%u\n",
1172 ow->left_offset, ow->right_offset, ow->top_offset, ow->bottom_offset);
1173 if (avctx->err_recognition & AV_EF_EXPLODE) {
1174 return AVERROR_INVALIDDATA;
1176 av_log(avctx, AV_LOG_WARNING,
1177 "Displaying the whole video surface.\n");
1178 memset(ow, 0, sizeof(*ow));
1179 memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
1182 // Inferred parameters
1183 sps->log2_ctb_size = sps->log2_min_cb_size +
1184 sps->log2_diff_max_min_coding_block_size;
1185 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1187 if (sps->log2_ctb_size > HEVC_MAX_LOG2_CTB_SIZE) {
1188 av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1189 return AVERROR_INVALIDDATA;
1191 if (sps->log2_ctb_size < 4) {
1194 "log2_ctb_size %d differs from the bounds of any known profile\n",
1195 sps->log2_ctb_size);
1196 avpriv_request_sample(avctx, "log2_ctb_size %d", sps->log2_ctb_size);
1197 return AVERROR_INVALIDDATA;
1200 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1201 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1202 sps->ctb_size = sps->ctb_width * sps->ctb_height;
1204 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
1205 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
1206 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
1207 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
1208 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
1209 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1210 sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1212 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1214 if (av_mod_uintp2(sps->width, sps->log2_min_cb_size) ||
1215 av_mod_uintp2(sps->height, sps->log2_min_cb_size)) {
1216 av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
1217 return AVERROR_INVALIDDATA;
1220 if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
1221 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1222 sps->max_transform_hierarchy_depth_inter);
1223 return AVERROR_INVALIDDATA;
1225 if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
1226 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1227 sps->max_transform_hierarchy_depth_intra);
1228 return AVERROR_INVALIDDATA;
1230 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1231 av_log(avctx, AV_LOG_ERROR,
1232 "max transform block size out of range: %d\n",
1233 sps->log2_max_trafo_size);
1234 return AVERROR_INVALIDDATA;
1237 if (get_bits_left(gb) < 0) {
1238 av_log(avctx, AV_LOG_ERROR,
1239 "Overread SPS by %d bits\n", -get_bits_left(gb));
1240 return AVERROR_INVALIDDATA;
1246 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
1247 HEVCParamSets *ps, int apply_defdispwin)
1250 AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
1251 unsigned int sps_id;
1256 return AVERROR(ENOMEM);
1257 sps = (HEVCSPS*)sps_buf->data;
1259 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1261 nal_size = gb->buffer_end - gb->buffer;
1262 if (nal_size > sizeof(sps->data)) {
1263 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized SPS "
1264 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1265 nal_size, sizeof(sps->data));
1266 sps->data_size = sizeof(sps->data);
1268 sps->data_size = nal_size;
1270 memcpy(sps->data, gb->buffer, sps->data_size);
1272 ret = ff_hevc_parse_sps(sps, gb, &sps_id,
1274 ps->vps_list, avctx);
1276 av_buffer_unref(&sps_buf);
1280 if (avctx->debug & FF_DEBUG_BITSTREAM) {
1281 av_log(avctx, AV_LOG_DEBUG,
1282 "Parsed SPS: id %d; coded wxh: %dx%d; "
1283 "cropped wxh: %dx%d; pix_fmt: %s.\n",
1284 sps_id, sps->width, sps->height,
1285 sps->width - (sps->output_window.left_offset + sps->output_window.right_offset),
1286 sps->height - (sps->output_window.top_offset + sps->output_window.bottom_offset),
1287 av_get_pix_fmt_name(sps->pix_fmt));
1290 /* check if this is a repeat of an already parsed SPS, then keep the
1292 * otherwise drop all PPSes that depend on it */
1293 if (ps->sps_list[sps_id] &&
1294 !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
1295 av_buffer_unref(&sps_buf);
1297 remove_sps(ps, sps_id);
1298 ps->sps_list[sps_id] = sps_buf;
1304 static void hevc_pps_free(void *opaque, uint8_t *data)
1306 HEVCPPS *pps = (HEVCPPS*)data;
1308 av_freep(&pps->column_width);
1309 av_freep(&pps->row_height);
1310 av_freep(&pps->col_bd);
1311 av_freep(&pps->row_bd);
1312 av_freep(&pps->col_idxX);
1313 av_freep(&pps->ctb_addr_rs_to_ts);
1314 av_freep(&pps->ctb_addr_ts_to_rs);
1315 av_freep(&pps->tile_pos_rs);
1316 av_freep(&pps->tile_id);
1317 av_freep(&pps->min_tb_addr_zs_tab);
1322 static int pps_range_extensions(GetBitContext *gb, AVCodecContext *avctx,
1323 HEVCPPS *pps, HEVCSPS *sps) {
1326 if (pps->transform_skip_enabled_flag) {
1327 pps->log2_max_transform_skip_block_size = get_ue_golomb_long(gb) + 2;
1329 pps->cross_component_prediction_enabled_flag = get_bits1(gb);
1330 pps->chroma_qp_offset_list_enabled_flag = get_bits1(gb);
1331 if (pps->chroma_qp_offset_list_enabled_flag) {
1332 pps->diff_cu_chroma_qp_offset_depth = get_ue_golomb_long(gb);
1333 pps->chroma_qp_offset_list_len_minus1 = get_ue_golomb_long(gb);
1334 if (pps->chroma_qp_offset_list_len_minus1 > 5) {
1335 av_log(avctx, AV_LOG_ERROR,
1336 "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
1337 return AVERROR_INVALIDDATA;
1339 for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
1340 pps->cb_qp_offset_list[i] = get_se_golomb_long(gb);
1341 if (pps->cb_qp_offset_list[i]) {
1342 av_log(avctx, AV_LOG_WARNING,
1343 "cb_qp_offset_list not tested yet.\n");
1345 pps->cr_qp_offset_list[i] = get_se_golomb_long(gb);
1346 if (pps->cr_qp_offset_list[i]) {
1347 av_log(avctx, AV_LOG_WARNING,
1348 "cb_qp_offset_list not tested yet.\n");
1352 pps->log2_sao_offset_scale_luma = get_ue_golomb_long(gb);
1353 pps->log2_sao_offset_scale_chroma = get_ue_golomb_long(gb);
1355 if ( pps->log2_sao_offset_scale_luma > FFMAX(sps->bit_depth - 10, 0)
1356 || pps->log2_sao_offset_scale_chroma > FFMAX(sps->bit_depth_chroma - 10, 0)
1358 return AVERROR_INVALIDDATA;
1363 static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
1364 HEVCPPS *pps, HEVCSPS *sps)
1367 int pic_area_in_ctbs;
1368 int i, j, x, y, ctb_addr_rs, tile_id;
1370 // Inferred parameters
1371 pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1372 pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1373 pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1374 if (!pps->col_bd || !pps->row_bd || !pps->col_idxX)
1375 return AVERROR(ENOMEM);
1377 if (pps->uniform_spacing_flag) {
1378 if (!pps->column_width) {
1379 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1380 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1382 if (!pps->column_width || !pps->row_height)
1383 return AVERROR(ENOMEM);
1385 for (i = 0; i < pps->num_tile_columns; i++) {
1386 pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1387 (i * sps->ctb_width) / pps->num_tile_columns;
1390 for (i = 0; i < pps->num_tile_rows; i++) {
1391 pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1392 (i * sps->ctb_height) / pps->num_tile_rows;
1397 for (i = 0; i < pps->num_tile_columns; i++)
1398 pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1401 for (i = 0; i < pps->num_tile_rows; i++)
1402 pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1404 for (i = 0, j = 0; i < sps->ctb_width; i++) {
1405 if (i > pps->col_bd[j])
1407 pps->col_idxX[i] = j;
1413 pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1415 pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1416 pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1417 pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1418 pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
1419 if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1420 !pps->tile_id || !pps->min_tb_addr_zs_tab) {
1421 return AVERROR(ENOMEM);
1424 for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1425 int tb_x = ctb_addr_rs % sps->ctb_width;
1426 int tb_y = ctb_addr_rs / sps->ctb_width;
1431 for (i = 0; i < pps->num_tile_columns; i++) {
1432 if (tb_x < pps->col_bd[i + 1]) {
1438 for (i = 0; i < pps->num_tile_rows; i++) {
1439 if (tb_y < pps->row_bd[i + 1]) {
1445 for (i = 0; i < tile_x; i++)
1446 val += pps->row_height[tile_y] * pps->column_width[i];
1447 for (i = 0; i < tile_y; i++)
1448 val += sps->ctb_width * pps->row_height[i];
1450 val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1451 tb_x - pps->col_bd[tile_x];
1453 pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1454 pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1457 for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1458 for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1459 for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1460 for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1461 pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1463 pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1464 if (!pps->tile_pos_rs)
1465 return AVERROR(ENOMEM);
1467 for (j = 0; j < pps->num_tile_rows; j++)
1468 for (i = 0; i < pps->num_tile_columns; i++)
1469 pps->tile_pos_rs[j * pps->num_tile_columns + i] =
1470 pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1472 log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
1473 pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
1474 for (y = 0; y < sps->tb_mask+2; y++) {
1475 pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
1476 pps->min_tb_addr_zs_tab[y] = -1;
1478 for (y = 0; y < sps->tb_mask+1; y++) {
1479 for (x = 0; x < sps->tb_mask+1; x++) {
1480 int tb_x = x >> log2_diff;
1481 int tb_y = y >> log2_diff;
1482 int rs = sps->ctb_width * tb_y + tb_x;
1483 int val = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
1484 for (i = 0; i < log2_diff; i++) {
1486 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1488 pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
1495 int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
1498 HEVCSPS *sps = NULL;
1500 unsigned int pps_id = 0;
1502 unsigned log2_parallel_merge_level_minus2;
1504 AVBufferRef *pps_buf;
1505 HEVCPPS *pps = av_mallocz(sizeof(*pps));
1508 return AVERROR(ENOMEM);
1510 pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
1511 hevc_pps_free, NULL, 0);
1514 return AVERROR(ENOMEM);
1517 av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1519 nal_size = gb->buffer_end - gb->buffer;
1520 if (nal_size > sizeof(pps->data)) {
1521 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "
1522 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1523 nal_size, sizeof(pps->data));
1524 pps->data_size = sizeof(pps->data);
1526 pps->data_size = nal_size;
1528 memcpy(pps->data, gb->buffer, pps->data_size);
1531 pps->loop_filter_across_tiles_enabled_flag = 1;
1532 pps->num_tile_columns = 1;
1533 pps->num_tile_rows = 1;
1534 pps->uniform_spacing_flag = 1;
1535 pps->disable_dbf = 0;
1536 pps->beta_offset = 0;
1538 pps->log2_max_transform_skip_block_size = 2;
1541 pps_id = get_ue_golomb_long(gb);
1542 if (pps_id >= HEVC_MAX_PPS_COUNT) {
1543 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1544 ret = AVERROR_INVALIDDATA;
1547 pps->sps_id = get_ue_golomb_long(gb);
1548 if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
1549 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1550 ret = AVERROR_INVALIDDATA;
1553 if (!ps->sps_list[pps->sps_id]) {
1554 av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
1555 ret = AVERROR_INVALIDDATA;
1558 sps = (HEVCSPS *)ps->sps_list[pps->sps_id]->data;
1560 pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
1561 pps->output_flag_present_flag = get_bits1(gb);
1562 pps->num_extra_slice_header_bits = get_bits(gb, 3);
1564 pps->sign_data_hiding_flag = get_bits1(gb);
1566 pps->cabac_init_present_flag = get_bits1(gb);
1568 pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1;
1569 pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1;
1571 pps->pic_init_qp_minus26 = get_se_golomb(gb);
1573 pps->constrained_intra_pred_flag = get_bits1(gb);
1574 pps->transform_skip_enabled_flag = get_bits1(gb);
1576 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
1577 pps->diff_cu_qp_delta_depth = 0;
1578 if (pps->cu_qp_delta_enabled_flag)
1579 pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
1581 if (pps->diff_cu_qp_delta_depth < 0 ||
1582 pps->diff_cu_qp_delta_depth > sps->log2_diff_max_min_coding_block_size) {
1583 av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
1584 pps->diff_cu_qp_delta_depth);
1585 ret = AVERROR_INVALIDDATA;
1589 pps->cb_qp_offset = get_se_golomb(gb);
1590 if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1591 av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1593 ret = AVERROR_INVALIDDATA;
1596 pps->cr_qp_offset = get_se_golomb(gb);
1597 if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1598 av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1600 ret = AVERROR_INVALIDDATA;
1603 pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
1605 pps->weighted_pred_flag = get_bits1(gb);
1606 pps->weighted_bipred_flag = get_bits1(gb);
1608 pps->transquant_bypass_enable_flag = get_bits1(gb);
1609 pps->tiles_enabled_flag = get_bits1(gb);
1610 pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
1612 if (pps->tiles_enabled_flag) {
1613 int num_tile_columns_minus1 = get_ue_golomb(gb);
1614 int num_tile_rows_minus1 = get_ue_golomb(gb);
1616 if (num_tile_columns_minus1 < 0 ||
1617 num_tile_columns_minus1 >= sps->ctb_width) {
1618 av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1619 num_tile_columns_minus1);
1620 ret = num_tile_columns_minus1 < 0 ? num_tile_columns_minus1 : AVERROR_INVALIDDATA;
1623 if (num_tile_rows_minus1 < 0 ||
1624 num_tile_rows_minus1 >= sps->ctb_height) {
1625 av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1626 num_tile_rows_minus1);
1627 ret = num_tile_rows_minus1 < 0 ? num_tile_rows_minus1 : AVERROR_INVALIDDATA;
1630 pps->num_tile_columns = num_tile_columns_minus1 + 1;
1631 pps->num_tile_rows = num_tile_rows_minus1 + 1;
1633 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1634 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1635 if (!pps->column_width || !pps->row_height) {
1636 ret = AVERROR(ENOMEM);
1640 pps->uniform_spacing_flag = get_bits1(gb);
1641 if (!pps->uniform_spacing_flag) {
1643 for (i = 0; i < pps->num_tile_columns - 1; i++) {
1644 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1645 sum += pps->column_width[i];
1647 if (sum >= sps->ctb_width) {
1648 av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1649 ret = AVERROR_INVALIDDATA;
1652 pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1655 for (i = 0; i < pps->num_tile_rows - 1; i++) {
1656 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1657 sum += pps->row_height[i];
1659 if (sum >= sps->ctb_height) {
1660 av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1661 ret = AVERROR_INVALIDDATA;
1664 pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1666 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
1669 pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1671 pps->deblocking_filter_control_present_flag = get_bits1(gb);
1672 if (pps->deblocking_filter_control_present_flag) {
1673 pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
1674 pps->disable_dbf = get_bits1(gb);
1675 if (!pps->disable_dbf) {
1676 int beta_offset_div2 = get_se_golomb(gb);
1677 int tc_offset_div2 = get_se_golomb(gb) ;
1678 if (beta_offset_div2 < -6 || beta_offset_div2 > 6) {
1679 av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1681 ret = AVERROR_INVALIDDATA;
1684 if (tc_offset_div2 < -6 || tc_offset_div2 > 6) {
1685 av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1687 ret = AVERROR_INVALIDDATA;
1690 pps->beta_offset = 2 * beta_offset_div2;
1691 pps->tc_offset = 2 * tc_offset_div2;
1695 pps->scaling_list_data_present_flag = get_bits1(gb);
1696 if (pps->scaling_list_data_present_flag) {
1697 set_default_scaling_list_data(&pps->scaling_list);
1698 ret = scaling_list_data(gb, avctx, &pps->scaling_list, sps);
1702 pps->lists_modification_present_flag = get_bits1(gb);
1703 log2_parallel_merge_level_minus2 = get_ue_golomb_long(gb);
1704 if (log2_parallel_merge_level_minus2 > sps->log2_ctb_size) {
1705 av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1706 log2_parallel_merge_level_minus2);
1707 ret = AVERROR_INVALIDDATA;
1710 pps->log2_parallel_merge_level = log2_parallel_merge_level_minus2 + 2;
1712 pps->slice_header_extension_present_flag = get_bits1(gb);
1714 if (get_bits1(gb)) { // pps_extension_present_flag
1715 pps->pps_range_extensions_flag = get_bits1(gb);
1716 skip_bits(gb, 7); // pps_extension_7bits
1717 if (sps->ptl.general_ptl.profile_idc == FF_PROFILE_HEVC_REXT && pps->pps_range_extensions_flag) {
1718 if ((ret = pps_range_extensions(gb, avctx, pps, sps)) < 0)
1723 ret = setup_pps(avctx, gb, pps, sps);
1727 if (get_bits_left(gb) < 0) {
1728 av_log(avctx, AV_LOG_ERROR,
1729 "Overread PPS by %d bits\n", -get_bits_left(gb));
1733 remove_pps(ps, pps_id);
1734 ps->pps_list[pps_id] = pps_buf;
1739 av_buffer_unref(&pps_buf);
1743 void ff_hevc_ps_uninit(HEVCParamSets *ps)
1747 for (i = 0; i < FF_ARRAY_ELEMS(ps->vps_list); i++)
1748 av_buffer_unref(&ps->vps_list[i]);
1749 for (i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
1750 av_buffer_unref(&ps->sps_list[i]);
1751 for (i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
1752 av_buffer_unref(&ps->pps_list[i]);
1759 int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
1761 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
1762 int prev_poc_lsb = pocTid0 % max_poc_lsb;
1763 int prev_poc_msb = pocTid0 - prev_poc_lsb;
1766 if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
1767 poc_msb = prev_poc_msb + max_poc_lsb;
1768 else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
1769 poc_msb = prev_poc_msb - max_poc_lsb;
1771 poc_msb = prev_poc_msb;
1773 // For BLA picture types, POCmsb is set to 0.
1774 if (nal_unit_type == HEVC_NAL_BLA_W_LP ||
1775 nal_unit_type == HEVC_NAL_BLA_W_RADL ||
1776 nal_unit_type == HEVC_NAL_BLA_N_LP)
1779 return poc_msb + poc_lsb;