1 /*
2  * Copyright (c) 2018 Ronald S. Bultje <rsbultje gmail com>
3  * Copyright (c) 2018 James Almer <jamrial gmail com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <dav1d/dav1d.h>
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/cpu.h"
26 #include "libavutil/film_grain_params.h"
27 #include "libavutil/mastering_display_metadata.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 
31 #include "atsc_a53.h"
32 #include "avcodec.h"
33 #include "bytestream.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "internal.h"
37 
38 #define FF_DAV1D_VERSION_AT_LEAST(x,y) \
39     (DAV1D_API_VERSION_MAJOR > (x) || DAV1D_API_VERSION_MAJOR == (x) && DAV1D_API_VERSION_MINOR >= (y))
40 
41 typedef struct Libdav1dContext {
42     AVClass *class;
43     Dav1dContext *c;
44     /* This packet coincides with AVCodecInternal.in_pkt
45      * and is not owned by us. */
46     AVPacket *pkt;
47     AVBufferPool *pool;
48     int pool_size;
49 
50     Dav1dData data;
51     int tile_threads;
52     int frame_threads;
53     int apply_grain;
54     int operating_point;
55     int all_layers;
56 } Libdav1dContext;
57 
58 static const enum AVPixelFormat pix_fmt[][3] = {
59     [DAV1D_PIXEL_LAYOUT_I400] = { AV_PIX_FMT_GRAY8,   AV_PIX_FMT_GRAY10,    AV_PIX_FMT_GRAY12 },
60     [DAV1D_PIXEL_LAYOUT_I420] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12 },
61     [DAV1D_PIXEL_LAYOUT_I422] = { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12 },
62     [DAV1D_PIXEL_LAYOUT_I444] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12 },
63 };
64 
65 static const enum AVPixelFormat pix_fmt_rgb[3] = {
66     AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
67 };
68 
libdav1d_log_callback(void *opaque, const char *fmt, va_list vl)69 static void libdav1d_log_callback(void *opaque, const char *fmt, va_list vl)
70 {
71     AVCodecContext *c = opaque;
72 
73     av_vlog(c, AV_LOG_ERROR, fmt, vl);
74 }
75 
libdav1d_picture_allocator(Dav1dPicture *p, void *cookie)76 static int libdav1d_picture_allocator(Dav1dPicture *p, void *cookie)
77 {
78     Libdav1dContext *dav1d = cookie;
79     enum AVPixelFormat format = pix_fmt[p->p.layout][p->seq_hdr->hbd];
80     int ret, linesize[4], h = FFALIGN(p->p.h, 128), w = FFALIGN(p->p.w, 128);
81     uint8_t *aligned_ptr, *data[4];
82     AVBufferRef *buf;
83 
84     ret = av_image_get_buffer_size(format, w, h, DAV1D_PICTURE_ALIGNMENT);
85     if (ret < 0)
86         return ret;
87 
88     if (ret != dav1d->pool_size) {
89         av_buffer_pool_uninit(&dav1d->pool);
90         // Use twice the amount of required padding bytes for aligned_ptr below.
91         dav1d->pool = av_buffer_pool_init(ret + DAV1D_PICTURE_ALIGNMENT * 2, NULL);
92         if (!dav1d->pool) {
93             dav1d->pool_size = 0;
94             return AVERROR(ENOMEM);
95         }
96         dav1d->pool_size = ret;
97     }
98     buf = av_buffer_pool_get(dav1d->pool);
99     if (!buf)
100         return AVERROR(ENOMEM);
101 
102     // libdav1d requires DAV1D_PICTURE_ALIGNMENT aligned buffers, which av_malloc()
103     // doesn't guarantee for example when AVX is disabled at configure time.
104     // Use the extra DAV1D_PICTURE_ALIGNMENT padding bytes in the buffer to align it
105     // if required.
106     aligned_ptr = (uint8_t *)FFALIGN((uintptr_t)buf->data, DAV1D_PICTURE_ALIGNMENT);
107     ret = av_image_fill_arrays(data, linesize, aligned_ptr, format, w, h,
108                                DAV1D_PICTURE_ALIGNMENT);
109     if (ret < 0) {
110         av_buffer_unref(&buf);
111         return ret;
112     }
113 
114     p->data[0] = data[0];
115     p->data[1] = data[1];
116     p->data[2] = data[2];
117     p->stride[0] = linesize[0];
118     p->stride[1] = linesize[1];
119     p->allocator_data = buf;
120 
121     return 0;
122 }
123 
libdav1d_picture_release(Dav1dPicture *p, void *cookie)124 static void libdav1d_picture_release(Dav1dPicture *p, void *cookie)
125 {
126     AVBufferRef *buf = p->allocator_data;
127 
128     av_buffer_unref(&buf);
129 }
130 
libdav1d_init_params(AVCodecContext *c, const Dav1dSequenceHeader *seq)131 static void libdav1d_init_params(AVCodecContext *c, const Dav1dSequenceHeader *seq)
132 {
133     c->profile = seq->profile;
134     c->level = ((seq->operating_points[0].major_level - 2) << 2)
135                | seq->operating_points[0].minor_level;
136 
137     switch (seq->chr) {
138     case DAV1D_CHR_VERTICAL:
139         c->chroma_sample_location = AVCHROMA_LOC_LEFT;
140         break;
141     case DAV1D_CHR_COLOCATED:
142         c->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
143         break;
144     }
145     c->colorspace = (enum AVColorSpace) seq->mtrx;
146     c->color_primaries = (enum AVColorPrimaries) seq->pri;
147     c->color_trc = (enum AVColorTransferCharacteristic) seq->trc;
148     c->color_range = seq->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
149 
150     if (seq->layout == DAV1D_PIXEL_LAYOUT_I444 &&
151         seq->mtrx == DAV1D_MC_IDENTITY &&
152         seq->pri  == DAV1D_COLOR_PRI_BT709 &&
153         seq->trc  == DAV1D_TRC_SRGB)
154         c->pix_fmt = pix_fmt_rgb[seq->hbd];
155     else
156         c->pix_fmt = pix_fmt[seq->layout][seq->hbd];
157 
158     if (seq->num_units_in_tick && seq->time_scale) {
159         av_reduce(&c->framerate.den, &c->framerate.num,
160                   seq->num_units_in_tick, seq->time_scale, INT_MAX);
161         if (seq->equal_picture_interval)
162             c->ticks_per_frame = seq->num_ticks_per_picture;
163     }
164 
165    if (seq->film_grain_present)
166        c->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
167    else
168        c->properties &= ~FF_CODEC_PROPERTY_FILM_GRAIN;
169 }
170 
libdav1d_parse_extradata(AVCodecContext *c)171 static av_cold int libdav1d_parse_extradata(AVCodecContext *c)
172 {
173     Dav1dSequenceHeader seq;
174     size_t offset = 0;
175     int res;
176 
177     if (!c->extradata || c->extradata_size <= 0)
178         return 0;
179 
180     if (c->extradata[0] & 0x80) {
181         int version = c->extradata[0] & 0x7F;
182 
183         if (version != 1 || c->extradata_size < 4) {
184             int explode = !!(c->err_recognition & AV_EF_EXPLODE);
185             av_log(c, explode ? AV_LOG_ERROR : AV_LOG_WARNING,
186                    "Error decoding extradata\n");
187             return explode ? AVERROR_INVALIDDATA : 0;
188         }
189 
190         // Do nothing if there are no configOBUs to parse
191         if (c->extradata_size == 4)
192             return 0;
193 
194         offset = 4;
195     }
196 
197     res = dav1d_parse_sequence_header(&seq, c->extradata + offset,
198                                       c->extradata_size  - offset);
199     if (res < 0)
200         return 0; // Assume no seqhdr OBUs are present
201 
202     libdav1d_init_params(c, &seq);
203     res = ff_set_dimensions(c, seq.max_width, seq.max_height);
204     if (res < 0)
205         return res;
206 
207     return 0;
208 }
209 
libdav1d_init(AVCodecContext *c)210 static av_cold int libdav1d_init(AVCodecContext *c)
211 {
212     Libdav1dContext *dav1d = c->priv_data;
213     Dav1dSettings s;
214 #if FF_DAV1D_VERSION_AT_LEAST(6,0)
215     int threads = c->thread_count;
216 #else
217     int threads = (c->thread_count ? c->thread_count : av_cpu_count()) * 3 / 2;
218 #endif
219     int res;
220 
221     dav1d->pkt = c->internal->in_pkt;
222 
223     av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
224 
225     dav1d_default_settings(&s);
226     s.logger.cookie = c;
227     s.logger.callback = libdav1d_log_callback;
228     s.allocator.cookie = dav1d;
229     s.allocator.alloc_picture_callback = libdav1d_picture_allocator;
230     s.allocator.release_picture_callback = libdav1d_picture_release;
231     s.frame_size_limit = c->max_pixels;
232     if (dav1d->apply_grain >= 0)
233         s.apply_grain = dav1d->apply_grain;
234     else
235         s.apply_grain = !(c->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
236 
237     s.all_layers = dav1d->all_layers;
238     if (dav1d->operating_point >= 0)
239         s.operating_point = dav1d->operating_point;
240 #if FF_DAV1D_VERSION_AT_LEAST(6,2)
241     s.strict_std_compliance = c->strict_std_compliance > 0;
242 #endif
243 
244 #if FF_DAV1D_VERSION_AT_LEAST(6,0)
245     if (dav1d->frame_threads || dav1d->tile_threads)
246         s.n_threads = FFMAX(dav1d->frame_threads, dav1d->tile_threads);
247     else
248         s.n_threads = FFMIN(threads, DAV1D_MAX_THREADS);
249     s.max_frame_delay = (c->flags & AV_CODEC_FLAG_LOW_DELAY) ? 1 : 0;
250     av_log(c, AV_LOG_DEBUG, "Using %d threads, %d max_frame_delay\n",
251            s.n_threads, s.max_frame_delay);
252 #else
253     s.n_tile_threads = dav1d->tile_threads
254                      ? dav1d->tile_threads
255                      : FFMIN(floor(sqrt(threads)), DAV1D_MAX_TILE_THREADS);
256     s.n_frame_threads = dav1d->frame_threads
257                       ? dav1d->frame_threads
258                       : FFMIN(ceil(threads / s.n_tile_threads), DAV1D_MAX_FRAME_THREADS);
259     av_log(c, AV_LOG_DEBUG, "Using %d frame threads, %d tile threads\n",
260            s.n_frame_threads, s.n_tile_threads);
261 #endif
262 
263     res = libdav1d_parse_extradata(c);
264     if (res < 0)
265         return res;
266 
267     res = dav1d_open(&dav1d->c, &s);
268     if (res < 0)
269         return AVERROR(ENOMEM);
270 
271     return 0;
272 }
273 
libdav1d_flush(AVCodecContext *c)274 static void libdav1d_flush(AVCodecContext *c)
275 {
276     Libdav1dContext *dav1d = c->priv_data;
277 
278     dav1d_data_unref(&dav1d->data);
279     dav1d_flush(dav1d->c);
280 }
281 
libdav1d_data_free(const uint8_t *data, void *opaque)282 static void libdav1d_data_free(const uint8_t *data, void *opaque) {
283     AVBufferRef *buf = opaque;
284 
285     av_buffer_unref(&buf);
286 }
287 
libdav1d_user_data_free(const uint8_t *data, void *opaque)288 static void libdav1d_user_data_free(const uint8_t *data, void *opaque) {
289     av_assert0(data == opaque);
290     av_free(opaque);
291 }
292 
libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)293 static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
294 {
295     Libdav1dContext *dav1d = c->priv_data;
296     Dav1dData *data = &dav1d->data;
297     Dav1dPicture pic = { 0 }, *p = &pic;
298 #if FF_DAV1D_VERSION_AT_LEAST(5,1)
299     enum Dav1dEventFlags event_flags = 0;
300 #endif
301     int res;
302 
303     if (!data->sz) {
304         AVPacket *const pkt = dav1d->pkt;
305 
306         res = ff_decode_get_packet(c, pkt);
307         if (res < 0 && res != AVERROR_EOF)
308             return res;
309 
310         if (pkt->size) {
311             res = dav1d_data_wrap(data, pkt->data, pkt->size,
312                                   libdav1d_data_free, pkt->buf);
313             if (res < 0) {
314                 av_packet_unref(pkt);
315                 return res;
316             }
317 
318             data->m.timestamp = pkt->pts;
319             data->m.offset    = pkt->pos;
320             data->m.duration  = pkt->duration;
321 
322             pkt->buf = NULL;
323             av_packet_unref(pkt);
324 
325             if (c->reordered_opaque != AV_NOPTS_VALUE) {
326                 uint8_t *reordered_opaque = av_memdup(&c->reordered_opaque,
327                                                       sizeof(c->reordered_opaque));
328                 if (!reordered_opaque) {
329                     dav1d_data_unref(data);
330                     return AVERROR(ENOMEM);
331                 }
332 
333                 res = dav1d_data_wrap_user_data(data, reordered_opaque,
334                                                 libdav1d_user_data_free, reordered_opaque);
335                 if (res < 0) {
336                     av_free(reordered_opaque);
337                     dav1d_data_unref(data);
338                     return res;
339                 }
340             }
341         } else if (res >= 0) {
342             av_packet_unref(pkt);
343             return AVERROR(EAGAIN);
344         }
345     }
346 
347     res = dav1d_send_data(dav1d->c, data);
348     if (res < 0) {
349         if (res == AVERROR(EINVAL))
350             res = AVERROR_INVALIDDATA;
351         if (res != AVERROR(EAGAIN)) {
352             dav1d_data_unref(data);
353             return res;
354         }
355     }
356 
357     res = dav1d_get_picture(dav1d->c, p);
358     if (res < 0) {
359         if (res == AVERROR(EINVAL))
360             res = AVERROR_INVALIDDATA;
361         else if (res == AVERROR(EAGAIN) && c->internal->draining)
362             res = AVERROR_EOF;
363 
364         return res;
365     }
366 
367     av_assert0(p->data[0] && p->allocator_data);
368 
369     // This requires the custom allocator above
370     frame->buf[0] = av_buffer_ref(p->allocator_data);
371     if (!frame->buf[0]) {
372         dav1d_picture_unref(p);
373         return AVERROR(ENOMEM);
374     }
375 
376     frame->data[0] = p->data[0];
377     frame->data[1] = p->data[1];
378     frame->data[2] = p->data[2];
379     frame->linesize[0] = p->stride[0];
380     frame->linesize[1] = p->stride[1];
381     frame->linesize[2] = p->stride[1];
382 
383 #if FF_DAV1D_VERSION_AT_LEAST(5,1)
384     dav1d_get_event_flags(dav1d->c, &event_flags);
385     if (c->pix_fmt == AV_PIX_FMT_NONE ||
386         event_flags & DAV1D_EVENT_FLAG_NEW_SEQUENCE)
387 #endif
388     libdav1d_init_params(c, p->seq_hdr);
389     res = ff_decode_frame_props(c, frame);
390     if (res < 0)
391         goto fail;
392 
393     frame->width = p->p.w;
394     frame->height = p->p.h;
395     if (c->width != p->p.w || c->height != p->p.h) {
396         res = ff_set_dimensions(c, p->p.w, p->p.h);
397         if (res < 0)
398             goto fail;
399     }
400 
401     av_reduce(&frame->sample_aspect_ratio.num,
402               &frame->sample_aspect_ratio.den,
403               frame->height * (int64_t)p->frame_hdr->render_width,
404               frame->width  * (int64_t)p->frame_hdr->render_height,
405               INT_MAX);
406     ff_set_sar(c, frame->sample_aspect_ratio);
407 
408     if (p->m.user_data.data)
409         memcpy(&frame->reordered_opaque, p->m.user_data.data, sizeof(frame->reordered_opaque));
410     else
411         frame->reordered_opaque = AV_NOPTS_VALUE;
412 
413     // match timestamps and packet size
414     frame->pts = p->m.timestamp;
415     frame->pkt_dts = p->m.timestamp;
416     frame->pkt_pos = p->m.offset;
417     frame->pkt_size = p->m.size;
418     frame->pkt_duration = p->m.duration;
419     frame->key_frame = p->frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY;
420 
421     switch (p->frame_hdr->frame_type) {
422     case DAV1D_FRAME_TYPE_KEY:
423     case DAV1D_FRAME_TYPE_INTRA:
424         frame->pict_type = AV_PICTURE_TYPE_I;
425         break;
426     case DAV1D_FRAME_TYPE_INTER:
427         frame->pict_type = AV_PICTURE_TYPE_P;
428         break;
429     case DAV1D_FRAME_TYPE_SWITCH:
430         frame->pict_type = AV_PICTURE_TYPE_SP;
431         break;
432     default:
433         res = AVERROR_INVALIDDATA;
434         goto fail;
435     }
436 
437     if (p->mastering_display) {
438         AVMasteringDisplayMetadata *mastering = av_mastering_display_metadata_create_side_data(frame);
439         if (!mastering) {
440             res = AVERROR(ENOMEM);
441             goto fail;
442         }
443 
444         for (int i = 0; i < 3; i++) {
445             mastering->display_primaries[i][0] = av_make_q(p->mastering_display->primaries[i][0], 1 << 16);
446             mastering->display_primaries[i][1] = av_make_q(p->mastering_display->primaries[i][1], 1 << 16);
447         }
448         mastering->white_point[0] = av_make_q(p->mastering_display->white_point[0], 1 << 16);
449         mastering->white_point[1] = av_make_q(p->mastering_display->white_point[1], 1 << 16);
450 
451         mastering->max_luminance = av_make_q(p->mastering_display->max_luminance, 1 << 8);
452         mastering->min_luminance = av_make_q(p->mastering_display->min_luminance, 1 << 14);
453 
454         mastering->has_primaries = 1;
455         mastering->has_luminance = 1;
456     }
457     if (p->content_light) {
458         AVContentLightMetadata *light = av_content_light_metadata_create_side_data(frame);
459         if (!light) {
460             res = AVERROR(ENOMEM);
461             goto fail;
462         }
463         light->MaxCLL = p->content_light->max_content_light_level;
464         light->MaxFALL = p->content_light->max_frame_average_light_level;
465     }
466     if (p->itut_t35) {
467         GetByteContext gb;
468         unsigned int user_identifier;
469 
470         bytestream2_init(&gb, p->itut_t35->payload, p->itut_t35->payload_size);
471         bytestream2_skip(&gb, 1); // terminal provider code
472         bytestream2_skip(&gb, 1); // terminal provider oriented code
473         user_identifier = bytestream2_get_be32(&gb);
474         switch (user_identifier) {
475         case MKBETAG('G', 'A', '9', '4'): { // closed captions
476             AVBufferRef *buf = NULL;
477 
478             res = ff_parse_a53_cc(&buf, gb.buffer, bytestream2_get_bytes_left(&gb));
479             if (res < 0)
480                 goto fail;
481             if (!res)
482                 break;
483 
484             if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, buf))
485                 av_buffer_unref(&buf);
486 
487             c->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
488             break;
489         }
490         default: // ignore unsupported identifiers
491             break;
492         }
493     }
494     if (p->frame_hdr->film_grain.present && (!dav1d->apply_grain ||
495         (c->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))) {
496         AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
497         if (!fgp) {
498             res = AVERROR(ENOMEM);
499             goto fail;
500         }
501 
502         fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
503         fgp->seed = p->frame_hdr->film_grain.data.seed;
504         fgp->codec.aom.num_y_points = p->frame_hdr->film_grain.data.num_y_points;
505         fgp->codec.aom.chroma_scaling_from_luma = p->frame_hdr->film_grain.data.chroma_scaling_from_luma;
506         fgp->codec.aom.scaling_shift = p->frame_hdr->film_grain.data.scaling_shift;
507         fgp->codec.aom.ar_coeff_lag = p->frame_hdr->film_grain.data.ar_coeff_lag;
508         fgp->codec.aom.ar_coeff_shift = p->frame_hdr->film_grain.data.ar_coeff_shift;
509         fgp->codec.aom.grain_scale_shift = p->frame_hdr->film_grain.data.grain_scale_shift;
510         fgp->codec.aom.overlap_flag = p->frame_hdr->film_grain.data.overlap_flag;
511         fgp->codec.aom.limit_output_range = p->frame_hdr->film_grain.data.clip_to_restricted_range;
512 
513         memcpy(&fgp->codec.aom.y_points, &p->frame_hdr->film_grain.data.y_points,
514                sizeof(fgp->codec.aom.y_points));
515         memcpy(&fgp->codec.aom.num_uv_points, &p->frame_hdr->film_grain.data.num_uv_points,
516                sizeof(fgp->codec.aom.num_uv_points));
517         memcpy(&fgp->codec.aom.uv_points, &p->frame_hdr->film_grain.data.uv_points,
518                sizeof(fgp->codec.aom.uv_points));
519         memcpy(&fgp->codec.aom.ar_coeffs_y, &p->frame_hdr->film_grain.data.ar_coeffs_y,
520                sizeof(fgp->codec.aom.ar_coeffs_y));
521         memcpy(&fgp->codec.aom.ar_coeffs_uv[0], &p->frame_hdr->film_grain.data.ar_coeffs_uv[0],
522                sizeof(fgp->codec.aom.ar_coeffs_uv[0]));
523         memcpy(&fgp->codec.aom.ar_coeffs_uv[1], &p->frame_hdr->film_grain.data.ar_coeffs_uv[1],
524                sizeof(fgp->codec.aom.ar_coeffs_uv[1]));
525         memcpy(&fgp->codec.aom.uv_mult, &p->frame_hdr->film_grain.data.uv_mult,
526                sizeof(fgp->codec.aom.uv_mult));
527         memcpy(&fgp->codec.aom.uv_mult_luma, &p->frame_hdr->film_grain.data.uv_luma_mult,
528                sizeof(fgp->codec.aom.uv_mult_luma));
529         memcpy(&fgp->codec.aom.uv_offset, &p->frame_hdr->film_grain.data.uv_offset,
530                sizeof(fgp->codec.aom.uv_offset));
531     }
532 
533     res = 0;
534 fail:
535     dav1d_picture_unref(p);
536     if (res < 0)
537         av_frame_unref(frame);
538     return res;
539 }
540 
libdav1d_close(AVCodecContext *c)541 static av_cold int libdav1d_close(AVCodecContext *c)
542 {
543     Libdav1dContext *dav1d = c->priv_data;
544 
545     av_buffer_pool_uninit(&dav1d->pool);
546     dav1d_data_unref(&dav1d->data);
547     dav1d_close(&dav1d->c);
548 
549     return 0;
550 }
551 
552 #ifndef DAV1D_MAX_FRAME_THREADS
553 #define DAV1D_MAX_FRAME_THREADS DAV1D_MAX_THREADS
554 #endif
555 #ifndef DAV1D_MAX_TILE_THREADS
556 #define DAV1D_MAX_TILE_THREADS DAV1D_MAX_THREADS
557 #endif
558 
559 #define OFFSET(x) offsetof(Libdav1dContext, x)
560 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
561 static const AVOption libdav1d_options[] = {
562     { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_TILE_THREADS, VD | AV_OPT_FLAG_DEPRECATED },
563     { "framethreads", "Frame threads", OFFSET(frame_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_FRAME_THREADS, VD | AV_OPT_FLAG_DEPRECATED },
564     { "filmgrain", "Apply Film Grain", OFFSET(apply_grain), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD | AV_OPT_FLAG_DEPRECATED },
565     { "oppoint",  "Select an operating point of the scalable bitstream", OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 31, VD },
566     { "alllayers", "Output all spatial layers", OFFSET(all_layers), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
567     { NULL }
568 };
569 
570 static const AVClass libdav1d_class = {
571     .class_name = "libdav1d decoder",
572     .item_name  = av_default_item_name,
573     .option     = libdav1d_options,
574     .version    = LIBAVUTIL_VERSION_INT,
575 };
576 
577 const FFCodec ff_libdav1d_decoder = {
578     .p.name         = "libdav1d",
579     .p.long_name    = NULL_IF_CONFIG_SMALL("dav1d AV1 decoder by VideoLAN"),
580     .p.type         = AVMEDIA_TYPE_VIDEO,
581     .p.id           = AV_CODEC_ID_AV1,
582     .priv_data_size = sizeof(Libdav1dContext),
583     .init           = libdav1d_init,
584     .close          = libdav1d_close,
585     .flush          = libdav1d_flush,
586     FF_CODEC_RECEIVE_FRAME_CB(libdav1d_receive_frame),
587     .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
588     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_SETS_PKT_DTS |
589                       FF_CODEC_CAP_AUTO_THREADS,
590     .p.priv_class   = &libdav1d_class,
591     .p.wrapper_name = "libdav1d",
592 };
593