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"
30 static const uint8_t default_scaling_list_intra[] = {
31 16, 16, 16, 16, 17, 18, 21, 24,
32 16, 16, 16, 16, 17, 19, 22, 25,
33 16, 16, 17, 18, 20, 22, 25, 29,
34 16, 16, 18, 21, 24, 27, 31, 36,
35 17, 17, 20, 24, 30, 35, 41, 47,
36 18, 19, 22, 27, 35, 44, 54, 65,
37 21, 22, 25, 31, 41, 54, 70, 88,
38 24, 25, 29, 36, 47, 65, 88, 115
41 static const uint8_t default_scaling_list_inter[] = {
42 16, 16, 16, 16, 17, 18, 20, 24,
43 16, 16, 16, 17, 18, 20, 24, 25,
44 16, 16, 17, 18, 20, 24, 25, 28,
45 16, 17, 18, 20, 24, 25, 28, 33,
46 17, 18, 20, 24, 25, 28, 33, 41,
47 18, 20, 24, 25, 28, 33, 41, 54,
48 20, 24, 25, 28, 33, 41, 54, 71,
49 24, 25, 28, 33, 41, 54, 71, 91
52 static const AVRational vui_sar[] = {
72 int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
73 const HEVCSPS *sps, int is_slice_header)
75 HEVCLocalContext *lc = s->HEVClc;
76 uint8_t rps_predict = 0;
83 GetBitContext *gb = &lc->gb;
85 if (rps != sps->st_rps && sps->nb_st_rps)
86 rps_predict = get_bits1(gb);
89 const ShortTermRPS *rps_ridx;
90 int delta_rps, abs_delta_rps;
91 uint8_t use_delta_flag = 0;
92 uint8_t delta_rps_sign;
94 if (is_slice_header) {
95 int delta_idx = get_ue_golomb_long(gb) + 1;
96 if (delta_idx > sps->nb_st_rps) {
97 av_log(s->avctx, AV_LOG_ERROR,
98 "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
99 delta_idx, sps->nb_st_rps);
100 return AVERROR_INVALIDDATA;
102 rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
104 rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
106 delta_rps_sign = get_bits1(gb);
107 abs_delta_rps = get_ue_golomb_long(gb) + 1;
108 delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
109 for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
110 int used = rps->used[k] = get_bits1(gb);
113 use_delta_flag = get_bits1(gb);
115 if (used || use_delta_flag) {
116 if (i < rps_ridx->num_delta_pocs)
117 delta_poc = delta_rps + rps_ridx->delta_poc[i];
119 delta_poc = delta_rps;
120 rps->delta_poc[k] = delta_poc;
129 rps->num_delta_pocs = k;
130 rps->num_negative_pics = k0;
131 // sort in increasing order (smallest first)
132 if (rps->num_delta_pocs != 0) {
134 for (i = 1; i < rps->num_delta_pocs; i++) {
135 delta_poc = rps->delta_poc[i];
137 for (k = i - 1; k >= 0; k--) {
138 tmp = rps->delta_poc[k];
139 if (delta_poc < tmp) {
140 rps->delta_poc[k + 1] = tmp;
141 rps->used[k + 1] = rps->used[k];
142 rps->delta_poc[k] = delta_poc;
148 if ((rps->num_negative_pics >> 1) != 0) {
150 k = rps->num_negative_pics - 1;
151 // flip the negative values to largest first
152 for (i = 0; i < rps->num_negative_pics >> 1; i++) {
153 delta_poc = rps->delta_poc[i];
155 rps->delta_poc[i] = rps->delta_poc[k];
156 rps->used[i] = rps->used[k];
157 rps->delta_poc[k] = delta_poc;
163 unsigned int prev, nb_positive_pics;
164 rps->num_negative_pics = get_ue_golomb_long(gb);
165 nb_positive_pics = get_ue_golomb_long(gb);
167 if (rps->num_negative_pics >= MAX_REFS ||
168 nb_positive_pics >= MAX_REFS) {
169 av_log(s->avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
170 return AVERROR_INVALIDDATA;
173 rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
174 if (rps->num_delta_pocs) {
176 for (i = 0; i < rps->num_negative_pics; i++) {
177 delta_poc = get_ue_golomb_long(gb) + 1;
179 rps->delta_poc[i] = prev;
180 rps->used[i] = get_bits1(gb);
183 for (i = 0; i < nb_positive_pics; i++) {
184 delta_poc = get_ue_golomb_long(gb) + 1;
186 rps->delta_poc[rps->num_negative_pics + i] = prev;
187 rps->used[rps->num_negative_pics + i] = get_bits1(gb);
195 static int decode_profile_tier_level(HEVCContext *s, ProfileTierLevel *ptl)
198 HEVCLocalContext *lc = s->HEVClc;
199 GetBitContext *gb = &lc->gb;
201 ptl->profile_space = get_bits(gb, 2);
202 ptl->tier_flag = get_bits1(gb);
203 ptl->profile_idc = get_bits(gb, 5);
204 if (ptl->profile_idc == 1)
205 av_log(s->avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
206 else if (ptl->profile_idc == 2)
207 av_log(s->avctx, AV_LOG_DEBUG, "Main10 profile bitstream\n");
209 av_log(s->avctx, AV_LOG_WARNING, "No profile indication! (%d)\n", ptl->profile_idc);
211 for (i = 0; i < 32; i++)
212 ptl->profile_compatibility_flag[i] = get_bits1(gb);
213 ptl->progressive_source_flag = get_bits1(gb);
214 ptl->interlaced_source_flag = get_bits1(gb);
215 ptl->non_packed_constraint_flag = get_bits1(gb);
216 ptl->frame_only_constraint_flag = get_bits1(gb);
217 if (get_bits(gb, 16) != 0) // XXX_reserved_zero_44bits[0..15]
219 if (get_bits(gb, 16) != 0) // XXX_reserved_zero_44bits[16..31]
221 if (get_bits(gb, 12) != 0) // XXX_reserved_zero_44bits[32..43]
223 ptl->level_idc = get_bits(gb, 8);
227 static int parse_ptl(HEVCContext *s, PTL *ptl, int max_num_sub_layers)
230 HEVCLocalContext *lc = s->HEVClc;
231 GetBitContext *gb = &lc->gb;
232 decode_profile_tier_level(s, &ptl->general_PTL);
234 for (i = 0; i < max_num_sub_layers - 1; i++) {
235 ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
236 ptl->sub_layer_level_present_flag[i] = get_bits1(gb);
238 if (max_num_sub_layers - 1> 0)
239 for (i = max_num_sub_layers - 1; i < 8; i++)
240 skip_bits(gb, 2); // reserved_zero_2bits[i]
241 for (i = 0; i < max_num_sub_layers - 1; i++) {
242 if (ptl->sub_layer_profile_present_flag[i]) {
243 decode_profile_tier_level(s, &ptl->sub_layer_PTL[i]);
244 ptl->sub_layer_PTL[i].level_idc = get_bits(gb, 8);
250 static void decode_sublayer_hrd(HEVCContext *s, int nb_cpb,
251 int subpic_params_present)
253 GetBitContext *gb = &s->HEVClc->gb;
256 for (i = 0; i < nb_cpb; i++) {
257 get_ue_golomb_long(gb); // bit_rate_value_minus1
258 get_ue_golomb_long(gb); // cpb_size_value_minus1
260 if (subpic_params_present) {
261 get_ue_golomb_long(gb); // cpb_size_du_value_minus1
262 get_ue_golomb_long(gb); // bit_rate_du_value_minus1
264 skip_bits1(gb); // cbr_flag
268 static void decode_hrd(HEVCContext *s, int common_inf_present,
271 GetBitContext *gb = &s->HEVClc->gb;
272 int nal_params_present = 0, vcl_params_present = 0;
273 int subpic_params_present = 0;
276 if (common_inf_present) {
277 nal_params_present = get_bits1(gb);
278 vcl_params_present = get_bits1(gb);
280 if (nal_params_present || vcl_params_present) {
281 subpic_params_present = get_bits1(gb);
283 if (subpic_params_present) {
284 skip_bits(gb, 8); // tick_divisor_minus2
285 skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
286 skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
287 skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
290 skip_bits(gb, 4); // bit_rate_scale
291 skip_bits(gb, 4); // cpb_size_scale
293 if (subpic_params_present)
294 skip_bits(gb, 4); // cpb_size_du_scale
296 skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
297 skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
298 skip_bits(gb, 5); // dpb_output_delay_length_minus1
302 for (i = 0; i < max_sublayers; i++) {
305 int fixed_rate = get_bits1(gb);
308 fixed_rate = get_bits1(gb);
311 get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
313 low_delay = get_bits1(gb);
316 nb_cpb = get_ue_golomb_long(gb) + 1;
318 if (nal_params_present)
319 decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
320 if (vcl_params_present)
321 decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
325 int ff_hevc_decode_nal_vps(HEVCContext *s)
328 GetBitContext *gb = &s->HEVClc->gb;
331 AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
334 return AVERROR(ENOMEM);
335 vps = (HEVCVPS*)vps_buf->data;
337 av_log(s->avctx, AV_LOG_DEBUG, "Decoding VPS\n");
340 return AVERROR(ENOMEM);
342 vps_id = get_bits(gb, 4);
343 if (vps_id >= MAX_VPS_COUNT) {
344 av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
348 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
349 av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
353 vps->vps_max_layers = get_bits(gb, 6) + 1;
354 vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
355 vps->vps_temporal_id_nesting_flag = get_bits1(gb);
357 if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
358 av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
362 if (vps->vps_max_sub_layers > MAX_SUB_LAYERS) {
363 av_log(s->avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
364 vps->vps_max_sub_layers);
368 if (parse_ptl(s, &vps->ptl, vps->vps_max_sub_layers) < 0) {
369 av_log(s->avctx, AV_LOG_ERROR, "Error decoding profile tier level.\n");
372 vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
374 i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
375 for (; i < vps->vps_max_sub_layers; i++) {
376 vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
377 vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb);
378 vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1;
380 if (vps->vps_max_dec_pic_buffering[i] > MAX_DPB_SIZE) {
381 av_log(s->avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
382 vps->vps_max_dec_pic_buffering[i] - 1);
385 if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
386 av_log(s->avctx, AV_LOG_ERROR, "vps_max_num_reorder_pics out of range: %d\n",
387 vps->vps_num_reorder_pics[i]);
392 vps->vps_max_layer_id = get_bits(gb, 6);
393 vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
394 for (i = 1; i < vps->vps_num_layer_sets; i++)
395 for (j = 0; j <= vps->vps_max_layer_id; j++)
396 skip_bits(gb, 1); // layer_id_included_flag[i][j]
398 vps->vps_timing_info_present_flag = get_bits1(gb);
399 if (vps->vps_timing_info_present_flag) {
400 vps->vps_num_units_in_tick = get_bits_long(gb, 32);
401 vps->vps_time_scale = get_bits_long(gb, 32);
402 vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
403 if (vps->vps_poc_proportional_to_timing_flag)
404 vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
405 vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
406 for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
407 int common_inf_present = 1;
409 get_ue_golomb_long(gb); // hrd_layer_set_idx
411 common_inf_present = get_bits1(gb);
412 decode_hrd(s, common_inf_present, vps->vps_max_sub_layers);
416 vps->vps_extension_flag = get_bits1(gb);
418 av_buffer_unref(&s->vps_list[vps_id]);
419 s->vps_list[vps_id] = vps_buf;
424 av_buffer_unref(&vps_buf);
425 return AVERROR_INVALIDDATA;
428 static void decode_vui(HEVCContext *s, HEVCSPS *sps)
430 VUI *vui = &sps->vui;
431 GetBitContext *gb = &s->HEVClc->gb;
434 av_log(s->avctx, AV_LOG_DEBUG, "Decoding VUI\n");
436 sar_present = get_bits1(gb);
438 uint8_t sar_idx = get_bits(gb, 8);
439 if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
440 vui->sar = vui_sar[sar_idx];
441 else if (sar_idx == 255) {
442 vui->sar.num = get_bits(gb, 16);
443 vui->sar.den = get_bits(gb, 16);
445 av_log(s->avctx, AV_LOG_WARNING,
446 "Unknown SAR index: %u.\n", sar_idx);
449 vui->overscan_info_present_flag = get_bits1(gb);
450 if (vui->overscan_info_present_flag)
451 vui->overscan_appropriate_flag = get_bits1(gb);
453 vui->video_signal_type_present_flag = get_bits1(gb);
454 if (vui->video_signal_type_present_flag) {
455 vui->video_format = get_bits(gb, 3);
456 vui->video_full_range_flag = get_bits1(gb);
457 vui->colour_description_present_flag = get_bits1(gb);
458 if (vui->video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
459 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
460 if (vui->colour_description_present_flag) {
461 vui->colour_primaries = get_bits(gb, 8);
462 vui->transfer_characteristic = get_bits(gb, 8);
463 vui->matrix_coeffs = get_bits(gb, 8);
465 // Set invalid values to "unspecified"
466 if (vui->colour_primaries >= AVCOL_PRI_NB)
467 vui->colour_primaries = AVCOL_PRI_UNSPECIFIED;
468 if (vui->transfer_characteristic >= AVCOL_TRC_NB)
469 vui->transfer_characteristic = AVCOL_TRC_UNSPECIFIED;
470 if (vui->matrix_coeffs >= AVCOL_SPC_NB)
471 vui->matrix_coeffs = AVCOL_SPC_UNSPECIFIED;
475 vui->chroma_loc_info_present_flag = get_bits1(gb);
476 if (vui->chroma_loc_info_present_flag) {
477 vui->chroma_sample_loc_type_top_field = get_ue_golomb_long(gb);
478 vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_long(gb);
481 vui->neutra_chroma_indication_flag = get_bits1(gb);
482 vui->field_seq_flag = get_bits1(gb);
483 vui->frame_field_info_present_flag = get_bits1(gb);
485 vui->default_display_window_flag = get_bits1(gb);
486 if (vui->default_display_window_flag) {
487 //TODO: * 2 is only valid for 420
488 vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * 2;
489 vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * 2;
490 vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * 2;
491 vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * 2;
493 if (s->apply_defdispwin &&
494 s->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
495 av_log(s->avctx, AV_LOG_DEBUG,
496 "discarding vui default display window, "
497 "original values are l:%u r:%u t:%u b:%u\n",
498 vui->def_disp_win.left_offset,
499 vui->def_disp_win.right_offset,
500 vui->def_disp_win.top_offset,
501 vui->def_disp_win.bottom_offset);
503 vui->def_disp_win.left_offset =
504 vui->def_disp_win.right_offset =
505 vui->def_disp_win.top_offset =
506 vui->def_disp_win.bottom_offset = 0;
510 vui->vui_timing_info_present_flag = get_bits1(gb);
512 if (vui->vui_timing_info_present_flag) {
513 vui->vui_num_units_in_tick = get_bits(gb, 32);
514 vui->vui_time_scale = get_bits(gb, 32);
515 s->avctx->time_base.num = vui->vui_num_units_in_tick;
516 s->avctx->time_base.den = vui->vui_time_scale;
517 vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
518 if (vui->vui_poc_proportional_to_timing_flag)
519 vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
520 vui->vui_hrd_parameters_present_flag = get_bits1(gb);
521 if (vui->vui_hrd_parameters_present_flag)
522 decode_hrd(s, 1, sps->max_sub_layers);
525 vui->bitstream_restriction_flag = get_bits1(gb);
526 if (vui->bitstream_restriction_flag) {
527 vui->tiles_fixed_structure_flag = get_bits1(gb);
528 vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
529 vui->restricted_ref_pic_lists_flag = get_bits1(gb);
530 vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb);
531 vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb);
532 vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb);
533 vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb);
534 vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb);
538 static void set_default_scaling_list_data(ScalingList *sl)
542 for (matrixId = 0; matrixId < 6; matrixId++) {
544 memset(sl->sl[0][matrixId], 16, 16);
545 sl->sl_dc[0][matrixId] = 16; // default for 16x16
546 sl->sl_dc[1][matrixId] = 16; // default for 32x32
548 memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
549 memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
550 memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
551 memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
552 memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
553 memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
554 memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
555 memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
556 memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
557 memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
558 memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
559 memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
560 memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
561 memcpy(sl->sl[3][1], default_scaling_list_inter, 64);
564 static int scaling_list_data(HEVCContext *s, ScalingList *sl)
566 GetBitContext *gb = &s->HEVClc->gb;
567 uint8_t scaling_list_pred_mode_flag[4][6];
568 int32_t scaling_list_dc_coef[2][6];
569 int size_id, matrix_id, i, pos, delta;
571 for (size_id = 0; size_id < 4; size_id++)
572 for (matrix_id = 0; matrix_id < (size_id == 3 ? 2 : 6); matrix_id++) {
573 scaling_list_pred_mode_flag[size_id][matrix_id] = get_bits1(gb);
574 if (!scaling_list_pred_mode_flag[size_id][matrix_id]) {
575 delta = get_ue_golomb_long(gb);
576 /* Only need to handle non-zero delta. Zero means default,
577 * which should already be in the arrays. */
579 // Copy from previous array.
580 if (matrix_id - delta < 0) {
581 av_log(s->avctx, AV_LOG_ERROR,
582 "Invalid delta in scaling list data: %d.\n", delta);
583 return AVERROR_INVALIDDATA;
586 memcpy(sl->sl[size_id][matrix_id],
587 sl->sl[size_id][matrix_id - delta],
588 size_id > 0 ? 64 : 16);
590 sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
593 int next_coef, coef_num;
594 int32_t scaling_list_delta_coef;
597 coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
599 scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
600 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
601 sl->sl_dc[size_id - 2][matrix_id] = next_coef;
603 for (i = 0; i < coef_num; i++) {
605 pos = 4 * ff_hevc_diag_scan4x4_y[i] +
606 ff_hevc_diag_scan4x4_x[i];
608 pos = 8 * ff_hevc_diag_scan8x8_y[i] +
609 ff_hevc_diag_scan8x8_x[i];
611 scaling_list_delta_coef = get_se_golomb(gb);
612 next_coef = (next_coef + scaling_list_delta_coef + 256) % 256;
613 sl->sl[size_id][matrix_id][pos] = next_coef;
621 int ff_hevc_decode_nal_sps(HEVCContext *s)
623 const AVPixFmtDescriptor *desc;
624 GetBitContext *gb = &s->HEVClc->gb;
627 int log2_diff_max_min_transform_block_size;
628 int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
632 AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
635 return AVERROR(ENOMEM);
636 sps = (HEVCSPS*)sps_buf->data;
638 av_log(s->avctx, AV_LOG_DEBUG, "Decoding SPS\n");
642 sps->vps_id = get_bits(gb, 4);
643 if (sps->vps_id >= MAX_VPS_COUNT) {
644 av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
645 ret = AVERROR_INVALIDDATA;
649 sps->max_sub_layers = get_bits(gb, 3) + 1;
650 if (sps->max_sub_layers > MAX_SUB_LAYERS) {
651 av_log(s->avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
652 sps->max_sub_layers);
653 ret = AVERROR_INVALIDDATA;
657 skip_bits1(gb); // temporal_id_nesting_flag
659 if (parse_ptl(s, &sps->ptl, sps->max_sub_layers) < 0) {
660 av_log(s->avctx, AV_LOG_ERROR, "error decoding profile tier level\n");
661 ret = AVERROR_INVALIDDATA;
664 sps_id = get_ue_golomb_long(gb);
665 if (sps_id >= MAX_SPS_COUNT) {
666 av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", sps_id);
667 ret = AVERROR_INVALIDDATA;
671 sps->chroma_format_idc = get_ue_golomb_long(gb);
672 if (sps->chroma_format_idc != 1) {
673 avpriv_report_missing_feature(s->avctx, "chroma_format_idc != 1\n");
674 ret = AVERROR_PATCHWELCOME;
678 if (sps->chroma_format_idc == 3)
679 sps->separate_colour_plane_flag = get_bits1(gb);
681 sps->width = get_ue_golomb_long(gb);
682 sps->height = get_ue_golomb_long(gb);
683 if ((ret = av_image_check_size(sps->width,
684 sps->height, 0, s->avctx)) < 0)
687 if (get_bits1(gb)) { // pic_conformance_flag
688 //TODO: * 2 is only valid for 420
689 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * 2;
690 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * 2;
691 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * 2;
692 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * 2;
694 if (s->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
695 av_log(s->avctx, AV_LOG_DEBUG,
696 "discarding sps conformance window, "
697 "original values are l:%u r:%u t:%u b:%u\n",
698 sps->pic_conf_win.left_offset,
699 sps->pic_conf_win.right_offset,
700 sps->pic_conf_win.top_offset,
701 sps->pic_conf_win.bottom_offset);
703 sps->pic_conf_win.left_offset =
704 sps->pic_conf_win.right_offset =
705 sps->pic_conf_win.top_offset =
706 sps->pic_conf_win.bottom_offset = 0;
708 sps->output_window = sps->pic_conf_win;
711 sps->bit_depth = get_ue_golomb_long(gb) + 8;
712 bit_depth_chroma = get_ue_golomb_long(gb) + 8;
713 if (bit_depth_chroma != sps->bit_depth) {
714 av_log(s->avctx, AV_LOG_ERROR,
715 "Luma bit depth (%d) is different from chroma bit depth (%d), "
716 "this is unsupported.\n",
717 sps->bit_depth, bit_depth_chroma);
718 ret = AVERROR_INVALIDDATA;
722 if (sps->chroma_format_idc == 1) {
723 switch (sps->bit_depth) {
724 case 8: sps->pix_fmt = AV_PIX_FMT_YUV420P; break;
725 case 9: sps->pix_fmt = AV_PIX_FMT_YUV420P9; break;
726 case 10: sps->pix_fmt = AV_PIX_FMT_YUV420P10; break;
728 av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
730 ret = AVERROR_PATCHWELCOME;
734 av_log(s->avctx, AV_LOG_ERROR,
735 "non-4:2:0 support is currently unspecified.\n");
736 return AVERROR_PATCHWELCOME;
739 desc = av_pix_fmt_desc_get(sps->pix_fmt);
741 ret = AVERROR(EINVAL);
745 sps->hshift[0] = sps->vshift[0] = 0;
746 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
747 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
749 sps->pixel_shift = sps->bit_depth > 8;
751 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
752 if (sps->log2_max_poc_lsb > 16) {
753 av_log(s->avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
754 sps->log2_max_poc_lsb - 4);
755 ret = AVERROR_INVALIDDATA;
759 sublayer_ordering_info = get_bits1(gb);
760 start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
761 for (i = start; i < sps->max_sub_layers; i++) {
762 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
763 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
764 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
765 if (sps->temporal_layer[i].max_dec_pic_buffering > MAX_DPB_SIZE) {
766 av_log(s->avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
767 sps->temporal_layer[i].max_dec_pic_buffering - 1);
768 ret = AVERROR_INVALIDDATA;
771 if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
772 av_log(s->avctx, AV_LOG_ERROR, "sps_max_num_reorder_pics out of range: %d\n",
773 sps->temporal_layer[i].num_reorder_pics);
774 ret = AVERROR_INVALIDDATA;
779 if (!sublayer_ordering_info) {
780 for (i = 0; i < start; i++) {
781 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
782 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
783 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
787 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
788 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
789 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
790 log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
791 sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
792 sps->log2_min_tb_size;
794 if (sps->log2_min_tb_size >= sps->log2_min_cb_size) {
795 av_log(s->avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
796 ret = AVERROR_INVALIDDATA;
799 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
800 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
802 sps->scaling_list_enable_flag = get_bits1(gb);
803 if (sps->scaling_list_enable_flag) {
804 set_default_scaling_list_data(&sps->scaling_list);
807 ret = scaling_list_data(s, &sps->scaling_list);
813 sps->amp_enabled_flag = get_bits1(gb);
814 sps->sao_enabled = get_bits1(gb);
816 sps->pcm_enabled_flag = get_bits1(gb);
817 if (sps->pcm_enabled_flag) {
818 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
819 sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
820 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
821 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
822 get_ue_golomb_long(gb);
823 if (sps->pcm.bit_depth > sps->bit_depth) {
824 av_log(s->avctx, AV_LOG_ERROR,
825 "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
826 sps->pcm.bit_depth, sps->bit_depth);
827 ret = AVERROR_INVALIDDATA;
831 sps->pcm.loop_filter_disable_flag = get_bits1(gb);
834 sps->nb_st_rps = get_ue_golomb_long(gb);
835 if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {
836 av_log(s->avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
838 ret = AVERROR_INVALIDDATA;
841 for (i = 0; i < sps->nb_st_rps; i++) {
842 if ((ret = ff_hevc_decode_short_term_rps(s, &sps->st_rps[i],
847 sps->long_term_ref_pics_present_flag = get_bits1(gb);
848 if (sps->long_term_ref_pics_present_flag) {
849 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
850 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
851 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
852 sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
856 sps->sps_temporal_mvp_enabled_flag = get_bits1(gb);
857 sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
858 sps->vui.sar = (AVRational){0, 1};
859 vui_present = get_bits1(gb);
862 skip_bits1(gb); // sps_extension_flag
864 if (s->apply_defdispwin) {
865 sps->output_window.left_offset += sps->vui.def_disp_win.left_offset;
866 sps->output_window.right_offset += sps->vui.def_disp_win.right_offset;
867 sps->output_window.top_offset += sps->vui.def_disp_win.top_offset;
868 sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
870 if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&
871 !(s->avctx->flags & CODEC_FLAG_UNALIGNED)) {
872 sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));
873 av_log(s->avctx, AV_LOG_WARNING, "Reducing left output window to %d "
874 "chroma samples to preserve alignment.\n",
875 sps->output_window.left_offset);
877 sps->output_width = sps->width -
878 (sps->output_window.left_offset + sps->output_window.right_offset);
879 sps->output_height = sps->height -
880 (sps->output_window.top_offset + sps->output_window.bottom_offset);
881 if (sps->output_width <= 0 || sps->output_height <= 0) {
882 av_log(s->avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n",
883 sps->output_width, sps->output_height);
884 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
885 ret = AVERROR_INVALIDDATA;
888 av_log(s->avctx, AV_LOG_WARNING,
889 "Displaying the whole video surface.\n");
890 sps->pic_conf_win.left_offset =
891 sps->pic_conf_win.right_offset =
892 sps->pic_conf_win.top_offset =
893 sps->pic_conf_win.bottom_offset = 0;
894 sps->output_width = sps->width;
895 sps->output_height = sps->height;
898 // Inferred parameters
899 sps->log2_ctb_size = sps->log2_min_cb_size +
900 sps->log2_diff_max_min_coding_block_size;
901 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
903 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
904 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
905 sps->ctb_size = sps->ctb_width * sps->ctb_height;
907 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
908 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
909 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
910 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
911 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
912 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
914 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
916 if (sps->width & ((1 << sps->log2_min_cb_size) - 1) ||
917 sps->height & ((1 << sps->log2_min_cb_size) - 1)) {
918 av_log(s->avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
922 if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) {
923 av_log(s->avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
926 if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
927 av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
928 sps->max_transform_hierarchy_depth_inter);
931 if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
932 av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
933 sps->max_transform_hierarchy_depth_intra);
936 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
937 av_log(s->avctx, AV_LOG_ERROR,
938 "max transform block size out of range: %d\n",
939 sps->log2_max_trafo_size);
943 if (s->avctx->debug & FF_DEBUG_BITSTREAM) {
944 av_log(s->avctx, AV_LOG_DEBUG,
945 "Parsed SPS: id %d; coded wxh: %dx%d; "
946 "cropped wxh: %dx%d; pix_fmt: %s.\n",
947 sps_id, sps->width, sps->height,
948 sps->output_width, sps->output_height,
949 av_get_pix_fmt_name(sps->pix_fmt));
952 /* check if this is a repeat of an already parsed SPS, then keep the
954 * otherwise drop all PPSes that depend on it */
955 if (s->sps_list[sps_id] &&
956 !memcmp(s->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
957 av_buffer_unref(&sps_buf);
959 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
960 if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == sps_id)
961 av_buffer_unref(&s->pps_list[i]);
963 av_buffer_unref(&s->sps_list[sps_id]);
964 s->sps_list[sps_id] = sps_buf;
970 av_buffer_unref(&sps_buf);
974 static void hevc_pps_free(void *opaque, uint8_t *data)
976 HEVCPPS *pps = (HEVCPPS*)data;
978 av_freep(&pps->column_width);
979 av_freep(&pps->row_height);
980 av_freep(&pps->col_bd);
981 av_freep(&pps->row_bd);
982 av_freep(&pps->col_idxX);
983 av_freep(&pps->ctb_addr_rs_to_ts);
984 av_freep(&pps->ctb_addr_ts_to_rs);
985 av_freep(&pps->tile_pos_rs);
986 av_freep(&pps->tile_id);
987 av_freep(&pps->min_cb_addr_zs);
988 av_freep(&pps->min_tb_addr_zs);
993 int ff_hevc_decode_nal_pps(HEVCContext *s)
995 GetBitContext *gb = &s->HEVClc->gb;
997 int pic_area_in_ctbs, pic_area_in_min_cbs, pic_area_in_min_tbs;
998 int log2_diff_ctb_min_tb_size;
999 int i, j, x, y, ctb_addr_rs, tile_id;
1003 AVBufferRef *pps_buf;
1004 HEVCPPS *pps = av_mallocz(sizeof(*pps));
1007 return AVERROR(ENOMEM);
1009 pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
1010 hevc_pps_free, NULL, 0);
1013 return AVERROR(ENOMEM);
1016 av_log(s->avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1019 pps->loop_filter_across_tiles_enabled_flag = 1;
1020 pps->num_tile_columns = 1;
1021 pps->num_tile_rows = 1;
1022 pps->uniform_spacing_flag = 1;
1023 pps->disable_dbf = 0;
1024 pps->beta_offset = 0;
1028 pps_id = get_ue_golomb_long(gb);
1029 if (pps_id >= MAX_PPS_COUNT) {
1030 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1031 ret = AVERROR_INVALIDDATA;
1034 pps->sps_id = get_ue_golomb_long(gb);
1035 if (pps->sps_id >= MAX_SPS_COUNT) {
1036 av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1037 ret = AVERROR_INVALIDDATA;
1040 if (!s->sps_list[pps->sps_id]) {
1041 av_log(s->avctx, AV_LOG_ERROR, "SPS does not exist \n");
1042 ret = AVERROR_INVALIDDATA;
1045 sps = (HEVCSPS *)s->sps_list[pps->sps_id]->data;
1047 pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
1048 pps->output_flag_present_flag = get_bits1(gb);
1049 pps->num_extra_slice_header_bits = get_bits(gb, 3);
1051 pps->sign_data_hiding_flag = get_bits1(gb);
1053 pps->cabac_init_present_flag = get_bits1(gb);
1055 pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1;
1056 pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1;
1058 pps->pic_init_qp_minus26 = get_se_golomb(gb);
1060 pps->constrained_intra_pred_flag = get_bits1(gb);
1061 pps->transform_skip_enabled_flag = get_bits1(gb);
1063 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
1064 pps->diff_cu_qp_delta_depth = 0;
1065 if (pps->cu_qp_delta_enabled_flag)
1066 pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
1068 pps->cb_qp_offset = get_se_golomb(gb);
1069 if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1070 av_log(s->avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1072 ret = AVERROR_INVALIDDATA;
1075 pps->cr_qp_offset = get_se_golomb(gb);
1076 if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1077 av_log(s->avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1079 ret = AVERROR_INVALIDDATA;
1082 pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
1084 pps->weighted_pred_flag = get_bits1(gb);
1085 pps->weighted_bipred_flag = get_bits1(gb);
1087 pps->transquant_bypass_enable_flag = get_bits1(gb);
1088 pps->tiles_enabled_flag = get_bits1(gb);
1089 pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
1091 if (pps->tiles_enabled_flag) {
1092 pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
1093 pps->num_tile_rows = get_ue_golomb_long(gb) + 1;
1094 if (pps->num_tile_columns == 0 ||
1095 pps->num_tile_columns >= sps->width) {
1096 av_log(s->avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1097 pps->num_tile_columns - 1);
1098 ret = AVERROR_INVALIDDATA;
1101 if (pps->num_tile_rows == 0 ||
1102 pps->num_tile_rows >= sps->height) {
1103 av_log(s->avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1104 pps->num_tile_rows - 1);
1105 ret = AVERROR_INVALIDDATA;
1109 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1110 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1111 if (!pps->column_width || !pps->row_height) {
1112 ret = AVERROR(ENOMEM);
1116 pps->uniform_spacing_flag = get_bits1(gb);
1117 if (!pps->uniform_spacing_flag) {
1119 for (i = 0; i < pps->num_tile_columns - 1; i++) {
1120 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1121 sum += pps->column_width[i];
1123 if (sum >= sps->ctb_width) {
1124 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1125 ret = AVERROR_INVALIDDATA;
1128 pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1131 for (i = 0; i < pps->num_tile_rows - 1; i++) {
1132 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1133 sum += pps->row_height[i];
1135 if (sum >= sps->ctb_height) {
1136 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1137 ret = AVERROR_INVALIDDATA;
1140 pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1142 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
1145 pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1147 pps->deblocking_filter_control_present_flag = get_bits1(gb);
1148 if (pps->deblocking_filter_control_present_flag) {
1149 pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
1150 pps->disable_dbf = get_bits1(gb);
1151 if (!pps->disable_dbf) {
1152 pps->beta_offset = get_se_golomb(gb) * 2;
1153 pps->tc_offset = get_se_golomb(gb) * 2;
1154 if (pps->beta_offset/2 < -6 || pps->beta_offset/2 > 6) {
1155 av_log(s->avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1156 pps->beta_offset/2);
1157 ret = AVERROR_INVALIDDATA;
1160 if (pps->tc_offset/2 < -6 || pps->tc_offset/2 > 6) {
1161 av_log(s->avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1163 ret = AVERROR_INVALIDDATA;
1169 pps->scaling_list_data_present_flag = get_bits1(gb);
1170 if (pps->scaling_list_data_present_flag) {
1171 set_default_scaling_list_data(&pps->scaling_list);
1172 ret = scaling_list_data(s, &pps->scaling_list);
1176 pps->lists_modification_present_flag = get_bits1(gb);
1177 pps->log2_parallel_merge_level = get_ue_golomb_long(gb) + 2;
1178 if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
1179 av_log(s->avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1180 pps->log2_parallel_merge_level - 2);
1181 ret = AVERROR_INVALIDDATA;
1185 pps->slice_header_extension_present_flag = get_bits1(gb);
1186 pps->pps_extension_flag = get_bits1(gb);
1188 // Inferred parameters
1189 pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1190 pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1191 pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1192 if (!pps->col_bd || !pps->row_bd || !pps->col_idxX) {
1193 ret = AVERROR(ENOMEM);
1197 if (pps->uniform_spacing_flag) {
1198 if (!pps->column_width) {
1199 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1200 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1202 if (!pps->column_width || !pps->row_height) {
1203 ret = AVERROR(ENOMEM);
1207 for (i = 0; i < pps->num_tile_columns; i++) {
1208 pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1209 (i * sps->ctb_width) / pps->num_tile_columns;
1212 for (i = 0; i < pps->num_tile_rows; i++) {
1213 pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1214 (i * sps->ctb_height) / pps->num_tile_rows;
1219 for (i = 0; i < pps->num_tile_columns; i++)
1220 pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1223 for (i = 0; i < pps->num_tile_rows; i++)
1224 pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1226 for (i = 0, j = 0; i < sps->ctb_width; i++) {
1227 if (i > pps->col_bd[j])
1229 pps->col_idxX[i] = j;
1235 pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1236 pic_area_in_min_cbs = sps->min_cb_width * sps->min_cb_height;
1237 pic_area_in_min_tbs = sps->min_tb_width * sps->min_tb_height;
1239 pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1240 pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1241 pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1242 pps->min_cb_addr_zs = av_malloc_array(pic_area_in_min_cbs, sizeof(*pps->min_cb_addr_zs));
1243 pps->min_tb_addr_zs = av_malloc_array(pic_area_in_min_tbs, sizeof(*pps->min_tb_addr_zs));
1244 if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1245 !pps->tile_id || !pps->min_cb_addr_zs || !pps->min_tb_addr_zs) {
1246 ret = AVERROR(ENOMEM);
1250 for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1251 int tb_x = ctb_addr_rs % sps->ctb_width;
1252 int tb_y = ctb_addr_rs / sps->ctb_width;
1257 for (i = 0; i < pps->num_tile_columns; i++) {
1258 if (tb_x < pps->col_bd[i + 1]) {
1264 for (i = 0; i < pps->num_tile_rows; i++) {
1265 if (tb_y < pps->row_bd[i + 1]) {
1271 for (i = 0; i < tile_x; i++)
1272 val += pps->row_height[tile_y] * pps->column_width[i];
1273 for (i = 0; i < tile_y; i++)
1274 val += sps->ctb_width * pps->row_height[i];
1276 val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1277 tb_x - pps->col_bd[tile_x];
1279 pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1280 pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1283 for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1284 for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1285 for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1286 for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1287 pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1289 pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1290 if (!pps->tile_pos_rs) {
1291 ret = AVERROR(ENOMEM);
1295 for (j = 0; j < pps->num_tile_rows; j++)
1296 for (i = 0; i < pps->num_tile_columns; i++)
1297 pps->tile_pos_rs[j * pps->num_tile_columns + i] = pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1299 for (y = 0; y < sps->min_cb_height; y++) {
1300 for (x = 0; x < sps->min_cb_width; x++) {
1301 int tb_x = x >> sps->log2_diff_max_min_coding_block_size;
1302 int tb_y = y >> sps->log2_diff_max_min_coding_block_size;
1303 int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1304 int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1305 (sps->log2_diff_max_min_coding_block_size * 2);
1306 for (i = 0; i < sps->log2_diff_max_min_coding_block_size; i++) {
1308 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1310 pps->min_cb_addr_zs[y * sps->min_cb_width + x] = val;
1314 log2_diff_ctb_min_tb_size = sps->log2_ctb_size - sps->log2_min_tb_size;
1315 for (y = 0; y < sps->min_tb_height; y++) {
1316 for (x = 0; x < sps->min_tb_width; x++) {
1317 int tb_x = x >> log2_diff_ctb_min_tb_size;
1318 int tb_y = y >> log2_diff_ctb_min_tb_size;
1319 int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1320 int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1321 (log2_diff_ctb_min_tb_size * 2);
1322 for (i = 0; i < log2_diff_ctb_min_tb_size; i++) {
1324 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1326 pps->min_tb_addr_zs[y * sps->min_tb_width + x] = val;
1330 av_buffer_unref(&s->pps_list[pps_id]);
1331 s->pps_list[pps_id] = pps_buf;
1336 av_buffer_unref(&pps_buf);