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/film_grain_params.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
26 #include "bytestream.h"
31 static uint32_t inverse_recenter(int r, uint32_t v)
36 return r - ((v + 1) >> 1);
41 static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
45 return inverse_recenter(r, sub_exp);
47 return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
51 static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
54 int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
58 static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
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;
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;
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.
73 if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
74 prev_gm_param = s->cur_frame.gm_params[ref][idx];
76 prev_gm_param = s->ref[prev_frame].gm_params[ref][idx];
79 if (type == AV1_WARP_MODEL_TRANSLATION) {
80 abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
81 !s->raw_frame_header->allow_high_precision_mv;
82 prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
83 !s->raw_frame_header->allow_high_precision_mv;
85 abs_bits = AV1_GM_ABS_TRANS_BITS;
86 prec_bits = AV1_GM_TRANS_PREC_BITS;
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;
93 r = (prev_gm_param >> prec_diff) - sub;
95 s->cur_frame.gm_params[ref][idx] =
96 (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],
97 -mx, mx + 1, r) << prec_diff) + round;
101 * update gm type/params, since cbs already implemented part of this funcation,
102 * so we don't need to full implement spec.
104 static void global_motion_params(AV1DecContext *s)
106 const AV1RawFrameHeader *header = s->raw_frame_header;
109 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
110 s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;
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;
115 if (header->frame_type == AV1_FRAME_KEY ||
116 header->frame_type == AV1_FRAME_INTRA_ONLY)
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;
124 type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
125 : AV1_WARP_MODEL_AFFINE;
128 type = AV1_WARP_MODEL_IDENTITY;
130 s->cur_frame.gm_type[ref] = type;
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);
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];
143 if (type >= AV1_WARP_MODEL_TRANSLATION) {
144 read_global_param(s, type, ref, 0);
145 read_global_param(s, type, ref, 1);
150 static int get_relative_dist(const AV1RawSequenceHeader *seq,
151 unsigned int a, unsigned int b)
153 unsigned int diff = a - b;
154 unsigned int m = 1 << seq->order_hint_bits_minus_1;
155 return (diff & (m - 1)) - (diff & m);
158 static void skip_mode_params(AV1DecContext *s)
160 const AV1RawFrameHeader *header = s->raw_frame_header;
161 const AV1RawSequenceHeader *seq = s->raw_seq;
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;
168 if (!header->skip_mode_present)
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);
177 if (forward_idx < 0 ||
178 get_relative_dist(seq, ref_hint, forward_hint) > 0) {
180 forward_hint = ref_hint;
182 } else if (dist > 0) {
183 if (backward_idx < 0 ||
184 get_relative_dist(seq, ref_hint, backward_hint) < 0) {
186 backward_hint = ref_hint;
191 if (forward_idx < 0) {
193 } else if (backward_idx >= 0) {
194 s->cur_frame.skip_mode_frame_idx[0] =
195 AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx);
196 s->cur_frame.skip_mode_frame_idx[1] =
197 AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx);
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;
213 if (second_forward_idx < 0)
216 s->cur_frame.skip_mode_frame_idx[0] =
217 AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx);
218 s->cur_frame.skip_mode_frame_idx[1] =
219 AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx);
222 static void coded_lossless_param(AV1DecContext *s)
224 const AV1RawFrameHeader *header = s->raw_frame_header;
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;
234 s->cur_frame.coded_lossless = 1;
235 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
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]);
241 qindex = header->base_q_idx;
243 qindex = av_clip_uintp2(qindex, 8);
246 s->cur_frame.coded_lossless = 0;
252 static void load_grain_params(AV1DecContext *s)
254 const AV1RawFrameHeader *header = s->raw_frame_header;
255 const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src;
256 AV1RawFilmGrainParams *dst = &s->cur_frame.film_grain;
258 if (!film_grain->apply_grain)
261 if (film_grain->update_grain) {
262 memcpy(dst, film_grain, sizeof(*dst));
266 src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain;
268 memcpy(dst, src, sizeof(*dst));
269 dst->grain_seed = film_grain->grain_seed;
272 static int init_tile_data(AV1DecContext *s)
276 s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;
277 if (s->tile_num < cur_tile_num) {
278 int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
279 sizeof(TileGroupInfo));
285 s->tile_num = cur_tile_num;
290 static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
292 AV1DecContext *s = avctx->priv_data;
294 uint16_t tile_num, tile_row, tile_col;
295 uint32_t size = 0, size_bytes = 0;
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;
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;
306 if (tile_num == tile_group->tg_end) {
307 s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
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;
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;
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;
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;
328 bytestream2_skipu(&gb, size);
335 static int get_pixel_format(AVCodecContext *avctx)
337 AV1DecContext *s = avctx->priv_data;
338 const AV1RawSequenceHeader *seq = s->raw_seq;
341 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
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;
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;
353 av_log(avctx, AV_LOG_ERROR,
354 "Unknown AV1 profile %d.\n", seq->seq_profile);
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) {
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;
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) {
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;
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) {
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;
389 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
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;
399 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
402 av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
403 av_get_pix_fmt_name(pix_fmt));
405 if (pix_fmt == AV_PIX_FMT_NONE)
407 s->pix_fmt = pix_fmt;
409 switch (s->pix_fmt) {
410 case AV_PIX_FMT_YUV420P:
411 #if CONFIG_AV1_DXVA2_HWACCEL
412 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
414 #if CONFIG_AV1_D3D11VA_HWACCEL
415 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
416 *fmtp++ = AV_PIX_FMT_D3D11;
418 #if CONFIG_AV1_NVDEC_HWACCEL
419 *fmtp++ = AV_PIX_FMT_CUDA;
421 #if CONFIG_AV1_VAAPI_HWACCEL
422 *fmtp++ = AV_PIX_FMT_VAAPI;
425 case AV_PIX_FMT_YUV420P10:
426 #if CONFIG_AV1_DXVA2_HWACCEL
427 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
429 #if CONFIG_AV1_D3D11VA_HWACCEL
430 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
431 *fmtp++ = AV_PIX_FMT_D3D11;
433 #if CONFIG_AV1_NVDEC_HWACCEL
434 *fmtp++ = AV_PIX_FMT_CUDA;
436 #if CONFIG_AV1_VAAPI_HWACCEL
437 *fmtp++ = AV_PIX_FMT_VAAPI;
440 case AV_PIX_FMT_GRAY8:
441 #if CONFIG_AV1_NVDEC_HWACCEL
442 *fmtp++ = AV_PIX_FMT_CUDA;
445 case AV_PIX_FMT_GRAY10:
446 #if CONFIG_AV1_NVDEC_HWACCEL
447 *fmtp++ = AV_PIX_FMT_CUDA;
452 *fmtp++ = s->pix_fmt;
453 *fmtp = AV_PIX_FMT_NONE;
455 ret = ff_thread_get_format(avctx, pix_fmts);
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.
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);
470 avctx->pix_fmt = ret;
475 static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
477 ff_thread_release_buffer(avctx, &f->tf);
478 av_buffer_unref(&f->hwaccel_priv_buf);
479 f->hwaccel_picture_private = NULL;
480 av_buffer_unref(&f->header_ref);
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;
489 static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
493 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
497 dst->header_ref = av_buffer_ref(src->header_ref);
498 if (!dst->header_ref)
501 dst->raw_frame_header = src->raw_frame_header;
503 if (src->hwaccel_picture_private) {
504 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
505 if (!dst->hwaccel_priv_buf)
507 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
510 dst->spatial_id = src->spatial_id;
511 dst->temporal_id = src->temporal_id;
514 AV1_NUM_REF_FRAMES * sizeof(uint8_t));
515 memcpy(dst->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,
523 sizeof(dst->film_grain));
524 dst->coded_lossless = src->coded_lossless;
529 av1_frame_unref(avctx, dst);
530 return AVERROR(ENOMEM);
533 static av_cold int av1_decode_free(AVCodecContext *avctx)
535 AV1DecContext *s = avctx->priv_data;
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);
541 av1_frame_unref(avctx, &s->cur_frame);
542 av_frame_free(&s->cur_frame.tf.f);
544 av_buffer_unref(&s->seq_ref);
545 av_buffer_unref(&s->header_ref);
546 av_freep(&s->tile_group_info);
548 ff_cbs_fragment_free(&s->current_obu);
549 ff_cbs_close(&s->cbc);
554 static int set_context_with_sequence(AVCodecContext *avctx,
555 const AV1RawSequenceHeader *seq)
557 int width = seq->max_frame_width_minus_1 + 1;
558 int height = seq->max_frame_height_minus_1 + 1;
560 avctx->profile = seq->seq_profile;
561 avctx->level = seq->seq_level_idx[0];
564 seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
565 avctx->color_primaries = seq->color_config.color_primaries;
566 avctx->colorspace = seq->color_config.color_primaries;
567 avctx->color_trc = seq->color_config.transfer_characteristics;
569 switch (seq->color_config.chroma_sample_position) {
570 case AV1_CSP_VERTICAL:
571 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
573 case AV1_CSP_COLOCATED:
574 avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
578 if (avctx->width != width || avctx->height != height) {
579 int ret = ff_set_dimensions(avctx, width, height);
583 avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
585 if (seq->timing_info.num_units_in_display_tick &&
586 seq->timing_info.time_scale) {
587 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
588 seq->timing_info.num_units_in_display_tick,
589 seq->timing_info.time_scale,
591 if (seq->timing_info.equal_picture_interval)
592 avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
598 static int update_context_with_frame_header(AVCodecContext *avctx,
599 const AV1RawFrameHeader *header)
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;
608 if (avctx->width != width || avctx->height != height) {
609 ret = ff_set_dimensions(avctx, width, height);
614 av_reduce(&aspect_ratio.num, &aspect_ratio.den,
615 (int64_t)height * r_width,
616 (int64_t)width * r_height,
619 if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
620 ret = ff_set_sar(avctx, aspect_ratio);
628 static av_cold int av1_decode_init(AVCodecContext *avctx)
630 AV1DecContext *s = avctx->priv_data;
631 AV1RawSequenceHeader *seq;
635 s->pix_fmt = AV_PIX_FMT_NONE;
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);
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);
653 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
657 av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
659 if (avctx->extradata && avctx->extradata_size) {
660 ret = ff_cbs_read_extradata_from_codec(s->cbc,
664 av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
668 seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
670 av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
674 ret = set_context_with_sequence(avctx, seq);
676 av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
681 ff_cbs_fragment_reset(&s->current_obu);
687 static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
689 AV1DecContext *s = avctx->priv_data;
690 AV1RawFrameHeader *header= s->raw_frame_header;
694 ret = update_context_with_frame_header(avctx, header);
696 av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
700 f->header_ref = av_buffer_ref(s->header_ref);
702 return AVERROR(ENOMEM);
704 f->raw_frame_header = s->raw_frame_header;
706 if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
710 frame->key_frame = header->frame_type == AV1_FRAME_KEY;
712 switch (header->frame_type) {
714 case AV1_FRAME_INTRA_ONLY:
715 frame->pict_type = AV_PICTURE_TYPE_I;
717 case AV1_FRAME_INTER:
718 frame->pict_type = AV_PICTURE_TYPE_P;
720 case AV1_FRAME_SWITCH:
721 frame->pict_type = AV_PICTURE_TYPE_SP;
725 if (avctx->hwaccel) {
726 const AVHWAccel *hwaccel = avctx->hwaccel;
727 if (hwaccel->frame_priv_data_size) {
728 f->hwaccel_priv_buf =
729 av_buffer_allocz(hwaccel->frame_priv_data_size);
730 if (!f->hwaccel_priv_buf) {
731 ret = AVERROR(ENOMEM);
734 f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
740 av1_frame_unref(avctx, f);
744 static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
746 AV1DecContext *s = avctx->priv_data;
747 const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain;
748 AVFilmGrainParams *fgp;
749 AVFilmGrainAOMParams *aom;
751 if (!film_grain->apply_grain)
754 fgp = av_film_grain_params_create_side_data(frame);
756 return AVERROR(ENOMEM);
758 fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
759 fgp->seed = film_grain->grain_seed;
761 aom = &fgp->codec.aom;
762 aom->chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma;
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;
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];
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];
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];
786 for (int i = 0; i < 24; i++) {
787 aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128;
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;
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;
804 static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
805 const AVPacket *pkt, int *got_frame)
807 AV1DecContext *s = avctx->priv_data;
808 const AVFrame *srcframe = s->cur_frame.tf.f;
812 if (s->operating_point_idc &&
813 av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id)
816 ret = av_frame_ref(frame, srcframe);
820 if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
821 ret = export_film_grain(avctx, frame);
823 av_frame_unref(frame);
828 frame->pts = pkt->pts;
829 frame->pkt_dts = pkt->dts;
830 frame->pkt_size = pkt->size;
837 static int update_reference_list(AVCodecContext *avctx)
839 AV1DecContext *s = avctx->priv_data;
840 const AV1RawFrameHeader *header = s->raw_frame_header;
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);
857 static int get_current_frame(AVCodecContext *avctx)
859 AV1DecContext *s = avctx->priv_data;
862 if (s->cur_frame.tf.f->buf[0])
863 av1_frame_unref(avctx, &s->cur_frame);
865 ret = av1_frame_alloc(avctx, &s->cur_frame);
867 av_log(avctx, AV_LOG_ERROR,
868 "Failed to allocate space for current frame.\n");
872 ret = init_tile_data(s);
874 av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
878 global_motion_params(s);
880 coded_lossless_param(s);
881 load_grain_params(s);
886 static int av1_decode_frame(AVCodecContext *avctx, void *frame,
887 int *got_frame, AVPacket *pkt)
889 AV1DecContext *s = avctx->priv_data;
890 AV1RawTileGroup *raw_tile_group = NULL;
893 ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
895 av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
898 av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
899 s->current_obu.nb_units);
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;
909 header = &obu->header;
910 av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
912 switch (unit->type) {
913 case AV1_OBU_SEQUENCE_HEADER:
914 av_buffer_unref(&s->seq_ref);
915 s->seq_ref = av_buffer_ref(unit->content_ref);
917 ret = AVERROR(ENOMEM);
921 s->raw_seq = &obu->obu.sequence_header;
923 ret = set_context_with_sequence(avctx, s->raw_seq);
925 av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
930 s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point];
932 if (s->pix_fmt == AV_PIX_FMT_NONE) {
933 ret = get_pixel_format(avctx);
935 av_log(avctx, AV_LOG_ERROR,
936 "Failed to get pixel format.\n");
942 if (avctx->hwaccel && avctx->hwaccel->decode_params) {
943 ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
946 av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
952 case AV1_OBU_REDUNDANT_FRAME_HEADER:
953 if (s->raw_frame_header)
957 case AV1_OBU_FRAME_HEADER:
959 av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
960 ret = AVERROR_INVALIDDATA;
964 av_buffer_unref(&s->header_ref);
965 s->header_ref = av_buffer_ref(unit->content_ref);
966 if (!s->header_ref) {
967 ret = AVERROR(ENOMEM);
971 if (unit->type == AV1_OBU_FRAME)
972 s->raw_frame_header = &obu->obu.frame.header;
974 s->raw_frame_header = &obu->obu.frame_header;
976 if (s->raw_frame_header->show_existing_frame) {
977 if (s->cur_frame.tf.f->buf[0])
978 av1_frame_unref(avctx, &s->cur_frame);
980 ret = av1_frame_ref(avctx, &s->cur_frame,
981 &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
983 av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
987 ret = update_reference_list(avctx);
989 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
993 ret = set_output_frame(avctx, frame, pkt, got_frame);
995 av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
997 s->raw_frame_header = NULL;
1002 ret = get_current_frame(avctx);
1004 av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
1008 s->cur_frame.spatial_id = header->spatial_id;
1009 s->cur_frame.temporal_id = header->temporal_id;
1011 if (avctx->hwaccel) {
1012 ret = avctx->hwaccel->start_frame(avctx, unit->data,
1015 av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
1019 if (unit->type != AV1_OBU_FRAME)
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;
1029 if (unit->type == AV1_OBU_FRAME)
1030 raw_tile_group = &obu->obu.frame.tile_group;
1032 raw_tile_group = &obu->obu.tile_group;
1034 ret = get_tiles_info(avctx, raw_tile_group);
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);
1043 av_log(avctx, AV_LOG_ERROR,
1044 "HW accel decode slice fail.\n");
1049 case AV1_OBU_TILE_LIST:
1050 case AV1_OBU_TEMPORAL_DELIMITER:
1051 case AV1_OBU_PADDING:
1052 case AV1_OBU_METADATA:
1055 av_log(avctx, AV_LOG_DEBUG,
1056 "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
1057 unit->type, unit->data_size);
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);
1064 av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
1069 ret = update_reference_list(avctx);
1071 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
1075 if (s->raw_frame_header->show_frame) {
1076 ret = set_output_frame(avctx, frame, pkt, got_frame);
1078 av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
1082 raw_tile_group = NULL;
1083 s->raw_frame_header = NULL;
1088 ff_cbs_fragment_reset(&s->current_obu);
1090 s->raw_frame_header = NULL;
1094 static void av1_decode_flush(AVCodecContext *avctx)
1096 AV1DecContext *s = avctx->priv_data;
1098 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
1099 av1_frame_unref(avctx, &s->ref[i]);
1101 av1_frame_unref(avctx, &s->cur_frame);
1102 s->operating_point_idc = 0;
1103 s->raw_frame_header = NULL;
1106 ff_cbs_flush(s->cbc);
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 },
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,
1124 AVCodec ff_av1_decoder = {
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,
1132 .decode = av1_decode_frame,
1133 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
1134 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1135 FF_CODEC_CAP_INIT_CLEANUP |
1136 FF_CODEC_CAP_SETS_PKT_DTS,
1137 .flush = av1_decode_flush,
1138 .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
1139 .priv_class = &av1_class,
1140 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1141 #if CONFIG_AV1_DXVA2_HWACCEL
1144 #if CONFIG_AV1_D3D11VA_HWACCEL
1145 HWACCEL_D3D11VA(av1),
1147 #if CONFIG_AV1_D3D11VA2_HWACCEL
1148 HWACCEL_D3D11VA2(av1),
1150 #if CONFIG_AV1_NVDEC_HWACCEL
1153 #if CONFIG_AV1_VAAPI_HWACCEL