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_vdpau
,
41 static const AVClass hwdevice_ctx_class
= {
42 .class_name
= "AVHWDeviceContext",
43 .item_name
= av_default_item_name
,
44 .version
= LIBAVUTIL_VERSION_INT
,
47 static void hwdevice_ctx_free(void *opaque
, uint8_t *data
)
49 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)data
;
51 /* uninit might still want access the hw context and the user
52 * free() callback might destroy it, so uninit has to be called first */
53 if (ctx
->internal
->hw_type
->device_uninit
)
54 ctx
->internal
->hw_type
->device_uninit(ctx
);
59 av_freep(&ctx
->hwctx
);
60 av_freep(&ctx
->internal
->priv
);
61 av_freep(&ctx
->internal
);
65 AVBufferRef
*av_hwdevice_ctx_alloc(enum AVHWDeviceType type
)
67 AVHWDeviceContext
*ctx
;
69 const HWContextType
*hw_type
= NULL
;
72 for (i
= 0; hw_table
[i
]; i
++) {
73 if (hw_table
[i
]->type
== type
) {
74 hw_type
= hw_table
[i
];
81 ctx
= av_mallocz(sizeof(*ctx
));
85 ctx
->internal
= av_mallocz(sizeof(*ctx
->internal
));
89 if (hw_type
->device_priv_size
) {
90 ctx
->internal
->priv
= av_mallocz(hw_type
->device_priv_size
);
91 if (!ctx
->internal
->priv
)
95 if (hw_type
->device_hwctx_size
) {
96 ctx
->hwctx
= av_mallocz(hw_type
->device_hwctx_size
);
101 buf
= av_buffer_create((uint8_t*)ctx
, sizeof(*ctx
),
102 hwdevice_ctx_free
, NULL
,
103 AV_BUFFER_FLAG_READONLY
);
108 ctx
->av_class
= &hwdevice_ctx_class
;
110 ctx
->internal
->hw_type
= hw_type
;
116 av_freep(&ctx
->internal
->priv
);
117 av_freep(&ctx
->internal
);
118 av_freep(&ctx
->hwctx
);
123 int av_hwdevice_ctx_init(AVBufferRef
*ref
)
125 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
128 if (ctx
->internal
->hw_type
->device_init
) {
129 ret
= ctx
->internal
->hw_type
->device_init(ctx
);
136 if (ctx
->internal
->hw_type
->device_uninit
)
137 ctx
->internal
->hw_type
->device_uninit(ctx
);
141 static const AVClass hwframe_ctx_class
= {
142 .class_name
= "AVHWFramesContext",
143 .item_name
= av_default_item_name
,
144 .version
= LIBAVUTIL_VERSION_INT
,
147 static void hwframe_ctx_free(void *opaque
, uint8_t *data
)
149 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)data
;
151 if (ctx
->internal
->pool_internal
)
152 av_buffer_pool_uninit(&ctx
->internal
->pool_internal
);
154 if (ctx
->internal
->hw_type
->frames_uninit
)
155 ctx
->internal
->hw_type
->frames_uninit(ctx
);
160 av_buffer_unref(&ctx
->device_ref
);
162 av_freep(&ctx
->hwctx
);
163 av_freep(&ctx
->internal
->priv
);
164 av_freep(&ctx
->internal
);
168 AVBufferRef
*av_hwframe_ctx_alloc(AVBufferRef
*device_ref_in
)
170 AVHWDeviceContext
*device_ctx
= (AVHWDeviceContext
*)device_ref_in
->data
;
171 const HWContextType
*hw_type
= device_ctx
->internal
->hw_type
;
172 AVHWFramesContext
*ctx
;
173 AVBufferRef
*buf
, *device_ref
= NULL
;;
175 ctx
= av_mallocz(sizeof(*ctx
));
179 ctx
->internal
= av_mallocz(sizeof(*ctx
->internal
));
183 if (hw_type
->frames_priv_size
) {
184 ctx
->internal
->priv
= av_mallocz(hw_type
->frames_priv_size
);
185 if (!ctx
->internal
->priv
)
189 if (hw_type
->frames_hwctx_size
) {
190 ctx
->hwctx
= av_mallocz(hw_type
->frames_hwctx_size
);
195 device_ref
= av_buffer_ref(device_ref_in
);
199 buf
= av_buffer_create((uint8_t*)ctx
, sizeof(*ctx
),
200 hwframe_ctx_free
, NULL
,
201 AV_BUFFER_FLAG_READONLY
);
205 ctx
->av_class
= &hwframe_ctx_class
;
206 ctx
->device_ref
= device_ref
;
207 ctx
->device_ctx
= device_ctx
;
208 ctx
->format
= AV_PIX_FMT_NONE
;
210 ctx
->internal
->hw_type
= hw_type
;
216 av_buffer_unref(&device_ref
);
218 av_freep(&ctx
->internal
->priv
);
219 av_freep(&ctx
->internal
);
220 av_freep(&ctx
->hwctx
);
225 static int hwframe_pool_prealloc(AVBufferRef
*ref
)
227 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)ref
->data
;
231 frames
= av_mallocz_array(ctx
->initial_pool_size
, sizeof(*frames
));
233 return AVERROR(ENOMEM
);
235 for (i
= 0; i
< ctx
->initial_pool_size
; i
++) {
236 frames
[i
] = av_frame_alloc();
240 ret
= av_hwframe_get_buffer(ref
, frames
[i
], 0);
246 for (i
= 0; i
< ctx
->initial_pool_size
; i
++)
247 av_frame_free(&frames
[i
]);
253 int av_hwframe_ctx_init(AVBufferRef
*ref
)
255 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)ref
->data
;
256 const enum AVPixelFormat
*pix_fmt
;
259 /* validate the pixel format */
260 for (pix_fmt
= ctx
->internal
->hw_type
->pix_fmts
; *pix_fmt
!= AV_PIX_FMT_NONE
; pix_fmt
++) {
261 if (*pix_fmt
== ctx
->format
)
264 if (*pix_fmt
== AV_PIX_FMT_NONE
) {
265 av_log(ctx
, AV_LOG_ERROR
,
266 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
267 av_get_pix_fmt_name(ctx
->format
), ctx
->internal
->hw_type
->name
);
268 return AVERROR(ENOSYS
);
271 /* validate the dimensions */
272 ret
= av_image_check_size(ctx
->width
, ctx
->height
, 0, ctx
);
276 /* format-specific init */
277 if (ctx
->internal
->hw_type
->frames_init
) {
278 ret
= ctx
->internal
->hw_type
->frames_init(ctx
);
283 if (ctx
->internal
->pool_internal
&& !ctx
->pool
)
284 ctx
->pool
= ctx
->internal
->pool_internal
;
286 /* preallocate the frames in the pool, if requested */
287 if (ctx
->initial_pool_size
> 0) {
288 ret
= hwframe_pool_prealloc(ref
);
295 if (ctx
->internal
->hw_type
->frames_uninit
)
296 ctx
->internal
->hw_type
->frames_uninit(ctx
);
300 int av_hwframe_transfer_get_formats(AVBufferRef
*hwframe_ref
,
301 enum AVHWFrameTransferDirection dir
,
302 enum AVPixelFormat
**formats
, int flags
)
304 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)hwframe_ref
->data
;
306 if (!ctx
->internal
->hw_type
->transfer_get_formats
)
307 return AVERROR(ENOSYS
);
309 return ctx
->internal
->hw_type
->transfer_get_formats(ctx
, dir
, formats
);
312 static int transfer_data_alloc(AVFrame
*dst
, const AVFrame
*src
, int flags
)
317 frame_tmp
= av_frame_alloc();
319 return AVERROR(ENOMEM
);
321 /* if the format is set, use that
322 * otherwise pick the first supported one */
323 if (dst
->format
>= 0) {
324 frame_tmp
->format
= dst
->format
;
326 enum AVPixelFormat
*formats
;
328 ret
= av_hwframe_transfer_get_formats(src
->hw_frames_ctx
,
329 AV_HWFRAME_TRANSFER_DIRECTION_FROM
,
333 frame_tmp
->format
= formats
[0];
336 frame_tmp
->width
= src
->width
;
337 frame_tmp
->height
= src
->height
;
339 ret
= av_frame_get_buffer(frame_tmp
, 32);
343 ret
= av_hwframe_transfer_data(frame_tmp
, src
, flags
);
347 av_frame_move_ref(dst
, frame_tmp
);
350 av_frame_free(&frame_tmp
);
354 int av_hwframe_transfer_data(AVFrame
*dst
, const AVFrame
*src
, int flags
)
356 AVHWFramesContext
*ctx
;
360 return transfer_data_alloc(dst
, src
, flags
);
362 if (src
->hw_frames_ctx
) {
363 ctx
= (AVHWFramesContext
*)src
->hw_frames_ctx
->data
;
365 ret
= ctx
->internal
->hw_type
->transfer_data_from(ctx
, dst
, src
);
368 } else if (dst
->hw_frames_ctx
) {
369 ctx
= (AVHWFramesContext
*)dst
->hw_frames_ctx
->data
;
371 ret
= ctx
->internal
->hw_type
->transfer_data_to(ctx
, dst
, src
);
375 return AVERROR(ENOSYS
);
380 int av_hwframe_get_buffer(AVBufferRef
*hwframe_ref
, AVFrame
*frame
, int flags
)
382 AVHWFramesContext
*ctx
= (AVHWFramesContext
*)hwframe_ref
->data
;
385 if (!ctx
->internal
->hw_type
->frames_get_buffer
)
386 return AVERROR(ENOSYS
);
389 return AVERROR(EINVAL
);
391 frame
->hw_frames_ctx
= av_buffer_ref(hwframe_ref
);
392 if (!frame
->hw_frames_ctx
)
393 return AVERROR(ENOMEM
);
395 ret
= ctx
->internal
->hw_type
->frames_get_buffer(ctx
, frame
);
397 av_buffer_unref(&frame
->hw_frames_ctx
);
404 void *av_hwdevice_hwconfig_alloc(AVBufferRef
*ref
)
406 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
407 const HWContextType
*hw_type
= ctx
->internal
->hw_type
;
409 if (hw_type
->device_hwconfig_size
== 0)
412 return av_mallocz(hw_type
->device_hwconfig_size
);
415 AVHWFramesConstraints
*av_hwdevice_get_hwframe_constraints(AVBufferRef
*ref
,
416 const void *hwconfig
)
418 AVHWDeviceContext
*ctx
= (AVHWDeviceContext
*)ref
->data
;
419 const HWContextType
*hw_type
= ctx
->internal
->hw_type
;
420 AVHWFramesConstraints
*constraints
;
422 if (!hw_type
->frames_get_constraints
)
425 constraints
= av_mallocz(sizeof(*constraints
));
429 constraints
->min_width
= constraints
->min_height
= 0;
430 constraints
->max_width
= constraints
->max_height
= INT_MAX
;
432 if (hw_type
->frames_get_constraints(ctx
, hwconfig
, constraints
) >= 0) {
435 av_hwframe_constraints_free(&constraints
);
440 void av_hwframe_constraints_free(AVHWFramesConstraints
**constraints
)
443 av_freep(&(*constraints
)->valid_hw_formats
);
444 av_freep(&(*constraints
)->valid_sw_formats
);
446 av_freep(constraints
);