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