FFmpeg
av1dec.c
Go to the documentation of this file.
1 /*
2  * AV1 video decoder
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
24 #include "avcodec.h"
25 #include "av1dec.h"
26 #include "bytestream.h"
27 #include "hwconfig.h"
28 #include "internal.h"
29 #include "profiles.h"
30 
31 static uint32_t inverse_recenter(int r, uint32_t v)
32 {
33  if (v > 2 * r)
34  return v;
35  else if (v & 1)
36  return r - ((v + 1) >> 1);
37  else
38  return r + (v >> 1);
39 }
40 
41 static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
42  int mx, int r)
43 {
44  if ((r << 1) <= mx) {
45  return inverse_recenter(r, sub_exp);
46  } else {
47  return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
48  }
49 }
50 
51 static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
52  int high, int r)
53 {
54  int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
55  return x + low;
56 }
57 
58 static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
59 {
60  uint8_t primary_frame, prev_frame;
61  uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;
62  int32_t r, prev_gm_param;
63 
64  primary_frame = s->raw_frame_header->primary_ref_frame;
65  prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
66  abs_bits = AV1_GM_ABS_ALPHA_BITS;
67  prec_bits = AV1_GM_ALPHA_PREC_BITS;
68 
69  /* setup_past_independence() sets PrevGmParams to default values. We can
70  * simply point to the current's frame gm_params as they will be initialized
71  * with defaults at this point.
72  */
74  prev_gm_param = s->cur_frame.gm_params[ref][idx];
75  else
76  prev_gm_param = s->ref[prev_frame].gm_params[ref][idx];
77 
78  if (idx < 2) {
79  if (type == AV1_WARP_MODEL_TRANSLATION) {
80  abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
82  prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
84  } else {
85  abs_bits = AV1_GM_ABS_TRANS_BITS;
86  prec_bits = AV1_GM_TRANS_PREC_BITS;
87  }
88  }
89  round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
90  prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;
91  sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
92  mx = 1 << abs_bits;
93  r = (prev_gm_param >> prec_diff) - sub;
94 
95  s->cur_frame.gm_params[ref][idx] =
97  -mx, mx + 1, r) << prec_diff) + round;
98 }
99 
100 /**
101 * update gm type/params, since cbs already implemented part of this funcation,
102 * so we don't need to full implement spec.
103 */
105 {
107  int type, ref;
108 
109  for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
111  for (int i = 0; i < 6; i++)
112  s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?
113  1 << AV1_WARPEDMODEL_PREC_BITS : 0;
114  }
115  if (header->frame_type == AV1_FRAME_KEY ||
116  header->frame_type == AV1_FRAME_INTRA_ONLY)
117  return;
118 
119  for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
120  if (header->is_global[ref]) {
121  if (header->is_rot_zoom[ref]) {
122  type = AV1_WARP_MODEL_ROTZOOM;
123  } else {
126  }
127  } else {
129  }
130  s->cur_frame.gm_type[ref] = type;
131 
132  if (type >= AV1_WARP_MODEL_ROTZOOM) {
133  read_global_param(s, type, ref, 2);
134  read_global_param(s, type, ref, 3);
135  if (type == AV1_WARP_MODEL_AFFINE) {
136  read_global_param(s, type, ref, 4);
137  read_global_param(s, type, ref, 5);
138  } else {
139  s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];
140  s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];
141  }
142  }
143  if (type >= AV1_WARP_MODEL_TRANSLATION) {
144  read_global_param(s, type, ref, 0);
145  read_global_param(s, type, ref, 1);
146  }
147  }
148 }
149 
151  unsigned int a, unsigned int b)
152 {
153  unsigned int diff = a - b;
154  unsigned int m = 1 << seq->order_hint_bits_minus_1;
155  return (diff & (m - 1)) - (diff & m);
156 }
157 
159 {
161  const AV1RawSequenceHeader *seq = s->raw_seq;
162 
163  int forward_idx, backward_idx;
164  int forward_hint, backward_hint;
165  int second_forward_idx, second_forward_hint;
166  int ref_hint, dist, i;
167 
168  if (!header->skip_mode_present)
169  return;
170 
171  forward_idx = -1;
172  backward_idx = -1;
173  for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
174  ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
175  dist = get_relative_dist(seq, ref_hint, header->order_hint);
176  if (dist < 0) {
177  if (forward_idx < 0 ||
178  get_relative_dist(seq, ref_hint, forward_hint) > 0) {
179  forward_idx = i;
180  forward_hint = ref_hint;
181  }
182  } else if (dist > 0) {
183  if (backward_idx < 0 ||
184  get_relative_dist(seq, ref_hint, backward_hint) < 0) {
185  backward_idx = i;
186  backward_hint = ref_hint;
187  }
188  }
189  }
190 
191  if (forward_idx < 0) {
192  return;
193  } else if (backward_idx >= 0) {
195  AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx);
197  AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx);
198  return;
199  }
200 
201  second_forward_idx = -1;
202  for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
203  ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
204  if (get_relative_dist(seq, ref_hint, forward_hint) < 0) {
205  if (second_forward_idx < 0 ||
206  get_relative_dist(seq, ref_hint, second_forward_hint) > 0) {
207  second_forward_idx = i;
208  second_forward_hint = ref_hint;
209  }
210  }
211  }
212 
213  if (second_forward_idx < 0)
214  return;
215 
217  AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx);
219  AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx);
220 }
221 
223 {
225  int i;
226 
227  if (header->delta_q_y_dc || header->delta_q_u_ac ||
228  header->delta_q_u_dc || header->delta_q_v_ac ||
229  header->delta_q_v_dc) {
230  s->cur_frame.coded_lossless = 0;
231  return;
232  }
233 
234  s->cur_frame.coded_lossless = 1;
235  for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
236  int qindex;
237  if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
238  qindex = (header->base_q_idx +
239  header->feature_value[i][AV1_SEG_LVL_ALT_Q]);
240  } else {
241  qindex = header->base_q_idx;
242  }
243  qindex = av_clip_uintp2(qindex, 8);
244 
245  if (qindex) {
246  s->cur_frame.coded_lossless = 0;
247  return;
248  }
249  }
250 }
251 
253 {
255  const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src;
257 
258  if (!film_grain->apply_grain)
259  return;
260 
261  if (film_grain->update_grain) {
262  memcpy(dst, film_grain, sizeof(*dst));
263  return;
264  }
265 
266  src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain;
267 
268  memcpy(dst, src, sizeof(*dst));
269  dst->grain_seed = film_grain->grain_seed;
270 }
271 
273 
274 {
275  int cur_tile_num =
277  if (s->tile_num < cur_tile_num) {
278  int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
279  sizeof(TileGroupInfo));
280  if (ret < 0) {
281  s->tile_num = 0;
282  return ret;
283  }
284  }
285  s->tile_num = cur_tile_num;
286 
287  return 0;
288 }
289 
290 static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
291 {
292  AV1DecContext *s = avctx->priv_data;
293  GetByteContext gb;
294  uint16_t tile_num, tile_row, tile_col;
295  uint32_t size = 0, size_bytes = 0;
296 
297  bytestream2_init(&gb, tile_group->tile_data.data,
298  tile_group->tile_data.data_size);
299  s->tg_start = tile_group->tg_start;
300  s->tg_end = tile_group->tg_end;
301 
302  for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
303  tile_row = tile_num / s->raw_frame_header->tile_cols;
304  tile_col = tile_num % s->raw_frame_header->tile_cols;
305 
306  if (tile_num == tile_group->tg_end) {
308  s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
309  s->tile_group_info[tile_num].tile_row = tile_row;
310  s->tile_group_info[tile_num].tile_column = tile_col;
311  return 0;
312  }
313  size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
314  if (bytestream2_get_bytes_left(&gb) < size_bytes)
315  return AVERROR_INVALIDDATA;
316  size = 0;
317  for (int i = 0; i < size_bytes; i++)
318  size |= bytestream2_get_byteu(&gb) << 8 * i;
319  if (bytestream2_get_bytes_left(&gb) <= size)
320  return AVERROR_INVALIDDATA;
321  size++;
322 
323  s->tile_group_info[tile_num].tile_size = size;
324  s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
325  s->tile_group_info[tile_num].tile_row = tile_row;
326  s->tile_group_info[tile_num].tile_column = tile_col;
327 
328  bytestream2_skipu(&gb, size);
329  }
330 
331  return 0;
332 
333 }
334 
336 {
337  AV1DecContext *s = avctx->priv_data;
338  const AV1RawSequenceHeader *seq = s->raw_seq;
340  int ret;
342 #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
343  CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
344  CONFIG_AV1_NVDEC_HWACCEL + \
345  CONFIG_AV1_VAAPI_HWACCEL)
346  enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
347 
348  if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
349  bit_depth = seq->color_config.twelve_bit ? 12 : 10;
350  else if (seq->seq_profile <= 2)
351  bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
352  else {
353  av_log(avctx, AV_LOG_ERROR,
354  "Unknown AV1 profile %d.\n", seq->seq_profile);
355  return -1;
356  }
357 
358  if (!seq->color_config.mono_chrome) {
359  // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
360  if (seq->color_config.subsampling_x == 0 &&
361  seq->color_config.subsampling_y == 0) {
362  if (bit_depth == 8)
363  pix_fmt = AV_PIX_FMT_YUV444P;
364  else if (bit_depth == 10)
365  pix_fmt = AV_PIX_FMT_YUV444P10;
366  else if (bit_depth == 12)
367  pix_fmt = AV_PIX_FMT_YUV444P12;
368  else
369  av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
370  } else if (seq->color_config.subsampling_x == 1 &&
371  seq->color_config.subsampling_y == 0) {
372  if (bit_depth == 8)
373  pix_fmt = AV_PIX_FMT_YUV422P;
374  else if (bit_depth == 10)
375  pix_fmt = AV_PIX_FMT_YUV422P10;
376  else if (bit_depth == 12)
377  pix_fmt = AV_PIX_FMT_YUV422P12;
378  else
379  av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
380  } else if (seq->color_config.subsampling_x == 1 &&
381  seq->color_config.subsampling_y == 1) {
382  if (bit_depth == 8)
383  pix_fmt = AV_PIX_FMT_YUV420P;
384  else if (bit_depth == 10)
385  pix_fmt = AV_PIX_FMT_YUV420P10;
386  else if (bit_depth == 12)
387  pix_fmt = AV_PIX_FMT_YUV420P12;
388  else
389  av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
390  }
391  } else {
392  if (bit_depth == 8)
393  pix_fmt = AV_PIX_FMT_GRAY8;
394  else if (bit_depth == 10)
395  pix_fmt = AV_PIX_FMT_GRAY10;
396  else if (bit_depth == 12)
397  pix_fmt = AV_PIX_FMT_GRAY12;
398  else
399  av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
400  }
401 
402  av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
403  av_get_pix_fmt_name(pix_fmt));
404 
405  if (pix_fmt == AV_PIX_FMT_NONE)
406  return -1;
407  s->pix_fmt = pix_fmt;
408 
409  switch (s->pix_fmt) {
410  case AV_PIX_FMT_YUV420P:
411 #if CONFIG_AV1_DXVA2_HWACCEL
412  *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
413 #endif
414 #if CONFIG_AV1_D3D11VA_HWACCEL
415  *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
416  *fmtp++ = AV_PIX_FMT_D3D11;
417 #endif
418 #if CONFIG_AV1_NVDEC_HWACCEL
419  *fmtp++ = AV_PIX_FMT_CUDA;
420 #endif
421 #if CONFIG_AV1_VAAPI_HWACCEL
422  *fmtp++ = AV_PIX_FMT_VAAPI;
423 #endif
424  break;
426 #if CONFIG_AV1_DXVA2_HWACCEL
427  *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
428 #endif
429 #if CONFIG_AV1_D3D11VA_HWACCEL
430  *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
431  *fmtp++ = AV_PIX_FMT_D3D11;
432 #endif
433 #if CONFIG_AV1_NVDEC_HWACCEL
434  *fmtp++ = AV_PIX_FMT_CUDA;
435 #endif
436 #if CONFIG_AV1_VAAPI_HWACCEL
437  *fmtp++ = AV_PIX_FMT_VAAPI;
438 #endif
439  break;
440  case AV_PIX_FMT_GRAY8:
441 #if CONFIG_AV1_NVDEC_HWACCEL
442  *fmtp++ = AV_PIX_FMT_CUDA;
443 #endif
444  break;
445  case AV_PIX_FMT_GRAY10:
446 #if CONFIG_AV1_NVDEC_HWACCEL
447  *fmtp++ = AV_PIX_FMT_CUDA;
448 #endif
449  break;
450  }
451 
452  *fmtp++ = s->pix_fmt;
453  *fmtp = AV_PIX_FMT_NONE;
454 
455  ret = ff_thread_get_format(avctx, pix_fmts);
456  if (ret < 0)
457  return ret;
458 
459  /**
460  * check if the HW accel is inited correctly. If not, return un-implemented.
461  * Since now the av1 decoder doesn't support native decode, if it will be
462  * implemented in the future, need remove this check.
463  */
464  if (!avctx->hwaccel) {
465  av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
466  " hardware accelerated AV1 decoding.\n");
467  return AVERROR(ENOSYS);
468  }
469 
470  avctx->pix_fmt = ret;
471 
472  return 0;
473 }
474 
476 {
477  ff_thread_release_buffer(avctx, &f->tf);
481  f->raw_frame_header = NULL;
482  f->spatial_id = f->temporal_id = 0;
483  memset(f->skip_mode_frame_idx, 0,
484  2 * sizeof(uint8_t));
485  memset(&f->film_grain, 0, sizeof(f->film_grain));
486  f->coded_lossless = 0;
487 }
488 
489 static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
490 {
491  int ret;
492 
493  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
494  if (ret < 0)
495  return ret;
496 
497  dst->header_ref = av_buffer_ref(src->header_ref);
498  if (!dst->header_ref)
499  goto fail;
500 
502 
503  if (src->hwaccel_picture_private) {
505  if (!dst->hwaccel_priv_buf)
506  goto fail;
508  }
509 
510  dst->spatial_id = src->spatial_id;
511  dst->temporal_id = src->temporal_id;
512  memcpy(dst->gm_type,
513  src->gm_type,
514  AV1_NUM_REF_FRAMES * sizeof(uint8_t));
515  memcpy(dst->gm_params,
516  src->gm_params,
517  AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
518  memcpy(dst->skip_mode_frame_idx,
519  src->skip_mode_frame_idx,
520  2 * sizeof(uint8_t));
521  memcpy(&dst->film_grain,
522  &src->film_grain,
523  sizeof(dst->film_grain));
524  dst->coded_lossless = src->coded_lossless;
525 
526  return 0;
527 
528 fail:
529  av1_frame_unref(avctx, dst);
530  return AVERROR(ENOMEM);
531 }
532 
534 {
535  AV1DecContext *s = avctx->priv_data;
536 
537  for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
538  av1_frame_unref(avctx, &s->ref[i]);
539  av_frame_free(&s->ref[i].tf.f);
540  }
541  av1_frame_unref(avctx, &s->cur_frame);
543 
547 
549  ff_cbs_close(&s->cbc);
550 
551  return 0;
552 }
553 
555  const AV1RawSequenceHeader *seq)
556 {
557  int width = seq->max_frame_width_minus_1 + 1;
558  int height = seq->max_frame_height_minus_1 + 1;
559 
560  avctx->profile = seq->seq_profile;
561  avctx->level = seq->seq_level_idx[0];
562 
563  avctx->color_range =
568 
569  switch (seq->color_config.chroma_sample_position) {
570  case AV1_CSP_VERTICAL:
572  break;
573  case AV1_CSP_COLOCATED:
575  break;
576  }
577 
578  if (avctx->width != width || avctx->height != height) {
579  int ret = ff_set_dimensions(avctx, width, height);
580  if (ret < 0)
581  return ret;
582  }
583  avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
584 
586  seq->timing_info.time_scale) {
587  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
589  seq->timing_info.time_scale,
590  INT_MAX);
593  }
594 
595  return 0;
596 }
597 
599  const AV1RawFrameHeader *header)
600 {
601  AVRational aspect_ratio;
602  int width = header->frame_width_minus_1 + 1;
603  int height = header->frame_height_minus_1 + 1;
604  int r_width = header->render_width_minus_1 + 1;
605  int r_height = header->render_height_minus_1 + 1;
606  int ret;
607 
608  if (avctx->width != width || avctx->height != height) {
609  ret = ff_set_dimensions(avctx, width, height);
610  if (ret < 0)
611  return ret;
612  }
613 
614  av_reduce(&aspect_ratio.num, &aspect_ratio.den,
615  (int64_t)height * r_width,
616  (int64_t)width * r_height,
617  INT_MAX);
618 
619  if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
620  ret = ff_set_sar(avctx, aspect_ratio);
621  if (ret < 0)
622  return ret;
623  }
624 
625  return 0;
626 }
627 
629 {
630  AV1DecContext *s = avctx->priv_data;
632  int ret;
633 
634  s->avctx = avctx;
636 
637  for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
638  s->ref[i].tf.f = av_frame_alloc();
639  if (!s->ref[i].tf.f) {
640  av_log(avctx, AV_LOG_ERROR,
641  "Failed to allocate reference frame buffer %d.\n", i);
642  return AVERROR(ENOMEM);
643  }
644  }
645 
646  s->cur_frame.tf.f = av_frame_alloc();
647  if (!s->cur_frame.tf.f) {
648  av_log(avctx, AV_LOG_ERROR,
649  "Failed to allocate current frame buffer.\n");
650  return AVERROR(ENOMEM);
651  }
652 
653  ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
654  if (ret < 0)
655  return ret;
656 
657  av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
658 
659  if (avctx->extradata && avctx->extradata_size) {
661  &s->current_obu,
662  avctx);
663  if (ret < 0) {
664  av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
665  return ret;
666  }
667 
669  if (!seq) {
670  av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
671  goto end;
672  }
673 
674  ret = set_context_with_sequence(avctx, seq);
675  if (ret < 0) {
676  av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
677  goto end;
678  }
679 
680  end:
682  }
683 
684  return ret;
685 }
686 
688 {
689  AV1DecContext *s = avctx->priv_data;
691  AVFrame *frame;
692  int ret;
693 
694  ret = update_context_with_frame_header(avctx, header);
695  if (ret < 0) {
696  av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
697  return ret;
698  }
699 
701  if (!f->header_ref)
702  return AVERROR(ENOMEM);
703 
705 
706  if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
707  goto fail;
708 
709  frame = f->tf.f;
710  frame->key_frame = header->frame_type == AV1_FRAME_KEY;
711 
712  switch (header->frame_type) {
713  case AV1_FRAME_KEY:
715  frame->pict_type = AV_PICTURE_TYPE_I;
716  break;
717  case AV1_FRAME_INTER:
718  frame->pict_type = AV_PICTURE_TYPE_P;
719  break;
720  case AV1_FRAME_SWITCH:
721  frame->pict_type = AV_PICTURE_TYPE_SP;
722  break;
723  }
724 
725  if (avctx->hwaccel) {
726  const AVHWAccel *hwaccel = avctx->hwaccel;
727  if (hwaccel->frame_priv_data_size) {
728  f->hwaccel_priv_buf =
730  if (!f->hwaccel_priv_buf) {
731  ret = AVERROR(ENOMEM);
732  goto fail;
733  }
735  }
736  }
737  return 0;
738 
739 fail:
740  av1_frame_unref(avctx, f);
741  return ret;
742 }
743 
745 {
746  AV1DecContext *s = avctx->priv_data;
747  const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain;
748  AVFilmGrainParams *fgp;
750 
751  if (!film_grain->apply_grain)
752  return 0;
753 
755  if (!fgp)
756  return AVERROR(ENOMEM);
757 
759  fgp->seed = film_grain->grain_seed;
760 
761  aom = &fgp->codec.aom;
763  aom->scaling_shift = film_grain->grain_scaling_minus_8 + 8;
764  aom->ar_coeff_lag = film_grain->ar_coeff_lag;
765  aom->ar_coeff_shift = film_grain->ar_coeff_shift_minus_6 + 6;
766  aom->grain_scale_shift = film_grain->grain_scale_shift;
767  aom->overlap_flag = film_grain->overlap_flag;
768  aom->limit_output_range = film_grain->clip_to_restricted_range;
769 
770  aom->num_y_points = film_grain->num_y_points;
771  for (int i = 0; i < film_grain->num_y_points; i++) {
772  aom->y_points[i][0] = film_grain->point_y_value[i];
773  aom->y_points[i][1] = film_grain->point_y_scaling[i];
774  }
775  aom->num_uv_points[0] = film_grain->num_cb_points;
776  for (int i = 0; i < film_grain->num_cb_points; i++) {
777  aom->uv_points[0][i][0] = film_grain->point_cb_value[i];
778  aom->uv_points[0][i][1] = film_grain->point_cb_scaling[i];
779  }
780  aom->num_uv_points[1] = film_grain->num_cr_points;
781  for (int i = 0; i < film_grain->num_cr_points; i++) {
782  aom->uv_points[1][i][0] = film_grain->point_cr_value[i];
783  aom->uv_points[1][i][1] = film_grain->point_cr_scaling[i];
784  }
785 
786  for (int i = 0; i < 24; i++) {
787  aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128;
788  }
789  for (int i = 0; i < 25; i++) {
790  aom->ar_coeffs_uv[0][i] = film_grain->ar_coeffs_cb_plus_128[i] - 128;
791  aom->ar_coeffs_uv[1][i] = film_grain->ar_coeffs_cr_plus_128[i] - 128;
792  }
793 
794  aom->uv_mult[0] = film_grain->cb_mult;
795  aom->uv_mult[1] = film_grain->cr_mult;
796  aom->uv_mult_luma[0] = film_grain->cb_luma_mult;
797  aom->uv_mult_luma[1] = film_grain->cr_luma_mult;
798  aom->uv_offset[0] = film_grain->cb_offset;
799  aom->uv_offset[1] = film_grain->cr_offset;
800 
801  return 0;
802 }
803 
805  const AVPacket *pkt, int *got_frame)
806 {
807  AV1DecContext *s = avctx->priv_data;
808  const AVFrame *srcframe = s->cur_frame.tf.f;
809  int ret;
810 
811  // TODO: all layers
812  if (s->operating_point_idc &&
814  return 0;
815 
816  ret = av_frame_ref(frame, srcframe);
817  if (ret < 0)
818  return ret;
819 
821  ret = export_film_grain(avctx, frame);
822  if (ret < 0) {
823  av_frame_unref(frame);
824  return ret;
825  }
826  }
827 
828  frame->pts = pkt->pts;
829  frame->pkt_dts = pkt->dts;
830  frame->pkt_size = pkt->size;
831 
832  *got_frame = 1;
833 
834  return 0;
835 }
836 
838 {
839  AV1DecContext *s = avctx->priv_data;
841  int ret;
842 
843  for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
844  if (header->refresh_frame_flags & (1 << i)) {
845  if (s->ref[i].tf.f->buf[0])
846  av1_frame_unref(avctx, &s->ref[i]);
847  if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
848  av_log(avctx, AV_LOG_ERROR,
849  "Failed to update frame %d in reference list\n", i);
850  return ret;
851  }
852  }
853  }
854  return 0;
855 }
856 
858 {
859  AV1DecContext *s = avctx->priv_data;
860  int ret;
861 
862  if (s->cur_frame.tf.f->buf[0])
863  av1_frame_unref(avctx, &s->cur_frame);
864 
865  ret = av1_frame_alloc(avctx, &s->cur_frame);
866  if (ret < 0) {
867  av_log(avctx, AV_LOG_ERROR,
868  "Failed to allocate space for current frame.\n");
869  return ret;
870  }
871 
872  ret = init_tile_data(s);
873  if (ret < 0) {
874  av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
875  return ret;
876  }
877 
879  skip_mode_params(s);
882 
883  return ret;
884 }
885 
886 static int av1_decode_frame(AVCodecContext *avctx, void *frame,
887  int *got_frame, AVPacket *pkt)
888 {
889  AV1DecContext *s = avctx->priv_data;
890  AV1RawTileGroup *raw_tile_group = NULL;
891  int ret;
892 
893  ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
894  if (ret < 0) {
895  av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
896  goto end;
897  }
898  av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
899  s->current_obu.nb_units);
900 
901  for (int i = 0; i < s->current_obu.nb_units; i++) {
902  CodedBitstreamUnit *unit = &s->current_obu.units[i];
903  AV1RawOBU *obu = unit->content;
904  const AV1RawOBUHeader *header;
905 
906  if (!obu)
907  continue;
908 
909  header = &obu->header;
910  av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
911 
912  switch (unit->type) {
915  s->seq_ref = av_buffer_ref(unit->content_ref);
916  if (!s->seq_ref) {
917  ret = AVERROR(ENOMEM);
918  goto end;
919  }
920 
921  s->raw_seq = &obu->obu.sequence_header;
922 
923  ret = set_context_with_sequence(avctx, s->raw_seq);
924  if (ret < 0) {
925  av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
926  s->raw_seq = NULL;
927  goto end;
928  }
929 
931 
932  if (s->pix_fmt == AV_PIX_FMT_NONE) {
933  ret = get_pixel_format(avctx);
934  if (ret < 0) {
935  av_log(avctx, AV_LOG_ERROR,
936  "Failed to get pixel format.\n");
937  s->raw_seq = NULL;
938  goto end;
939  }
940  }
941 
942  if (avctx->hwaccel && avctx->hwaccel->decode_params) {
943  ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
944  unit->data_size);
945  if (ret < 0) {
946  av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
947  s->raw_seq = NULL;
948  goto end;
949  }
950  }
951  break;
953  if (s->raw_frame_header)
954  break;
955  // fall-through
956  case AV1_OBU_FRAME:
958  if (!s->raw_seq) {
959  av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
960  ret = AVERROR_INVALIDDATA;
961  goto end;
962  }
963 
966  if (!s->header_ref) {
967  ret = AVERROR(ENOMEM);
968  goto end;
969  }
970 
971  if (unit->type == AV1_OBU_FRAME)
972  s->raw_frame_header = &obu->obu.frame.header;
973  else
974  s->raw_frame_header = &obu->obu.frame_header;
975 
977  if (s->cur_frame.tf.f->buf[0])
978  av1_frame_unref(avctx, &s->cur_frame);
979 
980  ret = av1_frame_ref(avctx, &s->cur_frame,
982  if (ret < 0) {
983  av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
984  goto end;
985  }
986 
987  ret = update_reference_list(avctx);
988  if (ret < 0) {
989  av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
990  goto end;
991  }
992 
993  ret = set_output_frame(avctx, frame, pkt, got_frame);
994  if (ret < 0)
995  av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
996 
997  s->raw_frame_header = NULL;
998 
999  goto end;
1000  }
1001 
1002  ret = get_current_frame(avctx);
1003  if (ret < 0) {
1004  av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
1005  goto end;
1006  }
1007 
1008  s->cur_frame.spatial_id = header->spatial_id;
1009  s->cur_frame.temporal_id = header->temporal_id;
1010 
1011  if (avctx->hwaccel) {
1012  ret = avctx->hwaccel->start_frame(avctx, unit->data,
1013  unit->data_size);
1014  if (ret < 0) {
1015  av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
1016  goto end;
1017  }
1018  }
1019  if (unit->type != AV1_OBU_FRAME)
1020  break;
1021  // fall-through
1022  case AV1_OBU_TILE_GROUP:
1023  if (!s->raw_frame_header) {
1024  av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
1025  ret = AVERROR_INVALIDDATA;
1026  goto end;
1027  }
1028 
1029  if (unit->type == AV1_OBU_FRAME)
1030  raw_tile_group = &obu->obu.frame.tile_group;
1031  else
1032  raw_tile_group = &obu->obu.tile_group;
1033 
1034  ret = get_tiles_info(avctx, raw_tile_group);
1035  if (ret < 0)
1036  goto end;
1037 
1038  if (avctx->hwaccel) {
1039  ret = avctx->hwaccel->decode_slice(avctx,
1040  raw_tile_group->tile_data.data,
1041  raw_tile_group->tile_data.data_size);
1042  if (ret < 0) {
1043  av_log(avctx, AV_LOG_ERROR,
1044  "HW accel decode slice fail.\n");
1045  goto end;
1046  }
1047  }
1048  break;
1049  case AV1_OBU_TILE_LIST:
1051  case AV1_OBU_PADDING:
1052  case AV1_OBU_METADATA:
1053  break;
1054  default:
1055  av_log(avctx, AV_LOG_DEBUG,
1056  "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
1057  unit->type, unit->data_size);
1058  }
1059 
1060  if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
1061  if (avctx->hwaccel) {
1062  ret = avctx->hwaccel->end_frame(avctx);
1063  if (ret < 0) {
1064  av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
1065  goto end;
1066  }
1067  }
1068 
1069  ret = update_reference_list(avctx);
1070  if (ret < 0) {
1071  av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
1072  goto end;
1073  }
1074 
1075  if (s->raw_frame_header->show_frame) {
1076  ret = set_output_frame(avctx, frame, pkt, got_frame);
1077  if (ret < 0) {
1078  av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
1079  goto end;
1080  }
1081  }
1082  raw_tile_group = NULL;
1083  s->raw_frame_header = NULL;
1084  }
1085  }
1086 
1087 end:
1089  if (ret < 0)
1090  s->raw_frame_header = NULL;
1091  return ret;
1092 }
1093 
1095 {
1096  AV1DecContext *s = avctx->priv_data;
1097 
1098  for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
1099  av1_frame_unref(avctx, &s->ref[i]);
1100 
1101  av1_frame_unref(avctx, &s->cur_frame);
1102  s->operating_point_idc = 0;
1103  s->raw_frame_header = NULL;
1104  s->raw_seq = NULL;
1105 
1106  ff_cbs_flush(s->cbc);
1107 }
1108 
1109 #define OFFSET(x) offsetof(AV1DecContext, x)
1110 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1111 static const AVOption av1_options[] = {
1112  { "operating_point", "Select an operating point of the scalable bitstream",
1113  OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD },
1114  { NULL }
1115 };
1116 
1117 static const AVClass av1_class = {
1118  .class_name = "AV1 decoder",
1119  .item_name = av_default_item_name,
1120  .option = av1_options,
1121  .version = LIBAVUTIL_VERSION_INT,
1122 };
1123 
1125  .name = "av1",
1126  .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
1127  .type = AVMEDIA_TYPE_VIDEO,
1128  .id = AV_CODEC_ID_AV1,
1129  .priv_data_size = sizeof(AV1DecContext),
1130  .init = av1_decode_init,
1131  .close = av1_decode_free,
1133  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
1134  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1139  .priv_class = &av1_class,
1140  .hw_configs = (const AVCodecHWConfigInternal *const []) {
1141 #if CONFIG_AV1_DXVA2_HWACCEL
1142  HWACCEL_DXVA2(av1),
1143 #endif
1144 #if CONFIG_AV1_D3D11VA_HWACCEL
1145  HWACCEL_D3D11VA(av1),
1146 #endif
1147 #if CONFIG_AV1_D3D11VA2_HWACCEL
1148  HWACCEL_D3D11VA2(av1),
1149 #endif
1150 #if CONFIG_AV1_NVDEC_HWACCEL
1151  HWACCEL_NVDEC(av1),
1152 #endif
1153 #if CONFIG_AV1_VAAPI_HWACCEL
1154  HWACCEL_VAAPI(av1),
1155 #endif
1156  NULL
1157  },
1158 };
#define HWACCEL_MAX
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
uint8_t clip_to_restricted_range
Definition: cbs_av1.h:162
uint8_t coded_lossless
Definition: av1dec.h:52
static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp, int mx, int r)
Definition: av1dec.c:41
The union is valid when interpreted as AVFilmGrainAOMParams (codec.aom)
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:2084
int nb_units
Number of units in this fragment.
Definition: cbs.h:149
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static enum AVPixelFormat pix_fmt
uint16_t render_width_minus_1
Definition: cbs_av1.h:193
int8_t ref_frame_idx[AV1_REFS_PER_FRAME]
Definition: cbs_av1.h:204
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:607
static int update_context_with_frame_header(AVCodecContext *avctx, const AV1RawFrameHeader *header)
Definition: av1dec.c:598
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
static void skip_mode_params(AV1DecContext *s)
Definition: av1dec.c:158
AV1RawFrameHeader * raw_frame_header
Definition: av1dec.h:73
uint8_t cb_luma_mult
Definition: cbs_av1.h:156
int ar_coeff_lag
Specifies the auto-regression lag.
AVOption.
Definition: opt.h:248
static void flush(AVCodecContext *avctx)
uint16_t grain_seed
Definition: cbs_av1.h:135
int num_uv_points[2]
If chroma_scaling_from_luma is set to 0, signals the chroma scaling function parameters.
uint64_t seed
Seed to use for the synthesis process, if the codec allows for it.
This structure describes how to handle film grain synthesis in video for specific codecs...
#define VD
Definition: av1dec.c:1110
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
AVFrame * f
Definition: thread.h:35
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint32_t time_scale
Definition: cbs_av1.h:60
uint8_t refresh_frame_flags
Definition: cbs_av1.h:198
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:106
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:505
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t mono_chrome
Definition: cbs_av1.h:44
int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:75
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:70
AVBufferRef * header_ref
Definition: av1dec.h:72
uint8_t ar_coeffs_cb_plus_128[25]
Definition: cbs_av1.h:151
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1166
static void av1_decode_flush(AVCodecContext *avctx)
Definition: av1dec.c:1094
int num
Numerator.
Definition: rational.h:59
#define HWACCEL_NVDEC(codec)
Definition: hwconfig.h:71
int size
Definition: packet.h:364
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int av_log2(unsigned v)
Definition: intmath.c:26
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:910
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:36
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
uint8_t primary_ref_frame
Definition: cbs_av1.h:187
uint8_t allow_high_precision_mv
Definition: cbs_av1.h:207
static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
Definition: av1dec.c:744
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
uint16_t tg_end
Definition: av1dec.h:77
GLint GLenum type
Definition: opengl_enc.c:104
uint8_t order_hint_bits_minus_1
Definition: cbs_av1.h:122
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
uint32_t num_units_in_display_tick
Definition: cbs_av1.h:59
uint32_t gm_params[AV1_TOTAL_REFS_PER_FRAME][6]
Definition: cbs_av1.h:284
static AVPacket pkt
uint8_t color_range
Definition: cbs_av1.h:51
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1695
This structure describes how to handle film grain synthesis for AOM codecs.
int profile
profile
Definition: avcodec.h:1871
uint16_t frame_width_minus_1
Definition: cbs_av1.h:188
AVCodec.
Definition: codec.h:190
uint8_t seq_profile
Definition: cbs_av1.h:74
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
uint8_t overlap_flag
Definition: cbs_av1.h:161
int grain_scale_shift
Signals the down shift applied to the generated gaussian numbers during synthesis.
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
uint16_t cr_offset
Definition: cbs_av1.h:160
AV1Frame cur_frame
Definition: av1dec.h:82
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
uint8_t point_cr_scaling[10]
Definition: cbs_av1.h:147
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
TileGroupInfo * tile_group_info
Definition: av1dec.h:74
uint16_t tile_rows
Definition: cbs_av1.h:226
int limit_output_range
Signals to clip to limited color levels after film grain application.
uint8_t order_hint
Definition: cbs_av1.h:182
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame...
Definition: avcodec.h:2369
uint8_t chroma_scaling_from_luma
Definition: cbs_av1.h:141
uint8_t
static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
Definition: av1dec.c:58
#define av_cold
Definition: attributes.h:88
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
int(* decode_params)(AVCodecContext *avctx, int type, const uint8_t *buf, uint32_t buf_size)
Callback for parameter data (SPS/PPS/VPS etc).
Definition: avcodec.h:2501
int scaling_shift
Specifies the shift applied to the chroma components.
AVOptions.
uint8_t * data
Definition: cbs_av1.h:290
uint16_t max_frame_width_minus_1
Definition: cbs_av1.h:98
#define f(width, name)
Definition: cbs_vp9.c:255
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:407
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:632
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:288
uint8_t grain_scale_shift
Definition: cbs_av1.h:154
#define height
uint16_t cb_offset
Definition: cbs_av1.h:157
static const AVClass av1_class
Definition: av1dec.c:1117
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:121
uint8_t num_y_points
Definition: cbs_av1.h:138
uint8_t high_bitdepth
Definition: cbs_av1.h:42
Coded bitstream unit structure.
Definition: cbs.h:66
ptrdiff_t size
Definition: opengl_enc.c:100
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:103
static const uint8_t header[24]
Definition: sdr2.c:67
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:164
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:77
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1173
#define av_log(a,...)
int num_y_points
Number of points, and the scale and value for each point of the piecewise linear scaling function for...
void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment&#39;s own data buffer, but not the units a...
Definition: cbs.c:156
uint8_t y_points[14][2]
int8_t delta_q_v_dc
Definition: cbs_av1.h:233
#define HWACCEL_D3D11VA(codec)
Definition: hwconfig.h:79
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
#define OFFSET(x)
Definition: av1dec.c:1109
#define src
Definition: vp8dsp.c:255
int ar_coeff_shift
Specifies the range of the auto-regressive coefficients.
uint16_t operating_point_idc[AV1_MAX_OPERATING_POINTS]
Definition: cbs_av1.h:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int temporal_id
Definition: av1dec.h:42
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
AV1RawTileData tile_data
Definition: cbs_av1.h:300
uint8_t temporal_id
Definition: cbs_av1.h:36
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
uint16_t frame_height_minus_1
Definition: cbs_av1.h:189
uint8_t twelve_bit
Definition: cbs_av1.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
AV1RawFrame frame
Definition: cbs_av1.h:399
const char * r
Definition: vf_curves.c:114
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
uint8_t cr_luma_mult
Definition: cbs_av1.h:159
static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low, int high, int r)
Definition: av1dec.c:51
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:586
uint8_t point_cr_value[10]
Definition: cbs_av1.h:146
int chroma_scaling_from_luma
Signals whether to derive the chroma scaling function from the luma.
const char * name
Name of the codec implementation.
Definition: codec.h:197
static av_always_inline av_const double round(double x)
Definition: libm.h:444
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
static int get_pixel_format(AVCodecContext *avctx)
Definition: av1dec.c:335
union AV1RawOBU::@26 obu
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:123
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
Definition: mem.c:206
AV1RawColorConfig color_config
Definition: cbs_av1.h:128
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in AVCodec caps_internal and use ff_thread_get_buffer() to allocate frames.The frames must then be freed with ff_thread_release_buffer().Otherwise decode directly into the user-supplied frames.Call ff_thread_report_progress() after some part of the current picture has decoded.A good place to put this is where draw_horiz_band() is called-add this if it isn't called anywhere
uint16_t tg_start
Definition: cbs_av1.h:297
ThreadFrame tf
Definition: av1dec.h:34
static av_cold int av1_decode_init(AVCodecContext *avctx)
Definition: av1dec.c:628
CodedBitstreamFragment current_obu
Definition: av1dec.h:68
#define b
Definition: input.c:41
AVFilmGrainParams * av_film_grain_params_create_side_data(AVFrame *frame)
Allocate a complete AVFilmGrainParams and add it to the frame.
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:397
uint16_t tg_start
Definition: av1dec.h:76
#define FFMIN(a, b)
Definition: common.h:96
uint8_t frame_type
Definition: cbs_av1.h:171
#define width
int width
picture width / height.
Definition: avcodec.h:704
uint8_t film_grain_params_ref_idx
Definition: cbs_av1.h:137
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:254
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:609
AV1RawOBUHeader header
Definition: cbs_av1.h:392
uint8_t is_global[AV1_TOTAL_REFS_PER_FRAME]
Definition: cbs_av1.h:280
int32_t
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1145
static int get_current_frame(AVCodecContext *avctx)
Definition: av1dec.c:857
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
int level
level
Definition: avcodec.h:1997
int uv_mult[2]
Specifies the luma/chroma multipliers for the index to the component scaling function.
uint32_t tile_offset
Definition: av1dec.h:56
uint8_t color_primaries
Definition: cbs_av1.h:47
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:663
static int set_context_with_sequence(AVCodecContext *avctx, const AV1RawSequenceHeader *seq)
Definition: av1dec.c:554
uint8_t ar_coeff_lag
Definition: cbs_av1.h:149
uint16_t render_height_minus_1
Definition: cbs_av1.h:194
uint8_t ar_coeffs_cr_plus_128[25]
Definition: cbs_av1.h:152
AV1RawFilmGrainParams film_grain
Definition: av1dec.h:50
int overlap_flag
Signals whether to overlap film grain blocks.
uint8_t grain_scaling_minus_8
Definition: cbs_av1.h:148
HW acceleration through CUDA.
Definition: pixfmt.h:235
#define HWACCEL_DXVA2(codec)
Definition: hwconfig.h:67
#define FF_ARRAY_ELEMS(a)
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:1893
size_t data_size
Definition: cbs_av1.h:292
Full range content.
Definition: pixfmt.h:586
int operating_point_idc
Definition: av1dec.h:79
int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecContext *avctx)
Read the extradata bitstream found in a codec context into a fragment, then split into units and deco...
Definition: cbs.c:279
static void coded_lossless_param(AV1DecContext *s)
Definition: av1dec.c:222
uint8_t point_cb_scaling[10]
Definition: cbs_av1.h:144
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
uint8_t skip_mode_frame_idx[2]
Definition: av1dec.h:48
static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
Definition: av1dec.c:687
int8_t ar_coeffs_uv[2][25]
Chroma auto-regression coefficients.
uint8_t subsampling_y
Definition: cbs_av1.h:53
#define AV_CODEC_EXPORT_DATA_FILM_GRAIN
Decoding only.
Definition: avcodec.h:417
static int FUNC() sequence_header(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawSequenceHeader *current)
uint16_t max_frame_height_minus_1
Definition: cbs_av1.h:99
Libavcodec external API header.
uint8_t ar_coeff_shift_minus_6
Definition: cbs_av1.h:153
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:2526
static int get_relative_dist(const AV1RawSequenceHeader *seq, unsigned int a, unsigned int b)
Definition: av1dec.c:150
const AVProfile ff_av1_profiles[]
Definition: profiles.c:147
int8_t delta_q_u_dc
Definition: cbs_av1.h:231
uint8_t subsampling_x
Definition: cbs_av1.h:52
main external API structure.
Definition: avcodec.h:531
uint8_t * data
The data buffer.
Definition: buffer.h:89
int operating_point
Definition: av1dec.h:85
int extradata_size
Definition: avcodec.h:633
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
static const AVOption av1_options[]
Definition: av1dec.c:1111
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
Switching Predicted.
Definition: avutil.h:279
Describe the class of an AVClass context structure.
Definition: log.h:67
static const AVProfile profiles[]
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1159
Rational number (pair of numerator and denominator).
Definition: rational.h:58
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1152
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:108
static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
Definition: av1dec.c:489
AVBufferRef * header_ref
Definition: av1dec.h:39
#define HWACCEL_D3D11VA2(codec)
Definition: hwconfig.h:69
uint8_t show_existing_frame
Definition: cbs_av1.h:166
union AVFilmGrainParams::@296 codec
Additional fields may be added both here and in any structure included.
int16_t feature_value[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX]
Definition: cbs_av1.h:245
static uint32_t inverse_recenter(int r, uint32_t v)
Definition: av1dec.c:31
AV1RawTileGroup tile_group
Definition: cbs_av1.h:305
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:126
int8_t delta_q_u_ac
Definition: cbs_av1.h:232
uint8_t base_q_idx
Definition: cbs_av1.h:228
uint8_t spatial_id
Definition: cbs_av1.h:37
uint8_t feature_enabled[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX]
Definition: cbs_av1.h:244
uint8_t point_cb_value[10]
Definition: cbs_av1.h:143
AVCodec ff_av1_decoder
Definition: av1dec.c:1124
uint8_t point_y_scaling[14]
Definition: cbs_av1.h:140
uint32_t num_ticks_per_picture_minus_1
Definition: cbs_av1.h:63
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:137
AV1RawSequenceHeader sequence_header
Definition: cbs_av1.h:397
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
uint8_t equal_picture_interval
Definition: cbs_av1.h:62
uint8_t tile_size_bytes_minus1
Definition: cbs_av1.h:221
enum AVFilmGrainParamsType type
Specifies the codec for which this structure is valid.
int32_t gm_params[AV1_NUM_REF_FRAMES][6]
Definition: av1dec.h:46
CodedBitstreamContext * cbc
Definition: av1dec.h:67
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
#define SIZE_SPECIFIER
Definition: internal.h:191
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define FF_CODEC_CAP_SETS_PKT_DTS
Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set AVFrame.pkt_dts manually.
Definition: internal.h:55
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:2515
uint16_t tile_cols
Definition: cbs_av1.h:225
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
AVBufferRef * seq_ref
Definition: av1dec.h:70
uint8_t update_grain
Definition: cbs_av1.h:136
static void global_motion_params(AV1DecContext *s)
update gm type/params, since cbs already implemented part of this funcation, so we don&#39;t need to full...
Definition: av1dec.c:104
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:313
Narrow or limited range content.
Definition: pixfmt.h:569
int(* start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Called at the beginning of each frame or field picture.
Definition: avcodec.h:2487
void * priv_data
Internal codec-specific data.
Definition: cbs.h:191
uint16_t tile_column
Definition: av1dec.h:59
enum AVPixelFormat pix_fmt
Definition: av1dec.h:66
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:423
AV1RawFilmGrainParams film_grain
Definition: cbs_av1.h:286
AV1Frame ref[AV1_NUM_REF_FRAMES]
Definition: av1dec.h:81
AVFilmGrainAOMParams aom
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
uint8_t is_translation[AV1_TOTAL_REFS_PER_FRAME]
Definition: cbs_av1.h:282
common internal api header.
uint16_t tile_row
Definition: av1dec.h:58
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
Definition: av1dec.c:475
AVCodecContext * avctx
Definition: av1dec.h:64
int den
Denominator.
Definition: rational.h:60
static int av1_decode_frame(AVCodecContext *avctx, void *frame, int *got_frame, AVPacket *pkt)
Definition: av1dec.c:886
static int update_reference_list(AVCodecContext *avctx)
Definition: av1dec.c:837
AV1RawSequenceHeader * raw_seq
Definition: av1dec.h:71
void * priv_data
Definition: avcodec.h:558
int8_t delta_q_v_ac
Definition: cbs_av1.h:234
uint8_t point_y_value[14]
Definition: cbs_av1.h:139
static void load_grain_params(AV1DecContext *s)
Definition: av1dec.c:252
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int uv_offset[2]
Offset used for component scaling function.
static int set_output_frame(AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt, int *got_frame)
Definition: av1dec.c:804
#define HWACCEL_VAAPI(codec)
Definition: hwconfig.h:73
int8_t delta_q_y_dc
Definition: cbs_av1.h:229
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:2535
uint16_t tg_end
Definition: cbs_av1.h:298
uint8_t skip_mode_present
Definition: cbs_av1.h:275
int8_t ar_coeffs_y[24]
Luma auto-regression coefficients.
uint8_t is_rot_zoom[AV1_TOTAL_REFS_PER_FRAME]
Definition: cbs_av1.h:281
#define AV_CODEC_CAP_AVOID_PROBING
Decoder is not a preferred choice for probing.
Definition: codec.h:132
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:392
uint8_t chroma_sample_position
Definition: cbs_av1.h:54
uint32_t tile_size
Definition: av1dec.h:57
void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:170
AV1RawTileGroup tile_group
Definition: cbs_av1.h:400
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:362
uint16_t tile_num
Definition: av1dec.h:75
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:398
AV1RawFrameHeader * raw_frame_header
Definition: av1dec.h:40
static int init_tile_data(AV1DecContext *s)
Definition: av1dec.c:272
static av_cold int av1_decode_free(AVCodecContext *avctx)
Definition: av1dec.c:533
int spatial_id
Definition: av1dec.h:43
#define av_freep(p)
static float sub(float src0, float src1)
HW decoding through Direct3D11 via old API, Picture.data[3] contains a ID3D11VideoDecoderOutputView p...
Definition: pixfmt.h:229
void ff_cbs_flush(CodedBitstreamContext *ctx)
Reset all internal state in a context.
Definition: cbs.c:120
AV1RawTimingInfo timing_info
Definition: cbs_av1.h:83
uint8_t show_frame
Definition: cbs_av1.h:172
uint8_t gm_type[AV1_NUM_REF_FRAMES]
Definition: av1dec.h:45
uint8_t frame_to_show_map_idx
Definition: cbs_av1.h:167
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2489
static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
Definition: av1dec.c:290
uint8_t uv_points[2][10][2]
uint8_t num_cr_points
Definition: cbs_av1.h:145
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
void * hwaccel_picture_private
Definition: av1dec.h:37
AV1RawFrameHeader header
Definition: cbs_av1.h:304
uint8_t num_cb_points
Definition: cbs_av1.h:142
int pkt_size
size of the corresponding packet containing the compressed frame.
Definition: frame.h:629
uint8_t ar_coeffs_y_plus_128[24]
Definition: cbs_av1.h:150
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: packet.h:340
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:514
uint8_t seq_level_idx[AV1_MAX_OPERATING_POINTS]
Definition: cbs_av1.h:87
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
uint8_t transfer_characteristics
Definition: cbs_av1.h:48
int i
Definition: input.c:407
Predicted.
Definition: avutil.h:275
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:82
FF_DISABLE_DEPRECATION_WARNINGS enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
AVBufferRef * hwaccel_priv_buf
Definition: av1dec.h:36