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