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