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