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,
62 static const char *const hw_type_names[] = {
63 [AV_HWDEVICE_TYPE_CUDA] = "cuda",
64 [AV_HWDEVICE_TYPE_DRM] = "drm",
65 [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
66 [AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
67 [AV_HWDEVICE_TYPE_OPENCL] = "opencl",
68 [AV_HWDEVICE_TYPE_QSV] = "qsv",
69 [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
70 [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
71 [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
74 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
77 for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
78 if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
81 return AV_HWDEVICE_TYPE_NONE;
84 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
86 if (type > AV_HWDEVICE_TYPE_NONE &&
87 type < FF_ARRAY_ELEMS(hw_type_names))
88 return hw_type_names[type];
93 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
95 enum AVHWDeviceType next;
97 for (i = 0; hw_table[i]; i++) {
98 if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
100 if (!set || hw_table[i]->type < next) {
101 next = hw_table[i]->type;
105 return set ? next : AV_HWDEVICE_TYPE_NONE;
108 static const AVClass hwdevice_ctx_class = {
109 .class_name = "AVHWDeviceContext",
110 .item_name = av_default_item_name,
111 .version = LIBAVUTIL_VERSION_INT,
114 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
116 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
118 /* uninit might still want access the hw context and the user
119 * free() callback might destroy it, so uninit has to be called first */
120 if (ctx->internal->hw_type->device_uninit)
121 ctx->internal->hw_type->device_uninit(ctx);
126 av_buffer_unref(&ctx->internal->source_device);
128 av_freep(&ctx->hwctx);
129 av_freep(&ctx->internal->priv);
130 av_freep(&ctx->internal);
134 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
136 AVHWDeviceContext *ctx;
138 const HWContextType *hw_type = NULL;
141 for (i = 0; hw_table[i]; i++) {
142 if (hw_table[i]->type == type) {
143 hw_type = hw_table[i];
150 ctx = av_mallocz(sizeof(*ctx));
154 ctx->internal = av_mallocz(sizeof(*ctx->internal));
158 if (hw_type->device_priv_size) {
159 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
160 if (!ctx->internal->priv)
164 if (hw_type->device_hwctx_size) {
165 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
170 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
171 hwdevice_ctx_free, NULL,
172 AV_BUFFER_FLAG_READONLY);
177 ctx->av_class = &hwdevice_ctx_class;
179 ctx->internal->hw_type = hw_type;
185 av_freep(&ctx->internal->priv);
186 av_freep(&ctx->internal);
187 av_freep(&ctx->hwctx);
192 int av_hwdevice_ctx_init(AVBufferRef *ref)
194 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
197 if (ctx->internal->hw_type->device_init) {
198 ret = ctx->internal->hw_type->device_init(ctx);
205 if (ctx->internal->hw_type->device_uninit)
206 ctx->internal->hw_type->device_uninit(ctx);
210 static const AVClass hwframe_ctx_class = {
211 .class_name = "AVHWFramesContext",
212 .item_name = av_default_item_name,
213 .version = LIBAVUTIL_VERSION_INT,
216 static void hwframe_ctx_free(void *opaque, uint8_t *data)
218 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
220 if (ctx->internal->pool_internal)
221 av_buffer_pool_uninit(&ctx->internal->pool_internal);
223 if (ctx->internal->hw_type->frames_uninit)
224 ctx->internal->hw_type->frames_uninit(ctx);
229 av_buffer_unref(&ctx->internal->source_frames);
231 av_buffer_unref(&ctx->device_ref);
233 av_freep(&ctx->hwctx);
234 av_freep(&ctx->internal->priv);
235 av_freep(&ctx->internal);
239 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
241 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
242 const HWContextType *hw_type = device_ctx->internal->hw_type;
243 AVHWFramesContext *ctx;
244 AVBufferRef *buf, *device_ref = NULL;
246 ctx = av_mallocz(sizeof(*ctx));
250 ctx->internal = av_mallocz(sizeof(*ctx->internal));
254 if (hw_type->frames_priv_size) {
255 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
256 if (!ctx->internal->priv)
260 if (hw_type->frames_hwctx_size) {
261 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
266 device_ref = av_buffer_ref(device_ref_in);
270 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
271 hwframe_ctx_free, NULL,
272 AV_BUFFER_FLAG_READONLY);
276 ctx->av_class = &hwframe_ctx_class;
277 ctx->device_ref = device_ref;
278 ctx->device_ctx = device_ctx;
279 ctx->format = AV_PIX_FMT_NONE;
280 ctx->sw_format = AV_PIX_FMT_NONE;
282 ctx->internal->hw_type = hw_type;
288 av_buffer_unref(&device_ref);
290 av_freep(&ctx->internal->priv);
291 av_freep(&ctx->internal);
292 av_freep(&ctx->hwctx);
297 static int hwframe_pool_prealloc(AVBufferRef *ref)
299 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
303 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
305 return AVERROR(ENOMEM);
307 for (i = 0; i < ctx->initial_pool_size; i++) {
308 frames[i] = av_frame_alloc();
312 ret = av_hwframe_get_buffer(ref, frames[i], 0);
318 for (i = 0; i < ctx->initial_pool_size; i++)
319 av_frame_free(&frames[i]);
325 int av_hwframe_ctx_init(AVBufferRef *ref)
327 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
328 const enum AVPixelFormat *pix_fmt;
331 if (ctx->internal->source_frames) {
332 /* A derived frame context is already initialised. */
336 /* validate the pixel format */
337 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
338 if (*pix_fmt == ctx->format)
341 if (*pix_fmt == AV_PIX_FMT_NONE) {
342 av_log(ctx, AV_LOG_ERROR,
343 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
344 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
345 return AVERROR(ENOSYS);
348 /* validate the dimensions */
349 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
353 /* format-specific init */
354 if (ctx->internal->hw_type->frames_init) {
355 ret = ctx->internal->hw_type->frames_init(ctx);
360 if (ctx->internal->pool_internal && !ctx->pool)
361 ctx->pool = ctx->internal->pool_internal;
363 /* preallocate the frames in the pool, if requested */
364 if (ctx->initial_pool_size > 0) {
365 ret = hwframe_pool_prealloc(ref);
372 if (ctx->internal->hw_type->frames_uninit)
373 ctx->internal->hw_type->frames_uninit(ctx);
377 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
378 enum AVHWFrameTransferDirection dir,
379 enum AVPixelFormat **formats, int flags)
381 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
383 if (!ctx->internal->hw_type->transfer_get_formats)
384 return AVERROR(ENOSYS);
386 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
389 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
391 AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
395 frame_tmp = av_frame_alloc();
397 return AVERROR(ENOMEM);
399 /* if the format is set, use that
400 * otherwise pick the first supported one */
401 if (dst->format >= 0) {
402 frame_tmp->format = dst->format;
404 enum AVPixelFormat *formats;
406 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
407 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
411 frame_tmp->format = formats[0];
414 frame_tmp->width = ctx->width;
415 frame_tmp->height = ctx->height;
417 ret = av_frame_get_buffer(frame_tmp, 32);
421 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
425 frame_tmp->width = src->width;
426 frame_tmp->height = src->height;
428 av_frame_move_ref(dst, frame_tmp);
431 av_frame_free(&frame_tmp);
435 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
437 AVHWFramesContext *ctx;
441 return transfer_data_alloc(dst, src, flags);
443 if (src->hw_frames_ctx) {
444 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
446 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
449 } else if (dst->hw_frames_ctx) {
450 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
452 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
456 return AVERROR(ENOSYS);
461 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
463 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
466 if (ctx->internal->source_frames) {
467 // This is a derived frame context, so we allocate in the source
468 // and map the frame immediately.
471 frame->format = ctx->format;
472 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
473 if (!frame->hw_frames_ctx)
474 return AVERROR(ENOMEM);
476 src_frame = av_frame_alloc();
478 return AVERROR(ENOMEM);
480 ret = av_hwframe_get_buffer(ctx->internal->source_frames,
485 ret = av_hwframe_map(frame, src_frame,
486 ctx->internal->source_allocation_map_flags);
488 av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
489 "frame context: %d.\n", ret);
490 av_frame_free(&src_frame);
494 // Free the source frame immediately - the mapped frame still
495 // contains a reference to it.
496 av_frame_free(&src_frame);
501 if (!ctx->internal->hw_type->frames_get_buffer)
502 return AVERROR(ENOSYS);
505 return AVERROR(EINVAL);
507 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
508 if (!frame->hw_frames_ctx)
509 return AVERROR(ENOMEM);
511 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
513 av_buffer_unref(&frame->hw_frames_ctx);
520 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
522 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
523 const HWContextType *hw_type = ctx->internal->hw_type;
525 if (hw_type->device_hwconfig_size == 0)
528 return av_mallocz(hw_type->device_hwconfig_size);
531 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
532 const void *hwconfig)
534 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
535 const HWContextType *hw_type = ctx->internal->hw_type;
536 AVHWFramesConstraints *constraints;
538 if (!hw_type->frames_get_constraints)
541 constraints = av_mallocz(sizeof(*constraints));
545 constraints->min_width = constraints->min_height = 0;
546 constraints->max_width = constraints->max_height = INT_MAX;
548 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
551 av_hwframe_constraints_free(&constraints);
556 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
559 av_freep(&(*constraints)->valid_hw_formats);
560 av_freep(&(*constraints)->valid_sw_formats);
562 av_freep(constraints);
565 int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
566 const char *device, AVDictionary *opts, int flags)
568 AVBufferRef *device_ref = NULL;
569 AVHWDeviceContext *device_ctx;
572 device_ref = av_hwdevice_ctx_alloc(type);
574 ret = AVERROR(ENOMEM);
577 device_ctx = (AVHWDeviceContext*)device_ref->data;
579 if (!device_ctx->internal->hw_type->device_create) {
580 ret = AVERROR(ENOSYS);
584 ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
589 ret = av_hwdevice_ctx_init(device_ref);
593 *pdevice_ref = device_ref;
596 av_buffer_unref(&device_ref);
601 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
602 enum AVHWDeviceType type,
603 AVBufferRef *src_ref, int flags)
605 AVBufferRef *dst_ref = NULL, *tmp_ref;
606 AVHWDeviceContext *dst_ctx, *tmp_ctx;
611 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
612 if (tmp_ctx->type == type) {
613 dst_ref = av_buffer_ref(tmp_ref);
615 ret = AVERROR(ENOMEM);
620 tmp_ref = tmp_ctx->internal->source_device;
623 dst_ref = av_hwdevice_ctx_alloc(type);
625 ret = AVERROR(ENOMEM);
628 dst_ctx = (AVHWDeviceContext*)dst_ref->data;
632 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
633 if (dst_ctx->internal->hw_type->device_derive) {
634 ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
638 dst_ctx->internal->source_device = av_buffer_ref(src_ref);
639 if (!dst_ctx->internal->source_device) {
640 ret = AVERROR(ENOMEM);
645 if (ret != AVERROR(ENOSYS))
648 tmp_ref = tmp_ctx->internal->source_device;
651 ret = AVERROR(ENOSYS);
655 ret = av_hwdevice_ctx_init(dst_ref);
659 *dst_ref_ptr = dst_ref;
663 av_buffer_unref(&dst_ref);
668 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
670 HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
671 AVHWFramesContext *ctx = opaque;
674 hwmap->unmap(ctx, hwmap);
676 av_frame_free(&hwmap->source);
678 av_buffer_unref(&hwmap->hw_frames_ctx);
683 int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
684 AVFrame *dst, const AVFrame *src,
685 void (*unmap)(AVHWFramesContext *ctx,
686 HWMapDescriptor *hwmap),
689 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
690 HWMapDescriptor *hwmap;
693 hwmap = av_mallocz(sizeof(*hwmap));
695 ret = AVERROR(ENOMEM);
699 hwmap->source = av_frame_alloc();
700 if (!hwmap->source) {
701 ret = AVERROR(ENOMEM);
704 ret = av_frame_ref(hwmap->source, src);
708 hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
709 if (!hwmap->hw_frames_ctx) {
710 ret = AVERROR(ENOMEM);
714 hwmap->unmap = unmap;
717 dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
718 &ff_hwframe_unmap, ctx, 0);
720 ret = AVERROR(ENOMEM);
728 av_buffer_unref(&hwmap->hw_frames_ctx);
729 av_frame_free(&hwmap->source);
735 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
737 AVHWFramesContext *src_frames, *dst_frames;
738 HWMapDescriptor *hwmap;
741 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
742 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
743 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
745 if ((src_frames == dst_frames &&
746 src->format == dst_frames->sw_format &&
747 dst->format == dst_frames->format) ||
748 (src_frames->internal->source_frames &&
749 src_frames->internal->source_frames->data ==
750 (uint8_t*)dst_frames)) {
751 // This is an unmap operation. We don't need to directly
752 // do anything here other than fill in the original frame,
753 // because the real unmap will be invoked when the last
754 // reference to the mapped frame disappears.
756 av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
757 "found when attempting unmap.\n");
758 return AVERROR(EINVAL);
760 hwmap = (HWMapDescriptor*)src->buf[0]->data;
762 return av_frame_ref(dst, hwmap->source);
766 if (src->hw_frames_ctx) {
767 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
769 if (src_frames->format == src->format &&
770 src_frames->internal->hw_type->map_from) {
771 ret = src_frames->internal->hw_type->map_from(src_frames,
773 if (ret != AVERROR(ENOSYS))
778 if (dst->hw_frames_ctx) {
779 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
781 if (dst_frames->format == dst->format &&
782 dst_frames->internal->hw_type->map_to) {
783 ret = dst_frames->internal->hw_type->map_to(dst_frames,
785 if (ret != AVERROR(ENOSYS))
790 return AVERROR(ENOSYS);
793 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
794 enum AVPixelFormat format,
795 AVBufferRef *derived_device_ctx,
796 AVBufferRef *source_frame_ctx,
799 AVBufferRef *dst_ref = NULL;
800 AVHWFramesContext *dst = NULL;
801 AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
804 if (src->internal->source_frames) {
805 AVHWFramesContext *src_src =
806 (AVHWFramesContext*)src->internal->source_frames->data;
807 AVHWDeviceContext *dst_dev =
808 (AVHWDeviceContext*)derived_device_ctx->data;
810 if (src_src->device_ctx == dst_dev) {
811 // This is actually an unmapping, so we just return a
812 // reference to the source frame context.
814 av_buffer_ref(src->internal->source_frames);
815 if (!*derived_frame_ctx) {
816 ret = AVERROR(ENOMEM);
823 dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
825 ret = AVERROR(ENOMEM);
829 dst = (AVHWFramesContext*)dst_ref->data;
831 dst->format = format;
832 dst->sw_format = src->sw_format;
833 dst->width = src->width;
834 dst->height = src->height;
836 dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
837 if (!dst->internal->source_frames) {
838 ret = AVERROR(ENOMEM);
842 dst->internal->source_allocation_map_flags =
843 flags & (AV_HWFRAME_MAP_READ |
844 AV_HWFRAME_MAP_WRITE |
845 AV_HWFRAME_MAP_OVERWRITE |
846 AV_HWFRAME_MAP_DIRECT);
848 ret = AVERROR(ENOSYS);
849 if (src->internal->hw_type->frames_derive_from)
850 ret = src->internal->hw_type->frames_derive_from(dst, src, flags);
851 if (ret == AVERROR(ENOSYS) &&
852 dst->internal->hw_type->frames_derive_to)
853 ret = dst->internal->hw_type->frames_derive_to(dst, src, flags);
854 if (ret == AVERROR(ENOSYS))
859 *derived_frame_ctx = dst_ref;
864 av_buffer_unref(&dst->internal->source_frames);
865 av_buffer_unref(&dst_ref);