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