kmsgrab.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <drm.h>
  23. #include <drm_fourcc.h>
  24. #include <drm_mode.h>
  25. #include <xf86drm.h>
  26. #include <xf86drmMode.h>
  27. #include "libavutil/hwcontext.h"
  28. #include "libavutil/hwcontext_drm.h"
  29. #include "libavutil/internal.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixfmt.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/time.h"
  35. #include "libavformat/avformat.h"
  36. #include "libavformat/internal.h"
  37. typedef struct KMSGrabContext {
  38. const AVClass *class;
  39. AVBufferRef *device_ref;
  40. AVHWDeviceContext *device;
  41. AVDRMDeviceContext *hwctx;
  42. AVBufferRef *frames_ref;
  43. AVHWFramesContext *frames;
  44. uint32_t plane_id;
  45. uint32_t drm_format;
  46. unsigned int width;
  47. unsigned int height;
  48. int64_t frame_delay;
  49. int64_t frame_last;
  50. const char *device_path;
  51. enum AVPixelFormat format;
  52. int64_t drm_format_modifier;
  53. int64_t source_plane;
  54. int64_t source_crtc;
  55. AVRational framerate;
  56. } KMSGrabContext;
  57. static void kmsgrab_free_desc(void *opaque, uint8_t *data)
  58. {
  59. AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)data;
  60. close(desc->objects[0].fd);
  61. av_free(desc);
  62. }
  63. static void kmsgrab_free_frame(void *opaque, uint8_t *data)
  64. {
  65. AVFrame *frame = (AVFrame*)data;
  66. av_frame_free(&frame);
  67. }
  68. static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  69. {
  70. KMSGrabContext *ctx = avctx->priv_data;
  71. drmModePlane *plane;
  72. drmModeFB *fb;
  73. AVDRMFrameDescriptor *desc;
  74. AVFrame *frame;
  75. int64_t now;
  76. int err, fd;
  77. now = av_gettime();
  78. if (ctx->frame_last) {
  79. int64_t delay;
  80. while (1) {
  81. delay = ctx->frame_last + ctx->frame_delay - now;
  82. if (delay <= 0)
  83. break;
  84. av_usleep(delay);
  85. now = av_gettime();
  86. }
  87. }
  88. ctx->frame_last = now;
  89. plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
  90. if (!plane) {
  91. av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
  92. "%"PRIu32".\n", ctx->plane_id);
  93. return AVERROR(EIO);
  94. }
  95. if (!plane->fb_id) {
  96. av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" no longer has "
  97. "an associated framebuffer.\n", ctx->plane_id);
  98. return AVERROR(EIO);
  99. }
  100. fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
  101. if (!fb) {
  102. av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
  103. "%"PRIu32".\n", plane->fb_id);
  104. return AVERROR(EIO);
  105. }
  106. if (fb->width != ctx->width || fb->height != ctx->height) {
  107. av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
  108. "dimensions changed: now %"PRIu32"x%"PRIu32".\n",
  109. ctx->plane_id, fb->width, fb->height);
  110. return AVERROR(EIO);
  111. }
  112. if (!fb->handle) {
  113. av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
  114. return AVERROR(EIO);
  115. }
  116. err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handle, O_RDONLY, &fd);
  117. if (err < 0) {
  118. err = errno;
  119. av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
  120. "framebuffer handle: %s.\n", strerror(errno));
  121. return AVERROR(err);
  122. }
  123. desc = av_mallocz(sizeof(*desc));
  124. if (!desc)
  125. return AVERROR(ENOMEM);
  126. *desc = (AVDRMFrameDescriptor) {
  127. .nb_objects = 1,
  128. .objects[0] = {
  129. .fd = fd,
  130. .size = fb->height * fb->pitch,
  131. .format_modifier = ctx->drm_format_modifier,
  132. },
  133. .nb_layers = 1,
  134. .layers[0] = {
  135. .format = ctx->drm_format,
  136. .nb_planes = 1,
  137. .planes[0] = {
  138. .object_index = 0,
  139. .offset = 0,
  140. .pitch = fb->pitch,
  141. },
  142. },
  143. };
  144. frame = av_frame_alloc();
  145. if (!frame)
  146. return AVERROR(ENOMEM);
  147. frame->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
  148. if (!frame->hw_frames_ctx)
  149. return AVERROR(ENOMEM);
  150. frame->buf[0] = av_buffer_create((uint8_t*)desc, sizeof(*desc),
  151. &kmsgrab_free_desc, avctx, 0);
  152. if (!frame->buf[0])
  153. return AVERROR(ENOMEM);
  154. frame->data[0] = (uint8_t*)desc;
  155. frame->format = AV_PIX_FMT_DRM_PRIME;
  156. frame->width = fb->width;
  157. frame->height = fb->height;
  158. drmModeFreeFB(fb);
  159. drmModeFreePlane(plane);
  160. pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
  161. &kmsgrab_free_frame, avctx, 0);
  162. if (!pkt->buf)
  163. return AVERROR(ENOMEM);
  164. pkt->data = (uint8_t*)frame;
  165. pkt->size = sizeof(*frame);
  166. pkt->pts = now;
  167. pkt->flags |= AV_PKT_FLAG_TRUSTED;
  168. return 0;
  169. }
  170. static const struct {
  171. enum AVPixelFormat pixfmt;
  172. uint32_t drm_format;
  173. } kmsgrab_formats[] = {
  174. #ifdef DRM_FORMAT_R8
  175. { AV_PIX_FMT_GRAY8, DRM_FORMAT_R8 },
  176. #endif
  177. #ifdef DRM_FORMAT_R16
  178. { AV_PIX_FMT_GRAY16LE, DRM_FORMAT_R16 },
  179. { AV_PIX_FMT_GRAY16BE, DRM_FORMAT_R16 | DRM_FORMAT_BIG_ENDIAN },
  180. #endif
  181. { AV_PIX_FMT_BGR8, DRM_FORMAT_BGR233 },
  182. { AV_PIX_FMT_RGB555LE, DRM_FORMAT_XRGB1555 },
  183. { AV_PIX_FMT_RGB555BE, DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
  184. { AV_PIX_FMT_BGR555LE, DRM_FORMAT_XBGR1555 },
  185. { AV_PIX_FMT_BGR555BE, DRM_FORMAT_XBGR1555 | DRM_FORMAT_BIG_ENDIAN },
  186. { AV_PIX_FMT_RGB565LE, DRM_FORMAT_RGB565 },
  187. { AV_PIX_FMT_RGB565BE, DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN },
  188. { AV_PIX_FMT_BGR565LE, DRM_FORMAT_BGR565 },
  189. { AV_PIX_FMT_BGR565BE, DRM_FORMAT_BGR565 | DRM_FORMAT_BIG_ENDIAN },
  190. { AV_PIX_FMT_RGB24, DRM_FORMAT_RGB888 },
  191. { AV_PIX_FMT_BGR24, DRM_FORMAT_BGR888 },
  192. { AV_PIX_FMT_0RGB, DRM_FORMAT_BGRX8888 },
  193. { AV_PIX_FMT_0BGR, DRM_FORMAT_RGBX8888 },
  194. { AV_PIX_FMT_RGB0, DRM_FORMAT_XBGR8888 },
  195. { AV_PIX_FMT_BGR0, DRM_FORMAT_XRGB8888 },
  196. { AV_PIX_FMT_ARGB, DRM_FORMAT_BGRA8888 },
  197. { AV_PIX_FMT_ABGR, DRM_FORMAT_RGBA8888 },
  198. { AV_PIX_FMT_RGBA, DRM_FORMAT_ABGR8888 },
  199. { AV_PIX_FMT_BGRA, DRM_FORMAT_ARGB8888 },
  200. { AV_PIX_FMT_YUYV422, DRM_FORMAT_YUYV },
  201. { AV_PIX_FMT_YVYU422, DRM_FORMAT_YVYU },
  202. { AV_PIX_FMT_UYVY422, DRM_FORMAT_UYVY },
  203. };
  204. static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
  205. {
  206. KMSGrabContext *ctx = avctx->priv_data;
  207. drmModePlaneRes *plane_res = NULL;
  208. drmModePlane *plane = NULL;
  209. drmModeFB *fb = NULL;
  210. AVStream *stream;
  211. int err, i;
  212. for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
  213. if (kmsgrab_formats[i].pixfmt == ctx->format) {
  214. ctx->drm_format = kmsgrab_formats[i].drm_format;
  215. break;
  216. }
  217. }
  218. if (i >= FF_ARRAY_ELEMS(kmsgrab_formats)) {
  219. av_log(avctx, AV_LOG_ERROR, "Unsupported format %s.\n",
  220. av_get_pix_fmt_name(ctx->format));
  221. return AVERROR(EINVAL);
  222. }
  223. err = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_DRM,
  224. ctx->device_path, NULL, 0);
  225. if (err < 0) {
  226. av_log(avctx, AV_LOG_ERROR, "Failed to open DRM device.\n");
  227. return err;
  228. }
  229. ctx->device = (AVHWDeviceContext*) ctx->device_ref->data;
  230. ctx->hwctx = (AVDRMDeviceContext*)ctx->device->hwctx;
  231. err = drmSetClientCap(ctx->hwctx->fd,
  232. DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
  233. if (err < 0) {
  234. av_log(avctx, AV_LOG_WARNING, "Failed to set universal planes "
  235. "capability: primary planes will not be usable.\n");
  236. }
  237. if (ctx->source_plane > 0) {
  238. plane = drmModeGetPlane(ctx->hwctx->fd, ctx->source_plane);
  239. if (!plane) {
  240. err = errno;
  241. av_log(avctx, AV_LOG_ERROR, "Failed to get plane %"PRId64": "
  242. "%s.\n", ctx->source_plane, strerror(err));
  243. err = AVERROR(err);
  244. goto fail;
  245. }
  246. if (plane->fb_id == 0) {
  247. av_log(avctx, AV_LOG_ERROR, "Plane %"PRId64" does not have "
  248. "an attached framebuffer.\n", ctx->source_plane);
  249. err = AVERROR(EINVAL);
  250. goto fail;
  251. }
  252. } else {
  253. plane_res = drmModeGetPlaneResources(ctx->hwctx->fd);
  254. if (!plane_res) {
  255. av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
  256. "resources: %s.\n", strerror(errno));
  257. err = AVERROR(EINVAL);
  258. goto fail;
  259. }
  260. for (i = 0; i < plane_res->count_planes; i++) {
  261. plane = drmModeGetPlane(ctx->hwctx->fd,
  262. plane_res->planes[i]);
  263. if (!plane) {
  264. err = errno;
  265. av_log(avctx, AV_LOG_VERBOSE, "Failed to get "
  266. "plane %"PRIu32": %s.\n",
  267. plane_res->planes[i], strerror(err));
  268. continue;
  269. }
  270. av_log(avctx, AV_LOG_DEBUG, "Plane %"PRIu32": "
  271. "CRTC %"PRIu32" FB %"PRIu32".\n",
  272. plane->plane_id, plane->crtc_id, plane->fb_id);
  273. if ((ctx->source_crtc > 0 &&
  274. plane->crtc_id != ctx->source_crtc) ||
  275. plane->fb_id == 0) {
  276. // Either not connected to the target source CRTC
  277. // or not active.
  278. drmModeFreePlane(plane);
  279. plane = NULL;
  280. continue;
  281. }
  282. break;
  283. }
  284. if (i == plane_res->count_planes) {
  285. if (ctx->source_crtc > 0) {
  286. av_log(avctx, AV_LOG_ERROR, "No usable planes found on "
  287. "CRTC %"PRId64".\n", ctx->source_crtc);
  288. } else {
  289. av_log(avctx, AV_LOG_ERROR, "No usable planes found.\n");
  290. }
  291. err = AVERROR(EINVAL);
  292. goto fail;
  293. }
  294. av_log(avctx, AV_LOG_INFO, "Using plane %"PRIu32" to "
  295. "locate framebuffers.\n", plane->plane_id);
  296. }
  297. ctx->plane_id = plane->plane_id;
  298. fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
  299. if (!fb) {
  300. err = errno;
  301. av_log(avctx, AV_LOG_ERROR, "Failed to get "
  302. "framebuffer %"PRIu32": %s.\n",
  303. plane->fb_id, strerror(err));
  304. err = AVERROR(err);
  305. goto fail;
  306. }
  307. av_log(avctx, AV_LOG_INFO, "Template framebuffer is %"PRIu32": "
  308. "%"PRIu32"x%"PRIu32" %"PRIu32"bpp %"PRIu32"b depth.\n",
  309. fb->fb_id, fb->width, fb->height, fb->bpp, fb->depth);
  310. ctx->width = fb->width;
  311. ctx->height = fb->height;
  312. if (!fb->handle) {
  313. av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
  314. "maybe you need some additional capabilities?\n");
  315. err = AVERROR(EINVAL);
  316. goto fail;
  317. }
  318. stream = avformat_new_stream(avctx, NULL);
  319. if (!stream) {
  320. err = AVERROR(ENOMEM);
  321. goto fail;
  322. }
  323. stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  324. stream->codecpar->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME;
  325. stream->codecpar->width = fb->width;
  326. stream->codecpar->height = fb->height;
  327. stream->codecpar->format = AV_PIX_FMT_DRM_PRIME;
  328. avpriv_set_pts_info(stream, 64, 1, 1000000);
  329. ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  330. if (!ctx->frames_ref) {
  331. err = AVERROR(ENOMEM);
  332. goto fail;
  333. }
  334. ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
  335. ctx->frames->format = AV_PIX_FMT_DRM_PRIME;
  336. ctx->frames->sw_format = ctx->format,
  337. ctx->frames->width = fb->width;
  338. ctx->frames->height = fb->height;
  339. err = av_hwframe_ctx_init(ctx->frames_ref);
  340. if (err < 0) {
  341. av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
  342. "hardware frames context: %d.\n", err);
  343. goto fail;
  344. }
  345. ctx->frame_delay = av_rescale_q(1, (AVRational) { ctx->framerate.den,
  346. ctx->framerate.num }, AV_TIME_BASE_Q);
  347. err = 0;
  348. fail:
  349. if (plane_res)
  350. drmModeFreePlaneResources(plane_res);
  351. if (plane)
  352. drmModeFreePlane(plane);
  353. if (fb)
  354. drmModeFreeFB(fb);
  355. return err;
  356. }
  357. static av_cold int kmsgrab_read_close(AVFormatContext *avctx)
  358. {
  359. KMSGrabContext *ctx = avctx->priv_data;
  360. av_buffer_unref(&ctx->frames_ref);
  361. av_buffer_unref(&ctx->device_ref);
  362. return 0;
  363. }
  364. #define OFFSET(x) offsetof(KMSGrabContext, x)
  365. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  366. static const AVOption options[] = {
  367. { "device", "DRM device path",
  368. OFFSET(device_path), AV_OPT_TYPE_STRING,
  369. { .str = "/dev/dri/card0" }, 0, 0, FLAGS },
  370. { "format", "Pixel format for framebuffer",
  371. OFFSET(format), AV_OPT_TYPE_PIXEL_FMT,
  372. { .i64 = AV_PIX_FMT_BGR0 }, 0, UINT32_MAX, FLAGS },
  373. { "format_modifier", "DRM format modifier for framebuffer",
  374. OFFSET(drm_format_modifier), AV_OPT_TYPE_INT64,
  375. { .i64 = DRM_FORMAT_MOD_NONE }, 0, INT64_MAX, FLAGS },
  376. { "crtc_id", "CRTC ID to define capture source",
  377. OFFSET(source_crtc), AV_OPT_TYPE_INT64,
  378. { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
  379. { "plane_id", "Plane ID to define capture source",
  380. OFFSET(source_plane), AV_OPT_TYPE_INT64,
  381. { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
  382. { "framerate", "Framerate to capture at",
  383. OFFSET(framerate), AV_OPT_TYPE_RATIONAL,
  384. { .dbl = 30.0 }, 0, 1000, FLAGS },
  385. { NULL },
  386. };
  387. static const AVClass kmsgrab_class = {
  388. .class_name = "kmsgrab indev",
  389. .item_name = av_default_item_name,
  390. .option = options,
  391. .version = LIBAVUTIL_VERSION_INT,
  392. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  393. };
  394. AVInputFormat ff_kmsgrab_demuxer = {
  395. .name = "kmsgrab",
  396. .long_name = NULL_IF_CONFIG_SMALL("KMS screen capture"),
  397. .priv_data_size = sizeof(KMSGrabContext),
  398. .read_header = &kmsgrab_read_header,
  399. .read_packet = &kmsgrab_read_packet,
  400. .read_close = &kmsgrab_read_close,
  401. .flags = AVFMT_NOFILE,
  402. .priv_class = &kmsgrab_class,
  403. };