2 * This file is part of FFmpeg.
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.
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.
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
24 #include <vdpau/vdpau.h>
28 #include "hwcontext.h"
29 #include "hwcontext_internal.h"
30 #include "hwcontext_vdpau.h"
35 typedef struct VDPAUDeviceContext {
36 VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *get_transfer_caps;
37 VdpVideoSurfaceGetBitsYCbCr *get_data;
38 VdpVideoSurfacePutBitsYCbCr *put_data;
39 VdpVideoSurfaceCreate *surf_create;
40 VdpVideoSurfaceDestroy *surf_destroy;
42 enum AVPixelFormat *pix_fmts[3];
46 typedef struct VDPAUFramesContext {
47 VdpVideoSurfaceGetBitsYCbCr *get_data;
48 VdpVideoSurfacePutBitsYCbCr *put_data;
49 VdpChromaType chroma_type;
52 const enum AVPixelFormat *pix_fmts;
56 typedef struct VDPAUPixFmtMap {
57 VdpYCbCrFormat vdpau_fmt;
58 enum AVPixelFormat pix_fmt;
61 static const VDPAUPixFmtMap pix_fmts_420[] = {
62 { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 },
63 { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P },
64 { 0, AV_PIX_FMT_NONE, },
67 static const VDPAUPixFmtMap pix_fmts_422[] = {
68 { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV16 },
69 { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV422P },
70 { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 },
71 { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 },
72 { 0, AV_PIX_FMT_NONE, },
75 static const VDPAUPixFmtMap pix_fmts_444[] = {
76 { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV444P },
77 { 0, AV_PIX_FMT_NONE, },
81 VdpChromaType chroma_type;
82 const VDPAUPixFmtMap *map;
83 } vdpau_pix_fmts[] = {
84 { VDP_CHROMA_TYPE_420, pix_fmts_420 },
85 { VDP_CHROMA_TYPE_422, pix_fmts_422 },
86 { VDP_CHROMA_TYPE_444, pix_fmts_444 },
89 static int count_pixfmts(const VDPAUPixFmtMap *map)
92 while (map->pix_fmt != AV_PIX_FMT_NONE) {
99 static int vdpau_init_pixmfts(AVHWDeviceContext *ctx)
101 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
102 VDPAUDeviceContext *priv = ctx->internal->priv;
105 for (i = 0; i < FF_ARRAY_ELEMS(priv->pix_fmts); i++) {
106 const VDPAUPixFmtMap *map = vdpau_pix_fmts[i].map;
109 nb_pix_fmts = count_pixfmts(map);
110 priv->pix_fmts[i] = av_malloc_array(nb_pix_fmts + 1, sizeof(*priv->pix_fmts[i]));
111 if (!priv->pix_fmts[i])
112 return AVERROR(ENOMEM);
115 while (map->pix_fmt != AV_PIX_FMT_NONE) {
117 VdpStatus err = priv->get_transfer_caps(hwctx->device, vdpau_pix_fmts[i].chroma_type,
118 map->vdpau_fmt, &supported);
119 if (err == VDP_STATUS_OK && supported)
120 priv->pix_fmts[i][nb_pix_fmts++] = map->pix_fmt;
123 priv->pix_fmts[i][nb_pix_fmts++] = AV_PIX_FMT_NONE;
124 priv->nb_pix_fmts[i] = nb_pix_fmts;
130 static int vdpau_device_init(AVHWDeviceContext *ctx)
132 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
133 VDPAUDeviceContext *priv = ctx->internal->priv;
137 #define GET_CALLBACK(id, result) \
140 err = hwctx->get_proc_address(hwctx->device, id, &tmp); \
141 if (err != VDP_STATUS_OK) { \
142 av_log(ctx, AV_LOG_ERROR, "Error getting the " #id " callback.\n"); \
143 return AVERROR_UNKNOWN; \
145 priv->result = tmp; \
148 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
150 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, get_data);
151 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR, put_data);
152 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, surf_create);
153 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, surf_destroy);
155 ret = vdpau_init_pixmfts(ctx);
157 av_log(ctx, AV_LOG_ERROR, "Error querying the supported pixel formats\n");
165 static void vdpau_device_uninit(AVHWDeviceContext *ctx)
167 VDPAUDeviceContext *priv = ctx->internal->priv;
170 for (i = 0; i < FF_ARRAY_ELEMS(priv->pix_fmts); i++)
171 av_freep(&priv->pix_fmts[i]);
174 static void vdpau_buffer_free(void *opaque, uint8_t *data)
176 AVHWFramesContext *ctx = opaque;
177 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
178 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)data;
180 device_priv->surf_destroy(surf);
183 static AVBufferRef *vdpau_pool_alloc(void *opaque, int size)
185 AVHWFramesContext *ctx = opaque;
186 VDPAUFramesContext *priv = ctx->internal->priv;
187 AVVDPAUDeviceContext *device_hwctx = ctx->device_ctx->hwctx;
188 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
191 VdpVideoSurface surf;
194 err = device_priv->surf_create(device_hwctx->device, priv->chroma_type,
195 ctx->width, ctx->height, &surf);
196 if (err != VDP_STATUS_OK) {
197 av_log(ctx, AV_LOG_ERROR, "Error allocating a VDPAU video surface\n");
201 ret = av_buffer_create((uint8_t*)(uintptr_t)surf, sizeof(surf),
202 vdpau_buffer_free, ctx, AV_BUFFER_FLAG_READONLY);
204 device_priv->surf_destroy(surf);
211 static int vdpau_frames_init(AVHWFramesContext *ctx)
213 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
214 VDPAUFramesContext *priv = ctx->internal->priv;
218 switch (ctx->sw_format) {
219 case AV_PIX_FMT_YUV420P: priv->chroma_type = VDP_CHROMA_TYPE_420; break;
220 case AV_PIX_FMT_YUV422P: priv->chroma_type = VDP_CHROMA_TYPE_422; break;
221 case AV_PIX_FMT_YUV444P: priv->chroma_type = VDP_CHROMA_TYPE_444; break;
223 av_log(ctx, AV_LOG_ERROR, "Unsupported data layout: %s\n",
224 av_get_pix_fmt_name(ctx->sw_format));
225 return AVERROR(ENOSYS);
228 for (i = 0; i < FF_ARRAY_ELEMS(vdpau_pix_fmts); i++) {
229 if (vdpau_pix_fmts[i].chroma_type == priv->chroma_type) {
230 priv->chroma_idx = i;
231 priv->pix_fmts = device_priv->pix_fmts[i];
232 priv->nb_pix_fmts = device_priv->nb_pix_fmts[i];
236 if (!priv->pix_fmts) {
237 av_log(ctx, AV_LOG_ERROR, "Unsupported chroma type: %d\n", priv->chroma_type);
238 return AVERROR(ENOSYS);
242 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(VdpVideoSurface), ctx,
243 vdpau_pool_alloc, NULL);
244 if (!ctx->internal->pool_internal)
245 return AVERROR(ENOMEM);
248 priv->get_data = device_priv->get_data;
249 priv->put_data = device_priv->put_data;
254 static int vdpau_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
256 frame->buf[0] = av_buffer_pool_get(ctx->pool);
258 return AVERROR(ENOMEM);
260 frame->data[3] = frame->buf[0]->data;
261 frame->format = AV_PIX_FMT_VDPAU;
262 frame->width = ctx->width;
263 frame->height = ctx->height;
268 static int vdpau_transfer_get_formats(AVHWFramesContext *ctx,
269 enum AVHWFrameTransferDirection dir,
270 enum AVPixelFormat **formats)
272 VDPAUFramesContext *priv = ctx->internal->priv;
274 enum AVPixelFormat *fmts;
276 if (priv->nb_pix_fmts == 1) {
277 av_log(ctx, AV_LOG_ERROR,
278 "No target formats are supported for this chroma type\n");
279 return AVERROR(ENOSYS);
282 fmts = av_malloc_array(priv->nb_pix_fmts, sizeof(*fmts));
284 return AVERROR(ENOMEM);
286 memcpy(fmts, priv->pix_fmts, sizeof(*fmts) * (priv->nb_pix_fmts));
292 static int vdpau_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
295 VDPAUFramesContext *priv = ctx->internal->priv;
296 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
299 uint32_t linesize[3];
301 const VDPAUPixFmtMap *map;
302 VdpYCbCrFormat vdpau_format;
306 for (i = 0; i< FF_ARRAY_ELEMS(data) && dst->data[i]; i++) {
307 data[i] = dst->data[i];
308 if (dst->linesize[i] < 0 || (uint64_t)dst->linesize > UINT32_MAX) {
309 av_log(ctx, AV_LOG_ERROR,
310 "The linesize %d cannot be represented as uint32\n",
312 return AVERROR(ERANGE);
314 linesize[i] = dst->linesize[i];
317 map = vdpau_pix_fmts[priv->chroma_idx].map;
318 for (i = 0; map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
319 if (map[i].pix_fmt == dst->format) {
320 vdpau_format = map[i].vdpau_fmt;
324 if (map[i].pix_fmt == AV_PIX_FMT_NONE) {
325 av_log(ctx, AV_LOG_ERROR,
326 "Unsupported target pixel format: %s\n",
327 av_get_pix_fmt_name(dst->format));
328 return AVERROR(EINVAL);
331 if (vdpau_format == VDP_YCBCR_FORMAT_YV12)
332 FFSWAP(void*, data[1], data[2]);
334 err = priv->get_data(surf, vdpau_format, data, linesize);
335 if (err != VDP_STATUS_OK) {
336 av_log(ctx, AV_LOG_ERROR, "Error retrieving the data from a VDPAU surface\n");
337 return AVERROR_UNKNOWN;
343 static int vdpau_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
346 VDPAUFramesContext *priv = ctx->internal->priv;
347 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)dst->data[3];
350 uint32_t linesize[3];
352 const VDPAUPixFmtMap *map;
353 VdpYCbCrFormat vdpau_format;
357 for (i = 0; i< FF_ARRAY_ELEMS(data) && src->data[i]; i++) {
358 data[i] = src->data[i];
359 if (src->linesize[i] < 0 || (uint64_t)src->linesize > UINT32_MAX) {
360 av_log(ctx, AV_LOG_ERROR,
361 "The linesize %d cannot be represented as uint32\n",
363 return AVERROR(ERANGE);
365 linesize[i] = src->linesize[i];
368 map = vdpau_pix_fmts[priv->chroma_idx].map;
369 for (i = 0; map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
370 if (map[i].pix_fmt == src->format) {
371 vdpau_format = map[i].vdpau_fmt;
375 if (map[i].pix_fmt == AV_PIX_FMT_NONE) {
376 av_log(ctx, AV_LOG_ERROR,
377 "Unsupported source pixel format: %s\n",
378 av_get_pix_fmt_name(src->format));
379 return AVERROR(EINVAL);
382 if (vdpau_format == VDP_YCBCR_FORMAT_YV12)
383 FFSWAP(const void*, data[1], data[2]);
385 err = priv->put_data(surf, vdpau_format, data, linesize);
386 if (err != VDP_STATUS_OK) {
387 av_log(ctx, AV_LOG_ERROR, "Error uploading the data to a VDPAU surface\n");
388 return AVERROR_UNKNOWN;
395 #include <vdpau/vdpau_x11.h>
396 #include <X11/Xlib.h>
398 typedef struct VDPAUDevicePriv {
399 VdpDeviceDestroy *device_destroy;
403 static void vdpau_device_free(AVHWDeviceContext *ctx)
405 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
406 VDPAUDevicePriv *priv = ctx->user_opaque;
408 if (priv->device_destroy)
409 priv->device_destroy(hwctx->device);
411 XCloseDisplay(priv->dpy);
415 static int vdpau_device_create(AVHWDeviceContext *ctx, const char *device,
416 AVDictionary *opts, int flags)
418 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
420 VDPAUDevicePriv *priv;
422 VdpGetInformationString *get_information_string;
423 const char *display, *vendor;
425 priv = av_mallocz(sizeof(*priv));
427 return AVERROR(ENOMEM);
429 ctx->user_opaque = priv;
430 ctx->free = vdpau_device_free;
432 priv->dpy = XOpenDisplay(device);
434 av_log(ctx, AV_LOG_ERROR, "Cannot open the X11 display %s.\n",
435 XDisplayName(device));
436 return AVERROR_UNKNOWN;
438 display = XDisplayString(priv->dpy);
440 err = vdp_device_create_x11(priv->dpy, XDefaultScreen(priv->dpy),
441 &hwctx->device, &hwctx->get_proc_address);
442 if (err != VDP_STATUS_OK) {
443 av_log(ctx, AV_LOG_ERROR, "VDPAU device creation on X11 display %s failed.\n",
445 return AVERROR_UNKNOWN;
448 #define GET_CALLBACK(id, result) \
451 err = hwctx->get_proc_address(hwctx->device, id, &tmp); \
452 if (err != VDP_STATUS_OK) { \
453 av_log(ctx, AV_LOG_ERROR, "Error getting the " #id " callback.\n"); \
454 return AVERROR_UNKNOWN; \
459 GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
460 GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, priv->device_destroy);
462 get_information_string(&vendor);
463 av_log(ctx, AV_LOG_VERBOSE, "Successfully created a VDPAU device (%s) on "
464 "X11 display %s\n", vendor, display);
470 const HWContextType ff_hwcontext_type_vdpau = {
471 .type = AV_HWDEVICE_TYPE_VDPAU,
474 .device_hwctx_size = sizeof(AVVDPAUDeviceContext),
475 .device_priv_size = sizeof(VDPAUDeviceContext),
476 .frames_priv_size = sizeof(VDPAUFramesContext),
479 .device_create = vdpau_device_create,
481 .device_init = vdpau_device_init,
482 .device_uninit = vdpau_device_uninit,
483 .frames_init = vdpau_frames_init,
484 .frames_get_buffer = vdpau_get_buffer,
485 .transfer_get_formats = vdpau_transfer_get_formats,
486 .transfer_data_to = vdpau_transfer_data_to,
487 .transfer_data_from = vdpau_transfer_data_from,
489 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU, AV_PIX_FMT_NONE },