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