FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmpeg_vdpau.c
Go to the documentation of this file.
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 
19 #include <stdint.h>
20 
21 #include <vdpau/vdpau.h>
22 #include <vdpau/vdpau_x11.h>
23 
24 #include <X11/Xlib.h>
25 
26 #include "ffmpeg.h"
27 
28 #include "libavcodec/vdpau.h"
29 
30 #include "libavutil/avassert.h"
31 #include "libavutil/buffer.h"
32 #include "libavutil/frame.h"
33 #include "libavutil/pixfmt.h"
34 
35 typedef struct VDPAUContext {
36  Display *dpy;
37 
38  VdpDevice device;
39  VdpDecoder decoder;
40  VdpGetProcAddress *get_proc_address;
41 
42  VdpGetErrorString *get_error_string;
43  VdpGetInformationString *get_information_string;
44  VdpDeviceDestroy *device_destroy;
45 #if 1 // for ffmpegs older vdpau API, not the oldest though
46  VdpDecoderCreate *decoder_create;
47  VdpDecoderDestroy *decoder_destroy;
48  VdpDecoderRender *decoder_render;
49 #endif
50  VdpVideoSurfaceCreate *video_surface_create;
51  VdpVideoSurfaceDestroy *video_surface_destroy;
52  VdpVideoSurfaceGetBitsYCbCr *video_surface_get_bits;
53  VdpVideoSurfaceGetParameters *video_surface_get_parameters;
54  VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *video_surface_query;
55 
57 
59  VdpYCbCrFormat vdpau_format;
60 } VDPAUContext;
61 
62 int vdpau_api_ver = 2;
63 
65 {
66  InputStream *ist = s->opaque;
67  VDPAUContext *ctx = ist->hwaccel_ctx;
68 
69  ist->hwaccel_uninit = NULL;
70  ist->hwaccel_get_buffer = NULL;
72 
73  if (ctx->decoder_destroy)
74  ctx->decoder_destroy(ctx->decoder);
75 
76  if (ctx->device_destroy)
77  ctx->device_destroy(ctx->device);
78 
79  if (ctx->dpy)
80  XCloseDisplay(ctx->dpy);
81 
82  av_frame_free(&ctx->tmp_frame);
83 
84  av_freep(&ist->hwaccel_ctx);
86 }
87 
88 static void vdpau_release_buffer(void *opaque, uint8_t *data)
89 {
90  VdpVideoSurface surface = *(VdpVideoSurface*)data;
91  VDPAUContext *ctx = opaque;
92 
93  ctx->video_surface_destroy(surface);
94  av_freep(&data);
95 }
96 
98 {
99  InputStream *ist = s->opaque;
100  VDPAUContext *ctx = ist->hwaccel_ctx;
101  VdpVideoSurface *surface;
102  VdpStatus err;
103  VdpChromaType chroma;
104  uint32_t width, height;
105 
107 
108  if (av_vdpau_get_surface_parameters(s, &chroma, &width, &height))
109  return AVERROR(ENOSYS);
110 
111  surface = av_malloc(sizeof(*surface));
112  if (!surface)
113  return AVERROR(ENOMEM);
114 
115  frame->buf[0] = av_buffer_create((uint8_t*)surface, sizeof(*surface),
118  if (!frame->buf[0]) {
119  av_freep(&surface);
120  return AVERROR(ENOMEM);
121  }
122 
123  // properly we should keep a pool of surfaces instead of creating
124  // them anew for each frame, but since we don't care about speed
125  // much in this code, we don't bother
126  err = ctx->video_surface_create(ctx->device, chroma, width, height,
127  surface);
128  if (err != VDP_STATUS_OK) {
129  av_log(NULL, AV_LOG_ERROR, "Error allocating a VDPAU video surface: %s\n",
130  ctx->get_error_string(err));
131  av_buffer_unref(&frame->buf[0]);
132  return AVERROR_UNKNOWN;
133  }
134 
135  frame->data[3] = (uint8_t*)(uintptr_t)*surface;
136 
137  return 0;
138 }
139 
141 {
142  VdpVideoSurface surface = (VdpVideoSurface)(uintptr_t)frame->data[3];
143  InputStream *ist = s->opaque;
144  VDPAUContext *ctx = ist->hwaccel_ctx;
145  VdpStatus err;
146  int ret, chroma_type;
147 
148  err = ctx->video_surface_get_parameters(surface, &chroma_type,
149  &ctx->tmp_frame->width,
150  &ctx->tmp_frame->height);
151  if (err != VDP_STATUS_OK) {
152  av_log(NULL, AV_LOG_ERROR, "Error getting surface parameters: %s\n",
153  ctx->get_error_string(err));
154  return AVERROR_UNKNOWN;
155  }
156  ctx->tmp_frame->format = ctx->pix_fmt;
157 
158  ret = av_frame_get_buffer(ctx->tmp_frame, 32);
159  if (ret < 0)
160  return ret;
161 
162  ctx->tmp_frame->width = frame->width;
163  ctx->tmp_frame->height = frame->height;
164 
165  err = ctx->video_surface_get_bits(surface, ctx->vdpau_format,
166  (void * const *)ctx->tmp_frame->data,
167  ctx->tmp_frame->linesize);
168  if (err != VDP_STATUS_OK) {
169  av_log(NULL, AV_LOG_ERROR, "Error retrieving frame data from VDPAU: %s\n",
170  ctx->get_error_string(err));
171  ret = AVERROR_UNKNOWN;
172  goto fail;
173  }
174 
175  if (ctx->vdpau_format == VDP_YCBCR_FORMAT_YV12)
176  FFSWAP(uint8_t*, ctx->tmp_frame->data[1], ctx->tmp_frame->data[2]);
177 
178  ret = av_frame_copy_props(ctx->tmp_frame, frame);
179  if (ret < 0)
180  goto fail;
181 
182  av_frame_unref(frame);
183  av_frame_move_ref(frame, ctx->tmp_frame);
184  return 0;
185 
186 fail:
188  return ret;
189 }
190 
191 static const int vdpau_formats[][2] = {
192  { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P },
193  { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 },
194  { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 },
195  { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 },
196 };
197 
199 {
200  InputStream *ist = s->opaque;
201  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
202  AVVDPAUContext *vdpau_ctx;
203  VDPAUContext *ctx;
204  const char *display, *vendor;
205  VdpStatus err;
206  int i;
207 
208  ctx = av_mallocz(sizeof(*ctx));
209  if (!ctx)
210  return AVERROR(ENOMEM);
211 
212  ist->hwaccel_ctx = ctx;
216 
217  ctx->tmp_frame = av_frame_alloc();
218  if (!ctx->tmp_frame)
219  goto fail;
220 
221  ctx->dpy = XOpenDisplay(ist->hwaccel_device);
222  if (!ctx->dpy) {
223  av_log(NULL, loglevel, "Cannot open the X11 display %s.\n",
224  XDisplayName(ist->hwaccel_device));
225  goto fail;
226  }
227  display = XDisplayString(ctx->dpy);
228 
229  err = vdp_device_create_x11(ctx->dpy, XDefaultScreen(ctx->dpy), &ctx->device,
230  &ctx->get_proc_address);
231  if (err != VDP_STATUS_OK) {
232  av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n",
233  display);
234  goto fail;
235  }
236 
237 #define GET_CALLBACK(id, result) \
238 do { \
239  void *tmp; \
240  err = ctx->get_proc_address(ctx->device, id, &tmp); \
241  if (err != VDP_STATUS_OK) { \
242  av_log(NULL, loglevel, "Error getting the " #id " callback.\n"); \
243  goto fail; \
244  } \
245  ctx->result = tmp; \
246 } while (0)
247 
248  GET_CALLBACK(VDP_FUNC_ID_GET_ERROR_STRING, get_error_string);
249  GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
250  GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, device_destroy);
251  if (vdpau_api_ver == 1) {
252  GET_CALLBACK(VDP_FUNC_ID_DECODER_CREATE, decoder_create);
253  GET_CALLBACK(VDP_FUNC_ID_DECODER_DESTROY, decoder_destroy);
254  GET_CALLBACK(VDP_FUNC_ID_DECODER_RENDER, decoder_render);
255  }
256  GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, video_surface_create);
257  GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, video_surface_destroy);
258  GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, video_surface_get_bits);
259  GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_PARAMETERS, video_surface_get_parameters);
260  GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
261  video_surface_query);
262 
263  for (i = 0; i < FF_ARRAY_ELEMS(vdpau_formats); i++) {
264  VdpBool supported;
265  err = ctx->video_surface_query(ctx->device, VDP_CHROMA_TYPE_420,
266  vdpau_formats[i][0], &supported);
267  if (err != VDP_STATUS_OK) {
268  av_log(NULL, loglevel,
269  "Error querying VDPAU surface capabilities: %s\n",
270  ctx->get_error_string(err));
271  goto fail;
272  }
273  if (supported)
274  break;
275  }
276  if (i == FF_ARRAY_ELEMS(vdpau_formats)) {
277  av_log(NULL, loglevel,
278  "No supported VDPAU format for retrieving the data.\n");
279  return AVERROR(EINVAL);
280  }
281  ctx->vdpau_format = vdpau_formats[i][0];
282  ctx->pix_fmt = vdpau_formats[i][1];
283 
284  if (vdpau_api_ver == 1) {
285  vdpau_ctx = av_vdpau_alloc_context();
286  if (!vdpau_ctx)
287  goto fail;
288  vdpau_ctx->render = ctx->decoder_render;
289 
290  s->hwaccel_context = vdpau_ctx;
291  } else
292  if (av_vdpau_bind_context(s, ctx->device, ctx->get_proc_address, 0))
293  goto fail;
294 
295  ctx->get_information_string(&vendor);
296  av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, "
297  "to decode input stream #%d:%d.\n", vendor,
298  display, ist->file_index, ist->st->index);
299 
300  return 0;
301 
302 fail:
303  av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
304  ist->file_index, ist->st->index);
305  vdpau_uninit(s);
306  return AVERROR(EINVAL);
307 }
308 
310 {
311  InputStream *ist = s->opaque;
312  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
313  AVVDPAUContext *vdpau_ctx;
314  VDPAUContext *ctx;
315  VdpStatus err;
316  int profile, ret;
317 
318  if (!ist->hwaccel_ctx) {
319  ret = vdpau_alloc(s);
320  if (ret < 0)
321  return ret;
322  }
323  ctx = ist->hwaccel_ctx;
324  vdpau_ctx = s->hwaccel_context;
325 
326  ret = av_vdpau_get_profile(s, &profile);
327  if (ret < 0) {
328  av_log(NULL, loglevel, "No known VDPAU decoder profile for this stream.\n");
329  return AVERROR(EINVAL);
330  }
331 
332  if (ctx->decoder)
333  ctx->decoder_destroy(ctx->decoder);
334 
335  err = ctx->decoder_create(ctx->device, profile,
336  s->coded_width, s->coded_height,
337  16, &ctx->decoder);
338  if (err != VDP_STATUS_OK) {
339  av_log(NULL, loglevel, "Error creating the VDPAU decoder: %s\n",
340  ctx->get_error_string(err));
341  return AVERROR_UNKNOWN;
342  }
343 
344  vdpau_ctx->decoder = ctx->decoder;
345 
348 
349  return 0;
350 }
351 
353 {
354  InputStream *ist = s->opaque;
355 
356  if (vdpau_api_ver == 1)
357  return vdpau_old_init(s);
358 
359  if (!ist->hwaccel_ctx) {
360  int ret = vdpau_alloc(s);
361  if (ret < 0)
362  return ret;
363  }
364 
367 
368  return 0;
369 }
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:83
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:124
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
VdpDevice device
Definition: ffmpeg_vdpau.c:38
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1424
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:441
VdpDecoder decoder
Definition: ffmpeg_vdpau.c:39
int index
stream index in AVFormatContext
Definition: avformat.h:843
AVFrame * tmp_frame
Definition: ffmpeg_vdpau.c:56
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:479
#define FF_ARRAY_ELEMS(a)
int(* hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags)
Definition: ffmpeg.h:322
Public libavcodec VDPAU header.
AVVDPAUContext * av_vdpau_alloc_context(void)
Allocate an AVVDPAUContext.
Definition: vdpau.c:743
int vdpau_init(AVCodecContext *s)
Definition: ffmpeg_vdpau.c:352
int av_vdpau_get_surface_parameters(AVCodecContext *avctx, VdpChromaType *type, uint32_t *width, uint32_t *height)
Gets the parameters to create an adequate VDPAU video surface for the codec context using VDPAU hardw...
Definition: vdpau.c:75
VdpVideoSurfaceCreate * video_surface_create
Definition: ffmpeg_vdpau.c:50
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2656
VdpDecoderDestroy * decoder_destroy
Definition: ffmpeg_vdpau.c:47
VdpGetProcAddress * get_proc_address
Definition: ffmpeg_vdpau.c:40
static AVFrame * frame
VdpDecoder decoder
VDPAU decoder handle.
Definition: vdpau.h:96
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:113
int file_index
Definition: ffmpeg.h:248
#define av_log(a,...)
int av_vdpau_get_profile(AVCodecContext *avctx, VdpDecoderProfile *profile)
Get a decoder profile that should be used for initializing a VDPAU decoder.
Definition: vdpau.c:696
VdpGetErrorString * get_error_string
Definition: ffmpeg_vdpau.c:42
VdpDecoderCreate * decoder_create
Definition: ffmpeg_vdpau.c:46
int width
width and height of the video frame
Definition: frame.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int vdpau_alloc(AVCodecContext *s)
Definition: ffmpeg_vdpau.c:198
This structure is used to share data between the libavcodec library and the client video application...
Definition: vdpau.h:90
int profile
Definition: mxfenc.c:1804
void(* hwaccel_uninit)(AVCodecContext *s)
Definition: ffmpeg.h:321
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
VdpGetInformationString * get_information_string
Definition: ffmpeg_vdpau.c:43
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:91
simple assert() macros that are a bit more flexible than ISO C assert().
static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
Definition: ffmpeg_vdpau.c:97
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
reference-counted frame API
static void decoder_destroy(Decoder *d)
Definition: ffplay.c:628
VdpDecoderRender * decoder_render
Definition: ffmpeg_vdpau.c:48
ret
Definition: avfilter.c:974
int(* hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg.h:323
#define GET_CALLBACK(id, result)
static int vdpau_old_init(AVCodecContext *s)
Definition: ffmpeg_vdpau.c:309
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
VdpVideoSurfaceGetParameters * video_surface_get_parameters
Definition: ffmpeg_vdpau.c:53
VdpVideoSurfaceGetBitsYCbCr * video_surface_get_bits
Definition: ffmpeg_vdpau.c:52
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1241
enum AVPixelFormat pix_fmt
Definition: ffmpeg_vdpau.c:58
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:64
VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities * video_surface_query
Definition: ffmpeg_vdpau.c:54
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
AVStream * st
Definition: ffmpeg.h:249
int coded_height
Definition: avcodec.h:1424
int vdpau_api_ver
Definition: ffmpeg_vdpau.c:62
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:211
VdpDecoderRender * render
VDPAU decoder render callback.
Definition: vdpau.h:103
refcounted data buffer API
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:265
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:462
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device, VdpGetProcAddress *get_proc, unsigned flags)
Associate a VDPAU device with a codec context for hardware acceleration.
Definition: vdpau.c:748
VdpYCbCrFormat vdpau_format
Definition: ffmpeg_vdpau.c:59
static const int vdpau_formats[][2]
Definition: ffmpeg_vdpau.c:191
VdpVideoSurfaceDestroy * video_surface_destroy
Definition: ffmpeg_vdpau.c:51
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
static void vdpau_release_buffer(void *opaque, uint8_t *data)
Definition: ffmpeg_vdpau.c:88
static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg_vdpau.c:140
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
pixel format definitions
void * hwaccel_ctx
Definition: ffmpeg.h:320
char * hwaccel_device
Definition: ffmpeg.h:316
int height
Definition: frame.h:220
#define av_freep(p)
static void vdpau_uninit(AVCodecContext *s)
Definition: ffmpeg_vdpau.c:64
VdpDeviceDestroy * device_destroy
Definition: ffmpeg_vdpau.c:44
#define FFSWAP(type, a, b)
Definition: common.h:69
enum HWAccelID hwaccel_id
Definition: ffmpeg.h:315
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1298
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:548
Display * dpy
Definition: ffmpeg_vdpau.c:36
static int width