hwcontext_vdpau.c 16 KB

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