FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hevc_ps.c
Go to the documentation of this file.
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 FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "libavutil/imgutils.h"
27 #include "golomb.h"
28 #include "hevc_data.h"
29 #include "hevc_ps.h"
30 
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 
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_assert0(!(s->sps_list[id] && s->sps == (HEVCSPS*)s->sps_list[id]->data));
93  }
94  av_buffer_unref(&s->sps_list[id]);
95 }
96 
97 static void remove_vps(HEVCParamSets *s, int id)
98 {
99  int i;
100  if (s->vps_list[id]) {
101  if (s->vps == (const HEVCVPS*)s->vps_list[id]->data)
102  s->vps = NULL;
103 
104  for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
105  if (s->sps_list[i] && ((HEVCSPS*)s->sps_list[i]->data)->vps_id == id)
106  remove_sps(s, i);
107  }
108  av_buffer_unref(&s->vps_list[id]);
109 }
110 
112  ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
113 {
114  uint8_t rps_predict = 0;
115  int delta_poc;
116  int k0 = 0;
117  int k1 = 0;
118  int k = 0;
119  int i;
120 
121  if (rps != sps->st_rps && sps->nb_st_rps)
122  rps_predict = get_bits1(gb);
123 
124  if (rps_predict) {
125  const ShortTermRPS *rps_ridx;
126  int delta_rps;
127  unsigned abs_delta_rps;
128  uint8_t use_delta_flag = 0;
129  uint8_t delta_rps_sign;
130 
131  if (is_slice_header) {
132  unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
133  if (delta_idx > sps->nb_st_rps) {
134  av_log(avctx, AV_LOG_ERROR,
135  "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
136  delta_idx, sps->nb_st_rps);
137  return AVERROR_INVALIDDATA;
138  }
139  rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
140  rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
141  } else
142  rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
143 
144  delta_rps_sign = get_bits1(gb);
145  abs_delta_rps = get_ue_golomb_long(gb) + 1;
146  if (abs_delta_rps < 1 || abs_delta_rps > 32768) {
147  av_log(avctx, AV_LOG_ERROR,
148  "Invalid value of abs_delta_rps: %d\n",
149  abs_delta_rps);
150  return AVERROR_INVALIDDATA;
151  }
152  delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
153  for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
154  int used = rps->used[k] = get_bits1(gb);
155 
156  if (!used)
157  use_delta_flag = get_bits1(gb);
158 
159  if (used || use_delta_flag) {
160  if (i < rps_ridx->num_delta_pocs)
161  delta_poc = delta_rps + rps_ridx->delta_poc[i];
162  else
163  delta_poc = delta_rps;
164  rps->delta_poc[k] = delta_poc;
165  if (delta_poc < 0)
166  k0++;
167  else
168  k1++;
169  k++;
170  }
171  }
172 
173  if (k >= FF_ARRAY_ELEMS(rps->used)) {
174  av_log(avctx, AV_LOG_ERROR,
175  "Invalid num_delta_pocs: %d\n", k);
176  return AVERROR_INVALIDDATA;
177  }
178 
179  rps->num_delta_pocs = k;
180  rps->num_negative_pics = k0;
181  // sort in increasing order (smallest first)
182  if (rps->num_delta_pocs != 0) {
183  int used, tmp;
184  for (i = 1; i < rps->num_delta_pocs; i++) {
185  delta_poc = rps->delta_poc[i];
186  used = rps->used[i];
187  for (k = i - 1; k >= 0; k--) {
188  tmp = rps->delta_poc[k];
189  if (delta_poc < tmp) {
190  rps->delta_poc[k + 1] = tmp;
191  rps->used[k + 1] = rps->used[k];
192  rps->delta_poc[k] = delta_poc;
193  rps->used[k] = used;
194  }
195  }
196  }
197  }
198  if ((rps->num_negative_pics >> 1) != 0) {
199  int used;
200  k = rps->num_negative_pics - 1;
201  // flip the negative values to largest first
202  for (i = 0; i < rps->num_negative_pics >> 1; i++) {
203  delta_poc = rps->delta_poc[i];
204  used = rps->used[i];
205  rps->delta_poc[i] = rps->delta_poc[k];
206  rps->used[i] = rps->used[k];
207  rps->delta_poc[k] = delta_poc;
208  rps->used[k] = used;
209  k--;
210  }
211  }
212  } else {
213  unsigned int prev, nb_positive_pics;
215  nb_positive_pics = get_ue_golomb_long(gb);
216 
217  if (rps->num_negative_pics >= HEVC_MAX_REFS ||
218  nb_positive_pics >= HEVC_MAX_REFS) {
219  av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
220  return AVERROR_INVALIDDATA;
221  }
222 
223  rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
224  if (rps->num_delta_pocs) {
225  prev = 0;
226  for (i = 0; i < rps->num_negative_pics; i++) {
227  delta_poc = get_ue_golomb_long(gb) + 1;
228  if (delta_poc < 1 || delta_poc > 32768) {
229  av_log(avctx, AV_LOG_ERROR,
230  "Invalid value of delta_poc: %d\n",
231  delta_poc);
232  return AVERROR_INVALIDDATA;
233  }
234  prev -= delta_poc;
235  rps->delta_poc[i] = prev;
236  rps->used[i] = get_bits1(gb);
237  }
238  prev = 0;
239  for (i = 0; i < nb_positive_pics; i++) {
240  delta_poc = get_ue_golomb_long(gb) + 1;
241  if (delta_poc < 1 || delta_poc > 32768) {
242  av_log(avctx, AV_LOG_ERROR,
243  "Invalid value of delta_poc: %d\n",
244  delta_poc);
245  return AVERROR_INVALIDDATA;
246  }
247  prev += delta_poc;
248  rps->delta_poc[rps->num_negative_pics + i] = prev;
249  rps->used[rps->num_negative_pics + i] = get_bits1(gb);
250  }
251  }
252  }
253  return 0;
254 }
255 
256 
258  PTLCommon *ptl)
259 {
260  int i;
261 
262  if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 16 + 16 + 12)
263  return -1;
264 
265  ptl->profile_space = get_bits(gb, 2);
266  ptl->tier_flag = get_bits1(gb);
267  ptl->profile_idc = get_bits(gb, 5);
268  if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
269  av_log(avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
270  else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
271  av_log(avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
273  av_log(avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
274  else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT)
275  av_log(avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n");
276  else
277  av_log(avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
278 
279  for (i = 0; i < 32; i++) {
281 
282  if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
283  ptl->profile_idc = i;
284  }
289 
290  skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15]
291  skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31]
292  skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43]
293 
294  return 0;
295 }
296 
297 static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
298  PTL *ptl, int max_num_sub_layers)
299 {
300  int i;
301  if (decode_profile_tier_level(gb, avctx, &ptl->general_ptl) < 0 ||
302  get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
303  av_log(avctx, AV_LOG_ERROR, "PTL information too short\n");
304  return -1;
305  }
306 
307  ptl->general_ptl.level_idc = get_bits(gb, 8);
308 
309  for (i = 0; i < max_num_sub_layers - 1; i++) {
312  }
313 
314  if (max_num_sub_layers - 1> 0)
315  for (i = max_num_sub_layers - 1; i < 8; i++)
316  skip_bits(gb, 2); // reserved_zero_2bits[i]
317  for (i = 0; i < max_num_sub_layers - 1; i++) {
318  if (ptl->sub_layer_profile_present_flag[i] &&
319  decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]) < 0) {
320  av_log(avctx, AV_LOG_ERROR,
321  "PTL information for sublayer %i too short\n", i);
322  return -1;
323  }
324  if (ptl->sub_layer_level_present_flag[i]) {
325  if (get_bits_left(gb) < 8) {
326  av_log(avctx, AV_LOG_ERROR,
327  "Not enough data for sublayer %i level_idc\n", i);
328  return -1;
329  } else
330  ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
331  }
332  }
333 
334  return 0;
335 }
336 
337 static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
338  int subpic_params_present)
339 {
340  int i;
341 
342  for (i = 0; i < nb_cpb; i++) {
343  get_ue_golomb_long(gb); // bit_rate_value_minus1
344  get_ue_golomb_long(gb); // cpb_size_value_minus1
345 
346  if (subpic_params_present) {
347  get_ue_golomb_long(gb); // cpb_size_du_value_minus1
348  get_ue_golomb_long(gb); // bit_rate_du_value_minus1
349  }
350  skip_bits1(gb); // cbr_flag
351  }
352 }
353 
354 static int decode_hrd(GetBitContext *gb, int common_inf_present,
355  int max_sublayers)
356 {
357  int nal_params_present = 0, vcl_params_present = 0;
358  int subpic_params_present = 0;
359  int i;
360 
361  if (common_inf_present) {
362  nal_params_present = get_bits1(gb);
363  vcl_params_present = get_bits1(gb);
364 
365  if (nal_params_present || vcl_params_present) {
366  subpic_params_present = get_bits1(gb);
367 
368  if (subpic_params_present) {
369  skip_bits(gb, 8); // tick_divisor_minus2
370  skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
371  skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
372  skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
373  }
374 
375  skip_bits(gb, 4); // bit_rate_scale
376  skip_bits(gb, 4); // cpb_size_scale
377 
378  if (subpic_params_present)
379  skip_bits(gb, 4); // cpb_size_du_scale
380 
381  skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
382  skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
383  skip_bits(gb, 5); // dpb_output_delay_length_minus1
384  }
385  }
386 
387  for (i = 0; i < max_sublayers; i++) {
388  int low_delay = 0;
389  unsigned int nb_cpb = 1;
390  int fixed_rate = get_bits1(gb);
391 
392  if (!fixed_rate)
393  fixed_rate = get_bits1(gb);
394 
395  if (fixed_rate)
396  get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
397  else
398  low_delay = get_bits1(gb);
399 
400  if (!low_delay) {
401  nb_cpb = get_ue_golomb_long(gb) + 1;
402  if (nb_cpb < 1 || nb_cpb > 32) {
403  av_log(NULL, AV_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb);
404  return AVERROR_INVALIDDATA;
405  }
406  }
407 
408  if (nal_params_present)
409  decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
410  if (vcl_params_present)
411  decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
412  }
413  return 0;
414 }
415 
417  HEVCParamSets *ps)
418 {
419  int i,j;
420  int vps_id = 0;
421  ptrdiff_t nal_size;
422  HEVCVPS *vps;
423  AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
424 
425  if (!vps_buf)
426  return AVERROR(ENOMEM);
427  vps = (HEVCVPS*)vps_buf->data;
428 
429  av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
430 
431  nal_size = gb->buffer_end - gb->buffer;
432  if (nal_size > sizeof(vps->data)) {
433  av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "
434  "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
435  nal_size, sizeof(vps->data));
436  vps->data_size = sizeof(vps->data);
437  } else {
438  vps->data_size = nal_size;
439  }
440  memcpy(vps->data, gb->buffer, vps->data_size);
441 
442  vps_id = get_bits(gb, 4);
443  if (vps_id >= HEVC_MAX_VPS_COUNT) {
444  av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
445  goto err;
446  }
447 
448  if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
449  av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
450  goto err;
451  }
452 
453  vps->vps_max_layers = get_bits(gb, 6) + 1;
454  vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
456 
457  if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
458  av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
459  goto err;
460  }
461 
463  av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
464  vps->vps_max_sub_layers);
465  goto err;
466  }
467 
468  if (parse_ptl(gb, avctx, &vps->ptl, vps->vps_max_sub_layers) < 0)
469  goto err;
470 
472 
474  for (; i < vps->vps_max_sub_layers; i++) {
478 
480  av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
481  vps->vps_max_dec_pic_buffering[i] - 1);
482  goto err;
483  }
484  if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
485  av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
486  vps->vps_num_reorder_pics[i]);
487  if (avctx->err_recognition & AV_EF_EXPLODE)
488  goto err;
489  }
490  }
491 
492  vps->vps_max_layer_id = get_bits(gb, 6);
493  vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
494  if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
495  (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
496  av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
497  goto err;
498  }
499 
500  for (i = 1; i < vps->vps_num_layer_sets; i++)
501  for (j = 0; j <= vps->vps_max_layer_id; j++)
502  skip_bits(gb, 1); // layer_id_included_flag[i][j]
503 
505  if (vps->vps_timing_info_present_flag) {
506  vps->vps_num_units_in_tick = get_bits_long(gb, 32);
507  vps->vps_time_scale = get_bits_long(gb, 32);
512  if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
513  av_log(avctx, AV_LOG_ERROR,
514  "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
515  goto err;
516  }
517  for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
518  int common_inf_present = 1;
519 
520  get_ue_golomb_long(gb); // hrd_layer_set_idx
521  if (i)
522  common_inf_present = get_bits1(gb);
523  decode_hrd(gb, common_inf_present, vps->vps_max_sub_layers);
524  }
525  }
526  get_bits1(gb); /* vps_extension_flag */
527 
528  if (get_bits_left(gb) < 0) {
529  av_log(avctx, AV_LOG_ERROR,
530  "Overread VPS by %d bits\n", -get_bits_left(gb));
531  if (ps->vps_list[vps_id])
532  goto err;
533  }
534 
535  if (ps->vps_list[vps_id] &&
536  !memcmp(ps->vps_list[vps_id]->data, vps_buf->data, vps_buf->size)) {
537  av_buffer_unref(&vps_buf);
538  } else {
539  remove_vps(ps, vps_id);
540  ps->vps_list[vps_id] = vps_buf;
541  }
542 
543  return 0;
544 
545 err:
546  av_buffer_unref(&vps_buf);
547  return AVERROR_INVALIDDATA;
548 }
549 
550 static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
551  int apply_defdispwin, HEVCSPS *sps)
552 {
553  VUI backup_vui, *vui = &sps->vui;
554  GetBitContext backup;
555  int sar_present, alt = 0;
556 
557  av_log(avctx, AV_LOG_DEBUG, "Decoding VUI\n");
558 
559  sar_present = get_bits1(gb);
560  if (sar_present) {
561  uint8_t sar_idx = get_bits(gb, 8);
562  if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
563  vui->sar = vui_sar[sar_idx];
564  else if (sar_idx == 255) {
565  vui->sar.num = get_bits(gb, 16);
566  vui->sar.den = get_bits(gb, 16);
567  } else
568  av_log(avctx, AV_LOG_WARNING,
569  "Unknown SAR index: %u.\n", sar_idx);
570  }
571 
575 
578  vui->video_format = get_bits(gb, 3);
579  vui->video_full_range_flag = get_bits1(gb);
584  vui->colour_primaries = get_bits(gb, 8);
585  vui->transfer_characteristic = get_bits(gb, 8);
586  vui->matrix_coeffs = get_bits(gb, 8);
587 
588  // Set invalid values to "unspecified"
589  if (vui->colour_primaries >= AVCOL_PRI_NB)
593  if (vui->matrix_coeffs >= AVCOL_SPC_NB)
595  if (vui->matrix_coeffs == AVCOL_SPC_RGB) {
596  switch (sps->pix_fmt) {
597  case AV_PIX_FMT_YUV444P:
598  sps->pix_fmt = AV_PIX_FMT_GBRP;
599  break;
601  sps->pix_fmt = AV_PIX_FMT_GBRP10;
602  break;
604  sps->pix_fmt = AV_PIX_FMT_GBRP12;
605  break;
606  }
607  }
608  }
609  }
610 
612  if (vui->chroma_loc_info_present_flag) {
615  }
616 
618  vui->field_seq_flag = get_bits1(gb);
620 
621  // Backup context in case an alternate header is detected
622  memcpy(&backup, gb, sizeof(backup));
623  memcpy(&backup_vui, vui, sizeof(backup_vui));
624  if (get_bits_left(gb) >= 68 && show_bits_long(gb, 21) == 0x100000) {
626  av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
627  } else
629 
630  if (vui->default_display_window_flag) {
631  int vert_mult = 1 + (sps->chroma_format_idc < 2);
632  int horiz_mult = 1 + (sps->chroma_format_idc < 3);
633  vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
634  vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
635  vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
636  vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
637 
638  if (apply_defdispwin &&
640  av_log(avctx, AV_LOG_DEBUG,
641  "discarding vui default display window, "
642  "original values are l:%u r:%u t:%u b:%u\n",
647 
650  vui->def_disp_win.top_offset =
651  vui->def_disp_win.bottom_offset = 0;
652  }
653  }
654 
655 timing_info:
657 
658  if (vui->vui_timing_info_present_flag) {
659  if( get_bits_left(gb) < 66 && !alt) {
660  // The alternate syntax seem to have timing info located
661  // at where def_disp_win is normally located
662  av_log(avctx, AV_LOG_WARNING,
663  "Strange VUI timing information, retrying...\n");
664  memcpy(vui, &backup_vui, sizeof(backup_vui));
665  memcpy(gb, &backup, sizeof(backup));
666  alt = 1;
667  goto timing_info;
668  }
669  vui->vui_num_units_in_tick = get_bits_long(gb, 32);
670  vui->vui_time_scale = get_bits_long(gb, 32);
671  if (alt) {
672  av_log(avctx, AV_LOG_INFO, "Retry got %"PRIu32"/%"PRIu32" fps\n",
674  }
680  decode_hrd(gb, 1, sps->max_sub_layers);
681  }
682 
684  if (vui->bitstream_restriction_flag) {
685  if (get_bits_left(gb) < 8 && !alt) {
686  av_log(avctx, AV_LOG_WARNING,
687  "Strange VUI bitstream restriction information, retrying"
688  " from timing information...\n");
689  memcpy(vui, &backup_vui, sizeof(backup_vui));
690  memcpy(gb, &backup, sizeof(backup));
691  alt = 1;
692  goto timing_info;
693  }
702  }
703 
704  if (get_bits_left(gb) < 1 && !alt) {
705  // XXX: Alternate syntax when sps_range_extension_flag != 0?
706  av_log(avctx, AV_LOG_WARNING,
707  "Overread in VUI, retrying from timing information...\n");
708  memcpy(vui, &backup_vui, sizeof(backup_vui));
709  memcpy(gb, &backup, sizeof(backup));
710  alt = 1;
711  goto timing_info;
712  }
713 }
714 
716 {
717  int matrixId;
718 
719  for (matrixId = 0; matrixId < 6; matrixId++) {
720  // 4x4 default is 16
721  memset(sl->sl[0][matrixId], 16, 16);
722  sl->sl_dc[0][matrixId] = 16; // default for 16x16
723  sl->sl_dc[1][matrixId] = 16; // default for 32x32
724  }
725  memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
726  memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
727  memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
728  memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
729  memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
730  memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
731  memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
732  memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
733  memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
734  memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
735  memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
736  memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
737  memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
738  memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
739  memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
740  memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
741  memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
742  memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
743 }
744 
746 {
747  uint8_t scaling_list_pred_mode_flag;
748  int32_t scaling_list_dc_coef[2][6];
749  int size_id, matrix_id, pos;
750  int i;
751 
752  for (size_id = 0; size_id < 4; size_id++)
753  for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
754  scaling_list_pred_mode_flag = get_bits1(gb);
755  if (!scaling_list_pred_mode_flag) {
756  unsigned int delta = get_ue_golomb_long(gb);
757  /* Only need to handle non-zero delta. Zero means default,
758  * which should already be in the arrays. */
759  if (delta) {
760  // Copy from previous array.
761  if (matrix_id < delta) {
762  av_log(avctx, AV_LOG_ERROR,
763  "Invalid delta in scaling list data: %d.\n", delta);
764  return AVERROR_INVALIDDATA;
765  }
766 
767  memcpy(sl->sl[size_id][matrix_id],
768  sl->sl[size_id][matrix_id - delta],
769  size_id > 0 ? 64 : 16);
770  if (size_id > 1)
771  sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
772  }
773  } else {
774  int next_coef, coef_num;
775  int32_t scaling_list_delta_coef;
776 
777  next_coef = 8;
778  coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
779  if (size_id > 1) {
780  scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
781  next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
782  sl->sl_dc[size_id - 2][matrix_id] = next_coef;
783  }
784  for (i = 0; i < coef_num; i++) {
785  if (size_id == 0)
786  pos = 4 * ff_hevc_diag_scan4x4_y[i] +
788  else
789  pos = 8 * ff_hevc_diag_scan8x8_y[i] +
791 
792  scaling_list_delta_coef = get_se_golomb(gb);
793  next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
794  sl->sl[size_id][matrix_id][pos] = next_coef;
795  }
796  }
797  }
798 
799  if (sps->chroma_format_idc == 3) {
800  for (i = 0; i < 64; i++) {
801  sl->sl[3][1][i] = sl->sl[2][1][i];
802  sl->sl[3][2][i] = sl->sl[2][2][i];
803  sl->sl[3][4][i] = sl->sl[2][4][i];
804  sl->sl[3][5][i] = sl->sl[2][5][i];
805  }
806  sl->sl_dc[1][1] = sl->sl_dc[0][1];
807  sl->sl_dc[1][2] = sl->sl_dc[0][2];
808  sl->sl_dc[1][4] = sl->sl_dc[0][4];
809  sl->sl_dc[1][5] = sl->sl_dc[0][5];
810  }
811 
812 
813  return 0;
814 }
815 
816 static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
817 {
818  const AVPixFmtDescriptor *desc;
819  switch (sps->bit_depth) {
820  case 8:
821  if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
822  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
823  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
824  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
825  break;
826  case 9:
827  if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY16;
828  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
829  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
830  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
831  break;
832  case 10:
833  if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
834  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
835  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
836  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
837  break;
838  case 12:
839  if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
840  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
841  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
842  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
843  break;
844  default:
845  av_log(avctx, AV_LOG_ERROR,
846  "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
847  "chroma_format_idc is %d, depth is %d\n",
848  sps->chroma_format_idc, sps->bit_depth);
849  return AVERROR_INVALIDDATA;
850  }
851 
852  desc = av_pix_fmt_desc_get(sps->pix_fmt);
853  if (!desc)
854  return AVERROR(EINVAL);
855 
856  sps->hshift[0] = sps->vshift[0] = 0;
857  sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
858  sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
859 
860  sps->pixel_shift = sps->bit_depth > 8;
861 
862  return 0;
863 }
864 
865 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
866  int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
867 {
868  int ret = 0;
869  int log2_diff_max_min_transform_block_size;
870  int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
871  int i;
872 
873  // Coded parameters
874 
875  sps->vps_id = get_bits(gb, 4);
876  if (sps->vps_id >= HEVC_MAX_VPS_COUNT) {
877  av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
878  return AVERROR_INVALIDDATA;
879  }
880 
881  if (vps_list && !vps_list[sps->vps_id]) {
882  av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
883  sps->vps_id);
884  return AVERROR_INVALIDDATA;
885  }
886 
887  sps->max_sub_layers = get_bits(gb, 3) + 1;
888  if (sps->max_sub_layers > HEVC_MAX_SUB_LAYERS) {
889  av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
890  sps->max_sub_layers);
891  return AVERROR_INVALIDDATA;
892  }
893 
894  skip_bits1(gb); // temporal_id_nesting_flag
895 
896  if ((ret = parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers)) < 0)
897  return ret;
898 
899  *sps_id = get_ue_golomb_long(gb);
900  if (*sps_id >= HEVC_MAX_SPS_COUNT) {
901  av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
902  return AVERROR_INVALIDDATA;
903  }
904 
906  if (sps->chroma_format_idc > 3U) {
907  av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
908  return AVERROR_INVALIDDATA;
909  }
910 
911  if (sps->chroma_format_idc == 3)
913 
915  sps->chroma_format_idc = 0;
916 
917  sps->width = get_ue_golomb_long(gb);
918  sps->height = get_ue_golomb_long(gb);
919  if ((ret = av_image_check_size(sps->width,
920  sps->height, 0, avctx)) < 0)
921  return ret;
922 
923  if (get_bits1(gb)) { // pic_conformance_flag
924  int vert_mult = 1 + (sps->chroma_format_idc < 2);
925  int horiz_mult = 1 + (sps->chroma_format_idc < 3);
926  sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
927  sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
928  sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
929  sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
930 
931  if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
932  av_log(avctx, AV_LOG_DEBUG,
933  "discarding sps conformance window, "
934  "original values are l:%u r:%u t:%u b:%u\n",
939 
942  sps->pic_conf_win.top_offset =
943  sps->pic_conf_win.bottom_offset = 0;
944  }
945  sps->output_window = sps->pic_conf_win;
946  }
947 
948  sps->bit_depth = get_ue_golomb_long(gb) + 8;
949  bit_depth_chroma = get_ue_golomb_long(gb) + 8;
950  if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
951  av_log(avctx, AV_LOG_ERROR,
952  "Luma bit depth (%d) is different from chroma bit depth (%d), "
953  "this is unsupported.\n",
954  sps->bit_depth, bit_depth_chroma);
955  return AVERROR_INVALIDDATA;
956  }
957 
958  ret = map_pixel_format(avctx, sps);
959  if (ret < 0)
960  return ret;
961 
962  sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
963  if (sps->log2_max_poc_lsb > 16) {
964  av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
965  sps->log2_max_poc_lsb - 4);
966  return AVERROR_INVALIDDATA;
967  }
968 
969  sublayer_ordering_info = get_bits1(gb);
970  start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
971  for (i = start; i < sps->max_sub_layers; i++) {
975  if (sps->temporal_layer[i].max_dec_pic_buffering > (unsigned)HEVC_MAX_DPB_SIZE) {
976  av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
978  return AVERROR_INVALIDDATA;
979  }
981  av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
983  if (avctx->err_recognition & AV_EF_EXPLODE ||
984  sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
985  return AVERROR_INVALIDDATA;
986  }
988  }
989  }
990 
991  if (!sublayer_ordering_info) {
992  for (i = 0; i < start; i++) {
996  }
997  }
998 
999  sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
1001  sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
1002  log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
1003  sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
1004  sps->log2_min_tb_size;
1005 
1006  if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
1007  av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
1008  return AVERROR_INVALIDDATA;
1009  }
1010 
1011  if (sps->log2_diff_max_min_coding_block_size > 30) {
1012  av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_coding_block_size", sps->log2_diff_max_min_coding_block_size);
1013  return AVERROR_INVALIDDATA;
1014  }
1015 
1016  if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
1017  av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
1018  return AVERROR_INVALIDDATA;
1019  }
1020 
1021  if (log2_diff_max_min_transform_block_size < 0 || log2_diff_max_min_transform_block_size > 30) {
1022  av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size", log2_diff_max_min_transform_block_size);
1023  return AVERROR_INVALIDDATA;
1024  }
1025 
1028 
1030  if (sps->scaling_list_enable_flag) {
1032 
1033  if (get_bits1(gb)) {
1034  ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
1035  if (ret < 0)
1036  return ret;
1037  }
1038  }
1039 
1040  sps->amp_enabled_flag = get_bits1(gb);
1041  sps->sao_enabled = get_bits1(gb);
1042 
1043  sps->pcm_enabled_flag = get_bits1(gb);
1044  if (sps->pcm_enabled_flag) {
1045  sps->pcm.bit_depth = get_bits(gb, 4) + 1;
1046  sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
1049  get_ue_golomb_long(gb);
1050  if (FFMAX(sps->pcm.bit_depth, sps->pcm.bit_depth_chroma) > sps->bit_depth) {
1051  av_log(avctx, AV_LOG_ERROR,
1052  "PCM bit depth (%d, %d) is greater than normal bit depth (%d)\n",
1053  sps->pcm.bit_depth, sps->pcm.bit_depth_chroma, sps->bit_depth);
1054  return AVERROR_INVALIDDATA;
1055  }
1056 
1058  }
1059 
1060  sps->nb_st_rps = get_ue_golomb_long(gb);
1062  av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
1063  sps->nb_st_rps);
1064  return AVERROR_INVALIDDATA;
1065  }
1066  for (i = 0; i < sps->nb_st_rps; i++) {
1067  if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
1068  sps, 0)) < 0)
1069  return ret;
1070  }
1071 
1075  if (sps->num_long_term_ref_pics_sps > 31U) {
1076  av_log(avctx, AV_LOG_ERROR, "num_long_term_ref_pics_sps %d is out of range.\n",
1078  return AVERROR_INVALIDDATA;
1079  }
1080  for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
1081  sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
1083  }
1084  }
1085 
1088  sps->vui.sar = (AVRational){0, 1};
1089  vui_present = get_bits1(gb);
1090  if (vui_present)
1091  decode_vui(gb, avctx, apply_defdispwin, sps);
1092 
1093  if (get_bits1(gb)) { // sps_extension_flag
1094  int sps_extension_flag[1];
1095  for (i = 0; i < 1; i++)
1096  sps_extension_flag[i] = get_bits1(gb);
1097  skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7);
1098  if (sps_extension_flag[0]) {
1099  int extended_precision_processing_flag;
1100  int high_precision_offsets_enabled_flag;
1101  int cabac_bypass_alignment_enabled_flag;
1102 
1106 
1108 
1109  extended_precision_processing_flag = get_bits1(gb);
1110  if (extended_precision_processing_flag)
1111  av_log(avctx, AV_LOG_WARNING,
1112  "extended_precision_processing_flag not yet implemented\n");
1113 
1115  high_precision_offsets_enabled_flag = get_bits1(gb);
1116  if (high_precision_offsets_enabled_flag)
1117  av_log(avctx, AV_LOG_WARNING,
1118  "high_precision_offsets_enabled_flag not yet implemented\n");
1119 
1121 
1122  cabac_bypass_alignment_enabled_flag = get_bits1(gb);
1123  if (cabac_bypass_alignment_enabled_flag)
1124  av_log(avctx, AV_LOG_WARNING,
1125  "cabac_bypass_alignment_enabled_flag not yet implemented\n");
1126  }
1127  }
1128  if (apply_defdispwin) {
1133  }
1134  if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&
1135  !(avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
1136  sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));
1137  av_log(avctx, AV_LOG_WARNING, "Reducing left output window to %d "
1138  "chroma samples to preserve alignment.\n",
1139  sps->output_window.left_offset);
1140  }
1141  sps->output_width = sps->width -
1143  sps->output_height = sps->height -
1145  if (sps->width <= sps->output_window.left_offset + (int64_t)sps->output_window.right_offset ||
1146  sps->height <= sps->output_window.top_offset + (int64_t)sps->output_window.bottom_offset) {
1147  av_log(avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n",
1148  sps->output_width, sps->output_height);
1149  if (avctx->err_recognition & AV_EF_EXPLODE) {
1150  return AVERROR_INVALIDDATA;
1151  }
1152  av_log(avctx, AV_LOG_WARNING,
1153  "Displaying the whole video surface.\n");
1154  memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
1155  memset(&sps->output_window, 0, sizeof(sps->output_window));
1156  sps->output_width = sps->width;
1157  sps->output_height = sps->height;
1158  }
1159 
1160  // Inferred parameters
1161  sps->log2_ctb_size = sps->log2_min_cb_size +
1163  sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1164 
1166  av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1167  return AVERROR_INVALIDDATA;
1168  }
1169  if (sps->log2_ctb_size < 4) {
1170  av_log(avctx,
1171  AV_LOG_ERROR,
1172  "log2_ctb_size %d differs from the bounds of any known profile\n",
1173  sps->log2_ctb_size);
1174  avpriv_request_sample(avctx, "log2_ctb_size %d", sps->log2_ctb_size);
1175  return AVERROR_INVALIDDATA;
1176  }
1177 
1178  sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1179  sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1180  sps->ctb_size = sps->ctb_width * sps->ctb_height;
1181 
1182  sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
1183  sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
1184  sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
1185  sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
1186  sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
1187  sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1188  sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1189 
1190  sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1191 
1192  if (av_mod_uintp2(sps->width, sps->log2_min_cb_size) ||
1193  av_mod_uintp2(sps->height, sps->log2_min_cb_size)) {
1194  av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
1195  return AVERROR_INVALIDDATA;
1196  }
1197 
1199  av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1201  return AVERROR_INVALIDDATA;
1202  }
1204  av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1206  return AVERROR_INVALIDDATA;
1207  }
1208  if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1209  av_log(avctx, AV_LOG_ERROR,
1210  "max transform block size out of range: %d\n",
1211  sps->log2_max_trafo_size);
1212  return AVERROR_INVALIDDATA;
1213  }
1214 
1215  if (get_bits_left(gb) < 0) {
1216  av_log(avctx, AV_LOG_ERROR,
1217  "Overread SPS by %d bits\n", -get_bits_left(gb));
1218  return AVERROR_INVALIDDATA;
1219  }
1220 
1221  return 0;
1222 }
1223 
1225  HEVCParamSets *ps, int apply_defdispwin)
1226 {
1227  HEVCSPS *sps;
1228  AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
1229  unsigned int sps_id;
1230  int ret;
1231  ptrdiff_t nal_size;
1232 
1233  if (!sps_buf)
1234  return AVERROR(ENOMEM);
1235  sps = (HEVCSPS*)sps_buf->data;
1236 
1237  av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1238 
1239  nal_size = gb->buffer_end - gb->buffer;
1240  if (nal_size > sizeof(sps->data)) {
1241  av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized SPS "
1242  "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1243  nal_size, sizeof(sps->data));
1244  sps->data_size = sizeof(sps->data);
1245  } else {
1246  sps->data_size = nal_size;
1247  }
1248  memcpy(sps->data, gb->buffer, sps->data_size);
1249 
1250  ret = ff_hevc_parse_sps(sps, gb, &sps_id,
1251  apply_defdispwin,
1252  ps->vps_list, avctx);
1253  if (ret < 0) {
1254  av_buffer_unref(&sps_buf);
1255  return ret;
1256  }
1257 
1258  if (avctx->debug & FF_DEBUG_BITSTREAM) {
1259  av_log(avctx, AV_LOG_DEBUG,
1260  "Parsed SPS: id %d; coded wxh: %dx%d; "
1261  "cropped wxh: %dx%d; pix_fmt: %s.\n",
1262  sps_id, sps->width, sps->height,
1263  sps->output_width, sps->output_height,
1265  }
1266 
1267  /* check if this is a repeat of an already parsed SPS, then keep the
1268  * original one.
1269  * otherwise drop all PPSes that depend on it */
1270  if (ps->sps_list[sps_id] &&
1271  !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
1272  av_buffer_unref(&sps_buf);
1273  } else {
1274  remove_sps(ps, sps_id);
1275  ps->sps_list[sps_id] = sps_buf;
1276  }
1277 
1278  return 0;
1279 }
1280 
1281 static void hevc_pps_free(void *opaque, uint8_t *data)
1282 {
1283  HEVCPPS *pps = (HEVCPPS*)data;
1284 
1285  av_freep(&pps->column_width);
1286  av_freep(&pps->row_height);
1287  av_freep(&pps->col_bd);
1288  av_freep(&pps->row_bd);
1289  av_freep(&pps->col_idxX);
1290  av_freep(&pps->ctb_addr_rs_to_ts);
1291  av_freep(&pps->ctb_addr_ts_to_rs);
1292  av_freep(&pps->tile_pos_rs);
1293  av_freep(&pps->tile_id);
1295 
1296  av_freep(&pps);
1297 }
1298 
1300  HEVCPPS *pps, HEVCSPS *sps) {
1301  int i;
1302 
1303  if (pps->transform_skip_enabled_flag) {
1305  }
1312  av_log(avctx, AV_LOG_ERROR,
1313  "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
1314  return AVERROR_INVALIDDATA;
1315  }
1316  for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
1317  pps->cb_qp_offset_list[i] = get_se_golomb_long(gb);
1318  if (pps->cb_qp_offset_list[i]) {
1319  av_log(avctx, AV_LOG_WARNING,
1320  "cb_qp_offset_list not tested yet.\n");
1321  }
1322  pps->cr_qp_offset_list[i] = get_se_golomb_long(gb);
1323  if (pps->cr_qp_offset_list[i]) {
1324  av_log(avctx, AV_LOG_WARNING,
1325  "cb_qp_offset_list not tested yet.\n");
1326  }
1327  }
1328  }
1331 
1332  return(0);
1333 }
1334 
1335 static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
1336  HEVCPPS *pps, HEVCSPS *sps)
1337 {
1338  int log2_diff;
1339  int pic_area_in_ctbs;
1340  int i, j, x, y, ctb_addr_rs, tile_id;
1341 
1342  // Inferred parameters
1343  pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1344  pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1345  pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1346  if (!pps->col_bd || !pps->row_bd || !pps->col_idxX)
1347  return AVERROR(ENOMEM);
1348 
1349  if (pps->uniform_spacing_flag) {
1350  if (!pps->column_width) {
1351  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1352  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1353  }
1354  if (!pps->column_width || !pps->row_height)
1355  return AVERROR(ENOMEM);
1356 
1357  for (i = 0; i < pps->num_tile_columns; i++) {
1358  pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1359  (i * sps->ctb_width) / pps->num_tile_columns;
1360  }
1361 
1362  for (i = 0; i < pps->num_tile_rows; i++) {
1363  pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1364  (i * sps->ctb_height) / pps->num_tile_rows;
1365  }
1366  }
1367 
1368  pps->col_bd[0] = 0;
1369  for (i = 0; i < pps->num_tile_columns; i++)
1370  pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1371 
1372  pps->row_bd[0] = 0;
1373  for (i = 0; i < pps->num_tile_rows; i++)
1374  pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1375 
1376  for (i = 0, j = 0; i < sps->ctb_width; i++) {
1377  if (i > pps->col_bd[j])
1378  j++;
1379  pps->col_idxX[i] = j;
1380  }
1381 
1382  /**
1383  * 6.5
1384  */
1385  pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1386 
1387  pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1388  pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1389  pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1390  pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
1391  if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1392  !pps->tile_id || !pps->min_tb_addr_zs_tab) {
1393  return AVERROR(ENOMEM);
1394  }
1395 
1396  for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1397  int tb_x = ctb_addr_rs % sps->ctb_width;
1398  int tb_y = ctb_addr_rs / sps->ctb_width;
1399  int tile_x = 0;
1400  int tile_y = 0;
1401  int val = 0;
1402 
1403  for (i = 0; i < pps->num_tile_columns; i++) {
1404  if (tb_x < pps->col_bd[i + 1]) {
1405  tile_x = i;
1406  break;
1407  }
1408  }
1409 
1410  for (i = 0; i < pps->num_tile_rows; i++) {
1411  if (tb_y < pps->row_bd[i + 1]) {
1412  tile_y = i;
1413  break;
1414  }
1415  }
1416 
1417  for (i = 0; i < tile_x; i++)
1418  val += pps->row_height[tile_y] * pps->column_width[i];
1419  for (i = 0; i < tile_y; i++)
1420  val += sps->ctb_width * pps->row_height[i];
1421 
1422  val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1423  tb_x - pps->col_bd[tile_x];
1424 
1425  pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1426  pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1427  }
1428 
1429  for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1430  for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1431  for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1432  for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1433  pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1434 
1435  pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1436  if (!pps->tile_pos_rs)
1437  return AVERROR(ENOMEM);
1438 
1439  for (j = 0; j < pps->num_tile_rows; j++)
1440  for (i = 0; i < pps->num_tile_columns; i++)
1441  pps->tile_pos_rs[j * pps->num_tile_columns + i] =
1442  pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1443 
1444  log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
1445  pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
1446  for (y = 0; y < sps->tb_mask+2; y++) {
1447  pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
1448  pps->min_tb_addr_zs_tab[y] = -1;
1449  }
1450  for (y = 0; y < sps->tb_mask+1; y++) {
1451  for (x = 0; x < sps->tb_mask+1; x++) {
1452  int tb_x = x >> log2_diff;
1453  int tb_y = y >> log2_diff;
1454  int rs = sps->ctb_width * tb_y + tb_x;
1455  int val = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
1456  for (i = 0; i < log2_diff; i++) {
1457  int m = 1 << i;
1458  val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1459  }
1460  pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
1461  }
1462  }
1463 
1464  return 0;
1465 }
1466 
1468  HEVCParamSets *ps)
1469 {
1470  HEVCSPS *sps = NULL;
1471  int i, ret = 0;
1472  unsigned int pps_id = 0;
1473  ptrdiff_t nal_size;
1474  unsigned log2_parallel_merge_level_minus2;
1475 
1476  AVBufferRef *pps_buf;
1477  HEVCPPS *pps = av_mallocz(sizeof(*pps));
1478 
1479  if (!pps)
1480  return AVERROR(ENOMEM);
1481 
1482  pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
1483  hevc_pps_free, NULL, 0);
1484  if (!pps_buf) {
1485  av_freep(&pps);
1486  return AVERROR(ENOMEM);
1487  }
1488 
1489  av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1490 
1491  nal_size = gb->buffer_end - gb->buffer;
1492  if (nal_size > sizeof(pps->data)) {
1493  av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "
1494  "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1495  nal_size, sizeof(pps->data));
1496  pps->data_size = sizeof(pps->data);
1497  } else {
1498  pps->data_size = nal_size;
1499  }
1500  memcpy(pps->data, gb->buffer, pps->data_size);
1501 
1502  // Default values
1504  pps->num_tile_columns = 1;
1505  pps->num_tile_rows = 1;
1506  pps->uniform_spacing_flag = 1;
1507  pps->disable_dbf = 0;
1508  pps->beta_offset = 0;
1509  pps->tc_offset = 0;
1511 
1512  // Coded parameters
1513  pps_id = get_ue_golomb_long(gb);
1514  if (pps_id >= HEVC_MAX_PPS_COUNT) {
1515  av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1516  ret = AVERROR_INVALIDDATA;
1517  goto err;
1518  }
1519  pps->sps_id = get_ue_golomb_long(gb);
1520  if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
1521  av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1522  ret = AVERROR_INVALIDDATA;
1523  goto err;
1524  }
1525  if (!ps->sps_list[pps->sps_id]) {
1526  av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
1527  ret = AVERROR_INVALIDDATA;
1528  goto err;
1529  }
1530  sps = (HEVCSPS *)ps->sps_list[pps->sps_id]->data;
1531 
1534  pps->num_extra_slice_header_bits = get_bits(gb, 3);
1535 
1536  pps->sign_data_hiding_flag = get_bits1(gb);
1537 
1539 
1542 
1544 
1547 
1549  pps->diff_cu_qp_delta_depth = 0;
1550  if (pps->cu_qp_delta_enabled_flag)
1552 
1553  if (pps->diff_cu_qp_delta_depth < 0 ||
1555  av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
1556  pps->diff_cu_qp_delta_depth);
1557  ret = AVERROR_INVALIDDATA;
1558  goto err;
1559  }
1560 
1561  pps->cb_qp_offset = get_se_golomb(gb);
1562  if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1563  av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1564  pps->cb_qp_offset);
1565  ret = AVERROR_INVALIDDATA;
1566  goto err;
1567  }
1568  pps->cr_qp_offset = get_se_golomb(gb);
1569  if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1570  av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1571  pps->cr_qp_offset);
1572  ret = AVERROR_INVALIDDATA;
1573  goto err;
1574  }
1576 
1577  pps->weighted_pred_flag = get_bits1(gb);
1578  pps->weighted_bipred_flag = get_bits1(gb);
1579 
1581  pps->tiles_enabled_flag = get_bits1(gb);
1583 
1584  if (pps->tiles_enabled_flag) {
1585  pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
1586  pps->num_tile_rows = get_ue_golomb_long(gb) + 1;
1587  if (pps->num_tile_columns <= 0 ||
1588  pps->num_tile_columns >= sps->width) {
1589  av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1590  pps->num_tile_columns - 1);
1591  ret = AVERROR_INVALIDDATA;
1592  goto err;
1593  }
1594  if (pps->num_tile_rows <= 0 ||
1595  pps->num_tile_rows >= sps->height) {
1596  av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1597  pps->num_tile_rows - 1);
1598  ret = AVERROR_INVALIDDATA;
1599  goto err;
1600  }
1601 
1602  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1603  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1604  if (!pps->column_width || !pps->row_height) {
1605  ret = AVERROR(ENOMEM);
1606  goto err;
1607  }
1608 
1609  pps->uniform_spacing_flag = get_bits1(gb);
1610  if (!pps->uniform_spacing_flag) {
1611  uint64_t sum = 0;
1612  for (i = 0; i < pps->num_tile_columns - 1; i++) {
1613  pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1614  sum += pps->column_width[i];
1615  }
1616  if (sum >= sps->ctb_width) {
1617  av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1618  ret = AVERROR_INVALIDDATA;
1619  goto err;
1620  }
1621  pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1622 
1623  sum = 0;
1624  for (i = 0; i < pps->num_tile_rows - 1; i++) {
1625  pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1626  sum += pps->row_height[i];
1627  }
1628  if (sum >= sps->ctb_height) {
1629  av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1630  ret = AVERROR_INVALIDDATA;
1631  goto err;
1632  }
1633  pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1634  }
1636  }
1637 
1639 
1643  pps->disable_dbf = get_bits1(gb);
1644  if (!pps->disable_dbf) {
1645  int beta_offset_div2 = get_se_golomb(gb);
1646  int tc_offset_div2 = get_se_golomb(gb) ;
1647  if (beta_offset_div2 < -6 || beta_offset_div2 > 6) {
1648  av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1649  beta_offset_div2);
1650  ret = AVERROR_INVALIDDATA;
1651  goto err;
1652  }
1653  if (tc_offset_div2 < -6 || tc_offset_div2 > 6) {
1654  av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1655  tc_offset_div2);
1656  ret = AVERROR_INVALIDDATA;
1657  goto err;
1658  }
1659  pps->beta_offset = 2 * beta_offset_div2;
1660  pps->tc_offset = 2 * tc_offset_div2;
1661  }
1662  }
1663 
1665  if (pps->scaling_list_data_present_flag) {
1667  ret = scaling_list_data(gb, avctx, &pps->scaling_list, sps);
1668  if (ret < 0)
1669  goto err;
1670  }
1672  log2_parallel_merge_level_minus2 = get_ue_golomb_long(gb);
1673  if (log2_parallel_merge_level_minus2 > sps->log2_ctb_size) {
1674  av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1675  log2_parallel_merge_level_minus2);
1676  ret = AVERROR_INVALIDDATA;
1677  goto err;
1678  }
1679  pps->log2_parallel_merge_level = log2_parallel_merge_level_minus2 + 2;
1680 
1682 
1683  if (get_bits1(gb)) { // pps_extension_present_flag
1684  int pps_range_extensions_flag = get_bits1(gb);
1685  /* int pps_extension_7bits = */ get_bits(gb, 7);
1686  if (sps->ptl.general_ptl.profile_idc == FF_PROFILE_HEVC_REXT && pps_range_extensions_flag) {
1687  if ((ret = pps_range_extensions(gb, avctx, pps, sps)) < 0)
1688  goto err;
1689  }
1690  }
1691 
1692  ret = setup_pps(avctx, gb, pps, sps);
1693  if (ret < 0)
1694  goto err;
1695 
1696  if (get_bits_left(gb) < 0) {
1697  av_log(avctx, AV_LOG_ERROR,
1698  "Overread PPS by %d bits\n", -get_bits_left(gb));
1699  goto err;
1700  }
1701 
1702  remove_pps(ps, pps_id);
1703  ps->pps_list[pps_id] = pps_buf;
1704 
1705  return 0;
1706 
1707 err:
1708  av_buffer_unref(&pps_buf);
1709  return ret;
1710 }
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:397
PTLCommon sub_layer_ptl[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:106
const HEVCPPS * pps
Definition: hevc_ps.h:319
#define NULL
Definition: coverity.c:32
unsigned int log2_min_cb_size
Definition: hevc_ps.h:194
const char const char void * val
Definition: avisynth_c.h:771
uint8_t log2_sao_offset_scale_luma
Definition: hevc_ps.h:290
const char * s
Definition: avisynth_c.h:768
int min_spatial_segmentation_idc
Definition: hevc_ps.h:85
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2333
int ctb_height
Definition: hevc_ps.h:215
AVBufferRef * vps_list[HEVC_MAX_VPS_COUNT]
Definition: hevc_ps.h:312
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
uint8_t diff_cu_chroma_qp_offset_depth
Definition: hevc_ps.h:286
struct HEVCSPS::@68 pcm
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:183
#define HEVC_MAX_DPB_SIZE
Definition: hevc.h:72
int vui_num_ticks_poc_diff_one_minus1
Definition: hevc_ps.h:78
int max_dec_pic_buffering
Definition: hevc_ps.h:162
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
int transform_skip_rotation_enabled_flag
Definition: hevc_ps.h:204
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
const char * desc
Definition: nvenc.c:60
unsigned int * row_height
RowHeight.
Definition: hevc_ps.h:295
ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_RPS_COUNT]
Definition: hevc_ps.h:174
int vps_num_ticks_poc_diff_one
vps_num_ticks_poc_diff_one_minus1 + 1
Definition: hevc_ps.h:128
int pic_init_qp_minus26
Definition: hevc_ps.h:243
unsigned int vps_max_latency_increase[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:121
VUI vui
Definition: hevc_ps.h:167
int vshift[3]
Definition: hevc_ps.h:226
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
int num
Numerator.
Definition: rational.h:59
#define FF_DEBUG_BITSTREAM
Definition: avcodec.h:2976
uint32_t vui_time_scale
Definition: hevc_ps.h:76
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:367
uint8_t weighted_bipred_flag
Definition: hevc_ps.h:255
const uint8_t * buffer
Definition: get_bits.h:56
int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx, ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
Definition: hevc_ps.c:111
unsigned int left_offset
Definition: hevc_ps.h:43
Definition: hevc_ps.h:104
unsigned int vps_max_dec_pic_buffering[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:119
uint8_t seq_loop_filter_across_slices_enabled_flag
Definition: hevc_ps.h:268
uint8_t cabac_init_present_flag
Definition: hevc_ps.h:239
int chroma_loc_info_present_flag
Definition: hevc_ps.h:63
#define HEVC_MAX_SHORT_TERM_RPS_COUNT
Definition: hevc.h:68
static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb, int subpic_params_present)
Definition: hevc_ps.c:337
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:355
int max_latency_increase
Definition: hevc_ps.h:164
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
int min_cb_height
Definition: hevc_ps.h:218
int * ctb_addr_ts_to_rs
CtbAddrTSToRS.
Definition: hevc_ps.h:301
int explicit_rdpcm_enabled_flag
Definition: hevc_ps.h:207
int num_ref_idx_l0_default_active
num_ref_idx_l0_default_active_minus1 + 1
Definition: hevc_ps.h:241
static const AVRational vui_sar[]
Definition: hevc_ps.c:53
int8_t cr_qp_offset_list[6]
Definition: hevc_ps.h:289
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:452
int chroma_sample_loc_type_top_field
Definition: hevc_ps.h:64
int width
Definition: hevc_ps.h:212
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
int qp_bd_offset
Definition: hevc_ps.h:228
int pixel_shift
Definition: hevc_ps.h:154
uint8_t entropy_coding_sync_enabled_flag
Definition: hevc_ps.h:261
#define HEVC_MAX_VPS_COUNT
Definition: hevc.h:65
HEVCWindow output_window
Definition: hevc_ps.h:149
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:333
int output_width
Definition: hevc_ps.h:148
int log2_parallel_merge_level
log2_parallel_merge_level_minus2 + 2
Definition: hevc_ps.h:280
uint8_t log2_sao_offset_scale_chroma
Definition: hevc_ps.h:291
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:334
int restricted_ref_pic_lists_flag
Definition: hevc_ps.h:84
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static void remove_vps(HEVCParamSets *s, int id)
Definition: hevc_ps.c:97
int chroma_format_idc
Definition: hevc_ps.h:144
uint8_t disable_dbf
Definition: hevc_ps.h:272
unsigned int log2_max_trafo_size
Definition: hevc_ps.h:197
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AVBufferRef * sps_list[HEVC_MAX_SPS_COUNT]
Definition: hevc_ps.h:313
uint8_t profile_compatibility_flag[32]
Definition: hevc_ps.h:96
Definition: hevc_ps.h:49
static int decode_hrd(GetBitContext *gb, int common_inf_present, int max_sublayers)
Definition: hevc_ps.c:354
unsigned int num_negative_pics
Definition: hevc_ps.h:35
uint8_t
static void remove_pps(HEVCParamSets *s, int id)
Definition: hevc_ps.c:73
static const uint8_t default_scaling_list_inter[]
Definition: hevc_ps.c:42
float delta
HEVCWindow pic_conf_win
Definition: hevc_ps.h:151
uint8_t log2_max_transform_skip_block_size
Definition: hevc_ps.h:283
int overscan_info_present_flag
Definition: hevc_ps.h:52
uint8_t vps_timing_info_present_flag
Definition: hevc_ps.h:124
#define AV_CODEC_FLAG_UNALIGNED
Allow decoders to produce frames with data planes that are not aligned to CPU requirements (e...
Definition: avcodec.h:864
uint8_t matrix_coeffs
Definition: hevc_ps.h:61
int min_tb_width
Definition: hevc_ps.h:219
#define FF_PROFILE_HEVC_MAIN
Definition: avcodec.h:3323
const uint8_t ff_hevc_diag_scan8x8_x[64]
Definition: hevc_data.c:39
int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id, int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
Parse the SPS from the bitstream into the provided HEVCSPS struct.
Definition: hevc_ps.c:865
const HEVCVPS * vps
Definition: hevc_ps.h:317
int num_ref_idx_l1_default_active
num_ref_idx_l1_default_active_minus1 + 1
Definition: hevc_ps.h:242
unsigned int log2_min_pcm_cb_size
Definition: hevc_ps.h:187
int min_cb_width
Definition: hevc_ps.h:217
int vps_max_sub_layers
vps_max_temporal_layers_minus1 + 1
Definition: hevc_ps.h:115
int frame_field_info_present_flag
Definition: hevc_ps.h:69
uint8_t tier_flag
Definition: hevc_ps.h:94
uint8_t scaling_list_data_present_flag
Definition: hevc_ps.h:276
uint8_t bit_depth_chroma
Definition: hevc_ps.h:186
AVBufferRef * pps_list[HEVC_MAX_PPS_COUNT]
Definition: hevc_ps.h:314
uint8_t loop_filter_disable_flag
Definition: hevc_ps.h:189
static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
Definition: hevc_ps.c:816
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:356
#define FF_PROFILE_HEVC_MAIN_10
Definition: avcodec.h:3324
int8_t cb_qp_offset_list[6]
Definition: hevc_ps.h:288
int bitstream_restriction_flag
Definition: hevc_ps.h:81
uint8_t transquant_bypass_enable_flag
Definition: hevc_ps.h:257
#define av_log(a,...)
int vps_max_layer_id
Definition: hevc_ps.h:122
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t frame_only_constraint_flag
Definition: hevc_ps.h:101
#define AV_CODEC_FLAG2_IGNORE_CROP
Discard cropping information from SPS.
Definition: avcodec.h:959
unsigned int log2_max_poc_lsb
Definition: hevc_ps.h:157
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
int vps_max_layers
Definition: hevc_ps.h:114
int min_pu_height
Definition: hevc_ps.h:222
#define PTRDIFF_SPECIFIER
Definition: internal.h:254
int persistent_rice_adaptation_enabled_flag
coded frame dimension in various units
Definition: hevc_ps.h:209
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:416
#define AVERROR(e)
Definition: error.h:43
int data_size
Definition: hevc_ps.h:308
int vui_timing_info_present_flag
Definition: hevc_ps.h:74
uint8_t amp_enabled_flag
Definition: hevc_ps.h:176
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
Not part of ABI.
Definition: pixfmt.h:417
unsigned int log2_ctb_size
Definition: hevc_ps.h:198
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1827
int vui_poc_proportional_to_timing_flag
Definition: hevc_ps.h:77
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:354
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
uint8_t vps_temporal_id_nesting_flag
Definition: hevc_ps.h:113
#define FFMAX(a, b)
Definition: common.h:94
#define HEVC_MAX_LOG2_CTB_SIZE
Definition: hevc.h:74
uint8_t colour_primaries
Definition: hevc_ps.h:59
uint8_t used[32]
Definition: hevc_ps.h:39
int motion_vectors_over_pic_boundaries_flag
Definition: hevc_ps.h:83
uint8_t tiles_enabled_flag
Definition: hevc_ps.h:260
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
uint32_t vps_num_units_in_tick
Definition: hevc_ps.h:125
int * col_idxX
Definition: hevc_ps.h:298
uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:109
const HEVCSPS * sps
Definition: hevc_ps.h:318
int vps_num_layer_sets
vps_num_layer_sets_minus1 + 1
Definition: hevc_ps.h:123
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:349
uint8_t lists_modification_present_flag
Definition: hevc_ps.h:279
int * min_tb_addr_zs_tab
MinTbAddrZS.
Definition: hevc_ps.h:305
uint8_t profile_idc
Definition: hevc_ps.h:95
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:281
static int pps_range_extensions(GetBitContext *gb, AVCodecContext *avctx, HEVCPPS *pps, HEVCSPS *sps)
Definition: hevc_ps.c:1299
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:3020
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:335
int max_transform_hierarchy_depth_inter
Definition: hevc_ps.h:201
#define FFMIN(a, b)
Definition: common.h:96
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:74
uint8_t vps_poc_proportional_to_timing_flag
Definition: hevc_ps.h:127
int num_tile_columns
num_tile_columns_minus1 + 1
Definition: hevc_ps.h:263
int output_height
Definition: hevc_ps.h:148
unsigned int top_offset
Definition: hevc_ps.h:45
int hshift[3]
Definition: hevc_ps.h:225
unsigned int vps_num_reorder_pics[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:120
struct HEVCSPS::@67 temporal_layer[HEVC_MAX_SUB_LAYERS]
#define FF_PROFILE_HEVC_MAIN_STILL_PICTURE
Definition: avcodec.h:3325
int32_t
int max_bits_per_min_cu_denom
Definition: hevc_ps.h:87
uint8_t cu_qp_delta_enabled_flag
Definition: hevc_ps.h:248
uint8_t used_by_curr_pic_lt_sps_flag[32]
Definition: hevc_ps.h:181
int ctb_width
Definition: hevc_ps.h:214
uint8_t sl_dc[2][6]
Definition: hevc_ps.h:139
int32_t delta_poc[32]
Definition: hevc_ps.h:38
uint8_t sign_data_hiding_flag
Definition: hevc_ps.h:237
int height
Definition: hevc_ps.h:213
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:3031
uint8_t output_flag_present_flag
Definition: hevc_ps.h:256
int log2_max_mv_length_vertical
Definition: hevc_ps.h:89
int data_size
Definition: hevc_ps.h:132
PTLCommon general_ptl
Definition: hevc_ps.h:105
const uint8_t ff_hevc_diag_scan4x4_y[16]
Definition: hevc_data.c:32
uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:108
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:350
int max_bytes_per_pic_denom
Definition: hevc_ps.h:86
int overscan_appropriate_flag
Definition: hevc_ps.h:53
static void set_default_scaling_list_data(ScalingList *sl)
Definition: hevc_ps.c:715
int implicit_rdpcm_enabled_flag
Definition: hevc_ps.h:206
unsigned vps_id
Definition: hevc_ps.h:143
#define FF_ARRAY_ELEMS(a)
uint8_t constrained_intra_pred_flag
Definition: hevc_ps.h:245
int tb_mask
Definition: hevc_ps.h:223
uint8_t sl[4][6][64]
Definition: hevc_ps.h:138
PTL ptl
Definition: hevc_ps.h:117
uint8_t pic_slice_level_chroma_qp_offsets_present_flag
Definition: hevc_ps.h:253
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:85
uint32_t vps_time_scale
Definition: hevc_ps.h:126
int colour_description_present_flag
Definition: hevc_ps.h:58
enum AVPixelFormat pix_fmt
Definition: hevc_ps.h:155
static int get_se_golomb_long(GetBitContext *gb)
Definition: golomb.h:215
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
uint8_t transform_skip_enabled_flag
Definition: hevc_ps.h:246
uint8_t uniform_spacing_flag
Definition: hevc_ps.h:265
int ctb_size
Definition: hevc_ps.h:216
static void decode_vui(GetBitContext *gb, AVCodecContext *avctx, int apply_defdispwin, HEVCSPS *sps)
Definition: hevc_ps.c:550
int * ctb_addr_rs_to_ts
CtbAddrRSToTS.
Definition: hevc_ps.h:300
PTL ptl
Definition: hevc_ps.h:168
int max_sub_layers
Definition: hevc_ps.h:160
unsigned int log2_min_pu_size
Definition: hevc_ps.h:199
unsigned int sps_id
seq_parameter_set_id
Definition: hevc_ps.h:235
int debug
debug
Definition: avcodec.h:2973
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
ScalingList scaling_list
Definition: hevc_ps.h:277
main external API structure.
Definition: avcodec.h:1732
static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx, PTL *ptl, int max_num_sub_layers)
Definition: hevc_ps.c:297
int log2_max_mv_length_horizontal
Definition: hevc_ps.h:88
uint8_t sao_enabled
Definition: hevc_ps.h:177
int num_extra_slice_header_bits
Definition: hevc_ps.h:281
uint8_t * data
The data buffer.
Definition: buffer.h:89
uint8_t loop_filter_across_tiles_enabled_flag
Definition: hevc_ps.h:266
uint8_t num_long_term_ref_pics_sps
Definition: hevc_ps.h:182
uint8_t cross_component_prediction_enabled_flag
Definition: hevc_ps.h:284
uint32_t vui_num_units_in_tick
Definition: hevc_ps.h:75
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
uint8_t deblocking_filter_control_present_flag
Definition: hevc_ps.h:270
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
int transform_skip_context_enabled_flag
Definition: hevc_ps.h:205
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:338
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:351
uint8_t sps_temporal_mvp_enabled_flag
Definition: hevc_ps.h:191
unsigned int nb_st_rps
Definition: hevc_ps.h:173
int chroma_sample_loc_type_bottom_field
Definition: hevc_ps.h:65
int num_tile_rows
num_tile_rows_minus1 + 1
Definition: hevc_ps.h:264
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
uint8_t chroma_qp_offset_list_enabled_flag
Definition: hevc_ps.h:285
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int vps_num_hrd_parameters
Definition: hevc_ps.h:129
Not part of ABI.
Definition: pixfmt.h:445
#define HEVC_MAX_SPS_COUNT
Definition: hevc.h:66
static void hevc_pps_free(void *opaque, uint8_t *data)
Definition: hevc_ps.c:1281
unsigned int log2_min_tb_size
Definition: hevc_ps.h:196
uint16_t lt_ref_pic_poc_lsb_sps[32]
Definition: hevc_ps.h:180
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:1467
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:348
#define HEVC_MAX_SUB_LAYERS
7.4.2.1
Definition: hevc.h:64
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, int apply_defdispwin)
Definition: hevc_ps.c:1224
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:346
uint8_t scaling_list_enable_flag
Definition: hevc_ps.h:170
int * tile_id
TileId.
Definition: hevc_ps.h:302
static const uint8_t default_scaling_list_intra[]
Definition: hevc_ps.c:31
int tc_offset
tc_offset_div2 * 2
Definition: hevc_ps.h:274
uint8_t transfer_characteristic
Definition: hevc_ps.h:60
int default_display_window_flag
Definition: hevc_ps.h:71
int size
Size of data in bytes.
Definition: buffer.h:93
HEVCWindow def_disp_win
Definition: hevc_ps.h:72
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:368
int cr_qp_offset
Definition: hevc_ps.h:252
ScalingList scaling_list
Definition: hevc_ps.h:171
#define SIZE_SPECIFIER
Definition: internal.h:255
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:352
unsigned int log2_diff_max_min_coding_block_size
Definition: hevc_ps.h:195
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:358
static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx, ScalingList *sl, HEVCSPS *sps)
Definition: hevc_ps.c:745
unsigned int log2_max_pcm_cb_size
Definition: hevc_ps.h:188
static int setup_pps(AVCodecContext *avctx, GetBitContext *gb, HEVCPPS *pps, HEVCSPS *sps)
Definition: hevc_ps.c:1335
uint8_t level_idc
Definition: hevc_ps.h:97
int neutra_chroma_indication_flag
Definition: hevc_ps.h:66
int * tile_pos_rs
TilePosRS.
Definition: hevc_ps.h:303
const uint8_t ff_hevc_diag_scan4x4_x[16]
Definition: hevc_data.c:25
int data_size
Definition: hevc_ps.h:231
A reference to a data buffer.
Definition: buffer.h:81
int max_transform_hierarchy_depth_intra
Definition: hevc_ps.h:202
uint8_t profile_space
Definition: hevc_ps.h:93
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
unsigned int * row_bd
RowBd.
Definition: hevc_ps.h:297
int video_format
Definition: hevc_ps.h:56
uint8_t weighted_pred_flag
Definition: hevc_ps.h:254
uint8_t data[4096]
Definition: hevc_ps.h:131
unsigned int * col_bd
ColBd.
Definition: hevc_ps.h:296
const uint8_t * buffer_end
Definition: get_bits.h:56
unsigned int * column_width
ColumnWidth.
Definition: hevc_ps.h:294
uint8_t slice_header_extension_present_flag
Definition: hevc_ps.h:282
#define HEVC_MAX_PPS_COUNT
Definition: hevc.h:67
static int decode_profile_tier_level(GetBitContext *gb, AVCodecContext *avctx, PTLCommon *ptl)
Definition: hevc_ps.c:257
int den
Denominator.
Definition: rational.h:60
int video_full_range_flag
Definition: hevc_ps.h:57
AVRational sar
Definition: hevc_ps.h:50
uint8_t chroma_qp_offset_list_len_minus1
Definition: hevc_ps.h:287
const uint8_t ff_hevc_diag_scan8x8_y[64]
Definition: hevc_data.c:58
int num_delta_pocs
Definition: hevc_ps.h:36
#define FF_PROFILE_HEVC_REXT
Definition: avcodec.h:3326
uint8_t sps_strong_intra_smoothing_enable_flag
Definition: hevc_ps.h:192
int rps_idx_num_delta_pocs
Definition: hevc_ps.h:37
uint8_t data[4096]
Definition: hevc_ps.h:230
int min_pu_width
Definition: hevc_ps.h:221
uint8_t long_term_ref_pics_present_flag
Definition: hevc_ps.h:179
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1834
int vui_hrd_parameters_present_flag
Definition: hevc_ps.h:79
unsigned int right_offset
Definition: hevc_ps.h:44
int diff_cu_qp_delta_depth
Definition: hevc_ps.h:249
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
int num_reorder_pics
Definition: hevc_ps.h:163
static void remove_sps(HEVCParamSets *s, int id)
Definition: hevc_ps.c:80
uint8_t progressive_source_flag
Definition: hevc_ps.h:98
int cb_qp_offset
Definition: hevc_ps.h:251
#define av_malloc_array(a, b)
int video_signal_type_present_flag
Definition: hevc_ps.h:55
uint8_t deblocking_filter_override_enabled_flag
Definition: hevc_ps.h:271
int bit_depth
Definition: hevc_ps.h:153
int beta_offset
beta_offset_div2 * 2
Definition: hevc_ps.h:273
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2249
int min_tb_height
Definition: hevc_ps.h:220
int * min_tb_addr_zs
MinTbAddrZS.
Definition: hevc_ps.h:304
#define HEVC_MAX_REFS
Definition: hevc.h:71
uint8_t non_packed_constraint_flag
Definition: hevc_ps.h:100
int intra_smoothing_disabled_flag
Definition: hevc_ps.h:208
exp golomb vlc stuff
int pcm_enabled_flag
Definition: hevc_ps.h:158
enum AVCodecID id
int tiles_fixed_structure_flag
Definition: hevc_ps.h:82
uint8_t interlaced_source_flag
Definition: hevc_ps.h:99
int field_seq_flag
Definition: hevc_ps.h:68
Not part of ABI.
Definition: pixfmt.h:465
for(j=16;j >0;--j)
uint8_t separate_colour_plane_flag
output (i.e. cropped) values
Definition: hevc_ps.h:145
unsigned int bottom_offset
Definition: hevc_ps.h:46
int vps_sub_layer_ordering_info_present_flag
Definition: hevc_ps.h:118
uint8_t data[4096]
Definition: hevc_ps.h:307
uint8_t dependent_slice_segments_enabled_flag
Definition: hevc_ps.h:259
static uint8_t tmp[11]
Definition: aes_ctr.c:26