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