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 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_drm.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixfmt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/time.h"
39 #include "libavformat/avformat.h"
40 #include "libavformat/internal.h"
42 typedef struct KMSGrabContext {
45 AVBufferRef *device_ref;
46 AVHWDeviceContext *device;
47 AVDRMDeviceContext *hwctx;
49 AVBufferRef *frames_ref;
50 AVHWFramesContext *frames;
60 const char *device_path;
61 enum AVPixelFormat format;
62 int64_t drm_format_modifier;
68 static void kmsgrab_free_desc(void *opaque, uint8_t *data)
70 AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)data;
72 close(desc->objects[0].fd);
77 static void kmsgrab_free_frame(void *opaque, uint8_t *data)
79 AVFrame *frame = (AVFrame*)data;
81 av_frame_free(&frame);
84 static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
86 KMSGrabContext *ctx = avctx->priv_data;
89 AVDRMFrameDescriptor *desc;
95 if (ctx->frame_last) {
98 delay = ctx->frame_last + ctx->frame_delay - now;
105 ctx->frame_last = now;
107 plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
109 av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
110 "%"PRIu32".\n", ctx->plane_id);
114 av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" no longer has "
115 "an associated framebuffer.\n", ctx->plane_id);
119 fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
121 av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
122 "%"PRIu32".\n", plane->fb_id);
125 if (fb->width != ctx->width || fb->height != ctx->height) {
126 av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
127 "dimensions changed: now %"PRIu32"x%"PRIu32".\n",
128 ctx->plane_id, fb->width, fb->height);
132 av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
136 err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handle, O_RDONLY, &fd);
139 av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
140 "framebuffer handle: %s.\n", strerror(errno));
144 desc = av_mallocz(sizeof(*desc));
146 return AVERROR(ENOMEM);
148 *desc = (AVDRMFrameDescriptor) {
152 .size = fb->height * fb->pitch,
153 .format_modifier = ctx->drm_format_modifier,
157 .format = ctx->drm_format,
167 frame = av_frame_alloc();
169 return AVERROR(ENOMEM);
171 frame->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
172 if (!frame->hw_frames_ctx)
173 return AVERROR(ENOMEM);
175 frame->buf[0] = av_buffer_create((uint8_t*)desc, sizeof(*desc),
176 &kmsgrab_free_desc, avctx, 0);
178 return AVERROR(ENOMEM);
180 frame->data[0] = (uint8_t*)desc;
181 frame->format = AV_PIX_FMT_DRM_PRIME;
182 frame->width = fb->width;
183 frame->height = fb->height;
186 drmModeFreePlane(plane);
188 pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
189 &kmsgrab_free_frame, avctx, 0);
191 return AVERROR(ENOMEM);
193 pkt->data = (uint8_t*)frame;
194 pkt->size = sizeof(*frame);
196 pkt->flags |= AV_PKT_FLAG_TRUSTED;
201 static const struct {
202 enum AVPixelFormat pixfmt;
204 } kmsgrab_formats[] = {
206 { AV_PIX_FMT_GRAY8, DRM_FORMAT_R8 },
208 #ifdef DRM_FORMAT_R16
209 { AV_PIX_FMT_GRAY16LE, DRM_FORMAT_R16 },
210 { AV_PIX_FMT_GRAY16BE, DRM_FORMAT_R16 | DRM_FORMAT_BIG_ENDIAN },
212 { AV_PIX_FMT_BGR8, DRM_FORMAT_BGR233 },
213 { AV_PIX_FMT_RGB555LE, DRM_FORMAT_XRGB1555 },
214 { AV_PIX_FMT_RGB555BE, DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
215 { AV_PIX_FMT_BGR555LE, DRM_FORMAT_XBGR1555 },
216 { AV_PIX_FMT_BGR555BE, DRM_FORMAT_XBGR1555 | DRM_FORMAT_BIG_ENDIAN },
217 { AV_PIX_FMT_RGB565LE, DRM_FORMAT_RGB565 },
218 { AV_PIX_FMT_RGB565BE, DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN },
219 { AV_PIX_FMT_BGR565LE, DRM_FORMAT_BGR565 },
220 { AV_PIX_FMT_BGR565BE, DRM_FORMAT_BGR565 | DRM_FORMAT_BIG_ENDIAN },
221 { AV_PIX_FMT_RGB24, DRM_FORMAT_RGB888 },
222 { AV_PIX_FMT_BGR24, DRM_FORMAT_BGR888 },
223 { AV_PIX_FMT_0RGB, DRM_FORMAT_BGRX8888 },
224 { AV_PIX_FMT_0BGR, DRM_FORMAT_RGBX8888 },
225 { AV_PIX_FMT_RGB0, DRM_FORMAT_XBGR8888 },
226 { AV_PIX_FMT_BGR0, DRM_FORMAT_XRGB8888 },
227 { AV_PIX_FMT_ARGB, DRM_FORMAT_BGRA8888 },
228 { AV_PIX_FMT_ABGR, DRM_FORMAT_RGBA8888 },
229 { AV_PIX_FMT_RGBA, DRM_FORMAT_ABGR8888 },
230 { AV_PIX_FMT_BGRA, DRM_FORMAT_ARGB8888 },
231 { AV_PIX_FMT_YUYV422, DRM_FORMAT_YUYV },
232 { AV_PIX_FMT_YVYU422, DRM_FORMAT_YVYU },
233 { AV_PIX_FMT_UYVY422, DRM_FORMAT_UYVY },
236 static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
238 KMSGrabContext *ctx = avctx->priv_data;
239 drmModePlaneRes *plane_res = NULL;
240 drmModePlane *plane = NULL;
241 drmModeFB *fb = NULL;
245 for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
246 if (kmsgrab_formats[i].pixfmt == ctx->format) {
247 ctx->drm_format = kmsgrab_formats[i].drm_format;
251 if (i >= FF_ARRAY_ELEMS(kmsgrab_formats)) {
252 av_log(avctx, AV_LOG_ERROR, "Unsupported format %s.\n",
253 av_get_pix_fmt_name(ctx->format));
254 return AVERROR(EINVAL);
257 err = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_DRM,
258 ctx->device_path, NULL, 0);
260 av_log(avctx, AV_LOG_ERROR, "Failed to open DRM device.\n");
263 ctx->device = (AVHWDeviceContext*) ctx->device_ref->data;
264 ctx->hwctx = (AVDRMDeviceContext*)ctx->device->hwctx;
266 err = drmSetClientCap(ctx->hwctx->fd,
267 DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
269 av_log(avctx, AV_LOG_WARNING, "Failed to set universal planes "
270 "capability: primary planes will not be usable.\n");
273 if (ctx->source_plane > 0) {
274 plane = drmModeGetPlane(ctx->hwctx->fd, ctx->source_plane);
277 av_log(avctx, AV_LOG_ERROR, "Failed to get plane %"PRId64": "
278 "%s.\n", ctx->source_plane, strerror(err));
283 if (plane->fb_id == 0) {
284 av_log(avctx, AV_LOG_ERROR, "Plane %"PRId64" does not have "
285 "an attached framebuffer.\n", ctx->source_plane);
286 err = AVERROR(EINVAL);
290 plane_res = drmModeGetPlaneResources(ctx->hwctx->fd);
292 av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
293 "resources: %s.\n", strerror(errno));
294 err = AVERROR(EINVAL);
298 for (i = 0; i < plane_res->count_planes; i++) {
299 plane = drmModeGetPlane(ctx->hwctx->fd,
300 plane_res->planes[i]);
303 av_log(avctx, AV_LOG_VERBOSE, "Failed to get "
304 "plane %"PRIu32": %s.\n",
305 plane_res->planes[i], strerror(err));
309 av_log(avctx, AV_LOG_DEBUG, "Plane %"PRIu32": "
310 "CRTC %"PRIu32" FB %"PRIu32".\n",
311 plane->plane_id, plane->crtc_id, plane->fb_id);
313 if ((ctx->source_crtc > 0 &&
314 plane->crtc_id != ctx->source_crtc) ||
316 // Either not connected to the target source CRTC
318 drmModeFreePlane(plane);
326 if (i == plane_res->count_planes) {
327 if (ctx->source_crtc > 0) {
328 av_log(avctx, AV_LOG_ERROR, "No usable planes found on "
329 "CRTC %"PRId64".\n", ctx->source_crtc);
331 av_log(avctx, AV_LOG_ERROR, "No usable planes found.\n");
333 err = AVERROR(EINVAL);
337 av_log(avctx, AV_LOG_INFO, "Using plane %"PRIu32" to "
338 "locate framebuffers.\n", plane->plane_id);
341 ctx->plane_id = plane->plane_id;
343 fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
346 av_log(avctx, AV_LOG_ERROR, "Failed to get "
347 "framebuffer %"PRIu32": %s.\n",
348 plane->fb_id, strerror(err));
353 av_log(avctx, AV_LOG_INFO, "Template framebuffer is %"PRIu32": "
354 "%"PRIu32"x%"PRIu32" %"PRIu32"bpp %"PRIu32"b depth.\n",
355 fb->fb_id, fb->width, fb->height, fb->bpp, fb->depth);
357 ctx->width = fb->width;
358 ctx->height = fb->height;
361 av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
362 "maybe you need some additional capabilities?\n");
363 err = AVERROR(EINVAL);
367 stream = avformat_new_stream(avctx, NULL);
369 err = AVERROR(ENOMEM);
373 stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
374 stream->codecpar->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME;
375 stream->codecpar->width = fb->width;
376 stream->codecpar->height = fb->height;
377 stream->codecpar->format = AV_PIX_FMT_DRM_PRIME;
379 avpriv_set_pts_info(stream, 64, 1, 1000000);
381 ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
382 if (!ctx->frames_ref) {
383 err = AVERROR(ENOMEM);
386 ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
388 ctx->frames->format = AV_PIX_FMT_DRM_PRIME;
389 ctx->frames->sw_format = ctx->format,
390 ctx->frames->width = fb->width;
391 ctx->frames->height = fb->height;
393 err = av_hwframe_ctx_init(ctx->frames_ref);
395 av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
396 "hardware frames context: %d.\n", err);
400 ctx->frame_delay = av_rescale_q(1, (AVRational) { ctx->framerate.den,
401 ctx->framerate.num }, AV_TIME_BASE_Q);
406 drmModeFreePlaneResources(plane_res);
408 drmModeFreePlane(plane);
415 static av_cold int kmsgrab_read_close(AVFormatContext *avctx)
417 KMSGrabContext *ctx = avctx->priv_data;
419 av_buffer_unref(&ctx->frames_ref);
420 av_buffer_unref(&ctx->device_ref);
425 #define OFFSET(x) offsetof(KMSGrabContext, x)
426 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
427 static const AVOption options[] = {
428 { "device", "DRM device path",
429 OFFSET(device_path), AV_OPT_TYPE_STRING,
430 { .str = "/dev/dri/card0" }, 0, 0, FLAGS },
431 { "format", "Pixel format for framebuffer",
432 OFFSET(format), AV_OPT_TYPE_PIXEL_FMT,
433 { .i64 = AV_PIX_FMT_BGR0 }, 0, UINT32_MAX, FLAGS },
434 { "format_modifier", "DRM format modifier for framebuffer",
435 OFFSET(drm_format_modifier), AV_OPT_TYPE_INT64,
436 { .i64 = DRM_FORMAT_MOD_NONE }, 0, INT64_MAX, FLAGS },
437 { "crtc_id", "CRTC ID to define capture source",
438 OFFSET(source_crtc), AV_OPT_TYPE_INT64,
439 { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
440 { "plane_id", "Plane ID to define capture source",
441 OFFSET(source_plane), AV_OPT_TYPE_INT64,
442 { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
443 { "framerate", "Framerate to capture at",
444 OFFSET(framerate), AV_OPT_TYPE_RATIONAL,
445 { .dbl = 30.0 }, 0, 1000, FLAGS },
449 static const AVClass kmsgrab_class = {
450 .class_name = "kmsgrab indev",
451 .item_name = av_default_item_name,
453 .version = LIBAVUTIL_VERSION_INT,
456 AVInputFormat ff_kmsgrab_demuxer = {
458 .long_name = NULL_IF_CONFIG_SMALL("KMS screen capture"),
459 .priv_data_size = sizeof(KMSGrabContext),
460 .read_header = &kmsgrab_read_header,
461 .read_packet = &kmsgrab_read_packet,
462 .read_close = &kmsgrab_read_close,
463 .flags = AVFMT_NOFILE,
464 .priv_class = &kmsgrab_class,