4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/pixdesc.h"
24 #include "bytestream.h"
29 static uint32_t inverse_recenter(int r, uint32_t v)
34 return r - ((v + 1) >> 1);
39 static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
43 return inverse_recenter(r, sub_exp);
45 return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
49 static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
52 int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
56 static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
58 uint8_t primary_frame, prev_frame;
59 uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;
60 int32_t r, prev_gm_param;
62 primary_frame = s->raw_frame_header->primary_ref_frame;
63 prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
64 abs_bits = AV1_GM_ABS_ALPHA_BITS;
65 prec_bits = AV1_GM_ALPHA_PREC_BITS;
67 /* setup_past_independence() sets PrevGmParams to default values. We can
68 * simply point to the current's frame gm_params as they will be initialized
69 * with defaults at this point.
71 if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
72 prev_gm_param = s->cur_frame.gm_params[ref][idx];
74 prev_gm_param = s->ref[prev_frame].gm_params[ref][idx];
77 if (type == AV1_WARP_MODEL_TRANSLATION) {
78 abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
79 !s->raw_frame_header->allow_high_precision_mv;
80 prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
81 !s->raw_frame_header->allow_high_precision_mv;
83 abs_bits = AV1_GM_ABS_TRANS_BITS;
84 prec_bits = AV1_GM_TRANS_PREC_BITS;
87 round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
88 prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;
89 sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
91 r = (prev_gm_param >> prec_diff) - sub;
93 s->cur_frame.gm_params[ref][idx] =
94 (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],
95 -mx, mx + 1, r) << prec_diff) + round;
99 * update gm type/params, since cbs already implemented part of this funcation,
100 * so we don't need to full implement spec.
102 static void global_motion_params(AV1DecContext *s)
104 const AV1RawFrameHeader *header = s->raw_frame_header;
107 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
108 s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;
109 for (int i = 0; i < 6; i++)
110 s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?
111 1 << AV1_WARPEDMODEL_PREC_BITS : 0;
113 if (header->frame_type == AV1_FRAME_KEY ||
114 header->frame_type == AV1_FRAME_INTRA_ONLY)
117 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
118 if (header->is_global[ref]) {
119 if (header->is_rot_zoom[ref]) {
120 type = AV1_WARP_MODEL_ROTZOOM;
122 type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
123 : AV1_WARP_MODEL_AFFINE;
126 type = AV1_WARP_MODEL_IDENTITY;
128 s->cur_frame.gm_type[ref] = type;
130 if (type >= AV1_WARP_MODEL_ROTZOOM) {
131 read_global_param(s, type, ref, 2);
132 read_global_param(s, type, ref, 3);
133 if (type == AV1_WARP_MODEL_AFFINE) {
134 read_global_param(s, type, ref, 4);
135 read_global_param(s, type, ref, 5);
137 s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];
138 s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];
141 if (type >= AV1_WARP_MODEL_TRANSLATION) {
142 read_global_param(s, type, ref, 0);
143 read_global_param(s, type, ref, 1);
148 static int get_relative_dist(const AV1RawSequenceHeader *seq,
149 unsigned int a, unsigned int b)
151 unsigned int diff = a - b;
152 unsigned int m = 1 << seq->order_hint_bits_minus_1;
153 return (diff & (m - 1)) - (diff & m);
156 static void skip_mode_params(AV1DecContext *s)
158 const AV1RawFrameHeader *header = s->raw_frame_header;
159 const AV1RawSequenceHeader *seq = s->raw_seq;
161 int forward_idx, backward_idx;
162 int forward_hint, backward_hint;
163 int second_forward_idx, second_forward_hint;
164 int ref_hint, dist, i;
166 if (!header->skip_mode_present)
171 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
172 ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
173 dist = get_relative_dist(seq, ref_hint, header->order_hint);
175 if (forward_idx < 0 ||
176 get_relative_dist(seq, ref_hint, forward_hint) > 0) {
178 forward_hint = ref_hint;
180 } else if (dist > 0) {
181 if (backward_idx < 0 ||
182 get_relative_dist(seq, ref_hint, backward_hint) < 0) {
184 backward_hint = ref_hint;
189 if (forward_idx < 0) {
191 } else if (backward_idx >= 0) {
192 s->cur_frame.skip_mode_frame_idx[0] =
193 AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx);
194 s->cur_frame.skip_mode_frame_idx[1] =
195 AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx);
199 second_forward_idx = -1;
200 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
201 ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
202 if (get_relative_dist(seq, ref_hint, forward_hint) < 0) {
203 if (second_forward_idx < 0 ||
204 get_relative_dist(seq, ref_hint, second_forward_hint) > 0) {
205 second_forward_idx = i;
206 second_forward_hint = ref_hint;
211 if (second_forward_idx < 0)
214 s->cur_frame.skip_mode_frame_idx[0] =
215 AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx);
216 s->cur_frame.skip_mode_frame_idx[1] =
217 AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx);
220 static void coded_lossless_param(AV1DecContext *s)
222 const AV1RawFrameHeader *header = s->raw_frame_header;
225 if (header->delta_q_y_dc || header->delta_q_u_ac ||
226 header->delta_q_u_dc || header->delta_q_v_ac ||
227 header->delta_q_v_dc) {
228 s->cur_frame.coded_lossless = 0;
232 s->cur_frame.coded_lossless = 1;
233 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
235 if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
236 qindex = (header->base_q_idx +
237 header->feature_value[i][AV1_SEG_LVL_ALT_Q]);
239 qindex = header->base_q_idx;
241 qindex = av_clip_uintp2(qindex, 8);
244 s->cur_frame.coded_lossless = 0;
250 static void load_grain_params(AV1DecContext *s)
252 const AV1RawFrameHeader *header = s->raw_frame_header;
253 const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src;
254 AV1RawFilmGrainParams *dst = &s->cur_frame.film_grain;
256 if (!film_grain->apply_grain)
259 if (film_grain->update_grain) {
260 memcpy(dst, film_grain, sizeof(*dst));
264 src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain;
266 memcpy(dst, src, sizeof(*dst));
267 dst->grain_seed = film_grain->grain_seed;
270 static int init_tile_data(AV1DecContext *s)
274 s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;
275 if (s->tile_num < cur_tile_num) {
276 int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
277 sizeof(TileGroupInfo));
283 s->tile_num = cur_tile_num;
288 static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
290 AV1DecContext *s = avctx->priv_data;
292 uint16_t tile_num, tile_row, tile_col;
293 uint32_t size = 0, size_bytes = 0;
295 bytestream2_init(&gb, tile_group->tile_data.data,
296 tile_group->tile_data.data_size);
297 s->tg_start = tile_group->tg_start;
298 s->tg_end = tile_group->tg_end;
300 for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
301 tile_row = tile_num / s->raw_frame_header->tile_cols;
302 tile_col = tile_num % s->raw_frame_header->tile_cols;
304 if (tile_num == tile_group->tg_end) {
305 s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
306 s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
307 s->tile_group_info[tile_num].tile_row = tile_row;
308 s->tile_group_info[tile_num].tile_column = tile_col;
311 size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
312 if (bytestream2_get_bytes_left(&gb) < size_bytes)
313 return AVERROR_INVALIDDATA;
315 for (int i = 0; i < size_bytes; i++)
316 size |= bytestream2_get_byteu(&gb) << 8 * i;
317 if (bytestream2_get_bytes_left(&gb) <= size)
318 return AVERROR_INVALIDDATA;
321 s->tile_group_info[tile_num].tile_size = size;
322 s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
323 s->tile_group_info[tile_num].tile_row = tile_row;
324 s->tile_group_info[tile_num].tile_column = tile_col;
326 bytestream2_skipu(&gb, size);
333 static int get_pixel_format(AVCodecContext *avctx)
335 AV1DecContext *s = avctx->priv_data;
336 const AV1RawSequenceHeader *seq = s->raw_seq;
339 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
340 #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
341 CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
342 CONFIG_AV1_NVDEC_HWACCEL + \
343 CONFIG_AV1_VAAPI_HWACCEL)
344 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
346 if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
347 bit_depth = seq->color_config.twelve_bit ? 12 : 10;
348 else if (seq->seq_profile <= 2)
349 bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
351 av_log(avctx, AV_LOG_ERROR,
352 "Unknown AV1 profile %d.\n", seq->seq_profile);
356 if (!seq->color_config.mono_chrome) {
357 // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
358 if (seq->color_config.subsampling_x == 0 &&
359 seq->color_config.subsampling_y == 0) {
361 pix_fmt = AV_PIX_FMT_YUV444P;
362 else if (bit_depth == 10)
363 pix_fmt = AV_PIX_FMT_YUV444P10;
364 else if (bit_depth == 12)
365 pix_fmt = AV_PIX_FMT_YUV444P12;
367 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
368 } else if (seq->color_config.subsampling_x == 1 &&
369 seq->color_config.subsampling_y == 0) {
371 pix_fmt = AV_PIX_FMT_YUV422P;
372 else if (bit_depth == 10)
373 pix_fmt = AV_PIX_FMT_YUV422P10;
374 else if (bit_depth == 12)
375 pix_fmt = AV_PIX_FMT_YUV422P12;
377 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
378 } else if (seq->color_config.subsampling_x == 1 &&
379 seq->color_config.subsampling_y == 1) {
381 pix_fmt = AV_PIX_FMT_YUV420P;
382 else if (bit_depth == 10)
383 pix_fmt = AV_PIX_FMT_YUV420P10;
384 else if (bit_depth == 12)
385 pix_fmt = AV_PIX_FMT_YUV420P12;
387 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
391 pix_fmt = AV_PIX_FMT_GRAY8;
392 else if (bit_depth == 10)
393 pix_fmt = AV_PIX_FMT_GRAY10;
394 else if (bit_depth == 12)
395 pix_fmt = AV_PIX_FMT_GRAY12;
397 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
400 av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
401 av_get_pix_fmt_name(pix_fmt));
403 if (pix_fmt == AV_PIX_FMT_NONE)
405 s->pix_fmt = pix_fmt;
407 switch (s->pix_fmt) {
408 case AV_PIX_FMT_YUV420P:
409 #if CONFIG_AV1_DXVA2_HWACCEL
410 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
412 #if CONFIG_AV1_D3D11VA_HWACCEL
413 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
414 *fmtp++ = AV_PIX_FMT_D3D11;
416 #if CONFIG_AV1_NVDEC_HWACCEL
417 *fmtp++ = AV_PIX_FMT_CUDA;
419 #if CONFIG_AV1_VAAPI_HWACCEL
420 *fmtp++ = AV_PIX_FMT_VAAPI;
423 case AV_PIX_FMT_YUV420P10:
424 #if CONFIG_AV1_DXVA2_HWACCEL
425 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
427 #if CONFIG_AV1_D3D11VA_HWACCEL
428 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
429 *fmtp++ = AV_PIX_FMT_D3D11;
431 #if CONFIG_AV1_NVDEC_HWACCEL
432 *fmtp++ = AV_PIX_FMT_CUDA;
434 #if CONFIG_AV1_VAAPI_HWACCEL
435 *fmtp++ = AV_PIX_FMT_VAAPI;
438 case AV_PIX_FMT_GRAY8:
439 #if CONFIG_AV1_NVDEC_HWACCEL
440 *fmtp++ = AV_PIX_FMT_CUDA;
443 case AV_PIX_FMT_GRAY10:
444 #if CONFIG_AV1_NVDEC_HWACCEL
445 *fmtp++ = AV_PIX_FMT_CUDA;
450 *fmtp++ = s->pix_fmt;
451 *fmtp = AV_PIX_FMT_NONE;
453 ret = ff_thread_get_format(avctx, pix_fmts);
458 * check if the HW accel is inited correctly. If not, return un-implemented.
459 * Since now the av1 decoder doesn't support native decode, if it will be
460 * implemented in the future, need remove this check.
462 if (!avctx->hwaccel) {
463 av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
464 " hardware accelerated AV1 decoding.\n");
465 return AVERROR(ENOSYS);
468 avctx->pix_fmt = ret;
473 static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
475 ff_thread_release_buffer(avctx, &f->tf);
476 av_buffer_unref(&f->hwaccel_priv_buf);
477 f->hwaccel_picture_private = NULL;
478 av_buffer_unref(&f->header_ref);
479 f->raw_frame_header = NULL;
480 f->spatial_id = f->temporal_id = 0;
481 memset(f->skip_mode_frame_idx, 0,
482 2 * sizeof(uint8_t));
483 memset(&f->film_grain, 0, sizeof(f->film_grain));
484 f->coded_lossless = 0;
487 static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
491 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
495 dst->header_ref = av_buffer_ref(src->header_ref);
496 if (!dst->header_ref)
499 dst->raw_frame_header = src->raw_frame_header;
501 if (src->hwaccel_picture_private) {
502 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
503 if (!dst->hwaccel_priv_buf)
505 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
508 dst->spatial_id = src->spatial_id;
509 dst->temporal_id = src->temporal_id;
512 AV1_NUM_REF_FRAMES * sizeof(uint8_t));
513 memcpy(dst->gm_params,
515 AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
516 memcpy(dst->skip_mode_frame_idx,
517 src->skip_mode_frame_idx,
518 2 * sizeof(uint8_t));
519 memcpy(&dst->film_grain,
521 sizeof(dst->film_grain));
522 dst->coded_lossless = src->coded_lossless;
527 av1_frame_unref(avctx, dst);
528 return AVERROR(ENOMEM);
531 static av_cold int av1_decode_free(AVCodecContext *avctx)
533 AV1DecContext *s = avctx->priv_data;
535 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
536 av1_frame_unref(avctx, &s->ref[i]);
537 av_frame_free(&s->ref[i].tf.f);
539 av1_frame_unref(avctx, &s->cur_frame);
540 av_frame_free(&s->cur_frame.tf.f);
542 av_buffer_unref(&s->seq_ref);
543 av_buffer_unref(&s->header_ref);
544 av_freep(&s->tile_group_info);
546 ff_cbs_fragment_free(&s->current_obu);
547 ff_cbs_close(&s->cbc);
552 static int set_context_with_sequence(AVCodecContext *avctx,
553 const AV1RawSequenceHeader *seq)
555 int width = seq->max_frame_width_minus_1 + 1;
556 int height = seq->max_frame_height_minus_1 + 1;
558 avctx->profile = seq->seq_profile;
559 avctx->level = seq->seq_level_idx[0];
562 seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
563 avctx->color_primaries = seq->color_config.color_primaries;
564 avctx->colorspace = seq->color_config.color_primaries;
565 avctx->color_trc = seq->color_config.transfer_characteristics;
567 switch (seq->color_config.chroma_sample_position) {
568 case AV1_CSP_VERTICAL:
569 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
571 case AV1_CSP_COLOCATED:
572 avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
576 if (avctx->width != width || avctx->height != height) {
577 int ret = ff_set_dimensions(avctx, width, height);
581 avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
583 if (seq->timing_info.num_units_in_display_tick &&
584 seq->timing_info.time_scale) {
585 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
586 seq->timing_info.num_units_in_display_tick,
587 seq->timing_info.time_scale,
589 if (seq->timing_info.equal_picture_interval)
590 avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
596 static int update_context_with_frame_header(AVCodecContext *avctx,
597 const AV1RawFrameHeader *header)
599 AVRational aspect_ratio;
600 int width = header->frame_width_minus_1 + 1;
601 int height = header->frame_height_minus_1 + 1;
602 int r_width = header->render_width_minus_1 + 1;
603 int r_height = header->render_height_minus_1 + 1;
606 if (avctx->width != width || avctx->height != height) {
607 ret = ff_set_dimensions(avctx, width, height);
612 av_reduce(&aspect_ratio.num, &aspect_ratio.den,
613 (int64_t)height * r_width,
614 (int64_t)width * r_height,
617 if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
618 ret = ff_set_sar(avctx, aspect_ratio);
626 static av_cold int av1_decode_init(AVCodecContext *avctx)
628 AV1DecContext *s = avctx->priv_data;
629 AV1RawSequenceHeader *seq;
633 s->pix_fmt = AV_PIX_FMT_NONE;
635 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
636 s->ref[i].tf.f = av_frame_alloc();
637 if (!s->ref[i].tf.f) {
638 av_log(avctx, AV_LOG_ERROR,
639 "Failed to allocate reference frame buffer %d.\n", i);
640 return AVERROR(ENOMEM);
644 s->cur_frame.tf.f = av_frame_alloc();
645 if (!s->cur_frame.tf.f) {
646 av_log(avctx, AV_LOG_ERROR,
647 "Failed to allocate current frame buffer.\n");
648 return AVERROR(ENOMEM);
651 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
655 if (avctx->extradata && avctx->extradata_size) {
656 ret = ff_cbs_read_extradata_from_codec(s->cbc,
660 av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
664 seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
666 av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
670 ret = set_context_with_sequence(avctx, seq);
672 av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
677 ff_cbs_fragment_reset(&s->current_obu);
683 static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
685 AV1DecContext *s = avctx->priv_data;
686 AV1RawFrameHeader *header= s->raw_frame_header;
690 ret = update_context_with_frame_header(avctx, header);
692 av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
696 f->header_ref = av_buffer_ref(s->header_ref);
698 return AVERROR(ENOMEM);
700 f->raw_frame_header = s->raw_frame_header;
702 if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
706 frame->key_frame = header->frame_type == AV1_FRAME_KEY;
708 switch (header->frame_type) {
710 case AV1_FRAME_INTRA_ONLY:
711 frame->pict_type = AV_PICTURE_TYPE_I;
713 case AV1_FRAME_INTER:
714 frame->pict_type = AV_PICTURE_TYPE_P;
716 case AV1_FRAME_SWITCH:
717 frame->pict_type = AV_PICTURE_TYPE_SP;
721 if (avctx->hwaccel) {
722 const AVHWAccel *hwaccel = avctx->hwaccel;
723 if (hwaccel->frame_priv_data_size) {
724 f->hwaccel_priv_buf =
725 av_buffer_allocz(hwaccel->frame_priv_data_size);
726 if (!f->hwaccel_priv_buf) {
727 ret = AVERROR(ENOMEM);
730 f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
736 av1_frame_unref(avctx, f);
740 static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
741 const AVPacket *pkt, int *got_frame)
743 AV1DecContext *s = avctx->priv_data;
744 const AVFrame *srcframe = s->cur_frame.tf.f;
747 ret = av_frame_ref(frame, srcframe);
751 frame->pts = pkt->pts;
752 frame->pkt_dts = pkt->dts;
753 frame->pkt_size = pkt->size;
760 static int update_reference_list(AVCodecContext *avctx)
762 AV1DecContext *s = avctx->priv_data;
763 const AV1RawFrameHeader *header = s->raw_frame_header;
766 for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
767 if (header->refresh_frame_flags & (1 << i)) {
768 if (s->ref[i].tf.f->buf[0])
769 av1_frame_unref(avctx, &s->ref[i]);
770 if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
771 av_log(avctx, AV_LOG_ERROR,
772 "Failed to update frame %d in reference list\n", i);
780 static int get_current_frame(AVCodecContext *avctx)
782 AV1DecContext *s = avctx->priv_data;
785 if (s->cur_frame.tf.f->buf[0])
786 av1_frame_unref(avctx, &s->cur_frame);
788 ret = av1_frame_alloc(avctx, &s->cur_frame);
790 av_log(avctx, AV_LOG_ERROR,
791 "Failed to allocate space for current frame.\n");
795 ret = init_tile_data(s);
797 av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
801 global_motion_params(s);
803 coded_lossless_param(s);
804 load_grain_params(s);
809 static int av1_decode_frame(AVCodecContext *avctx, void *frame,
810 int *got_frame, AVPacket *pkt)
812 AV1DecContext *s = avctx->priv_data;
813 AV1RawTileGroup *raw_tile_group = NULL;
816 ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
818 av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
821 av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
822 s->current_obu.nb_units);
824 for (int i = 0; i < s->current_obu.nb_units; i++) {
825 CodedBitstreamUnit *unit = &s->current_obu.units[i];
826 AV1RawOBU *obu = unit->content;
827 const AV1RawOBUHeader *header;
832 header = &obu->header;
833 av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
835 switch (unit->type) {
836 case AV1_OBU_SEQUENCE_HEADER:
837 av_buffer_unref(&s->seq_ref);
838 s->seq_ref = av_buffer_ref(unit->content_ref);
840 ret = AVERROR(ENOMEM);
844 s->raw_seq = &obu->obu.sequence_header;
846 ret = set_context_with_sequence(avctx, s->raw_seq);
848 av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
853 if (s->pix_fmt == AV_PIX_FMT_NONE) {
854 ret = get_pixel_format(avctx);
856 av_log(avctx, AV_LOG_ERROR,
857 "Failed to get pixel format.\n");
863 if (avctx->hwaccel && avctx->hwaccel->decode_params) {
864 ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
867 av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
873 case AV1_OBU_REDUNDANT_FRAME_HEADER:
874 if (s->raw_frame_header)
878 case AV1_OBU_FRAME_HEADER:
880 av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
881 ret = AVERROR_INVALIDDATA;
885 av_buffer_unref(&s->header_ref);
886 s->header_ref = av_buffer_ref(unit->content_ref);
887 if (!s->header_ref) {
888 ret = AVERROR(ENOMEM);
892 if (unit->type == AV1_OBU_FRAME)
893 s->raw_frame_header = &obu->obu.frame.header;
895 s->raw_frame_header = &obu->obu.frame_header;
897 if (s->raw_frame_header->show_existing_frame) {
898 if (s->cur_frame.tf.f->buf[0])
899 av1_frame_unref(avctx, &s->cur_frame);
901 ret = av1_frame_ref(avctx, &s->cur_frame,
902 &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
904 av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
908 ret = update_reference_list(avctx);
910 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
914 ret = set_output_frame(avctx, frame, pkt, got_frame);
916 av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
918 s->raw_frame_header = NULL;
923 ret = get_current_frame(avctx);
925 av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
929 s->cur_frame.spatial_id = header->spatial_id;
930 s->cur_frame.temporal_id = header->temporal_id;
932 if (avctx->hwaccel) {
933 ret = avctx->hwaccel->start_frame(avctx, unit->data,
936 av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
940 if (unit->type != AV1_OBU_FRAME)
943 case AV1_OBU_TILE_GROUP:
944 if (!s->raw_frame_header) {
945 av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
946 ret = AVERROR_INVALIDDATA;
950 if (unit->type == AV1_OBU_FRAME)
951 raw_tile_group = &obu->obu.frame.tile_group;
953 raw_tile_group = &obu->obu.tile_group;
955 ret = get_tiles_info(avctx, raw_tile_group);
959 if (avctx->hwaccel) {
960 ret = avctx->hwaccel->decode_slice(avctx,
961 raw_tile_group->tile_data.data,
962 raw_tile_group->tile_data.data_size);
964 av_log(avctx, AV_LOG_ERROR,
965 "HW accel decode slice fail.\n");
970 case AV1_OBU_TILE_LIST:
971 case AV1_OBU_TEMPORAL_DELIMITER:
972 case AV1_OBU_PADDING:
973 case AV1_OBU_METADATA:
976 av_log(avctx, AV_LOG_DEBUG,
977 "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
978 unit->type, unit->data_size);
981 if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
982 if (avctx->hwaccel) {
983 ret = avctx->hwaccel->end_frame(avctx);
985 av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
990 ret = update_reference_list(avctx);
992 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
996 if (s->raw_frame_header->show_frame) {
997 ret = set_output_frame(avctx, frame, pkt, got_frame);
999 av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
1003 raw_tile_group = NULL;
1004 s->raw_frame_header = NULL;
1009 ff_cbs_fragment_reset(&s->current_obu);
1011 s->raw_frame_header = NULL;
1015 static void av1_decode_flush(AVCodecContext *avctx)
1017 AV1DecContext *s = avctx->priv_data;
1019 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
1020 av1_frame_unref(avctx, &s->ref[i]);
1022 av1_frame_unref(avctx, &s->cur_frame);
1023 s->raw_frame_header = NULL;
1026 ff_cbs_flush(s->cbc);
1029 AVCodec ff_av1_decoder = {
1031 .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
1032 .type = AVMEDIA_TYPE_VIDEO,
1033 .id = AV_CODEC_ID_AV1,
1034 .priv_data_size = sizeof(AV1DecContext),
1035 .init = av1_decode_init,
1036 .close = av1_decode_free,
1037 .decode = av1_decode_frame,
1038 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
1039 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1040 FF_CODEC_CAP_INIT_CLEANUP |
1041 FF_CODEC_CAP_SETS_PKT_DTS,
1042 .flush = av1_decode_flush,
1043 .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
1044 .hw_configs = (const AVCodecHWConfigInternal * []) {
1045 #if CONFIG_AV1_DXVA2_HWACCEL
1048 #if CONFIG_AV1_D3D11VA_HWACCEL
1049 HWACCEL_D3D11VA(av1),
1051 #if CONFIG_AV1_D3D11VA2_HWACCEL
1052 HWACCEL_D3D11VA2(av1),
1054 #if CONFIG_AV1_NVDEC_HWACCEL
1057 #if CONFIG_AV1_VAAPI_HWACCEL