]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libdav1d.c
avcodec/libdav1d: export level from the Sequence Header
[ffmpeg] / libavcodec / libdav1d.c
index 9b697a8197b52178ed51e0233070e02d4ccca11c..541d56dac895c2d5271e1f67a508ebc42d5ec751 100644 (file)
@@ -22,6 +22,7 @@
 #include <dav1d/dav1d.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/mastering_display_metadata.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
 
@@ -37,6 +38,7 @@ typedef struct Libdav1dContext {
 
     Dav1dData data;
     int tile_threads;
+    int frame_threads;
     int apply_grain;
 } Libdav1dContext;
 
@@ -71,8 +73,10 @@ static int libdav1d_picture_allocator(Dav1dPicture *p, void *cookie)
         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)
+        if (!dav1d->pool) {
+            dav1d->pool_size = 0;
             return AVERROR(ENOMEM);
+        }
         dav1d->pool_size = ret;
     }
     buf = av_buffer_pool_get(dav1d->pool);
@@ -111,6 +115,7 @@ static av_cold int libdav1d_init(AVCodecContext *c)
 {
     Libdav1dContext *dav1d = c->priv_data;
     Dav1dSettings s;
+    int threads = (c->thread_count ? c->thread_count : av_cpu_count()) * 3 / 2;
     int res;
 
     av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
@@ -121,9 +126,16 @@ static av_cold int libdav1d_init(AVCodecContext *c)
     s.allocator.cookie = dav1d;
     s.allocator.alloc_picture_callback = libdav1d_picture_allocator;
     s.allocator.release_picture_callback = libdav1d_picture_release;
-    s.n_tile_threads = dav1d->tile_threads;
     s.apply_grain = dav1d->apply_grain;
-    s.n_frame_threads = FFMIN(c->thread_count ? c->thread_count : av_cpu_count(), DAV1D_MAX_FRAME_THREADS);
+
+    s.n_tile_threads = dav1d->tile_threads
+                     ? dav1d->tile_threads
+                     : FFMIN(floor(sqrt(threads)), DAV1D_MAX_TILE_THREADS);
+    s.n_frame_threads = dav1d->frame_threads
+                      ? dav1d->frame_threads
+                      : FFMIN(ceil(threads / s.n_tile_threads), DAV1D_MAX_FRAME_THREADS);
+    av_log(c, AV_LOG_DEBUG, "Using %d frame threads, %d tile threads\n",
+           s.n_frame_threads, s.n_tile_threads);
 
     res = dav1d_open(&dav1d->c, &s);
     if (res < 0)
@@ -146,18 +158,11 @@ static void libdav1d_data_free(const uint8_t *data, void *opaque) {
     av_buffer_unref(&buf);
 }
 
-static void libdav1d_frame_free(void *opaque, uint8_t *data) {
-    Dav1dPicture *p = opaque;
-
-    dav1d_picture_unref(p);
-    av_free(p);
-}
-
 static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
 {
     Libdav1dContext *dav1d = c->priv_data;
     Dav1dData *data = &dav1d->data;
-    Dav1dPicture *p;
+    Dav1dPicture pic = { 0 }, *p = &pic;
     int res;
 
     if (!data->sz) {
@@ -185,34 +190,28 @@ static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
 
     res = dav1d_send_data(dav1d->c, data);
     if (res < 0) {
-        if (res == -EINVAL)
+        if (res == AVERROR(EINVAL))
             res = AVERROR_INVALIDDATA;
-        if (res != -EAGAIN)
+        if (res != AVERROR(EAGAIN))
             return res;
     }
 
-    p = av_mallocz(sizeof(*p));
-    if (!p)
-        return AVERROR(ENOMEM);
-
     res = dav1d_get_picture(dav1d->c, p);
     if (res < 0) {
-        if (res == -EINVAL)
+        if (res == AVERROR(EINVAL))
             res = AVERROR_INVALIDDATA;
-        else if (res == -EAGAIN && c->internal->draining)
+        else if (res == AVERROR(EAGAIN) && c->internal->draining)
             res = AVERROR_EOF;
 
-        av_free(p);
         return res;
     }
 
     av_assert0(p->data[0] != NULL);
 
-    frame->buf[0] = av_buffer_create(NULL, 0, libdav1d_frame_free,
-                                     p, AV_BUFFER_FLAG_READONLY);
+    // This requires the custom allocator above
+    frame->buf[0] = av_buffer_ref(p->allocator_data);
     if (!frame->buf[0]) {
         dav1d_picture_unref(p);
-        av_free(p);
         return AVERROR(ENOMEM);
     }
 
@@ -224,13 +223,15 @@ static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
     frame->linesize[2] = p->stride[1];
 
     c->profile = p->seq_hdr->profile;
+    c->level = ((p->seq_hdr->operating_points[0].major_level - 2) << 2)
+               | p->seq_hdr->operating_points[0].minor_level;
     frame->format = c->pix_fmt = pix_fmt[p->p.layout][p->seq_hdr->hbd];
     frame->width = p->p.w;
     frame->height = p->p.h;
     if (c->width != p->p.w || c->height != p->p.h) {
         res = ff_set_dimensions(c, p->p.w, p->p.h);
         if (res < 0)
-            return res;
+            goto fail;
     }
 
     switch (p->seq_hdr->chr) {
@@ -271,10 +272,46 @@ FF_ENABLE_DEPRECATION_WARNINGS
         frame->pict_type = AV_PICTURE_TYPE_SP;
         break;
     default:
-        return AVERROR_INVALIDDATA;
+        res = AVERROR_INVALIDDATA;
+        goto fail;
     }
 
-    return 0;
+    if (p->mastering_display) {
+        AVMasteringDisplayMetadata *mastering = av_mastering_display_metadata_create_side_data(frame);
+        if (!mastering) {
+            res = AVERROR(ENOMEM);
+            goto fail;
+        }
+
+        for (int i = 0; i < 3; i++) {
+            mastering->display_primaries[i][0] = av_make_q(p->mastering_display->primaries[i][0], 1 << 16);
+            mastering->display_primaries[i][1] = av_make_q(p->mastering_display->primaries[i][1], 1 << 16);
+        }
+        mastering->white_point[0] = av_make_q(p->mastering_display->white_point[0], 1 << 16);
+        mastering->white_point[1] = av_make_q(p->mastering_display->white_point[1], 1 << 16);
+
+        mastering->max_luminance = av_make_q(p->mastering_display->max_luminance, 1 << 8);
+        mastering->min_luminance = av_make_q(p->mastering_display->min_luminance, 1 << 14);
+
+        mastering->has_primaries = 1;
+        mastering->has_luminance = 1;
+    }
+    if (p->content_light) {
+        AVContentLightMetadata *light = av_content_light_metadata_create_side_data(frame);
+        if (!light) {
+            res = AVERROR(ENOMEM);
+            goto fail;
+        }
+        light->MaxCLL = p->content_light->max_content_light_level;
+        light->MaxFALL = p->content_light->max_frame_average_light_level;
+    }
+
+    res = 0;
+fail:
+    dav1d_picture_unref(p);
+    if (res < 0)
+        av_frame_unref(frame);
+    return res;
 }
 
 static av_cold int libdav1d_close(AVCodecContext *c)
@@ -291,7 +328,8 @@ static av_cold int libdav1d_close(AVCodecContext *c)
 #define OFFSET(x) offsetof(Libdav1dContext, x)
 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 static const AVOption libdav1d_options[] = {
-    { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, DAV1D_MAX_TILE_THREADS, VD },
+    { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_TILE_THREADS, VD },
+    { "framethreads", "Frame threads", OFFSET(frame_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_FRAME_THREADS, VD },
     { "filmgrain", "Apply Film Grain", OFFSET(apply_grain), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VD },
     { NULL }
 };