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