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