FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1dec.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 170 833 20.4%
Functions: 8 33 24.2%
Branches: 64 459 13.9%

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