hwcontext: add a VDPAU implementation
[libav.git] / avconv_vdpau.c
CommitLineData
7671dd7c
AK
1/*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; 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 "avconv.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
35typedef 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;
7671dd7c
AK
45 VdpVideoSurfaceCreate *video_surface_create;
46 VdpVideoSurfaceDestroy *video_surface_destroy;
47 VdpVideoSurfaceGetBitsYCbCr *video_surface_get_bits;
48 VdpVideoSurfaceGetParameters *video_surface_get_parameters;
49 VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *video_surface_query;
50
51 AVFrame *tmp_frame;
52
53 enum AVPixelFormat pix_fmt;
54 VdpYCbCrFormat vdpau_format;
55} VDPAUContext;
56
57static void vdpau_uninit(AVCodecContext *s)
58{
59 InputStream *ist = s->opaque;
60 VDPAUContext *ctx = ist->hwaccel_ctx;
61
62 ist->hwaccel_uninit = NULL;
63 ist->hwaccel_get_buffer = NULL;
64 ist->hwaccel_retrieve_data = NULL;
65
7671dd7c
AK
66 if (ctx->device_destroy)
67 ctx->device_destroy(ctx->device);
68
69 if (ctx->dpy)
70 XCloseDisplay(ctx->dpy);
71
72 av_frame_free(&ctx->tmp_frame);
73
74 av_freep(&ist->hwaccel_ctx);
75 av_freep(&s->hwaccel_context);
76}
77
78static void vdpau_release_buffer(void *opaque, uint8_t *data)
79{
80 VdpVideoSurface surface = *(VdpVideoSurface*)data;
81 VDPAUContext *ctx = opaque;
82
83 ctx->video_surface_destroy(surface);
84 av_freep(&data);
85}
86
87static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
88{
89 InputStream *ist = s->opaque;
90 VDPAUContext *ctx = ist->hwaccel_ctx;
91 VdpVideoSurface *surface;
92 VdpStatus err;
1f9237f2
RDC
93 VdpChromaType chroma;
94 uint32_t width, height;
7671dd7c
AK
95
96 av_assert0(frame->format == AV_PIX_FMT_VDPAU);
97
1f9237f2
RDC
98 if (av_vdpau_get_surface_parameters(s, &chroma, &width, &height))
99 return AVERROR(ENOSYS);
100
7671dd7c
AK
101 surface = av_malloc(sizeof(*surface));
102 if (!surface)
103 return AVERROR(ENOMEM);
104
105 frame->buf[0] = av_buffer_create((uint8_t*)surface, sizeof(*surface),
106 vdpau_release_buffer, ctx,
107 AV_BUFFER_FLAG_READONLY);
108 if (!frame->buf[0]) {
109 av_freep(&surface);
110 return AVERROR(ENOMEM);
111 }
112
113 // properly we should keep a pool of surfaces instead of creating
114 // them anew for each frame, but since we don't care about speed
115 // much in this code, we don't bother
1f9237f2
RDC
116 err = ctx->video_surface_create(ctx->device, chroma, width, height,
117 surface);
7671dd7c
AK
118 if (err != VDP_STATUS_OK) {
119 av_log(NULL, AV_LOG_ERROR, "Error allocating a VDPAU video surface: %s\n",
120 ctx->get_error_string(err));
121 av_buffer_unref(&frame->buf[0]);
122 return AVERROR_UNKNOWN;
123 }
124
125 frame->data[3] = (uint8_t*)(uintptr_t)*surface;
126
127 return 0;
128}
129
130static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
131{
132 VdpVideoSurface surface = (VdpVideoSurface)(uintptr_t)frame->data[3];
133 InputStream *ist = s->opaque;
134 VDPAUContext *ctx = ist->hwaccel_ctx;
135 VdpStatus err;
136 int ret, chroma_type;
137
138 err = ctx->video_surface_get_parameters(surface, &chroma_type,
139 &ctx->tmp_frame->width,
140 &ctx->tmp_frame->height);
141 if (err != VDP_STATUS_OK) {
142 av_log(NULL, AV_LOG_ERROR, "Error getting surface parameters: %s\n",
143 ctx->get_error_string(err));
144 return AVERROR_UNKNOWN;
145 }
146 ctx->tmp_frame->format = ctx->pix_fmt;
147
148 ret = av_frame_get_buffer(ctx->tmp_frame, 32);
149 if (ret < 0)
150 return ret;
151
152 ctx->tmp_frame->width = frame->width;
153 ctx->tmp_frame->height = frame->height;
154
155 err = ctx->video_surface_get_bits(surface, ctx->vdpau_format,
156 (void * const *)ctx->tmp_frame->data,
157 ctx->tmp_frame->linesize);
158 if (err != VDP_STATUS_OK) {
159 av_log(NULL, AV_LOG_ERROR, "Error retrieving frame data from VDPAU: %s\n",
160 ctx->get_error_string(err));
161 ret = AVERROR_UNKNOWN;
162 goto fail;
163 }
164
165 if (ctx->vdpau_format == VDP_YCBCR_FORMAT_YV12)
166 FFSWAP(uint8_t*, ctx->tmp_frame->data[1], ctx->tmp_frame->data[2]);
167
168 ret = av_frame_copy_props(ctx->tmp_frame, frame);
169 if (ret < 0)
170 goto fail;
171
172 av_frame_unref(frame);
173 av_frame_move_ref(frame, ctx->tmp_frame);
174 return 0;
175
176fail:
177 av_frame_unref(ctx->tmp_frame);
178 return ret;
179}
180
181static const int vdpau_formats[][2] = {
182 { VDP_YCBCR_FORMAT_YV12, AV_PIX_FMT_YUV420P },
183 { VDP_YCBCR_FORMAT_NV12, AV_PIX_FMT_NV12 },
184 { VDP_YCBCR_FORMAT_YUYV, AV_PIX_FMT_YUYV422 },
185 { VDP_YCBCR_FORMAT_UYVY, AV_PIX_FMT_UYVY422 },
186};
187
188static int vdpau_alloc(AVCodecContext *s)
189{
190 InputStream *ist = s->opaque;
191 int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
7671dd7c
AK
192 VDPAUContext *ctx;
193 const char *display, *vendor;
194 VdpStatus err;
195 int i;
196
197 ctx = av_mallocz(sizeof(*ctx));
198 if (!ctx)
199 return AVERROR(ENOMEM);
200
201 ist->hwaccel_ctx = ctx;
202 ist->hwaccel_uninit = vdpau_uninit;
203 ist->hwaccel_get_buffer = vdpau_get_buffer;
204 ist->hwaccel_retrieve_data = vdpau_retrieve_data;
205
206 ctx->tmp_frame = av_frame_alloc();
207 if (!ctx->tmp_frame)
208 goto fail;
209
210 ctx->dpy = XOpenDisplay(ist->hwaccel_device);
211 if (!ctx->dpy) {
212 av_log(NULL, loglevel, "Cannot open the X11 display %s.\n",
213 XDisplayName(ist->hwaccel_device));
214 goto fail;
215 }
216 display = XDisplayString(ctx->dpy);
217
218 err = vdp_device_create_x11(ctx->dpy, XDefaultScreen(ctx->dpy), &ctx->device,
219 &ctx->get_proc_address);
220 if (err != VDP_STATUS_OK) {
221 av_log(NULL, loglevel, "VDPAU device creation on X11 display %s failed.\n",
222 display);
223 goto fail;
224 }
225
226#define GET_CALLBACK(id, result) \
227do { \
228 void *tmp; \
229 err = ctx->get_proc_address(ctx->device, id, &tmp); \
230 if (err != VDP_STATUS_OK) { \
231 av_log(NULL, loglevel, "Error getting the " #id " callback.\n"); \
232 goto fail; \
233 } \
234 ctx->result = tmp; \
235} while (0)
236
237 GET_CALLBACK(VDP_FUNC_ID_GET_ERROR_STRING, get_error_string);
238 GET_CALLBACK(VDP_FUNC_ID_GET_INFORMATION_STRING, get_information_string);
239 GET_CALLBACK(VDP_FUNC_ID_DEVICE_DESTROY, device_destroy);
7671dd7c
AK
240 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_CREATE, video_surface_create);
241 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, video_surface_destroy);
242 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR, video_surface_get_bits);
243 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_GET_PARAMETERS, video_surface_get_parameters);
244 GET_CALLBACK(VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
245 video_surface_query);
246
247 for (i = 0; i < FF_ARRAY_ELEMS(vdpau_formats); i++) {
248 VdpBool supported;
249 err = ctx->video_surface_query(ctx->device, VDP_CHROMA_TYPE_420,
250 vdpau_formats[i][0], &supported);
251 if (err != VDP_STATUS_OK) {
252 av_log(NULL, loglevel,
253 "Error querying VDPAU surface capabilities: %s\n",
254 ctx->get_error_string(err));
255 goto fail;
256 }
257 if (supported)
258 break;
259 }
260 if (i == FF_ARRAY_ELEMS(vdpau_formats)) {
261 av_log(NULL, loglevel,
262 "No supported VDPAU format for retrieving the data.\n");
263 return AVERROR(EINVAL);
264 }
265 ctx->vdpau_format = vdpau_formats[i][0];
266 ctx->pix_fmt = vdpau_formats[i][1];
267
8de1d679 268 if (av_vdpau_bind_context(s, ctx->device, ctx->get_proc_address, 0))
7671dd7c 269 goto fail;
7671dd7c
AK
270
271 ctx->get_information_string(&vendor);
272 av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU -- %s -- on X11 display %s, "
273 "to decode input stream #%d:%d.\n", vendor,
274 display, ist->file_index, ist->st->index);
275
276 return 0;
277
278fail:
279 av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
280 ist->file_index, ist->st->index);
281 vdpau_uninit(s);
282 return AVERROR(EINVAL);
283}
284
285int vdpau_init(AVCodecContext *s)
286{
287 InputStream *ist = s->opaque;
7671dd7c
AK
288
289 if (!ist->hwaccel_ctx) {
8de1d679 290 int ret = vdpau_alloc(s);
7671dd7c
AK
291 if (ret < 0)
292 return ret;
293 }
7671dd7c
AK
294
295 ist->hwaccel_get_buffer = vdpau_get_buffer;
296 ist->hwaccel_retrieve_data = vdpau_retrieve_data;
297
298 return 0;
299}