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