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