kmsgrab.c 22 KB

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