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