]> git.sesse.net Git - ffmpeg/blob - libavdevice/kmsgrab.c
kmsgrab: Use invalid modifier if modifiers weren't used.
[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 // 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)
33 #endif
34
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"
43
44 #include "libavformat/avformat.h"
45 #include "libavformat/internal.h"
46
47 typedef struct KMSGrabContext {
48     const AVClass *class;
49
50     AVBufferRef *device_ref;
51     AVHWDeviceContext *device;
52     AVDRMDeviceContext *hwctx;
53     int fb2_available;
54
55     AVBufferRef *frames_ref;
56     AVHWFramesContext *frames;
57
58     uint32_t plane_id;
59     uint32_t drm_format;
60     unsigned int width;
61     unsigned int height;
62
63     int64_t frame_delay;
64     int64_t frame_last;
65
66     const char *device_path;
67     enum AVPixelFormat format;
68     int64_t drm_format_modifier;
69     int64_t source_plane;
70     int64_t source_crtc;
71     AVRational framerate;
72 } KMSGrabContext;
73
74 static void kmsgrab_free_desc(void *opaque, uint8_t *data)
75 {
76     AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)data;
77     int i;
78
79     for (i = 0; i < desc->nb_objects; i++)
80         close(desc->objects[i].fd);
81
82     av_free(desc);
83 }
84
85 static void kmsgrab_free_frame(void *opaque, uint8_t *data)
86 {
87     AVFrame *frame = (AVFrame*)data;
88
89     av_frame_free(&frame);
90 }
91
92 static int kmsgrab_get_fb(AVFormatContext *avctx,
93                           drmModePlane *plane,
94                           AVDRMFrameDescriptor *desc)
95 {
96     KMSGrabContext *ctx = avctx->priv_data;
97     drmModeFB *fb = NULL;
98     int err, fd;
99
100     fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
101     if (!fb) {
102         err = errno;
103         av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
104                "%"PRIu32": %s.\n", plane->fb_id, strerror(err));
105         err = AVERROR(err);
106         goto fail;
107     }
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);
112         err = AVERROR(EIO);
113         goto fail;
114     }
115     if (!fb->handle) {
116         av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
117         err = AVERROR(EIO);
118         goto fail;
119     }
120
121     err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handle, O_RDONLY, &fd);
122     if (err < 0) {
123         err = errno;
124         av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
125                "framebuffer handle: %s.\n", strerror(err));
126         err = AVERROR(err);
127         goto fail;
128     }
129
130     *desc = (AVDRMFrameDescriptor) {
131         .nb_objects = 1,
132         .objects[0] = {
133             .fd               = fd,
134             .size             = fb->height * fb->pitch,
135             .format_modifier  = ctx->drm_format_modifier,
136         },
137         .nb_layers = 1,
138         .layers[0] = {
139             .format           = ctx->drm_format,
140             .nb_planes        = 1,
141             .planes[0] = {
142                 .object_index = 0,
143                 .offset       = 0,
144                 .pitch        = fb->pitch,
145             },
146         },
147     };
148
149     err = 0;
150 fail:
151     drmModeFreeFB(fb);
152     return err;
153 }
154
155 #if HAVE_LIBDRM_GETFB2
156 static int kmsgrab_get_fb2(AVFormatContext *avctx,
157                            drmModePlane *plane,
158                            AVDRMFrameDescriptor *desc)
159 {
160     KMSGrabContext *ctx = avctx->priv_data;
161     drmModeFB2 *fb;
162     int err, i, nb_objects;
163     uint64_t modifier = ctx->drm_format_modifier;
164
165     fb = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
166     if (!fb) {
167         err = errno;
168         av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
169                "%"PRIu32": %s.\n", plane->fb_id, strerror(err));
170         return AVERROR(err);
171     }
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);
176         err = AVERROR(EIO);
177         goto fail;
178     }
179     if (fb->modifier != ctx->drm_format_modifier) {
180         av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
181                "format modifier changed: now %"PRIx64".\n",
182                ctx->plane_id, fb->modifier);
183         err = AVERROR(EIO);
184         goto fail;
185     }
186     if (fb->width != ctx->width || fb->height != ctx->height) {
187         av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
188                "dimensions changed: now %"PRIu32"x%"PRIu32".\n",
189                ctx->plane_id, fb->width, fb->height);
190         err = AVERROR(EIO);
191         goto fail;
192     }
193     if (!fb->handles[0]) {
194         av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
195         err = AVERROR(EIO);
196         goto fail;
197     }
198
199     if (fb->flags & DRM_MODE_FB_MODIFIERS)
200         modifier = fb->modifier;
201
202     *desc = (AVDRMFrameDescriptor) {
203         .nb_layers = 1,
204         .layers[0] = {
205             .format = ctx->drm_format,
206         },
207     };
208
209     nb_objects = 0;
210     for (i = 0; i < 4 && fb->handles[i]; i++) {
211         size_t size;
212         int dup = 0, j, obj;
213
214         size = fb->offsets[i] + fb->height * fb->pitches[i];
215
216         for (j = 0; j < i; j++) {
217             if (fb->handles[i] == fb->handles[j]) {
218                 dup = 1;
219                 break;
220             }
221         }
222         if (dup) {
223             obj = desc->layers[0].planes[j].object_index;
224
225             if (desc->objects[j].size < size)
226                 desc->objects[j].size = size;
227
228             desc->layers[0].planes[i] = (AVDRMPlaneDescriptor) {
229                 .object_index = obj,
230                 .offset       = fb->offsets[i],
231                 .pitch        = fb->pitches[i],
232             };
233
234         } else {
235             int fd;
236             err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handles[i],
237                                      O_RDONLY, &fd);
238             if (err < 0) {
239                 err = errno;
240                 av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
241                        "framebuffer handle: %s.\n", strerror(err));
242                 err = AVERROR(err);
243                 goto fail;
244             }
245
246             obj = nb_objects++;
247             desc->objects[obj] = (AVDRMObjectDescriptor) {
248                 .fd              = fd,
249                 .size            = size,
250                 .format_modifier = modifier,
251             };
252             desc->layers[0].planes[i] = (AVDRMPlaneDescriptor) {
253                 .object_index = obj,
254                 .offset       = fb->offsets[i],
255                 .pitch        = fb->pitches[i],
256             };
257         }
258     }
259     desc->nb_objects = nb_objects;
260     desc->layers[0].nb_planes = i;
261
262     err = 0;
263 fail:
264     drmModeFreeFB2(fb);
265     return err;
266 }
267 #endif
268
269 static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
270 {
271     KMSGrabContext *ctx = avctx->priv_data;
272     drmModePlane *plane = NULL;
273     AVDRMFrameDescriptor *desc = NULL;
274     AVFrame *frame = NULL;
275     int64_t now;
276     int err;
277
278     now = av_gettime();
279     if (ctx->frame_last) {
280         int64_t delay;
281         while (1) {
282             delay = ctx->frame_last + ctx->frame_delay - now;
283             if (delay <= 0)
284                 break;
285             av_usleep(delay);
286             now = av_gettime();
287         }
288     }
289     ctx->frame_last = now;
290
291     plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
292     if (!plane) {
293         err = errno;
294         av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
295                "%"PRIu32": %s.\n", ctx->plane_id, strerror(err));
296         err = AVERROR(err);
297         goto fail;
298     }
299     if (!plane->fb_id) {
300         av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" no longer has "
301                "an associated framebuffer.\n", ctx->plane_id);
302         err = AVERROR(EIO);
303         goto fail;
304     }
305
306     desc = av_mallocz(sizeof(*desc));
307     if (!desc) {
308         err = AVERROR(ENOMEM);
309         goto fail;
310     }
311
312 #if HAVE_LIBDRM_GETFB2
313     if (ctx->fb2_available)
314         err = kmsgrab_get_fb2(avctx, plane, desc);
315     else
316 #endif
317         err = kmsgrab_get_fb(avctx, plane, desc);
318     if (err < 0)
319         goto fail;
320
321     frame = av_frame_alloc();
322     if (!frame) {
323         err = AVERROR(ENOMEM);
324         goto fail;
325     }
326
327     frame->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
328     if (!frame->hw_frames_ctx) {
329         err = AVERROR(ENOMEM);
330         goto fail;
331     }
332
333     frame->buf[0] = av_buffer_create((uint8_t*)desc, sizeof(*desc),
334                                      &kmsgrab_free_desc, avctx, 0);
335     if (!frame->buf[0]) {
336         err = AVERROR(ENOMEM);
337         goto fail;
338     }
339
340     frame->data[0] = (uint8_t*)desc;
341     frame->format  = AV_PIX_FMT_DRM_PRIME;
342     frame->width   = ctx->width;
343     frame->height  = ctx->height;
344
345     drmModeFreePlane(plane);
346     plane = NULL;
347     desc  = NULL;
348
349     pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
350                                 &kmsgrab_free_frame, avctx, 0);
351     if (!pkt->buf) {
352         err = AVERROR(ENOMEM);
353         goto fail;
354     }
355
356     pkt->data   = (uint8_t*)frame;
357     pkt->size   = sizeof(*frame);
358     pkt->pts    = now;
359     pkt->flags |= AV_PKT_FLAG_TRUSTED;
360
361     return 0;
362
363 fail:
364     drmModeFreePlane(plane);
365     av_freep(&desc);
366     av_frame_free(&frame);
367     return err;
368 }
369
370 static const struct {
371     enum AVPixelFormat pixfmt;
372     uint32_t drm_format;
373 } kmsgrab_formats[] = {
374     // Monochrome.
375 #ifdef DRM_FORMAT_R8
376     { AV_PIX_FMT_GRAY8,    DRM_FORMAT_R8       },
377 #endif
378 #ifdef DRM_FORMAT_R16
379     { AV_PIX_FMT_GRAY16LE, DRM_FORMAT_R16      },
380     { AV_PIX_FMT_GRAY16BE, DRM_FORMAT_R16      | DRM_FORMAT_BIG_ENDIAN },
381 #endif
382     // <8-bit RGB.
383     { AV_PIX_FMT_BGR8,     DRM_FORMAT_BGR233   },
384     { AV_PIX_FMT_RGB555LE, DRM_FORMAT_XRGB1555 },
385     { AV_PIX_FMT_RGB555BE, DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
386     { AV_PIX_FMT_BGR555LE, DRM_FORMAT_XBGR1555 },
387     { AV_PIX_FMT_BGR555BE, DRM_FORMAT_XBGR1555 | DRM_FORMAT_BIG_ENDIAN },
388     { AV_PIX_FMT_RGB565LE, DRM_FORMAT_RGB565   },
389     { AV_PIX_FMT_RGB565BE, DRM_FORMAT_RGB565   | DRM_FORMAT_BIG_ENDIAN },
390     { AV_PIX_FMT_BGR565LE, DRM_FORMAT_BGR565   },
391     { AV_PIX_FMT_BGR565BE, DRM_FORMAT_BGR565   | DRM_FORMAT_BIG_ENDIAN },
392     // 8-bit RGB.
393     { AV_PIX_FMT_RGB24,    DRM_FORMAT_RGB888   },
394     { AV_PIX_FMT_BGR24,    DRM_FORMAT_BGR888   },
395     { AV_PIX_FMT_0RGB,     DRM_FORMAT_BGRX8888 },
396     { AV_PIX_FMT_0BGR,     DRM_FORMAT_RGBX8888 },
397     { AV_PIX_FMT_RGB0,     DRM_FORMAT_XBGR8888 },
398     { AV_PIX_FMT_BGR0,     DRM_FORMAT_XRGB8888 },
399     { AV_PIX_FMT_ARGB,     DRM_FORMAT_BGRA8888 },
400     { AV_PIX_FMT_ABGR,     DRM_FORMAT_RGBA8888 },
401     { AV_PIX_FMT_RGBA,     DRM_FORMAT_ABGR8888 },
402     { AV_PIX_FMT_BGRA,     DRM_FORMAT_ARGB8888 },
403     // 10-bit RGB.
404     { AV_PIX_FMT_X2RGB10LE, DRM_FORMAT_XRGB2101010 },
405     { AV_PIX_FMT_X2RGB10BE, DRM_FORMAT_XRGB2101010 | DRM_FORMAT_BIG_ENDIAN },
406     // 8-bit YUV 4:2:0.
407     { AV_PIX_FMT_NV12,     DRM_FORMAT_NV12     },
408     // 8-bit YUV 4:2:2.
409     { AV_PIX_FMT_YUYV422,  DRM_FORMAT_YUYV     },
410     { AV_PIX_FMT_YVYU422,  DRM_FORMAT_YVYU     },
411     { AV_PIX_FMT_UYVY422,  DRM_FORMAT_UYVY     },
412 };
413
414 static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
415 {
416     KMSGrabContext *ctx = avctx->priv_data;
417     drmModePlaneRes *plane_res = NULL;
418     drmModePlane *plane = NULL;
419     drmModeFB *fb = NULL;
420 #if HAVE_LIBDRM_GETFB2
421     drmModeFB2 *fb2 = NULL;
422 #endif
423     AVStream *stream;
424     int err, i;
425
426     err = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_DRM,
427                                  ctx->device_path, NULL, 0);
428     if (err < 0) {
429         av_log(avctx, AV_LOG_ERROR, "Failed to open DRM device.\n");
430         return err;
431     }
432     ctx->device = (AVHWDeviceContext*) ctx->device_ref->data;
433     ctx->hwctx  = (AVDRMDeviceContext*)ctx->device->hwctx;
434
435     err = drmSetClientCap(ctx->hwctx->fd,
436                           DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
437     if (err < 0) {
438         av_log(avctx, AV_LOG_WARNING, "Failed to set universal planes "
439                "capability: primary planes will not be usable.\n");
440     }
441
442     if (ctx->source_plane > 0) {
443         plane = drmModeGetPlane(ctx->hwctx->fd, ctx->source_plane);
444         if (!plane) {
445             err = errno;
446             av_log(avctx, AV_LOG_ERROR, "Failed to get plane %"PRId64": "
447                    "%s.\n", ctx->source_plane, strerror(err));
448             err = AVERROR(err);
449             goto fail;
450         }
451
452         if (plane->fb_id == 0) {
453             av_log(avctx, AV_LOG_ERROR, "Plane %"PRId64" does not have "
454                    "an attached framebuffer.\n", ctx->source_plane);
455             err = AVERROR(EINVAL);
456             goto fail;
457         }
458     } else {
459         plane_res = drmModeGetPlaneResources(ctx->hwctx->fd);
460         if (!plane_res) {
461             err = errno;
462             av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
463                    "resources: %s.\n", strerror(err));
464             err = AVERROR(err);
465             goto fail;
466         }
467
468         for (i = 0; i < plane_res->count_planes; i++) {
469             plane = drmModeGetPlane(ctx->hwctx->fd,
470                                     plane_res->planes[i]);
471             if (!plane) {
472                 err = errno;
473                 av_log(avctx, AV_LOG_VERBOSE, "Failed to get "
474                        "plane %"PRIu32": %s.\n",
475                        plane_res->planes[i], strerror(err));
476                 continue;
477             }
478
479             av_log(avctx, AV_LOG_DEBUG, "Plane %"PRIu32": "
480                    "CRTC %"PRIu32" FB %"PRIu32".\n",
481                    plane->plane_id, plane->crtc_id, plane->fb_id);
482
483             if ((ctx->source_crtc > 0 &&
484                  plane->crtc_id != ctx->source_crtc) ||
485                 plane->fb_id == 0) {
486                 // Either not connected to the target source CRTC
487                 // or not active.
488                 drmModeFreePlane(plane);
489                 plane = NULL;
490                 continue;
491             }
492
493             break;
494         }
495
496         if (i == plane_res->count_planes) {
497             if (ctx->source_crtc > 0) {
498                 av_log(avctx, AV_LOG_ERROR, "No usable planes found on "
499                        "CRTC %"PRId64".\n", ctx->source_crtc);
500             } else {
501                 av_log(avctx, AV_LOG_ERROR, "No usable planes found.\n");
502             }
503             err = AVERROR(EINVAL);
504             goto fail;
505         }
506
507         av_log(avctx, AV_LOG_INFO, "Using plane %"PRIu32" to "
508                "locate framebuffers.\n", plane->plane_id);
509     }
510
511     ctx->plane_id = plane->plane_id;
512
513 #if HAVE_LIBDRM_GETFB2
514     fb2 = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
515     if (!fb2 && errno == ENOSYS) {
516         av_log(avctx, AV_LOG_INFO, "GETFB2 not supported, "
517                "will try to use GETFB instead.\n");
518     } else if (!fb2) {
519         err = errno;
520         av_log(avctx, AV_LOG_ERROR, "Failed to get "
521                "framebuffer %"PRIu32": %s.\n",
522                plane->fb_id, strerror(err));
523         err = AVERROR(err);
524         goto fail;
525     } else {
526         av_log(avctx, AV_LOG_INFO, "Template framebuffer is "
527                "%"PRIu32": %"PRIu32"x%"PRIu32" "
528                "format %"PRIx32" modifier %"PRIx64" flags %"PRIx32".\n",
529                fb2->fb_id, fb2->width, fb2->height,
530                fb2->pixel_format, fb2->modifier, fb2->flags);
531
532         ctx->width  = fb2->width;
533         ctx->height = fb2->height;
534
535         if (!fb2->handles[0]) {
536             av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
537                    "maybe you need some additional capabilities?\n");
538             err = AVERROR(EINVAL);
539             goto fail;
540         }
541
542         for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
543             if (kmsgrab_formats[i].drm_format == fb2->pixel_format) {
544                 if (ctx->format != AV_PIX_FMT_NONE &&
545                     ctx->format != kmsgrab_formats[i].pixfmt) {
546                     av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
547                            "%"PRIx32" does not match expected format.\n",
548                            fb2->pixel_format);
549                     err = AVERROR(EINVAL);
550                     goto fail;
551                 }
552                 ctx->drm_format = fb2->pixel_format;
553                 ctx->format     = kmsgrab_formats[i].pixfmt;
554                 break;
555             }
556         }
557         if (i == FF_ARRAY_ELEMS(kmsgrab_formats)) {
558             av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
559                    "%"PRIx32" is not a known supported format.\n",
560                    fb2->pixel_format);
561             err = AVERROR(EINVAL);
562             goto fail;
563         }
564
565         if (fb2->flags & DRM_MODE_FB_MODIFIERS) {
566             if (ctx->drm_format_modifier != DRM_FORMAT_MOD_INVALID &&
567                 ctx->drm_format_modifier != fb2->modifier) {
568                 av_log(avctx, AV_LOG_ERROR, "Framebuffer format modifier "
569                        "%"PRIx64" does not match expected modifier.\n",
570                        fb2->modifier);
571                 err = AVERROR(EINVAL);
572                 goto fail;
573             } else {
574                 ctx->drm_format_modifier = fb2->modifier;
575             }
576         }
577         av_log(avctx, AV_LOG_VERBOSE, "Format is %s, from "
578                "DRM format %"PRIx32" modifier %"PRIx64".\n",
579                av_get_pix_fmt_name(ctx->format),
580                ctx->drm_format, ctx->drm_format_modifier);
581
582         ctx->fb2_available = 1;
583     }
584 #endif
585
586     if (!ctx->fb2_available) {
587         if (ctx->format == AV_PIX_FMT_NONE) {
588             // Backward compatibility: assume BGR0 if no format supplied.
589             ctx->format = AV_PIX_FMT_BGR0;
590         }
591         for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
592             if (kmsgrab_formats[i].pixfmt == ctx->format) {
593                 ctx->drm_format = kmsgrab_formats[i].drm_format;
594                 break;
595             }
596         }
597         if (i >= FF_ARRAY_ELEMS(kmsgrab_formats)) {
598             av_log(avctx, AV_LOG_ERROR, "Unsupported format %s.\n",
599                    av_get_pix_fmt_name(ctx->format));
600             return AVERROR(EINVAL);
601         }
602
603         fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
604         if (!fb) {
605             err = errno;
606             av_log(avctx, AV_LOG_ERROR, "Failed to get "
607                    "framebuffer %"PRIu32": %s.\n",
608                    plane->fb_id, strerror(err));
609             err = AVERROR(err);
610             goto fail;
611         }
612
613         av_log(avctx, AV_LOG_INFO, "Template framebuffer is %"PRIu32": "
614                "%"PRIu32"x%"PRIu32" %"PRIu32"bpp %"PRIu32"b depth.\n",
615                fb->fb_id, fb->width, fb->height, fb->bpp, fb->depth);
616
617         ctx->width  = fb->width;
618         ctx->height = fb->height;
619
620         if (!fb->handle) {
621             av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
622                    "maybe you need some additional capabilities?\n");
623             err = AVERROR(EINVAL);
624             goto fail;
625         }
626     }
627
628     stream = avformat_new_stream(avctx, NULL);
629     if (!stream) {
630         err = AVERROR(ENOMEM);
631         goto fail;
632     }
633
634     stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
635     stream->codecpar->codec_id   = AV_CODEC_ID_WRAPPED_AVFRAME;
636     stream->codecpar->width      = ctx->width;
637     stream->codecpar->height     = ctx->height;
638     stream->codecpar->format     = AV_PIX_FMT_DRM_PRIME;
639
640     avpriv_set_pts_info(stream, 64, 1, 1000000);
641
642     ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
643     if (!ctx->frames_ref) {
644         err = AVERROR(ENOMEM);
645         goto fail;
646     }
647     ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
648
649     ctx->frames->format    = AV_PIX_FMT_DRM_PRIME;
650     ctx->frames->sw_format = ctx->format,
651     ctx->frames->width     = ctx->width;
652     ctx->frames->height    = ctx->height;
653
654     err = av_hwframe_ctx_init(ctx->frames_ref);
655     if (err < 0) {
656         av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
657                "hardware frames context: %d.\n", err);
658         goto fail;
659     }
660
661     ctx->frame_delay = av_rescale_q(1, (AVRational) { ctx->framerate.den,
662                 ctx->framerate.num }, AV_TIME_BASE_Q);
663
664     err = 0;
665 fail:
666     drmModeFreePlaneResources(plane_res);
667     drmModeFreePlane(plane);
668     drmModeFreeFB(fb);
669 #if HAVE_LIBDRM_GETFB2
670     drmModeFreeFB2(fb2);
671 #endif
672     return err;
673 }
674
675 static av_cold int kmsgrab_read_close(AVFormatContext *avctx)
676 {
677     KMSGrabContext *ctx = avctx->priv_data;
678
679     av_buffer_unref(&ctx->frames_ref);
680     av_buffer_unref(&ctx->device_ref);
681
682     return 0;
683 }
684
685 #define OFFSET(x) offsetof(KMSGrabContext, x)
686 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
687 static const AVOption options[] = {
688     { "device", "DRM device path",
689       OFFSET(device_path), AV_OPT_TYPE_STRING,
690       { .str = "/dev/dri/card0" }, 0, 0, FLAGS },
691     { "format", "Pixel format for framebuffer",
692       OFFSET(format), AV_OPT_TYPE_PIXEL_FMT,
693       { .i64 = AV_PIX_FMT_NONE }, -1, INT32_MAX, FLAGS },
694     { "format_modifier", "DRM format modifier for framebuffer",
695       OFFSET(drm_format_modifier), AV_OPT_TYPE_INT64,
696       { .i64 = DRM_FORMAT_MOD_INVALID }, 0, INT64_MAX, FLAGS },
697     { "crtc_id", "CRTC ID to define capture source",
698       OFFSET(source_crtc), AV_OPT_TYPE_INT64,
699       { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
700     { "plane_id", "Plane ID to define capture source",
701       OFFSET(source_plane), AV_OPT_TYPE_INT64,
702       { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
703     { "framerate", "Framerate to capture at",
704       OFFSET(framerate), AV_OPT_TYPE_RATIONAL,
705       { .dbl = 30.0 }, 0, 1000, FLAGS },
706     { NULL },
707 };
708
709 static const AVClass kmsgrab_class = {
710     .class_name = "kmsgrab indev",
711     .item_name  = av_default_item_name,
712     .option     = options,
713     .version    = LIBAVUTIL_VERSION_INT,
714     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
715 };
716
717 AVInputFormat ff_kmsgrab_demuxer = {
718     .name           = "kmsgrab",
719     .long_name      = NULL_IF_CONFIG_SMALL("KMS screen capture"),
720     .priv_data_size = sizeof(KMSGrabContext),
721     .read_header    = &kmsgrab_read_header,
722     .read_packet    = &kmsgrab_read_packet,
723     .read_close     = &kmsgrab_read_close,
724     .flags          = AVFMT_NOFILE,
725     .priv_class     = &kmsgrab_class,
726 };