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