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