kmsgrab.c 22 KB

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