hwcontext_vdpau.c 16 KB

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