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