]> git.sesse.net Git - ffmpeg/blob - libavdevice/kmsgrab.c
Merge commit 'e1e3a12242347dd11174b2fb9ddac8dc8df16224'
[ffmpeg] / libavdevice / kmsgrab.c
1 /*
2  * KMS/DRM input device
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include <drm.h>
25 #include <drm_fourcc.h>
26 #include <drm_mode.h>
27 #include <xf86drm.h>
28 #include <xf86drmMode.h>
29
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"
38
39 #include "libavformat/avformat.h"
40 #include "libavformat/internal.h"
41
42 typedef struct KMSGrabContext {
43     const AVClass *class;
44
45     AVBufferRef *device_ref;
46     AVHWDeviceContext *device;
47     AVDRMDeviceContext *hwctx;
48
49     AVBufferRef *frames_ref;
50     AVHWFramesContext *frames;
51
52     uint32_t plane_id;
53     uint32_t drm_format;
54     unsigned int width;
55     unsigned int height;
56
57     int64_t frame_delay;
58     int64_t frame_last;
59
60     const char *device_path;
61     enum AVPixelFormat format;
62     int64_t drm_format_modifier;
63     int64_t source_plane;
64     int64_t source_crtc;
65     AVRational framerate;
66 } KMSGrabContext;
67
68 static void kmsgrab_free_desc(void *opaque, uint8_t *data)
69 {
70     AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)data;
71
72     close(desc->objects[0].fd);
73
74     av_free(desc);
75 }
76
77 static void kmsgrab_free_frame(void *opaque, uint8_t *data)
78 {
79     AVFrame *frame = (AVFrame*)data;
80
81     av_frame_free(&frame);
82 }
83
84 static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
85 {
86     KMSGrabContext *ctx = avctx->priv_data;
87     drmModePlane *plane;
88     drmModeFB *fb;
89     AVDRMFrameDescriptor *desc;
90     AVFrame *frame;
91     int64_t now;
92     int err, fd;
93
94     now = av_gettime();
95     if (ctx->frame_last) {
96         int64_t delay;
97         while (1) {
98             delay = ctx->frame_last + ctx->frame_delay - now;
99             if (delay <= 0)
100                 break;
101             av_usleep(delay);
102             now = av_gettime();
103         }
104     }
105     ctx->frame_last = now;
106
107     plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
108     if (!plane) {
109         av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
110                "%"PRIu32".\n", ctx->plane_id);
111         return AVERROR(EIO);
112     }
113     if (!plane->fb_id) {
114         av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" no longer has "
115                "an associated framebuffer.\n", ctx->plane_id);
116         return AVERROR(EIO);
117     }
118
119     fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
120     if (!fb) {
121         av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
122                "%"PRIu32".\n", plane->fb_id);
123         return AVERROR(EIO);
124     }
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);
129         return AVERROR(EIO);
130     }
131     if (!fb->handle) {
132         av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
133         return AVERROR(EIO);
134     }
135
136     err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handle, O_RDONLY, &fd);
137     if (err < 0) {
138         err = errno;
139         av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
140                "framebuffer handle: %s.\n", strerror(errno));
141         return AVERROR(err);
142     }
143
144     desc = av_mallocz(sizeof(*desc));
145     if (!desc)
146         return AVERROR(ENOMEM);
147
148     *desc = (AVDRMFrameDescriptor) {
149         .nb_objects = 1,
150         .objects[0] = {
151             .fd               = fd,
152             .size             = fb->height * fb->pitch,
153             .format_modifier  = ctx->drm_format_modifier,
154         },
155         .nb_layers = 1,
156         .layers[0] = {
157             .format           = ctx->drm_format,
158             .nb_planes        = 1,
159             .planes[0] = {
160                 .object_index = 0,
161                 .offset       = 0,
162                 .pitch        = fb->pitch,
163             },
164         },
165     };
166
167     frame = av_frame_alloc();
168     if (!frame)
169         return AVERROR(ENOMEM);
170
171     frame->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
172     if (!frame->hw_frames_ctx)
173         return AVERROR(ENOMEM);
174
175     frame->buf[0] = av_buffer_create((uint8_t*)desc, sizeof(*desc),
176                                      &kmsgrab_free_desc, avctx, 0);
177     if (!frame->buf[0])
178         return AVERROR(ENOMEM);
179
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;
184
185     drmModeFreeFB(fb);
186     drmModeFreePlane(plane);
187
188     pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
189                                 &kmsgrab_free_frame, avctx, 0);
190     if (!pkt->buf)
191         return AVERROR(ENOMEM);
192
193     pkt->data   = (uint8_t*)frame;
194     pkt->size   = sizeof(*frame);
195     pkt->pts    = now;
196     pkt->flags |= AV_PKT_FLAG_TRUSTED;
197
198     return 0;
199 }
200
201 static const struct {
202     enum AVPixelFormat pixfmt;
203     uint32_t drm_format;
204 } kmsgrab_formats[] = {
205 #ifdef DRM_FORMAT_R8
206     { AV_PIX_FMT_GRAY8,    DRM_FORMAT_R8       },
207 #endif
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 },
211 #endif
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     },
234 };
235
236 static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
237 {
238     KMSGrabContext *ctx = avctx->priv_data;
239     drmModePlaneRes *plane_res = NULL;
240     drmModePlane *plane = NULL;
241     drmModeFB *fb = NULL;
242     AVStream *stream;
243     int err, i;
244
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;
248             break;
249         }
250     }
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);
255     }
256
257     err = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_DRM,
258                                  ctx->device_path, NULL, 0);
259     if (err < 0) {
260         av_log(avctx, AV_LOG_ERROR, "Failed to open DRM device.\n");
261         return err;
262     }
263     ctx->device = (AVHWDeviceContext*) ctx->device_ref->data;
264     ctx->hwctx  = (AVDRMDeviceContext*)ctx->device->hwctx;
265
266     err = drmSetClientCap(ctx->hwctx->fd,
267                           DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
268     if (err < 0) {
269         av_log(avctx, AV_LOG_WARNING, "Failed to set universal planes "
270                "capability: primary planes will not be usable.\n");
271     }
272
273     if (ctx->source_plane > 0) {
274         plane = drmModeGetPlane(ctx->hwctx->fd, ctx->source_plane);
275         if (!plane) {
276             err = errno;
277             av_log(avctx, AV_LOG_ERROR, "Failed to get plane %"PRId64": "
278                    "%s.\n", ctx->source_plane, strerror(err));
279             err = AVERROR(err);
280             goto fail;
281         }
282
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);
287             goto fail;
288         }
289     } else {
290         plane_res = drmModeGetPlaneResources(ctx->hwctx->fd);
291         if (!plane_res) {
292             av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
293                    "resources: %s.\n", strerror(errno));
294             err = AVERROR(EINVAL);
295             goto fail;
296         }
297
298         for (i = 0; i < plane_res->count_planes; i++) {
299             plane = drmModeGetPlane(ctx->hwctx->fd,
300                                     plane_res->planes[i]);
301             if (!plane) {
302                 err = errno;
303                 av_log(avctx, AV_LOG_VERBOSE, "Failed to get "
304                        "plane %"PRIu32": %s.\n",
305                        plane_res->planes[i], strerror(err));
306                 continue;
307             }
308
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);
312
313             if ((ctx->source_crtc > 0 &&
314                  plane->crtc_id != ctx->source_crtc) ||
315                 plane->fb_id == 0) {
316                 // Either not connected to the target source CRTC
317                 // or not active.
318                 drmModeFreePlane(plane);
319                 plane = NULL;
320                 continue;
321             }
322
323             break;
324         }
325
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);
330             } else {
331                 av_log(avctx, AV_LOG_ERROR, "No usable planes found.\n");
332             }
333             err = AVERROR(EINVAL);
334             goto fail;
335         }
336
337         av_log(avctx, AV_LOG_INFO, "Using plane %"PRIu32" to "
338                "locate framebuffers.\n", plane->plane_id);
339     }
340
341     ctx->plane_id = plane->plane_id;
342
343     fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
344     if (!fb) {
345         err = errno;
346         av_log(avctx, AV_LOG_ERROR, "Failed to get "
347                "framebuffer %"PRIu32": %s.\n",
348                plane->fb_id, strerror(err));
349         err = AVERROR(err);
350         goto fail;
351     }
352
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);
356
357     ctx->width  = fb->width;
358     ctx->height = fb->height;
359
360     if (!fb->handle) {
361         av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
362                "maybe you need some additional capabilities?\n");
363         err = AVERROR(EINVAL);
364         goto fail;
365     }
366
367     stream = avformat_new_stream(avctx, NULL);
368     if (!stream) {
369         err = AVERROR(ENOMEM);
370         goto fail;
371     }
372
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;
378
379     avpriv_set_pts_info(stream, 64, 1, 1000000);
380
381     ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
382     if (!ctx->frames_ref) {
383         err = AVERROR(ENOMEM);
384         goto fail;
385     }
386     ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
387
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;
392
393     err = av_hwframe_ctx_init(ctx->frames_ref);
394     if (err < 0) {
395         av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
396                "hardware frames context: %d.\n", err);
397         goto fail;
398     }
399
400     ctx->frame_delay = av_rescale_q(1, (AVRational) { ctx->framerate.den,
401                 ctx->framerate.num }, AV_TIME_BASE_Q);
402
403     err = 0;
404 fail:
405     if (plane_res)
406         drmModeFreePlaneResources(plane_res);
407     if (plane)
408         drmModeFreePlane(plane);
409     if (fb)
410         drmModeFreeFB(fb);
411
412     return err;
413 }
414
415 static av_cold int kmsgrab_read_close(AVFormatContext *avctx)
416 {
417     KMSGrabContext *ctx = avctx->priv_data;
418
419     av_buffer_unref(&ctx->frames_ref);
420     av_buffer_unref(&ctx->device_ref);
421
422     return 0;
423 }
424
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 },
446     { NULL },
447 };
448
449 static const AVClass kmsgrab_class = {
450     .class_name = "kmsgrab indev",
451     .item_name  = av_default_item_name,
452     .option     = options,
453     .version    = LIBAVUTIL_VERSION_INT,
454     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
455 };
456
457 AVInputFormat ff_kmsgrab_demuxer = {
458     .name           = "kmsgrab",
459     .long_name      = NULL_IF_CONFIG_SMALL("KMS screen capture"),
460     .priv_data_size = sizeof(KMSGrabContext),
461     .read_header    = &kmsgrab_read_header,
462     .read_packet    = &kmsgrab_read_packet,
463     .read_close     = &kmsgrab_read_close,
464     .flags          = AVFMT_NOFILE,
465     .priv_class     = &kmsgrab_class,
466 };