2aa712e96376daa158a9cfad50737be704d82efe
[libav.git] / libavutil / hwcontext.c
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 "config.h"
20
21 #include "buffer.h"
22 #include "common.h"
23 #include "hwcontext.h"
24 #include "hwcontext_internal.h"
25 #include "imgutils.h"
26 #include "log.h"
27 #include "mem.h"
28 #include "pixdesc.h"
29 #include "pixfmt.h"
30
31 static const HWContextType *hw_table[] = {
32 #if CONFIG_VDPAU
33 &ff_hwcontext_type_vdpau,
34 #endif
35 NULL,
36 };
37
38 static const AVClass hwdevice_ctx_class = {
39 .class_name = "AVHWDeviceContext",
40 .item_name = av_default_item_name,
41 .version = LIBAVUTIL_VERSION_INT,
42 };
43
44 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
45 {
46 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
47
48 /* uninit might still want access the hw context and the user
49 * free() callback might destroy it, so uninit has to be called first */
50 if (ctx->internal->hw_type->device_uninit)
51 ctx->internal->hw_type->device_uninit(ctx);
52
53 if (ctx->free)
54 ctx->free(ctx);
55
56 av_freep(&ctx->hwctx);
57 av_freep(&ctx->internal->priv);
58 av_freep(&ctx->internal);
59 av_freep(&ctx);
60 }
61
62 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
63 {
64 AVHWDeviceContext *ctx;
65 AVBufferRef *buf;
66 const HWContextType *hw_type = NULL;
67 int i;
68
69 for (i = 0; hw_table[i]; i++) {
70 if (hw_table[i]->type == type) {
71 hw_type = hw_table[i];
72 break;
73 }
74 }
75 if (!hw_type)
76 return NULL;
77
78 ctx = av_mallocz(sizeof(*ctx));
79 if (!ctx)
80 return NULL;
81
82 ctx->internal = av_mallocz(sizeof(*ctx->internal));
83 if (!ctx->internal)
84 goto fail;
85
86 if (hw_type->device_priv_size) {
87 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
88 if (!ctx->internal->priv)
89 goto fail;
90 }
91
92 if (hw_type->device_hwctx_size) {
93 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
94 if (!ctx->hwctx)
95 goto fail;
96 }
97
98 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
99 hwdevice_ctx_free, NULL,
100 AV_BUFFER_FLAG_READONLY);
101 if (!buf)
102 goto fail;
103
104 ctx->type = type;
105 ctx->av_class = &hwdevice_ctx_class;
106
107 ctx->internal->hw_type = hw_type;
108
109 return buf;
110
111 fail:
112 if (ctx->internal)
113 av_freep(&ctx->internal->priv);
114 av_freep(&ctx->internal);
115 av_freep(&ctx->hwctx);
116 av_freep(&ctx);
117 return NULL;
118 }
119
120 int av_hwdevice_ctx_init(AVBufferRef *ref)
121 {
122 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
123 int ret;
124
125 if (ctx->internal->hw_type->device_init) {
126 ret = ctx->internal->hw_type->device_init(ctx);
127 if (ret < 0)
128 goto fail;
129 }
130
131 return 0;
132 fail:
133 if (ctx->internal->hw_type->device_uninit)
134 ctx->internal->hw_type->device_uninit(ctx);
135 return ret;
136 }
137
138 static const AVClass hwframe_ctx_class = {
139 .class_name = "AVHWFramesContext",
140 .item_name = av_default_item_name,
141 .version = LIBAVUTIL_VERSION_INT,
142 };
143
144 static void hwframe_ctx_free(void *opaque, uint8_t *data)
145 {
146 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
147
148 if (ctx->internal->pool_internal)
149 av_buffer_pool_uninit(&ctx->internal->pool_internal);
150
151 if (ctx->internal->hw_type->frames_uninit)
152 ctx->internal->hw_type->frames_uninit(ctx);
153
154 if (ctx->free)
155 ctx->free(ctx);
156
157 av_buffer_unref(&ctx->device_ref);
158
159 av_freep(&ctx->hwctx);
160 av_freep(&ctx->internal->priv);
161 av_freep(&ctx->internal);
162 av_freep(&ctx);
163 }
164
165 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
166 {
167 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
168 const HWContextType *hw_type = device_ctx->internal->hw_type;
169 AVHWFramesContext *ctx;
170 AVBufferRef *buf, *device_ref = NULL;;
171
172 ctx = av_mallocz(sizeof(*ctx));
173 if (!ctx)
174 return NULL;
175
176 ctx->internal = av_mallocz(sizeof(*ctx->internal));
177 if (!ctx->internal)
178 goto fail;
179
180 if (hw_type->frames_priv_size) {
181 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
182 if (!ctx->internal->priv)
183 goto fail;
184 }
185
186 if (hw_type->frames_hwctx_size) {
187 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
188 if (!ctx->hwctx)
189 goto fail;
190 }
191
192 device_ref = av_buffer_ref(device_ref_in);
193 if (!device_ref)
194 goto fail;
195
196 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
197 hwframe_ctx_free, NULL,
198 AV_BUFFER_FLAG_READONLY);
199 if (!buf)
200 goto fail;
201
202 ctx->av_class = &hwframe_ctx_class;
203 ctx->device_ref = device_ref;
204 ctx->device_ctx = device_ctx;
205 ctx->format = AV_PIX_FMT_NONE;
206
207 ctx->internal->hw_type = hw_type;
208
209 return buf;
210
211 fail:
212 if (device_ref)
213 av_buffer_unref(&device_ref);
214 if (ctx->internal)
215 av_freep(&ctx->internal->priv);
216 av_freep(&ctx->internal);
217 av_freep(&ctx->hwctx);
218 av_freep(&ctx);
219 return NULL;
220 }
221
222 static int hwframe_pool_prealloc(AVBufferRef *ref)
223 {
224 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
225 AVFrame **frames;
226 int i, ret = 0;
227
228 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
229 if (!frames)
230 return AVERROR(ENOMEM);
231
232 for (i = 0; i < ctx->initial_pool_size; i++) {
233 frames[i] = av_frame_alloc();
234 if (!frames[i])
235 goto fail;
236
237 ret = av_hwframe_get_buffer(ref, frames[i], 0);
238 if (ret < 0)
239 goto fail;
240 }
241
242 fail:
243 for (i = 0; i < ctx->initial_pool_size; i++)
244 av_frame_free(&frames[i]);
245 av_freep(&frames);
246
247 return ret;
248 }
249
250 int av_hwframe_ctx_init(AVBufferRef *ref)
251 {
252 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
253 const enum AVPixelFormat *pix_fmt;
254 int ret;
255
256 /* validate the pixel format */
257 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
258 if (*pix_fmt == ctx->format)
259 break;
260 }
261 if (*pix_fmt == AV_PIX_FMT_NONE) {
262 av_log(ctx, AV_LOG_ERROR,
263 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
264 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
265 return AVERROR(ENOSYS);
266 }
267
268 /* validate the dimensions */
269 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
270 if (ret < 0)
271 return ret;
272
273 /* format-specific init */
274 if (ctx->internal->hw_type->frames_init) {
275 ret = ctx->internal->hw_type->frames_init(ctx);
276 if (ret < 0)
277 goto fail;
278 }
279
280 if (ctx->internal->pool_internal && !ctx->pool)
281 ctx->pool = ctx->internal->pool_internal;
282
283 /* preallocate the frames in the pool, if requested */
284 if (ctx->initial_pool_size > 0) {
285 ret = hwframe_pool_prealloc(ref);
286 if (ret < 0)
287 goto fail;
288 }
289
290 return 0;
291 fail:
292 if (ctx->internal->hw_type->frames_uninit)
293 ctx->internal->hw_type->frames_uninit(ctx);
294 return ret;
295 }
296
297 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
298 enum AVHWFrameTransferDirection dir,
299 enum AVPixelFormat **formats, int flags)
300 {
301 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
302
303 if (!ctx->internal->hw_type->transfer_get_formats)
304 return AVERROR(ENOSYS);
305
306 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
307 }
308
309 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
310 {
311 AVFrame *frame_tmp;
312 int ret = 0;
313
314 frame_tmp = av_frame_alloc();
315 if (!frame_tmp)
316 return AVERROR(ENOMEM);
317
318 /* if the format is set, use that
319 * otherwise pick the first supported one */
320 if (dst->format >= 0) {
321 frame_tmp->format = dst->format;
322 } else {
323 enum AVPixelFormat *formats;
324
325 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
326 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
327 &formats, 0);
328 if (ret < 0)
329 goto fail;
330 frame_tmp->format = formats[0];
331 av_freep(&formats);
332 }
333 frame_tmp->width = src->width;
334 frame_tmp->height = src->height;
335
336 ret = av_frame_get_buffer(frame_tmp, 32);
337 if (ret < 0)
338 goto fail;
339
340 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
341 if (ret < 0)
342 goto fail;
343
344 av_frame_move_ref(dst, frame_tmp);
345
346 fail:
347 av_frame_free(&frame_tmp);
348 return ret;
349 }
350
351 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
352 {
353 AVHWFramesContext *ctx;
354 int ret;
355
356 if (!dst->buf[0])
357 return transfer_data_alloc(dst, src, flags);
358
359 if (src->hw_frames_ctx) {
360 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
361
362 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
363 if (ret < 0)
364 return ret;
365 } else if (dst->hw_frames_ctx) {
366 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
367
368 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
369 if (ret < 0)
370 return ret;
371 } else
372 return AVERROR(ENOSYS);
373
374 return 0;
375 }
376
377 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
378 {
379 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
380 int ret;
381
382 if (!ctx->internal->hw_type->frames_get_buffer)
383 return AVERROR(ENOSYS);
384
385 if (!ctx->pool)
386 return AVERROR(EINVAL);
387
388 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
389 if (!frame->hw_frames_ctx)
390 return AVERROR(ENOMEM);
391
392 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
393 if (ret < 0) {
394 av_buffer_unref(&frame->hw_frames_ctx);
395 return ret;
396 }
397
398 return 0;
399 }