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_qsv,
48 &ff_hwcontext_type_vaapi,
51 &ff_hwcontext_type_vdpau,
53 #if CONFIG_VIDEOTOOLBOX
54 &ff_hwcontext_type_videotoolbox,
59 static const char *const hw_type_names[] = {
60 [AV_HWDEVICE_TYPE_CUDA] = "cuda",
61 [AV_HWDEVICE_TYPE_DRM] = "drm",
62 [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
63 [AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
64 [AV_HWDEVICE_TYPE_QSV] = "qsv",
65 [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
66 [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
67 [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
70 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
73 for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
74 if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
77 return AV_HWDEVICE_TYPE_NONE;
80 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
82 if (type >= 0 && type < FF_ARRAY_ELEMS(hw_type_names))
83 return hw_type_names[type];
88 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
90 enum AVHWDeviceType next;
92 for (i = 0; hw_table[i]; i++) {
93 if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
95 if (!set || hw_table[i]->type < next) {
96 next = hw_table[i]->type;
100 return set ? next : AV_HWDEVICE_TYPE_NONE;
103 static const AVClass hwdevice_ctx_class = {
104 .class_name = "AVHWDeviceContext",
105 .item_name = av_default_item_name,
106 .version = LIBAVUTIL_VERSION_INT,
109 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
111 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
113 /* uninit might still want access the hw context and the user
114 * free() callback might destroy it, so uninit has to be called first */
115 if (ctx->internal->hw_type->device_uninit)
116 ctx->internal->hw_type->device_uninit(ctx);
121 av_buffer_unref(&ctx->internal->source_device);
123 av_freep(&ctx->hwctx);
124 av_freep(&ctx->internal->priv);
125 av_freep(&ctx->internal);
129 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
131 AVHWDeviceContext *ctx;
133 const HWContextType *hw_type = NULL;
136 for (i = 0; hw_table[i]; i++) {
137 if (hw_table[i]->type == type) {
138 hw_type = hw_table[i];
145 ctx = av_mallocz(sizeof(*ctx));
149 ctx->internal = av_mallocz(sizeof(*ctx->internal));
153 if (hw_type->device_priv_size) {
154 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
155 if (!ctx->internal->priv)
159 if (hw_type->device_hwctx_size) {
160 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
165 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
166 hwdevice_ctx_free, NULL,
167 AV_BUFFER_FLAG_READONLY);
172 ctx->av_class = &hwdevice_ctx_class;
174 ctx->internal->hw_type = hw_type;
180 av_freep(&ctx->internal->priv);
181 av_freep(&ctx->internal);
182 av_freep(&ctx->hwctx);
187 int av_hwdevice_ctx_init(AVBufferRef *ref)
189 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
192 if (ctx->internal->hw_type->device_init) {
193 ret = ctx->internal->hw_type->device_init(ctx);
200 if (ctx->internal->hw_type->device_uninit)
201 ctx->internal->hw_type->device_uninit(ctx);
205 static const AVClass hwframe_ctx_class = {
206 .class_name = "AVHWFramesContext",
207 .item_name = av_default_item_name,
208 .version = LIBAVUTIL_VERSION_INT,
211 static void hwframe_ctx_free(void *opaque, uint8_t *data)
213 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
215 if (ctx->internal->source_frames) {
216 av_buffer_unref(&ctx->internal->source_frames);
219 if (ctx->internal->pool_internal)
220 av_buffer_pool_uninit(&ctx->internal->pool_internal);
222 if (ctx->internal->hw_type->frames_uninit)
223 ctx->internal->hw_type->frames_uninit(ctx);
229 av_buffer_unref(&ctx->device_ref);
231 av_freep(&ctx->hwctx);
232 av_freep(&ctx->internal->priv);
233 av_freep(&ctx->internal);
237 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
239 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
240 const HWContextType *hw_type = device_ctx->internal->hw_type;
241 AVHWFramesContext *ctx;
242 AVBufferRef *buf, *device_ref = NULL;
244 ctx = av_mallocz(sizeof(*ctx));
248 ctx->internal = av_mallocz(sizeof(*ctx->internal));
252 if (hw_type->frames_priv_size) {
253 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
254 if (!ctx->internal->priv)
258 if (hw_type->frames_hwctx_size) {
259 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
264 device_ref = av_buffer_ref(device_ref_in);
268 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
269 hwframe_ctx_free, NULL,
270 AV_BUFFER_FLAG_READONLY);
274 ctx->av_class = &hwframe_ctx_class;
275 ctx->device_ref = device_ref;
276 ctx->device_ctx = device_ctx;
277 ctx->format = AV_PIX_FMT_NONE;
278 ctx->sw_format = AV_PIX_FMT_NONE;
280 ctx->internal->hw_type = hw_type;
286 av_buffer_unref(&device_ref);
288 av_freep(&ctx->internal->priv);
289 av_freep(&ctx->internal);
290 av_freep(&ctx->hwctx);
295 static int hwframe_pool_prealloc(AVBufferRef *ref)
297 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
301 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
303 return AVERROR(ENOMEM);
305 for (i = 0; i < ctx->initial_pool_size; i++) {
306 frames[i] = av_frame_alloc();
310 ret = av_hwframe_get_buffer(ref, frames[i], 0);
316 for (i = 0; i < ctx->initial_pool_size; i++)
317 av_frame_free(&frames[i]);
323 int av_hwframe_ctx_init(AVBufferRef *ref)
325 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
326 const enum AVPixelFormat *pix_fmt;
329 if (ctx->internal->source_frames) {
330 /* A derived frame context is already initialised. */
334 /* validate the pixel format */
335 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
336 if (*pix_fmt == ctx->format)
339 if (*pix_fmt == AV_PIX_FMT_NONE) {
340 av_log(ctx, AV_LOG_ERROR,
341 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
342 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
343 return AVERROR(ENOSYS);
346 /* validate the dimensions */
347 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
351 /* format-specific init */
352 if (ctx->internal->hw_type->frames_init) {
353 ret = ctx->internal->hw_type->frames_init(ctx);
358 if (ctx->internal->pool_internal && !ctx->pool)
359 ctx->pool = ctx->internal->pool_internal;
361 /* preallocate the frames in the pool, if requested */
362 if (ctx->initial_pool_size > 0) {
363 ret = hwframe_pool_prealloc(ref);
370 if (ctx->internal->hw_type->frames_uninit)
371 ctx->internal->hw_type->frames_uninit(ctx);
375 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
376 enum AVHWFrameTransferDirection dir,
377 enum AVPixelFormat **formats, int flags)
379 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
381 if (!ctx->internal->hw_type->transfer_get_formats)
382 return AVERROR(ENOSYS);
384 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
387 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
389 AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
393 frame_tmp = av_frame_alloc();
395 return AVERROR(ENOMEM);
397 /* if the format is set, use that
398 * otherwise pick the first supported one */
399 if (dst->format >= 0) {
400 frame_tmp->format = dst->format;
402 enum AVPixelFormat *formats;
404 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
405 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
409 frame_tmp->format = formats[0];
412 frame_tmp->width = ctx->width;
413 frame_tmp->height = ctx->height;
415 ret = av_frame_get_buffer(frame_tmp, 32);
419 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
423 frame_tmp->width = src->width;
424 frame_tmp->height = src->height;
426 av_frame_move_ref(dst, frame_tmp);
429 av_frame_free(&frame_tmp);
433 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
435 AVHWFramesContext *ctx;
439 return transfer_data_alloc(dst, src, flags);
441 if (src->hw_frames_ctx) {
442 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
444 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
447 } else if (dst->hw_frames_ctx) {
448 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
450 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
454 return AVERROR(ENOSYS);
459 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
461 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
464 if (ctx->internal->source_frames) {
465 // This is a derived frame context, so we allocate in the source
466 // and map the frame immediately.
469 frame->format = ctx->format;
470 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
471 if (!frame->hw_frames_ctx)
472 return AVERROR(ENOMEM);
474 src_frame = av_frame_alloc();
476 return AVERROR(ENOMEM);
478 ret = av_hwframe_get_buffer(ctx->internal->source_frames,
483 ret = av_hwframe_map(frame, src_frame,
484 ctx->internal->source_allocation_map_flags);
486 av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
487 "frame context: %d.\n", ret);
488 av_frame_free(&src_frame);
492 // Free the source frame immediately - the mapped frame still
493 // contains a reference to it.
494 av_frame_free(&src_frame);
499 if (!ctx->internal->hw_type->frames_get_buffer)
500 return AVERROR(ENOSYS);
503 return AVERROR(EINVAL);
505 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
506 if (!frame->hw_frames_ctx)
507 return AVERROR(ENOMEM);
509 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
511 av_buffer_unref(&frame->hw_frames_ctx);
518 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
520 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
521 const HWContextType *hw_type = ctx->internal->hw_type;
523 if (hw_type->device_hwconfig_size == 0)
526 return av_mallocz(hw_type->device_hwconfig_size);
529 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
530 const void *hwconfig)
532 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
533 const HWContextType *hw_type = ctx->internal->hw_type;
534 AVHWFramesConstraints *constraints;
536 if (!hw_type->frames_get_constraints)
539 constraints = av_mallocz(sizeof(*constraints));
543 constraints->min_width = constraints->min_height = 0;
544 constraints->max_width = constraints->max_height = INT_MAX;
546 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
549 av_hwframe_constraints_free(&constraints);
554 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
557 av_freep(&(*constraints)->valid_hw_formats);
558 av_freep(&(*constraints)->valid_sw_formats);
560 av_freep(constraints);
563 int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
564 const char *device, AVDictionary *opts, int flags)
566 AVBufferRef *device_ref = NULL;
567 AVHWDeviceContext *device_ctx;
570 device_ref = av_hwdevice_ctx_alloc(type);
572 ret = AVERROR(ENOMEM);
575 device_ctx = (AVHWDeviceContext*)device_ref->data;
577 if (!device_ctx->internal->hw_type->device_create) {
578 ret = AVERROR(ENOSYS);
582 ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
587 ret = av_hwdevice_ctx_init(device_ref);
591 *pdevice_ref = device_ref;
594 av_buffer_unref(&device_ref);
599 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
600 enum AVHWDeviceType type,
601 AVBufferRef *src_ref, int flags)
603 AVBufferRef *dst_ref = NULL, *tmp_ref;
604 AVHWDeviceContext *dst_ctx, *tmp_ctx;
609 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
610 if (tmp_ctx->type == type) {
611 dst_ref = av_buffer_ref(tmp_ref);
613 ret = AVERROR(ENOMEM);
618 tmp_ref = tmp_ctx->internal->source_device;
621 dst_ref = av_hwdevice_ctx_alloc(type);
623 ret = AVERROR(ENOMEM);
626 dst_ctx = (AVHWDeviceContext*)dst_ref->data;
630 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
631 if (dst_ctx->internal->hw_type->device_derive) {
632 ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
636 dst_ctx->internal->source_device = av_buffer_ref(src_ref);
637 if (!dst_ctx->internal->source_device) {
638 ret = AVERROR(ENOMEM);
643 if (ret != AVERROR(ENOSYS))
646 tmp_ref = tmp_ctx->internal->source_device;
649 ret = AVERROR(ENOSYS);
653 *dst_ref_ptr = dst_ref;
657 av_buffer_unref(&dst_ref);
662 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
664 HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
665 AVHWFramesContext *ctx = opaque;
668 hwmap->unmap(ctx, hwmap);
670 av_frame_free(&hwmap->source);
672 av_buffer_unref(&hwmap->hw_frames_ctx);
677 int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
678 AVFrame *dst, const AVFrame *src,
679 void (*unmap)(AVHWFramesContext *ctx,
680 HWMapDescriptor *hwmap),
683 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
684 HWMapDescriptor *hwmap;
687 hwmap = av_mallocz(sizeof(*hwmap));
689 ret = AVERROR(ENOMEM);
693 hwmap->source = av_frame_alloc();
694 if (!hwmap->source) {
695 ret = AVERROR(ENOMEM);
698 ret = av_frame_ref(hwmap->source, src);
702 hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
703 if (!hwmap->hw_frames_ctx) {
704 ret = AVERROR(ENOMEM);
708 hwmap->unmap = unmap;
711 dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
712 &ff_hwframe_unmap, ctx, 0);
714 ret = AVERROR(ENOMEM);
722 av_buffer_unref(&hwmap->hw_frames_ctx);
723 av_frame_free(&hwmap->source);
729 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
731 AVHWFramesContext *src_frames, *dst_frames;
732 HWMapDescriptor *hwmap;
735 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
736 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
737 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
739 if ((src_frames == dst_frames &&
740 src->format == dst_frames->sw_format &&
741 dst->format == dst_frames->format) ||
742 (src_frames->internal->source_frames &&
743 src_frames->internal->source_frames->data ==
744 (uint8_t*)dst_frames)) {
745 // This is an unmap operation. We don't need to directly
746 // do anything here other than fill in the original frame,
747 // because the real unmap will be invoked when the last
748 // reference to the mapped frame disappears.
750 av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
751 "found when attempting unmap.\n");
752 return AVERROR(EINVAL);
754 hwmap = (HWMapDescriptor*)src->buf[0]->data;
756 return av_frame_ref(dst, hwmap->source);
760 if (src->hw_frames_ctx) {
761 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
763 if (src_frames->format == src->format &&
764 src_frames->internal->hw_type->map_from) {
765 ret = src_frames->internal->hw_type->map_from(src_frames,
767 if (ret != AVERROR(ENOSYS))
772 if (dst->hw_frames_ctx) {
773 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
775 if (dst_frames->format == dst->format &&
776 dst_frames->internal->hw_type->map_to) {
777 ret = dst_frames->internal->hw_type->map_to(dst_frames,
779 if (ret != AVERROR(ENOSYS))
784 return AVERROR(ENOSYS);
787 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
788 enum AVPixelFormat format,
789 AVBufferRef *derived_device_ctx,
790 AVBufferRef *source_frame_ctx,
793 AVBufferRef *dst_ref = NULL;
794 AVHWFramesContext *dst = NULL;
795 AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
798 if (src->internal->source_frames) {
799 AVHWFramesContext *src_src =
800 (AVHWFramesContext*)src->internal->source_frames->data;
801 AVHWDeviceContext *dst_dev =
802 (AVHWDeviceContext*)derived_device_ctx->data;
804 if (src_src->device_ctx == dst_dev) {
805 // This is actually an unmapping, so we just return a
806 // reference to the source frame context.
808 av_buffer_ref(src->internal->source_frames);
809 if (!*derived_frame_ctx) {
810 ret = AVERROR(ENOMEM);
817 dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
819 ret = AVERROR(ENOMEM);
823 dst = (AVHWFramesContext*)dst_ref->data;
825 dst->format = format;
826 dst->sw_format = src->sw_format;
827 dst->width = src->width;
828 dst->height = src->height;
830 dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
831 if (!dst->internal->source_frames) {
832 ret = AVERROR(ENOMEM);
836 dst->internal->source_allocation_map_flags =
837 flags & (AV_HWFRAME_MAP_READ |
838 AV_HWFRAME_MAP_WRITE |
839 AV_HWFRAME_MAP_OVERWRITE |
840 AV_HWFRAME_MAP_DIRECT);
842 ret = AVERROR(ENOSYS);
843 if (src->internal->hw_type->frames_derive_from)
844 ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
845 if (ret == AVERROR(ENOSYS) &&
846 dst->internal->hw_type->frames_derive_to)
847 ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
848 if (ret == AVERROR(ENOSYS))
853 *derived_frame_ctx = dst_ref;
858 av_buffer_unref(&dst->internal->source_frames);
859 av_buffer_unref(&dst_ref);