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. fb = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
  142. if (!fb) {
  143. err = errno;
  144. av_log(avctx, AV_LOG_ERROR, "Failed to get framebuffer "
  145. "%"PRIu32": %s.\n", plane->fb_id, strerror(err));
  146. return AVERROR(err);
  147. }
  148. if (fb->pixel_format != ctx->drm_format) {
  149. av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
  150. "format changed: now %"PRIx32".\n",
  151. ctx->plane_id, fb->pixel_format);
  152. err = AVERROR(EIO);
  153. goto fail;
  154. }
  155. if (fb->modifier != ctx->drm_format_modifier) {
  156. av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
  157. "format modifier changed: now %"PRIx64".\n",
  158. ctx->plane_id, fb->modifier);
  159. err = AVERROR(EIO);
  160. goto fail;
  161. }
  162. if (fb->width != ctx->width || fb->height != ctx->height) {
  163. av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" framebuffer "
  164. "dimensions changed: now %"PRIu32"x%"PRIu32".\n",
  165. ctx->plane_id, fb->width, fb->height);
  166. err = AVERROR(EIO);
  167. goto fail;
  168. }
  169. if (!fb->handles[0]) {
  170. av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer.\n");
  171. err = AVERROR(EIO);
  172. goto fail;
  173. }
  174. *desc = (AVDRMFrameDescriptor) {
  175. .nb_layers = 1,
  176. .layers[0] = {
  177. .format = ctx->drm_format,
  178. },
  179. };
  180. nb_objects = 0;
  181. for (i = 0; i < 4 && fb->handles[i]; i++) {
  182. size_t size;
  183. int dup = 0, j, obj;
  184. size = fb->offsets[i] + fb->height * fb->pitches[i];
  185. for (j = 0; j < i; j++) {
  186. if (fb->handles[i] == fb->handles[j]) {
  187. dup = 1;
  188. break;
  189. }
  190. }
  191. if (dup) {
  192. obj = desc->layers[0].planes[j].object_index;
  193. if (desc->objects[j].size < size)
  194. desc->objects[j].size = size;
  195. desc->layers[0].planes[i] = (AVDRMPlaneDescriptor) {
  196. .object_index = obj,
  197. .offset = fb->offsets[i],
  198. .pitch = fb->pitches[i],
  199. };
  200. } else {
  201. int fd;
  202. err = drmPrimeHandleToFD(ctx->hwctx->fd, fb->handles[i],
  203. O_RDONLY, &fd);
  204. if (err < 0) {
  205. err = errno;
  206. av_log(avctx, AV_LOG_ERROR, "Failed to get PRIME fd from "
  207. "framebuffer handle: %s.\n", strerror(err));
  208. err = AVERROR(err);
  209. goto fail;
  210. }
  211. obj = nb_objects++;
  212. desc->objects[obj] = (AVDRMObjectDescriptor) {
  213. .fd = fd,
  214. .size = size,
  215. .format_modifier = fb->modifier,
  216. };
  217. desc->layers[0].planes[i] = (AVDRMPlaneDescriptor) {
  218. .object_index = obj,
  219. .offset = fb->offsets[i],
  220. .pitch = fb->pitches[i],
  221. };
  222. }
  223. }
  224. desc->nb_objects = nb_objects;
  225. desc->layers[0].nb_planes = i;
  226. err = 0;
  227. fail:
  228. drmModeFreeFB2(fb);
  229. return err;
  230. }
  231. #endif
  232. static int kmsgrab_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  233. {
  234. KMSGrabContext *ctx = avctx->priv_data;
  235. drmModePlane *plane = NULL;
  236. AVDRMFrameDescriptor *desc = NULL;
  237. AVFrame *frame = NULL;
  238. int64_t now;
  239. int err;
  240. now = av_gettime();
  241. if (ctx->frame_last) {
  242. int64_t delay;
  243. while (1) {
  244. delay = ctx->frame_last + ctx->frame_delay - now;
  245. if (delay <= 0)
  246. break;
  247. av_usleep(delay);
  248. now = av_gettime();
  249. }
  250. }
  251. ctx->frame_last = now;
  252. plane = drmModeGetPlane(ctx->hwctx->fd, ctx->plane_id);
  253. if (!plane) {
  254. err = errno;
  255. av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
  256. "%"PRIu32": %s.\n", ctx->plane_id, strerror(err));
  257. err = AVERROR(err);
  258. goto fail;
  259. }
  260. if (!plane->fb_id) {
  261. av_log(avctx, AV_LOG_ERROR, "Plane %"PRIu32" no longer has "
  262. "an associated framebuffer.\n", ctx->plane_id);
  263. err = AVERROR(EIO);
  264. goto fail;
  265. }
  266. desc = av_mallocz(sizeof(*desc));
  267. if (!desc) {
  268. err = AVERROR(ENOMEM);
  269. goto fail;
  270. }
  271. #if HAVE_LIBDRM_GETFB2
  272. if (ctx->fb2_available)
  273. err = kmsgrab_get_fb2(avctx, plane, desc);
  274. else
  275. #endif
  276. err = kmsgrab_get_fb(avctx, plane, desc);
  277. if (err < 0)
  278. goto fail;
  279. frame = av_frame_alloc();
  280. if (!frame) {
  281. err = AVERROR(ENOMEM);
  282. goto fail;
  283. }
  284. frame->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
  285. if (!frame->hw_frames_ctx) {
  286. err = AVERROR(ENOMEM);
  287. goto fail;
  288. }
  289. frame->buf[0] = av_buffer_create((uint8_t*)desc, sizeof(*desc),
  290. &kmsgrab_free_desc, avctx, 0);
  291. if (!frame->buf[0]) {
  292. err = AVERROR(ENOMEM);
  293. goto fail;
  294. }
  295. frame->data[0] = (uint8_t*)desc;
  296. frame->format = AV_PIX_FMT_DRM_PRIME;
  297. frame->width = ctx->width;
  298. frame->height = ctx->height;
  299. drmModeFreePlane(plane);
  300. plane = NULL;
  301. desc = NULL;
  302. pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
  303. &kmsgrab_free_frame, avctx, 0);
  304. if (!pkt->buf) {
  305. err = AVERROR(ENOMEM);
  306. goto fail;
  307. }
  308. pkt->data = (uint8_t*)frame;
  309. pkt->size = sizeof(*frame);
  310. pkt->pts = now;
  311. pkt->flags |= AV_PKT_FLAG_TRUSTED;
  312. return 0;
  313. fail:
  314. drmModeFreePlane(plane);
  315. av_freep(&desc);
  316. av_frame_free(&frame);
  317. return err;
  318. }
  319. static const struct {
  320. enum AVPixelFormat pixfmt;
  321. uint32_t drm_format;
  322. } kmsgrab_formats[] = {
  323. // Monochrome.
  324. #ifdef DRM_FORMAT_R8
  325. { AV_PIX_FMT_GRAY8, DRM_FORMAT_R8 },
  326. #endif
  327. #ifdef DRM_FORMAT_R16
  328. { AV_PIX_FMT_GRAY16LE, DRM_FORMAT_R16 },
  329. { AV_PIX_FMT_GRAY16BE, DRM_FORMAT_R16 | DRM_FORMAT_BIG_ENDIAN },
  330. #endif
  331. // <8-bit RGB.
  332. { AV_PIX_FMT_BGR8, DRM_FORMAT_BGR233 },
  333. { AV_PIX_FMT_RGB555LE, DRM_FORMAT_XRGB1555 },
  334. { AV_PIX_FMT_RGB555BE, DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
  335. { AV_PIX_FMT_BGR555LE, DRM_FORMAT_XBGR1555 },
  336. { AV_PIX_FMT_BGR555BE, DRM_FORMAT_XBGR1555 | DRM_FORMAT_BIG_ENDIAN },
  337. { AV_PIX_FMT_RGB565LE, DRM_FORMAT_RGB565 },
  338. { AV_PIX_FMT_RGB565BE, DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN },
  339. { AV_PIX_FMT_BGR565LE, DRM_FORMAT_BGR565 },
  340. { AV_PIX_FMT_BGR565BE, DRM_FORMAT_BGR565 | DRM_FORMAT_BIG_ENDIAN },
  341. // 8-bit RGB.
  342. { AV_PIX_FMT_RGB24, DRM_FORMAT_RGB888 },
  343. { AV_PIX_FMT_BGR24, DRM_FORMAT_BGR888 },
  344. { AV_PIX_FMT_0RGB, DRM_FORMAT_BGRX8888 },
  345. { AV_PIX_FMT_0BGR, DRM_FORMAT_RGBX8888 },
  346. { AV_PIX_FMT_RGB0, DRM_FORMAT_XBGR8888 },
  347. { AV_PIX_FMT_BGR0, DRM_FORMAT_XRGB8888 },
  348. { AV_PIX_FMT_ARGB, DRM_FORMAT_BGRA8888 },
  349. { AV_PIX_FMT_ABGR, DRM_FORMAT_RGBA8888 },
  350. { AV_PIX_FMT_RGBA, DRM_FORMAT_ABGR8888 },
  351. { AV_PIX_FMT_BGRA, DRM_FORMAT_ARGB8888 },
  352. // 10-bit RGB.
  353. { AV_PIX_FMT_X2RGB10LE, DRM_FORMAT_XRGB2101010 },
  354. { AV_PIX_FMT_X2RGB10BE, DRM_FORMAT_XRGB2101010 | DRM_FORMAT_BIG_ENDIAN },
  355. // 8-bit YUV 4:2:0.
  356. { AV_PIX_FMT_NV12, DRM_FORMAT_NV12 },
  357. // 8-bit YUV 4:2:2.
  358. { AV_PIX_FMT_YUYV422, DRM_FORMAT_YUYV },
  359. { AV_PIX_FMT_YVYU422, DRM_FORMAT_YVYU },
  360. { AV_PIX_FMT_UYVY422, DRM_FORMAT_UYVY },
  361. };
  362. static av_cold int kmsgrab_read_header(AVFormatContext *avctx)
  363. {
  364. KMSGrabContext *ctx = avctx->priv_data;
  365. drmModePlaneRes *plane_res = NULL;
  366. drmModePlane *plane = NULL;
  367. drmModeFB *fb = NULL;
  368. #if HAVE_LIBDRM_GETFB2
  369. drmModeFB2 *fb2 = NULL;
  370. #endif
  371. AVStream *stream;
  372. int err, i;
  373. err = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_DRM,
  374. ctx->device_path, NULL, 0);
  375. if (err < 0) {
  376. av_log(avctx, AV_LOG_ERROR, "Failed to open DRM device.\n");
  377. return err;
  378. }
  379. ctx->device = (AVHWDeviceContext*) ctx->device_ref->data;
  380. ctx->hwctx = (AVDRMDeviceContext*)ctx->device->hwctx;
  381. err = drmSetClientCap(ctx->hwctx->fd,
  382. DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
  383. if (err < 0) {
  384. av_log(avctx, AV_LOG_WARNING, "Failed to set universal planes "
  385. "capability: primary planes will not be usable.\n");
  386. }
  387. if (ctx->source_plane > 0) {
  388. plane = drmModeGetPlane(ctx->hwctx->fd, ctx->source_plane);
  389. if (!plane) {
  390. err = errno;
  391. av_log(avctx, AV_LOG_ERROR, "Failed to get plane %"PRId64": "
  392. "%s.\n", ctx->source_plane, strerror(err));
  393. err = AVERROR(err);
  394. goto fail;
  395. }
  396. if (plane->fb_id == 0) {
  397. av_log(avctx, AV_LOG_ERROR, "Plane %"PRId64" does not have "
  398. "an attached framebuffer.\n", ctx->source_plane);
  399. err = AVERROR(EINVAL);
  400. goto fail;
  401. }
  402. } else {
  403. plane_res = drmModeGetPlaneResources(ctx->hwctx->fd);
  404. if (!plane_res) {
  405. err = errno;
  406. av_log(avctx, AV_LOG_ERROR, "Failed to get plane "
  407. "resources: %s.\n", strerror(err));
  408. err = AVERROR(err);
  409. goto fail;
  410. }
  411. for (i = 0; i < plane_res->count_planes; i++) {
  412. plane = drmModeGetPlane(ctx->hwctx->fd,
  413. plane_res->planes[i]);
  414. if (!plane) {
  415. err = errno;
  416. av_log(avctx, AV_LOG_VERBOSE, "Failed to get "
  417. "plane %"PRIu32": %s.\n",
  418. plane_res->planes[i], strerror(err));
  419. continue;
  420. }
  421. av_log(avctx, AV_LOG_DEBUG, "Plane %"PRIu32": "
  422. "CRTC %"PRIu32" FB %"PRIu32".\n",
  423. plane->plane_id, plane->crtc_id, plane->fb_id);
  424. if ((ctx->source_crtc > 0 &&
  425. plane->crtc_id != ctx->source_crtc) ||
  426. plane->fb_id == 0) {
  427. // Either not connected to the target source CRTC
  428. // or not active.
  429. drmModeFreePlane(plane);
  430. plane = NULL;
  431. continue;
  432. }
  433. break;
  434. }
  435. if (i == plane_res->count_planes) {
  436. if (ctx->source_crtc > 0) {
  437. av_log(avctx, AV_LOG_ERROR, "No usable planes found on "
  438. "CRTC %"PRId64".\n", ctx->source_crtc);
  439. } else {
  440. av_log(avctx, AV_LOG_ERROR, "No usable planes found.\n");
  441. }
  442. err = AVERROR(EINVAL);
  443. goto fail;
  444. }
  445. av_log(avctx, AV_LOG_INFO, "Using plane %"PRIu32" to "
  446. "locate framebuffers.\n", plane->plane_id);
  447. }
  448. ctx->plane_id = plane->plane_id;
  449. #if HAVE_LIBDRM_GETFB2
  450. fb2 = drmModeGetFB2(ctx->hwctx->fd, plane->fb_id);
  451. if (!fb2 && errno == ENOSYS) {
  452. av_log(avctx, AV_LOG_INFO, "GETFB2 not supported, "
  453. "will try to use GETFB instead.\n");
  454. } else if (!fb2) {
  455. err = errno;
  456. av_log(avctx, AV_LOG_ERROR, "Failed to get "
  457. "framebuffer %"PRIu32": %s.\n",
  458. plane->fb_id, strerror(err));
  459. err = AVERROR(err);
  460. goto fail;
  461. } else {
  462. av_log(avctx, AV_LOG_INFO, "Template framebuffer is "
  463. "%"PRIu32": %"PRIu32"x%"PRIu32" "
  464. "format %"PRIx32" modifier %"PRIx64" flags %"PRIx32".\n",
  465. fb2->fb_id, fb2->width, fb2->height,
  466. fb2->pixel_format, fb2->modifier, fb2->flags);
  467. ctx->width = fb2->width;
  468. ctx->height = fb2->height;
  469. if (!fb2->handles[0]) {
  470. av_log(avctx, AV_LOG_ERROR, "No handle set on framebuffer: "
  471. "maybe you need some additional capabilities?\n");
  472. err = AVERROR(EINVAL);
  473. goto fail;
  474. }
  475. for (i = 0; i < FF_ARRAY_ELEMS(kmsgrab_formats); i++) {
  476. if (kmsgrab_formats[i].drm_format == fb2->pixel_format) {
  477. if (ctx->format != AV_PIX_FMT_NONE &&
  478. ctx->format != kmsgrab_formats[i].pixfmt) {
  479. av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
  480. "%"PRIx32" does not match expected format.\n",
  481. fb2->pixel_format);
  482. err = AVERROR(EINVAL);
  483. goto fail;
  484. }
  485. ctx->drm_format = fb2->pixel_format;
  486. ctx->format = kmsgrab_formats[i].pixfmt;
  487. break;
  488. }
  489. }
  490. if (i == FF_ARRAY_ELEMS(kmsgrab_formats)) {
  491. av_log(avctx, AV_LOG_ERROR, "Framebuffer pixel format "
  492. "%"PRIx32" is not a known supported format.\n",
  493. fb2->pixel_format);
  494. err = AVERROR(EINVAL);
  495. goto fail;
  496. }
  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. 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. AVInputFormat ff_kmsgrab_demuxer = {
  628. .name = "kmsgrab",
  629. .long_name = NULL_IF_CONFIG_SMALL("KMS screen capture"),
  630. .priv_data_size = sizeof(KMSGrabContext),
  631. .read_header = &kmsgrab_read_header,
  632. .read_packet = &kmsgrab_read_packet,
  633. .read_close = &kmsgrab_read_close,
  634. .flags = AVFMT_NOFILE,
  635. .priv_class = &kmsgrab_class,
  636. };