FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1dec.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 198 658 30.1%
Branches: 76 358 21.2%

Line Branch Exec Source
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
21 #include "libavutil/film_grain_params.h"
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 */
73 if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
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 -
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;
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] =
96 (decode_signed_subexp_with_ref(s->raw_frame_header->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 */
104 static void global_motion_params(AV1DecContext *s)
105 {
106 const AV1RawFrameHeader *header = s->raw_frame_header;
107 int type, ref;
108
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;
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 {
124 type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
125 : AV1_WARP_MODEL_AFFINE;
126 }
127 } else {
128 type = AV1_WARP_MODEL_IDENTITY;
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
150 static int get_relative_dist(const AV1RawSequenceHeader *seq,
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
158 static void skip_mode_params(AV1DecContext *s)
159 {
160 const AV1RawFrameHeader *header = s->raw_frame_header;
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) {
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);
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
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);
220 }
221
222 static void coded_lossless_param(AV1DecContext *s)
223 {
224 const AV1RawFrameHeader *header = s->raw_frame_header;
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
252 static void load_grain_params(AV1DecContext *s)
253 {
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;
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
272 static int init_tile_data(AV1DecContext *s)
273
274 {
275 int cur_tile_num =
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));
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) {
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;
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
335 21 static int get_pixel_format(AVCodecContext *avctx)
336 {
337 21 AV1DecContext *s = avctx->priv_data;
338 21 const AV1RawSequenceHeader *seq = s->raw_seq;
339 uint8_t bit_depth;
340 int ret;
341 21 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 21 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
347
348
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21 if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
349 bit_depth = seq->color_config.twelve_bit ? 12 : 10;
350
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 else if (seq->seq_profile <= 2)
351
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 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
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 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
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 } else if (seq->color_config.subsampling_x == 1 &&
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 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
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 } else if (seq->color_config.subsampling_x == 1 &&
381
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 seq->color_config.subsampling_y == 1) {
382
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 if (bit_depth == 8)
383 20 pix_fmt = AV_PIX_FMT_YUV420P;
384
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (bit_depth == 10)
385 1 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 21 av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
403 av_get_pix_fmt_name(pix_fmt));
404
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (pix_fmt == AV_PIX_FMT_NONE)
406 return -1;
407 21 s->pix_fmt = pix_fmt;
408
409
2/5
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
21 switch (s->pix_fmt) {
410 20 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 20 *fmtp++ = AV_PIX_FMT_VAAPI;
423 #endif
424 20 break;
425 1 case AV_PIX_FMT_YUV420P10:
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 1 *fmtp++ = AV_PIX_FMT_VAAPI;
438 #endif
439 1 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 21 *fmtp++ = s->pix_fmt;
453 21 *fmtp = AV_PIX_FMT_NONE;
454
455 21 ret = ff_thread_get_format(avctx, pix_fmts);
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 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
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!avctx->hwaccel) {
465 21 av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
466 " hardware accelerated AV1 decoding.\n");
467 21 return AVERROR(ENOSYS);
468 }
469
470 avctx->pix_fmt = ret;
471
472 return 0;
473 }
474
475 282 static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
476 {
477 282 ff_thread_release_buffer(avctx, &f->tf);
478 282 av_buffer_unref(&f->hwaccel_priv_buf);
479 282 f->hwaccel_picture_private = NULL;
480 282 av_buffer_unref(&f->header_ref);
481 282 f->raw_frame_header = NULL;
482 282 f->spatial_id = f->temporal_id = 0;
483 282 memset(f->skip_mode_frame_idx, 0,
484 2 * sizeof(uint8_t));
485 282 memset(&f->film_grain, 0, sizeof(f->film_grain));
486 282 f->coded_lossless = 0;
487 282 }
488
489 6 static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
490 {
491 int ret;
492
493 6 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
494
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (ret < 0)
495 6 return ret;
496
497 dst->header_ref = av_buffer_ref(src->header_ref);
498 if (!dst->header_ref)
499 goto fail;
500
501 dst->raw_frame_header = src->raw_frame_header;
502
503 if (src->hwaccel_picture_private) {
504 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
505 if (!dst->hwaccel_priv_buf)
506 goto fail;
507 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
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
533 21 static av_cold int av1_decode_free(AVCodecContext *avctx)
534 {
535 21 AV1DecContext *s = avctx->priv_data;
536
537
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 21 times.
189 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
538 168 av1_frame_unref(avctx, &s->ref[i]);
539 168 av_frame_free(&s->ref[i].tf.f);
540 }
541 21 av1_frame_unref(avctx, &s->cur_frame);
542 21 av_frame_free(&s->cur_frame.tf.f);
543
544 21 av_buffer_unref(&s->seq_ref);
545 21 av_buffer_unref(&s->header_ref);
546 21 av_freep(&s->tile_group_info);
547
548 21 ff_cbs_fragment_free(&s->current_obu);
549 21 ff_cbs_close(&s->cbc);
550
551 21 return 0;
552 }
553
554 104 static int set_context_with_sequence(AVCodecContext *avctx,
555 const AV1RawSequenceHeader *seq)
556 {
557 104 int width = seq->max_frame_width_minus_1 + 1;
558 104 int height = seq->max_frame_height_minus_1 + 1;
559
560 104 avctx->profile = seq->seq_profile;
561 104 avctx->level = seq->seq_level_idx[0];
562
563 104 avctx->color_range =
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
565 104 avctx->color_primaries = seq->color_config.color_primaries;
566 104 avctx->colorspace = seq->color_config.color_primaries;
567 104 avctx->color_trc = seq->color_config.transfer_characteristics;
568
569
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
104 switch (seq->color_config.chroma_sample_position) {
570 case AV1_CSP_VERTICAL:
571 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
572 break;
573 case AV1_CSP_COLOCATED:
574 avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
575 break;
576 }
577
578
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 102 times.
104 if (seq->film_grain_params_present)
579 2 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
580 else
581 102 avctx->properties &= ~FF_CODEC_PROPERTY_FILM_GRAIN;
582
583
3/4
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 101 times.
104 if (avctx->width != width || avctx->height != height) {
584 3 int ret = ff_set_dimensions(avctx, width, height);
585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
586 return ret;
587 }
588 104 avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
589
590
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 39 times.
104 if (seq->timing_info.num_units_in_display_tick &&
591
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 seq->timing_info.time_scale) {
592 65 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
593 65 seq->timing_info.num_units_in_display_tick,
594 65 seq->timing_info.time_scale,
595 INT_MAX);
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (seq->timing_info.equal_picture_interval)
597 avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
598 }
599
600 104 return 0;
601 }
602
603 93 static int update_context_with_frame_header(AVCodecContext *avctx,
604 const AV1RawFrameHeader *header)
605 {
606 AVRational aspect_ratio;
607 93 int width = header->frame_width_minus_1 + 1;
608 93 int height = header->frame_height_minus_1 + 1;
609 93 int r_width = header->render_width_minus_1 + 1;
610 93 int r_height = header->render_height_minus_1 + 1;
611 int ret;
612
613
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
93 if (avctx->width != width || avctx->height != height) {
614 ret = ff_set_dimensions(avctx, width, height);
615 if (ret < 0)
616 return ret;
617 }
618
619 93 av_reduce(&aspect_ratio.num, &aspect_ratio.den,
620 93 (int64_t)height * r_width,
621 93 (int64_t)width * r_height,
622 INT_MAX);
623
624
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
93 if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
625 ret = ff_set_sar(avctx, aspect_ratio);
626 if (ret < 0)
627 return ret;
628 }
629
630 93 return 0;
631 }
632
633 21 static av_cold int av1_decode_init(AVCodecContext *avctx)
634 {
635 21 AV1DecContext *s = avctx->priv_data;
636 AV1RawSequenceHeader *seq;
637 int ret;
638
639 21 s->avctx = avctx;
640 21 s->pix_fmt = AV_PIX_FMT_NONE;
641
642
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 21 times.
189 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
643 168 s->ref[i].tf.f = av_frame_alloc();
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (!s->ref[i].tf.f) {
645 av_log(avctx, AV_LOG_ERROR,
646 "Failed to allocate reference frame buffer %d.\n", i);
647 return AVERROR(ENOMEM);
648 }
649 }
650
651 21 s->cur_frame.tf.f = av_frame_alloc();
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!s->cur_frame.tf.f) {
653 av_log(avctx, AV_LOG_ERROR,
654 "Failed to allocate current frame buffer.\n");
655 return AVERROR(ENOMEM);
656 }
657
658 21 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
660 return ret;
661
662 21 av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
663
664
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
21 if (avctx->extradata && avctx->extradata_size) {
665 2 ret = ff_cbs_read_extradata_from_codec(s->cbc,
666 &s->current_obu,
667 avctx);
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
669 av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
670 return ret;
671 }
672
673 2 seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!seq) {
675 av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
676 goto end;
677 }
678
679 2 ret = set_context_with_sequence(avctx, seq);
680
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ret < 0) {
681 av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
682 goto end;
683 }
684
685 2 end:
686 2 ff_cbs_fragment_reset(&s->current_obu);
687 }
688
689 21 return ret;
690 }
691
692 93 static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
693 {
694 93 AV1DecContext *s = avctx->priv_data;
695 93 AV1RawFrameHeader *header= s->raw_frame_header;
696 AVFrame *frame;
697 int ret;
698
699 93 ret = update_context_with_frame_header(avctx, header);
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (ret < 0) {
701 av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
702 return ret;
703 }
704
705 93 f->header_ref = av_buffer_ref(s->header_ref);
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (!f->header_ref)
707 return AVERROR(ENOMEM);
708
709 93 f->raw_frame_header = s->raw_frame_header;
710
711
1/2
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
93 if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
712 93 goto fail;
713
714 frame = f->tf.f;
715 frame->key_frame = header->frame_type == AV1_FRAME_KEY;
716
717 switch (header->frame_type) {
718 case AV1_FRAME_KEY:
719 case AV1_FRAME_INTRA_ONLY:
720 frame->pict_type = AV_PICTURE_TYPE_I;
721 break;
722 case AV1_FRAME_INTER:
723 frame->pict_type = AV_PICTURE_TYPE_P;
724 break;
725 case AV1_FRAME_SWITCH:
726 frame->pict_type = AV_PICTURE_TYPE_SP;
727 break;
728 }
729
730 if (avctx->hwaccel) {
731 const AVHWAccel *hwaccel = avctx->hwaccel;
732 if (hwaccel->frame_priv_data_size) {
733 f->hwaccel_priv_buf =
734 av_buffer_allocz(hwaccel->frame_priv_data_size);
735 if (!f->hwaccel_priv_buf) {
736 ret = AVERROR(ENOMEM);
737 goto fail;
738 }
739 f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
740 }
741 }
742 return 0;
743
744 93 fail:
745 93 av1_frame_unref(avctx, f);
746 93 return ret;
747 }
748
749 static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
750 {
751 AV1DecContext *s = avctx->priv_data;
752 const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain;
753 AVFilmGrainParams *fgp;
754 AVFilmGrainAOMParams *aom;
755
756 if (!film_grain->apply_grain)
757 return 0;
758
759 fgp = av_film_grain_params_create_side_data(frame);
760 if (!fgp)
761 return AVERROR(ENOMEM);
762
763 fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
764 fgp->seed = film_grain->grain_seed;
765
766 aom = &fgp->codec.aom;
767 aom->chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma;
768 aom->scaling_shift = film_grain->grain_scaling_minus_8 + 8;
769 aom->ar_coeff_lag = film_grain->ar_coeff_lag;
770 aom->ar_coeff_shift = film_grain->ar_coeff_shift_minus_6 + 6;
771 aom->grain_scale_shift = film_grain->grain_scale_shift;
772 aom->overlap_flag = film_grain->overlap_flag;
773 aom->limit_output_range = film_grain->clip_to_restricted_range;
774
775 aom->num_y_points = film_grain->num_y_points;
776 for (int i = 0; i < film_grain->num_y_points; i++) {
777 aom->y_points[i][0] = film_grain->point_y_value[i];
778 aom->y_points[i][1] = film_grain->point_y_scaling[i];
779 }
780 aom->num_uv_points[0] = film_grain->num_cb_points;
781 for (int i = 0; i < film_grain->num_cb_points; i++) {
782 aom->uv_points[0][i][0] = film_grain->point_cb_value[i];
783 aom->uv_points[0][i][1] = film_grain->point_cb_scaling[i];
784 }
785 aom->num_uv_points[1] = film_grain->num_cr_points;
786 for (int i = 0; i < film_grain->num_cr_points; i++) {
787 aom->uv_points[1][i][0] = film_grain->point_cr_value[i];
788 aom->uv_points[1][i][1] = film_grain->point_cr_scaling[i];
789 }
790
791 for (int i = 0; i < 24; i++) {
792 aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128;
793 }
794 for (int i = 0; i < 25; i++) {
795 aom->ar_coeffs_uv[0][i] = film_grain->ar_coeffs_cb_plus_128[i] - 128;
796 aom->ar_coeffs_uv[1][i] = film_grain->ar_coeffs_cr_plus_128[i] - 128;
797 }
798
799 aom->uv_mult[0] = film_grain->cb_mult;
800 aom->uv_mult[1] = film_grain->cr_mult;
801 aom->uv_mult_luma[0] = film_grain->cb_luma_mult;
802 aom->uv_mult_luma[1] = film_grain->cr_luma_mult;
803 aom->uv_offset[0] = film_grain->cb_offset;
804 aom->uv_offset[1] = film_grain->cr_offset;
805
806 return 0;
807 }
808
809 static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
810 const AVPacket *pkt, int *got_frame)
811 {
812 AV1DecContext *s = avctx->priv_data;
813 const AVFrame *srcframe = s->cur_frame.tf.f;
814 int ret;
815
816 // TODO: all layers
817 if (s->operating_point_idc &&
818 av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id)
819 return 0;
820
821 ret = av_frame_ref(frame, srcframe);
822 if (ret < 0)
823 return ret;
824
825 if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
826 ret = export_film_grain(avctx, frame);
827 if (ret < 0) {
828 av_frame_unref(frame);
829 return ret;
830 }
831 }
832
833 frame->pts = pkt->pts;
834 frame->pkt_dts = pkt->dts;
835 frame->pkt_size = pkt->size;
836
837 *got_frame = 1;
838
839 return 0;
840 }
841
842 static int update_reference_list(AVCodecContext *avctx)
843 {
844 AV1DecContext *s = avctx->priv_data;
845 const AV1RawFrameHeader *header = s->raw_frame_header;
846 int ret;
847
848 for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
849 if (header->refresh_frame_flags & (1 << i)) {
850 if (s->ref[i].tf.f->buf[0])
851 av1_frame_unref(avctx, &s->ref[i]);
852 if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
853 av_log(avctx, AV_LOG_ERROR,
854 "Failed to update frame %d in reference list\n", i);
855 return ret;
856 }
857 }
858 }
859 return 0;
860 }
861
862 93 static int get_current_frame(AVCodecContext *avctx)
863 {
864 93 AV1DecContext *s = avctx->priv_data;
865 int ret;
866
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (s->cur_frame.tf.f->buf[0])
868 av1_frame_unref(avctx, &s->cur_frame);
869
870 93 ret = av1_frame_alloc(avctx, &s->cur_frame);
871
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 if (ret < 0) {
872 93 av_log(avctx, AV_LOG_ERROR,
873 "Failed to allocate space for current frame.\n");
874 93 return ret;
875 }
876
877 ret = init_tile_data(s);
878 if (ret < 0) {
879 av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
880 return ret;
881 }
882
883 global_motion_params(s);
884 skip_mode_params(s);
885 coded_lossless_param(s);
886 load_grain_params(s);
887
888 return ret;
889 }
890
891 330 static int av1_decode_frame(AVCodecContext *avctx, void *frame,
892 int *got_frame, AVPacket *pkt)
893 {
894 330 AV1DecContext *s = avctx->priv_data;
895 330 AV1RawTileGroup *raw_tile_group = NULL;
896 int ret;
897
898 330 ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 330 times.
330 if (ret < 0) {
900 av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
901 goto end;
902 }
903 330 av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
904 s->current_obu.nb_units);
905
906
1/2
✓ Branch 0 taken 737 times.
✗ Branch 1 not taken.
737 for (int i = 0; i < s->current_obu.nb_units; i++) {
907 737 CodedBitstreamUnit *unit = &s->current_obu.units[i];
908 737 AV1RawOBU *obu = unit->content;
909 const AV1RawOBUHeader *header;
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 737 times.
737 if (!obu)
912 continue;
913
914 737 header = &obu->header;
915 737 av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
916
917
3/6
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 309 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 326 times.
✗ Branch 5 not taken.
737 switch (unit->type) {
918 102 case AV1_OBU_SEQUENCE_HEADER:
919 102 av_buffer_unref(&s->seq_ref);
920 102 s->seq_ref = av_buffer_ref(unit->content_ref);
921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (!s->seq_ref) {
922 ret = AVERROR(ENOMEM);
923 goto end;
924 }
925
926 102 s->raw_seq = &obu->obu.sequence_header;
927
928 102 ret = set_context_with_sequence(avctx, s->raw_seq);
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (ret < 0) {
930 av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
931 s->raw_seq = NULL;
932 goto end;
933 }
934
935 102 s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point];
936
937
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 81 times.
102 if (s->pix_fmt == AV_PIX_FMT_NONE) {
938 21 ret = get_pixel_format(avctx);
939
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (ret < 0) {
940 21 av_log(avctx, AV_LOG_ERROR,
941 "Failed to get pixel format.\n");
942 21 s->raw_seq = NULL;
943 21 goto end;
944 }
945 }
946
947
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
81 if (avctx->hwaccel && avctx->hwaccel->decode_params) {
948 ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
949 unit->data_size);
950 if (ret < 0) {
951 av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
952 s->raw_seq = NULL;
953 goto end;
954 }
955 }
956 81 break;
957 case AV1_OBU_REDUNDANT_FRAME_HEADER:
958 if (s->raw_frame_header)
959 break;
960 // fall-through
961 case AV1_OBU_FRAME:
962 case AV1_OBU_FRAME_HEADER:
963
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 99 times.
309 if (!s->raw_seq) {
964 210 av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
965 210 ret = AVERROR_INVALIDDATA;
966 210 goto end;
967 }
968
969 99 av_buffer_unref(&s->header_ref);
970 99 s->header_ref = av_buffer_ref(unit->content_ref);
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (!s->header_ref) {
972 ret = AVERROR(ENOMEM);
973 goto end;
974 }
975
976
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 6 times.
99 if (unit->type == AV1_OBU_FRAME)
977 93 s->raw_frame_header = &obu->obu.frame.header;
978 else
979 6 s->raw_frame_header = &obu->obu.frame_header;
980
981
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 93 times.
99 if (s->raw_frame_header->show_existing_frame) {
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->cur_frame.tf.f->buf[0])
983 av1_frame_unref(avctx, &s->cur_frame);
984
985 6 ret = av1_frame_ref(avctx, &s->cur_frame,
986 6 &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
987
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (ret < 0) {
988 6 av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
989 6 goto end;
990 }
991
992 ret = update_reference_list(avctx);
993 if (ret < 0) {
994 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
995 goto end;
996 }
997
998 ret = set_output_frame(avctx, frame, pkt, got_frame);
999 if (ret < 0)
1000 av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
1001
1002 s->raw_frame_header = NULL;
1003
1004 goto end;
1005 }
1006
1007 93 ret = get_current_frame(avctx);
1008
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 if (ret < 0) {
1009 93 av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
1010 93 goto end;
1011 }
1012
1013 s->cur_frame.spatial_id = header->spatial_id;
1014 s->cur_frame.temporal_id = header->temporal_id;
1015
1016 if (avctx->hwaccel) {
1017 ret = avctx->hwaccel->start_frame(avctx, unit->data,
1018 unit->data_size);
1019 if (ret < 0) {
1020 av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
1021 goto end;
1022 }
1023 }
1024 if (unit->type != AV1_OBU_FRAME)
1025 break;
1026 // fall-through
1027 case AV1_OBU_TILE_GROUP:
1028 if (!s->raw_frame_header) {
1029 av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
1030 ret = AVERROR_INVALIDDATA;
1031 goto end;
1032 }
1033
1034 if (unit->type == AV1_OBU_FRAME)
1035 raw_tile_group = &obu->obu.frame.tile_group;
1036 else
1037 raw_tile_group = &obu->obu.tile_group;
1038
1039 ret = get_tiles_info(avctx, raw_tile_group);
1040 if (ret < 0)
1041 goto end;
1042
1043 if (avctx->hwaccel) {
1044 ret = avctx->hwaccel->decode_slice(avctx,
1045 raw_tile_group->tile_data.data,
1046 raw_tile_group->tile_data.data_size);
1047 if (ret < 0) {
1048 av_log(avctx, AV_LOG_ERROR,
1049 "HW accel decode slice fail.\n");
1050 goto end;
1051 }
1052 }
1053 break;
1054 326 case AV1_OBU_TILE_LIST:
1055 case AV1_OBU_TEMPORAL_DELIMITER:
1056 case AV1_OBU_PADDING:
1057 case AV1_OBU_METADATA:
1058 326 break;
1059 default:
1060 av_log(avctx, AV_LOG_DEBUG,
1061 "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
1062 unit->type, unit->data_size);
1063 }
1064
1065
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
407 if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
1066 if (avctx->hwaccel) {
1067 ret = avctx->hwaccel->end_frame(avctx);
1068 if (ret < 0) {
1069 av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
1070 goto end;
1071 }
1072 }
1073
1074 ret = update_reference_list(avctx);
1075 if (ret < 0) {
1076 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
1077 goto end;
1078 }
1079
1080 if (s->raw_frame_header->show_frame) {
1081 ret = set_output_frame(avctx, frame, pkt, got_frame);
1082 if (ret < 0) {
1083 av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
1084 goto end;
1085 }
1086 }
1087 raw_tile_group = NULL;
1088 s->raw_frame_header = NULL;
1089 }
1090 }
1091
1092 end:
1093 330 ff_cbs_fragment_reset(&s->current_obu);
1094
1/2
✓ Branch 0 taken 330 times.
✗ Branch 1 not taken.
330 if (ret < 0)
1095 330 s->raw_frame_header = NULL;
1096 330 return ret;
1097 }
1098
1099 static void av1_decode_flush(AVCodecContext *avctx)
1100 {
1101 AV1DecContext *s = avctx->priv_data;
1102
1103 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
1104 av1_frame_unref(avctx, &s->ref[i]);
1105
1106 av1_frame_unref(avctx, &s->cur_frame);
1107 s->operating_point_idc = 0;
1108 s->raw_frame_header = NULL;
1109 s->raw_seq = NULL;
1110
1111 ff_cbs_flush(s->cbc);
1112 }
1113
1114 #define OFFSET(x) offsetof(AV1DecContext, x)
1115 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1116 static const AVOption av1_options[] = {
1117 { "operating_point", "Select an operating point of the scalable bitstream",
1118 OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD },
1119 { NULL }
1120 };
1121
1122 static const AVClass av1_class = {
1123 .class_name = "AV1 decoder",
1124 .item_name = av_default_item_name,
1125 .option = av1_options,
1126 .version = LIBAVUTIL_VERSION_INT,
1127 };
1128
1129 const AVCodec ff_av1_decoder = {
1130 .name = "av1",
1131 .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
1132 .type = AVMEDIA_TYPE_VIDEO,
1133 .id = AV_CODEC_ID_AV1,
1134 .priv_data_size = sizeof(AV1DecContext),
1135 .init = av1_decode_init,
1136 .close = av1_decode_free,
1137 .decode = av1_decode_frame,
1138 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
1139 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1140 FF_CODEC_CAP_INIT_CLEANUP |
1141 FF_CODEC_CAP_SETS_PKT_DTS,
1142 .flush = av1_decode_flush,
1143 .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
1144 .priv_class = &av1_class,
1145 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1146 #if CONFIG_AV1_DXVA2_HWACCEL
1147 HWACCEL_DXVA2(av1),
1148 #endif
1149 #if CONFIG_AV1_D3D11VA_HWACCEL
1150 HWACCEL_D3D11VA(av1),
1151 #endif
1152 #if CONFIG_AV1_D3D11VA2_HWACCEL
1153 HWACCEL_D3D11VA2(av1),
1154 #endif
1155 #if CONFIG_AV1_NVDEC_HWACCEL
1156 HWACCEL_NVDEC(av1),
1157 #endif
1158 #if CONFIG_AV1_VAAPI_HWACCEL
1159 HWACCEL_VAAPI(av1),
1160 #endif
1161 NULL
1162 },
1163 };
1164