ef35d22ae84d434dc78bdf5cec28c39b44424d7d
[ffmpeg.git] / ffmpeg_vdpau.c
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     VdpDecoderCreate                                *decoder_create;
46     VdpDecoderDestroy                               *decoder_destroy;
47     VdpDecoderRender                                *decoder_render;
48     VdpVideoSurfaceCreate                           *video_surface_create;
49     VdpVideoSurfaceDestroy                          *video_surface_destroy;
50     VdpVideoSurfaceGetBitsYCbCr                     *video_surface_get_bits;
51     VdpVideoSurfaceGetParameters                    *video_surface_get_parameters;
52     VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *video_surface_query;
53
54     AVFrame *tmp_frame;
55
56     enum AVPixelFormat pix_fmt;
57     VdpYCbCrFormat vdpau_format;
58 } VDPAUContext;
59
60 int vdpau_api_ver = 2;
61
62 static void vdpau_uninit(AVCodecContext *s)
63 {
64     InputStream  *ist = s->opaque;
65     VDPAUContext *ctx = ist->hwaccel_ctx;
66
67     ist->hwaccel_uninit        = NULL;
68     ist->hwaccel_get_buffer    = NULL;
69     ist->hwaccel_retrieve_data = NULL;
70
71     if (ctx->decoder_destroy)
72         ctx->decoder_destroy(ctx->decoder);
73
74     if (ctx->device_destroy)
75         ctx->device_destroy(ctx->device);
76
77     if (ctx->dpy)
78         XCloseDisplay(ctx->dpy);
79
80     av_frame_free(&ctx->tmp_frame);
81
82     av_freep(&ist->hwaccel_ctx);
83     av_freep(&s->hwaccel_context);
84 }
85
86 static void vdpau_release_buffer(void *opaque, uint8_t *data)
87 {
88     VdpVideoSurface surface = *(VdpVideoSurface*)data;
89     VDPAUContext *ctx = opaque;
90
91     ctx->video_surface_destroy(surface);
92     av_freep(&data);
93 }
94
95 static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
96 {
97     InputStream         *ist = s->opaque;
98     VDPAUContext        *ctx = ist->hwaccel_ctx;
99     VdpVideoSurface *surface;
100     VdpStatus err;
101
102     av_assert0(frame->format == AV_PIX_FMT_VDPAU);
103
104     surface = av_malloc(sizeof(*surface));
105     if (!surface)
106         return AVERROR(ENOMEM);
107
108     frame->buf[0] = av_buffer_create((uint8_t*)surface, sizeof(*surface),
109                                      vdpau_release_buffer, ctx,
110                                      AV_BUFFER_FLAG_READONLY);
111     if (!frame->buf[0]) {
112         av_freep(&surface);
113         return AVERROR(ENOMEM);
114     }
115
116     // properly we should keep a pool of surfaces instead of creating
117     // them anew for each frame, but since we don't care about speed
118     // much in this code, we don't bother
119     err = ctx->video_surface_create(ctx->device, VDP_CHROMA_TYPE_420,
120                                     frame->width, frame->height, surface);
121     if (err != VDP_STATUS_OK) {
122         av_log(NULL, AV_LOG_ERROR, "Error allocating a VDPAU video surface: %s\n",
123                ctx->get_error_string(err));
124         av_buffer_unref(&frame->buf[0]);
125         return AVERROR_UNKNOWN;
126     }
127
128     frame->data[3] = (uint8_t*)(uintptr_t)*surface;
129
130     return 0;
131 }
132
133 static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
134 {
135     VdpVideoSurface surface = (VdpVideoSurface)(uintptr_t)frame->data[3];
136     InputStream        *ist = s->opaque;
137     VDPAUContext       *ctx = ist->hwaccel_ctx;
138     VdpStatus err;
139     int ret, chroma_type;
140
141     err = ctx->video_surface_get_parameters(surface, &chroma_type,
142                                             &ctx->tmp_frame->width,
143                                             &ctx->tmp_frame->height);
144     if (err != VDP_STATUS_OK) {
145         av_log(NULL, AV_LOG_ERROR, "Error getting surface parameters: %s\n",
146                ctx->get_error_string(err));
147         return AVERROR_UNKNOWN;
148     }
149     ctx->tmp_frame->format = ctx->pix_fmt;
150
151     ret = av_frame_get_buffer(ctx->tmp_frame, 32);
152     if (ret < 0)
153         return ret;
154
155     ctx->tmp_frame->width  = frame->width;
156     ctx->tmp_frame->height = frame->height;
157
158     err = ctx->video_surface_get_bits(surface, ctx->vdpau_format,
159                                       (void * const *)ctx->tmp_frame->data,
160                                       ctx->tmp_frame->linesize);
161     if (err != VDP_STATUS_OK) {
162         av_log(NULL, AV_LOG_ERROR, "Error retrieving frame data from VDPAU: %s\n",
163                ctx->get_error_string(err));
164         ret = AVERROR_UNKNOWN;
165         goto fail;
166     }
167
168     if (ctx->vdpau_format == VDP_YCBCR_FORMAT_YV12)
169         FFSWAP(uint8_t*, ctx->tmp_frame->data[1], ctx->tmp_frame->data[2]);
170
171     ret = av_frame_copy_props(ctx->tmp_frame, frame);
172     if (ret < 0)
173         goto fail;
174
175     av_frame_unref(frame);
176     av_frame_move_ref(frame, ctx->tmp_frame);
177     return 0;
178
179 fail:
180     av_frame_unref(ctx->tmp_frame);
181     return ret;
182 }
183
184 static const int vdpau_formats[][2] = {
185     { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P },
186     { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 },
187     { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 },
188     { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 },
189 };
190
191 static int vdpau_alloc(AVCodecContext *s)
192 {
193     InputStream  *ist = s->opaque;
194     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
195     AVVDPAUContext *vdpau_ctx;
196     VDPAUContext *ctx;
197     const char *display, *vendor;
198     VdpStatus err;
199     int i;
200
201     ctx = av_mallocz(sizeof(*ctx));
202     if (!ctx)
203         return AVERROR(ENOMEM);
204
205     ist->hwaccel_ctx           = ctx;
206     ist->hwaccel_uninit        = vdpau_uninit;
207     ist->hwaccel_get_buffer    = vdpau_get_buffer;
208     ist->hwaccel_retrieve_data = vdpau_retrieve_data;
209
210     ctx->tmp_frame = av_frame_alloc();
211     if (!ctx->tmp_frame)
212         goto fail;
213
214     ctx->dpy = XOpenDisplay(ist->hwaccel_device);
215     if (!ctx->dpy) {
216         av_log(NULL, loglevel, "Cannot open the X11 display %s.\n",
217                XDisplayName(ist->hwaccel_device));
218         goto fail;
219     }
220     display = XDisplayString(ctx->dpy);
221
222     err = vdp_device_create_x11(ctx->dpy, XDefaultScreen(ctx->dpy), &ctx->device,
223                                 &ctx->get_proc_address);
224     if (err != VDP_STATUS_OK) {
225         av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n",
226                display);
227         goto fail;
228     }
229
230 #define GET_CALLBACK(id, result)                                                \
231 do {                                                                            \
232     void *tmp;                                                                  \
233     err = ctx->get_proc_address(ctx->device, id, &tmp);                         \
234     if (err != VDP_STATUS_OK) {                                                 \
235         av_log(NULL, loglevel, "Error getting the " #id " callback.\n");        \
236         goto fail;                                                              \
237     }                                                                           \
238     ctx->result = tmp;                                                          \
239 } while (0)
240
241     GET_CALLBACK(VDP_FUNC_ID_GET_ERROR_STRING,               get_error_string);
242     GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING,         get_information_string);
243     GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY,                 device_destroy);
244     GET_CALLBACK(VDP_FUNC_ID_DECODER_CREATE,                 decoder_create);
245     GET_CALLBACK(VDP_FUNC_ID_DECODER_DESTROY,                decoder_destroy);
246     GET_CALLBACK(VDP_FUNC_ID_DECODER_RENDER,                 decoder_render);
247     GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE,           video_surface_create);
248     GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY,          video_surface_destroy);
249     GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, video_surface_get_bits);
250     GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_PARAMETERS,   video_surface_get_parameters);
251     GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
252                  video_surface_query);
253
254     for (i = 0; i < FF_ARRAY_ELEMS(vdpau_formats); i++) {
255         VdpBool supported;
256         err = ctx->video_surface_query(ctx->device, VDP_CHROMA_TYPE_420,
257                                        vdpau_formats[i][0], &supported);
258         if (err != VDP_STATUS_OK) {
259             av_log(NULL, loglevel,
260                    "Error querying VDPAU surface capabilities: %s\n",
261                    ctx->get_error_string(err));
262             goto fail;
263         }
264         if (supported)
265             break;
266     }
267     if (i == FF_ARRAY_ELEMS(vdpau_formats)) {
268         av_log(NULL, loglevel,
269                "No supported VDPAU format for retrieving the data.\n");
270         return AVERROR(EINVAL);
271     }
272     ctx->vdpau_format = vdpau_formats[i][0];
273     ctx->pix_fmt      = vdpau_formats[i][1];
274
275     vdpau_ctx = av_vdpau_alloc_context();
276     if (!vdpau_ctx)
277         goto fail;
278     vdpau_ctx->render = ctx->decoder_render;
279
280     s->hwaccel_context = vdpau_ctx;
281
282     ctx->get_information_string(&vendor);
283     av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, "
284            "to decode input stream #%d:%d.\n", vendor,
285            display, ist->file_index, ist->st->index);
286
287     return 0;
288
289 fail:
290     av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
291            ist->file_index, ist->st->index);
292     vdpau_uninit(s);
293     return AVERROR(EINVAL);
294 }
295
296 static int vdpau_old_init(AVCodecContext *s)
297 {
298     InputStream *ist = s->opaque;
299     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
300     AVVDPAUContext *vdpau_ctx;
301     VDPAUContext *ctx;
302     VdpStatus err;
303     int profile, ret;
304
305     if (!ist->hwaccel_ctx) {
306         ret = vdpau_alloc(s);
307         if (ret < 0)
308             return ret;
309     }
310     ctx       = ist->hwaccel_ctx;
311     vdpau_ctx = s->hwaccel_context;
312
313     ret = av_vdpau_get_profile(s, &profile);
314     if (ret < 0) {
315         av_log(NULL, loglevel, "No known VDPAU decoder profile for this stream.\n");
316         return AVERROR(EINVAL);
317     }
318
319     if (ctx->decoder)
320         ctx->decoder_destroy(ctx->decoder);
321
322     err = ctx->decoder_create(ctx->device, profile,
323                               s->coded_width, s->coded_height,
324                               16, &ctx->decoder);
325     if (err != VDP_STATUS_OK) {
326         av_log(NULL, loglevel, "Error creating the VDPAU decoder: %s\n",
327                ctx->get_error_string(err));
328         return AVERROR_UNKNOWN;
329     }
330
331     vdpau_ctx->decoder = ctx->decoder;
332
333     ist->hwaccel_get_buffer    = vdpau_get_buffer;
334     ist->hwaccel_retrieve_data = vdpau_retrieve_data;
335
336     return 0;
337 }
338
339 int vdpau_init(AVCodecContext *s)
340 {
341     InputStream *ist = s->opaque;
342     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
343     AVVDPAUContext *vdpau_ctx;
344     VDPAUContext *ctx;
345     VdpStatus err;
346     int profile, ret;
347
348     if (vdpau_api_ver == 1)
349         return vdpau_old_init(s);
350
351     if (!ist->hwaccel_ctx) {
352         ret = vdpau_alloc(s);
353         if (ret < 0)
354             return ret;
355     }
356     ctx       = ist->hwaccel_ctx;
357     vdpau_ctx = s->hwaccel_context;
358
359     ret = av_vdpau_get_profile(s, &profile);
360     if (ret < 0) {
361         av_log(NULL, loglevel, "No known VDPAU decoder profile for this stream.\n");
362         return AVERROR(EINVAL);
363     }
364
365     if (ctx->decoder)
366         ctx->decoder_destroy(ctx->decoder);
367
368     err = ctx->decoder_create(ctx->device, profile,
369                               s->coded_width, s->coded_height,
370                               16, &ctx->decoder);
371     if (err != VDP_STATUS_OK) {
372         av_log(NULL, loglevel, "Error creating the VDPAU decoder: %s\n",
373                ctx->get_error_string(err));
374         return AVERROR_UNKNOWN;
375     }
376
377     vdpau_ctx->decoder = ctx->decoder;
378
379     ist->hwaccel_get_buffer    = vdpau_get_buffer;
380     ist->hwaccel_retrieve_data = vdpau_retrieve_data;
381
382     return 0;
383 }