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