You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

720 lines
22KB

  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();
  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();
  245. }
  246. }
  247. ctx->frame_last = now;
  248. plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
  249. if (!plane) {
  250. err = errno;
  251. av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
  252. "%"PRIu32": %s.\n", ctx->plane_id, strerror(err));
  253. err = AVERROR(err);
  254. goto fail;
  255. }
  256. if (!plane->fb_id) {
  257. av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" no longer has "
  258. "an associated framebuffer.\n", ctx->plane_id);
  259. err = AVERROR(EIO);
  260. goto fail;
  261. }
  262. desc = av_mallocz(sizeof(*desc));
  263. if (!desc) {
  264. err = AVERROR(ENOMEM);
  265. goto fail;
  266. }
  267. #if HAVE_LIBDRM_GETFB2
  268. if (ctx->fb2_available)
  269. err = kmsgrab_get_fb2(avctx, plane, desc);
  270. else
  271. #endif
  272. err = kmsgrab_get_fb(avctx, plane, desc);
  273. if (err < 0)
  274. goto fail;
  275. frame = av_frame_alloc();
  276. if (!frame) {
  277. err = AVERROR(ENOMEM);
  278. goto fail;
  279. }
  280. frame->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
  281. if (!frame->hw_frames_ctx) {
  282. err = AVERROR(ENOMEM);
  283. goto fail;
  284. }
  285. frame->buf[0] = av_buffer_create((uint8_t*)desc, sizeof(*desc),
  286. &kmsgrab_free_desc, avctx, 0);
  287. if (!frame->buf[0]) {
  288. err = AVERROR(ENOMEM);
  289. goto fail;
  290. }
  291. frame->data[0] = (uint8_t*)desc;
  292. frame->format = AV_PIX_FMT_DRM_PRIME;
  293. frame->width = ctx->width;
  294. frame->height = ctx->height;
  295. drmModeFreePlane(plane);
  296. plane = NULL;
  297. desc = NULL;
  298. pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
  299. &kmsgrab_free_frame, avctx, 0);
  300. if (!pkt->buf) {
  301. err = AVERROR(ENOMEM);
  302. goto fail;
  303. }
  304. pkt->data = (uint8_t*)frame;
  305. pkt->size = sizeof(*frame);
  306. pkt->pts = now;
  307. pkt->flags |= AV_PKT_FLAG_TRUSTED;
  308. return 0;
  309. fail:
  310. drmModeFreePlane(plane);
  311. av_freep(&desc);
  312. av_frame_free(&frame);
  313. return err;
  314. }
  315. static const struct {
  316. enum AVPixelFormat pixfmt;
  317. uint32_t drm_format;
  318. } kmsgrab_formats[] = {
  319. // Monochrome.
  320. #ifdef DRM_FORMAT_R8
  321. { AV_PIX_FMT_GRAY8, DRM_FORMAT_R8 },
  322. #endif
  323. #ifdef DRM_FORMAT_R16
  324. { AV_PIX_FMT_GRAY16LE, DRM_FORMAT_R16 },
  325. { AV_PIX_FMT_GRAY16BE, DRM_FORMAT_R16 | DRM_FORMAT_BIG_ENDIAN },
  326. #endif
  327. // <8-bit RGB.
  328. { AV_PIX_FMT_BGR8, DRM_FORMAT_BGR233 },
  329. { AV_PIX_FMT_RGB555LE, DRM_FORMAT_XRGB1555 },
  330. { AV_PIX_FMT_RGB555BE, DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
  331. { AV_PIX_FMT_BGR555LE, DRM_FORMAT_XBGR1555 },
  332. { AV_PIX_FMT_BGR555BE, DRM_FORMAT_XBGR1555 | DRM_FORMAT_BIG_ENDIAN },
  333. { AV_PIX_FMT_RGB565LE, DRM_FORMAT_RGB565 },
  334. { AV_PIX_FMT_RGB565BE, DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN },
  335. { AV_PIX_FMT_BGR565LE, DRM_FORMAT_BGR565 },
  336. { AV_PIX_FMT_BGR565BE, DRM_FORMAT_BGR565 | DRM_FORMAT_BIG_ENDIAN },
  337. // 8-bit RGB.
  338. { AV_PIX_FMT_RGB24, DRM_FORMAT_RGB888 },
  339. { AV_PIX_FMT_BGR24, DRM_FORMAT_BGR888 },
  340. { AV_PIX_FMT_0RGB, DRM_FORMAT_BGRX8888 },
  341. { AV_PIX_FMT_0BGR, DRM_FORMAT_RGBX8888 },
  342. { AV_PIX_FMT_RGB0, DRM_FORMAT_XBGR8888 },
  343. { AV_PIX_FMT_BGR0, DRM_FORMAT_XRGB8888 },
  344. { AV_PIX_FMT_ARGB, DRM_FORMAT_BGRA8888 },
  345. { AV_PIX_FMT_ABGR, DRM_FORMAT_RGBA8888 },
  346. { AV_PIX_FMT_RGBA, DRM_FORMAT_ABGR8888 },
  347. { AV_PIX_FMT_BGRA, DRM_FORMAT_ARGB8888 },
  348. // 10-bit RGB.
  349. { AV_PIX_FMT_X2RGB10LE, DRM_FORMAT_XRGB2101010 },
  350. { AV_PIX_FMT_X2RGB10BE, DRM_FORMAT_XRGB2101010 | DRM_FORMAT_BIG_ENDIAN },
  351. // 8-bit YUV 4:2:0.
  352. { AV_PIX_FMT_NV12, DRM_FORMAT_NV12 },
  353. // 8-bit YUV 4:2:2.
  354. { AV_PIX_FMT_YUYV422, DRM_FORMAT_YUYV },
  355. { AV_PIX_FMT_YVYU422, DRM_FORMAT_YVYU },
  356. { AV_PIX_FMT_UYVY422, DRM_FORMAT_UYVY },
  357. };
  358. static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
  359. {
  360. KMSGrabContext *ctx = avctx->priv_data;
  361. drmModePlaneRes *plane_res = NULL;
  362. drmModePlane *plane = NULL;
  363. drmModeFB *fb = NULL;
  364. #if HAVE_LIBDRM_GETFB2
  365. drmModeFB2 *fb2 = NULL;
  366. #endif
  367. AVStream *stream;
  368. int err, i;
  369. err = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_DRM,
  370. ctx->device_path, NULL, 0);
  371. if (err < 0) {
  372. av_log(avctx, AV_LOG_ERROR, "Failed to open DRM device.\n");
  373. return err;
  374. }
  375. ctx->device = (AVHWDeviceContext*) ctx->device_ref->data;
  376. ctx->hwctx = (AVDRMDeviceContext*)ctx->device->hwctx;
  377. err = drmSetClientCap(ctx->hwctx->fd,
  378. DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
  379. if (err < 0) {
  380. av_log(avctx, AV_LOG_WARNING, "Failed to set universal planes "
  381. "capability: primary planes will not be usable.\n");
  382. }
  383. if (ctx->source_plane > 0) {
  384. plane = drmModeGetPlane(ctx->hwctx->fd, ctx->source_plane);
  385. if (!plane) {
  386. err = errno;
  387. av_log(avctx, AV_LOG_ERROR, "Failed to get plane %"PRId64": "
  388. "%s.\n", ctx->source_plane, strerror(err));
  389. err = AVERROR(err);
  390. goto fail;
  391. }
  392. if (plane->fb_id == 0) {
  393. av_log(avctx, AV_LOG_ERROR, "Plane %"PRId64" does not have "
  394. "an attached framebuffer.\n", ctx->source_plane);
  395. err = AVERROR(EINVAL);
  396. goto fail;
  397. }
  398. } else {
  399. plane_res = drmModeGetPlaneResources(ctx->hwctx->fd);
  400. if (!plane_res) {
  401. err = errno;
  402. av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
  403. "resources: %s.\n", strerror(err));
  404. err = AVERROR(err);
  405. goto fail;
  406. }
  407. for (i = 0; i < plane_res->count_planes; i++) {
  408. plane = drmModeGetPlane(ctx->hwctx->fd,
  409. plane_res->planes[i]);
  410. if (!plane) {
  411. err = errno;
  412. av_log(avctx, AV_LOG_VERBOSE, "Failed to get "
  413. "plane %"PRIu32": %s.\n",
  414. plane_res->planes[i], strerror(err));
  415. continue;
  416. }
  417. av_log(avctx, AV_LOG_DEBUG, "Plane %"PRIu32": "
  418. "CRTC %"PRIu32" FB %"PRIu32".\n",
  419. plane->plane_id, plane->crtc_id, plane->fb_id);
  420. if ((ctx->source_crtc > 0 &&
  421. plane->crtc_id != ctx->source_crtc) ||
  422. plane->fb_id == 0) {
  423. // Either not connected to the target source CRTC
  424. // or not active.
  425. drmModeFreePlane(plane);
  426. plane = NULL;
  427. continue;
  428. }
  429. break;
  430. }
  431. if (i == plane_res->count_planes) {
  432. if (ctx->source_crtc > 0) {
  433. av_log(avctx, AV_LOG_ERROR, "No usable planes found on "
  434. "CRTC %"PRId64".\n", ctx->source_crtc);
  435. } else {
  436. av_log(avctx, AV_LOG_ERROR, "No usable planes found.\n");
  437. }
  438. err = AVERROR(EINVAL);
  439. goto fail;
  440. }
  441. av_log(avctx, AV_LOG_INFO, "Using plane %"PRIu32" to "
  442. "locate framebuffers.\n", plane->plane_id);
  443. }
  444. ctx->plane_id = plane->plane_id;
  445. #if HAVE_LIBDRM_GETFB2
  446. fb2 = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
  447. if (!fb2 && errno == ENOSYS) {
  448. av_log(avctx, AV_LOG_INFO, "GETFB2 not supported, "
  449. "will try to use GETFB instead.\n");
  450. } else if (!fb2) {
  451. err = errno;
  452. av_log(avctx, AV_LOG_ERROR, "Failed to get "
  453. "framebuffer %"PRIu32": %s.\n",
  454. plane->fb_id, strerror(err));
  455. err = AVERROR(err);
  456. goto fail;
  457. } else {
  458. av_log(avctx, AV_LOG_INFO, "Template framebuffer is "
  459. "%"PRIu32": %"PRIu32"x%"PRIu32" "
  460. "format %"PRIx32" modifier %"PRIx64" flags %"PRIx32".\n",
  461. fb2->fb_id, fb2->width, fb2->height,
  462. fb2->pixel_format, fb2->modifier, fb2->flags);
  463. ctx->width = fb2->width;
  464. ctx->height = fb2->height;
  465. if (!fb2->handles[0]) {
  466. av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
  467. "maybe you need some additional capabilities?\n");
  468. err = AVERROR(EINVAL);
  469. goto fail;
  470. }
  471. for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
  472. if (kmsgrab_formats[i].drm_format == fb2->pixel_format) {
  473. if (ctx->format != AV_PIX_FMT_NONE &&
  474. ctx->format != kmsgrab_formats[i].pixfmt) {
  475. av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
  476. "%"PRIx32" does not match expected format.\n",
  477. fb2->pixel_format);
  478. err = AVERROR(EINVAL);
  479. goto fail;
  480. }
  481. ctx->drm_format = fb2->pixel_format;
  482. ctx->format = kmsgrab_formats[i].pixfmt;
  483. break;
  484. }
  485. }
  486. if (i == FF_ARRAY_ELEMS(kmsgrab_formats)) {
  487. av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
  488. "%"PRIx32" is not a known supported format.\n",
  489. fb2->pixel_format);
  490. err = AVERROR(EINVAL);
  491. goto fail;
  492. }
  493. if (fb2->flags & DRM_MODE_FB_MODIFIERS) {
  494. if (ctx->drm_format_modifier != DRM_FORMAT_MOD_INVALID &&
  495. ctx->drm_format_modifier != fb2->modifier) {
  496. av_log(avctx, AV_LOG_ERROR, "Framebuffer format modifier "
  497. "%"PRIx64" does not match expected modifier.\n",
  498. fb2->modifier);
  499. err = AVERROR(EINVAL);
  500. goto fail;
  501. } else {
  502. ctx->drm_format_modifier = fb2->modifier;
  503. }
  504. }
  505. av_log(avctx, AV_LOG_VERBOSE, "Format is %s, from "
  506. "DRM format %"PRIx32" modifier %"PRIx64".\n",
  507. av_get_pix_fmt_name(ctx->format),
  508. ctx->drm_format, ctx->drm_format_modifier);
  509. ctx->fb2_available = 1;
  510. }
  511. #endif
  512. if (!ctx->fb2_available) {
  513. if (ctx->format == AV_PIX_FMT_NONE) {
  514. // Backward compatibility: assume BGR0 if no format supplied.
  515. ctx->format = AV_PIX_FMT_BGR0;
  516. }
  517. for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
  518. if (kmsgrab_formats[i].pixfmt == ctx->format) {
  519. ctx->drm_format = kmsgrab_formats[i].drm_format;
  520. break;
  521. }
  522. }
  523. if (i >= FF_ARRAY_ELEMS(kmsgrab_formats)) {
  524. av_log(avctx, AV_LOG_ERROR, "Unsupported format %s.\n",
  525. av_get_pix_fmt_name(ctx->format));
  526. return AVERROR(EINVAL);
  527. }
  528. fb = drmModeGetFB(ctx->hwctx->fd, plane->fb_id);
  529. if (!fb) {
  530. err = errno;
  531. av_log(avctx, AV_LOG_ERROR, "Failed to get "
  532. "framebuffer %"PRIu32": %s.\n",
  533. plane->fb_id, strerror(err));
  534. err = AVERROR(err);
  535. goto fail;
  536. }
  537. av_log(avctx, AV_LOG_INFO, "Template framebuffer is %"PRIu32": "
  538. "%"PRIu32"x%"PRIu32" %"PRIu32"bpp %"PRIu32"b depth.\n",
  539. fb->fb_id, fb->width, fb->height, fb->bpp, fb->depth);
  540. ctx->width = fb->width;
  541. ctx->height = fb->height;
  542. if (!fb->handle) {
  543. av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
  544. "maybe you need some additional capabilities?\n");
  545. err = AVERROR(EINVAL);
  546. goto fail;
  547. }
  548. }
  549. stream = avformat_new_stream(avctx, NULL);
  550. if (!stream) {
  551. err = AVERROR(ENOMEM);
  552. goto fail;
  553. }
  554. stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  555. stream->codecpar->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME;
  556. stream->codecpar->width = ctx->width;
  557. stream->codecpar->height = ctx->height;
  558. stream->codecpar->format = AV_PIX_FMT_DRM_PRIME;
  559. avpriv_set_pts_info(stream, 64, 1, 1000000);
  560. ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  561. if (!ctx->frames_ref) {
  562. err = AVERROR(ENOMEM);
  563. goto fail;
  564. }
  565. ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
  566. ctx->frames->format = AV_PIX_FMT_DRM_PRIME;
  567. ctx->frames->sw_format = ctx->format,
  568. ctx->frames->width = ctx->width;
  569. ctx->frames->height = ctx->height;
  570. err = av_hwframe_ctx_init(ctx->frames_ref);
  571. if (err < 0) {
  572. av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
  573. "hardware frames context: %d.\n", err);
  574. goto fail;
  575. }
  576. ctx->frame_delay = av_rescale_q(1, (AVRational) { ctx->framerate.den,
  577. ctx->framerate.num }, AV_TIME_BASE_Q);
  578. err = 0;
  579. fail:
  580. drmModeFreePlaneResources(plane_res);
  581. drmModeFreePlane(plane);
  582. drmModeFreeFB(fb);
  583. #if HAVE_LIBDRM_GETFB2
  584. drmModeFreeFB2(fb2);
  585. #endif
  586. return err;
  587. }
  588. static av_cold int kmsgrab_read_close(AVFormatContext *avctx)
  589. {
  590. KMSGrabContext *ctx = avctx->priv_data;
  591. av_buffer_unref(&ctx->frames_ref);
  592. av_buffer_unref(&ctx->device_ref);
  593. return 0;
  594. }
  595. #define OFFSET(x) offsetof(KMSGrabContext, x)
  596. #define FLAGS AV_OPT_FLAG_DECODING_PARAM
  597. static const AVOption options[] = {
  598. { "device", "DRM device path",
  599. OFFSET(device_path), AV_OPT_TYPE_STRING,
  600. { .str = "/dev/dri/card0" }, 0, 0, FLAGS },
  601. { "format", "Pixel format for framebuffer",
  602. OFFSET(format), AV_OPT_TYPE_PIXEL_FMT,
  603. { .i64 = AV_PIX_FMT_NONE }, -1, INT32_MAX, FLAGS },
  604. { "format_modifier", "DRM format modifier for framebuffer",
  605. OFFSET(drm_format_modifier), AV_OPT_TYPE_INT64,
  606. { .i64 = DRM_FORMAT_MOD_INVALID }, 0, INT64_MAX, FLAGS },
  607. { "crtc_id", "CRTC ID to define capture source",
  608. OFFSET(source_crtc), AV_OPT_TYPE_INT64,
  609. { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
  610. { "plane_id", "Plane ID to define capture source",
  611. OFFSET(source_plane), AV_OPT_TYPE_INT64,
  612. { .i64 = 0 }, 0, UINT32_MAX, FLAGS },
  613. { "framerate", "Framerate to capture at",
  614. OFFSET(framerate), AV_OPT_TYPE_RATIONAL,
  615. { .dbl = 30.0 }, 0, 1000, FLAGS },
  616. { NULL },
  617. };
  618. static const AVClass kmsgrab_class = {
  619. .class_name = "kmsgrab indev",
  620. .item_name = av_default_item_name,
  621. .option = options,
  622. .version = LIBAVUTIL_VERSION_INT,
  623. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  624. };
  625. AVInputFormat ff_kmsgrab_demuxer = {
  626. .name = "kmsgrab",
  627. .long_name = NULL_IF_CONFIG_SMALL("KMS screen capture"),
  628. .priv_data_size = sizeof(KMSGrabContext),
  629. .read_header = &kmsgrab_read_header,
  630. .read_packet = &kmsgrab_read_packet,
  631. .read_close = &kmsgrab_read_close,
  632. .flags = AVFMT_NOFILE,
  633. .priv_class = &kmsgrab_class,
  634. };