FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1dec.c
Date: 2026-11-01 11:15:23
Exec Total Coverage
Lines: 286 859 33.3%
Functions: 12 34 35.3%
Branches: 117 473 24.7%

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