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_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 char *const hw_type_names[] = {
54 [AV_HWDEVICE_TYPE_CUDA] = "cuda",
55 [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
56 [AV_HWDEVICE_TYPE_QSV] = "qsv",
57 [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
58 [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
59 [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
62 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
65 for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
66 if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
69 return AV_HWDEVICE_TYPE_NONE;
72 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
74 if (type >= 0 && type < FF_ARRAY_ELEMS(hw_type_names))
75 return hw_type_names[type];
80 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
82 enum AVHWDeviceType next;
84 for (i = 0; hw_table[i]; i++) {
85 if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
87 if (!set || hw_table[i]->type < next) {
88 next = hw_table[i]->type;
92 return set ? next : AV_HWDEVICE_TYPE_NONE;
95 static const AVClass hwdevice_ctx_class = {
96 .class_name = "AVHWDeviceContext",
97 .item_name = av_default_item_name,
98 .version = LIBAVUTIL_VERSION_INT,
101 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
103 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
105 /* uninit might still want access the hw context and the user
106 * free() callback might destroy it, so uninit has to be called first */
107 if (ctx->internal->hw_type->device_uninit)
108 ctx->internal->hw_type->device_uninit(ctx);
113 av_buffer_unref(&ctx->internal->source_device);
115 av_freep(&ctx->hwctx);
116 av_freep(&ctx->internal->priv);
117 av_freep(&ctx->internal);
121 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
123 AVHWDeviceContext *ctx;
125 const HWContextType *hw_type = NULL;
128 for (i = 0; hw_table[i]; i++) {
129 if (hw_table[i]->type == type) {
130 hw_type = hw_table[i];
137 ctx = av_mallocz(sizeof(*ctx));
141 ctx->internal = av_mallocz(sizeof(*ctx->internal));
145 if (hw_type->device_priv_size) {
146 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
147 if (!ctx->internal->priv)
151 if (hw_type->device_hwctx_size) {
152 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
157 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
158 hwdevice_ctx_free, NULL,
159 AV_BUFFER_FLAG_READONLY);
164 ctx->av_class = &hwdevice_ctx_class;
166 ctx->internal->hw_type = hw_type;
172 av_freep(&ctx->internal->priv);
173 av_freep(&ctx->internal);
174 av_freep(&ctx->hwctx);
179 int av_hwdevice_ctx_init(AVBufferRef *ref)
181 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
184 if (ctx->internal->hw_type->device_init) {
185 ret = ctx->internal->hw_type->device_init(ctx);
192 if (ctx->internal->hw_type->device_uninit)
193 ctx->internal->hw_type->device_uninit(ctx);
197 static const AVClass hwframe_ctx_class = {
198 .class_name = "AVHWFramesContext",
199 .item_name = av_default_item_name,
200 .version = LIBAVUTIL_VERSION_INT,
203 static void hwframe_ctx_free(void *opaque, uint8_t *data)
205 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
207 if (ctx->internal->source_frames) {
208 av_buffer_unref(&ctx->internal->source_frames);
211 if (ctx->internal->pool_internal)
212 av_buffer_pool_uninit(&ctx->internal->pool_internal);
214 if (ctx->internal->hw_type->frames_uninit)
215 ctx->internal->hw_type->frames_uninit(ctx);
221 av_buffer_unref(&ctx->device_ref);
223 av_freep(&ctx->hwctx);
224 av_freep(&ctx->internal->priv);
225 av_freep(&ctx->internal);
229 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
231 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
232 const HWContextType *hw_type = device_ctx->internal->hw_type;
233 AVHWFramesContext *ctx;
234 AVBufferRef *buf, *device_ref = NULL;
236 ctx = av_mallocz(sizeof(*ctx));
240 ctx->internal = av_mallocz(sizeof(*ctx->internal));
244 if (hw_type->frames_priv_size) {
245 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
246 if (!ctx->internal->priv)
250 if (hw_type->frames_hwctx_size) {
251 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
256 device_ref = av_buffer_ref(device_ref_in);
260 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
261 hwframe_ctx_free, NULL,
262 AV_BUFFER_FLAG_READONLY);
266 ctx->av_class = &hwframe_ctx_class;
267 ctx->device_ref = device_ref;
268 ctx->device_ctx = device_ctx;
269 ctx->format = AV_PIX_FMT_NONE;
270 ctx->sw_format = AV_PIX_FMT_NONE;
272 ctx->internal->hw_type = hw_type;
278 av_buffer_unref(&device_ref);
280 av_freep(&ctx->internal->priv);
281 av_freep(&ctx->internal);
282 av_freep(&ctx->hwctx);
287 static int hwframe_pool_prealloc(AVBufferRef *ref)
289 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
293 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
295 return AVERROR(ENOMEM);
297 for (i = 0; i < ctx->initial_pool_size; i++) {
298 frames[i] = av_frame_alloc();
302 ret = av_hwframe_get_buffer(ref, frames[i], 0);
308 for (i = 0; i < ctx->initial_pool_size; i++)
309 av_frame_free(&frames[i]);
315 int av_hwframe_ctx_init(AVBufferRef *ref)
317 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
318 const enum AVPixelFormat *pix_fmt;
321 if (ctx->internal->source_frames) {
322 /* A derived frame context is already initialised. */
326 /* validate the pixel format */
327 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
328 if (*pix_fmt == ctx->format)
331 if (*pix_fmt == AV_PIX_FMT_NONE) {
332 av_log(ctx, AV_LOG_ERROR,
333 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
334 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
335 return AVERROR(ENOSYS);
338 /* validate the dimensions */
339 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
343 /* format-specific init */
344 if (ctx->internal->hw_type->frames_init) {
345 ret = ctx->internal->hw_type->frames_init(ctx);
350 if (ctx->internal->pool_internal && !ctx->pool)
351 ctx->pool = ctx->internal->pool_internal;
353 /* preallocate the frames in the pool, if requested */
354 if (ctx->initial_pool_size > 0) {
355 ret = hwframe_pool_prealloc(ref);
362 if (ctx->internal->hw_type->frames_uninit)
363 ctx->internal->hw_type->frames_uninit(ctx);
367 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
368 enum AVHWFrameTransferDirection dir,
369 enum AVPixelFormat **formats, int flags)
371 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
373 if (!ctx->internal->hw_type->transfer_get_formats)
374 return AVERROR(ENOSYS);
376 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
379 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
381 AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
385 frame_tmp = av_frame_alloc();
387 return AVERROR(ENOMEM);
389 /* if the format is set, use that
390 * otherwise pick the first supported one */
391 if (dst->format >= 0) {
392 frame_tmp->format = dst->format;
394 enum AVPixelFormat *formats;
396 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
397 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
401 frame_tmp->format = formats[0];
404 frame_tmp->width = ctx->width;
405 frame_tmp->height = ctx->height;
407 ret = av_frame_get_buffer(frame_tmp, 32);
411 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
415 frame_tmp->width = src->width;
416 frame_tmp->height = src->height;
418 av_frame_move_ref(dst, frame_tmp);
421 av_frame_free(&frame_tmp);
425 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
427 AVHWFramesContext *ctx;
431 return transfer_data_alloc(dst, src, flags);
433 if (src->hw_frames_ctx) {
434 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
436 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
439 } else if (dst->hw_frames_ctx) {
440 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
442 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
446 return AVERROR(ENOSYS);
451 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
453 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
456 if (ctx->internal->source_frames) {
457 // This is a derived frame context, so we allocate in the source
458 // and map the frame immediately.
461 src_frame = av_frame_alloc();
463 return AVERROR(ENOMEM);
465 ret = av_hwframe_get_buffer(ctx->internal->source_frames,
470 ret = av_hwframe_map(frame, src_frame, 0);
472 av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
473 "frame context: %d.\n", ret);
474 av_frame_free(&src_frame);
478 // Free the source frame immediately - the mapped frame still
479 // contains a reference to it.
480 av_frame_free(&src_frame);
485 if (!ctx->internal->hw_type->frames_get_buffer)
486 return AVERROR(ENOSYS);
489 return AVERROR(EINVAL);
491 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
492 if (!frame->hw_frames_ctx)
493 return AVERROR(ENOMEM);
495 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
497 av_buffer_unref(&frame->hw_frames_ctx);
504 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
506 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
507 const HWContextType *hw_type = ctx->internal->hw_type;
509 if (hw_type->device_hwconfig_size == 0)
512 return av_mallocz(hw_type->device_hwconfig_size);
515 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
516 const void *hwconfig)
518 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
519 const HWContextType *hw_type = ctx->internal->hw_type;
520 AVHWFramesConstraints *constraints;
522 if (!hw_type->frames_get_constraints)
525 constraints = av_mallocz(sizeof(*constraints));
529 constraints->min_width = constraints->min_height = 0;
530 constraints->max_width = constraints->max_height = INT_MAX;
532 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
535 av_hwframe_constraints_free(&constraints);
540 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
543 av_freep(&(*constraints)->valid_hw_formats);
544 av_freep(&(*constraints)->valid_sw_formats);
546 av_freep(constraints);
549 int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
550 const char *device, AVDictionary *opts, int flags)
552 AVBufferRef *device_ref = NULL;
553 AVHWDeviceContext *device_ctx;
556 device_ref = av_hwdevice_ctx_alloc(type);
558 ret = AVERROR(ENOMEM);
561 device_ctx = (AVHWDeviceContext*)device_ref->data;
563 if (!device_ctx->internal->hw_type->device_create) {
564 ret = AVERROR(ENOSYS);
568 ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
573 ret = av_hwdevice_ctx_init(device_ref);
577 *pdevice_ref = device_ref;
580 av_buffer_unref(&device_ref);
585 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
586 enum AVHWDeviceType type,
587 AVBufferRef *src_ref, int flags)
589 AVBufferRef *dst_ref = NULL, *tmp_ref;
590 AVHWDeviceContext *dst_ctx, *tmp_ctx;
595 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
596 if (tmp_ctx->type == type) {
597 dst_ref = av_buffer_ref(tmp_ref);
599 ret = AVERROR(ENOMEM);
604 tmp_ref = tmp_ctx->internal->source_device;
607 dst_ref = av_hwdevice_ctx_alloc(type);
609 ret = AVERROR(ENOMEM);
612 dst_ctx = (AVHWDeviceContext*)dst_ref->data;
616 tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
617 if (dst_ctx->internal->hw_type->device_derive) {
618 ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
622 dst_ctx->internal->source_device = av_buffer_ref(src_ref);
623 if (!dst_ctx->internal->source_device) {
624 ret = AVERROR(ENOMEM);
629 if (ret != AVERROR(ENOSYS))
632 tmp_ref = tmp_ctx->internal->source_device;
635 ret = AVERROR(ENOSYS);
639 *dst_ref_ptr = dst_ref;
643 av_buffer_unref(&dst_ref);
648 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
650 HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
651 AVHWFramesContext *ctx = opaque;
654 hwmap->unmap(ctx, hwmap);
656 av_frame_free(&hwmap->source);
658 av_buffer_unref(&hwmap->hw_frames_ctx);
663 int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
664 AVFrame *dst, const AVFrame *src,
665 void (*unmap)(AVHWFramesContext *ctx,
666 HWMapDescriptor *hwmap),
669 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
670 HWMapDescriptor *hwmap;
673 hwmap = av_mallocz(sizeof(*hwmap));
675 ret = AVERROR(ENOMEM);
679 hwmap->source = av_frame_alloc();
680 if (!hwmap->source) {
681 ret = AVERROR(ENOMEM);
684 ret = av_frame_ref(hwmap->source, src);
688 hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
689 if (!hwmap->hw_frames_ctx) {
690 ret = AVERROR(ENOMEM);
694 hwmap->unmap = unmap;
697 dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
698 &ff_hwframe_unmap, ctx, 0);
700 ret = AVERROR(ENOMEM);
708 av_buffer_unref(&hwmap->hw_frames_ctx);
709 av_frame_free(&hwmap->source);
715 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
717 AVHWFramesContext *src_frames, *dst_frames;
718 HWMapDescriptor *hwmap;
721 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
722 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
723 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
725 if ((src_frames == dst_frames &&
726 src->format == dst_frames->sw_format &&
727 dst->format == dst_frames->format) ||
728 (src_frames->internal->source_frames &&
729 src_frames->internal->source_frames->data ==
730 (uint8_t*)dst_frames)) {
731 // This is an unmap operation. We don't need to directly
732 // do anything here other than fill in the original frame,
733 // because the real unmap will be invoked when the last
734 // reference to the mapped frame disappears.
736 av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
737 "found when attempting unmap.\n");
738 return AVERROR(EINVAL);
740 hwmap = (HWMapDescriptor*)src->buf[0]->data;
742 return av_frame_ref(dst, hwmap->source);
746 if (src->hw_frames_ctx) {
747 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
749 if (src_frames->format == src->format &&
750 src_frames->internal->hw_type->map_from) {
751 ret = src_frames->internal->hw_type->map_from(src_frames,
753 if (ret != AVERROR(ENOSYS))
758 if (dst->hw_frames_ctx) {
759 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
761 if (dst_frames->format == dst->format &&
762 dst_frames->internal->hw_type->map_to) {
763 ret = dst_frames->internal->hw_type->map_to(dst_frames,
765 if (ret != AVERROR(ENOSYS))
770 return AVERROR(ENOSYS);
773 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
774 enum AVPixelFormat format,
775 AVBufferRef *derived_device_ctx,
776 AVBufferRef *source_frame_ctx,
779 AVBufferRef *dst_ref = NULL;
780 AVHWFramesContext *dst = NULL;
781 AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
784 if (src->internal->source_frames) {
785 AVHWFramesContext *src_src =
786 (AVHWFramesContext*)src->internal->source_frames->data;
787 AVHWDeviceContext *dst_dev =
788 (AVHWDeviceContext*)derived_device_ctx->data;
790 if (src_src->device_ctx == dst_dev) {
791 // This is actually an unmapping, so we just return a
792 // reference to the source frame context.
794 av_buffer_ref(src->internal->source_frames);
795 if (!*derived_frame_ctx) {
796 ret = AVERROR(ENOMEM);
803 dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
805 ret = AVERROR(ENOMEM);
809 dst = (AVHWFramesContext*)dst_ref->data;
811 dst->format = format;
812 dst->sw_format = src->sw_format;
813 dst->width = src->width;
814 dst->height = src->height;
816 dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
817 if (!dst->internal->source_frames) {
818 ret = AVERROR(ENOMEM);
822 ret = av_hwframe_ctx_init(dst_ref);
826 *derived_frame_ctx = dst_ref;
831 av_buffer_unref(&dst->internal->source_frames);
832 av_buffer_unref(&dst_ref);