2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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_dxva2,
39 &ff_hwcontext_type_qsv,
42 &ff_hwcontext_type_vaapi,
45 &ff_hwcontext_type_vdpau,
50 const char *hw_type_names[] = {
51 [AV_HWDEVICE_TYPE_CUDA] = "cuda",
52 [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
53 [AV_HWDEVICE_TYPE_QSV] = "qsv",
54 [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
55 [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
58 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
61 for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
62 if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
65 return AV_HWDEVICE_TYPE_NONE;
68 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
70 if (type > AV_HWDEVICE_TYPE_NONE &&
71 type < FF_ARRAY_ELEMS(hw_type_names))
72 return hw_type_names[type];
77 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
79 enum AVHWDeviceType next;
81 for (i = 0; hw_table[i]; i++) {
82 if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
84 if (!set || hw_table[i]->type < next) {
85 next = hw_table[i]->type;
89 return set ? next : AV_HWDEVICE_TYPE_NONE;
92 static const AVClass hwdevice_ctx_class = {
93 .class_name = "AVHWDeviceContext",
94 .item_name = av_default_item_name,
95 .version = LIBAVUTIL_VERSION_INT,
98 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
100 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
102 /* uninit might still want access the hw context and the user
103 * free() callback might destroy it, so uninit has to be called first */
104 if (ctx->internal->hw_type->device_uninit)
105 ctx->internal->hw_type->device_uninit(ctx);
110 av_buffer_unref(&ctx->internal->source_device);
112 av_freep(&ctx->hwctx);
113 av_freep(&ctx->internal->priv);
114 av_freep(&ctx->internal);
118 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
120 AVHWDeviceContext *ctx;
122 const HWContextType *hw_type = NULL;
125 for (i = 0; hw_table[i]; i++) {
126 if (hw_table[i]->type == type) {
127 hw_type = hw_table[i];
134 ctx = av_mallocz(sizeof(*ctx));
138 ctx->internal = av_mallocz(sizeof(*ctx->internal));
142 if (hw_type->device_priv_size) {
143 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
144 if (!ctx->internal->priv)
148 if (hw_type->device_hwctx_size) {
149 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
154 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
155 hwdevice_ctx_free, NULL,
156 AV_BUFFER_FLAG_READONLY);
161 ctx->av_class = &hwdevice_ctx_class;
163 ctx->internal->hw_type = hw_type;
169 av_freep(&ctx->internal->priv);
170 av_freep(&ctx->internal);
171 av_freep(&ctx->hwctx);
176 int av_hwdevice_ctx_init(AVBufferRef *ref)
178 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
181 if (ctx->internal->hw_type->device_init) {
182 ret = ctx->internal->hw_type->device_init(ctx);
189 if (ctx->internal->hw_type->device_uninit)
190 ctx->internal->hw_type->device_uninit(ctx);
194 static const AVClass hwframe_ctx_class = {
195 .class_name = "AVHWFramesContext",
196 .item_name = av_default_item_name,
197 .version = LIBAVUTIL_VERSION_INT,
200 static void hwframe_ctx_free(void *opaque, uint8_t *data)
202 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
204 if (ctx->internal->source_frames) {
205 av_buffer_unref(&ctx->internal->source_frames);
208 if (ctx->internal->pool_internal)
209 av_buffer_pool_uninit(&ctx->internal->pool_internal);
211 if (ctx->internal->hw_type->frames_uninit)
212 ctx->internal->hw_type->frames_uninit(ctx);
218 av_buffer_unref(&ctx->device_ref);
220 av_freep(&ctx->hwctx);
221 av_freep(&ctx->internal->priv);
222 av_freep(&ctx->internal);
226 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
228 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
229 const HWContextType *hw_type = device_ctx->internal->hw_type;
230 AVHWFramesContext *ctx;
231 AVBufferRef *buf, *device_ref = NULL;;
233 ctx = av_mallocz(sizeof(*ctx));
237 ctx->internal = av_mallocz(sizeof(*ctx->internal));
241 if (hw_type->frames_priv_size) {
242 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
243 if (!ctx->internal->priv)
247 if (hw_type->frames_hwctx_size) {
248 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
253 device_ref = av_buffer_ref(device_ref_in);
257 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
258 hwframe_ctx_free, NULL,
259 AV_BUFFER_FLAG_READONLY);
263 ctx->av_class = &hwframe_ctx_class;
264 ctx->device_ref = device_ref;
265 ctx->device_ctx = device_ctx;
266 ctx->format = AV_PIX_FMT_NONE;
267 ctx->sw_format = AV_PIX_FMT_NONE;
269 ctx->internal->hw_type = hw_type;
275 av_buffer_unref(&device_ref);
277 av_freep(&ctx->internal->priv);
278 av_freep(&ctx->internal);
279 av_freep(&ctx->hwctx);
284 static int hwframe_pool_prealloc(AVBufferRef *ref)
286 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
290 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
292 return AVERROR(ENOMEM);
294 for (i = 0; i < ctx->initial_pool_size; i++) {
295 frames[i] = av_frame_alloc();
299 ret = av_hwframe_get_buffer(ref, frames[i], 0);
305 for (i = 0; i < ctx->initial_pool_size; i++)
306 av_frame_free(&frames[i]);
312 int av_hwframe_ctx_init(AVBufferRef *ref)
314 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
315 const enum AVPixelFormat *pix_fmt;
318 if (ctx->internal->source_frames) {
319 /* A derived frame context is already initialised. */
323 /* validate the pixel format */
324 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
325 if (*pix_fmt == ctx->format)
328 if (*pix_fmt == AV_PIX_FMT_NONE) {
329 av_log(ctx, AV_LOG_ERROR,
330 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
331 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
332 return AVERROR(ENOSYS);
335 /* validate the dimensions */
336 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
340 /* format-specific init */
341 if (ctx->internal->hw_type->frames_init) {
342 ret = ctx->internal->hw_type->frames_init(ctx);
347 if (ctx->internal->pool_internal && !ctx->pool)
348 ctx->pool = ctx->internal->pool_internal;
350 /* preallocate the frames in the pool, if requested */
351 if (ctx->initial_pool_size > 0) {
352 ret = hwframe_pool_prealloc(ref);
359 if (ctx->internal->hw_type->frames_uninit)
360 ctx->internal->hw_type->frames_uninit(ctx);
364 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
365 enum AVHWFrameTransferDirection dir,
366 enum AVPixelFormat **formats, int flags)
368 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
370 if (!ctx->internal->hw_type->transfer_get_formats)
371 return AVERROR(ENOSYS);
373 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
376 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
378 AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
382 frame_tmp = av_frame_alloc();
384 return AVERROR(ENOMEM);
386 /* if the format is set, use that
387 * otherwise pick the first supported one */
388 if (dst->format >= 0) {
389 frame_tmp->format = dst->format;
391 enum AVPixelFormat *formats;
393 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
394 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
398 frame_tmp->format = formats[0];
401 frame_tmp->width = ctx->width;
402 frame_tmp->height = ctx->height;
404 ret = av_frame_get_buffer(frame_tmp, 32);
408 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
412 frame_tmp->width = src->width;
413 frame_tmp->height = src->height;
415 av_frame_move_ref(dst, frame_tmp);
418 av_frame_free(&frame_tmp);
422 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
424 AVHWFramesContext *ctx;
428 return transfer_data_alloc(dst, src, flags);
430 if (src->hw_frames_ctx) {
431 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
433 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
436 } else if (dst->hw_frames_ctx) {
437 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
439 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
443 return AVERROR(ENOSYS);
448 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
450 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
453 if (ctx->internal->source_frames) {
454 // This is a derived frame context, so we allocate in the source
455 // and map the frame immediately.
458 frame->format = ctx->format;
459 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
460 if (!frame->hw_frames_ctx)
461 return AVERROR(ENOMEM);
463 src_frame = av_frame_alloc();
465 return AVERROR(ENOMEM);
467 ret = av_hwframe_get_buffer(ctx->internal->source_frames,
472 ret = av_hwframe_map(frame, src_frame,
473 ctx->internal->source_allocation_map_flags);
475 av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
476 "frame context: %d.\n", ret);
477 av_frame_free(&src_frame);
481 // Free the source frame immediately - the mapped frame still
482 // contains a reference to it.
483 av_frame_free(&src_frame);
488 if (!ctx->internal->hw_type->frames_get_buffer)
489 return AVERROR(ENOSYS);
492 return AVERROR(EINVAL);
494 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
495 if (!frame->hw_frames_ctx)
496 return AVERROR(ENOMEM);
498 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
500 av_buffer_unref(&frame->hw_frames_ctx);
507 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
509 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
510 const HWContextType *hw_type = ctx->internal->hw_type;
512 if (hw_type->device_hwconfig_size == 0)
515 return av_mallocz(hw_type->device_hwconfig_size);
518 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
519 const void *hwconfig)
521 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
522 const HWContextType *hw_type = ctx->internal->hw_type;
523 AVHWFramesConstraints *constraints;
525 if (!hw_type->frames_get_constraints)
528 constraints = av_mallocz(sizeof(*constraints));
532 constraints->min_width = constraints->min_height = 0;
533 constraints->max_width = constraints->max_height = INT_MAX;
535 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
538 av_hwframe_constraints_free(&constraints);
543 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
546 av_freep(&(*constraints)->valid_hw_formats);
547 av_freep(&(*constraints)->valid_sw_formats);
549 av_freep(constraints);
552 int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
553 const char *device, AVDictionary *opts, int flags)
555 AVBufferRef *device_ref = NULL;
556 AVHWDeviceContext *device_ctx;
559 device_ref = av_hwdevice_ctx_alloc(type);
561 ret = AVERROR(ENOMEM);
564 device_ctx = (AVHWDeviceContext*)device_ref->data;
566 if (!device_ctx->internal->hw_type->device_create) {
567 ret = AVERROR(ENOSYS);
571 ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
576 ret = av_hwdevice_ctx_init(device_ref);
580 *pdevice_ref = device_ref;
583 av_buffer_unref(&device_ref);
588 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
589 enum AVHWDeviceType type,
590 AVBufferRef *src_ref, int flags)
592 AVBufferRef *dst_ref = NULL, *tmp_ref;
593 AVHWDeviceContext *dst_ctx, *tmp_ctx;
598 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
599 if (tmp_ctx->type == type) {
600 dst_ref = av_buffer_ref(tmp_ref);
602 ret = AVERROR(ENOMEM);
607 tmp_ref = tmp_ctx->internal->source_device;
610 dst_ref = av_hwdevice_ctx_alloc(type);
612 ret = AVERROR(ENOMEM);
615 dst_ctx = (AVHWDeviceContext*)dst_ref->data;
619 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
620 if (dst_ctx->internal->hw_type->device_derive) {
621 ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
625 dst_ctx->internal->source_device = av_buffer_ref(src_ref);
626 if (!dst_ctx->internal->source_device) {
627 ret = AVERROR(ENOMEM);
632 if (ret != AVERROR(ENOSYS))
635 tmp_ref = tmp_ctx->internal->source_device;
638 ret = AVERROR(ENOSYS);
642 *dst_ref_ptr = dst_ref;
646 av_buffer_unref(&dst_ref);
651 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
653 HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
654 AVHWFramesContext *ctx = opaque;
657 hwmap->unmap(ctx, hwmap);
659 av_frame_free(&hwmap->source);
661 av_buffer_unref(&hwmap->hw_frames_ctx);
666 int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
667 AVFrame *dst, const AVFrame *src,
668 void (*unmap)(AVHWFramesContext *ctx,
669 HWMapDescriptor *hwmap),
672 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
673 HWMapDescriptor *hwmap;
676 hwmap = av_mallocz(sizeof(*hwmap));
678 ret = AVERROR(ENOMEM);
682 hwmap->source = av_frame_alloc();
683 if (!hwmap->source) {
684 ret = AVERROR(ENOMEM);
687 ret = av_frame_ref(hwmap->source, src);
691 hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
692 if (!hwmap->hw_frames_ctx) {
693 ret = AVERROR(ENOMEM);
697 hwmap->unmap = unmap;
700 dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
701 &ff_hwframe_unmap, ctx, 0);
703 ret = AVERROR(ENOMEM);
711 av_buffer_unref(&hwmap->hw_frames_ctx);
712 av_frame_free(&hwmap->source);
718 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
720 AVHWFramesContext *src_frames, *dst_frames;
721 HWMapDescriptor *hwmap;
724 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
725 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
726 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
728 if ((src_frames == dst_frames &&
729 src->format == dst_frames->sw_format &&
730 dst->format == dst_frames->format) ||
731 (src_frames->internal->source_frames &&
732 src_frames->internal->source_frames->data ==
733 (uint8_t*)dst_frames)) {
734 // This is an unmap operation. We don't need to directly
735 // do anything here other than fill in the original frame,
736 // because the real unmap will be invoked when the last
737 // reference to the mapped frame disappears.
739 av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
740 "found when attempting unmap.\n");
741 return AVERROR(EINVAL);
743 hwmap = (HWMapDescriptor*)src->buf[0]->data;
745 return av_frame_ref(dst, hwmap->source);
749 if (src->hw_frames_ctx) {
750 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
752 if (src_frames->format == src->format &&
753 src_frames->internal->hw_type->map_from) {
754 ret = src_frames->internal->hw_type->map_from(src_frames,
756 if (ret != AVERROR(ENOSYS))
761 if (dst->hw_frames_ctx) {
762 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
764 if (dst_frames->format == dst->format &&
765 dst_frames->internal->hw_type->map_to) {
766 ret = dst_frames->internal->hw_type->map_to(dst_frames,
768 if (ret != AVERROR(ENOSYS))
773 return AVERROR(ENOSYS);
776 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
777 enum AVPixelFormat format,
778 AVBufferRef *derived_device_ctx,
779 AVBufferRef *source_frame_ctx,
782 AVBufferRef *dst_ref = NULL;
783 AVHWFramesContext *dst = NULL;
784 AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
787 if (src->internal->source_frames) {
788 AVHWFramesContext *src_src =
789 (AVHWFramesContext*)src->internal->source_frames->data;
790 AVHWDeviceContext *dst_dev =
791 (AVHWDeviceContext*)derived_device_ctx->data;
793 if (src_src->device_ctx == dst_dev) {
794 // This is actually an unmapping, so we just return a
795 // reference to the source frame context.
797 av_buffer_ref(src->internal->source_frames);
798 if (!*derived_frame_ctx) {
799 ret = AVERROR(ENOMEM);
806 dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
808 ret = AVERROR(ENOMEM);
812 dst = (AVHWFramesContext*)dst_ref->data;
814 dst->format = format;
815 dst->sw_format = src->sw_format;
816 dst->width = src->width;
817 dst->height = src->height;
819 dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
820 if (!dst->internal->source_frames) {
821 ret = AVERROR(ENOMEM);
825 dst->internal->source_allocation_map_flags =
826 flags & (AV_HWFRAME_MAP_READ |
827 AV_HWFRAME_MAP_WRITE |
828 AV_HWFRAME_MAP_OVERWRITE |
829 AV_HWFRAME_MAP_DIRECT);
831 ret = AVERROR(ENOSYS);
832 if (src->internal->hw_type->frames_derive_from)
833 ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
834 if (ret == AVERROR(ENOSYS) &&
835 dst->internal->hw_type->frames_derive_to)
836 ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
837 if (ret == AVERROR(ENOSYS))
842 *derived_frame_ctx = dst_ref;
847 av_buffer_unref(&dst->internal->source_frames);
848 av_buffer_unref(&dst_ref);