]> git.sesse.net Git - ffmpeg/blob - libavcodec/av1dec.c
avcodec/av1dec: don't derive loop filter delta parameters
[ffmpeg] / libavcodec / av1dec.c
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 "libavutil/pixdesc.h"
22 #include "avcodec.h"
23 #include "av1dec.h"
24 #include "bytestream.h"
25 #include "hwconfig.h"
26 #include "internal.h"
27 #include "profiles.h"
28
29 static uint32_t inverse_recenter(int r, uint32_t v)
30 {
31     if (v > 2 * r)
32         return v;
33     else if (v & 1)
34         return r - ((v + 1) >> 1);
35     else
36         return r + (v >> 1);
37 }
38
39 static uint32_t decode_unsigned_subexp_with_ref(uint32_t sub_exp,
40                                                 int mx, int r)
41 {
42     if ((r << 1) <= mx) {
43         return inverse_recenter(r, sub_exp);
44     } else {
45         return mx - 1 - inverse_recenter(mx - 1 - r, sub_exp);
46     }
47 }
48
49 static int32_t decode_signed_subexp_with_ref(uint32_t sub_exp, int low,
50                                              int high, int r)
51 {
52     int32_t x = decode_unsigned_subexp_with_ref(sub_exp, high - low, r - low);
53     return x + low;
54 }
55
56 static void read_global_param(AV1DecContext *s, int type, int ref, int idx)
57 {
58     uint8_t primary_frame, prev_frame;
59     uint32_t abs_bits, prec_bits, round, prec_diff, sub, mx;
60     int32_t r;
61
62     primary_frame = s->raw_frame_header->primary_ref_frame;
63     prev_frame = s->raw_frame_header->ref_frame_idx[primary_frame];
64     abs_bits = AV1_GM_ABS_ALPHA_BITS;
65     prec_bits = AV1_GM_ALPHA_PREC_BITS;
66
67     if (idx < 2) {
68         if (type == AV1_WARP_MODEL_TRANSLATION) {
69             abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS -
70                 !s->raw_frame_header->allow_high_precision_mv;
71             prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS -
72                 !s->raw_frame_header->allow_high_precision_mv;
73         } else {
74             abs_bits = AV1_GM_ABS_TRANS_BITS;
75             prec_bits = AV1_GM_TRANS_PREC_BITS;
76         }
77     }
78     round = (idx % 3) == 2 ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
79     prec_diff = AV1_WARPEDMODEL_PREC_BITS - prec_bits;
80     sub = (idx % 3) == 2 ? (1 << prec_bits) : 0;
81     mx = 1 << abs_bits;
82     r = (s->ref[prev_frame].gm_params[ref][idx] >> prec_diff) - sub;
83
84     s->cur_frame.gm_params[ref][idx] =
85         (decode_signed_subexp_with_ref(s->raw_frame_header->gm_params[ref][idx],
86                                        -mx, mx + 1, r) << prec_diff) + round;
87 }
88
89 /**
90 * update gm type/params, since cbs already implemented part of this funcation,
91 * so we don't need to full implement spec.
92 */
93 static void global_motion_params(AV1DecContext *s)
94 {
95     const AV1RawFrameHeader *header = s->raw_frame_header;
96     int type, ref;
97
98     for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
99         s->cur_frame.gm_type[ref] = AV1_WARP_MODEL_IDENTITY;
100         for (int i = 0; i < 6; i++)
101             s->cur_frame.gm_params[ref][i] = (i % 3 == 2) ?
102                                              1 << AV1_WARPEDMODEL_PREC_BITS : 0;
103     }
104     if (header->frame_type == AV1_FRAME_KEY ||
105         header->frame_type == AV1_FRAME_INTRA_ONLY)
106         return;
107
108     for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
109         if (header->is_global[ref]) {
110             if (header->is_rot_zoom[ref]) {
111                 type = AV1_WARP_MODEL_ROTZOOM;
112             } else {
113                 type = header->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
114                                                    : AV1_WARP_MODEL_AFFINE;
115             }
116         } else {
117             type = AV1_WARP_MODEL_IDENTITY;
118         }
119         s->cur_frame.gm_type[ref] = type;
120
121         if (type >= AV1_WARP_MODEL_ROTZOOM) {
122             read_global_param(s, type, ref, 2);
123             read_global_param(s, type, ref, 3);
124             if (type == AV1_WARP_MODEL_AFFINE) {
125                 read_global_param(s, type, ref, 4);
126                 read_global_param(s, type, ref, 5);
127             } else {
128                 s->cur_frame.gm_params[ref][4] = -s->cur_frame.gm_params[ref][3];
129                 s->cur_frame.gm_params[ref][5] = s->cur_frame.gm_params[ref][2];
130             }
131         }
132         if (type >= AV1_WARP_MODEL_TRANSLATION) {
133             read_global_param(s, type, ref, 0);
134             read_global_param(s, type, ref, 1);
135         }
136     }
137 }
138
139 static int init_tile_data(AV1DecContext *s)
140
141 {
142     int cur_tile_num =
143         s->raw_frame_header->tile_cols * s->raw_frame_header->tile_rows;
144     if (s->tile_num < cur_tile_num) {
145         int ret = av_reallocp_array(&s->tile_group_info, cur_tile_num,
146                                     sizeof(TileGroupInfo));
147         if (ret < 0) {
148             s->tile_num = 0;
149             return ret;
150         }
151     }
152     s->tile_num = cur_tile_num;
153
154     return 0;
155 }
156
157 static int get_tiles_info(AVCodecContext *avctx, const AV1RawTileGroup *tile_group)
158 {
159     AV1DecContext *s = avctx->priv_data;
160     GetByteContext gb;
161     uint16_t tile_num, tile_row, tile_col;
162     uint32_t size = 0, size_bytes = 0;
163
164     bytestream2_init(&gb, tile_group->tile_data.data,
165                      tile_group->tile_data.data_size);
166     s->tg_start = tile_group->tg_start;
167     s->tg_end = tile_group->tg_end;
168
169     for (tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
170         tile_row = tile_num / s->raw_frame_header->tile_cols;
171         tile_col = tile_num % s->raw_frame_header->tile_cols;
172
173         if (tile_num == tile_group->tg_end) {
174             s->tile_group_info[tile_num].tile_size = bytestream2_get_bytes_left(&gb);
175             s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
176             s->tile_group_info[tile_num].tile_row = tile_row;
177             s->tile_group_info[tile_num].tile_column = tile_col;
178             return 0;
179         }
180         size_bytes = s->raw_frame_header->tile_size_bytes_minus1 + 1;
181         if (bytestream2_get_bytes_left(&gb) < size_bytes)
182             return AVERROR_INVALIDDATA;
183         size = 0;
184         for (int i = 0; i < size_bytes; i++)
185             size |= bytestream2_get_byteu(&gb) << 8 * i;
186         if (bytestream2_get_bytes_left(&gb) <= size)
187             return AVERROR_INVALIDDATA;
188         size++;
189
190         s->tile_group_info[tile_num].tile_size = size;
191         s->tile_group_info[tile_num].tile_offset = bytestream2_tell(&gb);
192         s->tile_group_info[tile_num].tile_row = tile_row;
193         s->tile_group_info[tile_num].tile_column = tile_col;
194
195         bytestream2_skipu(&gb, size);
196     }
197
198     return 0;
199
200 }
201
202 static int get_pixel_format(AVCodecContext *avctx)
203 {
204     AV1DecContext *s = avctx->priv_data;
205     const AV1RawSequenceHeader *seq = s->raw_seq;
206     uint8_t bit_depth;
207     int ret;
208     enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
209 #define HWACCEL_MAX (0)
210     enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
211
212     if (seq->seq_profile == 2 && seq->color_config.high_bitdepth)
213         bit_depth = seq->color_config.twelve_bit ? 12 : 10;
214     else if (seq->seq_profile <= 2)
215         bit_depth = seq->color_config.high_bitdepth ? 10 : 8;
216     else {
217         av_log(avctx, AV_LOG_ERROR,
218                "Unknown AV1 profile %d.\n", seq->seq_profile);
219         return -1;
220     }
221
222     if (!seq->color_config.mono_chrome) {
223         // 4:4:4 x:0 y:0, 4:2:2 x:1 y:0, 4:2:0 x:1 y:1
224         if (seq->color_config.subsampling_x == 0 &&
225             seq->color_config.subsampling_y == 0) {
226             if (bit_depth == 8)
227                 pix_fmt = AV_PIX_FMT_YUV444P;
228             else if (bit_depth == 10)
229                 pix_fmt = AV_PIX_FMT_YUV444P10;
230             else if (bit_depth == 12)
231                 pix_fmt = AV_PIX_FMT_YUV444P12;
232             else
233                 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
234         } else if (seq->color_config.subsampling_x == 1 &&
235                    seq->color_config.subsampling_y == 0) {
236             if (bit_depth == 8)
237                 pix_fmt = AV_PIX_FMT_YUV422P;
238             else if (bit_depth == 10)
239                 pix_fmt = AV_PIX_FMT_YUV422P10;
240             else if (bit_depth == 12)
241                 pix_fmt = AV_PIX_FMT_YUV422P12;
242             else
243                 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
244         } else if (seq->color_config.subsampling_x == 1 &&
245                    seq->color_config.subsampling_y == 1) {
246             if (bit_depth == 8)
247                 pix_fmt = AV_PIX_FMT_YUV420P;
248             else if (bit_depth == 10)
249                 pix_fmt = AV_PIX_FMT_YUV420P10;
250             else if (bit_depth == 12)
251                 pix_fmt = AV_PIX_FMT_YUV420P12;
252             else
253                 av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
254         }
255     } else {
256         if (seq->color_config.subsampling_x == 1 &&
257             seq->color_config.subsampling_y == 1)
258             pix_fmt = AV_PIX_FMT_YUV440P;
259         else
260             av_log(avctx, AV_LOG_WARNING, "Unknown AV1 pixel format.\n");
261     }
262
263     av_log(avctx, AV_LOG_DEBUG, "AV1 decode get format: %s.\n",
264            av_get_pix_fmt_name(pix_fmt));
265
266     if (pix_fmt == AV_PIX_FMT_NONE)
267         return -1;
268     s->pix_fmt = pix_fmt;
269
270     *fmtp++ = s->pix_fmt;
271     *fmtp = AV_PIX_FMT_NONE;
272
273     ret = ff_thread_get_format(avctx, pix_fmts);
274     if (ret < 0)
275         return ret;
276
277     /**
278      * check if the HW accel is inited correctly. If not, return un-implemented.
279      * Since now the av1 decoder doesn't support native decode, if it will be
280      * implemented in the future, need remove this check.
281      */
282     if (!avctx->hwaccel) {
283         av_log(avctx, AV_LOG_ERROR, "Your platform doesn't suppport"
284                " hardware accelerated AV1 decoding.\n");
285         return AVERROR(ENOSYS);
286     }
287
288     avctx->pix_fmt = ret;
289
290     return 0;
291 }
292
293 static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
294 {
295     ff_thread_release_buffer(avctx, &f->tf);
296     av_buffer_unref(&f->hwaccel_priv_buf);
297     f->hwaccel_picture_private = NULL;
298     f->spatial_id = f->temporal_id = 0;
299 }
300
301 static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
302 {
303     int ret;
304
305     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
306     if (ret < 0)
307         return ret;
308
309     if (src->hwaccel_picture_private) {
310         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
311         if (!dst->hwaccel_priv_buf)
312             goto fail;
313         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
314     }
315
316     dst->spatial_id = src->spatial_id;
317     dst->temporal_id = src->temporal_id;
318     memcpy(dst->gm_type,
319            src->gm_type,
320            AV1_NUM_REF_FRAMES * sizeof(uint8_t));
321     memcpy(dst->gm_params,
322            src->gm_params,
323            AV1_NUM_REF_FRAMES * 6 * sizeof(int32_t));
324
325     return 0;
326
327 fail:
328     av1_frame_unref(avctx, dst);
329     return AVERROR(ENOMEM);
330 }
331
332 static av_cold int av1_decode_free(AVCodecContext *avctx)
333 {
334     AV1DecContext *s = avctx->priv_data;
335
336     for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
337         av1_frame_unref(avctx, &s->ref[i]);
338         av_frame_free(&s->ref[i].tf.f);
339     }
340     av1_frame_unref(avctx, &s->cur_frame);
341     av_frame_free(&s->cur_frame.tf.f);
342
343     av_buffer_unref(&s->seq_ref);
344     av_buffer_unref(&s->header_ref);
345     av_freep(&s->tile_group_info);
346
347     ff_cbs_fragment_free(&s->current_obu);
348     ff_cbs_close(&s->cbc);
349
350     return 0;
351 }
352
353 static int set_context_with_sequence(AVCodecContext *avctx,
354                                      const AV1RawSequenceHeader *seq)
355 {
356     int width = seq->max_frame_width_minus_1 + 1;
357     int height = seq->max_frame_height_minus_1 + 1;
358
359     avctx->profile = seq->seq_profile;
360     avctx->level = seq->seq_level_idx[0];
361
362     avctx->color_range =
363         seq->color_config.color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
364     avctx->color_primaries = seq->color_config.color_primaries;
365     avctx->colorspace = seq->color_config.color_primaries;
366     avctx->color_trc = seq->color_config.transfer_characteristics;
367
368     switch (seq->color_config.chroma_sample_position) {
369     case AV1_CSP_VERTICAL:
370         avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
371         break;
372     case AV1_CSP_COLOCATED:
373         avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
374         break;
375     }
376
377     if (avctx->width != width || avctx->height != height) {
378         int ret = ff_set_dimensions(avctx, width, height);
379         if (ret < 0)
380             return ret;
381     }
382     avctx->sample_aspect_ratio = (AVRational) { 1, 1 };
383
384     if (seq->timing_info.num_units_in_display_tick &&
385         seq->timing_info.time_scale) {
386         av_reduce(&avctx->framerate.den, &avctx->framerate.num,
387                   seq->timing_info.num_units_in_display_tick,
388                   seq->timing_info.time_scale,
389                   INT_MAX);
390         if (seq->timing_info.equal_picture_interval)
391             avctx->ticks_per_frame = seq->timing_info.num_ticks_per_picture_minus_1 + 1;
392     }
393
394     return 0;
395 }
396
397 static int update_context_with_frame_header(AVCodecContext *avctx,
398                                             const AV1RawFrameHeader *header)
399 {
400     AVRational aspect_ratio;
401     int width = header->frame_width_minus_1 + 1;
402     int height = header->frame_height_minus_1 + 1;
403     int r_width = header->render_width_minus_1 + 1;
404     int r_height = header->render_height_minus_1 + 1;
405     int ret;
406
407     if (avctx->width != width || avctx->height != height) {
408         ret = ff_set_dimensions(avctx, width, height);
409         if (ret < 0)
410             return ret;
411     }
412
413     av_reduce(&aspect_ratio.num, &aspect_ratio.den,
414               (int64_t)height * r_width,
415               (int64_t)width * r_height,
416               INT_MAX);
417
418     if (av_cmp_q(avctx->sample_aspect_ratio, aspect_ratio)) {
419         ret = ff_set_sar(avctx, aspect_ratio);
420         if (ret < 0)
421             return ret;
422     }
423
424     return 0;
425 }
426
427 static av_cold int av1_decode_init(AVCodecContext *avctx)
428 {
429     AV1DecContext *s = avctx->priv_data;
430     AV1RawSequenceHeader *seq;
431     int ret;
432
433     s->avctx = avctx;
434     s->pix_fmt = AV_PIX_FMT_NONE;
435
436     for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
437         s->ref[i].tf.f = av_frame_alloc();
438         if (!s->ref[i].tf.f) {
439             av_log(avctx, AV_LOG_ERROR,
440                    "Failed to allocate reference frame buffer %d.\n", i);
441             return AVERROR(ENOMEM);
442         }
443     }
444
445     s->cur_frame.tf.f = av_frame_alloc();
446     if (!s->cur_frame.tf.f) {
447         av_log(avctx, AV_LOG_ERROR,
448                "Failed to allocate current frame buffer.\n");
449         return AVERROR(ENOMEM);
450     }
451
452     ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
453     if (ret < 0)
454         return ret;
455
456     if (avctx->extradata && avctx->extradata_size) {
457         ret = ff_cbs_read(s->cbc, &s->current_obu, avctx->extradata,
458                           avctx->extradata_size);
459         if (ret < 0) {
460             av_log(avctx, AV_LOG_WARNING, "Failed to read extradata.\n");
461             return ret;
462         }
463
464         seq = ((CodedBitstreamAV1Context *)(s->cbc->priv_data))->sequence_header;
465         if (!seq) {
466             av_log(avctx, AV_LOG_WARNING, "No sequence header available.\n");
467             goto end;
468         }
469
470         ret = set_context_with_sequence(avctx, seq);
471         if (ret < 0) {
472             av_log(avctx, AV_LOG_WARNING, "Failed to set decoder context.\n");
473             goto end;
474         }
475
476         end:
477         ff_cbs_fragment_reset(&s->current_obu);
478     }
479
480     return ret;
481 }
482
483 static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
484 {
485     AV1DecContext *s = avctx->priv_data;
486     AV1RawFrameHeader *header= s->raw_frame_header;
487     AVFrame *frame;
488     int ret;
489
490     ret = update_context_with_frame_header(avctx, header);
491     if (ret < 0) {
492         av_log(avctx, AV_LOG_ERROR, "Failed to update context with frame header\n");
493         return ret;
494     }
495
496     if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
497         return ret;
498
499     frame = f->tf.f;
500     frame->key_frame = header->frame_type == AV1_FRAME_KEY;
501
502     switch (header->frame_type) {
503     case AV1_FRAME_KEY:
504     case AV1_FRAME_INTRA_ONLY:
505         frame->pict_type = AV_PICTURE_TYPE_I;
506         break;
507     case AV1_FRAME_INTER:
508         frame->pict_type = AV_PICTURE_TYPE_P;
509         break;
510     case AV1_FRAME_SWITCH:
511         frame->pict_type = AV_PICTURE_TYPE_SP;
512         break;
513     }
514
515     if (avctx->hwaccel) {
516         const AVHWAccel *hwaccel = avctx->hwaccel;
517         if (hwaccel->frame_priv_data_size) {
518             f->hwaccel_priv_buf =
519                 av_buffer_allocz(hwaccel->frame_priv_data_size);
520             if (!f->hwaccel_priv_buf)
521                 goto fail;
522             f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
523         }
524     }
525     return 0;
526
527 fail:
528     av1_frame_unref(avctx, f);
529     return AVERROR(ENOMEM);
530 }
531
532 static int set_output_frame(AVCodecContext *avctx, AVFrame *frame,
533                             const AVPacket *pkt, int *got_frame)
534 {
535     AV1DecContext *s = avctx->priv_data;
536     const AVFrame *srcframe = s->cur_frame.tf.f;
537     int ret;
538
539     ret = av_frame_ref(frame, srcframe);
540     if (ret < 0)
541         return ret;
542
543     frame->pts = pkt->pts;
544     frame->pkt_dts = pkt->dts;
545     frame->pkt_size = pkt->size;
546
547     *got_frame = 1;
548
549     return 0;
550 }
551
552 static int update_reference_list(AVCodecContext *avctx)
553 {
554     AV1DecContext *s = avctx->priv_data;
555     const AV1RawFrameHeader *header = s->raw_frame_header;
556     int ret;
557
558     for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
559         if (header->refresh_frame_flags & (1 << i)) {
560             if (s->ref[i].tf.f->buf[0])
561                 av1_frame_unref(avctx, &s->ref[i]);
562             if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
563                 av_log(avctx, AV_LOG_ERROR,
564                        "Failed to update frame %d in reference list\n", i);
565                 return ret;
566             }
567         }
568     }
569     return 0;
570 }
571
572 static int get_current_frame(AVCodecContext *avctx)
573 {
574     AV1DecContext *s = avctx->priv_data;
575     int ret;
576
577     if (s->cur_frame.tf.f->buf[0])
578         av1_frame_unref(avctx, &s->cur_frame);
579
580     ret = av1_frame_alloc(avctx, &s->cur_frame);
581     if (ret < 0) {
582         av_log(avctx, AV_LOG_ERROR,
583                "Failed to allocate space for current frame.\n");
584         return ret;
585     }
586
587     ret = init_tile_data(s);
588     if (ret < 0) {
589         av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
590         return ret;
591     }
592
593     global_motion_params(s);
594
595     return ret;
596 }
597
598 static int av1_decode_frame(AVCodecContext *avctx, void *frame,
599                             int *got_frame, AVPacket *pkt)
600 {
601     AV1DecContext *s = avctx->priv_data;
602     AV1RawTileGroup *raw_tile_group = NULL;
603     int ret;
604
605     ret = ff_cbs_read_packet(s->cbc, &s->current_obu, pkt);
606     if (ret < 0) {
607         av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
608         goto end;
609     }
610     av_log(avctx, AV_LOG_DEBUG, "Total obu for this frame:%d.\n",
611            s->current_obu.nb_units);
612
613     for (int i = 0; i < s->current_obu.nb_units; i++) {
614         CodedBitstreamUnit *unit = &s->current_obu.units[i];
615         AV1RawOBU *obu = unit->content;
616         const AV1RawOBUHeader *header;
617
618         if (!obu)
619             continue;
620
621         header = &obu->header;
622         av_log(avctx, AV_LOG_DEBUG, "Obu idx:%d, obu type:%d.\n", i, unit->type);
623
624         switch (unit->type) {
625         case AV1_OBU_SEQUENCE_HEADER:
626             av_buffer_unref(&s->seq_ref);
627             s->seq_ref = av_buffer_ref(unit->content_ref);
628             if (!s->seq_ref) {
629                 ret = AVERROR(ENOMEM);
630                 goto end;
631             }
632
633             s->raw_seq = &obu->obu.sequence_header;
634
635             ret = set_context_with_sequence(avctx, s->raw_seq);
636             if (ret < 0) {
637                 av_log(avctx, AV_LOG_ERROR, "Failed to set context.\n");
638                 s->raw_seq = NULL;
639                 goto end;
640             }
641
642             if (s->pix_fmt == AV_PIX_FMT_NONE) {
643                 ret = get_pixel_format(avctx);
644                 if (ret < 0) {
645                     av_log(avctx, AV_LOG_ERROR,
646                            "Failed to get pixel format.\n");
647                     s->raw_seq = NULL;
648                     goto end;
649                 }
650             }
651
652             if (avctx->hwaccel && avctx->hwaccel->decode_params) {
653                 ret = avctx->hwaccel->decode_params(avctx, unit->type, unit->data,
654                                                     unit->data_size);
655                 if (ret < 0) {
656                     av_log(avctx, AV_LOG_ERROR, "HW accel decode params fail.\n");
657                     s->raw_seq = NULL;
658                     goto end;
659                 }
660             }
661             break;
662         case AV1_OBU_REDUNDANT_FRAME_HEADER:
663             if (s->raw_frame_header)
664                 break;
665         // fall-through
666         case AV1_OBU_FRAME:
667         case AV1_OBU_FRAME_HEADER:
668             if (!s->raw_seq) {
669                 av_log(avctx, AV_LOG_ERROR, "Missing Sequence Header.\n");
670                 ret = AVERROR_INVALIDDATA;
671                 goto end;
672             }
673
674             av_buffer_unref(&s->header_ref);
675             s->header_ref = av_buffer_ref(unit->content_ref);
676             if (!s->header_ref) {
677                 ret = AVERROR(ENOMEM);
678                 goto end;
679             }
680
681             if (unit->type == AV1_OBU_FRAME)
682                 s->raw_frame_header = &obu->obu.frame.header;
683             else
684                 s->raw_frame_header = &obu->obu.frame_header;
685
686             if (s->raw_frame_header->show_existing_frame) {
687                 if (s->cur_frame.tf.f->buf[0])
688                     av1_frame_unref(avctx, &s->cur_frame);
689
690                 ret = av1_frame_ref(avctx, &s->cur_frame,
691                                     &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
692                 if (ret < 0) {
693                     av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
694                     goto end;
695                 }
696
697                 ret = update_reference_list(avctx);
698                 if (ret < 0) {
699                     av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
700                     goto end;
701                 }
702
703                 ret = set_output_frame(avctx, frame, pkt, got_frame);
704                 if (ret < 0)
705                     av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
706
707                 s->raw_frame_header = NULL;
708
709                 goto end;
710             }
711
712             ret = get_current_frame(avctx);
713             if (ret < 0) {
714                 av_log(avctx, AV_LOG_ERROR, "Get current frame error\n");
715                 goto end;
716             }
717
718             s->cur_frame.spatial_id  = header->spatial_id;
719             s->cur_frame.temporal_id = header->temporal_id;
720
721             if (avctx->hwaccel) {
722                 ret = avctx->hwaccel->start_frame(avctx, unit->data,
723                                                   unit->data_size);
724                 if (ret < 0) {
725                     av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
726                     goto end;
727                 }
728             }
729             if (unit->type != AV1_OBU_FRAME)
730                 break;
731         // fall-through
732         case AV1_OBU_TILE_GROUP:
733             if (!s->raw_frame_header) {
734                 av_log(avctx, AV_LOG_ERROR, "Missing Frame Header.\n");
735                 ret = AVERROR_INVALIDDATA;
736                 goto end;
737             }
738
739             if (unit->type == AV1_OBU_FRAME)
740                 raw_tile_group = &obu->obu.frame.tile_group;
741             else
742                 raw_tile_group = &obu->obu.tile_group;
743
744             ret = get_tiles_info(avctx, raw_tile_group);
745             if (ret < 0)
746                 goto end;
747
748             if (avctx->hwaccel) {
749                 ret = avctx->hwaccel->decode_slice(avctx,
750                                                    raw_tile_group->tile_data.data,
751                                                    raw_tile_group->tile_data.data_size);
752                 if (ret < 0) {
753                     av_log(avctx, AV_LOG_ERROR,
754                            "HW accel decode slice fail.\n");
755                     goto end;
756                 }
757             }
758             break;
759         case AV1_OBU_TILE_LIST:
760         case AV1_OBU_TEMPORAL_DELIMITER:
761         case AV1_OBU_PADDING:
762         case AV1_OBU_METADATA:
763             break;
764         default:
765             av_log(avctx, AV_LOG_DEBUG,
766                    "Unknown obu type: %d (%"SIZE_SPECIFIER" bits).\n",
767                    unit->type, unit->data_size);
768         }
769
770         if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
771             if (avctx->hwaccel) {
772                 ret = avctx->hwaccel->end_frame(avctx);
773                 if (ret < 0) {
774                     av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
775                     goto end;
776                 }
777             }
778
779             ret = update_reference_list(avctx);
780             if (ret < 0) {
781                 av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
782                 goto end;
783             }
784
785             if (s->raw_frame_header->show_frame) {
786                 ret = set_output_frame(avctx, frame, pkt, got_frame);
787                 if (ret < 0) {
788                     av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
789                     goto end;
790                 }
791             }
792             raw_tile_group = NULL;
793             s->raw_frame_header = NULL;
794         }
795     }
796
797 end:
798     ff_cbs_fragment_reset(&s->current_obu);
799     if (ret < 0)
800         s->raw_frame_header = NULL;
801     return ret;
802 }
803
804 static void av1_decode_flush(AVCodecContext *avctx)
805 {
806     AV1DecContext *s = avctx->priv_data;
807
808     for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
809         av1_frame_unref(avctx, &s->ref[i]);
810
811     av1_frame_unref(avctx, &s->cur_frame);
812     s->raw_frame_header = NULL;
813     s->raw_seq = NULL;
814
815     ff_cbs_flush(s->cbc);
816 }
817
818 AVCodec ff_av1_decoder = {
819     .name                  = "av1",
820     .long_name             = NULL_IF_CONFIG_SMALL("Alliance for Open Media AV1"),
821     .type                  = AVMEDIA_TYPE_VIDEO,
822     .id                    = AV_CODEC_ID_AV1,
823     .priv_data_size        = sizeof(AV1DecContext),
824     .init                  = av1_decode_init,
825     .close                 = av1_decode_free,
826     .decode                = av1_decode_frame,
827     .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
828     .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE |
829                              FF_CODEC_CAP_INIT_CLEANUP |
830                              FF_CODEC_CAP_SETS_PKT_DTS,
831     .flush                 = av1_decode_flush,
832     .profiles              = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
833     .hw_configs            = (const AVCodecHWConfigInternal * []) {
834         NULL
835     },
836 };