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 enum AVPixelFormat frames_sw_format;
83 const VDPAUPixFmtMap *map;
84 } vdpau_pix_fmts[] = {
85 { VDP_CHROMA_TYPE_420, AV_PIX_FMT_YUV420P, pix_fmts_420 },
86 { VDP_CHROMA_TYPE_422, AV_PIX_FMT_YUV422P, pix_fmts_422 },
87 { VDP_CHROMA_TYPE_444, AV_PIX_FMT_YUV444P, pix_fmts_444 },
90 static int count_pixfmts(const VDPAUPixFmtMap *map)
93 while (map->pix_fmt != AV_PIX_FMT_NONE) {
100 static int vdpau_init_pixmfts(AVHWDeviceContext *ctx)
102 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
103 VDPAUDeviceContext *priv = ctx->internal->priv;
106 for (i = 0; i < FF_ARRAY_ELEMS(priv->pix_fmts); i++) {
107 const VDPAUPixFmtMap *map = vdpau_pix_fmts[i].map;
110 nb_pix_fmts = count_pixfmts(map);
111 priv->pix_fmts[i] = av_malloc_array(nb_pix_fmts + 1, sizeof(*priv->pix_fmts[i]));
112 if (!priv->pix_fmts[i])
113 return AVERROR(ENOMEM);
116 while (map->pix_fmt != AV_PIX_FMT_NONE) {
118 VdpStatus err = priv->get_transfer_caps(hwctx->device, vdpau_pix_fmts[i].chroma_type,
119 map->vdpau_fmt, &supported);
120 if (err == VDP_STATUS_OK && supported)
121 priv->pix_fmts[i][nb_pix_fmts++] = map->pix_fmt;
124 priv->pix_fmts[i][nb_pix_fmts++] = AV_PIX_FMT_NONE;
125 priv->nb_pix_fmts[i] = nb_pix_fmts;
131 #define GET_CALLBACK(id, result) \
134 err = hwctx->get_proc_address(hwctx->device, id, &tmp); \
135 if (err != VDP_STATUS_OK) { \
136 av_log(ctx, AV_LOG_ERROR, "Error getting the " #id " callback.\n"); \
137 return AVERROR_UNKNOWN; \
142 static int vdpau_device_init(AVHWDeviceContext *ctx)
144 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
145 VDPAUDeviceContext *priv = ctx->internal->priv;
149 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
150 priv->get_transfer_caps);
151 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, priv->get_data);
152 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR, priv->put_data);
153 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, priv->surf_create);
154 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, priv->surf_destroy);
156 ret = vdpau_init_pixmfts(ctx);
158 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 int vdpau_frames_get_constraints(AVHWDeviceContext *ctx,
175 const void *hwconfig,
176 AVHWFramesConstraints *constraints)
178 VDPAUDeviceContext *priv = ctx->internal->priv;
179 int nb_sw_formats = 0;
182 constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(vdpau_pix_fmts) + 1,
183 sizeof(*constraints->valid_sw_formats));
184 if (!constraints->valid_sw_formats)
185 return AVERROR(ENOMEM);
187 for (i = 0; i < FF_ARRAY_ELEMS(vdpau_pix_fmts); i++) {
188 if (priv->nb_pix_fmts[i] > 1)
189 constraints->valid_sw_formats[nb_sw_formats++] = vdpau_pix_fmts[i].frames_sw_format;
191 constraints->valid_sw_formats[nb_sw_formats] = AV_PIX_FMT_NONE;
193 constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
194 if (!constraints->valid_hw_formats)
195 return AVERROR(ENOMEM);
197 constraints->valid_hw_formats[0] = AV_PIX_FMT_VDPAU;
198 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
203 static void vdpau_buffer_free(void *opaque, uint8_t *data)
205 AVHWFramesContext *ctx = opaque;
206 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
207 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)data;
209 device_priv->surf_destroy(surf);
212 static AVBufferRef *vdpau_pool_alloc(void *opaque, int size)
214 AVHWFramesContext *ctx = opaque;
215 VDPAUFramesContext *priv = ctx->internal->priv;
216 AVVDPAUDeviceContext *device_hwctx = ctx->device_ctx->hwctx;
217 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
220 VdpVideoSurface surf;
223 err = device_priv->surf_create(device_hwctx->device, priv->chroma_type,
224 ctx->width, ctx->height, &surf);
225 if (err != VDP_STATUS_OK) {
226 av_log(ctx, AV_LOG_ERROR, "Error allocating a VDPAU video surface\n");
230 ret = av_buffer_create((uint8_t*)(uintptr_t)surf, sizeof(surf),
231 vdpau_buffer_free, ctx, AV_BUFFER_FLAG_READONLY);
233 device_priv->surf_destroy(surf);
240 static int vdpau_frames_init(AVHWFramesContext *ctx)
242 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
243 VDPAUFramesContext *priv = ctx->internal->priv;
247 for (i = 0; i < FF_ARRAY_ELEMS(vdpau_pix_fmts); i++) {
248 if (vdpau_pix_fmts[i].frames_sw_format == ctx->sw_format) {
249 priv->chroma_type = vdpau_pix_fmts[i].chroma_type;
250 priv->chroma_idx = i;
251 priv->pix_fmts = device_priv->pix_fmts[i];
252 priv->nb_pix_fmts = device_priv->nb_pix_fmts[i];
256 if (priv->nb_pix_fmts < 2) {
257 av_log(ctx, AV_LOG_ERROR, "Unsupported sw format: %s\n",
258 av_get_pix_fmt_name(ctx->sw_format));
259 return AVERROR(ENOSYS);
263 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(VdpVideoSurface), ctx,
264 vdpau_pool_alloc, NULL);
265 if (!ctx->internal->pool_internal)
266 return AVERROR(ENOMEM);
269 priv->get_data = device_priv->get_data;
270 priv->put_data = device_priv->put_data;
275 static int vdpau_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
277 frame->buf[0] = av_buffer_pool_get(ctx->pool);
279 return AVERROR(ENOMEM);
281 frame->data[3] = frame->buf[0]->data;
282 frame->format = AV_PIX_FMT_VDPAU;
283 frame->width = ctx->width;
284 frame->height = ctx->height;
289 static int vdpau_transfer_get_formats(AVHWFramesContext *ctx,
290 enum AVHWFrameTransferDirection dir,
291 enum AVPixelFormat **formats)
293 VDPAUFramesContext *priv = ctx->internal->priv;
295 enum AVPixelFormat *fmts;
297 if (priv->nb_pix_fmts == 1) {
298 av_log(ctx, AV_LOG_ERROR,
299 "No target formats are supported for this chroma type\n");
300 return AVERROR(ENOSYS);
303 fmts = av_malloc_array(priv->nb_pix_fmts, sizeof(*fmts));
305 return AVERROR(ENOMEM);
307 memcpy(fmts, priv->pix_fmts, sizeof(*fmts) * (priv->nb_pix_fmts));
313 static int vdpau_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
316 VDPAUFramesContext *priv = ctx->internal->priv;
317 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
320 uint32_t linesize[3];
322 const VDPAUPixFmtMap *map;
323 VdpYCbCrFormat vdpau_format;
327 for (i = 0; i< FF_ARRAY_ELEMS(data) && dst->data[i]; i++) {
328 data[i] = dst->data[i];
329 if (dst->linesize[i] < 0 || dst->linesize[i] > UINT32_MAX) {
330 av_log(ctx, AV_LOG_ERROR,
331 "The linesize %d cannot be represented as uint32\n",
333 return AVERROR(ERANGE);
335 linesize[i] = dst->linesize[i];
338 map = vdpau_pix_fmts[priv->chroma_idx].map;
339 for (i = 0; map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
340 if (map[i].pix_fmt == dst->format) {
341 vdpau_format = map[i].vdpau_fmt;
345 if (map[i].pix_fmt == AV_PIX_FMT_NONE) {
346 av_log(ctx, AV_LOG_ERROR,
347 "Unsupported target pixel format: %s\n",
348 av_get_pix_fmt_name(dst->format));
349 return AVERROR(EINVAL);
352 if (vdpau_format == VDP_YCBCR_FORMAT_YV12)
353 FFSWAP(void*, data[1], data[2]);
355 err = priv->get_data(surf, vdpau_format, data, linesize);
356 if (err != VDP_STATUS_OK) {
357 av_log(ctx, AV_LOG_ERROR, "Error retrieving the data from a VDPAU surface\n");
358 return AVERROR_UNKNOWN;
364 static int vdpau_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
367 VDPAUFramesContext *priv = ctx->internal->priv;
368 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)dst->data[3];
371 uint32_t linesize[3];
373 const VDPAUPixFmtMap *map;
374 VdpYCbCrFormat vdpau_format;
378 for (i = 0; i< FF_ARRAY_ELEMS(data) && src->data[i]; i++) {
379 data[i] = src->data[i];
380 if (src->linesize[i] < 0 || src->linesize[i] > UINT32_MAX) {
381 av_log(ctx, AV_LOG_ERROR,
382 "The linesize %d cannot be represented as uint32\n",
384 return AVERROR(ERANGE);
386 linesize[i] = src->linesize[i];
389 map = vdpau_pix_fmts[priv->chroma_idx].map;
390 for (i = 0; map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
391 if (map[i].pix_fmt == src->format) {
392 vdpau_format = map[i].vdpau_fmt;
396 if (map[i].pix_fmt == AV_PIX_FMT_NONE) {
397 av_log(ctx, AV_LOG_ERROR,
398 "Unsupported source pixel format: %s\n",
399 av_get_pix_fmt_name(src->format));
400 return AVERROR(EINVAL);
403 if (vdpau_format == VDP_YCBCR_FORMAT_YV12)
404 FFSWAP(const void*, data[1], data[2]);
406 err = priv->put_data(surf, vdpau_format, data, linesize);
407 if (err != VDP_STATUS_OK) {
408 av_log(ctx, AV_LOG_ERROR, "Error uploading the data to a VDPAU surface\n");
409 return AVERROR_UNKNOWN;
416 #include <vdpau/vdpau_x11.h>
417 #include <X11/Xlib.h>
419 typedef struct VDPAUDevicePriv {
420 VdpDeviceDestroy *device_destroy;
424 static void vdpau_device_free(AVHWDeviceContext *ctx)
426 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
427 VDPAUDevicePriv *priv = ctx->user_opaque;
429 if (priv->device_destroy)
430 priv->device_destroy(hwctx->device);
432 XCloseDisplay(priv->dpy);
436 static int vdpau_device_create(AVHWDeviceContext *ctx, const char *device,
437 AVDictionary *opts, int flags)
439 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
441 VDPAUDevicePriv *priv;
443 VdpGetInformationString *get_information_string;
444 const char *display, *vendor;
446 priv = av_mallocz(sizeof(*priv));
448 return AVERROR(ENOMEM);
450 ctx->user_opaque = priv;
451 ctx->free = vdpau_device_free;
453 priv->dpy = XOpenDisplay(device);
455 av_log(ctx, AV_LOG_ERROR, "Cannot open the X11 display %s.\n",
456 XDisplayName(device));
457 return AVERROR_UNKNOWN;
459 display = XDisplayString(priv->dpy);
461 err = vdp_device_create_x11(priv->dpy, XDefaultScreen(priv->dpy),
462 &hwctx->device, &hwctx->get_proc_address);
463 if (err != VDP_STATUS_OK) {
464 av_log(ctx, AV_LOG_ERROR, "VDPAU device creation on X11 display %s failed.\n",
466 return AVERROR_UNKNOWN;
469 GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
470 GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, priv->device_destroy);
472 get_information_string(&vendor);
473 av_log(ctx, AV_LOG_VERBOSE, "Successfully created a VDPAU device (%s) on "
474 "X11 display %s\n", vendor, display);
480 const HWContextType ff_hwcontext_type_vdpau = {
481 .type = AV_HWDEVICE_TYPE_VDPAU,
484 .device_hwctx_size = sizeof(AVVDPAUDeviceContext),
485 .device_priv_size = sizeof(VDPAUDeviceContext),
486 .frames_priv_size = sizeof(VDPAUFramesContext),
489 .device_create = vdpau_device_create,
491 .device_init = vdpau_device_init,
492 .device_uninit = vdpau_device_uninit,
493 .frames_get_constraints = vdpau_frames_get_constraints,
494 .frames_init = vdpau_frames_init,
495 .frames_get_buffer = vdpau_get_buffer,
496 .transfer_get_formats = vdpau_transfer_get_formats,
497 .transfer_data_to = vdpau_transfer_data_to,
498 .transfer_data_from = vdpau_transfer_data_from,
500 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU, AV_PIX_FMT_NONE },