+static const enum AVPixelFormat pix_fmt[][3] = {
+ [DAV1D_PIXEL_LAYOUT_I400] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12 },
+ [DAV1D_PIXEL_LAYOUT_I420] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12 },
+ [DAV1D_PIXEL_LAYOUT_I422] = { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12 },
+ [DAV1D_PIXEL_LAYOUT_I444] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12 },
+};
+
+static const enum AVPixelFormat pix_fmt_rgb[3] = {
+ AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
+};
+
+static void libdav1d_log_callback(void *opaque, const char *fmt, va_list vl)
+{
+ AVCodecContext *c = opaque;
+
+ av_vlog(c, AV_LOG_ERROR, fmt, vl);
+}
+
+static int libdav1d_picture_allocator(Dav1dPicture *p, void *cookie)
+{
+ Libdav1dContext *dav1d = cookie;
+ enum AVPixelFormat format = pix_fmt[p->p.layout][p->seq_hdr->hbd];
+ int ret, linesize[4], h = FFALIGN(p->p.h, 128), w = FFALIGN(p->p.w, 128);
+ uint8_t *aligned_ptr, *data[4];
+ AVBufferRef *buf;
+
+ ret = av_image_get_buffer_size(format, w, h, DAV1D_PICTURE_ALIGNMENT);
+ if (ret < 0)
+ return ret;
+
+ if (ret != dav1d->pool_size) {
+ av_buffer_pool_uninit(&dav1d->pool);
+ // Use twice the amount of required padding bytes for aligned_ptr below.
+ dav1d->pool = av_buffer_pool_init(ret + DAV1D_PICTURE_ALIGNMENT * 2, NULL);
+ if (!dav1d->pool) {
+ dav1d->pool_size = 0;
+ return AVERROR(ENOMEM);
+ }
+ dav1d->pool_size = ret;
+ }
+ buf = av_buffer_pool_get(dav1d->pool);
+ if (!buf)
+ return AVERROR(ENOMEM);
+
+ // libdav1d requires DAV1D_PICTURE_ALIGNMENT aligned buffers, which av_malloc()
+ // doesn't guarantee for example when AVX is disabled at configure time.
+ // Use the extra DAV1D_PICTURE_ALIGNMENT padding bytes in the buffer to align it
+ // if required.
+ aligned_ptr = (uint8_t *)FFALIGN((uintptr_t)buf->data, DAV1D_PICTURE_ALIGNMENT);
+ ret = av_image_fill_arrays(data, linesize, aligned_ptr, format, w, h,
+ DAV1D_PICTURE_ALIGNMENT);
+ if (ret < 0) {
+ av_buffer_unref(&buf);
+ return ret;
+ }
+
+ p->data[0] = data[0];
+ p->data[1] = data[1];
+ p->data[2] = data[2];
+ p->stride[0] = linesize[0];
+ p->stride[1] = linesize[1];
+ p->allocator_data = buf;
+
+ return 0;
+}
+
+static void libdav1d_picture_release(Dav1dPicture *p, void *cookie)
+{
+ AVBufferRef *buf = p->allocator_data;
+
+ av_buffer_unref(&buf);
+}
+