FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1dec.c
Date: 2022-01-21 12:56:39
Exec Total Coverage
Lines: 222 702 31.6%
Branches: 88 390 22.6%

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 /**< same with Div_Lut defined in spec 7.11.3.7 */
32 static const uint16_t div_lut[AV1_DIV_LUT_NUM] = {
33 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,
34 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,
35 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,
36 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,
37 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,
38 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,
39 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,
40 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,
41 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,
42 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,
43 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,
44 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,
45 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
46 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,
47 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,
48 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732,
49 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489,
50 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259,
51 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039,
52 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830,
53 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630,
54 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439,
55 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257,
56 8240, 8224, 8208, 8192
57 };
58
59 static uint32_t inverse_recenter(int r, uint32_t v)
60 {
61 if (v > 2 * r)
62 return v;
63 else if (v & 1)
64 return r - ((v + 1) >> 1);
65 else
66 return r + (v >> 1);
67 }
68
69 static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
70 int mx, int r)
71 {
72 if ((r << 1) <= mx) {
73 return inverse_recenter(r, sub_exp);
74 } else {
75 return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
76 }
77 }
78
79 static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
80 int high, int r)
81 {
82 int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
83 return x + low;
84 }
85
86 static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
87 {
88 uint8_t primary_frame, prev_frame;
89 uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;
90 int32_t r, prev_gm_param;
91
92 primary_frame = s->raw_frame_header->primary_ref_frame;
93 prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
94 abs_bits = AV1_GM_ABS_ALPHA_BITS;
95 prec_bits = AV1_GM_ALPHA_PREC_BITS;
96
97 /* setup_past_independence() sets PrevGmParams to default values. We can
98 * simply point to the current's frame gm_params as they will be initialized
99 * with defaults at this point.
100 */
101 if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
102 prev_gm_param = s->cur_frame.gm_params[ref][idx];
103 else
104 prev_gm_param = s->ref[prev_frame].gm_params[ref][idx];
105
106 if (idx < 2) {
107 if (type == AV1_WARP_MODEL_TRANSLATION) {
108 abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
109 !s->raw_frame_header->allow_high_precision_mv;
110 prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
111 !s->raw_frame_header->allow_high_precision_mv;
112 } else {
113 abs_bits = AV1_GM_ABS_TRANS_BITS;
114 prec_bits = AV1_GM_TRANS_PREC_BITS;
115 }
116 }
117 round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
118 prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;
119 sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
120 mx = 1 << abs_bits;
121 r = (prev_gm_param >> prec_diff) - sub;
122
123 s->cur_frame.gm_params[ref][idx] =
124 (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],
125 -mx, mx + 1, r) << prec_diff) + round;
126 }
127
128 static uint64_t round_two(uint64_t x, uint16_t n)
129 {
130 if (n == 0)
131 return x;
132 return ((x + ((uint64_t)1 << (n - 1))) >> n);
133 }
134
135 static int64_t round_two_signed(int64_t x, uint16_t n)
136 {
137 return ((x<0) ? -((int64_t)round_two(-x, n)) : (int64_t)round_two(x, n));
138 }
139
140 /**
141 * Resolve divisor process.
142 * see spec 7.11.3.7
143 */
144 static int16_t resolve_divisor(uint32_t d, uint16_t *shift)
145 {
146 int32_t e, f;
147
148 *shift = av_log2(d);
149 e = d - (1 << (*shift));
150 if (*shift > AV1_DIV_LUT_BITS)
151 f = round_two(e, *shift - AV1_DIV_LUT_BITS);
152 else
153 f = e << (AV1_DIV_LUT_BITS - (*shift));
154
155 *shift += AV1_DIV_LUT_PREC_BITS;
156
157 return div_lut[f];
158 }
159
160 /**
161 * check if global motion params is valid.
162 * see spec 7.11.3.6
163 */
164 static uint8_t get_shear_params_valid(AV1DecContext *s, int idx)
165 {
166 int16_t alpha, beta, gamma, delta, divf, divs;
167 int64_t v, w;
168 int32_t *param = &s->cur_frame.gm_params[idx][0];
169 if (param[2] < 0)
170 return 0;
171
172 alpha = av_clip_int16(param[2] - (1 << AV1_WARPEDMODEL_PREC_BITS));
173 beta = av_clip_int16(param[3]);
174 divf = resolve_divisor(abs(param[2]), &divs);
175 v = (int64_t)param[4] * (1 << AV1_WARPEDMODEL_PREC_BITS);
176 w = (int64_t)param[3] * param[4];
177 gamma = av_clip_int16((int)round_two_signed((v * divf), divs));
178 delta = av_clip_int16(param[5] - (int)round_two_signed((w * divf), divs) - (1 << AV1_WARPEDMODEL_PREC_BITS));
179
180 alpha = round_two_signed(alpha, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
181 beta = round_two_signed(beta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
182 gamma = round_two_signed(gamma, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
183 delta = round_two_signed(delta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
184
185 if ((4 * abs(alpha) + 7 * abs(beta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS) ||
186 (4 * abs(gamma) + 4 * abs(delta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS))
187 return 0;
188
189 return 1;
190 }
191
192 /**
193 * update gm type/params, since cbs already implemented part of this funcation,
194 * so we don't need to full implement spec.
195 */
196 static void global_motion_params(AV1DecContext *s)
197 {
198 const AV1RawFrameHeader *header = s->raw_frame_header;
199 int type, ref;
200
201 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
202 s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;
203 for (int i = 0; i < 6; i++)
204 s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?
205 1 << AV1_WARPEDMODEL_PREC_BITS : 0;
206 }
207 if (header->frame_type == AV1_FRAME_KEY ||
208 header->frame_type == AV1_FRAME_INTRA_ONLY)
209 return;
210
211 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
212 if (header->is_global[ref]) {
213 if (header->is_rot_zoom[ref]) {
214 type = AV1_WARP_MODEL_ROTZOOM;
215 } else {
216 type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
217 : AV1_WARP_MODEL_AFFINE;
218 }
219 } else {
220 type = AV1_WARP_MODEL_IDENTITY;
221 }
222 s->cur_frame.gm_type[ref] = type;
223
224 if (type >= AV1_WARP_MODEL_ROTZOOM) {
225 read_global_param(s, type, ref, 2);
226 read_global_param(s, type, ref, 3);
227 if (type == AV1_WARP_MODEL_AFFINE) {
228 read_global_param(s, type, ref, 4);
229 read_global_param(s, type, ref, 5);
230 } else {
231 s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];
232 s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];
233 }
234 }
235 if (type >= AV1_WARP_MODEL_TRANSLATION) {
236 read_global_param(s, type, ref, 0);
237 read_global_param(s, type, ref, 1);
238 }
239 if (type <= AV1_WARP_MODEL_AFFINE) {
240 s->cur_frame.gm_invalid[ref] = !get_shear_params_valid(s, ref);
241 }
242 }
243 }
244
245 static int get_relative_dist(const AV1RawSequenceHeader *seq,
246 unsigned int a, unsigned int b)
247 {
248 unsigned int diff = a - b;
249 unsigned int m = 1 << seq->order_hint_bits_minus_1;
250 return (diff & (m - 1)) - (diff & m);
251 }
252
253 static void skip_mode_params(AV1DecContext *s)
254 {
255 const AV1RawFrameHeader *header = s->raw_frame_header;
256 const AV1RawSequenceHeader *seq = s->raw_seq;
257
258 int forward_idx, backward_idx;
259 int forward_hint, backward_hint;
260 int second_forward_idx, second_forward_hint;
261 int ref_hint, dist, i;
262
263 if (!header->skip_mode_present)
264 return;
265
266 forward_idx = -1;
267 backward_idx = -1;
268 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
269 ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
270 dist = get_relative_dist(seq, ref_hint, header->order_hint);
271 if (dist < 0) {
272 if (forward_idx < 0 ||
273 get_relative_dist(seq, ref_hint, forward_hint) > 0) {
274 forward_idx = i;
275 forward_hint = ref_hint;
276 }
277 } else if (dist > 0) {
278 if (backward_idx < 0 ||
279 get_relative_dist(seq, ref_hint, backward_hint) < 0) {
280 backward_idx = i;
281 backward_hint = ref_hint;
282 }
283 }
284 }
285
286 if (forward_idx < 0) {
287 return;
288 } else if (backward_idx >= 0) {
289 s->cur_frame.skip_mode_frame_idx[0] =
290 AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx);
291 s->cur_frame.skip_mode_frame_idx[1] =
292 AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx);
293 return;
294 }
295
296 second_forward_idx = -1;
297 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
298 ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
299 if (get_relative_dist(seq, ref_hint, forward_hint) < 0) {
300 if (second_forward_idx < 0 ||
301 get_relative_dist(seq, ref_hint, second_forward_hint) > 0) {
302 second_forward_idx = i;
303 second_forward_hint = ref_hint;
304 }
305 }
306 }
307
308 if (second_forward_idx < 0)
309 return;
310
311 s->cur_frame.skip_mode_frame_idx[0] =
312 AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx);
313 s->cur_frame.skip_mode_frame_idx[1] =
314 AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx);
315 }
316
317 static void coded_lossless_param(AV1DecContext *s)
318 {
319 const AV1RawFrameHeader *header = s->raw_frame_header;
320 int i;
321
322 if (header->delta_q_y_dc || header->delta_q_u_ac ||
323 header->delta_q_u_dc || header->delta_q_v_ac ||
324 header->delta_q_v_dc) {
325 s->cur_frame.coded_lossless = 0;
326 return;
327 }
328
329 s->cur_frame.coded_lossless = 1;
330 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
331 int qindex;
332 if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
333 qindex = (header->base_q_idx +
334 header->feature_value[i][AV1_SEG_LVL_ALT_Q]);
335 } else {
336 qindex = header->base_q_idx;
337 }
338 qindex = av_clip_uintp2(qindex, 8);
339
340 if (qindex) {
341 s->cur_frame.coded_lossless = 0;
342 return;
343 }
344 }
345 }
346
347 static void load_grain_params(AV1DecContext *s)
348 {
349 const AV1RawFrameHeader *header = s->raw_frame_header;
350 const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src;
351 AV1RawFilmGrainParams *dst = &s->cur_frame.film_grain;
352
353 if (!film_grain->apply_grain)
354 return;
355
356 if (film_grain->update_grain) {
357 memcpy(dst, film_grain, sizeof(*dst));
358 return;
359 }
360
361 src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain;
362
363 memcpy(dst, src, sizeof(*dst));
364 dst->grain_seed = film_grain->grain_seed;
365 }
366
367 99 static int init_tile_data(AV1DecContext *s)
368
369 {
370 99 int cur_tile_num =
371 99 s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;
372
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 95 times.
99 if (s->tile_num < cur_tile_num) {
373 4 int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
374 sizeof(TileGroupInfo));
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
376 s->tile_num = 0;
377 return ret;
378 }
379 }
380 99 s->tile_num = cur_tile_num;
381
382 99 return 0;
383 }
384
385 static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
386 {
387 AV1DecContext *s = avctx->priv_data;
388 GetByteContext gb;
389 uint16_t tile_num, tile_row, tile_col;
390 uint32_t size = 0, size_bytes = 0;
391
392 bytestream2_init(&gb, tile_group->tile_data.data,
393 tile_group->tile_data.data_size);
394 s->tg_start = tile_group->tg_start;
395 s->tg_end = tile_group->tg_end;
396
397 for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
398 tile_row = tile_num / s->raw_frame_header->tile_cols;
399 tile_col = tile_num % s->raw_frame_header->tile_cols;
400
401 if (tile_num == tile_group->tg_end) {
402 s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
403 s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
404 s->tile_group_info[tile_num].tile_row = tile_row;
405 s->tile_group_info[tile_num].tile_column = tile_col;
406 return 0;
407 }
408 size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
409 if (bytestream2_get_bytes_left(&gb) < size_bytes)
410 return AVERROR_INVALIDDATA;
411 size = 0;
412 for (int i = 0; i < size_bytes; i++)
413 size |= bytestream2_get_byteu(&gb) << 8 * i;
414 if (bytestream2_get_bytes_left(&gb) <= size)
415 return AVERROR_INVALIDDATA;
416 size++;
417
418 s->tile_group_info[tile_num].tile_size = size;
419 s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
420 s->tile_group_info[tile_num].tile_row = tile_row;
421 s->tile_group_info[tile_num].tile_column = tile_col;
422
423 bytestream2_skipu(&gb, size);
424 }
425
426 return 0;
427
428 }
429
430 21 static int get_pixel_format(AVCodecContext *avctx)
431 {
432 21 AV1DecContext *s = avctx->priv_data;
433 21 const AV1RawSequenceHeader *seq = s->raw_seq;
434 uint8_t bit_depth;
435 int ret;
436 21 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
437 #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
438 CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
439 CONFIG_AV1_NVDEC_HWACCEL + \
440 CONFIG_AV1_VAAPI_HWACCEL)
441 21 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
442
443
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)
444 bit_depth = seq->color_config.twelve_bit ? 12 : 10;
445
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 else if (seq->seq_profile <= 2)
446
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
447 else {
448 av_log(avctx, AV_LOG_ERROR,
449 "Unknown AV1 profile %d.\n", seq->seq_profile);
450 return -1;
451 }
452
453
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!seq->color_config.mono_chrome) {
454 // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (seq->color_config.subsampling_x == 0 &&
456 seq->color_config.subsampling_y == 0) {
457 if (bit_depth == 8)
458 pix_fmt = AV_PIX_FMT_YUV444P;
459 else if (bit_depth == 10)
460 pix_fmt = AV_PIX_FMT_YUV444P10;
461 else if (bit_depth == 12)
462 pix_fmt = AV_PIX_FMT_YUV444P12;
463 else
464 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
465
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 } else if (seq->color_config.subsampling_x == 1 &&
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 seq->color_config.subsampling_y == 0) {
467 if (bit_depth == 8)
468 pix_fmt = AV_PIX_FMT_YUV422P;
469 else if (bit_depth == 10)
470 pix_fmt = AV_PIX_FMT_YUV422P10;
471 else if (bit_depth == 12)
472 pix_fmt = AV_PIX_FMT_YUV422P12;
473 else
474 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
475
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 } else if (seq->color_config.subsampling_x == 1 &&
476
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 seq->color_config.subsampling_y == 1) {
477
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 if (bit_depth == 8)
478 20 pix_fmt = AV_PIX_FMT_YUV420P;
479
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (bit_depth == 10)
480 1 pix_fmt = AV_PIX_FMT_YUV420P10;
481 else if (bit_depth == 12)
482 pix_fmt = AV_PIX_FMT_YUV420P12;
483 else
484 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
485 }
486 } else {
487 if (bit_depth == 8)
488 pix_fmt = AV_PIX_FMT_GRAY8;
489 else if (bit_depth == 10)
490 pix_fmt = AV_PIX_FMT_GRAY10;
491 else if (bit_depth == 12)
492 pix_fmt = AV_PIX_FMT_GRAY12;
493 else
494 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
495 }
496
497 21 av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
498 av_get_pix_fmt_name(pix_fmt));
499
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (pix_fmt == AV_PIX_FMT_NONE)
501 return -1;
502 21 s->pix_fmt = pix_fmt;
503
504
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) {
505 20 case AV_PIX_FMT_YUV420P:
506 #if CONFIG_AV1_DXVA2_HWACCEL
507 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
508 #endif
509 #if CONFIG_AV1_D3D11VA_HWACCEL
510 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
511 *fmtp++ = AV_PIX_FMT_D3D11;
512 #endif
513 #if CONFIG_AV1_NVDEC_HWACCEL
514 *fmtp++ = AV_PIX_FMT_CUDA;
515 #endif
516 #if CONFIG_AV1_VAAPI_HWACCEL
517 20 *fmtp++ = AV_PIX_FMT_VAAPI;
518 #endif
519 20 break;
520 1 case AV_PIX_FMT_YUV420P10:
521 #if CONFIG_AV1_DXVA2_HWACCEL
522 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
523 #endif
524 #if CONFIG_AV1_D3D11VA_HWACCEL
525 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
526 *fmtp++ = AV_PIX_FMT_D3D11;
527 #endif
528 #if CONFIG_AV1_NVDEC_HWACCEL
529 *fmtp++ = AV_PIX_FMT_CUDA;
530 #endif
531 #if CONFIG_AV1_VAAPI_HWACCEL
532 1 *fmtp++ = AV_PIX_FMT_VAAPI;
533 #endif
534 1 break;
535 case AV_PIX_FMT_GRAY8:
536 #if CONFIG_AV1_NVDEC_HWACCEL
537 *fmtp++ = AV_PIX_FMT_CUDA;
538 #endif
539 break;
540 case AV_PIX_FMT_GRAY10:
541 #if CONFIG_AV1_NVDEC_HWACCEL
542 *fmtp++ = AV_PIX_FMT_CUDA;
543 #endif
544 break;
545 }
546
547 21 *fmtp++ = s->pix_fmt;
548 21 *fmtp = AV_PIX_FMT_NONE;
549
550 21 ret = ff_thread_get_format(avctx, pix_fmts);
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
552 return ret;
553
554 /**
555 * check if the HW accel is inited correctly. If not, return un-implemented.
556 * Since now the av1 decoder doesn't support native decode, if it will be
557 * implemented in the future, need remove this check.
558 */
559
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (!avctx->hwaccel) {
560 21 av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
561 " hardware accelerated AV1 decoding.\n");
562 21 return AVERROR(ENOSYS);
563 }
564
565 avctx->pix_fmt = ret;
566
567 return 0;
568 }
569
570 391 static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
571 {
572 391 ff_thread_release_buffer(avctx, &f->tf);
573 391 av_buffer_unref(&f->hwaccel_priv_buf);
574 391 f->hwaccel_picture_private = NULL;
575 391 av_buffer_unref(&f->header_ref);
576 391 f->raw_frame_header = NULL;
577 391 f->spatial_id = f->temporal_id = 0;
578 391 memset(f->skip_mode_frame_idx, 0,
579 2 * sizeof(uint8_t));
580 391 memset(&f->film_grain, 0, sizeof(f->film_grain));
581 391 f->coded_lossless = 0;
582 391 }
583
584 4 static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
585 {
586 int ret;
587
588 4 ret = av_buffer_replace(&dst->header_ref, src->header_ref);
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
590 return ret;
591
592 4 dst->raw_frame_header = src->raw_frame_header;
593
594
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!src->tf.f->buf[0])
595 4 return 0;
596
597 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
598 if (ret < 0)
599 goto fail;
600
601 if (src->hwaccel_picture_private) {
602 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
603 if (!dst->hwaccel_priv_buf)
604 goto fail;
605 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
606 }
607
608 dst->spatial_id = src->spatial_id;
609 dst->temporal_id = src->temporal_id;
610 memcpy(dst->gm_invalid,
611 src->gm_invalid,
612 AV1_NUM_REF_FRAMES * sizeof(uint8_t));
613 memcpy(dst->gm_type,
614 src->gm_type,
615 AV1_NUM_REF_FRAMES * sizeof(uint8_t));
616 memcpy(dst->gm_params,
617 src->gm_params,
618 AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
619 memcpy(dst->skip_mode_frame_idx,
620 src->skip_mode_frame_idx,
621 2 * sizeof(uint8_t));
622 memcpy(&dst->film_grain,
623 &src->film_grain,
624 sizeof(dst->film_grain));
625 dst->coded_lossless = src->coded_lossless;
626
627 return 0;
628
629 fail:
630 av1_frame_unref(avctx, dst);
631 return AVERROR(ENOMEM);
632 }
633
634 21 static av_cold int av1_decode_free(AVCodecContext *avctx)
635 {
636 21 AV1DecContext *s = avctx->priv_data;
637
638
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 21 times.
189 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
639 168 av1_frame_unref(avctx, &s->ref[i]);
640 168 av_frame_free(&s->ref[i].tf.f);
641 }
642 21 av1_frame_unref(avctx, &s->cur_frame);
643 21 av_frame_free(&s->cur_frame.tf.f);
644
645 21 av_buffer_unref(&s->seq_ref);
646 21 av_buffer_unref(&s->header_ref);
647 21 av_freep(&s->tile_group_info);
648
649 21 ff_cbs_fragment_free(&s->current_obu);
650 21 ff_cbs_close(&s->cbc);
651
652 21 return 0;
653 }
654
655 104 static int set_context_with_sequence(AVCodecContext *avctx,
656 const AV1RawSequenceHeader *seq)
657 {
658 104 int width = seq->max_frame_width_minus_1 + 1;
659 104 int height = seq->max_frame_height_minus_1 + 1;
660
661 104 avctx->profile = seq->seq_profile;
662 104 avctx->level = seq->seq_level_idx[0];
663
664 104 avctx->color_range =
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
666 104 avctx->color_primaries = seq->color_config.color_primaries;
667 104 avctx->colorspace = seq->color_config.color_primaries;
668 104 avctx->color_trc = seq->color_config.transfer_characteristics;
669
670
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
104 switch (seq->color_config.chroma_sample_position) {
671 case AV1_CSP_VERTICAL:
672 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
673 break;
674 case AV1_CSP_COLOCATED:
675 avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
676 break;
677 }
678
679
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 102 times.
104 if (seq->film_grain_params_present)
680 2 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
681 else
682 102 avctx->properties &= ~FF_CODEC_PROPERTY_FILM_GRAIN;
683
684
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) {
685 3 int ret = ff_set_dimensions(avctx, width, height);
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
687 return ret;
688 }
689 104 avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
690
691
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 39 times.
104 if (seq->timing_info.num_units_in_display_tick &&
692
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 seq->timing_info.time_scale) {
693 65 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
694 65 seq->timing_info.num_units_in_display_tick,
695 65 seq->timing_info.time_scale,
696 INT_MAX);
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (seq->timing_info.equal_picture_interval)
698 avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
699 }
700
701 104 return 0;
702 }
703
704 99 static int update_context_with_frame_header(AVCodecContext *avctx,
705 const AV1RawFrameHeader *header)
706 {
707 AVRational aspect_ratio;
708 99 int width = header->frame_width_minus_1 + 1;
709 99 int height = header->frame_height_minus_1 + 1;
710 99 int r_width = header->render_width_minus_1 + 1;
711 99 int r_height = header->render_height_minus_1 + 1;
712 int ret;
713
714
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 99 times.
99 if (avctx->width != width || avctx->height != height) {
715 ret = ff_set_dimensions(avctx, width, height);
716 if (ret < 0)
717 return ret;
718 }
719
720 99 av_reduce(&aspect_ratio.num, &aspect_ratio.den,
721 99 (int64_t)height * r_width,
722 99 (int64_t)width * r_height,
723 INT_MAX);
724
725
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
99 if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
726 ret = ff_set_sar(avctx, aspect_ratio);
727 if (ret < 0)
728 return ret;
729 }
730
731 99 return 0;
732 }
733
734 21 static av_cold int av1_decode_init(AVCodecContext *avctx)
735 {
736 21 AV1DecContext *s = avctx->priv_data;
737 AV1RawSequenceHeader *seq;
738 int ret;
739
740 21 s->avctx = avctx;
741 21 s->pix_fmt = AV_PIX_FMT_NONE;
742
743
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 21 times.
189 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
744 168 s->ref[i].tf.f = av_frame_alloc();
745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (!s->ref[i].tf.f) {
746 av_log(avctx, AV_LOG_ERROR,
747 "Failed to allocate reference frame buffer %d.\n", i);
748 return AVERROR(ENOMEM);
749 }
750 }
751
752 21 s->cur_frame.tf.f = av_frame_alloc();
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!s->cur_frame.tf.f) {
754 av_log(avctx, AV_LOG_ERROR,
755 "Failed to allocate current frame buffer.\n");
756 return AVERROR(ENOMEM);
757 }
758
759 21 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
761 return ret;
762
763 21 av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
764
765
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) {
766 2 ret = ff_cbs_read_extradata_from_codec(s->cbc,
767 &s->current_obu,
768 avctx);
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
770 av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
771 return ret;
772 }
773
774 2 seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!seq) {
776 av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
777 goto end;
778 }
779
780 2 ret = set_context_with_sequence(avctx, seq);
781
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ret < 0) {
782 av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
783 goto end;
784 }
785
786 2 end:
787 2 ff_cbs_fragment_reset(&s->current_obu);
788 }
789
790 21 return ret;
791 }
792
793 99 static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
794 {
795 99 AV1DecContext *s = avctx->priv_data;
796 99 AV1RawFrameHeader *header= s->raw_frame_header;
797 AVFrame *frame;
798 int ret;
799
800 99 ret = update_context_with_frame_header(avctx, header);
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (ret < 0) {
802 av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
803 return ret;
804 }
805
806
1/2
✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
99 if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
807 99 goto fail;
808
809 frame = f->tf.f;
810 frame->key_frame = header->frame_type == AV1_FRAME_KEY;
811
812 switch (header->frame_type) {
813 case AV1_FRAME_KEY:
814 case AV1_FRAME_INTRA_ONLY:
815 frame->pict_type = AV_PICTURE_TYPE_I;
816 break;
817 case AV1_FRAME_INTER:
818 frame->pict_type = AV_PICTURE_TYPE_P;
819 break;
820 case AV1_FRAME_SWITCH:
821 frame->pict_type = AV_PICTURE_TYPE_SP;
822 break;
823 }
824
825 if (avctx->hwaccel) {
826 const AVHWAccel *hwaccel = avctx->hwaccel;
827 if (hwaccel->frame_priv_data_size) {
828 f->hwaccel_priv_buf =
829 av_buffer_allocz(hwaccel->frame_priv_data_size);
830 if (!f->hwaccel_priv_buf) {
831 ret = AVERROR(ENOMEM);
832 goto fail;
833 }
834 f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
835 }
836 }
837 return 0;
838
839 99 fail:
840 99 av1_frame_unref(avctx, f);
841 99 return ret;
842 }
843
844 static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
845 {
846 AV1DecContext *s = avctx->priv_data;
847 const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain;
848 AVFilmGrainParams *fgp;
849 AVFilmGrainAOMParams *aom;
850
851 if (!film_grain->apply_grain)
852 return 0;
853
854 fgp = av_film_grain_params_create_side_data(frame);
855 if (!fgp)
856 return AVERROR(ENOMEM);
857
858 fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
859 fgp->seed = film_grain->grain_seed;
860
861 aom = &fgp->codec.aom;
862 aom->chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma;
863 aom->scaling_shift = film_grain->grain_scaling_minus_8 + 8;
864 aom->ar_coeff_lag = film_grain->ar_coeff_lag;
865 aom->ar_coeff_shift = film_grain->ar_coeff_shift_minus_6 + 6;
866 aom->grain_scale_shift = film_grain->grain_scale_shift;
867 aom->overlap_flag = film_grain->overlap_flag;
868 aom->limit_output_range = film_grain->clip_to_restricted_range;
869
870 aom->num_y_points = film_grain->num_y_points;
871 for (int i = 0; i < film_grain->num_y_points; i++) {
872 aom->y_points[i][0] = film_grain->point_y_value[i];
873 aom->y_points[i][1] = film_grain->point_y_scaling[i];
874 }
875 aom->num_uv_points[0] = film_grain->num_cb_points;
876 for (int i = 0; i < film_grain->num_cb_points; i++) {
877 aom->uv_points[0][i][0] = film_grain->point_cb_value[i];
878 aom->uv_points[0][i][1] = film_grain->point_cb_scaling[i];
879 }
880 aom->num_uv_points[1] = film_grain->num_cr_points;
881 for (int i = 0; i < film_grain->num_cr_points; i++) {
882 aom->uv_points[1][i][0] = film_grain->point_cr_value[i];
883 aom->uv_points[1][i][1] = film_grain->point_cr_scaling[i];
884 }
885
886 for (int i = 0; i < 24; i++) {
887 aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128;
888 }
889 for (int i = 0; i < 25; i++) {
890 aom->ar_coeffs_uv[0][i] = film_grain->ar_coeffs_cb_plus_128[i] - 128;
891 aom->ar_coeffs_uv[1][i] = film_grain->ar_coeffs_cr_plus_128[i] - 128;
892 }
893
894 aom->uv_mult[0] = film_grain->cb_mult;
895 aom->uv_mult[1] = film_grain->cr_mult;
896 aom->uv_mult_luma[0] = film_grain->cb_luma_mult;
897 aom->uv_mult_luma[1] = film_grain->cr_luma_mult;
898 aom->uv_offset[0] = film_grain->cb_offset;
899 aom->uv_offset[1] = film_grain->cr_offset;
900
901 return 0;
902 }
903
904 static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
905 const AVPacket *pkt, int *got_frame)
906 {
907 AV1DecContext *s = avctx->priv_data;
908 const AVFrame *srcframe = s->cur_frame.tf.f;
909 int ret;
910
911 // TODO: all layers
912 if (s->operating_point_idc &&
913 av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id)
914 return 0;
915
916 ret = av_frame_ref(frame, srcframe);
917 if (ret < 0)
918 return ret;
919
920 if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
921 ret = export_film_grain(avctx, frame);
922 if (ret < 0) {
923 av_frame_unref(frame);
924 return ret;
925 }
926 }
927
928 frame->pts = pkt->pts;
929 frame->pkt_dts = pkt->dts;
930 frame->pkt_size = pkt->size;
931
932 *got_frame = 1;
933
934 return 0;
935 }
936
937 4 static int update_reference_list(AVCodecContext *avctx)
938 {
939 4 AV1DecContext *s = avctx->priv_data;
940 4 const AV1RawFrameHeader *header = s->raw_frame_header;
941 int ret;
942
943
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
36 for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (header->refresh_frame_flags & (1 << i)) {
945 av1_frame_unref(avctx, &s->ref[i]);
946 if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
947 av_log(avctx, AV_LOG_ERROR,
948 "Failed to update frame %d in reference list\n", i);
949 return ret;
950 }
951 }
952 }
953 4 return 0;
954 }
955
956 99 static int get_current_frame(AVCodecContext *avctx)
957 {
958 99 AV1DecContext *s = avctx->priv_data;
959 int ret;
960
961 99 av1_frame_unref(avctx, &s->cur_frame);
962
963 99 s->cur_frame.header_ref = av_buffer_ref(s->header_ref);
964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (!s->cur_frame.header_ref)
965 return AVERROR(ENOMEM);
966
967 99 s->cur_frame.raw_frame_header = s->raw_frame_header;
968
969 99 ret = init_tile_data(s);
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (ret < 0) {
971 av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
972 return ret;
973 }
974
975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if ((avctx->skip_frame >= AVDISCARD_NONINTRA &&
976 (s->raw_frame_header->frame_type != AV1_FRAME_KEY &&
977 s->raw_frame_header->frame_type != AV1_FRAME_INTRA_ONLY)) ||
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 (avctx->skip_frame >= AVDISCARD_NONKEY &&
979 s->raw_frame_header->frame_type != AV1_FRAME_KEY) ||
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 avctx->skip_frame >= AVDISCARD_ALL)
981 return 0;
982
983 99 ret = av1_frame_alloc(avctx, &s->cur_frame);
984
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
99 if (ret < 0) {
985 99 av_log(avctx, AV_LOG_ERROR,
986 "Failed to allocate space for current frame.\n");
987 99 return ret;
988 }
989
990 global_motion_params(s);
991 skip_mode_params(s);
992 coded_lossless_param(s);
993 load_grain_params(s);
994
995 return ret;
996 }
997
998 334 static int av1_decode_frame(AVCodecContext *avctx, void *frame,
999 int *got_frame, AVPacket *pkt)
1000 {
1001 334 AV1DecContext *s = avctx->priv_data;
1002 334 AV1RawTileGroup *raw_tile_group = NULL;
1003 int ret;
1004
1005 334 ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
1006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 334 times.
334 if (ret < 0) {
1007 av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
1008 goto end;
1009 }
1010 334 av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
1011 s->current_obu.nb_units);
1012
1013
1/2
✓ Branch 0 taken 676 times.
✗ Branch 1 not taken.
676 for (int i = 0; i < s->current_obu.nb_units; i++) {
1014 676 CodedBitstreamUnit *unit = &s->current_obu.units[i];
1015 676 AV1RawOBU *obu = unit->content;
1016 const AV1RawOBUHeader *header;
1017
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 676 times.
676 if (!obu)
1019 continue;
1020
1021 676 header = &obu->header;
1022 676 av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
1023
1024
3/6
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 313 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 261 times.
✗ Branch 5 not taken.
676 switch (unit->type) {
1025 102 case AV1_OBU_SEQUENCE_HEADER:
1026 102 av_buffer_unref(&s->seq_ref);
1027 102 s->seq_ref = av_buffer_ref(unit->content_ref);
1028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (!s->seq_ref) {
1029 ret = AVERROR(ENOMEM);
1030 goto end;
1031 }
1032
1033 102 s->raw_seq = &obu->obu.sequence_header;
1034
1035 102 ret = set_context_with_sequence(avctx, s->raw_seq);
1036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (ret < 0) {
1037 av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
1038 s->raw_seq = NULL;
1039 goto end;
1040 }
1041
1042 102 s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point];
1043
1044
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 81 times.
102 if (s->pix_fmt == AV_PIX_FMT_NONE) {
1045 21 ret = get_pixel_format(avctx);
1046
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (ret < 0) {
1047 21 av_log(avctx, AV_LOG_ERROR,
1048 "Failed to get pixel format.\n");
1049 21 s->raw_seq = NULL;
1050 21 goto end;
1051 }
1052 }
1053
1054
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) {
1055 ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
1056 unit->data_size);
1057 if (ret < 0) {
1058 av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
1059 s->raw_seq = NULL;
1060 goto end;
1061 }
1062 }
1063 81 break;
1064 case AV1_OBU_REDUNDANT_FRAME_HEADER:
1065 if (s->raw_frame_header)
1066 break;
1067 // fall-through
1068 case AV1_OBU_FRAME:
1069 case AV1_OBU_FRAME_HEADER:
1070
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 103 times.
313 if (!s->raw_seq) {
1071 210 av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
1072 210 ret = AVERROR_INVALIDDATA;
1073 210 goto end;
1074 }
1075
1076 103 av_buffer_unref(&s->header_ref);
1077 103 s->header_ref = av_buffer_ref(unit->content_ref);
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (!s->header_ref) {
1079 ret = AVERROR(ENOMEM);
1080 goto end;
1081 }
1082
1083
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 4 times.
103 if (unit->type == AV1_OBU_FRAME)
1084 99 s->raw_frame_header = &obu->obu.frame.header;
1085 else
1086 4 s->raw_frame_header = &obu->obu.frame_header;
1087
1088
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 99 times.
103 if (s->raw_frame_header->show_existing_frame) {
1089 4 av1_frame_unref(avctx, &s->cur_frame);
1090
1091 4 ret = av1_frame_ref(avctx, &s->cur_frame,
1092 4 &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
1093
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
1094 av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
1095 goto end;
1096 }
1097
1098 4 ret = update_reference_list(avctx);
1099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
1100 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
1101 goto end;
1102 }
1103
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->cur_frame.tf.f->buf[0]) {
1105 ret = set_output_frame(avctx, frame, pkt, got_frame);
1106 if (ret < 0)
1107 av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
1108 }
1109
1110 4 s->raw_frame_header = NULL;
1111
1112 4 goto end;
1113 }
1114
1115 99 ret = get_current_frame(avctx);
1116
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
99 if (ret < 0) {
1117 99 av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
1118 99 goto end;
1119 }
1120
1121 s->cur_frame.spatial_id = header->spatial_id;
1122 s->cur_frame.temporal_id = header->temporal_id;
1123
1124 if (avctx->hwaccel && s->cur_frame.tf.f->buf[0]) {
1125 ret = avctx->hwaccel->start_frame(avctx, unit->data,
1126 unit->data_size);
1127 if (ret < 0) {
1128 av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
1129 goto end;
1130 }
1131 }
1132 if (unit->type != AV1_OBU_FRAME)
1133 break;
1134 // fall-through
1135 case AV1_OBU_TILE_GROUP:
1136 if (!s->raw_frame_header) {
1137 av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
1138 ret = AVERROR_INVALIDDATA;
1139 goto end;
1140 }
1141
1142 if (unit->type == AV1_OBU_FRAME)
1143 raw_tile_group = &obu->obu.frame.tile_group;
1144 else
1145 raw_tile_group = &obu->obu.tile_group;
1146
1147 ret = get_tiles_info(avctx, raw_tile_group);
1148 if (ret < 0)
1149 goto end;
1150
1151 if (avctx->hwaccel && s->cur_frame.tf.f->buf[0]) {
1152 ret = avctx->hwaccel->decode_slice(avctx,
1153 raw_tile_group->tile_data.data,
1154 raw_tile_group->tile_data.data_size);
1155 if (ret < 0) {
1156 av_log(avctx, AV_LOG_ERROR,
1157 "HW accel decode slice fail.\n");
1158 goto end;
1159 }
1160 }
1161 break;
1162 261 case AV1_OBU_TILE_LIST:
1163 case AV1_OBU_TEMPORAL_DELIMITER:
1164 case AV1_OBU_PADDING:
1165 case AV1_OBU_METADATA:
1166 261 break;
1167 default:
1168 av_log(avctx, AV_LOG_DEBUG,
1169 "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
1170 unit->type, unit->data_size);
1171 }
1172
1173
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
342 if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
1174 if (avctx->hwaccel && s->cur_frame.tf.f->buf[0]) {
1175 ret = avctx->hwaccel->end_frame(avctx);
1176 if (ret < 0) {
1177 av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
1178 goto end;
1179 }
1180 }
1181
1182 ret = update_reference_list(avctx);
1183 if (ret < 0) {
1184 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
1185 goto end;
1186 }
1187
1188 if (s->raw_frame_header->show_frame && s->cur_frame.tf.f->buf[0]) {
1189 ret = set_output_frame(avctx, frame, pkt, got_frame);
1190 if (ret < 0) {
1191 av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
1192 goto end;
1193 }
1194 }
1195 raw_tile_group = NULL;
1196 s->raw_frame_header = NULL;
1197 }
1198 }
1199
1200 end:
1201 334 ff_cbs_fragment_reset(&s->current_obu);
1202
2/2
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 4 times.
334 if (ret < 0)
1203 330 s->raw_frame_header = NULL;
1204 334 return ret;
1205 }
1206
1207 static void av1_decode_flush(AVCodecContext *avctx)
1208 {
1209 AV1DecContext *s = avctx->priv_data;
1210
1211 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
1212 av1_frame_unref(avctx, &s->ref[i]);
1213
1214 av1_frame_unref(avctx, &s->cur_frame);
1215 s->operating_point_idc = 0;
1216 s->raw_frame_header = NULL;
1217 s->raw_seq = NULL;
1218
1219 ff_cbs_flush(s->cbc);
1220 }
1221
1222 #define OFFSET(x) offsetof(AV1DecContext, x)
1223 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1224 static const AVOption av1_options[] = {
1225 { "operating_point", "Select an operating point of the scalable bitstream",
1226 OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD },
1227 { NULL }
1228 };
1229
1230 static const AVClass av1_class = {
1231 .class_name = "AV1 decoder",
1232 .item_name = av_default_item_name,
1233 .option = av1_options,
1234 .version = LIBAVUTIL_VERSION_INT,
1235 };
1236
1237 const AVCodec ff_av1_decoder = {
1238 .name = "av1",
1239 .long_name = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
1240 .type = AVMEDIA_TYPE_VIDEO,
1241 .id = AV_CODEC_ID_AV1,
1242 .priv_data_size = sizeof(AV1DecContext),
1243 .init = av1_decode_init,
1244 .close = av1_decode_free,
1245 .decode = av1_decode_frame,
1246 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
1247 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1248 FF_CODEC_CAP_INIT_CLEANUP |
1249 FF_CODEC_CAP_SETS_PKT_DTS,
1250 .flush = av1_decode_flush,
1251 .profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
1252 .priv_class = &av1_class,
1253 .bsfs = "av1_frame_split",
1254 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1255 #if CONFIG_AV1_DXVA2_HWACCEL
1256 HWACCEL_DXVA2(av1),
1257 #endif
1258 #if CONFIG_AV1_D3D11VA_HWACCEL
1259 HWACCEL_D3D11VA(av1),
1260 #endif
1261 #if CONFIG_AV1_D3D11VA2_HWACCEL
1262 HWACCEL_D3D11VA2(av1),
1263 #endif
1264 #if CONFIG_AV1_NVDEC_HWACCEL
1265 HWACCEL_NVDEC(av1),
1266 #endif
1267 #if CONFIG_AV1_VAAPI_HWACCEL
1268 HWACCEL_VAAPI(av1),
1269 #endif
1270 NULL
1271 },
1272 };
1273