]> git.sesse.net Git - ffmpeg/blobdiff - libavdevice/kmsgrab.c
lavf/mux: rewrite guessing the packet duration
[ffmpeg] / libavdevice / kmsgrab.c
index 8e8c7de54981d143db5ebf7a15d9adf5036a61c0..a0aa9dc22f524882d43b7122eebb104c1872bf80 100644 (file)
 #include <xf86drm.h>
 #include <xf86drmMode.h>
 
+// Required for compatibility when building against libdrm < 2.4.83.
+#ifndef DRM_FORMAT_MOD_INVALID
+#define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
+#endif
+
 #include "libavutil/hwcontext.h"
 #include "libavutil/hwcontext_drm.h"
 #include "libavutil/internal.h"
@@ -45,6 +50,7 @@ typedef struct KMSGrabContext {
     AVBufferRef *device_ref;
     AVHWDeviceContext *device;
     AVDRMDeviceContext *hwctx;
+    int fb2_available;
 
     AVBufferRef *frames_ref;
     AVHWFramesContext *frames;
@@ -68,8 +74,10 @@ typedef struct KMSGrabContext {
 static void kmsgrab_free_desc(void *opaque, uint8_t *data)
 {
     AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)data;
+    int i;
 
-    close(desc->objects[0].fd);
+    for (i = 0; i < desc->nb_objects; i++)
+        close(desc->objects[i].fd);
 
     av_free(desc);
 }
@@ -144,6 +152,116 @@ fail:
     return err;
 }
 
+#if HAVE_LIBDRM_GETFB2
+static int kmsgrab_get_fb2(AVFormatContext *avctx,
+                           drmModePlane *plane,
+                           AVDRMFrameDescriptor *desc)
+{
+    KMSGrabContext *ctx = avctx->priv_data;
+    drmModeFB2 *fb;
+    int err, i, nb_objects;
+
+    fb = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
+    if (!fb) {
+        err = errno;
+        av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
+               "%"PRIu32": %s.\n", plane->fb_id, strerror(err));
+        return AVERROR(err);
+    }
+    if (fb->pixel_format != ctx->drm_format) {
+        av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
+               "format changed: now %"PRIx32".\n",
+               ctx->plane_id, fb->pixel_format);
+        err = AVERROR(EIO);
+        goto fail;
+    }
+    if (fb->modifier != ctx->drm_format_modifier) {
+        av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
+               "format modifier changed: now %"PRIx64".\n",
+               ctx->plane_id, fb->modifier);
+        err = AVERROR(EIO);
+        goto fail;
+    }
+    if (fb->width != ctx->width || fb->height != ctx->height) {
+        av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
+               "dimensions changed: now %"PRIu32"x%"PRIu32".\n",
+               ctx->plane_id, fb->width, fb->height);
+        err = AVERROR(EIO);
+        goto fail;
+    }
+    if (!fb->handles[0]) {
+        av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
+        err = AVERROR(EIO);
+        goto fail;
+    }
+
+    *desc = (AVDRMFrameDescriptor) {
+        .nb_layers = 1,
+        .layers[0] = {
+            .format = ctx->drm_format,
+        },
+    };
+
+    nb_objects = 0;
+    for (i = 0; i < 4 && fb->handles[i]; i++) {
+        size_t size;
+        int dup = 0, j, obj;
+
+        size = fb->offsets[i] + fb->height * fb->pitches[i];
+
+        for (j = 0; j < i; j++) {
+            if (fb->handles[i] == fb->handles[j]) {
+                dup = 1;
+                break;
+            }
+        }
+        if (dup) {
+            obj = desc->layers[0].planes[j].object_index;
+
+            if (desc->objects[j].size < size)
+                desc->objects[j].size = size;
+
+            desc->layers[0].planes[i] = (AVDRMPlaneDescriptor) {
+                .object_index = obj,
+                .offset       = fb->offsets[i],
+                .pitch        = fb->pitches[i],
+            };
+
+        } else {
+            int fd;
+            err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handles[i],
+                                     O_RDONLY, &fd);
+            if (err < 0) {
+                err = errno;
+                av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
+                       "framebuffer handle: %s.\n", strerror(err));
+                err = AVERROR(err);
+                goto fail;
+            }
+
+            obj = nb_objects++;
+            desc->objects[obj] = (AVDRMObjectDescriptor) {
+                .fd              = fd,
+                .size            = size,
+                .format_modifier = fb->modifier,
+            };
+            desc->layers[0].planes[i] = (AVDRMPlaneDescriptor) {
+                .object_index = obj,
+                .offset       = fb->offsets[i],
+                .pitch        = fb->pitches[i],
+            };
+        }
+    }
+    desc->nb_objects = nb_objects;
+    desc->layers[0].nb_planes = i;
+
+    err = 0;
+fail:
+    drmModeFreeFB2(fb);
+    return err;
+}
+#endif
+
 static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
 {
     KMSGrabContext *ctx = avctx->priv_data;
@@ -187,7 +305,12 @@ static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
         goto fail;
     }
 
-    err = kmsgrab_get_fb(avctx, plane, desc);
+#if HAVE_LIBDRM_GETFB2
+    if (ctx->fb2_available)
+        err = kmsgrab_get_fb2(avctx, plane, desc);
+    else
+#endif
+        err = kmsgrab_get_fb(avctx, plane, desc);
     if (err < 0)
         goto fail;
 
@@ -244,6 +367,7 @@ static const struct {
     enum AVPixelFormat pixfmt;
     uint32_t drm_format;
 } kmsgrab_formats[] = {
+    // Monochrome.
 #ifdef DRM_FORMAT_R8
     { AV_PIX_FMT_GRAY8,    DRM_FORMAT_R8       },
 #endif
@@ -251,6 +375,7 @@ static const struct {
     { AV_PIX_FMT_GRAY16LE, DRM_FORMAT_R16      },
     { AV_PIX_FMT_GRAY16BE, DRM_FORMAT_R16      | DRM_FORMAT_BIG_ENDIAN },
 #endif
+    // <8-bit RGB.
     { AV_PIX_FMT_BGR8,     DRM_FORMAT_BGR233   },
     { AV_PIX_FMT_RGB555LE, DRM_FORMAT_XRGB1555 },
     { AV_PIX_FMT_RGB555BE, DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
@@ -260,6 +385,7 @@ static const struct {
     { AV_PIX_FMT_RGB565BE, DRM_FORMAT_RGB565   | DRM_FORMAT_BIG_ENDIAN },
     { AV_PIX_FMT_BGR565LE, DRM_FORMAT_BGR565   },
     { AV_PIX_FMT_BGR565BE, DRM_FORMAT_BGR565   | DRM_FORMAT_BIG_ENDIAN },
+    // 8-bit RGB.
     { AV_PIX_FMT_RGB24,    DRM_FORMAT_RGB888   },
     { AV_PIX_FMT_BGR24,    DRM_FORMAT_BGR888   },
     { AV_PIX_FMT_0RGB,     DRM_FORMAT_BGRX8888 },
@@ -270,6 +396,12 @@ static const struct {
     { AV_PIX_FMT_ABGR,     DRM_FORMAT_RGBA8888 },
     { AV_PIX_FMT_RGBA,     DRM_FORMAT_ABGR8888 },
     { AV_PIX_FMT_BGRA,     DRM_FORMAT_ARGB8888 },
+    // 10-bit RGB.
+    { AV_PIX_FMT_X2RGB10LE, DRM_FORMAT_XRGB2101010 },
+    { AV_PIX_FMT_X2RGB10BE, DRM_FORMAT_XRGB2101010 | DRM_FORMAT_BIG_ENDIAN },
+    // 8-bit YUV 4:2:0.
+    { AV_PIX_FMT_NV12,     DRM_FORMAT_NV12     },
+    // 8-bit YUV 4:2:2.
     { AV_PIX_FMT_YUYV422,  DRM_FORMAT_YUYV     },
     { AV_PIX_FMT_YVYU422,  DRM_FORMAT_YVYU     },
     { AV_PIX_FMT_UYVY422,  DRM_FORMAT_UYVY     },
@@ -281,21 +413,12 @@ static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
     drmModePlaneRes *plane_res = NULL;
     drmModePlane *plane = NULL;
     drmModeFB *fb = NULL;
+#if HAVE_LIBDRM_GETFB2
+    drmModeFB2 *fb2 = NULL;
+#endif
     AVStream *stream;
     int err, i;
 
-    for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
-        if (kmsgrab_formats[i].pixfmt == ctx->format) {
-            ctx->drm_format = kmsgrab_formats[i].drm_format;
-            break;
-        }
-    }
-    if (i >= FF_ARRAY_ELEMS(kmsgrab_formats)) {
-        av_log(avctx, AV_LOG_ERROR, "Unsupported format %s.\n",
-               av_get_pix_fmt_name(ctx->format));
-        return AVERROR(EINVAL);
-    }
-
     err = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_DRM,
                                  ctx->device_path, NULL, 0);
     if (err < 0) {
@@ -383,28 +506,116 @@ static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
 
     ctx->plane_id = plane->plane_id;
 
-    fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
-    if (!fb) {
+#if HAVE_LIBDRM_GETFB2
+    fb2 = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
+    if (!fb2 && errno == ENOSYS) {
+        av_log(avctx, AV_LOG_INFO, "GETFB2 not supported, "
+               "will try to use GETFB instead.\n");
+    } else if (!fb2) {
         err = errno;
         av_log(avctx, AV_LOG_ERROR, "Failed to get "
                "framebuffer %"PRIu32": %s.\n",
                plane->fb_id, strerror(err));
         err = AVERROR(err);
         goto fail;
+    } else {
+        av_log(avctx, AV_LOG_INFO, "Template framebuffer is "
+               "%"PRIu32": %"PRIu32"x%"PRIu32" "
+               "format %"PRIx32" modifier %"PRIx64" flags %"PRIx32".\n",
+               fb2->fb_id, fb2->width, fb2->height,
+               fb2->pixel_format, fb2->modifier, fb2->flags);
+
+        ctx->width  = fb2->width;
+        ctx->height = fb2->height;
+
+        if (!fb2->handles[0]) {
+            av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
+                   "maybe you need some additional capabilities?\n");
+            err = AVERROR(EINVAL);
+            goto fail;
+        }
+
+        for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
+            if (kmsgrab_formats[i].drm_format == fb2->pixel_format) {
+                if (ctx->format != AV_PIX_FMT_NONE &&
+                    ctx->format != kmsgrab_formats[i].pixfmt) {
+                    av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
+                           "%"PRIx32" does not match expected format.\n",
+                           fb2->pixel_format);
+                    err = AVERROR(EINVAL);
+                    goto fail;
+                }
+                ctx->drm_format = fb2->pixel_format;
+                ctx->format     = kmsgrab_formats[i].pixfmt;
+                break;
+            }
+        }
+        if (i == FF_ARRAY_ELEMS(kmsgrab_formats)) {
+            av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
+                   "%"PRIx32" is not a known supported format.\n",
+                   fb2->pixel_format);
+            err = AVERROR(EINVAL);
+            goto fail;
+        }
+        if (ctx->drm_format_modifier != DRM_FORMAT_MOD_INVALID &&
+            ctx->drm_format_modifier != fb2->modifier) {
+            av_log(avctx, AV_LOG_ERROR, "Framebuffer format modifier "
+                   "%"PRIx64" does not match expected modifier.\n",
+                   fb2->modifier);
+            err = AVERROR(EINVAL);
+            goto fail;
+        } else {
+            ctx->drm_format_modifier = fb2->modifier;
+        }
+        av_log(avctx, AV_LOG_VERBOSE, "Format is %s, from "
+               "DRM format %"PRIx32" modifier %"PRIx64".\n",
+               av_get_pix_fmt_name(ctx->format),
+               ctx->drm_format, ctx->drm_format_modifier);
+
+        ctx->fb2_available = 1;
     }
+#endif
 
-    av_log(avctx, AV_LOG_INFO, "Template framebuffer is %"PRIu32": "
-           "%"PRIu32"x%"PRIu32" %"PRIu32"bpp %"PRIu32"b depth.\n",
-           fb->fb_id, fb->width, fb->height, fb->bpp, fb->depth);
+    if (!ctx->fb2_available) {
+        if (ctx->format == AV_PIX_FMT_NONE) {
+            // Backward compatibility: assume BGR0 if no format supplied.
+            ctx->format = AV_PIX_FMT_BGR0;
+        }
+        for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
+            if (kmsgrab_formats[i].pixfmt == ctx->format) {
+                ctx->drm_format = kmsgrab_formats[i].drm_format;
+                break;
+            }
+        }
+        if (i >= FF_ARRAY_ELEMS(kmsgrab_formats)) {
+            av_log(avctx, AV_LOG_ERROR, "Unsupported format %s.\n",
+                   av_get_pix_fmt_name(ctx->format));
+            return AVERROR(EINVAL);
+        }
 
-    ctx->width  = fb->width;
-    ctx->height = fb->height;
+        fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
+        if (!fb) {
+            err = errno;
+            av_log(avctx, AV_LOG_ERROR, "Failed to get "
+                   "framebuffer %"PRIu32": %s.\n",
+                   plane->fb_id, strerror(err));
+            err = AVERROR(err);
+            goto fail;
+        }
 
-    if (!fb->handle) {
-        av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
-               "maybe you need some additional capabilities?\n");
-        err = AVERROR(EINVAL);
-        goto fail;
+        av_log(avctx, AV_LOG_INFO, "Template framebuffer is %"PRIu32": "
+               "%"PRIu32"x%"PRIu32" %"PRIu32"bpp %"PRIu32"b depth.\n",
+               fb->fb_id, fb->width, fb->height, fb->bpp, fb->depth);
+
+        ctx->width  = fb->width;
+        ctx->height = fb->height;
+
+        if (!fb->handle) {
+            av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
+                   "maybe you need some additional capabilities?\n");
+            err = AVERROR(EINVAL);
+            goto fail;
+        }
     }
 
     stream = avformat_new_stream(avctx, NULL);
@@ -415,8 +626,8 @@ static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
 
     stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     stream->codecpar->codec_id   = AV_CODEC_ID_WRAPPED_AVFRAME;
-    stream->codecpar->width      = fb->width;
-    stream->codecpar->height     = fb->height;
+    stream->codecpar->width      = ctx->width;
+    stream->codecpar->height     = ctx->height;
     stream->codecpar->format     = AV_PIX_FMT_DRM_PRIME;
 
     avpriv_set_pts_info(stream, 64, 1, 1000000);
@@ -430,8 +641,8 @@ static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
 
     ctx->frames->format    = AV_PIX_FMT_DRM_PRIME;
     ctx->frames->sw_format = ctx->format,
-    ctx->frames->width     = fb->width;
-    ctx->frames->height    = fb->height;
+    ctx->frames->width     = ctx->width;
+    ctx->frames->height    = ctx->height;
 
     err = av_hwframe_ctx_init(ctx->frames_ref);
     if (err < 0) {
@@ -448,6 +659,9 @@ fail:
     drmModeFreePlaneResources(plane_res);
     drmModeFreePlane(plane);
     drmModeFreeFB(fb);
+#if HAVE_LIBDRM_GETFB2
+    drmModeFreeFB2(fb2);
+#endif
     return err;
 }
 
@@ -469,10 +683,10 @@ static const AVOption options[] = {
       { .str = "/dev/dri/card0" }, 0, 0, FLAGS },
     { "format", "Pixel format for framebuffer",
       OFFSET(format), AV_OPT_TYPE_PIXEL_FMT,
-      { .i64 = AV_PIX_FMT_BGR0 }, 0, UINT32_MAX, FLAGS },
+      { .i64 = AV_PIX_FMT_NONE }, -1, INT32_MAX, FLAGS },
     { "format_modifier", "DRM format modifier for framebuffer",
       OFFSET(drm_format_modifier), AV_OPT_TYPE_INT64,
-      { .i64 = DRM_FORMAT_MOD_NONE }, 0, INT64_MAX, FLAGS },
+      { .i64 = DRM_FORMAT_MOD_INVALID }, 0, INT64_MAX, FLAGS },
     { "crtc_id", "CRTC ID to define capture source",
       OFFSET(source_crtc), AV_OPT_TYPE_INT64,
       { .i64 = 0 }, 0, UINT32_MAX, FLAGS },