4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <drm_fourcc.h>
28 #include <xf86drmMode.h>
30 // Required for compatibility when building against libdrm < 2.4.83.
31 #ifndef DRM_FORMAT_MOD_INVALID
32 #define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
35 #include "libavutil/hwcontext.h"
36 #include "libavutil/hwcontext_drm.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/pixfmt.h"
41 #include "libavutil/pixdesc.h"
42 #include "libavutil/time.h"
44 #include "libavformat/avformat.h"
45 #include "libavformat/internal.h"
47 typedef struct KMSGrabContext {
50 AVBufferRef *device_ref;
51 AVHWDeviceContext *device;
52 AVDRMDeviceContext *hwctx;
55 AVBufferRef *frames_ref;
56 AVHWFramesContext *frames;
66 const char *device_path;
67 enum AVPixelFormat format;
68 int64_t drm_format_modifier;
74 static void kmsgrab_free_desc(void *opaque, uint8_t *data)
76 AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)data;
79 for (i = 0; i < desc->nb_objects; i++)
80 close(desc->objects[i].fd);
85 static void kmsgrab_free_frame(void *opaque, uint8_t *data)
87 AVFrame *frame = (AVFrame*)data;
89 av_frame_free(&frame);
92 static int kmsgrab_get_fb(AVFormatContext *avctx,
94 AVDRMFrameDescriptor *desc)
96 KMSGrabContext *ctx = avctx->priv_data;
100 fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
103 av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
104 "%"PRIu32": %s.\n", plane->fb_id, strerror(err));
108 if (fb->width != ctx->width || fb->height != ctx->height) {
109 av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
110 "dimensions changed: now %"PRIu32"x%"PRIu32".\n",
111 ctx->plane_id, fb->width, fb->height);
116 av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
121 err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handle, O_RDONLY, &fd);
124 av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
125 "framebuffer handle: %s.\n", strerror(err));
130 *desc = (AVDRMFrameDescriptor) {
134 .size = fb->height * fb->pitch,
135 .format_modifier = ctx->drm_format_modifier,
139 .format = ctx->drm_format,
155 #if HAVE_LIBDRM_GETFB2
156 static int kmsgrab_get_fb2(AVFormatContext *avctx,
158 AVDRMFrameDescriptor *desc)
160 KMSGrabContext *ctx = avctx->priv_data;
162 int err, i, nb_objects;
163 uint64_t modifier = ctx->drm_format_modifier;
165 fb = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
168 av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
169 "%"PRIu32": %s.\n", plane->fb_id, strerror(err));
172 if (fb->pixel_format != ctx->drm_format) {
173 av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
174 "format changed: now %"PRIx32".\n",
175 ctx->plane_id, fb->pixel_format);
179 if (fb->width != ctx->width || fb->height != ctx->height) {
180 av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
181 "dimensions changed: now %"PRIu32"x%"PRIu32".\n",
182 ctx->plane_id, fb->width, fb->height);
186 if (!fb->handles[0]) {
187 av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
192 if (fb->flags & DRM_MODE_FB_MODIFIERS)
193 modifier = fb->modifier;
195 *desc = (AVDRMFrameDescriptor) {
198 .format = ctx->drm_format,
203 for (i = 0; i < 4 && fb->handles[i]; i++) {
207 size = fb->offsets[i] + fb->height * fb->pitches[i];
209 for (j = 0; j < i; j++) {
210 if (fb->handles[i] == fb->handles[j]) {
216 obj = desc->layers[0].planes[j].object_index;
218 if (desc->objects[j].size < size)
219 desc->objects[j].size = size;
221 desc->layers[0].planes[i] = (AVDRMPlaneDescriptor) {
223 .offset = fb->offsets[i],
224 .pitch = fb->pitches[i],
229 err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handles[i],
233 av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
234 "framebuffer handle: %s.\n", strerror(err));
240 desc->objects[obj] = (AVDRMObjectDescriptor) {
243 .format_modifier = modifier,
245 desc->layers[0].planes[i] = (AVDRMPlaneDescriptor) {
247 .offset = fb->offsets[i],
248 .pitch = fb->pitches[i],
252 desc->nb_objects = nb_objects;
253 desc->layers[0].nb_planes = i;
262 static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
264 KMSGrabContext *ctx = avctx->priv_data;
265 drmModePlane *plane = NULL;
266 AVDRMFrameDescriptor *desc = NULL;
267 AVFrame *frame = NULL;
271 now = av_gettime_relative();
272 if (ctx->frame_last) {
275 delay = ctx->frame_last + ctx->frame_delay - now;
279 now = av_gettime_relative();
282 ctx->frame_last = now;
285 plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
288 av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
289 "%"PRIu32": %s.\n", ctx->plane_id, strerror(err));
294 av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" no longer has "
295 "an associated framebuffer.\n", ctx->plane_id);
300 desc = av_mallocz(sizeof(*desc));
302 err = AVERROR(ENOMEM);
306 #if HAVE_LIBDRM_GETFB2
307 if (ctx->fb2_available)
308 err = kmsgrab_get_fb2(avctx, plane, desc);
311 err = kmsgrab_get_fb(avctx, plane, desc);
315 frame = av_frame_alloc();
317 err = AVERROR(ENOMEM);
321 frame->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
322 if (!frame->hw_frames_ctx) {
323 err = AVERROR(ENOMEM);
327 frame->buf[0] = av_buffer_create((uint8_t*)desc, sizeof(*desc),
328 &kmsgrab_free_desc, avctx, 0);
329 if (!frame->buf[0]) {
330 err = AVERROR(ENOMEM);
334 frame->data[0] = (uint8_t*)desc;
335 frame->format = AV_PIX_FMT_DRM_PRIME;
336 frame->width = ctx->width;
337 frame->height = ctx->height;
339 drmModeFreePlane(plane);
343 pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
344 &kmsgrab_free_frame, avctx, 0);
346 err = AVERROR(ENOMEM);
350 pkt->data = (uint8_t*)frame;
351 pkt->size = sizeof(*frame);
353 pkt->flags |= AV_PKT_FLAG_TRUSTED;
358 drmModeFreePlane(plane);
360 av_frame_free(&frame);
364 static const struct {
365 enum AVPixelFormat pixfmt;
367 } kmsgrab_formats[] = {
370 { AV_PIX_FMT_GRAY8, DRM_FORMAT_R8 },
372 #ifdef DRM_FORMAT_R16
373 { AV_PIX_FMT_GRAY16LE, DRM_FORMAT_R16 },
374 { AV_PIX_FMT_GRAY16BE, DRM_FORMAT_R16 | DRM_FORMAT_BIG_ENDIAN },
377 { AV_PIX_FMT_BGR8, DRM_FORMAT_BGR233 },
378 { AV_PIX_FMT_RGB555LE, DRM_FORMAT_XRGB1555 },
379 { AV_PIX_FMT_RGB555BE, DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
380 { AV_PIX_FMT_BGR555LE, DRM_FORMAT_XBGR1555 },
381 { AV_PIX_FMT_BGR555BE, DRM_FORMAT_XBGR1555 | DRM_FORMAT_BIG_ENDIAN },
382 { AV_PIX_FMT_RGB565LE, DRM_FORMAT_RGB565 },
383 { AV_PIX_FMT_RGB565BE, DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN },
384 { AV_PIX_FMT_BGR565LE, DRM_FORMAT_BGR565 },
385 { AV_PIX_FMT_BGR565BE, DRM_FORMAT_BGR565 | DRM_FORMAT_BIG_ENDIAN },
387 { AV_PIX_FMT_RGB24, DRM_FORMAT_RGB888 },
388 { AV_PIX_FMT_BGR24, DRM_FORMAT_BGR888 },
389 { AV_PIX_FMT_0RGB, DRM_FORMAT_BGRX8888 },
390 { AV_PIX_FMT_0BGR, DRM_FORMAT_RGBX8888 },
391 { AV_PIX_FMT_RGB0, DRM_FORMAT_XBGR8888 },
392 { AV_PIX_FMT_BGR0, DRM_FORMAT_XRGB8888 },
393 { AV_PIX_FMT_ARGB, DRM_FORMAT_BGRA8888 },
394 { AV_PIX_FMT_ABGR, DRM_FORMAT_RGBA8888 },
395 { AV_PIX_FMT_RGBA, DRM_FORMAT_ABGR8888 },
396 { AV_PIX_FMT_BGRA, DRM_FORMAT_ARGB8888 },
398 { AV_PIX_FMT_X2RGB10LE, DRM_FORMAT_XRGB2101010 },
399 { AV_PIX_FMT_X2RGB10BE, DRM_FORMAT_XRGB2101010 | DRM_FORMAT_BIG_ENDIAN },
401 { AV_PIX_FMT_NV12, DRM_FORMAT_NV12 },
403 { AV_PIX_FMT_YUYV422, DRM_FORMAT_YUYV },
404 { AV_PIX_FMT_YVYU422, DRM_FORMAT_YVYU },
405 { AV_PIX_FMT_UYVY422, DRM_FORMAT_UYVY },
408 static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
410 KMSGrabContext *ctx = avctx->priv_data;
411 drmModePlaneRes *plane_res = NULL;
412 drmModePlane *plane = NULL;
413 drmModeFB *fb = NULL;
414 #if HAVE_LIBDRM_GETFB2
415 drmModeFB2 *fb2 = NULL;
420 err = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_DRM,
421 ctx->device_path, NULL, 0);
423 av_log(avctx, AV_LOG_ERROR, "Failed to open DRM device.\n");
426 ctx->device = (AVHWDeviceContext*) ctx->device_ref->data;
427 ctx->hwctx = (AVDRMDeviceContext*)ctx->device->hwctx;
429 err = drmSetClientCap(ctx->hwctx->fd,
430 DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
432 av_log(avctx, AV_LOG_WARNING, "Failed to set universal planes "
433 "capability: primary planes will not be usable.\n");
436 if (ctx->source_plane > 0) {
437 plane = drmModeGetPlane(ctx->hwctx->fd, ctx->source_plane);
440 av_log(avctx, AV_LOG_ERROR, "Failed to get plane %"PRId64": "
441 "%s.\n", ctx->source_plane, strerror(err));
446 if (plane->fb_id == 0) {
447 av_log(avctx, AV_LOG_ERROR, "Plane %"PRId64" does not have "
448 "an attached framebuffer.\n", ctx->source_plane);
449 err = AVERROR(EINVAL);
453 plane_res = drmModeGetPlaneResources(ctx->hwctx->fd);
456 av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
457 "resources: %s.\n", strerror(err));
462 for (i = 0; i < plane_res->count_planes; i++) {
463 plane = drmModeGetPlane(ctx->hwctx->fd,
464 plane_res->planes[i]);
467 av_log(avctx, AV_LOG_VERBOSE, "Failed to get "
468 "plane %"PRIu32": %s.\n",
469 plane_res->planes[i], strerror(err));
473 av_log(avctx, AV_LOG_DEBUG, "Plane %"PRIu32": "
474 "CRTC %"PRIu32" FB %"PRIu32".\n",
475 plane->plane_id, plane->crtc_id, plane->fb_id);
477 if ((ctx->source_crtc > 0 &&
478 plane->crtc_id != ctx->source_crtc) ||
480 // Either not connected to the target source CRTC
482 drmModeFreePlane(plane);
490 if (i == plane_res->count_planes) {
491 if (ctx->source_crtc > 0) {
492 av_log(avctx, AV_LOG_ERROR, "No usable planes found on "
493 "CRTC %"PRId64".\n", ctx->source_crtc);
495 av_log(avctx, AV_LOG_ERROR, "No usable planes found.\n");
497 err = AVERROR(EINVAL);
501 av_log(avctx, AV_LOG_INFO, "Using plane %"PRIu32" to "
502 "locate framebuffers.\n", plane->plane_id);
505 ctx->plane_id = plane->plane_id;
507 #if HAVE_LIBDRM_GETFB2
508 fb2 = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
509 if (!fb2 && errno == ENOSYS) {
510 av_log(avctx, AV_LOG_INFO, "GETFB2 not supported, "
511 "will try to use GETFB instead.\n");
514 av_log(avctx, AV_LOG_ERROR, "Failed to get "
515 "framebuffer %"PRIu32": %s.\n",
516 plane->fb_id, strerror(err));
520 av_log(avctx, AV_LOG_INFO, "Template framebuffer is "
521 "%"PRIu32": %"PRIu32"x%"PRIu32" "
522 "format %"PRIx32" modifier %"PRIx64" flags %"PRIx32".\n",
523 fb2->fb_id, fb2->width, fb2->height,
524 fb2->pixel_format, fb2->modifier, fb2->flags);
526 ctx->width = fb2->width;
527 ctx->height = fb2->height;
529 if (!fb2->handles[0]) {
530 av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
531 "maybe you need some additional capabilities?\n");
532 err = AVERROR(EINVAL);
536 for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
537 if (kmsgrab_formats[i].drm_format == fb2->pixel_format) {
538 if (ctx->format != AV_PIX_FMT_NONE &&
539 ctx->format != kmsgrab_formats[i].pixfmt) {
540 av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
541 "%"PRIx32" does not match expected format.\n",
543 err = AVERROR(EINVAL);
546 ctx->drm_format = fb2->pixel_format;
547 ctx->format = kmsgrab_formats[i].pixfmt;
551 if (i == FF_ARRAY_ELEMS(kmsgrab_formats)) {
552 av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
553 "%"PRIx32" is not a known supported format.\n",
555 err = AVERROR(EINVAL);
559 if (fb2->flags & DRM_MODE_FB_MODIFIERS) {
560 if (ctx->drm_format_modifier != DRM_FORMAT_MOD_INVALID &&
561 ctx->drm_format_modifier != fb2->modifier) {
562 av_log(avctx, AV_LOG_ERROR, "Framebuffer format modifier "
563 "%"PRIx64" does not match expected modifier.\n",
565 err = AVERROR(EINVAL);
568 ctx->drm_format_modifier = fb2->modifier;
571 av_log(avctx, AV_LOG_VERBOSE, "Format is %s, from "
572 "DRM format %"PRIx32" modifier %"PRIx64".\n",
573 av_get_pix_fmt_name(ctx->format),
574 ctx->drm_format, ctx->drm_format_modifier);
576 ctx->fb2_available = 1;
580 if (!ctx->fb2_available) {
581 if (ctx->format == AV_PIX_FMT_NONE) {
582 // Backward compatibility: assume BGR0 if no format supplied.
583 ctx->format = AV_PIX_FMT_BGR0;
585 for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
586 if (kmsgrab_formats[i].pixfmt == ctx->format) {
587 ctx->drm_format = kmsgrab_formats[i].drm_format;
591 if (i >= FF_ARRAY_ELEMS(kmsgrab_formats)) {
592 av_log(avctx, AV_LOG_ERROR, "Unsupported format %s.\n",
593 av_get_pix_fmt_name(ctx->format));
594 return AVERROR(EINVAL);
597 fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
600 av_log(avctx, AV_LOG_ERROR, "Failed to get "
601 "framebuffer %"PRIu32": %s.\n",
602 plane->fb_id, strerror(err));
607 av_log(avctx, AV_LOG_INFO, "Template framebuffer is %"PRIu32": "
608 "%"PRIu32"x%"PRIu32" %"PRIu32"bpp %"PRIu32"b depth.\n",
609 fb->fb_id, fb->width, fb->height, fb->bpp, fb->depth);
611 ctx->width = fb->width;
612 ctx->height = fb->height;
615 av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
616 "maybe you need some additional capabilities?\n");
617 err = AVERROR(EINVAL);
622 stream = avformat_new_stream(avctx, NULL);
624 err = AVERROR(ENOMEM);
628 stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
629 stream->codecpar->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME;
630 stream->codecpar->width = ctx->width;
631 stream->codecpar->height = ctx->height;
632 stream->codecpar->format = AV_PIX_FMT_DRM_PRIME;
634 avpriv_set_pts_info(stream, 64, 1, 1000000);
636 ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
637 if (!ctx->frames_ref) {
638 err = AVERROR(ENOMEM);
641 ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
643 ctx->frames->format = AV_PIX_FMT_DRM_PRIME;
644 ctx->frames->sw_format = ctx->format,
645 ctx->frames->width = ctx->width;
646 ctx->frames->height = ctx->height;
648 err = av_hwframe_ctx_init(ctx->frames_ref);
650 av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
651 "hardware frames context: %d.\n", err);
655 ctx->frame_delay = av_rescale_q(1, (AVRational) { ctx->framerate.den,
656 ctx->framerate.num }, AV_TIME_BASE_Q);
660 drmModeFreePlaneResources(plane_res);
661 drmModeFreePlane(plane);
663 #if HAVE_LIBDRM_GETFB2
669 static av_cold int kmsgrab_read_close(AVFormatContext *avctx)
671 KMSGrabContext *ctx = avctx->priv_data;
673 av_buffer_unref(&ctx->frames_ref);
674 av_buffer_unref(&ctx->device_ref);
679 #define OFFSET(x) offsetof(KMSGrabContext, x)
680 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
681 static const AVOption options[] = {
682 { "device", "DRM device path",
683 OFFSET(device_path), AV_OPT_TYPE_STRING,
684 { .str = "/dev/dri/card0" }, 0, 0, FLAGS },
685 { "format", "Pixel format for framebuffer",
686 OFFSET(format), AV_OPT_TYPE_PIXEL_FMT,
687 { .i64 = AV_PIX_FMT_NONE }, -1, INT32_MAX, FLAGS },
688 { "format_modifier", "DRM format modifier for framebuffer",
689 OFFSET(drm_format_modifier), AV_OPT_TYPE_INT64,
690 { .i64 = DRM_FORMAT_MOD_INVALID }, 0, INT64_MAX, FLAGS },
691 { "crtc_id", "CRTC ID to define capture source",
692 OFFSET(source_crtc), AV_OPT_TYPE_INT64,
693 { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
694 { "plane_id", "Plane ID to define capture source",
695 OFFSET(source_plane), AV_OPT_TYPE_INT64,
696 { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
697 { "framerate", "Framerate to capture at",
698 OFFSET(framerate), AV_OPT_TYPE_RATIONAL,
699 { .dbl = 30.0 }, 0, 1000, FLAGS },
703 static const AVClass kmsgrab_class = {
704 .class_name = "kmsgrab indev",
705 .item_name = av_default_item_name,
707 .version = LIBAVUTIL_VERSION_INT,
708 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
711 const AVInputFormat ff_kmsgrab_demuxer = {
713 .long_name = NULL_IF_CONFIG_SMALL("KMS screen capture"),
714 .priv_data_size = sizeof(KMSGrabContext),
715 .read_header = &kmsgrab_read_header,
716 .read_packet = &kmsgrab_read_packet,
717 .read_close = &kmsgrab_read_close,
718 .flags = AVFMT_NOFILE,
719 .priv_class = &kmsgrab_class,