kmsgrab.c 22 KB

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