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.

409 lines
13KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <vdpau/vdpau.h>
  21. #include "buffer.h"
  22. #include "common.h"
  23. #include "hwcontext.h"
  24. #include "hwcontext_internal.h"
  25. #include "hwcontext_vdpau.h"
  26. #include "mem.h"
  27. #include "pixfmt.h"
  28. #include "pixdesc.h"
  29. typedef struct VDPAUDeviceContext {
  30. VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *get_transfer_caps;
  31. VdpVideoSurfaceGetBitsYCbCr *get_data;
  32. VdpVideoSurfacePutBitsYCbCr *put_data;
  33. VdpVideoSurfaceCreate *surf_create;
  34. VdpVideoSurfaceDestroy *surf_destroy;
  35. enum AVPixelFormat *pix_fmts[3];
  36. int nb_pix_fmts[3];
  37. } VDPAUDeviceContext;
  38. typedef struct VDPAUFramesContext {
  39. VdpVideoSurfaceGetBitsYCbCr *get_data;
  40. VdpVideoSurfacePutBitsYCbCr *put_data;
  41. VdpChromaType chroma_type;
  42. int chroma_idx;
  43. const enum AVPixelFormat *pix_fmts;
  44. int nb_pix_fmts;
  45. } VDPAUFramesContext;
  46. typedef struct VDPAUPixFmtMap {
  47. VdpYCbCrFormat vdpau_fmt;
  48. enum AVPixelFormat pix_fmt;
  49. } VDPAUPixFmtMap;
  50. static const VDPAUPixFmtMap pix_fmts_420[] = {
  51. { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 },
  52. { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P },
  53. { 0, AV_PIX_FMT_NONE, },
  54. };
  55. static const VDPAUPixFmtMap pix_fmts_422[] = {
  56. { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV16 },
  57. { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV422P },
  58. { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 },
  59. { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 },
  60. { 0, AV_PIX_FMT_NONE, },
  61. };
  62. static const VDPAUPixFmtMap pix_fmts_444[] = {
  63. { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV444P },
  64. { 0, AV_PIX_FMT_NONE, },
  65. };
  66. static const struct {
  67. VdpChromaType chroma_type;
  68. const VDPAUPixFmtMap *map;
  69. } vdpau_pix_fmts[] = {
  70. { VDP_CHROMA_TYPE_420, pix_fmts_420 },
  71. { VDP_CHROMA_TYPE_422, pix_fmts_422 },
  72. { VDP_CHROMA_TYPE_444, pix_fmts_444 },
  73. };
  74. static int count_pixfmts(const VDPAUPixFmtMap *map)
  75. {
  76. int count = 0;
  77. while (map->pix_fmt != AV_PIX_FMT_NONE) {
  78. map++;
  79. count++;
  80. }
  81. return count;
  82. }
  83. static int vdpau_init_pixmfts(AVHWDeviceContext *ctx)
  84. {
  85. AVVDPAUDeviceContext *hwctx = ctx->hwctx;
  86. VDPAUDeviceContext *priv = ctx->internal->priv;
  87. int i;
  88. for (i = 0; i < FF_ARRAY_ELEMS(priv->pix_fmts); i++) {
  89. const VDPAUPixFmtMap *map = vdpau_pix_fmts[i].map;
  90. int nb_pix_fmts;
  91. nb_pix_fmts = count_pixfmts(map);
  92. priv->pix_fmts[i] = av_malloc_array(nb_pix_fmts + 1, sizeof(*priv->pix_fmts[i]));
  93. if (!priv->pix_fmts[i])
  94. return AVERROR(ENOMEM);
  95. nb_pix_fmts = 0;
  96. while (map->pix_fmt != AV_PIX_FMT_NONE) {
  97. VdpBool supported;
  98. VdpStatus err = priv->get_transfer_caps(hwctx->device, vdpau_pix_fmts[i].chroma_type,
  99. map->vdpau_fmt, &supported);
  100. if (err == VDP_STATUS_OK && supported)
  101. priv->pix_fmts[i][nb_pix_fmts++] = map->pix_fmt;
  102. map++;
  103. }
  104. priv->pix_fmts[i][nb_pix_fmts++] = AV_PIX_FMT_NONE;
  105. priv->nb_pix_fmts[i] = nb_pix_fmts;
  106. }
  107. return 0;
  108. }
  109. static int vdpau_device_init(AVHWDeviceContext *ctx)
  110. {
  111. AVVDPAUDeviceContext *hwctx = ctx->hwctx;
  112. VDPAUDeviceContext *priv = ctx->internal->priv;
  113. VdpStatus err;
  114. int ret;
  115. #define GET_CALLBACK(id, result) \
  116. do { \
  117. void *tmp; \
  118. err = hwctx->get_proc_address(hwctx->device, id, &tmp); \
  119. if (err != VDP_STATUS_OK) { \
  120. av_log(ctx, AV_LOG_ERROR, "Error getting the " #id " callback.\n"); \
  121. return AVERROR_UNKNOWN; \
  122. } \
  123. priv->result = tmp; \
  124. } while (0)
  125. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
  126. get_transfer_caps);
  127. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, get_data);
  128. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR, put_data);
  129. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, surf_create);
  130. GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, surf_destroy);
  131. ret = vdpau_init_pixmfts(ctx);
  132. if (ret < 0) {
  133. av_log(ctx, AV_LOG_ERROR, "Error querying the supported pixel formats\n");
  134. return ret;
  135. }
  136. return 0;
  137. }
  138. static void vdpau_device_uninit(AVHWDeviceContext *ctx)
  139. {
  140. VDPAUDeviceContext *priv = ctx->internal->priv;
  141. int i;
  142. for (i = 0; i < FF_ARRAY_ELEMS(priv->pix_fmts); i++)
  143. av_freep(&priv->pix_fmts[i]);
  144. }
  145. static void vdpau_buffer_free(void *opaque, uint8_t *data)
  146. {
  147. AVHWFramesContext *ctx = opaque;
  148. VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
  149. VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)data;
  150. device_priv->surf_destroy(surf);
  151. }
  152. static AVBufferRef *vdpau_pool_alloc(void *opaque, int size)
  153. {
  154. AVHWFramesContext *ctx = opaque;
  155. VDPAUFramesContext *priv = ctx->internal->priv;
  156. AVVDPAUDeviceContext *device_hwctx = ctx->device_ctx->hwctx;
  157. VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
  158. AVBufferRef *ret;
  159. VdpVideoSurface surf;
  160. VdpStatus err;
  161. err = device_priv->surf_create(device_hwctx->device, priv->chroma_type,
  162. ctx->width, ctx->height, &surf);
  163. if (err != VDP_STATUS_OK) {
  164. av_log(ctx, AV_LOG_ERROR, "Error allocating a VDPAU video surface\n");
  165. return NULL;
  166. }
  167. ret = av_buffer_create((uint8_t*)(uintptr_t)surf, sizeof(surf),
  168. vdpau_buffer_free, ctx, AV_BUFFER_FLAG_READONLY);
  169. if (!ret) {
  170. device_priv->surf_destroy(surf);
  171. return NULL;
  172. }
  173. return ret;
  174. }
  175. static int vdpau_frames_init(AVHWFramesContext *ctx)
  176. {
  177. VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
  178. VDPAUFramesContext *priv = ctx->internal->priv;
  179. int i;
  180. switch (ctx->sw_format) {
  181. case AV_PIX_FMT_YUV420P: priv->chroma_type = VDP_CHROMA_TYPE_420; break;
  182. case AV_PIX_FMT_YUV422P: priv->chroma_type = VDP_CHROMA_TYPE_422; break;
  183. case AV_PIX_FMT_YUV444P: priv->chroma_type = VDP_CHROMA_TYPE_444; break;
  184. default:
  185. av_log(ctx, AV_LOG_ERROR, "Unsupported data layout: %s\n",
  186. av_get_pix_fmt_name(ctx->sw_format));
  187. return AVERROR(ENOSYS);
  188. }
  189. for (i = 0; i < FF_ARRAY_ELEMS(vdpau_pix_fmts); i++) {
  190. if (vdpau_pix_fmts[i].chroma_type == priv->chroma_type) {
  191. priv->chroma_idx = i;
  192. priv->pix_fmts = device_priv->pix_fmts[i];
  193. priv->nb_pix_fmts = device_priv->nb_pix_fmts[i];
  194. break;
  195. }
  196. }
  197. if (!priv->pix_fmts) {
  198. av_log(ctx, AV_LOG_ERROR, "Unsupported chroma type: %d\n", priv->chroma_type);
  199. return AVERROR(ENOSYS);
  200. }
  201. if (!ctx->pool) {
  202. ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(VdpVideoSurface), ctx,
  203. vdpau_pool_alloc, NULL);
  204. if (!ctx->internal->pool_internal)
  205. return AVERROR(ENOMEM);
  206. }
  207. priv->get_data = device_priv->get_data;
  208. priv->put_data = device_priv->put_data;
  209. return 0;
  210. }
  211. static int vdpau_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  212. {
  213. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  214. if (!frame->buf[0])
  215. return AVERROR(ENOMEM);
  216. frame->data[3] = frame->buf[0]->data;
  217. frame->format = AV_PIX_FMT_VDPAU;
  218. frame->width = ctx->width;
  219. frame->height = ctx->height;
  220. return 0;
  221. }
  222. static int vdpau_transfer_get_formats(AVHWFramesContext *ctx,
  223. enum AVHWFrameTransferDirection dir,
  224. enum AVPixelFormat **formats)
  225. {
  226. VDPAUFramesContext *priv = ctx->internal->priv;
  227. enum AVPixelFormat *fmts;
  228. if (priv->nb_pix_fmts == 1) {
  229. av_log(ctx, AV_LOG_ERROR,
  230. "No target formats are supported for this chroma type\n");
  231. return AVERROR(ENOSYS);
  232. }
  233. fmts = av_malloc_array(priv->nb_pix_fmts, sizeof(*fmts));
  234. if (!fmts)
  235. return AVERROR(ENOMEM);
  236. memcpy(fmts, priv->pix_fmts, sizeof(*fmts) * (priv->nb_pix_fmts));
  237. *formats = fmts;
  238. return 0;
  239. }
  240. static int vdpau_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
  241. const AVFrame *src)
  242. {
  243. VDPAUFramesContext *priv = ctx->internal->priv;
  244. VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
  245. void *data[3];
  246. uint32_t linesize[3];
  247. const VDPAUPixFmtMap *map;
  248. VdpYCbCrFormat vdpau_format;
  249. VdpStatus err;
  250. int i;
  251. for (i = 0; i< FF_ARRAY_ELEMS(data) && dst->data[i]; i++) {
  252. data[i] = dst->data[i];
  253. if (dst->linesize[i] < 0 || (uint64_t)dst->linesize > UINT32_MAX) {
  254. av_log(ctx, AV_LOG_ERROR,
  255. "The linesize %d cannot be represented as uint32\n",
  256. dst->linesize[i]);
  257. return AVERROR(ERANGE);
  258. }
  259. linesize[i] = dst->linesize[i];
  260. }
  261. map = vdpau_pix_fmts[priv->chroma_idx].map;
  262. for (i = 0; map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
  263. if (map[i].pix_fmt == dst->format) {
  264. vdpau_format = map[i].vdpau_fmt;
  265. break;
  266. }
  267. }
  268. if (map[i].pix_fmt == AV_PIX_FMT_NONE) {
  269. av_log(ctx, AV_LOG_ERROR,
  270. "Unsupported target pixel format: %s\n",
  271. av_get_pix_fmt_name(dst->format));
  272. return AVERROR(EINVAL);
  273. }
  274. if (vdpau_format == VDP_YCBCR_FORMAT_YV12)
  275. FFSWAP(void*, data[1], data[2]);
  276. err = priv->get_data(surf, vdpau_format, data, linesize);
  277. if (err != VDP_STATUS_OK) {
  278. av_log(ctx, AV_LOG_ERROR, "Error retrieving the data from a VDPAU surface\n");
  279. return AVERROR_UNKNOWN;
  280. }
  281. return 0;
  282. }
  283. static int vdpau_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
  284. const AVFrame *src)
  285. {
  286. VDPAUFramesContext *priv = ctx->internal->priv;
  287. VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)dst->data[3];
  288. const void *data[3];
  289. uint32_t linesize[3];
  290. const VDPAUPixFmtMap *map;
  291. VdpYCbCrFormat vdpau_format;
  292. VdpStatus err;
  293. int i;
  294. for (i = 0; i< FF_ARRAY_ELEMS(data) && src->data[i]; i++) {
  295. data[i] = src->data[i];
  296. if (src->linesize[i] < 0 || (uint64_t)src->linesize > UINT32_MAX) {
  297. av_log(ctx, AV_LOG_ERROR,
  298. "The linesize %d cannot be represented as uint32\n",
  299. src->linesize[i]);
  300. return AVERROR(ERANGE);
  301. }
  302. linesize[i] = src->linesize[i];
  303. }
  304. map = vdpau_pix_fmts[priv->chroma_idx].map;
  305. for (i = 0; map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
  306. if (map[i].pix_fmt == src->format) {
  307. vdpau_format = map[i].vdpau_fmt;
  308. break;
  309. }
  310. }
  311. if (map[i].pix_fmt == AV_PIX_FMT_NONE) {
  312. av_log(ctx, AV_LOG_ERROR,
  313. "Unsupported source pixel format: %s\n",
  314. av_get_pix_fmt_name(src->format));
  315. return AVERROR(EINVAL);
  316. }
  317. if (vdpau_format == VDP_YCBCR_FORMAT_YV12)
  318. FFSWAP(const void*, data[1], data[2]);
  319. err = priv->put_data(surf, vdpau_format, data, linesize);
  320. if (err != VDP_STATUS_OK) {
  321. av_log(ctx, AV_LOG_ERROR, "Error uploading the data to a VDPAU surface\n");
  322. return AVERROR_UNKNOWN;
  323. }
  324. return 0;
  325. }
  326. const HWContextType ff_hwcontext_type_vdpau = {
  327. .type = AV_HWDEVICE_TYPE_VDPAU,
  328. .name = "VDPAU",
  329. .device_hwctx_size = sizeof(AVVDPAUDeviceContext),
  330. .device_priv_size = sizeof(VDPAUDeviceContext),
  331. .frames_priv_size = sizeof(VDPAUFramesContext),
  332. .device_init = vdpau_device_init,
  333. .device_uninit = vdpau_device_uninit,
  334. .frames_init = vdpau_frames_init,
  335. .frames_get_buffer = vdpau_get_buffer,
  336. .transfer_get_formats = vdpau_transfer_get_formats,
  337. .transfer_data_to = vdpau_transfer_data_to,
  338. .transfer_data_from = vdpau_transfer_data_from,
  339. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU, AV_PIX_FMT_NONE },
  340. };