hwcontext: Make it easier to work with device types
[libav.git] / 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 AV_HWDEVICE_TYPE_DXVA2,
32 AV_HWDEVICE_TYPE_QSV,
33 AV_HWDEVICE_TYPE_NONE,
34 };
35
36 typedef struct AVHWDeviceInternal AVHWDeviceInternal;
37
38 /**
39 * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
40 * i.e. state that is not tied to a concrete processing configuration.
41 * E.g., in an API that supports hardware-accelerated encoding and decoding,
42 * this struct will (if possible) wrap the state that is common to both encoding
43 * and decoding and from which specific instances of encoders or decoders can be
44 * derived.
45 *
46 * This struct is reference-counted with the AVBuffer mechanism. The
47 * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
48 * points to the actual AVHWDeviceContext. Further objects derived from
49 * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
50 * specific properties) will hold an internal reference to it. After all the
51 * references are released, the AVHWDeviceContext itself will be freed,
52 * optionally invoking a user-specified callback for uninitializing the hardware
53 * state.
54 */
55 typedef struct AVHWDeviceContext {
56 /**
57 * A class for logging. Set by av_hwdevice_ctx_alloc().
58 */
59 const AVClass *av_class;
60
61 /**
62 * Private data used internally by libavutil. Must not be accessed in any
63 * way by the caller.
64 */
65 AVHWDeviceInternal *internal;
66
67 /**
68 * This field identifies the underlying API used for hardware access.
69 *
70 * This field is set when this struct is allocated and never changed
71 * afterwards.
72 */
73 enum AVHWDeviceType type;
74
75 /**
76 * The format-specific data, allocated and freed by libavutil along with
77 * this context.
78 *
79 * Should be cast by the user to the format-specific context defined in the
80 * corresponding header (hwcontext_*.h) and filled as described in the
81 * documentation before calling av_hwdevice_ctx_init().
82 *
83 * After calling av_hwdevice_ctx_init() this struct should not be modified
84 * by the caller.
85 */
86 void *hwctx;
87
88 /**
89 * This field may be set by the caller before calling av_hwdevice_ctx_init().
90 *
91 * If non-NULL, this callback will be called when the last reference to
92 * this context is unreferenced, immediately before it is freed.
93 *
94 * @note when other objects (e.g an AVHWFramesContext) are derived from this
95 * struct, this callback will be invoked after all such child objects
96 * are fully uninitialized and their respective destructors invoked.
97 */
98 void (*free)(struct AVHWDeviceContext *ctx);
99
100 /**
101 * Arbitrary user data, to be used e.g. by the free() callback.
102 */
103 void *user_opaque;
104 } AVHWDeviceContext;
105
106 typedef struct AVHWFramesInternal AVHWFramesInternal;
107
108 /**
109 * This struct describes a set or pool of "hardware" frames (i.e. those with
110 * data not located in normal system memory). All the frames in the pool are
111 * assumed to be allocated in the same way and interchangeable.
112 *
113 * This struct is reference-counted with the AVBuffer mechanism and tied to a
114 * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
115 * yields a reference, whose data field points to the actual AVHWFramesContext
116 * struct.
117 */
118 typedef struct AVHWFramesContext {
119 /**
120 * A class for logging.
121 */
122 const AVClass *av_class;
123
124 /**
125 * Private data used internally by libavutil. Must not be accessed in any
126 * way by the caller.
127 */
128 AVHWFramesInternal *internal;
129
130 /**
131 * A reference to the parent AVHWDeviceContext. This reference is owned and
132 * managed by the enclosing AVHWFramesContext, but the caller may derive
133 * additional references from it.
134 */
135 AVBufferRef *device_ref;
136
137 /**
138 * The parent AVHWDeviceContext. This is simply a pointer to
139 * device_ref->data provided for convenience.
140 *
141 * Set by libavutil in av_hwframe_ctx_init().
142 */
143 AVHWDeviceContext *device_ctx;
144
145 /**
146 * The format-specific data, allocated and freed automatically along with
147 * this context.
148 *
149 * Should be cast by the user to the format-specific context defined in the
150 * corresponding header (hwframe_*.h) and filled as described in the
151 * documentation before calling av_hwframe_ctx_init().
152 *
153 * After any frames using this context are created, the contents of this
154 * struct should not be modified by the caller.
155 */
156 void *hwctx;
157
158 /**
159 * This field may be set by the caller before calling av_hwframe_ctx_init().
160 *
161 * If non-NULL, this callback will be called when the last reference to
162 * this context is unreferenced, immediately before it is freed.
163 */
164 void (*free)(struct AVHWFramesContext *ctx);
165
166 /**
167 * Arbitrary user data, to be used e.g. by the free() callback.
168 */
169 void *user_opaque;
170
171 /**
172 * A pool from which the frames are allocated by av_hwframe_get_buffer().
173 * This field may be set by the caller before calling av_hwframe_ctx_init().
174 * The buffers returned by calling av_buffer_pool_get() on this pool must
175 * have the properties described in the documentation in the corresponding hw
176 * type's header (hwcontext_*.h). The pool will be freed strictly before
177 * this struct's free() callback is invoked.
178 *
179 * This field may be NULL, then libavutil will attempt to allocate a pool
180 * internally. Note that certain device types enforce pools allocated at
181 * fixed size (frame count), which cannot be extended dynamically. In such a
182 * case, initial_pool_size must be set appropriately.
183 */
184 AVBufferPool *pool;
185
186 /**
187 * Initial size of the frame pool. If a device type does not support
188 * dynamically resizing the pool, then this is also the maximum pool size.
189 *
190 * May be set by the caller before calling av_hwframe_ctx_init(). Must be
191 * set if pool is NULL and the device type does not support dynamic pools.
192 */
193 int initial_pool_size;
194
195 /**
196 * The pixel format identifying the underlying HW surface type.
197 *
198 * Must be a hwaccel format, i.e. the corresponding descriptor must have the
199 * AV_PIX_FMT_FLAG_HWACCEL flag set.
200 *
201 * Must be set by the user before calling av_hwframe_ctx_init().
202 */
203 enum AVPixelFormat format;
204
205 /**
206 * The pixel format identifying the actual data layout of the hardware
207 * frames.
208 *
209 * Must be set by the caller before calling av_hwframe_ctx_init().
210 *
211 * @note when the underlying API does not provide the exact data layout, but
212 * only the colorspace/bit depth, this field should be set to the fully
213 * planar version of that format (e.g. for 8-bit 420 YUV it should be
214 * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
215 */
216 enum AVPixelFormat sw_format;
217
218 /**
219 * The allocated dimensions of the frames in this pool.
220 *
221 * Must be set by the user before calling av_hwframe_ctx_init().
222 */
223 int width, height;
224 } AVHWFramesContext;
225
226 /**
227 * Look up an AVHWDeviceType by name.
228 *
229 * @param name String name of the device type (case-insensitive).
230 * @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if
231 * not found.
232 */
233 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);
234
235 /** Get the string name of an AVHWDeviceType.
236 *
237 * @param type Type from enum AVHWDeviceType.
238 * @return Pointer to a static string containing the name, or NULL if the type
239 * is not valid.
240 */
241 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);
242
243 /**
244 * Iterate over supported device types.
245 *
246 * @param type AV_HWDEVICE_TYPE_NONE initially, then the previous type
247 * returned by this function in subsequent iterations.
248 * @return The next usable device type from enum AVHWDeviceType, or
249 * AV_HWDEVICE_TYPE_NONE if there are no more.
250 */
251 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);
252
253 /**
254 * Allocate an AVHWDeviceContext for a given pixel format.
255 *
256 * @return a reference to the newly created AVHWDeviceContext on success or NULL
257 * on failure.
258 */
259 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
260
261 /**
262 * Finalize the device context before use. This function must be called after
263 * the context is filled with all the required information and before it is
264 * used in any way.
265 *
266 * @param ref a reference to the AVHWDeviceContext
267 * @return 0 on success, a negative AVERROR code on failure
268 */
269 int av_hwdevice_ctx_init(AVBufferRef *ref);
270
271 /**
272 * Open a device of the specified type and create an AVHWDeviceContext for it.
273 *
274 * This is a convenience function intended to cover the simple cases. Callers
275 * who need to fine-tune device creation/management should open the device
276 * manually and then wrap it in an AVHWDeviceContext using
277 * av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().
278 *
279 * The returned context is already initialized and ready for use, the caller
280 * should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of
281 * the created AVHWDeviceContext are set by this function and should not be
282 * touched by the caller.
283 *
284 * @param device_ctx On success, a reference to the newly-created device context
285 * will be written here. The reference is owned by the caller
286 * and must be released with av_buffer_unref() when no longer
287 * needed. On failure, NULL will be written to this pointer.
288 * @param type The type of the device to create.
289 * @param device A type-specific string identifying the device to open.
290 * @param opts A dictionary of additional (type-specific) options to use in
291 * opening the device. The dictionary remains owned by the caller.
292 * @param flags currently unused
293 *
294 * @return 0 on success, a negative AVERROR code on failure.
295 */
296 int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,
297 const char *device, AVDictionary *opts, int flags);
298
299 /**
300 * Create a new device of the specified type from an existing device.
301 *
302 * If the source device is a device of the target type or was originally
303 * derived from such a device (possibly through one or more intermediate
304 * devices of other types), then this will return a reference to the
305 * existing device of the same type as is requested.
306 *
307 * Otherwise, it will attempt to derive a new device from the given source
308 * device. If direct derivation to the new type is not implemented, it will
309 * attempt the same derivation from each ancestor of the source device in
310 * turn looking for an implemented derivation method.
311 *
312 * @param dst_ctx On success, a reference to the newly-created
313 * AVHWDeviceContext.
314 * @param type The type of the new device to create.
315 * @param src_ctx A reference to an existing AVHWDeviceContext which will be
316 * used to create the new device.
317 * @param flags Currently unused; should be set to zero.
318 * @return Zero on success, a negative AVERROR code on failure.
319 */
320 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,
321 enum AVHWDeviceType type,
322 AVBufferRef *src_ctx, int flags);
323
324
325 /**
326 * Allocate an AVHWFramesContext tied to a given device context.
327 *
328 * @param device_ctx a reference to a AVHWDeviceContext. This function will make
329 * a new reference for internal use, the one passed to the
330 * function remains owned by the caller.
331 * @return a reference to the newly created AVHWFramesContext on success or NULL
332 * on failure.
333 */
334 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
335
336 /**
337 * Finalize the context before use. This function must be called after the
338 * context is filled with all the required information and before it is attached
339 * to any frames.
340 *
341 * @param ref a reference to the AVHWFramesContext
342 * @return 0 on success, a negative AVERROR code on failure
343 */
344 int av_hwframe_ctx_init(AVBufferRef *ref);
345
346 /**
347 * Allocate a new frame attached to the given AVHWFramesContext.
348 *
349 * @param hwframe_ctx a reference to an AVHWFramesContext
350 * @param frame an empty (freshly allocated or unreffed) frame to be filled with
351 * newly allocated buffers.
352 * @param flags currently unused, should be set to zero
353 * @return 0 on success, a negative AVERROR code on failure
354 */
355 int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
356
357 /**
358 * Copy data to or from a hw surface. At least one of dst/src must have an
359 * AVHWFramesContext attached.
360 *
361 * If src has an AVHWFramesContext attached, then the format of dst (if set)
362 * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
363 * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
364 * If dst has an AVHWFramesContext attached, then the format of src must use one
365 * of the formats returned by av_hwframe_transfer_get_formats(dst,
366 * AV_HWFRAME_TRANSFER_DIRECTION_TO)
367 *
368 * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
369 * data buffers will be allocated by this function using av_frame_get_buffer().
370 * If dst->format is set, then this format will be used, otherwise (when
371 * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
372 *
373 * The two frames must have matching allocated dimensions (i.e. equal to
374 * AVHWFramesContext.width/height), since not all device types support
375 * transferring a sub-rectangle of the whole surface. The display dimensions
376 * (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but
377 * also have to be equal for both frames. When the display dimensions are
378 * smaller than the allocated dimensions, the content of the padding in the
379 * destination frame is unspecified.
380 *
381 * @param dst the destination frame. dst is not touched on failure.
382 * @param src the source frame.
383 * @param flags currently unused, should be set to zero
384 * @return 0 on success, a negative AVERROR error code on failure.
385 */
386 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
387
388 enum AVHWFrameTransferDirection {
389 /**
390 * Transfer the data from the queried hw frame.
391 */
392 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
393
394 /**
395 * Transfer the data to the queried hw frame.
396 */
397 AV_HWFRAME_TRANSFER_DIRECTION_TO,
398 };
399
400 /**
401 * Get a list of possible source or target formats usable in
402 * av_hwframe_transfer_data().
403 *
404 * @param hwframe_ctx the frame context to obtain the information for
405 * @param dir the direction of the transfer
406 * @param formats the pointer to the output format list will be written here.
407 * The list is terminated with AV_PIX_FMT_NONE and must be freed
408 * by the caller when no longer needed using av_free().
409 * If this function returns successfully, the format list will
410 * have at least one item (not counting the terminator).
411 * On failure, the contents of this pointer are unspecified.
412 * @param flags currently unused, should be set to zero
413 * @return 0 on success, a negative AVERROR code on failure.
414 */
415 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
416 enum AVHWFrameTransferDirection dir,
417 enum AVPixelFormat **formats, int flags);
418
419
420 /**
421 * This struct describes the constraints on hardware frames attached to
422 * a given device with a hardware-specific configuration. This is returned
423 * by av_hwdevice_get_hwframe_constraints() and must be freed by
424 * av_hwframe_constraints_free() after use.
425 */
426 typedef struct AVHWFramesConstraints {
427 /**
428 * A list of possible values for format in the hw_frames_ctx,
429 * terminated by AV_PIX_FMT_NONE. This member will always be filled.
430 */
431 enum AVPixelFormat *valid_hw_formats;
432
433 /**
434 * A list of possible values for sw_format in the hw_frames_ctx,
435 * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
436 * not known.
437 */
438 enum AVPixelFormat *valid_sw_formats;
439
440 /**
441 * The minimum size of frames in this hw_frames_ctx.
442 * (Zero if not known.)
443 */
444 int min_width;
445 int min_height;
446
447 /**
448 * The maximum size of frames in this hw_frames_ctx.
449 * (INT_MAX if not known / no limit.)
450 */
451 int max_width;
452 int max_height;
453 } AVHWFramesConstraints;
454
455 /**
456 * Allocate a HW-specific configuration structure for a given HW device.
457 * After use, the user must free all members as required by the specific
458 * hardware structure being used, then free the structure itself with
459 * av_free().
460 *
461 * @param device_ctx a reference to the associated AVHWDeviceContext.
462 * @return The newly created HW-specific configuration structure on
463 * success or NULL on failure.
464 */
465 void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
466
467 /**
468 * Get the constraints on HW frames given a device and the HW-specific
469 * configuration to be used with that device. If no HW-specific
470 * configuration is provided, returns the maximum possible capabilities
471 * of the device.
472 *
473 * @param ref a reference to the associated AVHWDeviceContext.
474 * @param hwconfig a filled HW-specific configuration structure, or NULL
475 * to return the maximum possible capabilities of the device.
476 * @return AVHWFramesConstraints structure describing the constraints
477 * on the device, or NULL if not available.
478 */
479 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
480 const void *hwconfig);
481
482 /**
483 * Free an AVHWFrameConstraints structure.
484 *
485 * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
486 */
487 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
488
489
490 /**
491 * Flags to apply to frame mappings.
492 */
493 enum {
494 /**
495 * The mapping must be readable.
496 */
497 AV_HWFRAME_MAP_READ = 1 << 0,
498 /**
499 * The mapping must be writeable.
500 */
501 AV_HWFRAME_MAP_WRITE = 1 << 1,
502 /**
503 * The mapped frame will be overwritten completely in subsequent
504 * operations, so the current frame data need not be loaded. Any values
505 * which are not overwritten are unspecified.
506 */
507 AV_HWFRAME_MAP_OVERWRITE = 1 << 2,
508 /**
509 * The mapping must be direct. That is, there must not be any copying in
510 * the map or unmap steps. Note that performance of direct mappings may
511 * be much lower than normal memory.
512 */
513 AV_HWFRAME_MAP_DIRECT = 1 << 3,
514 };
515
516 /**
517 * Map a hardware frame.
518 *
519 * This has a number of different possible effects, depending on the format
520 * and origin of the src and dst frames. On input, src should be a usable
521 * frame with valid buffers and dst should be blank (typically as just created
522 * by av_frame_alloc()). src should have an associated hwframe context, and
523 * dst may optionally have a format and associated hwframe context.
524 *
525 * If src was created by mapping a frame from the hwframe context of dst,
526 * then this function undoes the mapping - dst is replaced by a reference to
527 * the frame that src was originally mapped from.
528 *
529 * If both src and dst have an associated hwframe context, then this function
530 * attempts to map the src frame from its hardware context to that of dst and
531 * then fill dst with appropriate data to be usable there. This will only be
532 * possible if the hwframe contexts and associated devices are compatible -
533 * given compatible devices, av_hwframe_ctx_create_derived() can be used to
534 * create a hwframe context for dst in which mapping should be possible.
535 *
536 * If src has a hwframe context but dst does not, then the src frame is
537 * mapped to normal memory and should thereafter be usable as a normal frame.
538 * If the format is set on dst, then the mapping will attempt to create dst
539 * with that format and fail if it is not possible. If format is unset (is
540 * AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate
541 * format to use is (probably the sw_format of the src hwframe context).
542 *
543 * A return value of AVERROR(ENOSYS) indicates that the mapping is not
544 * possible with the given arguments and hwframe setup, while other return
545 * values indicate that it failed somehow.
546 *
547 * @param dst Destination frame, to contain the mapping.
548 * @param src Source frame, to be mapped.
549 * @param flags Some combination of AV_HWFRAME_MAP_* flags.
550 * @return Zero on success, negative AVERROR code on failure.
551 */
552 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);
553
554
555 /**
556 * Create and initialise an AVHWFramesContext as a mapping of another existing
557 * AVHWFramesContext on a different device.
558 *
559 * av_hwframe_ctx_init() should not be called after this.
560 *
561 * @param derived_frame_ctx On success, a reference to the newly created
562 * AVHWFramesContext.
563 * @param derived_device_ctx A reference to the device to create the new
564 * AVHWFramesContext on.
565 * @param source_frame_ctx A reference to an existing AVHWFramesContext
566 * which will be mapped to the derived context.
567 * @param flags Currently unused; should be set to zero.
568 * @return Zero on success, negative AVERROR code on failure.
569 */
570 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
571 enum AVPixelFormat format,
572 AVBufferRef *derived_device_ctx,
573 AVBufferRef *source_frame_ctx,
574 int flags);
575
576 #endif /* AVUTIL_HWCONTEXT_H */