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  prev -= delta_poc;
229  rps->delta_poc[i] = prev;
230  rps->used[i] = get_bits1(gb);
231  }
232  prev = 0;
233  for (i = 0; i < nb_positive_pics; i++) {
234  delta_poc = get_ue_golomb_long(gb) + 1;
235  prev += delta_poc;
236  rps->delta_poc[rps->num_negative_pics + i] = prev;
237  rps->used[rps->num_negative_pics + i] = get_bits1(gb);
238  }
239  }
240  }
241  return 0;
242 }
243 
244 
246  PTLCommon *ptl)
247 {
248  int i;
249 
250  if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 16 + 16 + 12)
251  return -1;
252 
253  ptl->profile_space = get_bits(gb, 2);
254  ptl->tier_flag = get_bits1(gb);
255  ptl->profile_idc = get_bits(gb, 5);
256  if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
257  av_log(avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
258  else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
259  av_log(avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
261  av_log(avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
262  else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT)
263  av_log(avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n");
264  else
265  av_log(avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
266 
267  for (i = 0; i < 32; i++) {
269 
270  if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
271  ptl->profile_idc = i;
272  }
277 
278  skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15]
279  skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31]
280  skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43]
281 
282  return 0;
283 }
284 
285 static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
286  PTL *ptl, int max_num_sub_layers)
287 {
288  int i;
289  if (decode_profile_tier_level(gb, avctx, &ptl->general_ptl) < 0 ||
290  get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
291  av_log(avctx, AV_LOG_ERROR, "PTL information too short\n");
292  return -1;
293  }
294 
295  ptl->general_ptl.level_idc = get_bits(gb, 8);
296 
297  for (i = 0; i < max_num_sub_layers - 1; i++) {
300  }
301 
302  if (max_num_sub_layers - 1> 0)
303  for (i = max_num_sub_layers - 1; i < 8; i++)
304  skip_bits(gb, 2); // reserved_zero_2bits[i]
305  for (i = 0; i < max_num_sub_layers - 1; i++) {
306  if (ptl->sub_layer_profile_present_flag[i] &&
307  decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]) < 0) {
308  av_log(avctx, AV_LOG_ERROR,
309  "PTL information for sublayer %i too short\n", i);
310  return -1;
311  }
312  if (ptl->sub_layer_level_present_flag[i]) {
313  if (get_bits_left(gb) < 8) {
314  av_log(avctx, AV_LOG_ERROR,
315  "Not enough data for sublayer %i level_idc\n", i);
316  return -1;
317  } else
318  ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
319  }
320  }
321 
322  return 0;
323 }
324 
325 static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
326  int subpic_params_present)
327 {
328  int i;
329 
330  for (i = 0; i < nb_cpb; i++) {
331  get_ue_golomb_long(gb); // bit_rate_value_minus1
332  get_ue_golomb_long(gb); // cpb_size_value_minus1
333 
334  if (subpic_params_present) {
335  get_ue_golomb_long(gb); // cpb_size_du_value_minus1
336  get_ue_golomb_long(gb); // bit_rate_du_value_minus1
337  }
338  skip_bits1(gb); // cbr_flag
339  }
340 }
341 
342 static int decode_hrd(GetBitContext *gb, int common_inf_present,
343  int max_sublayers)
344 {
345  int nal_params_present = 0, vcl_params_present = 0;
346  int subpic_params_present = 0;
347  int i;
348 
349  if (common_inf_present) {
350  nal_params_present = get_bits1(gb);
351  vcl_params_present = get_bits1(gb);
352 
353  if (nal_params_present || vcl_params_present) {
354  subpic_params_present = get_bits1(gb);
355 
356  if (subpic_params_present) {
357  skip_bits(gb, 8); // tick_divisor_minus2
358  skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
359  skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
360  skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
361  }
362 
363  skip_bits(gb, 4); // bit_rate_scale
364  skip_bits(gb, 4); // cpb_size_scale
365 
366  if (subpic_params_present)
367  skip_bits(gb, 4); // cpb_size_du_scale
368 
369  skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
370  skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
371  skip_bits(gb, 5); // dpb_output_delay_length_minus1
372  }
373  }
374 
375  for (i = 0; i < max_sublayers; i++) {
376  int low_delay = 0;
377  unsigned int nb_cpb = 1;
378  int fixed_rate = get_bits1(gb);
379 
380  if (!fixed_rate)
381  fixed_rate = get_bits1(gb);
382 
383  if (fixed_rate)
384  get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
385  else
386  low_delay = get_bits1(gb);
387 
388  if (!low_delay) {
389  nb_cpb = get_ue_golomb_long(gb) + 1;
390  if (nb_cpb < 1 || nb_cpb > 32) {
391  av_log(NULL, AV_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb);
392  return AVERROR_INVALIDDATA;
393  }
394  }
395 
396  if (nal_params_present)
397  decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
398  if (vcl_params_present)
399  decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
400  }
401  return 0;
402 }
403 
405  HEVCParamSets *ps)
406 {
407  int i,j;
408  int vps_id = 0;
409  ptrdiff_t nal_size;
410  HEVCVPS *vps;
411  AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
412 
413  if (!vps_buf)
414  return AVERROR(ENOMEM);
415  vps = (HEVCVPS*)vps_buf->data;
416 
417  av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
418 
419  nal_size = gb->buffer_end - gb->buffer;
420  if (nal_size > sizeof(vps->data)) {
421  av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "
422  "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
423  nal_size, sizeof(vps->data));
424  vps->data_size = sizeof(vps->data);
425  } else {
426  vps->data_size = nal_size;
427  }
428  memcpy(vps->data, gb->buffer, vps->data_size);
429 
430  vps_id = get_bits(gb, 4);
431  if (vps_id >= HEVC_MAX_VPS_COUNT) {
432  av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
433  goto err;
434  }
435 
436  if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
437  av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
438  goto err;
439  }
440 
441  vps->vps_max_layers = get_bits(gb, 6) + 1;
442  vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
444 
445  if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
446  av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
447  goto err;
448  }
449 
451  av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
452  vps->vps_max_sub_layers);
453  goto err;
454  }
455 
456  if (parse_ptl(gb, avctx, &vps->ptl, vps->vps_max_sub_layers) < 0)
457  goto err;
458 
460 
462  for (; i < vps->vps_max_sub_layers; i++) {
466 
468  av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
469  vps->vps_max_dec_pic_buffering[i] - 1);
470  goto err;
471  }
472  if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
473  av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
474  vps->vps_num_reorder_pics[i]);
475  if (avctx->err_recognition & AV_EF_EXPLODE)
476  goto err;
477  }
478  }
479 
480  vps->vps_max_layer_id = get_bits(gb, 6);
481  vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
482  if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
483  (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
484  av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
485  goto err;
486  }
487 
488  for (i = 1; i < vps->vps_num_layer_sets; i++)
489  for (j = 0; j <= vps->vps_max_layer_id; j++)
490  skip_bits(gb, 1); // layer_id_included_flag[i][j]
491 
493  if (vps->vps_timing_info_present_flag) {
494  vps->vps_num_units_in_tick = get_bits_long(gb, 32);
495  vps->vps_time_scale = get_bits_long(gb, 32);
500  if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
501  av_log(avctx, AV_LOG_ERROR,
502  "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
503  goto err;
504  }
505  for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
506  int common_inf_present = 1;
507 
508  get_ue_golomb_long(gb); // hrd_layer_set_idx
509  if (i)
510  common_inf_present = get_bits1(gb);
511  decode_hrd(gb, common_inf_present, vps->vps_max_sub_layers);
512  }
513  }
514  get_bits1(gb); /* vps_extension_flag */
515 
516  if (get_bits_left(gb) < 0) {
517  av_log(avctx, AV_LOG_ERROR,
518  "Overread VPS by %d bits\n", -get_bits_left(gb));
519  if (ps->vps_list[vps_id])
520  goto err;
521  }
522 
523  if (ps->vps_list[vps_id] &&
524  !memcmp(ps->vps_list[vps_id]->data, vps_buf->data, vps_buf->size)) {
525  av_buffer_unref(&vps_buf);
526  } else {
527  remove_vps(ps, vps_id);
528  ps->vps_list[vps_id] = vps_buf;
529  }
530 
531  return 0;
532 
533 err:
534  av_buffer_unref(&vps_buf);
535  return AVERROR_INVALIDDATA;
536 }
537 
538 static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
539  int apply_defdispwin, HEVCSPS *sps)
540 {
541  VUI *vui = &sps->vui;
542  GetBitContext backup;
543  int sar_present, alt = 0;
544 
545  av_log(avctx, AV_LOG_DEBUG, "Decoding VUI\n");
546 
547  sar_present = get_bits1(gb);
548  if (sar_present) {
549  uint8_t sar_idx = get_bits(gb, 8);
550  if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
551  vui->sar = vui_sar[sar_idx];
552  else if (sar_idx == 255) {
553  vui->sar.num = get_bits(gb, 16);
554  vui->sar.den = get_bits(gb, 16);
555  } else
556  av_log(avctx, AV_LOG_WARNING,
557  "Unknown SAR index: %u.\n", sar_idx);
558  }
559 
563 
566  vui->video_format = get_bits(gb, 3);
567  vui->video_full_range_flag = get_bits1(gb);
572  vui->colour_primaries = get_bits(gb, 8);
573  vui->transfer_characteristic = get_bits(gb, 8);
574  vui->matrix_coeffs = get_bits(gb, 8);
575 
576  // Set invalid values to "unspecified"
583  if (vui->matrix_coeffs == AVCOL_SPC_RGB) {
584  switch (sps->pix_fmt) {
585  case AV_PIX_FMT_YUV444P:
586  sps->pix_fmt = AV_PIX_FMT_GBRP;
587  break;
589  sps->pix_fmt = AV_PIX_FMT_GBRP10;
590  break;
592  sps->pix_fmt = AV_PIX_FMT_GBRP12;
593  break;
594  }
595  }
596  }
597  }
598 
600  if (vui->chroma_loc_info_present_flag) {
603  }
604 
606  vui->field_seq_flag = get_bits1(gb);
608 
609  if (get_bits_left(gb) >= 68 && show_bits_long(gb, 21) == 0x100000) {
611  av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
612  } else
614  // Backup context in case an alternate header is detected
615  memcpy(&backup, gb, sizeof(backup));
616 
617  if (vui->default_display_window_flag) {
618  int vert_mult = 1 + (sps->chroma_format_idc < 2);
619  int horiz_mult = 1 + (sps->chroma_format_idc < 3);
620  vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
621  vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
622  vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
623  vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
624 
625  if (apply_defdispwin &&
627  av_log(avctx, AV_LOG_DEBUG,
628  "discarding vui default display window, "
629  "original values are l:%u r:%u t:%u b:%u\n",
634 
637  vui->def_disp_win.top_offset =
638  vui->def_disp_win.bottom_offset = 0;
639  }
640  }
641 
643 
644  if (vui->vui_timing_info_present_flag) {
645  if( get_bits_left(gb) < 66) {
646  // The alternate syntax seem to have timing info located
647  // at where def_disp_win is normally located
648  av_log(avctx, AV_LOG_WARNING,
649  "Strange VUI timing information, retrying...\n");
651  memset(&vui->def_disp_win, 0, sizeof(vui->def_disp_win));
652  memcpy(gb, &backup, sizeof(backup));
653  alt = 1;
654  }
655  vui->vui_num_units_in_tick = get_bits_long(gb, 32);
656  vui->vui_time_scale = get_bits_long(gb, 32);
657  if (alt) {
658  av_log(avctx, AV_LOG_INFO, "Retry got %"PRIu32"/%"PRIu32" fps\n",
660  }
666  decode_hrd(gb, 1, sps->max_sub_layers);
667  }
668 
670  if (vui->bitstream_restriction_flag) {
679  }
680 }
681 
683 {
684  int matrixId;
685 
686  for (matrixId = 0; matrixId < 6; matrixId++) {
687  // 4x4 default is 16
688  memset(sl->sl[0][matrixId], 16, 16);
689  sl->sl_dc[0][matrixId] = 16; // default for 16x16
690  sl->sl_dc[1][matrixId] = 16; // default for 32x32
691  }
692  memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
693  memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
694  memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
695  memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
696  memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
697  memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
698  memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
699  memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
700  memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
701  memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
702  memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
703  memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
704  memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
705  memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
706  memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
707  memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
708  memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
709  memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
710 }
711 
713 {
714  uint8_t scaling_list_pred_mode_flag;
715  int32_t scaling_list_dc_coef[2][6];
716  int size_id, matrix_id, pos;
717  int i;
718 
719  for (size_id = 0; size_id < 4; size_id++)
720  for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
721  scaling_list_pred_mode_flag = get_bits1(gb);
722  if (!scaling_list_pred_mode_flag) {
723  unsigned int delta = get_ue_golomb_long(gb);
724  /* Only need to handle non-zero delta. Zero means default,
725  * which should already be in the arrays. */
726  if (delta) {
727  // Copy from previous array.
728  delta *= (size_id == 3) ? 3 : 1;
729  if (matrix_id < delta) {
730  av_log(avctx, AV_LOG_ERROR,
731  "Invalid delta in scaling list data: %d.\n", delta);
732  return AVERROR_INVALIDDATA;
733  }
734 
735  memcpy(sl->sl[size_id][matrix_id],
736  sl->sl[size_id][matrix_id - delta],
737  size_id > 0 ? 64 : 16);
738  if (size_id > 1)
739  sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
740  }
741  } else {
742  int next_coef, coef_num;
743  int32_t scaling_list_delta_coef;
744 
745  next_coef = 8;
746  coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
747  if (size_id > 1) {
748  scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
749  next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
750  sl->sl_dc[size_id - 2][matrix_id] = next_coef;
751  }
752  for (i = 0; i < coef_num; i++) {
753  if (size_id == 0)
754  pos = 4 * ff_hevc_diag_scan4x4_y[i] +
756  else
757  pos = 8 * ff_hevc_diag_scan8x8_y[i] +
759 
760  scaling_list_delta_coef = get_se_golomb(gb);
761  next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
762  sl->sl[size_id][matrix_id][pos] = next_coef;
763  }
764  }
765  }
766 
767  if (sps->chroma_format_idc == 3) {
768  for (i = 0; i < 64; i++) {
769  sl->sl[3][1][i] = sl->sl[2][1][i];
770  sl->sl[3][2][i] = sl->sl[2][2][i];
771  sl->sl[3][4][i] = sl->sl[2][4][i];
772  sl->sl[3][5][i] = sl->sl[2][5][i];
773  }
774  sl->sl_dc[1][1] = sl->sl_dc[0][1];
775  sl->sl_dc[1][2] = sl->sl_dc[0][2];
776  sl->sl_dc[1][4] = sl->sl_dc[0][4];
777  sl->sl_dc[1][5] = sl->sl_dc[0][5];
778  }
779 
780 
781  return 0;
782 }
783 
784 static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
785 {
786  const AVPixFmtDescriptor *desc;
787  switch (sps->bit_depth) {
788  case 8:
789  if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
790  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
791  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
792  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
793  break;
794  case 9:
795  if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY16;
796  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
797  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
798  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
799  break;
800  case 10:
801  if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
802  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
803  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
804  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
805  break;
806  case 12:
807  if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
808  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
809  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
810  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
811  break;
812  default:
813  av_log(avctx, AV_LOG_ERROR,
814  "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
815  "chroma_format_idc is %d, depth is %d\n",
816  sps->chroma_format_idc, sps->bit_depth);
817  return AVERROR_INVALIDDATA;
818  }
819 
820  desc = av_pix_fmt_desc_get(sps->pix_fmt);
821  if (!desc)
822  return AVERROR(EINVAL);
823 
824  sps->hshift[0] = sps->vshift[0] = 0;
825  sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
826  sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
827 
828  sps->pixel_shift = sps->bit_depth > 8;
829 
830  return 0;
831 }
832 
833 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
834  int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
835 {
836  HEVCWindow *ow;
837  int ret = 0;
838  int log2_diff_max_min_transform_block_size;
839  int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
840  int i;
841 
842  // Coded parameters
843 
844  sps->vps_id = get_bits(gb, 4);
845  if (sps->vps_id >= HEVC_MAX_VPS_COUNT) {
846  av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
847  return AVERROR_INVALIDDATA;
848  }
849 
850  if (vps_list && !vps_list[sps->vps_id]) {
851  av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
852  sps->vps_id);
853  return AVERROR_INVALIDDATA;
854  }
855 
856  sps->max_sub_layers = get_bits(gb, 3) + 1;
857  if (sps->max_sub_layers > HEVC_MAX_SUB_LAYERS) {
858  av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
859  sps->max_sub_layers);
860  return AVERROR_INVALIDDATA;
861  }
862 
863  skip_bits1(gb); // temporal_id_nesting_flag
864 
865  if ((ret = parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers)) < 0)
866  return ret;
867 
868  *sps_id = get_ue_golomb_long(gb);
869  if (*sps_id >= HEVC_MAX_SPS_COUNT) {
870  av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
871  return AVERROR_INVALIDDATA;
872  }
873 
875  if (sps->chroma_format_idc > 3U) {
876  av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
877  return AVERROR_INVALIDDATA;
878  }
879 
880  if (sps->chroma_format_idc == 3)
882 
884  sps->chroma_format_idc = 0;
885 
886  sps->width = get_ue_golomb_long(gb);
887  sps->height = get_ue_golomb_long(gb);
888  if ((ret = av_image_check_size(sps->width,
889  sps->height, 0, avctx)) < 0)
890  return ret;
891 
892  if (get_bits1(gb)) { // pic_conformance_flag
893  int vert_mult = 1 + (sps->chroma_format_idc < 2);
894  int horiz_mult = 1 + (sps->chroma_format_idc < 3);
895  sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
896  sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
897  sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
898  sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
899 
900  if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
901  av_log(avctx, AV_LOG_DEBUG,
902  "discarding sps conformance window, "
903  "original values are l:%u r:%u t:%u b:%u\n",
908 
911  sps->pic_conf_win.top_offset =
912  sps->pic_conf_win.bottom_offset = 0;
913  }
914  sps->output_window = sps->pic_conf_win;
915  }
916 
917  sps->bit_depth = get_ue_golomb_long(gb) + 8;
918  bit_depth_chroma = get_ue_golomb_long(gb) + 8;
919  if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
920  av_log(avctx, AV_LOG_ERROR,
921  "Luma bit depth (%d) is different from chroma bit depth (%d), "
922  "this is unsupported.\n",
923  sps->bit_depth, bit_depth_chroma);
924  return AVERROR_INVALIDDATA;
925  }
926 
927  ret = map_pixel_format(avctx, sps);
928  if (ret < 0)
929  return ret;
930 
931  sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
932  if (sps->log2_max_poc_lsb > 16) {
933  av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
934  sps->log2_max_poc_lsb - 4);
935  return AVERROR_INVALIDDATA;
936  }
937 
938  sublayer_ordering_info = get_bits1(gb);
939  start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
940  for (i = start; i < sps->max_sub_layers; i++) {
944  if (sps->temporal_layer[i].max_dec_pic_buffering > (unsigned)HEVC_MAX_DPB_SIZE) {
945  av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
947  return AVERROR_INVALIDDATA;
948  }
950  av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
952  if (avctx->err_recognition & AV_EF_EXPLODE ||
953  sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
954  return AVERROR_INVALIDDATA;
955  }
957  }
958  }
959 
960  if (!sublayer_ordering_info) {
961  for (i = 0; i < start; i++) {
965  }
966  }
967 
968  sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
970  sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
971  log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
972  sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
973  sps->log2_min_tb_size;
974 
975  if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
976  av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
977  return AVERROR_INVALIDDATA;
978  }
979 
980  if (sps->log2_diff_max_min_coding_block_size > 30) {
981  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);
982  return AVERROR_INVALIDDATA;
983  }
984 
985  if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
986  av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
987  return AVERROR_INVALIDDATA;
988  }
989 
990  if (log2_diff_max_min_transform_block_size < 0 || log2_diff_max_min_transform_block_size > 30) {
991  av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size", log2_diff_max_min_transform_block_size);
992  return AVERROR_INVALIDDATA;
993  }
994 
997 
999  if (sps->scaling_list_enable_flag) {
1001 
1002  if (get_bits1(gb)) {
1003  ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
1004  if (ret < 0)
1005  return ret;
1006  }
1007  }
1008 
1009  sps->amp_enabled_flag = get_bits1(gb);
1010  sps->sao_enabled = get_bits1(gb);
1011 
1012  sps->pcm_enabled_flag = get_bits1(gb);
1013  if (sps->pcm_enabled_flag) {
1014  sps->pcm.bit_depth = get_bits(gb, 4) + 1;
1015  sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
1018  get_ue_golomb_long(gb);
1019  if (sps->pcm.bit_depth > sps->bit_depth) {
1020  av_log(avctx, AV_LOG_ERROR,
1021  "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
1022  sps->pcm.bit_depth, sps->bit_depth);
1023  return AVERROR_INVALIDDATA;
1024  }
1025 
1027  }
1028 
1029  sps->nb_st_rps = get_ue_golomb_long(gb);
1031  av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
1032  sps->nb_st_rps);
1033  return AVERROR_INVALIDDATA;
1034  }
1035  for (i = 0; i < sps->nb_st_rps; i++) {
1036  if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
1037  sps, 0)) < 0)
1038  return ret;
1039  }
1040 
1044  if (sps->num_long_term_ref_pics_sps > 31U) {
1045  av_log(avctx, AV_LOG_ERROR, "num_long_term_ref_pics_sps %d is out of range.\n",
1047  return AVERROR_INVALIDDATA;
1048  }
1049  for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
1050  sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
1052  }
1053  }
1054 
1057  sps->vui.sar = (AVRational){0, 1};
1058  vui_present = get_bits1(gb);
1059  if (vui_present)
1060  decode_vui(gb, avctx, apply_defdispwin, sps);
1061 
1062  if (get_bits1(gb)) { // sps_extension_flag
1063  int sps_extension_flag[1];
1064  for (i = 0; i < 1; i++)
1065  sps_extension_flag[i] = get_bits1(gb);
1066  skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7);
1067  if (sps_extension_flag[0]) {
1068  int extended_precision_processing_flag;
1069  int high_precision_offsets_enabled_flag;
1070  int cabac_bypass_alignment_enabled_flag;
1071 
1075 
1077 
1078  extended_precision_processing_flag = get_bits1(gb);
1079  if (extended_precision_processing_flag)
1080  av_log(avctx, AV_LOG_WARNING,
1081  "extended_precision_processing_flag not yet implemented\n");
1082 
1084  high_precision_offsets_enabled_flag = get_bits1(gb);
1085  if (high_precision_offsets_enabled_flag)
1086  av_log(avctx, AV_LOG_WARNING,
1087  "high_precision_offsets_enabled_flag not yet implemented\n");
1088 
1090 
1091  cabac_bypass_alignment_enabled_flag = get_bits1(gb);
1092  if (cabac_bypass_alignment_enabled_flag)
1093  av_log(avctx, AV_LOG_WARNING,
1094  "cabac_bypass_alignment_enabled_flag not yet implemented\n");
1095  }
1096  }
1097  if (apply_defdispwin) {
1102  }
1103 
1104  ow = &sps->output_window;
1105  if (ow->left_offset >= INT_MAX - ow->right_offset ||
1106  ow->top_offset >= INT_MAX - ow->bottom_offset ||
1107  ow->left_offset + ow->right_offset >= sps->width ||
1108  ow->top_offset + ow->bottom_offset >= sps->height) {
1109  av_log(avctx, AV_LOG_WARNING, "Invalid cropping offsets: %u/%u/%u/%u\n",
1110  ow->left_offset, ow->right_offset, ow->top_offset, ow->bottom_offset);
1111  if (avctx->err_recognition & AV_EF_EXPLODE) {
1112  return AVERROR_INVALIDDATA;
1113  }
1114  av_log(avctx, AV_LOG_WARNING,
1115  "Displaying the whole video surface.\n");
1116  memset(ow, 0, sizeof(*ow));
1117  memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
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 
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,
1226  }
1227 
1228  /* check if this is a repeat of an already parsed SPS, then keep the
1229  * original one.
1230  * otherwise drop all PPSes that depend on it */
1231  if (ps->sps_list[sps_id] &&
1232  !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
1233  av_buffer_unref(&sps_buf);
1234  } else {
1235  remove_sps(ps, sps_id);
1236  ps->sps_list[sps_id] = sps_buf;
1237  }
1238 
1239  return 0;
1240 }
1241 
1242 static void hevc_pps_free(void *opaque, uint8_t *data)
1243 {
1244  HEVCPPS *pps = (HEVCPPS*)data;
1245 
1246  av_freep(&pps->column_width);
1247  av_freep(&pps->row_height);
1248  av_freep(&pps->col_bd);
1249  av_freep(&pps->row_bd);
1250  av_freep(&pps->col_idxX);
1251  av_freep(&pps->ctb_addr_rs_to_ts);
1252  av_freep(&pps->ctb_addr_ts_to_rs);
1253  av_freep(&pps->tile_pos_rs);
1254  av_freep(&pps->tile_id);
1256 
1257  av_freep(&pps);
1258 }
1259 
1261  HEVCPPS *pps, HEVCSPS *sps) {
1262  int i;
1263 
1264  if (pps->transform_skip_enabled_flag) {
1266  }
1273  av_log(avctx, AV_LOG_ERROR,
1274  "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
1275  return AVERROR_INVALIDDATA;
1276  }
1277  for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
1278  pps->cb_qp_offset_list[i] = get_se_golomb_long(gb);
1279  if (pps->cb_qp_offset_list[i]) {
1280  av_log(avctx, AV_LOG_WARNING,
1281  "cb_qp_offset_list not tested yet.\n");
1282  }
1283  pps->cr_qp_offset_list[i] = get_se_golomb_long(gb);
1284  if (pps->cr_qp_offset_list[i]) {
1285  av_log(avctx, AV_LOG_WARNING,
1286  "cb_qp_offset_list not tested yet.\n");
1287  }
1288  }
1289  }
1292 
1293  return(0);
1294 }
1295 
1296 static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
1297  HEVCPPS *pps, HEVCSPS *sps)
1298 {
1299  int log2_diff;
1300  int pic_area_in_ctbs;
1301  int i, j, x, y, ctb_addr_rs, tile_id;
1302 
1303  // Inferred parameters
1304  pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1305  pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1306  pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1307  if (!pps->col_bd || !pps->row_bd || !pps->col_idxX)
1308  return AVERROR(ENOMEM);
1309 
1310  if (pps->uniform_spacing_flag) {
1311  if (!pps->column_width) {
1312  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1313  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1314  }
1315  if (!pps->column_width || !pps->row_height)
1316  return AVERROR(ENOMEM);
1317 
1318  for (i = 0; i < pps->num_tile_columns; i++) {
1319  pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1320  (i * sps->ctb_width) / pps->num_tile_columns;
1321  }
1322 
1323  for (i = 0; i < pps->num_tile_rows; i++) {
1324  pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1325  (i * sps->ctb_height) / pps->num_tile_rows;
1326  }
1327  }
1328 
1329  pps->col_bd[0] = 0;
1330  for (i = 0; i < pps->num_tile_columns; i++)
1331  pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1332 
1333  pps->row_bd[0] = 0;
1334  for (i = 0; i < pps->num_tile_rows; i++)
1335  pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1336 
1337  for (i = 0, j = 0; i < sps->ctb_width; i++) {
1338  if (i > pps->col_bd[j])
1339  j++;
1340  pps->col_idxX[i] = j;
1341  }
1342 
1343  /**
1344  * 6.5
1345  */
1346  pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1347 
1348  pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1349  pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1350  pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1351  pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
1352  if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1353  !pps->tile_id || !pps->min_tb_addr_zs_tab) {
1354  return AVERROR(ENOMEM);
1355  }
1356 
1357  for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1358  int tb_x = ctb_addr_rs % sps->ctb_width;
1359  int tb_y = ctb_addr_rs / sps->ctb_width;
1360  int tile_x = 0;
1361  int tile_y = 0;
1362  int val = 0;
1363 
1364  for (i = 0; i < pps->num_tile_columns; i++) {
1365  if (tb_x < pps->col_bd[i + 1]) {
1366  tile_x = i;
1367  break;
1368  }
1369  }
1370 
1371  for (i = 0; i < pps->num_tile_rows; i++) {
1372  if (tb_y < pps->row_bd[i + 1]) {
1373  tile_y = i;
1374  break;
1375  }
1376  }
1377 
1378  for (i = 0; i < tile_x; i++)
1379  val += pps->row_height[tile_y] * pps->column_width[i];
1380  for (i = 0; i < tile_y; i++)
1381  val += sps->ctb_width * pps->row_height[i];
1382 
1383  val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1384  tb_x - pps->col_bd[tile_x];
1385 
1386  pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1387  pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1388  }
1389 
1390  for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1391  for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1392  for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1393  for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1394  pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1395 
1396  pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1397  if (!pps->tile_pos_rs)
1398  return AVERROR(ENOMEM);
1399 
1400  for (j = 0; j < pps->num_tile_rows; j++)
1401  for (i = 0; i < pps->num_tile_columns; i++)
1402  pps->tile_pos_rs[j * pps->num_tile_columns + i] =
1403  pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1404 
1405  log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
1406  pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
1407  for (y = 0; y < sps->tb_mask+2; y++) {
1408  pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
1409  pps->min_tb_addr_zs_tab[y] = -1;
1410  }
1411  for (y = 0; y < sps->tb_mask+1; y++) {
1412  for (x = 0; x < sps->tb_mask+1; x++) {
1413  int tb_x = x >> log2_diff;
1414  int tb_y = y >> log2_diff;
1415  int rs = sps->ctb_width * tb_y + tb_x;
1416  int val = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
1417  for (i = 0; i < log2_diff; i++) {
1418  int m = 1 << i;
1419  val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1420  }
1421  pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
1422  }
1423  }
1424 
1425  return 0;
1426 }
1427 
1429  HEVCParamSets *ps)
1430 {
1431  HEVCSPS *sps = NULL;
1432  int i, ret = 0;
1433  unsigned int pps_id = 0;
1434  ptrdiff_t nal_size;
1435 
1436  AVBufferRef *pps_buf;
1437  HEVCPPS *pps = av_mallocz(sizeof(*pps));
1438 
1439  if (!pps)
1440  return AVERROR(ENOMEM);
1441 
1442  pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
1443  hevc_pps_free, NULL, 0);
1444  if (!pps_buf) {
1445  av_freep(&pps);
1446  return AVERROR(ENOMEM);
1447  }
1448 
1449  av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1450 
1451  nal_size = gb->buffer_end - gb->buffer;
1452  if (nal_size > sizeof(pps->data)) {
1453  av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "
1454  "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1455  nal_size, sizeof(pps->data));
1456  pps->data_size = sizeof(pps->data);
1457  } else {
1458  pps->data_size = nal_size;
1459  }
1460  memcpy(pps->data, gb->buffer, pps->data_size);
1461 
1462  // Default values
1464  pps->num_tile_columns = 1;
1465  pps->num_tile_rows = 1;
1466  pps->uniform_spacing_flag = 1;
1467  pps->disable_dbf = 0;
1468  pps->beta_offset = 0;
1469  pps->tc_offset = 0;
1471 
1472  // Coded parameters
1473  pps_id = get_ue_golomb_long(gb);
1474  if (pps_id >= HEVC_MAX_PPS_COUNT) {
1475  av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1476  ret = AVERROR_INVALIDDATA;
1477  goto err;
1478  }
1479  pps->sps_id = get_ue_golomb_long(gb);
1480  if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
1481  av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1482  ret = AVERROR_INVALIDDATA;
1483  goto err;
1484  }
1485  if (!ps->sps_list[pps->sps_id]) {
1486  av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
1487  ret = AVERROR_INVALIDDATA;
1488  goto err;
1489  }
1490  sps = (HEVCSPS *)ps->sps_list[pps->sps_id]->data;
1491 
1494  pps->num_extra_slice_header_bits = get_bits(gb, 3);
1495 
1496  pps->sign_data_hiding_flag = get_bits1(gb);
1497 
1499 
1502 
1504 
1507 
1509  pps->diff_cu_qp_delta_depth = 0;
1510  if (pps->cu_qp_delta_enabled_flag)
1512 
1513  if (pps->diff_cu_qp_delta_depth < 0 ||
1515  av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
1516  pps->diff_cu_qp_delta_depth);
1517  ret = AVERROR_INVALIDDATA;
1518  goto err;
1519  }
1520 
1521  pps->cb_qp_offset = get_se_golomb(gb);
1522  if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1523  av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1524  pps->cb_qp_offset);
1525  ret = AVERROR_INVALIDDATA;
1526  goto err;
1527  }
1528  pps->cr_qp_offset = get_se_golomb(gb);
1529  if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1530  av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1531  pps->cr_qp_offset);
1532  ret = AVERROR_INVALIDDATA;
1533  goto err;
1534  }
1536 
1537  pps->weighted_pred_flag = get_bits1(gb);
1538  pps->weighted_bipred_flag = get_bits1(gb);
1539 
1541  pps->tiles_enabled_flag = get_bits1(gb);
1543 
1544  if (pps->tiles_enabled_flag) {
1545  pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
1546  pps->num_tile_rows = get_ue_golomb_long(gb) + 1;
1547  if (pps->num_tile_columns <= 0 ||
1548  pps->num_tile_columns >= sps->width) {
1549  av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1550  pps->num_tile_columns - 1);
1551  ret = AVERROR_INVALIDDATA;
1552  goto err;
1553  }
1554  if (pps->num_tile_rows <= 0 ||
1555  pps->num_tile_rows >= sps->height) {
1556  av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1557  pps->num_tile_rows - 1);
1558  ret = AVERROR_INVALIDDATA;
1559  goto err;
1560  }
1561 
1562  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1563  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1564  if (!pps->column_width || !pps->row_height) {
1565  ret = AVERROR(ENOMEM);
1566  goto err;
1567  }
1568 
1569  pps->uniform_spacing_flag = get_bits1(gb);
1570  if (!pps->uniform_spacing_flag) {
1571  uint64_t sum = 0;
1572  for (i = 0; i < pps->num_tile_columns - 1; i++) {
1573  pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1574  sum += pps->column_width[i];
1575  }
1576  if (sum >= sps->ctb_width) {
1577  av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1578  ret = AVERROR_INVALIDDATA;
1579  goto err;
1580  }
1581  pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1582 
1583  sum = 0;
1584  for (i = 0; i < pps->num_tile_rows - 1; i++) {
1585  pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1586  sum += pps->row_height[i];
1587  }
1588  if (sum >= sps->ctb_height) {
1589  av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1590  ret = AVERROR_INVALIDDATA;
1591  goto err;
1592  }
1593  pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1594  }
1596  }
1597 
1599 
1603  pps->disable_dbf = get_bits1(gb);
1604  if (!pps->disable_dbf) {
1605  int beta_offset_div2 = get_se_golomb(gb);
1606  int tc_offset_div2 = get_se_golomb(gb) ;
1607  if (beta_offset_div2 < -6 || beta_offset_div2 > 6) {
1608  av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1609  beta_offset_div2);
1610  ret = AVERROR_INVALIDDATA;
1611  goto err;
1612  }
1613  if (tc_offset_div2 < -6 || tc_offset_div2 > 6) {
1614  av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1615  tc_offset_div2);
1616  ret = AVERROR_INVALIDDATA;
1617  goto err;
1618  }
1619  pps->beta_offset = 2 * beta_offset_div2;
1620  pps->tc_offset = 2 * tc_offset_div2;
1621  }
1622  }
1623 
1625  if (pps->scaling_list_data_present_flag) {
1627  ret = scaling_list_data(gb, avctx, &pps->scaling_list, sps);
1628  if (ret < 0)
1629  goto err;
1630  }
1633  if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
1634  av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1635  pps->log2_parallel_merge_level - 2);
1636  ret = AVERROR_INVALIDDATA;
1637  goto err;
1638  }
1639 
1641 
1642  if (get_bits1(gb)) { // pps_extension_present_flag
1643  int pps_range_extensions_flag = get_bits1(gb);
1644  /* int pps_extension_7bits = */ get_bits(gb, 7);
1645  if (sps->ptl.general_ptl.profile_idc == FF_PROFILE_HEVC_REXT && pps_range_extensions_flag) {
1646  if ((ret = pps_range_extensions(gb, avctx, pps, sps)) < 0)
1647  goto err;
1648  }
1649  }
1650 
1651  ret = setup_pps(avctx, gb, pps, sps);
1652  if (ret < 0)
1653  goto err;
1654 
1655  if (get_bits_left(gb) < 0) {
1656  av_log(avctx, AV_LOG_ERROR,
1657  "Overread PPS by %d bits\n", -get_bits_left(gb));
1658  goto err;
1659  }
1660 
1661  remove_pps(ps, pps_id);
1662  ps->pps_list[pps_id] = pps_buf;
1663 
1664  return 0;
1665 
1666 err:
1667  av_buffer_unref(&pps_buf);
1668  return ret;
1669 }
1670 
1671 int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
1672 {
1673  int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
1674  int prev_poc_lsb = pocTid0 % max_poc_lsb;
1675  int prev_poc_msb = pocTid0 - prev_poc_lsb;
1676  int poc_msb;
1677 
1678  if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
1679  poc_msb = prev_poc_msb + max_poc_lsb;
1680  else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
1681  poc_msb = prev_poc_msb - max_poc_lsb;
1682  else
1683  poc_msb = prev_poc_msb;
1684 
1685  // For BLA picture types, POCmsb is set to 0.
1686  if (nal_unit_type == HEVC_NAL_BLA_W_LP ||
1687  nal_unit_type == HEVC_NAL_BLA_W_RADL ||
1688  nal_unit_type == HEVC_NAL_BLA_N_LP)
1689  poc_msb = 0;
1690 
1691  return poc_msb + poc_lsb;
1692 }
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:189
const HEVCPPS * pps
Definition: hevc_ps.h:400
#define NULL
Definition: coverity.c:32
unsigned int log2_min_cb_size
Definition: hevc_ps.h:275
const char const char void * val
Definition: avisynth_c.h:771
uint8_t log2_sao_offset_scale_luma
Definition: hevc_ps.h:371
const char * s
Definition: avisynth_c.h:768
int min_spatial_segmentation_idc
Definition: hevc_ps.h:168
#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:2337
int ctb_height
Definition: hevc_ps.h:296
AVBufferRef * vps_list[HEVC_MAX_VPS_COUNT]
Definition: hevc_ps.h:393
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
uint8_t diff_cu_chroma_qp_offset_depth
Definition: hevc_ps.h:367
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:161
int max_dec_pic_buffering
Definition: hevc_ps.h:243
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:285
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
int8_t cb_qp_offset_list[5]
Definition: hevc_ps.h:369
const char * desc
Definition: nvenc.c:60
unsigned int * row_height
RowHeight.
Definition: hevc_ps.h:376
ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_RPS_COUNT]
Definition: hevc_ps.h:255
int vps_num_ticks_poc_diff_one
vps_num_ticks_poc_diff_one_minus1 + 1
Definition: hevc_ps.h:211
int pic_init_qp_minus26
Definition: hevc_ps.h:324
unsigned int vps_max_latency_increase[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:204
VUI vui
Definition: hevc_ps.h:248
int vshift[3]
Definition: hevc_ps.h:307
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
int num
Numerator.
Definition: rational.h:59
#define FF_DEBUG_BITSTREAM
Definition: avcodec.h:2990
uint32_t vui_time_scale
Definition: hevc_ps.h:159
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:379
uint8_t weighted_bipred_flag
Definition: hevc_ps.h:336
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:126
Definition: hevc_ps.h:187
unsigned int vps_max_dec_pic_buffering[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:202
uint8_t seq_loop_filter_across_slices_enabled_flag
Definition: hevc_ps.h:349
uint8_t cabac_init_present_flag
Definition: hevc_ps.h:320
int chroma_loc_info_present_flag
Definition: hevc_ps.h:146
#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:325
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:367
int max_latency_increase
Definition: hevc_ps.h:245
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:299
int * ctb_addr_ts_to_rs
CtbAddrTSToRS.
Definition: hevc_ps.h:382
int explicit_rdpcm_enabled_flag
Definition: hevc_ps.h:288
int num_ref_idx_l0_default_active
num_ref_idx_l0_default_active_minus1 + 1
Definition: hevc_ps.h:322
static const AVRational vui_sar[]
Definition: hevc_ps.c:53
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:467
int chroma_sample_loc_type_top_field
Definition: hevc_ps.h:147
int width
Definition: hevc_ps.h:293
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
const char * av_color_space_name(enum AVColorSpace space)
Definition: pixdesc.c:2676
int qp_bd_offset
Definition: hevc_ps.h:309
int pixel_shift
Definition: hevc_ps.h:235
uint8_t entropy_coding_sync_enabled_flag
Definition: hevc_ps.h:342
#define HEVC_MAX_VPS_COUNT
Definition: hevc.h:65
HEVCWindow output_window
Definition: hevc_ps.h:230
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:345
int log2_parallel_merge_level
log2_parallel_merge_level_minus2 + 2
Definition: hevc_ps.h:361
uint8_t log2_sao_offset_scale_chroma
Definition: hevc_ps.h:372
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:346
int restricted_ref_pic_lists_flag
Definition: hevc_ps.h:167
#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:227
uint8_t disable_dbf
Definition: hevc_ps.h:353
unsigned int log2_max_trafo_size
Definition: hevc_ps.h:278
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:394
uint8_t profile_compatibility_flag[32]
Definition: hevc_ps.h:179
Definition: hevc_ps.h:132
static int decode_hrd(GetBitContext *gb, int common_inf_present, int max_sublayers)
Definition: hevc_ps.c:342
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:232
uint8_t log2_max_transform_skip_block_size
Definition: hevc_ps.h:364
int overscan_info_present_flag
Definition: hevc_ps.h:135
uint8_t vps_timing_info_present_flag
Definition: hevc_ps.h:207
uint8_t matrix_coeffs
Definition: hevc_ps.h:144
int min_tb_width
Definition: hevc_ps.h:300
#define FF_PROFILE_HEVC_MAIN
Definition: avcodec.h:3337
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:833
const HEVCVPS * vps
Definition: hevc_ps.h:398
int num_ref_idx_l1_default_active
num_ref_idx_l1_default_active_minus1 + 1
Definition: hevc_ps.h:323
unsigned int log2_min_pcm_cb_size
Definition: hevc_ps.h:268
struct HEVCSPS::@69 temporal_layer[HEVC_MAX_SUB_LAYERS]
int min_cb_width
Definition: hevc_ps.h:298
int vps_max_sub_layers
vps_max_temporal_layers_minus1 + 1
Definition: hevc_ps.h:198
int frame_field_info_present_flag
Definition: hevc_ps.h:152
uint8_t tier_flag
Definition: hevc_ps.h:177
uint8_t scaling_list_data_present_flag
Definition: hevc_ps.h:357
uint8_t bit_depth_chroma
Definition: hevc_ps.h:267
AVBufferRef * pps_list[HEVC_MAX_PPS_COUNT]
Definition: hevc_ps.h:395
uint8_t loop_filter_disable_flag
Definition: hevc_ps.h:270
static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
Definition: hevc_ps.c:784
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:368
#define FF_PROFILE_HEVC_MAIN_10
Definition: avcodec.h:3338
int bitstream_restriction_flag
Definition: hevc_ps.h:164
uint8_t transquant_bypass_enable_flag
Definition: hevc_ps.h:338
#define av_log(a,...)
int8_t cr_qp_offset_list[5]
Definition: hevc_ps.h:370
int vps_max_layer_id
Definition: hevc_ps.h:205
#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:184
#define AV_CODEC_FLAG2_IGNORE_CROP
Discard cropping information from SPS.
Definition: avcodec.h:966
unsigned int log2_max_poc_lsb
Definition: hevc_ps.h:238
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:197
int min_pu_height
Definition: hevc_ps.h:303
int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
Compute POC of the current frame and return it.
Definition: hevc_ps.c:1671
#define PTRDIFF_SPECIFIER
Definition: internal.h:254
int persistent_rice_adaptation_enabled_flag
coded frame dimension in various units
Definition: hevc_ps.h:290
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:404
#define AVERROR(e)
Definition: error.h:43
int data_size
Definition: hevc_ps.h:389
int vui_timing_info_present_flag
Definition: hevc_ps.h:157
uint8_t amp_enabled_flag
Definition: hevc_ps.h:257
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
unsigned int log2_ctb_size
Definition: hevc_ps.h:279
int vui_poc_proportional_to_timing_flag
Definition: hevc_ps.h:160
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:366
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:196
#define HEVC_MAX_LOG2_CTB_SIZE
Definition: hevc.h:74
uint8_t colour_primaries
Definition: hevc_ps.h:142
uint8_t used[32]
Definition: hevc_ps.h:39
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
Definition: pixdesc.c:2664
int motion_vectors_over_pic_boundaries_flag
Definition: hevc_ps.h:166
uint8_t tiles_enabled_flag
Definition: hevc_ps.h:341
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:208
int * col_idxX
Definition: hevc_ps.h:379
uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:192
const HEVCSPS * sps
Definition: hevc_ps.h:399
int vps_num_layer_sets
vps_num_layer_sets_minus1 + 1
Definition: hevc_ps.h:206
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:361
uint8_t lists_modification_present_flag
Definition: hevc_ps.h:360
int * min_tb_addr_zs_tab
MinTbAddrZS.
Definition: hevc_ps.h:386
uint8_t profile_idc
Definition: hevc_ps.h:178
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:1260
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:3034
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:347
int max_transform_hierarchy_depth_inter
Definition: hevc_ps.h:282
#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:210
int num_tile_columns
num_tile_columns_minus1 + 1
Definition: hevc_ps.h:344
unsigned int top_offset
Definition: hevc_ps.h:128
int hshift[3]
Definition: hevc_ps.h:306
unsigned int vps_num_reorder_pics[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:203
#define FF_PROFILE_HEVC_MAIN_STILL_PICTURE
Definition: avcodec.h:3339
int32_t
int max_bits_per_min_cu_denom
Definition: hevc_ps.h:170
uint8_t cu_qp_delta_enabled_flag
Definition: hevc_ps.h:329
uint8_t used_by_curr_pic_lt_sps_flag[32]
Definition: hevc_ps.h:262
int ctb_width
Definition: hevc_ps.h:295
uint8_t sl_dc[2][6]
Definition: hevc_ps.h:222
int32_t delta_poc[32]
Definition: hevc_ps.h:38
uint8_t sign_data_hiding_flag
Definition: hevc_ps.h:318
int height
Definition: hevc_ps.h:294
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:3045
uint8_t output_flag_present_flag
Definition: hevc_ps.h:337
int log2_max_mv_length_vertical
Definition: hevc_ps.h:172
int data_size
Definition: hevc_ps.h:215
PTLCommon general_ptl
Definition: hevc_ps.h:188
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:191
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:362
int max_bytes_per_pic_denom
Definition: hevc_ps.h:169
int overscan_appropriate_flag
Definition: hevc_ps.h:136
static void set_default_scaling_list_data(ScalingList *sl)
Definition: hevc_ps.c:682
int implicit_rdpcm_enabled_flag
Definition: hevc_ps.h:287
unsigned vps_id
Definition: hevc_ps.h:226
#define FF_ARRAY_ELEMS(a)
uint8_t constrained_intra_pred_flag
Definition: hevc_ps.h:326
int tb_mask
Definition: hevc_ps.h:304
uint8_t sl[4][6][64]
Definition: hevc_ps.h:221
PTL ptl
Definition: hevc_ps.h:200
uint8_t pic_slice_level_chroma_qp_offsets_present_flag
Definition: hevc_ps.h:334
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:209
int colour_description_present_flag
Definition: hevc_ps.h:141
enum AVPixelFormat pix_fmt
Definition: hevc_ps.h:236
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:327
uint8_t uniform_spacing_flag
Definition: hevc_ps.h:346
int ctb_size
Definition: hevc_ps.h:297
static void decode_vui(GetBitContext *gb, AVCodecContext *avctx, int apply_defdispwin, HEVCSPS *sps)
Definition: hevc_ps.c:538
int * ctb_addr_rs_to_ts
CtbAddrRSToTS.
Definition: hevc_ps.h:381
PTL ptl
Definition: hevc_ps.h:249
int max_sub_layers
Definition: hevc_ps.h:241
unsigned int log2_min_pu_size
Definition: hevc_ps.h:280
unsigned int sps_id
seq_parameter_set_id
Definition: hevc_ps.h:316
int debug
debug
Definition: avcodec.h:2987
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:358
main external API structure.
Definition: avcodec.h:1746
static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx, PTL *ptl, int max_num_sub_layers)
Definition: hevc_ps.c:285
int log2_max_mv_length_horizontal
Definition: hevc_ps.h:171
uint8_t sao_enabled
Definition: hevc_ps.h:258
int num_extra_slice_header_bits
Definition: hevc_ps.h:362
uint8_t * data
The data buffer.
Definition: buffer.h:89
uint8_t loop_filter_across_tiles_enabled_flag
Definition: hevc_ps.h:347
uint8_t num_long_term_ref_pics_sps
Definition: hevc_ps.h:263
uint8_t cross_component_prediction_enabled_flag
Definition: hevc_ps.h:365
uint32_t vui_num_units_in_tick
Definition: hevc_ps.h:158
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:351
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
int transform_skip_context_enabled_flag
Definition: hevc_ps.h:286
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:338
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:363
uint8_t sps_temporal_mvp_enabled_flag
Definition: hevc_ps.h:272
unsigned int nb_st_rps
Definition: hevc_ps.h:254
int chroma_sample_loc_type_bottom_field
Definition: hevc_ps.h:148
int num_tile_rows
num_tile_rows_minus1 + 1
Definition: hevc_ps.h:345
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:366
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int vps_num_hrd_parameters
Definition: hevc_ps.h:212
#define HEVC_MAX_SPS_COUNT
Definition: hevc.h:66
static void hevc_pps_free(void *opaque, uint8_t *data)
Definition: hevc_ps.c:1242
unsigned int log2_min_tb_size
Definition: hevc_ps.h:277
uint16_t lt_ref_pic_poc_lsb_sps[32]
Definition: hevc_ps.h:261
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:1428
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:360
#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:1184
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:251
int * tile_id
TileId.
Definition: hevc_ps.h:383
static const uint8_t default_scaling_list_intra[]
Definition: hevc_ps.c:31
int tc_offset
tc_offset_div2 * 2
Definition: hevc_ps.h:355
uint8_t transfer_characteristic
Definition: hevc_ps.h:143
int default_display_window_flag
Definition: hevc_ps.h:154
int size
Size of data in bytes.
Definition: buffer.h:93
HEVCWindow def_disp_win
Definition: hevc_ps.h:155
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:380
int cr_qp_offset
Definition: hevc_ps.h:333
ScalingList scaling_list
Definition: hevc_ps.h:252
#define SIZE_SPECIFIER
Definition: internal.h:255
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:364
unsigned int log2_diff_max_min_coding_block_size
Definition: hevc_ps.h:276
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:370
static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx, ScalingList *sl, HEVCSPS *sps)
Definition: hevc_ps.c:712
unsigned int log2_max_pcm_cb_size
Definition: hevc_ps.h:269
static int setup_pps(AVCodecContext *avctx, GetBitContext *gb, HEVCPPS *pps, HEVCSPS *sps)
Definition: hevc_ps.c:1296
uint8_t level_idc
Definition: hevc_ps.h:180
int neutra_chroma_indication_flag
Definition: hevc_ps.h:149
int * tile_pos_rs
TilePosRS.
Definition: hevc_ps.h:384
const uint8_t ff_hevc_diag_scan4x4_x[16]
Definition: hevc_data.c:25
int data_size
Definition: hevc_ps.h:312
A reference to a data buffer.
Definition: buffer.h:81
int max_transform_hierarchy_depth_intra
Definition: hevc_ps.h:283
uint8_t profile_space
Definition: hevc_ps.h:176
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:378
int video_format
Definition: hevc_ps.h:139
uint8_t weighted_pred_flag
Definition: hevc_ps.h:335
uint8_t data[4096]
Definition: hevc_ps.h:214
unsigned int * col_bd
ColBd.
Definition: hevc_ps.h:377
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition: pixdesc.c:2670
const uint8_t * buffer_end
Definition: get_bits.h:56
unsigned int * column_width
ColumnWidth.
Definition: hevc_ps.h:375
uint8_t slice_header_extension_present_flag
Definition: hevc_ps.h:363
#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:245
int den
Denominator.
Definition: rational.h:60
int video_full_range_flag
Definition: hevc_ps.h:140
AVRational sar
Definition: hevc_ps.h:133
uint8_t chroma_qp_offset_list_len_minus1
Definition: hevc_ps.h:368
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:3340
uint8_t sps_strong_intra_smoothing_enable_flag
Definition: hevc_ps.h:273
int rps_idx_num_delta_pocs
Definition: hevc_ps.h:37
uint8_t data[4096]
Definition: hevc_ps.h:311
int min_pu_width
Definition: hevc_ps.h:302
uint8_t long_term_ref_pics_present_flag
Definition: hevc_ps.h:260
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1848
int vui_hrd_parameters_present_flag
Definition: hevc_ps.h:162
unsigned int right_offset
Definition: hevc_ps.h:127
int diff_cu_qp_delta_depth
Definition: hevc_ps.h:330
#define av_freep(p)
struct HEVCSPS::@70 pcm
void INT64 start
Definition: avisynth_c.h:690
int num_reorder_pics
Definition: hevc_ps.h:244
static void remove_sps(HEVCParamSets *s, int id)
Definition: hevc_ps.c:80
uint8_t progressive_source_flag
Definition: hevc_ps.h:181
int cb_qp_offset
Definition: hevc_ps.h:332
#define av_malloc_array(a, b)
int video_signal_type_present_flag
Definition: hevc_ps.h:138
uint8_t deblocking_filter_override_enabled_flag
Definition: hevc_ps.h:352
int bit_depth
Definition: hevc_ps.h:234
int beta_offset
beta_offset_div2 * 2
Definition: hevc_ps.h:354
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:2253
int min_tb_height
Definition: hevc_ps.h:301
int * min_tb_addr_zs
MinTbAddrZS.
Definition: hevc_ps.h:385
#define HEVC_MAX_REFS
Definition: hevc.h:71
uint8_t non_packed_constraint_flag
Definition: hevc_ps.h:183
int intra_smoothing_disabled_flag
Definition: hevc_ps.h:289
exp golomb vlc stuff
int pcm_enabled_flag
Definition: hevc_ps.h:239
enum AVCodecID id
int tiles_fixed_structure_flag
Definition: hevc_ps.h:165
uint8_t interlaced_source_flag
Definition: hevc_ps.h:182
int field_seq_flag
Definition: hevc_ps.h:151
for(j=16;j >0;--j)
uint8_t separate_colour_plane_flag
Definition: hevc_ps.h:228
unsigned int bottom_offset
Definition: hevc_ps.h:129
int vps_sub_layer_ordering_info_present_flag
Definition: hevc_ps.h:201
uint8_t data[4096]
Definition: hevc_ps.h:388
uint8_t dependent_slice_segments_enabled_flag
Definition: hevc_ps.h:340
static uint8_t tmp[11]
Definition: aes_ctr.c:26