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.

324 lines
8.6KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include <fcntl.h>
  20. #include <sys/mman.h>
  21. #include <unistd.h>
  22. /* This was introduced in version 4.6. And may not exist all without an
  23. * optional package. So to prevent a hard dependency on needing the Linux
  24. * kernel headers to compile, make this optional. */
  25. #if HAVE_LINUX_DMA_BUF_H
  26. #include <linux/dma-buf.h>
  27. #include <sys/ioctl.h>
  28. #endif
  29. #include <drm.h>
  30. #include <xf86drm.h>
  31. #include "avassert.h"
  32. #include "hwcontext.h"
  33. #include "hwcontext_drm.h"
  34. #include "hwcontext_internal.h"
  35. #include "imgutils.h"
  36. static void drm_device_free(AVHWDeviceContext *hwdev)
  37. {
  38. AVDRMDeviceContext *hwctx = hwdev->hwctx;
  39. close(hwctx->fd);
  40. }
  41. static int drm_device_create(AVHWDeviceContext *hwdev, const char *device,
  42. AVDictionary *opts, int flags)
  43. {
  44. AVDRMDeviceContext *hwctx = hwdev->hwctx;
  45. drmVersionPtr version;
  46. hwctx->fd = open(device, O_RDWR);
  47. if (hwctx->fd < 0)
  48. return AVERROR(errno);
  49. version = drmGetVersion(hwctx->fd);
  50. if (!version) {
  51. av_log(hwdev, AV_LOG_ERROR, "Failed to get version information "
  52. "from %s: probably not a DRM device?\n", device);
  53. close(hwctx->fd);
  54. return AVERROR(EINVAL);
  55. }
  56. av_log(hwdev, AV_LOG_VERBOSE, "Opened DRM device %s: driver %s "
  57. "version %d.%d.%d.\n", device, version->name,
  58. version->version_major, version->version_minor,
  59. version->version_patchlevel);
  60. drmFreeVersion(version);
  61. hwdev->free = &drm_device_free;
  62. return 0;
  63. }
  64. static int drm_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
  65. {
  66. frame->buf[0] = av_buffer_pool_get(hwfc->pool);
  67. if (!frame->buf[0])
  68. return AVERROR(ENOMEM);
  69. frame->data[0] = (uint8_t*)frame->buf[0]->data;
  70. frame->format = AV_PIX_FMT_DRM_PRIME;
  71. frame->width = hwfc->width;
  72. frame->height = hwfc->height;
  73. return 0;
  74. }
  75. typedef struct DRMMapping {
  76. // Address and length of each mmap()ed region.
  77. int nb_regions;
  78. int sync_flags;
  79. int object[AV_DRM_MAX_PLANES];
  80. void *address[AV_DRM_MAX_PLANES];
  81. size_t length[AV_DRM_MAX_PLANES];
  82. } DRMMapping;
  83. static void drm_unmap_frame(AVHWFramesContext *hwfc,
  84. HWMapDescriptor *hwmap)
  85. {
  86. DRMMapping *map = hwmap->priv;
  87. for (int i = 0; i < map->nb_regions; i++) {
  88. #if HAVE_LINUX_DMA_BUF_H
  89. struct dma_buf_sync sync = { .flags = DMA_BUF_SYNC_END | map->sync_flags };
  90. ioctl(map->object[i], DMA_BUF_IOCTL_SYNC, &sync);
  91. #endif
  92. munmap(map->address[i], map->length[i]);
  93. }
  94. av_free(map);
  95. }
  96. static int drm_map_frame(AVHWFramesContext *hwfc,
  97. AVFrame *dst, const AVFrame *src, int flags)
  98. {
  99. const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)src->data[0];
  100. #if HAVE_LINUX_DMA_BUF_H
  101. struct dma_buf_sync sync_start = { 0 };
  102. #endif
  103. DRMMapping *map;
  104. int err, i, p, plane;
  105. int mmap_prot;
  106. void *addr;
  107. map = av_mallocz(sizeof(*map));
  108. if (!map)
  109. return AVERROR(ENOMEM);
  110. mmap_prot = 0;
  111. if (flags & AV_HWFRAME_MAP_READ)
  112. mmap_prot |= PROT_READ;
  113. if (flags & AV_HWFRAME_MAP_WRITE)
  114. mmap_prot |= PROT_WRITE;
  115. #if HAVE_LINUX_DMA_BUF_H
  116. if (flags & AV_HWFRAME_MAP_READ)
  117. map->sync_flags |= DMA_BUF_SYNC_READ;
  118. if (flags & AV_HWFRAME_MAP_WRITE)
  119. map->sync_flags |= DMA_BUF_SYNC_WRITE;
  120. sync_start.flags = DMA_BUF_SYNC_START | map->sync_flags;
  121. #endif
  122. av_assert0(desc->nb_objects <= AV_DRM_MAX_PLANES);
  123. for (i = 0; i < desc->nb_objects; i++) {
  124. addr = mmap(NULL, desc->objects[i].size, mmap_prot, MAP_SHARED,
  125. desc->objects[i].fd, 0);
  126. if (addr == MAP_FAILED) {
  127. err = AVERROR(errno);
  128. av_log(hwfc, AV_LOG_ERROR, "Failed to map DRM object %d to "
  129. "memory: %d.\n", desc->objects[i].fd, errno);
  130. goto fail;
  131. }
  132. map->address[i] = addr;
  133. map->length[i] = desc->objects[i].size;
  134. map->object[i] = desc->objects[i].fd;
  135. #if HAVE_LINUX_DMA_BUF_H
  136. /* We're not checking for errors here because the kernel may not
  137. * support the ioctl, in which case its okay to carry on */
  138. ioctl(desc->objects[i].fd, DMA_BUF_IOCTL_SYNC, &sync_start);
  139. #endif
  140. }
  141. map->nb_regions = i;
  142. plane = 0;
  143. for (i = 0; i < desc->nb_layers; i++) {
  144. const AVDRMLayerDescriptor *layer = &desc->layers[i];
  145. for (p = 0; p < layer->nb_planes; p++) {
  146. dst->data[plane] =
  147. (uint8_t*)map->address[layer->planes[p].object_index] +
  148. layer->planes[p].offset;
  149. dst->linesize[plane] = layer->planes[p].pitch;
  150. ++plane;
  151. }
  152. }
  153. av_assert0(plane <= AV_DRM_MAX_PLANES);
  154. dst->width = src->width;
  155. dst->height = src->height;
  156. err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
  157. &drm_unmap_frame, map);
  158. if (err < 0)
  159. goto fail;
  160. return 0;
  161. fail:
  162. for (i = 0; i < desc->nb_objects; i++) {
  163. if (map->address[i])
  164. munmap(map->address[i], map->length[i]);
  165. }
  166. av_free(map);
  167. return err;
  168. }
  169. static int drm_transfer_get_formats(AVHWFramesContext *ctx,
  170. enum AVHWFrameTransferDirection dir,
  171. enum AVPixelFormat **formats)
  172. {
  173. enum AVPixelFormat *pix_fmts;
  174. pix_fmts = av_malloc_array(2, sizeof(*pix_fmts));
  175. if (!pix_fmts)
  176. return AVERROR(ENOMEM);
  177. pix_fmts[0] = ctx->sw_format;
  178. pix_fmts[1] = AV_PIX_FMT_NONE;
  179. *formats = pix_fmts;
  180. return 0;
  181. }
  182. static int drm_transfer_data_from(AVHWFramesContext *hwfc,
  183. AVFrame *dst, const AVFrame *src)
  184. {
  185. AVFrame *map;
  186. int err;
  187. if (dst->width > hwfc->width || dst->height > hwfc->height)
  188. return AVERROR(EINVAL);
  189. map = av_frame_alloc();
  190. if (!map)
  191. return AVERROR(ENOMEM);
  192. map->format = dst->format;
  193. err = drm_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
  194. if (err)
  195. goto fail;
  196. map->width = dst->width;
  197. map->height = dst->height;
  198. err = av_frame_copy(dst, map);
  199. if (err)
  200. goto fail;
  201. err = 0;
  202. fail:
  203. av_frame_free(&map);
  204. return err;
  205. }
  206. static int drm_transfer_data_to(AVHWFramesContext *hwfc,
  207. AVFrame *dst, const AVFrame *src)
  208. {
  209. AVFrame *map;
  210. int err;
  211. if (src->width > hwfc->width || src->height > hwfc->height)
  212. return AVERROR(EINVAL);
  213. map = av_frame_alloc();
  214. if (!map)
  215. return AVERROR(ENOMEM);
  216. map->format = src->format;
  217. err = drm_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE |
  218. AV_HWFRAME_MAP_OVERWRITE);
  219. if (err)
  220. goto fail;
  221. map->width = src->width;
  222. map->height = src->height;
  223. err = av_frame_copy(map, src);
  224. if (err)
  225. goto fail;
  226. err = 0;
  227. fail:
  228. av_frame_free(&map);
  229. return err;
  230. }
  231. static int drm_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
  232. const AVFrame *src, int flags)
  233. {
  234. int err;
  235. if (hwfc->sw_format != dst->format)
  236. return AVERROR(ENOSYS);
  237. err = drm_map_frame(hwfc, dst, src, flags);
  238. if (err)
  239. return err;
  240. err = av_frame_copy_props(dst, src);
  241. if (err)
  242. return err;
  243. return 0;
  244. }
  245. const HWContextType ff_hwcontext_type_drm = {
  246. .type = AV_HWDEVICE_TYPE_DRM,
  247. .name = "DRM",
  248. .device_hwctx_size = sizeof(AVDRMDeviceContext),
  249. .device_create = &drm_device_create,
  250. .frames_get_buffer = &drm_get_buffer,
  251. .transfer_get_formats = &drm_transfer_get_formats,
  252. .transfer_data_to = &drm_transfer_data_to,
  253. .transfer_data_from = &drm_transfer_data_from,
  254. .map_from = &drm_map_from,
  255. .pix_fmts = (const enum AVPixelFormat[]) {
  256. AV_PIX_FMT_DRM_PRIME,
  257. AV_PIX_FMT_NONE
  258. },
  259. };