libdav1d: fix build after a recent API break
[libav.git] / libavcodec / libdav1d.c
CommitLineData
9bf9358b
JA
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
34typedef struct Libdav1dContext {
35 AVClass *class;
36 Dav1dContext *c;
37
38 AVFifoBuffer *cache;
39 Dav1dData data;
40 int tile_threads;
41} Libdav1dContext;
42
43static 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
66static 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
75static 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
87static void libdav1d_data_free(const uint8_t *data, void *opaque) {
88 AVBufferRef *buf = opaque;
89
90 av_buffer_unref(&buf);
91}
92
93static 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
101static 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.
109static 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
116static 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);
9bf9358b
JA
145 }
146 }
147
56f50183
JA
148 res = dav1d_send_data(dav1d->c, data);
149 if (res < 0) {
150 if (res == -EINVAL)
151 res = AVERROR_INVALIDDATA;
152 if (res != -EAGAIN)
153 return res;
154 }
155
156 res = dav1d_get_picture(dav1d->c, &p);
9bf9358b
JA
157 if (res < 0) {
158 if (res == -EINVAL)
159 res = AVERROR_INVALIDDATA;
160 else if (res == -EAGAIN && c->internal->draining)
161 res = AVERROR_EOF;
162
163 return res;
164 }
165
166 av_assert0(p.data[0] != NULL);
167
168 av_fifo_generic_read(dav1d->cache, &pkt, sizeof(pkt), NULL);
169
170 frame->buf[0] = av_buffer_create(NULL, 0, libdav1d_frame_free,
171 p.ref, AV_BUFFER_FLAG_READONLY);
172 if (!frame->buf[0]) {
173 dav1d_picture_unref(&p);
174 return AVERROR(ENOMEM);
175 }
176
177 frame->data[0] = p.data[0];
178 frame->data[1] = p.data[1];
179 frame->data[2] = p.data[2];
180 frame->linesize[0] = p.stride[0];
181 frame->linesize[1] = p.stride[1];
182 frame->linesize[2] = p.stride[1];
183
184 c->profile = profile[p.p.layout];
185 frame->format = c->pix_fmt = pix_fmt[p.p.layout][p.p.bpc == 10];
186 frame->width = p.p.w;
187 frame->height = p.p.h;
188 if (c->width != p.p.w || c->height != p.p.h) {
189 res = ff_set_dimensions(c, p.p.w, p.p.h);
190 if (res < 0)
191 return res;
192 }
193
194 switch (p.p.chr) {
195 case DAV1D_CHR_VERTICAL:
196 frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_LEFT;
197 break;
198 case DAV1D_CHR_COLOCATED:
199 frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
200 break;
201 }
202 frame->colorspace = c->colorspace = (enum AVColorSpace) p.p.mtrx;
203 frame->color_primaries = c->color_primaries = (enum AVColorPrimaries) p.p.pri;
204 frame->color_trc = c->color_trc = (enum AVColorTransferCharacteristic) p.p.trc;
205 frame->color_range = c->color_range = p.p.fullrange ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
206
207 // match timestamps and packet size
208 frame->pts = pkt.pts;
209#if FF_API_PKT_PTS
210FF_DISABLE_DEPRECATION_WARNINGS
211 frame->pkt_pts = pkt.pts;
212FF_ENABLE_DEPRECATION_WARNINGS
213#endif
214 frame->pkt_dts = pkt.dts;
215 frame->key_frame = p.p.type == DAV1D_FRAME_TYPE_KEY;
216
217 switch (p.p.type) {
218 case DAV1D_FRAME_TYPE_KEY:
219 case DAV1D_FRAME_TYPE_INTRA:
220 frame->pict_type = AV_PICTURE_TYPE_I;
221 break;
222 case DAV1D_FRAME_TYPE_INTER:
223 frame->pict_type = AV_PICTURE_TYPE_P;
224 break;
225 case DAV1D_FRAME_TYPE_SWITCH:
226 frame->pict_type = AV_PICTURE_TYPE_SP;
227 break;
228 default:
229 return AVERROR_INVALIDDATA;
230 }
231
232 return 0;
233}
234
235static av_cold int libdav1d_close(AVCodecContext *c)
236{
237 Libdav1dContext *dav1d = c->priv_data;
238
239 av_fifo_free(dav1d->cache);
240 dav1d_data_unref(&dav1d->data);
241 dav1d_close(&dav1d->c);
242
243 return 0;
244}
245
246#define OFFSET(x) offsetof(Libdav1dContext, x)
247#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
248static const AVOption libdav1d_options[] = {
249 { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 64, VD, NULL },
250 { NULL }
251};
252
253static const AVClass libdav1d_class = {
254 .class_name = "libdav1d decoder",
255 .item_name = av_default_item_name,
256 .option = libdav1d_options,
257 .version = LIBAVUTIL_VERSION_INT,
258};
259
260AVCodec ff_libdav1d_decoder = {
261 .name = "libdav1d",
262 .long_name = NULL_IF_CONFIG_SMALL("dav1d AV1 decoder by VideoLAN"),
263 .type = AVMEDIA_TYPE_VIDEO,
264 .id = AV_CODEC_ID_AV1,
265 .priv_data_size = sizeof(Libdav1dContext),
266 .init = libdav1d_init,
267 .close = libdav1d_close,
268 .flush = libdav1d_flush,
269 .receive_frame = libdav1d_receive_frame,
270 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
271 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
272 FF_CODEC_CAP_SETS_PKT_DTS,
273 .priv_class = &libdav1d_class,
274 .wrapper_name = "libdav1d",
275};