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
22 #include <vdpau/vdpau.h>
26 #include "hwcontext.h"
27 #include "hwcontext_internal.h"
28 #include "hwcontext_vdpau.h"
33 typedef struct VDPAUDeviceContext {
34 VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *get_transfer_caps;
35 VdpVideoSurfaceGetBitsYCbCr *get_data;
36 VdpVideoSurfacePutBitsYCbCr *put_data;
37 VdpVideoSurfaceCreate *surf_create;
38 VdpVideoSurfaceDestroy *surf_destroy;
40 enum AVPixelFormat *pix_fmts[3];
44 typedef struct VDPAUFramesContext {
45 VdpVideoSurfaceGetBitsYCbCr *get_data;
46 VdpVideoSurfacePutBitsYCbCr *put_data;
47 VdpChromaType chroma_type;
50 const enum AVPixelFormat *pix_fmts;
54 typedef struct VDPAUPixFmtMap {
55 VdpYCbCrFormat vdpau_fmt;
56 enum AVPixelFormat pix_fmt;
59 static const VDPAUPixFmtMap pix_fmts_420[] = {
60 { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 },
61 { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P },
62 { 0, AV_PIX_FMT_NONE, },
65 static const VDPAUPixFmtMap pix_fmts_422[] = {
66 { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV16 },
67 { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV422P },
68 { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 },
69 { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 },
70 { 0, AV_PIX_FMT_NONE, },
73 static const VDPAUPixFmtMap pix_fmts_444[] = {
74 { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV444P },
75 { 0, AV_PIX_FMT_NONE, },
79 VdpChromaType chroma_type;
80 const VDPAUPixFmtMap *map;
81 } vdpau_pix_fmts[] = {
82 { VDP_CHROMA_TYPE_420, pix_fmts_420 },
83 { VDP_CHROMA_TYPE_422, pix_fmts_422 },
84 { VDP_CHROMA_TYPE_444, pix_fmts_444 },
87 static int count_pixfmts(const VDPAUPixFmtMap *map)
90 while (map->pix_fmt != AV_PIX_FMT_NONE) {
97 static int vdpau_init_pixmfts(AVHWDeviceContext *ctx)
99 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
100 VDPAUDeviceContext *priv = ctx->internal->priv;
103 for (i = 0; i < FF_ARRAY_ELEMS(priv->pix_fmts); i++) {
104 const VDPAUPixFmtMap *map = vdpau_pix_fmts[i].map;
107 nb_pix_fmts = count_pixfmts(map);
108 priv->pix_fmts[i] = av_malloc_array(nb_pix_fmts + 1, sizeof(*priv->pix_fmts[i]));
109 if (!priv->pix_fmts[i])
110 return AVERROR(ENOMEM);
113 while (map->pix_fmt != AV_PIX_FMT_NONE) {
115 VdpStatus err = priv->get_transfer_caps(hwctx->device, vdpau_pix_fmts[i].chroma_type,
116 map->vdpau_fmt, &supported);
117 if (err == VDP_STATUS_OK && supported)
118 priv->pix_fmts[i][nb_pix_fmts++] = map->pix_fmt;
121 priv->pix_fmts[i][nb_pix_fmts++] = AV_PIX_FMT_NONE;
122 priv->nb_pix_fmts[i] = nb_pix_fmts;
128 static int vdpau_device_init(AVHWDeviceContext *ctx)
130 AVVDPAUDeviceContext *hwctx = ctx->hwctx;
131 VDPAUDeviceContext *priv = ctx->internal->priv;
135 #define GET_CALLBACK(id, result) \
138 err = hwctx->get_proc_address(hwctx->device, id, &tmp); \
139 if (err != VDP_STATUS_OK) { \
140 av_log(ctx, AV_LOG_ERROR, "Error getting the " #id " callback.\n"); \
141 return AVERROR_UNKNOWN; \
143 priv->result = tmp; \
146 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
148 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, get_data);
149 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR, put_data);
150 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, surf_create);
151 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, surf_destroy);
153 ret = vdpau_init_pixmfts(ctx);
155 av_log(ctx, AV_LOG_ERROR, "Error querying the supported pixel formats\n");
162 static void vdpau_device_uninit(AVHWDeviceContext *ctx)
164 VDPAUDeviceContext *priv = ctx->internal->priv;
167 for (i = 0; i < FF_ARRAY_ELEMS(priv->pix_fmts); i++)
168 av_freep(&priv->pix_fmts[i]);
171 static void vdpau_buffer_free(void *opaque, uint8_t *data)
173 AVHWFramesContext *ctx = opaque;
174 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
175 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)data;
177 device_priv->surf_destroy(surf);
180 static AVBufferRef *vdpau_pool_alloc(void *opaque, int size)
182 AVHWFramesContext *ctx = opaque;
183 VDPAUFramesContext *priv = ctx->internal->priv;
184 AVVDPAUDeviceContext *device_hwctx = ctx->device_ctx->hwctx;
185 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
188 VdpVideoSurface surf;
191 err = device_priv->surf_create(device_hwctx->device, priv->chroma_type,
192 ctx->width, ctx->height, &surf);
193 if (err != VDP_STATUS_OK) {
194 av_log(ctx, AV_LOG_ERROR, "Error allocating a VDPAU video surface\n");
198 ret = av_buffer_create((uint8_t*)(uintptr_t)surf, sizeof(surf),
199 vdpau_buffer_free, ctx, AV_BUFFER_FLAG_READONLY);
201 device_priv->surf_destroy(surf);
208 static int vdpau_frames_init(AVHWFramesContext *ctx)
210 VDPAUDeviceContext *device_priv = ctx->device_ctx->internal->priv;
211 VDPAUFramesContext *priv = ctx->internal->priv;
215 switch (ctx->sw_format) {
216 case AV_PIX_FMT_YUV420P: priv->chroma_type = VDP_CHROMA_TYPE_420; break;
217 case AV_PIX_FMT_YUV422P: priv->chroma_type = VDP_CHROMA_TYPE_422; break;
218 case AV_PIX_FMT_YUV444P: priv->chroma_type = VDP_CHROMA_TYPE_444; break;
220 av_log(ctx, AV_LOG_ERROR, "Unsupported data layout: %s\n",
221 av_get_pix_fmt_name(ctx->sw_format));
222 return AVERROR(ENOSYS);
225 for (i = 0; i < FF_ARRAY_ELEMS(vdpau_pix_fmts); i++) {
226 if (vdpau_pix_fmts[i].chroma_type == priv->chroma_type) {
227 priv->chroma_idx = i;
228 priv->pix_fmts = device_priv->pix_fmts[i];
229 priv->nb_pix_fmts = device_priv->nb_pix_fmts[i];
233 if (!priv->pix_fmts) {
234 av_log(ctx, AV_LOG_ERROR, "Unsupported chroma type: %d\n", priv->chroma_type);
235 return AVERROR(ENOSYS);
239 ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(VdpVideoSurface), ctx,
240 vdpau_pool_alloc, NULL);
241 if (!ctx->internal->pool_internal)
242 return AVERROR(ENOMEM);
245 priv->get_data = device_priv->get_data;
246 priv->put_data = device_priv->put_data;
251 static int vdpau_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
253 frame->buf[0] = av_buffer_pool_get(ctx->pool);
255 return AVERROR(ENOMEM);
257 frame->data[3] = frame->buf[0]->data;
258 frame->format = AV_PIX_FMT_VDPAU;
259 frame->width = ctx->width;
260 frame->height = ctx->height;
265 static int vdpau_transfer_get_formats(AVHWFramesContext *ctx,
266 enum AVHWFrameTransferDirection dir,
267 enum AVPixelFormat **formats)
269 VDPAUFramesContext *priv = ctx->internal->priv;
271 enum AVPixelFormat *fmts;
273 if (priv->nb_pix_fmts == 1) {
274 av_log(ctx, AV_LOG_ERROR,
275 "No target formats are supported for this chroma type\n");
276 return AVERROR(ENOSYS);
279 fmts = av_malloc_array(priv->nb_pix_fmts, sizeof(*fmts));
281 return AVERROR(ENOMEM);
283 memcpy(fmts, priv->pix_fmts, sizeof(*fmts) * (priv->nb_pix_fmts));
289 static int vdpau_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
292 VDPAUFramesContext *priv = ctx->internal->priv;
293 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)src->data[3];
296 uint32_t linesize[3];
298 const VDPAUPixFmtMap *map;
299 VdpYCbCrFormat vdpau_format;
303 for (i = 0; i< FF_ARRAY_ELEMS(data) && dst->data[i]; i++) {
304 data[i] = dst->data[i];
305 if (dst->linesize[i] < 0 || (uint64_t)dst->linesize > UINT32_MAX) {
306 av_log(ctx, AV_LOG_ERROR,
307 "The linesize %d cannot be represented as uint32\n",
309 return AVERROR(ERANGE);
311 linesize[i] = dst->linesize[i];
314 map = vdpau_pix_fmts[priv->chroma_idx].map;
315 for (i = 0; map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
316 if (map[i].pix_fmt == dst->format) {
317 vdpau_format = map[i].vdpau_fmt;
321 if (map[i].pix_fmt == AV_PIX_FMT_NONE) {
322 av_log(ctx, AV_LOG_ERROR,
323 "Unsupported target pixel format: %s\n",
324 av_get_pix_fmt_name(dst->format));
325 return AVERROR(EINVAL);
328 if (vdpau_format == VDP_YCBCR_FORMAT_YV12)
329 FFSWAP(void*, data[1], data[2]);
331 err = priv->get_data(surf, vdpau_format, data, linesize);
332 if (err != VDP_STATUS_OK) {
333 av_log(ctx, AV_LOG_ERROR, "Error retrieving the data from a VDPAU surface\n");
334 return AVERROR_UNKNOWN;
340 static int vdpau_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
343 VDPAUFramesContext *priv = ctx->internal->priv;
344 VdpVideoSurface surf = (VdpVideoSurface)(uintptr_t)dst->data[3];
347 uint32_t linesize[3];
349 const VDPAUPixFmtMap *map;
350 VdpYCbCrFormat vdpau_format;
354 for (i = 0; i< FF_ARRAY_ELEMS(data) && src->data[i]; i++) {
355 data[i] = src->data[i];
356 if (src->linesize[i] < 0 || (uint64_t)src->linesize > UINT32_MAX) {
357 av_log(ctx, AV_LOG_ERROR,
358 "The linesize %d cannot be represented as uint32\n",
360 return AVERROR(ERANGE);
362 linesize[i] = src->linesize[i];
365 map = vdpau_pix_fmts[priv->chroma_idx].map;
366 for (i = 0; map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
367 if (map[i].pix_fmt == src->format) {
368 vdpau_format = map[i].vdpau_fmt;
372 if (map[i].pix_fmt == AV_PIX_FMT_NONE) {
373 av_log(ctx, AV_LOG_ERROR,
374 "Unsupported source pixel format: %s\n",
375 av_get_pix_fmt_name(src->format));
376 return AVERROR(EINVAL);
379 if (vdpau_format == VDP_YCBCR_FORMAT_YV12)
380 FFSWAP(const void*, data[1], data[2]);
382 err = priv->put_data(surf, vdpau_format, data, linesize);
383 if (err != VDP_STATUS_OK) {
384 av_log(ctx, AV_LOG_ERROR, "Error uploading the data to a VDPAU surface\n");
385 return AVERROR_UNKNOWN;
391 const HWContextType ff_hwcontext_type_vdpau = {
392 .type = AV_HWDEVICE_TYPE_VDPAU,
395 .device_hwctx_size = sizeof(AVVDPAUDeviceContext),
396 .device_priv_size = sizeof(VDPAUDeviceContext),
397 .frames_priv_size = sizeof(VDPAUFramesContext),
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,
407 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU, AV_PIX_FMT_NONE },