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