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