FFmpeg
vvc_ps.c
Go to the documentation of this file.
1 /*
2  * VVC parameter set parser
3  *
4  * Copyright (C) 2023 Nuo Mi
5  * Copyright (C) 2022 Xu Mu
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 #include "libavcodec/cbs_h266.h"
24 #include "libavutil/imgutils.h"
25 #include "libavcodec/refstruct.h"
26 #include "vvc_data.h"
27 #include "vvc_ps.h"
28 #include "vvcdec.h"
29 
30 static int sps_map_pixel_format(VVCSPS *sps, void *log_ctx)
31 {
32  const H266RawSPS *r = sps->r;
33  const AVPixFmtDescriptor *desc;
34 
35  switch (sps->bit_depth) {
36  case 8:
37  if (r->sps_chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
38  if (r->sps_chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
39  if (r->sps_chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
40  if (r->sps_chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
41  break;
42  case 10:
43  if (r->sps_chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
44  if (r->sps_chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
45  if (r->sps_chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
46  if (r->sps_chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
47  break;
48  case 12:
49  if (r->sps_chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
50  if (r->sps_chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
51  if (r->sps_chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
52  if (r->sps_chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
53  break;
54  default:
55  av_log(log_ctx, AV_LOG_ERROR,
56  "The following bit-depths are currently specified: 8, 10, 12 bits, "
57  "chroma_format_idc is %d, depth is %d\n",
58  r->sps_chroma_format_idc, sps->bit_depth);
59  return AVERROR_INVALIDDATA;
60  }
61 
62  desc = av_pix_fmt_desc_get(sps->pix_fmt);
63  if (!desc)
64  return AVERROR(EINVAL);
65 
66  sps->hshift[0] = sps->vshift[0] = 0;
67  sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
68  sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
69 
70  sps->pixel_shift = sps->bit_depth > 8;
71 
72  return 0;
73 }
74 
75 static int sps_bit_depth(VVCSPS *sps, void *log_ctx)
76 {
77  const H266RawSPS *r = sps->r;
78 
79  sps->bit_depth = r->sps_bitdepth_minus8 + 8;
80  sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
81  sps->log2_transform_range =
82  r->sps_extended_precision_flag ? FFMAX(15, FFMIN(20, sps->bit_depth + 6)) : 15;
83  return sps_map_pixel_format(sps, log_ctx);
84 }
85 
87 {
88  const H266RawSPS *r = sps->r;
89  const int num_qp_tables = r->sps_same_qp_table_for_chroma_flag ?
90  1 : (r->sps_joint_cbcr_enabled_flag ? 3 : 2);
91 
92  for (int i = 0; i < num_qp_tables; i++) {
93  int num_points_in_qp_table;
95  unsigned int delta_qp_in[VVC_MAX_POINTS_IN_QP_TABLE];
96  int off = sps->qp_bd_offset;
97 
98  num_points_in_qp_table = r->sps_num_points_in_qp_table_minus1[i] + 1;
99 
100  qp_out[0] = qp_in[0] = r->sps_qp_table_start_minus26[i] + 26;
101  for (int j = 0; j < num_points_in_qp_table; j++ ) {
102  delta_qp_in[j] = r->sps_delta_qp_in_val_minus1[i][j] + 1;
103  qp_in[j+1] = qp_in[j] + delta_qp_in[j];
104  qp_out[j+1] = qp_out[j] + (r->sps_delta_qp_in_val_minus1[i][j] ^ r->sps_delta_qp_diff_val[i][j]);
105  }
106  sps->chroma_qp_table[i][qp_in[0] + off] = qp_out[0];
107  for (int k = qp_in[0] - 1 + off; k >= 0; k--)
108  sps->chroma_qp_table[i][k] = av_clip(sps->chroma_qp_table[i][k+1]-1, -off, 63);
109 
110  for (int j = 0; j < num_points_in_qp_table; j++) {
111  int sh = delta_qp_in[j] >> 1;
112  for (int k = qp_in[j] + 1 + off, m = 1; k <= qp_in[j+1] + off; k++, m++) {
113  sps->chroma_qp_table[i][k] = sps->chroma_qp_table[i][qp_in[j] + off] +
114  ((qp_out[j+1] - qp_out[j]) * m + sh) / delta_qp_in[j];
115  }
116  }
117  for (int k = qp_in[num_points_in_qp_table] + 1 + off; k <= 63 + off; k++)
118  sps->chroma_qp_table[i][k] = av_clip(sps->chroma_qp_table[i][k-1] + 1, -sps->qp_bd_offset, 63);
119  }
120  if (r->sps_same_qp_table_for_chroma_flag) {
121  memcpy(&sps->chroma_qp_table[1], &sps->chroma_qp_table[0], sizeof(sps->chroma_qp_table[0]));
122  memcpy(&sps->chroma_qp_table[2], &sps->chroma_qp_table[0], sizeof(sps->chroma_qp_table[0]));
123  }
124 
125  return 0;
126 }
127 
128 static void sps_poc(VVCSPS *sps)
129 {
130  sps->max_pic_order_cnt_lsb = 1 << (sps->r->sps_log2_max_pic_order_cnt_lsb_minus4 + 4);
131 }
132 
133 static void sps_inter(VVCSPS *sps)
134 {
135  const H266RawSPS *r = sps->r;
136 
137  sps->max_num_merge_cand = 6 - r->sps_six_minus_max_num_merge_cand;
138  sps->max_num_ibc_merge_cand = 6 - r->sps_six_minus_max_num_ibc_merge_cand;
139 
140  if (sps->r->sps_gpm_enabled_flag) {
141  sps->max_num_gpm_merge_cand = 2;
142  if (sps->max_num_merge_cand >= 3)
143  sps->max_num_gpm_merge_cand = sps->max_num_merge_cand - r->sps_max_num_merge_cand_minus_max_num_gpm_cand;
144  }
145 
146  sps->log2_parallel_merge_level = r->sps_log2_parallel_merge_level_minus2 + 2;
147 }
148 
150 {
151  const H266RawSPS *r = sps->r;
152 
153  sps->ctb_log2_size_y = r->sps_log2_ctu_size_minus5 + 5;
154  sps->ctb_size_y = 1 << sps->ctb_log2_size_y;
155  sps->min_cb_log2_size_y = r->sps_log2_min_luma_coding_block_size_minus2 + 2;
156  sps->min_cb_size_y = 1 << sps->min_cb_log2_size_y;
157  sps->max_tb_size_y = 1 << (r->sps_max_luma_transform_size_64_flag ? 6 : 5);
158  sps->max_ts_size = 1 << (r->sps_log2_transform_skip_max_size_minus2 + 2);
159 }
160 
161 static void sps_ladf(VVCSPS* sps)
162 {
163  const H266RawSPS *r = sps->r;
164 
165  if (r->sps_ladf_enabled_flag) {
166  sps->num_ladf_intervals = r->sps_num_ladf_intervals_minus2 + 2;
167  sps->ladf_interval_lower_bound[0] = 0;
168  for (int i = 0; i < sps->num_ladf_intervals - 1; i++) {
169  sps->ladf_interval_lower_bound[i + 1] =
170  sps->ladf_interval_lower_bound[i] + r->sps_ladf_delta_threshold_minus1[i] + 1;
171  }
172  }
173 }
174 
175 static int sps_derive(VVCSPS *sps, void *log_ctx)
176 {
177  int ret;
178  const H266RawSPS *r = sps->r;
179 
180  ret = sps_bit_depth(sps, log_ctx);
181  if (ret < 0)
182  return ret;
183  sps_poc(sps);
184  sps_inter(sps);
186  sps_ladf(sps);
187  if (r->sps_chroma_format_idc != 0)
189 
190  return 0;
191 }
192 
193 static void sps_free(FFRefStructOpaque opaque, void *obj)
194 {
195  VVCSPS *sps = obj;
196  ff_refstruct_unref(&sps->r);
197 }
198 
199 static const VVCSPS *sps_alloc(const H266RawSPS *rsps, void *log_ctx)
200 {
201  int ret;
202  VVCSPS *sps = ff_refstruct_alloc_ext(sizeof(*sps), 0, NULL, sps_free);
203 
204  if (!sps)
205  return NULL;
206 
207  ff_refstruct_replace(&sps->r, rsps);
208 
209  ret = sps_derive(sps, log_ctx);
210  if (ret < 0)
211  goto fail;
212 
213  return sps;
214 
215 fail:
217  return NULL;
218 }
219 
220 static int decode_sps(VVCParamSets *ps, const H266RawSPS *rsps, void *log_ctx)
221 {
222  const int sps_id = rsps->sps_seq_parameter_set_id;
223  const VVCSPS *old_sps = ps->sps_list[sps_id];
224  const VVCSPS *sps;
225 
226  if (old_sps && old_sps->r == rsps)
227  return 0;
228 
229  sps = sps_alloc(rsps, log_ctx);
230  if (!sps)
231  return AVERROR(ENOMEM);
232 
233  ff_refstruct_unref(&ps->sps_list[sps_id]);
234  ps->sps_list[sps_id] = sps;
235 
236  return 0;
237 }
238 
240 {
241  pps->chroma_qp_offset[CB - 1] = pps->r->pps_cb_qp_offset;
242  pps->chroma_qp_offset[CR - 1] = pps->r->pps_cr_qp_offset;
243  pps->chroma_qp_offset[JCBCR - 1]= pps->r->pps_joint_cbcr_qp_offset_value;
244  for (int i = 0; i < 6; i++) {
245  pps->chroma_qp_offset_list[i][CB - 1] = pps->r->pps_cb_qp_offset_list[i];
246  pps->chroma_qp_offset_list[i][CR - 1] = pps->r->pps_cr_qp_offset_list[i];
247  pps->chroma_qp_offset_list[i][JCBCR - 1]= pps->r->pps_joint_cbcr_qp_offset_list[i];
248  }
249 }
250 
251 static void pps_width_height(VVCPPS *pps, const VVCSPS *sps)
252 {
253  const H266RawPPS *r = pps->r;
254 
255  pps->width = r->pps_pic_width_in_luma_samples;
256  pps->height = r->pps_pic_height_in_luma_samples;
257 
258  pps->ctb_width = AV_CEIL_RSHIFT(pps->width, sps->ctb_log2_size_y);
259  pps->ctb_height = AV_CEIL_RSHIFT(pps->height, sps->ctb_log2_size_y);
260  pps->ctb_count = pps->ctb_width * pps->ctb_height;
261 
262  pps->min_cb_width = pps->width >> sps->min_cb_log2_size_y;
263  pps->min_cb_height = pps->height >> sps->min_cb_log2_size_y;
264 
265  pps->min_pu_width = pps->width >> MIN_PU_LOG2;
266  pps->min_pu_height = pps->height >> MIN_PU_LOG2;
267  pps->min_tu_width = pps->width >> MIN_TU_LOG2;
268  pps->min_tu_height = pps->height >> MIN_TU_LOG2;
269 
270  pps->width32 = AV_CEIL_RSHIFT(pps->width, 5);
271  pps->height32 = AV_CEIL_RSHIFT(pps->height, 5);
272  pps->width64 = AV_CEIL_RSHIFT(pps->width, 6);
273  pps->height64 = AV_CEIL_RSHIFT(pps->height, 6);
274 }
275 
276 static int pps_bd(VVCPPS *pps)
277 {
278  const H266RawPPS *r = pps->r;
279 
280  pps->col_bd = av_calloc(r->num_tile_columns + 1, sizeof(*pps->col_bd));
281  pps->row_bd = av_calloc(r->num_tile_rows + 1, sizeof(*pps->row_bd));
282  pps->ctb_to_col_bd = av_calloc(pps->ctb_width + 1, sizeof(*pps->ctb_to_col_bd));
283  pps->ctb_to_row_bd = av_calloc(pps->ctb_height + 1, sizeof(*pps->ctb_to_col_bd));
284  if (!pps->col_bd || !pps->row_bd || !pps->ctb_to_col_bd || !pps->ctb_to_row_bd)
285  return AVERROR(ENOMEM);
286 
287  for (int i = 0, j = 0; i < r->num_tile_columns; i++) {
288  pps->col_bd[i] = j;
289  j += r->col_width_val[i];
290  for (int k = pps->col_bd[i]; k < j; k++)
291  pps->ctb_to_col_bd[k] = pps->col_bd[i];
292  }
293 
294  for (int i = 0, j = 0; i < r->num_tile_rows; i++) {
295  pps->row_bd[i] = j;
296  j += r->row_height_val[i];
297  for (int k = pps->row_bd[i]; k < j; k++)
298  pps->ctb_to_row_bd[k] = pps->row_bd[i];
299  }
300  return 0;
301 }
302 
303 
304 static int next_tile_idx(int tile_idx, const int i, const H266RawPPS *r)
305 {
306  if (r->pps_tile_idx_delta_present_flag) {
307  tile_idx += r->pps_tile_idx_delta_val[i];
308  } else {
309  tile_idx += r->pps_slice_width_in_tiles_minus1[i] + 1;
310  if (tile_idx % r->num_tile_columns == 0)
311  tile_idx += (r->pps_slice_height_in_tiles_minus1[i]) * r->num_tile_columns;
312  }
313  return tile_idx;
314 }
315 
316 static void tile_xy(int *tile_x, int *tile_y, const int tile_idx, const VVCPPS *pps)
317 {
318  *tile_x = tile_idx % pps->r->num_tile_columns;
319  *tile_y = tile_idx / pps->r->num_tile_columns;
320 }
321 
322 static void ctu_xy(int *ctu_x, int *ctu_y, const int tile_x, const int tile_y, const VVCPPS *pps)
323 {
324  *ctu_x = pps->col_bd[tile_x];
325  *ctu_y = pps->row_bd[tile_y];
326 }
327 
328 static int ctu_rs(const int ctu_x, const int ctu_y, const VVCPPS *pps)
329 {
330  return pps->ctb_width * ctu_y + ctu_x;
331 }
332 
333 static int pps_add_ctus(VVCPPS *pps, int *off, const int ctu_x, const int ctu_y,
334  const int w, const int h)
335 {
336  int start = *off;
337  for (int y = 0; y < h; y++) {
338  for (int x = 0; x < w; x++) {
339  pps->ctb_addr_in_slice[*off] = ctu_rs(ctu_x + x, ctu_y + y, pps);
340  (*off)++;
341  }
342  }
343  return *off - start;
344 }
345 
346 static int pps_one_tile_slices(VVCPPS *pps, const int tile_idx, int i, int *off)
347 {
348  const H266RawPPS *r = pps->r;
349  int ctu_x, ctu_y, ctu_y_end, tile_x, tile_y;
350 
351  tile_xy(&tile_x, &tile_y, tile_idx, pps);
352  ctu_xy(&ctu_x, &ctu_y, tile_x, tile_y, pps);
353  ctu_y_end = ctu_y + r->row_height_val[tile_y];
354  while (ctu_y < ctu_y_end) {
355  pps->slice_start_offset[i] = *off;
356  pps->num_ctus_in_slice[i] = pps_add_ctus(pps, off, ctu_x, ctu_y,
357  r->col_width_val[tile_x], r->slice_height_in_ctus[i]);
358  ctu_y += r->slice_height_in_ctus[i++];
359  }
360  i--;
361  return i;
362 }
363 
364 static void pps_multi_tiles_slice(VVCPPS *pps, const int tile_idx, const int i, int *off)
365 {
366  const H266RawPPS *r = pps->r;
367  int ctu_x, ctu_y,tile_x, tile_y;
368 
369  tile_xy(&tile_x, &tile_y, tile_idx, pps);
370  pps->slice_start_offset[i] = *off;
371  pps->num_ctus_in_slice[i] = 0;
372  for (int ty = tile_y; ty <= tile_y + r->pps_slice_height_in_tiles_minus1[i]; ty++) {
373  for (int tx = tile_x; tx <= tile_x + r->pps_slice_width_in_tiles_minus1[i]; tx++) {
374  ctu_xy(&ctu_x, &ctu_y, tx, ty, pps);
375  pps->num_ctus_in_slice[i] += pps_add_ctus(pps, off, ctu_x, ctu_y,
376  r->col_width_val[tx], r->row_height_val[ty]);
377  }
378  }
379 }
380 
381 static void pps_rect_slice(VVCPPS* pps)
382 {
383  const H266RawPPS* r = pps->r;
384  int tile_idx = 0, off = 0;
385 
386  for (int i = 0; i < r->pps_num_slices_in_pic_minus1 + 1; i++) {
387  if (!r->pps_slice_width_in_tiles_minus1[i] &&
388  !r->pps_slice_height_in_tiles_minus1[i]) {
389  i = pps_one_tile_slices(pps, tile_idx, i, &off);
390  } else {
391  pps_multi_tiles_slice(pps, tile_idx, i, &off);
392 
393  }
394  tile_idx = next_tile_idx(tile_idx, i, r);
395  }
396 }
397 
399 {
400  const H266RawPPS* r = pps->r;
401  int ctu_x, ctu_y, off = 0;
402 
403  for (int tile_y = 0; tile_y < r->num_tile_rows; tile_y++) {
404  for (int tile_x = 0; tile_x < r->num_tile_columns; tile_x++) {
405  ctu_xy(&ctu_x, &ctu_y, tile_x, tile_y, pps);
406  pps_add_ctus(pps, &off, ctu_x, ctu_y, r->col_width_val[tile_x], r->row_height_val[tile_y]);
407  }
408  }
409 }
410 
412 {
413  pps->ctb_addr_in_slice = av_calloc(pps->ctb_count, sizeof(*pps->ctb_addr_in_slice));
414  if (!pps->ctb_addr_in_slice)
415  return AVERROR(ENOMEM);
416 
417  if (pps->r->pps_rect_slice_flag)
419  else
421 
422  return 0;
423 }
424 
426 {
427  const H266RawPPS *r = pps->r;
428 
429  if (r->pps_ref_wraparound_enabled_flag)
430  pps->ref_wraparound_offset = (pps->width / sps->min_cb_size_y) - r->pps_pic_width_minus_wraparound_offset;
431 }
432 
433 static int pps_derive(VVCPPS *pps, const VVCSPS *sps)
434 {
435  int ret;
436 
439 
440  ret = pps_bd(pps);
441  if (ret < 0)
442  return ret;
443 
444  ret = pps_slice_map(pps);
445  if (ret < 0)
446  return ret;
447 
449 
450  return 0;
451 }
452 
453 static void pps_free(FFRefStructOpaque opaque, void *obj)
454 {
455  VVCPPS *pps = obj;
456 
457  ff_refstruct_unref(&pps->r);
458 
459  av_freep(&pps->col_bd);
460  av_freep(&pps->row_bd);
461  av_freep(&pps->ctb_to_col_bd);
462  av_freep(&pps->ctb_to_row_bd);
463  av_freep(&pps->ctb_addr_in_slice);
464 }
465 
466 static const VVCPPS *pps_alloc(const H266RawPPS *rpps, const VVCSPS *sps)
467 {
468  int ret;
469  VVCPPS *pps = ff_refstruct_alloc_ext(sizeof(*pps), 0, NULL, pps_free);
470 
471  if (!pps)
472  return NULL;
473 
474  ff_refstruct_replace(&pps->r, rpps);
475 
476  ret = pps_derive(pps, sps);
477  if (ret < 0)
478  goto fail;
479 
480  return pps;
481 
482 fail:
484  return NULL;
485 }
486 
487 static int decode_pps(VVCParamSets *ps, const H266RawPPS *rpps)
488 {
489  int ret = 0;
490  const int pps_id = rpps->pps_pic_parameter_set_id;
491  const int sps_id = rpps->pps_seq_parameter_set_id;
492  const VVCPPS *old_pps = ps->pps_list[pps_id];
493  const VVCPPS *pps;
494 
495  if (old_pps && old_pps->r == rpps)
496  return 0;
497 
498  pps = pps_alloc(rpps, ps->sps_list[sps_id]);
499  if (!pps)
500  return AVERROR(ENOMEM);
501 
502  ff_refstruct_unref(&ps->pps_list[pps_id]);
503  ps->pps_list[pps_id] = pps;
504 
505  return ret;
506 }
507 
508 static int decode_ps(VVCParamSets *ps, const CodedBitstreamH266Context *h266, void *log_ctx)
509 {
510  const H266RawPictureHeader *ph = h266->ph;
511  const H266RawPPS *rpps;
512  const H266RawSPS *rsps;
513  int ret;
514 
515  if (!ph)
516  return AVERROR_INVALIDDATA;
517 
518  rpps = h266->pps[ph->ph_pic_parameter_set_id];
519  if (!rpps)
520  return AVERROR_INVALIDDATA;
521 
522  rsps = h266->sps[rpps->pps_seq_parameter_set_id];
523  if (!rsps)
524  return AVERROR_INVALIDDATA;
525 
526  ret = decode_sps(ps, rsps, log_ctx);
527  if (ret < 0)
528  return ret;
529 
530  ret = decode_pps(ps, rpps);
531  if (ret < 0)
532  return ret;
533 
534  return 0;
535 }
536 
537 #define WEIGHT_TABLE(x) \
538  w->nb_weights[L##x] = r->num_weights_l##x; \
539  for (int i = 0; i < w->nb_weights[L##x]; i++) { \
540  w->weight_flag[L##x][LUMA][i] = r->luma_weight_l##x##_flag[i]; \
541  w->weight_flag[L##x][CHROMA][i] = r->chroma_weight_l##x##_flag[i]; \
542  w->weight[L##x][LUMA][i] = denom[LUMA] + r->delta_luma_weight_l##x[i]; \
543  w->offset[L##x][LUMA][i] = r->luma_offset_l##x[i]; \
544  for (int j = CB; j <= CR; j++) { \
545  w->weight[L##x][j][i] = denom[CHROMA] + r->delta_chroma_weight_l##x[i][j - 1]; \
546  w->offset[L##x][j][i] = 128 + r->delta_chroma_offset_l##x[i][j - 1]; \
547  w->offset[L##x][j][i] -= (128 * w->weight[L##x][j][i]) >> w->log2_denom[CHROMA]; \
548  w->offset[L##x][j][i] = av_clip_intp2(w->offset[L##x][j][i], 7); \
549  } \
550  } \
551 
553 {
554  int denom[2];
555 
556  w->log2_denom[LUMA] = r->luma_log2_weight_denom;
557  w->log2_denom[CHROMA] = w->log2_denom[LUMA] + r->delta_chroma_log2_weight_denom;
558  denom[LUMA] = 1 << w->log2_denom[LUMA];
559  denom[CHROMA] = 1 << w->log2_denom[CHROMA];
560  WEIGHT_TABLE(0)
561  WEIGHT_TABLE(1)
562 }
563 
564 // 8.3.1 Decoding process for picture order count
565 static int ph_compute_poc(const H266RawPictureHeader *ph, const H266RawSPS *sps, const int poc_tid0, const int is_clvss)
566 {
567  const int max_poc_lsb = 1 << (sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4);
568  const int prev_poc_lsb = poc_tid0 % max_poc_lsb;
569  const int prev_poc_msb = poc_tid0 - prev_poc_lsb;
570  const int poc_lsb = ph->ph_pic_order_cnt_lsb;
571  int poc_msb;
572 
573  if (ph->ph_poc_msb_cycle_present_flag) {
574  poc_msb = ph->ph_poc_msb_cycle_val * max_poc_lsb;
575  } else if (is_clvss) {
576  poc_msb = 0;
577  } else {
578  if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
579  poc_msb = prev_poc_msb + max_poc_lsb;
580  else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
581  poc_msb = prev_poc_msb - max_poc_lsb;
582  else
583  poc_msb = prev_poc_msb;
584  }
585 
586  return poc_msb + poc_lsb;
587 }
588 
590  uint16_t *pivot1, uint16_t *pivot2, uint16_t *scale_coeff, const int idx, const int max)
591 {
592  const int lut_sample =
593  pivot1[idx] + ((scale_coeff[idx] * (sample - pivot2[idx]) + (1<< 10)) >> 11);
594  return av_clip(lut_sample, 0, max - 1);
595 }
596 
597 //8.8.2.2 Inverse mapping process for a luma sample
598 static int lmcs_derive_lut(VVCLMCS *lmcs, const H266RawAPS *rlmcs, const H266RawSPS *sps)
599 {
600  const int bit_depth = (sps->sps_bitdepth_minus8 + 8);
601  const int max = (1 << bit_depth);
602  const int org_cw = max / LMCS_MAX_BIN_SIZE;
603  const int shift = av_log2(org_cw);
604  const int off = 1 << (shift - 1);
605  int cw[LMCS_MAX_BIN_SIZE];
606  uint16_t input_pivot[LMCS_MAX_BIN_SIZE];
607  uint16_t scale_coeff[LMCS_MAX_BIN_SIZE];
608  uint16_t inv_scale_coeff[LMCS_MAX_BIN_SIZE];
609  int i, delta_crs;
611  return AVERROR_PATCHWELCOME;
612 
613  if (!rlmcs)
614  return AVERROR_INVALIDDATA;
615 
616  lmcs->min_bin_idx = rlmcs->lmcs_min_bin_idx;
617  lmcs->max_bin_idx = LMCS_MAX_BIN_SIZE - 1 - rlmcs->lmcs_min_bin_idx;
618 
619  memset(cw, 0, sizeof(cw));
620  for (int i = lmcs->min_bin_idx; i <= lmcs->max_bin_idx; i++)
621  cw[i] = org_cw + (1 - 2 * rlmcs->lmcs_delta_sign_cw_flag[i]) * rlmcs->lmcs_delta_abs_cw[i];
622 
623  delta_crs = (1 - 2 * rlmcs->lmcs_delta_sign_crs_flag) * rlmcs->lmcs_delta_abs_crs;
624 
625  lmcs->pivot[0] = 0;
626  for (i = 0; i < LMCS_MAX_BIN_SIZE; i++) {
627  input_pivot[i] = i * org_cw;
628  lmcs->pivot[i + 1] = lmcs->pivot[i] + cw[i];
629  scale_coeff[i] = (cw[i] * (1 << 11) + off) >> shift;
630  if (cw[i] == 0) {
631  inv_scale_coeff[i] = 0;
632  lmcs->chroma_scale_coeff[i] = (1 << 11);
633  } else {
634  inv_scale_coeff[i] = org_cw * (1 << 11) / cw[i];
635  lmcs->chroma_scale_coeff[i] = org_cw * (1 << 11) / (cw[i] + delta_crs);
636  }
637  }
638 
639  //derive lmcs_fwd_lut
640  for (uint16_t sample = 0; sample < max; sample++) {
641  const int idx_y = sample / org_cw;
642  const uint16_t fwd_sample = lmcs_derive_lut_sample(sample, lmcs->pivot,
643  input_pivot, scale_coeff, idx_y, max);
644  if (bit_depth > 8)
645  lmcs->fwd_lut.u16[sample] = fwd_sample;
646  else
647  lmcs->fwd_lut.u8 [sample] = fwd_sample;
648 
649  }
650 
651  //derive lmcs_inv_lut
652  i = lmcs->min_bin_idx;
653  for (uint16_t sample = 0; sample < max; sample++) {
654  uint16_t inv_sample;
655  while (i <= lmcs->max_bin_idx && sample >= lmcs->pivot[i + 1])
656  i++;
657 
658  inv_sample = lmcs_derive_lut_sample(sample, input_pivot, lmcs->pivot,
659  inv_scale_coeff, i, max);
660 
661  if (bit_depth > 8)
662  lmcs->inv_lut.u16[sample] = inv_sample;
663  else
664  lmcs->inv_lut.u8 [sample] = inv_sample;
665  }
666 
667  return 0;
668 }
669 
671 {
672  if (sps->sps_affine_enabled_flag)
673  return 5 - sps->sps_five_minus_max_num_subblock_merge_cand;
674  return sps->sps_sbtmvp_enabled_flag && ph->ph_temporal_mvp_enabled_flag;
675 }
676 
677 static int ph_derive(VVCPH *ph, const H266RawSPS *sps, const H266RawPPS *pps, const int poc_tid0, const int is_clvss)
678 {
679  ph->max_num_subblock_merge_cand = ph_max_num_subblock_merge_cand(sps, ph->r);
680 
681  ph->poc = ph_compute_poc(ph->r, sps, poc_tid0, is_clvss);
682 
683  if (pps->pps_wp_info_in_ph_flag)
684  pred_weight_table(&ph->pwt, &ph->r->ph_pred_weight_table);
685 
686  return 0;
687 }
688 
689 static int decode_ph(VVCFrameParamSets *fps, const H266RawPictureHeader *rph, void *rph_ref,
690  const int poc_tid0, const int is_clvss)
691 {
692  int ret;
693  VVCPH *ph = &fps->ph;
694  const H266RawSPS *sps = fps->sps->r;
695  const H266RawPPS *pps = fps->pps->r;
696 
697  ph->r = rph;
698  ff_refstruct_replace(&ph->rref, rph_ref);
699  ret = ph_derive(ph, sps, pps, poc_tid0, is_clvss);
700  if (ret < 0)
701  return ret;
702 
703  return 0;
704 }
705 
706 static int decode_frame_ps(VVCFrameParamSets *fps, const VVCParamSets *ps,
707  const CodedBitstreamH266Context *h266, const int poc_tid0, const int is_clvss)
708 {
709  const H266RawPictureHeader *ph = h266->ph;
710  const H266RawPPS *rpps;
711  int ret;
712 
713  if (!ph)
714  return AVERROR_INVALIDDATA;
715 
716  rpps = h266->pps[ph->ph_pic_parameter_set_id];
717  if (!rpps)
718  return AVERROR_INVALIDDATA;
719 
722 
723  ret = decode_ph(fps, ph, h266->ph_ref, poc_tid0, is_clvss);
724  if (ret < 0)
725  return ret;
726 
727  if (ph->ph_explicit_scaling_list_enabled_flag)
728  ff_refstruct_replace(&fps->sl, ps->scaling_list[ph->ph_scaling_list_aps_id]);
729 
730  if (ph->ph_lmcs_enabled_flag) {
731  ret = lmcs_derive_lut(&fps->lmcs, ps->lmcs_list[ph->ph_lmcs_aps_id], fps->sps->r);
732  if (ret < 0)
733  return ret;
734  }
735 
736  for (int i = 0; i < FF_ARRAY_ELEMS(fps->alf_list); i++)
737  ff_refstruct_replace(&fps->alf_list[i], ps->alf_list[i]);
738 
739  return 0;
740 }
741 
743 {
744  if (IS_IDR(s))
745  s->no_output_before_recovery_flag = 1;
746  else if (IS_CRA(s) || IS_GDR(s))
747  s->no_output_before_recovery_flag = s->last_eos;
748 }
749 
750 static void decode_recovery_poc(VVCContext *s, const VVCPH *ph)
751 {
752  if (s->no_output_before_recovery_flag) {
753  if (IS_GDR(s))
754  s->gdr_recovery_point_poc = ph->poc + ph->r->ph_recovery_poc_cnt;
755  if (!GDR_IS_RECOVERED(s) && s->gdr_recovery_point_poc <= ph->poc)
757  }
758 }
759 
761 {
762  int ret = 0;
763  VVCParamSets *ps = &s->ps;
764  const CodedBitstreamH266Context *h266 = s->cbc->priv_data;
765 
766  ret = decode_ps(ps, h266, s->avctx);
767  if (ret < 0)
768  return ret;
769 
771  ret = decode_frame_ps(fps, ps, h266, s->poc_tid0, IS_CLVSS(s));
772  decode_recovery_poc(s, &fps->ph);
773  return ret;
774 }
775 
777 {
778  ff_refstruct_unref(&fps->sps);
779  ff_refstruct_unref(&fps->pps);
780  ff_refstruct_unref(&fps->ph.rref);
781  ff_refstruct_unref(&fps->sl);
782  for (int i = 0; i < FF_ARRAY_ELEMS(fps->alf_list); i++)
783  ff_refstruct_unref(&fps->alf_list[i]);
784 }
785 
787 {
788  for (int i = 0; i < FF_ARRAY_ELEMS(ps->scaling_list); i++)
790  for (int i = 0; i < FF_ARRAY_ELEMS(ps->lmcs_list); i++)
792  for (int i = 0; i < FF_ARRAY_ELEMS(ps->alf_list); i++)
794  for (int i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
796  for (int i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
798 }
799 
800 static void alf_coeff(int16_t *coeff,
801  const uint8_t *abs, const uint8_t *sign, const int size)
802 {
803  for (int i = 0; i < size; i++)
804  coeff[i] = (1 - 2 * sign[i]) * abs[i];
805 }
806 
807 static void alf_coeff_cc(int16_t *coeff,
808  const uint8_t *mapped_abs, const uint8_t *sign)
809 {
810  for (int i = 0; i < ALF_NUM_COEFF_CC; i++) {
811  int c = mapped_abs[i];
812  if (c)
813  c = (1 - 2 * sign[i]) * (1 << (c - 1));
814  coeff[i] = c;
815  }
816 }
817 
818 static void alf_luma(VVCALF *alf, const H266RawAPS *aps)
819 {
820  if (!aps->alf_luma_filter_signal_flag)
821  return;
822 
823  for (int i = 0; i < ALF_NUM_FILTERS_LUMA; i++) {
824  const int ref = aps->alf_luma_coeff_delta_idx[i];
825  const uint8_t *abs = aps->alf_luma_coeff_abs[ref];
826  const uint8_t *sign = aps->alf_luma_coeff_sign[ref];
827 
829  memcpy(alf->luma_clip_idx[i], aps->alf_luma_clip_idx[ref],
830  sizeof(alf->luma_clip_idx[i]));
831  }
832 }
833 
834 static void alf_chroma(VVCALF *alf, const H266RawAPS *aps)
835 {
836  if (!aps->alf_chroma_filter_signal_flag)
837  return;
838 
839  alf->num_chroma_filters = aps->alf_chroma_num_alt_filters_minus1 + 1;
840  for (int i = 0; i < alf->num_chroma_filters; i++) {
841  const uint8_t *abs = aps->alf_chroma_coeff_abs[i];
842  const uint8_t *sign = aps->alf_chroma_coeff_sign[i];
843 
845  memcpy(alf->chroma_clip_idx[i], aps->alf_chroma_clip_idx[i],
846  sizeof(alf->chroma_clip_idx[i]));
847  }
848 }
849 
850 static void alf_cc(VVCALF *alf, const H266RawAPS *aps)
851 {
852  const uint8_t (*abs[])[ALF_NUM_COEFF_CC] =
853  { aps->alf_cc_cb_mapped_coeff_abs, aps->alf_cc_cr_mapped_coeff_abs };
854  const uint8_t (*sign[])[ALF_NUM_COEFF_CC] =
855  {aps->alf_cc_cb_coeff_sign, aps->alf_cc_cr_coeff_sign };
856  const int signaled[] = { aps->alf_cc_cb_filter_signal_flag, aps->alf_cc_cr_filter_signal_flag};
857 
858  alf->num_cc_filters[0] = aps->alf_cc_cb_filters_signalled_minus1 + 1;
859  alf->num_cc_filters[1] = aps->alf_cc_cr_filters_signalled_minus1 + 1;
860 
861  for (int idx = 0; idx < 2; idx++) {
862  if (signaled[idx]) {
863  for (int i = 0; i < alf->num_cc_filters[idx]; i++)
864  alf_coeff_cc(alf->cc_coeff[idx][i], abs[idx][i], sign[idx][i]);
865  }
866  }
867 }
868 
869 static void alf_derive(VVCALF *alf, const H266RawAPS *aps)
870 {
871  alf_luma(alf, aps);
872  alf_chroma(alf, aps);
873  alf_cc(alf, aps);
874 }
875 
876 static int aps_decode_alf(const VVCALF **alf, const H266RawAPS *aps)
877 {
878  VVCALF *a = ff_refstruct_allocz(sizeof(*a));
879  if (!a)
880  return AVERROR(ENOMEM);
881 
882  alf_derive(a, aps);
883  ff_refstruct_replace(alf, a);
885 
886  return 0;
887 }
888 
889 static int is_luma_list(const int id)
890 {
891  return id % VVC_MAX_SAMPLE_ARRAYS == SL_START_4x4 || id == SL_START_64x64 + 1;
892 }
893 
894 static int derive_matrix_size(const int id)
895 {
896  return id < SL_START_4x4 ? 2 : (id < SL_START_8x8 ? 4 : 8);
897 }
898 
899 // 7.4.3.20 Scaling list data semantics
900 static void scaling_derive(VVCScalingList *sl, const H266RawAPS *aps)
901 {
902  for (int id = 0; id < SL_MAX_ID; id++) {
903  const int matrix_size = derive_matrix_size(id);
904  const int log2_size = av_log2(matrix_size);
905  const int list_size = matrix_size * matrix_size;
907  const uint8_t *pred;
908  const int *scaling_list;
909  int dc = 0;
910 
911  if (aps->aps_chroma_present_flag || is_luma_list(id)) {
912  if (!aps->scaling_list_copy_mode_flag[id]) {
913  int next_coef = 0;
914 
915  if (id >= SL_START_16x16)
916  dc = next_coef = aps->scaling_list_dc_coef[id - SL_START_16x16];
917 
918  for (int i = 0; i < list_size; i++) {
919  const int x = ff_vvc_diag_scan_x[3][3][i];
920  const int y = ff_vvc_diag_scan_y[3][3][i];
921 
922  if (!(id >= SL_START_64x64 && x >= 4 && y >= 4))
923  next_coef += aps->scaling_list_delta_coef[id][i];
924  coeff[i] = next_coef;
925  }
926  }
927  }
928 
929  //dc
930  if (id >= SL_START_16x16) {
931  if (!aps->scaling_list_copy_mode_flag[id] && !aps->scaling_list_pred_mode_flag[id]) {
933  } else if (!aps->scaling_list_pred_id_delta[id]) {
934  sl->scaling_matrix_dc_rec[id - SL_START_16x16] = 16;
935  } else {
936  const int ref_id = id - aps->scaling_list_pred_id_delta[id];
937  if (ref_id >= SL_START_16x16)
938  dc += sl->scaling_matrix_dc_rec[ref_id - SL_START_16x16];
939  else
940  dc += sl->scaling_matrix_rec[ref_id][0];
941  sl->scaling_matrix_dc_rec[id - SL_START_16x16] = dc & 255;
942  }
943  }
944 
945  //ac
946  scaling_list = aps->scaling_list_copy_mode_flag[id] ? ff_vvc_scaling_list0 : coeff;
947  if (!aps->scaling_list_copy_mode_flag[id] && !aps->scaling_list_pred_mode_flag[id])
949  else if (!aps->scaling_list_pred_id_delta[id])
951  else
952  pred = sl->scaling_matrix_rec[id - aps->scaling_list_pred_id_delta[id]];
953  for (int i = 0; i < list_size; i++) {
954  const int x = ff_vvc_diag_scan_x[log2_size][log2_size][i];
955  const int y = ff_vvc_diag_scan_y[log2_size][log2_size][i];
956  const int off = y * matrix_size + x;
957  sl->scaling_matrix_rec[id][off] = (pred[off] + scaling_list[i]) & 255;
958  }
959  }
960 }
961 
962 static int aps_decode_scaling(const VVCScalingList **scaling, const H266RawAPS *aps)
963 {
964  VVCScalingList *sl = ff_refstruct_allocz(sizeof(*sl));
965  if (!sl)
966  return AVERROR(ENOMEM);
967 
968  scaling_derive(sl, aps);
969  ff_refstruct_replace(scaling, sl);
970  ff_refstruct_unref(&sl);
971 
972  return 0;
973 }
974 
976 {
977  const H266RawAPS *aps = unit->content_ref;
978  int ret = 0;
979 
980  if (!aps)
981  return AVERROR_INVALIDDATA;
982 
983  switch (aps->aps_params_type) {
984  case VVC_ASP_TYPE_ALF:
985  ret = aps_decode_alf(&ps->alf_list[aps->aps_adaptation_parameter_set_id], aps);
986  break;
987  case VVC_ASP_TYPE_LMCS:
988  ff_refstruct_replace(&ps->lmcs_list[aps->aps_adaptation_parameter_set_id], aps);
989  break;
991  ret = aps_decode_scaling(&ps->scaling_list[aps->aps_adaptation_parameter_set_id], aps);
992  break;
993  }
994 
995  return ret;
996 }
997 
998 static int sh_alf_aps(const VVCSH *sh, const VVCFrameParamSets *fps)
999 {
1000  if (!sh->r->sh_alf_enabled_flag)
1001  return 0;
1002 
1003  for (int i = 0; i < sh->r->sh_num_alf_aps_ids_luma; i++) {
1004  const VVCALF *alf_aps_luma = fps->alf_list[sh->r->sh_alf_aps_id_luma[i]];
1005  if (!alf_aps_luma)
1006  return AVERROR_INVALIDDATA;
1007  }
1008 
1009  if (sh->r->sh_alf_cb_enabled_flag || sh->r->sh_alf_cr_enabled_flag) {
1010  const VVCALF *alf_aps_chroma = fps->alf_list[sh->r->sh_alf_aps_id_chroma];
1011  if (!alf_aps_chroma)
1012  return AVERROR_INVALIDDATA;
1013  }
1014 
1015  if (fps->sps->r->sps_ccalf_enabled_flag) {
1016  if (sh->r->sh_alf_cc_cb_enabled_flag) {
1017  const VVCALF *alf_aps_cc_cr = fps->alf_list[sh->r->sh_alf_cc_cb_aps_id];
1018  if (!alf_aps_cc_cr)
1019  return AVERROR_INVALIDDATA;
1020  }
1021  if (sh->r->sh_alf_cc_cr_enabled_flag) {
1022  const VVCALF *alf_aps_cc_cr = fps->alf_list[sh->r->sh_alf_cc_cr_aps_id];
1023  if (!alf_aps_cc_cr)
1024  return AVERROR_INVALIDDATA;
1025  }
1026  }
1027 
1028  return 0;
1029 }
1030 
1031 static void sh_slice_address(VVCSH *sh, const H266RawSPS *sps, const VVCPPS *pps)
1032 {
1033  const int slice_address = sh->r->sh_slice_address;
1034 
1035  if (pps->r->pps_rect_slice_flag) {
1036  int pic_level_slice_idx = slice_address;
1037  for (int j = 0; j < sh->r->curr_subpic_idx; j++)
1038  pic_level_slice_idx += pps->r->num_slices_in_subpic[j];
1039  sh->ctb_addr_in_curr_slice = pps->ctb_addr_in_slice + pps->slice_start_offset[pic_level_slice_idx];
1040  sh->num_ctus_in_curr_slice = pps->num_ctus_in_slice[pic_level_slice_idx];
1041  } else {
1042  int tile_x = slice_address % pps->r->num_tile_columns;
1043  int tile_y = slice_address / pps->r->num_tile_columns;
1044  const int slice_start_ctb = pps->row_bd[tile_y] * pps->ctb_width + pps->col_bd[tile_x] * pps->r->row_height_val[tile_y];
1045 
1046  sh->ctb_addr_in_curr_slice = pps->ctb_addr_in_slice + slice_start_ctb;
1047 
1048  sh->num_ctus_in_curr_slice = 0;
1049  for (int tile_idx = slice_address; tile_idx <= slice_address + sh->r->sh_num_tiles_in_slice_minus1; tile_idx++) {
1050  tile_x = tile_idx % pps->r->num_tile_columns;
1051  tile_y = tile_idx / pps->r->num_tile_columns;
1052  sh->num_ctus_in_curr_slice += pps->r->row_height_val[tile_y] * pps->r->col_width_val[tile_x];
1053  }
1054  }
1055 }
1056 
1057 static void sh_qp_y(VVCSH *sh, const H266RawPPS *pps, const H266RawPictureHeader *ph)
1058 {
1059  const int init_qp = pps->pps_init_qp_minus26 + 26;
1060 
1061  if (!pps->pps_qp_delta_info_in_ph_flag)
1062  sh->slice_qp_y = init_qp + sh->r->sh_qp_delta;
1063  else
1064  sh->slice_qp_y = init_qp + ph->ph_qp_delta;
1065 }
1066 
1067 static void sh_inter(VVCSH *sh, const H266RawSPS *sps, const H266RawPPS *pps)
1068 {
1069  const H266RawSliceHeader *rsh = sh->r;
1070 
1071  if (!pps->pps_wp_info_in_ph_flag &&
1072  ((pps->pps_weighted_pred_flag && IS_P(rsh)) ||
1073  (pps->pps_weighted_bipred_flag && IS_B(rsh))))
1075 }
1076 
1077 static void sh_deblock_offsets(VVCSH *sh)
1078 {
1079  const H266RawSliceHeader *r = sh->r;
1080 
1081  if (!r->sh_deblocking_filter_disabled_flag) {
1082  sh->deblock.beta_offset[LUMA] = r->sh_luma_beta_offset_div2 << 1;
1083  sh->deblock.tc_offset[LUMA] = r->sh_luma_tc_offset_div2 << 1;
1084  sh->deblock.beta_offset[CB] = r->sh_cb_beta_offset_div2 << 1;
1085  sh->deblock.tc_offset[CB] = r->sh_cb_tc_offset_div2 << 1;
1086  sh->deblock.beta_offset[CR] = r->sh_cr_beta_offset_div2 << 1;
1087  sh->deblock.tc_offset[CR] = r->sh_cr_tc_offset_div2 << 1;
1088  }
1089 }
1090 
1092 {
1093  const int min_cb_log2_size_y = sps->sps_log2_min_luma_coding_block_size_minus2 + 2;
1094  int min_qt_log2_size_y[2];
1095 
1096  if (IS_I(sh->r)) {
1097  min_qt_log2_size_y[LUMA] = (min_cb_log2_size_y + ph->ph_log2_diff_min_qt_min_cb_intra_slice_luma);
1098  min_qt_log2_size_y[CHROMA] = (min_cb_log2_size_y + ph->ph_log2_diff_min_qt_min_cb_intra_slice_chroma);
1099 
1100  sh->max_bt_size[LUMA] = 1 << (min_qt_log2_size_y[LUMA] + ph->ph_log2_diff_max_bt_min_qt_intra_slice_luma);
1101  sh->max_bt_size[CHROMA] = 1 << (min_qt_log2_size_y[CHROMA]+ ph->ph_log2_diff_max_bt_min_qt_intra_slice_chroma);
1102 
1103  sh->max_tt_size[LUMA] = 1 << (min_qt_log2_size_y[LUMA] + ph->ph_log2_diff_max_tt_min_qt_intra_slice_luma);
1104  sh->max_tt_size[CHROMA] = 1 << (min_qt_log2_size_y[CHROMA]+ ph->ph_log2_diff_max_tt_min_qt_intra_slice_chroma);
1105 
1106  sh->max_mtt_depth[LUMA] = ph->ph_max_mtt_hierarchy_depth_intra_slice_luma;
1107  sh->max_mtt_depth[CHROMA] = ph->ph_max_mtt_hierarchy_depth_intra_slice_chroma;
1108 
1109  sh->cu_qp_delta_subdiv = ph->ph_cu_qp_delta_subdiv_intra_slice;
1110  sh->cu_chroma_qp_offset_subdiv = ph->ph_cu_chroma_qp_offset_subdiv_intra_slice;
1111  } else {
1112  for (int i = LUMA; i <= CHROMA; i++) {
1113  min_qt_log2_size_y[i] = (min_cb_log2_size_y + ph->ph_log2_diff_min_qt_min_cb_inter_slice);
1114  sh->max_bt_size[i] = 1 << (min_qt_log2_size_y[i] + ph->ph_log2_diff_max_bt_min_qt_inter_slice);
1115  sh->max_tt_size[i] = 1 << (min_qt_log2_size_y[i] + ph->ph_log2_diff_max_tt_min_qt_inter_slice);
1116  sh->max_mtt_depth[i] = ph->ph_max_mtt_hierarchy_depth_inter_slice;
1117  }
1118 
1119  sh->cu_qp_delta_subdiv = ph->ph_cu_qp_delta_subdiv_inter_slice;
1120  sh->cu_chroma_qp_offset_subdiv = ph->ph_cu_chroma_qp_offset_subdiv_inter_slice;
1121  }
1122 
1123  sh->min_qt_size[LUMA] = 1 << min_qt_log2_size_y[LUMA];
1124  sh->min_qt_size[CHROMA] = 1 << min_qt_log2_size_y[CHROMA];
1125 }
1126 
1127 static void sh_entry_points(VVCSH *sh, const H266RawSPS *sps, const VVCPPS *pps)
1128 {
1129  if (sps->sps_entry_point_offsets_present_flag) {
1130  for (int i = 1, j = 0; i < sh->num_ctus_in_curr_slice; i++) {
1131  const int pre_ctb_addr_x = sh->ctb_addr_in_curr_slice[i - 1] % pps->ctb_width;
1132  const int pre_ctb_addr_y = sh->ctb_addr_in_curr_slice[i - 1] / pps->ctb_width;
1133  const int ctb_addr_x = sh->ctb_addr_in_curr_slice[i] % pps->ctb_width;
1134  const int ctb_addr_y = sh->ctb_addr_in_curr_slice[i] / pps->ctb_width;
1135  if (pps->ctb_to_row_bd[ctb_addr_y] != pps->ctb_to_row_bd[pre_ctb_addr_y] ||
1136  pps->ctb_to_col_bd[ctb_addr_x] != pps->ctb_to_col_bd[pre_ctb_addr_x] ||
1137  (ctb_addr_y != pre_ctb_addr_y && sps->sps_entropy_coding_sync_enabled_flag)) {
1138  sh->entry_point_start_ctu[j++] = i;
1139  }
1140  }
1141  }
1142 }
1143 
1144 static int sh_derive(VVCSH *sh, const VVCFrameParamSets *fps)
1145 {
1146  const H266RawSPS *sps = fps->sps->r;
1147  const H266RawPPS *pps = fps->pps->r;
1148  const H266RawPictureHeader *ph = fps->ph.r;
1149  int ret;
1150 
1151  sh_slice_address(sh, sps, fps->pps);
1152  ret = sh_alf_aps(sh, fps);
1153  if (ret < 0)
1154  return ret;
1155  sh_inter(sh, sps, pps);
1156  sh_qp_y(sh, pps, ph);
1157  sh_deblock_offsets(sh);
1159  sh_entry_points(sh, sps, fps->pps);
1160 
1161  return 0;
1162 }
1163 
1165 {
1166  int ret;
1167 
1168  if (!fps->sps || !fps->pps)
1169  return AVERROR_INVALIDDATA;
1170 
1171  ff_refstruct_replace(&sh->r, unit->content_ref);
1172 
1173  ret = sh_derive(sh, fps);
1174  if (ret < 0)
1175  return ret;
1176 
1177  return 0;
1178 }
CB
#define CB
Definition: hevc_filter.c:32
VVCSPS
Definition: vvc_ps.h:58
MIN_TU_LOG2
#define MIN_TU_LOG2
MinTbLog2SizeY.
Definition: vvcdec.h:39
ph_derive
static int ph_derive(VVCPH *ph, const H266RawSPS *sps, const H266RawPPS *pps, const int poc_tid0, const int is_clvss)
Definition: vvc_ps.c:677
pps_bd
static int pps_bd(VVCPPS *pps)
Definition: vvc_ps.c:276
aps_decode_scaling
static int aps_decode_scaling(const VVCScalingList **scaling, const H266RawAPS *aps)
Definition: vvc_ps.c:962
VVCSH::cu_qp_delta_subdiv
uint8_t cu_qp_delta_subdiv
CuQpDeltaSubdiv.
Definition: vvc_ps.h:247
H266RawSliceHeader::sh_alf_cc_cr_enabled_flag
uint8_t sh_alf_cc_cr_enabled_flag
Definition: cbs_h266.h:789
CodedBitstreamUnit::content_ref
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition: cbs.h:112
VVCSH::num_ctus_in_curr_slice
uint32_t num_ctus_in_curr_slice
NumCtusInCurrSlice.
Definition: vvc_ps.h:229
VVCPH
Definition: vvc_ps.h:143
sps_alloc
static const VVCSPS * sps_alloc(const H266RawSPS *rsps, void *log_ctx)
Definition: vvc_ps.c:199
VVCPPS
Definition: vvc_ps.h:92
av_clip
#define av_clip
Definition: common.h:98
decode_frame_ps
static int decode_frame_ps(VVCFrameParamSets *fps, const VVCParamSets *ps, const CodedBitstreamH266Context *h266, const int poc_tid0, const int is_clvss)
Definition: vvc_ps.c:706
VVCLMCS::min_bin_idx
uint8_t min_bin_idx
Definition: vvc_ps.h:191
cbs_h266.h
sh_qp_y
static void sh_qp_y(VVCSH *sh, const H266RawPPS *pps, const H266RawPictureHeader *ph)
Definition: vvc_ps.c:1057
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
LMCS_MAX_BIT_DEPTH
#define LMCS_MAX_BIT_DEPTH
Definition: vvc_ps.h:46
IS_GDR
#define IS_GDR(s)
Definition: vvc_ps.h:32
VVCSH::entry_point_start_ctu
uint32_t entry_point_start_ctu[VVC_MAX_ENTRY_POINTS]
entry point start in ctu_addr
Definition: vvc_ps.h:251
VVCPPS::r
const H266RawPPS * r
RefStruct reference.
Definition: vvc_ps.h:93
sh_partition_constraints
static void sh_partition_constraints(VVCSH *sh, const H266RawSPS *sps, const H266RawPictureHeader *ph)
Definition: vvc_ps.c:1091
VVC_MAX_SAMPLE_ARRAYS
@ VVC_MAX_SAMPLE_ARRAYS
Definition: vvc.h:77
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
VVCSH::cu_chroma_qp_offset_subdiv
uint8_t cu_chroma_qp_offset_subdiv
CuChromaQpOffsetSubdiv.
Definition: vvc_ps.h:248
ff_refstruct_alloc_ext
static void * ff_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(FFRefStructOpaque opaque, void *obj))
A wrapper around ff_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition: refstruct.h:94
sps_map_pixel_format
static int sps_map_pixel_format(VVCSPS *sps, void *log_ctx)
Definition: vvc_ps.c:30
MIN_PU_LOG2
#define MIN_PU_LOG2
Definition: vvcdec.h:40
WEIGHT_TABLE
#define WEIGHT_TABLE(x)
Definition: vvc_ps.c:537
ph
static int FUNC() ph(CodedBitstreamContext *ctx, RWContext *rw, H266RawPH *current)
Definition: cbs_h266_syntax_template.c:3002
w
uint8_t w
Definition: llviddspenc.c:38
VVCLMCS::max_bin_idx
uint8_t max_bin_idx
Definition: vvc_ps.h:192
H266RawAPS::lmcs_delta_abs_crs
uint8_t lmcs_delta_abs_crs
Definition: cbs_h266.h:637
H266RawSPS::sps_ccalf_enabled_flag
uint8_t sps_ccalf_enabled_flag
Definition: cbs_h266.h:404
H266RawSliceHeader::sh_qp_delta
int8_t sh_qp_delta
Definition: cbs_h266.h:805
pps_ref_wraparound_offset
static void pps_ref_wraparound_offset(VVCPPS *pps, const VVCSPS *sps)
Definition: vvc_ps.c:425
SL_START_4x4
@ SL_START_4x4
Definition: vvc_ps.h:175
pps_multi_tiles_slice
static void pps_multi_tiles_slice(VVCPPS *pps, const int tile_idx, const int i, int *off)
Definition: vvc_ps.c:364
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:478
VVCSH::r
const H266RawSliceHeader * r
RefStruct reference.
Definition: vvc_ps.h:225
ff_vvc_decode_aps
int ff_vvc_decode_aps(VVCParamSets *ps, const CodedBitstreamUnit *unit)
Definition: vvc_ps.c:975
max
#define max(a, b)
Definition: cuda_runtime.h:33
FFRefStructOpaque
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition: refstruct.h:58
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
H266RawPPS::pps_seq_parameter_set_id
uint8_t pps_seq_parameter_set_id
Definition: cbs_h266.h:500
VVCLMCS
Definition: vvc_ps.h:190
pps_slice_map
static int pps_slice_map(VVCPPS *pps)
Definition: vvc_ps.c:411
bit_depth
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition: af_astats.c:245
sps_partition_constraints
static void sps_partition_constraints(VVCSPS *sps)
Definition: vvc_ps.c:149
VVCALF::num_chroma_filters
uint8_t num_chroma_filters
Definition: vvc_ps.h:165
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:70
VVCLMCS::chroma_scale_coeff
uint16_t chroma_scale_coeff[LMCS_MAX_BIN_SIZE]
Definition: vvc_ps.h:200
CodedBitstreamH266Context::pps
H266RawPPS * pps[VVC_MAX_PPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:874
H266RawSliceHeader::sh_alf_cc_cr_aps_id
uint8_t sh_alf_cc_cr_aps_id
Definition: cbs_h266.h:790
pps_chroma_qp_offset
static void pps_chroma_qp_offset(VVCPPS *pps)
Definition: vvc_ps.c:239
VVCFrameParamSets::sps
const VVCSPS * sps
RefStruct reference.
Definition: vvc_ps.h:216
alf_coeff
static void alf_coeff(int16_t *coeff, const uint8_t *abs, const uint8_t *sign, const int size)
Definition: vvc_ps.c:800
VVC_ASP_TYPE_ALF
@ VVC_ASP_TYPE_ALF
Definition: vvc.h:70
vvc_data.h
VVCFrameParamSets
Definition: vvc_ps.h:215
fail
#define fail()
Definition: checkasm.h:179
VVCSH::pwt
PredWeightTable pwt
Definition: vvc_ps.h:233
H266RawAPS
Definition: cbs_h266.h:598
VVCParamSets::pps_list
const VVCPPS * pps_list[VVC_MAX_PPS_COUNT]
RefStruct reference.
Definition: vvc_ps.h:209
scaling_derive
static void scaling_derive(VVCScalingList *sl, const H266RawAPS *aps)
Definition: vvc_ps.c:900
VVCALF::cc_coeff
int16_t cc_coeff[2][ALF_NUM_FILTERS_CC][ALF_NUM_COEFF_CC]
Definition: vvc_ps.h:170
ph_compute_poc
static int ph_compute_poc(const H266RawPictureHeader *ph, const H266RawSPS *sps, const int poc_tid0, const int is_clvss)
Definition: vvc_ps.c:565
VVCFrameParamSets::sl
const VVCScalingList * sl
RefStruct reference.
Definition: vvc_ps.h:221
VVCALF
Definition: vvc_ps.h:161
sps_free
static void sps_free(FFRefStructOpaque opaque, void *obj)
Definition: vvc_ps.c:193
H266RawSliceHeader::sh_alf_enabled_flag
uint8_t sh_alf_enabled_flag
Definition: cbs_h266.h:781
refstruct.h
pps_alloc
static const VVCPPS * pps_alloc(const H266RawPPS *rpps, const VVCSPS *sps)
Definition: vvc_ps.c:466
VVCALF::chroma_clip_idx
uint8_t chroma_clip_idx[ALF_NUM_FILTERS_CHROMA][ALF_NUM_COEFF_CHROMA]
Definition: vvc_ps.h:167
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:481
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
H266RawAPS::lmcs_delta_abs_cw
uint16_t lmcs_delta_abs_cw[16]
Definition: cbs_h266.h:635
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
DBParams::beta_offset
int beta_offset
Definition: hevcdec.h:342
DBParams::tc_offset
int tc_offset
Definition: hevcdec.h:343
pps_no_rect_slice
static void pps_no_rect_slice(VVCPPS *pps)
Definition: vvc_ps.c:398
pps_one_tile_slices
static int pps_one_tile_slices(VVCPPS *pps, const int tile_idx, int i, int *off)
Definition: vvc_ps.c:346
H266RawSPS::sps_seq_parameter_set_id
uint8_t sps_seq_parameter_set_id
Definition: cbs_h266.h:311
s
#define s(width, name)
Definition: cbs_vp9.c:198
H266RawSliceHeader::sh_slice_address
uint16_t sh_slice_address
Definition: cbs_h266.h:775
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
pps_add_ctus
static int pps_add_ctus(VVCPPS *pps, int *off, const int ctu_x, const int ctu_y, const int w, const int h)
Definition: vvc_ps.c:333
derive_matrix_size
static int derive_matrix_size(const int id)
Definition: vvc_ps.c:894
sh_inter
static void sh_inter(VVCSH *sh, const H266RawSPS *sps, const H266RawPPS *pps)
Definition: vvc_ps.c:1067
ff_vvc_diag_scan_y
const uint8_t ff_vvc_diag_scan_y[5][5][16 *16]
Definition: vvc_data.c:152
VVCFrameParamSets::lmcs
VVCLMCS lmcs
Definition: vvc_ps.h:220
lmcs_derive_lut_sample
static av_always_inline uint16_t lmcs_derive_lut_sample(uint16_t sample, uint16_t *pivot1, uint16_t *pivot2, uint16_t *scale_coeff, const int idx, const int max)
Definition: vvc_ps.c:589
VVCSH
Definition: vvc_ps.h:224
IS_IDR
#define IS_IDR(s)
Definition: hevcdec.h:75
PredWeightTable
Definition: vvc_ps.h:133
VVCFrameParamSets::ph
VVCPH ph
Definition: vvc_ps.h:218
vvcdec.h
ALF_NUM_COEFF_CHROMA
#define ALF_NUM_COEFF_CHROMA
Definition: vvc_ps.h:158
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
VVC_MAX_POINTS_IN_QP_TABLE
@ VVC_MAX_POINTS_IN_QP_TABLE
Definition: vvc.h:121
pps_width_height
static void pps_width_height(VVCPPS *pps, const VVCSPS *sps)
Definition: vvc_ps.c:251
VVCParamSets::sps_list
const VVCSPS * sps_list[VVC_MAX_SPS_COUNT]
RefStruct reference.
Definition: vvc_ps.h:208
ff_vvc_decode_frame_ps
int ff_vvc_decode_frame_ps(VVCFrameParamSets *fps, struct VVCContext *s)
Definition: vvc_ps.c:760
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:459
H266RawSPS
Definition: cbs_h266.h:308
H266RawPPS
Definition: cbs_h266.h:496
lmcs_derive_lut
static int lmcs_derive_lut(VVCLMCS *lmcs, const H266RawAPS *rlmcs, const H266RawSPS *sps)
Definition: vvc_ps.c:598
alf_derive
static void alf_derive(VVCALF *alf, const H266RawAPS *aps)
Definition: vvc_ps.c:869
aps
static int FUNC() aps(CodedBitstreamContext *ctx, RWContext *rw, H266RawAPS *current, int prefix)
Definition: cbs_h266_syntax_template.c:2460
GDR_SET_RECOVERED
#define GDR_SET_RECOVERED(s)
Definition: vvc_ps.h:44
ff_vvc_decode_sh
int ff_vvc_decode_sh(VVCSH *sh, const VVCFrameParamSets *fps, const CodedBitstreamUnit *unit)
Definition: vvc_ps.c:1164
NULL
#define NULL
Definition: coverity.c:32
H266RawPictureHeader
Definition: cbs_h266.h:674
pps_derive
static int pps_derive(VVCPPS *pps, const VVCSPS *sps)
Definition: vvc_ps.c:433
ALF_NUM_COEFF_CC
#define ALF_NUM_COEFF_CC
Definition: vvc_ps.h:159
pred_weight_table
static void pred_weight_table(PredWeightTable *w, const H266RawPredWeightTable *r)
Definition: vvc_ps.c:552
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
H266RawPPS::pps_pic_parameter_set_id
uint8_t pps_pic_parameter_set_id
Definition: cbs_h266.h:499
H266RawSliceHeader::curr_subpic_idx
uint16_t curr_subpic_idx
CurrSubpicIdx.
Definition: cbs_h266.h:835
VVCFrameParamSets::alf_list
const VVCALF * alf_list[VVC_MAX_ALF_COUNT]
RefStruct reference.
Definition: vvc_ps.h:219
ff_vvc_frame_ps_free
void ff_vvc_frame_ps_free(VVCFrameParamSets *fps)
Definition: vvc_ps.c:776
H266RawAPS::lmcs_delta_sign_cw_flag
uint8_t lmcs_delta_sign_cw_flag[16]
Definition: cbs_h266.h:636
sh_alf_aps
static int sh_alf_aps(const VVCSH *sh, const VVCFrameParamSets *fps)
Definition: vvc_ps.c:998
H266RawPredWeightTable
Definition: cbs_h266.h:650
VVCSH::max_tt_size
uint8_t max_tt_size[2]
MaxTtSizeY, MaxTtSizeC.
Definition: vvc_ps.h:245
decode_ps
static int decode_ps(VVCParamSets *ps, const CodedBitstreamH266Context *h266, void *log_ctx)
Definition: vvc_ps.c:508
sps_ladf
static void sps_ladf(VVCSPS *sps)
Definition: vvc_ps.c:161
VVC_ASP_TYPE_LMCS
@ VVC_ASP_TYPE_LMCS
Definition: vvc.h:71
SL_MAX_MATRIX_SIZE
#define SL_MAX_MATRIX_SIZE
Definition: vvc_ps.h:183
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
VVCParamSets::scaling_list
const VVCScalingList * scaling_list[VVC_MAX_SL_COUNT]
RefStruct reference.
Definition: vvc_ps.h:212
H266RawSliceHeader::sh_num_tiles_in_slice_minus1
uint8_t sh_num_tiles_in_slice_minus1
Definition: cbs_h266.h:777
abs
#define abs(x)
Definition: cuda_runtime.h:35
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:479
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
ff_vvc_scaling_pred_8
const uint8_t ff_vvc_scaling_pred_8[8 *8]
Definition: vvc_data.c:277
aps_decode_alf
static int aps_decode_alf(const VVCALF **alf, const H266RawAPS *aps)
Definition: vvc_ps.c:876
VVCPH::r
const H266RawPictureHeader * r
Definition: vvc_ps.h:144
JCBCR
#define JCBCR
Definition: vvcdec.h:37
SL_START_8x8
@ SL_START_8x8
Definition: vvc_ps.h:176
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
VVCALF::luma_coeff
int16_t luma_coeff[ALF_NUM_FILTERS_LUMA][ALF_NUM_COEFF_LUMA]
Definition: vvc_ps.h:162
ff_vvc_ps_uninit
void ff_vvc_ps_uninit(VVCParamSets *ps)
Definition: vvc_ps.c:786
VVCFrameParamSets::pps
const VVCPPS * pps
RefStruct reference.
Definition: vvc_ps.h:217
decode_recovery_poc
static void decode_recovery_poc(VVCContext *s, const VVCPH *ph)
Definition: vvc_ps.c:750
VVCSH::max_mtt_depth
uint8_t max_mtt_depth[2]
MaxMttDepthY, MaxMttDepthC.
Definition: vvc_ps.h:246
VVCScalingList
Definition: vvc_ps.h:185
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
pps
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
Definition: cbs_h264_syntax_template.c:404
shift
static int shift(int a, int b)
Definition: bonk.c:262
alf_luma
static void alf_luma(VVCALF *alf, const H266RawAPS *aps)
Definition: vvc_ps.c:818
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
ALF_NUM_COEFF_LUMA
#define ALF_NUM_COEFF_LUMA
Definition: vvc_ps.h:157
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:483
decode_sps
static int decode_sps(VVCParamSets *ps, const H266RawSPS *rsps, void *log_ctx)
Definition: vvc_ps.c:220
sample
#define sample
Definition: flacdsp_template.c:44
size
int size
Definition: twinvq_data.h:10344
sh_derive
static int sh_derive(VVCSH *sh, const VVCFrameParamSets *fps)
Definition: vvc_ps.c:1144
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:485
CR
#define CR
Definition: hevc_filter.c:33
decode_ph
static int decode_ph(VVCFrameParamSets *fps, const H266RawPictureHeader *rph, void *rph_ref, const int poc_tid0, const int is_clvss)
Definition: vvc_ps.c:689
H266RawAPS::lmcs_min_bin_idx
uint8_t lmcs_min_bin_idx
Definition: cbs_h266.h:632
is_luma_list
static int is_luma_list(const int id)
Definition: vvc_ps.c:889
H266RawSliceHeader::sh_alf_aps_id_chroma
uint8_t sh_alf_aps_id_chroma
Definition: cbs_h266.h:786
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
CodedBitstreamH266Context
Definition: cbs_h266.h:866
VVCSH::deblock
DBParams deblock
Definition: vvc_ps.h:240
VVCSH::ctb_addr_in_curr_slice
const uint32_t * ctb_addr_in_curr_slice
CtbAddrInCurrSlice.
Definition: vvc_ps.h:230
VVCScalingList::scaling_matrix_dc_rec
uint8_t scaling_matrix_dc_rec[SL_MAX_ID - SL_START_16x16]
ScalingMatrixDcRec[refId − 14].
Definition: vvc_ps.h:187
ALF_NUM_FILTERS_LUMA
#define ALF_NUM_FILTERS_LUMA
Definition: vvc_ps.h:153
H266RawSliceHeader::sh_alf_cb_enabled_flag
uint8_t sh_alf_cb_enabled_flag
Definition: cbs_h266.h:784
decode_pps
static int decode_pps(VVCParamSets *ps, const H266RawPPS *rpps)
Definition: vvc_ps.c:487
ff_vvc_diag_scan_x
const uint8_t ff_vvc_diag_scan_x[5][5][16 *16]
Definition: vvc_data.c:27
SL_START_16x16
@ SL_START_16x16
Definition: vvc_ps.h:177
H266RawSliceHeader::sh_pred_weight_table
H266RawPredWeightTable sh_pred_weight_table
Definition: cbs_h266.h:803
H266RawSliceHeader
Definition: cbs_h266.h:769
sps_derive
static int sps_derive(VVCSPS *sps, void *log_ctx)
Definition: vvc_ps.c:175
VVCLMCS::pivot
uint16_t pivot[LMCS_MAX_BIN_SIZE+1]
Definition: vvc_ps.h:199
pps_rect_slice
static void pps_rect_slice(VVCPPS *pps)
Definition: vvc_ps.c:381
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
H266RawSliceHeader::sh_alf_aps_id_luma
uint8_t sh_alf_aps_id_luma[8]
Definition: cbs_h266.h:783
tile_xy
static void tile_xy(int *tile_x, int *tile_y, const int tile_idx, const VVCPPS *pps)
Definition: vvc_ps.c:316
CodedBitstreamH266Context::sps
H266RawSPS * sps[VVC_MAX_SPS_COUNT]
RefStruct references.
Definition: cbs_h266.h:873
ctu_xy
static void ctu_xy(int *ctu_x, int *ctu_y, const int tile_x, const int tile_y, const VVCPPS *pps)
Definition: vvc_ps.c:322
IS_B
#define IS_B(rsh)
Definition: vvc_ps.h:40
VVCPH::rref
void * rref
RefStruct reference, backing ph above.
Definition: vvc_ps.h:145
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
VVCLMCS::u16
uint16_t u16[LMCS_MAX_LUT_SIZE]
for high bit-depth
Definition: vvc_ps.h:196
alf_cc
static void alf_cc(VVCALF *alf, const H266RawAPS *aps)
Definition: vvc_ps.c:850
GDR_IS_RECOVERED
#define GDR_IS_RECOVERED(s)
Definition: vvc_ps.h:43
H266RawSliceHeader::sh_alf_cc_cb_aps_id
uint8_t sh_alf_cc_cb_aps_id
Definition: cbs_h266.h:788
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
H266RawAPS::lmcs_delta_sign_crs_flag
uint8_t lmcs_delta_sign_crs_flag
Definition: cbs_h266.h:638
alf_chroma
static void alf_chroma(VVCALF *alf, const H266RawAPS *aps)
Definition: vvc_ps.c:834
sps_bit_depth
static int sps_bit_depth(VVCSPS *sps, void *log_ctx)
Definition: vvc_ps.c:75
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
SL_MAX_ID
@ SL_MAX_ID
Definition: vvc_ps.h:180
ff_vvc_scaling_pred_16
const uint8_t ff_vvc_scaling_pred_16[8 *8]
Definition: vvc_data.c:288
CHROMA
@ CHROMA
Definition: vf_waveform.c:49
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
H266RawSliceHeader::sh_alf_cc_cb_enabled_flag
uint8_t sh_alf_cc_cb_enabled_flag
Definition: cbs_h266.h:787
ff_refstruct_replace
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
LMCS_MAX_BIN_SIZE
#define LMCS_MAX_BIN_SIZE
Definition: vvc_ps.h:48
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:482
id
enum AVCodecID id
Definition: dts2pts.c:364
sh_deblock_offsets
static void sh_deblock_offsets(VVCSH *sh)
Definition: vvc_ps.c:1077
sps_poc
static void sps_poc(VVCSPS *sps)
Definition: vvc_ps.c:128
decode_recovery_flag
static void decode_recovery_flag(VVCContext *s)
Definition: vvc_ps.c:742
ctu_rs
static int ctu_rs(const int ctu_x, const int ctu_y, const VVCPPS *pps)
Definition: vvc_ps.c:328
IS_P
#define IS_P(rsh)
Definition: vvc_ps.h:39
IS_CRA
#define IS_CRA(s)
Definition: vvc_ps.h:30
alf_coeff_cc
static void alf_coeff_cc(int16_t *coeff, const uint8_t *mapped_abs, const uint8_t *sign)
Definition: vvc_ps.c:807
VVCParamSets::alf_list
const VVCALF * alf_list[VVC_MAX_ALF_COUNT]
RefStruct reference.
Definition: vvc_ps.h:210
sh_entry_points
static void sh_entry_points(VVCSH *sh, const H266RawSPS *sps, const VVCPPS *pps)
Definition: vvc_ps.c:1127
VVCSH::slice_qp_y
int8_t slice_qp_y
SliceQpY.
Definition: vvc_ps.h:237
SL_START_64x64
@ SL_START_64x64
Definition: vvc_ps.h:179
VVC_ASP_TYPE_SCALING
@ VVC_ASP_TYPE_SCALING
Definition: vvc.h:72
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
scaling_list
static int FUNC() scaling_list(CodedBitstreamContext *ctx, RWContext *rw, H264RawScalingList *current, int size_of_scaling_list)
Definition: cbs_h264_syntax_template.c:71
next_tile_idx
static int next_tile_idx(int tile_idx, const int i, const H266RawPPS *r)
Definition: vvc_ps.c:304
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
sps_inter
static void sps_inter(VVCSPS *sps)
Definition: vvc_ps.c:133
pps_free
static void pps_free(FFRefStructOpaque opaque, void *obj)
Definition: vvc_ps.c:453
VVCLMCS::u8
uint8_t u8[LMCS_MAX_LUT_SIZE]
Definition: vvc_ps.h:195
IS_I
#define IS_I(rsh)
Definition: vvc_ps.h:38
desc
const char * desc
Definition: libsvtav1.c:75
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
VVCALF::luma_clip_idx
uint8_t luma_clip_idx[ALF_NUM_FILTERS_LUMA][ALF_NUM_COEFF_LUMA]
Definition: vvc_ps.h:163
VVCLMCS::fwd_lut
union VVCLMCS::@225 fwd_lut
sps_chroma_qp_table
static int sps_chroma_qp_table(VVCSPS *sps)
Definition: vvc_ps.c:86
VVCParamSets
Definition: vvc_ps.h:207
VVCSH::max_bt_size
uint8_t max_bt_size[2]
MaxBtSizeY, MaxBtSizeC.
Definition: vvc_ps.h:244
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
VVCSPS::r
const H266RawSPS * r
RefStruct reference.
Definition: vvc_ps.h:59
VVCALF::num_cc_filters
uint8_t num_cc_filters[2]
alf_cc_cb_filters_signalled_minus1 + 1, alf_cc_cr_filters_signalled_minus1 + 1
Definition: vvc_ps.h:169
H266RawSliceHeader::sh_num_alf_aps_ids_luma
uint8_t sh_num_alf_aps_ids_luma
Definition: cbs_h266.h:782
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
VVCLMCS::inv_lut
union VVCLMCS::@225 inv_lut
CodedBitstreamH266Context::ph_ref
void * ph_ref
RefStruct reference backing ph above.
Definition: cbs_h266.h:876
imgutils.h
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:79
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
VVCSH::min_qt_size
uint8_t min_qt_size[2]
MinQtSizeY, MinQtSizeC.
Definition: vvc_ps.h:243
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
IS_CLVSS
#define IS_CLVSS(s)
Definition: vvc_ps.h:34
ff_vvc_scaling_list0
const int ff_vvc_scaling_list0[8 *8]
Definition: vvc_data.c:299
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:460
scale_coeff
static av_always_inline int scale_coeff(const TransformBlock *tb, int coeff, const int scale, const int scale_m, const int log2_transform_range)
Definition: vvc_intra.c:416
H266RawSliceHeader::sh_alf_cr_enabled_flag
uint8_t sh_alf_cr_enabled_flag
Definition: cbs_h266.h:785
ph_max_num_subblock_merge_cand
static int ph_max_num_subblock_merge_cand(const H266RawSPS *sps, const H266RawPictureHeader *ph)
Definition: vvc_ps.c:670
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
VVCALF::chroma_coeff
int16_t chroma_coeff[ALF_NUM_FILTERS_CHROMA][ALF_NUM_COEFF_CHROMA]
Definition: vvc_ps.h:166
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
LUMA
#define LUMA
Definition: hevc_filter.c:31
VVCParamSets::lmcs_list
const H266RawAPS * lmcs_list[VVC_MAX_LMCS_COUNT]
RefStruct reference.
Definition: vvc_ps.h:211
VVCContext
Definition: vvcdec.h:195
VVCScalingList::scaling_matrix_rec
uint8_t scaling_matrix_rec[SL_MAX_ID][SL_MAX_MATRIX_SIZE *SL_MAX_MATRIX_SIZE]
ScalingMatrixRec.
Definition: vvc_ps.h:186
vvc_ps.h