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_dxva2
,
39 &ff_hwcontext_type_vaapi
,
42 &ff_hwcontext_type_vdpau
,
47 static const AVClass hwdevice_ctx_class
= {
48 .class_name
= "AVHWDeviceContext",
49 .item_name
= av_default_item_name
,
50 .version
= LIBAVUTIL_VERSION_INT
,
53 static void hwdevice_ctx_free(void *opaque
, uint8_t *data
)
55 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)data
;
57 /* uninit might still want access the hw context and the user
58 * free() callback might destroy it, so uninit has to be called first */
59 if (ctx
->internal
->hw_type
->device_uninit
)
60 ctx
->internal
->hw_type
->device_uninit(ctx
);
65 av_freep(&ctx
->hwctx
);
66 av_freep(&ctx
->internal
->priv
);
67 av_freep(&ctx
->internal
);
71 AVBufferRef
*av_hwdevice_ctx_alloc(enum AVHWDeviceType type
)
73 AVHWDeviceContext
*ctx
;
75 const HWContextType
*hw_type
= NULL
;
78 for (i
= 0; hw_table
[i
]; i
++) {
79 if (hw_table
[i
]->type
== type
) {
80 hw_type
= hw_table
[i
];
87 ctx
= av_mallocz(sizeof(*ctx
));
91 ctx
->internal
= av_mallocz(sizeof(*ctx
->internal
));
95 if (hw_type
->device_priv_size
) {
96 ctx
->internal
->priv
= av_mallocz(hw_type
->device_priv_size
);
97 if (!ctx
->internal
->priv
)
101 if (hw_type
->device_hwctx_size
) {
102 ctx
->hwctx
= av_mallocz(hw_type
->device_hwctx_size
);
107 buf
= av_buffer_create((uint8_t*)ctx
, sizeof(*ctx
),
108 hwdevice_ctx_free
, NULL
,
109 AV_BUFFER_FLAG_READONLY
);
114 ctx
->av_class
= &hwdevice_ctx_class
;
116 ctx
->internal
->hw_type
= hw_type
;
122 av_freep(&ctx
->internal
->priv
);
123 av_freep(&ctx
->internal
);
124 av_freep(&ctx
->hwctx
);
129 int av_hwdevice_ctx_init(AVBufferRef
*ref
)
131 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
134 if (ctx
->internal
->hw_type
->device_init
) {
135 ret
= ctx
->internal
->hw_type
->device_init(ctx
);
142 if (ctx
->internal
->hw_type
->device_uninit
)
143 ctx
->internal
->hw_type
->device_uninit(ctx
);
147 static const AVClass hwframe_ctx_class
= {
148 .class_name
= "AVHWFramesContext",
149 .item_name
= av_default_item_name
,
150 .version
= LIBAVUTIL_VERSION_INT
,
153 static void hwframe_ctx_free(void *opaque
, uint8_t *data
)
155 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)data
;
157 if (ctx
->internal
->pool_internal
)
158 av_buffer_pool_uninit(&ctx
->internal
->pool_internal
);
160 if (ctx
->internal
->hw_type
->frames_uninit
)
161 ctx
->internal
->hw_type
->frames_uninit(ctx
);
166 av_buffer_unref(&ctx
->device_ref
);
168 av_freep(&ctx
->hwctx
);
169 av_freep(&ctx
->internal
->priv
);
170 av_freep(&ctx
->internal
);
174 AVBufferRef
*av_hwframe_ctx_alloc(AVBufferRef
*device_ref_in
)
176 AVHWDeviceContext
*device_ctx
= (AVHWDeviceContext
*)device_ref_in
->data
;
177 const HWContextType
*hw_type
= device_ctx
->internal
->hw_type
;
178 AVHWFramesContext
*ctx
;
179 AVBufferRef
*buf
, *device_ref
= NULL
;;
181 ctx
= av_mallocz(sizeof(*ctx
));
185 ctx
->internal
= av_mallocz(sizeof(*ctx
->internal
));
189 if (hw_type
->frames_priv_size
) {
190 ctx
->internal
->priv
= av_mallocz(hw_type
->frames_priv_size
);
191 if (!ctx
->internal
->priv
)
195 if (hw_type
->frames_hwctx_size
) {
196 ctx
->hwctx
= av_mallocz(hw_type
->frames_hwctx_size
);
201 device_ref
= av_buffer_ref(device_ref_in
);
205 buf
= av_buffer_create((uint8_t*)ctx
, sizeof(*ctx
),
206 hwframe_ctx_free
, NULL
,
207 AV_BUFFER_FLAG_READONLY
);
211 ctx
->av_class
= &hwframe_ctx_class
;
212 ctx
->device_ref
= device_ref
;
213 ctx
->device_ctx
= device_ctx
;
214 ctx
->format
= AV_PIX_FMT_NONE
;
215 ctx
->sw_format
= AV_PIX_FMT_NONE
;
217 ctx
->internal
->hw_type
= hw_type
;
223 av_buffer_unref(&device_ref
);
225 av_freep(&ctx
->internal
->priv
);
226 av_freep(&ctx
->internal
);
227 av_freep(&ctx
->hwctx
);
232 static int hwframe_pool_prealloc(AVBufferRef
*ref
)
234 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)ref
->data
;
238 frames
= av_mallocz_array(ctx
->initial_pool_size
, sizeof(*frames
));
240 return AVERROR(ENOMEM
);
242 for (i
= 0; i
< ctx
->initial_pool_size
; i
++) {
243 frames
[i
] = av_frame_alloc();
247 ret
= av_hwframe_get_buffer(ref
, frames
[i
], 0);
253 for (i
= 0; i
< ctx
->initial_pool_size
; i
++)
254 av_frame_free(&frames
[i
]);
260 int av_hwframe_ctx_init(AVBufferRef
*ref
)
262 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)ref
->data
;
263 const enum AVPixelFormat
*pix_fmt
;
266 /* validate the pixel format */
267 for (pix_fmt
= ctx
->internal
->hw_type
->pix_fmts
; *pix_fmt
!= AV_PIX_FMT_NONE
; pix_fmt
++) {
268 if (*pix_fmt
== ctx
->format
)
271 if (*pix_fmt
== AV_PIX_FMT_NONE
) {
272 av_log(ctx
, AV_LOG_ERROR
,
273 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
274 av_get_pix_fmt_name(ctx
->format
), ctx
->internal
->hw_type
->name
);
275 return AVERROR(ENOSYS
);
278 /* validate the dimensions */
279 ret
= av_image_check_size(ctx
->width
, ctx
->height
, 0, ctx
);
283 /* format-specific init */
284 if (ctx
->internal
->hw_type
->frames_init
) {
285 ret
= ctx
->internal
->hw_type
->frames_init(ctx
);
290 if (ctx
->internal
->pool_internal
&& !ctx
->pool
)
291 ctx
->pool
= ctx
->internal
->pool_internal
;
293 /* preallocate the frames in the pool, if requested */
294 if (ctx
->initial_pool_size
> 0) {
295 ret
= hwframe_pool_prealloc(ref
);
302 if (ctx
->internal
->hw_type
->frames_uninit
)
303 ctx
->internal
->hw_type
->frames_uninit(ctx
);
307 int av_hwframe_transfer_get_formats(AVBufferRef
*hwframe_ref
,
308 enum AVHWFrameTransferDirection dir
,
309 enum AVPixelFormat
**formats
, int flags
)
311 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)hwframe_ref
->data
;
313 if (!ctx
->internal
->hw_type
->transfer_get_formats
)
314 return AVERROR(ENOSYS
);
316 return ctx
->internal
->hw_type
->transfer_get_formats(ctx
, dir
, formats
);
319 static int transfer_data_alloc(AVFrame
*dst
, const AVFrame
*src
, int flags
)
324 frame_tmp
= av_frame_alloc();
326 return AVERROR(ENOMEM
);
328 /* if the format is set, use that
329 * otherwise pick the first supported one */
330 if (dst
->format
>= 0) {
331 frame_tmp
->format
= dst
->format
;
333 enum AVPixelFormat
*formats
;
335 ret
= av_hwframe_transfer_get_formats(src
->hw_frames_ctx
,
336 AV_HWFRAME_TRANSFER_DIRECTION_FROM
,
340 frame_tmp
->format
= formats
[0];
343 frame_tmp
->width
= src
->width
;
344 frame_tmp
->height
= src
->height
;
346 ret
= av_frame_get_buffer(frame_tmp
, 32);
350 ret
= av_hwframe_transfer_data(frame_tmp
, src
, flags
);
354 av_frame_move_ref(dst
, frame_tmp
);
357 av_frame_free(&frame_tmp
);
361 int av_hwframe_transfer_data(AVFrame
*dst
, const AVFrame
*src
, int flags
)
363 AVHWFramesContext
*ctx
;
367 return transfer_data_alloc(dst
, src
, flags
);
369 if (src
->hw_frames_ctx
) {
370 ctx
= (AVHWFramesContext
*)src
->hw_frames_ctx
->data
;
372 ret
= ctx
->internal
->hw_type
->transfer_data_from(ctx
, dst
, src
);
375 } else if (dst
->hw_frames_ctx
) {
376 ctx
= (AVHWFramesContext
*)dst
->hw_frames_ctx
->data
;
378 ret
= ctx
->internal
->hw_type
->transfer_data_to(ctx
, dst
, src
);
382 return AVERROR(ENOSYS
);
387 int av_hwframe_get_buffer(AVBufferRef
*hwframe_ref
, AVFrame
*frame
, int flags
)
389 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)hwframe_ref
->data
;
392 if (!ctx
->internal
->hw_type
->frames_get_buffer
)
393 return AVERROR(ENOSYS
);
396 return AVERROR(EINVAL
);
398 frame
->hw_frames_ctx
= av_buffer_ref(hwframe_ref
);
399 if (!frame
->hw_frames_ctx
)
400 return AVERROR(ENOMEM
);
402 ret
= ctx
->internal
->hw_type
->frames_get_buffer(ctx
, frame
);
404 av_buffer_unref(&frame
->hw_frames_ctx
);
411 void *av_hwdevice_hwconfig_alloc(AVBufferRef
*ref
)
413 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
414 const HWContextType
*hw_type
= ctx
->internal
->hw_type
;
416 if (hw_type
->device_hwconfig_size
== 0)
419 return av_mallocz(hw_type
->device_hwconfig_size
);
422 AVHWFramesConstraints
*av_hwdevice_get_hwframe_constraints(AVBufferRef
*ref
,
423 const void *hwconfig
)
425 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
426 const HWContextType
*hw_type
= ctx
->internal
->hw_type
;
427 AVHWFramesConstraints
*constraints
;
429 if (!hw_type
->frames_get_constraints
)
432 constraints
= av_mallocz(sizeof(*constraints
));
436 constraints
->min_width
= constraints
->min_height
= 0;
437 constraints
->max_width
= constraints
->max_height
= INT_MAX
;
439 if (hw_type
->frames_get_constraints(ctx
, hwconfig
, constraints
) >= 0) {
442 av_hwframe_constraints_free(&constraints
);
447 void av_hwframe_constraints_free(AVHWFramesConstraints
**constraints
)
450 av_freep(&(*constraints
)->valid_hw_formats
);
451 av_freep(&(*constraints
)->valid_sw_formats
);
453 av_freep(constraints
);
456 int av_hwdevice_ctx_create(AVBufferRef
**pdevice_ref
, enum AVHWDeviceType type
,
457 const char *device
, AVDictionary
*opts
, int flags
)
459 AVBufferRef
*device_ref
= NULL
;
460 AVHWDeviceContext
*device_ctx
;
463 device_ref
= av_hwdevice_ctx_alloc(type
);
465 ret
= AVERROR(ENOMEM
);
468 device_ctx
= (AVHWDeviceContext
*)device_ref
->data
;
470 if (!device_ctx
->internal
->hw_type
->device_create
) {
471 ret
= AVERROR(ENOSYS
);
475 ret
= device_ctx
->internal
->hw_type
->device_create(device_ctx
, device
,
480 ret
= av_hwdevice_ctx_init(device_ref
);
484 *pdevice_ref
= device_ref
;
487 av_buffer_unref(&device_ref
);