1 /* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 #ifndef VPX_VPX_ENCODER_H_ 11 #define VPX_VPX_ENCODER_H_ 12 13 /*!\defgroup encoder Encoder Algorithm Interface 14 * \ingroup codec 15 * This abstraction allows applications using this encoder to easily support 16 * multiple video formats with minimal code duplication. This section describes 17 * the interface common to all encoders. 18 * @{ 19 */ 20 21 /*!\file 22 * \brief Describes the encoder algorithm interface to applications. 23 * 24 * This file describes the interface between an application and a 25 * video encoder algorithm. 26 * 27 */ 28 #ifdef __cplusplus 29 extern "C" { 30 #endif 31 32 #include "./vpx_codec.h" 33 34 /*! Temporal Scalability: Maximum length of the sequence defining frame 35 * layer membership 36 */ 37 #define VPX_TS_MAX_PERIODICITY 16 38 39 /*! Temporal Scalability: Maximum number of coding layers */ 40 #define VPX_TS_MAX_LAYERS 5 41 42 /*!\deprecated Use #VPX_TS_MAX_PERIODICITY instead. */ 43 #define MAX_PERIODICITY VPX_TS_MAX_PERIODICITY 44 45 /*! Temporal+Spatial Scalability: Maximum number of coding layers */ 46 #define VPX_MAX_LAYERS 12 // 3 temporal + 4 spatial layers are allowed. 47 48 /*!\deprecated Use #VPX_MAX_LAYERS instead. */ 49 #define MAX_LAYERS VPX_MAX_LAYERS // 3 temporal + 4 spatial layers allowed. 50 51 /*! Spatial Scalability: Maximum number of coding layers */ 52 #define VPX_SS_MAX_LAYERS 5 53 54 /*! Spatial Scalability: Default number of coding layers */ 55 #define VPX_SS_DEFAULT_LAYERS 1 56 57 /*!\brief Current ABI version number 58 * 59 * \internal 60 * If this file is altered in any way that changes the ABI, this value 61 * must be bumped. Examples include, but are not limited to, changing 62 * types, removing or reassigning enums, adding/removing/rearranging 63 * fields to structures 64 */ 65 #define VPX_ENCODER_ABI_VERSION \ 66 (6 + VPX_CODEC_ABI_VERSION) /**<\hideinitializer*/ 67 68 /*! \brief Encoder capabilities bitfield 69 * 70 * Each encoder advertises the capabilities it supports as part of its 71 * ::vpx_codec_iface_t interface structure. Capabilities are extra 72 * interfaces or functionality, and are not required to be supported 73 * by an encoder. 74 * 75 * The available flags are specified by VPX_CODEC_CAP_* defines. 76 */ 77 #define VPX_CODEC_CAP_PSNR 0x10000 /**< Can issue PSNR packets */ 78 79 /*! Can output one partition at a time. Each partition is returned in its 80 * own VPX_CODEC_CX_FRAME_PKT, with the FRAME_IS_FRAGMENT flag set for 81 * every partition but the last. In this mode all frames are always 82 * returned partition by partition. 83 */ 84 #define VPX_CODEC_CAP_OUTPUT_PARTITION 0x20000 85 86 /*! \brief Initialization-time Feature Enabling 87 * 88 * Certain codec features must be known at initialization time, to allow 89 * for proper memory allocation. 90 * 91 * The available flags are specified by VPX_CODEC_USE_* defines. 92 */ 93 #define VPX_CODEC_USE_PSNR 0x10000 /**< Calculate PSNR on each frame */ 94 /*!\brief Make the encoder output one partition at a time. */ 95 #define VPX_CODEC_USE_OUTPUT_PARTITION 0x20000 96 #define VPX_CODEC_USE_HIGHBITDEPTH 0x40000 /**< Use high bitdepth */ 97 98 /*!\brief Generic fixed size buffer structure 99 * 100 * This structure is able to hold a reference to any fixed size buffer. 101 */ 102 typedef struct vpx_fixed_buf { 103 void *buf; /**< Pointer to the data */ 104 size_t sz; /**< Length of the buffer, in chars */ 105 } vpx_fixed_buf_t; /**< alias for struct vpx_fixed_buf */ 106 107 /*!\brief Time Stamp Type 108 * 109 * An integer, which when multiplied by the stream's time base, provides 110 * the absolute time of a sample. 111 */ 112 typedef int64_t vpx_codec_pts_t; 113 114 /*!\brief Compressed Frame Flags 115 * 116 * This type represents a bitfield containing information about a compressed 117 * frame that may be useful to an application. The most significant 16 bits 118 * can be used by an algorithm to provide additional detail, for example to 119 * support frame types that are codec specific (MPEG-1 D-frames for example) 120 */ 121 typedef uint32_t vpx_codec_frame_flags_t; 122 #define VPX_FRAME_IS_KEY 0x1 /**< frame is the start of a GOP */ 123 /*!\brief frame can be dropped without affecting the stream (no future frame 124 * depends on this one) */ 125 #define VPX_FRAME_IS_DROPPABLE 0x2 126 /*!\brief frame should be decoded but will not be shown */ 127 #define VPX_FRAME_IS_INVISIBLE 0x4 128 /*!\brief this is a fragment of the encoded frame */ 129 #define VPX_FRAME_IS_FRAGMENT 0x8 130 131 /*!\brief Error Resilient flags 132 * 133 * These flags define which error resilient features to enable in the 134 * encoder. The flags are specified through the 135 * vpx_codec_enc_cfg::g_error_resilient variable. 136 */ 137 typedef uint32_t vpx_codec_er_flags_t; 138 /*!\brief Improve resiliency against losses of whole frames */ 139 #define VPX_ERROR_RESILIENT_DEFAULT 0x1 140 /*!\brief The frame partitions are independently decodable by the bool decoder, 141 * meaning that partitions can be decoded even though earlier partitions have 142 * been lost. Note that intra prediction is still done over the partition 143 * boundary. */ 144 #define VPX_ERROR_RESILIENT_PARTITIONS 0x2 145 146 /*!\brief Encoder output packet variants 147 * 148 * This enumeration lists the different kinds of data packets that can be 149 * returned by calls to vpx_codec_get_cx_data(). Algorithms \ref MAY 150 * extend this list to provide additional functionality. 151 */ 152 enum vpx_codec_cx_pkt_kind { 153 VPX_CODEC_CX_FRAME_PKT, /**< Compressed video frame */ 154 VPX_CODEC_STATS_PKT, /**< Two-pass statistics for this frame */ 155 VPX_CODEC_FPMB_STATS_PKT, /**< first pass mb statistics for this frame */ 156 VPX_CODEC_PSNR_PKT, /**< PSNR statistics for this frame */ 157 // Spatial SVC is still experimental and may be removed. 158 #if defined(VPX_TEST_SPATIAL_SVC) 159 VPX_CODEC_SPATIAL_SVC_LAYER_SIZES, /**< Sizes for each layer in this frame*/ 160 VPX_CODEC_SPATIAL_SVC_LAYER_PSNR, /**< PSNR for each layer in this frame*/ 161 #endif 162 VPX_CODEC_CUSTOM_PKT = 256 /**< Algorithm extensions */ 163 }; 164 165 /*!\brief Encoder output packet 166 * 167 * This structure contains the different kinds of output data the encoder 168 * may produce while compressing a frame. 169 */ 170 typedef struct vpx_codec_cx_pkt { 171 enum vpx_codec_cx_pkt_kind kind; /**< packet variant */ 172 union { 173 struct { 174 void *buf; /**< compressed data buffer */ 175 size_t sz; /**< length of compressed data */ 176 /*!\brief time stamp to show frame (in timebase units) */ 177 vpx_codec_pts_t pts; 178 /*!\brief duration to show frame (in timebase units) */ 179 unsigned long duration; 180 vpx_codec_frame_flags_t flags; /**< flags for this frame */ 181 /*!\brief the partition id defines the decoding order of the partitions. 182 * Only applicable when "output partition" mode is enabled. First 183 * partition has id 0.*/ 184 int partition_id; 185 } frame; /**< data for compressed frame packet */ 186 vpx_fixed_buf_t twopass_stats; /**< data for two-pass packet */ 187 vpx_fixed_buf_t firstpass_mb_stats; /**< first pass mb packet */ 188 struct vpx_psnr_pkt { 189 unsigned int samples[4]; /**< Number of samples, total/y/u/v */ 190 uint64_t sse[4]; /**< sum squared error, total/y/u/v */ 191 double psnr[4]; /**< PSNR, total/y/u/v */ 192 } psnr; /**< data for PSNR packet */ 193 vpx_fixed_buf_t raw; /**< data for arbitrary packets */ 194 // Spatial SVC is still experimental and may be removed. 195 #if defined(VPX_TEST_SPATIAL_SVC) 196 size_t layer_sizes[VPX_SS_MAX_LAYERS]; 197 struct vpx_psnr_pkt layer_psnr[VPX_SS_MAX_LAYERS]; 198 #endif 199 200 /* This packet size is fixed to allow codecs to extend this 201 * interface without having to manage storage for raw packets, 202 * i.e., if it's smaller than 128 bytes, you can store in the 203 * packet list directly. 204 */ 205 char pad[128 - sizeof(enum vpx_codec_cx_pkt_kind)]; /**< fixed sz */ 206 } data; /**< packet data */ 207 } vpx_codec_cx_pkt_t; /**< alias for struct vpx_codec_cx_pkt */ 208 209 /*!\brief Encoder return output buffer callback 210 * 211 * This callback function, when registered, returns with packets when each 212 * spatial layer is encoded. 213 */ 214 // putting the definitions here for now. (agrange: find if there 215 // is a better place for this) 216 typedef void (*vpx_codec_enc_output_cx_pkt_cb_fn_t)(vpx_codec_cx_pkt_t *pkt, 217 void *user_data); 218 219 /*!\brief Callback function pointer / user data pair storage */ 220 typedef struct vpx_codec_enc_output_cx_cb_pair { 221 vpx_codec_enc_output_cx_pkt_cb_fn_t output_cx_pkt; /**< Callback function */ 222 void *user_priv; /**< Pointer to private data */ 223 } vpx_codec_priv_output_cx_pkt_cb_pair_t; 224 225 /*!\brief Rational Number 226 * 227 * This structure holds a fractional value. 228 */ 229 typedef struct vpx_rational { 230 int num; /**< fraction numerator */ 231 int den; /**< fraction denominator */ 232 } vpx_rational_t; /**< alias for struct vpx_rational */ 233 234 /*!\brief Multi-pass Encoding Pass */ 235 enum vpx_enc_pass { 236 VPX_RC_ONE_PASS, /**< Single pass mode */ 237 VPX_RC_FIRST_PASS, /**< First pass of multi-pass mode */ 238 VPX_RC_LAST_PASS /**< Final pass of multi-pass mode */ 239 }; 240 241 /*!\brief Rate control mode */ 242 enum vpx_rc_mode { 243 VPX_VBR, /**< Variable Bit Rate (VBR) mode */ 244 VPX_CBR, /**< Constant Bit Rate (CBR) mode */ 245 VPX_CQ, /**< Constrained Quality (CQ) mode */ 246 VPX_Q, /**< Constant Quality (Q) mode */ 247 }; 248 249 /*!\brief Keyframe placement mode. 250 * 251 * This enumeration determines whether keyframes are placed automatically by 252 * the encoder or whether this behavior is disabled. Older releases of this 253 * SDK were implemented such that VPX_KF_FIXED meant keyframes were disabled. 254 * This name is confusing for this behavior, so the new symbols to be used 255 * are VPX_KF_AUTO and VPX_KF_DISABLED. 256 */ 257 enum vpx_kf_mode { 258 VPX_KF_FIXED, /**< deprecated, implies VPX_KF_DISABLED */ 259 VPX_KF_AUTO, /**< Encoder determines optimal placement automatically */ 260 VPX_KF_DISABLED = 0 /**< Encoder does not place keyframes. */ 261 }; 262 263 /*!\brief Encoded Frame Flags 264 * 265 * This type indicates a bitfield to be passed to vpx_codec_encode(), defining 266 * per-frame boolean values. By convention, bits common to all codecs will be 267 * named VPX_EFLAG_*, and bits specific to an algorithm will be named 268 * /algo/_eflag_*. The lower order 16 bits are reserved for common use. 269 */ 270 typedef long vpx_enc_frame_flags_t; 271 #define VPX_EFLAG_FORCE_KF (1 << 0) /**< Force this frame to be a keyframe */ 272 273 /*!\brief Encoder configuration structure 274 * 275 * This structure contains the encoder settings that have common representations 276 * across all codecs. This doesn't imply that all codecs support all features, 277 * however. 278 */ 279 typedef struct vpx_codec_enc_cfg { 280 /* 281 * generic settings (g) 282 */ 283 284 /*!\brief Algorithm specific "usage" value 285 * 286 * Algorithms may define multiple values for usage, which may convey the 287 * intent of how the application intends to use the stream. If this value 288 * is non-zero, consult the documentation for the codec to determine its 289 * meaning. 290 */ 291 unsigned int g_usage; 292 293 /*!\brief Maximum number of threads to use 294 * 295 * For multi-threaded implementations, use no more than this number of 296 * threads. The codec may use fewer threads than allowed. The value 297 * 0 is equivalent to the value 1. 298 */ 299 unsigned int g_threads; 300 301 /*!\brief Bitstream profile to use 302 * 303 * Some codecs support a notion of multiple bitstream profiles. Typically 304 * this maps to a set of features that are turned on or off. Often the 305 * profile to use is determined by the features of the intended decoder. 306 * Consult the documentation for the codec to determine the valid values 307 * for this parameter, or set to zero for a sane default. 308 */ 309 unsigned int g_profile; /**< profile of bitstream to use */ 310 311 /*!\brief Width of the frame 312 * 313 * This value identifies the presentation resolution of the frame, 314 * in pixels. Note that the frames passed as input to the encoder must 315 * have this resolution. Frames will be presented by the decoder in this 316 * resolution, independent of any spatial resampling the encoder may do. 317 */ 318 unsigned int g_w; 319 320 /*!\brief Height of the frame 321 * 322 * This value identifies the presentation resolution of the frame, 323 * in pixels. Note that the frames passed as input to the encoder must 324 * have this resolution. Frames will be presented by the decoder in this 325 * resolution, independent of any spatial resampling the encoder may do. 326 */ 327 unsigned int g_h; 328 329 /*!\brief Bit-depth of the codec 330 * 331 * This value identifies the bit_depth of the codec, 332 * Only certain bit-depths are supported as identified in the 333 * vpx_bit_depth_t enum. 334 */ 335 vpx_bit_depth_t g_bit_depth; 336 337 /*!\brief Bit-depth of the input frames 338 * 339 * This value identifies the bit_depth of the input frames in bits. 340 * Note that the frames passed as input to the encoder must have 341 * this bit-depth. 342 */ 343 unsigned int g_input_bit_depth; 344 345 /*!\brief Stream timebase units 346 * 347 * Indicates the smallest interval of time, in seconds, used by the stream. 348 * For fixed frame rate material, or variable frame rate material where 349 * frames are timed at a multiple of a given clock (ex: video capture), 350 * the \ref RECOMMENDED method is to set the timebase to the reciprocal 351 * of the frame rate (ex: 1001/30000 for 29.970 Hz NTSC). This allows the 352 * pts to correspond to the frame number, which can be handy. For 353 * re-encoding video from containers with absolute time timestamps, the 354 * \ref RECOMMENDED method is to set the timebase to that of the parent 355 * container or multimedia framework (ex: 1/1000 for ms, as in FLV). 356 */ 357 struct vpx_rational g_timebase; 358 359 /*!\brief Enable error resilient modes. 360 * 361 * The error resilient bitfield indicates to the encoder which features 362 * it should enable to take measures for streaming over lossy or noisy 363 * links. 364 */ 365 vpx_codec_er_flags_t g_error_resilient; 366 367 /*!\brief Multi-pass Encoding Mode 368 * 369 * This value should be set to the current phase for multi-pass encoding. 370 * For single pass, set to #VPX_RC_ONE_PASS. 371 */ 372 enum vpx_enc_pass g_pass; 373 374 /*!\brief Allow lagged encoding 375 * 376 * If set, this value allows the encoder to consume a number of input 377 * frames before producing output frames. This allows the encoder to 378 * base decisions for the current frame on future frames. This does 379 * increase the latency of the encoding pipeline, so it is not appropriate 380 * in all situations (ex: realtime encoding). 381 * 382 * Note that this is a maximum value -- the encoder may produce frames 383 * sooner than the given limit. Set this value to 0 to disable this 384 * feature. 385 */ 386 unsigned int g_lag_in_frames; 387 388 /* 389 * rate control settings (rc) 390 */ 391 392 /*!\brief Temporal resampling configuration, if supported by the codec. 393 * 394 * Temporal resampling allows the codec to "drop" frames as a strategy to 395 * meet its target data rate. This can cause temporal discontinuities in 396 * the encoded video, which may appear as stuttering during playback. This 397 * trade-off is often acceptable, but for many applications is not. It can 398 * be disabled in these cases. 399 * 400 * Note that not all codecs support this feature. All vpx VPx codecs do. 401 * For other codecs, consult the documentation for that algorithm. 402 * 403 * This threshold is described as a percentage of the target data buffer. 404 * When the data buffer falls below this percentage of fullness, a 405 * dropped frame is indicated. Set the threshold to zero (0) to disable 406 * this feature. 407 */ 408 unsigned int rc_dropframe_thresh; 409 410 /*!\brief Enable/disable spatial resampling, if supported by the codec. 411 * 412 * Spatial resampling allows the codec to compress a lower resolution 413 * version of the frame, which is then upscaled by the encoder to the 414 * correct presentation resolution. This increases visual quality at 415 * low data rates, at the expense of CPU time on the encoder/decoder. 416 */ 417 unsigned int rc_resize_allowed; 418 419 /*!\brief Internal coded frame width. 420 * 421 * If spatial resampling is enabled this specifies the width of the 422 * encoded frame. 423 */ 424 unsigned int rc_scaled_width; 425 426 /*!\brief Internal coded frame height. 427 * 428 * If spatial resampling is enabled this specifies the height of the 429 * encoded frame. 430 */ 431 unsigned int rc_scaled_height; 432 433 /*!\brief Spatial resampling up watermark. 434 * 435 * This threshold is described as a percentage of the target data buffer. 436 * When the data buffer rises above this percentage of fullness, the 437 * encoder will step up to a higher resolution version of the frame. 438 */ 439 unsigned int rc_resize_up_thresh; 440 441 /*!\brief Spatial resampling down watermark. 442 * 443 * This threshold is described as a percentage of the target data buffer. 444 * When the data buffer falls below this percentage of fullness, the 445 * encoder will step down to a lower resolution version of the frame. 446 */ 447 unsigned int rc_resize_down_thresh; 448 449 /*!\brief Rate control algorithm to use. 450 * 451 * Indicates whether the end usage of this stream is to be streamed over 452 * a bandwidth constrained link, indicating that Constant Bit Rate (CBR) 453 * mode should be used, or whether it will be played back on a high 454 * bandwidth link, as from a local disk, where higher variations in 455 * bitrate are acceptable. 456 */ 457 enum vpx_rc_mode rc_end_usage; 458 459 /*!\brief Two-pass stats buffer. 460 * 461 * A buffer containing all of the stats packets produced in the first 462 * pass, concatenated. 463 */ 464 vpx_fixed_buf_t rc_twopass_stats_in; 465 466 /*!\brief first pass mb stats buffer. 467 * 468 * A buffer containing all of the first pass mb stats packets produced 469 * in the first pass, concatenated. 470 */ 471 vpx_fixed_buf_t rc_firstpass_mb_stats_in; 472 473 /*!\brief Target data rate 474 * 475 * Target bandwidth to use for this stream, in kilobits per second. 476 */ 477 unsigned int rc_target_bitrate; 478 479 /* 480 * quantizer settings 481 */ 482 483 /*!\brief Minimum (Best Quality) Quantizer 484 * 485 * The quantizer is the most direct control over the quality of the 486 * encoded image. The range of valid values for the quantizer is codec 487 * specific. Consult the documentation for the codec to determine the 488 * values to use. To determine the range programmatically, call 489 * vpx_codec_enc_config_default() with a usage value of 0. 490 */ 491 unsigned int rc_min_quantizer; 492 493 /*!\brief Maximum (Worst Quality) Quantizer 494 * 495 * The quantizer is the most direct control over the quality of the 496 * encoded image. The range of valid values for the quantizer is codec 497 * specific. Consult the documentation for the codec to determine the 498 * values to use. To determine the range programmatically, call 499 * vpx_codec_enc_config_default() with a usage value of 0. 500 */ 501 unsigned int rc_max_quantizer; 502 503 /* 504 * bitrate tolerance 505 */ 506 507 /*!\brief Rate control adaptation undershoot control 508 * 509 * VP8: Expressed as a percentage of the target bitrate, 510 * controls the maximum allowed adaptation speed of the codec. 511 * This factor controls the maximum amount of bits that can 512 * be subtracted from the target bitrate in order to compensate 513 * for prior overshoot. 514 * VP9: Expressed as a percentage of the target bitrate, a threshold 515 * undershoot level (current rate vs target) beyond which more agressive 516 * corrective measures are taken. 517 * * 518 * Valid values in the range VP8:0-1000 VP9: 0-100. 519 */ 520 unsigned int rc_undershoot_pct; 521 522 /*!\brief Rate control adaptation overshoot control 523 * 524 * VP8: Expressed as a percentage of the target bitrate, 525 * controls the maximum allowed adaptation speed of the codec. 526 * This factor controls the maximum amount of bits that can 527 * be added to the target bitrate in order to compensate for 528 * prior undershoot. 529 * VP9: Expressed as a percentage of the target bitrate, a threshold 530 * overshoot level (current rate vs target) beyond which more agressive 531 * corrective measures are taken. 532 * 533 * Valid values in the range VP8:0-1000 VP9: 0-100. 534 */ 535 unsigned int rc_overshoot_pct; 536 537 /* 538 * decoder buffer model parameters 539 */ 540 541 /*!\brief Decoder Buffer Size 542 * 543 * This value indicates the amount of data that may be buffered by the 544 * decoding application. Note that this value is expressed in units of 545 * time (milliseconds). For example, a value of 5000 indicates that the 546 * client will buffer (at least) 5000ms worth of encoded data. Use the 547 * target bitrate (#rc_target_bitrate) to convert to bits/bytes, if 548 * necessary. 549 */ 550 unsigned int rc_buf_sz; 551 552 /*!\brief Decoder Buffer Initial Size 553 * 554 * This value indicates the amount of data that will be buffered by the 555 * decoding application prior to beginning playback. This value is 556 * expressed in units of time (milliseconds). Use the target bitrate 557 * (#rc_target_bitrate) to convert to bits/bytes, if necessary. 558 */ 559 unsigned int rc_buf_initial_sz; 560 561 /*!\brief Decoder Buffer Optimal Size 562 * 563 * This value indicates the amount of data that the encoder should try 564 * to maintain in the decoder's buffer. This value is expressed in units 565 * of time (milliseconds). Use the target bitrate (#rc_target_bitrate) 566 * to convert to bits/bytes, if necessary. 567 */ 568 unsigned int rc_buf_optimal_sz; 569 570 /* 571 * 2 pass rate control parameters 572 */ 573 574 /*!\brief Two-pass mode CBR/VBR bias 575 * 576 * Bias, expressed on a scale of 0 to 100, for determining target size 577 * for the current frame. The value 0 indicates the optimal CBR mode 578 * value should be used. The value 100 indicates the optimal VBR mode 579 * value should be used. Values in between indicate which way the 580 * encoder should "lean." 581 */ 582 unsigned int rc_2pass_vbr_bias_pct; 583 584 /*!\brief Two-pass mode per-GOP minimum bitrate 585 * 586 * This value, expressed as a percentage of the target bitrate, indicates 587 * the minimum bitrate to be used for a single GOP (aka "section") 588 */ 589 unsigned int rc_2pass_vbr_minsection_pct; 590 591 /*!\brief Two-pass mode per-GOP maximum bitrate 592 * 593 * This value, expressed as a percentage of the target bitrate, indicates 594 * the maximum bitrate to be used for a single GOP (aka "section") 595 */ 596 unsigned int rc_2pass_vbr_maxsection_pct; 597 598 /*!\brief Two-pass corpus vbr mode complexity control 599 * Used only in VP9: A value representing the corpus midpoint complexity 600 * for corpus vbr mode. This value defaults to 0 which disables corpus vbr 601 * mode in favour of normal vbr mode. 602 */ 603 unsigned int rc_2pass_vbr_corpus_complexity; 604 605 /* 606 * keyframing settings (kf) 607 */ 608 609 /*!\brief Keyframe placement mode 610 * 611 * This value indicates whether the encoder should place keyframes at a 612 * fixed interval, or determine the optimal placement automatically 613 * (as governed by the #kf_min_dist and #kf_max_dist parameters) 614 */ 615 enum vpx_kf_mode kf_mode; 616 617 /*!\brief Keyframe minimum interval 618 * 619 * This value, expressed as a number of frames, prevents the encoder from 620 * placing a keyframe nearer than kf_min_dist to the previous keyframe. At 621 * least kf_min_dist frames non-keyframes will be coded before the next 622 * keyframe. Set kf_min_dist equal to kf_max_dist for a fixed interval. 623 */ 624 unsigned int kf_min_dist; 625 626 /*!\brief Keyframe maximum interval 627 * 628 * This value, expressed as a number of frames, forces the encoder to code 629 * a keyframe if one has not been coded in the last kf_max_dist frames. 630 * A value of 0 implies all frames will be keyframes. Set kf_min_dist 631 * equal to kf_max_dist for a fixed interval. 632 */ 633 unsigned int kf_max_dist; 634 635 /* 636 * Spatial scalability settings (ss) 637 */ 638 639 /*!\brief Number of spatial coding layers. 640 * 641 * This value specifies the number of spatial coding layers to be used. 642 */ 643 unsigned int ss_number_layers; 644 645 /*!\brief Enable auto alt reference flags for each spatial layer. 646 * 647 * These values specify if auto alt reference frame is enabled for each 648 * spatial layer. 649 */ 650 int ss_enable_auto_alt_ref[VPX_SS_MAX_LAYERS]; 651 652 /*!\brief Target bitrate for each spatial layer. 653 * 654 * These values specify the target coding bitrate to be used for each 655 * spatial layer. 656 */ 657 unsigned int ss_target_bitrate[VPX_SS_MAX_LAYERS]; 658 659 /*!\brief Number of temporal coding layers. 660 * 661 * This value specifies the number of temporal layers to be used. 662 */ 663 unsigned int ts_number_layers; 664 665 /*!\brief Target bitrate for each temporal layer. 666 * 667 * These values specify the target coding bitrate to be used for each 668 * temporal layer. 669 */ 670 unsigned int ts_target_bitrate[VPX_TS_MAX_LAYERS]; 671 672 /*!\brief Frame rate decimation factor for each temporal layer. 673 * 674 * These values specify the frame rate decimation factors to apply 675 * to each temporal layer. 676 */ 677 unsigned int ts_rate_decimator[VPX_TS_MAX_LAYERS]; 678 679 /*!\brief Length of the sequence defining frame temporal layer membership. 680 * 681 * This value specifies the length of the sequence that defines the 682 * membership of frames to temporal layers. For example, if the 683 * ts_periodicity = 8, then the frames are assigned to coding layers with a 684 * repeated sequence of length 8. 685 */ 686 unsigned int ts_periodicity; 687 688 /*!\brief Template defining the membership of frames to temporal layers. 689 * 690 * This array defines the membership of frames to temporal coding layers. 691 * For a 2-layer encoding that assigns even numbered frames to one temporal 692 * layer (0) and odd numbered frames to a second temporal layer (1) with 693 * ts_periodicity=8, then ts_layer_id = (0,1,0,1,0,1,0,1). 694 */ 695 unsigned int ts_layer_id[VPX_TS_MAX_PERIODICITY]; 696 697 /*!\brief Target bitrate for each spatial/temporal layer. 698 * 699 * These values specify the target coding bitrate to be used for each 700 * spatial/temporal layer. 701 * 702 */ 703 unsigned int layer_target_bitrate[VPX_MAX_LAYERS]; 704 705 /*!\brief Temporal layering mode indicating which temporal layering scheme to 706 * use. 707 * 708 * The value (refer to VP9E_TEMPORAL_LAYERING_MODE) specifies the 709 * temporal layering mode to use. 710 * 711 */ 712 int temporal_layering_mode; 713 } vpx_codec_enc_cfg_t; /**< alias for struct vpx_codec_enc_cfg */ 714 715 /*!\brief vp9 svc extra configure parameters 716 * 717 * This defines max/min quantizers and scale factors for each layer 718 * 719 */ 720 typedef struct vpx_svc_parameters { 721 int max_quantizers[VPX_MAX_LAYERS]; /**< Max Q for each layer */ 722 int min_quantizers[VPX_MAX_LAYERS]; /**< Min Q for each layer */ 723 int scaling_factor_num[VPX_MAX_LAYERS]; /**< Scaling factor-numerator */ 724 int scaling_factor_den[VPX_MAX_LAYERS]; /**< Scaling factor-denominator */ 725 int speed_per_layer[VPX_MAX_LAYERS]; /**< Speed setting for each sl */ 726 int temporal_layering_mode; /**< Temporal layering mode */ 727 } vpx_svc_extra_cfg_t; 728 729 /*!\brief Initialize an encoder instance 730 * 731 * Initializes a encoder context using the given interface. Applications 732 * should call the vpx_codec_enc_init convenience macro instead of this 733 * function directly, to ensure that the ABI version number parameter 734 * is properly initialized. 735 * 736 * If the library was configured with --disable-multithread, this call 737 * is not thread safe and should be guarded with a lock if being used 738 * in a multithreaded context. 739 * 740 * \param[in] ctx Pointer to this instance's context. 741 * \param[in] iface Pointer to the algorithm interface to use. 742 * \param[in] cfg Configuration to use, if known. May be NULL. 743 * \param[in] flags Bitfield of VPX_CODEC_USE_* flags 744 * \param[in] ver ABI version number. Must be set to 745 * VPX_ENCODER_ABI_VERSION 746 * \retval #VPX_CODEC_OK 747 * The decoder algorithm initialized. 748 * \retval #VPX_CODEC_MEM_ERROR 749 * Memory allocation failed. 750 */ 751 vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t *ctx, 752 vpx_codec_iface_t *iface, 753 const vpx_codec_enc_cfg_t *cfg, 754 vpx_codec_flags_t flags, int ver); 755 756 /*!\brief Convenience macro for vpx_codec_enc_init_ver() 757 * 758 * Ensures the ABI version parameter is properly set. 759 */ 760 #define vpx_codec_enc_init(ctx, iface, cfg, flags) \ 761 vpx_codec_enc_init_ver(ctx, iface, cfg, flags, VPX_ENCODER_ABI_VERSION) 762 763 /*!\brief Initialize multi-encoder instance 764 * 765 * Initializes multi-encoder context using the given interface. 766 * Applications should call the vpx_codec_enc_init_multi convenience macro 767 * instead of this function directly, to ensure that the ABI version number 768 * parameter is properly initialized. 769 * 770 * \param[in] ctx Pointer to this instance's context. 771 * \param[in] iface Pointer to the algorithm interface to use. 772 * \param[in] cfg Configuration to use, if known. May be NULL. 773 * \param[in] num_enc Total number of encoders. 774 * \param[in] flags Bitfield of VPX_CODEC_USE_* flags 775 * \param[in] dsf Pointer to down-sampling factors. 776 * \param[in] ver ABI version number. Must be set to 777 * VPX_ENCODER_ABI_VERSION 778 * \retval #VPX_CODEC_OK 779 * The decoder algorithm initialized. 780 * \retval #VPX_CODEC_MEM_ERROR 781 * Memory allocation failed. 782 */ 783 vpx_codec_err_t vpx_codec_enc_init_multi_ver( 784 vpx_codec_ctx_t *ctx, vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, 785 int num_enc, vpx_codec_flags_t flags, vpx_rational_t *dsf, int ver); 786 787 /*!\brief Convenience macro for vpx_codec_enc_init_multi_ver() 788 * 789 * Ensures the ABI version parameter is properly set. 790 */ 791 #define vpx_codec_enc_init_multi(ctx, iface, cfg, num_enc, flags, dsf) \ 792 vpx_codec_enc_init_multi_ver(ctx, iface, cfg, num_enc, flags, dsf, \ 793 VPX_ENCODER_ABI_VERSION) 794 795 /*!\brief Get a default configuration 796 * 797 * Initializes a encoder configuration structure with default values. Supports 798 * the notion of "usages" so that an algorithm may offer different default 799 * settings depending on the user's intended goal. This function \ref SHOULD 800 * be called by all applications to initialize the configuration structure 801 * before specializing the configuration with application specific values. 802 * 803 * \param[in] iface Pointer to the algorithm interface to use. 804 * \param[out] cfg Configuration buffer to populate. 805 * \param[in] reserved Must set to 0 for VP8 and VP9. 806 * 807 * \retval #VPX_CODEC_OK 808 * The configuration was populated. 809 * \retval #VPX_CODEC_INCAPABLE 810 * Interface is not an encoder interface. 811 * \retval #VPX_CODEC_INVALID_PARAM 812 * A parameter was NULL, or the usage value was not recognized. 813 */ 814 vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, 815 vpx_codec_enc_cfg_t *cfg, 816 unsigned int reserved); 817 818 /*!\brief Set or change configuration 819 * 820 * Reconfigures an encoder instance according to the given configuration. 821 * 822 * \param[in] ctx Pointer to this instance's context 823 * \param[in] cfg Configuration buffer to use 824 * 825 * \retval #VPX_CODEC_OK 826 * The configuration was populated. 827 * \retval #VPX_CODEC_INCAPABLE 828 * Interface is not an encoder interface. 829 * \retval #VPX_CODEC_INVALID_PARAM 830 * A parameter was NULL, or the usage value was not recognized. 831 */ 832 vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx, 833 const vpx_codec_enc_cfg_t *cfg); 834 835 /*!\brief Get global stream headers 836 * 837 * Retrieves a stream level global header packet, if supported by the codec. 838 * 839 * \param[in] ctx Pointer to this instance's context 840 * 841 * \retval NULL 842 * Encoder does not support global header 843 * \retval Non-NULL 844 * Pointer to buffer containing global header packet 845 */ 846 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx); 847 848 /*!\brief deadline parameter analogous to VPx REALTIME mode. */ 849 #define VPX_DL_REALTIME (1) 850 /*!\brief deadline parameter analogous to VPx GOOD QUALITY mode. */ 851 #define VPX_DL_GOOD_QUALITY (1000000) 852 /*!\brief deadline parameter analogous to VPx BEST QUALITY mode. */ 853 #define VPX_DL_BEST_QUALITY (0) 854 /*!\brief Encode a frame 855 * 856 * Encodes a video frame at the given "presentation time." The presentation 857 * time stamp (PTS) \ref MUST be strictly increasing. 858 * 859 * The encoder supports the notion of a soft real-time deadline. Given a 860 * non-zero value to the deadline parameter, the encoder will make a "best 861 * effort" guarantee to return before the given time slice expires. It is 862 * implicit that limiting the available time to encode will degrade the 863 * output quality. The encoder can be given an unlimited time to produce the 864 * best possible frame by specifying a deadline of '0'. This deadline 865 * supercedes the VPx notion of "best quality, good quality, realtime". 866 * Applications that wish to map these former settings to the new deadline 867 * based system can use the symbols #VPX_DL_REALTIME, #VPX_DL_GOOD_QUALITY, 868 * and #VPX_DL_BEST_QUALITY. 869 * 870 * When the last frame has been passed to the encoder, this function should 871 * continue to be called, with the img parameter set to NULL. This will 872 * signal the end-of-stream condition to the encoder and allow it to encode 873 * any held buffers. Encoding is complete when vpx_codec_encode() is called 874 * and vpx_codec_get_cx_data() returns no data. 875 * 876 * \param[in] ctx Pointer to this instance's context 877 * \param[in] img Image data to encode, NULL to flush. 878 * \param[in] pts Presentation time stamp, in timebase units. 879 * \param[in] duration Duration to show frame, in timebase units. 880 * \param[in] flags Flags to use for encoding this frame. 881 * \param[in] deadline Time to spend encoding, in microseconds. (0=infinite) 882 * 883 * \retval #VPX_CODEC_OK 884 * The configuration was populated. 885 * \retval #VPX_CODEC_INCAPABLE 886 * Interface is not an encoder interface. 887 * \retval #VPX_CODEC_INVALID_PARAM 888 * A parameter was NULL, the image format is unsupported, etc. 889 */ 890 vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, 891 vpx_codec_pts_t pts, unsigned long duration, 892 vpx_enc_frame_flags_t flags, 893 unsigned long deadline); 894 895 /*!\brief Set compressed data output buffer 896 * 897 * Sets the buffer that the codec should output the compressed data 898 * into. This call effectively sets the buffer pointer returned in the 899 * next VPX_CODEC_CX_FRAME_PKT packet. Subsequent packets will be 900 * appended into this buffer. The buffer is preserved across frames, 901 * so applications must periodically call this function after flushing 902 * the accumulated compressed data to disk or to the network to reset 903 * the pointer to the buffer's head. 904 * 905 * `pad_before` bytes will be skipped before writing the compressed 906 * data, and `pad_after` bytes will be appended to the packet. The size 907 * of the packet will be the sum of the size of the actual compressed 908 * data, pad_before, and pad_after. The padding bytes will be preserved 909 * (not overwritten). 910 * 911 * Note that calling this function does not guarantee that the returned 912 * compressed data will be placed into the specified buffer. In the 913 * event that the encoded data will not fit into the buffer provided, 914 * the returned packet \ref MAY point to an internal buffer, as it would 915 * if this call were never used. In this event, the output packet will 916 * NOT have any padding, and the application must free space and copy it 917 * to the proper place. This is of particular note in configurations 918 * that may output multiple packets for a single encoded frame (e.g., lagged 919 * encoding) or if the application does not reset the buffer periodically. 920 * 921 * Applications may restore the default behavior of the codec providing 922 * the compressed data buffer by calling this function with a NULL 923 * buffer. 924 * 925 * Applications \ref MUSTNOT call this function during iteration of 926 * vpx_codec_get_cx_data(). 927 * 928 * \param[in] ctx Pointer to this instance's context 929 * \param[in] buf Buffer to store compressed data into 930 * \param[in] pad_before Bytes to skip before writing compressed data 931 * \param[in] pad_after Bytes to skip after writing compressed data 932 * 933 * \retval #VPX_CODEC_OK 934 * The buffer was set successfully. 935 * \retval #VPX_CODEC_INVALID_PARAM 936 * A parameter was NULL, the image format is unsupported, etc. 937 */ 938 vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx, 939 const vpx_fixed_buf_t *buf, 940 unsigned int pad_before, 941 unsigned int pad_after); 942 943 /*!\brief Encoded data iterator 944 * 945 * Iterates over a list of data packets to be passed from the encoder to the 946 * application. The different kinds of packets available are enumerated in 947 * #vpx_codec_cx_pkt_kind. 948 * 949 * #VPX_CODEC_CX_FRAME_PKT packets should be passed to the application's 950 * muxer. Multiple compressed frames may be in the list. 951 * #VPX_CODEC_STATS_PKT packets should be appended to a global buffer. 952 * 953 * The application \ref MUST silently ignore any packet kinds that it does 954 * not recognize or support. 955 * 956 * The data buffers returned from this function are only guaranteed to be 957 * valid until the application makes another call to any vpx_codec_* function. 958 * 959 * \param[in] ctx Pointer to this instance's context 960 * \param[in,out] iter Iterator storage, initialized to NULL 961 * 962 * \return Returns a pointer to an output data packet (compressed frame data, 963 * two-pass statistics, etc.) or NULL to signal end-of-list. 964 * 965 */ 966 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, 967 vpx_codec_iter_t *iter); 968 969 /*!\brief Get Preview Frame 970 * 971 * Returns an image that can be used as a preview. Shows the image as it would 972 * exist at the decompressor. The application \ref MUST NOT write into this 973 * image buffer. 974 * 975 * \param[in] ctx Pointer to this instance's context 976 * 977 * \return Returns a pointer to a preview image, or NULL if no image is 978 * available. 979 * 980 */ 981 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx); 982 983 /*!@} - end defgroup encoder*/ 984 #ifdef __cplusplus 985 } 986 #endif 987 #endif // VPX_VPX_ENCODER_H_ 988