avcodec: libdav1d AV1 decoder wrapper.
[libav.git] / libavcodec / libdav1d.c
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 Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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/fifo.h"
26 #include "libavutil/common.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29
30 #include "avcodec.h"
31 #include "decode.h"
32 #include "internal.h"
33
34 typedef struct Libdav1dContext {
35 AVClass *class;
36 Dav1dContext *c;
37
38 AVFifoBuffer *cache;
39 Dav1dData data;
40 int tile_threads;
41 } Libdav1dContext;
42
43 static av_cold int libdav1d_init(AVCodecContext *c)
44 {
45 Libdav1dContext *dav1d = c->priv_data;
46 Dav1dSettings s;
47 int res;
48
49 av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
50
51 dav1d_default_settings(&s);
52 s.n_tile_threads = dav1d->tile_threads;
53 s.n_frame_threads = FFMIN(c->thread_count ? c->thread_count : av_cpu_count(), 256);
54
55 dav1d->cache = av_fifo_alloc(8 * sizeof(AVPacket));
56 if (!dav1d->cache)
57 return AVERROR(ENOMEM);
58
59 res = dav1d_open(&dav1d->c, &s);
60 if (res < 0)
61 return AVERROR(ENOMEM);
62
63 return 0;
64 }
65
66 static void libdav1d_flush(AVCodecContext *c)
67 {
68 Libdav1dContext *dav1d = c->priv_data;
69
70 av_fifo_reset(dav1d->cache);
71 dav1d_data_unref(&dav1d->data);
72 dav1d_flush(dav1d->c);
73 }
74
75 static int libdav1d_fifo_write(void *src, void *dst, int dst_size) {
76 AVPacket *pkt_dst = dst, *pkt_src = src;
77
78 av_assert2(dst_size >= sizeof(AVPacket));
79
80 pkt_src->buf = NULL;
81 av_packet_free_side_data(pkt_src);
82 *pkt_dst = *pkt_src;
83
84 return sizeof(AVPacket);
85 }
86
87 static void libdav1d_data_free(const uint8_t *data, void *opaque) {
88 AVBufferRef *buf = opaque;
89
90 av_buffer_unref(&buf);
91 }
92
93 static void libdav1d_frame_free(void *opaque, uint8_t *data) {
94 Dav1dPicture p = { 0 };
95
96 p.ref = opaque;
97 p.data[0] = (void *) 0x1; // this has to be non-NULL
98 dav1d_picture_unref(&p);
99 }
100
101 static const enum AVPixelFormat pix_fmt[][2] = {
102 [DAV1D_PIXEL_LAYOUT_I400] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10 },
103 [DAV1D_PIXEL_LAYOUT_I420] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10 },
104 [DAV1D_PIXEL_LAYOUT_I422] = { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10 },
105 [DAV1D_PIXEL_LAYOUT_I444] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10 },
106 };
107
108 // TODO: Update once 12bit support is added.
109 static const int profile[] = {
110 [DAV1D_PIXEL_LAYOUT_I400] = FF_PROFILE_AV1_MAIN,
111 [DAV1D_PIXEL_LAYOUT_I420] = FF_PROFILE_AV1_MAIN,
112 [DAV1D_PIXEL_LAYOUT_I422] = FF_PROFILE_AV1_PROFESSIONAL,
113 [DAV1D_PIXEL_LAYOUT_I444] = FF_PROFILE_AV1_HIGH,
114 };
115
116 static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
117 {
118 Libdav1dContext *dav1d = c->priv_data;
119 Dav1dData *data = &dav1d->data;
120 AVPacket pkt = { 0 };
121 Dav1dPicture p = { 0 };
122 int res;
123
124 if (!data->sz) {
125 res = ff_decode_get_packet(c, &pkt);
126 if (res < 0 && res != AVERROR_EOF)
127 return res;
128
129 if (pkt.size) {
130 if (!av_fifo_space(dav1d->cache)) {
131 res = av_fifo_realloc2(dav1d->cache, av_fifo_size(dav1d->cache) + 8 * sizeof(pkt));
132 if (res < 0) {
133 av_packet_unref(&pkt);
134 return res;
135 }
136 }
137
138 res = dav1d_data_wrap(data, pkt.data, pkt.size, libdav1d_data_free, pkt.buf);
139 if (res < 0) {
140 av_packet_unref(&pkt);
141 return res;
142 }
143
144 av_fifo_generic_write(dav1d->cache, &pkt, sizeof(pkt), libdav1d_fifo_write);
145 } else {
146 data = NULL;
147 }
148 }
149
150 res = dav1d_decode(dav1d->c, data, &p);
151 if (res < 0) {
152 if (res == -EINVAL)
153 res = AVERROR_INVALIDDATA;
154 else if (res == -EAGAIN && c->internal->draining)
155 res = AVERROR_EOF;
156
157 return res;
158 }
159
160 av_assert0(p.data[0] != NULL);
161
162 av_fifo_generic_read(dav1d->cache, &pkt, sizeof(pkt), NULL);
163
164 frame->buf[0] = av_buffer_create(NULL, 0, libdav1d_frame_free,
165 p.ref, AV_BUFFER_FLAG_READONLY);
166 if (!frame->buf[0]) {
167 dav1d_picture_unref(&p);
168 return AVERROR(ENOMEM);
169 }
170
171 frame->data[0] = p.data[0];
172 frame->data[1] = p.data[1];
173 frame->data[2] = p.data[2];
174 frame->linesize[0] = p.stride[0];
175 frame->linesize[1] = p.stride[1];
176 frame->linesize[2] = p.stride[1];
177
178 c->profile = profile[p.p.layout];
179 frame->format = c->pix_fmt = pix_fmt[p.p.layout][p.p.bpc == 10];
180 frame->width = p.p.w;
181 frame->height = p.p.h;
182 if (c->width != p.p.w || c->height != p.p.h) {
183 res = ff_set_dimensions(c, p.p.w, p.p.h);
184 if (res < 0)
185 return res;
186 }
187
188 switch (p.p.chr) {
189 case DAV1D_CHR_VERTICAL:
190 frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_LEFT;
191 break;
192 case DAV1D_CHR_COLOCATED:
193 frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
194 break;
195 }
196 frame->colorspace = c->colorspace = (enum AVColorSpace) p.p.mtrx;
197 frame->color_primaries = c->color_primaries = (enum AVColorPrimaries) p.p.pri;
198 frame->color_trc = c->color_trc = (enum AVColorTransferCharacteristic) p.p.trc;
199 frame->color_range = c->color_range = p.p.fullrange ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
200
201 // match timestamps and packet size
202 frame->pts = pkt.pts;
203 #if FF_API_PKT_PTS
204 FF_DISABLE_DEPRECATION_WARNINGS
205 frame->pkt_pts = pkt.pts;
206 FF_ENABLE_DEPRECATION_WARNINGS
207 #endif
208 frame->pkt_dts = pkt.dts;
209 frame->key_frame = p.p.type == DAV1D_FRAME_TYPE_KEY;
210
211 switch (p.p.type) {
212 case DAV1D_FRAME_TYPE_KEY:
213 case DAV1D_FRAME_TYPE_INTRA:
214 frame->pict_type = AV_PICTURE_TYPE_I;
215 break;
216 case DAV1D_FRAME_TYPE_INTER:
217 frame->pict_type = AV_PICTURE_TYPE_P;
218 break;
219 case DAV1D_FRAME_TYPE_SWITCH:
220 frame->pict_type = AV_PICTURE_TYPE_SP;
221 break;
222 default:
223 return AVERROR_INVALIDDATA;
224 }
225
226 return 0;
227 }
228
229 static av_cold int libdav1d_close(AVCodecContext *c)
230 {
231 Libdav1dContext *dav1d = c->priv_data;
232
233 av_fifo_free(dav1d->cache);
234 dav1d_data_unref(&dav1d->data);
235 dav1d_close(&dav1d->c);
236
237 return 0;
238 }
239
240 #define OFFSET(x) offsetof(Libdav1dContext, x)
241 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
242 static const AVOption libdav1d_options[] = {
243 { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 64, VD, NULL },
244 { NULL }
245 };
246
247 static const AVClass libdav1d_class = {
248 .class_name = "libdav1d decoder",
249 .item_name = av_default_item_name,
250 .option = libdav1d_options,
251 .version = LIBAVUTIL_VERSION_INT,
252 };
253
254 AVCodec ff_libdav1d_decoder = {
255 .name = "libdav1d",
256 .long_name = NULL_IF_CONFIG_SMALL("dav1d AV1 decoder by VideoLAN"),
257 .type = AVMEDIA_TYPE_VIDEO,
258 .id = AV_CODEC_ID_AV1,
259 .priv_data_size = sizeof(Libdav1dContext),
260 .init = libdav1d_init,
261 .close = libdav1d_close,
262 .flush = libdav1d_flush,
263 .receive_frame = libdav1d_receive_frame,
264 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
265 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
266 FF_CODEC_CAP_SETS_PKT_DTS,
267 .priv_class = &libdav1d_class,
268 .wrapper_name = "libdav1d",
269 };