hwcontext_drm.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <fcntl.h>
  19. #include <sys/mman.h>
  20. #include <unistd.h>
  21. #include <drm.h>
  22. #include <xf86drm.h>
  23. #include "avassert.h"
  24. #include "hwcontext.h"
  25. #include "hwcontext_drm.h"
  26. #include "hwcontext_internal.h"
  27. #include "imgutils.h"
  28. static void drm_device_free(AVHWDeviceContext *hwdev)
  29. {
  30. AVDRMDeviceContext *hwctx = hwdev->hwctx;
  31. close(hwctx->fd);
  32. }
  33. static int drm_device_create(AVHWDeviceContext *hwdev, const char *device,
  34. AVDictionary *opts, int flags)
  35. {
  36. AVDRMDeviceContext *hwctx = hwdev->hwctx;
  37. drmVersionPtr version;
  38. hwctx->fd = open(device, O_RDWR);
  39. if (hwctx->fd < 0)
  40. return AVERROR(errno);
  41. version = drmGetVersion(hwctx->fd);
  42. if (!version) {
  43. av_log(hwdev, AV_LOG_ERROR, "Failed to get version information "
  44. "from %s: probably not a DRM device?\n", device);
  45. close(hwctx->fd);
  46. return AVERROR(EINVAL);
  47. }
  48. av_log(hwdev, AV_LOG_VERBOSE, "Opened DRM device %s: driver %s "
  49. "version %d.%d.%d.\n", device, version->name,
  50. version->version_major, version->version_minor,
  51. version->version_patchlevel);
  52. drmFreeVersion(version);
  53. hwdev->free = &drm_device_free;
  54. return 0;
  55. }
  56. static int drm_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
  57. {
  58. frame->buf[0] = av_buffer_pool_get(hwfc->pool);
  59. if (!frame->buf[0])
  60. return AVERROR(ENOMEM);
  61. frame->data[0] = (uint8_t*)frame->buf[0]->data;
  62. frame->format = AV_PIX_FMT_DRM_PRIME;
  63. frame->width = hwfc->width;
  64. frame->height = hwfc->height;
  65. return 0;
  66. }
  67. typedef struct DRMMapping {
  68. // Address and length of each mmap()ed region.
  69. int nb_regions;
  70. void *address[AV_DRM_MAX_PLANES];
  71. size_t length[AV_DRM_MAX_PLANES];
  72. } DRMMapping;
  73. static void drm_unmap_frame(AVHWFramesContext *hwfc,
  74. HWMapDescriptor *hwmap)
  75. {
  76. DRMMapping *map = hwmap->priv;
  77. int i;
  78. for (i = 0; i < map->nb_regions; i++)
  79. munmap(map->address[i], map->length[i]);
  80. av_free(map);
  81. }
  82. static int drm_map_frame(AVHWFramesContext *hwfc,
  83. AVFrame *dst, const AVFrame *src, int flags)
  84. {
  85. const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)src->data[0];
  86. DRMMapping *map;
  87. int err, i, p, plane;
  88. int mmap_prot;
  89. void *addr;
  90. map = av_mallocz(sizeof(*map));
  91. if (!map)
  92. return AVERROR(ENOMEM);
  93. mmap_prot = 0;
  94. if (flags & AV_HWFRAME_MAP_READ)
  95. mmap_prot |= PROT_READ;
  96. if (flags & AV_HWFRAME_MAP_WRITE)
  97. mmap_prot |= PROT_WRITE;
  98. av_assert0(desc->nb_objects <= AV_DRM_MAX_PLANES);
  99. for (i = 0; i < desc->nb_objects; i++) {
  100. addr = mmap(NULL, desc->objects[i].size, mmap_prot, MAP_SHARED,
  101. desc->objects[i].fd, 0);
  102. if (addr == MAP_FAILED) {
  103. err = AVERROR(errno);
  104. av_log(hwfc, AV_LOG_ERROR, "Failed to map DRM object %d to "
  105. "memory: %d.\n", desc->objects[i].fd, errno);
  106. goto fail;
  107. }
  108. map->address[i] = addr;
  109. map->length[i] = desc->objects[i].size;
  110. }
  111. map->nb_regions = i;
  112. plane = 0;
  113. for (i = 0; i < desc->nb_layers; i++) {
  114. const AVDRMLayerDescriptor *layer = &desc->layers[i];
  115. for (p = 0; p < layer->nb_planes; p++) {
  116. dst->data[plane] =
  117. (uint8_t*)map->address[layer->planes[p].object_index] +
  118. layer->planes[p].offset;
  119. dst->linesize[plane] = layer->planes[p].pitch;
  120. ++plane;
  121. }
  122. }
  123. av_assert0(plane <= AV_DRM_MAX_PLANES);
  124. dst->width = src->width;
  125. dst->height = src->height;
  126. err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
  127. &drm_unmap_frame, map);
  128. if (err < 0)
  129. goto fail;
  130. return 0;
  131. fail:
  132. for (i = 0; i < desc->nb_objects; i++) {
  133. if (map->address[i])
  134. munmap(map->address[i], map->length[i]);
  135. }
  136. av_free(map);
  137. return err;
  138. }
  139. static int drm_transfer_get_formats(AVHWFramesContext *ctx,
  140. enum AVHWFrameTransferDirection dir,
  141. enum AVPixelFormat **formats)
  142. {
  143. enum AVPixelFormat *pix_fmts;
  144. pix_fmts = av_malloc_array(2, sizeof(*pix_fmts));
  145. if (!pix_fmts)
  146. return AVERROR(ENOMEM);
  147. pix_fmts[0] = ctx->sw_format;
  148. pix_fmts[1] = AV_PIX_FMT_NONE;
  149. *formats = pix_fmts;
  150. return 0;
  151. }
  152. static int drm_transfer_data_from(AVHWFramesContext *hwfc,
  153. AVFrame *dst, const AVFrame *src)
  154. {
  155. AVFrame *map;
  156. int err;
  157. if (dst->width > hwfc->width || dst->height > hwfc->height)
  158. return AVERROR(EINVAL);
  159. map = av_frame_alloc();
  160. if (!map)
  161. return AVERROR(ENOMEM);
  162. map->format = dst->format;
  163. err = drm_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
  164. if (err)
  165. goto fail;
  166. map->width = dst->width;
  167. map->height = dst->height;
  168. err = av_frame_copy(dst, map);
  169. if (err)
  170. goto fail;
  171. err = 0;
  172. fail:
  173. av_frame_free(&map);
  174. return err;
  175. }
  176. static int drm_transfer_data_to(AVHWFramesContext *hwfc,
  177. AVFrame *dst, const AVFrame *src)
  178. {
  179. AVFrame *map;
  180. int err;
  181. if (src->width > hwfc->width || src->height > hwfc->height)
  182. return AVERROR(EINVAL);
  183. map = av_frame_alloc();
  184. if (!map)
  185. return AVERROR(ENOMEM);
  186. map->format = src->format;
  187. err = drm_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE |
  188. AV_HWFRAME_MAP_OVERWRITE);
  189. if (err)
  190. goto fail;
  191. map->width = src->width;
  192. map->height = src->height;
  193. err = av_frame_copy(map, src);
  194. if (err)
  195. goto fail;
  196. err = 0;
  197. fail:
  198. av_frame_free(&map);
  199. return err;
  200. }
  201. static int drm_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
  202. const AVFrame *src, int flags)
  203. {
  204. int err;
  205. if (hwfc->sw_format != dst->format)
  206. return AVERROR(ENOSYS);
  207. err = drm_map_frame(hwfc, dst, src, flags);
  208. if (err)
  209. return err;
  210. err = av_frame_copy_props(dst, src);
  211. if (err)
  212. return err;
  213. return 0;
  214. }
  215. const HWContextType ff_hwcontext_type_drm = {
  216. .type = AV_HWDEVICE_TYPE_DRM,
  217. .name = "DRM",
  218. .device_hwctx_size = sizeof(AVDRMDeviceContext),
  219. .device_create = &drm_device_create,
  220. .frames_get_buffer = &drm_get_buffer,
  221. .transfer_get_formats = &drm_transfer_get_formats,
  222. .transfer_data_to = &drm_transfer_data_to,
  223. .transfer_data_from = &drm_transfer_data_from,
  224. .map_from = &drm_map_from,
  225. .pix_fmts = (const enum AVPixelFormat[]) {
  226. AV_PIX_FMT_DRM_PRIME,
  227. AV_PIX_FMT_NONE
  228. },
  229. };