FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1dec.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 259 897 28.9%
Functions: 12 34 35.3%
Branches: 114 496 23.0%

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