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