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_cuda,
36 &ff_hwcontext_type_dxva2,
39 &ff_hwcontext_type_qsv,
42 &ff_hwcontext_type_vaapi,
45 &ff_hwcontext_type_vdpau,
47 #if CONFIG_VIDEOTOOLBOX
48 &ff_hwcontext_type_videotoolbox,
53 static const AVClass hwdevice_ctx_class = {
54 .class_name = "AVHWDeviceContext",
55 .item_name = av_default_item_name,
56 .version = LIBAVUTIL_VERSION_INT,
59 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
61 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
63 /* uninit might still want access the hw context and the user
64 * free() callback might destroy it, so uninit has to be called first */
65 if (ctx->internal->hw_type->device_uninit)
66 ctx->internal->hw_type->device_uninit(ctx);
71 av_freep(&ctx->hwctx);
72 av_freep(&ctx->internal->priv);
73 av_freep(&ctx->internal);
77 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
79 AVHWDeviceContext *ctx;
81 const HWContextType *hw_type = NULL;
84 for (i = 0; hw_table[i]; i++) {
85 if (hw_table[i]->type == type) {
86 hw_type = hw_table[i];
93 ctx = av_mallocz(sizeof(*ctx));
97 ctx->internal = av_mallocz(sizeof(*ctx->internal));
101 if (hw_type->device_priv_size) {
102 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
103 if (!ctx->internal->priv)
107 if (hw_type->device_hwctx_size) {
108 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
113 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
114 hwdevice_ctx_free, NULL,
115 AV_BUFFER_FLAG_READONLY);
120 ctx->av_class = &hwdevice_ctx_class;
122 ctx->internal->hw_type = hw_type;
128 av_freep(&ctx->internal->priv);
129 av_freep(&ctx->internal);
130 av_freep(&ctx->hwctx);
135 int av_hwdevice_ctx_init(AVBufferRef *ref)
137 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
140 if (ctx->internal->hw_type->device_init) {
141 ret = ctx->internal->hw_type->device_init(ctx);
148 if (ctx->internal->hw_type->device_uninit)
149 ctx->internal->hw_type->device_uninit(ctx);
153 static const AVClass hwframe_ctx_class = {
154 .class_name = "AVHWFramesContext",
155 .item_name = av_default_item_name,
156 .version = LIBAVUTIL_VERSION_INT,
159 static void hwframe_ctx_free(void *opaque, uint8_t *data)
161 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
163 if (ctx->internal->source_frames) {
164 av_buffer_unref(&ctx->internal->source_frames);
167 if (ctx->internal->pool_internal)
168 av_buffer_pool_uninit(&ctx->internal->pool_internal);
170 if (ctx->internal->hw_type->frames_uninit)
171 ctx->internal->hw_type->frames_uninit(ctx);
177 av_buffer_unref(&ctx->device_ref);
179 av_freep(&ctx->hwctx);
180 av_freep(&ctx->internal->priv);
181 av_freep(&ctx->internal);
185 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
187 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
188 const HWContextType *hw_type = device_ctx->internal->hw_type;
189 AVHWFramesContext *ctx;
190 AVBufferRef *buf, *device_ref = NULL;
192 ctx = av_mallocz(sizeof(*ctx));
196 ctx->internal = av_mallocz(sizeof(*ctx->internal));
200 if (hw_type->frames_priv_size) {
201 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
202 if (!ctx->internal->priv)
206 if (hw_type->frames_hwctx_size) {
207 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
212 device_ref = av_buffer_ref(device_ref_in);
216 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
217 hwframe_ctx_free, NULL,
218 AV_BUFFER_FLAG_READONLY);
222 ctx->av_class = &hwframe_ctx_class;
223 ctx->device_ref = device_ref;
224 ctx->device_ctx = device_ctx;
225 ctx->format = AV_PIX_FMT_NONE;
226 ctx->sw_format = AV_PIX_FMT_NONE;
228 ctx->internal->hw_type = hw_type;
234 av_buffer_unref(&device_ref);
236 av_freep(&ctx->internal->priv);
237 av_freep(&ctx->internal);
238 av_freep(&ctx->hwctx);
243 static int hwframe_pool_prealloc(AVBufferRef *ref)
245 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
249 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
251 return AVERROR(ENOMEM);
253 for (i = 0; i < ctx->initial_pool_size; i++) {
254 frames[i] = av_frame_alloc();
258 ret = av_hwframe_get_buffer(ref, frames[i], 0);
264 for (i = 0; i < ctx->initial_pool_size; i++)
265 av_frame_free(&frames[i]);
271 int av_hwframe_ctx_init(AVBufferRef *ref)
273 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
274 const enum AVPixelFormat *pix_fmt;
277 if (ctx->internal->source_frames) {
278 /* A derived frame context is already initialised. */
282 /* validate the pixel format */
283 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
284 if (*pix_fmt == ctx->format)
287 if (*pix_fmt == AV_PIX_FMT_NONE) {
288 av_log(ctx, AV_LOG_ERROR,
289 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
290 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
291 return AVERROR(ENOSYS);
294 /* validate the dimensions */
295 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
299 /* format-specific init */
300 if (ctx->internal->hw_type->frames_init) {
301 ret = ctx->internal->hw_type->frames_init(ctx);
306 if (ctx->internal->pool_internal && !ctx->pool)
307 ctx->pool = ctx->internal->pool_internal;
309 /* preallocate the frames in the pool, if requested */
310 if (ctx->initial_pool_size > 0) {
311 ret = hwframe_pool_prealloc(ref);
318 if (ctx->internal->hw_type->frames_uninit)
319 ctx->internal->hw_type->frames_uninit(ctx);
323 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
324 enum AVHWFrameTransferDirection dir,
325 enum AVPixelFormat **formats, int flags)
327 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
329 if (!ctx->internal->hw_type->transfer_get_formats)
330 return AVERROR(ENOSYS);
332 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
335 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
337 AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
341 frame_tmp = av_frame_alloc();
343 return AVERROR(ENOMEM);
345 /* if the format is set, use that
346 * otherwise pick the first supported one */
347 if (dst->format >= 0) {
348 frame_tmp->format = dst->format;
350 enum AVPixelFormat *formats;
352 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
353 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
357 frame_tmp->format = formats[0];
360 frame_tmp->width = ctx->width;
361 frame_tmp->height = ctx->height;
363 ret = av_frame_get_buffer(frame_tmp, 32);
367 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
371 frame_tmp->width = src->width;
372 frame_tmp->height = src->height;
374 av_frame_move_ref(dst, frame_tmp);
377 av_frame_free(&frame_tmp);
381 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
383 AVHWFramesContext *ctx;
387 return transfer_data_alloc(dst, src, flags);
389 if (src->hw_frames_ctx) {
390 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
392 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
395 } else if (dst->hw_frames_ctx) {
396 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
398 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
402 return AVERROR(ENOSYS);
407 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
409 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
412 if (ctx->internal->source_frames) {
413 // This is a derived frame context, so we allocate in the source
414 // and map the frame immediately.
417 src_frame = av_frame_alloc();
419 return AVERROR(ENOMEM);
421 ret = av_hwframe_get_buffer(ctx->internal->source_frames,
426 ret = av_hwframe_map(frame, src_frame, 0);
428 av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
429 "frame context: %d.\n", ret);
430 av_frame_free(&src_frame);
434 // Free the source frame immediately - the mapped frame still
435 // contains a reference to it.
436 av_frame_free(&src_frame);
441 if (!ctx->internal->hw_type->frames_get_buffer)
442 return AVERROR(ENOSYS);
445 return AVERROR(EINVAL);
447 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
448 if (!frame->hw_frames_ctx)
449 return AVERROR(ENOMEM);
451 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
453 av_buffer_unref(&frame->hw_frames_ctx);
460 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
462 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
463 const HWContextType *hw_type = ctx->internal->hw_type;
465 if (hw_type->device_hwconfig_size == 0)
468 return av_mallocz(hw_type->device_hwconfig_size);
471 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
472 const void *hwconfig)
474 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
475 const HWContextType *hw_type = ctx->internal->hw_type;
476 AVHWFramesConstraints *constraints;
478 if (!hw_type->frames_get_constraints)
481 constraints = av_mallocz(sizeof(*constraints));
485 constraints->min_width = constraints->min_height = 0;
486 constraints->max_width = constraints->max_height = INT_MAX;
488 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
491 av_hwframe_constraints_free(&constraints);
496 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
499 av_freep(&(*constraints)->valid_hw_formats);
500 av_freep(&(*constraints)->valid_sw_formats);
502 av_freep(constraints);
505 int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
506 const char *device, AVDictionary *opts, int flags)
508 AVBufferRef *device_ref = NULL;
509 AVHWDeviceContext *device_ctx;
512 device_ref = av_hwdevice_ctx_alloc(type);
514 ret = AVERROR(ENOMEM);
517 device_ctx = (AVHWDeviceContext*)device_ref->data;
519 if (!device_ctx->internal->hw_type->device_create) {
520 ret = AVERROR(ENOSYS);
524 ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
529 ret = av_hwdevice_ctx_init(device_ref);
533 *pdevice_ref = device_ref;
536 av_buffer_unref(&device_ref);
541 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
543 HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
544 AVHWFramesContext *ctx = opaque;
547 hwmap->unmap(ctx, hwmap);
549 av_frame_free(&hwmap->source);
551 av_buffer_unref(&hwmap->hw_frames_ctx);
556 int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
557 AVFrame *dst, const AVFrame *src,
558 void (*unmap)(AVHWFramesContext *ctx,
559 HWMapDescriptor *hwmap),
562 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
563 HWMapDescriptor *hwmap;
566 hwmap = av_mallocz(sizeof(*hwmap));
568 ret = AVERROR(ENOMEM);
572 hwmap->source = av_frame_alloc();
573 if (!hwmap->source) {
574 ret = AVERROR(ENOMEM);
577 ret = av_frame_ref(hwmap->source, src);
581 hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
582 if (!hwmap->hw_frames_ctx) {
583 ret = AVERROR(ENOMEM);
587 hwmap->unmap = unmap;
590 dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
591 &ff_hwframe_unmap, ctx, 0);
593 ret = AVERROR(ENOMEM);
601 av_buffer_unref(&hwmap->hw_frames_ctx);
602 av_frame_free(&hwmap->source);
608 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
610 AVHWFramesContext *src_frames, *dst_frames;
611 HWMapDescriptor *hwmap;
614 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
615 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
616 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
618 if ((src_frames == dst_frames &&
619 src->format == dst_frames->sw_format &&
620 dst->format == dst_frames->format) ||
621 (src_frames->internal->source_frames &&
622 src_frames->internal->source_frames->data ==
623 (uint8_t*)dst_frames)) {
624 // This is an unmap operation. We don't need to directly
625 // do anything here other than fill in the original frame,
626 // because the real unmap will be invoked when the last
627 // reference to the mapped frame disappears.
629 av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
630 "found when attempting unmap.\n");
631 return AVERROR(EINVAL);
633 hwmap = (HWMapDescriptor*)src->buf[0]->data;
635 return av_frame_ref(dst, hwmap->source);
639 if (src->hw_frames_ctx) {
640 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
642 if (src_frames->format == src->format &&
643 src_frames->internal->hw_type->map_from) {
644 ret = src_frames->internal->hw_type->map_from(src_frames,
646 if (ret != AVERROR(ENOSYS))
651 if (dst->hw_frames_ctx) {
652 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
654 if (dst_frames->format == dst->format &&
655 dst_frames->internal->hw_type->map_to) {
656 ret = dst_frames->internal->hw_type->map_to(dst_frames,
658 if (ret != AVERROR(ENOSYS))
663 return AVERROR(ENOSYS);
666 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
667 enum AVPixelFormat format,
668 AVBufferRef *derived_device_ctx,
669 AVBufferRef *source_frame_ctx,
672 AVBufferRef *dst_ref = NULL;
673 AVHWFramesContext *dst = NULL;
674 AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
677 if (src->internal->source_frames) {
678 AVHWFramesContext *src_src =
679 (AVHWFramesContext*)src->internal->source_frames->data;
680 AVHWDeviceContext *dst_dev =
681 (AVHWDeviceContext*)derived_device_ctx->data;
683 if (src_src->device_ctx == dst_dev) {
684 // This is actually an unmapping, so we just return a
685 // reference to the source frame context.
687 av_buffer_ref(src->internal->source_frames);
688 if (!*derived_frame_ctx) {
689 ret = AVERROR(ENOMEM);
696 dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
698 ret = AVERROR(ENOMEM);
702 dst = (AVHWFramesContext*)dst_ref->data;
704 dst->format = format;
705 dst->sw_format = src->sw_format;
706 dst->width = src->width;
707 dst->height = src->height;
709 dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
710 if (!dst->internal->source_frames) {
711 ret = AVERROR(ENOMEM);
715 ret = av_hwframe_ctx_init(dst_ref);
719 *derived_frame_ctx = dst_ref;
724 av_buffer_unref(&dst->internal->source_frames);
725 av_buffer_unref(&dst_ref);