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