1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 #ifndef AOM_AOM_AOM_ENCODER_H_
12 #define AOM_AOM_AOM_ENCODER_H_
13 
14 /*!\defgroup encoder Encoder Algorithm Interface
15  * \ingroup codec
16  * This abstraction allows applications using this encoder to easily support
17  * multiple video formats with minimal code duplication. This section describes
18  * the interface common to all encoders.
19  * @{
20  */
21 
22 /*!\file
23  * \brief Describes the encoder algorithm interface to applications.
24  *
25  * This file describes the interface between an application and a
26  * video encoder algorithm.
27  *
28  */
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include "aom/aom_codec.h"
34 #include "aom/aom_external_partition.h"
35 
36 /*!\brief Current ABI version number
37  *
38  * \internal
39  * If this file is altered in any way that changes the ABI, this value
40  * must be bumped.  Examples include, but are not limited to, changing
41  * types, removing or reassigning enums, adding/removing/rearranging
42  * fields to structures
43  */
44 #define AOM_ENCODER_ABI_VERSION \
45   (10 + AOM_CODEC_ABI_VERSION + AOM_EXT_PART_ABI_VERSION) /**<\hideinitializer*/
46 
47 /*! \brief Encoder capabilities bitfield
48  *
49  *  Each encoder advertises the capabilities it supports as part of its
50  *  ::aom_codec_iface_t interface structure. Capabilities are extra
51  *  interfaces or functionality, and are not required to be supported
52  *  by an encoder.
53  *
54  *  The available flags are specified by AOM_CODEC_CAP_* defines.
55  */
56 #define AOM_CODEC_CAP_PSNR 0x10000 /**< Can issue PSNR packets */
57 
58 /*! Can support input images at greater than 8 bitdepth.
59  */
60 #define AOM_CODEC_CAP_HIGHBITDEPTH 0x40000
61 
62 /*! \brief Initialization-time Feature Enabling
63  *
64  *  Certain codec features must be known at initialization time, to allow
65  *  for proper memory allocation.
66  *
67  *  The available flags are specified by AOM_CODEC_USE_* defines.
68  */
69 #define AOM_CODEC_USE_PSNR 0x10000 /**< Calculate PSNR on each frame */
70 /*!\brief Make the encoder output one  partition at a time. */
71 #define AOM_CODEC_USE_HIGHBITDEPTH 0x40000 /**< Use high bitdepth */
72 
73 /*!\brief Generic fixed size buffer structure
74  *
75  * This structure is able to hold a reference to any fixed size buffer.
76  */
77 typedef struct aom_fixed_buf {
78   void *buf;       /**< Pointer to the data. Does NOT own the data! */
79   size_t sz;       /**< Length of the buffer, in chars */
80 } aom_fixed_buf_t; /**< alias for struct aom_fixed_buf */
81 
82 /*!\brief Error Resilient flags
83  *
84  * These flags define which error resilient features to enable in the
85  * encoder. The flags are specified through the
86  * aom_codec_enc_cfg::g_error_resilient variable.
87  */
88 typedef uint32_t aom_codec_er_flags_t;
89 /*!\brief Improve resiliency against losses of whole frames */
90 #define AOM_ERROR_RESILIENT_DEFAULT 0x1
91 
92 /*!\brief Encoder output packet variants
93  *
94  * This enumeration lists the different kinds of data packets that can be
95  * returned by calls to aom_codec_get_cx_data(). Algorithms \ref MAY
96  * extend this list to provide additional functionality.
97  */
98 enum aom_codec_cx_pkt_kind {
99   AOM_CODEC_CX_FRAME_PKT,    /**< Compressed video frame */
100   AOM_CODEC_STATS_PKT,       /**< Two-pass statistics for this frame */
101   AOM_CODEC_FPMB_STATS_PKT,  /**< first pass mb statistics for this frame */
102   AOM_CODEC_PSNR_PKT,        /**< PSNR statistics for this frame */
103   AOM_CODEC_CUSTOM_PKT = 256 /**< Algorithm extensions  */
104 };
105 
106 /*!\brief Encoder output packet
107  *
108  * This structure contains the different kinds of output data the encoder
109  * may produce while compressing a frame.
110  */
111 typedef struct aom_codec_cx_pkt {
112   enum aom_codec_cx_pkt_kind kind; /**< packet variant */
113   union {
114     struct {
115       void *buf; /**< compressed data buffer */
116       size_t sz; /**< length of compressed data */
117       /*!\brief time stamp to show frame (in timebase units) */
118       aom_codec_pts_t pts;
119       /*!\brief duration to show frame (in timebase units) */
120       unsigned long duration;
121       aom_codec_frame_flags_t flags; /**< flags for this frame */
122       /*!\brief the partition id defines the decoding order of the partitions.
123        * Only applicable when "output partition" mode is enabled. First
124        * partition has id 0.*/
125       int partition_id;
126       /*!\brief size of the visible frame in this packet */
127       size_t vis_frame_size;
128     } frame;                            /**< data for compressed frame packet */
129     aom_fixed_buf_t twopass_stats;      /**< data for two-pass packet */
130     aom_fixed_buf_t firstpass_mb_stats; /**< first pass mb packet */
131     struct aom_psnr_pkt {
132       unsigned int samples[4]; /**< Number of samples, total/y/u/v */
133       uint64_t sse[4];         /**< sum squared error, total/y/u/v */
134       double psnr[4];          /**< PSNR, total/y/u/v */
135       /*!\brief Number of samples, total/y/u/v when
136        * input bit-depth < stream bit-depth.*/
137       unsigned int samples_hbd[4];
138       /*!\brief sum squared error, total/y/u/v when
139        * input bit-depth < stream bit-depth.*/
140       uint64_t sse_hbd[4];
141       /*!\brief PSNR, total/y/u/v when
142        * input bit-depth < stream bit-depth.*/
143       double psnr_hbd[4];
144     } psnr;              /**< data for PSNR packet */
145     aom_fixed_buf_t raw; /**< data for arbitrary packets */
146   } data;                /**< packet data */
147 } aom_codec_cx_pkt_t;    /**< alias for struct aom_codec_cx_pkt */
148 
149 /*!\brief Rational Number
150  *
151  * This structure holds a fractional value.
152  */
153 typedef struct aom_rational {
154   int num;        /**< fraction numerator */
155   int den;        /**< fraction denominator */
156 } aom_rational_t; /**< alias for struct aom_rational */
157 
158 /*!\brief Multi-pass Encoding Pass
159  *
160  * AOM_RC_LAST_PASS is kept for backward compatibility.
161  * If passes is not given and pass==2, the codec will assume passes=2.
162  * For new code, it is recommended to use AOM_RC_SECOND_PASS and set
163  * the "passes" member to 2 via the key & val API for two-pass encoding.
164  */
165 enum aom_enc_pass {
166   AOM_RC_ONE_PASS = 0,    /**< Single pass mode */
167   AOM_RC_FIRST_PASS = 1,  /**< First pass of multi-pass mode */
168   AOM_RC_SECOND_PASS = 2, /**< Second pass of multi-pass mode */
169   AOM_RC_THIRD_PASS = 3,  /**< Third pass of multi-pass mode */
170   AOM_RC_LAST_PASS = 2,   /**< Final pass of two-pass mode */
171 };
172 
173 /*!\brief Rate control mode */
174 enum aom_rc_mode {
175   AOM_VBR, /**< Variable Bit Rate (VBR) mode */
176   AOM_CBR, /**< Constant Bit Rate (CBR) mode */
177   AOM_CQ,  /**< Constrained Quality (CQ)  mode */
178   AOM_Q,   /**< Constant Quality (Q) mode */
179 };
180 
181 /*!\brief Keyframe placement mode.
182  *
183  * This enumeration determines whether keyframes are placed automatically by
184  * the encoder or whether this behavior is disabled. Older releases of this
185  * SDK were implemented such that AOM_KF_FIXED meant keyframes were disabled.
186  * This name is confusing for this behavior, so the new symbols to be used
187  * are AOM_KF_AUTO and AOM_KF_DISABLED.
188  */
189 enum aom_kf_mode {
190   AOM_KF_FIXED,       /**< deprecated, implies AOM_KF_DISABLED */
191   AOM_KF_AUTO,        /**< Encoder determines optimal placement automatically */
192   AOM_KF_DISABLED = 0 /**< Encoder does not place keyframes. */
193 };
194 
195 /*!\brief Frame super-resolution mode. */
196 typedef enum {
197   /**< Frame super-resolution is disabled for all frames. */
198   AOM_SUPERRES_NONE,
199   /**< All frames are coded at the specified scale and super-resolved. */
200   AOM_SUPERRES_FIXED,
201   /**< All frames are coded at a random scale and super-resolved. */
202   AOM_SUPERRES_RANDOM,
203   /**< Super-resolution scale for each frame is determined based on the q index
204      of that frame. */
205   AOM_SUPERRES_QTHRESH,
206   /**< Full-resolution or super-resolution and the scale (in case of
207      super-resolution) are automatically selected for each frame. */
208   AOM_SUPERRES_AUTO,
209 } aom_superres_mode;
210 
211 /*!\brief Encoder Config Options
212  *
213  * This type allows to enumerate and control flags defined for encoder control
214  * via config file at runtime.
215  */
216 typedef struct cfg_options {
217   /*!\brief Indicate init by cfg file
218    * 0 or 1
219    */
220   unsigned int init_by_cfg_file;
221   /*!\brief Superblock size
222    * 0, 64 or 128
223    */
224   unsigned int super_block_size;
225   /*!\brief max partition size
226    * 8, 16, 32, 64, 128
227    */
228   unsigned int max_partition_size;
229   /*!\brief min partition size
230    * 8, 16, 32, 64, 128
231    */
232   unsigned int min_partition_size;
233   /*!\brief disable AB Shape partition type
234    *
235    */
236   unsigned int disable_ab_partition_type;
237   /*!\brief disable rectangular partition type
238    *
239    */
240   unsigned int disable_rect_partition_type;
241   /*!\brief disable 1:4/4:1 partition type
242    *
243    */
244   unsigned int disable_1to4_partition_type;
245   /*!\brief disable flip and identity transform type
246    *
247    */
248   unsigned int disable_flip_idtx;
249   /*!\brief disable CDEF filter
250    *
251    */
252   unsigned int disable_cdef;
253   /*!\brief disable Loop Restoration Filter
254    *
255    */
256   unsigned int disable_lr;
257   /*!\brief disable OBMC
258    *
259    */
260   unsigned int disable_obmc;
261   /*!\brief disable Warped Motion
262    *
263    */
264   unsigned int disable_warp_motion;
265   /*!\brief disable global motion
266    *
267    */
268   unsigned int disable_global_motion;
269   /*!\brief disable dist weighted compound
270    *
271    */
272   unsigned int disable_dist_wtd_comp;
273   /*!\brief disable diff weighted compound
274    *
275    */
276   unsigned int disable_diff_wtd_comp;
277   /*!\brief disable inter/intra compound
278    *
279    */
280   unsigned int disable_inter_intra_comp;
281   /*!\brief disable masked compound
282    *
283    */
284   unsigned int disable_masked_comp;
285   /*!\brief disable one sided compound
286    *
287    */
288   unsigned int disable_one_sided_comp;
289   /*!\brief disable Palette
290    *
291    */
292   unsigned int disable_palette;
293   /*!\brief disable Intra Block Copy
294    *
295    */
296   unsigned int disable_intrabc;
297   /*!\brief disable chroma from luma
298    *
299    */
300   unsigned int disable_cfl;
301   /*!\brief disable intra smooth mode
302    *
303    */
304   unsigned int disable_smooth_intra;
305   /*!\brief disable filter intra
306    *
307    */
308   unsigned int disable_filter_intra;
309   /*!\brief disable dual filter
310    *
311    */
312   unsigned int disable_dual_filter;
313   /*!\brief disable intra angle delta
314    *
315    */
316   unsigned int disable_intra_angle_delta;
317   /*!\brief disable intra edge filter
318    *
319    */
320   unsigned int disable_intra_edge_filter;
321   /*!\brief disable 64x64 transform
322    *
323    */
324   unsigned int disable_tx_64x64;
325   /*!\brief disable smooth inter/intra
326    *
327    */
328   unsigned int disable_smooth_inter_intra;
329   /*!\brief disable inter/inter wedge comp
330    *
331    */
332   unsigned int disable_inter_inter_wedge;
333   /*!\brief disable inter/intra wedge comp
334    *
335    */
336   unsigned int disable_inter_intra_wedge;
337   /*!\brief disable paeth intra
338    *
339    */
340   unsigned int disable_paeth_intra;
341   /*!\brief disable trellis quantization
342    *
343    */
344   unsigned int disable_trellis_quant;
345   /*!\brief disable ref frame MV
346    *
347    */
348   unsigned int disable_ref_frame_mv;
349   /*!\brief use reduced reference frame set
350    *
351    */
352   unsigned int reduced_reference_set;
353   /*!\brief use reduced transform type set
354    *
355    */
356   unsigned int reduced_tx_type_set;
357 } cfg_options_t;
358 
359 /*!\brief Encoded Frame Flags
360  *
361  * This type indicates a bitfield to be passed to aom_codec_encode(), defining
362  * per-frame boolean values. By convention, bits common to all codecs will be
363  * named AOM_EFLAG_*, and bits specific to an algorithm will be named
364  * /algo/_eflag_*. The lower order 16 bits are reserved for common use.
365  */
366 typedef long aom_enc_frame_flags_t;
367 /*!\brief Force this frame to be a keyframe */
368 #define AOM_EFLAG_FORCE_KF (1 << 0)
369 
370 /*!\brief Encoder configuration structure
371  *
372  * This structure contains the encoder settings that have common representations
373  * across all codecs. This doesn't imply that all codecs support all features,
374  * however.
375  */
376 typedef struct aom_codec_enc_cfg {
377   /*
378    * generic settings (g)
379    */
380 
381   /*!\brief Algorithm specific "usage" value
382    *
383    * Algorithms may define multiple values for usage, which may convey the
384    * intent of how the application intends to use the stream. If this value
385    * is non-zero, consult the documentation for the codec to determine its
386    * meaning.
387    */
388   unsigned int g_usage;
389 
390   /*!\brief Maximum number of threads to use
391    *
392    * For multi-threaded implementations, use no more than this number of
393    * threads. The codec may use fewer threads than allowed. The value
394    * 0 is equivalent to the value 1.
395    */
396   unsigned int g_threads;
397 
398   /*!\brief Bitstream profile to use
399    *
400    * Some codecs support a notion of multiple bitstream profiles. Typically
401    * this maps to a set of features that are turned on or off. Often the
402    * profile to use is determined by the features of the intended decoder.
403    * Consult the documentation for the codec to determine the valid values
404    * for this parameter, or set to zero for a sane default.
405    */
406   unsigned int g_profile; /**< profile of bitstream to use */
407 
408   /*!\brief Width of the frame
409    *
410    * This value identifies the presentation resolution of the frame,
411    * in pixels. Note that the frames passed as input to the encoder must
412    * have this resolution. Frames will be presented by the decoder in this
413    * resolution, independent of any spatial resampling the encoder may do.
414    */
415   unsigned int g_w;
416 
417   /*!\brief Height of the frame
418    *
419    * This value identifies the presentation resolution of the frame,
420    * in pixels. Note that the frames passed as input to the encoder must
421    * have this resolution. Frames will be presented by the decoder in this
422    * resolution, independent of any spatial resampling the encoder may do.
423    */
424   unsigned int g_h;
425 
426   /*!\brief Max number of frames to encode
427    *
428    */
429   unsigned int g_limit;
430 
431   /*!\brief Forced maximum width of the frame
432    *
433    * If this value is non-zero then it is used to force the maximum frame
434    * width written in write_sequence_header().
435    */
436   unsigned int g_forced_max_frame_width;
437 
438   /*!\brief Forced maximum height of the frame
439    *
440    * If this value is non-zero then it is used to force the maximum frame
441    * height written in write_sequence_header().
442    */
443   unsigned int g_forced_max_frame_height;
444 
445   /*!\brief Bit-depth of the codec
446    *
447    * This value identifies the bit_depth of the codec,
448    * Only certain bit-depths are supported as identified in the
449    * aom_bit_depth_t enum.
450    */
451   aom_bit_depth_t g_bit_depth;
452 
453   /*!\brief Bit-depth of the input frames
454    *
455    * This value identifies the bit_depth of the input frames in bits.
456    * Note that the frames passed as input to the encoder must have
457    * this bit-depth.
458    */
459   unsigned int g_input_bit_depth;
460 
461   /*!\brief Stream timebase units
462    *
463    * Indicates the smallest interval of time, in seconds, used by the stream.
464    * For fixed frame rate material, or variable frame rate material where
465    * frames are timed at a multiple of a given clock (ex: video capture),
466    * the \ref RECOMMENDED method is to set the timebase to the reciprocal
467    * of the frame rate (ex: 1001/30000 for 29.970 Hz NTSC). This allows the
468    * pts to correspond to the frame number, which can be handy. For
469    * re-encoding video from containers with absolute time timestamps, the
470    * \ref RECOMMENDED method is to set the timebase to that of the parent
471    * container or multimedia framework (ex: 1/1000 for ms, as in FLV).
472    */
473   struct aom_rational g_timebase;
474 
475   /*!\brief Enable error resilient modes.
476    *
477    * The error resilient bitfield indicates to the encoder which features
478    * it should enable to take measures for streaming over lossy or noisy
479    * links.
480    */
481   aom_codec_er_flags_t g_error_resilient;
482 
483   /*!\brief Multi-pass Encoding Mode
484    *
485    * This value should be set to the current phase for multi-pass encoding.
486    * For single pass, set to #AOM_RC_ONE_PASS.
487    */
488   enum aom_enc_pass g_pass;
489 
490   /*!\brief Allow lagged encoding
491    *
492    * If set, this value allows the encoder to consume a number of input
493    * frames before producing output frames. This allows the encoder to
494    * base decisions for the current frame on future frames. This does
495    * increase the latency of the encoding pipeline, so it is not appropriate
496    * in all situations (ex: realtime encoding).
497    *
498    * Note that this is a maximum value -- the encoder may produce frames
499    * sooner than the given limit. Set this value to 0 to disable this
500    * feature.
501    */
502   unsigned int g_lag_in_frames;
503 
504   /*
505    * rate control settings (rc)
506    */
507 
508   /*!\brief Temporal resampling configuration, if supported by the codec.
509    *
510    * Temporal resampling allows the codec to "drop" frames as a strategy to
511    * meet its target data rate. This can cause temporal discontinuities in
512    * the encoded video, which may appear as stuttering during playback. This
513    * trade-off is often acceptable, but for many applications is not. It can
514    * be disabled in these cases.
515    *
516    * Note that not all codecs support this feature. All aom AVx codecs do.
517    * For other codecs, consult the documentation for that algorithm.
518    *
519    * This threshold is described as a percentage of the target data buffer.
520    * When the data buffer falls below this percentage of fullness, a
521    * dropped frame is indicated. Set the threshold to zero (0) to disable
522    * this feature.
523    */
524   unsigned int rc_dropframe_thresh;
525 
526   /*!\brief Mode for spatial resampling, if supported by the codec.
527    *
528    * Spatial resampling allows the codec to compress a lower resolution
529    * version of the frame, which is then upscaled by the decoder to the
530    * correct presentation resolution. This increases visual quality at
531    * low data rates, at the expense of CPU time on the encoder/decoder.
532    */
533   unsigned int rc_resize_mode;
534 
535   /*!\brief Frame resize denominator.
536    *
537    * The denominator for resize to use, assuming 8 as the numerator.
538    *
539    * Valid denominators are  8 - 16 for now.
540    */
541   unsigned int rc_resize_denominator;
542 
543   /*!\brief Keyframe resize denominator.
544    *
545    * The denominator for resize to use, assuming 8 as the numerator.
546    *
547    * Valid denominators are  8 - 16 for now.
548    */
549   unsigned int rc_resize_kf_denominator;
550 
551   /*!\brief Frame super-resolution scaling mode.
552    *
553    * Similar to spatial resampling, frame super-resolution integrates
554    * upscaling after the encode/decode process. Taking control of upscaling and
555    * using restoration filters should allow it to outperform normal resizing.
556    */
557   aom_superres_mode rc_superres_mode;
558 
559   /*!\brief Frame super-resolution denominator.
560    *
561    * The denominator for superres to use. If fixed it will only change if the
562    * cumulative scale change over resizing and superres is greater than 1/2;
563    * this forces superres to reduce scaling.
564    *
565    * Valid denominators are 8 to 16.
566    *
567    * Used only by AOM_SUPERRES_FIXED.
568    */
569   unsigned int rc_superres_denominator;
570 
571   /*!\brief Keyframe super-resolution denominator.
572    *
573    * The denominator for superres to use. If fixed it will only change if the
574    * cumulative scale change over resizing and superres is greater than 1/2;
575    * this forces superres to reduce scaling.
576    *
577    * Valid denominators are 8 - 16 for now.
578    */
579   unsigned int rc_superres_kf_denominator;
580 
581   /*!\brief Frame super-resolution q threshold.
582    *
583    * The q level threshold after which superres is used.
584    * Valid values are 1 to 63.
585    *
586    * Used only by AOM_SUPERRES_QTHRESH
587    */
588   unsigned int rc_superres_qthresh;
589 
590   /*!\brief Keyframe super-resolution q threshold.
591    *
592    * The q level threshold after which superres is used for key frames.
593    * Valid values are 1 to 63.
594    *
595    * Used only by AOM_SUPERRES_QTHRESH
596    */
597   unsigned int rc_superres_kf_qthresh;
598 
599   /*!\brief Rate control algorithm to use.
600    *
601    * Indicates whether the end usage of this stream is to be streamed over
602    * a bandwidth constrained link, indicating that Constant Bit Rate (CBR)
603    * mode should be used, or whether it will be played back on a high
604    * bandwidth link, as from a local disk, where higher variations in
605    * bitrate are acceptable.
606    */
607   enum aom_rc_mode rc_end_usage;
608 
609   /*!\brief Two-pass stats buffer.
610    *
611    * A buffer containing all of the stats packets produced in the first
612    * pass, concatenated.
613    */
614   aom_fixed_buf_t rc_twopass_stats_in;
615 
616   /*!\brief first pass mb stats buffer.
617    *
618    * A buffer containing all of the first pass mb stats packets produced
619    * in the first pass, concatenated.
620    */
621   aom_fixed_buf_t rc_firstpass_mb_stats_in;
622 
623   /*!\brief Target data rate
624    *
625    * Target bitrate to use for this stream, in kilobits per second.
626    */
627   unsigned int rc_target_bitrate;
628 
629   /*
630    * quantizer settings
631    */
632 
633   /*!\brief Minimum (Best Quality) Quantizer
634    *
635    * The quantizer is the most direct control over the quality of the
636    * encoded image. The range of valid values for the quantizer is codec
637    * specific. Consult the documentation for the codec to determine the
638    * values to use. To determine the range programmatically, call
639    * aom_codec_enc_config_default() with a usage value of 0.
640    */
641   unsigned int rc_min_quantizer;
642 
643   /*!\brief Maximum (Worst Quality) Quantizer
644    *
645    * The quantizer is the most direct control over the quality of the
646    * encoded image. The range of valid values for the quantizer is codec
647    * specific. Consult the documentation for the codec to determine the
648    * values to use. To determine the range programmatically, call
649    * aom_codec_enc_config_default() with a usage value of 0.
650    */
651   unsigned int rc_max_quantizer;
652 
653   /*
654    * bitrate tolerance
655    */
656 
657   /*!\brief Rate control adaptation undershoot control
658    *
659    * This value, controls the tolerance of the VBR algorithm to undershoot
660    * and is used as a trigger threshold for more aggressive adaptation of Q.
661    *
662    * Valid values in the range 0-100.
663    */
664   unsigned int rc_undershoot_pct;
665 
666   /*!\brief Rate control adaptation overshoot control
667    *
668    * This value, controls the tolerance of the VBR algorithm to overshoot
669    * and is used as a trigger threshold for more aggressive adaptation of Q.
670    *
671    * Valid values in the range 0-100.
672    */
673   unsigned int rc_overshoot_pct;
674 
675   /*
676    * decoder buffer model parameters
677    */
678 
679   /*!\brief Decoder Buffer Size
680    *
681    * This value indicates the amount of data that may be buffered by the
682    * decoding application. Note that this value is expressed in units of
683    * time (milliseconds). For example, a value of 5000 indicates that the
684    * client will buffer (at least) 5000ms worth of encoded data. Use the
685    * target bitrate (#rc_target_bitrate) to convert to bits/bytes, if
686    * necessary.
687    */
688   unsigned int rc_buf_sz;
689 
690   /*!\brief Decoder Buffer Initial Size
691    *
692    * This value indicates the amount of data that will be buffered by the
693    * decoding application prior to beginning playback. This value is
694    * expressed in units of time (milliseconds). Use the target bitrate
695    * (#rc_target_bitrate) to convert to bits/bytes, if necessary.
696    */
697   unsigned int rc_buf_initial_sz;
698 
699   /*!\brief Decoder Buffer Optimal Size
700    *
701    * This value indicates the amount of data that the encoder should try
702    * to maintain in the decoder's buffer. This value is expressed in units
703    * of time (milliseconds). Use the target bitrate (#rc_target_bitrate)
704    * to convert to bits/bytes, if necessary.
705    */
706   unsigned int rc_buf_optimal_sz;
707 
708   /*
709    * 2 pass rate control parameters
710    */
711 
712   /*!\brief Two-pass mode CBR/VBR bias
713    *
714    * Bias, expressed on a scale of 0 to 100, for determining target size
715    * for the current frame. The value 0 indicates the optimal CBR mode
716    * value should be used. The value 100 indicates the optimal VBR mode
717    * value should be used. Values in between indicate which way the
718    * encoder should "lean."
719    */
720   unsigned int rc_2pass_vbr_bias_pct;
721 
722   /*!\brief Two-pass mode per-GOP minimum bitrate
723    *
724    * This value, expressed as a percentage of the target bitrate, indicates
725    * the minimum bitrate to be used for a single GOP (aka "section")
726    */
727   unsigned int rc_2pass_vbr_minsection_pct;
728 
729   /*!\brief Two-pass mode per-GOP maximum bitrate
730    *
731    * This value, expressed as a percentage of the target bitrate, indicates
732    * the maximum bitrate to be used for a single GOP (aka "section")
733    */
734   unsigned int rc_2pass_vbr_maxsection_pct;
735 
736   /*
737    * keyframing settings (kf)
738    */
739 
740   /*!\brief Option to enable forward reference key frame
741    *
742    */
743   int fwd_kf_enabled;
744 
745   /*!\brief Keyframe placement mode
746    *
747    * This value indicates whether the encoder should place keyframes at a
748    * fixed interval, or determine the optimal placement automatically
749    * (as governed by the #kf_min_dist and #kf_max_dist parameters)
750    */
751   enum aom_kf_mode kf_mode;
752 
753   /*!\brief Keyframe minimum interval
754    *
755    * This value, expressed as a number of frames, prevents the encoder from
756    * placing a keyframe nearer than kf_min_dist to the previous keyframe. At
757    * least kf_min_dist frames non-keyframes will be coded before the next
758    * keyframe. Set kf_min_dist equal to kf_max_dist for a fixed interval.
759    */
760   unsigned int kf_min_dist;
761 
762   /*!\brief Keyframe maximum interval
763    *
764    * This value, expressed as a number of frames, forces the encoder to code
765    * a keyframe if one has not been coded in the last kf_max_dist frames.
766    * A value of 0 implies all frames will be keyframes. Set kf_min_dist
767    * equal to kf_max_dist for a fixed interval.
768    */
769   unsigned int kf_max_dist;
770 
771   /*!\brief sframe interval
772    *
773    * This value, expressed as a number of frames, forces the encoder to code
774    * an S-Frame every sframe_dist frames.
775    */
776   unsigned int sframe_dist;
777 
778   /*!\brief sframe insertion mode
779    *
780    * This value must be set to 1 or 2, and tells the encoder how to insert
781    * S-Frames. It will only have an effect if sframe_dist != 0.
782    *
783    * If altref is enabled:
784    *   - if sframe_mode == 1, the considered frame will be made into an
785    *     S-Frame only if it is an altref frame
786    *   - if sframe_mode == 2, the next altref frame will be made into an
787    *     S-Frame.
788    *
789    * Otherwise: the considered frame will be made into an S-Frame.
790    */
791   unsigned int sframe_mode;
792 
793   /*!\brief Tile coding mode
794    *
795    * This value indicates the tile coding mode.
796    * A value of 0 implies a normal non-large-scale tile coding. A value of 1
797    * implies a large-scale tile coding.
798    */
799   unsigned int large_scale_tile;
800 
801   /*!\brief Monochrome mode
802    *
803    * If this is nonzero, the encoder will generate a monochrome stream
804    * with no chroma planes.
805    */
806   unsigned int monochrome;
807 
808   /*!\brief full_still_picture_hdr
809    *
810    * If this is nonzero, the encoder will generate a full header even for
811    * still picture encoding. if zero, a reduced header is used for still
812    * picture. This flag has no effect when a regular video with more than
813    * a single frame is encoded.
814    */
815   unsigned int full_still_picture_hdr;
816 
817   /*!\brief Bitstream syntax mode
818    *
819    * This value indicates the bitstream syntax mode.
820    * A value of 0 indicates bitstream is saved as Section 5 bitstream. A value
821    * of 1 indicates the bitstream is saved in Annex-B format
822    */
823   unsigned int save_as_annexb;
824 
825   /*!\brief Number of explicit tile widths specified
826    *
827    * This value indicates the number of tile widths specified
828    * A value of 0 implies no tile widths are specified.
829    * Tile widths are given in the array tile_widths[]
830    */
831   int tile_width_count;
832 
833   /*!\brief Number of explicit tile heights specified
834    *
835    * This value indicates the number of tile heights specified
836    * A value of 0 implies no tile heights are specified.
837    * Tile heights are given in the array tile_heights[]
838    */
839   int tile_height_count;
840 
841 /*!\brief Maximum number of tile widths in tile widths array
842  *
843  * This define gives the maximum number of elements in the tile_widths array.
844  */
845 #define MAX_TILE_WIDTHS 64  // maximum tile width array length
846 
847   /*!\brief Array of specified tile widths
848    *
849    * This array specifies tile widths (and may be empty)
850    * The number of widths specified is given by tile_width_count
851    */
852   int tile_widths[MAX_TILE_WIDTHS];
853 
854 /*!\brief Maximum number of tile heights in tile heights array.
855  *
856  * This define gives the maximum number of elements in the tile_heights array.
857  */
858 #define MAX_TILE_HEIGHTS 64  // maximum tile height array length
859 
860   /*!\brief Array of specified tile heights
861    *
862    * This array specifies tile heights (and may be empty)
863    * The number of heights specified is given by tile_height_count
864    */
865   int tile_heights[MAX_TILE_HEIGHTS];
866 
867   /*!\brief Whether encoder should use fixed QP offsets.
868    *
869    * If a value of 1 is provided, encoder will use fixed QP offsets for frames
870    * at different levels of the pyramid.
871    * - If 'fixed_qp_offsets' is also provided, encoder will use the given
872    * offsets
873    * - If not, encoder will select the fixed offsets based on the cq-level
874    *   provided.
875    * If a value of 0 is provided and fixed_qp_offset are not provided, encoder
876    * will NOT use fixed QP offsets.
877    * Note: This option is only relevant for --end-usage=q.
878    */
879   unsigned int use_fixed_qp_offsets;
880 
881 /*!\brief Number of fixed QP offsets
882  *
883  * This defines the number of elements in the fixed_qp_offsets array.
884  */
885 #define FIXED_QP_OFFSET_COUNT 5
886 
887   /*!\brief Array of fixed QP offsets
888    *
889    * This array specifies fixed QP offsets (range: 0 to 63) for frames at
890    * different levels of the pyramid. It is a comma-separated list of 5 values:
891    * - QP offset for keyframe
892    * - QP offset for ALTREF frame
893    * - QP offset for 1st level internal ARF
894    * - QP offset for 2nd level internal ARF
895    * - QP offset for 3rd level internal ARF
896    * Notes:
897    * - QP offset for leaf level frames is not explicitly specified. These frames
898    *   use the worst quality allowed (--cq-level).
899    * - This option is only relevant for --end-usage=q.
900    */
901   int fixed_qp_offsets[FIXED_QP_OFFSET_COUNT];
902 
903   /*!\brief Options defined per config file
904    *
905    */
906   cfg_options_t encoder_cfg;
907 } aom_codec_enc_cfg_t; /**< alias for struct aom_codec_enc_cfg */
908 
909 /*!\brief Initialize an encoder instance
910  *
911  * Initializes a encoder context using the given interface. Applications
912  * should call the aom_codec_enc_init convenience macro instead of this
913  * function directly, to ensure that the ABI version number parameter
914  * is properly initialized.
915  *
916  * If the library was configured with -DCONFIG_MULTITHREAD=0, this call
917  * is not thread safe and should be guarded with a lock if being used
918  * in a multithreaded context.
919  *
920  * \param[in]    ctx     Pointer to this instance's context.
921  * \param[in]    iface   Pointer to the algorithm interface to use.
922  * \param[in]    cfg     Configuration to use, if known.
923  * \param[in]    flags   Bitfield of AOM_CODEC_USE_* flags
924  * \param[in]    ver     ABI version number. Must be set to
925  *                       AOM_ENCODER_ABI_VERSION
926  * \retval #AOM_CODEC_OK
927  *     The decoder algorithm initialized.
928  * \retval #AOM_CODEC_MEM_ERROR
929  *     Memory allocation failed.
930  */
931 aom_codec_err_t aom_codec_enc_init_ver(aom_codec_ctx_t *ctx,
932                                        aom_codec_iface_t *iface,
933                                        const aom_codec_enc_cfg_t *cfg,
934                                        aom_codec_flags_t flags, int ver);
935 
936 /*!\brief Convenience macro for aom_codec_enc_init_ver()
937  *
938  * Ensures the ABI version parameter is properly set.
939  */
940 #define aom_codec_enc_init(ctx, iface, cfg, flags) \
941   aom_codec_enc_init_ver(ctx, iface, cfg, flags, AOM_ENCODER_ABI_VERSION)
942 
943 /*!\brief Get the default configuration for a usage.
944  *
945  * Initializes an encoder configuration structure with default values. Supports
946  * the notion of "usages" so that an algorithm may offer different default
947  * settings depending on the user's intended goal. This function \ref SHOULD
948  * be called by all applications to initialize the configuration structure
949  * before specializing the configuration with application specific values.
950  *
951  * \param[in]    iface     Pointer to the algorithm interface to use.
952  * \param[out]   cfg       Configuration buffer to populate.
953  * \param[in]    usage     Algorithm specific usage value. For AV1, must be
954  *                         set to AOM_USAGE_GOOD_QUALITY (0),
955  *                         AOM_USAGE_REALTIME (1), or AOM_USAGE_ALL_INTRA (2).
956  *
957  * \retval #AOM_CODEC_OK
958  *     The configuration was populated.
959  * \retval #AOM_CODEC_INCAPABLE
960  *     Interface is not an encoder interface.
961  * \retval #AOM_CODEC_INVALID_PARAM
962  *     A parameter was NULL, or the usage value was not recognized.
963  */
964 aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface,
965                                              aom_codec_enc_cfg_t *cfg,
966                                              unsigned int usage);
967 
968 /*!\brief Set or change configuration
969  *
970  * Reconfigures an encoder instance according to the given configuration.
971  *
972  * \param[in]    ctx     Pointer to this instance's context
973  * \param[in]    cfg     Configuration buffer to use
974  *
975  * \retval #AOM_CODEC_OK
976  *     The configuration was populated.
977  * \retval #AOM_CODEC_INCAPABLE
978  *     Interface is not an encoder interface.
979  * \retval #AOM_CODEC_INVALID_PARAM
980  *     A parameter was NULL, or the usage value was not recognized.
981  */
982 aom_codec_err_t aom_codec_enc_config_set(aom_codec_ctx_t *ctx,
983                                          const aom_codec_enc_cfg_t *cfg);
984 
985 /*!\brief Get global stream headers
986  *
987  * Retrieves a stream level global header packet, if supported by the codec.
988  * Calls to this function should be deferred until all configuration information
989  * has been passed to libaom. Otherwise the global header data may be
990  * invalidated by additional configuration changes.
991  *
992  * The AV1 implementation of this function returns an OBU. The OBU returned is
993  * in Low Overhead Bitstream Format. Specifically, the obu_has_size_field bit is
994  * set, and the buffer contains the obu_size field for the returned OBU.
995  *
996  * \param[in]    ctx     Pointer to this instance's context
997  *
998  * \retval NULL
999  *     Encoder does not support global header, or an error occurred while
1000  *     generating the global header.
1001  *
1002  * \retval Non-NULL
1003  *     Pointer to buffer containing global header packet. The caller owns the
1004  *     memory associated with this buffer, and must free the 'buf' member of the
1005  *     aom_fixed_buf_t as well as the aom_fixed_buf_t pointer. Memory returned
1006  *     must be freed via call to free().
1007  */
1008 aom_fixed_buf_t *aom_codec_get_global_headers(aom_codec_ctx_t *ctx);
1009 
1010 /*!\brief usage parameter analogous to AV1 GOOD QUALITY mode. */
1011 #define AOM_USAGE_GOOD_QUALITY (0)
1012 /*!\brief usage parameter analogous to AV1 REALTIME mode. */
1013 #define AOM_USAGE_REALTIME (1)
1014 /*!\brief usage parameter analogous to AV1 all intra mode. */
1015 #define AOM_USAGE_ALL_INTRA (2)
1016 
1017 /*!\brief Encode a frame
1018  *
1019  * Encodes a video frame at the given "presentation time." The presentation
1020  * time stamp (PTS) \ref MUST be strictly increasing.
1021  *
1022  * When the last frame has been passed to the encoder, this function should
1023  * continue to be called in a loop, with the img parameter set to NULL. This
1024  * will signal the end-of-stream condition to the encoder and allow it to
1025  * encode any held buffers. Encoding is complete when aom_codec_encode() is
1026  * called with img set to NULL and aom_codec_get_cx_data() returns no data.
1027  *
1028  * \param[in]    ctx       Pointer to this instance's context
1029  * \param[in]    img       Image data to encode, NULL to flush.
1030  * \param[in]    pts       Presentation time stamp, in timebase units. If img
1031  *                         is NULL, pts is ignored.
1032  * \param[in]    duration  Duration to show frame, in timebase units. If img
1033  *                         is not NULL, duration must be nonzero. If img is
1034  *                         NULL, duration is ignored.
1035  * \param[in]    flags     Flags to use for encoding this frame.
1036  *
1037  * \retval #AOM_CODEC_OK
1038  *     The configuration was populated.
1039  * \retval #AOM_CODEC_INCAPABLE
1040  *     Interface is not an encoder interface.
1041  * \retval #AOM_CODEC_INVALID_PARAM
1042  *     A parameter was NULL, the image format is unsupported, etc.
1043  */
1044 aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img,
1045                                  aom_codec_pts_t pts, unsigned long duration,
1046                                  aom_enc_frame_flags_t flags);
1047 
1048 /*!\brief Set compressed data output buffer
1049  *
1050  * Sets the buffer that the codec should output the compressed data
1051  * into. This call effectively sets the buffer pointer returned in the
1052  * next AOM_CODEC_CX_FRAME_PKT packet. Subsequent packets will be
1053  * appended into this buffer. The buffer is preserved across frames,
1054  * so applications must periodically call this function after flushing
1055  * the accumulated compressed data to disk or to the network to reset
1056  * the pointer to the buffer's head.
1057  *
1058  * `pad_before` bytes will be skipped before writing the compressed
1059  * data, and `pad_after` bytes will be appended to the packet. The size
1060  * of the packet will be the sum of the size of the actual compressed
1061  * data, pad_before, and pad_after. The padding bytes will be preserved
1062  * (not overwritten).
1063  *
1064  * Note that calling this function does not guarantee that the returned
1065  * compressed data will be placed into the specified buffer. In the
1066  * event that the encoded data will not fit into the buffer provided,
1067  * the returned packet \ref MAY point to an internal buffer, as it would
1068  * if this call were never used. In this event, the output packet will
1069  * NOT have any padding, and the application must free space and copy it
1070  * to the proper place. This is of particular note in configurations
1071  * that may output multiple packets for a single encoded frame (e.g., lagged
1072  * encoding) or if the application does not reset the buffer periodically.
1073  *
1074  * Applications may restore the default behavior of the codec providing
1075  * the compressed data buffer by calling this function with a NULL
1076  * buffer.
1077  *
1078  * Applications \ref MUSTNOT call this function during iteration of
1079  * aom_codec_get_cx_data().
1080  *
1081  * \param[in]    ctx         Pointer to this instance's context
1082  * \param[in]    buf         Buffer to store compressed data into
1083  * \param[in]    pad_before  Bytes to skip before writing compressed data
1084  * \param[in]    pad_after   Bytes to skip after writing compressed data
1085  *
1086  * \retval #AOM_CODEC_OK
1087  *     The buffer was set successfully.
1088  * \retval #AOM_CODEC_INVALID_PARAM
1089  *     A parameter was NULL, the image format is unsupported, etc.
1090  */
1091 aom_codec_err_t aom_codec_set_cx_data_buf(aom_codec_ctx_t *ctx,
1092                                           const aom_fixed_buf_t *buf,
1093                                           unsigned int pad_before,
1094                                           unsigned int pad_after);
1095 
1096 /*!\brief Encoded data iterator
1097  *
1098  * Iterates over a list of data packets to be passed from the encoder to the
1099  * application. The different kinds of packets available are enumerated in
1100  * #aom_codec_cx_pkt_kind.
1101  *
1102  * #AOM_CODEC_CX_FRAME_PKT packets should be passed to the application's
1103  * muxer. Multiple compressed frames may be in the list.
1104  * #AOM_CODEC_STATS_PKT packets should be appended to a global buffer.
1105  *
1106  * The application \ref MUST silently ignore any packet kinds that it does
1107  * not recognize or support.
1108  *
1109  * The data buffers returned from this function are only guaranteed to be
1110  * valid until the application makes another call to any aom_codec_* function.
1111  *
1112  * \param[in]     ctx      Pointer to this instance's context
1113  * \param[in,out] iter     Iterator storage, initialized to NULL
1114  *
1115  * \return Returns a pointer to an output data packet (compressed frame data,
1116  *         two-pass statistics, etc.) or NULL to signal end-of-list.
1117  *
1118  */
1119 const aom_codec_cx_pkt_t *aom_codec_get_cx_data(aom_codec_ctx_t *ctx,
1120                                                 aom_codec_iter_t *iter);
1121 
1122 /*!\brief Get Preview Frame
1123  *
1124  * Returns an image that can be used as a preview. Shows the image as it would
1125  * exist at the decompressor. The application \ref MUST NOT write into this
1126  * image buffer.
1127  *
1128  * \param[in]     ctx      Pointer to this instance's context
1129  *
1130  * \return Returns a pointer to a preview image, or NULL if no image is
1131  *         available.
1132  *
1133  */
1134 const aom_image_t *aom_codec_get_preview_frame(aom_codec_ctx_t *ctx);
1135 
1136 /*!@} - end defgroup encoder*/
1137 #ifdef __cplusplus
1138 }
1139 #endif
1140 #endif  // AOM_AOM_AOM_ENCODER_H_
1141