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 "golomb.h"
27 #include "libavutil/imgutils.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  int delta_idx = get_ue_golomb_long(gb) + 1;
96  if (delta_idx > sps->nb_st_rps) {
97  av_log(s->avctx, AV_LOG_ERROR, "Invalid value of delta_idx "
98  "in slice header RPS: %d > %d.\n", delta_idx,
99  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 static int decode_profile_tier_level(HEVCLocalContext *lc, PTL *ptl, int max_num_sub_layers)
195 {
196  int i, j;
197  GetBitContext *gb = &lc->gb;
198 
199  ptl->general_profile_space = get_bits(gb, 2);
200  ptl->general_tier_flag = get_bits1(gb);
201  ptl->general_profile_idc = get_bits(gb, 5);
202  for (i = 0; i < 32; i++)
204  skip_bits1(gb);// general_progressive_source_flag
205  skip_bits1(gb);// general_interlaced_source_flag
206  skip_bits1(gb);// general_non_packed_constraint_flag
207  skip_bits1(gb);// general_frame_only_constraint_flag
208  if (get_bits(gb, 16) != 0) // XXX_reserved_zero_44bits[0..15]
209  return -1;
210  if (get_bits(gb, 16) != 0) // XXX_reserved_zero_44bits[16..31]
211  return -1;
212  if (get_bits(gb, 12) != 0) // XXX_reserved_zero_44bits[32..43]
213  return -1;
214 
215  ptl->general_level_idc = get_bits(gb, 8);
216  for (i = 0; i < max_num_sub_layers - 1; i++) {
219  }
220  if (max_num_sub_layers - 1 > 0)
221  for (i = max_num_sub_layers - 1; i < 8; i++)
222  skip_bits(gb, 2); // reserved_zero_2bits[i]
223  for (i = 0; i < max_num_sub_layers - 1; i++) {
224  if (ptl->sub_layer_profile_present_flag[i]) {
225  ptl->sub_layer_profile_space[i] = get_bits(gb, 2);
226  ptl->sub_layer_tier_flag[i] = get_bits(gb, 1);
227  ptl->sub_layer_profile_idc[i] = get_bits(gb, 5);
228  for (j = 0; j < 32; j++)
230  skip_bits1(gb);// sub_layer_progressive_source_flag
231  skip_bits1(gb);// sub_layer_interlaced_source_flag
232  skip_bits1(gb);// sub_layer_non_packed_constraint_flag
233  skip_bits1(gb);// sub_layer_frame_only_constraint_flag
234 
235  if (get_bits(gb, 16) != 0) // sub_layer_reserved_zero_44bits[0..15]
236  return -1;
237  if (get_bits(gb, 16) != 0) // sub_layer_reserved_zero_44bits[16..31]
238  return -1;
239  if (get_bits(gb, 12) != 0) // sub_layer_reserved_zero_44bits[32..43]
240  return -1;
241  }
242  if (ptl->sub_layer_level_present_flag[i])
243  ptl->sub_layer_level_idc[i] = get_bits(gb, 8);
244  }
245  return 0;
246 }
247 
248 static void decode_sublayer_hrd(HEVCContext *s, int nb_cpb, int subpic_params_present)
249 {
250  GetBitContext *gb = &s->HEVClc->gb;
251  int i;
252 
253  for (i = 0; i < nb_cpb; i++) {
254  get_ue_golomb_long(gb); // bit_rate_value_minus1
255  get_ue_golomb_long(gb); // cpb_size_value_minus1
256 
257  if (subpic_params_present) {
258  get_ue_golomb_long(gb); // cpb_size_du_value_minus1
259  get_ue_golomb_long(gb); // bit_rate_du_value_minus1
260  }
261  skip_bits1(gb); // cbr_flag
262  }
263 }
264 
265 static void decode_hrd(HEVCContext *s, int common_inf_present, int max_sublayers)
266 {
267  GetBitContext *gb = &s->HEVClc->gb;
268  int nal_params_present = 0, vcl_params_present = 0;
269  int subpic_params_present = 0;
270  int i;
271 
272  if (common_inf_present) {
273  nal_params_present = get_bits1(gb);
274  vcl_params_present = get_bits1(gb);
275 
276  if (nal_params_present || vcl_params_present) {
277  subpic_params_present = get_bits1(gb);
278 
279  if (subpic_params_present) {
280  skip_bits(gb, 8); // tick_divisor_minus2
281  skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
282  skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
283  skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
284  }
285 
286  skip_bits(gb, 4); // bit_rate_scale
287  skip_bits(gb, 4); // cpb_size_scale
288 
289  if (subpic_params_present)
290  skip_bits(gb, 4); // cpb_size_du_scale
291 
292  skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
293  skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
294  skip_bits(gb, 5); // dpb_output_delay_length_minus1
295  }
296  }
297 
298  for (i = 0; i < max_sublayers; i++) {
299  int low_delay = 0;
300  int nb_cpb = 1;
301  int fixed_rate = get_bits1(gb);
302 
303  if (!fixed_rate)
304  fixed_rate = get_bits1(gb);
305 
306  if (fixed_rate)
307  get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
308  else
309  low_delay = get_bits1(gb);
310 
311  if (!low_delay)
312  nb_cpb = get_ue_golomb_long(gb) + 1;
313 
314  if (nal_params_present)
315  decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
316  if (vcl_params_present)
317  decode_sublayer_hrd(s, nb_cpb, subpic_params_present);
318  }
319 }
320 
322 {
323  int i,j;
324  GetBitContext *gb = &s->HEVClc->gb;
325  int vps_id = 0;
326  VPS *vps;
327 
328  av_log(s->avctx, AV_LOG_DEBUG, "Decoding VPS\n");
329 
330  vps = av_mallocz(sizeof(*vps));
331  if (!vps)
332  return AVERROR(ENOMEM);
333 
334  vps_id = get_bits(gb, 4);
335  if (vps_id >= MAX_VPS_COUNT) {
336  av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
337  goto err;
338  }
339 
340  if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
341  av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
342  goto err;
343  }
344 
345  vps->vps_max_layers = get_bits(gb, 6) + 1;
346  vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
348 
349  if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
350  av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
351  goto err;
352  }
353 
354  if (vps->vps_max_sub_layers > MAX_SUB_LAYERS) {
355  av_log(s->avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
356  vps->vps_max_sub_layers);
357  goto err;
358  }
359 
360  if (decode_profile_tier_level(s->HEVClc, &vps->ptl, vps->vps_max_sub_layers) < 0) {
361  av_log(s->avctx, AV_LOG_ERROR, "Error decoding profile tier level.\n");
362  goto err;
363  }
365 
367  for (; i < vps->vps_max_sub_layers; i++) {
371 
372  if (vps->vps_max_dec_pic_buffering[i] > MAX_DPB_SIZE) {
373  av_log(s->avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
374  vps->vps_max_dec_pic_buffering[i] - 1);
375  goto err;
376  }
377  if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
378  av_log(s->avctx, AV_LOG_ERROR, "vps_max_num_reorder_pics out of range: %d\n",
379  vps->vps_num_reorder_pics[i]);
380  goto err;
381  }
382  }
383 
384  vps->vps_max_layer_id = get_bits(gb, 6);
385  vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
386  for (i = 1; i < vps->vps_num_layer_sets; i++)
387  for (j = 0; j <= vps->vps_max_layer_id; j++)
388  skip_bits(gb, 1); // layer_id_included_flag[i][j]
389 
391  if (vps->vps_timing_info_present_flag) {
392  vps->vps_num_units_in_tick = get_bits_long(gb, 32);
393  vps->vps_time_scale = get_bits_long(gb, 32);
398  for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
399  int common_inf_present = 1;
400 
401  get_ue_golomb_long(gb); // hrd_layer_set_idx
402  if (i)
403  common_inf_present = get_bits1(gb);
404  decode_hrd(s, common_inf_present, vps->vps_max_sub_layers);
405  }
406  }
407  get_bits1(gb); /* vps_extension_flag */
408 
409  av_free(s->vps_list[vps_id]);
410  s->vps_list[vps_id] = vps;
411  return 0;
412 
413 err:
414  av_free(vps);
415  return AVERROR_INVALIDDATA;
416 }
417 
418 static void decode_vui(HEVCContext *s, HEVCSPS *sps)
419 {
420  VUI *vui = &sps->vui;
421  GetBitContext *gb = &s->HEVClc->gb;
422  int sar_present;
423 
424  av_log(s->avctx, AV_LOG_DEBUG, "Decoding VUI\n");
425 
426  sar_present = get_bits1(gb);
427  if (sar_present) {
428  uint8_t sar_idx = get_bits(gb, 8);
429  if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
430  vui->sar = vui_sar[sar_idx];
431  else if (sar_idx == 255) {
432  vui->sar.num = get_bits(gb, 16);
433  vui->sar.den = get_bits(gb, 16);
434  } else
435  av_log(s->avctx, AV_LOG_WARNING, "Unknown SAR index: %u.\n",
436  sar_idx);
437  }
438 
442 
445  vui->video_format = get_bits(gb, 3);
446  vui->video_full_range_flag = get_bits1(gb);
449  vui->colour_primaries = get_bits(gb, 8);
450  vui->transfer_characteristic = get_bits(gb, 8);
451  vui->matrix_coeffs = get_bits(gb, 8);
452  }
453  }
454 
456  if (vui->chroma_loc_info_present_flag) {
459  }
460 
462  vui->field_seq_flag = get_bits1(gb);
464 
466  if (vui->default_display_window_flag) {
467  //TODO: * 2 is only valid for 420
472 
473  if (s->strict_def_disp_win &&
476  "discarding vui default display window, "
477  "original values are l:%u r:%u t:%u b:%u\n",
482 
485  vui->def_disp_win.top_offset =
486  vui->def_disp_win.bottom_offset = 0;
487  }
488  }
489 
491  if (vui->vui_timing_info_present_flag) {
492  vui->vui_num_units_in_tick = get_bits(gb, 32);
493  vui->vui_time_scale = get_bits(gb, 32);
499  decode_hrd(s, 1, sps->max_sub_layers);
500  }
501 
503  if (vui->bitstream_restriction_flag) {
512  }
513 }
514 
516 {
517  int matrixId;
518 
519  for (matrixId = 0; matrixId < 6; matrixId++) {
520  // 4x4 default is 16
521  memset(sl->sl[0][matrixId], 16, 16);
522  sl->sl_dc[0][matrixId] = 16; // default for 16x16
523  sl->sl_dc[1][matrixId] = 16; // default for 32x32
524  }
525  memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
526  memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
527  memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
528  memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
529  memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
530  memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
531  memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
532  memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
533  memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
534  memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
535  memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
536  memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
537  memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
538  memcpy(sl->sl[3][1], default_scaling_list_inter, 64);
539 }
540 
542 {
543  GetBitContext *gb = &s->HEVClc->gb;
544  uint8_t scaling_list_pred_mode_flag[4][6];
545  int32_t scaling_list_dc_coef[2][6];
546 
547  int size_id, matrix_id, i, pos, delta;
548  for (size_id = 0; size_id < 4; size_id++)
549  for (matrix_id = 0; matrix_id < ((size_id == 3) ? 2 : 6); matrix_id++) {
550  scaling_list_pred_mode_flag[size_id][matrix_id] = get_bits1(gb);
551  if (!scaling_list_pred_mode_flag[size_id][matrix_id]) {
552  delta = get_ue_golomb_long(gb);
553  // Only need to handle non-zero delta. Zero means default, which should already be in the arrays.
554  if (delta) {
555  // Copy from previous array.
556  if (matrix_id - delta < 0) {
558  "Invalid delta in scaling list data: %d.\n", delta);
559  return AVERROR_INVALIDDATA;
560  }
561 
562  memcpy(sl->sl[size_id][matrix_id],
563  sl->sl[size_id][matrix_id - delta],
564  size_id > 0 ? 64 : 16);
565  if (size_id > 1)
566  sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
567  }
568  } else {
569  int next_coef;
570  int coef_num;
571  int32_t scaling_list_delta_coef;
572 
573  next_coef = 8;
574  coef_num = FFMIN(64, (1 << (4 + (size_id << 1))));
575  if (size_id > 1) {
576  scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
577  next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
578  sl->sl_dc[size_id - 2][matrix_id] = next_coef;
579  }
580  for (i = 0; i < coef_num; i++) {
581  if (size_id == 0)
583  else
585 
586  scaling_list_delta_coef = get_se_golomb(gb);
587  next_coef = (next_coef + scaling_list_delta_coef + 256 ) % 256;
588  sl->sl[size_id][matrix_id][pos] = next_coef;
589  }
590  }
591  }
592 
593  return 0;
594 }
595 
597 {
598  const AVPixFmtDescriptor *desc;
599  GetBitContext *gb = &s->HEVClc->gb;
600  int ret = 0;
601  int sps_id = 0;
602  int log2_diff_max_min_transform_block_size;
603  int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
604  int i;
605 
606  HEVCSPS *sps;
607  AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
608 
609  if (!sps_buf)
610  return AVERROR(ENOMEM);
611  sps = (HEVCSPS*)sps_buf->data;
612 
613  av_log(s->avctx, AV_LOG_DEBUG, "Decoding SPS\n");
614 
615  // Coded parameters
616 
617  sps->vps_id = get_bits(gb, 4);
618  if (sps->vps_id >= MAX_VPS_COUNT) {
619  av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
620  ret = AVERROR_INVALIDDATA;
621  goto err;
622  }
623 
624  if (!s->vps_list[sps->vps_id]) {
625  av_log(s->avctx, AV_LOG_ERROR, "VPS does not exist \n");
626  ret = AVERROR_INVALIDDATA;
627  goto err;
628  }
629 
630  sps->max_sub_layers = get_bits(gb, 3) + 1;
631  if (sps->max_sub_layers > MAX_SUB_LAYERS) {
632  av_log(s->avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
633  sps->max_sub_layers);
634  ret = AVERROR_INVALIDDATA;
635  goto err;
636  }
637 
638  skip_bits1(gb); // temporal_id_nesting_flag
639  if (decode_profile_tier_level(s->HEVClc, &sps->ptl, sps->max_sub_layers) < 0) {
640  av_log(s->avctx, AV_LOG_ERROR, "error decoding profile tier level\n");
641  ret = AVERROR_INVALIDDATA;
642  goto err;
643  }
644  sps_id = get_ue_golomb_long(gb);
645  if (sps_id >= MAX_SPS_COUNT) {
646  av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", sps_id);
647  ret = AVERROR_INVALIDDATA;
648  goto err;
649  }
650 
652  if (sps->chroma_format_idc != 1) {
653  avpriv_report_missing_feature(s->avctx, "chroma_format_idc != 1\n");
654  ret = AVERROR_INVALIDDATA;
655  goto err;
656  }
657 
658  if (sps->chroma_format_idc == 3)
660 
661  sps->width = get_ue_golomb_long(gb);
662  sps->height = get_ue_golomb_long(gb);
663  if ((ret = av_image_check_size(sps->width,
664  sps->height, 0, s->avctx)) < 0)
665  goto err;
666 
667  if (get_bits1(gb)) { // pic_conformance_flag
668  //TODO: * 2 is only valid for 420
673 
676  "discarding sps conformance window, "
677  "original values are l:%u r:%u t:%u b:%u\n",
682 
685  sps->pic_conf_win.top_offset =
686  sps->pic_conf_win.bottom_offset = 0;
687  }
688  sps->output_window = sps->pic_conf_win;
689  }
690 
691  sps->bit_depth = get_ue_golomb_long(gb) + 8;
692  bit_depth_chroma = get_ue_golomb_long(gb) + 8;
693  if (bit_depth_chroma != sps->bit_depth) {
695  "Luma bit depth (%d) is different from chroma bit depth (%d), this is unsupported.\n",
696  sps->bit_depth, bit_depth_chroma);
697  ret = AVERROR_INVALIDDATA;
698  goto err;
699  }
700 
701  if (sps->chroma_format_idc == 1) {
702  switch (sps->bit_depth) {
703  case 8: sps->pix_fmt = AV_PIX_FMT_YUV420P; break;
704  case 9: sps->pix_fmt = AV_PIX_FMT_YUV420P9; break;
705  case 10: sps->pix_fmt = AV_PIX_FMT_YUV420P10; break;
706  default:
707  av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
708  sps->bit_depth);
709  ret = AVERROR_PATCHWELCOME;
710  goto err;
711  }
712  } else {
713  av_log(s->avctx, AV_LOG_ERROR, "non-4:2:0 support is currently unspecified.\n");
714  return AVERROR_PATCHWELCOME;
715  }
716 
717  desc = av_pix_fmt_desc_get(sps->pix_fmt);
718  if (!desc) {
719  ret = AVERROR(EINVAL);
720  goto err;
721  }
722 
723  sps->hshift[0] = sps->vshift[0] = 0;
724  sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
725  sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
726 
727  sps->pixel_shift = sps->bit_depth > 8;
728 
729  sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
730  if (sps->log2_max_poc_lsb > 16) {
731  av_log(s->avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
732  sps->log2_max_poc_lsb - 4);
733  ret = AVERROR_INVALIDDATA;
734  goto err;
735  }
736 
737  sublayer_ordering_info = get_bits1(gb);
738  start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
739  for (i = start; i < sps->max_sub_layers; i++) {
744  av_log(s->avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
746  ret = AVERROR_INVALIDDATA;
747  goto err;
748  }
750  av_log(s->avctx, AV_LOG_ERROR, "sps_max_num_reorder_pics out of range: %d\n",
752  ret = AVERROR_INVALIDDATA;
753  goto err;
754  }
755  }
756 
757  if (!sublayer_ordering_info) {
758  for (i = 0; i < start; i++){
762  }
763  }
764 
765  sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
767  sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
768  log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
769  sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size + sps->log2_min_tb_size;
770 
771  if (sps->log2_min_tb_size >= sps->log2_min_cb_size) {
772  av_log(s->avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
773  ret = AVERROR_INVALIDDATA;
774  goto err;
775  }
778 
780  if (sps->scaling_list_enable_flag) {
782 
783  if (get_bits1(gb)) {
784  ret = scaling_list_data(s, &sps->scaling_list);
785  if (ret < 0)
786  goto err;
787  }
788  }
789 
790  sps->amp_enabled_flag = get_bits1(gb);
791  sps->sao_enabled = get_bits1(gb);
792 
793  sps->pcm_enabled_flag = get_bits1(gb);
794  if (sps->pcm_enabled_flag) {
795  sps->pcm.bit_depth = get_bits(gb, 4) + 1;
796  sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
799  get_ue_golomb_long(gb);
800  if (sps->pcm.bit_depth > sps->bit_depth) {
802  "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
803  sps->pcm.bit_depth, sps->bit_depth);
804  ret = AVERROR_INVALIDDATA;
805  goto err;
806  }
807 
809  }
810 
811  sps->nb_st_rps = get_ue_golomb_long(gb);
812  if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {
813  av_log(s->avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
814  sps->nb_st_rps);
815  ret = AVERROR_INVALIDDATA;
816  goto err;
817  }
818  for (i = 0; i < sps->nb_st_rps; i++) {
819  if ((ret = ff_hevc_decode_short_term_rps(s, &sps->st_rps[i],
820  sps, 0)) < 0)
821  goto err;
822  }
823 
827  for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
830  }
831  }
832 
835  sps->vui.sar = (AVRational){0, 1};
836  vui_present = get_bits1(gb);
837  if (vui_present)
838  decode_vui(s, sps);
839  skip_bits1(gb); // sps_extension_flag
840 
841  if (s->strict_def_disp_win) {
846  }
847  if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&
848  !(s->avctx->flags & CODEC_FLAG_UNALIGNED)) {
849  sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));
850  av_log(s->avctx, AV_LOG_WARNING, "Reducing left output window to %d "
851  "chroma samples to preserve alignment.\n",
853  }
854  sps->output_width = sps->width -
856  sps->output_height = sps->height -
858  if (sps->output_width <= 0 || sps->output_height <= 0) {
859  av_log(s->avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n",
860  sps->output_width, sps->output_height);
861  if (s->avctx->err_recognition & AV_EF_EXPLODE) {
862  ret = AVERROR_INVALIDDATA;
863  goto err;
864  }
865  av_log(s->avctx, AV_LOG_WARNING, "Displaying the whole video surface.\n");
868  sps->pic_conf_win.top_offset =
869  sps->pic_conf_win.bottom_offset = 0;
870  sps->output_width = sps->width;
871  sps->output_height = sps->height;
872  }
873 
874  // Inferred parameters
875  sps->log2_ctb_size = sps->log2_min_cb_size
877  sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
878 
879  sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
880  sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
881  sps->ctb_size = sps->ctb_width * sps->ctb_height;
882 
883  sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
884  sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
885  sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
886  sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
887  sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
888  sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
889 
890  sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
891 
892  if (sps->width & ((1 << sps->log2_min_cb_size) - 1) ||
893  sps->height & ((1 << sps->log2_min_cb_size) - 1)) {
894  av_log(s->avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
895  goto err;
896  }
897 
898  if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) {
899  av_log(s->avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
900  goto err;
901  }
903  av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
905  goto err;
906  }
908  av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
910  goto err;
911  }
912  if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
913  av_log(s->avctx, AV_LOG_ERROR, "max transform block size out of range: %d\n",
914  sps->log2_max_trafo_size);
915  goto err;
916  }
917 
918  if (s->avctx->debug & FF_DEBUG_BITSTREAM) {
919  av_log(s->avctx, AV_LOG_DEBUG, "Parsed SPS: id %d; coded wxh: %dx%d; "
920  "cropped wxh: %dx%d; pix_fmt: %s.\n",
921  sps_id, sps->width, sps->height,
922  sps->output_width, sps->output_height,
924  }
925 
926  /* check if this is a repeat of an already parsed SPS, then keep the
927  * original one.
928  * otherwise drop all PPSes that depend on it */
929  if (s->sps_list[sps_id] &&
930  !memcmp(s->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
931  av_buffer_unref(&sps_buf);
932  } else {
933  for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
934  if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == sps_id)
935  av_buffer_unref(&s->pps_list[i]);
936  }
937  av_buffer_unref(&s->sps_list[sps_id]);
938  s->sps_list[sps_id] = sps_buf;
939  }
940 
941  return 0;
942 err:
943 
944  av_buffer_unref(&sps_buf);
945  return ret;
946 }
947 
948 static void hevc_pps_free(void *opaque, uint8_t *data)
949 {
950  HEVCPPS *pps = (HEVCPPS*)data;
951 
952  av_freep(&pps->column_width);
953  av_freep(&pps->row_height);
954  av_freep(&pps->col_bd);
955  av_freep(&pps->row_bd);
956  av_freep(&pps->col_idxX);
959  av_freep(&pps->tile_pos_rs);
960  av_freep(&pps->tile_id);
961  av_freep(&pps->min_cb_addr_zs);
962  av_freep(&pps->min_tb_addr_zs);
963 
964  av_freep(&pps);
965 }
966 
968 {
969  GetBitContext *gb = &s->HEVClc->gb;
970  HEVCSPS *sps = NULL;
971  int pic_area_in_ctbs, pic_area_in_min_cbs, pic_area_in_min_tbs;
972  int log2_diff_ctb_min_tb_size;
973  int i, j, x, y, ctb_addr_rs, tile_id;
974  int ret = 0;
975  int pps_id = 0;
976 
977  AVBufferRef *pps_buf;
978  HEVCPPS *pps = av_mallocz(sizeof(*pps));
979 
980  if (!pps)
981  return AVERROR(ENOMEM);
982 
983  pps_buf = av_buffer_create((uint8_t*)pps, sizeof(*pps), hevc_pps_free, NULL, 0);
984  if (!pps_buf) {
985  av_freep(&pps);
986  return AVERROR(ENOMEM);
987  }
988 
989  av_log(s->avctx, AV_LOG_DEBUG, "Decoding PPS\n");
990 
991  // Default values
993  pps->num_tile_columns = 1;
994  pps->num_tile_rows = 1;
995  pps->uniform_spacing_flag = 1;
996  pps->disable_dbf = 0;
997  pps->beta_offset = 0;
998  pps->tc_offset = 0;
999 
1000  // Coded parameters
1001  pps_id = get_ue_golomb_long(gb);
1002  if (pps_id >= MAX_PPS_COUNT) {
1003  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1004  ret = AVERROR_INVALIDDATA;
1005  goto err;
1006  }
1007  pps->sps_id = get_ue_golomb_long(gb);
1008  if (pps->sps_id >= MAX_SPS_COUNT) {
1009  av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1010  ret = AVERROR_INVALIDDATA;
1011  goto err;
1012  }
1013  if (!s->sps_list[pps->sps_id]) {
1014  av_log(s->avctx, AV_LOG_ERROR, "SPS does not exist \n");
1015  ret = AVERROR_INVALIDDATA;
1016  goto err;
1017  }
1018  sps = (HEVCSPS*)s->sps_list[pps->sps_id]->data;
1019 
1022  pps->num_extra_slice_header_bits = get_bits(gb, 3);
1023 
1024  pps->sign_data_hiding_flag = get_bits1(gb);
1025 
1027 
1030 
1032 
1035 
1037  pps->diff_cu_qp_delta_depth = 0;
1038  if (pps->cu_qp_delta_enabled_flag)
1040 
1041  pps->cb_qp_offset = get_se_golomb(gb);
1042  if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1043  av_log(s->avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1044  pps->cb_qp_offset);
1045  ret = AVERROR_INVALIDDATA;
1046  goto err;
1047  }
1048  pps->cr_qp_offset = get_se_golomb(gb);
1049  if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1050  av_log(s->avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1051  pps->cr_qp_offset);
1052  ret = AVERROR_INVALIDDATA;
1053  goto err;
1054  }
1056 
1057  pps->weighted_pred_flag = get_bits1(gb);
1058  pps->weighted_bipred_flag = get_bits1(gb);
1059 
1061  pps->tiles_enabled_flag = get_bits1(gb);
1063 
1064  if (pps->tiles_enabled_flag) {
1065  pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
1066  pps->num_tile_rows = get_ue_golomb_long(gb) + 1;
1067  if (pps->num_tile_columns == 0 ||
1068  pps->num_tile_columns >= sps->width) {
1069  av_log(s->avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1070  pps->num_tile_columns - 1);
1071  ret = AVERROR_INVALIDDATA;
1072  goto err;
1073  }
1074  if (pps->num_tile_rows == 0 ||
1075  pps->num_tile_rows >= sps->height) {
1076  av_log(s->avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1077  pps->num_tile_rows - 1);
1078  ret = AVERROR_INVALIDDATA;
1079  goto err;
1080  }
1081 
1082  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1083  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1084  if (!pps->column_width || !pps->row_height) {
1085  ret = AVERROR(ENOMEM);
1086  goto err;
1087  }
1088 
1089  pps->uniform_spacing_flag = get_bits1(gb);
1090  if (!pps->uniform_spacing_flag) {
1091  int sum = 0;
1092  for (i = 0; i < pps->num_tile_columns - 1; i++) {
1093  pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1094  sum += pps->column_width[i];
1095  }
1096  if (sum >= sps->ctb_width) {
1097  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1098  ret = AVERROR_INVALIDDATA;
1099  goto err;
1100  }
1101  pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1102 
1103  sum = 0;
1104  for (i = 0; i < pps->num_tile_rows - 1; i++) {
1105  pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1106  sum += pps->row_height[i];
1107  }
1108  if (sum >= sps->ctb_height) {
1109  av_log(s->avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1110  ret = AVERROR_INVALIDDATA;
1111  goto err;
1112  }
1113  pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1114  }
1116  }
1117 
1119 
1123  pps->disable_dbf = get_bits1(gb);
1124  if (!pps->disable_dbf) {
1125  pps->beta_offset = get_se_golomb(gb) * 2;
1126  pps->tc_offset = get_se_golomb(gb) * 2;
1127  if (pps->beta_offset/2 < -6 || pps->beta_offset/2 > 6) {
1128  av_log(s->avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1129  pps->beta_offset/2);
1130  ret = AVERROR_INVALIDDATA;
1131  goto err;
1132  }
1133  if (pps->tc_offset/2 < -6 || pps->tc_offset/2 > 6) {
1134  av_log(s->avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1135  pps->tc_offset/2);
1136  ret = AVERROR_INVALIDDATA;
1137  goto err;
1138  }
1139  }
1140  }
1141 
1145  ret = scaling_list_data(s, &pps->scaling_list);
1146  if (ret < 0)
1147  goto err;
1148  }
1151  if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
1152  av_log(s->avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1153  pps->log2_parallel_merge_level - 2);
1154  ret = AVERROR_INVALIDDATA;
1155  goto err;
1156  }
1157 
1159  pps->pps_extension_flag = get_bits1(gb);
1160 
1161  // Inferred parameters
1162  pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1163  pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1164  pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1165  if (!pps->col_bd || !pps->row_bd || !pps->col_idxX) {
1166  ret = AVERROR(ENOMEM);
1167  goto err;
1168  }
1169 
1170  if (pps->uniform_spacing_flag) {
1171  if (!pps->column_width) {
1172  pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1173  pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1174  }
1175  if (!pps->column_width || !pps->row_height) {
1176  ret = AVERROR(ENOMEM);
1177  goto err;
1178  }
1179 
1180  for (i = 0; i < pps->num_tile_columns; i++) {
1181  pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1182  (i * sps->ctb_width) / pps->num_tile_columns;
1183  }
1184 
1185  for (i = 0; i < pps->num_tile_rows; i++) {
1186  pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1187  (i * sps->ctb_height) / pps->num_tile_rows;
1188  }
1189  }
1190 
1191  pps->col_bd[0] = 0;
1192  for (i = 0; i < pps->num_tile_columns; i++)
1193  pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1194 
1195  pps->row_bd[0] = 0;
1196  for (i = 0; i < pps->num_tile_rows; i++)
1197  pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1198 
1199  for (i = 0, j = 0; i < sps->ctb_width; i++) {
1200  if (i > pps->col_bd[j])
1201  j++;
1202  pps->col_idxX[i] = j;
1203  }
1204 
1205  /**
1206  * 6.5
1207  */
1208  pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1209  pic_area_in_min_cbs = sps->min_cb_width * sps->min_cb_height;
1210  pic_area_in_min_tbs = sps->min_tb_width * sps->min_tb_height;
1211 
1212  pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1213  pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1214  pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1215  pps->min_cb_addr_zs = av_malloc_array(pic_area_in_min_cbs, sizeof(*pps->min_cb_addr_zs));
1216  pps->min_tb_addr_zs = av_malloc_array(pic_area_in_min_tbs, sizeof(*pps->min_tb_addr_zs));
1217  if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1218  !pps->tile_id || !pps->min_cb_addr_zs || !pps->min_tb_addr_zs) {
1219  ret = AVERROR(ENOMEM);
1220  goto err;
1221  }
1222 
1223  for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1224  int tb_x = ctb_addr_rs % sps->ctb_width;
1225  int tb_y = ctb_addr_rs / sps->ctb_width;
1226  int tile_x = 0;
1227  int tile_y = 0;
1228  int val = 0;
1229 
1230  for (i = 0; i < pps->num_tile_columns; i++) {
1231  if (tb_x < pps->col_bd[i + 1]) {
1232  tile_x = i;
1233  break;
1234  }
1235  }
1236 
1237  for (i = 0; i < pps->num_tile_rows; i++) {
1238  if (tb_y < pps->row_bd[i + 1]) {
1239  tile_y = i;
1240  break;
1241  }
1242  }
1243 
1244  for (i = 0; i < tile_x; i++ )
1245  val += pps->row_height[tile_y] * pps->column_width[i];
1246  for (i = 0; i < tile_y; i++ )
1247  val += sps->ctb_width * pps->row_height[i];
1248 
1249  val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1250  tb_x - pps->col_bd[tile_x];
1251 
1252  pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1253  pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1254  }
1255 
1256  for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1257  for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1258  for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1259  for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1260  pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1261 
1262  pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1263  if (!pps->tile_pos_rs) {
1264  ret = AVERROR(ENOMEM);
1265  goto err;
1266  }
1267 
1268  for (j = 0; j < pps->num_tile_rows; j++)
1269  for (i = 0; i < pps->num_tile_columns; i++)
1270  pps->tile_pos_rs[j * pps->num_tile_columns + i] = pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1271 
1272  for (y = 0; y < sps->min_cb_height; y++) {
1273  for (x = 0; x < sps->min_cb_width; x++) {
1274  int tb_x = x >> sps->log2_diff_max_min_coding_block_size;
1275  int tb_y = y >> sps->log2_diff_max_min_coding_block_size;
1276  int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1277  int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1279  for (i = 0; i < sps->log2_diff_max_min_coding_block_size; i++) {
1280  int m = 1 << i;
1281  val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1282  }
1283  pps->min_cb_addr_zs[y * sps->min_cb_width + x] = val;
1284  }
1285  }
1286 
1287  log2_diff_ctb_min_tb_size = sps->log2_ctb_size - sps->log2_min_tb_size;
1288  for (y = 0; y < sps->min_tb_height; y++) {
1289  for (x = 0; x < sps->min_tb_width; x++) {
1290  int tb_x = x >> log2_diff_ctb_min_tb_size;
1291  int tb_y = y >> log2_diff_ctb_min_tb_size;
1292  int ctb_addr_rs = sps->ctb_width * tb_y + tb_x;
1293  int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] <<
1294  (log2_diff_ctb_min_tb_size * 2);
1295  for (i = 0; i < log2_diff_ctb_min_tb_size; i++) {
1296  int m = 1 << i;
1297  val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1298  }
1299  pps->min_tb_addr_zs[y * sps->min_tb_width + x] = val;
1300  }
1301  }
1302 
1303  av_buffer_unref(&s->pps_list[pps_id]);
1304  s->pps_list[pps_id] = pps_buf;
1305 
1306  return 0;
1307 
1308 err:
1309  av_buffer_unref(&pps_buf);
1310  return ret;
1311 }