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