FFmpeg
cbs_h266_syntax_template.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
20  RWContext *rw)
21 {
22  int err;
23 
24  fixed(1, rbsp_stop_one_bit, 1);
25  while (byte_alignment(rw) != 0)
26  fixed(1, rbsp_alignment_zero_bit, 0);
27  return 0;
28 }
29 
31  H266RawNALUnitHeader *current,
32  int expected_nal_unit_type)
33 {
34  int err;
35 
36  fixed(1, forbidden_zero_bit, 0);
37  flag(nuh_reserved_zero_bit);
38 
39  u(6, nuh_layer_id, 0, 55);
40 
41  if (expected_nal_unit_type >= 0)
42  u(5, nal_unit_type, expected_nal_unit_type, expected_nal_unit_type);
43  else
44  ub(5, nal_unit_type);
45 
46  u(3, nuh_temporal_id_plus1, 1, 7);
47  return 0;
48 }
49 
51 {
52  int err;
53 
54  fixed(1, byte_alignment_bit_equal_to_one, 1);
55  while (byte_alignment(rw) != 0)
56  fixed(1, byte_alignment_bit_equal_to_zero, 0);
57  return 0;
58 }
59 
61  RWContext *rw,
63 {
64  int err, i, num_additional_bits_used;
65 
66  flag(gci_present_flag);
67  if (current->gci_present_flag) {
68  /* general */
69  flag(gci_intra_only_constraint_flag);
70  flag(gci_all_layers_independent_constraint_flag);
71  flag(gci_one_au_only_constraint_flag);
72 
73  /* picture format */
74  u(4, gci_sixteen_minus_max_bitdepth_constraint_idc, 0, 8);
75  ub(2, gci_three_minus_max_chroma_format_constraint_idc);
76 
77  /* NAL unit type related */
78  flag(gci_no_mixed_nalu_types_in_pic_constraint_flag);
79  flag(gci_no_trail_constraint_flag);
80  flag(gci_no_stsa_constraint_flag);
81  flag(gci_no_rasl_constraint_flag);
82  flag(gci_no_radl_constraint_flag);
83  flag(gci_no_idr_constraint_flag);
84  flag(gci_no_cra_constraint_flag);
85  flag(gci_no_gdr_constraint_flag);
86  flag(gci_no_aps_constraint_flag);
87  flag(gci_no_idr_rpl_constraint_flag);
88 
89  /* tile, slice, subpicture partitioning */
90  flag(gci_one_tile_per_pic_constraint_flag);
91  flag(gci_pic_header_in_slice_header_constraint_flag);
92  flag(gci_one_slice_per_pic_constraint_flag);
93  flag(gci_no_rectangular_slice_constraint_flag);
94  flag(gci_one_slice_per_subpic_constraint_flag);
95  flag(gci_no_subpic_info_constraint_flag);
96 
97  /* CTU and block partitioning */
98  ub(2, gci_three_minus_max_log2_ctu_size_constraint_idc);
99  flag(gci_no_partition_constraints_override_constraint_flag);
100  flag(gci_no_mtt_constraint_flag);
101  flag(gci_no_qtbtt_dual_tree_intra_constraint_flag);
102 
103  /* intra */
104  flag(gci_no_palette_constraint_flag);
105  flag(gci_no_ibc_constraint_flag);
106  flag(gci_no_isp_constraint_flag);
107  flag(gci_no_mrl_constraint_flag);
108  flag(gci_no_mip_constraint_flag);
109  flag(gci_no_cclm_constraint_flag);
110 
111  /* inter */
112  flag(gci_no_ref_pic_resampling_constraint_flag);
113  flag(gci_no_res_change_in_clvs_constraint_flag);
114  flag(gci_no_weighted_prediction_constraint_flag);
115  flag(gci_no_ref_wraparound_constraint_flag);
116  flag(gci_no_temporal_mvp_constraint_flag);
117  flag(gci_no_sbtmvp_constraint_flag);
118  flag(gci_no_amvr_constraint_flag);
119  flag(gci_no_bdof_constraint_flag);
120  flag(gci_no_smvd_constraint_flag);
121  flag(gci_no_dmvr_constraint_flag);
122  flag(gci_no_mmvd_constraint_flag);
123  flag(gci_no_affine_motion_constraint_flag);
124  flag(gci_no_prof_constraint_flag);
125  flag(gci_no_bcw_constraint_flag);
126  flag(gci_no_ciip_constraint_flag);
127  flag(gci_no_gpm_constraint_flag);
128 
129  /* transform, quantization, residual */
130  flag(gci_no_luma_transform_size_64_constraint_flag);
131  flag(gci_no_transform_skip_constraint_flag);
132  flag(gci_no_bdpcm_constraint_flag);
133  flag(gci_no_mts_constraint_flag);
134  flag(gci_no_lfnst_constraint_flag);
135  flag(gci_no_joint_cbcr_constraint_flag);
136  flag(gci_no_sbt_constraint_flag);
137  flag(gci_no_act_constraint_flag);
138  flag(gci_no_explicit_scaling_list_constraint_flag);
139  flag(gci_no_dep_quant_constraint_flag);
140  flag(gci_no_sign_data_hiding_constraint_flag);
141  flag(gci_no_cu_qp_delta_constraint_flag);
142  flag(gci_no_chroma_qp_offset_constraint_flag);
143 
144  /* loop filter */
145  flag(gci_no_sao_constraint_flag);
146  flag(gci_no_alf_constraint_flag);
147  flag(gci_no_ccalf_constraint_flag);
148  flag(gci_no_lmcs_constraint_flag);
149  flag(gci_no_ladf_constraint_flag);
150  flag(gci_no_virtual_boundaries_constraint_flag);
151  ub(8, gci_num_additional_bits);
152  if (current->gci_num_additional_bits > 5) {
153  flag(gci_all_rap_pictures_constraint_flag);
154  flag(gci_no_extended_precision_processing_constraint_flag);
155  flag(gci_no_ts_residual_coding_rice_constraint_flag);
156  flag(gci_no_rrc_rice_extension_constraint_flag);
157  flag(gci_no_persistent_rice_adaptation_constraint_flag);
158  flag(gci_no_reverse_last_sig_coeff_constraint_flag);
159  num_additional_bits_used = 6;
160  } else {
161  infer(gci_all_rap_pictures_constraint_flag, 0);
162  infer(gci_no_extended_precision_processing_constraint_flag, 0);
163  infer(gci_no_ts_residual_coding_rice_constraint_flag, 0);
164  infer(gci_no_rrc_rice_extension_constraint_flag, 0);
165  infer(gci_no_persistent_rice_adaptation_constraint_flag, 0);
166  infer(gci_no_reverse_last_sig_coeff_constraint_flag, 0);
167  num_additional_bits_used = 0;
168  }
169 
170  for (i = 0; i < current->gci_num_additional_bits - num_additional_bits_used; i++)
171  flags(gci_reserved_bit[i], 1, i);
172  }
173  while (byte_alignment(rw) != 0)
174  fixed(1, gci_alignment_zero_bit, 0);
175  return 0;
176 }
177 
179  RWContext *rw,
180  H266RawProfileTierLevel *current,
181  int profile_tier_present_flag,
182  int max_num_sub_layers_minus1)
183 {
184  int err, i;
185 
186  if (profile_tier_present_flag) {
187  ub(7, general_profile_idc);
188  flag(general_tier_flag);
189  }
190  ub(8, general_level_idc);
191  flag(ptl_frame_only_constraint_flag);
192  flag(ptl_multilayer_enabled_flag);
193  if (profile_tier_present_flag) {
195  &current->
197  }
198  for (i = max_num_sub_layers_minus1 - 1; i >= 0; i--)
199  flags(ptl_sublayer_level_present_flag[i], 1, i);
200  while (byte_alignment(rw) != 0)
201  flag(ptl_reserved_zero_bit);
202  for (i = max_num_sub_layers_minus1 - 1; i >= 0; i--)
203  if (current->ptl_sublayer_level_present_flag[i])
204  ubs(8, sublayer_level_idc[i], 1, i);
205  if (profile_tier_present_flag) {
206  ub(8, ptl_num_sub_profiles);
207  for (i = 0; i < current->ptl_num_sub_profiles; i++)
208  ubs(32, general_sub_profile_idc[i], 1, i);
209  }
210  return 0;
211 }
212 
214  RWContext *rw, H266RawVUI *current)
215 {
216  //defined in D.8
217  infer(vui_progressive_source_flag, 0);
218  infer(vui_interlaced_source_flag, 0);
219 
220  infer(vui_non_packed_constraint_flag, 0);
221  infer(vui_non_projected_constraint_flag, 0);
222 
223  infer(vui_aspect_ratio_constant_flag, 0);
224  infer(vui_aspect_ratio_idc, 0);
225 
226  infer(vui_overscan_info_present_flag, 0);
227 
228  infer(vui_colour_primaries, 2);
229  infer(vui_transfer_characteristics, 2);
230  infer(vui_matrix_coeffs, 2);
231  infer(vui_full_range_flag, 0);
232 
233  infer(vui_chroma_sample_loc_type_frame, 6);
234  infer(vui_chroma_sample_loc_type_top_field, 6);
235  infer(vui_chroma_sample_loc_type_bottom_field, 6);
236  return 0;
237 }
238 
240  H266RawVUI *current,
241  uint8_t chroma_format_idc)
242 {
243  int err;
244 
245  flag(vui_progressive_source_flag);
246  flag(vui_interlaced_source_flag);
247  flag(vui_non_packed_constraint_flag);
248  flag(vui_non_projected_constraint_flag);
249  flag(vui_aspect_ratio_info_present_flag);
250  if (current->vui_aspect_ratio_info_present_flag) {
251  flag(vui_aspect_ratio_constant_flag);
252  ub(8, vui_aspect_ratio_idc);
253  if (current->vui_aspect_ratio_idc == 255) {
254  ub(16, vui_sar_width);
255  ub(16, vui_sar_height);
256  }
257  } else {
258  infer(vui_aspect_ratio_constant_flag, 0);
259  infer(vui_aspect_ratio_idc, 0);
260  }
261  flag(vui_overscan_info_present_flag);
262  if (current->vui_overscan_info_present_flag)
263  flag(vui_overscan_appropriate_flag);
264  flag(vui_colour_description_present_flag);
265  if (current->vui_colour_description_present_flag) {
266  ub(8, vui_colour_primaries);
267  av_log(ctx->log_ctx, AV_LOG_DEBUG, "vui_colour_primaries == %d \n",
268  current->vui_colour_primaries);
269  ub(8, vui_transfer_characteristics);
270  av_log(ctx->log_ctx, AV_LOG_DEBUG,
271  "vui_transfer_characteristics == %d \n",
272  current->vui_transfer_characteristics);
273  ub(8, vui_matrix_coeffs);
274  av_log(ctx->log_ctx, AV_LOG_DEBUG, "vui_matrix_coeffs == %d \n",
275  current->vui_matrix_coeffs);
276  flag(vui_full_range_flag);
277  } else {
278  infer(vui_colour_primaries, 2);
279  infer(vui_transfer_characteristics, 2);
280  infer(vui_matrix_coeffs, 2);
281  infer(vui_full_range_flag, 0);
282  }
283  flag(vui_chroma_loc_info_present_flag);
284  if (chroma_format_idc != 1 && current->vui_chroma_loc_info_present_flag) {
285  av_log(ctx->log_ctx, AV_LOG_ERROR, "chroma_format_idc == %d,"
286  "vui_chroma_loc_info_present_flag can't not be true",
287  chroma_format_idc);
288  return AVERROR_INVALIDDATA;
289  }
290  if (current->vui_chroma_loc_info_present_flag) {
291  if (current->vui_progressive_source_flag &&
292  !current->vui_interlaced_source_flag) {
293  ue(vui_chroma_sample_loc_type_frame, 0, 6);
294  } else {
295  ue(vui_chroma_sample_loc_type_top_field, 0, 6);
296  ue(vui_chroma_sample_loc_type_bottom_field, 0, 6);
297  }
298  } else {
299  if (chroma_format_idc == 1) {
300  infer(vui_chroma_sample_loc_type_frame, 6);
301  infer(vui_chroma_sample_loc_type_top_field,
302  current->vui_chroma_sample_loc_type_frame);
303  infer(vui_chroma_sample_loc_type_bottom_field,
304  current->vui_chroma_sample_loc_type_frame);
305  }
306  }
307  return 0;
308 }
309 
311  H266RawExtensionData *current,
312  uint32_t payload_size, int cur_pos)
313 {
314  int err;
315  size_t byte_length, k;
316 
317 #ifdef READ
319  int bits_left, payload_zero_bits;
320 
321  if (!cbs_h265_payload_extension_present(rw, payload_size, cur_pos))
322  return 0;
323 
324  bits_left = 8 * payload_size - cur_pos;
325  tmp = *rw;
326  if (bits_left > 8)
328  payload_zero_bits = get_bits(&tmp, FFMIN(bits_left, 8));
329  if (!payload_zero_bits)
330  return AVERROR_INVALIDDATA;
331  payload_zero_bits = ff_ctz(payload_zero_bits);
332  current->bit_length = bits_left - payload_zero_bits - 1;
333  allocate(current->data, (current->bit_length + 7) / 8);
334 #endif
335 
336  byte_length = (current->bit_length + 7) / 8;
337  for (k = 0; k < byte_length; k++) {
338  int length = FFMIN(current->bit_length - k * 8, 8);
339  xu(length, reserved_payload_extension_data, current->data[k],
340  0, MAX_UINT_BITS(length), 0);
341  }
342 
343  return 0;
344 }
345 
347  H266RawVUI *current, uint16_t vui_payload_size,
348  uint8_t chroma_format_idc)
349 {
350  int err;
351  int start_position, current_position;
352 
353  start_position = bit_position(rw);
354  CHECK(FUNC(vui_parameters) (ctx, rw, current, chroma_format_idc));
355  current_position = bit_position(rw) - start_position;
356 
357  if (current_position < 8 * vui_payload_size) {
358  CHECK(FUNC(payload_extension) (ctx, rw, &current->extension_data,
359  vui_payload_size, current_position));
360  fixed(1, vui_payload_bit_equal_to_one, 1);
361  while (byte_alignment(rw) != 0)
362  fixed(1, vui_payload_bit_equal_to_zero, 0);
363  }
364  return 0;
365 }
366 
368  H266RawExtensionData *current)
369 {
370  int err;
371  size_t k;
372 #ifdef READ
373  GetBitContext start;
374  uint8_t bit;
375  start = *rw;
376  for (k = 0; cbs_h2645_read_more_rbsp_data(rw); k++)
377  skip_bits(rw, 1);
378  current->bit_length = k;
379  if (k > 0) {
380  *rw = start;
381  allocate(current->data, (current->bit_length + 7) / 8);
382  for (k = 0; k < current->bit_length; k++) {
383  xu(1, extension_data, bit, 0, 1, 0);
384  current->data[k / 8] |= bit << (7 - k % 8);
385  }
386  }
387 #else
388  for (k = 0; k < current->bit_length; k++)
389  xu(1, extension_data, current->data[k / 8] >> (7 - k % 8) & 1, 0, 1, 0);
390 #endif
391  return 0;
392 }
393 
395  H266DpbParameters *current,
396  uint8_t max_sublayers_minus1,
397  uint8_t sublayer_info_flag)
398 {
399  int err, i;
400  for (i = (sublayer_info_flag ? 0 : max_sublayers_minus1);
401  i <= max_sublayers_minus1; i++) {
402  ues(dpb_max_dec_pic_buffering_minus1[i], 0, VVC_MAX_DPB_SIZE - 1, 1, i);
403  ues(dpb_max_num_reorder_pics[i],
404  0, current->dpb_max_dec_pic_buffering_minus1[i], 1, i);
405  ues(dpb_max_latency_increase_plus1[i], 0, UINT32_MAX - 1, 1, i);
406  }
407  return 0;
408 }
409 
411  RWContext *rw,
412  H266RefPicListStruct *current,
413  uint8_t list_idx, uint8_t rpls_idx,
414  const H266RawSPS *sps)
415 {
417  int err, i, j, general_layer_idx = -1, num_direct_ref_layers = 0;
418  const H266RawVPS *vps = h266->vps[sps->sps_video_parameter_set_id];
419 
420  if (!vps) {
421  av_log(ctx->log_ctx, AV_LOG_ERROR,
422  "VPS id %d not available.\n", sps->sps_video_parameter_set_id);
423  return AVERROR_INVALIDDATA;
424  }
425  //7.4.3.3 (29)
426  for (i = 0; i <= vps->vps_max_layers_minus1; i++) {
427  if (sps->nal_unit_header.nuh_layer_id == vps->vps_layer_id[i]) {
428  general_layer_idx = i;
429  break;
430  }
431  }
432  if (general_layer_idx < 0) {
433  av_log(ctx->log_ctx, AV_LOG_ERROR, "vps_layer_id %d not available.\n",
434  sps->nal_unit_header.nuh_layer_id);
435  return AVERROR_INVALIDDATA;
436  }
437  //7.4.3.3 (28)
438  for (j = 0; j <= vps->vps_max_layers_minus1; j++) {
439  if (vps->vps_direct_ref_layer_flag[general_layer_idx][j])
440  num_direct_ref_layers++;
441  }
442 
443  ue(num_ref_entries, 0, VVC_MAX_REF_ENTRIES);
444  if (sps->sps_long_term_ref_pics_flag &&
445  rpls_idx < sps->sps_num_ref_pic_lists[list_idx] &&
446  current->num_ref_entries > 0)
447  flag(ltrp_in_header_flag);
448  if (sps->sps_long_term_ref_pics_flag &&
449  rpls_idx == sps->sps_num_ref_pic_lists[list_idx])
450  infer(ltrp_in_header_flag, 1);
451  for (i = 0, j = 0; i < current->num_ref_entries; i++) {
452  if (sps->sps_inter_layer_prediction_enabled_flag)
453  flags(inter_layer_ref_pic_flag[i], 1, i);
454  else
455  infer(inter_layer_ref_pic_flag[i], 0);
456 
457  if (!current->inter_layer_ref_pic_flag[i]) {
458  if (sps->sps_long_term_ref_pics_flag)
459  flags(st_ref_pic_flag[i], 1, i);
460  else
461  infer(st_ref_pic_flag[i], 1);
462  if (current->st_ref_pic_flag[i]) {
463  int abs_delta_poc_st;
464  ues(abs_delta_poc_st[i], 0, MAX_UINT_BITS(15), 1, i);
465  if ((sps->sps_weighted_pred_flag ||
466  sps->sps_weighted_bipred_flag) && i != 0)
467  abs_delta_poc_st = current->abs_delta_poc_st[i];
468  else
469  abs_delta_poc_st = current->abs_delta_poc_st[i] + 1;
470  if (abs_delta_poc_st > 0)
471  flags(strp_entry_sign_flag[i], 1, i);
472  } else {
473  if (!current->ltrp_in_header_flag) {
474  uint8_t bits = sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4;
475  ubs(bits, rpls_poc_lsb_lt[j], 1, j);
476  j++;
477  }
478  }
479  } else {
480  if (num_direct_ref_layers == 0) {
481  av_log(ctx->log_ctx, AV_LOG_ERROR,
482  "num_direct_ref_layers needs > 0.\n");
483  return AVERROR_INVALIDDATA;
484  }
485  ues(ilrp_idx[i], 0, num_direct_ref_layers - 1, 1, i);
486  }
487  }
488  return 0;
489 }
490 
492  const H266RawSPS *sps, const H266RawPPS *pps,
493  H266RefPicLists *current) {
494  const H266RefPicListStruct * ref_list;
495  int err, i, j, num_ltrp_entries;
496  for (i = 0; i < 2; i++) {
497  if (sps->sps_num_ref_pic_lists[i] > 0 &&
498  (i == 0 || (i == 1 && pps->pps_rpl1_idx_present_flag))) {
499  flags(rpl_sps_flag[i], 1, i);
500  } else {
501  if (sps->sps_num_ref_pic_lists[i] == 0) {
502  infer(rpl_sps_flag[i], 0);
503  } else {
504  if (!pps->pps_rpl1_idx_present_flag && i == 1)
505  infer(rpl_sps_flag[1], current->rpl_sps_flag[0]);
506  }
507  }
508  if (current->rpl_sps_flag[i]) {
509  if (sps->sps_num_ref_pic_lists[i] > 1 &&
510  (i == 0 || (i == 1 && pps->pps_rpl1_idx_present_flag))) {
511  uint8_t bits = av_ceil_log2(sps->sps_num_ref_pic_lists[i]);
512  us(bits, rpl_idx[i], 0, sps->sps_num_ref_pic_lists[i] - 1, 1, i);
513  } else if (sps->sps_num_ref_pic_lists[i] == 1) {
514  infer(rpl_idx[i], 0);
515  } else if (i == 1 && !pps->pps_rpl1_idx_present_flag) {
516  infer(rpl_idx[1], current->rpl_idx[0]);
517  } else {
518  //how to handle this? or never happpend?
519  av_log(ctx->log_ctx, AV_LOG_ERROR,
520  "can't infer the rpl_idx[i]\n");
521  return AVERROR_PATCHWELCOME;
522  }
523  memcpy(&current->rpl_ref_list[i],
524  &sps->sps_ref_pic_list_struct[i][current->rpl_idx[i]],
525  sizeof(current->rpl_ref_list[i]));
526  } else {
527  CHECK(FUNC(ref_pic_list_struct) (ctx, rw, &current->rpl_ref_list[i],
528  i, sps->sps_num_ref_pic_lists[i],
529  sps));
530  }
531  ref_list = &current->rpl_ref_list[i];
532 
533  num_ltrp_entries = 0;
534  for (int k = 0; k < ref_list->num_ref_entries; k++) {
535  if (!ref_list->inter_layer_ref_pic_flag[k]) {
536  if (!ref_list->st_ref_pic_flag[k]) {
537  num_ltrp_entries++;
538  }
539  }
540  }
541 
542  for (j = 0; j < num_ltrp_entries; j++) {
543  if (ref_list->ltrp_in_header_flag) {
544  ubs(sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4,
545  poc_lsb_lt[i][j], 2, i, j);
546  }
547  flags(delta_poc_msb_cycle_present_flag[i][j], 2, i, j);
548  if (current->delta_poc_msb_cycle_present_flag[i][j]) {
549  uint32_t max =
550  1 << (32 - sps->sps_log2_max_pic_order_cnt_lsb_minus4 - 4);
551  ues(delta_poc_msb_cycle_lt[i][j], 0, max, 2, i, j);
552  }
553  }
554  }
555  return 0;
556 }
557 
559  RWContext *rw,
561 {
562  int err;
563  ub(32, num_units_in_tick);
564  u(32, time_scale, 1, MAX_UINT_BITS(32));
565  flag(general_nal_hrd_params_present_flag);
566  flag(general_vcl_hrd_params_present_flag);
567 
568  if (current->general_nal_hrd_params_present_flag ||
569  current->general_vcl_hrd_params_present_flag) {
570  flag(general_same_pic_timing_in_all_ols_flag);
571  flag(general_du_hrd_params_present_flag);
572  if (current->general_du_hrd_params_present_flag)
573  ub(8, tick_divisor_minus2);
574  ub(4, bit_rate_scale);
575  ub(4, cpb_size_scale);
576  if (current->general_du_hrd_params_present_flag)
577  ub(4, cpb_size_du_scale);
578  ue(hrd_cpb_cnt_minus1, 0, 31);
579  } else {
580  //infer general_same_pic_timing_in_all_ols_flag?
581  infer(general_du_hrd_params_present_flag, 0);
582  }
583  return 0;
584 }
585 
587  RWContext *rw,
589  int sublayer_id,
590  const H266RawGeneralTimingHrdParameters *general)
591 {
592  int err, i;
593  for (i = 0; i <= general->hrd_cpb_cnt_minus1; i++) {
594  ues(bit_rate_value_minus1[sublayer_id][i], 0, UINT32_MAX - 1, 2,
595  sublayer_id, i);
596  ues(cpb_size_value_minus1[sublayer_id][i], 0, UINT32_MAX - 1, 2,
597  sublayer_id, i);
598  if (general->general_du_hrd_params_present_flag) {
599  ues(cpb_size_du_value_minus1[sublayer_id][i],
600  0, UINT32_MAX - 1, 2, sublayer_id, i);
601  ues(bit_rate_du_value_minus1[sublayer_id][i],
602  0, UINT32_MAX - 1, 2, sublayer_id, i);
603  }
604  flags(cbr_flag[sublayer_id][i], 2, sublayer_id, i);
605  }
606  return 0;
607 }
608 
611  uint8_t first_sublayer, uint8_t max_sublayers_minus1,
612  const H266RawGeneralTimingHrdParameters *general)
613 {
614  int err, i;
615  for (i = first_sublayer; i <= max_sublayers_minus1; i++) {
616  flags(fixed_pic_rate_general_flag[i], 1, i);
617  if (!current->fixed_pic_rate_general_flag[i])
618  flags(fixed_pic_rate_within_cvs_flag[i], 1, i);
619  else
620  infer(fixed_pic_rate_within_cvs_flag[i], 1);
621  if (current->fixed_pic_rate_within_cvs_flag[i]) {
622  ues(elemental_duration_in_tc_minus1[i], 0, 2047, 1, i);
623  infer(low_delay_hrd_flag[i], 0);
624  } else if ((general->general_nal_hrd_params_present_flag ||
625  general->general_vcl_hrd_params_present_flag) &&
626  general->hrd_cpb_cnt_minus1 == 0) {
627  flags(low_delay_hrd_flag[i], 1, i);
628  } else {
629  infer(low_delay_hrd_flag[i], 0);
630  }
631  if (general->general_nal_hrd_params_present_flag)
633  &current->nal_sub_layer_hrd_parameters,
634  i, general));
635  if (general->general_vcl_hrd_params_present_flag)
637  &current->nal_sub_layer_hrd_parameters,
638  i, general));
639  }
640  return 0;
641 }
642 
644  H266RawOPI *current)
645 {
646  int err;
647 
648  HEADER("Operating point information");
649 
651  &current->nal_unit_header, VVC_OPI_NUT));
652 
653  flag(opi_ols_info_present_flag);
654  flag(opi_htid_info_present_flag);
655 
656  if(current->opi_ols_info_present_flag)
657  ue(opi_ols_idx, 0, VVC_MAX_TOTAL_NUM_OLSS - 1);
658 
659  if(current->opi_htid_info_present_flag)
660  ub(3, opi_htid_plus1);
661 
662  flag(opi_extension_flag);
663  if (current->opi_extension_flag)
664  CHECK(FUNC(extension_data) (ctx, rw, &current->extension_data));
666 
667  return 0;
668 }
669 
671  H266RawDCI *current)
672 {
673  int err, i;
674 
675  HEADER("Decoding capability information");
676 
678  &current->nal_unit_header, VVC_DCI_NUT));
679 
680  ub(4, dci_reserved_zero_4bits);
681  ub(4, dci_num_ptls_minus1);
682  for (i = 0; i <= current->dci_num_ptls_minus1; i++)
684  current->dci_profile_tier_level + i, 1, 0));
685 
686  flag(dci_extension_flag);
687  if (current->dci_extension_flag)
688  CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
690 
691  return 0;
692 }
693 
695  H266RawVPS *current)
696 {
697  int err, i, j, k;
698  uint16_t total_num_olss = 0;
699  uint8_t ols_mode_idc = 0;
700  uint16_t num_multi_layer_olss = 0;
701  uint8_t layer_included_in_ols_flag[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_LAYERS];
702  uint8_t num_ref_layers[VVC_MAX_LAYERS];
703  uint8_t reference_layer_idx[VVC_MAX_LAYERS][VVC_MAX_LAYERS];
704 
705  HEADER("Video Parameter Set");
706 
708  &current->nal_unit_header, VVC_VPS_NUT));
709 
710  u(4, vps_video_parameter_set_id, 1, VVC_MAX_VPS_COUNT - 1);
711  ub(6, vps_max_layers_minus1);
712  u(3, vps_max_sublayers_minus1, 0, 6);
713  if (current->vps_max_layers_minus1 > 0
714  && current->vps_max_sublayers_minus1 > 0)
715  flag(vps_default_ptl_dpb_hrd_max_tid_flag);
716  else
717  infer(vps_default_ptl_dpb_hrd_max_tid_flag, 1);
718 
719  if (current->vps_max_layers_minus1 > 0)
720  flag(vps_all_independent_layers_flag);
721  else
722  infer(vps_all_independent_layers_flag, 1);
723 
724  for (i = 0; i <= current->vps_max_layers_minus1; i++) {
725  ubs(6, vps_layer_id[i], 1, i);
726  if (i > 0 && current->vps_layer_id[i] <= current->vps_layer_id[i - 1]) {
727  av_log(ctx->log_ctx, AV_LOG_ERROR,
728  "vps_layer_id[%d](%d) should > vps_layer_id[%d](%d).\n",
729  i, current->vps_layer_id[i], i - 1,
730  current->vps_layer_id[i - 1]);
731  return AVERROR_INVALIDDATA;
732  }
733  if (i > 0 && !current->vps_all_independent_layers_flag) {
734  flags(vps_independent_layer_flag[i], 1, i);
735  if (!current->vps_independent_layer_flag[i]) {
736  flags(vps_max_tid_ref_present_flag[i], 1, i);
737  for (j = 0; j < i; j++) {
738  flags(vps_direct_ref_layer_flag[i][j], 2, i, j);
739  if (current->vps_max_tid_ref_present_flag[i] &&
740  current->vps_direct_ref_layer_flag[i][j]) {
741  ubs(3, vps_max_tid_il_ref_pics_plus1[i][j], 2, i, j);
742  } else {
743  infer(vps_max_tid_il_ref_pics_plus1[i][j],
744  current->vps_max_sublayers_minus1 + 1);
745  }
746  }
747  } else {
748  for (j = 0; j < i; j++) {
749  infer(vps_direct_ref_layer_flag[i][j], 0);
750  }
751  }
752  } else {
753  infer(vps_independent_layer_flag[i], 1);
754  for (j = 0; j < i; j++) {
755  infer(vps_direct_ref_layer_flag[i][j], 0);
756  }
757  }
758  }
759 
760  if (current->vps_max_layers_minus1 > 0) {
761  if (current->vps_all_independent_layers_flag)
762  flag(vps_each_layer_is_an_ols_flag);
763  else
764  infer(vps_each_layer_is_an_ols_flag, 0);
765  if (!current->vps_each_layer_is_an_ols_flag) {
766  if (!current->vps_all_independent_layers_flag)
767  ub(2, vps_ols_mode_idc);
768  else
769  infer(vps_ols_mode_idc, 2);
770  if (current->vps_ols_mode_idc == 2) {
771  ub(8, vps_num_output_layer_sets_minus2);
772  for (i = 1; i <= current->vps_num_output_layer_sets_minus2 + 1;
773  i++)
774  for (j = 0; j <= current->vps_max_layers_minus1; j++)
775  flags(vps_ols_output_layer_flag[i][j], 2, i, j);
776  }
777  ols_mode_idc = current->vps_ols_mode_idc;
778  } else {
779  ols_mode_idc = 4;
780  }
781  if (ols_mode_idc == 4 || ols_mode_idc == 0 || ols_mode_idc == 1)
782  total_num_olss = current->vps_max_layers_minus1 + 1;
783  else if (ols_mode_idc == 2)
784  total_num_olss = current->vps_num_output_layer_sets_minus2 + 2;
785  else
786  av_log(ctx->log_ctx, AV_LOG_ERROR,
787  "ols_mode_idc == 3, patch welcome");
788  u(8, vps_num_ptls_minus1, 0, total_num_olss - 1);
789  } else {
790  infer(vps_each_layer_is_an_ols_flag, 1);
791  infer(vps_num_ptls_minus1, 0);
792  }
793 
794  for (i = 0; i <= current->vps_num_ptls_minus1; i++) {
795  if (i > 0)
796  flags(vps_pt_present_flag[i], 1, i);
797  else
798  infer(vps_pt_present_flag[i], 1);
799 
800  if (!current->vps_default_ptl_dpb_hrd_max_tid_flag)
801  us(3, vps_ptl_max_tid[i], 0, current->vps_max_sublayers_minus1, 1, i);
802  else
803  infer(vps_ptl_max_tid[i], current->vps_max_sublayers_minus1);
804  }
805  while (byte_alignment(rw) != 0)
806  fixed(1, vps_ptl_alignment_zero_bit, 0);
807 
808  {
809  //calc NumMultiLayerOlss
810  int m;
811  uint8_t dependency_flag[VVC_MAX_LAYERS][VVC_MAX_LAYERS];
812  uint16_t num_output_layers_in_ols[VVC_MAX_TOTAL_NUM_OLSS];
813  uint8_t num_sub_layers_in_layer_in_ols[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_TOTAL_NUM_OLSS];
814  uint8_t output_layer_idx[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_LAYERS];
815 
816  //7.4.3.3 vps_direct_ref_layer_flag section
817  for (i = 0; i <= current->vps_max_layers_minus1; i++) {
818  for (j = 0; j <= current->vps_max_layers_minus1; j++) {
819  dependency_flag[i][j] = current->vps_direct_ref_layer_flag[i][j];
820  for (k = 0; k < i; k++) {
821  if (current->vps_direct_ref_layer_flag[i][k] &&
822  dependency_flag[k][j])
823  dependency_flag[i][j] = 1;
824  }
825  }
826  }
827  for (i = 0; i <= current->vps_max_layers_minus1; i++) {
828  int r;
829  for (j = 0, r = 0; j <= current->vps_max_layers_minus1; j++) {
830  if (dependency_flag[i][j])
831  reference_layer_idx[i][r++] = j;
832  }
833  num_ref_layers[i] = r;
834  }
835 
836  //7.4.3.3 vps_ols_output_layer_flag section
837  num_output_layers_in_ols[0] = 1;
838  num_sub_layers_in_layer_in_ols[0][0] =
839  current->vps_ptl_max_tid[current->vps_ols_ptl_idx[0]] + 1;
840  for (i = 1; i < total_num_olss; i++) {
841  if (ols_mode_idc == 4 || ols_mode_idc == 0) {
842  num_output_layers_in_ols[i] = 1;
843  if (current->vps_each_layer_is_an_ols_flag) {
844  num_sub_layers_in_layer_in_ols[i][0] =
845  current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1;
846  } else {
847  num_sub_layers_in_layer_in_ols[i][i] =
848  current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1;
849  for (k = i - 1; k >= 0; k--) {
850  num_sub_layers_in_layer_in_ols[i][k] = 0;
851  for (m = k + 1; m <= i; m++) {
852  uint8_t max_sublayer_needed =
853  FFMIN(num_sub_layers_in_layer_in_ols[i][m],
854  current->vps_max_tid_il_ref_pics_plus1[m][k]);
855  if (current->vps_direct_ref_layer_flag[m][k] &&
856  num_sub_layers_in_layer_in_ols[i][k] < max_sublayer_needed)
857  num_sub_layers_in_layer_in_ols[i][k] = max_sublayer_needed;
858  }
859  }
860  }
861  } else if (current->vps_ols_mode_idc == 1) {
862  num_output_layers_in_ols[i] = i + 1;
863  for (j = 0; j < num_output_layers_in_ols[i]; j++) {
864  num_sub_layers_in_layer_in_ols[i][j] =
865  current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1;
866  }
867  } else if (current->vps_ols_mode_idc == 2) {
868  uint8_t highest_included_layer = 0;
869  for (j = 0; j <= current->vps_max_layers_minus1; j++) {
870  layer_included_in_ols_flag[i][j] = 0;
871  num_sub_layers_in_layer_in_ols[i][j] = 0;
872  }
873  for (k = 0, j = 0; k <= current->vps_max_layers_minus1; k++) {
874  if (current->vps_ols_output_layer_flag[i][k]) {
875  layer_included_in_ols_flag[i][k] = 1;
876  highest_included_layer = k;
877  output_layer_idx[i][j] = k;
878  num_sub_layers_in_layer_in_ols[i][k] =
879  current->vps_ptl_max_tid[current->
880  vps_ols_ptl_idx[i]] + 1;
881  j++;
882  }
883  }
884  num_output_layers_in_ols[i] = j;
885  for (j = 0; j < num_output_layers_in_ols[i]; j++) {
886  int idx = output_layer_idx[i][j];
887  for (k = 0; k < num_ref_layers[idx]; k++) {
888  if (!layer_included_in_ols_flag[i][reference_layer_idx[idx][k]])
889  layer_included_in_ols_flag[i][reference_layer_idx[idx][k]] = 1;
890  }
891  }
892  for (k = highest_included_layer - 1; k >= 0; k--) {
893  if (layer_included_in_ols_flag[i][k] &&
894  !current->vps_ols_output_layer_flag[i][k]) {
895  for (m = k + 1; m <= highest_included_layer; m++) {
896  uint8_t max_sublayer_needed =
897  FFMIN(num_sub_layers_in_layer_in_ols[i][m],
898  current->vps_max_tid_il_ref_pics_plus1[m][k]);
899  if (current->vps_direct_ref_layer_flag[m][k] &&
900  layer_included_in_ols_flag[i][m] &&
901  num_sub_layers_in_layer_in_ols[i][k] <
902  max_sublayer_needed)
903  num_sub_layers_in_layer_in_ols[i][k] =
904  max_sublayer_needed;
905  }
906  }
907  }
908  }
909  if (!num_output_layers_in_ols[i])
910  return AVERROR_INVALIDDATA;
911  }
912  for (i = 1; i < total_num_olss; i++) {
913  int num_layers_in_ols = 0;
914  if (current->vps_each_layer_is_an_ols_flag) {
915  num_layers_in_ols = 1;
916  } else if (current->vps_ols_mode_idc == 0 ||
917  current->vps_ols_mode_idc == 1) {
918  num_layers_in_ols = i + 1;
919  } else if (current->vps_ols_mode_idc == 2) {
920  for (k = 0, j = 0; k <= current->vps_max_layers_minus1; k++)
921  if (layer_included_in_ols_flag[i][k])
922  j++;
923  num_layers_in_ols = j;
924  }
925  if (num_layers_in_ols > 1) {
926  num_multi_layer_olss++;
927  }
928  }
929  if (!current->vps_each_layer_is_an_ols_flag && num_multi_layer_olss == 0)
930  return AVERROR_INVALIDDATA;
931  }
932 
933  for (i = 0; i <= current->vps_num_ptls_minus1; i++) {
935  current->vps_profile_tier_level + i,
936  current->vps_pt_present_flag[i],
937  current->vps_ptl_max_tid[i]));
938  }
939  for (i = 0; i < total_num_olss; i++) {
940  if (current->vps_num_ptls_minus1 > 0 &&
941  current->vps_num_ptls_minus1 + 1 != total_num_olss) {
942  us(8, vps_ols_ptl_idx[i], 0, current->vps_num_ptls_minus1, 1, i);
943  } else if (current->vps_num_ptls_minus1 == 0) {
944  infer(vps_ols_ptl_idx[i], 0);
945  } else {
946  infer(vps_ols_ptl_idx[i], i);
947  }
948  }
949 
950  if (!current->vps_each_layer_is_an_ols_flag) {
951  uint16_t vps_num_dpb_params;
952  ue(vps_num_dpb_params_minus1, 0, num_multi_layer_olss - 1);
953  if (current->vps_each_layer_is_an_ols_flag)
954  vps_num_dpb_params = 0;
955  else
956  vps_num_dpb_params = current->vps_num_dpb_params_minus1 + 1;
957 
958  if (current->vps_max_sublayers_minus1 > 0)
959  flag(vps_sublayer_dpb_params_present_flag);
960  else
961  infer(vps_sublayer_dpb_params_present_flag, 0);
962 
963  for (i = 0; i < vps_num_dpb_params; i++) {
964  if (!current->vps_default_ptl_dpb_hrd_max_tid_flag)
965  us(3, vps_dpb_max_tid[i], 0, current->vps_max_sublayers_minus1,
966  1, i);
967  else
968  infer(vps_dpb_max_tid[i], current->vps_max_sublayers_minus1);
969  CHECK(FUNC(dpb_parameters) (ctx, rw, current->vps_dpb_params + i,
970  current->vps_dpb_max_tid[i],
971  current->
972  vps_sublayer_dpb_params_present_flag));
973  }
974  for (i = 0; i < num_multi_layer_olss; i++) {
975  ues(vps_ols_dpb_pic_width[i], 0, UINT16_MAX, 1, i);
976  ues(vps_ols_dpb_pic_height[i], 0, UINT16_MAX, 1, i);
977  ubs(2, vps_ols_dpb_chroma_format[i], 1, i);
978  ues(vps_ols_dpb_bitdepth_minus8[i], 0, 8, 1, i);
979  if (vps_num_dpb_params > 1
980  && vps_num_dpb_params != num_multi_layer_olss)
981  ues(vps_ols_dpb_params_idx[i], 0, vps_num_dpb_params - 1, 1, i);
982  else if (vps_num_dpb_params == 1)
983  infer(vps_ols_dpb_params_idx[i], 0);
984  else
985  infer(vps_ols_dpb_params_idx[i], i);
986  }
987  flag(vps_timing_hrd_params_present_flag);
988  if (current->vps_timing_hrd_params_present_flag) {
990  &current->
991  vps_general_timing_hrd_parameters));
992  if (current->vps_max_sublayers_minus1 > 0)
993  flag(vps_sublayer_cpb_params_present_flag);
994  else
995  infer(vps_sublayer_cpb_params_present_flag, 0);
996  ue(vps_num_ols_timing_hrd_params_minus1, 0,
997  num_multi_layer_olss - 1);
998  for (i = 0; i <= current->vps_num_ols_timing_hrd_params_minus1; i++) {
999  uint8_t first_sublayer;
1000  if (!current->vps_default_ptl_dpb_hrd_max_tid_flag)
1001  us(3, vps_hrd_max_tid[i], 0,
1002  current->vps_max_sublayers_minus1, 1, i);
1003  else
1004  infer(vps_hrd_max_tid[i],
1005  current->vps_max_sublayers_minus1);
1006  first_sublayer = current->vps_sublayer_cpb_params_present_flag ?
1007  0 : current->vps_hrd_max_tid[i];
1009  (ctx, rw, &current->vps_ols_timing_hrd_parameters,
1010  first_sublayer, current->vps_max_sublayers_minus1,
1011  &current->vps_general_timing_hrd_parameters));
1012 
1013  }
1014  if (current->vps_num_ols_timing_hrd_params_minus1 > 0 &&
1015  current->vps_num_ols_timing_hrd_params_minus1 + 1 !=
1016  num_multi_layer_olss) {
1017  for (i = 0; i < num_multi_layer_olss; i++) {
1018  ues(vps_ols_timing_hrd_idx[i], 0,
1019  current->vps_num_ols_timing_hrd_params_minus1, 1, i);
1020  }
1021  } else if (current->vps_num_ols_timing_hrd_params_minus1 == 0) {
1022  for (i = 0; i < num_multi_layer_olss; i++)
1023  infer(vps_ols_timing_hrd_idx[i], 0);
1024  } else {
1025  for (i = 0; i < num_multi_layer_olss; i++)
1026  infer(vps_ols_timing_hrd_idx[i], i);
1027  }
1028  }
1029  }
1030 
1031  flag(vps_extension_flag);
1032  if (current->vps_extension_flag)
1033  CHECK(FUNC(extension_data) (ctx, rw, &current->extension_data));
1034  CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
1035 
1036  return 0;
1037 }
1038 
1040  H266RawSPS *current)
1041 {
1042  int err;
1043 
1044  flag(sps_extended_precision_flag);
1045  if (current->sps_transform_skip_enabled_flag)
1046  flag(sps_ts_residual_coding_rice_present_in_sh_flag);
1047  else
1048  infer(sps_ts_residual_coding_rice_present_in_sh_flag, 0);
1049  flag(sps_rrc_rice_extension_flag);
1050  flag(sps_persistent_rice_adaptation_enabled_flag);
1051  flag(sps_reverse_last_sig_coeff_enabled_flag);
1052 
1053  return 0;
1054 }
1055 
1057  H266RawSPS *current)
1058 {
1060  int err, i, j;
1061  unsigned int ctb_log2_size_y, min_cb_log2_size_y,
1062  min_qt_log2_size_intra_y, min_qt_log2_size_inter_y,
1063  ctb_size_y, max_num_merge_cand, tmp_width_val, tmp_height_val;
1064  uint8_t qp_bd_offset;
1065 
1066  static const uint8_t h266_sub_width_c[] = {
1067  1, 2, 2, 1
1068  };
1069  static const uint8_t h266_sub_height_c[] = {
1070  1, 2, 1, 1
1071  };
1072 
1073  HEADER("Sequence Parameter Set");
1074 
1075  CHECK(FUNC(nal_unit_header) (ctx, rw,
1076  &current->nal_unit_header, VVC_SPS_NUT));
1077 
1078  ub(4, sps_seq_parameter_set_id);
1079  ub(4, sps_video_parameter_set_id);
1080  if (current->sps_video_parameter_set_id == 0 && !h266->vps[0]) {
1081  H266RawVPS *vps = ff_refstruct_allocz(sizeof(*vps));
1082  if (!vps)
1083  return AVERROR(ENOMEM);
1084  vps->vps_max_layers_minus1 = 0;
1085  vps->vps_independent_layer_flag[0] = 1;
1086  vps->vps_layer_id[0] = current->nal_unit_header.nuh_layer_id;
1087  h266->vps[0] = vps;
1088  }
1089 
1090  u(3, sps_max_sublayers_minus1, 0, VVC_MAX_SUBLAYERS - 1);
1091  u(2, sps_chroma_format_idc, 0, 3);
1092  u(2, sps_log2_ctu_size_minus5, 0, 3);
1093  ctb_log2_size_y = current->sps_log2_ctu_size_minus5 + 5;
1094  ctb_size_y = 1 << ctb_log2_size_y;
1095 
1096  flag(sps_ptl_dpb_hrd_params_present_flag);
1097  if (current->sps_ptl_dpb_hrd_params_present_flag) {
1098  CHECK(FUNC(profile_tier_level) (ctx, rw, &current->profile_tier_level,
1099  1, current->sps_max_sublayers_minus1));
1100  }
1101  flag(sps_gdr_enabled_flag);
1102  flag(sps_ref_pic_resampling_enabled_flag);
1103  if (current->sps_ref_pic_resampling_enabled_flag)
1104  flag(sps_res_change_in_clvs_allowed_flag);
1105  else
1106  infer(sps_res_change_in_clvs_allowed_flag, 0);
1107 
1108  ue(sps_pic_width_max_in_luma_samples, 1, VVC_MAX_WIDTH);
1109  ue(sps_pic_height_max_in_luma_samples, 1, VVC_MAX_HEIGHT);
1110 
1111  flag(sps_conformance_window_flag);
1112  if (current->sps_conformance_window_flag) {
1113  uint8_t sub_width_c = h266_sub_width_c[current->sps_chroma_format_idc];
1114  uint8_t sub_height_c = h266_sub_height_c[current->sps_chroma_format_idc];
1115  uint16_t width = current->sps_pic_width_max_in_luma_samples / sub_width_c;
1116  uint16_t height = current->sps_pic_height_max_in_luma_samples / sub_height_c;
1117  ue(sps_conf_win_left_offset, 0, width);
1118  ue(sps_conf_win_right_offset, 0, width - current->sps_conf_win_left_offset);
1119  ue(sps_conf_win_top_offset, 0, height);
1120  ue(sps_conf_win_bottom_offset, 0, height - current->sps_conf_win_top_offset);
1121  } else {
1122  infer(sps_conf_win_left_offset, 0);
1123  infer(sps_conf_win_right_offset, 0);
1124  infer(sps_conf_win_top_offset, 0);
1125  infer(sps_conf_win_bottom_offset, 0);
1126  }
1127 
1128  tmp_width_val = AV_CEIL_RSHIFT(current->sps_pic_width_max_in_luma_samples,
1129  ctb_log2_size_y);
1130  tmp_height_val = AV_CEIL_RSHIFT(current->sps_pic_height_max_in_luma_samples,
1131  ctb_log2_size_y);
1132 
1133  flag(sps_subpic_info_present_flag);
1134  if (current->sps_subpic_info_present_flag) {
1135  ue(sps_num_subpics_minus1, 0, VVC_MAX_SLICES - 1);
1136  if (current->sps_num_subpics_minus1 > 0) {
1137  flag(sps_independent_subpics_flag);
1138  flag(sps_subpic_same_size_flag);
1139  }
1140 
1141  if (current->sps_num_subpics_minus1 > 0) {
1142  int wlen = av_ceil_log2(tmp_width_val);
1143  int hlen = av_ceil_log2(tmp_height_val);
1144  infer(sps_subpic_ctu_top_left_x[0], 0);
1145  infer(sps_subpic_ctu_top_left_y[0], 0);
1146  if (current->sps_pic_width_max_in_luma_samples > ctb_size_y)
1147  ubs(wlen, sps_subpic_width_minus1[0], 1, 0);
1148  else
1149  infer(sps_subpic_width_minus1[0], tmp_width_val - 1);
1150  if (current->sps_pic_height_max_in_luma_samples > ctb_size_y)
1151  ubs(hlen, sps_subpic_height_minus1[0], 1, 0);
1152  else
1153  infer(sps_subpic_height_minus1[0], tmp_height_val - 1);
1154  if (!current->sps_independent_subpics_flag) {
1155  flags(sps_subpic_treated_as_pic_flag[0], 1, 0);
1156  flags(sps_loop_filter_across_subpic_enabled_flag[0], 1, 0);
1157  } else {
1158  infer(sps_subpic_treated_as_pic_flag[0], 1);
1159  infer(sps_loop_filter_across_subpic_enabled_flag[0], 1);
1160  }
1161  for (i = 1; i <= current->sps_num_subpics_minus1; i++) {
1162  if (!current->sps_subpic_same_size_flag) {
1163  if (current->sps_pic_width_max_in_luma_samples > ctb_size_y)
1164  ubs(wlen, sps_subpic_ctu_top_left_x[i], 1, i);
1165  else
1166  infer(sps_subpic_ctu_top_left_x[i], 0);
1167  if (current->sps_pic_height_max_in_luma_samples >
1168  ctb_size_y)
1169  ubs(hlen, sps_subpic_ctu_top_left_y[i], 1, i);
1170  else
1171  infer(sps_subpic_ctu_top_left_y[i], 0);
1172  if (i < current->sps_num_subpics_minus1 &&
1173  current->sps_pic_width_max_in_luma_samples >
1174  ctb_size_y) {
1175  ubs(wlen, sps_subpic_width_minus1[i], 1, i);
1176  } else {
1177  infer(sps_subpic_width_minus1[i],
1178  tmp_width_val -
1179  current->sps_subpic_ctu_top_left_x[i] - 1);
1180  }
1181  if (i < current->sps_num_subpics_minus1 &&
1182  current->sps_pic_height_max_in_luma_samples >
1183  ctb_size_y) {
1184  ubs(hlen, sps_subpic_height_minus1[i], 1, i);
1185  } else {
1186  infer(sps_subpic_height_minus1[i],
1187  tmp_height_val -
1188  current->sps_subpic_ctu_top_left_y[i] - 1);
1189  }
1190  } else {
1191  int num_subpic_cols = tmp_width_val /
1192  (current->sps_subpic_width_minus1[0] + 1);
1193  if (tmp_width_val % (current->sps_subpic_width_minus1[0] + 1) ||
1194  tmp_height_val % (current->sps_subpic_width_minus1[0] + 1) ||
1195  current->sps_num_subpics_minus1 !=
1196  (num_subpic_cols * tmp_height_val /
1197  (current->sps_subpic_height_minus1[0] + 1) - 1))
1198  return AVERROR_INVALIDDATA;
1199  infer(sps_subpic_ctu_top_left_x[i],
1200  (i % num_subpic_cols) *
1201  (current->sps_subpic_width_minus1[0] + 1));
1202  infer(sps_subpic_ctu_top_left_y[i],
1203  (i / num_subpic_cols) *
1204  (current->sps_subpic_height_minus1[0] + 1));
1205  infer(sps_subpic_width_minus1[i],
1206  current->sps_subpic_width_minus1[0]);
1207  infer(sps_subpic_height_minus1[i],
1208  current->sps_subpic_height_minus1[0]);
1209  }
1210  if (!current->sps_independent_subpics_flag) {
1211  flags(sps_subpic_treated_as_pic_flag[i], 1, i);
1212  flags(sps_loop_filter_across_subpic_enabled_flag[i], 1, i);
1213  } else {
1214  infer(sps_subpic_treated_as_pic_flag[i], 1);
1215  infer(sps_loop_filter_across_subpic_enabled_flag[i], 0);
1216  }
1217  }
1218  } else {
1219  infer(sps_subpic_ctu_top_left_x[0], 0);
1220  infer(sps_subpic_ctu_top_left_y[0], 0);
1221  infer(sps_subpic_width_minus1[0], tmp_width_val - 1);
1222  infer(sps_subpic_height_minus1[0], tmp_height_val - 1);
1223  }
1224  ue(sps_subpic_id_len_minus1, 0, 15);
1225  if ((1 << (current->sps_subpic_id_len_minus1 + 1)) <
1226  current->sps_num_subpics_minus1 + 1) {
1227  av_log(ctx->log_ctx, AV_LOG_ERROR,
1228  "sps_subpic_id_len_minus1(%d) is too small\n",
1229  current->sps_subpic_id_len_minus1);
1230  return AVERROR_INVALIDDATA;
1231  }
1232  flag(sps_subpic_id_mapping_explicitly_signalled_flag);
1233  if (current->sps_subpic_id_mapping_explicitly_signalled_flag) {
1234  flag(sps_subpic_id_mapping_present_flag);
1235  if (current->sps_subpic_id_mapping_present_flag) {
1236  for (i = 0; i <= current->sps_num_subpics_minus1; i++) {
1237  ubs(current->sps_subpic_id_len_minus1 + 1,
1238  sps_subpic_id[i], 1, i);
1239  }
1240  }
1241  }
1242  } else {
1243  infer(sps_num_subpics_minus1, 0);
1244  infer(sps_independent_subpics_flag, 1);
1245  infer(sps_subpic_same_size_flag, 0);
1246  infer(sps_subpic_id_mapping_explicitly_signalled_flag, 0);
1247  infer(sps_subpic_ctu_top_left_x[0], 0);
1248  infer(sps_subpic_ctu_top_left_y[0], 0);
1249  infer(sps_subpic_width_minus1[0], tmp_width_val - 1);
1250  infer(sps_subpic_height_minus1[0], tmp_height_val - 1);
1251  }
1252 
1253 
1254  ue(sps_bitdepth_minus8, 0, 8);
1255  qp_bd_offset = 6 * current->sps_bitdepth_minus8;
1256 
1257  flag(sps_entropy_coding_sync_enabled_flag);
1258  flag(sps_entry_point_offsets_present_flag);
1259 
1260  u(4, sps_log2_max_pic_order_cnt_lsb_minus4, 0, 12);
1261  flag(sps_poc_msb_cycle_flag);
1262  if (current->sps_poc_msb_cycle_flag)
1263  ue(sps_poc_msb_cycle_len_minus1,
1264  0, 32 - current->sps_log2_max_pic_order_cnt_lsb_minus4 - 5);
1265 
1266  u(2, sps_num_extra_ph_bytes, 0, 2);
1267  for (i = 0; i < (current->sps_num_extra_ph_bytes * 8); i++) {
1268  flags(sps_extra_ph_bit_present_flag[i], 1, i);
1269  }
1270 
1271  u(2, sps_num_extra_sh_bytes, 0, 2);
1272  for (i = 0; i < (current->sps_num_extra_sh_bytes * 8); i++) {
1273  flags(sps_extra_sh_bit_present_flag[i], 1, i);
1274  }
1275 
1276  if (current->sps_ptl_dpb_hrd_params_present_flag) {
1277  if (current->sps_max_sublayers_minus1 > 0)
1278  flag(sps_sublayer_dpb_params_flag);
1279  else
1280  infer(sps_sublayer_dpb_params_flag, 0);
1281  CHECK(FUNC(dpb_parameters) (ctx, rw, &current->sps_dpb_params,
1282  current->sps_max_sublayers_minus1,
1283  current->sps_sublayer_dpb_params_flag));
1284  }
1285 
1286  ue(sps_log2_min_luma_coding_block_size_minus2,
1287  0, FFMIN(4, current->sps_log2_ctu_size_minus5 + 3));
1288  min_cb_log2_size_y =
1289  current->sps_log2_min_luma_coding_block_size_minus2 + 2;
1290 
1291  flag(sps_partition_constraints_override_enabled_flag);
1292 
1293  ue(sps_log2_diff_min_qt_min_cb_intra_slice_luma,
1294  0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
1295  min_qt_log2_size_intra_y =
1296  current->sps_log2_diff_min_qt_min_cb_intra_slice_luma +
1297  min_cb_log2_size_y;
1298 
1299  ue(sps_max_mtt_hierarchy_depth_intra_slice_luma,
1300  0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
1301 
1302  if (current->sps_max_mtt_hierarchy_depth_intra_slice_luma != 0) {
1303  ue(sps_log2_diff_max_bt_min_qt_intra_slice_luma,
1304  0, ctb_log2_size_y - min_qt_log2_size_intra_y);
1305  ue(sps_log2_diff_max_tt_min_qt_intra_slice_luma,
1306  0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_y);
1307  } else {
1308  infer(sps_log2_diff_max_bt_min_qt_intra_slice_luma, 0);
1309  infer(sps_log2_diff_max_tt_min_qt_intra_slice_luma, 0);
1310  }
1311 
1312  if (current->sps_chroma_format_idc != 0) {
1313  flag(sps_qtbtt_dual_tree_intra_flag);
1314  } else {
1315  infer(sps_qtbtt_dual_tree_intra_flag, 0);
1316  }
1317 
1318  if (current->sps_qtbtt_dual_tree_intra_flag) {
1319  ue(sps_log2_diff_min_qt_min_cb_intra_slice_chroma,
1320  0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
1321  ue(sps_max_mtt_hierarchy_depth_intra_slice_chroma,
1322  0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
1323  if (current->sps_max_mtt_hierarchy_depth_intra_slice_chroma != 0) {
1324  unsigned int min_qt_log2_size_intra_c =
1325  current->sps_log2_diff_min_qt_min_cb_intra_slice_chroma +
1326  min_cb_log2_size_y;
1327  ue(sps_log2_diff_max_bt_min_qt_intra_slice_chroma,
1328  0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c);
1329  ue(sps_log2_diff_max_tt_min_qt_intra_slice_chroma,
1330  0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c);
1331  }
1332  } else {
1333  infer(sps_log2_diff_min_qt_min_cb_intra_slice_chroma, 0);
1334  infer(sps_max_mtt_hierarchy_depth_intra_slice_chroma, 0);
1335  }
1336  if (current->sps_max_mtt_hierarchy_depth_intra_slice_chroma == 0) {
1337  infer(sps_log2_diff_max_bt_min_qt_intra_slice_chroma, 0);
1338  infer(sps_log2_diff_max_tt_min_qt_intra_slice_chroma, 0);
1339  }
1340 
1341  ue(sps_log2_diff_min_qt_min_cb_inter_slice,
1342  0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
1343  min_qt_log2_size_inter_y =
1344  current->sps_log2_diff_min_qt_min_cb_inter_slice + min_cb_log2_size_y;
1345 
1346  ue(sps_max_mtt_hierarchy_depth_inter_slice,
1347  0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
1348  if (current->sps_max_mtt_hierarchy_depth_inter_slice != 0) {
1349  ue(sps_log2_diff_max_bt_min_qt_inter_slice,
1350  0, ctb_log2_size_y - min_qt_log2_size_inter_y);
1351  ue(sps_log2_diff_max_tt_min_qt_inter_slice,
1352  0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_inter_y);
1353  } else {
1354  infer(sps_log2_diff_max_bt_min_qt_inter_slice, 0);
1355  infer(sps_log2_diff_max_tt_min_qt_inter_slice, 0);
1356  }
1357 
1358  if (ctb_size_y > 32)
1359  flag(sps_max_luma_transform_size_64_flag);
1360  else
1361  infer(sps_max_luma_transform_size_64_flag, 0);
1362 
1363  flag(sps_transform_skip_enabled_flag);
1364  if (current->sps_transform_skip_enabled_flag) {
1365  ue(sps_log2_transform_skip_max_size_minus2, 0, 3);
1366  flag(sps_bdpcm_enabled_flag);
1367  }
1368 
1369  flag(sps_mts_enabled_flag);
1370  if (current->sps_mts_enabled_flag) {
1371  flag(sps_explicit_mts_intra_enabled_flag);
1372  flag(sps_explicit_mts_inter_enabled_flag);
1373  } else {
1374  infer(sps_explicit_mts_intra_enabled_flag, 0);
1375  infer(sps_explicit_mts_inter_enabled_flag, 0);
1376  }
1377 
1378  flag(sps_lfnst_enabled_flag);
1379 
1380  if (current->sps_chroma_format_idc != 0) {
1381  uint8_t num_qp_tables;
1382  flag(sps_joint_cbcr_enabled_flag);
1383  flag(sps_same_qp_table_for_chroma_flag);
1384  num_qp_tables = current->sps_same_qp_table_for_chroma_flag ?
1385  1 : (current->sps_joint_cbcr_enabled_flag ? 3 : 2);
1386  for (i = 0; i < num_qp_tables; i++) {
1387  ses(sps_qp_table_start_minus26[i], -26 - qp_bd_offset, 36, 1, i);
1388  ues(sps_num_points_in_qp_table_minus1[i],
1389  0, 36 - current->sps_qp_table_start_minus26[i], 1, i);
1390  for (j = 0; j <= current->sps_num_points_in_qp_table_minus1[i]; j++) {
1391  uint8_t max = MAX_UINT_BITS(8);
1392  ues(sps_delta_qp_in_val_minus1[i][j], 0, max, 2, i, j);
1393  ues(sps_delta_qp_diff_val[i][j], 0, max, 2, i, j);
1394  }
1395  }
1396  } else {
1397  infer(sps_joint_cbcr_enabled_flag, 0);
1398  infer(sps_same_qp_table_for_chroma_flag, 0);
1399  }
1400 
1401  flag(sps_sao_enabled_flag);
1402  flag(sps_alf_enabled_flag);
1403  if (current->sps_alf_enabled_flag && current->sps_chroma_format_idc)
1404  flag(sps_ccalf_enabled_flag);
1405  else
1406  infer(sps_ccalf_enabled_flag, 0);
1407  flag(sps_lmcs_enabled_flag);
1408  flag(sps_weighted_pred_flag);
1409  flag(sps_weighted_bipred_flag);
1410  flag(sps_long_term_ref_pics_flag);
1411  if (current->sps_video_parameter_set_id > 0)
1412  flag(sps_inter_layer_prediction_enabled_flag);
1413  else
1414  infer(sps_inter_layer_prediction_enabled_flag, 0);
1415  flag(sps_idr_rpl_present_flag);
1416  flag(sps_rpl1_same_as_rpl0_flag);
1417 
1418  for (i = 0; i < (current->sps_rpl1_same_as_rpl0_flag ? 1 : 2); i++) {
1419  ues(sps_num_ref_pic_lists[i], 0, VVC_MAX_REF_PIC_LISTS, 1, i);
1420  for (j = 0; j < current->sps_num_ref_pic_lists[i]; j++)
1422  &current->
1423  sps_ref_pic_list_struct[i][j], i,
1424  j, current));
1425  }
1426 
1427  if (current->sps_rpl1_same_as_rpl0_flag) {
1428  current->sps_num_ref_pic_lists[1] = current->sps_num_ref_pic_lists[0];
1429  for (j = 0; j < current->sps_num_ref_pic_lists[0]; j++)
1430  memcpy(&current->sps_ref_pic_list_struct[1][j],
1431  &current->sps_ref_pic_list_struct[0][j],
1432  sizeof(current->sps_ref_pic_list_struct[0][j]));
1433  }
1434 
1435  flag(sps_ref_wraparound_enabled_flag);
1436 
1437  flag(sps_temporal_mvp_enabled_flag);
1438  if (current->sps_temporal_mvp_enabled_flag)
1439  flag(sps_sbtmvp_enabled_flag);
1440  else
1441  infer(sps_sbtmvp_enabled_flag, 0);
1442 
1443  flag(sps_amvr_enabled_flag);
1444  flag(sps_bdof_enabled_flag);
1445  if (current->sps_bdof_enabled_flag)
1446  flag(sps_bdof_control_present_in_ph_flag);
1447  else
1448  infer(sps_bdof_control_present_in_ph_flag, 0);
1449 
1450  flag(sps_smvd_enabled_flag);
1451  flag(sps_dmvr_enabled_flag);
1452  if (current->sps_dmvr_enabled_flag)
1453  flag(sps_dmvr_control_present_in_ph_flag);
1454  else
1455  infer(sps_dmvr_control_present_in_ph_flag, 0);
1456 
1457  flag(sps_mmvd_enabled_flag);
1458  if (current->sps_mmvd_enabled_flag)
1459  flag(sps_mmvd_fullpel_only_enabled_flag);
1460  else
1461  infer(sps_mmvd_fullpel_only_enabled_flag, 0);
1462 
1463  ue(sps_six_minus_max_num_merge_cand, 0, 5);
1464  max_num_merge_cand = 6 - current->sps_six_minus_max_num_merge_cand;
1465 
1466  flag(sps_sbt_enabled_flag);
1467 
1468  flag(sps_affine_enabled_flag);
1469  if (current->sps_affine_enabled_flag) {
1470  ue(sps_five_minus_max_num_subblock_merge_cand,
1471  0, 5 - current->sps_sbtmvp_enabled_flag);
1472  flag(sps_6param_affine_enabled_flag);
1473  if (current->sps_amvr_enabled_flag)
1474  flag(sps_affine_amvr_enabled_flag);
1475  else
1476  infer(sps_affine_amvr_enabled_flag, 0);
1477  flag(sps_affine_prof_enabled_flag);
1478  if (current->sps_affine_prof_enabled_flag)
1479  flag(sps_prof_control_present_in_ph_flag);
1480  else
1481  infer(sps_prof_control_present_in_ph_flag, 0);
1482  } else {
1483  infer(sps_6param_affine_enabled_flag, 0);
1484  infer(sps_affine_amvr_enabled_flag, 0);
1485  infer(sps_affine_prof_enabled_flag, 0);
1486  infer(sps_prof_control_present_in_ph_flag, 0);
1487  }
1488 
1489  flag(sps_bcw_enabled_flag);
1490  flag(sps_ciip_enabled_flag);
1491 
1492  if (max_num_merge_cand >= 2) {
1493  flag(sps_gpm_enabled_flag);
1494  if (current->sps_gpm_enabled_flag && max_num_merge_cand >= 3)
1495  ue(sps_max_num_merge_cand_minus_max_num_gpm_cand,
1496  0, max_num_merge_cand - 2);
1497  } else {
1498  infer(sps_gpm_enabled_flag, 0);
1499  }
1500 
1501  ue(sps_log2_parallel_merge_level_minus2, 0, ctb_log2_size_y - 2);
1502 
1503  flag(sps_isp_enabled_flag);
1504  flag(sps_mrl_enabled_flag);
1505  flag(sps_mip_enabled_flag);
1506 
1507  if (current->sps_chroma_format_idc != 0)
1508  flag(sps_cclm_enabled_flag);
1509  else
1510  infer(sps_cclm_enabled_flag, 0);
1511  if (current->sps_chroma_format_idc == 1) {
1512  flag(sps_chroma_horizontal_collocated_flag);
1513  flag(sps_chroma_vertical_collocated_flag);
1514  } else {
1515  infer(sps_chroma_horizontal_collocated_flag, 1);
1516  infer(sps_chroma_vertical_collocated_flag, 1);
1517  }
1518 
1519  flag(sps_palette_enabled_flag);
1520  if (current->sps_chroma_format_idc == 3 &&
1521  !current->sps_max_luma_transform_size_64_flag)
1522  flag(sps_act_enabled_flag);
1523  else
1524  infer(sps_act_enabled_flag, 0);
1525  if (current->sps_transform_skip_enabled_flag ||
1526  current->sps_palette_enabled_flag)
1527  ue(sps_min_qp_prime_ts, 0, 8);
1528 
1529  flag(sps_ibc_enabled_flag);
1530  if (current->sps_ibc_enabled_flag)
1531  ue(sps_six_minus_max_num_ibc_merge_cand, 0, 5);
1532 
1533  flag(sps_ladf_enabled_flag);
1534  if (current->sps_ladf_enabled_flag) {
1535  ub(2, sps_num_ladf_intervals_minus2);
1536  se(sps_ladf_lowest_interval_qp_offset, -63, 63);
1537  for (i = 0; i < current->sps_num_ladf_intervals_minus2 + 1; i++) {
1538  ses(sps_ladf_qp_offset[i], -63, 63, 1, i);
1539  ues(sps_ladf_delta_threshold_minus1[i],
1540  0, (2 << (8 + current->sps_bitdepth_minus8)) - 3, 1, i);
1541  }
1542  }
1543 
1544  flag(sps_explicit_scaling_list_enabled_flag);
1545  if (current->sps_lfnst_enabled_flag &&
1546  current->sps_explicit_scaling_list_enabled_flag)
1547  flag(sps_scaling_matrix_for_lfnst_disabled_flag);
1548 
1549  if (current->sps_act_enabled_flag &&
1550  current->sps_explicit_scaling_list_enabled_flag)
1551  flag(sps_scaling_matrix_for_alternative_colour_space_disabled_flag);
1552  else
1553  infer(sps_scaling_matrix_for_alternative_colour_space_disabled_flag, 0);
1554  if (current->sps_scaling_matrix_for_alternative_colour_space_disabled_flag)
1555  flag(sps_scaling_matrix_designated_colour_space_flag);
1556 
1557  flag(sps_dep_quant_enabled_flag);
1558  flag(sps_sign_data_hiding_enabled_flag);
1559 
1560  flag(sps_virtual_boundaries_enabled_flag);
1561  if (current->sps_virtual_boundaries_enabled_flag) {
1562  flag(sps_virtual_boundaries_present_flag);
1563  if (current->sps_virtual_boundaries_present_flag) {
1564  ue(sps_num_ver_virtual_boundaries,
1565  0, current->sps_pic_width_max_in_luma_samples <= 8 ? 0 : 3);
1566  for (i = 0; i < current->sps_num_ver_virtual_boundaries; i++)
1567  ues(sps_virtual_boundary_pos_x_minus1[i],
1568  0, (current->sps_pic_width_max_in_luma_samples + 7) / 8 - 2,
1569  1, i);
1570  ue(sps_num_hor_virtual_boundaries,
1571  0, current->sps_pic_height_max_in_luma_samples <= 8 ? 0 : 3);
1572  for (i = 0; i < current->sps_num_hor_virtual_boundaries; i++)
1573  ues(sps_virtual_boundary_pos_y_minus1[i],
1574  0, (current->sps_pic_height_max_in_luma_samples + 7) /
1575  8 - 2, 1, i);
1576  }
1577  } else {
1578  infer(sps_virtual_boundaries_present_flag, 0);
1579  infer(sps_num_ver_virtual_boundaries, 0);
1580  infer(sps_num_hor_virtual_boundaries, 0);
1581  }
1582 
1583  if (current->sps_ptl_dpb_hrd_params_present_flag) {
1584  flag(sps_timing_hrd_params_present_flag);
1585  if (current->sps_timing_hrd_params_present_flag) {
1586  uint8_t first_sublayer;
1588  &current->sps_general_timing_hrd_parameters));
1589  if (current->sps_max_sublayers_minus1 > 0)
1590  flag(sps_sublayer_cpb_params_present_flag);
1591  else
1592  infer(sps_sublayer_cpb_params_present_flag, 0);
1593  first_sublayer = current->sps_sublayer_cpb_params_present_flag ?
1594  0 : current->sps_max_sublayers_minus1;
1596  &current->sps_ols_timing_hrd_parameters, first_sublayer,
1597  current->sps_max_sublayers_minus1,
1598  &current->sps_general_timing_hrd_parameters));
1599  }
1600  }
1601 
1602  flag(sps_field_seq_flag);
1603  flag(sps_vui_parameters_present_flag);
1604  if (current->sps_vui_parameters_present_flag) {
1605  ue(sps_vui_payload_size_minus1, 0, 1023);
1606  while (byte_alignment(rw) != 0)
1607  fixed(1, sps_vui_alignment_zero_bit, 0);
1608  CHECK(FUNC(vui_payload) (ctx, rw, &current->vui,
1609  current->sps_vui_payload_size_minus1 + 1,
1610  current->sps_chroma_format_idc));
1611  } else {
1612  CHECK(FUNC(vui_parameters_default) (ctx, rw, &current->vui));
1613  }
1614 
1615  flag(sps_extension_flag);
1616  if (current->sps_extension_flag) {
1617  flag(sps_range_extension_flag);
1618  ub(7, sps_extension_7bits);
1619 
1620  if (current->sps_range_extension_flag) {
1621  CHECK(FUNC(sps_range_extension)(ctx, rw, current));
1622  } else {
1623  infer(sps_extended_precision_flag, 0);
1624  infer(sps_ts_residual_coding_rice_present_in_sh_flag, 0);
1625  infer(sps_rrc_rice_extension_flag, 0);
1626  infer(sps_persistent_rice_adaptation_enabled_flag, 0);
1627  infer(sps_reverse_last_sig_coeff_enabled_flag, 0);
1628  }
1629  } else {
1630  infer(sps_range_extension_flag, 0);
1631  infer(sps_extension_7bits, 0);
1632  infer(sps_extended_precision_flag, 0);
1633  infer(sps_ts_residual_coding_rice_present_in_sh_flag, 0);
1634  infer(sps_rrc_rice_extension_flag, 0);
1635  infer(sps_persistent_rice_adaptation_enabled_flag, 0);
1636  infer(sps_reverse_last_sig_coeff_enabled_flag, 0);
1637  }
1638 
1639  if (current->sps_extension_7bits)
1640  CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
1641 
1642  CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
1643 
1644  return 0;
1645 }
1646 
1648  H266RawPPS *current)
1649 {
1651  const H266RawSPS *sps;
1652  int err, i;
1653  unsigned int min_cb_size_y, divisor, ctb_size_y,
1654  pic_width_in_ctbs_y, pic_height_in_ctbs_y;
1655  uint8_t sub_width_c, sub_height_c, qp_bd_offset;
1656 
1657  static const uint8_t h266_sub_width_c[] = {
1658  1, 2, 2, 1
1659  };
1660  static const uint8_t h266_sub_height_c[] = {
1661  1, 2, 1, 1
1662  };
1663 
1664  HEADER("Picture Parameter Set");
1665 
1666  CHECK(FUNC(nal_unit_header) (ctx, rw,
1667  &current->nal_unit_header, VVC_PPS_NUT));
1668 
1669  ub(6, pps_pic_parameter_set_id);
1670  ub(4, pps_seq_parameter_set_id);
1671  sps = h266->sps[current->pps_seq_parameter_set_id];
1672  if (!sps) {
1673  av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1674  current->pps_seq_parameter_set_id);
1675  return AVERROR_INVALIDDATA;
1676  }
1677 
1678  flag(pps_mixed_nalu_types_in_pic_flag);
1679  ue(pps_pic_width_in_luma_samples,
1680  1, sps->sps_pic_width_max_in_luma_samples);
1681  ue(pps_pic_height_in_luma_samples,
1682  1, sps->sps_pic_height_max_in_luma_samples);
1683 
1684  min_cb_size_y = 1 << (sps->sps_log2_min_luma_coding_block_size_minus2 + 2);
1685  divisor = FFMAX(min_cb_size_y, 8);
1686  if (current->pps_pic_width_in_luma_samples % divisor ||
1687  current->pps_pic_height_in_luma_samples % divisor) {
1688  av_log(ctx->log_ctx, AV_LOG_ERROR,
1689  "Invalid dimensions: %ux%u not divisible "
1690  "by %u, MinCbSizeY = %u.\n",
1691  current->pps_pic_width_in_luma_samples,
1692  current->pps_pic_height_in_luma_samples, divisor, min_cb_size_y);
1693  return AVERROR_INVALIDDATA;
1694  }
1695  if (!sps->sps_res_change_in_clvs_allowed_flag &&
1696  (current->pps_pic_width_in_luma_samples !=
1697  sps->sps_pic_width_max_in_luma_samples ||
1698  current->pps_pic_height_in_luma_samples !=
1699  sps->sps_pic_height_max_in_luma_samples)) {
1700  av_log(ctx->log_ctx, AV_LOG_ERROR,
1701  "Resoltuion change is not allowed, "
1702  "in max resolution (%ux%u) mismatched with pps(%ux%u).\n",
1703  sps->sps_pic_width_max_in_luma_samples,
1704  sps->sps_pic_height_max_in_luma_samples,
1705  current->pps_pic_width_in_luma_samples,
1706  current->pps_pic_height_in_luma_samples);
1707  return AVERROR_INVALIDDATA;
1708  }
1709 
1710  ctb_size_y = 1 << (sps->sps_log2_ctu_size_minus5 + 5);
1711  if (sps->sps_ref_wraparound_enabled_flag) {
1712  if ((ctb_size_y / min_cb_size_y + 1) >
1713  (current->pps_pic_width_in_luma_samples / min_cb_size_y - 1)) {
1714  av_log(ctx->log_ctx, AV_LOG_ERROR,
1715  "Invalid width(%u), ctb_size_y = %u, min_cb_size_y = %u.\n",
1716  current->pps_pic_width_in_luma_samples,
1717  ctb_size_y, min_cb_size_y);
1718  return AVERROR_INVALIDDATA;
1719  }
1720  }
1721 
1722  flag(pps_conformance_window_flag);
1723  if (current->pps_pic_width_in_luma_samples ==
1724  sps->sps_pic_width_max_in_luma_samples &&
1725  current->pps_pic_height_in_luma_samples ==
1726  sps->sps_pic_height_max_in_luma_samples &&
1727  current->pps_conformance_window_flag) {
1728  av_log(ctx->log_ctx, AV_LOG_ERROR,
1729  "Conformance window flag should not true.\n");
1730  return AVERROR_INVALIDDATA;
1731  }
1732 
1733  sub_width_c = h266_sub_width_c[sps->sps_chroma_format_idc];
1734  sub_height_c = h266_sub_height_c[sps->sps_chroma_format_idc];
1735  if (current->pps_conformance_window_flag) {
1736  ue(pps_conf_win_left_offset, 0, current->pps_pic_width_in_luma_samples);
1737  ue(pps_conf_win_right_offset,
1738  0, current->pps_pic_width_in_luma_samples);
1739  ue(pps_conf_win_top_offset, 0, current->pps_pic_height_in_luma_samples);
1740  ue(pps_conf_win_bottom_offset,
1741  0, current->pps_pic_height_in_luma_samples);
1742  if (sub_width_c *
1743  (current->pps_conf_win_left_offset +
1744  current->pps_conf_win_right_offset) >=
1745  current->pps_pic_width_in_luma_samples ||
1746  sub_height_c *
1747  (current->pps_conf_win_top_offset +
1748  current->pps_conf_win_bottom_offset) >=
1749  current->pps_pic_height_in_luma_samples) {
1750  av_log(ctx->log_ctx, AV_LOG_ERROR,
1751  "Invalid pps conformance window: (%u, %u, %u, %u), "
1752  "resolution is %ux%u, sub wxh is %ux%u.\n",
1753  current->pps_conf_win_left_offset,
1754  current->pps_conf_win_right_offset,
1755  current->pps_conf_win_top_offset,
1756  current->pps_conf_win_bottom_offset,
1757  current->pps_pic_width_in_luma_samples,
1758  current->pps_pic_height_in_luma_samples,
1759  sub_width_c, sub_height_c);
1760  return AVERROR_INVALIDDATA;
1761  }
1762  } else {
1763  if (current->pps_pic_width_in_luma_samples ==
1764  sps->sps_pic_width_max_in_luma_samples &&
1765  current->pps_pic_height_in_luma_samples ==
1766  sps->sps_pic_height_max_in_luma_samples) {
1767  infer(pps_conf_win_left_offset, sps->sps_conf_win_left_offset);
1768  infer(pps_conf_win_right_offset, sps->sps_conf_win_right_offset);
1769  infer(pps_conf_win_top_offset, sps->sps_conf_win_top_offset);
1770  infer(pps_conf_win_bottom_offset, sps->sps_conf_win_bottom_offset);
1771  } else {
1772  infer(pps_conf_win_left_offset, 0);
1773  infer(pps_conf_win_right_offset, 0);
1774  infer(pps_conf_win_top_offset, 0);
1775  infer(pps_conf_win_bottom_offset, 0);
1776  }
1777 
1778  }
1779 
1780  flag(pps_scaling_window_explicit_signalling_flag);
1781  if (!sps->sps_ref_pic_resampling_enabled_flag &&
1782  current->pps_scaling_window_explicit_signalling_flag) {
1783  av_log(ctx->log_ctx, AV_LOG_ERROR,
1784  "Invalid data: sps_ref_pic_resampling_enabled_flag is false, "
1785  "but pps_scaling_window_explicit_signalling_flag is true.\n");
1786  return AVERROR_INVALIDDATA;
1787  }
1788  if (current->pps_scaling_window_explicit_signalling_flag) {
1789  se(pps_scaling_win_left_offset,
1790  -current->pps_pic_width_in_luma_samples * 15 / sub_width_c,
1791  current->pps_pic_width_in_luma_samples / sub_width_c);
1792  se(pps_scaling_win_right_offset,
1793  -current->pps_pic_width_in_luma_samples * 15 / sub_width_c,
1794  current->pps_pic_width_in_luma_samples / sub_width_c);
1795  se(pps_scaling_win_top_offset,
1796  -current->pps_pic_height_in_luma_samples * 15 / sub_height_c,
1797  current->pps_pic_height_in_luma_samples / sub_height_c);
1798  se(pps_scaling_win_bottom_offset,
1799  -current->pps_pic_height_in_luma_samples * 15 / sub_height_c,
1800  current->pps_pic_height_in_luma_samples / sub_height_c);
1801  } else {
1802  infer(pps_scaling_win_left_offset, current->pps_conf_win_left_offset);
1803  infer(pps_scaling_win_right_offset, current->pps_conf_win_right_offset);
1804  infer(pps_scaling_win_top_offset, current->pps_conf_win_top_offset);
1805  infer(pps_scaling_win_bottom_offset, current->pps_conf_win_bottom_offset);
1806  }
1807 
1808  flag(pps_output_flag_present_flag);
1809  flag(pps_no_pic_partition_flag);
1810  flag(pps_subpic_id_mapping_present_flag);
1811 
1812  if (current->pps_subpic_id_mapping_present_flag) {
1813  if (!current->pps_no_pic_partition_flag) {
1814  ue(pps_num_subpics_minus1,
1815  sps->sps_num_subpics_minus1, sps->sps_num_subpics_minus1);
1816  } else {
1817  infer(pps_num_subpics_minus1, 0);
1818  }
1819  ue(pps_subpic_id_len_minus1, sps->sps_subpic_id_len_minus1,
1820  sps->sps_subpic_id_len_minus1);
1821  for (i = 0; i <= current->pps_num_subpics_minus1; i++) {
1822  ubs(sps->sps_subpic_id_len_minus1 + 1, pps_subpic_id[i], 1, i);
1823  }
1824  }
1825 
1826  for (i = 0; i <= sps->sps_num_subpics_minus1; i++) {
1827  if (sps->sps_subpic_id_mapping_explicitly_signalled_flag)
1828  current->sub_pic_id_val[i] = current->pps_subpic_id_mapping_present_flag
1829  ? current->pps_subpic_id[i]
1830  : sps->sps_subpic_id[i];
1831  else
1832  current->sub_pic_id_val[i] = i;
1833  }
1834 
1835  pic_width_in_ctbs_y = AV_CEIL_RSHIFT
1836  (current->pps_pic_width_in_luma_samples, (sps->sps_log2_ctu_size_minus5 + 5));
1837  pic_height_in_ctbs_y = AV_CEIL_RSHIFT(
1838  current->pps_pic_height_in_luma_samples,(sps->sps_log2_ctu_size_minus5 + 5));
1839  if (!current->pps_no_pic_partition_flag) {
1840  unsigned int exp_tile_width = 0, exp_tile_height = 0;
1841  unsigned int unified_size, remaining_size;
1842 
1843  u(2, pps_log2_ctu_size_minus5,
1844  sps->sps_log2_ctu_size_minus5, sps->sps_log2_ctu_size_minus5);
1845  ue(pps_num_exp_tile_columns_minus1,
1846  0, FFMIN(pic_width_in_ctbs_y - 1, VVC_MAX_TILE_COLUMNS - 1));
1847  ue(pps_num_exp_tile_rows_minus1,
1848  0, FFMIN(pic_height_in_ctbs_y - 1, VVC_MAX_TILE_ROWS - 1));
1849 
1850  for (i = 0; i <= current->pps_num_exp_tile_columns_minus1; i++) {
1851  ues(pps_tile_column_width_minus1[i],
1852  0, pic_width_in_ctbs_y - exp_tile_width - 1, 1, i);
1853  exp_tile_width += current->pps_tile_column_width_minus1[i] + 1;
1854  }
1855  for (i = 0; i <= current->pps_num_exp_tile_rows_minus1; i++) {
1856  ues(pps_tile_row_height_minus1[i],
1857  0, pic_height_in_ctbs_y - exp_tile_height - 1, 1, i);
1858  exp_tile_height += current->pps_tile_row_height_minus1[i] + 1;
1859  }
1860 
1861  remaining_size = pic_width_in_ctbs_y;
1862  for (i = 0; i <= current->pps_num_exp_tile_columns_minus1; i++) {
1863  if (current->pps_tile_column_width_minus1[i] >= remaining_size) {
1864  av_log(ctx->log_ctx, AV_LOG_ERROR,
1865  "Tile column width(%d) exceeds picture width\n",i);
1866  return AVERROR_INVALIDDATA;
1867  }
1868  current->col_width_val[i] = current->pps_tile_column_width_minus1[i] + 1;
1869  remaining_size -= (current->pps_tile_column_width_minus1[i] + 1);
1870  }
1871  unified_size = current->pps_tile_column_width_minus1[i - 1] + 1;
1872  while (remaining_size > 0) {
1873  if (current->num_tile_columns > VVC_MAX_TILE_COLUMNS) {
1874  av_log(ctx->log_ctx, AV_LOG_ERROR,
1875  "NumTileColumns(%d) > than VVC_MAX_TILE_COLUMNS(%d)\n",
1876  current->num_tile_columns, VVC_MAX_TILE_COLUMNS);
1877  return AVERROR_INVALIDDATA;
1878  }
1879  unified_size = FFMIN(remaining_size, unified_size);
1880  current->col_width_val[i] = unified_size;
1881  remaining_size -= unified_size;
1882  i++;
1883  }
1884  current->num_tile_columns = i;
1885  if (current->num_tile_columns > VVC_MAX_TILE_COLUMNS) {
1886  av_log(ctx->log_ctx, AV_LOG_ERROR,
1887  "NumTileColumns(%d) > than VVC_MAX_TILE_COLUMNS(%d)\n",
1888  current->num_tile_columns, VVC_MAX_TILE_COLUMNS);
1889  return AVERROR_INVALIDDATA;
1890  }
1891 
1892  remaining_size = pic_height_in_ctbs_y;
1893  for (i = 0; i <= current->pps_num_exp_tile_rows_minus1; i++) {
1894  if (current->pps_tile_row_height_minus1[i] >= remaining_size) {
1895  av_log(ctx->log_ctx, AV_LOG_ERROR,
1896  "Tile row height(%d) exceeds picture height\n",i);
1897  return AVERROR_INVALIDDATA;
1898  }
1899  current->row_height_val[i] = current->pps_tile_row_height_minus1[i] + 1;
1900  remaining_size -= (current->pps_tile_row_height_minus1[i] + 1);
1901  }
1902  unified_size = current->pps_tile_row_height_minus1[i - 1] + 1;
1903 
1904  while (remaining_size > 0) {
1905  unified_size = FFMIN(remaining_size, unified_size);
1906  current->row_height_val[i] = unified_size;
1907  remaining_size -= unified_size;
1908  i++;
1909  }
1910  current->num_tile_rows=i;
1911  if (current->num_tile_rows > VVC_MAX_TILE_ROWS) {
1912  av_log(ctx->log_ctx, AV_LOG_ERROR,
1913  "NumTileRows(%d) > than VVC_MAX_TILE_ROWS(%d)\n",
1914  current->num_tile_rows, VVC_MAX_TILE_ROWS);
1915  return AVERROR_INVALIDDATA;
1916  }
1917 
1918  current->num_tiles_in_pic = current->num_tile_columns *
1919  current->num_tile_rows;
1920  if (current->num_tiles_in_pic > VVC_MAX_TILES_PER_AU) {
1921  av_log(ctx->log_ctx, AV_LOG_ERROR,
1922  "NumTilesInPic(%d) > than VVC_MAX_TILES_PER_AU(%d)\n",
1923  current->num_tiles_in_pic, VVC_MAX_TILES_PER_AU);
1924  return AVERROR_INVALIDDATA;
1925  }
1926 
1927  if (current->num_tiles_in_pic > 1) {
1928  flag(pps_loop_filter_across_tiles_enabled_flag);
1929  flag(pps_rect_slice_flag);
1930  } else {
1931  infer(pps_loop_filter_across_tiles_enabled_flag, 0);
1932  infer(pps_rect_slice_flag, 1);
1933  }
1934  if (current->pps_rect_slice_flag)
1935  flag(pps_single_slice_per_subpic_flag);
1936  else
1937  infer(pps_single_slice_per_subpic_flag, 1);
1938  if (current->pps_rect_slice_flag &&
1939  !current->pps_single_slice_per_subpic_flag) {
1940  int j;
1941  uint16_t tile_idx = 0, tile_x, tile_y, ctu_x, ctu_y;
1942  uint16_t slice_top_left_ctu_x[VVC_MAX_SLICES];
1943  uint16_t slice_top_left_ctu_y[VVC_MAX_SLICES];
1944  ue(pps_num_slices_in_pic_minus1, 0, VVC_MAX_SLICES - 1);
1945  if (current->pps_num_slices_in_pic_minus1 > 1)
1946  flag(pps_tile_idx_delta_present_flag);
1947  else
1948  infer(pps_tile_idx_delta_present_flag, 0);
1949  for (i = 0; i < current->pps_num_slices_in_pic_minus1; i++) {
1950  tile_x = tile_idx % current->num_tile_columns;
1951  tile_y = tile_idx / current->num_tile_columns;
1952  if (tile_x != current->num_tile_columns - 1) {
1953  ues(pps_slice_width_in_tiles_minus1[i],
1954  0, current->num_tile_columns - 1, 1, i);
1955  } else {
1956  infer(pps_slice_width_in_tiles_minus1[i], 0);
1957  }
1958  if (tile_y != current->num_tile_rows - 1 &&
1959  (current->pps_tile_idx_delta_present_flag || tile_x == 0)) {
1960  ues(pps_slice_height_in_tiles_minus1[i],
1961  0, current->num_tile_rows - 1, 1, i);
1962  } else {
1963  if (tile_y == current->num_tile_rows - 1)
1964  infer(pps_slice_height_in_tiles_minus1[i], 0);
1965  else
1966  infer(pps_slice_height_in_tiles_minus1[i],
1967  current->pps_slice_height_in_tiles_minus1[i - 1]);
1968  }
1969 
1970  ctu_x = ctu_y = 0;
1971  for (j = 0; j < tile_x; j++) {
1972  ctu_x += current->col_width_val[j];
1973  }
1974  for (j = 0; j < tile_y; j++) {
1975  ctu_y += current->row_height_val[j];
1976  }
1977  if (current->pps_slice_width_in_tiles_minus1[i] == 0 &&
1978  current->pps_slice_height_in_tiles_minus1[i] == 0 &&
1979  current->row_height_val[tile_y] > 1) {
1980  int num_slices_in_tile,
1981  uniform_slice_height, remaining_height_in_ctbs_y;
1982  remaining_height_in_ctbs_y =
1983  current->row_height_val[tile_y];
1984  ues(pps_num_exp_slices_in_tile[i],
1985  0, current->row_height_val[tile_y] - 1, 1, i);
1986  if (current->pps_num_exp_slices_in_tile[i] == 0) {
1987  num_slices_in_tile = 1;
1988  current->slice_height_in_ctus[i] = current->row_height_val[tile_y];
1989  slice_top_left_ctu_x[i] = ctu_x;
1990  slice_top_left_ctu_y[i] = ctu_y;
1991  } else {
1992  uint16_t slice_height_in_ctus;
1993  for (j = 0; j < current->pps_num_exp_slices_in_tile[i];
1994  j++) {
1995  ues(pps_exp_slice_height_in_ctus_minus1[i][j], 0,
1996  current->row_height_val[tile_y] - 1, 2,
1997  i, j);
1998  slice_height_in_ctus =
1999  current->
2000  pps_exp_slice_height_in_ctus_minus1[i][j] + 1;
2001 
2002  current->slice_height_in_ctus[i + j] =
2003  slice_height_in_ctus;
2004  slice_top_left_ctu_x[i + j] = ctu_x;
2005  slice_top_left_ctu_y[i + j] = ctu_y;
2006  ctu_y += slice_height_in_ctus;
2007 
2008  remaining_height_in_ctbs_y -= slice_height_in_ctus;
2009  }
2010  uniform_slice_height = 1 +
2011  (j == 0 ? current->row_height_val[tile_y] - 1:
2012  current->pps_exp_slice_height_in_ctus_minus1[i][j-1]);
2013  while (remaining_height_in_ctbs_y > uniform_slice_height) {
2014  current->slice_height_in_ctus[i + j] =
2015  uniform_slice_height;
2016  slice_top_left_ctu_x[i + j] = ctu_x;
2017  slice_top_left_ctu_y[i + j] = ctu_y;
2018  ctu_y += uniform_slice_height;
2019 
2020  remaining_height_in_ctbs_y -= uniform_slice_height;
2021  j++;
2022  }
2023  if (remaining_height_in_ctbs_y > 0) {
2024  current->slice_height_in_ctus[i + j] =
2025  remaining_height_in_ctbs_y;
2026  slice_top_left_ctu_x[i + j] = ctu_x;
2027  slice_top_left_ctu_y[i + j] = ctu_y;
2028  j++;
2029  }
2030  num_slices_in_tile = j;
2031  }
2032  i += num_slices_in_tile - 1;
2033  } else {
2034  uint16_t height = 0;
2035  infer(pps_num_exp_slices_in_tile[i], 0);
2036  for (j = 0;
2037  j <= current->pps_slice_height_in_tiles_minus1[i];
2038  j++) {
2039  height +=
2040  current->row_height_val[tile_y + j];
2041  }
2042  current->slice_height_in_ctus[i] = height;
2043 
2044  slice_top_left_ctu_x[i] = ctu_x;
2045  slice_top_left_ctu_y[i] = ctu_y;
2046  }
2047  if (i < current->pps_num_slices_in_pic_minus1) {
2048  if (current->pps_tile_idx_delta_present_flag) {
2049  // Two conditions must be met:
2050  // 1. −NumTilesInPic + 1 <= pps_tile_idx_delta_val[i] <= NumTilesInPic − 1
2051  // 2. 0 <= tile_idx + pps_tile_idx_delta_val[i] <= NumTilesInPic − 1
2052  // Combining these conditions yields: -tile_idx <= pps_tile_idx_delta_val[i] <= NumTilesInPic - 1 - tile_idx
2053  ses(pps_tile_idx_delta_val[i],
2054  -tile_idx, current->num_tiles_in_pic - 1 - tile_idx, 1, i);
2055  if (current->pps_tile_idx_delta_val[i] == 0) {
2056  av_log(ctx->log_ctx, AV_LOG_ERROR,
2057  "pps_tile_idx_delta_val[i] shall not be equal to 0.\n");
2058  }
2059  tile_idx += current->pps_tile_idx_delta_val[i];
2060  } else {
2061  infer(pps_tile_idx_delta_val[i], 0);
2062  tile_idx +=
2063  current->pps_slice_width_in_tiles_minus1[i] + 1;
2064  if (tile_idx % current->num_tile_columns == 0) {
2065  tile_idx +=
2066  current->pps_slice_height_in_tiles_minus1[i] *
2067  current->num_tile_columns;
2068  }
2069  }
2070  }
2071  }
2072  if (i == current->pps_num_slices_in_pic_minus1) {
2073  uint16_t height = 0;
2074 
2075  tile_x = tile_idx % current->num_tile_columns;
2076  tile_y = tile_idx / current->num_tile_columns;
2077  if (tile_y >= current->num_tile_rows)
2078  return AVERROR_INVALIDDATA;
2079 
2080  ctu_x = 0, ctu_y = 0;
2081  for (j = 0; j < tile_x; j++) {
2082  ctu_x += current->col_width_val[j];
2083  }
2084  for (j = 0; j < tile_y; j++) {
2085  ctu_y += current->row_height_val[j];
2086  }
2087  slice_top_left_ctu_x[i] = ctu_x;
2088  slice_top_left_ctu_y[i] = ctu_y;
2089 
2090  current->pps_slice_width_in_tiles_minus1[i] =
2091  current->num_tile_columns - tile_x - 1;
2092  current->pps_slice_height_in_tiles_minus1[i] =
2093  current->num_tile_rows - tile_y - 1;
2094 
2095  for (j = 0; j <= current->pps_slice_height_in_tiles_minus1[i];
2096  j++) {
2097  height +=
2098  current->row_height_val[tile_y + j];
2099  }
2100  current->slice_height_in_ctus[i] = height;
2101 
2102  infer(pps_num_exp_slices_in_tile[i], 0);
2103  }
2104  //now, we got all slice information, let's resolve NumSlicesInSubpic
2105  for (i = 0; i <= sps->sps_num_subpics_minus1; i++) {
2106  current->num_slices_in_subpic[i] = 0;
2107  for (j = 0; j <= current->pps_num_slices_in_pic_minus1; j++) {
2108  uint16_t pos_x = 0, pos_y = 0;
2109  pos_x = slice_top_left_ctu_x[j];
2110  pos_y = slice_top_left_ctu_y[j];
2111  if ((pos_x >= sps->sps_subpic_ctu_top_left_x[i]) &&
2112  (pos_x <
2113  sps->sps_subpic_ctu_top_left_x[i] +
2114  sps->sps_subpic_width_minus1[i] + 1) &&
2115  (pos_y >= sps->sps_subpic_ctu_top_left_y[i]) &&
2116  (pos_y < sps->sps_subpic_ctu_top_left_y[i] +
2117  sps->sps_subpic_height_minus1[i] + 1)) {
2118  current->num_slices_in_subpic[i]++;
2119  }
2120  }
2121  }
2122  } else {
2123  if (current->pps_no_pic_partition_flag)
2124  infer(pps_num_slices_in_pic_minus1, 0);
2125  else if (current->pps_single_slice_per_subpic_flag)
2126  infer(pps_num_slices_in_pic_minus1,
2127  sps->sps_num_subpics_minus1);
2128  // else?
2129  }
2130  if (!current->pps_rect_slice_flag ||
2131  current->pps_single_slice_per_subpic_flag ||
2132  current->pps_num_slices_in_pic_minus1 > 0)
2133  flag(pps_loop_filter_across_slices_enabled_flag);
2134  else
2135  infer(pps_loop_filter_across_slices_enabled_flag, 0);
2136  } else {
2137  infer(pps_num_exp_tile_columns_minus1, 0);
2138  infer(pps_tile_column_width_minus1[0], pic_width_in_ctbs_y - 1);
2139  infer(pps_num_exp_tile_rows_minus1, 0);
2140  infer(pps_tile_row_height_minus1[0], pic_height_in_ctbs_y - 1);
2141  current->col_width_val[0] = pic_width_in_ctbs_y;
2142  current->row_height_val[0] = pic_height_in_ctbs_y;
2143  current->num_tile_columns = 1;
2144  current->num_tile_rows = 1;
2145  current->num_tiles_in_pic = 1;
2146  }
2147 
2148  flag(pps_cabac_init_present_flag);
2149  for (i = 0; i < 2; i++)
2150  ues(pps_num_ref_idx_default_active_minus1[i], 0, 14, 1, i);
2151  flag(pps_rpl1_idx_present_flag);
2152  flag(pps_weighted_pred_flag);
2153  flag(pps_weighted_bipred_flag);
2154  flag(pps_ref_wraparound_enabled_flag);
2155  if (current->pps_ref_wraparound_enabled_flag) {
2156  ue(pps_pic_width_minus_wraparound_offset,
2157  0, (current->pps_pic_width_in_luma_samples / min_cb_size_y)
2158  - (ctb_size_y / min_cb_size_y) - 2);
2159  }
2160 
2161  qp_bd_offset = 6 * sps->sps_bitdepth_minus8;
2162  se(pps_init_qp_minus26, -(26 + qp_bd_offset), 37);
2163  flag(pps_cu_qp_delta_enabled_flag);
2164  flag(pps_chroma_tool_offsets_present_flag);
2165  if (current->pps_chroma_tool_offsets_present_flag) {
2166  se(pps_cb_qp_offset, -12, 12);
2167  se(pps_cr_qp_offset, -12, 12);
2168  flag(pps_joint_cbcr_qp_offset_present_flag);
2169  if (current->pps_joint_cbcr_qp_offset_present_flag)
2170  se(pps_joint_cbcr_qp_offset_value, -12, 12);
2171  else
2172  infer(pps_joint_cbcr_qp_offset_value, 0);
2173  flag(pps_slice_chroma_qp_offsets_present_flag);
2174  flag(pps_cu_chroma_qp_offset_list_enabled_flag);
2175  if (current->pps_cu_chroma_qp_offset_list_enabled_flag) {
2176  ue(pps_chroma_qp_offset_list_len_minus1, 0, 5);
2177  for (i = 0; i <= current->pps_chroma_qp_offset_list_len_minus1; i++) {
2178  ses(pps_cb_qp_offset_list[i], -12, 12, 1, i);
2179  ses(pps_cr_qp_offset_list[i], -12, 12, 1, i);
2180  if (current->pps_joint_cbcr_qp_offset_present_flag)
2181  ses(pps_joint_cbcr_qp_offset_list[i], -12, 12, 1, i);
2182  else
2183  infer(pps_joint_cbcr_qp_offset_list[i], 0);
2184  }
2185  }
2186  } else {
2187  infer(pps_cb_qp_offset, 0);
2188  infer(pps_cr_qp_offset, 0);
2189  infer(pps_joint_cbcr_qp_offset_present_flag, 0);
2190  infer(pps_joint_cbcr_qp_offset_value, 0);
2191  infer(pps_slice_chroma_qp_offsets_present_flag, 0);
2192  infer(pps_cu_chroma_qp_offset_list_enabled_flag, 0);
2193  }
2194  flag(pps_deblocking_filter_control_present_flag);
2195  if (current->pps_deblocking_filter_control_present_flag) {
2196  flag(pps_deblocking_filter_override_enabled_flag);
2197  flag(pps_deblocking_filter_disabled_flag);
2198  if (!current->pps_no_pic_partition_flag &&
2199  current->pps_deblocking_filter_override_enabled_flag)
2200  flag(pps_dbf_info_in_ph_flag);
2201  else
2202  infer(pps_dbf_info_in_ph_flag, 0);
2203  if (!current->pps_deblocking_filter_disabled_flag) {
2204  se(pps_luma_beta_offset_div2, -12, 12);
2205  se(pps_luma_tc_offset_div2, -12, 12);
2206  if (current->pps_chroma_tool_offsets_present_flag) {
2207  se(pps_cb_beta_offset_div2, -12, 12);
2208  se(pps_cb_tc_offset_div2, -12, 12);
2209  se(pps_cr_beta_offset_div2, -12, 12);
2210  se(pps_cr_tc_offset_div2, -12, 12);
2211  } else {
2212  infer(pps_cb_beta_offset_div2,
2213  current->pps_luma_beta_offset_div2);
2214  infer(pps_cb_tc_offset_div2, current->pps_luma_tc_offset_div2);
2215  infer(pps_cr_beta_offset_div2,
2216  current->pps_luma_beta_offset_div2);
2217  infer(pps_cr_tc_offset_div2, current->pps_luma_tc_offset_div2);
2218  }
2219  } else {
2220  infer(pps_luma_beta_offset_div2, 0);
2221  infer(pps_luma_tc_offset_div2, 0);
2222  infer(pps_cb_beta_offset_div2, 0);
2223  infer(pps_cb_tc_offset_div2, 0);
2224  infer(pps_cr_beta_offset_div2, 0);
2225  infer(pps_cr_tc_offset_div2, 0);
2226  }
2227  } else {
2228  infer(pps_deblocking_filter_override_enabled_flag, 0);
2229  infer(pps_deblocking_filter_disabled_flag, 0);
2230  infer(pps_dbf_info_in_ph_flag, 0);
2231  infer(pps_luma_beta_offset_div2, 0);
2232  infer(pps_luma_tc_offset_div2, 0);
2233  infer(pps_cb_beta_offset_div2, 0);
2234  infer(pps_cb_tc_offset_div2, 0);
2235  infer(pps_cr_beta_offset_div2, 0);
2236  infer(pps_cr_tc_offset_div2, 0);
2237  }
2238 
2239  if (!current->pps_no_pic_partition_flag) {
2240  flag(pps_rpl_info_in_ph_flag);
2241  flag(pps_sao_info_in_ph_flag);
2242  flag(pps_alf_info_in_ph_flag);
2243  if ((current->pps_weighted_pred_flag ||
2244  current->pps_weighted_bipred_flag) &&
2245  current->pps_rpl_info_in_ph_flag)
2246  flag(pps_wp_info_in_ph_flag);
2247  flag(pps_qp_delta_info_in_ph_flag);
2248  }
2249  flag(pps_picture_header_extension_present_flag);
2250  flag(pps_slice_header_extension_present_flag);
2251 
2252  flag(pps_extension_flag);
2253  if (current->pps_extension_flag)
2254  CHECK(FUNC(extension_data) (ctx, rw, &current->extension_data));
2255 
2256  CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
2257  return 0;
2258 }
2259 
2261  H266RawAPS *current)
2262 {
2263  int err, j, k;
2264 
2265  flag(alf_luma_filter_signal_flag);
2266 
2267  if (current->aps_chroma_present_flag) {
2268  flag(alf_chroma_filter_signal_flag);
2269  flag(alf_cc_cb_filter_signal_flag);
2270  flag(alf_cc_cr_filter_signal_flag);
2271  } else {
2272  infer(alf_chroma_filter_signal_flag, 0);
2273  infer(alf_cc_cb_filter_signal_flag, 0);
2274  infer(alf_cc_cr_filter_signal_flag, 0);
2275  }
2276 
2277  if (current->alf_luma_filter_signal_flag) {
2278  flag(alf_luma_clip_flag);
2279  ue(alf_luma_num_filters_signalled_minus1, 0, VVC_NUM_ALF_FILTERS - 1);
2280  if (current->alf_luma_num_filters_signalled_minus1 > 0) {
2281  unsigned int bits = av_ceil_log2(current->alf_luma_num_filters_signalled_minus1 + 1);
2282  for (int filt_idx = 0; filt_idx < VVC_NUM_ALF_FILTERS; filt_idx++)
2283  us(bits, alf_luma_coeff_delta_idx[filt_idx],
2284  0, current->alf_luma_num_filters_signalled_minus1,
2285  1, filt_idx);
2286  }
2287  for (int sf_idx = 0; sf_idx <= current->alf_luma_num_filters_signalled_minus1; sf_idx++)
2288  for (j = 0; j < 12; j++) {
2289  ues(alf_luma_coeff_abs[sf_idx][j], 0, 128, 2, sf_idx, j);
2290  if (current->alf_luma_coeff_abs[sf_idx][j])
2291  ubs(1, alf_luma_coeff_sign[sf_idx][j], 2, sf_idx, j);
2292  else
2293  infer(alf_luma_coeff_sign[sf_idx][j], 0);
2294  }
2295  } else {
2296  infer(alf_luma_clip_flag, 0);
2297  infer(alf_luma_num_filters_signalled_minus1, 0);
2298  }
2299  for (int sf_idx = 0; sf_idx <= current->alf_luma_num_filters_signalled_minus1; sf_idx++) {
2300  for (j = 0; j < 12; j++) {
2301  if (current->alf_luma_clip_flag)
2302  ubs(2, alf_luma_clip_idx[sf_idx][j], 2, sf_idx, j);
2303  else
2304  infer(alf_luma_clip_idx[sf_idx][j], 0);
2305  }
2306  }
2307 
2308  if (current->alf_chroma_filter_signal_flag) {
2309  flag(alf_chroma_clip_flag);
2310  ue(alf_chroma_num_alt_filters_minus1, 0, 7);
2311  } else {
2312  infer(alf_chroma_clip_flag, 0);
2313  infer(alf_chroma_num_alt_filters_minus1, 0);
2314  }
2315  for (int alt_idx = 0; alt_idx <= current->alf_chroma_num_alt_filters_minus1; alt_idx++) {
2316  for (j = 0; j < 6; j++) {
2317  if (current->alf_chroma_filter_signal_flag)
2318  ues(alf_chroma_coeff_abs[alt_idx][j], 0, 128, 2, alt_idx, j);
2319  else
2320  infer(alf_chroma_coeff_abs[alt_idx][j], 0);
2321  if (current->alf_chroma_coeff_abs[alt_idx][j] > 0)
2322  ubs(1, alf_chroma_coeff_sign[alt_idx][j], 2, alt_idx, j);
2323  else
2324  infer(alf_chroma_coeff_sign[alt_idx][j], 0);
2325  }
2326  for (j = 0; j < 6; j++) {
2327  if (current->alf_chroma_clip_flag)
2328  ubs(2, alf_chroma_clip_idx[alt_idx][j], 2, alt_idx, j);
2329  else
2330  infer(alf_chroma_clip_idx[alt_idx][j], 0);
2331  }
2332  }
2333 
2334  if (current->alf_cc_cb_filter_signal_flag)
2335  ue(alf_cc_cb_filters_signalled_minus1, 0, 3);
2336  else
2337  infer(alf_cc_cb_filters_signalled_minus1, 0);
2338  for (k = 0; k <= current->alf_cc_cb_filters_signalled_minus1; k++) {
2339  for (j = 0; j < 7; j++) {
2340  if (current->alf_cc_cb_filter_signal_flag)
2341  ubs(3, alf_cc_cb_mapped_coeff_abs[k][j], 2, k, j);
2342  else
2343  infer(alf_cc_cb_mapped_coeff_abs[k][j], 0);
2344  if (current->alf_cc_cb_mapped_coeff_abs[k][j])
2345  ubs(1, alf_cc_cb_coeff_sign[k][j], 2, k, j);
2346  else
2347  infer(alf_cc_cb_coeff_sign[k][j], 0);
2348  }
2349  }
2350 
2351  if (current->alf_cc_cr_filter_signal_flag)
2352  ue(alf_cc_cr_filters_signalled_minus1, 0, 3);
2353  else
2354  infer(alf_cc_cr_filters_signalled_minus1, 0);
2355  for (k = 0; k < current->alf_cc_cr_filters_signalled_minus1 + 1; k++) {
2356  for (j = 0; j < 7; j++) {
2357  if (current->alf_cc_cr_filter_signal_flag)
2358  ubs(3, alf_cc_cr_mapped_coeff_abs[k][j], 2, k, j);
2359  else
2360  infer(alf_cc_cr_mapped_coeff_abs[k][j], 0);
2361  if (current->alf_cc_cr_mapped_coeff_abs[k][j])
2362  ubs(1, alf_cc_cr_coeff_sign[k][j], 2, k, j);
2363  else
2364  infer(alf_cc_cr_coeff_sign[k][j], 0);
2365  }
2366  }
2367 
2368  return 0;
2369 }
2370 
2372  H266RawAPS *current)
2373 {
2374  int err, i, lmcs_max_bin_idx;
2375 
2376  ue(lmcs_min_bin_idx, 0, 15);
2377  ue(lmcs_delta_max_bin_idx, 0, 15);
2378  ue(lmcs_delta_cw_prec_minus1, 0, 14);
2379 
2380  lmcs_max_bin_idx = 15 - current->lmcs_delta_max_bin_idx;
2381 
2382  if (lmcs_max_bin_idx < current->lmcs_min_bin_idx)
2383  return AVERROR_INVALIDDATA;
2384 
2385  for (i = current->lmcs_min_bin_idx; i <= lmcs_max_bin_idx; i++) {
2386  ubs(current->lmcs_delta_cw_prec_minus1 + 1, lmcs_delta_abs_cw[i], 1, i);
2387  if (current->lmcs_delta_abs_cw[i] > 0)
2388  flags(lmcs_delta_sign_cw_flag[i], 1, i);
2389  else
2390  infer(lmcs_delta_sign_cw_flag[i], 0);
2391  }
2392 
2393  if (current->aps_chroma_present_flag) {
2394  ub(3, lmcs_delta_abs_crs);
2395  if (current->lmcs_delta_abs_crs > 0)
2396  flag(lmcs_delta_sign_crs_flag);
2397  else
2398  infer(lmcs_delta_sign_crs_flag, 0);
2399  } else {
2400  infer(lmcs_delta_abs_crs, 0);
2401  infer(lmcs_delta_sign_crs_flag, 0);
2402  }
2403 
2404  return 0;
2405 }
2406 
2408  H266RawAPS *current)
2409 {
2410  // 7.4.3.4, deriving DiagScanOrder
2411  static const uint8_t diag_scan_order[64][2] = {
2412  { 0, 0, }, { 0, 1, }, { 1, 0, }, { 0, 2, }, { 1, 1, }, { 2, 0, }, { 0, 3, }, { 1, 2, },
2413  { 2, 1, }, { 3, 0, }, { 0, 4, }, { 1, 3, }, { 2, 2, }, { 3, 1, }, { 4, 0, }, { 0, 5, },
2414  { 1, 4, }, { 2, 3, }, { 3, 2, }, { 4, 1, }, { 5, 0, }, { 0, 6, }, { 1, 5, }, { 2, 4, },
2415  { 3, 3, }, { 4, 2, }, { 5, 1, }, { 6, 0, }, { 0, 7, }, { 1, 6, }, { 2, 5, }, { 3, 4, },
2416  { 4, 3, }, { 5, 2, }, { 6, 1, }, { 7, 0, }, { 1, 7, }, { 2, 6, }, { 3, 5, }, { 4, 4, },
2417  { 5, 3, }, { 6, 2, }, { 7, 1, }, { 2, 7, }, { 3, 6, }, { 4, 5, }, { 5, 4, }, { 6, 3, },
2418  { 7, 2, }, { 3, 7, }, { 4, 6, }, { 5, 5, }, { 6, 4, }, { 7, 3, }, { 4, 7, }, { 5, 6, },
2419  { 6, 5, }, { 7, 4, }, { 5, 7, }, { 6, 6, }, { 7, 5, }, { 6, 7, }, { 7, 6, }, { 7, 7, }, };
2420  int err;
2421 
2422  for (int id = 0; id < 28; id ++) {
2423  if (current->aps_chroma_present_flag || id % 3 == 2 || id == 27) {
2424  flags(scaling_list_copy_mode_flag[id], 1, id);
2425  if (!current->scaling_list_copy_mode_flag[id])
2426  flags(scaling_list_pred_mode_flag[id], 1, id);
2427  else
2428  infer(scaling_list_pred_mode_flag[id], 0);
2429  if ((current->scaling_list_copy_mode_flag[id] ||
2430  current->scaling_list_pred_mode_flag[id]) &&
2431  id != 0 && id != 2 && id != 8) {
2432  int max_id_delta = (id < 2) ? id : ((id < 8) ? (id - 2) : (id - 8));
2433  ues(scaling_list_pred_id_delta[id], 0, max_id_delta, 1, id);
2434  }
2435  if (!current->scaling_list_copy_mode_flag[id]) {
2436  int matrix_size = id < 2 ? 2 : (id < 8 ? 4 : 8);
2437  if (id > 13) {
2438  int idx = id - 14;
2439  ses(scaling_list_dc_coef[idx], -128, 127, 1, idx);
2440  }
2441  for (int i = 0; i < matrix_size * matrix_size; i++) {
2442  int x = diag_scan_order[i][0];
2443  int y = diag_scan_order[i][1];
2444  if (!(id > 25 && x >= 4 && y >= 4))
2445  ses(scaling_list_delta_coef[id][i], -128, 127, 2, id, i);
2446  }
2447  } else if (id > 13) {
2448  int idx = id - 14;
2449  infer(scaling_list_dc_coef[idx], 0);
2450  }
2451  } else {
2452  infer(scaling_list_copy_mode_flag[id], 1);
2453  infer(scaling_list_pred_mode_flag[id], 0);
2454  }
2455  }
2456 
2457  return 0;
2458 }
2459 
2461  H266RawAPS *current, int prefix)
2462 {
2463  int aps_id_max = MAX_UINT_BITS(5);
2464  int err;
2465 
2466  if (prefix)
2467  HEADER("Prefix Adaptation parameter set");
2468  else
2469  HEADER("Suffix Adaptation parameter set");
2470 
2471  CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
2472  prefix ? VVC_PREFIX_APS_NUT
2473  : VVC_SUFFIX_APS_NUT));
2474 
2475  ub(3, aps_params_type);
2476  if (current->aps_params_type == VVC_ASP_TYPE_ALF ||
2477  current->aps_params_type == VVC_ASP_TYPE_SCALING)
2478  aps_id_max = 7;
2479  else if (current->aps_params_type == VVC_ASP_TYPE_LMCS)
2480  aps_id_max = 3;
2481  u(5, aps_adaptation_parameter_set_id, 0, aps_id_max);
2482  flag(aps_chroma_present_flag);
2483  if (current->aps_params_type == VVC_ASP_TYPE_ALF)
2484  CHECK(FUNC(alf_data)(ctx, rw, current));
2485  else if(current->aps_params_type == VVC_ASP_TYPE_LMCS)
2486  CHECK(FUNC(lmcs_data)(ctx, rw, current));
2487  else if (current->aps_params_type == VVC_ASP_TYPE_SCALING)
2488  CHECK(FUNC(scaling_list_data)(ctx, rw, current));
2489  flag(aps_extension_flag);
2490  if (current->aps_extension_flag)
2491  CHECK(FUNC(extension_data) (ctx, rw, &current->extension_data));
2492  CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
2493 
2494  return 0;
2495 }
2496 
2498  H266RawAUD *current)
2499 {
2500  int err;
2501 
2502  HEADER("Access Unit Delimiter");
2503 
2504  CHECK(FUNC(nal_unit_header) (ctx, rw,
2505  &current->nal_unit_header, VVC_AUD_NUT));
2506 
2507  flag(aud_irap_or_gdr_flag);
2508  u(3, aud_pic_type, 0, 2);
2509 
2510  CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
2511  return 0;
2512 }
2513 
2515  const H266RawSPS *sps,
2516  const H266RawPPS *pps,
2517  const H266RefPicLists *ref_lists,
2518  uint8_t num_ref_idx_active[2],
2519  H266RawPredWeightTable *current)
2520 {
2521  int err, i, j;
2522  ue(luma_log2_weight_denom, 0, 7);
2523  if (sps->sps_chroma_format_idc != 0) {
2524  se(delta_chroma_log2_weight_denom,
2525  -current->luma_log2_weight_denom,
2526  7 - current->luma_log2_weight_denom);
2527  } else {
2528  infer(delta_chroma_log2_weight_denom, 0);
2529  }
2530  if (pps->pps_wp_info_in_ph_flag) {
2531  ue(num_l0_weights, 0,
2532  FFMIN(15, ref_lists->rpl_ref_list[0].num_ref_entries));
2533  infer(num_weights_l0, current->num_l0_weights);
2534  } else {
2535  infer(num_weights_l0, num_ref_idx_active[0]);
2536  }
2537  for (i = 0; i < current->num_weights_l0; i++) {
2538  flags(luma_weight_l0_flag[i], 1, i);
2539  }
2540  if (sps->sps_chroma_format_idc != 0) {
2541  for (i = 0; i < current->num_weights_l0; i++)
2542  flags(chroma_weight_l0_flag[i], 1, i);
2543  }
2544  for (i = 0; i < current->num_weights_l0; i++) {
2545  if (current->luma_weight_l0_flag[i]) {
2546  ses(delta_luma_weight_l0[i], -128, 127, 1, i);
2547  ses(luma_offset_l0[i], -128, 127, 1, i);
2548  } else {
2549  infer(delta_luma_weight_l0[i], 0);
2550  infer(luma_offset_l0[i], 0);
2551  }
2552  if (current->chroma_weight_l0_flag[i]) {
2553  for (j = 0; j < 2; j++) {
2554  ses(delta_chroma_weight_l0[i][j], -128, 127, 2, i, j);
2555  ses(delta_chroma_offset_l0[i][j], -4 * 128, 4 * 127, 2, i, j);
2556  }
2557  }
2558  }
2559 
2560  if (pps->pps_weighted_bipred_flag &&
2561  ref_lists->rpl_ref_list[1].num_ref_entries > 0) {
2562  if (pps->pps_wp_info_in_ph_flag) {
2563  ue(num_l1_weights, 0,
2564  FFMIN(15, ref_lists->rpl_ref_list[1].num_ref_entries));
2565  infer(num_weights_l1, current->num_l1_weights);
2566  } else {
2567  infer(num_weights_l1, num_ref_idx_active[1]);
2568  }
2569  } else {
2570  infer(num_weights_l1, 0);
2571  }
2572 
2573  for (i = 0; i < current->num_weights_l1; i++)
2574  flags(luma_weight_l1_flag[i], 1, i);
2575  if (sps->sps_chroma_format_idc != 0) {
2576  for (i = 0; i < current->num_weights_l1; i++)
2577  flags(chroma_weight_l1_flag[i], 1, i);
2578  }
2579  for (i = 0; i < current->num_weights_l1; i++) {
2580  if (current->luma_weight_l1_flag[i]) {
2581  ses(delta_luma_weight_l1[i], -128, 127, 1, i);
2582  ses(luma_offset_l1[i], -128, 127, 1, i);
2583  } else {
2584  infer(delta_luma_weight_l1[i], 0);
2585  infer(luma_offset_l1[i], 0);
2586  }
2587  if (current->chroma_weight_l1_flag[i]) {
2588  for (j = 0; j < 2; j++) {
2589  ses(delta_chroma_weight_l1[i][j], -128, 127, 2, i, j);
2590  ses(delta_chroma_offset_l1[i][j], -4 * 128, 4 * 127, 2, i, j);
2591  }
2592  }
2593  }
2594  return 0;
2595 }
2596 
2598  H266RawPictureHeader *current) {
2600  const H266RawVPS *vps;
2601  const H266RawSPS *sps;
2602  const H266RawPPS *pps;
2603  int err, i;
2604  unsigned int ctb_log2_size_y, min_cb_log2_size_y,
2605  min_qt_log2_size_intra_y, min_qt_log2_size_inter_y;
2606  uint8_t qp_bd_offset;
2607 
2608  flag(ph_gdr_or_irap_pic_flag);
2609  flag(ph_non_ref_pic_flag);
2610  if (current->ph_gdr_or_irap_pic_flag)
2611  flag(ph_gdr_pic_flag);
2612  else
2613  infer(ph_gdr_pic_flag, 0);
2614  flag(ph_inter_slice_allowed_flag);
2615  if (current->ph_inter_slice_allowed_flag)
2616  flag(ph_intra_slice_allowed_flag);
2617  else
2618  infer(ph_intra_slice_allowed_flag, 1);
2619  ue(ph_pic_parameter_set_id, 0, VVC_MAX_PPS_COUNT - 1);
2620  pps = h266->pps[current->ph_pic_parameter_set_id];
2621  if (!pps) {
2622  av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
2623  current->ph_pic_parameter_set_id);
2624  return AVERROR_INVALIDDATA;
2625  }
2626  sps = h266->sps[pps->pps_seq_parameter_set_id];
2627  if (!sps) {
2628  av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
2629  pps->pps_seq_parameter_set_id);
2630  return AVERROR_INVALIDDATA;
2631  }
2632  vps = h266->vps[sps->sps_video_parameter_set_id];
2633  if (!vps) {
2634  av_log(ctx->log_ctx, AV_LOG_ERROR, "VPS id %d not available.\n",
2635  sps->sps_video_parameter_set_id);
2636  return AVERROR_INVALIDDATA;
2637  }
2638 
2639  ub(sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4, ph_pic_order_cnt_lsb);
2640  if (current->ph_gdr_pic_flag)
2641  ue(ph_recovery_poc_cnt, 0,
2642  1 << (sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4));
2643 
2644  for (i = 0; i < sps->sps_num_extra_ph_bytes * 8; i++) {
2645  if (sps->sps_extra_ph_bit_present_flag[i])
2646  flags(ph_extra_bit[i], 1, i);
2647  }
2648  if (sps->sps_poc_msb_cycle_flag) {
2649  flag(ph_poc_msb_cycle_present_flag);
2650  if (current->ph_poc_msb_cycle_present_flag)
2651  ub(sps->sps_poc_msb_cycle_len_minus1 + 1, ph_poc_msb_cycle_val);
2652  }
2653  if (sps->sps_alf_enabled_flag && pps->pps_alf_info_in_ph_flag) {
2654  flag(ph_alf_enabled_flag);
2655  if (current->ph_alf_enabled_flag) {
2656 
2657  ub(3, ph_num_alf_aps_ids_luma);
2658  for (i = 0; i < current->ph_num_alf_aps_ids_luma; i++)
2659  ubs(3, ph_alf_aps_id_luma[i], 1, i);
2660 
2661  if (sps->sps_chroma_format_idc != 0) {
2662  flag(ph_alf_cb_enabled_flag);
2663  flag(ph_alf_cr_enabled_flag);
2664  } else {
2665  infer(ph_alf_cb_enabled_flag, 0);
2666  infer(ph_alf_cr_enabled_flag, 0);
2667  }
2668 
2669  if (current->ph_alf_cb_enabled_flag
2670  || current->ph_alf_cr_enabled_flag) {
2671  ub(3, ph_alf_aps_id_chroma);
2672  }
2673 
2674  if (sps->sps_ccalf_enabled_flag) {
2675  flag(ph_alf_cc_cb_enabled_flag);
2676  if (current->ph_alf_cc_cb_enabled_flag)
2677  ub(3, ph_alf_cc_cb_aps_id);
2678  flag(ph_alf_cc_cr_enabled_flag);
2679  if (current->ph_alf_cc_cr_enabled_flag)
2680  ub(3, ph_alf_cc_cr_aps_id);
2681  }
2682  }
2683  } else {
2684  infer(ph_alf_enabled_flag, 0);
2685  }
2686  if (sps->sps_lmcs_enabled_flag) {
2687  flag(ph_lmcs_enabled_flag);
2688  if (current->ph_lmcs_enabled_flag) {
2689  ub(2, ph_lmcs_aps_id);
2690  if (sps->sps_chroma_format_idc != 0)
2691  flag(ph_chroma_residual_scale_flag);
2692  else
2693  infer(ph_chroma_residual_scale_flag, 0);
2694  }
2695  } else {
2696  infer(ph_lmcs_enabled_flag, 0);
2697  infer(ph_chroma_residual_scale_flag, 0);
2698  }
2699 
2700  if (sps->sps_explicit_scaling_list_enabled_flag) {
2701  flag(ph_explicit_scaling_list_enabled_flag);
2702  if (current->ph_explicit_scaling_list_enabled_flag) {
2703  //todo: check the ph_scaling_list_aps_id range, when aps ready
2704  ub(3, ph_scaling_list_aps_id);
2705  }
2706  } else {
2707  infer(ph_explicit_scaling_list_enabled_flag, 0);
2708  }
2709  if (sps->sps_virtual_boundaries_enabled_flag &&
2710  !sps->sps_virtual_boundaries_present_flag) {
2711  flag(ph_virtual_boundaries_present_flag);
2712  if (current->ph_virtual_boundaries_present_flag) {
2713  ue(ph_num_ver_virtual_boundaries,
2714  0, pps->pps_pic_width_in_luma_samples <= 8 ? 0 : 3);
2715  for (i = 0; i < current->ph_num_ver_virtual_boundaries; i++) {
2716  ues(ph_virtual_boundary_pos_x_minus1[i],
2717  0, (pps->pps_pic_width_in_luma_samples + 7) / 8 - 2, 1, i);
2718  }
2719  ue(ph_num_hor_virtual_boundaries,
2720  0, pps->pps_pic_height_in_luma_samples <= 8 ? 0 : 3);
2721  for (i = 0; i < current->ph_num_hor_virtual_boundaries; i++) {
2722  ues(ph_virtual_boundary_pos_y_minus1[i],
2723  0, (pps->pps_pic_height_in_luma_samples + 7) / 8 - 2, 1, i);
2724  }
2725  } else {
2726  infer(ph_num_ver_virtual_boundaries, 0);
2727  infer(ph_num_hor_virtual_boundaries, 0);
2728  }
2729  }
2730  if (pps->pps_output_flag_present_flag && !current->ph_non_ref_pic_flag)
2731  flag(ph_pic_output_flag);
2732  else
2733  infer(ph_pic_output_flag, 1);
2734  if (pps->pps_rpl_info_in_ph_flag) {
2736  (ctx, rw, sps, pps, &current->ph_ref_pic_lists));
2737  }
2738  if (sps->sps_partition_constraints_override_enabled_flag)
2739  flag(ph_partition_constraints_override_flag);
2740  else
2741  infer(ph_partition_constraints_override_flag, 0);
2742 
2743  ctb_log2_size_y = sps->sps_log2_ctu_size_minus5 + 5;
2744  min_cb_log2_size_y = sps->sps_log2_min_luma_coding_block_size_minus2 + 2;
2745  if (current->ph_intra_slice_allowed_flag) {
2746  if (current->ph_partition_constraints_override_flag) {
2747  ue(ph_log2_diff_min_qt_min_cb_intra_slice_luma,
2748  0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
2749  ue(ph_max_mtt_hierarchy_depth_intra_slice_luma,
2750  0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
2751  if (current->ph_max_mtt_hierarchy_depth_intra_slice_luma != 0) {
2752  min_qt_log2_size_intra_y =
2753  current->ph_log2_diff_min_qt_min_cb_intra_slice_luma +
2754  min_cb_log2_size_y;
2755  ue(ph_log2_diff_max_bt_min_qt_intra_slice_luma,
2756  0, (sps->sps_qtbtt_dual_tree_intra_flag ?
2757  FFMIN(6, ctb_log2_size_y) :
2758  ctb_log2_size_y) - min_qt_log2_size_intra_y);
2759  ue(ph_log2_diff_max_tt_min_qt_intra_slice_luma,
2760  0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_y);
2761  } else {
2762  infer(ph_log2_diff_max_bt_min_qt_intra_slice_luma,
2763  sps->sps_log2_diff_max_bt_min_qt_intra_slice_luma);
2764  infer(ph_log2_diff_max_tt_min_qt_intra_slice_luma,
2765  sps->sps_log2_diff_max_tt_min_qt_intra_slice_luma);
2766  }
2767  if (sps->sps_qtbtt_dual_tree_intra_flag) {
2768  ue(ph_log2_diff_min_qt_min_cb_intra_slice_chroma,
2769  0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
2770  ue(ph_max_mtt_hierarchy_depth_intra_slice_chroma,
2771  0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
2772  if (sps->sps_max_mtt_hierarchy_depth_intra_slice_chroma != 0) {
2773  unsigned int min_qt_log2_size_intra_c =
2774  sps->sps_log2_diff_min_qt_min_cb_intra_slice_chroma +
2775  min_cb_log2_size_y;
2776  ue(ph_log2_diff_max_bt_min_qt_intra_slice_chroma,
2777  0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c);
2778  ue(ph_log2_diff_max_tt_min_qt_intra_slice_chroma,
2779  0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c);
2780  } else {
2781  infer(ph_log2_diff_max_bt_min_qt_intra_slice_chroma,
2782  sps->sps_log2_diff_max_bt_min_qt_intra_slice_chroma);
2783  infer(ph_log2_diff_max_tt_min_qt_intra_slice_chroma,
2784  sps->sps_log2_diff_max_tt_min_qt_intra_slice_chroma);
2785  }
2786  }
2787  } else {
2788  infer(ph_log2_diff_min_qt_min_cb_intra_slice_luma,
2789  sps->sps_log2_diff_min_qt_min_cb_intra_slice_luma);
2790  infer(ph_max_mtt_hierarchy_depth_intra_slice_luma,
2791  sps->sps_max_mtt_hierarchy_depth_intra_slice_luma);
2792  infer(ph_log2_diff_max_bt_min_qt_intra_slice_luma,
2793  sps->sps_log2_diff_max_bt_min_qt_intra_slice_luma);
2794  infer(ph_log2_diff_max_tt_min_qt_intra_slice_luma,
2795  sps->sps_log2_diff_max_tt_min_qt_intra_slice_luma);
2796  infer(ph_log2_diff_min_qt_min_cb_intra_slice_chroma,
2797  sps->sps_log2_diff_min_qt_min_cb_intra_slice_chroma);
2798  infer(ph_max_mtt_hierarchy_depth_intra_slice_chroma,
2799  sps->sps_max_mtt_hierarchy_depth_intra_slice_chroma);
2800  infer(ph_log2_diff_max_bt_min_qt_intra_slice_chroma,
2801  sps->sps_log2_diff_max_bt_min_qt_intra_slice_chroma);
2802  infer(ph_log2_diff_max_tt_min_qt_intra_slice_chroma,
2803  sps->sps_log2_diff_max_tt_min_qt_intra_slice_chroma);
2804  }
2805 
2806  min_qt_log2_size_intra_y =
2807  current->ph_log2_diff_min_qt_min_cb_intra_slice_luma +
2808  min_cb_log2_size_y;
2809  if (pps->pps_cu_qp_delta_enabled_flag)
2810  ue(ph_cu_qp_delta_subdiv_intra_slice, 0,
2811  2 * (ctb_log2_size_y - min_qt_log2_size_intra_y +
2812  current->ph_max_mtt_hierarchy_depth_intra_slice_luma));
2813  else
2814  infer(ph_cu_qp_delta_subdiv_intra_slice, 0);
2815 
2816  if (pps->pps_cu_chroma_qp_offset_list_enabled_flag)
2817  ue(ph_cu_chroma_qp_offset_subdiv_intra_slice, 0,
2818  2 * (ctb_log2_size_y - min_qt_log2_size_intra_y +
2819  current->ph_max_mtt_hierarchy_depth_intra_slice_luma));
2820  else
2821  infer(ph_cu_chroma_qp_offset_subdiv_intra_slice, 0);
2822  }
2823  if (current->ph_inter_slice_allowed_flag) {
2824  if (current->ph_partition_constraints_override_flag) {
2825  ue(ph_log2_diff_min_qt_min_cb_inter_slice,
2826  0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
2827  min_qt_log2_size_inter_y =
2828  current->ph_log2_diff_min_qt_min_cb_inter_slice +
2829  min_cb_log2_size_y;
2830  ue(ph_max_mtt_hierarchy_depth_inter_slice, 0,
2831  2 * (ctb_log2_size_y - min_cb_log2_size_y));
2832  if (current->ph_max_mtt_hierarchy_depth_inter_slice != 0) {
2833  ue(ph_log2_diff_max_bt_min_qt_inter_slice,
2834  0, ctb_log2_size_y - min_qt_log2_size_inter_y);
2835  ue(ph_log2_diff_max_tt_min_qt_inter_slice,
2836  0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_inter_y);
2837  }
2838  } else {
2839  infer(ph_log2_diff_min_qt_min_cb_inter_slice,
2840  sps->sps_log2_diff_min_qt_min_cb_inter_slice);
2841  min_qt_log2_size_inter_y =
2842  current->ph_log2_diff_min_qt_min_cb_inter_slice +
2843  min_cb_log2_size_y;
2844  infer(ph_max_mtt_hierarchy_depth_inter_slice,
2845  sps->sps_max_mtt_hierarchy_depth_inter_slice);
2846  infer(ph_log2_diff_max_bt_min_qt_inter_slice,
2847  sps->sps_log2_diff_max_bt_min_qt_inter_slice);
2848  infer(ph_log2_diff_max_tt_min_qt_inter_slice,
2849  sps->sps_log2_diff_max_tt_min_qt_inter_slice);
2850  }
2851 
2852  if (pps->pps_cu_qp_delta_enabled_flag)
2853  ue(ph_cu_qp_delta_subdiv_inter_slice, 0,
2854  2 * (ctb_log2_size_y - min_qt_log2_size_inter_y +
2855  current->ph_max_mtt_hierarchy_depth_inter_slice));
2856  else
2857  infer(ph_cu_qp_delta_subdiv_inter_slice, 0);
2858 
2859  if (pps->pps_cu_chroma_qp_offset_list_enabled_flag)
2860  ue(ph_cu_chroma_qp_offset_subdiv_inter_slice, 0,
2861  2 * (ctb_log2_size_y - min_qt_log2_size_inter_y +
2862  current->ph_max_mtt_hierarchy_depth_inter_slice));
2863  else
2864  infer(ph_cu_chroma_qp_offset_subdiv_inter_slice, 0);
2865  if (sps->sps_temporal_mvp_enabled_flag) {
2866  flag(ph_temporal_mvp_enabled_flag);
2867  if (current->ph_temporal_mvp_enabled_flag &&
2868  pps->pps_rpl_info_in_ph_flag) {
2869  if (current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 0)
2870  flag(ph_collocated_from_l0_flag);
2871  else
2872  infer(ph_collocated_from_l0_flag, 1);
2873  if ((current->ph_collocated_from_l0_flag &&
2874  current->ph_ref_pic_lists.rpl_ref_list[0].num_ref_entries > 1)
2875  || (!current->ph_collocated_from_l0_flag &&
2876  current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 1)) {
2877  unsigned int idx =
2878  current->ph_collocated_from_l0_flag ? 0 : 1;
2879  ue(ph_collocated_ref_idx, 0,
2880  current->ph_ref_pic_lists.rpl_ref_list[idx].
2881  num_ref_entries - 1);
2882  } else {
2883  infer(ph_collocated_ref_idx, 0);
2884  }
2885  }
2886  }
2887  if (sps->sps_mmvd_fullpel_only_enabled_flag)
2888  flag(ph_mmvd_fullpel_only_flag);
2889  else
2890  infer(ph_mmvd_fullpel_only_flag, 0);
2891  if (!pps->pps_rpl_info_in_ph_flag ||
2892  current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 0) {
2893  flag(ph_mvd_l1_zero_flag);
2894  if (sps->sps_bdof_control_present_in_ph_flag) {
2895  flag(ph_bdof_disabled_flag);
2896  } else {
2897  if (!sps->sps_bdof_control_present_in_ph_flag)
2898  infer(ph_bdof_disabled_flag,
2899  1 - sps->sps_bdof_enabled_flag);
2900  else
2901  infer(ph_bdof_disabled_flag, 1);
2902  }
2903  if (sps->sps_dmvr_control_present_in_ph_flag) {
2904  flag(ph_dmvr_disabled_flag);
2905  } else {
2906  if (!sps->sps_dmvr_control_present_in_ph_flag)
2907  infer(ph_dmvr_disabled_flag,
2908  1 - sps->sps_dmvr_enabled_flag);
2909  else
2910  infer(ph_dmvr_disabled_flag, 1);
2911  }
2912  } else {
2913  infer(ph_mvd_l1_zero_flag, 1);
2914  }
2915  if (sps->sps_prof_control_present_in_ph_flag)
2916  flag(ph_prof_disabled_flag);
2917  else
2918  infer(ph_prof_disabled_flag, !sps->sps_affine_prof_enabled_flag);
2919  if ((pps->pps_weighted_pred_flag ||
2920  pps->pps_weighted_bipred_flag) && pps->pps_wp_info_in_ph_flag) {
2921 
2922  // if pps->pps_wp_info_in_ph_fla == 1
2923  // pred_weight_table will not use num_ref_idx_active
2924  uint8_t num_ref_idx_active[2] = { 0, 0 };
2926  (ctx, rw, sps, pps, &current->ph_ref_pic_lists,
2927  num_ref_idx_active, &current->ph_pred_weight_table));
2928  }
2929  }
2930 
2931  qp_bd_offset = 6 * sps->sps_bitdepth_minus8;
2932  if (pps->pps_qp_delta_info_in_ph_flag)
2933  se(ph_qp_delta, -qp_bd_offset - (26 + pps->pps_init_qp_minus26),
2934  63 - (26 + pps->pps_init_qp_minus26));
2935 
2936  if (sps->sps_joint_cbcr_enabled_flag)
2937  flag(ph_joint_cbcr_sign_flag);
2938  else
2939  infer(ph_joint_cbcr_sign_flag, 0);
2940  if (sps->sps_sao_enabled_flag && pps->pps_sao_info_in_ph_flag) {
2941  flag(ph_sao_luma_enabled_flag);
2942  if (sps->sps_chroma_format_idc != 0)
2943  flag(ph_sao_chroma_enabled_flag);
2944  else
2945  infer(ph_sao_chroma_enabled_flag, 0);
2946  } else {
2947  infer(ph_sao_luma_enabled_flag, 0);
2948  infer(ph_sao_chroma_enabled_flag, 0);
2949  }
2950 
2951  if (pps->pps_dbf_info_in_ph_flag)
2952  flag(ph_deblocking_params_present_flag);
2953  else
2954  infer(ph_deblocking_params_present_flag, 0);
2955 
2956  if (current->ph_deblocking_params_present_flag) {
2957  if (!pps->pps_deblocking_filter_disabled_flag) {
2958  flag(ph_deblocking_filter_disabled_flag);
2959  if (!current->ph_deblocking_filter_disabled_flag) {
2960  se(ph_luma_beta_offset_div2, -12, 12);
2961  se(ph_luma_tc_offset_div2, -12, 12);
2962  if (pps->pps_chroma_tool_offsets_present_flag) {
2963  se(ph_cb_beta_offset_div2, -12, 12);
2964  se(ph_cb_tc_offset_div2, -12, 12);
2965  se(ph_cr_beta_offset_div2, -12, 12);
2966  se(ph_cr_tc_offset_div2, -12, 12);
2967  } else {
2968  infer(ph_cb_beta_offset_div2,
2969  current->ph_luma_beta_offset_div2);
2970  infer(ph_cb_tc_offset_div2,
2971  current->ph_luma_tc_offset_div2);
2972  infer(ph_cr_beta_offset_div2,
2973  current->ph_luma_beta_offset_div2);
2974  infer(ph_cr_tc_offset_div2,
2975  current->ph_luma_tc_offset_div2);
2976  }
2977  }
2978  } else {
2979  infer(ph_deblocking_filter_disabled_flag, 0);
2980  }
2981  } else {
2982  infer(ph_deblocking_filter_disabled_flag, pps->pps_deblocking_filter_disabled_flag);
2983  if (!current->ph_deblocking_filter_disabled_flag) {
2984  infer(ph_luma_beta_offset_div2, pps->pps_luma_beta_offset_div2);
2985  infer(ph_luma_tc_offset_div2, pps->pps_luma_tc_offset_div2);
2986  infer(ph_cb_beta_offset_div2, pps->pps_cb_beta_offset_div2);
2987  infer(ph_cb_tc_offset_div2, pps->pps_cb_tc_offset_div2);
2988  infer(ph_cr_beta_offset_div2, pps->pps_cr_beta_offset_div2);
2989  infer(ph_cr_tc_offset_div2, pps->pps_cr_tc_offset_div2);
2990  }
2991  }
2992 
2993  if (pps->pps_picture_header_extension_present_flag) {
2994  ue(ph_extension_length, 0, 256);
2995  for (i = 0; i < current->ph_extension_length; i++)
2996  us(8, ph_extension_data_byte[i], 0x00, 0xff, 1, i);
2997  }
2998 
2999  return 0;
3000 }
3001 
3003  H266RawPH *current)
3004 {
3005  int err;
3006 
3007  HEADER("Picture Header");
3008 
3009  CHECK(FUNC(nal_unit_header) (ctx, rw, &current->nal_unit_header, VVC_PH_NUT));
3010  CHECK(FUNC(picture_header) (ctx, rw, &current->ph_picture_header));
3011  CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
3012  return 0;
3013 }
3014 
3016  H266RawSliceHeader *current)
3017 {
3019  const H266RawSPS *sps;
3020  const H266RawPPS *pps;
3021  const H266RawPictureHeader *ph;
3023  int err, i;
3024  uint8_t nal_unit_type, qp_bd_offset;
3025  uint16_t num_slices_in_subpic;
3026 
3027  HEADER("Slice Header");
3028 
3029  CHECK(FUNC(nal_unit_header) (ctx, rw, &current->nal_unit_header, -1));
3030 
3031  flag(sh_picture_header_in_slice_header_flag);
3032  if (current->sh_picture_header_in_slice_header_flag) {
3033  // 7.4.8 if sh_picture_header_in_slice_header_flag is true, we do not have a PH NAL unit
3034  CHECK(FUNC(picture_header) (ctx, rw, &current->sh_picture_header));
3035  ph = &current->sh_picture_header;
3036  } else {
3037  ph = h266->ph;
3038  if (!ph) {
3039  av_log(ctx->log_ctx, AV_LOG_ERROR,
3040  "Picture header not available.\n");
3041  return AVERROR_INVALIDDATA;
3042  }
3043  }
3044 
3045  pps = h266->pps[ph->ph_pic_parameter_set_id];
3046  if (!pps) {
3047  av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
3048  ph->ph_pic_parameter_set_id);
3049  return AVERROR_INVALIDDATA;
3050  }
3051  sps = h266->sps[pps->pps_seq_parameter_set_id];
3052  if (!sps) {
3053  av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
3054  pps->pps_seq_parameter_set_id);
3055  return AVERROR_INVALIDDATA;
3056  }
3057 
3058  if (sps->sps_subpic_info_present_flag) {
3059  ub(sps->sps_subpic_id_len_minus1 + 1, sh_subpic_id);
3060  for (i = 0; i <= sps->sps_num_subpics_minus1; i++) {
3061  if (pps->sub_pic_id_val[i] == current->sh_subpic_id) {
3062  current->curr_subpic_idx = i;
3063  break;
3064  }
3065  }
3066  if (i > sps->sps_num_subpics_minus1) {
3067  av_log(ctx->log_ctx, AV_LOG_ERROR, "invalid CurrSubpicIdx %d\n", i);
3068  return AVERROR_INVALIDDATA;
3069  }
3070  } else {
3071  current->curr_subpic_idx = 0;
3072  }
3073 
3074  num_slices_in_subpic = pps->num_slices_in_subpic[current->curr_subpic_idx];
3075 
3076  if ((pps->pps_rect_slice_flag && num_slices_in_subpic > 1) ||
3077  (!pps->pps_rect_slice_flag && pps->num_tiles_in_pic > 1)) {
3078  unsigned int bits, max;
3079  if (!pps->pps_rect_slice_flag) {
3080  bits = av_ceil_log2(pps->num_tiles_in_pic);
3081  max = pps->num_tiles_in_pic - 1;
3082  } else {
3083  bits = av_ceil_log2(num_slices_in_subpic);
3084  max = num_slices_in_subpic - 1;
3085  }
3086  u(bits, sh_slice_address, 0, max);
3087  } else {
3088  infer(sh_slice_address, 0);
3089  }
3090 
3091  for (i = 0; i < sps->sps_num_extra_sh_bytes * 8; i++) {
3092  if (sps->sps_extra_sh_bit_present_flag[i])
3093  flags(sh_extra_bit[i], 1, i);
3094  }
3095 
3096  if (!pps->pps_rect_slice_flag &&
3097  pps->num_tiles_in_pic - current->sh_slice_address > 1)
3098  ue(sh_num_tiles_in_slice_minus1, 0, pps->num_tiles_in_pic - 1);
3099  else
3100  infer(sh_num_tiles_in_slice_minus1, 0);
3101 
3102  if (ph->ph_inter_slice_allowed_flag)
3103  ue(sh_slice_type, 0, 2);
3104  else
3105  infer(sh_slice_type, 2);
3106 
3107  nal_unit_type = current->nal_unit_header.nal_unit_type;
3108  if (nal_unit_type == VVC_IDR_W_RADL || nal_unit_type == VVC_IDR_N_LP ||
3109  nal_unit_type == VVC_CRA_NUT || nal_unit_type == VVC_GDR_NUT)
3110  flag(sh_no_output_of_prior_pics_flag);
3111 
3112  if (sps->sps_alf_enabled_flag) {
3113  if (!pps->pps_alf_info_in_ph_flag) {
3114  flag(sh_alf_enabled_flag);
3115  if (current->sh_alf_enabled_flag) {
3116  ub(3, sh_num_alf_aps_ids_luma);
3117  for (i = 0; i < current->sh_num_alf_aps_ids_luma; i++)
3118  ubs(3, sh_alf_aps_id_luma[i], 1, i);
3119 
3120  if (sps->sps_chroma_format_idc != 0) {
3121  flag(sh_alf_cb_enabled_flag);
3122  flag(sh_alf_cr_enabled_flag);
3123  }
3124  if (current->sh_alf_cb_enabled_flag ||
3125  current->sh_alf_cr_enabled_flag) {
3126  ub(3, sh_alf_aps_id_chroma);
3127  }
3128 
3129  if (sps->sps_ccalf_enabled_flag) {
3130  flag(sh_alf_cc_cb_enabled_flag);
3131  if (current->sh_alf_cc_cb_enabled_flag)
3132  ub(3, sh_alf_cc_cb_aps_id);
3133 
3134  flag(sh_alf_cc_cr_enabled_flag);
3135  if (current->sh_alf_cc_cr_enabled_flag)
3136  ub(3, sh_alf_cc_cr_aps_id);
3137  }
3138  }
3139  } else {
3140  infer(sh_alf_enabled_flag, ph->ph_alf_enabled_flag);
3141  if (current->sh_alf_enabled_flag) {
3142  infer(sh_num_alf_aps_ids_luma, ph->ph_num_alf_aps_ids_luma);
3143  for (i = 0; i < current->sh_num_alf_aps_ids_luma; i++)
3144  infer(sh_alf_aps_id_luma[i], ph->ph_alf_aps_id_luma[i]);
3145 
3146  infer(sh_alf_cb_enabled_flag, ph->ph_alf_cb_enabled_flag);
3147  infer(sh_alf_cr_enabled_flag, ph->ph_alf_cr_enabled_flag);
3148  if (current->sh_alf_cb_enabled_flag ||current->sh_alf_cr_enabled_flag)
3149  infer(sh_alf_aps_id_chroma, ph->ph_alf_aps_id_chroma);
3150 
3151  if (sps->sps_ccalf_enabled_flag) {
3152  infer(sh_alf_cc_cb_enabled_flag, ph->ph_alf_cc_cb_enabled_flag);
3153  if (current->sh_alf_cc_cb_enabled_flag)
3154  infer(sh_alf_cc_cb_aps_id, ph->ph_alf_cc_cb_aps_id);
3155 
3156  infer(sh_alf_cc_cr_enabled_flag, ph->ph_alf_cc_cr_enabled_flag);
3157  if (current->sh_alf_cc_cr_enabled_flag)
3158  infer(sh_alf_cc_cr_aps_id, ph->ph_alf_cc_cr_aps_id);
3159  }
3160  }
3161  }
3162  }
3163 
3164  if (current->sh_picture_header_in_slice_header_flag) {
3165  infer(sh_lmcs_used_flag, ph->ph_lmcs_enabled_flag);
3166  infer(sh_explicit_scaling_list_used_flag,
3167  ph->ph_explicit_scaling_list_enabled_flag);
3168  } else {
3169  if (ph->ph_lmcs_enabled_flag)
3170  flag(sh_lmcs_used_flag);
3171  else
3172  infer(sh_lmcs_used_flag, 0);
3173 
3174  if (ph->ph_explicit_scaling_list_enabled_flag)
3175  flag(sh_explicit_scaling_list_used_flag);
3176  else
3177  infer(sh_explicit_scaling_list_used_flag, 0);
3178  }
3179 
3180  if (!pps->pps_rpl_info_in_ph_flag &&
3181  ((nal_unit_type != VVC_IDR_W_RADL &&
3182  nal_unit_type != VVC_IDR_N_LP) || sps->sps_idr_rpl_present_flag)) {
3184  (ctx, rw, sps, pps, &current->sh_ref_pic_lists));
3185  ref_pic_lists = &current->sh_ref_pic_lists;
3186  } else {
3187  ref_pic_lists = &ph->ph_ref_pic_lists;
3188  }
3189  if ((current->sh_slice_type != VVC_SLICE_TYPE_I &&
3190  ref_pic_lists->rpl_ref_list[0].num_ref_entries > 1) ||
3191  (current->sh_slice_type == VVC_SLICE_TYPE_B &&
3192  ref_pic_lists->rpl_ref_list[1].num_ref_entries > 1)) {
3193  flag(sh_num_ref_idx_active_override_flag);
3194  if (current->sh_num_ref_idx_active_override_flag) {
3195  for (i = 0;
3196  i < (current->sh_slice_type == VVC_SLICE_TYPE_B ? 2 : 1); i++)
3197  if (ref_pic_lists->rpl_ref_list[i].num_ref_entries > 1)
3198  ues(sh_num_ref_idx_active_minus1[i], 0, 14, 1, i);
3199  else
3200  infer(sh_num_ref_idx_active_minus1[i], 0);
3201  }
3202  } else {
3203  infer(sh_num_ref_idx_active_override_flag, 1);
3204  }
3205 
3206  for (i = 0; i < 2; i++) {
3207  if (current->sh_slice_type == VVC_SLICE_TYPE_B ||
3208  (current->sh_slice_type == VVC_SLICE_TYPE_P && i == 0)) {
3209  if (current->sh_num_ref_idx_active_override_flag) {
3210  current->num_ref_idx_active[i] = current->sh_num_ref_idx_active_minus1[i] + 1;
3211  } else {
3212  current->num_ref_idx_active[i] =
3213  FFMIN(ref_pic_lists->rpl_ref_list[i].num_ref_entries,
3214  pps->pps_num_ref_idx_default_active_minus1[i] + 1);
3215  }
3216  } else {
3217  current->num_ref_idx_active[i] = 0;
3218  }
3219  }
3220 
3221  if (current->sh_slice_type != VVC_SLICE_TYPE_I) {
3222  if (pps->pps_cabac_init_present_flag)
3223  flag(sh_cabac_init_flag);
3224  else
3225  infer(sh_cabac_init_flag, 0);
3226  if (ph->ph_temporal_mvp_enabled_flag) {
3227  if (!pps->pps_rpl_info_in_ph_flag) {
3228  if (current->sh_slice_type == VVC_SLICE_TYPE_B)
3229  flag(sh_collocated_from_l0_flag);
3230  else
3231  infer(sh_collocated_from_l0_flag, 1);
3232  if ((current->sh_collocated_from_l0_flag &&
3233  current->num_ref_idx_active[0] > 1) ||
3234  (!current->sh_collocated_from_l0_flag &&
3235  current->num_ref_idx_active[1] > 1)) {
3236  unsigned int idx = current->sh_collocated_from_l0_flag ? 0 : 1;
3237  ue(sh_collocated_ref_idx, 0, current->num_ref_idx_active[idx] - 1);
3238  } else {
3239  infer(sh_collocated_ref_idx, 0);
3240  }
3241  } else {
3242  if (current->sh_slice_type == VVC_SLICE_TYPE_B)
3243  infer(sh_collocated_from_l0_flag, ph->ph_collocated_from_l0_flag);
3244  else
3245  infer(sh_collocated_from_l0_flag, 1);
3246  infer(sh_collocated_ref_idx, ph->ph_collocated_ref_idx);
3247  }
3248  }
3249  if (!pps->pps_wp_info_in_ph_flag &&
3250  ((pps->pps_weighted_pred_flag &&
3251  current->sh_slice_type == VVC_SLICE_TYPE_P) ||
3252  (pps->pps_weighted_bipred_flag &&
3253  current->sh_slice_type == VVC_SLICE_TYPE_B))) {
3255  current->num_ref_idx_active,
3256  &current->sh_pred_weight_table));
3257  }
3258  }
3259  qp_bd_offset = 6 * sps->sps_bitdepth_minus8;
3260  if (!pps->pps_qp_delta_info_in_ph_flag)
3261  se(sh_qp_delta, -qp_bd_offset - (26 + pps->pps_init_qp_minus26),
3262  63 - (26 + pps->pps_init_qp_minus26));
3263  if (pps->pps_slice_chroma_qp_offsets_present_flag) {
3264  int8_t off;
3265 
3266  se(sh_cb_qp_offset, -12, 12);
3267  off = pps->pps_cb_qp_offset + current->sh_cb_qp_offset;
3268  if (off < -12 || off > 12) {
3269  av_log(ctx->log_ctx, AV_LOG_ERROR,
3270  "pps_cb_qp_offset + sh_cb_qp_offset (%d) not in range [-12, 12].\n",
3271  off);
3272  return AVERROR_INVALIDDATA;
3273  }
3274 
3275  se(sh_cr_qp_offset, -12, 12);
3276  off = pps->pps_cr_qp_offset + current->sh_cr_qp_offset;
3277  if (off < -12 || off > 12) {
3278  av_log(ctx->log_ctx, AV_LOG_ERROR,
3279  "pps_cr_qp_offset + sh_cr_qp_offset (%d) not in range [-12, 12].\n",
3280  off);
3281  return AVERROR_INVALIDDATA;
3282  }
3283 
3284  if (sps->sps_joint_cbcr_enabled_flag) {
3285  se(sh_joint_cbcr_qp_offset, -12, 12);
3286  off =
3287  pps->pps_joint_cbcr_qp_offset_value +
3288  current->sh_joint_cbcr_qp_offset;
3289  if (off < -12 || off > 12) {
3290  av_log(ctx->log_ctx, AV_LOG_ERROR,
3291  "pps_joint_cbcr_qp_offset_value + sh_joint_cbcr_qp_offset (%d)"
3292  "not in range [-12, 12]. \n", off);
3293  return AVERROR_INVALIDDATA;
3294  }
3295  } else {
3296  infer(sh_joint_cbcr_qp_offset, 0);
3297  }
3298  } else {
3299  infer(sh_cb_qp_offset, 0);
3300  infer(sh_cr_qp_offset, 0);
3301  infer(sh_joint_cbcr_qp_offset, 0);
3302  }
3303  if (pps->pps_cu_chroma_qp_offset_list_enabled_flag)
3304  flag(sh_cu_chroma_qp_offset_enabled_flag);
3305  else
3306  infer(sh_cu_chroma_qp_offset_enabled_flag, 0);
3307  if (sps->sps_sao_enabled_flag && !pps->pps_sao_info_in_ph_flag) {
3308  flag(sh_sao_luma_used_flag);
3309  if (sps->sps_chroma_format_idc != 0)
3310  flag(sh_sao_chroma_used_flag);
3311  else
3312  infer(sh_sao_chroma_used_flag, ph->ph_sao_chroma_enabled_flag);
3313  } else {
3314  infer(sh_sao_luma_used_flag, ph->ph_sao_luma_enabled_flag);
3315  infer(sh_sao_chroma_used_flag, ph->ph_sao_chroma_enabled_flag);
3316  }
3317 
3318  if (pps->pps_deblocking_filter_override_enabled_flag &&
3319  !pps->pps_dbf_info_in_ph_flag)
3320  flag(sh_deblocking_params_present_flag);
3321  else
3322  infer(sh_deblocking_params_present_flag, 0);
3323  if (current->sh_deblocking_params_present_flag) {
3324  if (!pps->pps_deblocking_filter_disabled_flag)
3325  flag(sh_deblocking_filter_disabled_flag);
3326  else
3327  infer(sh_deblocking_filter_disabled_flag, 0);
3328  if (!current->sh_deblocking_filter_disabled_flag) {
3329  se(sh_luma_beta_offset_div2, -12, 12);
3330  se(sh_luma_tc_offset_div2, -12, 12);
3331  if (pps->pps_chroma_tool_offsets_present_flag) {
3332  se(sh_cb_beta_offset_div2, -12, 12);
3333  se(sh_cb_tc_offset_div2, -12, 12);
3334  se(sh_cr_beta_offset_div2, -12, 12);
3335  se(sh_cr_tc_offset_div2, -12, 12);
3336  } else {
3337  infer(sh_cb_beta_offset_div2,
3338  current->sh_luma_beta_offset_div2);
3339  infer(sh_cb_tc_offset_div2, current->sh_luma_tc_offset_div2);
3340  infer(sh_cr_beta_offset_div2,
3341  current->sh_luma_beta_offset_div2);
3342  infer(sh_cr_tc_offset_div2, current->sh_luma_tc_offset_div2);
3343  }
3344  }
3345  } else {
3346  infer(sh_deblocking_filter_disabled_flag, ph->ph_deblocking_filter_disabled_flag);
3347  if (!current->sh_deblocking_filter_disabled_flag) {
3348  infer(sh_luma_beta_offset_div2, ph->ph_luma_beta_offset_div2);
3349  infer(sh_luma_tc_offset_div2, ph->ph_luma_tc_offset_div2);
3350  infer(sh_cb_beta_offset_div2, ph->ph_cb_beta_offset_div2);
3351  infer(sh_cb_tc_offset_div2, ph->ph_cb_tc_offset_div2);
3352  infer(sh_cr_beta_offset_div2, ph->ph_cr_beta_offset_div2);
3353  infer(sh_cr_tc_offset_div2, ph->ph_cr_tc_offset_div2);
3354  }
3355  }
3356 
3357  if (sps->sps_dep_quant_enabled_flag)
3358  flag(sh_dep_quant_used_flag);
3359  else
3360  infer(sh_dep_quant_used_flag, 0);
3361 
3362  if (sps->sps_sign_data_hiding_enabled_flag &&
3363  !current->sh_dep_quant_used_flag)
3364  flag(sh_sign_data_hiding_used_flag);
3365  else
3366  infer(sh_sign_data_hiding_used_flag, 0);
3367 
3368  if (sps->sps_transform_skip_enabled_flag &&
3369  !current->sh_dep_quant_used_flag &&
3370  !current->sh_sign_data_hiding_used_flag)
3371  flag(sh_ts_residual_coding_disabled_flag);
3372  else
3373  infer(sh_ts_residual_coding_disabled_flag, 0);
3374 
3375  if (!current->sh_ts_residual_coding_disabled_flag &&
3376  sps->sps_ts_residual_coding_rice_present_in_sh_flag)
3377  ub(3, sh_ts_residual_coding_rice_idx_minus1);
3378  else
3379  infer(sh_ts_residual_coding_rice_idx_minus1, 0);
3380 
3381  if (sps->sps_reverse_last_sig_coeff_enabled_flag)
3382  flag(sh_reverse_last_sig_coeff_flag);
3383  else
3384  infer(sh_reverse_last_sig_coeff_flag, 0);
3385 
3386  if (pps->pps_slice_header_extension_present_flag) {
3387  ue(sh_slice_header_extension_length, 0, 256);
3388  for (i = 0; i < current->sh_slice_header_extension_length; i++)
3389  us(8, sh_slice_header_extension_data_byte[i], 0x00, 0xff, 1, i);
3390  }
3391 
3392  current->num_entry_points = 0;
3393  if (sps->sps_entry_point_offsets_present_flag) {
3394  uint8_t entropy_sync = sps->sps_entropy_coding_sync_enabled_flag;
3395  int height;
3396  if (pps->pps_rect_slice_flag) {
3397  int width_in_tiles;
3398  int slice_idx = current->sh_slice_address;
3399  for (i = 0; i < current->curr_subpic_idx; i++) {
3400  slice_idx += pps->num_slices_in_subpic[i];
3401  }
3402  width_in_tiles =
3403  pps->pps_slice_width_in_tiles_minus1[slice_idx] + 1;
3404 
3405  if (entropy_sync)
3406  height = pps->slice_height_in_ctus[slice_idx];
3407  else
3408  height = pps->pps_slice_height_in_tiles_minus1[slice_idx] + 1;
3409 
3410  current->num_entry_points = width_in_tiles * height;
3411  } else {
3412  int tile_idx;
3413  int tile_y;
3414  for (tile_idx = current->sh_slice_address;
3415  tile_idx <=
3416  current->sh_slice_address +
3417  current->sh_num_tiles_in_slice_minus1; tile_idx++) {
3418  tile_y = tile_idx / pps->num_tile_rows;
3419  height = pps->row_height_val[tile_y];
3420  current->num_entry_points += (entropy_sync ? height : 1);
3421  }
3422  }
3423  current->num_entry_points--;
3424  if (current->num_entry_points > VVC_MAX_ENTRY_POINTS) {
3425  av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: "
3426  "%" PRIu16 ".\n", current->num_entry_points);
3427  return AVERROR_PATCHWELCOME;
3428  }
3429  if (current->num_entry_points > 0) {
3430  ue(sh_entry_offset_len_minus1, 0, 31);
3431  for (i = 0; i < current->num_entry_points; i++) {
3432  ubs(current->sh_entry_offset_len_minus1 + 1,
3433  sh_entry_point_offset_minus1[i], 1, i);
3434  }
3435  }
3436  }
3437  CHECK(FUNC(byte_alignment) (ctx, rw));
3438 
3439  return 0;
3440 }
3441 
3442 SEI_FUNC(sei_decoded_picture_hash, (CodedBitstreamContext *ctx,
3443  RWContext *rw,
3445  SEIMessageState *unused))
3446 {
3447  int err, c_idx, i;
3448 
3449  HEADER("Decoded Picture Hash");
3450 
3451  u(8, dph_sei_hash_type, 0, 2);
3452  flag(dph_sei_single_component_flag);
3453  ub(7, dph_sei_reserved_zero_7bits);
3454 
3455  for (c_idx = 0; c_idx < (current->dph_sei_single_component_flag ? 1 : 3);
3456  c_idx++) {
3457  if (current->dph_sei_hash_type == 0) {
3458  for (i = 0; i < 16; i++)
3459  us(8, dph_sei_picture_md5[c_idx][i], 0x00, 0xff, 2, c_idx, i);
3460  } else if (current->dph_sei_hash_type == 1) {
3461  us(16, dph_sei_picture_crc[c_idx], 0x0000, 0xffff, 1, c_idx);
3462  } else if (current->dph_sei_hash_type == 2) {
3463  us(32, dph_sei_picture_checksum[c_idx], 0x00000000, 0xffffffff, 1,
3464  c_idx);
3465  }
3466  }
3467  return 0;
3468 }
3469 
3471  H266RawSEI *current, int prefix)
3472 {
3473  int err;
3474 
3475  if (prefix)
3476  HEADER("Prefix Supplemental Enhancement Information");
3477  else
3478  HEADER("Suffix Supplemental Enhancement Information");
3479 
3480  CHECK(FUNC(nal_unit_header) (ctx, rw, &current->nal_unit_header,
3481  prefix ? VVC_PREFIX_SEI_NUT
3482  : VVC_SUFFIX_SEI_NUT));
3483 
3484  CHECK(FUNC_SEI(message_list) (ctx, rw, &current->message_list, prefix));
3485 
3486  CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
3487 
3488  return 0;
3489 }
sei
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H266RawSEI *current, int prefix)
Definition: cbs_h266_syntax_template.c:3470
bit_position
#define bit_position(rw)
Definition: cbs_h2645.c:442
VVC_GDR_NUT
@ VVC_GDR_NUT
Definition: vvc.h:39
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
VVC_MAX_REF_ENTRIES
@ VVC_MAX_REF_ENTRIES
Definition: vvc.h:115
VVC_NUM_ALF_FILTERS
@ VVC_NUM_ALF_FILTERS
Definition: vvc.h:106
dpb_parameters
static int FUNC() dpb_parameters(CodedBitstreamContext *ctx, RWContext *rw, H266DpbParameters *current, uint8_t max_sublayers_minus1, uint8_t sublayer_info_flag)
Definition: cbs_h266_syntax_template.c:394
r
const char * r
Definition: vf_curves.c:126
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
H266RawDCI
Definition: cbs_h266.h:252
H266RawSEIDecodedPictureHash::dph_sei_hash_type
uint8_t dph_sei_hash_type
Definition: cbs_h266.h:852
ff_ctz
#define ff_ctz
Definition: intmath.h:107
lmcs_data
static int FUNC() lmcs_data(CodedBitstreamContext *ctx, RWContext *rw, H266RawAPS *current)
Definition: cbs_h266_syntax_template.c:2371
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:250
VVC_DCI_NUT
@ VVC_DCI_NUT
Definition: vvc.h:42
sublayer_hrd_parameters
static int FUNC() sublayer_hrd_parameters(CodedBitstreamContext *ctx, RWContext *rw, H266RawSubLayerHRDParameters *current, int sublayer_id, const H266RawGeneralTimingHrdParameters *general)
Definition: cbs_h266_syntax_template.c:586
se
#define se(name, range_min, range_max)
Definition: cbs_h2645.c:259
HEADER
#define HEADER(name)
Definition: cbs_av1.c:456
ref_pic_list_struct
static int FUNC() ref_pic_list_struct(CodedBitstreamContext *ctx, RWContext *rw, H266RefPicListStruct *current, uint8_t list_idx, uint8_t rpls_idx, const H266RawSPS *sps)
Definition: cbs_h266_syntax_template.c:410
infer
#define infer(name, value)
Definition: cbs_av1.c:643
ph
static int FUNC() ph(CodedBitstreamContext *ctx, RWContext *rw, H266RawPH *current)
Definition: cbs_h266_syntax_template.c:3002
vui_parameters
static int FUNC() vui_parameters(CodedBitstreamContext *ctx, RWContext *rw, H266RawVUI *current, uint8_t chroma_format_idc)
Definition: cbs_h266_syntax_template.c:239
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
VVC_MAX_HEIGHT
@ VVC_MAX_HEIGHT
Definition: vvc.h:133
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:219
H266DpbParameters
Definition: cbs_h266.h:154
profile_tier_level
static int FUNC() profile_tier_level(CodedBitstreamContext *ctx, RWContext *rw, H266RawProfileTierLevel *current, int profile_tier_present_flag, int max_num_sub_layers_minus1)
Definition: cbs_h266_syntax_template.c:178
VVC_MAX_WIDTH
@ VVC_MAX_WIDTH
Definition: vvc.h:132
VVC_MAX_TOTAL_NUM_OLSS
@ VVC_MAX_TOTAL_NUM_OLSS
Definition: vvc.h:92
H266RefPicListStruct::st_ref_pic_flag
uint8_t st_ref_pic_flag[VVC_MAX_REF_ENTRIES]
Definition: cbs_h266.h:164
vps
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H266RawVPS *current)
Definition: cbs_h266_syntax_template.c:694
allocate
#define allocate(name, size)
Definition: cbs_h2645.c:445
H266RefPicListStruct::ltrp_in_header_flag
uint8_t ltrp_in_header_flag
Definition: cbs_h266.h:162
max
#define max(a, b)
Definition: cuda_runtime.h:33
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
H266RawExtensionData
Definition: cbs_h266.h:148
VVC_MAX_TILES_PER_AU
@ VVC_MAX_TILES_PER_AU
Definition: vvc.h:136
H266RefPicListStruct::num_ref_entries
uint8_t num_ref_entries
Definition: cbs_h266.h:161
ses
#define ses(name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:274
VVC_AUD_NUT
@ VVC_AUD_NUT
Definition: vvc.h:49
VVC_MAX_REF_PIC_LISTS
@ VVC_MAX_REF_PIC_LISTS
Definition: vvc.h:112
aud
static int FUNC() aud(CodedBitstreamContext *ctx, RWContext *rw, H266RawAUD *current)
Definition: cbs_h266_syntax_template.c:2497
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
CodedBitstreamH266Context::pps
H266RawPPS * pps[VVC_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:874
VVC_MAX_SLICES
@ VVC_MAX_SLICES
Definition: vvc.h:144
CodedBitstreamH266Context::vps
H266RawVPS * vps[VVC_MAX_VPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:872
H266RawAUD
Definition: cbs_h266.h:644
VVC_ASP_TYPE_ALF
@ VVC_ASP_TYPE_ALF
Definition: vvc.h:70
slice_header
static int FUNC() slice_header(CodedBitstreamContext *ctx, RWContext *rw, H266RawSliceHeader *current)
Definition: cbs_h266_syntax_template.c:3015
CHECK
CHECK(-1) CHECK(-2) }} }} CHECK(1) CHECK(2) }} }} } if(diff0+diff1 > 0) temp -
sps_range_extension
static int FUNC() sps_range_extension(CodedBitstreamContext *ctx, RWContext *rw, H266RawSPS *current)
Definition: cbs_h266_syntax_template.c:1039
H266RefPicListStruct::inter_layer_ref_pic_flag
uint8_t inter_layer_ref_pic_flag[VVC_MAX_REF_ENTRIES]
Definition: cbs_h266.h:163
av_ceil_log2
#define av_ceil_log2
Definition: common.h:95
GetBitContext
Definition: get_bits.h:108
H266RawAPS
Definition: cbs_h266.h:598
ub
#define ub(width, name)
Definition: cbs_h2645.c:400
ue
#define ue(name, range_min, range_max)
Definition: cbs_h2645.c:253
H266GeneralConstraintsInfo
Definition: cbs_h266.h:36
us
#define us(width, name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:262
pps
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H266RawPPS *current)
Definition: cbs_h266_syntax_template.c:1647
H266RawProfileTierLevel
Definition: cbs_h266.h:133
VVC_IDR_W_RADL
@ VVC_IDR_W_RADL
Definition: vvc.h:36
message_list
static int FUNC() message_list(CodedBitstreamContext *ctx, RWContext *rw, SEIRawMessageList *current, int prefix)
Definition: cbs_sei_syntax_template.c:253
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
VVC_MAX_SUBLAYERS
@ VVC_MAX_SUBLAYERS
Definition: vvc.h:83
H266RawVUI
Definition: cbs_h266.h:211
H266RawSEI
Definition: cbs_h266.h:861
width
#define width
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
bits
uint8_t bits
Definition: vp3data.h:128
rbsp_trailing_bits
static int FUNC() rbsp_trailing_bits(CodedBitstreamContext *ctx, RWContext *rw)
Definition: cbs_h266_syntax_template.c:19
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
H266RefPicLists
Definition: cbs_h266.h:171
FUNC_SEI
#define FUNC_SEI(name)
Definition: cbs_h2645.c:236
MAX_UINT_BITS
#define MAX_UINT_BITS(length)
Definition: cbs_internal.h:196
H266RawGeneralTimingHrdParameters
Definition: cbs_h266.h:180
cbs_h265_payload_extension_present
static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t payload_size, int cur_pos)
Definition: cbs_h2645.c:213
H266RawSPS
Definition: cbs_h266.h:308
VVC_SLICE_TYPE_P
@ VVC_SLICE_TYPE_P
Definition: vvc.h:65
H266RawVPS
Definition: cbs_h266.h:262
scaling_list_data
static int FUNC() scaling_list_data(CodedBitstreamContext *ctx, RWContext *rw, H266RawAPS *current)
Definition: cbs_h266_syntax_template.c:2407
H266RawPPS
Definition: cbs_h266.h:496
aps
static int FUNC() aps(CodedBitstreamContext *ctx, RWContext *rw, H266RawAPS *current, int prefix)
Definition: cbs_h266_syntax_template.c:2460
H266RawOPI
Definition: cbs_h266.h:241
H266RawPictureHeader
Definition: cbs_h266.h:674
bits_left
#define bits_left
Definition: bitstream.h:114
byte_alignment
static int FUNC() byte_alignment(CodedBitstreamContext *ctx, RWContext *rw)
Definition: cbs_h266_syntax_template.c:50
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
VVC_MAX_ENTRY_POINTS
@ VVC_MAX_ENTRY_POINTS
Definition: vvc.h:153
VVC_PREFIX_SEI_NUT
@ VVC_PREFIX_SEI_NUT
Definition: vvc.h:52
general_constraints_info
static int FUNC() general_constraints_info(CodedBitstreamContext *ctx, RWContext *rw, H266GeneralConstraintsInfo *current)
Definition: cbs_h266_syntax_template.c:60
VVC_SLICE_TYPE_B
@ VVC_SLICE_TYPE_B
Definition: vvc.h:64
H266RawPredWeightTable
Definition: cbs_h266.h:650
VVC_ASP_TYPE_LMCS
@ VVC_ASP_TYPE_LMCS
Definition: vvc.h:71
opi
static int FUNC() opi(CodedBitstreamContext *ctx, RWContext *rw, H266RawOPI *current)
Definition: cbs_h266_syntax_template.c:643
ff_refstruct_allocz
static void * ff_refstruct_allocz(size_t size)
Equivalent to ff_refstruct_alloc_ext(size, 0, NULL, NULL)
Definition: refstruct.h:105
VVC_SLICE_TYPE_I
@ VVC_SLICE_TYPE_I
Definition: vvc.h:66
VVC_PH_NUT
@ VVC_PH_NUT
Definition: vvc.h:48
SEI_FUNC
SEI_FUNC(sei_decoded_picture_hash,(CodedBitstreamContext *ctx, RWContext *rw, H266RawSEIDecodedPictureHash *current, SEIMessageState *unused))
Definition: cbs_h266_syntax_template.c:3442
payload_extension
static int FUNC() payload_extension(CodedBitstreamContext *ctx, RWContext *rw, H266RawExtensionData *current, uint32_t payload_size, int cur_pos)
Definition: cbs_h266_syntax_template.c:310
VVC_MAX_LAYERS
@ VVC_MAX_LAYERS
Definition: vvc.h:80
VVC_VPS_NUT
@ VVC_VPS_NUT
Definition: vvc.h:43
sh_slice_address
static void sh_slice_address(VVCSH *sh, const H266RawSPS *sps, const VVCPPS *pps)
Definition: vvc_ps.c:1031
CodedBitstreamH266Context::ph
H266RawPictureHeader * ph
Definition: cbs_h266.h:875
H266RefPicListStruct
Definition: cbs_h266.h:160
H266RawSubLayerHRDParameters
Definition: cbs_h266.h:194
height
#define height
CodedBitstreamH266Context
Definition: cbs_h266.h:866
vui_parameters_default
static int FUNC() vui_parameters_default(CodedBitstreamContext *ctx, RWContext *rw, H266RawVUI *current)
Definition: cbs_h266_syntax_template.c:213
VVC_MAX_TILE_COLUMNS
@ VVC_MAX_TILE_COLUMNS
Definition: vvc.h:141
VVC_SPS_NUT
@ VVC_SPS_NUT
Definition: vvc.h:44
alf_data
static int FUNC() alf_data(CodedBitstreamContext *ctx, RWContext *rw, H266RawAPS *current)
Definition: cbs_h266_syntax_template.c:2260
H266RawSliceHeader
Definition: cbs_h266.h:769
ols_timing_hrd_parameters
static int FUNC() ols_timing_hrd_parameters(CodedBitstreamContext *ctx, RWContext *rw, H266RawOlsTimingHrdParameters *current, uint8_t first_sublayer, uint8_t max_sublayers_minus1, const H266RawGeneralTimingHrdParameters *general)
Definition: cbs_h266_syntax_template.c:609
flag
#define flag(name)
Definition: cbs_av1.c:474
ubs
#define ubs(width, name, subs,...)
Definition: cbs_h2645.c:264
VVC_MAX_DPB_SIZE
@ VVC_MAX_DPB_SIZE
Definition: vvc.h:109
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
pred_weight_table
static int FUNC() pred_weight_table(CodedBitstreamContext *ctx, RWContext *rw, const H266RawSPS *sps, const H266RawPPS *pps, const H266RefPicLists *ref_lists, uint8_t num_ref_idx_active[2], H266RawPredWeightTable *current)
Definition: cbs_h266_syntax_template.c:2514
nal_unit_header
static int FUNC() nal_unit_header(CodedBitstreamContext *ctx, RWContext *rw, H266RawNALUnitHeader *current, int expected_nal_unit_type)
Definition: cbs_h266_syntax_template.c:30
CodedBitstreamH266Context::sps
H266RawSPS * sps[VVC_MAX_SPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:873
VVC_SUFFIX_SEI_NUT
@ VVC_SUFFIX_SEI_NUT
Definition: vvc.h:53
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
VVC_IDR_N_LP
@ VVC_IDR_N_LP
Definition: vvc.h:37
general_timing_hrd_parameters
static int FUNC() general_timing_hrd_parameters(CodedBitstreamContext *ctx, RWContext *rw, H266RawGeneralTimingHrdParameters *current)
Definition: cbs_h266_syntax_template.c:558
VVC_PPS_NUT
@ VVC_PPS_NUT
Definition: vvc.h:45
SEIMessageState
Definition: cbs_sei.h:86
VVC_MAX_VPS_COUNT
@ VVC_MAX_VPS_COUNT
Definition: vvc.h:95
VVC_PREFIX_APS_NUT
@ VVC_PREFIX_APS_NUT
Definition: vvc.h:46
H266RawPH
Definition: cbs_h266.h:764
ref_pic_lists
static int FUNC() ref_pic_lists(CodedBitstreamContext *ctx, RWContext *rw, const H266RawSPS *sps, const H266RawPPS *pps, H266RefPicLists *current)
Definition: cbs_h266_syntax_template.c:491
FUNC
#define FUNC(a)
Definition: bit_depth_template.c:104
VVC_MAX_PPS_COUNT
@ VVC_MAX_PPS_COUNT
Definition: vvc.h:99
VVC_SUFFIX_APS_NUT
@ VVC_SUFFIX_APS_NUT
Definition: vvc.h:47
VVC_ASP_TYPE_SCALING
@ VVC_ASP_TYPE_SCALING
Definition: vvc.h:72
H266RawOlsTimingHrdParameters
Definition: cbs_h266.h:202
VVC_CRA_NUT
@ VVC_CRA_NUT
Definition: vvc.h:38
dci
static int FUNC() dci(CodedBitstreamContext *ctx, RWContext *rw, H266RawDCI *current)
Definition: cbs_h266_syntax_template.c:670
VVC_OPI_NUT
@ VVC_OPI_NUT
Definition: vvc.h:41
extension_data
static int FUNC() extension_data(CodedBitstreamContext *ctx, RWContext *rw, H266RawExtensionData *current)
Definition: cbs_h266_syntax_template.c:367
VVC_MAX_TILE_ROWS
@ VVC_MAX_TILE_ROWS
Definition: vvc.h:139
H266RawSEIDecodedPictureHash::dph_sei_single_component_flag
uint8_t dph_sei_single_component_flag
Definition: cbs_h266.h:853
fixed
#define fixed(width, name, value)
Definition: cbs_av1.c:487
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
H266RawNALUnitHeader
Definition: cbs_h266.h:29
ues
#define ues(name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:268
H266RawSEIDecodedPictureHash
Definition: cbs_h266.h:851
xu
#define xu(width, name, var, range_min, range_max, subs,...)
Definition: cbs_h2645.c:405
vui_payload
static int FUNC() vui_payload(CodedBitstreamContext *ctx, RWContext *rw, H266RawVUI *current, uint16_t vui_payload_size, uint8_t chroma_format_idc)
Definition: cbs_h266_syntax_template.c:346
RWContext
#define RWContext
Definition: cbs_av1.c:591
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H266RawSPS *current)
Definition: cbs_h266_syntax_template.c:1056
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1283
cbs_h2645_read_more_rbsp_data
static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
Definition: cbs_h2645.c:327
picture_header
static int FUNC() picture_header(CodedBitstreamContext *ctx, RWContext *rw, H266RawPictureHeader *current)
Definition: cbs_h266_syntax_template.c:2597