FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1dec.c
Date: 2026-04-17 20:31:08
Exec Total Coverage
Lines: 276 919 30.0%
Functions: 12 34 35.3%
Branches: 108 502 21.5%

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 "config_components.h"
22
23 #include "libavutil/attributes.h"
24 #include "libavutil/hdr_dynamic_metadata.h"
25 #include "libavutil/film_grain_params.h"
26 #include "libavutil/mastering_display_metadata.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/opt.h"
30 #include "avcodec.h"
31 #include "av1_parse.h"
32 #include "av1dec.h"
33 #include "atsc_a53.h"
34 #include "bytestream.h"
35 #include "codec_internal.h"
36 #include "decode.h"
37 #include "hwaccel_internal.h"
38 #include "internal.h"
39 #include "itut35.h"
40 #include "hwconfig.h"
41 #include "profiles.h"
42 #include "progressframe.h"
43 #include "libavutil/refstruct.h"
44
45 /** same with Div_Lut defined in spec 7.11.3.7 */
46 static const uint16_t div_lut[AV1_DIV_LUT_NUM] = {
47 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,
48 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,
49 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,
50 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,
51 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,
52 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,
53 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,
54 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,
55 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,
56 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,
57 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,
58 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,
59 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
60 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,
61 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,
62 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732,
63 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489,
64 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259,
65 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039,
66 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830,
67 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630,
68 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439,
69 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257,
70 8240, 8224, 8208, 8192
71 };
72
73 static uint32_t inverse_recenter(int r, uint32_t v)
74 {
75 if (v > 2 * r)
76 return v;
77 else if (v & 1)
78 return r - ((v + 1) >> 1);
79 else
80 return r + (v >> 1);
81 }
82
83 static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
84 int mx, int r)
85 {
86 if ((r << 1) <= mx) {
87 return inverse_recenter(r, sub_exp);
88 } else {
89 return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
90 }
91 }
92
93 static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
94 int high, int r)
95 {
96 int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
97 return x + low;
98 }
99
100 static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
101 {
102 uint8_t primary_frame, prev_frame;
103 uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;
104 int32_t r, prev_gm_param;
105
106 primary_frame = s->raw_frame_header->primary_ref_frame;
107 prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
108 abs_bits = AV1_GM_ABS_ALPHA_BITS;
109 prec_bits = AV1_GM_ALPHA_PREC_BITS;
110
111 /* setup_past_independence() sets PrevGmParams to default values. We can
112 * simply point to the current's frame gm_params as they will be initialized
113 * with defaults at this point.
114 */
115 if (s->raw_frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE)
116 prev_gm_param = s->cur_frame.gm_params[ref][idx];
117 else
118 prev_gm_param = s->ref[prev_frame].gm_params[ref][idx];
119
120 if (idx < 2) {
121 if (type == AV1_WARP_MODEL_TRANSLATION) {
122 abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
123 !s->raw_frame_header->allow_high_precision_mv;
124 prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
125 !s->raw_frame_header->allow_high_precision_mv;
126 } else {
127 abs_bits = AV1_GM_ABS_TRANS_BITS;
128 prec_bits = AV1_GM_TRANS_PREC_BITS;
129 }
130 }
131 round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
132 prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;
133 sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
134 mx = 1 << abs_bits;
135 r = (prev_gm_param >> prec_diff) - sub;
136
137 s->cur_frame.gm_params[ref][idx] =
138 (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],
139 -mx, mx + 1, r) << prec_diff) + round;
140 }
141
142 static uint64_t round_two(uint64_t x, uint16_t n)
143 {
144 if (n == 0)
145 return x;
146 return ((x + ((uint64_t)1 << (n - 1))) >> n);
147 }
148
149 static int64_t round_two_signed(int64_t x, uint16_t n)
150 {
151 return ((x<0) ? -((int64_t)round_two(-x, n)) : (int64_t)round_two(x, n));
152 }
153
154 /**
155 * Resolve divisor process.
156 * see spec 7.11.3.7
157 */
158 static int16_t resolve_divisor(uint32_t d, uint16_t *shift)
159 {
160 int32_t e, f;
161
162 *shift = av_log2(d);
163 e = d - (1 << (*shift));
164 if (*shift > AV1_DIV_LUT_BITS)
165 f = round_two(e, *shift - AV1_DIV_LUT_BITS);
166 else
167 f = e << (AV1_DIV_LUT_BITS - (*shift));
168
169 *shift += AV1_DIV_LUT_PREC_BITS;
170
171 return div_lut[f];
172 }
173
174 /**
175 * check if global motion params is valid.
176 * see spec 7.11.3.6
177 */
178 static uint8_t get_shear_params_valid(AV1DecContext *s, int idx)
179 {
180 int16_t alpha, beta, gamma, delta, divf, divs;
181 int64_t v, w;
182 int32_t *param = &s->cur_frame.gm_params[idx][0];
183 if (param[2] <= 0)
184 return 0;
185
186 alpha = av_clip_int16(param[2] - (1 << AV1_WARPEDMODEL_PREC_BITS));
187 beta = av_clip_int16(param[3]);
188 divf = resolve_divisor(abs(param[2]), &divs);
189 v = (int64_t)param[4] * (1 << AV1_WARPEDMODEL_PREC_BITS);
190 w = (int64_t)param[3] * param[4];
191 gamma = av_clip_int16((int)round_two_signed((v * divf), divs));
192 delta = av_clip_int16(param[5] - (int)round_two_signed((w * divf), divs) - (1 << AV1_WARPEDMODEL_PREC_BITS));
193
194 alpha = round_two_signed(alpha, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
195 beta = round_two_signed(beta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
196 gamma = round_two_signed(gamma, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
197 delta = round_two_signed(delta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS;
198
199 if ((4 * abs(alpha) + 7 * abs(beta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS) ||
200 (4 * abs(gamma) + 4 * abs(delta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS))
201 return 0;
202
203 return 1;
204 }
205
206 /**
207 * update gm type/params, since cbs already implemented part of this function,
208 * so we don't need to full implement spec.
209 */
210 static void global_motion_params(AV1DecContext *s)
211 {
212 const AV1RawFrameHeader *header = s->raw_frame_header;
213 int type, ref;
214
215 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
216 s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;
217 for (int i = 0; i < 6; i++)
218 s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?
219 1 << AV1_WARPEDMODEL_PREC_BITS : 0;
220 }
221 if (header->frame_type == AV1_FRAME_KEY ||
222 header->frame_type == AV1_FRAME_INTRA_ONLY)
223 return;
224
225 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
226 if (header->is_global[ref]) {
227 if (header->is_rot_zoom[ref]) {
228 type = AV1_WARP_MODEL_ROTZOOM;
229 } else {
230 type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
231 : AV1_WARP_MODEL_AFFINE;
232 }
233 } else {
234 type = AV1_WARP_MODEL_IDENTITY;
235 }
236 s->cur_frame.gm_type[ref] = type;
237
238 if (type >= AV1_WARP_MODEL_ROTZOOM) {
239 read_global_param(s, type, ref, 2);
240 read_global_param(s, type, ref, 3);
241 if (type == AV1_WARP_MODEL_AFFINE) {
242 read_global_param(s, type, ref, 4);
243 read_global_param(s, type, ref, 5);
244 } else {
245 s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];
246 s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];
247 }
248 }
249 if (type >= AV1_WARP_MODEL_TRANSLATION) {
250 read_global_param(s, type, ref, 0);
251 read_global_param(s, type, ref, 1);
252 }
253 if (type <= AV1_WARP_MODEL_AFFINE) {
254 s->cur_frame.gm_invalid[ref] = !get_shear_params_valid(s, ref);
255 }
256 }
257 }
258
259 static int get_relative_dist(const AV1RawSequenceHeader *seq,
260 unsigned int a, unsigned int b)
261 {
262 unsigned int diff = a - b;
263 unsigned int m = 1 << seq->order_hint_bits_minus_1;
264 return (diff & (m - 1)) - (diff & m);
265 }
266
267 static void skip_mode_params(AV1DecContext *s)
268 {
269 const AV1RawFrameHeader *header = s->raw_frame_header;
270 const AV1RawSequenceHeader *seq = s->raw_seq;
271
272 int forward_idx, backward_idx;
273 int forward_hint, backward_hint;
274 int second_forward_idx, second_forward_hint;
275 int ref_hint, dist, i;
276
277 if (header->frame_type == AV1_FRAME_KEY ||
278 header->frame_type == AV1_FRAME_INTRA_ONLY ||
279 !header->reference_select || !seq->enable_order_hint)
280 return;
281
282 forward_idx = -1;
283 backward_idx = -1;
284 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
285 if (!s->ref[header->ref_frame_idx[i]].raw_frame_header)
286 return;
287 ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
288 dist = get_relative_dist(seq, ref_hint, header->order_hint);
289 if (dist < 0) {
290 if (forward_idx < 0 ||
291 get_relative_dist(seq, ref_hint, forward_hint) > 0) {
292 forward_idx = i;
293 forward_hint = ref_hint;
294 }
295 } else if (dist > 0) {
296 if (backward_idx < 0 ||
297 get_relative_dist(seq, ref_hint, backward_hint) < 0) {
298 backward_idx = i;
299 backward_hint = ref_hint;
300 }
301 }
302 }
303
304 if (forward_idx < 0) {
305 return;
306 } else if (backward_idx >= 0) {
307 s->cur_frame.skip_mode_frame_idx[0] =
308 AV1_REF_FRAME_LAST + FFMIN(forward_idx, backward_idx);
309 s->cur_frame.skip_mode_frame_idx[1] =
310 AV1_REF_FRAME_LAST + FFMAX(forward_idx, backward_idx);
311 return;
312 }
313
314 second_forward_idx = -1;
315 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
316 ref_hint = s->ref[header->ref_frame_idx[i]].raw_frame_header->order_hint;
317 if (get_relative_dist(seq, ref_hint, forward_hint) < 0) {
318 if (second_forward_idx < 0 ||
319 get_relative_dist(seq, ref_hint, second_forward_hint) > 0) {
320 second_forward_idx = i;
321 second_forward_hint = ref_hint;
322 }
323 }
324 }
325
326 if (second_forward_idx < 0)
327 return;
328
329 s->cur_frame.skip_mode_frame_idx[0] =
330 AV1_REF_FRAME_LAST + FFMIN(forward_idx, second_forward_idx);
331 s->cur_frame.skip_mode_frame_idx[1] =
332 AV1_REF_FRAME_LAST + FFMAX(forward_idx, second_forward_idx);
333 }
334
335 static void coded_lossless_param(AV1DecContext *s)
336 {
337 const AV1RawFrameHeader *header = s->raw_frame_header;
338 int i;
339
340 if (header->delta_q_y_dc || header->delta_q_u_ac ||
341 header->delta_q_u_dc || header->delta_q_v_ac ||
342 header->delta_q_v_dc) {
343 s->cur_frame.coded_lossless = 0;
344 return;
345 }
346
347 s->cur_frame.coded_lossless = 1;
348 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
349 int qindex;
350 if (header->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
351 qindex = (header->base_q_idx +
352 header->feature_value[i][AV1_SEG_LVL_ALT_Q]);
353 } else {
354 qindex = header->base_q_idx;
355 }
356 qindex = av_clip_uintp2(qindex, 8);
357
358 if (qindex) {
359 s->cur_frame.coded_lossless = 0;
360 return;
361 }
362 }
363 }
364
365 static void order_hint_info(AV1DecContext *s)
366 {
367 const AV1RawFrameHeader *header = s->raw_frame_header;
368 const AV1RawSequenceHeader *seq = s->raw_seq;
369 AV1Frame *frame = &s->cur_frame;
370
371 frame->order_hint = header->order_hint;
372
373 for (int i = 0; i < AV1_REFS_PER_FRAME; i++) {
374 int ref_name = i + AV1_REF_FRAME_LAST;
375 int ref_slot = header->ref_frame_idx[i];
376 int ref_order_hint = s->ref[ref_slot].order_hint;
377
378 frame->order_hints[ref_name] = ref_order_hint;
379 if (!seq->enable_order_hint) {
380 frame->ref_frame_sign_bias[ref_name] = 0;
381 } else {
382 frame->ref_frame_sign_bias[ref_name] =
383 get_relative_dist(seq, ref_order_hint,
384 frame->order_hint) > 0;
385 }
386 }
387 }
388
389 static void load_grain_params(AV1DecContext *s)
390 {
391 const AV1RawFrameHeader *header = s->raw_frame_header;
392 const AV1RawFilmGrainParams *film_grain = &header->film_grain, *src;
393 AV1RawFilmGrainParams *dst = &s->cur_frame.film_grain;
394
395 if (!film_grain->apply_grain)
396 return;
397
398 if (film_grain->update_grain) {
399 memcpy(dst, film_grain, sizeof(*dst));
400 return;
401 }
402
403 src = &s->ref[film_grain->film_grain_params_ref_idx].film_grain;
404
405 memcpy(dst, src, sizeof(*dst));
406 dst->grain_seed = film_grain->grain_seed;
407 }
408
409 24 static int init_tile_data(AV1DecContext *s)
410
411 {
412 24 int cur_tile_num =
413 24 s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;
414
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (s->tile_num < cur_tile_num) {
415 24 int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
416 sizeof(TileGroupInfo));
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0) {
418 s->tile_num = 0;
419 return ret;
420 }
421 }
422 24 s->tile_num = cur_tile_num;
423
424 24 return 0;
425 }
426
427 32 static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
428 {
429 32 AV1DecContext *s = avctx->priv_data;
430 GetByteContext gb;
431 uint16_t tile_num, tile_row, tile_col;
432 32 uint32_t size = 0, size_bytes = 0;
433
434 32 bytestream2_init(&gb, tile_group->tile_data.data,
435 32 tile_group->tile_data.data_size);
436 32 s->tg_start = tile_group->tg_start;
437 32 s->tg_end = tile_group->tg_end;
438
439
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
440 47 tile_row = tile_num / s->raw_frame_header->tile_cols;
441 47 tile_col = tile_num % s->raw_frame_header->tile_cols;
442
443
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 15 times.
47 if (tile_num == tile_group->tg_end) {
444 32 s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
445 32 s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
446 32 s->tile_group_info[tile_num].tile_row = tile_row;
447 32 s->tile_group_info[tile_num].tile_column = tile_col;
448 32 return 0;
449 }
450 15 size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (bytestream2_get_bytes_left(&gb) < size_bytes)
452 return AVERROR_INVALIDDATA;
453 15 size = 0;
454
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 15 times.
53 for (int i = 0; i < size_bytes; i++)
455 38 size |= bytestream2_get_byteu(&gb) << 8 * i;
456
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (bytestream2_get_bytes_left(&gb) <= size)
457 return AVERROR_INVALIDDATA;
458 15 size++;
459
460 15 s->tile_group_info[tile_num].tile_size = size;
461 15 s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
462 15 s->tile_group_info[tile_num].tile_row = tile_row;
463 15 s->tile_group_info[tile_num].tile_column = tile_col;
464
465 15 bytestream2_skipu(&gb, size);
466 }
467
468 return 0;
469
470 }
471
472 28 static enum AVPixelFormat get_sw_pixel_format(void *logctx,
473 const AV1RawSequenceHeader *seq)
474 {
475 int bit_depth;
476 28 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
477
478
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28 if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
479 bit_depth = seq->color_config.twelve_bit ? 12 : 10;
480
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 else if (seq->seq_profile <= 2)
481
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 27 times.
28 bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
482 else {
483 av_log(logctx, AV_LOG_ERROR,
484 "Unknown AV1 profile %d.\n", seq->seq_profile);
485 return AV_PIX_FMT_NONE;
486 }
487
488
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (!seq->color_config.mono_chrome) {
489 // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (seq->color_config.subsampling_x == 0 &&
491 seq->color_config.subsampling_y == 0) {
492 if (bit_depth == 8)
493 pix_fmt = AV_PIX_FMT_YUV444P;
494 else if (bit_depth == 10)
495 pix_fmt = AV_PIX_FMT_YUV444P10;
496 else if (bit_depth == 12)
497 pix_fmt = AV_PIX_FMT_YUV444P12;
498 else
499 av_assert0(0);
500
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 } else if (seq->color_config.subsampling_x == 1 &&
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 seq->color_config.subsampling_y == 0) {
502 if (bit_depth == 8)
503 pix_fmt = AV_PIX_FMT_YUV422P;
504 else if (bit_depth == 10)
505 pix_fmt = AV_PIX_FMT_YUV422P10;
506 else if (bit_depth == 12)
507 pix_fmt = AV_PIX_FMT_YUV422P12;
508 else
509 av_assert0(0);
510
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 } else if (seq->color_config.subsampling_x == 1 &&
511
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 seq->color_config.subsampling_y == 1) {
512
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 times.
28 if (bit_depth == 8)
513 27 pix_fmt = AV_PIX_FMT_YUV420P;
514
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (bit_depth == 10)
515 1 pix_fmt = AV_PIX_FMT_YUV420P10;
516 else if (bit_depth == 12)
517 pix_fmt = AV_PIX_FMT_YUV420P12;
518 else
519 av_assert0(0);
520 }
521 } else {
522 if (bit_depth == 8)
523 pix_fmt = AV_PIX_FMT_GRAY8;
524 else if (bit_depth == 10)
525 pix_fmt = AV_PIX_FMT_GRAY10;
526 else if (bit_depth == 12)
527 pix_fmt = AV_PIX_FMT_GRAY12;
528 else
529 av_assert0(0);
530 }
531
532 28 return pix_fmt;
533 }
534
535 static int get_pixel_format(AVCodecContext *avctx)
536 {
537 AV1DecContext *s = avctx->priv_data;
538 const AV1RawSequenceHeader *seq = s->raw_seq;
539 int ret;
540 enum AVPixelFormat pix_fmt = get_sw_pixel_format(avctx, seq);
541 #define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
542 CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
543 CONFIG_AV1_D3D12VA_HWACCEL + \
544 CONFIG_AV1_NVDEC_HWACCEL + \
545 CONFIG_AV1_VAAPI_HWACCEL + \
546 CONFIG_AV1_VDPAU_HWACCEL + \
547 CONFIG_AV1_VIDEOTOOLBOX_HWACCEL + \
548 CONFIG_AV1_VULKAN_HWACCEL)
549 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
550
551 if (pix_fmt == AV_PIX_FMT_NONE)
552 return -1;
553
554 switch (pix_fmt) {
555 case AV_PIX_FMT_YUV420P:
556 #if CONFIG_AV1_DXVA2_HWACCEL
557 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
558 #endif
559 #if CONFIG_AV1_D3D11VA_HWACCEL
560 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
561 *fmtp++ = AV_PIX_FMT_D3D11;
562 #endif
563 #if CONFIG_AV1_D3D12VA_HWACCEL
564 *fmtp++ = AV_PIX_FMT_D3D12;
565 #endif
566 #if CONFIG_AV1_NVDEC_HWACCEL
567 *fmtp++ = AV_PIX_FMT_CUDA;
568 #endif
569 #if CONFIG_AV1_VAAPI_HWACCEL
570 *fmtp++ = AV_PIX_FMT_VAAPI;
571 #endif
572 #if CONFIG_AV1_VDPAU_HWACCEL
573 *fmtp++ = AV_PIX_FMT_VDPAU;
574 #endif
575 #if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL
576 *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX;
577 #endif
578 #if CONFIG_AV1_VULKAN_HWACCEL
579 *fmtp++ = AV_PIX_FMT_VULKAN;
580 #endif
581 break;
582 case AV_PIX_FMT_YUV420P10:
583 #if CONFIG_AV1_DXVA2_HWACCEL
584 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
585 #endif
586 #if CONFIG_AV1_D3D11VA_HWACCEL
587 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
588 *fmtp++ = AV_PIX_FMT_D3D11;
589 #endif
590 #if CONFIG_AV1_D3D12VA_HWACCEL
591 *fmtp++ = AV_PIX_FMT_D3D12;
592 #endif
593 #if CONFIG_AV1_NVDEC_HWACCEL
594 *fmtp++ = AV_PIX_FMT_CUDA;
595 #endif
596 #if CONFIG_AV1_VAAPI_HWACCEL
597 *fmtp++ = AV_PIX_FMT_VAAPI;
598 #endif
599 #if CONFIG_AV1_VDPAU_HWACCEL
600 *fmtp++ = AV_PIX_FMT_VDPAU;
601 #endif
602 #if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL
603 *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX;
604 #endif
605 #if CONFIG_AV1_VULKAN_HWACCEL
606 *fmtp++ = AV_PIX_FMT_VULKAN;
607 #endif
608 break;
609 case AV_PIX_FMT_YUV420P12:
610 #if CONFIG_AV1_VULKAN_HWACCEL
611 *fmtp++ = AV_PIX_FMT_VULKAN;
612 #endif
613 break;
614 case AV_PIX_FMT_YUV422P:
615 #if CONFIG_AV1_VULKAN_HWACCEL
616 *fmtp++ = AV_PIX_FMT_VULKAN;
617 #endif
618 break;
619 case AV_PIX_FMT_YUV422P10:
620 #if CONFIG_AV1_VULKAN_HWACCEL
621 *fmtp++ = AV_PIX_FMT_VULKAN;
622 #endif
623 break;
624 case AV_PIX_FMT_YUV422P12:
625 #if CONFIG_AV1_VULKAN_HWACCEL
626 *fmtp++ = AV_PIX_FMT_VULKAN;
627 #endif
628 break;
629 case AV_PIX_FMT_YUV444P:
630 #if CONFIG_AV1_VULKAN_HWACCEL
631 *fmtp++ = AV_PIX_FMT_VULKAN;
632 #endif
633 break;
634 case AV_PIX_FMT_YUV444P10:
635 #if CONFIG_AV1_VULKAN_HWACCEL
636 *fmtp++ = AV_PIX_FMT_VULKAN;
637 #endif
638 break;
639 case AV_PIX_FMT_YUV444P12:
640 #if CONFIG_AV1_VULKAN_HWACCEL
641 *fmtp++ = AV_PIX_FMT_VULKAN;
642 #endif
643 break;
644 case AV_PIX_FMT_GRAY8:
645 #if CONFIG_AV1_NVDEC_HWACCEL
646 *fmtp++ = AV_PIX_FMT_CUDA;
647 #endif
648 break;
649 case AV_PIX_FMT_GRAY10:
650 #if CONFIG_AV1_NVDEC_HWACCEL
651 *fmtp++ = AV_PIX_FMT_CUDA;
652 #endif
653 break;
654 }
655
656 *fmtp++ = pix_fmt;
657 *fmtp = AV_PIX_FMT_NONE;
658
659 for (int i = 0; pix_fmts[i] != pix_fmt; i++)
660 if (pix_fmts[i] == avctx->pix_fmt) {
661 s->pix_fmt = pix_fmt;
662 return 1;
663 }
664
665 ret = ff_get_format(avctx, pix_fmts);
666
667 /**
668 * check if the HW accel is inited correctly. If not, return un-implemented.
669 * Since now the av1 decoder doesn't support native decode, if it will be
670 * implemented in the future, need remove this check.
671 */
672 if (!avctx->hwaccel) {
673 av_log(avctx, AV_LOG_ERROR, "Your platform doesn't support"
674 " hardware accelerated AV1 decoding.\n");
675 avctx->pix_fmt = AV_PIX_FMT_NONE;
676 return AVERROR(ENOSYS);
677 }
678
679 s->pix_fmt = pix_fmt;
680 avctx->pix_fmt = ret;
681
682 av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
683 av_get_pix_fmt_name(avctx->pix_fmt));
684
685 return 0;
686 }
687
688 285 static void av1_frame_unref(AV1Frame *f)
689 {
690 285 ff_progress_frame_unref(&f->pf);
691 285 av_refstruct_unref(&f->hwaccel_picture_private);
692 285 av_refstruct_unref(&f->header_ref);
693 285 f->raw_frame_header = NULL;
694 285 f->spatial_id = f->temporal_id = 0;
695 285 memset(f->skip_mode_frame_idx, 0,
696 2 * sizeof(uint8_t));
697 285 memset(&f->film_grain, 0, sizeof(f->film_grain));
698 285 f->coded_lossless = 0;
699 285 }
700
701 178 static void av1_frame_replace(AV1Frame *dst, const AV1Frame *src)
702 {
703 av_assert1(dst != src);
704
705 178 av_refstruct_replace(&dst->header_ref, src->header_ref);
706
707 178 dst->raw_frame_header = src->raw_frame_header;
708
709 178 ff_progress_frame_replace(&dst->pf, &src->pf);
710
711 178 av_refstruct_replace(&dst->hwaccel_picture_private,
712 178 src->hwaccel_picture_private);
713
714 178 dst->spatial_id = src->spatial_id;
715 178 dst->temporal_id = src->temporal_id;
716 178 memcpy(dst->gm_invalid,
717 178 src->gm_invalid,
718 AV1_NUM_REF_FRAMES * sizeof(uint8_t));
719 178 memcpy(dst->gm_type,
720 178 src->gm_type,
721 AV1_NUM_REF_FRAMES * sizeof(uint8_t));
722 178 memcpy(dst->gm_params,
723 178 src->gm_params,
724 AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
725 178 memcpy(dst->skip_mode_frame_idx,
726 178 src->skip_mode_frame_idx,
727 2 * sizeof(uint8_t));
728 178 memcpy(&dst->film_grain,
729 178 &src->film_grain,
730 sizeof(dst->film_grain));
731 178 dst->coded_lossless = src->coded_lossless;
732
733 178 dst->order_hint = src->order_hint;
734 178 memcpy(dst->ref_frame_sign_bias, src->ref_frame_sign_bias,
735 sizeof(dst->ref_frame_sign_bias));
736 178 memcpy(dst->order_hints, src->order_hints,
737 sizeof(dst->order_hints));
738
739 178 dst->force_integer_mv = src->force_integer_mv;
740 178 }
741
742 29 static av_cold int av1_decode_free(AVCodecContext *avctx)
743 {
744 29 AV1DecContext *s = avctx->priv_data;
745 AV1RawMetadataITUTT35 itut_t35;
746
747
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 29 times.
261 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
748 232 av1_frame_unref(&s->ref[i]);
749 29 av1_frame_unref(&s->cur_frame);
750 29 av_buffer_unref(&s->seq_data_ref);
751 29 av_refstruct_unref(&s->seq_ref);
752 29 av_refstruct_unref(&s->header_ref);
753 29 av_refstruct_unref(&s->cll_ref);
754 29 av_refstruct_unref(&s->mdcv_ref);
755 29 av_freep(&s->tile_group_info);
756
757
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
29 while (s->itut_t35_fifo && av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0)
758 av_buffer_unref(&itut_t35.payload_ref);
759 29 av_fifo_freep2(&s->itut_t35_fifo);
760
761 29 ff_cbs_fragment_free(&s->current_obu);
762 29 ff_cbs_close(&s->cbc);
763 29 ff_dovi_ctx_unref(&s->dovi);
764
765 29 return 0;
766 }
767
768 29 static int set_context_with_sequence(AVCodecContext *avctx,
769 const AV1RawSequenceHeader *seq)
770 {
771 29 int width = seq->max_frame_width_minus_1 + 1;
772 29 int height = seq->max_frame_height_minus_1 + 1;
773
774 29 avctx->profile = seq->seq_profile;
775 29 avctx->level = seq->seq_level_idx[0];
776
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (seq->color_config.color_description_present_flag) {
778 avctx->color_range =
779 seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
780 avctx->color_primaries = seq->color_config.color_primaries;
781 avctx->colorspace = seq->color_config.matrix_coefficients;
782 avctx->color_trc = seq->color_config.transfer_characteristics;
783 }
784
785
2/3
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
29 switch (seq->color_config.chroma_sample_position) {
786 4 case AV1_CSP_VERTICAL:
787 4 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
788 4 break;
789 case AV1_CSP_COLOCATED:
790 avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
791 break;
792 }
793
794 #if FF_API_CODEC_PROPS
795 FF_DISABLE_DEPRECATION_WARNINGS
796
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 27 times.
29 if (seq->film_grain_params_present)
797 2 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
798 else
799 27 avctx->properties &= ~FF_CODEC_PROPERTY_FILM_GRAIN;
800 FF_ENABLE_DEPRECATION_WARNINGS
801 #endif
802
803
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
29 if (avctx->width != width || avctx->height != height) {
804 4 int ret = ff_set_dimensions(avctx, width, height);
805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
806 return ret;
807 }
808
809
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 25 times.
29 if (seq->timing_info_present_flag)
810 4 avctx->framerate = ff_av1_framerate(1LL + seq->timing_info.num_ticks_per_picture_minus_1,
811 4 seq->timing_info.num_units_in_display_tick,
812 4 seq->timing_info.time_scale);
813
814
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1 times.
29 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
815 28 avctx->pix_fmt = get_sw_pixel_format(avctx, seq);
816
817 29 return 0;
818 }
819
820 static int update_context_with_frame_header(AVCodecContext *avctx,
821 const AV1RawFrameHeader *header)
822 {
823 AVRational aspect_ratio;
824 int width = header->frame_width_minus_1 + 1;
825 int height = header->frame_height_minus_1 + 1;
826 int r_width = header->render_width_minus_1 + 1;
827 int r_height = header->render_height_minus_1 + 1;
828 int ret;
829
830 if (avctx->width != width || avctx->height != height) {
831 ret = ff_set_dimensions(avctx, width, height);
832 if (ret < 0)
833 return ret;
834 }
835
836 av_reduce(&aspect_ratio.num, &aspect_ratio.den,
837 (int64_t)height * r_width,
838 (int64_t)width * r_height,
839 INT_MAX);
840
841 if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
842 ret = ff_set_sar(avctx, aspect_ratio);
843 if (ret < 0)
844 return ret;
845 }
846
847 return 0;
848 }
849
850 static const CodedBitstreamUnitType decompose_unit_types[] = {
851 AV1_OBU_FRAME,
852 AV1_OBU_FRAME_HEADER,
853 AV1_OBU_METADATA,
854 AV1_OBU_REDUNDANT_FRAME_HEADER,
855 AV1_OBU_SEQUENCE_HEADER,
856 AV1_OBU_TEMPORAL_DELIMITER,
857 AV1_OBU_TILE_GROUP,
858 };
859
860 29 static av_cold int av1_decode_init(AVCodecContext *avctx)
861 {
862 29 AV1DecContext *s = avctx->priv_data;
863 AV1RawSequenceHeader *seq;
864 const AVPacketSideData *sd;
865 int ret;
866
867 29 s->avctx = avctx;
868 29 s->pkt = avctx->internal->in_pkt;
869 29 s->pix_fmt = AV_PIX_FMT_NONE;
870
871 29 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (ret < 0)
873 return ret;
874
875 29 s->cbc->decompose_unit_types = decompose_unit_types;
876 29 s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
877
878 29 s->itut_t35_fifo = av_fifo_alloc2(1, sizeof(AV1RawMetadataITUTT35),
879 AV_FIFO_FLAG_AUTO_GROW);
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!s->itut_t35_fifo)
881 return AVERROR(ENOMEM);
882
883 29 av_opt_set_int(s->cbc->priv_data, "operating_point", s->operating_point, 0);
884
885
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
29 if (avctx->extradata && avctx->extradata_size) {
886 9 ret = ff_cbs_read_extradata_from_codec(s->cbc,
887 &s->current_obu,
888 avctx);
889
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0) {
890 av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
891 goto end;
892 }
893
894 9 seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
895
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (!seq) {
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->extradata[0] & 0x80))
897 av_log(avctx, AV_LOG_WARNING, "No sequence header available in extradata.\n");
898 2 goto end;
899 }
900
901 7 ret = set_context_with_sequence(avctx, seq);
902
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ret < 0) {
903 av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
904 goto end;
905 }
906
907 7 end:
908 9 ff_cbs_fragment_reset(&s->current_obu);
909 }
910
911 29 s->dovi.logctx = avctx;
912 29 s->dovi.cfg.dv_profile = 10; // default for AV1
913 29 sd = ff_get_coded_side_data(avctx, AV_PKT_DATA_DOVI_CONF);
914
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29 if (sd && sd->size >= sizeof(s->dovi.cfg))
915 s->dovi.cfg = *(AVDOVIDecoderConfigurationRecord *) sd->data;
916
917 29 return ret;
918 }
919
920 static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
921 {
922 AV1DecContext *s = avctx->priv_data;
923 AV1RawFrameHeader *header= s->raw_frame_header;
924 AVFrame *frame;
925 int ret;
926
927 ret = update_context_with_frame_header(avctx, header);
928 if (ret < 0) {
929 av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
930 return ret;
931 }
932
933 ret = ff_progress_frame_get_buffer(avctx, &f->pf, AV_GET_BUFFER_FLAG_REF);
934 if (ret < 0)
935 goto fail;
936
937 frame = f->f;
938 if (header->frame_type == AV1_FRAME_KEY)
939 frame->flags |= AV_FRAME_FLAG_KEY;
940 else
941 frame->flags &= ~AV_FRAME_FLAG_KEY;
942
943 switch (header->frame_type) {
944 case AV1_FRAME_KEY:
945 case AV1_FRAME_INTRA_ONLY:
946 frame->pict_type = AV_PICTURE_TYPE_I;
947 break;
948 case AV1_FRAME_INTER:
949 frame->pict_type = AV_PICTURE_TYPE_P;
950 break;
951 case AV1_FRAME_SWITCH:
952 frame->pict_type = AV_PICTURE_TYPE_SP;
953 break;
954 }
955
956 ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
957 if (ret < 0)
958 goto fail;
959
960 return 0;
961
962 fail:
963 av1_frame_unref(f);
964 return ret;
965 }
966
967 static int export_itut_t35(AVCodecContext *avctx, AVFrame *frame,
968 const AV1RawMetadataITUTT35 *itut_t35)
969 {
970 GetByteContext gb;
971 AV1DecContext *s = avctx->priv_data;
972 int ret, provider_code, country_code;
973
974 bytestream2_init(&gb, itut_t35->payload, itut_t35->payload_size);
975
976 country_code = itut_t35->itu_t_t35_country_code ;
977 switch (country_code) {
978 case ITU_T_T35_COUNTRY_CODE_US:
979 provider_code = bytestream2_get_be16(&gb);
980
981 switch (provider_code) {
982 case ITU_T_T35_PROVIDER_CODE_ATSC: {
983 uint32_t user_identifier = bytestream2_get_be32(&gb);
984 switch (user_identifier) {
985 case MKBETAG('G', 'A', '9', '4'): { // closed captions
986 AVBufferRef *buf = NULL;
987
988 ret = ff_parse_a53_cc(&buf, gb.buffer, bytestream2_get_bytes_left(&gb));
989 if (ret < 0)
990 return ret;
991 if (!ret)
992 break;
993
994 ret = ff_frame_new_side_data_from_buf(avctx, frame, AV_FRAME_DATA_A53_CC, &buf);
995 if (ret < 0)
996 return ret;
997
998 #if FF_API_CODEC_PROPS
999 FF_DISABLE_DEPRECATION_WARNINGS
1000 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
1001 FF_ENABLE_DEPRECATION_WARNINGS
1002 #endif
1003 break;
1004 }
1005 default: // ignore unsupported identifiers
1006 break;
1007 }
1008 break;
1009 }
1010 case ITU_T_T35_PROVIDER_CODE_SAMSUNG: {
1011 AVDynamicHDRPlus *hdrplus;
1012 int provider_oriented_code = bytestream2_get_be16(&gb);
1013 int application_identifier = bytestream2_get_byte(&gb);
1014
1015 if (provider_oriented_code != 1 || application_identifier != 4)
1016 return 0; // ignore
1017
1018 hdrplus = av_dynamic_hdr_plus_create_side_data(frame);
1019 if (!hdrplus)
1020 return AVERROR(ENOMEM);
1021
1022 ret = av_dynamic_hdr_plus_from_t35(hdrplus, gb.buffer,
1023 bytestream2_get_bytes_left(&gb));
1024 if (ret < 0)
1025 return ret;
1026 break;
1027 }
1028 case ITU_T_T35_PROVIDER_CODE_DOLBY: {
1029 int provider_oriented_code = bytestream2_get_be32(&gb);
1030 if (provider_oriented_code != 0x800)
1031 return 0; // ignore
1032
1033 ret = ff_dovi_rpu_parse(&s->dovi, gb.buffer, bytestream2_get_bytes_left(&gb),
1034 avctx->err_recognition);
1035 if (ret < 0) {
1036 av_log(avctx, AV_LOG_WARNING, "Error parsing DOVI OBU.\n");
1037 return 0; // ignore
1038 }
1039
1040 ret = ff_dovi_attach_side_data(&s->dovi, frame);
1041 if (ret < 0)
1042 return ret;
1043 break;
1044 }
1045 default:
1046 break;
1047 }
1048 break;
1049 case ITU_T_T35_COUNTRY_CODE_UK:
1050 bytestream2_skip(&gb, 1); // t35_uk_country_code_second_octet
1051
1052 provider_code = bytestream2_get_be16(&gb);
1053 switch (provider_code) {
1054 case ITU_T_T35_PROVIDER_CODE_VNOVA: {
1055 AVFrameSideData *sd;
1056 if (bytestream2_get_bytes_left(&gb) < 2)
1057 return AVERROR_INVALIDDATA;
1058
1059 ret = ff_frame_new_side_data(avctx, frame, AV_FRAME_DATA_LCEVC,
1060 bytestream2_get_bytes_left(&gb), &sd);
1061 if (ret < 0)
1062 return ret;
1063 if (!sd)
1064 break;
1065
1066 bytestream2_get_bufferu(&gb, sd->data, sd->size);
1067 break;
1068 }
1069 default:
1070 break;
1071 }
1072 break;
1073 default:
1074 // ignore unsupported provider codes
1075 break;
1076 }
1077
1078 return 0;
1079 }
1080
1081 static int export_metadata(AVCodecContext *avctx, AVFrame *frame)
1082 {
1083 AV1DecContext *s = avctx->priv_data;
1084 AV1RawMetadataITUTT35 itut_t35;
1085 int ret = 0;
1086
1087 if (s->mdcv) {
1088 AVMasteringDisplayMetadata *mastering;
1089
1090 ret = ff_decode_mastering_display_new(avctx, frame, &mastering);
1091 if (ret < 0)
1092 return ret;
1093
1094 if (mastering) {
1095 for (int i = 0; i < 3; i++) {
1096 mastering->display_primaries[i][0] = av_make_q(s->mdcv->primary_chromaticity_x[i], 1 << 16);
1097 mastering->display_primaries[i][1] = av_make_q(s->mdcv->primary_chromaticity_y[i], 1 << 16);
1098 }
1099 mastering->white_point[0] = av_make_q(s->mdcv->white_point_chromaticity_x, 1 << 16);
1100 mastering->white_point[1] = av_make_q(s->mdcv->white_point_chromaticity_y, 1 << 16);
1101
1102 mastering->max_luminance = av_make_q(s->mdcv->luminance_max, 1 << 8);
1103 mastering->min_luminance = av_make_q(s->mdcv->luminance_min, 1 << 14);
1104
1105 mastering->has_primaries = 1;
1106 mastering->has_luminance = 1;
1107 }
1108 }
1109
1110 if (s->cll) {
1111 AVContentLightMetadata *light;
1112
1113 ret = ff_decode_content_light_new(avctx, frame, &light);
1114 if (ret < 0)
1115 return ret;
1116
1117 if (light) {
1118 light->MaxCLL = s->cll->max_cll;
1119 light->MaxFALL = s->cll->max_fall;
1120 }
1121 }
1122
1123 while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0) {
1124 if (ret >= 0)
1125 ret = export_itut_t35(avctx, frame, &itut_t35);
1126 av_buffer_unref(&itut_t35.payload_ref);
1127 }
1128
1129 return ret;
1130 }
1131
1132 static int export_film_grain(AVCodecContext *avctx, AVFrame *frame)
1133 {
1134 AV1DecContext *s = avctx->priv_data;
1135 const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain;
1136 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(frame->format);
1137 AVFilmGrainParams *fgp;
1138 AVFilmGrainAOMParams *aom;
1139
1140 av_assert0(pixdesc);
1141 if (!film_grain->apply_grain)
1142 return 0;
1143
1144 fgp = av_film_grain_params_create_side_data(frame);
1145 if (!fgp)
1146 return AVERROR(ENOMEM);
1147
1148 fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
1149 fgp->seed = film_grain->grain_seed;
1150 fgp->width = frame->width;
1151 fgp->height = frame->height;
1152 fgp->color_range = frame->color_range;
1153 fgp->color_primaries = frame->color_primaries;
1154 fgp->color_trc = frame->color_trc;
1155 fgp->color_space = frame->colorspace;
1156 fgp->subsampling_x = pixdesc->log2_chroma_w;
1157 fgp->subsampling_y = pixdesc->log2_chroma_h;
1158
1159 aom = &fgp->codec.aom;
1160 aom->chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma;
1161 aom->scaling_shift = film_grain->grain_scaling_minus_8 + 8;
1162 aom->ar_coeff_lag = film_grain->ar_coeff_lag;
1163 aom->ar_coeff_shift = film_grain->ar_coeff_shift_minus_6 + 6;
1164 aom->grain_scale_shift = film_grain->grain_scale_shift;
1165 aom->overlap_flag = film_grain->overlap_flag;
1166 aom->limit_output_range = film_grain->clip_to_restricted_range;
1167
1168 aom->num_y_points = film_grain->num_y_points;
1169 for (int i = 0; i < film_grain->num_y_points; i++) {
1170 aom->y_points[i][0] = film_grain->point_y_value[i];
1171 aom->y_points[i][1] = film_grain->point_y_scaling[i];
1172 }
1173 aom->num_uv_points[0] = film_grain->num_cb_points;
1174 for (int i = 0; i < film_grain->num_cb_points; i++) {
1175 aom->uv_points[0][i][0] = film_grain->point_cb_value[i];
1176 aom->uv_points[0][i][1] = film_grain->point_cb_scaling[i];
1177 }
1178 aom->num_uv_points[1] = film_grain->num_cr_points;
1179 for (int i = 0; i < film_grain->num_cr_points; i++) {
1180 aom->uv_points[1][i][0] = film_grain->point_cr_value[i];
1181 aom->uv_points[1][i][1] = film_grain->point_cr_scaling[i];
1182 }
1183
1184 for (int i = 0; i < 24; i++) {
1185 aom->ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i] - 128;
1186 }
1187 for (int i = 0; i < 25; i++) {
1188 aom->ar_coeffs_uv[0][i] = film_grain->ar_coeffs_cb_plus_128[i] - 128;
1189 aom->ar_coeffs_uv[1][i] = film_grain->ar_coeffs_cr_plus_128[i] - 128;
1190 }
1191
1192 aom->uv_mult[0] = film_grain->cb_mult;
1193 aom->uv_mult[1] = film_grain->cr_mult;
1194 aom->uv_mult_luma[0] = film_grain->cb_luma_mult;
1195 aom->uv_mult_luma[1] = film_grain->cr_luma_mult;
1196 aom->uv_offset[0] = film_grain->cb_offset;
1197 aom->uv_offset[1] = film_grain->cr_offset;
1198
1199 return 0;
1200 }
1201
1202 static int set_output_frame(AVCodecContext *avctx, AVFrame *frame)
1203 {
1204 AV1DecContext *s = avctx->priv_data;
1205 const AVFrame *srcframe = s->cur_frame.f;
1206 AVPacket *pkt = s->pkt;
1207 int ret;
1208
1209 // TODO: all layers
1210 if (s->operating_point_idc &&
1211 av_log2(s->operating_point_idc >> 8) > s->cur_frame.spatial_id)
1212 return 0;
1213
1214 ret = av_frame_ref(frame, srcframe);
1215 if (ret < 0)
1216 return ret;
1217
1218 ret = export_metadata(avctx, frame);
1219 if (ret < 0) {
1220 av_frame_unref(frame);
1221 return ret;
1222 }
1223
1224 if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
1225 ret = export_film_grain(avctx, frame);
1226 if (ret < 0) {
1227 av_frame_unref(frame);
1228 return ret;
1229 }
1230 }
1231
1232 frame->pts = pkt->pts;
1233 frame->pkt_dts = pkt->dts;
1234
1235 av_packet_unref(pkt);
1236
1237 return 0;
1238 }
1239
1240 24 static void update_reference_list(AVCodecContext *avctx)
1241 {
1242 24 AV1DecContext *s = avctx->priv_data;
1243 24 const AV1RawFrameHeader *header = s->raw_frame_header;
1244
1245
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 24 times.
216 for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1246
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 14 times.
192 if (header->refresh_frame_flags & (1 << i))
1247 178 av1_frame_replace(&s->ref[i], &s->cur_frame);
1248 }
1249 24 }
1250
1251 24 static int get_current_frame(AVCodecContext *avctx)
1252 {
1253 24 AV1DecContext *s = avctx->priv_data;
1254 int ret;
1255
1256 24 av1_frame_unref(&s->cur_frame);
1257
1258 24 s->cur_frame.header_ref = av_refstruct_ref(s->header_ref);
1259
1260 24 s->cur_frame.raw_frame_header = s->raw_frame_header;
1261
1262 24 ret = init_tile_data(s);
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0) {
1264 av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
1265 return ret;
1266 }
1267
1268
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if ((avctx->skip_frame >= AVDISCARD_NONINTRA &&
1269
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22 times.
24 (s->raw_frame_header->frame_type != AV1_FRAME_KEY &&
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 s->raw_frame_header->frame_type != AV1_FRAME_INTRA_ONLY)) ||
1271
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 (avctx->skip_frame >= AVDISCARD_NONKEY &&
1272
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 s->raw_frame_header->frame_type != AV1_FRAME_KEY) ||
1273
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 avctx->skip_frame >= AVDISCARD_ALL)
1274 24 return 0;
1275
1276 if (s->pix_fmt == AV_PIX_FMT_NONE) {
1277 ret = get_pixel_format(avctx);
1278 if (ret < 0) {
1279 av_log(avctx, AV_LOG_ERROR, "Failed to get pixel format.\n");
1280 return ret;
1281 }
1282
1283 if (!ret && FF_HW_HAS_CB(avctx, decode_params)) {
1284 ret = FF_HW_CALL(avctx, decode_params, AV1_OBU_SEQUENCE_HEADER,
1285 s->seq_data_ref->data, s->seq_data_ref->size);
1286 if (ret < 0) {
1287 av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
1288 return ret;
1289 }
1290 }
1291 }
1292
1293 ret = av1_frame_alloc(avctx, &s->cur_frame);
1294 if (ret < 0) {
1295 av_log(avctx, AV_LOG_ERROR,
1296 "Failed to allocate space for current frame.\n");
1297 return ret;
1298 }
1299
1300 global_motion_params(s);
1301 skip_mode_params(s);
1302 coded_lossless_param(s);
1303 order_hint_info(s);
1304 load_grain_params(s);
1305
1306 s->cur_frame.force_integer_mv =
1307 s->raw_frame_header->force_integer_mv ||
1308 s->raw_frame_header->frame_type == AV1_FRAME_KEY ||
1309 s->raw_frame_header->frame_type == AV1_FRAME_INTRA_ONLY;
1310
1311 return ret;
1312 }
1313
1314 24 static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
1315 {
1316 24 AV1DecContext *s = avctx->priv_data;
1317 24 AV1RawTileGroup *raw_tile_group = NULL;
1318 24 int i = 0, ret;
1319
1320
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 for (i = s->nb_unit; i < s->current_obu.nb_units; i++) {
1321 83 CodedBitstreamUnit *unit = &s->current_obu.units[i];
1322 83 AV1RawOBU *obu = unit->content;
1323 const AV1RawOBUHeader *header;
1324
1325 83 av_log(avctx, AV_LOG_DEBUG, "OBU idx:%d, type:%d, content available:%d.\n", i, unit->type, !!obu);
1326
1327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (unit->type == AV1_OBU_TILE_LIST) {
1328 av_log(avctx, AV_LOG_ERROR, "Large scale tile decoding is unsupported.\n");
1329 ret = AVERROR_PATCHWELCOME;
1330 goto end;
1331 }
1332
1333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!obu)
1334 continue;
1335
1336 83 header = &obu->header;
1337
1338
5/8
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
83 switch (unit->type) {
1339 22 case AV1_OBU_SEQUENCE_HEADER:
1340 22 ret = av_buffer_replace(&s->seq_data_ref, unit->data_ref);
1341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
1342 goto end;
1343
1344 22 s->seq_data_ref->data = unit->data;
1345 22 s->seq_data_ref->size = unit->data_size;
1346 22 av_refstruct_replace(&s->seq_ref, unit->content_ref);
1347
1348 22 s->raw_seq = &obu->obu.sequence_header;
1349 22 s->raw_frame_header = NULL;
1350 22 raw_tile_group = NULL;
1351
1352 22 ret = set_context_with_sequence(avctx, s->raw_seq);
1353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0) {
1354 av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
1355 s->raw_seq = NULL;
1356 goto end;
1357 }
1358
1359 22 s->operating_point_idc = s->raw_seq->operating_point_idc[s->operating_point];
1360
1361 22 s->pix_fmt = AV_PIX_FMT_NONE;
1362
1363
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
22 if (FF_HW_HAS_CB(avctx, decode_params)) {
1364 ret = FF_HW_CALL(avctx, decode_params, AV1_OBU_SEQUENCE_HEADER,
1365 s->seq_data_ref->data, s->seq_data_ref->size);
1366 if (ret < 0) {
1367 av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
1368 return ret;
1369 }
1370 }
1371
1372 22 break;
1373 case AV1_OBU_REDUNDANT_FRAME_HEADER:
1374 if (s->raw_frame_header)
1375 break;
1376 // fall-through
1377 case AV1_OBU_FRAME:
1378 case AV1_OBU_FRAME_HEADER:
1379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!s->raw_seq) {
1380 av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
1381 ret = AVERROR_INVALIDDATA;
1382 goto end;
1383 }
1384
1385 24 av_refstruct_replace(&s->header_ref, unit->content_ref);
1386
1387 24 raw_tile_group = NULL;
1388
1389
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 5 times.
24 if (unit->type == AV1_OBU_FRAME)
1390 19 s->raw_frame_header = &obu->obu.frame.header;
1391 else
1392 5 s->raw_frame_header = &obu->obu.frame_header;
1393
1394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->raw_frame_header->show_existing_frame) {
1395 av1_frame_replace(&s->cur_frame,
1396 &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
1397
1398 update_reference_list(avctx);
1399
1400 if (s->cur_frame.f) {
1401 ret = set_output_frame(avctx, frame);
1402 if (ret < 0) {
1403 av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
1404 goto end;
1405 }
1406 }
1407
1408 s->raw_frame_header = NULL;
1409 i++;
1410 ret = 0;
1411
1412 goto end;
1413 }
1414
1415 24 ret = get_current_frame(avctx);
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0) {
1417 av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
1418 goto end;
1419 }
1420
1421 24 s->cur_frame.spatial_id = header->spatial_id;
1422 24 s->cur_frame.temporal_id = header->temporal_id;
1423
1424
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 if (avctx->hwaccel && s->cur_frame.f) {
1425 ret = FF_HW_CALL(avctx, start_frame, s->pkt->buf,
1426 unit->data, unit->data_size);
1427 if (ret < 0) {
1428 av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
1429 goto end;
1430 }
1431 }
1432
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 19 times.
24 if (unit->type != AV1_OBU_FRAME)
1433 5 break;
1434 // fall-through
1435 case AV1_OBU_TILE_GROUP:
1436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!s->raw_frame_header) {
1437 av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
1438 ret = AVERROR_INVALIDDATA;
1439 goto end;
1440 }
1441
1442
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 13 times.
32 if (unit->type == AV1_OBU_FRAME)
1443 19 raw_tile_group = &obu->obu.frame.tile_group;
1444 else
1445 13 raw_tile_group = &obu->obu.tile_group;
1446
1447 32 ret = get_tiles_info(avctx, raw_tile_group);
1448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
1449 goto end;
1450
1451
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
32 if (avctx->hwaccel && s->cur_frame.f) {
1452 ret = FF_HW_CALL(avctx, decode_slice, raw_tile_group->tile_data.data,
1453 raw_tile_group->tile_data.data_size);
1454 if (ret < 0) {
1455 av_log(avctx, AV_LOG_ERROR,
1456 "HW accel decode slice fail.\n");
1457 goto end;
1458 }
1459 }
1460 32 break;
1461 20 case AV1_OBU_TEMPORAL_DELIMITER:
1462 20 s->raw_frame_header = NULL;
1463 20 raw_tile_group = NULL;
1464 // fall-through
1465 20 case AV1_OBU_TILE_LIST:
1466 case AV1_OBU_PADDING:
1467 20 break;
1468 4 case AV1_OBU_METADATA:
1469
2/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 switch (obu->obu.metadata.metadata_type) {
1470 2 case AV1_METADATA_TYPE_HDR_CLL:
1471 2 av_refstruct_replace(&s->cll_ref, unit->content_ref);
1472 2 s->cll = &obu->obu.metadata.metadata.hdr_cll;
1473 2 break;
1474 2 case AV1_METADATA_TYPE_HDR_MDCV:
1475 2 av_refstruct_replace(&s->mdcv_ref, unit->content_ref);
1476 2 s->mdcv = &obu->obu.metadata.metadata.hdr_mdcv;
1477 2 break;
1478 case AV1_METADATA_TYPE_ITUT_T35: {
1479 AV1RawMetadataITUTT35 itut_t35;
1480 memcpy(&itut_t35, &obu->obu.metadata.metadata.itut_t35, sizeof(itut_t35));
1481 itut_t35.payload_ref = av_buffer_ref(obu->obu.metadata.metadata.itut_t35.payload_ref);
1482 if (!itut_t35.payload_ref) {
1483 ret = AVERROR(ENOMEM);
1484 goto end;
1485 }
1486 ret = av_fifo_write(s->itut_t35_fifo, &itut_t35, 1);
1487 if (ret < 0) {
1488 av_buffer_unref(&itut_t35.payload_ref);
1489 goto end;
1490 }
1491 break;
1492 }
1493 default:
1494 break;
1495 }
1496 4 break;
1497 default:
1498 av_log(avctx, AV_LOG_DEBUG,
1499 "Unknown obu type: %d (%zu bits).\n",
1500 unit->type, unit->data_size);
1501 }
1502
1503
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 8 times.
83 if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
1504 24 int show_frame = s->raw_frame_header->show_frame;
1505 // Set nb_unit to point at the next OBU, to indicate which
1506 // OBUs have been processed for this current frame. (If this
1507 // frame gets output, we set nb_unit to this value later too.)
1508 24 s->nb_unit = i + 1;
1509
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 if (avctx->hwaccel && s->cur_frame.f) {
1510 ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
1511 if (ret < 0) {
1512 av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
1513 goto end;
1514 }
1515 }
1516
1517 24 update_reference_list(avctx);
1518
1519 // Set start_unit to indicate the first OBU of the next frame.
1520 24 s->start_unit = s->nb_unit;
1521 24 raw_tile_group = NULL;
1522 24 s->raw_frame_header = NULL;
1523
1524
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (show_frame) {
1525 // cur_frame.f needn't exist due to skip_frame.
1526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->cur_frame.f) {
1527 ret = set_output_frame(avctx, frame);
1528 if (ret < 0) {
1529 av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
1530 goto end;
1531 }
1532 }
1533 24 i++;
1534 24 ret = 0;
1535 24 goto end;
1536 }
1537 }
1538 }
1539
1540 ret = AVERROR(EAGAIN);
1541 24 end:
1542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 av_assert0(i <= s->current_obu.nb_units);
1543 24 s->nb_unit = i;
1544
1545
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 2 times.
24 if ((ret < 0 && ret != AVERROR(EAGAIN)) || s->current_obu.nb_units == i) {
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
1547 s->raw_frame_header = NULL;
1548 22 av_packet_unref(s->pkt);
1549 22 ff_cbs_fragment_reset(&s->current_obu);
1550 22 s->nb_unit = s->start_unit = 0;
1551 }
1552
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 if (!ret && !frame->buf[0])
1553 24 ret = AVERROR(EAGAIN);
1554
1555 24 return ret;
1556 }
1557
1558 44 static int av1_receive_frame(AVCodecContext *avctx, AVFrame *frame)
1559 {
1560 44 AV1DecContext *s = avctx->priv_data;
1561 int ret;
1562
1563 do {
1564
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 2 times.
68 if (!s->current_obu.nb_units) {
1565 66 ret = ff_decode_get_packet(avctx, s->pkt);
1566
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 if (ret < 0)
1567 44 return ret;
1568
1569 22 ret = ff_cbs_read_packet(s->cbc, &s->current_obu, s->pkt);
1570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0) {
1571 ff_cbs_fragment_reset(&s->current_obu);
1572 av_packet_unref(s->pkt);
1573 av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
1574 return ret;
1575 }
1576
1577 22 s->nb_unit = s->start_unit = 0;
1578 22 av_log(avctx, AV_LOG_DEBUG, "Total OBUs on this packet: %d.\n",
1579 s->current_obu.nb_units);
1580 }
1581
1582 24 ret = av1_receive_frame_internal(avctx, frame);
1583
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 } while (ret == AVERROR(EAGAIN));
1584
1585 return ret;
1586 }
1587
1588 static av_cold void av1_decode_flush(AVCodecContext *avctx)
1589 {
1590 AV1DecContext *s = avctx->priv_data;
1591 AV1RawMetadataITUTT35 itut_t35;
1592
1593 for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
1594 av1_frame_unref(&s->ref[i]);
1595
1596 av1_frame_unref(&s->cur_frame);
1597 s->operating_point_idc = 0;
1598 s->nb_unit = s->start_unit = 0;
1599 s->raw_frame_header = NULL;
1600 s->raw_seq = NULL;
1601 s->cll = NULL;
1602 s->mdcv = NULL;
1603 while (av_fifo_read(s->itut_t35_fifo, &itut_t35, 1) >= 0)
1604 av_buffer_unref(&itut_t35.payload_ref);
1605
1606 ff_cbs_fragment_reset(&s->current_obu);
1607 ff_cbs_flush(s->cbc);
1608
1609 if (FF_HW_HAS_CB(avctx, flush))
1610 FF_HW_SIMPLE_CALL(avctx, flush);
1611 }
1612
1613 #define OFFSET(x) offsetof(AV1DecContext, x)
1614 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1615 static const AVOption av1_options[] = {
1616 { "operating_point", "Select an operating point of the scalable bitstream",
1617 OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, AV1_MAX_OPERATING_POINTS - 1, VD },
1618 { NULL }
1619 };
1620
1621 static const AVClass av1_class = {
1622 .class_name = "AV1 decoder",
1623 .item_name = av_default_item_name,
1624 .option = av1_options,
1625 .version = LIBAVUTIL_VERSION_INT,
1626 };
1627
1628 const FFCodec ff_av1_decoder = {
1629 .p.name = "av1",
1630 CODEC_LONG_NAME("Alliance for Open Media AV1"),
1631 .p.type = AVMEDIA_TYPE_VIDEO,
1632 .p.id = AV_CODEC_ID_AV1,
1633 .priv_data_size = sizeof(AV1DecContext),
1634 .init = av1_decode_init,
1635 .close = av1_decode_free,
1636 FF_CODEC_RECEIVE_FRAME_CB(av1_receive_frame),
1637 .p.capabilities = AV_CODEC_CAP_DR1,
1638 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1639 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
1640 FF_CODEC_CAP_USES_PROGRESSFRAMES,
1641 .flush = av1_decode_flush,
1642 .p.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
1643 .p.priv_class = &av1_class,
1644 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1645 #if CONFIG_AV1_DXVA2_HWACCEL
1646 HWACCEL_DXVA2(av1),
1647 #endif
1648 #if CONFIG_AV1_D3D11VA_HWACCEL
1649 HWACCEL_D3D11VA(av1),
1650 #endif
1651 #if CONFIG_AV1_D3D11VA2_HWACCEL
1652 HWACCEL_D3D11VA2(av1),
1653 #endif
1654 #if CONFIG_AV1_D3D12VA_HWACCEL
1655 HWACCEL_D3D12VA(av1),
1656 #endif
1657 #if CONFIG_AV1_NVDEC_HWACCEL
1658 HWACCEL_NVDEC(av1),
1659 #endif
1660 #if CONFIG_AV1_VAAPI_HWACCEL
1661 HWACCEL_VAAPI(av1),
1662 #endif
1663 #if CONFIG_AV1_VDPAU_HWACCEL
1664 HWACCEL_VDPAU(av1),
1665 #endif
1666 #if CONFIG_AV1_VIDEOTOOLBOX_HWACCEL
1667 HWACCEL_VIDEOTOOLBOX(av1),
1668 #endif
1669 #if CONFIG_AV1_VULKAN_HWACCEL
1670 HWACCEL_VULKAN(av1),
1671 #endif
1672
1673 NULL
1674 },
1675 };
1676