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