hevc: Print the non-supported chroma_format_idc
[libav.git] / libavcodec / hevc_ps.c
CommitLineData
064698d3
GM
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
31static 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
42static 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
53static 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
b11acd57 73static void remove_pps(HEVCParamSets *s, int id)
df528b11
AK
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
b11acd57 80static void remove_sps(HEVCParamSets *s, int id)
df528b11
AK
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
b11acd57 95static void remove_vps(HEVCParamSets *s, int id)
df528b11
AK
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
fd124d83
AK
109int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
110 ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
064698d3 111{
064698d3
GM
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
064698d3
GM
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) {
838740e6 129 unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
064698d3 130 if (delta_idx > sps->nb_st_rps) {
fd124d83 131 av_log(avctx, AV_LOG_ERROR,
064698d3
GM
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) {
fd124d83 203 av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
064698d3
GM
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
2d18aaa1 228
fd124d83
AK
229static void decode_profile_tier_level(GetBitContext *gb, AVCodecContext *avctx,
230 PTLCommon *ptl)
064698d3 231{
2d18aaa1 232 int i;
2d18aaa1
GC
233
234 ptl->profile_space = get_bits(gb, 2);
235 ptl->tier_flag = get_bits1(gb);
236 ptl->profile_idc = get_bits(gb, 5);
78d0b7f3 237 if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
fd124d83 238 av_log(avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
78d0b7f3 239 else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
fd124d83 240 av_log(avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
78d0b7f3 241 else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_STILL_PICTURE)
fd124d83 242 av_log(avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
78d0b7f3 243 else
fd124d83 244 av_log(avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
064698d3 245
064698d3 246 for (i = 0; i < 32; i++)
2d18aaa1
GC
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
67bbaed5
GM
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]
2d18aaa1
GC
256}
257
fd124d83
AK
258static void parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
259 PTL *ptl, int max_num_sub_layers)
2d18aaa1
GC
260{
261 int i;
fd124d83 262 decode_profile_tier_level(gb, avctx, &ptl->general_ptl);
2d18aaa1 263 ptl->general_ptl.level_idc = get_bits(gb, 8);
064698d3 264
064698d3
GM
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++)
2d18aaa1 271 skip_bits(gb, 2); // reserved_zero_2bits[i]
064698d3 272 for (i = 0; i < max_num_sub_layers - 1; i++) {
2d18aaa1 273 if (ptl->sub_layer_profile_present_flag[i])
fd124d83 274 decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]);
064698d3 275 if (ptl->sub_layer_level_present_flag[i])
2d18aaa1 276 ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
064698d3 277 }
064698d3
GM
278}
279
fd124d83 280static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
064698d3
GM
281 int subpic_params_present)
282{
064698d3
GM
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
fd124d83 297static void decode_hrd(GetBitContext *gb, int common_inf_present,
064698d3
GM
298 int max_sublayers)
299{
064698d3
GM
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;
838740e6 332 unsigned int nb_cpb = 1;
064698d3
GM
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)
fd124d83 347 decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
064698d3 348 if (vcl_params_present)
fd124d83 349 decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
064698d3
GM
350 }
351}
352
b11acd57
AK
353int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
354 HEVCParamSets *ps)
064698d3
GM
355{
356 int i,j;
064698d3
GM
357 int vps_id = 0;
358 HEVCVPS *vps;
33452aed 359 AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
064698d3 360
33452aed 361 if (!vps_buf)
259cddb1 362 return AVERROR(ENOMEM);
33452aed 363 vps = (HEVCVPS*)vps_buf->data;
064698d3 364
b11acd57 365 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
064698d3
GM
366
367 vps_id = get_bits(gb, 4);
368 if (vps_id >= MAX_VPS_COUNT) {
b11acd57 369 av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
064698d3
GM
370 goto err;
371 }
372
373 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
b11acd57 374 av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
064698d3
GM
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
b11acd57 383 av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
064698d3
GM
384 goto err;
385 }
386
387 if (vps->vps_max_sub_layers > MAX_SUB_LAYERS) {
b11acd57 388 av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
064698d3
GM
389 vps->vps_max_sub_layers);
390 goto err;
391 }
392
b11acd57 393 parse_ptl(gb, avctx, &vps->ptl, vps->vps_max_sub_layers);
2d18aaa1 394
064698d3
GM
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) {
b11acd57 404 av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
064698d3
GM
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) {
b11acd57 409 av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
064698d3 410 vps->vps_num_reorder_pics[i]);
b11acd57 411 if (avctx->err_recognition & AV_EF_EXPLODE)
9b60d919 412 goto err;
064698d3
GM
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);
fd124d83 436 decode_hrd(gb, common_inf_present, vps->vps_max_sub_layers);
064698d3
GM
437 }
438 }
439 get_bits1(gb); /* vps_extension_flag */
440
b11acd57
AK
441 if (ps->vps_list[vps_id] &&
442 !memcmp(ps->vps_list[vps_id]->data, vps_buf->data, vps_buf->size)) {
ce0bc09e
AK
443 av_buffer_unref(&vps_buf);
444 } else {
b11acd57
AK
445 remove_vps(ps, vps_id);
446 ps->vps_list[vps_id] = vps_buf;
ce0bc09e
AK
447 }
448
064698d3
GM
449 return 0;
450
451err:
33452aed 452 av_buffer_unref(&vps_buf);
064698d3
GM
453 return AVERROR_INVALIDDATA;
454}
455
fd124d83
AK
456static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
457 int apply_defdispwin, HEVCSPS *sps)
064698d3
GM
458{
459 VUI *vui = &sps->vui;
064698d3
GM
460 int sar_present;
461
fd124d83 462 av_log(avctx, AV_LOG_DEBUG, "Decoding VUI\n");
064698d3
GM
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
fd124d83 473 av_log(avctx, AV_LOG_WARNING,
064698d3
GM
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);
3ef9b7ab
YN
486 if (vui->video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
487 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
064698d3
GM
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);
885ec924
JE
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;
064698d3
GM
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
fd124d83
AK
521 if (apply_defdispwin &&
522 avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
523 av_log(avctx, AV_LOG_DEBUG,
064698d3
GM
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) {
920c01ad
MN
540 vui->vui_num_units_in_tick = get_bits_long(gb, 32);
541 vui->vui_time_scale = get_bits_long(gb, 32);
064698d3
GM
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)
fd124d83 547 decode_hrd(gb, 1, sps->max_sub_layers);
064698d3
GM
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
563static 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
fd124d83 589static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx, ScalingList *sl)
064698d3 590{
064698d3
GM
591 uint8_t scaling_list_pred_mode_flag[4][6];
592 int32_t scaling_list_dc_coef[2][6];
838740e6 593 int size_id, matrix_id, i, pos;
064698d3
GM
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]) {
838740e6 599 unsigned int delta = get_ue_golomb_long(gb);
064698d3
GM
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.
838740e6 604 if (matrix_id < delta) {
fd124d83 605 av_log(avctx, AV_LOG_ERROR,
064698d3
GM
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
8e373fe0
LB
645static 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
fd124d83
AK
677int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
678 int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
064698d3 679{
4d33873c 680 int ret = 0;
064698d3
GM
681 int log2_diff_max_min_transform_block_size;
682 int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
683 int i;
684
064698d3
GM
685 // Coded parameters
686
687 sps->vps_id = get_bits(gb, 4);
688 if (sps->vps_id >= MAX_VPS_COUNT) {
fd124d83 689 av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
064698d3
GM
690 ret = AVERROR_INVALIDDATA;
691 goto err;
692 }
693
fd124d83
AK
694 if (vps_list && !vps_list[sps->vps_id]) {
695 av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
a7a07cc9
MN
696 sps->vps_id);
697 ret = AVERROR_INVALIDDATA;
698 goto err;
699 }
700
064698d3
GM
701 sps->max_sub_layers = get_bits(gb, 3) + 1;
702 if (sps->max_sub_layers > MAX_SUB_LAYERS) {
fd124d83 703 av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
064698d3
GM
704 sps->max_sub_layers);
705 ret = AVERROR_INVALIDDATA;
706 goto err;
707 }
708
709 skip_bits1(gb); // temporal_id_nesting_flag
2d18aaa1 710
fd124d83 711 parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers);
2d18aaa1 712
fd124d83
AK
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);
064698d3
GM
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) {
32c8d89c
LB
722 avpriv_report_missing_feature(avctx, "chroma_format_idc %d",
723 sps->chroma_format_idc);
064698d3
GM
724 ret = AVERROR_PATCHWELCOME;
725 goto err;
726 }
727
728 if (sps->chroma_format_idc == 3)
729 sps->separate_colour_plane_flag = get_bits1(gb);
730
731 sps->width = get_ue_golomb_long(gb);
732 sps->height = get_ue_golomb_long(gb);
733 if ((ret = av_image_check_size(sps->width,
fd124d83 734 sps->height, 0, avctx)) < 0)
064698d3
GM
735 goto err;
736
737 if (get_bits1(gb)) { // pic_conformance_flag
738 //TODO: * 2 is only valid for 420
739 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * 2;
740 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * 2;
741 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * 2;
742 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * 2;
743
fd124d83
AK
744 if (avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
745 av_log(avctx, AV_LOG_DEBUG,
064698d3
GM
746 "discarding sps conformance window, "
747 "original values are l:%u r:%u t:%u b:%u\n",
748 sps->pic_conf_win.left_offset,
749 sps->pic_conf_win.right_offset,
750 sps->pic_conf_win.top_offset,
751 sps->pic_conf_win.bottom_offset);
752
753 sps->pic_conf_win.left_offset =
754 sps->pic_conf_win.right_offset =
755 sps->pic_conf_win.top_offset =
756 sps->pic_conf_win.bottom_offset = 0;
757 }
758 sps->output_window = sps->pic_conf_win;
759 }
760
761 sps->bit_depth = get_ue_golomb_long(gb) + 8;
762 bit_depth_chroma = get_ue_golomb_long(gb) + 8;
763 if (bit_depth_chroma != sps->bit_depth) {
fd124d83 764 av_log(avctx, AV_LOG_ERROR,
064698d3
GM
765 "Luma bit depth (%d) is different from chroma bit depth (%d), "
766 "this is unsupported.\n",
767 sps->bit_depth, bit_depth_chroma);
768 ret = AVERROR_INVALIDDATA;
769 goto err;
770 }
771
064698d3 772
8e373fe0
LB
773 ret = map_pixel_format(avctx, sps);
774 if (ret < 0)
064698d3 775 goto err;
064698d3
GM
776
777 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
778 if (sps->log2_max_poc_lsb > 16) {
fd124d83 779 av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
064698d3
GM
780 sps->log2_max_poc_lsb - 4);
781 ret = AVERROR_INVALIDDATA;
782 goto err;
783 }
784
785 sublayer_ordering_info = get_bits1(gb);
786 start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
787 for (i = start; i < sps->max_sub_layers; i++) {
788 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
789 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
790 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
791 if (sps->temporal_layer[i].max_dec_pic_buffering > MAX_DPB_SIZE) {
fd124d83 792 av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
064698d3
GM
793 sps->temporal_layer[i].max_dec_pic_buffering - 1);
794 ret = AVERROR_INVALIDDATA;
795 goto err;
796 }
797 if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
fd124d83 798 av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
064698d3 799 sps->temporal_layer[i].num_reorder_pics);
fd124d83 800 if (avctx->err_recognition & AV_EF_EXPLODE ||
9b60d919
KK
801 sps->temporal_layer[i].num_reorder_pics > MAX_DPB_SIZE - 1) {
802 ret = AVERROR_INVALIDDATA;
803 goto err;
804 }
805 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
064698d3
GM
806 }
807 }
808
809 if (!sublayer_ordering_info) {
810 for (i = 0; i < start; i++) {
811 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
812 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
813 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
814 }
815 }
816
817 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
818 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
819 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
820 log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
821 sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
822 sps->log2_min_tb_size;
823
824 if (sps->log2_min_tb_size >= sps->log2_min_cb_size) {
fd124d83 825 av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
064698d3
GM
826 ret = AVERROR_INVALIDDATA;
827 goto err;
828 }
829 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
830 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
831
832 sps->scaling_list_enable_flag = get_bits1(gb);
833 if (sps->scaling_list_enable_flag) {
834 set_default_scaling_list_data(&sps->scaling_list);
835
836 if (get_bits1(gb)) {
fd124d83 837 ret = scaling_list_data(gb, avctx, &sps->scaling_list);
064698d3
GM
838 if (ret < 0)
839 goto err;
840 }
841 }
842
843 sps->amp_enabled_flag = get_bits1(gb);
844 sps->sao_enabled = get_bits1(gb);
845
846 sps->pcm_enabled_flag = get_bits1(gb);
847 if (sps->pcm_enabled_flag) {
064698d3 848 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
5c3fa74b 849 sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
064698d3
GM
850 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
851 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
852 get_ue_golomb_long(gb);
853 if (sps->pcm.bit_depth > sps->bit_depth) {
fd124d83 854 av_log(avctx, AV_LOG_ERROR,
064698d3
GM
855 "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
856 sps->pcm.bit_depth, sps->bit_depth);
857 ret = AVERROR_INVALIDDATA;
858 goto err;
859 }
860
861 sps->pcm.loop_filter_disable_flag = get_bits1(gb);
862 }
863
864 sps->nb_st_rps = get_ue_golomb_long(gb);
865 if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {
fd124d83 866 av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
064698d3
GM
867 sps->nb_st_rps);
868 ret = AVERROR_INVALIDDATA;
869 goto err;
870 }
871 for (i = 0; i < sps->nb_st_rps; i++) {
fd124d83 872 if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
064698d3
GM
873 sps, 0)) < 0)
874 goto err;
875 }
876
877 sps->long_term_ref_pics_present_flag = get_bits1(gb);
878 if (sps->long_term_ref_pics_present_flag) {
879 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
880 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
881 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
882 sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
883 }
884 }
885
886 sps->sps_temporal_mvp_enabled_flag = get_bits1(gb);
887 sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
888 sps->vui.sar = (AVRational){0, 1};
889 vui_present = get_bits1(gb);
890 if (vui_present)
fd124d83 891 decode_vui(gb, avctx, apply_defdispwin, sps);
064698d3
GM
892 skip_bits1(gb); // sps_extension_flag
893
fd124d83 894 if (apply_defdispwin) {
064698d3
GM
895 sps->output_window.left_offset += sps->vui.def_disp_win.left_offset;
896 sps->output_window.right_offset += sps->vui.def_disp_win.right_offset;
897 sps->output_window.top_offset += sps->vui.def_disp_win.top_offset;
898 sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
899 }
900 if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&
fd124d83 901 !(avctx->flags & CODEC_FLAG_UNALIGNED)) {
064698d3 902 sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));
fd124d83 903 av_log(avctx, AV_LOG_WARNING, "Reducing left output window to %d "
064698d3
GM
904 "chroma samples to preserve alignment.\n",
905 sps->output_window.left_offset);
906 }
907 sps->output_width = sps->width -
908 (sps->output_window.left_offset + sps->output_window.right_offset);
909 sps->output_height = sps->height -
910 (sps->output_window.top_offset + sps->output_window.bottom_offset);
911 if (sps->output_width <= 0 || sps->output_height <= 0) {
fd124d83 912 av_log(avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n",
064698d3 913 sps->output_width, sps->output_height);
fd124d83 914 if (avctx->err_recognition & AV_EF_EXPLODE) {
064698d3
GM
915 ret = AVERROR_INVALIDDATA;
916 goto err;
917 }
fd124d83 918 av_log(avctx, AV_LOG_WARNING,
064698d3 919 "Displaying the whole video surface.\n");
5127c00b
AK
920 sps->output_window.left_offset =
921 sps->output_window.right_offset =
922 sps->output_window.top_offset =
923 sps->output_window.bottom_offset = 0;
064698d3
GM
924 sps->output_width = sps->width;
925 sps->output_height = sps->height;
926 }
927
928 // Inferred parameters
929 sps->log2_ctb_size = sps->log2_min_cb_size +
930 sps->log2_diff_max_min_coding_block_size;
931 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
932
933 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
934 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
935 sps->ctb_size = sps->ctb_width * sps->ctb_height;
936
937 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
938 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
939 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
940 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
941 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
942 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
943
944 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
945
946 if (sps->width & ((1 << sps->log2_min_cb_size) - 1) ||
947 sps->height & ((1 << sps->log2_min_cb_size) - 1)) {
fd124d83 948 av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
064698d3
GM
949 goto err;
950 }
951
952 if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) {
fd124d83 953 av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
064698d3
GM
954 goto err;
955 }
956 if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
fd124d83 957 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
064698d3
GM
958 sps->max_transform_hierarchy_depth_inter);
959 goto err;
960 }
961 if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
fd124d83 962 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
064698d3
GM
963 sps->max_transform_hierarchy_depth_intra);
964 goto err;
965 }
966 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
fd124d83 967 av_log(avctx, AV_LOG_ERROR,
064698d3
GM
968 "max transform block size out of range: %d\n",
969 sps->log2_max_trafo_size);
970 goto err;
971 }
972
fd124d83
AK
973 return 0;
974
975err:
b9f76d19 976 return ret < 0 ? ret : AVERROR_INVALIDDATA;
fd124d83
AK
977}
978
b11acd57
AK
979int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
980 HEVCParamSets *ps, int apply_defdispwin)
fd124d83
AK
981{
982 HEVCSPS *sps;
983 AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
984 unsigned int sps_id;
985 int ret;
986
987 if (!sps_buf)
988 return AVERROR(ENOMEM);
989 sps = (HEVCSPS*)sps_buf->data;
990
b11acd57 991 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
fd124d83 992
b11acd57
AK
993 ret = ff_hevc_parse_sps(sps, gb, &sps_id,
994 apply_defdispwin,
995 ps->vps_list, avctx);
fd124d83
AK
996 if (ret < 0) {
997 av_buffer_unref(&sps_buf);
998 return ret;
999 }
1000
b11acd57
AK
1001 if (avctx->debug & FF_DEBUG_BITSTREAM) {
1002 av_log(avctx, AV_LOG_DEBUG,
064698d3
GM
1003 "Parsed SPS: id %d; coded wxh: %dx%d; "
1004 "cropped wxh: %dx%d; pix_fmt: %s.\n",
1005 sps_id, sps->width, sps->height,
1006 sps->output_width, sps->output_height,
1007 av_get_pix_fmt_name(sps->pix_fmt));
1008 }
1009
1010 /* check if this is a repeat of an already parsed SPS, then keep the
1011 * original one.
1012 * otherwise drop all PPSes that depend on it */
b11acd57
AK
1013 if (ps->sps_list[sps_id] &&
1014 !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
064698d3
GM
1015 av_buffer_unref(&sps_buf);
1016 } else {
b11acd57
AK
1017 remove_sps(ps, sps_id);
1018 ps->sps_list[sps_id] = sps_buf;
064698d3
GM
1019 }
1020
1021 return 0;
064698d3
GM
1022}
1023
1024static void hevc_pps_free(void *opaque, uint8_t *data)
1025{
1026 HEVCPPS *pps = (HEVCPPS*)data;
1027
1028 av_freep(&pps->column_width);
1029 av_freep(&pps->row_height);
1030 av_freep(&pps->col_bd);
1031 av_freep(&pps->row_bd);
1032 av_freep(&pps->col_idxX);
1033 av_freep(&pps->ctb_addr_rs_to_ts);
1034 av_freep(&pps->ctb_addr_ts_to_rs);
1035 av_freep(&pps->tile_pos_rs);
1036 av_freep(&pps->tile_id);
064698d3
GM
1037 av_freep(&pps->min_tb_addr_zs);
1038
1039 av_freep(&pps);
1040}
1041
b11acd57
AK
1042int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
1043 HEVCParamSets *ps)
064698d3 1044{
064698d3 1045 HEVCSPS *sps = NULL;
17e9d52c 1046 int pic_area_in_ctbs, pic_area_in_min_tbs;
064698d3
GM
1047 int log2_diff_ctb_min_tb_size;
1048 int i, j, x, y, ctb_addr_rs, tile_id;
4d33873c
VG
1049 int ret = 0;
1050 unsigned int pps_id = 0;
064698d3
GM
1051
1052 AVBufferRef *pps_buf;
1053 HEVCPPS *pps = av_mallocz(sizeof(*pps));
1054
1055 if (!pps)
1056 return AVERROR(ENOMEM);
1057
1058 pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
1059 hevc_pps_free, NULL, 0);
1060 if (!pps_buf) {
1061 av_freep(&pps);
1062 return AVERROR(ENOMEM);
1063 }
1064
b11acd57 1065 av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
064698d3
GM
1066
1067 // Default values
1068 pps->loop_filter_across_tiles_enabled_flag = 1;
1069 pps->num_tile_columns = 1;
1070 pps->num_tile_rows = 1;
1071 pps->uniform_spacing_flag = 1;
1072 pps->disable_dbf = 0;
1073 pps->beta_offset = 0;
1074 pps->tc_offset = 0;
1075
1076 // Coded parameters
1077 pps_id = get_ue_golomb_long(gb);
1078 if (pps_id >= MAX_PPS_COUNT) {
b11acd57 1079 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
064698d3
GM
1080 ret = AVERROR_INVALIDDATA;
1081 goto err;
1082 }
1083 pps->sps_id = get_ue_golomb_long(gb);
1084 if (pps->sps_id >= MAX_SPS_COUNT) {
b11acd57 1085 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
064698d3
GM
1086 ret = AVERROR_INVALIDDATA;
1087 goto err;
1088 }
b11acd57
AK
1089 if (!ps->sps_list[pps->sps_id]) {
1090 av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
064698d3
GM
1091 ret = AVERROR_INVALIDDATA;
1092 goto err;
1093 }
b11acd57 1094 sps = (HEVCSPS *)ps->sps_list[pps->sps_id]->data;
064698d3
GM
1095
1096 pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
1097 pps->output_flag_present_flag = get_bits1(gb);
1098 pps->num_extra_slice_header_bits = get_bits(gb, 3);
1099
1100 pps->sign_data_hiding_flag = get_bits1(gb);
1101
1102 pps->cabac_init_present_flag = get_bits1(gb);
1103
1104 pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1;
1105 pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1;
1106
1107 pps->pic_init_qp_minus26 = get_se_golomb(gb);
1108
1109 pps->constrained_intra_pred_flag = get_bits1(gb);
1110 pps->transform_skip_enabled_flag = get_bits1(gb);
1111
1112 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
1113 pps->diff_cu_qp_delta_depth = 0;
1114 if (pps->cu_qp_delta_enabled_flag)
1115 pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
1116
1117 pps->cb_qp_offset = get_se_golomb(gb);
1118 if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
b11acd57 1119 av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
064698d3
GM
1120 pps->cb_qp_offset);
1121 ret = AVERROR_INVALIDDATA;
1122 goto err;
1123 }
1124 pps->cr_qp_offset = get_se_golomb(gb);
1125 if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
b11acd57 1126 av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
064698d3
GM
1127 pps->cr_qp_offset);
1128 ret = AVERROR_INVALIDDATA;
1129 goto err;
1130 }
1131 pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
1132
1133 pps->weighted_pred_flag = get_bits1(gb);
1134 pps->weighted_bipred_flag = get_bits1(gb);
1135
1136 pps->transquant_bypass_enable_flag = get_bits1(gb);
1137 pps->tiles_enabled_flag = get_bits1(gb);
1138 pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
1139
1140 if (pps->tiles_enabled_flag) {
1141 pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
1142 pps->num_tile_rows = get_ue_golomb_long(gb) + 1;
1143 if (pps->num_tile_columns == 0 ||
1144 pps->num_tile_columns >= sps->width) {
b11acd57 1145 av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
064698d3
GM
1146 pps->num_tile_columns - 1);
1147 ret = AVERROR_INVALIDDATA;
1148 goto err;
1149 }
1150 if (pps->num_tile_rows == 0 ||
1151 pps->num_tile_rows >= sps->height) {
b11acd57 1152 av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
064698d3
GM
1153 pps->num_tile_rows - 1);
1154 ret = AVERROR_INVALIDDATA;
1155 goto err;
1156 }
1157
1158 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1159 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1160 if (!pps->column_width || !pps->row_height) {
1161 ret = AVERROR(ENOMEM);
1162 goto err;
1163 }
1164
1165 pps->uniform_spacing_flag = get_bits1(gb);
1166 if (!pps->uniform_spacing_flag) {
b37e7960 1167 uint64_t sum = 0;
064698d3
GM
1168 for (i = 0; i < pps->num_tile_columns - 1; i++) {
1169 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1170 sum += pps->column_width[i];
1171 }
1172 if (sum >= sps->ctb_width) {
b11acd57 1173 av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
064698d3
GM
1174 ret = AVERROR_INVALIDDATA;
1175 goto err;
1176 }
1177 pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1178
1179 sum = 0;
1180 for (i = 0; i < pps->num_tile_rows - 1; i++) {
1181 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1182 sum += pps->row_height[i];
1183 }
1184 if (sum >= sps->ctb_height) {
b11acd57 1185 av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
064698d3
GM
1186 ret = AVERROR_INVALIDDATA;
1187 goto err;
1188 }
1189 pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1190 }
1191 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
1192 }
1193
1194 pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1195
1196 pps->deblocking_filter_control_present_flag = get_bits1(gb);
1197 if (pps->deblocking_filter_control_present_flag) {
1198 pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
1199 pps->disable_dbf = get_bits1(gb);
1200 if (!pps->disable_dbf) {
1201 pps->beta_offset = get_se_golomb(gb) * 2;
1202 pps->tc_offset = get_se_golomb(gb) * 2;
1203 if (pps->beta_offset/2 < -6 || pps->beta_offset/2 > 6) {
b11acd57 1204 av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
064698d3
GM
1205 pps->beta_offset/2);
1206 ret = AVERROR_INVALIDDATA;
1207 goto err;
1208 }
1209 if (pps->tc_offset/2 < -6 || pps->tc_offset/2 > 6) {
b11acd57 1210 av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
064698d3
GM
1211 pps->tc_offset/2);
1212 ret = AVERROR_INVALIDDATA;
1213 goto err;
1214 }
1215 }
1216 }
1217
1218 pps->scaling_list_data_present_flag = get_bits1(gb);
1219 if (pps->scaling_list_data_present_flag) {
1220 set_default_scaling_list_data(&pps->scaling_list);
b11acd57 1221 ret = scaling_list_data(gb, avctx, &pps->scaling_list);
064698d3
GM
1222 if (ret < 0)
1223 goto err;
1224 }
1225 pps->lists_modification_present_flag = get_bits1(gb);
1226 pps->log2_parallel_merge_level = get_ue_golomb_long(gb) + 2;
1227 if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
b11acd57 1228 av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
064698d3
GM
1229 pps->log2_parallel_merge_level - 2);
1230 ret = AVERROR_INVALIDDATA;
1231 goto err;
1232 }
1233
1234 pps->slice_header_extension_present_flag = get_bits1(gb);
1235 skip_bits1(gb); // pps_extension_flag
1236
1237 // Inferred parameters
1238 pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1239 pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1240 pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1241 if (!pps->col_bd || !pps->row_bd || !pps->col_idxX) {
1242 ret = AVERROR(ENOMEM);
1243 goto err;
1244 }
1245
1246 if (pps->uniform_spacing_flag) {
1247 if (!pps->column_width) {
1248 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1249 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1250 }
1251 if (!pps->column_width || !pps->row_height) {
1252 ret = AVERROR(ENOMEM);
1253 goto err;
1254 }
1255
1256 for (i = 0; i < pps->num_tile_columns; i++) {
1257 pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1258 (i * sps->ctb_width) / pps->num_tile_columns;
1259 }
1260
1261 for (i = 0; i < pps->num_tile_rows; i++) {
1262 pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1263 (i * sps->ctb_height) / pps->num_tile_rows;
1264 }
1265 }
1266
1267 pps->col_bd[0] = 0;
1268 for (i = 0; i < pps->num_tile_columns; i++)
1269 pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1270
1271 pps->row_bd[0] = 0;
1272 for (i = 0; i < pps->num_tile_rows; i++)
1273 pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1274
1275 for (i = 0, j = 0; i < sps->ctb_width; i++) {
1276 if (i > pps->col_bd[j])
1277 j++;
1278 pps->col_idxX[i] = j;
1279 }
1280
1281 /**
1282 * 6.5
1283 */
1284 pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
064698d3
GM
1285 pic_area_in_min_tbs = sps->min_tb_width * sps->min_tb_height;
1286
1287 pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1288 pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1289 pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
064698d3
GM
1290 pps->min_tb_addr_zs = av_malloc_array(pic_area_in_min_tbs, sizeof(*pps->min_tb_addr_zs));
1291 if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
a0e1c351 1292 !pps->tile_id || !pps->min_tb_addr_zs) {
064698d3
GM
1293 ret = AVERROR(ENOMEM);
1294 goto err;
1295 }
1296
1297 for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1298 int tb_x = ctb_addr_rs % sps->ctb_width;
1299 int tb_y = ctb_addr_rs / sps->ctb_width;
1300 int tile_x = 0;
1301 int tile_y = 0;
1302 int val = 0;
1303
1304 for (i = 0; i < pps->num_tile_columns; i++) {
1305 if (tb_x < pps->col_bd[i + 1]) {
1306 tile_x = i;
1307 break;
1308 }
1309 }
1310
1311 for (i = 0; i < pps->num_tile_rows; i++) {
1312 if (tb_y < pps->row_bd[i + 1]) {
1313 tile_y = i;
1314 break;
1315 }
1316 }
1317
1318 for (i = 0; i < tile_x; i++)
1319 val += pps->row_height[tile_y] * pps->column_width[i];
1320 for (i = 0; i < tile_y; i++)
1321 val += sps->ctb_width * pps->row_height[i];
1322
1323 val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1324 tb_x - pps->col_bd[tile_x];
1325
1326 pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1327 pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1328 }
1329
1330 for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1331 for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1332 for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1333 for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1334 pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1335
1336 pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1337 if (!pps->tile_pos_rs) {
1338 ret = AVERROR(ENOMEM);
1339 goto err;
1340 }
1341
1342 for (j = 0; j < pps->num_tile_rows; j++)
1343 for (i = 0; i < pps->num_tile_columns; i++)
1344 pps->tile_pos_rs[j * pps->num_tile_columns + i] = pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1345
064698d3
GM
1346 log2_diff_ctb_min_tb_size = sps->log2_ctb_size - sps->log2_min_tb_size;
1347 for (y = 0; y < sps->min_tb_height; y++) {
1348 for (x = 0; x < sps->min_tb_width; x++) {
1349 int tb_x = x >> log2_diff_ctb_min_tb_size;
1350 int tb_y = y >> log2_diff_ctb_min_tb_size;
1351 int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1352 int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1353 (log2_diff_ctb_min_tb_size * 2);
1354 for (i = 0; i < log2_diff_ctb_min_tb_size; i++) {
1355 int m = 1 << i;
1356 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1357 }
1358 pps->min_tb_addr_zs[y * sps->min_tb_width + x] = val;
1359 }
1360 }
1361
b11acd57
AK
1362 remove_pps(ps, pps_id);
1363 ps->pps_list[pps_id] = pps_buf;
064698d3
GM
1364
1365 return 0;
1366
1367err:
1368 av_buffer_unref(&pps_buf);
1369 return ret;
1370}