2 * This file is part of FFmpeg.
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.
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.
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
23 #include "hwcontext.h"
24 #include "hwcontext_internal.h"
31 static const HWContextType *hw_table[] = {
33 &ff_hwcontext_type_vdpau,
38 static const AVClass hwdevice_ctx_class = {
39 .class_name = "AVHWDeviceContext",
40 .item_name = av_default_item_name,
41 .version = LIBAVUTIL_VERSION_INT,
44 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
46 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
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);
56 av_freep(&ctx->hwctx);
57 av_freep(&ctx->internal->priv);
58 av_freep(&ctx->internal);
62 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
64 AVHWDeviceContext *ctx;
66 const HWContextType *hw_type = NULL;
69 for (i = 0; hw_table[i]; i++) {
70 if (hw_table[i]->type == type) {
71 hw_type = hw_table[i];
78 ctx = av_mallocz(sizeof(*ctx));
82 ctx->internal = av_mallocz(sizeof(*ctx->internal));
86 if (hw_type->device_priv_size) {
87 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
88 if (!ctx->internal->priv)
92 if (hw_type->device_hwctx_size) {
93 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
98 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
99 hwdevice_ctx_free, NULL,
100 AV_BUFFER_FLAG_READONLY);
105 ctx->av_class = &hwdevice_ctx_class;
107 ctx->internal->hw_type = hw_type;
113 av_freep(&ctx->internal->priv);
114 av_freep(&ctx->internal);
115 av_freep(&ctx->hwctx);
120 int av_hwdevice_ctx_init(AVBufferRef *ref)
122 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
125 if (ctx->internal->hw_type->device_init) {
126 ret = ctx->internal->hw_type->device_init(ctx);
133 if (ctx->internal->hw_type->device_uninit)
134 ctx->internal->hw_type->device_uninit(ctx);
138 static const AVClass hwframe_ctx_class = {
139 .class_name = "AVHWFramesContext",
140 .item_name = av_default_item_name,
141 .version = LIBAVUTIL_VERSION_INT,
144 static void hwframe_ctx_free(void *opaque, uint8_t *data)
146 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
148 if (ctx->internal->pool_internal)
149 av_buffer_pool_uninit(&ctx->internal->pool_internal);
151 if (ctx->internal->hw_type->frames_uninit)
152 ctx->internal->hw_type->frames_uninit(ctx);
157 av_buffer_unref(&ctx->device_ref);
159 av_freep(&ctx->hwctx);
160 av_freep(&ctx->internal->priv);
161 av_freep(&ctx->internal);
165 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
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;
172 ctx = av_mallocz(sizeof(*ctx));
176 ctx->internal = av_mallocz(sizeof(*ctx->internal));
180 if (hw_type->frames_priv_size) {
181 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
182 if (!ctx->internal->priv)
186 if (hw_type->frames_hwctx_size) {
187 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
192 device_ref = av_buffer_ref(device_ref_in);
196 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
197 hwframe_ctx_free, NULL,
198 AV_BUFFER_FLAG_READONLY);
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;
207 ctx->internal->hw_type = hw_type;
213 av_buffer_unref(&device_ref);
215 av_freep(&ctx->internal->priv);
216 av_freep(&ctx->internal);
217 av_freep(&ctx->hwctx);
222 static int hwframe_pool_prealloc(AVBufferRef *ref)
224 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
228 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
230 return AVERROR(ENOMEM);
232 for (i = 0; i < ctx->initial_pool_size; i++) {
233 frames[i] = av_frame_alloc();
237 ret = av_hwframe_get_buffer(ref, frames[i], 0);
243 for (i = 0; i < ctx->initial_pool_size; i++)
244 av_frame_free(&frames[i]);
250 int av_hwframe_ctx_init(AVBufferRef *ref)
252 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
253 const enum AVPixelFormat *pix_fmt;
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)
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);
268 /* validate the dimensions */
269 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
273 /* format-specific init */
274 if (ctx->internal->hw_type->frames_init) {
275 ret = ctx->internal->hw_type->frames_init(ctx);
280 if (ctx->internal->pool_internal && !ctx->pool)
281 ctx->pool = ctx->internal->pool_internal;
283 /* preallocate the frames in the pool, if requested */
284 if (ctx->initial_pool_size > 0) {
285 ret = hwframe_pool_prealloc(ref);
292 if (ctx->internal->hw_type->frames_uninit)
293 ctx->internal->hw_type->frames_uninit(ctx);
297 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
298 enum AVHWFrameTransferDirection dir,
299 enum AVPixelFormat **formats, int flags)
301 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
303 if (!ctx->internal->hw_type->transfer_get_formats)
304 return AVERROR(ENOSYS);
306 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
309 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
314 frame_tmp = av_frame_alloc();
316 return AVERROR(ENOMEM);
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;
323 enum AVPixelFormat *formats;
325 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
326 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
330 frame_tmp->format = formats[0];
333 frame_tmp->width = src->width;
334 frame_tmp->height = src->height;
336 ret = av_frame_get_buffer(frame_tmp, 32);
340 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
344 av_frame_move_ref(dst, frame_tmp);
347 av_frame_free(&frame_tmp);
351 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
353 AVHWFramesContext *ctx;
357 return transfer_data_alloc(dst, src, flags);
359 if (src->hw_frames_ctx) {
360 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
362 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
365 } else if (dst->hw_frames_ctx) {
366 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
368 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
372 return AVERROR(ENOSYS);
377 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
379 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
382 if (!ctx->internal->hw_type->frames_get_buffer)
383 return AVERROR(ENOSYS);
386 return AVERROR(EINVAL);
388 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
389 if (!frame->hw_frames_ctx)
390 return AVERROR(ENOMEM);
392 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
394 av_buffer_unref(&frame->hw_frames_ctx);