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