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