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 *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 static const AVClass hwdevice_ctx_class = {
51 .class_name = "AVHWDeviceContext",
52 .item_name = av_default_item_name,
53 .version = LIBAVUTIL_VERSION_INT,
56 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
58 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
60 /* uninit might still want access the hw context and the user
61 * free() callback might destroy it, so uninit has to be called first */
62 if (ctx->internal->hw_type->device_uninit)
63 ctx->internal->hw_type->device_uninit(ctx);
68 av_freep(&ctx->hwctx);
69 av_freep(&ctx->internal->priv);
70 av_freep(&ctx->internal);
74 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
76 AVHWDeviceContext *ctx;
78 const HWContextType *hw_type = NULL;
81 for (i = 0; hw_table[i]; i++) {
82 if (hw_table[i]->type == type) {
83 hw_type = hw_table[i];
90 ctx = av_mallocz(sizeof(*ctx));
94 ctx->internal = av_mallocz(sizeof(*ctx->internal));
98 if (hw_type->device_priv_size) {
99 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
100 if (!ctx->internal->priv)
104 if (hw_type->device_hwctx_size) {
105 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
110 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
111 hwdevice_ctx_free, NULL,
112 AV_BUFFER_FLAG_READONLY);
117 ctx->av_class = &hwdevice_ctx_class;
119 ctx->internal->hw_type = hw_type;
125 av_freep(&ctx->internal->priv);
126 av_freep(&ctx->internal);
127 av_freep(&ctx->hwctx);
132 int av_hwdevice_ctx_init(AVBufferRef *ref)
134 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
137 if (ctx->internal->hw_type->device_init) {
138 ret = ctx->internal->hw_type->device_init(ctx);
145 if (ctx->internal->hw_type->device_uninit)
146 ctx->internal->hw_type->device_uninit(ctx);
150 static const AVClass hwframe_ctx_class = {
151 .class_name = "AVHWFramesContext",
152 .item_name = av_default_item_name,
153 .version = LIBAVUTIL_VERSION_INT,
156 static void hwframe_ctx_free(void *opaque, uint8_t *data)
158 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
160 if (ctx->internal->source_frames) {
161 av_buffer_unref(&ctx->internal->source_frames);
164 if (ctx->internal->pool_internal)
165 av_buffer_pool_uninit(&ctx->internal->pool_internal);
167 if (ctx->internal->hw_type->frames_uninit)
168 ctx->internal->hw_type->frames_uninit(ctx);
174 av_buffer_unref(&ctx->device_ref);
176 av_freep(&ctx->hwctx);
177 av_freep(&ctx->internal->priv);
178 av_freep(&ctx->internal);
182 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
184 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
185 const HWContextType *hw_type = device_ctx->internal->hw_type;
186 AVHWFramesContext *ctx;
187 AVBufferRef *buf, *device_ref = NULL;
189 ctx = av_mallocz(sizeof(*ctx));
193 ctx->internal = av_mallocz(sizeof(*ctx->internal));
197 if (hw_type->frames_priv_size) {
198 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
199 if (!ctx->internal->priv)
203 if (hw_type->frames_hwctx_size) {
204 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
209 device_ref = av_buffer_ref(device_ref_in);
213 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
214 hwframe_ctx_free, NULL,
215 AV_BUFFER_FLAG_READONLY);
219 ctx->av_class = &hwframe_ctx_class;
220 ctx->device_ref = device_ref;
221 ctx->device_ctx = device_ctx;
222 ctx->format = AV_PIX_FMT_NONE;
223 ctx->sw_format = AV_PIX_FMT_NONE;
225 ctx->internal->hw_type = hw_type;
231 av_buffer_unref(&device_ref);
233 av_freep(&ctx->internal->priv);
234 av_freep(&ctx->internal);
235 av_freep(&ctx->hwctx);
240 static int hwframe_pool_prealloc(AVBufferRef *ref)
242 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
246 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
248 return AVERROR(ENOMEM);
250 for (i = 0; i < ctx->initial_pool_size; i++) {
251 frames[i] = av_frame_alloc();
255 ret = av_hwframe_get_buffer(ref, frames[i], 0);
261 for (i = 0; i < ctx->initial_pool_size; i++)
262 av_frame_free(&frames[i]);
268 int av_hwframe_ctx_init(AVBufferRef *ref)
270 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
271 const enum AVPixelFormat *pix_fmt;
274 if (ctx->internal->source_frames) {
275 /* A derived frame context is already initialised. */
279 /* validate the pixel format */
280 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
281 if (*pix_fmt == ctx->format)
284 if (*pix_fmt == AV_PIX_FMT_NONE) {
285 av_log(ctx, AV_LOG_ERROR,
286 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
287 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
288 return AVERROR(ENOSYS);
291 /* validate the dimensions */
292 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
296 /* format-specific init */
297 if (ctx->internal->hw_type->frames_init) {
298 ret = ctx->internal->hw_type->frames_init(ctx);
303 if (ctx->internal->pool_internal && !ctx->pool)
304 ctx->pool = ctx->internal->pool_internal;
306 /* preallocate the frames in the pool, if requested */
307 if (ctx->initial_pool_size > 0) {
308 ret = hwframe_pool_prealloc(ref);
315 if (ctx->internal->hw_type->frames_uninit)
316 ctx->internal->hw_type->frames_uninit(ctx);
320 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
321 enum AVHWFrameTransferDirection dir,
322 enum AVPixelFormat **formats, int flags)
324 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
326 if (!ctx->internal->hw_type->transfer_get_formats)
327 return AVERROR(ENOSYS);
329 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
332 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
334 AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
338 frame_tmp = av_frame_alloc();
340 return AVERROR(ENOMEM);
342 /* if the format is set, use that
343 * otherwise pick the first supported one */
344 if (dst->format >= 0) {
345 frame_tmp->format = dst->format;
347 enum AVPixelFormat *formats;
349 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
350 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
354 frame_tmp->format = formats[0];
357 frame_tmp->width = ctx->width;
358 frame_tmp->height = ctx->height;
360 ret = av_frame_get_buffer(frame_tmp, 32);
364 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
368 frame_tmp->width = src->width;
369 frame_tmp->height = src->height;
371 av_frame_move_ref(dst, frame_tmp);
374 av_frame_free(&frame_tmp);
378 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
380 AVHWFramesContext *ctx;
384 return transfer_data_alloc(dst, src, flags);
386 if (src->hw_frames_ctx) {
387 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
389 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
392 } else if (dst->hw_frames_ctx) {
393 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
395 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
399 return AVERROR(ENOSYS);
404 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
406 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
409 if (ctx->internal->source_frames) {
410 // This is a derived frame context, so we allocate in the source
411 // and map the frame immediately.
414 src_frame = av_frame_alloc();
416 return AVERROR(ENOMEM);
418 ret = av_hwframe_get_buffer(ctx->internal->source_frames,
423 ret = av_hwframe_map(frame, src_frame, 0);
425 av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
426 "frame context: %d.\n", ret);
427 av_frame_free(&src_frame);
431 // Free the source frame immediately - the mapped frame still
432 // contains a reference to it.
433 av_frame_free(&src_frame);
438 if (!ctx->internal->hw_type->frames_get_buffer)
439 return AVERROR(ENOSYS);
442 return AVERROR(EINVAL);
444 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
445 if (!frame->hw_frames_ctx)
446 return AVERROR(ENOMEM);
448 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
450 av_buffer_unref(&frame->hw_frames_ctx);
457 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
459 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
460 const HWContextType *hw_type = ctx->internal->hw_type;
462 if (hw_type->device_hwconfig_size == 0)
465 return av_mallocz(hw_type->device_hwconfig_size);
468 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
469 const void *hwconfig)
471 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
472 const HWContextType *hw_type = ctx->internal->hw_type;
473 AVHWFramesConstraints *constraints;
475 if (!hw_type->frames_get_constraints)
478 constraints = av_mallocz(sizeof(*constraints));
482 constraints->min_width = constraints->min_height = 0;
483 constraints->max_width = constraints->max_height = INT_MAX;
485 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
488 av_hwframe_constraints_free(&constraints);
493 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
496 av_freep(&(*constraints)->valid_hw_formats);
497 av_freep(&(*constraints)->valid_sw_formats);
499 av_freep(constraints);
502 int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
503 const char *device, AVDictionary *opts, int flags)
505 AVBufferRef *device_ref = NULL;
506 AVHWDeviceContext *device_ctx;
509 device_ref = av_hwdevice_ctx_alloc(type);
511 ret = AVERROR(ENOMEM);
514 device_ctx = (AVHWDeviceContext*)device_ref->data;
516 if (!device_ctx->internal->hw_type->device_create) {
517 ret = AVERROR(ENOSYS);
521 ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
526 ret = av_hwdevice_ctx_init(device_ref);
530 *pdevice_ref = device_ref;
533 av_buffer_unref(&device_ref);
538 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
540 HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
541 AVHWFramesContext *ctx = opaque;
544 hwmap->unmap(ctx, hwmap);
546 av_frame_free(&hwmap->source);
548 av_buffer_unref(&hwmap->hw_frames_ctx);
553 int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
554 AVFrame *dst, const AVFrame *src,
555 void (*unmap)(AVHWFramesContext *ctx,
556 HWMapDescriptor *hwmap),
559 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
560 HWMapDescriptor *hwmap;
563 hwmap = av_mallocz(sizeof(*hwmap));
565 ret = AVERROR(ENOMEM);
569 hwmap->source = av_frame_alloc();
570 if (!hwmap->source) {
571 ret = AVERROR(ENOMEM);
574 ret = av_frame_ref(hwmap->source, src);
578 hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
579 if (!hwmap->hw_frames_ctx) {
580 ret = AVERROR(ENOMEM);
584 hwmap->unmap = unmap;
587 dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
588 &ff_hwframe_unmap, ctx, 0);
590 ret = AVERROR(ENOMEM);
598 av_buffer_unref(&hwmap->hw_frames_ctx);
599 av_frame_free(&hwmap->source);
605 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
607 AVHWFramesContext *src_frames, *dst_frames;
608 HWMapDescriptor *hwmap;
611 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
612 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
613 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
615 if ((src_frames == dst_frames &&
616 src->format == dst_frames->sw_format &&
617 dst->format == dst_frames->format) ||
618 (src_frames->internal->source_frames &&
619 src_frames->internal->source_frames->data ==
620 (uint8_t*)dst_frames)) {
621 // This is an unmap operation. We don't need to directly
622 // do anything here other than fill in the original frame,
623 // because the real unmap will be invoked when the last
624 // reference to the mapped frame disappears.
626 av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
627 "found when attempting unmap.\n");
628 return AVERROR(EINVAL);
630 hwmap = (HWMapDescriptor*)src->buf[0]->data;
632 return av_frame_ref(dst, hwmap->source);
636 if (src->hw_frames_ctx) {
637 src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
639 if (src_frames->format == src->format &&
640 src_frames->internal->hw_type->map_from) {
641 ret = src_frames->internal->hw_type->map_from(src_frames,
643 if (ret != AVERROR(ENOSYS))
648 if (dst->hw_frames_ctx) {
649 dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
651 if (dst_frames->format == dst->format &&
652 dst_frames->internal->hw_type->map_to) {
653 ret = dst_frames->internal->hw_type->map_to(dst_frames,
655 if (ret != AVERROR(ENOSYS))
660 return AVERROR(ENOSYS);
663 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
664 enum AVPixelFormat format,
665 AVBufferRef *derived_device_ctx,
666 AVBufferRef *source_frame_ctx,
669 AVBufferRef *dst_ref = NULL;
670 AVHWFramesContext *dst = NULL;
671 AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
674 if (src->internal->source_frames) {
675 AVHWFramesContext *src_src =
676 (AVHWFramesContext*)src->internal->source_frames->data;
677 AVHWDeviceContext *dst_dev =
678 (AVHWDeviceContext*)derived_device_ctx->data;
680 if (src_src->device_ctx == dst_dev) {
681 // This is actually an unmapping, so we just return a
682 // reference to the source frame context.
684 av_buffer_ref(src->internal->source_frames);
685 if (!*derived_frame_ctx) {
686 ret = AVERROR(ENOMEM);
693 dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
695 ret = AVERROR(ENOMEM);
699 dst = (AVHWFramesContext*)dst_ref->data;
701 dst->format = format;
702 dst->sw_format = src->sw_format;
703 dst->width = src->width;
704 dst->height = src->height;
706 dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
707 if (!dst->internal->source_frames) {
708 ret = AVERROR(ENOMEM);
712 ret = av_hwframe_ctx_init(dst_ref);
716 *derived_frame_ctx = dst_ref;
721 av_buffer_unref(&dst->internal->source_frames);
722 av_buffer_unref(&dst_ref);