2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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_vaapi
,
39 &ff_hwcontext_type_vdpau
,
44 static const AVClass hwdevice_ctx_class
= {
45 .class_name
= "AVHWDeviceContext",
46 .item_name
= av_default_item_name
,
47 .version
= LIBAVUTIL_VERSION_INT
,
50 static void hwdevice_ctx_free(void *opaque
, uint8_t *data
)
52 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)data
;
54 /* uninit might still want access the hw context and the user
55 * free() callback might destroy it, so uninit has to be called first */
56 if (ctx
->internal
->hw_type
->device_uninit
)
57 ctx
->internal
->hw_type
->device_uninit(ctx
);
62 av_freep(&ctx
->hwctx
);
63 av_freep(&ctx
->internal
->priv
);
64 av_freep(&ctx
->internal
);
68 AVBufferRef
*av_hwdevice_ctx_alloc(enum AVHWDeviceType type
)
70 AVHWDeviceContext
*ctx
;
72 const HWContextType
*hw_type
= NULL
;
75 for (i
= 0; hw_table
[i
]; i
++) {
76 if (hw_table
[i
]->type
== type
) {
77 hw_type
= hw_table
[i
];
84 ctx
= av_mallocz(sizeof(*ctx
));
88 ctx
->internal
= av_mallocz(sizeof(*ctx
->internal
));
92 if (hw_type
->device_priv_size
) {
93 ctx
->internal
->priv
= av_mallocz(hw_type
->device_priv_size
);
94 if (!ctx
->internal
->priv
)
98 if (hw_type
->device_hwctx_size
) {
99 ctx
->hwctx
= av_mallocz(hw_type
->device_hwctx_size
);
104 buf
= av_buffer_create((uint8_t*)ctx
, sizeof(*ctx
),
105 hwdevice_ctx_free
, NULL
,
106 AV_BUFFER_FLAG_READONLY
);
111 ctx
->av_class
= &hwdevice_ctx_class
;
113 ctx
->internal
->hw_type
= hw_type
;
119 av_freep(&ctx
->internal
->priv
);
120 av_freep(&ctx
->internal
);
121 av_freep(&ctx
->hwctx
);
126 int av_hwdevice_ctx_init(AVBufferRef
*ref
)
128 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
131 if (ctx
->internal
->hw_type
->device_init
) {
132 ret
= ctx
->internal
->hw_type
->device_init(ctx
);
139 if (ctx
->internal
->hw_type
->device_uninit
)
140 ctx
->internal
->hw_type
->device_uninit(ctx
);
144 static const AVClass hwframe_ctx_class
= {
145 .class_name
= "AVHWFramesContext",
146 .item_name
= av_default_item_name
,
147 .version
= LIBAVUTIL_VERSION_INT
,
150 static void hwframe_ctx_free(void *opaque
, uint8_t *data
)
152 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)data
;
154 if (ctx
->internal
->pool_internal
)
155 av_buffer_pool_uninit(&ctx
->internal
->pool_internal
);
157 if (ctx
->internal
->hw_type
->frames_uninit
)
158 ctx
->internal
->hw_type
->frames_uninit(ctx
);
163 av_buffer_unref(&ctx
->device_ref
);
165 av_freep(&ctx
->hwctx
);
166 av_freep(&ctx
->internal
->priv
);
167 av_freep(&ctx
->internal
);
171 AVBufferRef
*av_hwframe_ctx_alloc(AVBufferRef
*device_ref_in
)
173 AVHWDeviceContext
*device_ctx
= (AVHWDeviceContext
*)device_ref_in
->data
;
174 const HWContextType
*hw_type
= device_ctx
->internal
->hw_type
;
175 AVHWFramesContext
*ctx
;
176 AVBufferRef
*buf
, *device_ref
= NULL
;;
178 ctx
= av_mallocz(sizeof(*ctx
));
182 ctx
->internal
= av_mallocz(sizeof(*ctx
->internal
));
186 if (hw_type
->frames_priv_size
) {
187 ctx
->internal
->priv
= av_mallocz(hw_type
->frames_priv_size
);
188 if (!ctx
->internal
->priv
)
192 if (hw_type
->frames_hwctx_size
) {
193 ctx
->hwctx
= av_mallocz(hw_type
->frames_hwctx_size
);
198 device_ref
= av_buffer_ref(device_ref_in
);
202 buf
= av_buffer_create((uint8_t*)ctx
, sizeof(*ctx
),
203 hwframe_ctx_free
, NULL
,
204 AV_BUFFER_FLAG_READONLY
);
208 ctx
->av_class
= &hwframe_ctx_class
;
209 ctx
->device_ref
= device_ref
;
210 ctx
->device_ctx
= device_ctx
;
211 ctx
->format
= AV_PIX_FMT_NONE
;
212 ctx
->sw_format
= AV_PIX_FMT_NONE
;
214 ctx
->internal
->hw_type
= hw_type
;
220 av_buffer_unref(&device_ref
);
222 av_freep(&ctx
->internal
->priv
);
223 av_freep(&ctx
->internal
);
224 av_freep(&ctx
->hwctx
);
229 static int hwframe_pool_prealloc(AVBufferRef
*ref
)
231 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)ref
->data
;
235 frames
= av_mallocz_array(ctx
->initial_pool_size
, sizeof(*frames
));
237 return AVERROR(ENOMEM
);
239 for (i
= 0; i
< ctx
->initial_pool_size
; i
++) {
240 frames
[i
] = av_frame_alloc();
244 ret
= av_hwframe_get_buffer(ref
, frames
[i
], 0);
250 for (i
= 0; i
< ctx
->initial_pool_size
; i
++)
251 av_frame_free(&frames
[i
]);
257 int av_hwframe_ctx_init(AVBufferRef
*ref
)
259 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)ref
->data
;
260 const enum AVPixelFormat
*pix_fmt
;
263 /* validate the pixel format */
264 for (pix_fmt
= ctx
->internal
->hw_type
->pix_fmts
; *pix_fmt
!= AV_PIX_FMT_NONE
; pix_fmt
++) {
265 if (*pix_fmt
== ctx
->format
)
268 if (*pix_fmt
== AV_PIX_FMT_NONE
) {
269 av_log(ctx
, AV_LOG_ERROR
,
270 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
271 av_get_pix_fmt_name(ctx
->format
), ctx
->internal
->hw_type
->name
);
272 return AVERROR(ENOSYS
);
275 /* validate the dimensions */
276 ret
= av_image_check_size(ctx
->width
, ctx
->height
, 0, ctx
);
280 /* format-specific init */
281 if (ctx
->internal
->hw_type
->frames_init
) {
282 ret
= ctx
->internal
->hw_type
->frames_init(ctx
);
287 if (ctx
->internal
->pool_internal
&& !ctx
->pool
)
288 ctx
->pool
= ctx
->internal
->pool_internal
;
290 /* preallocate the frames in the pool, if requested */
291 if (ctx
->initial_pool_size
> 0) {
292 ret
= hwframe_pool_prealloc(ref
);
299 if (ctx
->internal
->hw_type
->frames_uninit
)
300 ctx
->internal
->hw_type
->frames_uninit(ctx
);
304 int av_hwframe_transfer_get_formats(AVBufferRef
*hwframe_ref
,
305 enum AVHWFrameTransferDirection dir
,
306 enum AVPixelFormat
**formats
, int flags
)
308 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)hwframe_ref
->data
;
310 if (!ctx
->internal
->hw_type
->transfer_get_formats
)
311 return AVERROR(ENOSYS
);
313 return ctx
->internal
->hw_type
->transfer_get_formats(ctx
, dir
, formats
);
316 static int transfer_data_alloc(AVFrame
*dst
, const AVFrame
*src
, int flags
)
321 frame_tmp
= av_frame_alloc();
323 return AVERROR(ENOMEM
);
325 /* if the format is set, use that
326 * otherwise pick the first supported one */
327 if (dst
->format
>= 0) {
328 frame_tmp
->format
= dst
->format
;
330 enum AVPixelFormat
*formats
;
332 ret
= av_hwframe_transfer_get_formats(src
->hw_frames_ctx
,
333 AV_HWFRAME_TRANSFER_DIRECTION_FROM
,
337 frame_tmp
->format
= formats
[0];
340 frame_tmp
->width
= src
->width
;
341 frame_tmp
->height
= src
->height
;
343 ret
= av_frame_get_buffer(frame_tmp
, 32);
347 ret
= av_hwframe_transfer_data(frame_tmp
, src
, flags
);
351 av_frame_move_ref(dst
, frame_tmp
);
354 av_frame_free(&frame_tmp
);
358 int av_hwframe_transfer_data(AVFrame
*dst
, const AVFrame
*src
, int flags
)
360 AVHWFramesContext
*ctx
;
364 return transfer_data_alloc(dst
, src
, flags
);
366 if (src
->hw_frames_ctx
) {
367 ctx
= (AVHWFramesContext
*)src
->hw_frames_ctx
->data
;
369 ret
= ctx
->internal
->hw_type
->transfer_data_from(ctx
, dst
, src
);
372 } else if (dst
->hw_frames_ctx
) {
373 ctx
= (AVHWFramesContext
*)dst
->hw_frames_ctx
->data
;
375 ret
= ctx
->internal
->hw_type
->transfer_data_to(ctx
, dst
, src
);
379 return AVERROR(ENOSYS
);
384 int av_hwframe_get_buffer(AVBufferRef
*hwframe_ref
, AVFrame
*frame
, int flags
)
386 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)hwframe_ref
->data
;
389 if (!ctx
->internal
->hw_type
->frames_get_buffer
)
390 return AVERROR(ENOSYS
);
393 return AVERROR(EINVAL
);
395 frame
->hw_frames_ctx
= av_buffer_ref(hwframe_ref
);
396 if (!frame
->hw_frames_ctx
)
397 return AVERROR(ENOMEM
);
399 ret
= ctx
->internal
->hw_type
->frames_get_buffer(ctx
, frame
);
401 av_buffer_unref(&frame
->hw_frames_ctx
);
408 void *av_hwdevice_hwconfig_alloc(AVBufferRef
*ref
)
410 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
411 const HWContextType
*hw_type
= ctx
->internal
->hw_type
;
413 if (hw_type
->device_hwconfig_size
== 0)
416 return av_mallocz(hw_type
->device_hwconfig_size
);
419 AVHWFramesConstraints
*av_hwdevice_get_hwframe_constraints(AVBufferRef
*ref
,
420 const void *hwconfig
)
422 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
423 const HWContextType
*hw_type
= ctx
->internal
->hw_type
;
424 AVHWFramesConstraints
*constraints
;
426 if (!hw_type
->frames_get_constraints
)
429 constraints
= av_mallocz(sizeof(*constraints
));
433 constraints
->min_width
= constraints
->min_height
= 0;
434 constraints
->max_width
= constraints
->max_height
= INT_MAX
;
436 if (hw_type
->frames_get_constraints(ctx
, hwconfig
, constraints
) >= 0) {
439 av_hwframe_constraints_free(&constraints
);
444 void av_hwframe_constraints_free(AVHWFramesConstraints
**constraints
)
447 av_freep(&(*constraints
)->valid_hw_formats
);
448 av_freep(&(*constraints
)->valid_sw_formats
);
450 av_freep(constraints
);