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
24 #include "hwcontext.h"
25 #include "hwcontext_internal.h"
32 static const HWContextType
* const hw_table
[] = {
34 &ff_hwcontext_type_cuda
,
37 &ff_hwcontext_type_d3d11va
,
40 &ff_hwcontext_type_d3d12va
,
43 &ff_hwcontext_type_drm
,
46 &ff_hwcontext_type_dxva2
,
49 &ff_hwcontext_type_opencl
,
52 &ff_hwcontext_type_qsv
,
55 &ff_hwcontext_type_vaapi
,
58 &ff_hwcontext_type_vdpau
,
60 #if CONFIG_VIDEOTOOLBOX
61 &ff_hwcontext_type_videotoolbox
,
64 &ff_hwcontext_type_mediacodec
,
67 &ff_hwcontext_type_vulkan
,
70 &ff_hwcontext_type_amf
,
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
{
93 * The public AVHWDeviceContext. See hwcontext.h for it.
97 const HWContextType
*hw_type
;
100 * For a derived device, a reference to the original device
101 * context it was derived from.
103 AVBufferRef
*source_device
;
106 enum AVHWDeviceType
av_hwdevice_find_type_by_name(const char *name
)
109 for (type
= 0; type
< FF_ARRAY_ELEMS(hw_type_names
); type
++) {
110 if (hw_type_names
[type
] && !strcmp(hw_type_names
[type
], name
))
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
];
125 enum AVHWDeviceType
av_hwdevice_iterate_types(enum AVHWDeviceType prev
)
127 enum AVHWDeviceType next
;
129 for (i
= 0; hw_table
[i
]; i
++) {
130 if (prev
!= AV_HWDEVICE_TYPE_NONE
&& hw_table
[i
]->type
<= prev
)
132 if (!set
|| hw_table
[i
]->type
< next
) {
133 next
= hw_table
[i
]->type
;
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
);
159 av_buffer_unref(&ctxi
->source_device
);
161 av_freep(&ctx
->hwctx
);
165 AVBufferRef
*av_hwdevice_ctx_alloc(enum AVHWDeviceType type
)
167 FFHWDeviceContext
*ctxi
;
168 AVHWDeviceContext
*ctx
;
170 const HWContextType
*hw_type
= NULL
;
173 for (i
= 0; hw_table
[i
]; i
++) {
174 if (hw_table
[i
]->type
== type
) {
175 hw_type
= hw_table
[i
];
182 ctxi
= av_mallocz(sizeof(*ctxi
));
187 if (hw_type
->device_hwctx_size
) {
188 ctx
->hwctx
= av_mallocz(hw_type
->device_hwctx_size
);
193 buf
= av_buffer_create((uint8_t*)ctx
, sizeof(*ctx
),
194 hwdevice_ctx_free
, NULL
,
195 AV_BUFFER_FLAG_READONLY
);
200 ctx
->av_class
= &hwdevice_ctx_class
;
202 ctxi
->hw_type
= hw_type
;
207 av_freep(&ctx
->hwctx
);
212 int av_hwdevice_ctx_init(AVBufferRef
*ref
)
214 FFHWDeviceContext
*ctxi
= (FFHWDeviceContext
*)ref
->data
;
215 AVHWDeviceContext
*ctx
= &ctxi
->p
;
218 if (ctxi
->hw_type
->device_init
)
219 ret
= ctxi
->hw_type
->device_init(ctx
);
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
);
244 av_buffer_unref(&ctxi
->source_frames
);
246 av_buffer_unref(&ctx
->device_ref
);
248 av_freep(&ctx
->hwctx
);
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
));
265 if (hw_type
->frames_hwctx_size
) {
266 ctx
->hwctx
= av_mallocz(hw_type
->frames_hwctx_size
);
271 device_ref
= av_buffer_ref(device_ref_in
);
275 buf
= av_buffer_create((uint8_t*)ctx
, sizeof(*ctx
),
276 hwframe_ctx_free
, NULL
,
277 AV_BUFFER_FLAG_READONLY
);
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
;
292 av_buffer_unref(&device_ref
);
293 av_freep(&ctx
->hwctx
);
298 static int hwframe_pool_prealloc(AVBufferRef
*ref
)
300 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)ref
->data
;
304 frames
= av_calloc(ctx
->initial_pool_size
, sizeof(*frames
));
306 return AVERROR(ENOMEM
);
308 for (i
= 0; i
< ctx
->initial_pool_size
; i
++) {
309 frames
[i
] = av_frame_alloc();
313 ret
= av_hwframe_get_buffer(ref
, frames
[i
], 0);
319 for (i
= 0; i
< ctx
->initial_pool_size
; i
++)
320 av_frame_free(&frames
[i
]);
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
;
333 if (ctxi
->source_frames
) {
334 /* A derived frame context is already initialised. */
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
)
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
);
355 /* format-specific init */
356 if (ctxi
->hw_type
->frames_init
) {
357 ret
= ctxi
->hw_type
->frames_init(ctx
);
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
);
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
;
393 if (!src
->hw_frames_ctx
)
394 return AVERROR(EINVAL
);
395 ctx
= (AVHWFramesContext
*)src
->hw_frames_ctx
->data
;
397 frame_tmp
= av_frame_alloc();
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
;
406 enum AVPixelFormat
*formats
;
408 ret
= av_hwframe_transfer_get_formats(src
->hw_frames_ctx
,
409 AV_HWFRAME_TRANSFER_DIRECTION_FROM
,
413 frame_tmp
->format
= formats
[0];
416 frame_tmp
->width
= ctx
->width
;
417 frame_tmp
->height
= ctx
->height
;
419 ret
= av_frame_get_buffer(frame_tmp
, 0);
423 ret
= av_hwframe_transfer_data(frame_tmp
, src
, flags
);
427 frame_tmp
->width
= src
->width
;
428 frame_tmp
->height
= src
->height
;
430 av_frame_move_ref(dst
, frame_tmp
);
433 av_frame_free(&frame_tmp
);
437 int av_hwframe_transfer_data(AVFrame
*dst
, const AVFrame
*src
, int flags
)
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
);
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
);
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
);
489 return AVERROR(ENOSYS
);
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
;
501 if (ctxi
->source_frames
) {
502 // This is a derived frame context, so we allocate in the source
503 // and map the frame immediately.
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();
513 return AVERROR(ENOMEM
);
515 ret
= av_hwframe_get_buffer(ctxi
->source_frames
,
518 av_frame_free(&src_frame
);
522 ret
= av_hwframe_map(frame
, src_frame
,
523 ctxi
->source_allocation_map_flags
);
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
);
531 // Free the source frame immediately - the mapped frame still
532 // contains a reference to it.
533 av_frame_free(&src_frame
);
538 if (!ctxi
->hw_type
->frames_get_buffer
)
539 return AVERROR(ENOSYS
);
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
);
550 av_buffer_unref(&frame
->hw_frames_ctx
);
554 frame
->extended_data
= frame
->data
;
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)
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
)
580 constraints
= av_mallocz(sizeof(*constraints
));
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) {
590 av_hwframe_constraints_free(&constraints
);
595 void av_hwframe_constraints_free(AVHWFramesConstraints
**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
;
611 device_ref
= av_hwdevice_ctx_alloc(type
);
613 ret
= AVERROR(ENOMEM
);
616 device_ctx
= (FFHWDeviceContext
*)device_ref
->data
;
618 if (!device_ctx
->hw_type
->device_create
) {
619 ret
= AVERROR(ENOSYS
);
623 ret
= device_ctx
->hw_type
->device_create(&device_ctx
->p
, device
,
628 ret
= av_hwdevice_ctx_init(device_ref
);
632 *pdevice_ref
= device_ref
;
635 av_buffer_unref(&device_ref
);
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
;
651 FFHWDeviceContext
*tmp_ctx
= (FFHWDeviceContext
*)tmp_ref
->data
;
652 if (tmp_ctx
->p
.type
== type
) {
653 dst_ref
= av_buffer_ref(tmp_ref
);
655 ret
= AVERROR(ENOMEM
);
660 tmp_ref
= tmp_ctx
->source_device
;
663 dst_ref
= av_hwdevice_ctx_alloc(type
);
665 ret
= AVERROR(ENOMEM
);
668 dst_ctx
= (FFHWDeviceContext
*)dst_ref
->data
;
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
,
678 dst_ctx
->source_device
= av_buffer_ref(src_ref
);
679 if (!dst_ctx
->source_device
) {
680 ret
= AVERROR(ENOMEM
);
683 ret
= av_hwdevice_ctx_init(dst_ref
);
688 if (ret
!= AVERROR(ENOSYS
))
691 tmp_ref
= tmp_ctx
->source_device
;
694 ret
= AVERROR(ENOSYS
);
698 *dst_ref_ptr
= dst_ref
;
702 av_buffer_unref(&dst_ref
);
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
,
715 static void ff_hwframe_unmap(void *opaque
, uint8_t *data
)
717 HWMapDescriptor
*hwmap
= (HWMapDescriptor
*)data
;
718 AVHWFramesContext
*ctx
= opaque
;
721 hwmap
->unmap(ctx
, hwmap
);
723 av_frame_free(&hwmap
->source
);
725 av_buffer_unref(&hwmap
->hw_frames_ctx
);
730 int ff_hwframe_map_create(AVBufferRef
*hwframe_ref
,
731 AVFrame
*dst
, const AVFrame
*src
,
732 void (*unmap
)(AVHWFramesContext
*ctx
,
733 HWMapDescriptor
*hwmap
),
736 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)hwframe_ref
->data
;
737 HWMapDescriptor
*hwmap
;
740 hwmap
= av_mallocz(sizeof(*hwmap
));
742 ret
= AVERROR(ENOMEM
);
746 hwmap
->source
= av_frame_alloc();
747 if (!hwmap
->source
) {
748 ret
= AVERROR(ENOMEM
);
751 ret
= av_frame_ref(hwmap
->source
, src
);
755 hwmap
->hw_frames_ctx
= av_buffer_ref(hwframe_ref
);
756 if (!hwmap
->hw_frames_ctx
) {
757 ret
= AVERROR(ENOMEM
);
761 hwmap
->unmap
= unmap
;
764 dst
->buf
[0] = av_buffer_create((uint8_t*)hwmap
, sizeof(*hwmap
),
765 &ff_hwframe_unmap
, ctx
, 0);
767 ret
= AVERROR(ENOMEM
);
775 av_buffer_unref(&hwmap
->hw_frames_ctx
);
776 av_frame_free(&hwmap
->source
);
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
;
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.
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
,
822 else if (ret
!= AVERROR(ENOSYS
))
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
,
836 else if (ret
!= AVERROR(ENOSYS
))
841 return AVERROR(ENOSYS
);
844 // if the caller provided dst frames context, it should be preserved
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
;
854 dst
->hw_frames_ctx
= orig_dst_frames
;
855 dst
->format
= orig_dst_fmt
;
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
,
866 AVBufferRef
*dst_ref
= NULL
;
867 FFHWFramesContext
*dsti
= NULL
;
868 FFHWFramesContext
*srci
= (FFHWFramesContext
*)source_frame_ctx
->data
;
869 AVHWFramesContext
*dst
, *src
= &srci
->p
;
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
);
890 dst_ref
= av_hwframe_ctx_alloc(derived_device_ctx
);
892 ret
= AVERROR(ENOMEM
);
896 dsti
= (FFHWFramesContext
*)dst_ref
->data
;
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
);
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
))
927 *derived_frame_ctx
= dst_ref
;
932 av_buffer_unref(&dsti
->source_frames
);
933 av_buffer_unref(&dst_ref
);
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
);