avfilter/xpsnr: avoid division by zero
[ffmpeg.git] / libavutil / hwcontext.c
blobf06d49c45c8aa6ed79aba61d787e480051785aa8
1 /*
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
19 #include "config.h"
21 #include "avassert.h"
22 #include "buffer.h"
23 #include "common.h"
24 #include "hwcontext.h"
25 #include "hwcontext_internal.h"
26 #include "imgutils.h"
27 #include "log.h"
28 #include "mem.h"
29 #include "pixdesc.h"
30 #include "pixfmt.h"
32 static const HWContextType * const hw_table[] = {
33 #if CONFIG_CUDA
34 &ff_hwcontext_type_cuda,
35 #endif
36 #if CONFIG_D3D11VA
37 &ff_hwcontext_type_d3d11va,
38 #endif
39 #if CONFIG_D3D12VA
40 &ff_hwcontext_type_d3d12va,
41 #endif
42 #if CONFIG_LIBDRM
43 &ff_hwcontext_type_drm,
44 #endif
45 #if CONFIG_DXVA2
46 &ff_hwcontext_type_dxva2,
47 #endif
48 #if CONFIG_OPENCL
49 &ff_hwcontext_type_opencl,
50 #endif
51 #if CONFIG_QSV
52 &ff_hwcontext_type_qsv,
53 #endif
54 #if CONFIG_VAAPI
55 &ff_hwcontext_type_vaapi,
56 #endif
57 #if CONFIG_VDPAU
58 &ff_hwcontext_type_vdpau,
59 #endif
60 #if CONFIG_VIDEOTOOLBOX
61 &ff_hwcontext_type_videotoolbox,
62 #endif
63 #if CONFIG_MEDIACODEC
64 &ff_hwcontext_type_mediacodec,
65 #endif
66 #if CONFIG_VULKAN
67 &ff_hwcontext_type_vulkan,
68 #endif
69 #if CONFIG_AMF
70 &ff_hwcontext_type_amf,
71 #endif
72 NULL,
75 static const char *const hw_type_names[] = {
76 [AV_HWDEVICE_TYPE_CUDA] = "cuda",
77 [AV_HWDEVICE_TYPE_DRM] = "drm",
78 [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
79 [AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
80 [AV_HWDEVICE_TYPE_D3D12VA] = "d3d12va",
81 [AV_HWDEVICE_TYPE_OPENCL] = "opencl",
82 [AV_HWDEVICE_TYPE_QSV] = "qsv",
83 [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
84 [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
85 [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
86 [AV_HWDEVICE_TYPE_MEDIACODEC] = "mediacodec",
87 [AV_HWDEVICE_TYPE_VULKAN] = "vulkan",
88 [AV_HWDEVICE_TYPE_AMF] = "amf",
91 typedef struct FFHWDeviceContext {
92 /**
93 * The public AVHWDeviceContext. See hwcontext.h for it.
95 AVHWDeviceContext p;
97 const HWContextType *hw_type;
99 /**
100 * For a derived device, a reference to the original device
101 * context it was derived from.
103 AVBufferRef *source_device;
104 } FFHWDeviceContext;
106 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
108 int type;
109 for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
110 if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
111 return type;
113 return AV_HWDEVICE_TYPE_NONE;
116 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
118 if (type > AV_HWDEVICE_TYPE_NONE &&
119 type < FF_ARRAY_ELEMS(hw_type_names))
120 return hw_type_names[type];
121 else
122 return NULL;
125 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
127 enum AVHWDeviceType next;
128 int i, set = 0;
129 for (i = 0; hw_table[i]; i++) {
130 if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
131 continue;
132 if (!set || hw_table[i]->type < next) {
133 next = hw_table[i]->type;
134 set = 1;
137 return set ? next : AV_HWDEVICE_TYPE_NONE;
140 static const AVClass hwdevice_ctx_class = {
141 .class_name = "AVHWDeviceContext",
142 .item_name = av_default_item_name,
143 .version = LIBAVUTIL_VERSION_INT,
146 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
148 FFHWDeviceContext *ctxi = (FFHWDeviceContext*)data;
149 AVHWDeviceContext *ctx = &ctxi->p;
151 /* uninit might still want access the hw context and the user
152 * free() callback might destroy it, so uninit has to be called first */
153 if (ctxi->hw_type->device_uninit)
154 ctxi->hw_type->device_uninit(ctx);
156 if (ctx->free)
157 ctx->free(ctx);
159 av_buffer_unref(&ctxi->source_device);
161 av_freep(&ctx->hwctx);
162 av_freep(&ctx);
165 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
167 FFHWDeviceContext *ctxi;
168 AVHWDeviceContext *ctx;
169 AVBufferRef *buf;
170 const HWContextType *hw_type = NULL;
171 int i;
173 for (i = 0; hw_table[i]; i++) {
174 if (hw_table[i]->type == type) {
175 hw_type = hw_table[i];
176 break;
179 if (!hw_type)
180 return NULL;
182 ctxi = av_mallocz(sizeof(*ctxi));
183 if (!ctxi)
184 return NULL;
185 ctx = &ctxi->p;
187 if (hw_type->device_hwctx_size) {
188 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
189 if (!ctx->hwctx)
190 goto fail;
193 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
194 hwdevice_ctx_free, NULL,
195 AV_BUFFER_FLAG_READONLY);
196 if (!buf)
197 goto fail;
199 ctx->type = type;
200 ctx->av_class = &hwdevice_ctx_class;
202 ctxi->hw_type = hw_type;
204 return buf;
206 fail:
207 av_freep(&ctx->hwctx);
208 av_freep(&ctx);
209 return NULL;
212 int av_hwdevice_ctx_init(AVBufferRef *ref)
214 FFHWDeviceContext *ctxi = (FFHWDeviceContext*)ref->data;
215 AVHWDeviceContext *ctx = &ctxi->p;
216 int ret = 0;
218 if (ctxi->hw_type->device_init)
219 ret = ctxi->hw_type->device_init(ctx);
221 return ret;
224 static const AVClass hwframe_ctx_class = {
225 .class_name = "AVHWFramesContext",
226 .item_name = av_default_item_name,
227 .version = LIBAVUTIL_VERSION_INT,
230 static void hwframe_ctx_free(void *opaque, uint8_t *data)
232 FFHWFramesContext *ctxi = (FFHWFramesContext*)data;
233 AVHWFramesContext *ctx = &ctxi->p;
235 if (ctxi->pool_internal)
236 av_buffer_pool_uninit(&ctxi->pool_internal);
238 if (ctxi->hw_type->frames_uninit)
239 ctxi->hw_type->frames_uninit(ctx);
241 if (ctx->free)
242 ctx->free(ctx);
244 av_buffer_unref(&ctxi->source_frames);
246 av_buffer_unref(&ctx->device_ref);
248 av_freep(&ctx->hwctx);
249 av_freep(&ctx);
252 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
254 FFHWDeviceContext *device_ctx = (FFHWDeviceContext*)device_ref_in->data;
255 const HWContextType *hw_type = device_ctx->hw_type;
256 FFHWFramesContext *ctxi;
257 AVHWFramesContext *ctx;
258 AVBufferRef *buf, *device_ref = NULL;
260 ctxi = av_mallocz(sizeof(*ctxi));
261 if (!ctxi)
262 return NULL;
263 ctx = &ctxi->p;
265 if (hw_type->frames_hwctx_size) {
266 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
267 if (!ctx->hwctx)
268 goto fail;
271 device_ref = av_buffer_ref(device_ref_in);
272 if (!device_ref)
273 goto fail;
275 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
276 hwframe_ctx_free, NULL,
277 AV_BUFFER_FLAG_READONLY);
278 if (!buf)
279 goto fail;
281 ctx->av_class = &hwframe_ctx_class;
282 ctx->device_ref = device_ref;
283 ctx->device_ctx = &device_ctx->p;
284 ctx->format = AV_PIX_FMT_NONE;
285 ctx->sw_format = AV_PIX_FMT_NONE;
287 ctxi->hw_type = hw_type;
289 return buf;
291 fail:
292 av_buffer_unref(&device_ref);
293 av_freep(&ctx->hwctx);
294 av_freep(&ctx);
295 return NULL;
298 static int hwframe_pool_prealloc(AVBufferRef *ref)
300 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
301 AVFrame **frames;
302 int i, ret = 0;
304 frames = av_calloc(ctx->initial_pool_size, sizeof(*frames));
305 if (!frames)
306 return AVERROR(ENOMEM);
308 for (i = 0; i < ctx->initial_pool_size; i++) {
309 frames[i] = av_frame_alloc();
310 if (!frames[i])
311 goto fail;
313 ret = av_hwframe_get_buffer(ref, frames[i], 0);
314 if (ret < 0)
315 goto fail;
318 fail:
319 for (i = 0; i < ctx->initial_pool_size; i++)
320 av_frame_free(&frames[i]);
321 av_freep(&frames);
323 return ret;
326 int av_hwframe_ctx_init(AVBufferRef *ref)
328 FFHWFramesContext *ctxi = (FFHWFramesContext*)ref->data;
329 AVHWFramesContext *ctx = &ctxi->p;
330 const enum AVPixelFormat *pix_fmt;
331 int ret;
333 if (ctxi->source_frames) {
334 /* A derived frame context is already initialised. */
335 return 0;
338 /* validate the pixel format */
339 for (pix_fmt = ctxi->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
340 if (*pix_fmt == ctx->format)
341 break;
343 if (*pix_fmt == AV_PIX_FMT_NONE) {
344 av_log(ctx, AV_LOG_ERROR,
345 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
346 av_get_pix_fmt_name(ctx->format), ctxi->hw_type->name);
347 return AVERROR(ENOSYS);
350 /* validate the dimensions */
351 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
352 if (ret < 0)
353 return ret;
355 /* format-specific init */
356 if (ctxi->hw_type->frames_init) {
357 ret = ctxi->hw_type->frames_init(ctx);
358 if (ret < 0)
359 return ret;
362 if (ctxi->pool_internal && !ctx->pool)
363 ctx->pool = ctxi->pool_internal;
365 /* preallocate the frames in the pool, if requested */
366 if (ctx->initial_pool_size > 0) {
367 ret = hwframe_pool_prealloc(ref);
368 if (ret < 0)
369 return ret;
372 return 0;
375 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
376 enum AVHWFrameTransferDirection dir,
377 enum AVPixelFormat **formats, int flags)
379 FFHWFramesContext *ctxi = (FFHWFramesContext*)hwframe_ref->data;
381 if (!ctxi->hw_type->transfer_get_formats)
382 return AVERROR(ENOSYS);
384 return ctxi->hw_type->transfer_get_formats(&ctxi->p, dir, formats);
387 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
389 AVHWFramesContext *ctx;
390 AVFrame *frame_tmp;
391 int ret = 0;
393 if (!src->hw_frames_ctx)
394 return AVERROR(EINVAL);
395 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
397 frame_tmp = av_frame_alloc();
398 if (!frame_tmp)
399 return AVERROR(ENOMEM);
401 /* if the format is set, use that
402 * otherwise pick the first supported one */
403 if (dst->format >= 0) {
404 frame_tmp->format = dst->format;
405 } else {
406 enum AVPixelFormat *formats;
408 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
409 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
410 &formats, 0);
411 if (ret < 0)
412 goto fail;
413 frame_tmp->format = formats[0];
414 av_freep(&formats);
416 frame_tmp->width = ctx->width;
417 frame_tmp->height = ctx->height;
419 ret = av_frame_get_buffer(frame_tmp, 0);
420 if (ret < 0)
421 goto fail;
423 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
424 if (ret < 0)
425 goto fail;
427 frame_tmp->width = src->width;
428 frame_tmp->height = src->height;
430 av_frame_move_ref(dst, frame_tmp);
432 fail:
433 av_frame_free(&frame_tmp);
434 return ret;
437 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
439 int ret;
441 if (!dst->buf[0])
442 return transfer_data_alloc(dst, src, flags);
445 * Hardware -> Hardware Transfer.
446 * Unlike Software -> Hardware or Hardware -> Software, the transfer
447 * function could be provided by either the src or dst, depending on
448 * the specific combination of hardware.
450 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
451 FFHWFramesContext *src_ctx =
452 (FFHWFramesContext*)src->hw_frames_ctx->data;
453 FFHWFramesContext *dst_ctx =
454 (FFHWFramesContext*)dst->hw_frames_ctx->data;
456 if (src_ctx->source_frames) {
457 av_log(src_ctx, AV_LOG_ERROR,
458 "A device with a derived frame context cannot be used as "
459 "the source of a HW -> HW transfer.");
460 return AVERROR(ENOSYS);
463 if (dst_ctx->source_frames) {
464 av_log(src_ctx, AV_LOG_ERROR,
465 "A device with a derived frame context cannot be used as "
466 "the destination of a HW -> HW transfer.");
467 return AVERROR(ENOSYS);
470 ret = src_ctx->hw_type->transfer_data_from(&src_ctx->p, dst, src);
471 if (ret == AVERROR(ENOSYS))
472 ret = dst_ctx->hw_type->transfer_data_to(&dst_ctx->p, dst, src);
473 if (ret < 0)
474 return ret;
475 } else {
476 if (src->hw_frames_ctx) {
477 FFHWFramesContext *ctx = (FFHWFramesContext*)src->hw_frames_ctx->data;
479 ret = ctx->hw_type->transfer_data_from(&ctx->p, dst, src);
480 if (ret < 0)
481 return ret;
482 } else if (dst->hw_frames_ctx) {
483 FFHWFramesContext *ctx = (FFHWFramesContext*)dst->hw_frames_ctx->data;
485 ret = ctx->hw_type->transfer_data_to(&ctx->p, dst, src);
486 if (ret < 0)
487 return ret;
488 } else {
489 return AVERROR(ENOSYS);
492 return 0;
495 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
497 FFHWFramesContext *ctxi = (FFHWFramesContext*)hwframe_ref->data;
498 AVHWFramesContext *ctx = &ctxi->p;
499 int ret;
501 if (ctxi->source_frames) {
502 // This is a derived frame context, so we allocate in the source
503 // and map the frame immediately.
504 AVFrame *src_frame;
506 frame->format = ctx->format;
507 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
508 if (!frame->hw_frames_ctx)
509 return AVERROR(ENOMEM);
511 src_frame = av_frame_alloc();
512 if (!src_frame)
513 return AVERROR(ENOMEM);
515 ret = av_hwframe_get_buffer(ctxi->source_frames,
516 src_frame, 0);
517 if (ret < 0) {
518 av_frame_free(&src_frame);
519 return ret;
522 ret = av_hwframe_map(frame, src_frame,
523 ctxi->source_allocation_map_flags);
524 if (ret) {
525 av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
526 "frame context: %d.\n", ret);
527 av_frame_free(&src_frame);
528 return ret;
531 // Free the source frame immediately - the mapped frame still
532 // contains a reference to it.
533 av_frame_free(&src_frame);
535 return 0;
538 if (!ctxi->hw_type->frames_get_buffer)
539 return AVERROR(ENOSYS);
541 if (!ctx->pool)
542 return AVERROR(EINVAL);
544 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
545 if (!frame->hw_frames_ctx)
546 return AVERROR(ENOMEM);
548 ret = ctxi->hw_type->frames_get_buffer(ctx, frame);
549 if (ret < 0) {
550 av_buffer_unref(&frame->hw_frames_ctx);
551 return ret;
554 frame->extended_data = frame->data;
556 return 0;
559 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
561 FFHWDeviceContext *ctx = (FFHWDeviceContext*)ref->data;
562 const HWContextType *hw_type = ctx->hw_type;
564 if (hw_type->device_hwconfig_size == 0)
565 return NULL;
567 return av_mallocz(hw_type->device_hwconfig_size);
570 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
571 const void *hwconfig)
573 FFHWDeviceContext *ctx = (FFHWDeviceContext*)ref->data;
574 const HWContextType *hw_type = ctx->hw_type;
575 AVHWFramesConstraints *constraints;
577 if (!hw_type->frames_get_constraints)
578 return NULL;
580 constraints = av_mallocz(sizeof(*constraints));
581 if (!constraints)
582 return NULL;
584 constraints->min_width = constraints->min_height = 0;
585 constraints->max_width = constraints->max_height = INT_MAX;
587 if (hw_type->frames_get_constraints(&ctx->p, hwconfig, constraints) >= 0) {
588 return constraints;
589 } else {
590 av_hwframe_constraints_free(&constraints);
591 return NULL;
595 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
597 if (*constraints) {
598 av_freep(&(*constraints)->valid_hw_formats);
599 av_freep(&(*constraints)->valid_sw_formats);
601 av_freep(constraints);
604 int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
605 const char *device, AVDictionary *opts, int flags)
607 AVBufferRef *device_ref = NULL;
608 FFHWDeviceContext *device_ctx;
609 int ret = 0;
611 device_ref = av_hwdevice_ctx_alloc(type);
612 if (!device_ref) {
613 ret = AVERROR(ENOMEM);
614 goto fail;
616 device_ctx = (FFHWDeviceContext*)device_ref->data;
618 if (!device_ctx->hw_type->device_create) {
619 ret = AVERROR(ENOSYS);
620 goto fail;
623 ret = device_ctx->hw_type->device_create(&device_ctx->p, device,
624 opts, flags);
625 if (ret < 0)
626 goto fail;
628 ret = av_hwdevice_ctx_init(device_ref);
629 if (ret < 0)
630 goto fail;
632 *pdevice_ref = device_ref;
633 return 0;
634 fail:
635 av_buffer_unref(&device_ref);
636 *pdevice_ref = NULL;
637 return ret;
640 int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ref_ptr,
641 enum AVHWDeviceType type,
642 AVBufferRef *src_ref,
643 AVDictionary *options, int flags)
645 AVBufferRef *dst_ref = NULL, *tmp_ref;
646 FFHWDeviceContext *dst_ctx;
647 int ret = 0;
649 tmp_ref = src_ref;
650 while (tmp_ref) {
651 FFHWDeviceContext *tmp_ctx = (FFHWDeviceContext*)tmp_ref->data;
652 if (tmp_ctx->p.type == type) {
653 dst_ref = av_buffer_ref(tmp_ref);
654 if (!dst_ref) {
655 ret = AVERROR(ENOMEM);
656 goto fail;
658 goto done;
660 tmp_ref = tmp_ctx->source_device;
663 dst_ref = av_hwdevice_ctx_alloc(type);
664 if (!dst_ref) {
665 ret = AVERROR(ENOMEM);
666 goto fail;
668 dst_ctx = (FFHWDeviceContext*)dst_ref->data;
670 tmp_ref = src_ref;
671 while (tmp_ref) {
672 FFHWDeviceContext *tmp_ctx = (FFHWDeviceContext*)tmp_ref->data;
673 if (dst_ctx->hw_type->device_derive) {
674 ret = dst_ctx->hw_type->device_derive(&dst_ctx->p,
675 &tmp_ctx->p,
676 options, flags);
677 if (ret == 0) {
678 dst_ctx->source_device = av_buffer_ref(src_ref);
679 if (!dst_ctx->source_device) {
680 ret = AVERROR(ENOMEM);
681 goto fail;
683 ret = av_hwdevice_ctx_init(dst_ref);
684 if (ret < 0)
685 goto fail;
686 goto done;
688 if (ret != AVERROR(ENOSYS))
689 goto fail;
691 tmp_ref = tmp_ctx->source_device;
694 ret = AVERROR(ENOSYS);
695 goto fail;
697 done:
698 *dst_ref_ptr = dst_ref;
699 return 0;
701 fail:
702 av_buffer_unref(&dst_ref);
703 *dst_ref_ptr = NULL;
704 return ret;
707 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
708 enum AVHWDeviceType type,
709 AVBufferRef *src_ref, int flags)
711 return av_hwdevice_ctx_create_derived_opts(dst_ref_ptr, type, src_ref,
712 NULL, flags);
715 static void ff_hwframe_unmap(void *opaque, uint8_t *data)
717 HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
718 AVHWFramesContext *ctx = opaque;
720 if (hwmap->unmap)
721 hwmap->unmap(ctx, hwmap);
723 av_frame_free(&hwmap->source);
725 av_buffer_unref(&hwmap->hw_frames_ctx);
727 av_free(hwmap);
730 int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
731 AVFrame *dst, const AVFrame *src,
732 void (*unmap)(AVHWFramesContext *ctx,
733 HWMapDescriptor *hwmap),
734 void *priv)
736 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
737 HWMapDescriptor *hwmap;
738 int ret;
740 hwmap = av_mallocz(sizeof(*hwmap));
741 if (!hwmap) {
742 ret = AVERROR(ENOMEM);
743 goto fail;
746 hwmap->source = av_frame_alloc();
747 if (!hwmap->source) {
748 ret = AVERROR(ENOMEM);
749 goto fail;
751 ret = av_frame_ref(hwmap->source, src);
752 if (ret < 0)
753 goto fail;
755 hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
756 if (!hwmap->hw_frames_ctx) {
757 ret = AVERROR(ENOMEM);
758 goto fail;
761 hwmap->unmap = unmap;
762 hwmap->priv = priv;
764 dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
765 &ff_hwframe_unmap, ctx, 0);
766 if (!dst->buf[0]) {
767 ret = AVERROR(ENOMEM);
768 goto fail;
771 return 0;
773 fail:
774 if (hwmap) {
775 av_buffer_unref(&hwmap->hw_frames_ctx);
776 av_frame_free(&hwmap->source);
778 av_free(hwmap);
779 return ret;
782 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
784 AVBufferRef *orig_dst_frames = dst->hw_frames_ctx;
785 enum AVPixelFormat orig_dst_fmt = dst->format;
786 HWMapDescriptor *hwmap;
787 int ret;
789 if (src->hw_frames_ctx && dst->hw_frames_ctx) {
790 FFHWFramesContext *src_frames = (FFHWFramesContext*)src->hw_frames_ctx->data;
791 FFHWFramesContext *dst_frames = (FFHWFramesContext*)dst->hw_frames_ctx->data;
793 if ((src_frames == dst_frames &&
794 src->format == dst_frames->p.sw_format &&
795 dst->format == dst_frames->p.format) ||
796 (src_frames->source_frames &&
797 src_frames->source_frames->data ==
798 (uint8_t*)dst_frames)) {
799 // This is an unmap operation. We don't need to directly
800 // do anything here other than fill in the original frame,
801 // because the real unmap will be invoked when the last
802 // reference to the mapped frame disappears.
803 if (!src->buf[0]) {
804 av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
805 "found when attempting unmap.\n");
806 return AVERROR(EINVAL);
808 hwmap = (HWMapDescriptor*)src->buf[0]->data;
809 return av_frame_replace(dst, hwmap->source);
813 if (src->hw_frames_ctx) {
814 FFHWFramesContext *src_frames = (FFHWFramesContext*)src->hw_frames_ctx->data;
816 if (src_frames->p.format == src->format &&
817 src_frames->hw_type->map_from) {
818 ret = src_frames->hw_type->map_from(&src_frames->p,
819 dst, src, flags);
820 if (ret >= 0)
821 return ret;
822 else if (ret != AVERROR(ENOSYS))
823 goto fail;
827 if (dst->hw_frames_ctx) {
828 FFHWFramesContext *dst_frames = (FFHWFramesContext*)dst->hw_frames_ctx->data;
830 if (dst_frames->p.format == dst->format &&
831 dst_frames->hw_type->map_to) {
832 ret = dst_frames->hw_type->map_to(&dst_frames->p,
833 dst, src, flags);
834 if (ret >= 0)
835 return ret;
836 else if (ret != AVERROR(ENOSYS))
837 goto fail;
841 return AVERROR(ENOSYS);
843 fail:
844 // if the caller provided dst frames context, it should be preserved
845 // by this function
846 av_assert0(orig_dst_frames == NULL ||
847 orig_dst_frames == dst->hw_frames_ctx);
849 // preserve user-provided dst frame fields, but clean
850 // anything we might have set
851 dst->hw_frames_ctx = NULL;
852 av_frame_unref(dst);
854 dst->hw_frames_ctx = orig_dst_frames;
855 dst->format = orig_dst_fmt;
857 return ret;
860 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
861 enum AVPixelFormat format,
862 AVBufferRef *derived_device_ctx,
863 AVBufferRef *source_frame_ctx,
864 int flags)
866 AVBufferRef *dst_ref = NULL;
867 FFHWFramesContext *dsti = NULL;
868 FFHWFramesContext *srci = (FFHWFramesContext*)source_frame_ctx->data;
869 AVHWFramesContext *dst, *src = &srci->p;
870 int ret;
872 if (srci->source_frames) {
873 AVHWFramesContext *src_src =
874 (AVHWFramesContext*)srci->source_frames->data;
875 AVHWDeviceContext *dst_dev =
876 (AVHWDeviceContext*)derived_device_ctx->data;
878 if (src_src->device_ctx == dst_dev) {
879 // This is actually an unmapping, so we just return a
880 // reference to the source frame context.
881 *derived_frame_ctx = av_buffer_ref(srci->source_frames);
882 if (!*derived_frame_ctx) {
883 ret = AVERROR(ENOMEM);
884 goto fail;
886 return 0;
890 dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
891 if (!dst_ref) {
892 ret = AVERROR(ENOMEM);
893 goto fail;
896 dsti = (FFHWFramesContext*)dst_ref->data;
897 dst = &dsti->p;
899 dst->format = format;
900 dst->sw_format = src->sw_format;
901 dst->width = src->width;
902 dst->height = src->height;
904 dsti->source_frames = av_buffer_ref(source_frame_ctx);
905 if (!dsti->source_frames) {
906 ret = AVERROR(ENOMEM);
907 goto fail;
910 dsti->source_allocation_map_flags =
911 flags & (AV_HWFRAME_MAP_READ |
912 AV_HWFRAME_MAP_WRITE |
913 AV_HWFRAME_MAP_OVERWRITE |
914 AV_HWFRAME_MAP_DIRECT);
916 ret = AVERROR(ENOSYS);
917 if (srci->hw_type->frames_derive_from)
918 ret = srci->hw_type->frames_derive_from(dst, src, flags);
919 if (ret == AVERROR(ENOSYS) &&
920 dsti->hw_type->frames_derive_to)
921 ret = dsti->hw_type->frames_derive_to(dst, src, flags);
922 if (ret == AVERROR(ENOSYS))
923 ret = 0;
924 if (ret)
925 goto fail;
927 *derived_frame_ctx = dst_ref;
928 return 0;
930 fail:
931 if (dsti)
932 av_buffer_unref(&dsti->source_frames);
933 av_buffer_unref(&dst_ref);
934 return ret;
937 int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
939 HWMapDescriptor *hwmap = (HWMapDescriptor*)dst->buf[0]->data;
940 return av_frame_replace(hwmap->source, src);