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 * const hw_table[] = {
33 &ff_hwcontext_type_cuda,
36 &ff_hwcontext_type_d3d11va,
39 &ff_hwcontext_type_drm,
42 &ff_hwcontext_type_dxva2,
45 &ff_hwcontext_type_opencl,
48 &ff_hwcontext_type_qsv,
51 &ff_hwcontext_type_vaapi,
54 &ff_hwcontext_type_vdpau,
56 #if CONFIG_VIDEOTOOLBOX
57 &ff_hwcontext_type_videotoolbox,
60 &ff_hwcontext_type_mediacodec,
65 static const char *const hw_type_names[] = {
66 [AV_HWDEVICE_TYPE_CUDA] = "cuda",
67 [AV_HWDEVICE_TYPE_DRM] = "drm",
68 [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
69 [AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
70 [AV_HWDEVICE_TYPE_OPENCL] = "opencl",
71 [AV_HWDEVICE_TYPE_QSV] = "qsv",
72 [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
73 [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
74 [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
75 [AV_HWDEVICE_TYPE_MEDIACODEC] = "mediacodec",
78 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
81 for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
82 if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
85 return AV_HWDEVICE_TYPE_NONE;
88 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
90 if (type > AV_HWDEVICE_TYPE_NONE &&
91 type < FF_ARRAY_ELEMS(hw_type_names))
92 return hw_type_names[type];
97 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
99 enum AVHWDeviceType next;
101 for (i = 0; hw_table[i]; i++) {
102 if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
104 if (!set || hw_table[i]->type < next) {
105 next = hw_table[i]->type;
109 return set ? next : AV_HWDEVICE_TYPE_NONE;
112 static const AVClass hwdevice_ctx_class = {
113 .class_name = "AVHWDeviceContext",
114 .item_name = av_default_item_name,
115 .version = LIBAVUTIL_VERSION_INT,
118 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
120 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
122 /* uninit might still want access the hw context and the user
123 * free() callback might destroy it, so uninit has to be called first */
124 if (ctx->internal->hw_type->device_uninit)
125 ctx->internal->hw_type->device_uninit(ctx);
130 av_buffer_unref(&ctx->internal->source_device);
132 av_freep(&ctx->hwctx);
133 av_freep(&ctx->internal->priv);
134 av_freep(&ctx->internal);
138 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
140 AVHWDeviceContext *ctx;
142 const HWContextType *hw_type = NULL;
145 for (i = 0; hw_table[i]; i++) {
146 if (hw_table[i]->type == type) {
147 hw_type = hw_table[i];
154 ctx = av_mallocz(sizeof(*ctx));
158 ctx->internal = av_mallocz(sizeof(*ctx->internal));
162 if (hw_type->device_priv_size) {
163 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
164 if (!ctx->internal->priv)
168 if (hw_type->device_hwctx_size) {
169 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
174 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
175 hwdevice_ctx_free, NULL,
176 AV_BUFFER_FLAG_READONLY);
181 ctx->av_class = &hwdevice_ctx_class;
183 ctx->internal->hw_type = hw_type;
189 av_freep(&ctx->internal->priv);
190 av_freep(&ctx->internal);
191 av_freep(&ctx->hwctx);
196 int av_hwdevice_ctx_init(AVBufferRef *ref)
198 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
201 if (ctx->internal->hw_type->device_init) {
202 ret = ctx->internal->hw_type->device_init(ctx);
209 if (ctx->internal->hw_type->device_uninit)
210 ctx->internal->hw_type->device_uninit(ctx);
214 static const AVClass hwframe_ctx_class = {
215 .class_name = "AVHWFramesContext",
216 .item_name = av_default_item_name,
217 .version = LIBAVUTIL_VERSION_INT,
220 static void hwframe_ctx_free(void *opaque, uint8_t *data)
222 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
224 if (ctx->internal->pool_internal)
225 av_buffer_pool_uninit(&ctx->internal->pool_internal);
227 if (ctx->internal->hw_type->frames_uninit)
228 ctx->internal->hw_type->frames_uninit(ctx);
233 av_buffer_unref(&ctx->internal->source_frames);
235 av_buffer_unref(&ctx->device_ref);
237 av_freep(&ctx->hwctx);
238 av_freep(&ctx->internal->priv);
239 av_freep(&ctx->internal);
243 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
245 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
246 const HWContextType *hw_type = device_ctx->internal->hw_type;
247 AVHWFramesContext *ctx;
248 AVBufferRef *buf, *device_ref = NULL;
250 ctx = av_mallocz(sizeof(*ctx));
254 ctx->internal = av_mallocz(sizeof(*ctx->internal));
258 if (hw_type->frames_priv_size) {
259 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
260 if (!ctx->internal->priv)
264 if (hw_type->frames_hwctx_size) {
265 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
270 device_ref = av_buffer_ref(device_ref_in);
274 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
275 hwframe_ctx_free, NULL,
276 AV_BUFFER_FLAG_READONLY);
280 ctx->av_class = &hwframe_ctx_class;
281 ctx->device_ref = device_ref;
282 ctx->device_ctx = device_ctx;
283 ctx->format = AV_PIX_FMT_NONE;
284 ctx->sw_format = AV_PIX_FMT_NONE;
286 ctx->internal->hw_type = hw_type;
292 av_buffer_unref(&device_ref);
294 av_freep(&ctx->internal->priv);
295 av_freep(&ctx->internal);
296 av_freep(&ctx->hwctx);
301 static int hwframe_pool_prealloc(AVBufferRef *ref)
303 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
307 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
309 return AVERROR(ENOMEM);
311 for (i = 0; i < ctx->initial_pool_size; i++) {
312 frames[i] = av_frame_alloc();
316 ret = av_hwframe_get_buffer(ref, frames[i], 0);
322 for (i = 0; i < ctx->initial_pool_size; i++)
323 av_frame_free(&frames[i]);
329 int av_hwframe_ctx_init(AVBufferRef *ref)
331 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
332 const enum AVPixelFormat *pix_fmt;
335 if (ctx->internal->source_frames) {
336 /* A derived frame context is already initialised. */
340 /* validate the pixel format */
341 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
342 if (*pix_fmt == ctx->format)
345 if (*pix_fmt == AV_PIX_FMT_NONE) {
346 av_log(ctx, AV_LOG_ERROR,
347 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
348 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
349 return AVERROR(ENOSYS);
352 /* validate the dimensions */
353 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
357 /* format-specific init */
358 if (ctx->internal->hw_type->frames_init) {
359 ret = ctx->internal->hw_type->frames_init(ctx);
364 if (ctx->internal->pool_internal && !ctx->pool)
365 ctx->pool = ctx->internal->pool_internal;
367 /* preallocate the frames in the pool, if requested */
368 if (ctx->initial_pool_size > 0) {
369 ret = hwframe_pool_prealloc(ref);
376 if (ctx->internal->hw_type->frames_uninit)
377 ctx->internal->hw_type->frames_uninit(ctx);
381 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
382 enum AVHWFrameTransferDirection dir,
383 enum AVPixelFormat **formats, int flags)
385 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
387 if (!ctx->internal->hw_type->transfer_get_formats)
388 return AVERROR(ENOSYS);
390 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
393 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
395 AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
399 frame_tmp = av_frame_alloc();
401 return AVERROR(ENOMEM);
403 /* if the format is set, use that
404 * otherwise pick the first supported one */
405 if (dst->format >= 0) {
406 frame_tmp->format = dst->format;
408 enum AVPixelFormat *formats;
410 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
411 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
415 frame_tmp->format = formats[0];
418 frame_tmp->width = ctx->width;
419 frame_tmp->height = ctx->height;
421 ret = av_frame_get_buffer(frame_tmp, 32);
425 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
429 frame_tmp->width = src->width;
430 frame_tmp->height = src->height;
432 av_frame_move_ref(dst, frame_tmp);
435 av_frame_free(&frame_tmp);
439 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
441 AVHWFramesContext *ctx;
445 return transfer_data_alloc(dst, src, flags);
447 if (src->hw_frames_ctx) {
448 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
450 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
453 } else if (dst->hw_frames_ctx) {
454 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
456 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
460 return AVERROR(ENOSYS);
465 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
467 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
470 if (ctx->internal->source_frames) {
471 // This is a derived frame context, so we allocate in the source
472 // and map the frame immediately.
475 frame->format = ctx->format;
476 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
477 if (!frame->hw_frames_ctx)
478 return AVERROR(ENOMEM);
480 src_frame = av_frame_alloc();
482 return AVERROR(ENOMEM);
484 ret = av_hwframe_get_buffer(ctx->internal->source_frames,
487 av_frame_free(&src_frame);
491 ret = av_hwframe_map(frame, src_frame,
492 ctx->internal->source_allocation_map_flags);
494 av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
495 "frame context: %d.\n", ret);
496 av_frame_free(&src_frame);
500 // Free the source frame immediately - the mapped frame still
501 // contains a reference to it.
502 av_frame_free(&src_frame);
507 if (!ctx->internal->hw_type->frames_get_buffer)
508 return AVERROR(ENOSYS);
511 return AVERROR(EINVAL);
513 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
514 if (!frame->hw_frames_ctx)
515 return AVERROR(ENOMEM);
517 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
519 av_buffer_unref(&frame->hw_frames_ctx);
526 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
528 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
529 const HWContextType *hw_type = ctx->internal->hw_type;
531 if (hw_type->device_hwconfig_size == 0)
534 return av_mallocz(hw_type->device_hwconfig_size);
537 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
538 const void *hwconfig)
540 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
541 const HWContextType *hw_type = ctx->internal->hw_type;
542 AVHWFramesConstraints *constraints;
544 if (!hw_type->frames_get_constraints)
547 constraints = av_mallocz(sizeof(*constraints));
551 constraints->min_width = constraints->min_height = 0;
552 constraints->max_width = constraints->max_height = INT_MAX;
554 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
557 av_hwframe_constraints_free(&constraints);
562 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
565 av_freep(&(*constraints)->valid_hw_formats);
566 av_freep(&(*constraints)->valid_sw_formats);
568 av_freep(constraints);
571 int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
572 const char *device, AVDictionary *opts, int flags)
574 AVBufferRef *device_ref = NULL;
575 AVHWDeviceContext *device_ctx;
578 device_ref = av_hwdevice_ctx_alloc(type);
580 ret = AVERROR(ENOMEM);
583 device_ctx = (AVHWDeviceContext*)device_ref->data;
585 if (!device_ctx->internal->hw_type->device_create) {
586 ret = AVERROR(ENOSYS);
590 ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
595 ret = av_hwdevice_ctx_init(device_ref);
599 *pdevice_ref = device_ref;
602 av_buffer_unref(&device_ref);
607 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
608 enum AVHWDeviceType type,
609 AVBufferRef *src_ref, int flags)
611 AVBufferRef *dst_ref = NULL, *tmp_ref;
612 AVHWDeviceContext *dst_ctx, *tmp_ctx;
617 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
618 if (tmp_ctx->type == type) {
619 dst_ref = av_buffer_ref(tmp_ref);
621 ret = AVERROR(ENOMEM);
626 tmp_ref = tmp_ctx->internal->source_device;
629 dst_ref = av_hwdevice_ctx_alloc(type);
631 ret = AVERROR(ENOMEM);
634 dst_ctx = (AVHWDeviceContext*)dst_ref->data;
638 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
639 if (dst_ctx->internal->hw_type->device_derive) {
640 ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
644 dst_ctx->internal->source_device = av_buffer_ref(src_ref);
645 if (!dst_ctx->internal->source_device) {
646 ret = AVERROR(ENOMEM);
649 ret = av_hwdevice_ctx_init(dst_ref);
654 if (ret != AVERROR(ENOSYS))
657 tmp_ref = tmp_ctx->internal->source_device;
660 ret = AVERROR(ENOSYS);
664 *dst_ref_ptr = dst_ref;
668 av_buffer_unref(&dst_ref);
673 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
675 HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
676 AVHWFramesContext *ctx = opaque;
679 hwmap->unmap(ctx, hwmap);
681 av_frame_free(&hwmap->source);
683 av_buffer_unref(&hwmap->hw_frames_ctx);
688 int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
689 AVFrame *dst, const AVFrame *src,
690 void (*unmap)(AVHWFramesContext *ctx,
691 HWMapDescriptor *hwmap),
694 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
695 HWMapDescriptor *hwmap;
698 hwmap = av_mallocz(sizeof(*hwmap));
700 ret = AVERROR(ENOMEM);
704 hwmap->source = av_frame_alloc();
705 if (!hwmap->source) {
706 ret = AVERROR(ENOMEM);
709 ret = av_frame_ref(hwmap->source, src);
713 hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
714 if (!hwmap->hw_frames_ctx) {
715 ret = AVERROR(ENOMEM);
719 hwmap->unmap = unmap;
722 dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
723 &ff_hwframe_unmap, ctx, 0);
725 ret = AVERROR(ENOMEM);
733 av_buffer_unref(&hwmap->hw_frames_ctx);
734 av_frame_free(&hwmap->source);
740 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
742 AVHWFramesContext *src_frames, *dst_frames;
743 HWMapDescriptor *hwmap;
746 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
747 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
748 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
750 if ((src_frames == dst_frames &&
751 src->format == dst_frames->sw_format &&
752 dst->format == dst_frames->format) ||
753 (src_frames->internal->source_frames &&
754 src_frames->internal->source_frames->data ==
755 (uint8_t*)dst_frames)) {
756 // This is an unmap operation. We don't need to directly
757 // do anything here other than fill in the original frame,
758 // because the real unmap will be invoked when the last
759 // reference to the mapped frame disappears.
761 av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
762 "found when attempting unmap.\n");
763 return AVERROR(EINVAL);
765 hwmap = (HWMapDescriptor*)src->buf[0]->data;
767 return av_frame_ref(dst, hwmap->source);
771 if (src->hw_frames_ctx) {
772 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
774 if (src_frames->format == src->format &&
775 src_frames->internal->hw_type->map_from) {
776 ret = src_frames->internal->hw_type->map_from(src_frames,
778 if (ret != AVERROR(ENOSYS))
783 if (dst->hw_frames_ctx) {
784 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
786 if (dst_frames->format == dst->format &&
787 dst_frames->internal->hw_type->map_to) {
788 ret = dst_frames->internal->hw_type->map_to(dst_frames,
790 if (ret != AVERROR(ENOSYS))
795 return AVERROR(ENOSYS);
798 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
799 enum AVPixelFormat format,
800 AVBufferRef *derived_device_ctx,
801 AVBufferRef *source_frame_ctx,
804 AVBufferRef *dst_ref = NULL;
805 AVHWFramesContext *dst = NULL;
806 AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
809 if (src->internal->source_frames) {
810 AVHWFramesContext *src_src =
811 (AVHWFramesContext*)src->internal->source_frames->data;
812 AVHWDeviceContext *dst_dev =
813 (AVHWDeviceContext*)derived_device_ctx->data;
815 if (src_src->device_ctx == dst_dev) {
816 // This is actually an unmapping, so we just return a
817 // reference to the source frame context.
819 av_buffer_ref(src->internal->source_frames);
820 if (!*derived_frame_ctx) {
821 ret = AVERROR(ENOMEM);
828 dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
830 ret = AVERROR(ENOMEM);
834 dst = (AVHWFramesContext*)dst_ref->data;
836 dst->format = format;
837 dst->sw_format = src->sw_format;
838 dst->width = src->width;
839 dst->height = src->height;
841 dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
842 if (!dst->internal->source_frames) {
843 ret = AVERROR(ENOMEM);
847 dst->internal->source_allocation_map_flags =
848 flags & (AV_HWFRAME_MAP_READ |
849 AV_HWFRAME_MAP_WRITE |
850 AV_HWFRAME_MAP_OVERWRITE |
851 AV_HWFRAME_MAP_DIRECT);
853 ret = AVERROR(ENOSYS);
854 if (src->internal->hw_type->frames_derive_from)
855 ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
856 if (ret == AVERROR(ENOSYS) &&
857 dst->internal->hw_type->frames_derive_to)
858 ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
859 if (ret == AVERROR(ENOSYS))
864 *derived_frame_ctx = dst_ref;
869 av_buffer_unref(&dst->internal->source_frames);
870 av_buffer_unref(&dst_ref);
874 int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
876 HWMapDescriptor *hwmap = (HWMapDescriptor*)dst->buf[0]->data;
877 av_frame_unref(hwmap->source);
878 return av_frame_ref(hwmap->source, src);