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