/
codec_dav1d.c
216 lines (189 loc) · 7.34 KB
/
codec_dav1d.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2019 Joe Drago. All rights reserved.
// SPDX-License-Identifier: BSD-2-Clause
#include "avif/internal.h"
#if defined(_MSC_VER)
#pragma warning(disable : 4201) // nonstandard extension used: nameless struct/union
#endif
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc11-extensions" // C11 extension used: nameless struct/union
#endif
#include "dav1d/dav1d.h"
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#include <string.h>
// For those building with an older version of dav1d (not recommended).
#ifndef DAV1D_ERR
#define DAV1D_ERR(e) (-(e))
#endif
struct avifCodecInternal
{
Dav1dSettings dav1dSettings;
Dav1dContext * dav1dContext;
Dav1dPicture dav1dPicture;
avifBool hasPicture;
avifRange colorRange;
};
static void avifDav1dFreeCallback(const uint8_t * buf, void * cookie)
{
// This data is owned by the decoder; nothing to free here
(void)buf;
(void)cookie;
}
static void dav1dCodecDestroyInternal(avifCodec * codec)
{
if (codec->internal->hasPicture) {
dav1d_picture_unref(&codec->internal->dav1dPicture);
}
if (codec->internal->dav1dContext) {
dav1d_close(&codec->internal->dav1dContext);
}
avifFree(codec->internal);
}
static avifBool dav1dCodecOpen(avifCodec * codec)
{
if (codec->internal->dav1dContext == NULL) {
if (dav1d_open(&codec->internal->dav1dContext, &codec->internal->dav1dSettings) != 0) {
return AVIF_FALSE;
}
}
return AVIF_TRUE;
}
static avifBool dav1dCodecGetNextImage(struct avifCodec * codec, const avifDecodeSample * sample, avifBool alpha, avifImage * image)
{
avifBool gotPicture = AVIF_FALSE;
Dav1dPicture nextFrame;
memset(&nextFrame, 0, sizeof(Dav1dPicture));
Dav1dData dav1dData;
if (dav1d_data_wrap(&dav1dData, sample->data.data, sample->data.size, avifDav1dFreeCallback, NULL) != 0) {
return AVIF_FALSE;
}
for (;;) {
if (dav1dData.data) {
int res = dav1d_send_data(codec->internal->dav1dContext, &dav1dData);
if ((res < 0) && (res != DAV1D_ERR(EAGAIN))) {
dav1d_data_unref(&dav1dData);
return AVIF_FALSE;
}
}
int res = dav1d_get_picture(codec->internal->dav1dContext, &nextFrame);
if (res == DAV1D_ERR(EAGAIN)) {
if (dav1dData.data) {
// send more data
continue;
}
return AVIF_FALSE;
} else if (res < 0) {
// No more frames
if (dav1dData.data) {
dav1d_data_unref(&dav1dData);
}
return AVIF_FALSE;
} else {
// Got a picture!
gotPicture = AVIF_TRUE;
break;
}
}
if (dav1dData.data) {
dav1d_data_unref(&dav1dData);
}
if (gotPicture) {
dav1d_picture_unref(&codec->internal->dav1dPicture);
codec->internal->dav1dPicture = nextFrame;
codec->internal->colorRange = codec->internal->dav1dPicture.seq_hdr->color_range ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED;
codec->internal->hasPicture = AVIF_TRUE;
} else {
if (alpha && codec->internal->hasPicture) {
// Special case: reuse last alpha frame
} else {
return AVIF_FALSE;
}
}
Dav1dPicture * dav1dImage = &codec->internal->dav1dPicture;
avifBool isColor = !alpha;
if (isColor) {
// Color (YUV) planes - set image to correct size / format, fill color
avifPixelFormat yuvFormat = AVIF_PIXEL_FORMAT_NONE;
switch (dav1dImage->p.layout) {
case DAV1D_PIXEL_LAYOUT_I400:
yuvFormat = AVIF_PIXEL_FORMAT_YUV400;
break;
case DAV1D_PIXEL_LAYOUT_I420:
yuvFormat = AVIF_PIXEL_FORMAT_YUV420;
break;
case DAV1D_PIXEL_LAYOUT_I422:
yuvFormat = AVIF_PIXEL_FORMAT_YUV422;
break;
case DAV1D_PIXEL_LAYOUT_I444:
yuvFormat = AVIF_PIXEL_FORMAT_YUV444;
break;
}
if (image->width && image->height) {
if ((image->width != (uint32_t)dav1dImage->p.w) || (image->height != (uint32_t)dav1dImage->p.h) ||
(image->depth != (uint32_t)dav1dImage->p.bpc) || (image->yuvFormat != yuvFormat)) {
// Throw it all out
avifImageFreePlanes(image, AVIF_PLANES_ALL);
}
}
image->width = dav1dImage->p.w;
image->height = dav1dImage->p.h;
image->depth = dav1dImage->p.bpc;
image->yuvFormat = yuvFormat;
image->yuvRange = codec->internal->colorRange;
image->yuvChromaSamplePosition = (avifChromaSamplePosition)dav1dImage->seq_hdr->chr;
image->colorPrimaries = (avifColorPrimaries)dav1dImage->seq_hdr->pri;
image->transferCharacteristics = (avifTransferCharacteristics)dav1dImage->seq_hdr->trc;
image->matrixCoefficients = (avifMatrixCoefficients)dav1dImage->seq_hdr->mtrx;
avifPixelFormatInfo formatInfo;
avifGetPixelFormatInfo(yuvFormat, &formatInfo);
avifImageFreePlanes(image, AVIF_PLANES_YUV);
int yuvPlaneCount = (yuvFormat == AVIF_PIXEL_FORMAT_YUV400) ? 1 : 3;
for (int yuvPlane = 0; yuvPlane < yuvPlaneCount; ++yuvPlane) {
image->yuvPlanes[yuvPlane] = dav1dImage->data[yuvPlane];
image->yuvRowBytes[yuvPlane] = (uint32_t)dav1dImage->stride[(yuvPlane == AVIF_CHAN_Y) ? 0 : 1];
}
image->imageOwnsYUVPlanes = AVIF_FALSE;
} else {
// Alpha plane - ensure image is correct size, fill color
if (image->width && image->height) {
if ((image->width != (uint32_t)dav1dImage->p.w) || (image->height != (uint32_t)dav1dImage->p.h) ||
(image->depth != (uint32_t)dav1dImage->p.bpc)) {
// Alpha plane doesn't match previous alpha plane decode, bail out
return AVIF_FALSE;
}
}
image->width = dav1dImage->p.w;
image->height = dav1dImage->p.h;
image->depth = dav1dImage->p.bpc;
avifImageFreePlanes(image, AVIF_PLANES_A);
image->alphaPlane = dav1dImage->data[0];
image->alphaRowBytes = (uint32_t)dav1dImage->stride[0];
image->alphaRange = codec->internal->colorRange;
image->imageOwnsAlphaPlane = AVIF_FALSE;
}
return AVIF_TRUE;
}
const char * avifCodecVersionDav1d(void)
{
return dav1d_version();
}
avifCodec * avifCodecCreateDav1d(void)
{
avifCodec * codec = (avifCodec *)avifAlloc(sizeof(avifCodec));
memset(codec, 0, sizeof(struct avifCodec));
codec->open = dav1dCodecOpen;
codec->getNextImage = dav1dCodecGetNextImage;
codec->destroyInternal = dav1dCodecDestroyInternal;
codec->internal = (struct avifCodecInternal *)avifAlloc(sizeof(struct avifCodecInternal));
memset(codec->internal, 0, sizeof(struct avifCodecInternal));
dav1d_default_settings(&codec->internal->dav1dSettings);
// Set a maximum frame size limit to avoid OOM'ing fuzzers.
codec->internal->dav1dSettings.frame_size_limit = AVIF_MAX_IMAGE_SIZE;
// Ensure that we only get the "highest spatial layer" as a single frame
// for each input sample, instead of getting each spatial layer as its own
// frame one at a time ("all layers").
codec->internal->dav1dSettings.all_layers = 0;
return codec;
}