]> git.sesse.net Git - ffmpeg/blob - ffmpeg_vdpau.c
Merge commit 'dbbaad32e36b9046cd19d6e3329e2ca7ab49b401'
[ffmpeg] / 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/buffer.h"
31 #include "libavutil/frame.h"
32 #include "libavutil/hwcontext.h"
33 #include "libavutil/hwcontext_vdpau.h"
34 #include "libavutil/pixfmt.h"
35
36 typedef struct VDPAUContext {
37     AVBufferRef *hw_frames_ctx;
38     AVFrame *tmp_frame;
39 } VDPAUContext;
40
41 typedef struct VDPAUHWDevicePriv {
42     VdpDeviceDestroy *device_destroy;
43     Display *dpy;
44 } VDPAUHWDevicePriv;
45
46 static void device_free(AVHWDeviceContext *ctx)
47 {
48     AVVDPAUDeviceContext *hwctx = ctx->hwctx;
49     VDPAUHWDevicePriv     *priv = ctx->user_opaque;
50
51     if (priv->device_destroy)
52         priv->device_destroy(hwctx->device);
53     if (priv->dpy)
54         XCloseDisplay(priv->dpy);
55     av_freep(&priv);
56 }
57
58 static void vdpau_uninit(AVCodecContext *s)
59 {
60     InputStream  *ist = s->opaque;
61     VDPAUContext *ctx = ist->hwaccel_ctx;
62
63     ist->hwaccel_uninit        = NULL;
64     ist->hwaccel_get_buffer    = NULL;
65     ist->hwaccel_retrieve_data = NULL;
66
67     av_buffer_unref(&ctx->hw_frames_ctx);
68     av_frame_free(&ctx->tmp_frame);
69
70     av_freep(&ist->hwaccel_ctx);
71     av_freep(&s->hwaccel_context);
72 }
73
74 static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
75 {
76     InputStream         *ist = s->opaque;
77     VDPAUContext        *ctx = ist->hwaccel_ctx;
78
79     return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
80 }
81
82 static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
83 {
84     InputStream        *ist = s->opaque;
85     VDPAUContext       *ctx = ist->hwaccel_ctx;
86     int ret;
87
88     ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
89     if (ret < 0)
90         return ret;
91
92     ret = av_frame_copy_props(ctx->tmp_frame, frame);
93     if (ret < 0) {
94         av_frame_unref(ctx->tmp_frame);
95         return ret;
96     }
97
98     av_frame_unref(frame);
99     av_frame_move_ref(frame, ctx->tmp_frame);
100
101     return 0;
102 }
103
104 static int vdpau_alloc(AVCodecContext *s)
105 {
106     InputStream  *ist = s->opaque;
107     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
108     VDPAUContext *ctx;
109     const char *display, *vendor;
110     VdpStatus err;
111     int ret;
112
113     VdpDevice                device;
114     VdpGetProcAddress       *get_proc_address;
115     VdpGetInformationString *get_information_string;
116
117     VDPAUHWDevicePriv    *device_priv = NULL;
118     AVBufferRef          *device_ref = NULL;
119     AVHWDeviceContext    *device_ctx;
120     AVVDPAUDeviceContext *device_hwctx;
121     AVHWFramesContext    *frames_ctx;
122
123     ctx = av_mallocz(sizeof(*ctx));
124     if (!ctx)
125         return AVERROR(ENOMEM);
126
127     device_priv = av_mallocz(sizeof(*device_priv));
128     if (!device_priv) {
129         av_freep(&ctx);
130         goto fail;
131     }
132
133     ist->hwaccel_ctx           = ctx;
134     ist->hwaccel_uninit        = vdpau_uninit;
135     ist->hwaccel_get_buffer    = vdpau_get_buffer;
136     ist->hwaccel_retrieve_data = vdpau_retrieve_data;
137
138     ctx->tmp_frame = av_frame_alloc();
139     if (!ctx->tmp_frame)
140         goto fail;
141
142     device_priv->dpy = XOpenDisplay(ist->hwaccel_device);
143     if (!device_priv->dpy) {
144         av_log(NULL, loglevel, "Cannot open the X11 display %s.\n",
145                XDisplayName(ist->hwaccel_device));
146         goto fail;
147     }
148     display = XDisplayString(device_priv->dpy);
149
150     err = vdp_device_create_x11(device_priv->dpy, XDefaultScreen(device_priv->dpy),
151                                 &device, &get_proc_address);
152     if (err != VDP_STATUS_OK) {
153         av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n",
154                display);
155         goto fail;
156     }
157
158 #define GET_CALLBACK(id, result)                                                \
159 do {                                                                            \
160     void *tmp;                                                                  \
161     err = get_proc_address(device, id, &tmp);                                   \
162     if (err != VDP_STATUS_OK) {                                                 \
163         av_log(NULL, loglevel, "Error getting the " #id " callback.\n");        \
164         goto fail;                                                              \
165     }                                                                           \
166     result = tmp;                                                               \
167 } while (0)
168
169     GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
170     GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY,         device_priv->device_destroy);
171
172     device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VDPAU);
173     if (!device_ref)
174         goto fail;
175     device_ctx                     = (AVHWDeviceContext*)device_ref->data;
176     device_hwctx                   = device_ctx->hwctx;
177     device_ctx->user_opaque        = device_priv;
178     device_ctx->free               = device_free;
179     device_hwctx->device           = device;
180     device_hwctx->get_proc_address = get_proc_address;
181
182     device_priv = NULL;
183
184     ret = av_hwdevice_ctx_init(device_ref);
185     if (ret < 0)
186         goto fail;
187
188     ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
189     if (!ctx->hw_frames_ctx)
190         goto fail;
191     av_buffer_unref(&device_ref);
192
193     frames_ctx            = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
194     frames_ctx->format    = AV_PIX_FMT_VDPAU;
195     frames_ctx->sw_format = s->sw_pix_fmt;
196     frames_ctx->width     = s->coded_width;
197     frames_ctx->height    = s->coded_height;
198
199     ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
200     if (ret < 0)
201         goto fail;
202
203     if (av_vdpau_bind_context(s, device, get_proc_address, 0))
204         goto fail;
205
206     get_information_string(&vendor);
207     av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, "
208            "to decode input stream #%d:%d.\n", vendor,
209            display, ist->file_index, ist->st->index);
210
211     return 0;
212
213 fail:
214     av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
215            ist->file_index, ist->st->index);
216     if (device_priv) {
217         if (device_priv->device_destroy)
218             device_priv->device_destroy(device);
219         if (device_priv->dpy)
220             XCloseDisplay(device_priv->dpy);
221     }
222     av_freep(&device_priv);
223     av_buffer_unref(&device_ref);
224     vdpau_uninit(s);
225     return AVERROR(EINVAL);
226 }
227
228 int vdpau_init(AVCodecContext *s)
229 {
230     InputStream *ist = s->opaque;
231
232     if (!ist->hwaccel_ctx) {
233         int ret = vdpau_alloc(s);
234         if (ret < 0)
235             return ret;
236     }
237
238     ist->hwaccel_get_buffer    = vdpau_get_buffer;
239     ist->hwaccel_retrieve_data = vdpau_retrieve_data;
240
241     return 0;
242 }