hevc: refactor Profile Tier Level
[libav.git] / libavcodec / hevc_ps.c
1 /*
2 * HEVC Parameter Set decoding
3 *
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
8 *
9 * This file is part of Libav.
10 *
11 * Libav 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.
15 *
16 * Libav 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.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with Libav; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "libavutil/imgutils.h"
27
28 #include "golomb.h"
29 #include "hevc.h"
30
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
40 };
41
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
51 };
52
53 static const AVRational vui_sar[] = {
54 { 0, 1 },
55 { 1, 1 },
56 { 12, 11 },
57 { 10, 11 },
58 { 16, 11 },
59 { 40, 33 },
60 { 24, 11 },
61 { 20, 11 },
62 { 32, 11 },
63 { 80, 33 },
64 { 18, 11 },
65 { 15, 11 },
66 { 64, 33 },
67 { 160, 99 },
68 { 4, 3 },
69 { 3, 2 },
70 { 2, 1 },
71 };
72
73 int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
74 const HEVCSPS *sps, int is_slice_header)
75 {
76 HEVCLocalContext *lc = &s->HEVClc;
77 uint8_t rps_predict = 0;
78 int delta_poc;
79 int k0 = 0;
80 int k1 = 0;
81 int k = 0;
82 int i;
83
84 GetBitContext *gb = &lc->gb;
85
86 if (rps != sps->st_rps && sps->nb_st_rps)
87 rps_predict = get_bits1(gb);
88
89 if (rps_predict) {
90 const ShortTermRPS *rps_ridx;
91 int delta_rps, abs_delta_rps;
92 uint8_t use_delta_flag = 0;
93 uint8_t delta_rps_sign;
94
95 if (is_slice_header) {
96 int delta_idx = get_ue_golomb_long(gb) + 1;
97 if (delta_idx > sps->nb_st_rps) {
98 av_log(s->avctx, AV_LOG_ERROR,
99 "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
100 delta_idx, sps->nb_st_rps);
101 return AVERROR_INVALIDDATA;
102 }
103 rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
104 } else
105 rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
106
107 delta_rps_sign = get_bits1(gb);
108 abs_delta_rps = get_ue_golomb_long(gb) + 1;
109 delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
110 for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
111 int used = rps->used[k] = get_bits1(gb);
112
113 if (!used)
114 use_delta_flag = get_bits1(gb);
115
116 if (used || use_delta_flag) {
117 if (i < rps_ridx->num_delta_pocs)
118 delta_poc = delta_rps + rps_ridx->delta_poc[i];
119 else
120 delta_poc = delta_rps;
121 rps->delta_poc[k] = delta_poc;
122 if (delta_poc < 0)
123 k0++;
124 else
125 k1++;
126 k++;
127 }
128 }
129
130 rps->num_delta_pocs = k;
131 rps->num_negative_pics = k0;
132 // sort in increasing order (smallest first)
133 if (rps->num_delta_pocs != 0) {
134 int used, tmp;
135 for (i = 1; i < rps->num_delta_pocs; i++) {
136 delta_poc = rps->delta_poc[i];
137 used = rps->used[i];
138 for (k = i - 1; k >= 0; k--) {
139 tmp = rps->delta_poc[k];
140 if (delta_poc < tmp) {
141 rps->delta_poc[k + 1] = tmp;
142 rps->used[k + 1] = rps->used[k];
143 rps->delta_poc[k] = delta_poc;
144 rps->used[k] = used;
145 }
146 }
147 }
148 }
149 if ((rps->num_negative_pics >> 1) != 0) {
150 int used;
151 k = rps->num_negative_pics - 1;
152 // flip the negative values to largest first
153 for (i = 0; i < rps->num_negative_pics >> 1; i++) {
154 delta_poc = rps->delta_poc[i];
155 used = rps->used[i];
156 rps->delta_poc[i] = rps->delta_poc[k];
157 rps->used[i] = rps->used[k];
158 rps->delta_poc[k] = delta_poc;
159 rps->used[k] = used;
160 k--;
161 }
162 }
163 } else {
164 unsigned int prev, nb_positive_pics;
165 rps->num_negative_pics = get_ue_golomb_long(gb);
166 nb_positive_pics = get_ue_golomb_long(gb);
167
168 if (rps->num_negative_pics >= MAX_REFS ||
169 nb_positive_pics >= MAX_REFS) {
170 av_log(s->avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
171 return AVERROR_INVALIDDATA;
172 }
173
174 rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
175 if (rps->num_delta_pocs) {
176 prev = 0;
177 for (i = 0; i < rps->num_negative_pics; i++) {
178 delta_poc = get_ue_golomb_long(gb) + 1;
179 prev -= delta_poc;
180 rps->delta_poc[i] = prev;
181 rps->used[i] = get_bits1(gb);
182 }
183 prev = 0;
184 for (i = 0; i < nb_positive_pics; i++) {
185 delta_poc = get_ue_golomb_long(gb) + 1;
186 prev += delta_poc;
187 rps->delta_poc[rps->num_negative_pics + i] = prev;
188 rps->used[rps->num_negative_pics + i] = get_bits1(gb);
189 }
190 }
191 }
192 return 0;
193 }
194
195
196 static void decode_profile_tier_level(HEVCContext *s, PTLCommon *ptl)
197 {
198 int i;
199 GetBitContext *gb = &s->HEVClc.gb;
200
201 ptl->profile_space = get_bits(gb, 2);
202 ptl->tier_flag = get_bits1(gb);
203 ptl->profile_idc = get_bits(gb, 5);
204
205 for (i = 0; i < 32; i++)
206 ptl->profile_compatibility_flag[i] = get_bits1(gb);
207 ptl->progressive_source_flag = get_bits1(gb);
208 ptl->interlaced_source_flag = get_bits1(gb);
209 ptl->non_packed_constraint_flag = get_bits1(gb);
210 ptl->frame_only_constraint_flag = get_bits1(gb);
211
212 skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15]
213 skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31]
214 skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43]
215 }
216
217 static void parse_ptl(HEVCContext *s, PTL *ptl, int max_num_sub_layers)
218 {
219 int i;
220 GetBitContext *gb = &s->HEVClc.gb;
221 decode_profile_tier_level(s, &ptl->general_ptl);
222 ptl->general_ptl.level_idc = get_bits(gb, 8);
223
224 for (i = 0; i < max_num_sub_layers - 1; i++) {
225 ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
226 ptl->sub_layer_level_present_flag[i] = get_bits1(gb);
227 }
228 if (max_num_sub_layers - 1 > 0)
229 for (i = max_num_sub_layers - 1; i < 8; i++)
230 skip_bits(gb, 2); // reserved_zero_2bits[i]
231 for (i = 0; i < max_num_sub_layers - 1; i++) {
232 if (ptl->sub_layer_profile_present_flag[i])
233 decode_profile_tier_level(s, &ptl->sub_layer_ptl[i]);
234 if (ptl->sub_layer_level_present_flag[i])
235 ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
236 }
237 }
238
239 static void decode_sublayer_hrd(HEVCContext *s, int nb_cpb,
240 int subpic_params_present)
241 {
242 GetBitContext *gb = &s->HEVClc.gb;
243 int i;
244
245 for (i = 0; i < nb_cpb; i++) {
246 get_ue_golomb_long(gb); // bit_rate_value_minus1
247 get_ue_golomb_long(gb); // cpb_size_value_minus1
248
249 if (subpic_params_present) {
250 get_ue_golomb_long(gb); // cpb_size_du_value_minus1
251 get_ue_golomb_long(gb); // bit_rate_du_value_minus1
252 }
253 skip_bits1(gb); // cbr_flag
254 }
255 }
256
257 static void decode_hrd(HEVCContext *s, int common_inf_present,
258 int max_sublayers)
259 {
260 GetBitContext *gb = &s->HEVClc.gb;
261 int nal_params_present = 0, vcl_params_present = 0;
262 int subpic_params_present = 0;
263 int i;
264
265 if (common_inf_present) {
266 nal_params_present = get_bits1(gb);
267 vcl_params_present = get_bits1(gb);
268
269 if (nal_params_present || vcl_params_present) {
270 subpic_params_present = get_bits1(gb);
271
272 if (subpic_params_present) {
273 skip_bits(gb, 8); // tick_divisor_minus2
274 skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
275 skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
276 skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
277 }
278
279 skip_bits(gb, 4); // bit_rate_scale
280 skip_bits(gb, 4); // cpb_size_scale
281
282 if (subpic_params_present)
283 skip_bits(gb, 4); // cpb_size_du_scale
284
285 skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
286 skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
287 skip_bits(gb, 5); // dpb_output_delay_length_minus1
288 }
289 }
290
291 for (i = 0; i < max_sublayers; i++) {
292 int low_delay = 0;
293 int nb_cpb = 1;
294 int fixed_rate = get_bits1(gb);
295
296 if (!fixed_rate)
297 fixed_rate = get_bits1(gb);
298
299 if (fixed_rate)
300 get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
301 else
302 low_delay = get_bits1(gb);
303
304 if (!low_delay)
305 nb_cpb = get_ue_golomb_long(gb) + 1;
306
307 if (nal_params_present)
308 decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
309 if (vcl_params_present)
310 decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
311 }
312 }
313
314 int ff_hevc_decode_nal_vps(HEVCContext *s)
315 {
316 int i,j;
317 GetBitContext *gb = &s->HEVClc.gb;
318 int vps_id = 0;
319 HEVCVPS *vps;
320 AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
321
322 if (!vps_buf)
323 return AVERROR(ENOMEM);
324 vps = (HEVCVPS*)vps_buf->data;
325
326 av_log(s->avctx, AV_LOG_DEBUG, "Decoding VPS\n");
327
328 vps_id = get_bits(gb, 4);
329 if (vps_id >= MAX_VPS_COUNT) {
330 av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
331 goto err;
332 }
333
334 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
335 av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
336 goto err;
337 }
338
339 vps->vps_max_layers = get_bits(gb, 6) + 1;
340 vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
341 vps->vps_temporal_id_nesting_flag = get_bits1(gb);
342
343 if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
344 av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
345 goto err;
346 }
347
348 if (vps->vps_max_sub_layers > MAX_SUB_LAYERS) {
349 av_log(s->avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
350 vps->vps_max_sub_layers);
351 goto err;
352 }
353
354 parse_ptl(s, &vps->ptl, vps->vps_max_sub_layers);
355
356 vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
357
358 i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
359 for (; i < vps->vps_max_sub_layers; i++) {
360 vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
361 vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb);
362 vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1;
363
364 if (vps->vps_max_dec_pic_buffering[i] > MAX_DPB_SIZE) {
365 av_log(s->avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
366 vps->vps_max_dec_pic_buffering[i] - 1);
367 goto err;
368 }
369 if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
370 av_log(s->avctx, AV_LOG_ERROR, "vps_max_num_reorder_pics out of range: %d\n",
371 vps->vps_num_reorder_pics[i]);
372 goto err;
373 }
374 }
375
376 vps->vps_max_layer_id = get_bits(gb, 6);
377 vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
378 for (i = 1; i < vps->vps_num_layer_sets; i++)
379 for (j = 0; j <= vps->vps_max_layer_id; j++)
380 skip_bits(gb, 1); // layer_id_included_flag[i][j]
381
382 vps->vps_timing_info_present_flag = get_bits1(gb);
383 if (vps->vps_timing_info_present_flag) {
384 vps->vps_num_units_in_tick = get_bits_long(gb, 32);
385 vps->vps_time_scale = get_bits_long(gb, 32);
386 vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
387 if (vps->vps_poc_proportional_to_timing_flag)
388 vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
389 vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
390 for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
391 int common_inf_present = 1;
392
393 get_ue_golomb_long(gb); // hrd_layer_set_idx
394 if (i)
395 common_inf_present = get_bits1(gb);
396 decode_hrd(s, common_inf_present, vps->vps_max_sub_layers);
397 }
398 }
399 get_bits1(gb); /* vps_extension_flag */
400
401 av_buffer_unref(&s->vps_list[vps_id]);
402 s->vps_list[vps_id] = vps_buf;
403 return 0;
404
405 err:
406 av_buffer_unref(&vps_buf);
407 return AVERROR_INVALIDDATA;
408 }
409
410 static void decode_vui(HEVCContext *s, HEVCSPS *sps)
411 {
412 VUI *vui = &sps->vui;
413 GetBitContext *gb = &s->HEVClc.gb;
414 int sar_present;
415
416 av_log(s->avctx, AV_LOG_DEBUG, "Decoding VUI\n");
417
418 sar_present = get_bits1(gb);
419 if (sar_present) {
420 uint8_t sar_idx = get_bits(gb, 8);
421 if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
422 vui->sar = vui_sar[sar_idx];
423 else if (sar_idx == 255) {
424 vui->sar.num = get_bits(gb, 16);
425 vui->sar.den = get_bits(gb, 16);
426 } else
427 av_log(s->avctx, AV_LOG_WARNING,
428 "Unknown SAR index: %u.\n", sar_idx);
429 }
430
431 vui->overscan_info_present_flag = get_bits1(gb);
432 if (vui->overscan_info_present_flag)
433 vui->overscan_appropriate_flag = get_bits1(gb);
434
435 vui->video_signal_type_present_flag = get_bits1(gb);
436 if (vui->video_signal_type_present_flag) {
437 vui->video_format = get_bits(gb, 3);
438 vui->video_full_range_flag = get_bits1(gb);
439 vui->colour_description_present_flag = get_bits1(gb);
440 if (vui->video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
441 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
442 if (vui->colour_description_present_flag) {
443 vui->colour_primaries = get_bits(gb, 8);
444 vui->transfer_characteristic = get_bits(gb, 8);
445 vui->matrix_coeffs = get_bits(gb, 8);
446
447 // Set invalid values to "unspecified"
448 if (vui->colour_primaries >= AVCOL_PRI_NB)
449 vui->colour_primaries = AVCOL_PRI_UNSPECIFIED;
450 if (vui->transfer_characteristic >= AVCOL_TRC_NB)
451 vui->transfer_characteristic = AVCOL_TRC_UNSPECIFIED;
452 if (vui->matrix_coeffs >= AVCOL_SPC_NB)
453 vui->matrix_coeffs = AVCOL_SPC_UNSPECIFIED;
454 }
455 }
456
457 vui->chroma_loc_info_present_flag = get_bits1(gb);
458 if (vui->chroma_loc_info_present_flag) {
459 vui->chroma_sample_loc_type_top_field = get_ue_golomb_long(gb);
460 vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_long(gb);
461 }
462
463 vui->neutra_chroma_indication_flag = get_bits1(gb);
464 vui->field_seq_flag = get_bits1(gb);
465 vui->frame_field_info_present_flag = get_bits1(gb);
466
467 vui->default_display_window_flag = get_bits1(gb);
468 if (vui->default_display_window_flag) {
469 //TODO: * 2 is only valid for 420
470 vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * 2;
471 vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * 2;
472 vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * 2;
473 vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * 2;
474
475 if (s->apply_defdispwin &&
476 s->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
477 av_log(s->avctx, AV_LOG_DEBUG,
478 "discarding vui default display window, "
479 "original values are l:%u r:%u t:%u b:%u\n",
480 vui->def_disp_win.left_offset,
481 vui->def_disp_win.right_offset,
482 vui->def_disp_win.top_offset,
483 vui->def_disp_win.bottom_offset);
484
485 vui->def_disp_win.left_offset =
486 vui->def_disp_win.right_offset =
487 vui->def_disp_win.top_offset =
488 vui->def_disp_win.bottom_offset = 0;
489 }
490 }
491
492 vui->vui_timing_info_present_flag = get_bits1(gb);
493 if (vui->vui_timing_info_present_flag) {
494 vui->vui_num_units_in_tick = get_bits(gb, 32);
495 vui->vui_time_scale = get_bits(gb, 32);
496 vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
497 if (vui->vui_poc_proportional_to_timing_flag)
498 vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
499 vui->vui_hrd_parameters_present_flag = get_bits1(gb);
500 if (vui->vui_hrd_parameters_present_flag)
501 decode_hrd(s, 1, sps->max_sub_layers);
502 }
503
504 vui->bitstream_restriction_flag = get_bits1(gb);
505 if (vui->bitstream_restriction_flag) {
506 vui->tiles_fixed_structure_flag = get_bits1(gb);
507 vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
508 vui->restricted_ref_pic_lists_flag = get_bits1(gb);
509 vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb);
510 vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb);
511 vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb);
512 vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb);
513 vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb);
514 }
515 }
516
517 static void set_default_scaling_list_data(ScalingList *sl)
518 {
519 int matrixId;
520
521 for (matrixId = 0; matrixId < 6; matrixId++) {
522 // 4x4 default is 16
523 memset(sl->sl[0][matrixId], 16, 16);
524 sl->sl_dc[0][matrixId] = 16; // default for 16x16
525 sl->sl_dc[1][matrixId] = 16; // default for 32x32
526 }
527 memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
528 memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
529 memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
530 memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
531 memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
532 memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
533 memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
534 memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
535 memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
536 memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
537 memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
538 memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
539 memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
540 memcpy(sl->sl[3][1], default_scaling_list_inter, 64);
541 }
542
543 static int scaling_list_data(HEVCContext *s, ScalingList *sl)
544 {
545 GetBitContext *gb = &s->HEVClc.gb;
546 uint8_t scaling_list_pred_mode_flag[4][6];
547 int32_t scaling_list_dc_coef[2][6];
548 int size_id, matrix_id, i, pos, delta;
549
550 for (size_id = 0; size_id < 4; size_id++)
551 for (matrix_id = 0; matrix_id < (size_id == 3 ? 2 : 6); matrix_id++) {
552 scaling_list_pred_mode_flag[size_id][matrix_id] = get_bits1(gb);
553 if (!scaling_list_pred_mode_flag[size_id][matrix_id]) {
554 delta = get_ue_golomb_long(gb);
555 /* Only need to handle non-zero delta. Zero means default,
556 * which should already be in the arrays. */
557 if (delta) {
558 // Copy from previous array.
559 if (matrix_id - delta < 0) {
560 av_log(s->avctx, AV_LOG_ERROR,
561 "Invalid delta in scaling list data: %d.\n", delta);
562 return AVERROR_INVALIDDATA;
563 }
564
565 memcpy(sl->sl[size_id][matrix_id],
566 sl->sl[size_id][matrix_id - delta],
567 size_id > 0 ? 64 : 16);
568 if (size_id > 1)
569 sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
570 }
571 } else {
572 int next_coef, coef_num;
573 int32_t scaling_list_delta_coef;
574
575 next_coef = 8;
576 coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
577 if (size_id > 1) {
578 scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
579 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
580 sl->sl_dc[size_id - 2][matrix_id] = next_coef;
581 }
582 for (i = 0; i < coef_num; i++) {
583 if (size_id == 0)
584 pos = 4 * ff_hevc_diag_scan4x4_y[i] +
585 ff_hevc_diag_scan4x4_x[i];
586 else
587 pos = 8 * ff_hevc_diag_scan8x8_y[i] +
588 ff_hevc_diag_scan8x8_x[i];
589
590 scaling_list_delta_coef = get_se_golomb(gb);
591 next_coef = (next_coef + scaling_list_delta_coef + 256) % 256;
592 sl->sl[size_id][matrix_id][pos] = next_coef;
593 }
594 }
595 }
596
597 return 0;
598 }
599
600 int ff_hevc_decode_nal_sps(HEVCContext *s)
601 {
602 const AVPixFmtDescriptor *desc;
603 GetBitContext *gb = &s->HEVClc.gb;
604 int ret = 0;
605 int sps_id = 0;
606 int log2_diff_max_min_transform_block_size;
607 int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
608 int i;
609
610 HEVCSPS *sps;
611 AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
612
613 if (!sps_buf)
614 return AVERROR(ENOMEM);
615 sps = (HEVCSPS*)sps_buf->data;
616
617 av_log(s->avctx, AV_LOG_DEBUG, "Decoding SPS\n");
618
619 // Coded parameters
620
621 sps->vps_id = get_bits(gb, 4);
622 if (sps->vps_id >= MAX_VPS_COUNT) {
623 av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
624 ret = AVERROR_INVALIDDATA;
625 goto err;
626 }
627
628 sps->max_sub_layers = get_bits(gb, 3) + 1;
629 if (sps->max_sub_layers > MAX_SUB_LAYERS) {
630 av_log(s->avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
631 sps->max_sub_layers);
632 ret = AVERROR_INVALIDDATA;
633 goto err;
634 }
635
636 skip_bits1(gb); // temporal_id_nesting_flag
637
638 parse_ptl(s, &sps->ptl, sps->max_sub_layers);
639
640 sps_id = get_ue_golomb_long(gb);
641 if (sps_id >= MAX_SPS_COUNT) {
642 av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", sps_id);
643 ret = AVERROR_INVALIDDATA;
644 goto err;
645 }
646
647 sps->chroma_format_idc = get_ue_golomb_long(gb);
648 if (sps->chroma_format_idc != 1) {
649 avpriv_report_missing_feature(s->avctx, "chroma_format_idc != 1\n");
650 ret = AVERROR_PATCHWELCOME;
651 goto err;
652 }
653
654 if (sps->chroma_format_idc == 3)
655 sps->separate_colour_plane_flag = get_bits1(gb);
656
657 sps->width = get_ue_golomb_long(gb);
658 sps->height = get_ue_golomb_long(gb);
659 if ((ret = av_image_check_size(sps->width,
660 sps->height, 0, s->avctx)) < 0)
661 goto err;
662
663 if (get_bits1(gb)) { // pic_conformance_flag
664 //TODO: * 2 is only valid for 420
665 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * 2;
666 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * 2;
667 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * 2;
668 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * 2;
669
670 if (s->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
671 av_log(s->avctx, AV_LOG_DEBUG,
672 "discarding sps conformance window, "
673 "original values are l:%u r:%u t:%u b:%u\n",
674 sps->pic_conf_win.left_offset,
675 sps->pic_conf_win.right_offset,
676 sps->pic_conf_win.top_offset,
677 sps->pic_conf_win.bottom_offset);
678
679 sps->pic_conf_win.left_offset =
680 sps->pic_conf_win.right_offset =
681 sps->pic_conf_win.top_offset =
682 sps->pic_conf_win.bottom_offset = 0;
683 }
684 sps->output_window = sps->pic_conf_win;
685 }
686
687 sps->bit_depth = get_ue_golomb_long(gb) + 8;
688 bit_depth_chroma = get_ue_golomb_long(gb) + 8;
689 if (bit_depth_chroma != sps->bit_depth) {
690 av_log(s->avctx, AV_LOG_ERROR,
691 "Luma bit depth (%d) is different from chroma bit depth (%d), "
692 "this is unsupported.\n",
693 sps->bit_depth, bit_depth_chroma);
694 ret = AVERROR_INVALIDDATA;
695 goto err;
696 }
697
698 if (sps->chroma_format_idc == 1) {
699 switch (sps->bit_depth) {
700 case 8: sps->pix_fmt = AV_PIX_FMT_YUV420P; break;
701 case 9: sps->pix_fmt = AV_PIX_FMT_YUV420P9; break;
702 case 10: sps->pix_fmt = AV_PIX_FMT_YUV420P10; break;
703 default:
704 av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
705 sps->bit_depth);
706 ret = AVERROR_PATCHWELCOME;
707 goto err;
708 }
709 } else {
710 av_log(s->avctx, AV_LOG_ERROR,
711 "non-4:2:0 support is currently unspecified.\n");
712 return AVERROR_PATCHWELCOME;
713 }
714
715 desc = av_pix_fmt_desc_get(sps->pix_fmt);
716 if (!desc) {
717 ret = AVERROR(EINVAL);
718 goto err;
719 }
720
721 sps->hshift[0] = sps->vshift[0] = 0;
722 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
723 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
724
725 sps->pixel_shift = sps->bit_depth > 8;
726
727 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
728 if (sps->log2_max_poc_lsb > 16) {
729 av_log(s->avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
730 sps->log2_max_poc_lsb - 4);
731 ret = AVERROR_INVALIDDATA;
732 goto err;
733 }
734
735 sublayer_ordering_info = get_bits1(gb);
736 start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
737 for (i = start; i < sps->max_sub_layers; i++) {
738 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
739 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
740 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
741 if (sps->temporal_layer[i].max_dec_pic_buffering > MAX_DPB_SIZE) {
742 av_log(s->avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
743 sps->temporal_layer[i].max_dec_pic_buffering - 1);
744 ret = AVERROR_INVALIDDATA;
745 goto err;
746 }
747 if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
748 av_log(s->avctx, AV_LOG_ERROR, "sps_max_num_reorder_pics out of range: %d\n",
749 sps->temporal_layer[i].num_reorder_pics);
750 ret = AVERROR_INVALIDDATA;
751 goto err;
752 }
753 }
754
755 if (!sublayer_ordering_info) {
756 for (i = 0; i < start; i++) {
757 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
758 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
759 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
760 }
761 }
762
763 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
764 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
765 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
766 log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
767 sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
768 sps->log2_min_tb_size;
769
770 if (sps->log2_min_tb_size >= sps->log2_min_cb_size) {
771 av_log(s->avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
772 ret = AVERROR_INVALIDDATA;
773 goto err;
774 }
775 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
776 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
777
778 sps->scaling_list_enable_flag = get_bits1(gb);
779 if (sps->scaling_list_enable_flag) {
780 set_default_scaling_list_data(&sps->scaling_list);
781
782 if (get_bits1(gb)) {
783 ret = scaling_list_data(s, &sps->scaling_list);
784 if (ret < 0)
785 goto err;
786 }
787 }
788
789 sps->amp_enabled_flag = get_bits1(gb);
790 sps->sao_enabled = get_bits1(gb);
791
792 sps->pcm_enabled_flag = get_bits1(gb);
793 if (sps->pcm_enabled_flag) {
794 int pcm_bit_depth_chroma;
795 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
796 pcm_bit_depth_chroma = get_bits(gb, 4) + 1;
797 if (pcm_bit_depth_chroma != sps->pcm.bit_depth) {
798 av_log(s->avctx, AV_LOG_ERROR,
799 "PCM Luma bit depth (%d) is different from PCM chroma"
800 "bit depth (%d), this is unsupported.\n",
801 sps->pcm.bit_depth, pcm_bit_depth_chroma);
802 ret = AVERROR_INVALIDDATA;
803 goto err;
804 }
805
806 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
807 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
808 get_ue_golomb_long(gb);
809 if (sps->pcm.bit_depth > sps->bit_depth) {
810 av_log(s->avctx, AV_LOG_ERROR,
811 "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
812 sps->pcm.bit_depth, sps->bit_depth);
813 ret = AVERROR_INVALIDDATA;
814 goto err;
815 }
816
817 sps->pcm.loop_filter_disable_flag = get_bits1(gb);
818 }
819
820 sps->nb_st_rps = get_ue_golomb_long(gb);
821 if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {
822 av_log(s->avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
823 sps->nb_st_rps);
824 ret = AVERROR_INVALIDDATA;
825 goto err;
826 }
827 for (i = 0; i < sps->nb_st_rps; i++) {
828 if ((ret = ff_hevc_decode_short_term_rps(s, &sps->st_rps[i],
829 sps, 0)) < 0)
830 goto err;
831 }
832
833 sps->long_term_ref_pics_present_flag = get_bits1(gb);
834 if (sps->long_term_ref_pics_present_flag) {
835 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
836 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
837 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
838 sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
839 }
840 }
841
842 sps->sps_temporal_mvp_enabled_flag = get_bits1(gb);
843 sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
844 sps->vui.sar = (AVRational){0, 1};
845 vui_present = get_bits1(gb);
846 if (vui_present)
847 decode_vui(s, sps);
848 skip_bits1(gb); // sps_extension_flag
849
850 if (s->apply_defdispwin) {
851 sps->output_window.left_offset += sps->vui.def_disp_win.left_offset;
852 sps->output_window.right_offset += sps->vui.def_disp_win.right_offset;
853 sps->output_window.top_offset += sps->vui.def_disp_win.top_offset;
854 sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
855 }
856 if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&
857 !(s->avctx->flags & CODEC_FLAG_UNALIGNED)) {
858 sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));
859 av_log(s->avctx, AV_LOG_WARNING, "Reducing left output window to %d "
860 "chroma samples to preserve alignment.\n",
861 sps->output_window.left_offset);
862 }
863 sps->output_width = sps->width -
864 (sps->output_window.left_offset + sps->output_window.right_offset);
865 sps->output_height = sps->height -
866 (sps->output_window.top_offset + sps->output_window.bottom_offset);
867 if (sps->output_width <= 0 || sps->output_height <= 0) {
868 av_log(s->avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n",
869 sps->output_width, sps->output_height);
870 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
871 ret = AVERROR_INVALIDDATA;
872 goto err;
873 }
874 av_log(s->avctx, AV_LOG_WARNING,
875 "Displaying the whole video surface.\n");
876 sps->pic_conf_win.left_offset =
877 sps->pic_conf_win.right_offset =
878 sps->pic_conf_win.top_offset =
879 sps->pic_conf_win.bottom_offset = 0;
880 sps->output_width = sps->width;
881 sps->output_height = sps->height;
882 }
883
884 // Inferred parameters
885 sps->log2_ctb_size = sps->log2_min_cb_size +
886 sps->log2_diff_max_min_coding_block_size;
887 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
888
889 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
890 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
891 sps->ctb_size = sps->ctb_width * sps->ctb_height;
892
893 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
894 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
895 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
896 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
897 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
898 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
899
900 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
901
902 if (sps->width & ((1 << sps->log2_min_cb_size) - 1) ||
903 sps->height & ((1 << sps->log2_min_cb_size) - 1)) {
904 av_log(s->avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
905 goto err;
906 }
907
908 if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) {
909 av_log(s->avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
910 goto err;
911 }
912 if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
913 av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
914 sps->max_transform_hierarchy_depth_inter);
915 goto err;
916 }
917 if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
918 av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
919 sps->max_transform_hierarchy_depth_intra);
920 goto err;
921 }
922 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
923 av_log(s->avctx, AV_LOG_ERROR,
924 "max transform block size out of range: %d\n",
925 sps->log2_max_trafo_size);
926 goto err;
927 }
928
929 if (s->avctx->debug & FF_DEBUG_BITSTREAM) {
930 av_log(s->avctx, AV_LOG_DEBUG,
931 "Parsed SPS: id %d; coded wxh: %dx%d; "
932 "cropped wxh: %dx%d; pix_fmt: %s.\n",
933 sps_id, sps->width, sps->height,
934 sps->output_width, sps->output_height,
935 av_get_pix_fmt_name(sps->pix_fmt));
936 }
937
938 /* check if this is a repeat of an already parsed SPS, then keep the
939 * original one.
940 * otherwise drop all PPSes that depend on it */
941 if (s->sps_list[sps_id] &&
942 !memcmp(s->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
943 av_buffer_unref(&sps_buf);
944 } else {
945 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
946 if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == sps_id)
947 av_buffer_unref(&s->pps_list[i]);
948 }
949 av_buffer_unref(&s->sps_list[sps_id]);
950 s->sps_list[sps_id] = sps_buf;
951 }
952
953 return 0;
954
955 err:
956 av_buffer_unref(&sps_buf);
957 return ret;
958 }
959
960 static void hevc_pps_free(void *opaque, uint8_t *data)
961 {
962 HEVCPPS *pps = (HEVCPPS*)data;
963
964 av_freep(&pps->column_width);
965 av_freep(&pps->row_height);
966 av_freep(&pps->col_bd);
967 av_freep(&pps->row_bd);
968 av_freep(&pps->col_idxX);
969 av_freep(&pps->ctb_addr_rs_to_ts);
970 av_freep(&pps->ctb_addr_ts_to_rs);
971 av_freep(&pps->tile_pos_rs);
972 av_freep(&pps->tile_id);
973 av_freep(&pps->min_cb_addr_zs);
974 av_freep(&pps->min_tb_addr_zs);
975
976 av_freep(&pps);
977 }
978
979 int ff_hevc_decode_nal_pps(HEVCContext *s)
980 {
981 GetBitContext *gb = &s->HEVClc.gb;
982 HEVCSPS *sps = NULL;
983 int pic_area_in_ctbs, pic_area_in_min_cbs, pic_area_in_min_tbs;
984 int log2_diff_ctb_min_tb_size;
985 int i, j, x, y, ctb_addr_rs, tile_id;
986 int ret = 0;
987 int pps_id = 0;
988
989 AVBufferRef *pps_buf;
990 HEVCPPS *pps = av_mallocz(sizeof(*pps));
991
992 if (!pps)
993 return AVERROR(ENOMEM);
994
995 pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
996 hevc_pps_free, NULL, 0);
997 if (!pps_buf) {
998 av_freep(&pps);
999 return AVERROR(ENOMEM);
1000 }
1001
1002 av_log(s->avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1003
1004 // Default values
1005 pps->loop_filter_across_tiles_enabled_flag = 1;
1006 pps->num_tile_columns = 1;
1007 pps->num_tile_rows = 1;
1008 pps->uniform_spacing_flag = 1;
1009 pps->disable_dbf = 0;
1010 pps->beta_offset = 0;
1011 pps->tc_offset = 0;
1012
1013 // Coded parameters
1014 pps_id = get_ue_golomb_long(gb);
1015 if (pps_id >= MAX_PPS_COUNT) {
1016 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1017 ret = AVERROR_INVALIDDATA;
1018 goto err;
1019 }
1020 pps->sps_id = get_ue_golomb_long(gb);
1021 if (pps->sps_id >= MAX_SPS_COUNT) {
1022 av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1023 ret = AVERROR_INVALIDDATA;
1024 goto err;
1025 }
1026 if (!s->sps_list[pps->sps_id]) {
1027 av_log(s->avctx, AV_LOG_ERROR, "SPS does not exist \n");
1028 ret = AVERROR_INVALIDDATA;
1029 goto err;
1030 }
1031 sps = (HEVCSPS *)s->sps_list[pps->sps_id]->data;
1032
1033 pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
1034 pps->output_flag_present_flag = get_bits1(gb);
1035 pps->num_extra_slice_header_bits = get_bits(gb, 3);
1036
1037 pps->sign_data_hiding_flag = get_bits1(gb);
1038
1039 pps->cabac_init_present_flag = get_bits1(gb);
1040
1041 pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1;
1042 pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1;
1043
1044 pps->pic_init_qp_minus26 = get_se_golomb(gb);
1045
1046 pps->constrained_intra_pred_flag = get_bits1(gb);
1047 pps->transform_skip_enabled_flag = get_bits1(gb);
1048
1049 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
1050 pps->diff_cu_qp_delta_depth = 0;
1051 if (pps->cu_qp_delta_enabled_flag)
1052 pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
1053
1054 pps->cb_qp_offset = get_se_golomb(gb);
1055 if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1056 av_log(s->avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1057 pps->cb_qp_offset);
1058 ret = AVERROR_INVALIDDATA;
1059 goto err;
1060 }
1061 pps->cr_qp_offset = get_se_golomb(gb);
1062 if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1063 av_log(s->avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1064 pps->cr_qp_offset);
1065 ret = AVERROR_INVALIDDATA;
1066 goto err;
1067 }
1068 pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
1069
1070 pps->weighted_pred_flag = get_bits1(gb);
1071 pps->weighted_bipred_flag = get_bits1(gb);
1072
1073 pps->transquant_bypass_enable_flag = get_bits1(gb);
1074 pps->tiles_enabled_flag = get_bits1(gb);
1075 pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
1076
1077 if (pps->tiles_enabled_flag) {
1078 pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
1079 pps->num_tile_rows = get_ue_golomb_long(gb) + 1;
1080 if (pps->num_tile_columns == 0 ||
1081 pps->num_tile_columns >= sps->width) {
1082 av_log(s->avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1083 pps->num_tile_columns - 1);
1084 ret = AVERROR_INVALIDDATA;
1085 goto err;
1086 }
1087 if (pps->num_tile_rows == 0 ||
1088 pps->num_tile_rows >= sps->height) {
1089 av_log(s->avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1090 pps->num_tile_rows - 1);
1091 ret = AVERROR_INVALIDDATA;
1092 goto err;
1093 }
1094
1095 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1096 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1097 if (!pps->column_width || !pps->row_height) {
1098 ret = AVERROR(ENOMEM);
1099 goto err;
1100 }
1101
1102 pps->uniform_spacing_flag = get_bits1(gb);
1103 if (!pps->uniform_spacing_flag) {
1104 int sum = 0;
1105 for (i = 0; i < pps->num_tile_columns - 1; i++) {
1106 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1107 sum += pps->column_width[i];
1108 }
1109 if (sum >= sps->ctb_width) {
1110 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1111 ret = AVERROR_INVALIDDATA;
1112 goto err;
1113 }
1114 pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1115
1116 sum = 0;
1117 for (i = 0; i < pps->num_tile_rows - 1; i++) {
1118 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1119 sum += pps->row_height[i];
1120 }
1121 if (sum >= sps->ctb_height) {
1122 av_log(s->avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1123 ret = AVERROR_INVALIDDATA;
1124 goto err;
1125 }
1126 pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1127 }
1128 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
1129 }
1130
1131 pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1132
1133 pps->deblocking_filter_control_present_flag = get_bits1(gb);
1134 if (pps->deblocking_filter_control_present_flag) {
1135 pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
1136 pps->disable_dbf = get_bits1(gb);
1137 if (!pps->disable_dbf) {
1138 pps->beta_offset = get_se_golomb(gb) * 2;
1139 pps->tc_offset = get_se_golomb(gb) * 2;
1140 if (pps->beta_offset/2 < -6 || pps->beta_offset/2 > 6) {
1141 av_log(s->avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1142 pps->beta_offset/2);
1143 ret = AVERROR_INVALIDDATA;
1144 goto err;
1145 }
1146 if (pps->tc_offset/2 < -6 || pps->tc_offset/2 > 6) {
1147 av_log(s->avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1148 pps->tc_offset/2);
1149 ret = AVERROR_INVALIDDATA;
1150 goto err;
1151 }
1152 }
1153 }
1154
1155 pps->scaling_list_data_present_flag = get_bits1(gb);
1156 if (pps->scaling_list_data_present_flag) {
1157 set_default_scaling_list_data(&pps->scaling_list);
1158 ret = scaling_list_data(s, &pps->scaling_list);
1159 if (ret < 0)
1160 goto err;
1161 }
1162 pps->lists_modification_present_flag = get_bits1(gb);
1163 pps->log2_parallel_merge_level = get_ue_golomb_long(gb) + 2;
1164 if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
1165 av_log(s->avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1166 pps->log2_parallel_merge_level - 2);
1167 ret = AVERROR_INVALIDDATA;
1168 goto err;
1169 }
1170
1171 pps->slice_header_extension_present_flag = get_bits1(gb);
1172 skip_bits1(gb); // pps_extension_flag
1173
1174 // Inferred parameters
1175 pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1176 pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1177 pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1178 if (!pps->col_bd || !pps->row_bd || !pps->col_idxX) {
1179 ret = AVERROR(ENOMEM);
1180 goto err;
1181 }
1182
1183 if (pps->uniform_spacing_flag) {
1184 if (!pps->column_width) {
1185 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1186 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1187 }
1188 if (!pps->column_width || !pps->row_height) {
1189 ret = AVERROR(ENOMEM);
1190 goto err;
1191 }
1192
1193 for (i = 0; i < pps->num_tile_columns; i++) {
1194 pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1195 (i * sps->ctb_width) / pps->num_tile_columns;
1196 }
1197
1198 for (i = 0; i < pps->num_tile_rows; i++) {
1199 pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1200 (i * sps->ctb_height) / pps->num_tile_rows;
1201 }
1202 }
1203
1204 pps->col_bd[0] = 0;
1205 for (i = 0; i < pps->num_tile_columns; i++)
1206 pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1207
1208 pps->row_bd[0] = 0;
1209 for (i = 0; i < pps->num_tile_rows; i++)
1210 pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1211
1212 for (i = 0, j = 0; i < sps->ctb_width; i++) {
1213 if (i > pps->col_bd[j])
1214 j++;
1215 pps->col_idxX[i] = j;
1216 }
1217
1218 /**
1219 * 6.5
1220 */
1221 pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1222 pic_area_in_min_cbs = sps->min_cb_width * sps->min_cb_height;
1223 pic_area_in_min_tbs = sps->min_tb_width * sps->min_tb_height;
1224
1225 pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1226 pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1227 pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1228 pps->min_cb_addr_zs = av_malloc_array(pic_area_in_min_cbs, sizeof(*pps->min_cb_addr_zs));
1229 pps->min_tb_addr_zs = av_malloc_array(pic_area_in_min_tbs, sizeof(*pps->min_tb_addr_zs));
1230 if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1231 !pps->tile_id || !pps->min_cb_addr_zs || !pps->min_tb_addr_zs) {
1232 ret = AVERROR(ENOMEM);
1233 goto err;
1234 }
1235
1236 for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1237 int tb_x = ctb_addr_rs % sps->ctb_width;
1238 int tb_y = ctb_addr_rs / sps->ctb_width;
1239 int tile_x = 0;
1240 int tile_y = 0;
1241 int val = 0;
1242
1243 for (i = 0; i < pps->num_tile_columns; i++) {
1244 if (tb_x < pps->col_bd[i + 1]) {
1245 tile_x = i;
1246 break;
1247 }
1248 }
1249
1250 for (i = 0; i < pps->num_tile_rows; i++) {
1251 if (tb_y < pps->row_bd[i + 1]) {
1252 tile_y = i;
1253 break;
1254 }
1255 }
1256
1257 for (i = 0; i < tile_x; i++)
1258 val += pps->row_height[tile_y] * pps->column_width[i];
1259 for (i = 0; i < tile_y; i++)
1260 val += sps->ctb_width * pps->row_height[i];
1261
1262 val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1263 tb_x - pps->col_bd[tile_x];
1264
1265 pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1266 pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1267 }
1268
1269 for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1270 for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1271 for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1272 for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1273 pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1274
1275 pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1276 if (!pps->tile_pos_rs) {
1277 ret = AVERROR(ENOMEM);
1278 goto err;
1279 }
1280
1281 for (j = 0; j < pps->num_tile_rows; j++)
1282 for (i = 0; i < pps->num_tile_columns; i++)
1283 pps->tile_pos_rs[j * pps->num_tile_columns + i] = pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1284
1285 for (y = 0; y < sps->min_cb_height; y++) {
1286 for (x = 0; x < sps->min_cb_width; x++) {
1287 int tb_x = x >> sps->log2_diff_max_min_coding_block_size;
1288 int tb_y = y >> sps->log2_diff_max_min_coding_block_size;
1289 int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1290 int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1291 (sps->log2_diff_max_min_coding_block_size * 2);
1292 for (i = 0; i < sps->log2_diff_max_min_coding_block_size; i++) {
1293 int m = 1 << i;
1294 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1295 }
1296 pps->min_cb_addr_zs[y * sps->min_cb_width + x] = val;
1297 }
1298 }
1299
1300 log2_diff_ctb_min_tb_size = sps->log2_ctb_size - sps->log2_min_tb_size;
1301 for (y = 0; y < sps->min_tb_height; y++) {
1302 for (x = 0; x < sps->min_tb_width; x++) {
1303 int tb_x = x >> log2_diff_ctb_min_tb_size;
1304 int tb_y = y >> log2_diff_ctb_min_tb_size;
1305 int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1306 int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1307 (log2_diff_ctb_min_tb_size * 2);
1308 for (i = 0; i < log2_diff_ctb_min_tb_size; i++) {
1309 int m = 1 << i;
1310 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1311 }
1312 pps->min_tb_addr_zs[y * sps->min_tb_width + x] = val;
1313 }
1314 }
1315
1316 av_buffer_unref(&s->pps_list[pps_id]);
1317 s->pps_list[pps_id] = pps_buf;
1318
1319 return 0;
1320
1321 err:
1322 av_buffer_unref(&pps_buf);
1323 return ret;
1324 }