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 
73  const HEVCSPS *sps, int is_slice_header)
74 {
75  HEVCLocalContext *lc = s->HEVClc;
76  uint8_t rps_predict = 0;
77  int delta_poc;
78  int k0 = 0;
79  int k1 = 0;
80  int k = 0;
81  int i;
82 
83  GetBitContext *gb = &lc->gb;
84 
85  if (rps != sps->st_rps && sps->nb_st_rps)
86  rps_predict = get_bits1(gb);
87 
88  if (rps_predict) {
89  const ShortTermRPS *rps_ridx;
90  int delta_rps;
91  unsigned abs_delta_rps;
92  uint8_t use_delta_flag = 0;
93  uint8_t delta_rps_sign;
94 
95  if (is_slice_header) {
96  unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
97  if (delta_idx > sps->nb_st_rps) {
99  "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
100  delta_idx, sps->nb_st_rps);
101  return AVERROR_INVALIDDATA;
102  }
103  rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
104  } else
105  rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
106 
107  delta_rps_sign = get_bits1(gb);
108  abs_delta_rps = get_ue_golomb_long(gb) + 1;
109  if (abs_delta_rps < 1 || abs_delta_rps > 32768) {
111  "Invalid value of abs_delta_rps: %d\n",
112  abs_delta_rps);
113  return AVERROR_INVALIDDATA;
114  }
115  delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
116  for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
117  int used = rps->used[k] = get_bits1(gb);
118 
119  if (!used)
120  use_delta_flag = get_bits1(gb);
121 
122  if (used || use_delta_flag) {
123  if (i < rps_ridx->num_delta_pocs)
124  delta_poc = delta_rps + rps_ridx->delta_poc[i];
125  else
126  delta_poc = delta_rps;
127  rps->delta_poc[k] = delta_poc;
128  if (delta_poc < 0)
129  k0++;
130  else
131  k1++;
132  k++;
133  }
134  }
135 
136  rps->num_delta_pocs = k;
137  rps->num_negative_pics = k0;
138  // sort in increasing order (smallest first)
139  if (rps->num_delta_pocs != 0) {
140  int used, tmp;
141  for (i = 1; i < rps->num_delta_pocs; i++) {
142  delta_poc = rps->delta_poc[i];
143  used = rps->used[i];
144  for (k = i - 1; k >= 0; k--) {
145  tmp = rps->delta_poc[k];
146  if (delta_poc < tmp) {
147  rps->delta_poc[k + 1] = tmp;
148  rps->used[k + 1] = rps->used[k];
149  rps->delta_poc[k] = delta_poc;
150  rps->used[k] = used;
151  }
152  }
153  }
154  }
155  if ((rps->num_negative_pics >> 1) != 0) {
156  int used;
157  k = rps->num_negative_pics - 1;
158  // flip the negative values to largest first
159  for (i = 0; i < rps->num_negative_pics >> 1; i++) {
160  delta_poc = rps->delta_poc[i];
161  used = rps->used[i];
162  rps->delta_poc[i] = rps->delta_poc[k];
163  rps->used[i] = rps->used[k];
164  rps->delta_poc[k] = delta_poc;
165  rps->used[k] = used;
166  k--;
167  }
168  }
169  } else {
170  unsigned int prev, nb_positive_pics;
172  nb_positive_pics = get_ue_golomb_long(gb);
173 
174  if (rps->num_negative_pics >= MAX_REFS ||
175  nb_positive_pics >= MAX_REFS) {
176  av_log(s->avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
177  return AVERROR_INVALIDDATA;
178  }
179 
180  rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
181  if (rps->num_delta_pocs) {
182  prev = 0;
183  for (i = 0; i < rps->num_negative_pics; i++) {
184  delta_poc = get_ue_golomb_long(gb) + 1;
185  prev -= delta_poc;
186  rps->delta_poc[i] = prev;
187  rps->used[i] = get_bits1(gb);
188  }
189  prev = 0;
190  for (i = 0; i < nb_positive_pics; i++) {
191  delta_poc = get_ue_golomb_long(gb) + 1;
192  prev += delta_poc;
193  rps->delta_poc[rps->num_negative_pics + i] = prev;
194  rps->used[rps->num_negative_pics + i] = get_bits1(gb);
195  }
196  }
197  }
198  return 0;
199 }
200 
201 
203 {
204  int i;
205  HEVCLocalContext *lc = s->HEVClc;
206  GetBitContext *gb = &lc->gb;
207 
208  ptl->profile_space = get_bits(gb, 2);
209  ptl->tier_flag = get_bits1(gb);
210  ptl->profile_idc = get_bits(gb, 5);
211  if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
212  av_log(s->avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
213  else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
214  av_log(s->avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
216  av_log(s->avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
217  else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT)
218  av_log(s->avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n");
219  else
220  av_log(s->avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
221 
222  for (i = 0; i < 32; i++)
228 
229  skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15]
230  skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31]
231  skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43]
232 }
233 
234 static void parse_ptl(HEVCContext *s, PTL *ptl, int max_num_sub_layers)
235 {
236  int i;
237  HEVCLocalContext *lc = s->HEVClc;
238  GetBitContext *gb = &lc->gb;
240  ptl->general_ptl.level_idc = get_bits(gb, 8);
241 
242  for (i = 0; i < max_num_sub_layers - 1; i++) {
245  }
246  if (max_num_sub_layers - 1> 0)
247  for (i = max_num_sub_layers - 1; i < 8; i++)
248  skip_bits(gb, 2); // reserved_zero_2bits[i]
249  for (i = 0; i < max_num_sub_layers - 1; i++) {
250  if (ptl->sub_layer_profile_present_flag[i])
252  if (ptl->sub_layer_level_present_flag[i])
253  ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
254  }
255 }
256 
257 static void decode_sublayer_hrd(HEVCContext *s, unsigned int nb_cpb,
258  int subpic_params_present)
259 {
260  GetBitContext *gb = &s->HEVClc->gb;
261  int i;
262 
263  for (i = 0; i < nb_cpb; i++) {
264  get_ue_golomb_long(gb); // bit_rate_value_minus1
265  get_ue_golomb_long(gb); // cpb_size_value_minus1
266 
267  if (subpic_params_present) {
268  get_ue_golomb_long(gb); // cpb_size_du_value_minus1
269  get_ue_golomb_long(gb); // bit_rate_du_value_minus1
270  }
271  skip_bits1(gb); // cbr_flag
272  }
273 }
274 
275 static int decode_hrd(HEVCContext *s, int common_inf_present,
276  int max_sublayers)
277 {
278  GetBitContext *gb = &s->HEVClc->gb;
279  int nal_params_present = 0, vcl_params_present = 0;
280  int subpic_params_present = 0;
281  int i;
282 
283  if (common_inf_present) {
284  nal_params_present = get_bits1(gb);
285  vcl_params_present = get_bits1(gb);
286 
287  if (nal_params_present || vcl_params_present) {
288  subpic_params_present = get_bits1(gb);
289 
290  if (subpic_params_present) {
291  skip_bits(gb, 8); // tick_divisor_minus2
292  skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
293  skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
294  skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
295  }
296 
297  skip_bits(gb, 4); // bit_rate_scale
298  skip_bits(gb, 4); // cpb_size_scale
299 
300  if (subpic_params_present)
301  skip_bits(gb, 4); // cpb_size_du_scale
302 
303  skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
304  skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
305  skip_bits(gb, 5); // dpb_output_delay_length_minus1
306  }
307  }
308 
309  for (i = 0; i < max_sublayers; i++) {
310  int low_delay = 0;
311  unsigned int nb_cpb = 1;
312  int fixed_rate = get_bits1(gb);
313 
314  if (!fixed_rate)
315  fixed_rate = get_bits1(gb);
316 
317  if (fixed_rate)
318  get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
319  else
320  low_delay = get_bits1(gb);
321 
322  if (!low_delay) {
323  nb_cpb = get_ue_golomb_long(gb) + 1;
324  if (nb_cpb < 1 || nb_cpb > 32) {
325  av_log(s->avctx, AV_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb);
326  return AVERROR_INVALIDDATA;
327  }
328  }
329 
330  if (nal_params_present)
331  decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
332  if (vcl_params_present)
333  decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
334  }
335  return 0;
336 }
337 
339 {
340  int i,j;
341  GetBitContext *gb = &s->HEVClc->gb;
342  int vps_id = 0;
343  HEVCVPS *vps;
344  AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
345 
346  if (!vps_buf)
347  return AVERROR(ENOMEM);
348  vps = (HEVCVPS*)vps_buf->data;
349 
350  av_log(s->avctx, AV_LOG_DEBUG, "Decoding VPS\n");
351 
352  vps_id = get_bits(gb, 4);
353  if (vps_id >= MAX_VPS_COUNT) {
354  av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
355  goto err;
356  }
357 
358  if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
359  av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
360  goto err;
361  }
362 
363  vps->vps_max_layers = get_bits(gb, 6) + 1;
364  vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
366 
367  if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
368  av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
369  goto err;
370  }
371 
372  if (vps->vps_max_sub_layers > MAX_SUB_LAYERS) {
373  av_log(s->avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
374  vps->vps_max_sub_layers);
375  goto err;
376  }
377 
378  parse_ptl(s, &vps->ptl, vps->vps_max_sub_layers);
379 
381 
383  for (; i < vps->vps_max_sub_layers; i++) {
387 
389  av_log(s->avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
390  vps->vps_max_dec_pic_buffering[i] - 1);
391  goto err;
392  }
393  if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
394  av_log(s->avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
395  vps->vps_num_reorder_pics[i]);
397  goto err;
398  }
399  }
400 
401  vps->vps_max_layer_id = get_bits(gb, 6);
402  vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
403  if ((vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
404  av_log(s->avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
405  goto err;
406  }
407 
408  for (i = 1; i < vps->vps_num_layer_sets; i++)
409  for (j = 0; j <= vps->vps_max_layer_id; j++)
410  skip_bits(gb, 1); // layer_id_included_flag[i][j]
411 
413  if (vps->vps_timing_info_present_flag) {
414  vps->vps_num_units_in_tick = get_bits_long(gb, 32);
415  vps->vps_time_scale = get_bits_long(gb, 32);
420  for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
421  int common_inf_present = 1;
422 
423  get_ue_golomb_long(gb); // hrd_layer_set_idx
424  if (i)
425  common_inf_present = get_bits1(gb);
426  decode_hrd(s, common_inf_present, vps->vps_max_sub_layers);
427  }
428  }
429  get_bits1(gb); /* vps_extension_flag */
430 
431  av_buffer_unref(&s->vps_list[vps_id]);
432  s->vps_list[vps_id] = vps_buf;
433  return 0;
434 
435 err:
436  av_buffer_unref(&vps_buf);
437  return AVERROR_INVALIDDATA;
438 }
439 
440 static void decode_vui(HEVCContext *s, HEVCSPS *sps)
441 {
442  VUI *vui = &sps->vui;
443  GetBitContext *gb = &s->HEVClc->gb;
444  int sar_present;
445 
446  av_log(s->avctx, AV_LOG_DEBUG, "Decoding VUI\n");
447 
448  sar_present = get_bits1(gb);
449  if (sar_present) {
450  uint8_t sar_idx = get_bits(gb, 8);
451  if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
452  vui->sar = vui_sar[sar_idx];
453  else if (sar_idx == 255) {
454  vui->sar.num = get_bits(gb, 16);
455  vui->sar.den = get_bits(gb, 16);
456  } else
458  "Unknown SAR index: %u.\n", sar_idx);
459  }
460 
464 
467  vui->video_format = get_bits(gb, 3);
468  vui->video_full_range_flag = get_bits1(gb);
473  vui->colour_primaries = get_bits(gb, 8);
474  vui->transfer_characteristic = get_bits(gb, 8);
475  vui->matrix_coeffs = get_bits(gb, 8);
476 
477  // Set invalid values to "unspecified"
478  if (vui->colour_primaries >= AVCOL_PRI_NB)
482  if (vui->matrix_coeffs >= AVCOL_SPC_NB)
484  }
485  }
486 
488  if (vui->chroma_loc_info_present_flag) {
491  }
492 
494  vui->field_seq_flag = get_bits1(gb);
496 
498  if (vui->default_display_window_flag) {
499  //TODO: * 2 is only valid for 420
504 
505  if (s->apply_defdispwin &&
508  "discarding vui default display window, "
509  "original values are l:%u r:%u t:%u b:%u\n",
514 
517  vui->def_disp_win.top_offset =
518  vui->def_disp_win.bottom_offset = 0;
519  }
520  }
521 
523 
524  if (vui->vui_timing_info_present_flag) {
525  vui->vui_num_units_in_tick = get_bits_long(gb, 32);
526  vui->vui_time_scale = get_bits_long(gb, 32);
532  decode_hrd(s, 1, sps->max_sub_layers);
533  }
534 
536  if (vui->bitstream_restriction_flag) {
545  }
546 }
547 
549 {
550  int matrixId;
551 
552  for (matrixId = 0; matrixId < 6; matrixId++) {
553  // 4x4 default is 16
554  memset(sl->sl[0][matrixId], 16, 16);
555  sl->sl_dc[0][matrixId] = 16; // default for 16x16
556  sl->sl_dc[1][matrixId] = 16; // default for 32x32
557  }
558  memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
559  memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
560  memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
561  memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
562  memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
563  memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
564  memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
565  memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
566  memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
567  memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
568  memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
569  memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
570  memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
571  memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
572  memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
573  memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
574  memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
575  memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
576 }
577 
579 {
580  GetBitContext *gb = &s->HEVClc->gb;
581  uint8_t scaling_list_pred_mode_flag;
582  int32_t scaling_list_dc_coef[2][6];
583  int size_id, matrix_id, pos;
584  int i;
585 
586  for (size_id = 0; size_id < 4; size_id++)
587  for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
588  scaling_list_pred_mode_flag = get_bits1(gb);
589  if (!scaling_list_pred_mode_flag) {
590  unsigned int delta = get_ue_golomb_long(gb);
591  /* Only need to handle non-zero delta. Zero means default,
592  * which should already be in the arrays. */
593  if (delta) {
594  // Copy from previous array.
595  if (matrix_id < delta) {
597  "Invalid delta in scaling list data: %d.\n", delta);
598  return AVERROR_INVALIDDATA;
599  }
600 
601  memcpy(sl->sl[size_id][matrix_id],
602  sl->sl[size_id][matrix_id - delta],
603  size_id > 0 ? 64 : 16);
604  if (size_id > 1)
605  sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
606  }
607  } else {
608  int next_coef, coef_num;
609  int32_t scaling_list_delta_coef;
610 
611  next_coef = 8;
612  coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
613  if (size_id > 1) {
614  scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
615  next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
616  sl->sl_dc[size_id - 2][matrix_id] = next_coef;
617  }
618  for (i = 0; i < coef_num; i++) {
619  if (size_id == 0)
620  pos = 4 * ff_hevc_diag_scan4x4_y[i] +
622  else
623  pos = 8 * ff_hevc_diag_scan8x8_y[i] +
625 
626  scaling_list_delta_coef = get_se_golomb(gb);
627  next_coef = (next_coef + scaling_list_delta_coef + 256) % 256;
628  sl->sl[size_id][matrix_id][pos] = next_coef;
629  }
630  }
631  }
632 
633  if (sps->chroma_format_idc == 3) {
634  for (i = 0; i < 64; i++) {
635  sl->sl[3][1][i] = sl->sl[2][1][i];
636  sl->sl[3][2][i] = sl->sl[2][2][i];
637  sl->sl[3][4][i] = sl->sl[2][4][i];
638  sl->sl[3][5][i] = sl->sl[2][5][i];
639  }
640  sl->sl_dc[1][1] = sl->sl_dc[0][1];
641  sl->sl_dc[1][2] = sl->sl_dc[0][2];
642  sl->sl_dc[1][4] = sl->sl_dc[0][4];
643  sl->sl_dc[1][5] = sl->sl_dc[0][5];
644  }
645 
646 
647  return 0;
648 }
649 
651 {
652  const AVPixFmtDescriptor *desc;
653  GetBitContext *gb = &s->HEVClc->gb;
654  int ret = 0;
655  unsigned int sps_id = 0;
656  int log2_diff_max_min_transform_block_size;
657  int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
658  int i;
659 
660  HEVCSPS *sps;
661  AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
662 
663  if (!sps_buf)
664  return AVERROR(ENOMEM);
665  sps = (HEVCSPS*)sps_buf->data;
666 
667  av_log(s->avctx, AV_LOG_DEBUG, "Decoding SPS\n");
668 
669  // Coded parameters
670 
671  sps->vps_id = get_bits(gb, 4);
672  if (sps->vps_id >= MAX_VPS_COUNT) {
673  av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
674  ret = AVERROR_INVALIDDATA;
675  goto err;
676  }
677 
678  if (!s->vps_list[sps->vps_id]) {
679  av_log(s->avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
680  sps->vps_id);
681  ret = AVERROR_INVALIDDATA;
682  goto err;
683  }
684 
685  sps->max_sub_layers = get_bits(gb, 3) + 1;
686  if (sps->max_sub_layers > MAX_SUB_LAYERS) {
687  av_log(s->avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
688  sps->max_sub_layers);
689  ret = AVERROR_INVALIDDATA;
690  goto err;
691  }
692 
693  skip_bits1(gb); // temporal_id_nesting_flag
694 
695  parse_ptl(s, &sps->ptl, sps->max_sub_layers);
696 
697  sps_id = get_ue_golomb_long(gb);
698  if (sps_id >= MAX_SPS_COUNT) {
699  av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", sps_id);
700  ret = AVERROR_INVALIDDATA;
701  goto err;
702  }
703 
705  if (!(sps->chroma_format_idc == 1 || sps->chroma_format_idc == 2 || sps->chroma_format_idc == 3)) {
706  avpriv_report_missing_feature(s->avctx, "chroma_format_idc != {1, 2, 3}\n");
707  ret = AVERROR_PATCHWELCOME;
708  goto err;
709  }
710 
711  if (sps->chroma_format_idc == 3)
713 
715  sps->chroma_format_idc = 0;
716 
717  sps->width = get_ue_golomb_long(gb);
718  sps->height = get_ue_golomb_long(gb);
719  if ((ret = av_image_check_size(sps->width,
720  sps->height, 0, s->avctx)) < 0)
721  goto err;
722 
723  if (get_bits1(gb)) { // pic_conformance_flag
724  //TODO: * 2 is only valid for 420
729 
732  "discarding sps conformance window, "
733  "original values are l:%u r:%u t:%u b:%u\n",
738 
741  sps->pic_conf_win.top_offset =
742  sps->pic_conf_win.bottom_offset = 0;
743  }
744  sps->output_window = sps->pic_conf_win;
745  }
746 
747  sps->bit_depth = get_ue_golomb_long(gb) + 8;
748  bit_depth_chroma = get_ue_golomb_long(gb) + 8;
749  if (bit_depth_chroma != sps->bit_depth) {
751  "Luma bit depth (%d) is different from chroma bit depth (%d), "
752  "this is unsupported.\n",
753  sps->bit_depth, bit_depth_chroma);
754  ret = AVERROR_INVALIDDATA;
755  goto err;
756  }
757 
758  switch (sps->bit_depth) {
759  case 8:
760  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
761  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
762  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
763  break;
764  case 9:
765  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
766  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
767  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
768  break;
769  case 10:
770  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
771  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
772  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
773  break;
774  case 12:
775  if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
776  if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
777  if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
778  break;
779  default:
781  "4:2:0, 4:2:2, 4:4:4 supports are currently specified for 8, 10 and 12 bits.\n");
782  return AVERROR_PATCHWELCOME;
783  }
784 
785  desc = av_pix_fmt_desc_get(sps->pix_fmt);
786  if (!desc) {
787  ret = AVERROR(EINVAL);
788  goto err;
789  }
790 
791  sps->hshift[0] = sps->vshift[0] = 0;
792  sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
793  sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
794 
795  sps->pixel_shift = sps->bit_depth > 8;
796 
797  sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
798  if (sps->log2_max_poc_lsb > 16) {
799  av_log(s->avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
800  sps->log2_max_poc_lsb - 4);
801  ret = AVERROR_INVALIDDATA;
802  goto err;
803  }
804 
805  sublayer_ordering_info = get_bits1(gb);
806  start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
807  for (i = start; i < sps->max_sub_layers; i++) {
812  av_log(s->avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
814  ret = AVERROR_INVALIDDATA;
815  goto err;
816  }
818  av_log(s->avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
820  if (s->avctx->err_recognition & AV_EF_EXPLODE ||
822  ret = AVERROR_INVALIDDATA;
823  goto err;
824  }
826  }
827  }
828 
829  if (!sublayer_ordering_info) {
830  for (i = 0; i < start; i++) {
834  }
835  }
836 
837  sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
839  sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
840  log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
841  sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
842  sps->log2_min_tb_size;
843 
844  if (sps->log2_min_tb_size >= sps->log2_min_cb_size) {
845  av_log(s->avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
846  ret = AVERROR_INVALIDDATA;
847  goto err;
848  }
851 
853  if (sps->scaling_list_enable_flag) {
855 
856  if (get_bits1(gb)) {
857  ret = scaling_list_data(s, &sps->scaling_list, sps);
858  if (ret < 0)
859  goto err;
860  }
861  }
862 
863  sps->amp_enabled_flag = get_bits1(gb);
864  sps->sao_enabled = get_bits1(gb);
865 
866  sps->pcm_enabled_flag = get_bits1(gb);
867  if (sps->pcm_enabled_flag) {
868  sps->pcm.bit_depth = get_bits(gb, 4) + 1;
869  sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
872  get_ue_golomb_long(gb);
873  if (sps->pcm.bit_depth > sps->bit_depth) {
875  "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
876  sps->pcm.bit_depth, sps->bit_depth);
877  ret = AVERROR_INVALIDDATA;
878  goto err;
879  }
880 
882  }
883 
884  sps->nb_st_rps = get_ue_golomb_long(gb);
885  if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {
886  av_log(s->avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
887  sps->nb_st_rps);
888  ret = AVERROR_INVALIDDATA;
889  goto err;
890  }
891  for (i = 0; i < sps->nb_st_rps; i++) {
892  if ((ret = ff_hevc_decode_short_term_rps(s, &sps->st_rps[i],
893  sps, 0)) < 0)
894  goto err;
895  }
896 
900  for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
903  }
904  }
905 
908  sps->vui.sar = (AVRational){0, 1};
909  vui_present = get_bits1(gb);
910  if (vui_present)
911  decode_vui(s, sps);
912 
913  if (get_bits1(gb)) { // sps_extension_flag
914  int sps_extension_flag[1];
915  for (i = 0; i < 1; i++)
916  sps_extension_flag[i] = get_bits1(gb);
917  skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7);
918  if (sps_extension_flag[0]) {
919  int extended_precision_processing_flag;
920  int high_precision_offsets_enabled_flag;
921  int cabac_bypass_alignment_enabled_flag;
922 
926 
928 
929  extended_precision_processing_flag = get_bits1(gb);
930  if (extended_precision_processing_flag)
932  "extended_precision_processing_flag not yet implemented\n");
933 
935  high_precision_offsets_enabled_flag = get_bits1(gb);
936  if (high_precision_offsets_enabled_flag)
938  "high_precision_offsets_enabled_flag not yet implemented\n");
939 
941 
942  cabac_bypass_alignment_enabled_flag = get_bits1(gb);
943  if (cabac_bypass_alignment_enabled_flag)
945  "cabac_bypass_alignment_enabled_flag not yet implemented\n");
946  }
947  }
948  if (s->apply_defdispwin) {
953  }
954  if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&
955  !(s->avctx->flags & CODEC_FLAG_UNALIGNED)) {
956  sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));
957  av_log(s->avctx, AV_LOG_WARNING, "Reducing left output window to %d "
958  "chroma samples to preserve alignment.\n",
960  }
961  sps->output_width = sps->width -
963  sps->output_height = sps->height -
965  if (sps->output_width <= 0 || sps->output_height <= 0) {
966  av_log(s->avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n",
967  sps->output_width, sps->output_height);
968  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
969  ret = AVERROR_INVALIDDATA;
970  goto err;
971  }
973  "Displaying the whole video surface.\n");
976  sps->pic_conf_win.top_offset =
977  sps->pic_conf_win.bottom_offset = 0;
978  sps->output_width = sps->width;
979  sps->output_height = sps->height;
980  }
981 
982  // Inferred parameters
983  sps->log2_ctb_size = sps->log2_min_cb_size +
985  sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
986 
987  sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
988  sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
989  sps->ctb_size = sps->ctb_width * sps->ctb_height;
990 
991  sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
992  sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
993  sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
994  sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
995  sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
996  sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
997  sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
998 
999  sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1000 
1001  if (sps->width & ((1 << sps->log2_min_cb_size) - 1) ||
1002  sps->height & ((1 << sps->log2_min_cb_size) - 1)) {
1003  av_log(s->avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
1004  goto err;
1005  }
1006 
1007  if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) {
1008  av_log(s->avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1009  goto err;
1010  }
1012  av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1014  goto err;
1015  }
1017  av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1019  goto err;
1020  }
1021  if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1023  "max transform block size out of range: %d\n",
1024  sps->log2_max_trafo_size);
1025  goto err;
1026  }
1027 
1028  if (s->avctx->debug & FF_DEBUG_BITSTREAM) {
1030  "Parsed SPS: id %d; coded wxh: %dx%d; "
1031  "cropped wxh: %dx%d; pix_fmt: %s.\n",
1032  sps_id, sps->width, sps->height,
1033  sps->output_width, sps->output_height,
1035  }
1036 
1037  /* check if this is a repeat of an already parsed SPS, then keep the
1038  * original one.
1039  * otherwise drop all PPSes that depend on it */
1040  if (s->sps_list[sps_id] &&
1041  !memcmp(s->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
1042  av_buffer_unref(&sps_buf);
1043  } else {
1044  for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
1045  if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == sps_id)
1046  av_buffer_unref(&s->pps_list[i]);
1047  }
1048  if (s->sps_list[sps_id] && s->sps == (HEVCSPS*)s->sps_list[sps_id]->data) {
1050  s->current_sps = av_buffer_ref(s->sps_list[sps_id]);
1051  if (!s->current_sps)
1052  s->sps = NULL;
1053  }
1054  av_buffer_unref(&s->sps_list[sps_id]);
1055  s->sps_list[sps_id] = sps_buf;
1056  }
1057 
1058  return 0;
1059 
1060 err:
1061  av_buffer_unref(&sps_buf);
1062  return ret;
1063 }
1064 
1065 static void hevc_pps_free(void *opaque, uint8_t *data)
1066 {
1067  HEVCPPS *pps = (HEVCPPS*)data;
1068 
1069  av_freep(&pps->column_width);
1070  av_freep(&pps->row_height);
1071  av_freep(&pps->col_bd);
1072  av_freep(&pps->row_bd);
1073  av_freep(&pps->col_idxX);
1074  av_freep(&pps->ctb_addr_rs_to_ts);
1075  av_freep(&pps->ctb_addr_ts_to_rs);
1076  av_freep(&pps->tile_pos_rs);
1077  av_freep(&pps->tile_id);
1079 
1080  av_freep(&pps);
1081 }
1082 
1084  GetBitContext *gb = &s->HEVClc->gb;
1085  int i;
1086 
1087  if (pps->transform_skip_enabled_flag) {
1089  }
1097  "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
1098  return AVERROR_INVALIDDATA;
1099  }
1100  for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
1101  pps->cb_qp_offset_list[i] = get_se_golomb_long(gb);
1102  if (pps->cb_qp_offset_list[i]) {
1104  "cb_qp_offset_list not tested yet.\n");
1105  }
1106  pps->cr_qp_offset_list[i] = get_se_golomb_long(gb);
1107  if (pps->cr_qp_offset_list[i]) {
1109  "cb_qp_offset_list not tested yet.\n");
1110  }
1111  }
1112  }
1115 
1116  return(0);
1117 }
1118 
1120 {
1121  GetBitContext *gb = &s->HEVClc->gb;
1122  HEVCSPS *sps = NULL;
1123  int pic_area_in_ctbs;
1124  int log2_diff_ctb_min_tb_size;
1125  int i, j, x, y, ctb_addr_rs, tile_id;
1126  int ret = 0;
1127  unsigned int pps_id = 0;
1128 
1129  AVBufferRef *pps_buf;
1130  HEVCPPS *pps = av_mallocz(sizeof(*pps));
1131 
1132  if (!pps)
1133  return AVERROR(ENOMEM);
1134 
1135  pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
1136  hevc_pps_free, NULL, 0);
1137  if (!pps_buf) {
1138  av_freep(&pps);
1139  return AVERROR(ENOMEM);
1140  }
1141 
1142  av_log(s->avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1143 
1144  // Default values
1146  pps->num_tile_columns = 1;
1147  pps->num_tile_rows = 1;
1148  pps->uniform_spacing_flag = 1;
1149  pps->disable_dbf = 0;
1150  pps->beta_offset = 0;
1151  pps->tc_offset = 0;
1153 
1154  // Coded parameters
1155  pps_id = get_ue_golomb_long(gb);
1156  if (pps_id >= MAX_PPS_COUNT) {
1157  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1158  ret = AVERROR_INVALIDDATA;
1159  goto err;
1160  }
1161  pps->sps_id = get_ue_golomb_long(gb);
1162  if (pps->sps_id >= MAX_SPS_COUNT) {
1163  av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1164  ret = AVERROR_INVALIDDATA;
1165  goto err;
1166  }
1167  if (!s->sps_list[pps->sps_id]) {
1168  av_log(s->avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
1169  ret = AVERROR_INVALIDDATA;
1170  goto err;
1171  }
1172  sps = (HEVCSPS *)s->sps_list[pps->sps_id]->data;
1173 
1176  pps->num_extra_slice_header_bits = get_bits(gb, 3);
1177 
1178  pps->sign_data_hiding_flag = get_bits1(gb);
1179 
1181 
1184 
1186 
1189 
1191  pps->diff_cu_qp_delta_depth = 0;
1192  if (pps->cu_qp_delta_enabled_flag)
1194 
1195  pps->cb_qp_offset = get_se_golomb(gb);
1196  if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1197  av_log(s->avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1198  pps->cb_qp_offset);
1199  ret = AVERROR_INVALIDDATA;
1200  goto err;
1201  }
1202  pps->cr_qp_offset = get_se_golomb(gb);
1203  if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1204  av_log(s->avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1205  pps->cr_qp_offset);
1206  ret = AVERROR_INVALIDDATA;
1207  goto err;
1208  }
1210 
1211  pps->weighted_pred_flag = get_bits1(gb);
1212  pps->weighted_bipred_flag = get_bits1(gb);
1213 
1215  pps->tiles_enabled_flag = get_bits1(gb);
1217 
1218  if (pps->tiles_enabled_flag) {
1219  pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
1220  pps->num_tile_rows = get_ue_golomb_long(gb) + 1;
1221  if (pps->num_tile_columns == 0 ||
1222  pps->num_tile_columns >= sps->width) {
1223  av_log(s->avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1224  pps->num_tile_columns - 1);
1225  ret = AVERROR_INVALIDDATA;
1226  goto err;
1227  }
1228  if (pps->num_tile_rows == 0 ||
1229  pps->num_tile_rows >= sps->height) {
1230  av_log(s->avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1231  pps->num_tile_rows - 1);
1232  ret = AVERROR_INVALIDDATA;
1233  goto err;
1234  }
1235 
1236  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1237  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1238  if (!pps->column_width || !pps->row_height) {
1239  ret = AVERROR(ENOMEM);
1240  goto err;
1241  }
1242 
1243  pps->uniform_spacing_flag = get_bits1(gb);
1244  if (!pps->uniform_spacing_flag) {
1245  uint64_t sum = 0;
1246  for (i = 0; i < pps->num_tile_columns - 1; i++) {
1247  pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1248  sum += pps->column_width[i];
1249  }
1250  if (sum >= sps->ctb_width) {
1251  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1252  ret = AVERROR_INVALIDDATA;
1253  goto err;
1254  }
1255  pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1256 
1257  sum = 0;
1258  for (i = 0; i < pps->num_tile_rows - 1; i++) {
1259  pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1260  sum += pps->row_height[i];
1261  }
1262  if (sum >= sps->ctb_height) {
1263  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1264  ret = AVERROR_INVALIDDATA;
1265  goto err;
1266  }
1267  pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1268  }
1270  }
1271 
1273 
1277  pps->disable_dbf = get_bits1(gb);
1278  if (!pps->disable_dbf) {
1279  pps->beta_offset = get_se_golomb(gb) * 2;
1280  pps->tc_offset = get_se_golomb(gb) * 2;
1281  if (pps->beta_offset/2 < -6 || pps->beta_offset/2 > 6) {
1282  av_log(s->avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1283  pps->beta_offset/2);
1284  ret = AVERROR_INVALIDDATA;
1285  goto err;
1286  }
1287  if (pps->tc_offset/2 < -6 || pps->tc_offset/2 > 6) {
1288  av_log(s->avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1289  pps->tc_offset/2);
1290  ret = AVERROR_INVALIDDATA;
1291  goto err;
1292  }
1293  }
1294  }
1295 
1297  if (pps->scaling_list_data_present_flag) {
1299  ret = scaling_list_data(s, &pps->scaling_list, sps);
1300  if (ret < 0)
1301  goto err;
1302  }
1305  if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
1306  av_log(s->avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1307  pps->log2_parallel_merge_level - 2);
1308  ret = AVERROR_INVALIDDATA;
1309  goto err;
1310  }
1311 
1313 
1314  if (get_bits1(gb)) { // pps_extension_present_flag
1315  int pps_range_extensions_flag = get_bits1(gb);
1316  /* int pps_extension_7bits = */ get_bits(gb, 7);
1317  if (sps->ptl.general_ptl.profile_idc == FF_PROFILE_HEVC_REXT && pps_range_extensions_flag) {
1318  pps_range_extensions(s, pps, sps);
1319  }
1320  }
1321 
1322  // Inferred parameters
1323  pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1324  pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1325  pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1326  if (!pps->col_bd || !pps->row_bd || !pps->col_idxX) {
1327  ret = AVERROR(ENOMEM);
1328  goto err;
1329  }
1330 
1331  if (pps->uniform_spacing_flag) {
1332  if (!pps->column_width) {
1333  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1334  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1335  }
1336  if (!pps->column_width || !pps->row_height) {
1337  ret = AVERROR(ENOMEM);
1338  goto err;
1339  }
1340 
1341  for (i = 0; i < pps->num_tile_columns; i++) {
1342  pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1343  (i * sps->ctb_width) / pps->num_tile_columns;
1344  }
1345 
1346  for (i = 0; i < pps->num_tile_rows; i++) {
1347  pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1348  (i * sps->ctb_height) / pps->num_tile_rows;
1349  }
1350  }
1351 
1352  pps->col_bd[0] = 0;
1353  for (i = 0; i < pps->num_tile_columns; i++)
1354  pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1355 
1356  pps->row_bd[0] = 0;
1357  for (i = 0; i < pps->num_tile_rows; i++)
1358  pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1359 
1360  for (i = 0, j = 0; i < sps->ctb_width; i++) {
1361  if (i > pps->col_bd[j])
1362  j++;
1363  pps->col_idxX[i] = j;
1364  }
1365 
1366  /**
1367  * 6.5
1368  */
1369  pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1370 
1371  pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1372  pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1373  pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1374  pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
1375  if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1376  !pps->tile_id || !pps->min_tb_addr_zs_tab) {
1377  ret = AVERROR(ENOMEM);
1378  goto err;
1379  }
1380 
1381  for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1382  int tb_x = ctb_addr_rs % sps->ctb_width;
1383  int tb_y = ctb_addr_rs / sps->ctb_width;
1384  int tile_x = 0;
1385  int tile_y = 0;
1386  int val = 0;
1387 
1388  for (i = 0; i < pps->num_tile_columns; i++) {
1389  if (tb_x < pps->col_bd[i + 1]) {
1390  tile_x = i;
1391  break;
1392  }
1393  }
1394 
1395  for (i = 0; i < pps->num_tile_rows; i++) {
1396  if (tb_y < pps->row_bd[i + 1]) {
1397  tile_y = i;
1398  break;
1399  }
1400  }
1401 
1402  for (i = 0; i < tile_x; i++)
1403  val += pps->row_height[tile_y] * pps->column_width[i];
1404  for (i = 0; i < tile_y; i++)
1405  val += sps->ctb_width * pps->row_height[i];
1406 
1407  val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1408  tb_x - pps->col_bd[tile_x];
1409 
1410  pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1411  pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1412  }
1413 
1414  for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1415  for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1416  for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1417  for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1418  pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1419 
1420  pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1421  if (!pps->tile_pos_rs) {
1422  ret = AVERROR(ENOMEM);
1423  goto err;
1424  }
1425 
1426  for (j = 0; j < pps->num_tile_rows; j++)
1427  for (i = 0; i < pps->num_tile_columns; i++)
1428  pps->tile_pos_rs[j * pps->num_tile_columns + i] = pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1429 
1430  log2_diff_ctb_min_tb_size = sps->log2_ctb_size - sps->log2_min_tb_size;
1431  pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
1432  for (y = 0; y < sps->tb_mask+2; y++) {
1433  pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
1434  pps->min_tb_addr_zs_tab[y] = -1;
1435  }
1436  for (y = 0; y < sps->tb_mask+1; y++) {
1437  for (x = 0; x < sps->tb_mask+1; x++) {
1438  int tb_x = x >> log2_diff_ctb_min_tb_size;
1439  int tb_y = y >> log2_diff_ctb_min_tb_size;
1440  int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1441  int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1442  (log2_diff_ctb_min_tb_size * 2);
1443  for (i = 0; i < log2_diff_ctb_min_tb_size; i++) {
1444  int m = 1 << i;
1445  val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1446  }
1447  pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
1448  }
1449  }
1450 
1451  av_buffer_unref(&s->pps_list[pps_id]);
1452  s->pps_list[pps_id] = pps_buf;
1453 
1454  return 0;
1455 
1456 err:
1457  av_buffer_unref(&pps_buf);
1458  return ret;
1459 }