2
0

hwcontext_vdpau.c 16 KB

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