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, abs_delta_rps;
91  uint8_t use_delta_flag = 0;
92  uint8_t delta_rps_sign;
93 
94  if (is_slice_header) {
95  unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
96  if (delta_idx > sps->nb_st_rps) {
98  "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
99  delta_idx, sps->nb_st_rps);
100  return AVERROR_INVALIDDATA;
101  }
102  rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
103  } else
104  rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
105 
106  delta_rps_sign = get_bits1(gb);
107  abs_delta_rps = get_ue_golomb_long(gb) + 1;
108  delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
109  for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
110  int used = rps->used[k] = get_bits1(gb);
111 
112  if (!used)
113  use_delta_flag = get_bits1(gb);
114 
115  if (used || use_delta_flag) {
116  if (i < rps_ridx->num_delta_pocs)
117  delta_poc = delta_rps + rps_ridx->delta_poc[i];
118  else
119  delta_poc = delta_rps;
120  rps->delta_poc[k] = delta_poc;
121  if (delta_poc < 0)
122  k0++;
123  else
124  k1++;
125  k++;
126  }
127  }
128 
129  rps->num_delta_pocs = k;
130  rps->num_negative_pics = k0;
131  // sort in increasing order (smallest first)
132  if (rps->num_delta_pocs != 0) {
133  int used, tmp;
134  for (i = 1; i < rps->num_delta_pocs; i++) {
135  delta_poc = rps->delta_poc[i];
136  used = rps->used[i];
137  for (k = i - 1; k >= 0; k--) {
138  tmp = rps->delta_poc[k];
139  if (delta_poc < tmp) {
140  rps->delta_poc[k + 1] = tmp;
141  rps->used[k + 1] = rps->used[k];
142  rps->delta_poc[k] = delta_poc;
143  rps->used[k] = used;
144  }
145  }
146  }
147  }
148  if ((rps->num_negative_pics >> 1) != 0) {
149  int used;
150  k = rps->num_negative_pics - 1;
151  // flip the negative values to largest first
152  for (i = 0; i < rps->num_negative_pics >> 1; i++) {
153  delta_poc = rps->delta_poc[i];
154  used = rps->used[i];
155  rps->delta_poc[i] = rps->delta_poc[k];
156  rps->used[i] = rps->used[k];
157  rps->delta_poc[k] = delta_poc;
158  rps->used[k] = used;
159  k--;
160  }
161  }
162  } else {
163  unsigned int prev, nb_positive_pics;
165  nb_positive_pics = get_ue_golomb_long(gb);
166 
167  if (rps->num_negative_pics >= MAX_REFS ||
168  nb_positive_pics >= MAX_REFS) {
169  av_log(s->avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
170  return AVERROR_INVALIDDATA;
171  }
172 
173  rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
174  if (rps->num_delta_pocs) {
175  prev = 0;
176  for (i = 0; i < rps->num_negative_pics; i++) {
177  delta_poc = get_ue_golomb_long(gb) + 1;
178  prev -= delta_poc;
179  rps->delta_poc[i] = prev;
180  rps->used[i] = get_bits1(gb);
181  }
182  prev = 0;
183  for (i = 0; i < nb_positive_pics; i++) {
184  delta_poc = get_ue_golomb_long(gb) + 1;
185  prev += delta_poc;
186  rps->delta_poc[rps->num_negative_pics + i] = prev;
187  rps->used[rps->num_negative_pics + i] = get_bits1(gb);
188  }
189  }
190  }
191  return 0;
192 }
193 
194 
196 {
197  int i;
198  HEVCLocalContext *lc = s->HEVClc;
199  GetBitContext *gb = &lc->gb;
200 
201  ptl->profile_space = get_bits(gb, 2);
202  ptl->tier_flag = get_bits1(gb);
203  ptl->profile_idc = get_bits(gb, 5);
204  if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
205  av_log(s->avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
206  else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
207  av_log(s->avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
209  av_log(s->avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
210  else
211  av_log(s->avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
212 
213  for (i = 0; i < 32; i++)
219 
220  skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15]
221  skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31]
222  skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43]
223 }
224 
225 static void parse_ptl(HEVCContext *s, PTL *ptl, int max_num_sub_layers)
226 {
227  int i;
228  HEVCLocalContext *lc = s->HEVClc;
229  GetBitContext *gb = &lc->gb;
231  ptl->general_ptl.level_idc = get_bits(gb, 8);
232 
233  for (i = 0; i < max_num_sub_layers - 1; i++) {
236  }
237  if (max_num_sub_layers - 1> 0)
238  for (i = max_num_sub_layers - 1; i < 8; i++)
239  skip_bits(gb, 2); // reserved_zero_2bits[i]
240  for (i = 0; i < max_num_sub_layers - 1; i++) {
241  if (ptl->sub_layer_profile_present_flag[i])
243  if (ptl->sub_layer_level_present_flag[i])
244  ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
245  }
246 }
247 
248 static void decode_sublayer_hrd(HEVCContext *s, unsigned int nb_cpb,
249  int subpic_params_present)
250 {
251  GetBitContext *gb = &s->HEVClc->gb;
252  int i;
253 
254  for (i = 0; i < nb_cpb; i++) {
255  get_ue_golomb_long(gb); // bit_rate_value_minus1
256  get_ue_golomb_long(gb); // cpb_size_value_minus1
257 
258  if (subpic_params_present) {
259  get_ue_golomb_long(gb); // cpb_size_du_value_minus1
260  get_ue_golomb_long(gb); // bit_rate_du_value_minus1
261  }
262  skip_bits1(gb); // cbr_flag
263  }
264 }
265 
266 static void decode_hrd(HEVCContext *s, int common_inf_present,
267  int max_sublayers)
268 {
269  GetBitContext *gb = &s->HEVClc->gb;
270  int nal_params_present = 0, vcl_params_present = 0;
271  int subpic_params_present = 0;
272  int i;
273 
274  if (common_inf_present) {
275  nal_params_present = get_bits1(gb);
276  vcl_params_present = get_bits1(gb);
277 
278  if (nal_params_present || vcl_params_present) {
279  subpic_params_present = get_bits1(gb);
280 
281  if (subpic_params_present) {
282  skip_bits(gb, 8); // tick_divisor_minus2
283  skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
284  skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
285  skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
286  }
287 
288  skip_bits(gb, 4); // bit_rate_scale
289  skip_bits(gb, 4); // cpb_size_scale
290 
291  if (subpic_params_present)
292  skip_bits(gb, 4); // cpb_size_du_scale
293 
294  skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
295  skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
296  skip_bits(gb, 5); // dpb_output_delay_length_minus1
297  }
298  }
299 
300  for (i = 0; i < max_sublayers; i++) {
301  int low_delay = 0;
302  unsigned int nb_cpb = 1;
303  int fixed_rate = get_bits1(gb);
304 
305  if (!fixed_rate)
306  fixed_rate = get_bits1(gb);
307 
308  if (fixed_rate)
309  get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
310  else
311  low_delay = get_bits1(gb);
312 
313  if (!low_delay)
314  nb_cpb = get_ue_golomb_long(gb) + 1;
315 
316  if (nal_params_present)
317  decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
318  if (vcl_params_present)
319  decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
320  }
321 }
322 
324 {
325  int i,j;
326  GetBitContext *gb = &s->HEVClc->gb;
327  int vps_id = 0;
328  HEVCVPS *vps;
329  AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
330 
331  if (!vps_buf)
332  return AVERROR(ENOMEM);
333  vps = (HEVCVPS*)vps_buf->data;
334 
335  av_log(s->avctx, AV_LOG_DEBUG, "Decoding VPS\n");
336 
337  vps_id = get_bits(gb, 4);
338  if (vps_id >= MAX_VPS_COUNT) {
339  av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
340  goto err;
341  }
342 
343  if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
344  av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
345  goto err;
346  }
347 
348  vps->vps_max_layers = get_bits(gb, 6) + 1;
349  vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
351 
352  if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
353  av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
354  goto err;
355  }
356 
357  if (vps->vps_max_sub_layers > MAX_SUB_LAYERS) {
358  av_log(s->avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
359  vps->vps_max_sub_layers);
360  goto err;
361  }
362 
363  parse_ptl(s, &vps->ptl, vps->vps_max_sub_layers);
364 
366 
368  for (; i < vps->vps_max_sub_layers; i++) {
372 
373  if (vps->vps_max_dec_pic_buffering[i] > MAX_DPB_SIZE) {
374  av_log(s->avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
375  vps->vps_max_dec_pic_buffering[i] - 1);
376  goto err;
377  }
378  if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
379  av_log(s->avctx, AV_LOG_ERROR, "vps_max_num_reorder_pics out of range: %d\n",
380  vps->vps_num_reorder_pics[i]);
381  goto err;
382  }
383  }
384 
385  vps->vps_max_layer_id = get_bits(gb, 6);
386  vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
387  for (i = 1; i < vps->vps_num_layer_sets; i++)
388  for (j = 0; j <= vps->vps_max_layer_id; j++)
389  skip_bits(gb, 1); // layer_id_included_flag[i][j]
390 
392  if (vps->vps_timing_info_present_flag) {
393  vps->vps_num_units_in_tick = get_bits_long(gb, 32);
394  vps->vps_time_scale = get_bits_long(gb, 32);
399  for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
400  int common_inf_present = 1;
401 
402  get_ue_golomb_long(gb); // hrd_layer_set_idx
403  if (i)
404  common_inf_present = get_bits1(gb);
405  decode_hrd(s, common_inf_present, vps->vps_max_sub_layers);
406  }
407  }
408  get_bits1(gb); /* vps_extension_flag */
409 
410  av_buffer_unref(&s->vps_list[vps_id]);
411  s->vps_list[vps_id] = vps_buf;
412  return 0;
413 
414 err:
415  av_buffer_unref(&vps_buf);
416  return AVERROR_INVALIDDATA;
417 }
418 
419 static void decode_vui(HEVCContext *s, HEVCSPS *sps)
420 {
421  VUI *vui = &sps->vui;
422  GetBitContext *gb = &s->HEVClc->gb;
423  int sar_present;
424 
425  av_log(s->avctx, AV_LOG_DEBUG, "Decoding VUI\n");
426 
427  sar_present = get_bits1(gb);
428  if (sar_present) {
429  uint8_t sar_idx = get_bits(gb, 8);
430  if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
431  vui->sar = vui_sar[sar_idx];
432  else if (sar_idx == 255) {
433  vui->sar.num = get_bits(gb, 16);
434  vui->sar.den = get_bits(gb, 16);
435  } else
437  "Unknown SAR index: %u.\n", sar_idx);
438  }
439 
443 
446  vui->video_format = get_bits(gb, 3);
447  vui->video_full_range_flag = get_bits1(gb);
452  vui->colour_primaries = get_bits(gb, 8);
453  vui->transfer_characteristic = get_bits(gb, 8);
454  vui->matrix_coeffs = get_bits(gb, 8);
455 
456  // Set invalid values to "unspecified"
457  if (vui->colour_primaries >= AVCOL_PRI_NB)
461  if (vui->matrix_coeffs >= AVCOL_SPC_NB)
463  }
464  }
465 
467  if (vui->chroma_loc_info_present_flag) {
470  }
471 
473  vui->field_seq_flag = get_bits1(gb);
475 
477  if (vui->default_display_window_flag) {
478  //TODO: * 2 is only valid for 420
483 
484  if (s->apply_defdispwin &&
487  "discarding vui default display window, "
488  "original values are l:%u r:%u t:%u b:%u\n",
493 
496  vui->def_disp_win.top_offset =
497  vui->def_disp_win.bottom_offset = 0;
498  }
499  }
500 
502 
503  if (vui->vui_timing_info_present_flag) {
504  vui->vui_num_units_in_tick = get_bits_long(gb, 32);
505  vui->vui_time_scale = get_bits_long(gb, 32);
511  decode_hrd(s, 1, sps->max_sub_layers);
512  }
513 
515  if (vui->bitstream_restriction_flag) {
524  }
525 }
526 
528 {
529  int matrixId;
530 
531  for (matrixId = 0; matrixId < 6; matrixId++) {
532  // 4x4 default is 16
533  memset(sl->sl[0][matrixId], 16, 16);
534  sl->sl_dc[0][matrixId] = 16; // default for 16x16
535  sl->sl_dc[1][matrixId] = 16; // default for 32x32
536  }
537  memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
538  memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
539  memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
540  memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
541  memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
542  memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
543  memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
544  memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
545  memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
546  memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
547  memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
548  memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
549  memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
550  memcpy(sl->sl[3][1], default_scaling_list_inter, 64);
551 }
552 
554 {
555  GetBitContext *gb = &s->HEVClc->gb;
556  uint8_t scaling_list_pred_mode_flag[4][6];
557  int32_t scaling_list_dc_coef[2][6];
558  int size_id, matrix_id, i, pos;
559 
560  for (size_id = 0; size_id < 4; size_id++)
561  for (matrix_id = 0; matrix_id < (size_id == 3 ? 2 : 6); matrix_id++) {
562  scaling_list_pred_mode_flag[size_id][matrix_id] = get_bits1(gb);
563  if (!scaling_list_pred_mode_flag[size_id][matrix_id]) {
564  unsigned int delta = get_ue_golomb_long(gb);
565  /* Only need to handle non-zero delta. Zero means default,
566  * which should already be in the arrays. */
567  if (delta) {
568  // Copy from previous array.
569  if (matrix_id < delta) {
571  "Invalid delta in scaling list data: %d.\n", delta);
572  return AVERROR_INVALIDDATA;
573  }
574 
575  memcpy(sl->sl[size_id][matrix_id],
576  sl->sl[size_id][matrix_id - delta],
577  size_id > 0 ? 64 : 16);
578  if (size_id > 1)
579  sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
580  }
581  } else {
582  int next_coef, coef_num;
583  int32_t scaling_list_delta_coef;
584 
585  next_coef = 8;
586  coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
587  if (size_id > 1) {
588  scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
589  next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
590  sl->sl_dc[size_id - 2][matrix_id] = next_coef;
591  }
592  for (i = 0; i < coef_num; i++) {
593  if (size_id == 0)
594  pos = 4 * ff_hevc_diag_scan4x4_y[i] +
596  else
597  pos = 8 * ff_hevc_diag_scan8x8_y[i] +
599 
600  scaling_list_delta_coef = get_se_golomb(gb);
601  next_coef = (next_coef + scaling_list_delta_coef + 256) % 256;
602  sl->sl[size_id][matrix_id][pos] = next_coef;
603  }
604  }
605  }
606 
607  return 0;
608 }
609 
611 {
612  const AVPixFmtDescriptor *desc;
613  GetBitContext *gb = &s->HEVClc->gb;
614  int ret = 0;
615  unsigned int sps_id = 0;
616  int log2_diff_max_min_transform_block_size;
617  int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
618  int i;
619 
620  HEVCSPS *sps;
621  AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
622 
623  if (!sps_buf)
624  return AVERROR(ENOMEM);
625  sps = (HEVCSPS*)sps_buf->data;
626 
627  av_log(s->avctx, AV_LOG_DEBUG, "Decoding SPS\n");
628 
629  // Coded parameters
630 
631  sps->vps_id = get_bits(gb, 4);
632  if (sps->vps_id >= MAX_VPS_COUNT) {
633  av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
634  ret = AVERROR_INVALIDDATA;
635  goto err;
636  }
637 
638  if (!s->vps_list[sps->vps_id]) {
639  av_log(s->avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
640  sps->vps_id);
641  ret = AVERROR_INVALIDDATA;
642  goto err;
643  }
644 
645  sps->max_sub_layers = get_bits(gb, 3) + 1;
646  if (sps->max_sub_layers > MAX_SUB_LAYERS) {
647  av_log(s->avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
648  sps->max_sub_layers);
649  ret = AVERROR_INVALIDDATA;
650  goto err;
651  }
652 
653  skip_bits1(gb); // temporal_id_nesting_flag
654 
655  parse_ptl(s, &sps->ptl, sps->max_sub_layers);
656 
657  sps_id = get_ue_golomb_long(gb);
658  if (sps_id >= MAX_SPS_COUNT) {
659  av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", sps_id);
660  ret = AVERROR_INVALIDDATA;
661  goto err;
662  }
663 
665  if (sps->chroma_format_idc != 1) {
666  avpriv_report_missing_feature(s->avctx, "chroma_format_idc != 1\n");
667  ret = AVERROR_PATCHWELCOME;
668  goto err;
669  }
670 
671  if (sps->chroma_format_idc == 3)
673 
674  sps->width = get_ue_golomb_long(gb);
675  sps->height = get_ue_golomb_long(gb);
676  if ((ret = av_image_check_size(sps->width,
677  sps->height, 0, s->avctx)) < 0)
678  goto err;
679 
680  if (get_bits1(gb)) { // pic_conformance_flag
681  //TODO: * 2 is only valid for 420
686 
689  "discarding sps conformance window, "
690  "original values are l:%u r:%u t:%u b:%u\n",
695 
698  sps->pic_conf_win.top_offset =
699  sps->pic_conf_win.bottom_offset = 0;
700  }
701  sps->output_window = sps->pic_conf_win;
702  }
703 
704  sps->bit_depth = get_ue_golomb_long(gb) + 8;
705  bit_depth_chroma = get_ue_golomb_long(gb) + 8;
706  if (bit_depth_chroma != sps->bit_depth) {
708  "Luma bit depth (%d) is different from chroma bit depth (%d), "
709  "this is unsupported.\n",
710  sps->bit_depth, bit_depth_chroma);
711  ret = AVERROR_INVALIDDATA;
712  goto err;
713  }
714 
715  if (sps->chroma_format_idc == 1) {
716  switch (sps->bit_depth) {
717  case 8: sps->pix_fmt = AV_PIX_FMT_YUV420P; break;
718  case 9: sps->pix_fmt = AV_PIX_FMT_YUV420P9; break;
719  case 10: sps->pix_fmt = AV_PIX_FMT_YUV420P10; break;
720  default:
721  av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
722  sps->bit_depth);
723  ret = AVERROR_PATCHWELCOME;
724  goto err;
725  }
726  } else {
728  "non-4:2:0 support is currently unspecified.\n");
729  return AVERROR_PATCHWELCOME;
730  }
731 
732  desc = av_pix_fmt_desc_get(sps->pix_fmt);
733  if (!desc) {
734  ret = AVERROR(EINVAL);
735  goto err;
736  }
737 
738  sps->hshift[0] = sps->vshift[0] = 0;
739  sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
740  sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
741 
742  sps->pixel_shift = sps->bit_depth > 8;
743 
744  sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
745  if (sps->log2_max_poc_lsb > 16) {
746  av_log(s->avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
747  sps->log2_max_poc_lsb - 4);
748  ret = AVERROR_INVALIDDATA;
749  goto err;
750  }
751 
752  sublayer_ordering_info = get_bits1(gb);
753  start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
754  for (i = start; i < sps->max_sub_layers; i++) {
759  av_log(s->avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
761  ret = AVERROR_INVALIDDATA;
762  goto err;
763  }
765  av_log(s->avctx, AV_LOG_ERROR, "sps_max_num_reorder_pics out of range: %d\n",
767  if (sps->temporal_layer[i].num_reorder_pics > MAX_DPB_SIZE - 1) {
768  ret = AVERROR_INVALIDDATA;
769  goto err;
770  }
772  }
773  }
774 
775  if (!sublayer_ordering_info) {
776  for (i = 0; i < start; i++) {
780  }
781  }
782 
783  sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
785  sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
786  log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
787  sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
788  sps->log2_min_tb_size;
789 
790  if (sps->log2_min_tb_size >= sps->log2_min_cb_size) {
791  av_log(s->avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
792  ret = AVERROR_INVALIDDATA;
793  goto err;
794  }
797 
799  if (sps->scaling_list_enable_flag) {
801 
802  if (get_bits1(gb)) {
803  ret = scaling_list_data(s, &sps->scaling_list);
804  if (ret < 0)
805  goto err;
806  }
807  }
808 
809  sps->amp_enabled_flag = get_bits1(gb);
810  sps->sao_enabled = get_bits1(gb);
811 
812  sps->pcm_enabled_flag = get_bits1(gb);
813  if (sps->pcm_enabled_flag) {
814  sps->pcm.bit_depth = get_bits(gb, 4) + 1;
815  sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
818  get_ue_golomb_long(gb);
819  if (sps->pcm.bit_depth > sps->bit_depth) {
821  "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
822  sps->pcm.bit_depth, sps->bit_depth);
823  ret = AVERROR_INVALIDDATA;
824  goto err;
825  }
826 
828  }
829 
830  sps->nb_st_rps = get_ue_golomb_long(gb);
831  if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {
832  av_log(s->avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
833  sps->nb_st_rps);
834  ret = AVERROR_INVALIDDATA;
835  goto err;
836  }
837  for (i = 0; i < sps->nb_st_rps; i++) {
838  if ((ret = ff_hevc_decode_short_term_rps(s, &sps->st_rps[i],
839  sps, 0)) < 0)
840  goto err;
841  }
842 
846  for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
849  }
850  }
851 
854  sps->vui.sar = (AVRational){0, 1};
855  vui_present = get_bits1(gb);
856  if (vui_present)
857  decode_vui(s, sps);
858  skip_bits1(gb); // sps_extension_flag
859 
860  if (s->apply_defdispwin) {
865  }
866  if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&
867  !(s->avctx->flags & CODEC_FLAG_UNALIGNED)) {
868  sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));
869  av_log(s->avctx, AV_LOG_WARNING, "Reducing left output window to %d "
870  "chroma samples to preserve alignment.\n",
872  }
873  sps->output_width = sps->width -
875  sps->output_height = sps->height -
877  if (sps->output_width <= 0 || sps->output_height <= 0) {
878  av_log(s->avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n",
879  sps->output_width, sps->output_height);
880  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
881  ret = AVERROR_INVALIDDATA;
882  goto err;
883  }
885  "Displaying the whole video surface.\n");
888  sps->pic_conf_win.top_offset =
889  sps->pic_conf_win.bottom_offset = 0;
890  sps->output_width = sps->width;
891  sps->output_height = sps->height;
892  }
893 
894  // Inferred parameters
895  sps->log2_ctb_size = sps->log2_min_cb_size +
897  sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
898 
899  sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
900  sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
901  sps->ctb_size = sps->ctb_width * sps->ctb_height;
902 
903  sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
904  sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
905  sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
906  sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
907  sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
908  sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
909 
910  sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
911 
912  if (sps->width & ((1 << sps->log2_min_cb_size) - 1) ||
913  sps->height & ((1 << sps->log2_min_cb_size) - 1)) {
914  av_log(s->avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
915  goto err;
916  }
917 
918  if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) {
919  av_log(s->avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
920  goto err;
921  }
923  av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
925  goto err;
926  }
928  av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
930  goto err;
931  }
932  if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
934  "max transform block size out of range: %d\n",
935  sps->log2_max_trafo_size);
936  goto err;
937  }
938 
939  if (s->avctx->debug & FF_DEBUG_BITSTREAM) {
941  "Parsed SPS: id %d; coded wxh: %dx%d; "
942  "cropped wxh: %dx%d; pix_fmt: %s.\n",
943  sps_id, sps->width, sps->height,
944  sps->output_width, sps->output_height,
946  }
947 
948  /* check if this is a repeat of an already parsed SPS, then keep the
949  * original one.
950  * otherwise drop all PPSes that depend on it */
951  if (s->sps_list[sps_id] &&
952  !memcmp(s->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
953  av_buffer_unref(&sps_buf);
954  } else {
955  for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
956  if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == sps_id)
957  av_buffer_unref(&s->pps_list[i]);
958  }
959  av_buffer_unref(&s->sps_list[sps_id]);
960  s->sps_list[sps_id] = sps_buf;
961  }
962 
963  return 0;
964 
965 err:
966  av_buffer_unref(&sps_buf);
967  return ret;
968 }
969 
970 static void hevc_pps_free(void *opaque, uint8_t *data)
971 {
972  HEVCPPS *pps = (HEVCPPS*)data;
973 
974  av_freep(&pps->column_width);
975  av_freep(&pps->row_height);
976  av_freep(&pps->col_bd);
977  av_freep(&pps->row_bd);
978  av_freep(&pps->col_idxX);
981  av_freep(&pps->tile_pos_rs);
982  av_freep(&pps->tile_id);
983  av_freep(&pps->min_cb_addr_zs);
984  av_freep(&pps->min_tb_addr_zs);
985 
986  av_freep(&pps);
987 }
988 
990 {
991  GetBitContext *gb = &s->HEVClc->gb;
992  HEVCSPS *sps = NULL;
993  int pic_area_in_ctbs, pic_area_in_min_cbs, pic_area_in_min_tbs;
994  int log2_diff_ctb_min_tb_size;
995  int i, j, x, y, ctb_addr_rs, tile_id;
996  int ret = 0;
997  unsigned int pps_id = 0;
998 
999  AVBufferRef *pps_buf;
1000  HEVCPPS *pps = av_mallocz(sizeof(*pps));
1001 
1002  if (!pps)
1003  return AVERROR(ENOMEM);
1004 
1005  pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
1006  hevc_pps_free, NULL, 0);
1007  if (!pps_buf) {
1008  av_freep(&pps);
1009  return AVERROR(ENOMEM);
1010  }
1011 
1012  av_log(s->avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1013 
1014  // Default values
1016  pps->num_tile_columns = 1;
1017  pps->num_tile_rows = 1;
1018  pps->uniform_spacing_flag = 1;
1019  pps->disable_dbf = 0;
1020  pps->beta_offset = 0;
1021  pps->tc_offset = 0;
1022 
1023  // Coded parameters
1024  pps_id = get_ue_golomb_long(gb);
1025  if (pps_id >= MAX_PPS_COUNT) {
1026  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1027  ret = AVERROR_INVALIDDATA;
1028  goto err;
1029  }
1030  pps->sps_id = get_ue_golomb_long(gb);
1031  if (pps->sps_id >= MAX_SPS_COUNT) {
1032  av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1033  ret = AVERROR_INVALIDDATA;
1034  goto err;
1035  }
1036  if (!s->sps_list[pps->sps_id]) {
1037  av_log(s->avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
1038  ret = AVERROR_INVALIDDATA;
1039  goto err;
1040  }
1041  sps = (HEVCSPS *)s->sps_list[pps->sps_id]->data;
1042 
1045  pps->num_extra_slice_header_bits = get_bits(gb, 3);
1046 
1047  pps->sign_data_hiding_flag = get_bits1(gb);
1048 
1050 
1053 
1055 
1058 
1060  pps->diff_cu_qp_delta_depth = 0;
1061  if (pps->cu_qp_delta_enabled_flag)
1063 
1064  pps->cb_qp_offset = get_se_golomb(gb);
1065  if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1066  av_log(s->avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1067  pps->cb_qp_offset);
1068  ret = AVERROR_INVALIDDATA;
1069  goto err;
1070  }
1071  pps->cr_qp_offset = get_se_golomb(gb);
1072  if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1073  av_log(s->avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1074  pps->cr_qp_offset);
1075  ret = AVERROR_INVALIDDATA;
1076  goto err;
1077  }
1079 
1080  pps->weighted_pred_flag = get_bits1(gb);
1081  pps->weighted_bipred_flag = get_bits1(gb);
1082 
1084  pps->tiles_enabled_flag = get_bits1(gb);
1086 
1087  if (pps->tiles_enabled_flag) {
1088  pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
1089  pps->num_tile_rows = get_ue_golomb_long(gb) + 1;
1090  if (pps->num_tile_columns == 0 ||
1091  pps->num_tile_columns >= sps->width) {
1092  av_log(s->avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1093  pps->num_tile_columns - 1);
1094  ret = AVERROR_INVALIDDATA;
1095  goto err;
1096  }
1097  if (pps->num_tile_rows == 0 ||
1098  pps->num_tile_rows >= sps->height) {
1099  av_log(s->avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1100  pps->num_tile_rows - 1);
1101  ret = AVERROR_INVALIDDATA;
1102  goto err;
1103  }
1104 
1105  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1106  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1107  if (!pps->column_width || !pps->row_height) {
1108  ret = AVERROR(ENOMEM);
1109  goto err;
1110  }
1111 
1112  pps->uniform_spacing_flag = get_bits1(gb);
1113  if (!pps->uniform_spacing_flag) {
1114  uint64_t sum = 0;
1115  for (i = 0; i < pps->num_tile_columns - 1; i++) {
1116  pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1117  sum += pps->column_width[i];
1118  }
1119  if (sum >= sps->ctb_width) {
1120  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1121  ret = AVERROR_INVALIDDATA;
1122  goto err;
1123  }
1124  pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1125 
1126  sum = 0;
1127  for (i = 0; i < pps->num_tile_rows - 1; i++) {
1128  pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1129  sum += pps->row_height[i];
1130  }
1131  if (sum >= sps->ctb_height) {
1132  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1133  ret = AVERROR_INVALIDDATA;
1134  goto err;
1135  }
1136  pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1137  }
1139  }
1140 
1142 
1146  pps->disable_dbf = get_bits1(gb);
1147  if (!pps->disable_dbf) {
1148  pps->beta_offset = get_se_golomb(gb) * 2;
1149  pps->tc_offset = get_se_golomb(gb) * 2;
1150  if (pps->beta_offset/2 < -6 || pps->beta_offset/2 > 6) {
1151  av_log(s->avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1152  pps->beta_offset/2);
1153  ret = AVERROR_INVALIDDATA;
1154  goto err;
1155  }
1156  if (pps->tc_offset/2 < -6 || pps->tc_offset/2 > 6) {
1157  av_log(s->avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1158  pps->tc_offset/2);
1159  ret = AVERROR_INVALIDDATA;
1160  goto err;
1161  }
1162  }
1163  }
1164 
1166  if (pps->scaling_list_data_present_flag) {
1168  ret = scaling_list_data(s, &pps->scaling_list);
1169  if (ret < 0)
1170  goto err;
1171  }
1174  if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
1175  av_log(s->avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1176  pps->log2_parallel_merge_level - 2);
1177  ret = AVERROR_INVALIDDATA;
1178  goto err;
1179  }
1180 
1182  skip_bits1(gb); // pps_extension_flag
1183 
1184  // Inferred parameters
1185  pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1186  pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1187  pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1188  if (!pps->col_bd || !pps->row_bd || !pps->col_idxX) {
1189  ret = AVERROR(ENOMEM);
1190  goto err;
1191  }
1192 
1193  if (pps->uniform_spacing_flag) {
1194  if (!pps->column_width) {
1195  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1196  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1197  }
1198  if (!pps->column_width || !pps->row_height) {
1199  ret = AVERROR(ENOMEM);
1200  goto err;
1201  }
1202 
1203  for (i = 0; i < pps->num_tile_columns; i++) {
1204  pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1205  (i * sps->ctb_width) / pps->num_tile_columns;
1206  }
1207 
1208  for (i = 0; i < pps->num_tile_rows; i++) {
1209  pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1210  (i * sps->ctb_height) / pps->num_tile_rows;
1211  }
1212  }
1213 
1214  pps->col_bd[0] = 0;
1215  for (i = 0; i < pps->num_tile_columns; i++)
1216  pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1217 
1218  pps->row_bd[0] = 0;
1219  for (i = 0; i < pps->num_tile_rows; i++)
1220  pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1221 
1222  for (i = 0, j = 0; i < sps->ctb_width; i++) {
1223  if (i > pps->col_bd[j])
1224  j++;
1225  pps->col_idxX[i] = j;
1226  }
1227 
1228  /**
1229  * 6.5
1230  */
1231  pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1232  pic_area_in_min_cbs = sps->min_cb_width * sps->min_cb_height;
1233  pic_area_in_min_tbs = sps->min_tb_width * sps->min_tb_height;
1234 
1235  pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1236  pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1237  pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1238  pps->min_cb_addr_zs = av_malloc_array(pic_area_in_min_cbs, sizeof(*pps->min_cb_addr_zs));
1239  pps->min_tb_addr_zs = av_malloc_array(pic_area_in_min_tbs, sizeof(*pps->min_tb_addr_zs));
1240  if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1241  !pps->tile_id || !pps->min_cb_addr_zs || !pps->min_tb_addr_zs) {
1242  ret = AVERROR(ENOMEM);
1243  goto err;
1244  }
1245 
1246  for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1247  int tb_x = ctb_addr_rs % sps->ctb_width;
1248  int tb_y = ctb_addr_rs / sps->ctb_width;
1249  int tile_x = 0;
1250  int tile_y = 0;
1251  int val = 0;
1252 
1253  for (i = 0; i < pps->num_tile_columns; i++) {
1254  if (tb_x < pps->col_bd[i + 1]) {
1255  tile_x = i;
1256  break;
1257  }
1258  }
1259 
1260  for (i = 0; i < pps->num_tile_rows; i++) {
1261  if (tb_y < pps->row_bd[i + 1]) {
1262  tile_y = i;
1263  break;
1264  }
1265  }
1266 
1267  for (i = 0; i < tile_x; i++)
1268  val += pps->row_height[tile_y] * pps->column_width[i];
1269  for (i = 0; i < tile_y; i++)
1270  val += sps->ctb_width * pps->row_height[i];
1271 
1272  val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1273  tb_x - pps->col_bd[tile_x];
1274 
1275  pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1276  pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1277  }
1278 
1279  for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1280  for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1281  for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1282  for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1283  pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1284 
1285  pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1286  if (!pps->tile_pos_rs) {
1287  ret = AVERROR(ENOMEM);
1288  goto err;
1289  }
1290 
1291  for (j = 0; j < pps->num_tile_rows; j++)
1292  for (i = 0; i < pps->num_tile_columns; i++)
1293  pps->tile_pos_rs[j * pps->num_tile_columns + i] = pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1294 
1295  for (y = 0; y < sps->min_cb_height; y++) {
1296  for (x = 0; x < sps->min_cb_width; x++) {
1297  int tb_x = x >> sps->log2_diff_max_min_coding_block_size;
1298  int tb_y = y >> sps->log2_diff_max_min_coding_block_size;
1299  int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1300  int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1302  for (i = 0; i < sps->log2_diff_max_min_coding_block_size; i++) {
1303  int m = 1 << i;
1304  val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1305  }
1306  pps->min_cb_addr_zs[y * sps->min_cb_width + x] = val;
1307  }
1308  }
1309 
1310  log2_diff_ctb_min_tb_size = sps->log2_ctb_size - sps->log2_min_tb_size;
1311  for (y = 0; y < sps->min_tb_height; y++) {
1312  for (x = 0; x < sps->min_tb_width; x++) {
1313  int tb_x = x >> log2_diff_ctb_min_tb_size;
1314  int tb_y = y >> log2_diff_ctb_min_tb_size;
1315  int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1316  int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1317  (log2_diff_ctb_min_tb_size * 2);
1318  for (i = 0; i < log2_diff_ctb_min_tb_size; i++) {
1319  int m = 1 << i;
1320  val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1321  }
1322  pps->min_tb_addr_zs[y * sps->min_tb_width + x] = val;
1323  }
1324  }
1325 
1326  av_buffer_unref(&s->pps_list[pps_id]);
1327  s->pps_list[pps_id] = pps_buf;
1328 
1329  return 0;
1330 
1331 err:
1332  av_buffer_unref(&pps_buf);
1333  return ret;
1334 }