lavu: add a framework for handling hwaccel frames
[libav.git] / libavutil / hwcontext.h
CommitLineData
89923e41
AK
1/*
2 * This file is part of Libav.
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef AVUTIL_HWCONTEXT_H
20#define AVUTIL_HWCONTEXT_H
21
22#include "buffer.h"
23#include "frame.h"
24#include "log.h"
25#include "pixfmt.h"
26
27enum AVHWDeviceType {
28 AV_HWDEVICE_TYPE_VDPAU,
29};
30
31typedef struct AVHWDeviceInternal AVHWDeviceInternal;
32
33/**
34 * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
35 * i.e. state that is not tied to a concrete processing configuration.
36 * E.g., in an API that supports hardware-accelerated encoding and decoding,
37 * this struct will (if possible) wrap the state that is common to both encoding
38 * and decoding and from which specific instances of encoders or decoders can be
39 * derived.
40 *
41 * This struct is reference-counted with the AVBuffer mechanism. The
42 * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
43 * points to the actual AVHWDeviceContext. Further objects derived from
44 * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
45 * specific properties) will hold an internal reference to it. After all the
46 * references are released, the AVHWDeviceContext itself will be freed,
47 * optionally invoking a user-specified callback for uninitializing the hardware
48 * state.
49 */
50typedef struct AVHWDeviceContext {
51 /**
52 * A class for logging. Set by av_hwdevice_ctx_alloc().
53 */
54 const AVClass *av_class;
55
56 /**
57 * Private data used internally by libavutil. Must not be accessed in any
58 * way by the caller.
59 */
60 AVHWDeviceInternal *internal;
61
62 /**
63 * This field identifies the underlying API used for hardware access.
64 *
65 * This field is set when this struct is allocated and never changed
66 * afterwards.
67 */
68 enum AVHWDeviceType type;
69
70 /**
71 * The format-specific data, allocated and freed by libavutil along with
72 * this context.
73 *
74 * Should be cast by the user to the format-specific context defined in the
75 * corresponding header (hwcontext_*.h) and filled as described in the
76 * documentation before calling av_hwdevice_ctx_init().
77 *
78 * After calling av_hwdevice_ctx_init() this struct should not be modified
79 * by the caller.
80 */
81 void *hwctx;
82
83 /**
84 * This field may be set by the caller before calling av_hwdevice_ctx_init().
85 *
86 * If non-NULL, this callback will be called when the last reference to
87 * this context is unreferenced, immediately before it is freed.
88 *
89 * @note when other objects (e.g an AVHWFramesContext) are derived from this
90 * struct, this callback will be invoked after all such child objects
91 * are fully uninitialized and their respective destructors invoked.
92 */
93 void (*free)(struct AVHWDeviceContext *ctx);
94
95 /**
96 * Arbitrary user data, to be used e.g. by the free() callback.
97 */
98 void *user_opaque;
99} AVHWDeviceContext;
100
101typedef struct AVHWFramesInternal AVHWFramesInternal;
102
103/**
104 * This struct describes a set or pool of "hardware" frames (i.e. those with
105 * data not located in normal system memory). All the frames in the pool are
106 * assumed to be allocated in the same way and interchangeable.
107 *
108 * This struct is reference-counted with the AVBuffer mechanism and tied to a
109 * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
110 * yields a reference, whose data field points to the actual AVHWFramesContext
111 * struct.
112 */
113typedef struct AVHWFramesContext {
114 /**
115 * A class for logging.
116 */
117 const AVClass *av_class;
118
119 /**
120 * Private data used internally by libavutil. Must not be accessed in any
121 * way by the caller.
122 */
123 AVHWFramesInternal *internal;
124
125 /**
126 * A reference to the parent AVHWDeviceContext. This reference is owned and
127 * managed by the enclosing AVHWFramesContext, but the caller may derive
128 * additional references from it.
129 */
130 AVBufferRef *device_ref;
131
132 /**
133 * The parent AVHWDeviceContext. This is simply a pointer to
134 * device_ref->data provided for convenience.
135 *
136 * Set by libavutil in av_hwframe_ctx_init().
137 */
138 AVHWDeviceContext *device_ctx;
139
140 /**
141 * The format-specific data, allocated and freed automatically along with
142 * this context.
143 *
144 * Should be cast by the user to the format-specific context defined in the
145 * corresponding header (hwframe_*.h) and filled as described in the
146 * documentation before calling av_hwframe_ctx_init().
147 *
148 * After any frames using this context are created, the contents of this
149 * struct should not be modified by the caller.
150 */
151 void *hwctx;
152
153 /**
154 * This field may be set by the caller before calling av_hwframe_ctx_init().
155 *
156 * If non-NULL, this callback will be called when the last reference to
157 * this context is unreferenced, immediately before it is freed.
158 */
159 void (*free)(struct AVHWFramesContext *ctx);
160
161 /**
162 * Arbitrary user data, to be used e.g. by the free() callback.
163 */
164 void *user_opaque;
165
166 /**
167 * A pool from which the frames are allocated by av_hwframe_get_buffer().
168 * This field may be set by the caller before calling av_hwframe_ctx_init().
169 * The buffers returned by calling av_buffer_pool_get() on this pool must
170 * have the properties described in the documentation in the correponding hw
171 * type's header (hwcontext_*.h). The pool will be freed strictly before
172 * this struct's free() callback is invoked.
173 *
174 * This field may be NULL, then libavutil will attempt to allocate a pool
175 * internally. Note that certain device types enforce pools allocated at
176 * fixed size (frame count), which cannot be extended dynamically. In such a
177 * case, initial_pool_size must be set appropriately.
178 */
179 AVBufferPool *pool;
180
181 /**
182 * Initial size of the frame pool. If a device type does not support
183 * dynamically resizing the pool, then this is also the maximum pool size.
184 *
185 * May be set by the caller before calling av_hwframe_ctx_init(). Must be
186 * set if pool is NULL and the device type does not support dynamic pools.
187 */
188 int initial_pool_size;
189
190 /**
191 * The pixel format identifying the underlying HW surface type.
192 *
193 * Must be a hwaccel format, i.e. the corresponding descriptor must have the
194 * AV_PIX_FMT_FLAG_HWACCEL flag set.
195 *
196 * Must be set by the user before calling av_hwframe_ctx_init().
197 */
198 enum AVPixelFormat format;
199
200 /**
201 * The pixel format identifying the actual data layout of the hardware
202 * frames.
203 *
204 * Must be set by the caller before calling av_hwframe_ctx_init().
205 *
206 * @note when the underlying API does not provide the exact data layout, but
207 * only the colorspace/bit depth, this field should be set to the fully
208 * planar version of that format (e.g. for 8-bit 420 YUV it should be
209 * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
210 */
211 enum AVPixelFormat sw_format;
212
213 /**
214 * The allocated dimensions of the frames in this pool.
215 *
216 * Must be set by the user before calling av_hwframe_ctx_init().
217 */
218 int width, height;
219} AVHWFramesContext;
220
221/**
222 * Allocate an AVHWDeviceContext for a given pixel format.
223 *
224 * @param format a hwaccel pixel format (AV_PIX_FMT_FLAG_HWACCEL must be set
225 * on the corresponding format descriptor)
226 * @return a reference to the newly created AVHWDeviceContext on success or NULL
227 * on failure.
228 */
229AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
230
231/**
232 * Finalize the device context before use. This function must be called after
233 * the context is filled with all the required information and before it is
234 * used in any way.
235 *
236 * @param ref a reference to the AVHWDeviceContext
237 * @return 0 on success, a negative AVERROR code on failure
238 */
239int av_hwdevice_ctx_init(AVBufferRef *ref);
240
241/**
242 * Allocate an AVHWFramesContext tied to a given device context.
243 *
244 * @param device_ctx a reference to a AVHWDeviceContext. This function will make
245 * a new reference for internal use, the one passed to the
246 * function remains owned by the caller.
247 * @return a reference to the newly created AVHWFramesContext on success or NULL
248 * on failure.
249 */
250AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
251
252/**
253 * Finalize the context before use. This function must be called after the
254 * context is filled with all the required information and before it is attached
255 * to any frames.
256 *
257 * @param ref a reference to the AVHWFramesContext
258 * @return 0 on success, a negative AVERROR code on failure
259 */
260int av_hwframe_ctx_init(AVBufferRef *ref);
261
262/**
263 * Allocate a new frame attached to the given AVHWFramesContext.
264 *
265 * @param hwframe_ctx a reference to an AVHWFramesContext
266 * @param frame an empty (freshly allocated or unreffed) frame to be filled with
267 * newly allocated buffers.
268 * @param flags currently unused, should be set to zero
269 * @return 0 on success, a negative AVERROR code on failure
270 */
271int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
272
273/**
274 * Copy data to or from a hw surface. At least one of dst/src must have an
275 * AVHWFramesContext attached.
276 *
277 * If src has an AVHWFramesContext attached, then the format of dst (if set)
278 * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
279 * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
280 * If dst has an AVHWFramesContext attached, then the format of src must use one
281 * of the formats returned by av_hwframe_transfer_get_formats(dst,
282 * AV_HWFRAME_TRANSFER_DIRECTION_TO)
283 *
284 * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
285 * data buffers will be allocated by this function using av_frame_get_buffer().
286 * If dst->format is set, then this format will be used, otherwise (when
287 * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
288 *
289 * @param dst the destination frame. dst is not touched on failure.
290 * @param src the source frame.
291 * @param flags currently unused, should be set to zero
292 * @return 0 on success, a negative AVERROR error code on failure.
293 */
294int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
295
296enum AVHWFrameTransferDirection {
297 /**
298 * Transfer the data from the queried hw frame.
299 */
300 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
301
302 /**
303 * Transfer the data to the queried hw frame.
304 */
305 AV_HWFRAME_TRANSFER_DIRECTION_TO,
306};
307
308/**
309 * Get a list of possible source or target formats usable in
310 * av_hwframe_transfer_data().
311 *
312 * @param hwframe_ctx the frame context to obtain the information for
313 * @param dir the direction of the transfer
314 * @param formats the pointer to the output format list will be written here.
315 * The list is terminated with AV_PIX_FMT_NONE and must be freed
316 * by the caller when no longer needed using av_free().
317 * If this function returns successfully, the format list will
318 * have at least one item (not counting the terminator).
319 * On failure, the contents of this pointer are unspecified.
320 * @param flags currently unused, should be set to zero
321 * @return 0 on success, a negative AVERROR code on failure.
322 */
323int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
324 enum AVHWFrameTransferDirection dir,
325 enum AVPixelFormat **formats, int flags);
326
327
328#endif /* AVUTIL_HWCONTEXT_H */