xref: /aosp_15_r20/external/libaom/doc/dev_guide/av1_encoder.dox (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker/*!\page encoder_guide AV1 ENCODER GUIDE
2*77c1e3ccSAndroid Build Coastguard Worker
3*77c1e3ccSAndroid Build Coastguard Worker\tableofcontents
4*77c1e3ccSAndroid Build Coastguard Worker
5*77c1e3ccSAndroid Build Coastguard Worker\section architecture_introduction Introduction
6*77c1e3ccSAndroid Build Coastguard Worker
7*77c1e3ccSAndroid Build Coastguard WorkerThis document provides an architectural overview of the libaom AV1 encoder.
8*77c1e3ccSAndroid Build Coastguard Worker
9*77c1e3ccSAndroid Build Coastguard WorkerIt is intended as a high level starting point for anyone wishing to contribute
10*77c1e3ccSAndroid Build Coastguard Workerto the project, that will help them to more quickly understand the structure
11*77c1e3ccSAndroid Build Coastguard Workerof the encoder and find their way around the codebase.
12*77c1e3ccSAndroid Build Coastguard Worker
13*77c1e3ccSAndroid Build Coastguard WorkerIt stands above and will where necessary link to more detailed function
14*77c1e3ccSAndroid Build Coastguard Workerlevel documents.
15*77c1e3ccSAndroid Build Coastguard Worker
16*77c1e3ccSAndroid Build Coastguard Worker\subsection  architecture_gencodecs Generic Block Transform Based Codecs
17*77c1e3ccSAndroid Build Coastguard Worker
18*77c1e3ccSAndroid Build Coastguard WorkerMost modern video encoders including VP8, H.264, VP9, HEVC and AV1
19*77c1e3ccSAndroid Build Coastguard Worker(in increasing order of complexity) share a common basic paradigm. This
20*77c1e3ccSAndroid Build Coastguard Workercomprises separating a stream of raw video frames into a series of discrete
21*77c1e3ccSAndroid Build Coastguard Workerblocks (of one or more sizes), then computing a prediction signal and a
22*77c1e3ccSAndroid Build Coastguard Workerquantized, transform coded, residual error signal. The prediction and residual
23*77c1e3ccSAndroid Build Coastguard Workererror signal, along with any side information needed by the decoder, are then
24*77c1e3ccSAndroid Build Coastguard Workerentropy coded and packed to form the encoded bitstream. See Figure 1: below,
25*77c1e3ccSAndroid Build Coastguard Workerwhere the blue blocks are, to all intents and purposes, the lossless parts of
26*77c1e3ccSAndroid Build Coastguard Workerthe encoder and the red block is the lossy part.
27*77c1e3ccSAndroid Build Coastguard Worker
28*77c1e3ccSAndroid Build Coastguard WorkerThis is of course a gross oversimplification, even in regard to the simplest
29*77c1e3ccSAndroid Build Coastguard Workerof the above codecs.  For example, all of them allow for block based
30*77c1e3ccSAndroid Build Coastguard Workerprediction at multiple different scales (i.e. different block sizes) and may
31*77c1e3ccSAndroid Build Coastguard Workeruse previously coded pixels in the current frame for prediction or pixels from
32*77c1e3ccSAndroid Build Coastguard Workerone or more previously encoded frames. Further, they may support multiple
33*77c1e3ccSAndroid Build Coastguard Workerdifferent transforms and transform sizes and quality optimization tools like
34*77c1e3ccSAndroid Build Coastguard Workerloop filtering.
35*77c1e3ccSAndroid Build Coastguard Worker
36*77c1e3ccSAndroid Build Coastguard Worker\image html genericcodecflow.png "" width=70%
37*77c1e3ccSAndroid Build Coastguard Worker
38*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_av1_structure AV1 Structure and Complexity
39*77c1e3ccSAndroid Build Coastguard Worker
40*77c1e3ccSAndroid Build Coastguard WorkerAs previously stated, AV1 adopts the same underlying paradigm as other block
41*77c1e3ccSAndroid Build Coastguard Workertransform based codecs. However, it is much more complicated than previous
42*77c1e3ccSAndroid Build Coastguard Workergeneration codecs and supports many more block partitioning, prediction and
43*77c1e3ccSAndroid Build Coastguard Workertransform options.
44*77c1e3ccSAndroid Build Coastguard Worker
45*77c1e3ccSAndroid Build Coastguard WorkerAV1 supports block partitions of various sizes from 128x128 pixels down to 4x4
46*77c1e3ccSAndroid Build Coastguard Workerpixels using a multi-layer recursive tree structure as illustrated in figure 2
47*77c1e3ccSAndroid Build Coastguard Workerbelow.
48*77c1e3ccSAndroid Build Coastguard Worker
49*77c1e3ccSAndroid Build Coastguard Worker\image html av1partitions.png "" width=70%
50*77c1e3ccSAndroid Build Coastguard Worker
51*77c1e3ccSAndroid Build Coastguard WorkerAV1 also provides 71 basic intra prediction modes, 56 single frame inter prediction
52*77c1e3ccSAndroid Build Coastguard Workermodes (7 reference frames x 4 modes x 2 for OBMC (overlapped block motion
53*77c1e3ccSAndroid Build Coastguard Workercompensation)), 12768 compound inter prediction modes (that combine inter
54*77c1e3ccSAndroid Build Coastguard Workerpredictors from two reference frames) and 36708 compound inter / intra
55*77c1e3ccSAndroid Build Coastguard Workerprediction modes. Furthermore, in addition to simple inter motion estimation,
56*77c1e3ccSAndroid Build Coastguard WorkerAV1 also supports warped motion prediction using affine transforms.
57*77c1e3ccSAndroid Build Coastguard Worker
58*77c1e3ccSAndroid Build Coastguard WorkerIn terms of transform coding, it has 16 separable 2-D transform kernels
59*77c1e3ccSAndroid Build Coastguard Worker\f$(DCT, ADST, fADST, IDTX)^2\f$ that can be applied at up to 19 different
60*77c1e3ccSAndroid Build Coastguard Workerscales from 64x64 down to 4x4 pixels.
61*77c1e3ccSAndroid Build Coastguard Worker
62*77c1e3ccSAndroid Build Coastguard WorkerWhen combined together, this means that for any one 8x8 pixel block in a
63*77c1e3ccSAndroid Build Coastguard Workersource frame, there are approximately 45,000,000 different ways that it can
64*77c1e3ccSAndroid Build Coastguard Workerbe encoded.
65*77c1e3ccSAndroid Build Coastguard Worker
66*77c1e3ccSAndroid Build Coastguard WorkerConsequently, AV1 requires complex control processes. While not necessarily
67*77c1e3ccSAndroid Build Coastguard Workera normative part of the bitstream, these are the algorithms that turn a set
68*77c1e3ccSAndroid Build Coastguard Workerof compression tools and a bitstream format specification, into a coherent
69*77c1e3ccSAndroid Build Coastguard Workerand useful codec implementation. These may include but are not limited to
70*77c1e3ccSAndroid Build Coastguard Workerthings like :-
71*77c1e3ccSAndroid Build Coastguard Worker
72*77c1e3ccSAndroid Build Coastguard Worker- Rate distortion optimization (The process of trying to choose the most
73*77c1e3ccSAndroid Build Coastguard Worker  efficient combination of block size, prediction mode, transform type
74*77c1e3ccSAndroid Build Coastguard Worker  etc.)
75*77c1e3ccSAndroid Build Coastguard Worker- Rate control (regulation of the output bitrate)
76*77c1e3ccSAndroid Build Coastguard Worker- Encoder speed vs quality trade offs.
77*77c1e3ccSAndroid Build Coastguard Worker- Features such as two pass encoding or optimization for low delay
78*77c1e3ccSAndroid Build Coastguard Worker  encoding.
79*77c1e3ccSAndroid Build Coastguard Worker
80*77c1e3ccSAndroid Build Coastguard WorkerFor a more detailed overview of AV1's encoding tools and a discussion of some
81*77c1e3ccSAndroid Build Coastguard Workerof the design considerations and hardware constraints that had to be
82*77c1e3ccSAndroid Build Coastguard Workeraccommodated, please refer to <a href="https://arxiv.org/abs/2008.06091">
83*77c1e3ccSAndroid Build Coastguard WorkerA Technical Overview of AV1</a>.
84*77c1e3ccSAndroid Build Coastguard Worker
85*77c1e3ccSAndroid Build Coastguard WorkerFigure 3 provides a slightly expanded but still simplistic view of the
86*77c1e3ccSAndroid Build Coastguard WorkerAV1 encoder architecture with blocks that relate to some of the subsequent
87*77c1e3ccSAndroid Build Coastguard Workersections of this document. In this diagram, the raw uncompressed frame buffers
88*77c1e3ccSAndroid Build Coastguard Workerare shown in dark green and the reconstructed frame buffers used for
89*77c1e3ccSAndroid Build Coastguard Workerprediction in light green. Red indicates those parts of the codec that are
90*77c1e3ccSAndroid Build Coastguard Worker(or may be) lossy, where fidelity can be traded off against compression
91*77c1e3ccSAndroid Build Coastguard Workerefficiency, whilst light blue shows algorithms or coding tools that are
92*77c1e3ccSAndroid Build Coastguard Workerlossless. The yellow blocks represent non-bitstream normative configuration
93*77c1e3ccSAndroid Build Coastguard Workerand control algorithms.
94*77c1e3ccSAndroid Build Coastguard Worker
95*77c1e3ccSAndroid Build Coastguard Worker\image html av1encoderflow.png "" width=70%
96*77c1e3ccSAndroid Build Coastguard Worker
97*77c1e3ccSAndroid Build Coastguard Worker\section architecture_command_line The Libaom Command Line Interface
98*77c1e3ccSAndroid Build Coastguard Worker
99*77c1e3ccSAndroid Build Coastguard Worker Add details or links here: TODO ? elliotk@
100*77c1e3ccSAndroid Build Coastguard Worker
101*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_data_structures Main Encoder Data Structures
102*77c1e3ccSAndroid Build Coastguard Worker
103*77c1e3ccSAndroid Build Coastguard WorkerThe following are the main high level data structures used by the libaom AV1
104*77c1e3ccSAndroid Build Coastguard Workerencoder and referenced elsewhere in this overview document:
105*77c1e3ccSAndroid Build Coastguard Worker
106*77c1e3ccSAndroid Build Coastguard Worker- \ref AV1_PRIMARY
107*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_PRIMARY.gf_group (\ref GF_GROUP)
108*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_PRIMARY.lap_enabled
109*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_PRIMARY.twopass (\ref TWO_PASS)
110*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_PRIMARY.p_rc (\ref PRIMARY_RATE_CONTROL)
111*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_PRIMARY.tf_info (\ref TEMPORAL_FILTER_INFO)
112*77c1e3ccSAndroid Build Coastguard Worker
113*77c1e3ccSAndroid Build Coastguard Worker- \ref AV1_COMP
114*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.oxcf (\ref AV1EncoderConfig)
115*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.rc (\ref RATE_CONTROL)
116*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.speed
117*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.sf (\ref SPEED_FEATURES)
118*77c1e3ccSAndroid Build Coastguard Worker
119*77c1e3ccSAndroid Build Coastguard Worker- \ref AV1EncoderConfig (Encoder configuration parameters)
120*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1EncoderConfig.pass
121*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1EncoderConfig.algo_cfg (\ref AlgoCfg)
122*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1EncoderConfig.kf_cfg (\ref KeyFrameCfg)
123*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1EncoderConfig.rc_cfg (\ref RateControlCfg)
124*77c1e3ccSAndroid Build Coastguard Worker
125*77c1e3ccSAndroid Build Coastguard Worker- \ref AlgoCfg (Algorithm related configuration parameters)
126*77c1e3ccSAndroid Build Coastguard Worker    - \ref AlgoCfg.arnr_max_frames
127*77c1e3ccSAndroid Build Coastguard Worker    - \ref AlgoCfg.arnr_strength
128*77c1e3ccSAndroid Build Coastguard Worker
129*77c1e3ccSAndroid Build Coastguard Worker- \ref KeyFrameCfg (Keyframe coding configuration parameters)
130*77c1e3ccSAndroid Build Coastguard Worker    - \ref KeyFrameCfg.enable_keyframe_filtering
131*77c1e3ccSAndroid Build Coastguard Worker
132*77c1e3ccSAndroid Build Coastguard Worker- \ref RateControlCfg (Rate control configuration)
133*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.mode
134*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.target_bandwidth
135*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.best_allowed_q
136*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.worst_allowed_q
137*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.cq_level
138*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.under_shoot_pct
139*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.over_shoot_pct
140*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.maximum_buffer_size_ms
141*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.starting_buffer_level_ms
142*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.optimal_buffer_level_ms
143*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.vbrbias
144*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.vbrmin_section
145*77c1e3ccSAndroid Build Coastguard Worker    - \ref RateControlCfg.vbrmax_section
146*77c1e3ccSAndroid Build Coastguard Worker
147*77c1e3ccSAndroid Build Coastguard Worker- \ref PRIMARY_RATE_CONTROL (Primary Rate control status)
148*77c1e3ccSAndroid Build Coastguard Worker    - \ref PRIMARY_RATE_CONTROL.gf_intervals[]
149*77c1e3ccSAndroid Build Coastguard Worker    - \ref PRIMARY_RATE_CONTROL.cur_gf_index
150*77c1e3ccSAndroid Build Coastguard Worker
151*77c1e3ccSAndroid Build Coastguard Worker- \ref RATE_CONTROL (Rate control status)
152*77c1e3ccSAndroid Build Coastguard Worker    - \ref RATE_CONTROL.intervals_till_gf_calculate_due
153*77c1e3ccSAndroid Build Coastguard Worker    - \ref RATE_CONTROL.frames_till_gf_update_due
154*77c1e3ccSAndroid Build Coastguard Worker    - \ref RATE_CONTROL.frames_to_key
155*77c1e3ccSAndroid Build Coastguard Worker
156*77c1e3ccSAndroid Build Coastguard Worker- \ref TWO_PASS (Two pass status and control data)
157*77c1e3ccSAndroid Build Coastguard Worker
158*77c1e3ccSAndroid Build Coastguard Worker- \ref GF_GROUP (Data related to the current GF/ARF group)
159*77c1e3ccSAndroid Build Coastguard Worker
160*77c1e3ccSAndroid Build Coastguard Worker- \ref FIRSTPASS_STATS (Defines entries in the first pass stats buffer)
161*77c1e3ccSAndroid Build Coastguard Worker    - \ref FIRSTPASS_STATS.coded_error
162*77c1e3ccSAndroid Build Coastguard Worker
163*77c1e3ccSAndroid Build Coastguard Worker- \ref SPEED_FEATURES (Encode speed vs quality tradeoff parameters)
164*77c1e3ccSAndroid Build Coastguard Worker    - \ref SPEED_FEATURES.hl_sf (\ref HIGH_LEVEL_SPEED_FEATURES)
165*77c1e3ccSAndroid Build Coastguard Worker
166*77c1e3ccSAndroid Build Coastguard Worker- \ref HIGH_LEVEL_SPEED_FEATURES
167*77c1e3ccSAndroid Build Coastguard Worker    - \ref HIGH_LEVEL_SPEED_FEATURES.recode_loop
168*77c1e3ccSAndroid Build Coastguard Worker    - \ref HIGH_LEVEL_SPEED_FEATURES.recode_tolerance
169*77c1e3ccSAndroid Build Coastguard Worker
170*77c1e3ccSAndroid Build Coastguard Worker- \ref TplParams
171*77c1e3ccSAndroid Build Coastguard Worker
172*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_use_cases Encoder Use Cases
173*77c1e3ccSAndroid Build Coastguard Worker
174*77c1e3ccSAndroid Build Coastguard WorkerThe libaom AV1 encoder is configurable to support a number of different use
175*77c1e3ccSAndroid Build Coastguard Workercases and rate control strategies.
176*77c1e3ccSAndroid Build Coastguard Worker
177*77c1e3ccSAndroid Build Coastguard WorkerThe principle use cases for which it is optimised are as follows:
178*77c1e3ccSAndroid Build Coastguard Worker
179*77c1e3ccSAndroid Build Coastguard Worker - <b>Video on Demand / Streaming</b>
180*77c1e3ccSAndroid Build Coastguard Worker - <b>Low Delay or Live Streaming</b>
181*77c1e3ccSAndroid Build Coastguard Worker - <b>Video Conferencing / Real Time Coding (RTC)</b>
182*77c1e3ccSAndroid Build Coastguard Worker - <b>Fixed Quality / Testing</b>
183*77c1e3ccSAndroid Build Coastguard Worker
184*77c1e3ccSAndroid Build Coastguard WorkerOther examples of use cases for which the encoder could be configured but for
185*77c1e3ccSAndroid Build Coastguard Workerwhich there is less by way of specific optimizations include:
186*77c1e3ccSAndroid Build Coastguard Worker
187*77c1e3ccSAndroid Build Coastguard Worker - <b>Download and Play</b>
188*77c1e3ccSAndroid Build Coastguard Worker - <b>Disk Playback</b>>
189*77c1e3ccSAndroid Build Coastguard Worker - <b>Storage</b>
190*77c1e3ccSAndroid Build Coastguard Worker - <b>Editing</b>
191*77c1e3ccSAndroid Build Coastguard Worker - <b>Broadcast video</b>
192*77c1e3ccSAndroid Build Coastguard Worker
193*77c1e3ccSAndroid Build Coastguard WorkerSpecific use cases may have particular requirements or constraints. For
194*77c1e3ccSAndroid Build Coastguard Workerexample:
195*77c1e3ccSAndroid Build Coastguard Worker
196*77c1e3ccSAndroid Build Coastguard Worker<b>Video Conferencing:</b>  In a video conference we need to encode the video
197*77c1e3ccSAndroid Build Coastguard Workerin real time and to avoid any coding tools that could increase latency, such
198*77c1e3ccSAndroid Build Coastguard Workeras frame look ahead.
199*77c1e3ccSAndroid Build Coastguard Worker
200*77c1e3ccSAndroid Build Coastguard Worker<b>Live Streams:</b> In cases such as live streaming of games or events, it
201*77c1e3ccSAndroid Build Coastguard Workermay be possible to allow some limited buffering of the video and use of
202*77c1e3ccSAndroid Build Coastguard Workerlookahead coding tools to improve encoding quality. However,  whilst a lag of
203*77c1e3ccSAndroid Build Coastguard Workera second or two may be fine given the one way nature of this type of video,
204*77c1e3ccSAndroid Build Coastguard Workerit is clearly not possible to use tools such as two pass coding.
205*77c1e3ccSAndroid Build Coastguard Worker
206*77c1e3ccSAndroid Build Coastguard Worker<b>Broadcast:</b> Broadcast video (e.g. digital TV over satellite) may have
207*77c1e3ccSAndroid Build Coastguard Workerspecific requirements such as frequent and regular key frames (e.g. once per
208*77c1e3ccSAndroid Build Coastguard Workersecond or more) as these are important as entry points to users when switching
209*77c1e3ccSAndroid Build Coastguard Workerchannels. There may also be  strict upper limits on bandwidth over a short
210*77c1e3ccSAndroid Build Coastguard Workerwindow of time.
211*77c1e3ccSAndroid Build Coastguard Worker
212*77c1e3ccSAndroid Build Coastguard Worker<b>Download and Play:</b> Download and play applications may have less strict
213*77c1e3ccSAndroid Build Coastguard Workerrequirements in terms of local frame by frame rate control but there may be a
214*77c1e3ccSAndroid Build Coastguard Workerrequirement to accurately hit a file size target for the video clip as a
215*77c1e3ccSAndroid Build Coastguard Workerwhole. Similar considerations may apply to playback from mass storage devices
216*77c1e3ccSAndroid Build Coastguard Workersuch as DVD or disk drives.
217*77c1e3ccSAndroid Build Coastguard Worker
218*77c1e3ccSAndroid Build Coastguard Worker<b>Editing:</b> In certain special use cases such as offline editing, it may
219*77c1e3ccSAndroid Build Coastguard Workerbe desirable to have very high quality and data rate but also very frequent
220*77c1e3ccSAndroid Build Coastguard Workerkey frames or indeed to encode the video exclusively as key frames. Lossless
221*77c1e3ccSAndroid Build Coastguard Workervideo encoding may also be required in this use case.
222*77c1e3ccSAndroid Build Coastguard Worker
223*77c1e3ccSAndroid Build Coastguard Worker<b>VOD / Streaming:</b> One of the most important and common use cases for AV1
224*77c1e3ccSAndroid Build Coastguard Workeris video on demand or streaming, for services such as YouTube and Netflix. In
225*77c1e3ccSAndroid Build Coastguard Workerthis use case it is possible to do two or even multi-pass encoding to improve
226*77c1e3ccSAndroid Build Coastguard Workercompression efficiency. Streaming services will often store many encoded
227*77c1e3ccSAndroid Build Coastguard Workercopies of a video at different resolutions and data rates to support users
228*77c1e3ccSAndroid Build Coastguard Workerwith different types of playback device and bandwidth limitations.
229*77c1e3ccSAndroid Build Coastguard WorkerFurthermore, these services support dynamic switching between multiple
230*77c1e3ccSAndroid Build Coastguard Workerstreams, so that they can respond to changing network conditions.
231*77c1e3ccSAndroid Build Coastguard Worker
232*77c1e3ccSAndroid Build Coastguard WorkerExact rate control when encoding for a specific format (e.g 360P or 1080P on
233*77c1e3ccSAndroid Build Coastguard WorkerYouTube) may not be critical, provided that the video bandwidth remains within
234*77c1e3ccSAndroid Build Coastguard Workerallowed limits. Whilst a format may have a nominal target data rate, this can
235*77c1e3ccSAndroid Build Coastguard Workerbe considered more as the desired average egress rate over the video corpus
236*77c1e3ccSAndroid Build Coastguard Workerrather than a strict requirement for any individual clip. Indeed, in order
237*77c1e3ccSAndroid Build Coastguard Workerto maintain optimal quality of experience for the end user, it may be
238*77c1e3ccSAndroid Build Coastguard Workerdesirable to encode some easier videos or sections of video at a lower data
239*77c1e3ccSAndroid Build Coastguard Workerrate and harder videos or sections at a higher rate.
240*77c1e3ccSAndroid Build Coastguard Worker
241*77c1e3ccSAndroid Build Coastguard WorkerVOD / streaming does not usually require very frequent key frames (as in the
242*77c1e3ccSAndroid Build Coastguard Workerbroadcast case) but key frames are important in trick play (scanning back and
243*77c1e3ccSAndroid Build Coastguard Workerforth to different points in a video) and for adaptive stream switching. As
244*77c1e3ccSAndroid Build Coastguard Workersuch, in a use case like YouTube, there is normally an upper limit on the
245*77c1e3ccSAndroid Build Coastguard Workermaximum time between key frames of a few seconds, but within certain limits
246*77c1e3ccSAndroid Build Coastguard Workerthe encoder can try to align key frames with real scene cuts.
247*77c1e3ccSAndroid Build Coastguard Worker
248*77c1e3ccSAndroid Build Coastguard WorkerWhilst encoder speed may not seem to be as critical in this use case, for
249*77c1e3ccSAndroid Build Coastguard Workerservices such as YouTube, where millions of new videos have to be encoded
250*77c1e3ccSAndroid Build Coastguard Workerevery day, encoder speed is still important, so libaom allows command line
251*77c1e3ccSAndroid Build Coastguard Workercontrol of the encode speed vs quality trade off.
252*77c1e3ccSAndroid Build Coastguard Worker
253*77c1e3ccSAndroid Build Coastguard Worker<b>Fixed Quality / Testing Mode:</b> Libaom also has a fixed quality encoder
254*77c1e3ccSAndroid Build Coastguard Workerpathway designed for testing under highly constrained conditions.
255*77c1e3ccSAndroid Build Coastguard Worker
256*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_speed_quality Speed vs Quality Trade Off
257*77c1e3ccSAndroid Build Coastguard Worker
258*77c1e3ccSAndroid Build Coastguard WorkerIn any modern video encoder there are trade offs that can be made in regard to
259*77c1e3ccSAndroid Build Coastguard Workerthe amount of time spent encoding a video or video frame vs the quality of the
260*77c1e3ccSAndroid Build Coastguard Workerfinal encode.
261*77c1e3ccSAndroid Build Coastguard Worker
262*77c1e3ccSAndroid Build Coastguard WorkerThese trade offs typically limit the scope of the search for an optimal
263*77c1e3ccSAndroid Build Coastguard Workerprediction / transform combination with faster encode modes doing fewer
264*77c1e3ccSAndroid Build Coastguard Workerpartition, reference frame, prediction mode and transform searches at the cost
265*77c1e3ccSAndroid Build Coastguard Workerof some reduction in coding efficiency.
266*77c1e3ccSAndroid Build Coastguard Worker
267*77c1e3ccSAndroid Build Coastguard WorkerThe pruning of the size of the search tree is typically based on assumptions
268*77c1e3ccSAndroid Build Coastguard Workerabout the likelihood of different search modes being selected based on what
269*77c1e3ccSAndroid Build Coastguard Workerhas gone before and features such as the dimensions of the video frames and
270*77c1e3ccSAndroid Build Coastguard Workerthe Q value selected for encoding the frame. For example certain intra modes
271*77c1e3ccSAndroid Build Coastguard Workerare less likely to be chosen at high Q but may be more likely if similar
272*77c1e3ccSAndroid Build Coastguard Workermodes were used for the previously coded blocks above and to the left of the
273*77c1e3ccSAndroid Build Coastguard Workercurrent block.
274*77c1e3ccSAndroid Build Coastguard Worker
275*77c1e3ccSAndroid Build Coastguard WorkerThe speed settings depend both on the use case (e.g. Real Time encoding) and
276*77c1e3ccSAndroid Build Coastguard Workeran explicit speed control passed in on the command line as <b>--cpu-used</b>
277*77c1e3ccSAndroid Build Coastguard Workerand stored in the \ref AV1_COMP.speed field of the main compressor instance
278*77c1e3ccSAndroid Build Coastguard Workerdata structure (<b>cpi</b>).
279*77c1e3ccSAndroid Build Coastguard Worker
280*77c1e3ccSAndroid Build Coastguard WorkerThe control flags for the speed trade off are stored the \ref AV1_COMP.sf
281*77c1e3ccSAndroid Build Coastguard Workerfield of the compressor instancve and are set in the following functions:-
282*77c1e3ccSAndroid Build Coastguard Worker
283*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_set_speed_features_framesize_independent()
284*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_set_speed_features_framesize_dependent()
285*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_set_speed_features_qindex_dependent()
286*77c1e3ccSAndroid Build Coastguard Worker
287*77c1e3ccSAndroid Build Coastguard WorkerA second factor impacting the speed of encode is rate distortion optimisation
288*77c1e3ccSAndroid Build Coastguard Worker(<b>rd vs non-rd</b> encoding).
289*77c1e3ccSAndroid Build Coastguard Worker
290*77c1e3ccSAndroid Build Coastguard WorkerWhen rate distortion optimization is enabled each candidate combination of
291*77c1e3ccSAndroid Build Coastguard Workera prediction mode and transform coding strategy is fully encoded and the
292*77c1e3ccSAndroid Build Coastguard Workerresulting error (or distortion) as compared to the original source and the
293*77c1e3ccSAndroid Build Coastguard Workernumber of bits used, are passed to a rate distortion function. This function
294*77c1e3ccSAndroid Build Coastguard Workerconverts the distortion and cost in bits to a single <b>RD</b> value (where
295*77c1e3ccSAndroid Build Coastguard Workerlower is better). This <b>RD</b> value is used to decide between different
296*77c1e3ccSAndroid Build Coastguard Workerencoding strategies for the current block where, for example, a one may
297*77c1e3ccSAndroid Build Coastguard Workerresult in a lower distortion but a larger number of bits.
298*77c1e3ccSAndroid Build Coastguard Worker
299*77c1e3ccSAndroid Build Coastguard WorkerThe calculation of this <b>RD</b> value is broadly speaking as follows:
300*77c1e3ccSAndroid Build Coastguard Worker
301*77c1e3ccSAndroid Build Coastguard Worker\f[
302*77c1e3ccSAndroid Build Coastguard Worker  RD = (&lambda; * Rate) + Distortion
303*77c1e3ccSAndroid Build Coastguard Worker\f]
304*77c1e3ccSAndroid Build Coastguard Worker
305*77c1e3ccSAndroid Build Coastguard WorkerThis assumes a linear relationship between the number of bits used and
306*77c1e3ccSAndroid Build Coastguard Workerdistortion (represented by the rate multiplier value <b>&lambda;</b>) which is
307*77c1e3ccSAndroid Build Coastguard Workernot actually valid across a broad range of rate and distortion values.
308*77c1e3ccSAndroid Build Coastguard WorkerTypically, where distortion is high, expending a small number of extra bits
309*77c1e3ccSAndroid Build Coastguard Workerwill result in a large change in distortion. However, at lower values of
310*77c1e3ccSAndroid Build Coastguard Workerdistortion the cost in bits of each incremental improvement is large.
311*77c1e3ccSAndroid Build Coastguard Worker
312*77c1e3ccSAndroid Build Coastguard WorkerTo deal with this we scale the value of <b>&lambda;</b> based on the quantizer
313*77c1e3ccSAndroid Build Coastguard Workervalue chosen for the frame. This is assumed to be a proxy for our approximate
314*77c1e3ccSAndroid Build Coastguard Workerposition on the true rate distortion curve and it is further assumed that over
315*77c1e3ccSAndroid Build Coastguard Workera limited range of distortion values, a linear relationship between distortion
316*77c1e3ccSAndroid Build Coastguard Workerand rate is a valid approximation.
317*77c1e3ccSAndroid Build Coastguard Worker
318*77c1e3ccSAndroid Build Coastguard WorkerDoing a rate distortion test on each candidate prediction / transform
319*77c1e3ccSAndroid Build Coastguard Workercombination is expensive in terms of cpu cycles. Hence, for cases where encode
320*77c1e3ccSAndroid Build Coastguard Workerspeed is critical, libaom implements a non-rd pathway where the <b>RD</b>
321*77c1e3ccSAndroid Build Coastguard Workervalue is estimated based on the prediction error and quantizer setting.
322*77c1e3ccSAndroid Build Coastguard Worker
323*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_src_proc Source Frame Processing
324*77c1e3ccSAndroid Build Coastguard Worker
325*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_frame_proc_data Main Data Structures
326*77c1e3ccSAndroid Build Coastguard Worker
327*77c1e3ccSAndroid Build Coastguard WorkerThe following are the main data structures referenced in this section
328*77c1e3ccSAndroid Build Coastguard Worker(see also \ref architecture_enc_data_structures):
329*77c1e3ccSAndroid Build Coastguard Worker
330*77c1e3ccSAndroid Build Coastguard Worker- \ref AV1_PRIMARY ppi (the primary compressor instance data structure)
331*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_PRIMARY.tf_info (\ref TEMPORAL_FILTER_INFO)
332*77c1e3ccSAndroid Build Coastguard Worker
333*77c1e3ccSAndroid Build Coastguard Worker- \ref AV1_COMP cpi (the main compressor instance data structure)
334*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.oxcf (\ref AV1EncoderConfig)
335*77c1e3ccSAndroid Build Coastguard Worker
336*77c1e3ccSAndroid Build Coastguard Worker- \ref AV1EncoderConfig (Encoder configuration parameters)
337*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1EncoderConfig.algo_cfg (\ref AlgoCfg)
338*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1EncoderConfig.kf_cfg (\ref KeyFrameCfg)
339*77c1e3ccSAndroid Build Coastguard Worker
340*77c1e3ccSAndroid Build Coastguard Worker- \ref AlgoCfg (Algorithm related configuration parameters)
341*77c1e3ccSAndroid Build Coastguard Worker    - \ref AlgoCfg.arnr_max_frames
342*77c1e3ccSAndroid Build Coastguard Worker    - \ref AlgoCfg.arnr_strength
343*77c1e3ccSAndroid Build Coastguard Worker
344*77c1e3ccSAndroid Build Coastguard Worker- \ref KeyFrameCfg (Keyframe coding configuration parameters)
345*77c1e3ccSAndroid Build Coastguard Worker    - \ref KeyFrameCfg.enable_keyframe_filtering
346*77c1e3ccSAndroid Build Coastguard Worker
347*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_frame_proc_ingest Frame Ingest / Coding Pipeline
348*77c1e3ccSAndroid Build Coastguard Worker
349*77c1e3ccSAndroid Build Coastguard Worker To encode a frame, first call \ref av1_receive_raw_frame() to obtain the raw
350*77c1e3ccSAndroid Build Coastguard Worker frame data. Then call \ref av1_get_compressed_data() to encode raw frame data
351*77c1e3ccSAndroid Build Coastguard Worker into compressed frame data. The main body of \ref av1_get_compressed_data()
352*77c1e3ccSAndroid Build Coastguard Worker is \ref av1_encode_strategy(), which determines high-level encode strategy
353*77c1e3ccSAndroid Build Coastguard Worker (frame type, frame placement, etc.) and then encodes the frame by calling
354*77c1e3ccSAndroid Build Coastguard Worker \ref av1_encode(). In \ref av1_encode(), \ref av1_first_pass() will execute
355*77c1e3ccSAndroid Build Coastguard Worker the first_pass of two-pass encoding, while \ref encode_frame_to_data_rate()
356*77c1e3ccSAndroid Build Coastguard Worker will perform the final pass for either one-pass or two-pass encoding.
357*77c1e3ccSAndroid Build Coastguard Worker
358*77c1e3ccSAndroid Build Coastguard Worker The main body of \ref encode_frame_to_data_rate() is
359*77c1e3ccSAndroid Build Coastguard Worker \ref encode_with_recode_loop_and_filter(), which handles encoding before
360*77c1e3ccSAndroid Build Coastguard Worker in-loop filters (with recode loops \ref encode_with_recode_loop(), or
361*77c1e3ccSAndroid Build Coastguard Worker without any recode loop \ref encode_without_recode()), followed by in-loop
362*77c1e3ccSAndroid Build Coastguard Worker filters (deblocking filters \ref loopfilter_frame(), CDEF filters and
363*77c1e3ccSAndroid Build Coastguard Worker restoration filters \ref cdef_restoration_frame()).
364*77c1e3ccSAndroid Build Coastguard Worker
365*77c1e3ccSAndroid Build Coastguard Worker Except for rate/quality control, both \ref encode_with_recode_loop() and
366*77c1e3ccSAndroid Build Coastguard Worker \ref encode_without_recode() call \ref av1_encode_frame() to manage the
367*77c1e3ccSAndroid Build Coastguard Worker reference frame buffers and \ref encode_frame_internal() to perform the
368*77c1e3ccSAndroid Build Coastguard Worker rest of encoding that does not require access to external frames.
369*77c1e3ccSAndroid Build Coastguard Worker \ref encode_frame_internal() is the starting point for the partition search
370*77c1e3ccSAndroid Build Coastguard Worker (see \ref architecture_enc_partitions).
371*77c1e3ccSAndroid Build Coastguard Worker
372*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_frame_proc_tf Temporal Filtering
373*77c1e3ccSAndroid Build Coastguard Worker
374*77c1e3ccSAndroid Build Coastguard Worker\subsubsection architecture_enc_frame_proc_tf_overview Overview
375*77c1e3ccSAndroid Build Coastguard Worker
376*77c1e3ccSAndroid Build Coastguard WorkerVideo codecs exploit the spatial and temporal correlations in video signals to
377*77c1e3ccSAndroid Build Coastguard Workerachieve compression efficiency. The noise factor in the source signal
378*77c1e3ccSAndroid Build Coastguard Workerattenuates such correlation and impedes the codec performance. Denoising the
379*77c1e3ccSAndroid Build Coastguard Workervideo signal is potentially a promising solution.
380*77c1e3ccSAndroid Build Coastguard Worker
381*77c1e3ccSAndroid Build Coastguard WorkerOne strategy for denoising a source is motion compensated temporal filtering.
382*77c1e3ccSAndroid Build Coastguard WorkerUnlike image denoising, where only the spatial information is available,
383*77c1e3ccSAndroid Build Coastguard Workervideo denoising can leverage a combination of the spatial and temporal
384*77c1e3ccSAndroid Build Coastguard Workerinformation. Specifically, in the temporal domain, similar pixels can often be
385*77c1e3ccSAndroid Build Coastguard Workertracked along the motion trajectory of moving objects. Motion estimation is
386*77c1e3ccSAndroid Build Coastguard Workerapplied to neighboring frames to find similar patches or blocks of pixels that
387*77c1e3ccSAndroid Build Coastguard Workercan be combined to create a temporally filtered output.
388*77c1e3ccSAndroid Build Coastguard Worker
389*77c1e3ccSAndroid Build Coastguard WorkerAV1, in common with VP8 and VP9, uses an in-loop motion compensated temporal
390*77c1e3ccSAndroid Build Coastguard Workerfilter to generate what are referred to as alternate reference frames (or ARF
391*77c1e3ccSAndroid Build Coastguard Workerframes). These can be encoded in the bitstream and stored as frame buffers for
392*77c1e3ccSAndroid Build Coastguard Workeruse in the prediction of subsequent frames, but are not usually directly
393*77c1e3ccSAndroid Build Coastguard Workerdisplayed (hence they are sometimes referred to as non-display frames).
394*77c1e3ccSAndroid Build Coastguard Worker
395*77c1e3ccSAndroid Build Coastguard WorkerThe following command line parameters set the strength of the filter, the
396*77c1e3ccSAndroid Build Coastguard Workernumber of frames used and determine whether filtering is allowed for key
397*77c1e3ccSAndroid Build Coastguard Workerframes.
398*77c1e3ccSAndroid Build Coastguard Worker
399*77c1e3ccSAndroid Build Coastguard Worker- <b>--arnr-strength</b> (\ref AlgoCfg.arnr_strength)
400*77c1e3ccSAndroid Build Coastguard Worker- <b>--arnr-maxframes</b> (\ref AlgoCfg.arnr_max_frames)
401*77c1e3ccSAndroid Build Coastguard Worker- <b>--enable-keyframe-filtering</b>
402*77c1e3ccSAndroid Build Coastguard Worker  (\ref KeyFrameCfg.enable_keyframe_filtering)
403*77c1e3ccSAndroid Build Coastguard Worker
404*77c1e3ccSAndroid Build Coastguard WorkerNote that in AV1, the temporal filtering scheme is designed around the
405*77c1e3ccSAndroid Build Coastguard Workerhierarchical ARF based pyramid coding structure. We typically apply denoising
406*77c1e3ccSAndroid Build Coastguard Workeronly on key frame and ARF frames at the highest (and sometimes the second
407*77c1e3ccSAndroid Build Coastguard Workerhighest) layer in the hierarchical coding structure.
408*77c1e3ccSAndroid Build Coastguard Worker
409*77c1e3ccSAndroid Build Coastguard Worker\subsubsection architecture_enc_frame_proc_tf_algo Temporal Filtering Algorithm
410*77c1e3ccSAndroid Build Coastguard Worker
411*77c1e3ccSAndroid Build Coastguard WorkerOur method divides the current frame into "MxM" blocks. For each block, a
412*77c1e3ccSAndroid Build Coastguard Workermotion search is applied on frames before and after the current frame. Only
413*77c1e3ccSAndroid Build Coastguard Workerthe best matching patch with the smallest mean square error (MSE) is kept as a
414*77c1e3ccSAndroid Build Coastguard Workercandidate patch for a neighbour frame. The current block is also a candidate
415*77c1e3ccSAndroid Build Coastguard Workerpatch. A total of N candidate patches are combined to generate the filtered
416*77c1e3ccSAndroid Build Coastguard Workeroutput.
417*77c1e3ccSAndroid Build Coastguard Worker
418*77c1e3ccSAndroid Build Coastguard WorkerLet f(i) represent the filtered sample value and \f$p_{j}(i)\f$ the sample
419*77c1e3ccSAndroid Build Coastguard Workervalue of the j-th patch. The filtering process is:
420*77c1e3ccSAndroid Build Coastguard Worker
421*77c1e3ccSAndroid Build Coastguard Worker\f[
422*77c1e3ccSAndroid Build Coastguard Worker  f(i) = \frac{p_{0}(i) + \sum_{j=1}^{N} &omega;_{j}(i).p_{j}(i)}
423*77c1e3ccSAndroid Build Coastguard Worker              {1 + \sum_{j=1}^{N} &omega;_{j}(i)}
424*77c1e3ccSAndroid Build Coastguard Worker\f]
425*77c1e3ccSAndroid Build Coastguard Worker
426*77c1e3ccSAndroid Build Coastguard Workerwhere \f$ &omega;_{j}(i) \f$ is the weight of the j-th patch from a total of
427*77c1e3ccSAndroid Build Coastguard WorkerN patches. The weight is determined by the patch difference as:
428*77c1e3ccSAndroid Build Coastguard Worker
429*77c1e3ccSAndroid Build Coastguard Worker\f[
430*77c1e3ccSAndroid Build Coastguard Worker  &omega;_{j}(i) = exp(-\frac{D_{j}(i)}{h^2})
431*77c1e3ccSAndroid Build Coastguard Worker\f]
432*77c1e3ccSAndroid Build Coastguard Worker
433*77c1e3ccSAndroid Build Coastguard Workerwhere \f$ D_{j}(i) \f$ is the sum of squared difference between the current
434*77c1e3ccSAndroid Build Coastguard Workerblock and the j-th candidate patch:
435*77c1e3ccSAndroid Build Coastguard Worker
436*77c1e3ccSAndroid Build Coastguard Worker\f[
437*77c1e3ccSAndroid Build Coastguard Worker  D_{j}(i) = \sum_{k\in&Omega;_{i}}||p_{0}(k) - p_{j}(k)||_{2}
438*77c1e3ccSAndroid Build Coastguard Worker\f]
439*77c1e3ccSAndroid Build Coastguard Worker
440*77c1e3ccSAndroid Build Coastguard Workerwhere:
441*77c1e3ccSAndroid Build Coastguard Worker- \f$p_{0}\f$ refers to the current frame.
442*77c1e3ccSAndroid Build Coastguard Worker- \f$&Omega;_{i}\f$ is the patch window, an "LxL" pixel square.
443*77c1e3ccSAndroid Build Coastguard Worker- h is a critical parameter that controls the decay of the weights measured by
444*77c1e3ccSAndroid Build Coastguard Worker  the Euclidean distance. It is derived from an estimate of noise amplitude in
445*77c1e3ccSAndroid Build Coastguard Worker  the source. This allows the filter coefficients to adapt for videos with
446*77c1e3ccSAndroid Build Coastguard Worker  different noise characteristics.
447*77c1e3ccSAndroid Build Coastguard Worker- Usually, M = 32, N = 7, and L = 5, but they can be adjusted.
448*77c1e3ccSAndroid Build Coastguard Worker
449*77c1e3ccSAndroid Build Coastguard WorkerIt is recommended that the reader refers to the code for more details.
450*77c1e3ccSAndroid Build Coastguard Worker
451*77c1e3ccSAndroid Build Coastguard Worker\subsubsection architecture_enc_frame_proc_tf_funcs Temporal Filter Functions
452*77c1e3ccSAndroid Build Coastguard Worker
453*77c1e3ccSAndroid Build Coastguard WorkerThe main entry point for temporal filtering is \ref av1_temporal_filter().
454*77c1e3ccSAndroid Build Coastguard WorkerThis function returns 1 if temporal filtering is successful, otherwise 0.
455*77c1e3ccSAndroid Build Coastguard WorkerWhen temporal filtering is applied, the filtered frame will be held in
456*77c1e3ccSAndroid Build Coastguard Workerthe output_frame, which is the frame to be
457*77c1e3ccSAndroid Build Coastguard Workerencoded in the following encoding process.
458*77c1e3ccSAndroid Build Coastguard Worker
459*77c1e3ccSAndroid Build Coastguard WorkerAlmost all temporal filter related code is in av1/encoder/temporal_filter.c
460*77c1e3ccSAndroid Build Coastguard Workerand av1/encoder/temporal_filter.h.
461*77c1e3ccSAndroid Build Coastguard Worker
462*77c1e3ccSAndroid Build Coastguard WorkerInside \ref av1_temporal_filter(), the reader's attention is directed to
463*77c1e3ccSAndroid Build Coastguard Worker\ref tf_setup_filtering_buffer() and \ref tf_do_filtering().
464*77c1e3ccSAndroid Build Coastguard Worker
465*77c1e3ccSAndroid Build Coastguard Worker- \ref tf_setup_filtering_buffer(): sets up the frame buffer for
466*77c1e3ccSAndroid Build Coastguard Worker  temporal filtering, determines the number of frames to be used, and
467*77c1e3ccSAndroid Build Coastguard Worker  calculates the noise level of each frame.
468*77c1e3ccSAndroid Build Coastguard Worker
469*77c1e3ccSAndroid Build Coastguard Worker- \ref tf_do_filtering(): the main function for the temporal
470*77c1e3ccSAndroid Build Coastguard Worker  filtering algorithm. It breaks each frame into "MxM" blocks. For each
471*77c1e3ccSAndroid Build Coastguard Worker  block a motion search \ref tf_motion_search() is applied to find
472*77c1e3ccSAndroid Build Coastguard Worker  the motion vector from one neighboring frame. tf_build_predictor() is then
473*77c1e3ccSAndroid Build Coastguard Worker  called to build the matching patch and \ref av1_apply_temporal_filter_c() (see
474*77c1e3ccSAndroid Build Coastguard Worker  also optimised SIMD versions) to apply temporal filtering. The weighted
475*77c1e3ccSAndroid Build Coastguard Worker  average over each pixel is accumulated and finally normalized in
476*77c1e3ccSAndroid Build Coastguard Worker  \ref tf_normalize_filtered_frame() to generate the final filtered frame.
477*77c1e3ccSAndroid Build Coastguard Worker
478*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_apply_temporal_filter_c(): the core function of our temporal
479*77c1e3ccSAndroid Build Coastguard Worker  filtering algorithm (see also optimised SIMD versions).
480*77c1e3ccSAndroid Build Coastguard Worker
481*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_frame_proc_film Film Grain Modelling
482*77c1e3ccSAndroid Build Coastguard Worker
483*77c1e3ccSAndroid Build Coastguard Worker Add details here.
484*77c1e3ccSAndroid Build Coastguard Worker
485*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_rate_ctrl Rate Control
486*77c1e3ccSAndroid Build Coastguard Worker
487*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_rate_ctrl_data Main Data Structures
488*77c1e3ccSAndroid Build Coastguard Worker
489*77c1e3ccSAndroid Build Coastguard WorkerThe following are the main data structures referenced in this section
490*77c1e3ccSAndroid Build Coastguard Worker(see also \ref architecture_enc_data_structures):
491*77c1e3ccSAndroid Build Coastguard Worker
492*77c1e3ccSAndroid Build Coastguard Worker - \ref AV1_PRIMARY ppi (the primary compressor instance data structure)
493*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_PRIMARY.twopass (\ref TWO_PASS)
494*77c1e3ccSAndroid Build Coastguard Worker
495*77c1e3ccSAndroid Build Coastguard Worker - \ref AV1_COMP cpi (the main compressor instance data structure)
496*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.oxcf (\ref AV1EncoderConfig)
497*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.rc (\ref RATE_CONTROL)
498*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.sf (\ref SPEED_FEATURES)
499*77c1e3ccSAndroid Build Coastguard Worker
500*77c1e3ccSAndroid Build Coastguard Worker - \ref AV1EncoderConfig (Encoder configuration parameters)
501*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1EncoderConfig.rc_cfg (\ref RateControlCfg)
502*77c1e3ccSAndroid Build Coastguard Worker
503*77c1e3ccSAndroid Build Coastguard Worker - \ref FIRSTPASS_STATS *frame_stats_buf (used to store per frame first
504*77c1e3ccSAndroid Build Coastguard Worker   pass stats)
505*77c1e3ccSAndroid Build Coastguard Worker
506*77c1e3ccSAndroid Build Coastguard Worker - \ref SPEED_FEATURES (Encode speed vs quality tradeoff parameters)
507*77c1e3ccSAndroid Build Coastguard Worker    - \ref SPEED_FEATURES.hl_sf (\ref HIGH_LEVEL_SPEED_FEATURES)
508*77c1e3ccSAndroid Build Coastguard Worker
509*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_rate_ctrl_options Supported Rate Control Options
510*77c1e3ccSAndroid Build Coastguard Worker
511*77c1e3ccSAndroid Build Coastguard WorkerDifferent use cases (\ref architecture_enc_use_cases) may have different
512*77c1e3ccSAndroid Build Coastguard Workerrequirements in terms of data rate control.
513*77c1e3ccSAndroid Build Coastguard Worker
514*77c1e3ccSAndroid Build Coastguard WorkerThe broad rate control strategy is selected using the <b>--end-usage</b>
515*77c1e3ccSAndroid Build Coastguard Workerparameter on the command line, which maps onto the field
516*77c1e3ccSAndroid Build Coastguard Worker\ref aom_codec_enc_cfg_t.rc_end_usage in \ref aom_encoder.h.
517*77c1e3ccSAndroid Build Coastguard Worker
518*77c1e3ccSAndroid Build Coastguard WorkerThe four supported options are:-
519*77c1e3ccSAndroid Build Coastguard Worker
520*77c1e3ccSAndroid Build Coastguard Worker- <b>VBR</b> (Variable Bitrate)
521*77c1e3ccSAndroid Build Coastguard Worker- <b>CBR</b> (Constant Bitrate)
522*77c1e3ccSAndroid Build Coastguard Worker- <b>CQ</b> (Constrained Quality mode ; A constrained variant of VBR)
523*77c1e3ccSAndroid Build Coastguard Worker- <b>Fixed Q</b> (Constant quality of Q mode)
524*77c1e3ccSAndroid Build Coastguard Worker
525*77c1e3ccSAndroid Build Coastguard WorkerThe value of \ref aom_codec_enc_cfg_t.rc_end_usage is in turn copied over
526*77c1e3ccSAndroid Build Coastguard Workerinto the encoder rate control configuration data structure as
527*77c1e3ccSAndroid Build Coastguard Worker\ref RateControlCfg.mode.
528*77c1e3ccSAndroid Build Coastguard Worker
529*77c1e3ccSAndroid Build Coastguard WorkerIn regards to the most important use cases above, Video on demand uses either
530*77c1e3ccSAndroid Build Coastguard WorkerVBR or CQ mode. CBR is the preferred rate control model for RTC and Live
531*77c1e3ccSAndroid Build Coastguard Workerstreaming and Fixed Q is only used in testing.
532*77c1e3ccSAndroid Build Coastguard Worker
533*77c1e3ccSAndroid Build Coastguard WorkerThe behaviour of each of these modes is regulated by a series of secondary
534*77c1e3ccSAndroid Build Coastguard Workercommand line rate control options but also depends somewhat on the selected
535*77c1e3ccSAndroid Build Coastguard Workeruse case, whether 2-pass coding is enabled and the selected encode speed vs
536*77c1e3ccSAndroid Build Coastguard Workerquality trade offs (\ref AV1_COMP.speed and \ref AV1_COMP.sf).
537*77c1e3ccSAndroid Build Coastguard Worker
538*77c1e3ccSAndroid Build Coastguard WorkerThe list below gives the names of the main rate control command line
539*77c1e3ccSAndroid Build Coastguard Workeroptions together with the names of the corresponding fields in the rate
540*77c1e3ccSAndroid Build Coastguard Workercontrol configuration data structures.
541*77c1e3ccSAndroid Build Coastguard Worker
542*77c1e3ccSAndroid Build Coastguard Worker- <b>--target-bitrate</b> (\ref RateControlCfg.target_bandwidth)
543*77c1e3ccSAndroid Build Coastguard Worker- <b>--min-q</b> (\ref RateControlCfg.best_allowed_q)
544*77c1e3ccSAndroid Build Coastguard Worker- <b>--max-q</b> (\ref RateControlCfg.worst_allowed_q)
545*77c1e3ccSAndroid Build Coastguard Worker- <b>--cq-level</b> (\ref RateControlCfg.cq_level)
546*77c1e3ccSAndroid Build Coastguard Worker- <b>--undershoot-pct</b> (\ref RateControlCfg.under_shoot_pct)
547*77c1e3ccSAndroid Build Coastguard Worker- <b>--overshoot-pct</b> (\ref RateControlCfg.over_shoot_pct)
548*77c1e3ccSAndroid Build Coastguard Worker
549*77c1e3ccSAndroid Build Coastguard WorkerThe following control aspects of vbr encoding
550*77c1e3ccSAndroid Build Coastguard Worker
551*77c1e3ccSAndroid Build Coastguard Worker- <b>--bias-pct</b> (\ref RateControlCfg.vbrbias)
552*77c1e3ccSAndroid Build Coastguard Worker- <b>--minsection-pct</b> ((\ref RateControlCfg.vbrmin_section)
553*77c1e3ccSAndroid Build Coastguard Worker- <b>--maxsection-pct</b> ((\ref RateControlCfg.vbrmax_section)
554*77c1e3ccSAndroid Build Coastguard Worker
555*77c1e3ccSAndroid Build Coastguard WorkerThe following relate to buffer and delay management in one pass low delay and
556*77c1e3ccSAndroid Build Coastguard Workerreal time coding
557*77c1e3ccSAndroid Build Coastguard Worker
558*77c1e3ccSAndroid Build Coastguard Worker- <b>--buf-sz</b> (\ref RateControlCfg.maximum_buffer_size_ms)
559*77c1e3ccSAndroid Build Coastguard Worker- <b>--buf-initial-sz</b> (\ref RateControlCfg.starting_buffer_level_ms)
560*77c1e3ccSAndroid Build Coastguard Worker- <b>--buf-optimal-sz</b> (\ref RateControlCfg.optimal_buffer_level_ms)
561*77c1e3ccSAndroid Build Coastguard Worker
562*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_vbr Variable Bitrate (VBR) Encoding
563*77c1e3ccSAndroid Build Coastguard Worker
564*77c1e3ccSAndroid Build Coastguard WorkerFor streamed VOD content the most common rate control strategy is Variable
565*77c1e3ccSAndroid Build Coastguard WorkerBitrate (VBR) encoding. The CQ mode mentioned above is a variant of this
566*77c1e3ccSAndroid Build Coastguard Workerwhere additional quantizer and quality constraints are applied.  VBR
567*77c1e3ccSAndroid Build Coastguard Workerencoding may in theory be used in conjunction with either 1-pass or 2-pass
568*77c1e3ccSAndroid Build Coastguard Workerencoding.
569*77c1e3ccSAndroid Build Coastguard Worker
570*77c1e3ccSAndroid Build Coastguard WorkerVBR encoding varies the number of bits given to each frame or group of frames
571*77c1e3ccSAndroid Build Coastguard Workeraccording to the difficulty of that frame or group of frames, such that easier
572*77c1e3ccSAndroid Build Coastguard Workerframes are allocated fewer bits and harder frames are allocated more bits. The
573*77c1e3ccSAndroid Build Coastguard Workerintent here is to even out the quality between frames. This contrasts with
574*77c1e3ccSAndroid Build Coastguard WorkerConstant Bitrate (CBR) encoding where each frame is allocated the same number
575*77c1e3ccSAndroid Build Coastguard Workerof bits.
576*77c1e3ccSAndroid Build Coastguard Worker
577*77c1e3ccSAndroid Build Coastguard WorkerWhilst for any given frame or group of frames the data rate may vary, the VBR
578*77c1e3ccSAndroid Build Coastguard Workeralgorithm attempts to deliver a given average bitrate over a wider time
579*77c1e3ccSAndroid Build Coastguard Workerinterval. In standard VBR encoding, the time interval over which the data rate
580*77c1e3ccSAndroid Build Coastguard Workeris averaged is usually the duration of the video clip.  An alternative
581*77c1e3ccSAndroid Build Coastguard Workerapproach is to target an average VBR bitrate over the entire video corpus for
582*77c1e3ccSAndroid Build Coastguard Workera particular video format (corpus VBR).
583*77c1e3ccSAndroid Build Coastguard Worker
584*77c1e3ccSAndroid Build Coastguard Worker\subsubsection architecture_enc_1pass_vbr 1 Pass VBR Encoding
585*77c1e3ccSAndroid Build Coastguard Worker
586*77c1e3ccSAndroid Build Coastguard WorkerThe command line for libaom does allow 1 Pass VBR, but this has not been
587*77c1e3ccSAndroid Build Coastguard Workerproperly optimised and behaves much like 1 pass CBR in most regards, with bits
588*77c1e3ccSAndroid Build Coastguard Workerallocated to frames by the following functions:
589*77c1e3ccSAndroid Build Coastguard Worker
590*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_calc_iframe_target_size_one_pass_vbr(
591*77c1e3ccSAndroid Build Coastguard Worker           const struct AV1_COMP *const cpi)
592*77c1e3ccSAndroid Build Coastguard Worker       "av1_calc_iframe_target_size_one_pass_vbr()"
593*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_calc_pframe_target_size_one_pass_vbr(
594*77c1e3ccSAndroid Build Coastguard Worker           const struct AV1_COMP *const cpi,
595*77c1e3ccSAndroid Build Coastguard Worker           FRAME_UPDATE_TYPE frame_update_type)
596*77c1e3ccSAndroid Build Coastguard Worker       "av1_calc_pframe_target_size_one_pass_vbr()"
597*77c1e3ccSAndroid Build Coastguard Worker
598*77c1e3ccSAndroid Build Coastguard Worker\subsubsection architecture_enc_2pass_vbr 2 Pass VBR Encoding
599*77c1e3ccSAndroid Build Coastguard Worker
600*77c1e3ccSAndroid Build Coastguard WorkerThe main focus here will be on 2-pass VBR encoding (and the related CQ mode)
601*77c1e3ccSAndroid Build Coastguard Workeras these are the modes most commonly used for VOD content.
602*77c1e3ccSAndroid Build Coastguard Worker
603*77c1e3ccSAndroid Build Coastguard Worker2-pass encoding is selected on the command line by setting --passes=2
604*77c1e3ccSAndroid Build Coastguard Worker(or -p 2).
605*77c1e3ccSAndroid Build Coastguard Worker
606*77c1e3ccSAndroid Build Coastguard WorkerGenerally speaking, in 2-pass encoding, an encoder will first encode a video
607*77c1e3ccSAndroid Build Coastguard Workerusing a default set of parameters and assumptions. Depending on the outcome
608*77c1e3ccSAndroid Build Coastguard Workerof that first encode, the baseline assumptions and parameters will be adjusted
609*77c1e3ccSAndroid Build Coastguard Workerto optimize the output during the second pass.  In essence the first pass is a
610*77c1e3ccSAndroid Build Coastguard Workerfact finding mission to establish the complexity and variability of the video,
611*77c1e3ccSAndroid Build Coastguard Workerin order to allow a better allocation of bits in the second pass.
612*77c1e3ccSAndroid Build Coastguard Worker
613*77c1e3ccSAndroid Build Coastguard WorkerThe libaom 2-pass algorithm is unusual in that the first pass is not a full
614*77c1e3ccSAndroid Build Coastguard Workerencode of the video. Rather it uses a limited set of prediction and transform
615*77c1e3ccSAndroid Build Coastguard Workeroptions and a fixed quantizer,  to generate statistics about each frame. No
616*77c1e3ccSAndroid Build Coastguard Workeroutput bitstream is created and the per frame first pass statistics are stored
617*77c1e3ccSAndroid Build Coastguard Workerentirely in volatile memory. This has some disadvantages when compared to a
618*77c1e3ccSAndroid Build Coastguard Workerfull first pass encode, but avoids the need for file I/O and improves speed.
619*77c1e3ccSAndroid Build Coastguard Worker
620*77c1e3ccSAndroid Build Coastguard WorkerFor two pass encoding, the function \ref av1_encode() will first be called
621*77c1e3ccSAndroid Build Coastguard Workerfor each frame in the video with the value \ref AV1EncoderConfig.pass = 1.
622*77c1e3ccSAndroid Build Coastguard WorkerThis will result in calls to \ref av1_first_pass().
623*77c1e3ccSAndroid Build Coastguard Worker
624*77c1e3ccSAndroid Build Coastguard WorkerStatistics for each frame are stored in \ref FIRSTPASS_STATS frame_stats_buf.
625*77c1e3ccSAndroid Build Coastguard Worker
626*77c1e3ccSAndroid Build Coastguard WorkerAfter completion of the first pass, \ref av1_encode() will be called again for
627*77c1e3ccSAndroid Build Coastguard Workereach frame with \ref AV1EncoderConfig.pass = 2.  The frames are then encoded in
628*77c1e3ccSAndroid Build Coastguard Workeraccordance with the statistics gathered during the first pass by calls to
629*77c1e3ccSAndroid Build Coastguard Worker\ref encode_frame_to_data_rate() which in turn calls
630*77c1e3ccSAndroid Build Coastguard Worker \ref av1_get_second_pass_params().
631*77c1e3ccSAndroid Build Coastguard Worker
632*77c1e3ccSAndroid Build Coastguard WorkerIn summary the second pass code :-
633*77c1e3ccSAndroid Build Coastguard Worker
634*77c1e3ccSAndroid Build Coastguard Worker- Searches for scene cuts (if auto key frame detection is enabled).
635*77c1e3ccSAndroid Build Coastguard Worker- Defines the length of and hierarchical structure to be used in each
636*77c1e3ccSAndroid Build Coastguard Worker  ARF/GF group.
637*77c1e3ccSAndroid Build Coastguard Worker- Allocates bits based on the relative complexity of each frame, the quality
638*77c1e3ccSAndroid Build Coastguard Worker  of frame to frame prediction and the type of frame (e.g. key frame, ARF
639*77c1e3ccSAndroid Build Coastguard Worker  frame, golden frame or normal leaf frame).
640*77c1e3ccSAndroid Build Coastguard Worker- Suggests a maximum Q (quantizer value) for each ARF/GF group, based on
641*77c1e3ccSAndroid Build Coastguard Worker  estimated complexity and recent rate control compliance
642*77c1e3ccSAndroid Build Coastguard Worker  (\ref RATE_CONTROL.active_worst_quality)
643*77c1e3ccSAndroid Build Coastguard Worker- Tracks adherence to the overall rate control objectives and adjusts
644*77c1e3ccSAndroid Build Coastguard Worker  heuristics.
645*77c1e3ccSAndroid Build Coastguard Worker
646*77c1e3ccSAndroid Build Coastguard WorkerThe main two pass functions in regard to the above include:-
647*77c1e3ccSAndroid Build Coastguard Worker
648*77c1e3ccSAndroid Build Coastguard Worker- \ref find_next_key_frame()
649*77c1e3ccSAndroid Build Coastguard Worker- \ref define_gf_group()
650*77c1e3ccSAndroid Build Coastguard Worker- \ref calculate_total_gf_group_bits()
651*77c1e3ccSAndroid Build Coastguard Worker- \ref get_twopass_worst_quality()
652*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_gop_setup_structure()
653*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_gop_bit_allocation()
654*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_twopass_postencode_update()
655*77c1e3ccSAndroid Build Coastguard Worker
656*77c1e3ccSAndroid Build Coastguard WorkerFor each frame, the two pass algorithm defines a target number of bits
657*77c1e3ccSAndroid Build Coastguard Worker\ref RATE_CONTROL.base_frame_target,  which is then adjusted if necessary to
658*77c1e3ccSAndroid Build Coastguard Workerreflect any undershoot or overshoot on previous frames to give
659*77c1e3ccSAndroid Build Coastguard Worker\ref RATE_CONTROL.this_frame_target.
660*77c1e3ccSAndroid Build Coastguard Worker
661*77c1e3ccSAndroid Build Coastguard WorkerAs well as \ref RATE_CONTROL.active_worst_quality, the two pass code also
662*77c1e3ccSAndroid Build Coastguard Workermaintains a record of the actual Q value used to encode previous frames
663*77c1e3ccSAndroid Build Coastguard Workerat each level in the current pyramid hierarchy
664*77c1e3ccSAndroid Build Coastguard Worker(\ref PRIMARY_RATE_CONTROL.active_best_quality). The function
665*77c1e3ccSAndroid Build Coastguard Worker\ref rc_pick_q_and_bounds(), uses these values to set a permitted Q range
666*77c1e3ccSAndroid Build Coastguard Workerfor each frame.
667*77c1e3ccSAndroid Build Coastguard Worker
668*77c1e3ccSAndroid Build Coastguard Worker\subsubsection architecture_enc_1pass_lagged 1 Pass Lagged VBR Encoding
669*77c1e3ccSAndroid Build Coastguard Worker
670*77c1e3ccSAndroid Build Coastguard Worker1 pass lagged encode falls between simple 1 pass encoding and full two pass
671*77c1e3ccSAndroid Build Coastguard Workerencoding and is used for cases where it is not possible to do a full first
672*77c1e3ccSAndroid Build Coastguard Workerpass through the entire video clip, but where some delay is permissible. For
673*77c1e3ccSAndroid Build Coastguard Workerexample near live streaming where there is a delay of up to a few seconds. In
674*77c1e3ccSAndroid Build Coastguard Workerthis case the first pass and second pass are in effect combined such that the
675*77c1e3ccSAndroid Build Coastguard Workerfirst pass starts encoding the clip and the second pass lags behind it by a
676*77c1e3ccSAndroid Build Coastguard Workerfew frames.  When using this method, full sequence level statistics are not
677*77c1e3ccSAndroid Build Coastguard Workeravailable, but it is possible to collect and use frame or group of frame level
678*77c1e3ccSAndroid Build Coastguard Workerdata to help in the allocation of bits and in defining ARF/GF coding
679*77c1e3ccSAndroid Build Coastguard Workerhierarchies.  The reader is referred to the \ref AV1_PRIMARY.lap_enabled field
680*77c1e3ccSAndroid Build Coastguard Workerin the main compressor instance (where <b>lap</b> stands for
681*77c1e3ccSAndroid Build Coastguard Worker<b>look ahead processing</b>). This encoding mode for the most part uses the
682*77c1e3ccSAndroid Build Coastguard Workersame rate control pathways as two pass VBR encoding.
683*77c1e3ccSAndroid Build Coastguard Worker
684*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_rc_loop The Main Rate Control Loop
685*77c1e3ccSAndroid Build Coastguard Worker
686*77c1e3ccSAndroid Build Coastguard WorkerHaving established a target rate for a given frame and an allowed range of Q
687*77c1e3ccSAndroid Build Coastguard Workervalues, the encoder then tries to encode the frame at a rate that is as close
688*77c1e3ccSAndroid Build Coastguard Workeras possible to the target value, given the Q range constraints.
689*77c1e3ccSAndroid Build Coastguard Worker
690*77c1e3ccSAndroid Build Coastguard WorkerThere are two main mechanisms by which this is achieved.
691*77c1e3ccSAndroid Build Coastguard Worker
692*77c1e3ccSAndroid Build Coastguard WorkerThe first selects a frame level Q, using an adaptive estimate of the number of
693*77c1e3ccSAndroid Build Coastguard Workerbits that will be generated when the frame is encoded at any given Q.
694*77c1e3ccSAndroid Build Coastguard WorkerFundamentally this mechanism is common to VBR, CBR and to use cases such as
695*77c1e3ccSAndroid Build Coastguard WorkerRTC with small adjustments.
696*77c1e3ccSAndroid Build Coastguard Worker
697*77c1e3ccSAndroid Build Coastguard WorkerAs the Q value mainly adjusts the precision of the residual signal, it is not
698*77c1e3ccSAndroid Build Coastguard Workeractually a reliable basis for accurately predicting the number of bits that
699*77c1e3ccSAndroid Build Coastguard Workerwill be generated across all clips. A well predicted clip, for example, may
700*77c1e3ccSAndroid Build Coastguard Workerhave a much smaller error residual after prediction.  The algorithm copes with
701*77c1e3ccSAndroid Build Coastguard Workerthis by adapting its predictions on the fly using a feedback loop based on how
702*77c1e3ccSAndroid Build Coastguard Workerwell it did the previous time around.
703*77c1e3ccSAndroid Build Coastguard Worker
704*77c1e3ccSAndroid Build Coastguard WorkerThe main functions responsible for the prediction of Q and the adaptation over
705*77c1e3ccSAndroid Build Coastguard Workertime, for the two pass encoding pipeline are:
706*77c1e3ccSAndroid Build Coastguard Worker
707*77c1e3ccSAndroid Build Coastguard Worker- \ref rc_pick_q_and_bounds()
708*77c1e3ccSAndroid Build Coastguard Worker    - \ref get_q()
709*77c1e3ccSAndroid Build Coastguard Worker        - \ref av1_rc_regulate_q(
710*77c1e3ccSAndroid Build Coastguard Worker                   const struct AV1_COMP *cpi, int target_bits_per_frame,
711*77c1e3ccSAndroid Build Coastguard Worker                   int active_best_quality, int active_worst_quality,
712*77c1e3ccSAndroid Build Coastguard Worker                   int width, int height) "av1_rc_regulate_q()"
713*77c1e3ccSAndroid Build Coastguard Worker        - \ref get_rate_correction_factor()
714*77c1e3ccSAndroid Build Coastguard Worker        - \ref set_rate_correction_factor()
715*77c1e3ccSAndroid Build Coastguard Worker        - \ref find_closest_qindex_by_rate()
716*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_twopass_postencode_update()
717*77c1e3ccSAndroid Build Coastguard Worker    - \ref av1_rc_update_rate_correction_factors()
718*77c1e3ccSAndroid Build Coastguard Worker
719*77c1e3ccSAndroid Build Coastguard WorkerA second mechanism for control comes into play if there is a large rate miss
720*77c1e3ccSAndroid Build Coastguard Workerfor the current frame (much too big or too small). This is a recode mechanism
721*77c1e3ccSAndroid Build Coastguard Workerwhich allows the current frame to be re-encoded one or more times with a
722*77c1e3ccSAndroid Build Coastguard Workerrevised Q value. This obviously has significant implications for encode speed
723*77c1e3ccSAndroid Build Coastguard Workerand in the case of RTC latency (hence it is not used for the RTC pathway).
724*77c1e3ccSAndroid Build Coastguard Worker
725*77c1e3ccSAndroid Build Coastguard WorkerWhether or not a recode is allowed for a given frame depends on the selected
726*77c1e3ccSAndroid Build Coastguard Workerencode speed vs quality trade off. This is set on the command line using the
727*77c1e3ccSAndroid Build Coastguard Worker--cpu-used parameter which maps onto the \ref AV1_COMP.speed field in the main
728*77c1e3ccSAndroid Build Coastguard Workercompressor instance data structure.
729*77c1e3ccSAndroid Build Coastguard Worker
730*77c1e3ccSAndroid Build Coastguard WorkerThe value of \ref AV1_COMP.speed, combined with the use case, is used to
731*77c1e3ccSAndroid Build Coastguard Workerpopulate the speed features data structure AV1_COMP.sf. In particular
732*77c1e3ccSAndroid Build Coastguard Worker\ref HIGH_LEVEL_SPEED_FEATURES.recode_loop determines the types of frames that
733*77c1e3ccSAndroid Build Coastguard Workermay be recoded and \ref HIGH_LEVEL_SPEED_FEATURES.recode_tolerance is a rate
734*77c1e3ccSAndroid Build Coastguard Workererror trigger threshold.
735*77c1e3ccSAndroid Build Coastguard Worker
736*77c1e3ccSAndroid Build Coastguard WorkerFor more information the reader is directed to the following functions:
737*77c1e3ccSAndroid Build Coastguard Worker
738*77c1e3ccSAndroid Build Coastguard Worker- \ref encode_with_recode_loop()
739*77c1e3ccSAndroid Build Coastguard Worker- \ref encode_without_recode()
740*77c1e3ccSAndroid Build Coastguard Worker- \ref recode_loop_update_q()
741*77c1e3ccSAndroid Build Coastguard Worker- \ref recode_loop_test()
742*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_set_speed_features_framesize_independent()
743*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_set_speed_features_framesize_dependent()
744*77c1e3ccSAndroid Build Coastguard Worker
745*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_fixed_q Fixed Q Mode
746*77c1e3ccSAndroid Build Coastguard Worker
747*77c1e3ccSAndroid Build Coastguard WorkerThere are two main fixed Q cases:
748*77c1e3ccSAndroid Build Coastguard Worker-# Fixed Q with adaptive qp offsets: same qp offset for each pyramid level
749*77c1e3ccSAndroid Build Coastguard Worker   in a given video, but these offsets are adaptive based on video content.
750*77c1e3ccSAndroid Build Coastguard Worker-# Fixed Q with fixed qp offsets: content-independent fixed qp offsets for
751*77c1e3ccSAndroid Build Coastguard Worker   each pyramid level.
752*77c1e3ccSAndroid Build Coastguard Worker
753*77c1e3ccSAndroid Build Coastguard WorkerThe reader is also refered to the following functions:
754*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_rc_pick_q_and_bounds()
755*77c1e3ccSAndroid Build Coastguard Worker- \ref rc_pick_q_and_bounds_no_stats_cbr()
756*77c1e3ccSAndroid Build Coastguard Worker- \ref rc_pick_q_and_bounds_no_stats()
757*77c1e3ccSAndroid Build Coastguard Worker- \ref rc_pick_q_and_bounds()
758*77c1e3ccSAndroid Build Coastguard Worker
759*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_frame_groups GF/ ARF Frame Groups & Hierarchical Coding
760*77c1e3ccSAndroid Build Coastguard Worker
761*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_frame_groups_data Main Data Structures
762*77c1e3ccSAndroid Build Coastguard Worker
763*77c1e3ccSAndroid Build Coastguard WorkerThe following are the main data structures referenced in this section
764*77c1e3ccSAndroid Build Coastguard Worker(see also \ref architecture_enc_data_structures):
765*77c1e3ccSAndroid Build Coastguard Worker
766*77c1e3ccSAndroid Build Coastguard Worker- \ref AV1_COMP cpi (the main compressor instance data structure)
767*77c1e3ccSAndroid Build Coastguard Worker    - \ref AV1_COMP.rc (\ref RATE_CONTROL)
768*77c1e3ccSAndroid Build Coastguard Worker
769*77c1e3ccSAndroid Build Coastguard Worker- \ref FIRSTPASS_STATS *frame_stats_buf (used to store per frame first pass
770*77c1e3ccSAndroid Build Coastguard Workerstats)
771*77c1e3ccSAndroid Build Coastguard Worker
772*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_frame_groups_groups Frame Groups
773*77c1e3ccSAndroid Build Coastguard Worker
774*77c1e3ccSAndroid Build Coastguard WorkerTo process a sequence/stream of video frames, the encoder divides the frames
775*77c1e3ccSAndroid Build Coastguard Workerinto groups and encodes them sequentially (possibly dependent on previous
776*77c1e3ccSAndroid Build Coastguard Workergroups). In AV1 such a group is usually referred to as a golden frame group
777*77c1e3ccSAndroid Build Coastguard Worker(GF group) or sometimes an Alt-Ref (ARF) group or a group of pictures (GOP).
778*77c1e3ccSAndroid Build Coastguard WorkerA GF group determines and stores the coding structure of the frames (for
779*77c1e3ccSAndroid Build Coastguard Workerexample, frame type, usage of the hierarchical structure, usage of overlay
780*77c1e3ccSAndroid Build Coastguard Workerframes, etc.) and can be considered as the base unit to process the frames,
781*77c1e3ccSAndroid Build Coastguard Workertherefore playing an important role in the encoder.
782*77c1e3ccSAndroid Build Coastguard Worker
783*77c1e3ccSAndroid Build Coastguard WorkerThe length of a specific GF group is arguably the most important aspect when
784*77c1e3ccSAndroid Build Coastguard Workerdetermining a GF group. This is because most GF group level decisions are
785*77c1e3ccSAndroid Build Coastguard Workerbased on the frame characteristics, if not on the length itself directly.
786*77c1e3ccSAndroid Build Coastguard WorkerNote that the GF group is always a group of consecutive frames, which means
787*77c1e3ccSAndroid Build Coastguard Workerthe start and end of the group (so again, the length of it) determines which
788*77c1e3ccSAndroid Build Coastguard Workerframes are included in it and hence determines the characteristics of the GF
789*77c1e3ccSAndroid Build Coastguard Workergroup. Therefore, in this document we will first discuss the GF group length
790*77c1e3ccSAndroid Build Coastguard Workerdecision in Libaom, followed by frame structure decisions when defining a GF
791*77c1e3ccSAndroid Build Coastguard Workergroup with a certain length.
792*77c1e3ccSAndroid Build Coastguard Worker
793*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_gf_length GF / ARF Group Length Determination
794*77c1e3ccSAndroid Build Coastguard Worker
795*77c1e3ccSAndroid Build Coastguard WorkerThe basic intuition of determining the GF group length is that it is usually
796*77c1e3ccSAndroid Build Coastguard Workerdesirable to group together frames that are similar. Hence, we may choose
797*77c1e3ccSAndroid Build Coastguard Workerlonger groups when consecutive frames are very alike and shorter ones when
798*77c1e3ccSAndroid Build Coastguard Workerthey are very different.
799*77c1e3ccSAndroid Build Coastguard Worker
800*77c1e3ccSAndroid Build Coastguard WorkerThe determination of the GF group length is done in function \ref
801*77c1e3ccSAndroid Build Coastguard Workercalculate_gf_length(). The following encoder use cases are supported:
802*77c1e3ccSAndroid Build Coastguard Worker
803*77c1e3ccSAndroid Build Coastguard Worker<ul>
804*77c1e3ccSAndroid Build Coastguard Worker  <li><b>Single pass with look-ahead disabled(\ref has_no_stats_stage()):
805*77c1e3ccSAndroid Build Coastguard Worker  </b> in this case there is no information available on the following stream
806*77c1e3ccSAndroid Build Coastguard Worker  of frames, therefore the function will set the GF group length for the
807*77c1e3ccSAndroid Build Coastguard Worker  current and the following GF groups (a total number of MAX_NUM_GF_INTERVALS
808*77c1e3ccSAndroid Build Coastguard Worker  groups) to be the maximum value allowed.</li>
809*77c1e3ccSAndroid Build Coastguard Worker
810*77c1e3ccSAndroid Build Coastguard Worker  <li><b>Single pass with look-ahead enabled (\ref AV1_PRIMARY.lap_enabled):</b>
811*77c1e3ccSAndroid Build Coastguard Worker  look-ahead processing is enabled for single pass, therefore there is a
812*77c1e3ccSAndroid Build Coastguard Worker  limited amount of information available regarding future frames. In this
813*77c1e3ccSAndroid Build Coastguard Worker  case the function will determine the length based on \ref FIRSTPASS_STATS
814*77c1e3ccSAndroid Build Coastguard Worker  (which is generated when processing the look-ahead buffer) for only the
815*77c1e3ccSAndroid Build Coastguard Worker  current GF group.</li>
816*77c1e3ccSAndroid Build Coastguard Worker
817*77c1e3ccSAndroid Build Coastguard Worker  <li><b>Two pass:</b> the first pass in two-pass encoding collects the stats
818*77c1e3ccSAndroid Build Coastguard Worker  and will not call the function. In the second pass, the function tries to
819*77c1e3ccSAndroid Build Coastguard Worker  determine the GF group length of the current and the following GF groups (a
820*77c1e3ccSAndroid Build Coastguard Worker  total number of MAX_NUM_GF_INTERVALS groups) based on the first-pass
821*77c1e3ccSAndroid Build Coastguard Worker  statistics. Note that as we will be discussing later, such decisions may not
822*77c1e3ccSAndroid Build Coastguard Worker  be accurate and can be changed later.</li>
823*77c1e3ccSAndroid Build Coastguard Worker</ul>
824*77c1e3ccSAndroid Build Coastguard Worker
825*77c1e3ccSAndroid Build Coastguard WorkerExcept for the first trivial case where there is no prior knowledge of the
826*77c1e3ccSAndroid Build Coastguard Workerfollowing frames, the function \ref calculate_gf_length() tries to determine the
827*77c1e3ccSAndroid Build Coastguard WorkerGF group length based on the first pass statistics. The determination is divided
828*77c1e3ccSAndroid Build Coastguard Workerinto two parts:
829*77c1e3ccSAndroid Build Coastguard Worker
830*77c1e3ccSAndroid Build Coastguard Worker<ol>
831*77c1e3ccSAndroid Build Coastguard Worker   <li>Baseline decision based on accumulated statistics: this part of the function
832*77c1e3ccSAndroid Build Coastguard Worker   iterates through the firstpass statistics of the following frames and
833*77c1e3ccSAndroid Build Coastguard Worker   accumulates the statistics with function accumulate_next_frame_stats.
834*77c1e3ccSAndroid Build Coastguard Worker   The accumulated statistics are then used to determine whether the
835*77c1e3ccSAndroid Build Coastguard Worker   correlation in the GF group has dropped too much in function detect_gf_cut.
836*77c1e3ccSAndroid Build Coastguard Worker   If detect_gf_cut returns non-zero, or if we've reached the end of
837*77c1e3ccSAndroid Build Coastguard Worker   first-pass statistics, the baseline decision is set at the current point.</li>
838*77c1e3ccSAndroid Build Coastguard Worker
839*77c1e3ccSAndroid Build Coastguard Worker   <li>If we are not at the end of the first-pass statistics, the next part will
840*77c1e3ccSAndroid Build Coastguard Worker   try to refine the baseline decision. This algorithm is based on the analysis
841*77c1e3ccSAndroid Build Coastguard Worker   of firstpass stats. It tries to cut the groups in stable regions or
842*77c1e3ccSAndroid Build Coastguard Worker   relatively stable points. Also it tries to avoid cutting in a blending
843*77c1e3ccSAndroid Build Coastguard Worker   region.</li>
844*77c1e3ccSAndroid Build Coastguard Worker</ol>
845*77c1e3ccSAndroid Build Coastguard Worker
846*77c1e3ccSAndroid Build Coastguard WorkerAs mentioned, for two-pass encoding, the function \ref
847*77c1e3ccSAndroid Build Coastguard Workercalculate_gf_length() tries to determine the length of as many as
848*77c1e3ccSAndroid Build Coastguard WorkerMAX_NUM_GF_INTERVALS groups. The decisions are stored in
849*77c1e3ccSAndroid Build Coastguard Worker\ref PRIMARY_RATE_CONTROL.gf_intervals[]. The variables
850*77c1e3ccSAndroid Build Coastguard Worker\ref RATE_CONTROL.intervals_till_gf_calculate_due and
851*77c1e3ccSAndroid Build Coastguard Worker\ref PRIMARY_RATE_CONTROL.gf_intervals[] help with managing and updating the stored
852*77c1e3ccSAndroid Build Coastguard Workerdecisions. In the function \ref define_gf_group(), the corresponding
853*77c1e3ccSAndroid Build Coastguard Workerstored length decision will be used to define the current GF group.
854*77c1e3ccSAndroid Build Coastguard Worker
855*77c1e3ccSAndroid Build Coastguard WorkerWhen the maximum GF group length is larger or equal to 32, the encoder will
856*77c1e3ccSAndroid Build Coastguard Workerenforce an extra layer to determine whether to use maximum GF length of 32
857*77c1e3ccSAndroid Build Coastguard Workeror 16 for every GF group. In such a case, \ref calculate_gf_length() is
858*77c1e3ccSAndroid Build Coastguard Workerfirst called with the original maximum length (>=32). Afterwards,
859*77c1e3ccSAndroid Build Coastguard Worker\ref av1_tpl_setup_stats() is called to analyze the determined GF group
860*77c1e3ccSAndroid Build Coastguard Workerand compare the reference to the last frame and the middle frame. If it is
861*77c1e3ccSAndroid Build Coastguard Workerdecided that we should use a maximum GF length of 16, the function
862*77c1e3ccSAndroid Build Coastguard Worker\ref calculate_gf_length() is called again with the updated maximum
863*77c1e3ccSAndroid Build Coastguard Workerlength, and it only sets the length for a single GF group
864*77c1e3ccSAndroid Build Coastguard Worker(\ref RATE_CONTROL.intervals_till_gf_calculate_due is set to 1). This process
865*77c1e3ccSAndroid Build Coastguard Workeris shown below.
866*77c1e3ccSAndroid Build Coastguard Worker
867*77c1e3ccSAndroid Build Coastguard Worker\image html tplgfgroupdiagram.png "" width=40%
868*77c1e3ccSAndroid Build Coastguard Worker
869*77c1e3ccSAndroid Build Coastguard WorkerBefore encoding each frame, the encoder checks
870*77c1e3ccSAndroid Build Coastguard Worker\ref RATE_CONTROL.frames_till_gf_update_due. If it is zero, indicating
871*77c1e3ccSAndroid Build Coastguard Workerprocessing of the current GF group is done, the encoder will check whether
872*77c1e3ccSAndroid Build Coastguard Worker\ref RATE_CONTROL.intervals_till_gf_calculate_due is zero. If it is, as
873*77c1e3ccSAndroid Build Coastguard Workerdiscussed above, \ref calculate_gf_length() is called with original
874*77c1e3ccSAndroid Build Coastguard Workermaximum length. If it is not zero, then the GF group length value stored
875*77c1e3ccSAndroid Build Coastguard Workerin \ref PRIMARY_RATE_CONTROL.gf_intervals[\ref PRIMARY_RATE_CONTROL.cur_gf_index] is used
876*77c1e3ccSAndroid Build Coastguard Worker(subject to change as discussed above).
877*77c1e3ccSAndroid Build Coastguard Worker
878*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_gf_structure Defining a GF Group's Structure
879*77c1e3ccSAndroid Build Coastguard Worker
880*77c1e3ccSAndroid Build Coastguard WorkerThe function \ref define_gf_group() defines the frame structure as well
881*77c1e3ccSAndroid Build Coastguard Workeras other GF group level parameters (e.g. bit allocation) once the length of
882*77c1e3ccSAndroid Build Coastguard Workerthe current GF group is determined.
883*77c1e3ccSAndroid Build Coastguard Worker
884*77c1e3ccSAndroid Build Coastguard WorkerThe function first iterates through the first pass statistics in the GF group to
885*77c1e3ccSAndroid Build Coastguard Workeraccumulate various stats, using accumulate_this_frame_stats() and
886*77c1e3ccSAndroid Build Coastguard Workeraccumulate_next_frame_stats(). The accumulated statistics are then used to
887*77c1e3ccSAndroid Build Coastguard Workerdetermine the use of the use of ALTREF frame along with other properties of the
888*77c1e3ccSAndroid Build Coastguard WorkerGF group. The values of \ref PRIMARY_RATE_CONTROL.cur_gf_index, \ref
889*77c1e3ccSAndroid Build Coastguard WorkerRATE_CONTROL.intervals_till_gf_calculate_due and \ref
890*77c1e3ccSAndroid Build Coastguard WorkerRATE_CONTROL.frames_till_gf_update_due are also updated accordingly.
891*77c1e3ccSAndroid Build Coastguard Worker
892*77c1e3ccSAndroid Build Coastguard WorkerThe function \ref av1_gop_setup_structure() is called at the end to determine
893*77c1e3ccSAndroid Build Coastguard Workerthe frame layers and reference maps in the GF group, where the
894*77c1e3ccSAndroid Build Coastguard Workerconstruct_multi_layer_gf_structure() function sets the frame update types for
895*77c1e3ccSAndroid Build Coastguard Workereach frame and the group structure.
896*77c1e3ccSAndroid Build Coastguard Worker
897*77c1e3ccSAndroid Build Coastguard Worker- If ALTREF frames are allowed for the GF group: the first frame is set to
898*77c1e3ccSAndroid Build Coastguard Worker  KF_UPDATE, GF_UPDATE or ARF_UPDATE. The last frames of the GF group is set to
899*77c1e3ccSAndroid Build Coastguard Worker  OVERLAY_UPDATE.  Then in set_multi_layer_params(), frame update
900*77c1e3ccSAndroid Build Coastguard Worker  types are determined recursively in a binary tree fashion, and assigned to
901*77c1e3ccSAndroid Build Coastguard Worker  give the final IBBB structure for the group.  - If the current branch has more
902*77c1e3ccSAndroid Build Coastguard Worker  than 2 frames and we have not reached maximum layer depth, then the middle
903*77c1e3ccSAndroid Build Coastguard Worker  frame is set as INTNL_ARF_UPDATE, and the left and right branches are
904*77c1e3ccSAndroid Build Coastguard Worker  processed recursively.  - If the current branch has less than 3 frames, or we
905*77c1e3ccSAndroid Build Coastguard Worker  have reached maximum layer depth, then every frame in the branch is set to
906*77c1e3ccSAndroid Build Coastguard Worker  LF_UPDATE.
907*77c1e3ccSAndroid Build Coastguard Worker
908*77c1e3ccSAndroid Build Coastguard Worker- If ALTREF frame is not allowed for the GF group: the frames are set
909*77c1e3ccSAndroid Build Coastguard Worker  as LF_UPDATE. This basically forms an IPPP GF group structure.
910*77c1e3ccSAndroid Build Coastguard Worker
911*77c1e3ccSAndroid Build Coastguard WorkerAs mentioned, the encoder may use Temporal dependancy modelling (TPL - see \ref
912*77c1e3ccSAndroid Build Coastguard Workerarchitecture_enc_tpl) to determine whether we should use a maximum length of 32
913*77c1e3ccSAndroid Build Coastguard Workeror 16 for the current GF group. This requires calls to \ref define_gf_group()
914*77c1e3ccSAndroid Build Coastguard Workerbut should not change other settings (since it is in essence a trial). This
915*77c1e3ccSAndroid Build Coastguard Workerspecial case is indicated by the setting parameter <b>is_final_pass</b> for to
916*77c1e3ccSAndroid Build Coastguard Workerzero.
917*77c1e3ccSAndroid Build Coastguard Worker
918*77c1e3ccSAndroid Build Coastguard WorkerFor single pass encodes where look-ahead processing is disabled
919*77c1e3ccSAndroid Build Coastguard Worker(\ref AV1_PRIMARY.lap_enabled = 0), \ref define_gf_group_pass0() is used
920*77c1e3ccSAndroid Build Coastguard Workerinstead of \ref define_gf_group().
921*77c1e3ccSAndroid Build Coastguard Worker
922*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_kf_groups Key Frame Groups
923*77c1e3ccSAndroid Build Coastguard Worker
924*77c1e3ccSAndroid Build Coastguard WorkerA special constraint for GF group length is the location of the next keyframe
925*77c1e3ccSAndroid Build Coastguard Worker(KF). The frames between two KFs are referred to as a KF group. Each KF group
926*77c1e3ccSAndroid Build Coastguard Workercan be encoded and decoded independently. Because of this, a GF group cannot
927*77c1e3ccSAndroid Build Coastguard Workerspan beyond a KF and the location of the next KF is set as a hard boundary
928*77c1e3ccSAndroid Build Coastguard Workerfor GF group length.
929*77c1e3ccSAndroid Build Coastguard Worker
930*77c1e3ccSAndroid Build Coastguard Worker<ul>
931*77c1e3ccSAndroid Build Coastguard Worker   <li>For two-pass encoding \ref RATE_CONTROL.frames_to_key controls when to
932*77c1e3ccSAndroid Build Coastguard Worker   encode a key frame. When it is zero, the current frame is a keyframe and
933*77c1e3ccSAndroid Build Coastguard Worker   the function \ref find_next_key_frame() is called. This in turn calls
934*77c1e3ccSAndroid Build Coastguard Worker   \ref define_kf_interval() to work out where the next key frame should
935*77c1e3ccSAndroid Build Coastguard Worker   be placed.</li>
936*77c1e3ccSAndroid Build Coastguard Worker
937*77c1e3ccSAndroid Build Coastguard Worker   <li>For single-pass with look-ahead enabled, \ref define_kf_interval()
938*77c1e3ccSAndroid Build Coastguard Worker   is called whenever a GF group update is needed (when
939*77c1e3ccSAndroid Build Coastguard Worker   \ref RATE_CONTROL.frames_till_gf_update_due is zero). This is because
940*77c1e3ccSAndroid Build Coastguard Worker   generally KFs are more widely spaced and the look-ahead buffer is usually
941*77c1e3ccSAndroid Build Coastguard Worker   not long enough.</li>
942*77c1e3ccSAndroid Build Coastguard Worker
943*77c1e3ccSAndroid Build Coastguard Worker   <li>For single-pass with look-ahead disabled, the KFs are placed according
944*77c1e3ccSAndroid Build Coastguard Worker   to the command line parameter <b>--kf-max-dist</b> (The above two cases are
945*77c1e3ccSAndroid Build Coastguard Worker   also subject to this constraint).</li>
946*77c1e3ccSAndroid Build Coastguard Worker</ul>
947*77c1e3ccSAndroid Build Coastguard Worker
948*77c1e3ccSAndroid Build Coastguard WorkerThe function \ref define_kf_interval() tries to detect a scenecut.
949*77c1e3ccSAndroid Build Coastguard WorkerIf a scenecut within kf-max-dist is detected, then it is set as the next
950*77c1e3ccSAndroid Build Coastguard Workerkeyframe. Otherwise the given maximum value is used.
951*77c1e3ccSAndroid Build Coastguard Worker
952*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_tpl Temporal Dependency Modelling
953*77c1e3ccSAndroid Build Coastguard Worker
954*77c1e3ccSAndroid Build Coastguard WorkerThe temporal dependency model runs at the beginning of each GOP. It builds the
955*77c1e3ccSAndroid Build Coastguard Workermotion trajectory within the GOP in units of 16x16 blocks. The temporal
956*77c1e3ccSAndroid Build Coastguard Workerdependency of a 16x16 block is evaluated as the predictive coding gains it
957*77c1e3ccSAndroid Build Coastguard Workercontributes to its trailing motion trajectory. This temporal dependency model
958*77c1e3ccSAndroid Build Coastguard Workerreflects how important a coding block is for the coding efficiency of the
959*77c1e3ccSAndroid Build Coastguard Workeroverall GOP. It is hence used to scale the Lagrangian multiplier used in the
960*77c1e3ccSAndroid Build Coastguard Workerrate-distortion optimization framework.
961*77c1e3ccSAndroid Build Coastguard Worker
962*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_tpl_config Configurations
963*77c1e3ccSAndroid Build Coastguard Worker
964*77c1e3ccSAndroid Build Coastguard WorkerThe temporal dependency model and its applications are by default turned on in
965*77c1e3ccSAndroid Build Coastguard Workerlibaom encoder for the VoD use case. To disable it, use --tpl-model=0 in the
966*77c1e3ccSAndroid Build Coastguard Workeraomenc configuration.
967*77c1e3ccSAndroid Build Coastguard Worker
968*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_tpl_algoritms Algorithms
969*77c1e3ccSAndroid Build Coastguard Worker
970*77c1e3ccSAndroid Build Coastguard WorkerThe scheme works in the reverse frame processing order over the source frames,
971*77c1e3ccSAndroid Build Coastguard Workerpropagating information from future frames back to the current frame. For each
972*77c1e3ccSAndroid Build Coastguard Workerframe, a propagation step is run for each MB. it operates as follows:
973*77c1e3ccSAndroid Build Coastguard Worker
974*77c1e3ccSAndroid Build Coastguard Worker<ul>
975*77c1e3ccSAndroid Build Coastguard Worker   <li> Estimate the intra prediction cost in terms of sum of absolute Hadamard
976*77c1e3ccSAndroid Build Coastguard Worker   transform difference (SATD) noted as intra_cost. It also loads the motion
977*77c1e3ccSAndroid Build Coastguard Worker   information available from the first-pass encode and estimates the inter
978*77c1e3ccSAndroid Build Coastguard Worker   prediction cost as inter_cost. Due to the use of hybrid inter/intra
979*77c1e3ccSAndroid Build Coastguard Worker   prediction mode, the inter_cost value is further upper bounded by
980*77c1e3ccSAndroid Build Coastguard Worker   intra_cost. A propagation cost variable is used to collect all the
981*77c1e3ccSAndroid Build Coastguard Worker   information flowed back from future processing frames. It is initialized as
982*77c1e3ccSAndroid Build Coastguard Worker   0 for all the blocks in the last processing frame in a group of pictures
983*77c1e3ccSAndroid Build Coastguard Worker   (GOP).</li>
984*77c1e3ccSAndroid Build Coastguard Worker
985*77c1e3ccSAndroid Build Coastguard Worker   <li> The fraction of information from a current block to be propagated towards
986*77c1e3ccSAndroid Build Coastguard Worker   its reference block is estimated as:
987*77c1e3ccSAndroid Build Coastguard Worker\f[
988*77c1e3ccSAndroid Build Coastguard Worker   propagation\_fraction = (1 - inter\_cost/intra\_cost)
989*77c1e3ccSAndroid Build Coastguard Worker\f]
990*77c1e3ccSAndroid Build Coastguard Worker   It reflects how much the motion compensated reference would reduce the
991*77c1e3ccSAndroid Build Coastguard Worker   prediction error in percentage.</li>
992*77c1e3ccSAndroid Build Coastguard Worker
993*77c1e3ccSAndroid Build Coastguard Worker   <li> The total amount of information the current block contributes to the GOP
994*77c1e3ccSAndroid Build Coastguard Worker   is estimated as intra_cost + propagation_cost. The information that it
995*77c1e3ccSAndroid Build Coastguard Worker   propagates towards its reference block is captured by:
996*77c1e3ccSAndroid Build Coastguard Worker
997*77c1e3ccSAndroid Build Coastguard Worker\f[
998*77c1e3ccSAndroid Build Coastguard Worker   propagation\_amount =
999*77c1e3ccSAndroid Build Coastguard Worker   (intra\_cost + propagation\_cost) * propagation\_fraction
1000*77c1e3ccSAndroid Build Coastguard Worker\f]</li>
1001*77c1e3ccSAndroid Build Coastguard Worker
1002*77c1e3ccSAndroid Build Coastguard Worker   <li> Note that the reference block may not necessarily sit on the grid of
1003*77c1e3ccSAndroid Build Coastguard Worker   16x16 blocks. The propagation amount is hence dispensed to all the blocks
1004*77c1e3ccSAndroid Build Coastguard Worker   that overlap with the reference block. The corresponding block in the
1005*77c1e3ccSAndroid Build Coastguard Worker   reference frame accumulates its own propagation cost as it receives back
1006*77c1e3ccSAndroid Build Coastguard Worker   propagation.
1007*77c1e3ccSAndroid Build Coastguard Worker
1008*77c1e3ccSAndroid Build Coastguard Worker\f[
1009*77c1e3ccSAndroid Build Coastguard Worker   propagation\_cost = propagation\_cost +
1010*77c1e3ccSAndroid Build Coastguard Worker                       (\frac{overlap\_area}{(16*16)} * propagation\_amount)
1011*77c1e3ccSAndroid Build Coastguard Worker\f]</li>
1012*77c1e3ccSAndroid Build Coastguard Worker
1013*77c1e3ccSAndroid Build Coastguard Worker   <li> In the final encoding stage, the distortion propagation factor of a block
1014*77c1e3ccSAndroid Build Coastguard Worker   is evaluated as \f$(1 + \frac{propagation\_cost}{intra\_cost})\f$, where the second term
1015*77c1e3ccSAndroid Build Coastguard Worker   captures its impact on later frames in a GOP.</li>
1016*77c1e3ccSAndroid Build Coastguard Worker
1017*77c1e3ccSAndroid Build Coastguard Worker   <li> The Lagrangian multiplier is adapted at the 64x64 block level. For every
1018*77c1e3ccSAndroid Build Coastguard Worker   64x64 block in a frame, we have a distortion propagation factor:
1019*77c1e3ccSAndroid Build Coastguard Worker
1020*77c1e3ccSAndroid Build Coastguard Worker\f[
1021*77c1e3ccSAndroid Build Coastguard Worker  dist\_prop[i] = 1 + \frac{propagation\_cost[i]}{intra\_cost[i]}
1022*77c1e3ccSAndroid Build Coastguard Worker\f]
1023*77c1e3ccSAndroid Build Coastguard Worker
1024*77c1e3ccSAndroid Build Coastguard Worker   where i denotes the block index in the frame. We also have the frame level
1025*77c1e3ccSAndroid Build Coastguard Worker   distortion propagation factor:
1026*77c1e3ccSAndroid Build Coastguard Worker
1027*77c1e3ccSAndroid Build Coastguard Worker\f[
1028*77c1e3ccSAndroid Build Coastguard Worker  dist\_prop = 1 +
1029*77c1e3ccSAndroid Build Coastguard Worker  \frac{\sum_{i}propagation\_cost[i]}{\sum_{i}intra\_cost[i]}
1030*77c1e3ccSAndroid Build Coastguard Worker\f]
1031*77c1e3ccSAndroid Build Coastguard Worker
1032*77c1e3ccSAndroid Build Coastguard Worker   which is used to normalize the propagation factor at the 64x64 block level. The
1033*77c1e3ccSAndroid Build Coastguard Worker   Lagrangian multiplier is hence adapted as:
1034*77c1e3ccSAndroid Build Coastguard Worker
1035*77c1e3ccSAndroid Build Coastguard Worker\f[
1036*77c1e3ccSAndroid Build Coastguard Worker  &lambda;[i] = &lambda;[0] * \frac{dist\_prop}{dist\_prop[i]}
1037*77c1e3ccSAndroid Build Coastguard Worker\f]
1038*77c1e3ccSAndroid Build Coastguard Worker
1039*77c1e3ccSAndroid Build Coastguard Worker   where &lambda;0 is the multiplier associated with the frame level QP. The
1040*77c1e3ccSAndroid Build Coastguard Worker   64x64 block level QP is scaled according to the Lagrangian multiplier.
1041*77c1e3ccSAndroid Build Coastguard Worker</ul>
1042*77c1e3ccSAndroid Build Coastguard Worker
1043*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_enc_tpl_keyfun Key Functions and data structures
1044*77c1e3ccSAndroid Build Coastguard Worker
1045*77c1e3ccSAndroid Build Coastguard WorkerThe reader is also refered to the following functions and data structures:
1046*77c1e3ccSAndroid Build Coastguard Worker
1047*77c1e3ccSAndroid Build Coastguard Worker- \ref TplParams
1048*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_tpl_setup_stats() builds the TPL model.
1049*77c1e3ccSAndroid Build Coastguard Worker- \ref setup_delta_q() Assign different quantization parameters to each super
1050*77c1e3ccSAndroid Build Coastguard Worker  block based on its TPL weight.
1051*77c1e3ccSAndroid Build Coastguard Worker
1052*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_partitions Block Partition Search
1053*77c1e3ccSAndroid Build Coastguard Worker
1054*77c1e3ccSAndroid Build Coastguard Worker A frame is first split into tiles in \ref encode_tiles(), with each tile
1055*77c1e3ccSAndroid Build Coastguard Worker compressed by av1_encode_tile(). Then a tile is processed in superblock rows
1056*77c1e3ccSAndroid Build Coastguard Worker via \ref av1_encode_sb_row() and then \ref encode_sb_row().
1057*77c1e3ccSAndroid Build Coastguard Worker
1058*77c1e3ccSAndroid Build Coastguard Worker The partition search processes superblocks sequentially in \ref
1059*77c1e3ccSAndroid Build Coastguard Worker encode_sb_row(). Two search modes are supported, depending upon the encoding
1060*77c1e3ccSAndroid Build Coastguard Worker configuration, \ref encode_nonrd_sb() is for 1-pass and real-time modes,
1061*77c1e3ccSAndroid Build Coastguard Worker while \ref encode_rd_sb() performs more exhaustive rate distortion based
1062*77c1e3ccSAndroid Build Coastguard Worker searches.
1063*77c1e3ccSAndroid Build Coastguard Worker
1064*77c1e3ccSAndroid Build Coastguard Worker Partition search over the recursive quad-tree space is implemented by
1065*77c1e3ccSAndroid Build Coastguard Worker recursive calls to \ref av1_nonrd_use_partition(),
1066*77c1e3ccSAndroid Build Coastguard Worker \ref av1_rd_use_partition(), or av1_rd_pick_partition() and returning best
1067*77c1e3ccSAndroid Build Coastguard Worker options for sub-trees to their parent partitions.
1068*77c1e3ccSAndroid Build Coastguard Worker
1069*77c1e3ccSAndroid Build Coastguard Worker In libaom, the partition search lays on top of the mode search (predictor,
1070*77c1e3ccSAndroid Build Coastguard Worker transform, etc.), instead of being a separate module. The interface of mode
1071*77c1e3ccSAndroid Build Coastguard Worker search is \ref pick_sb_modes(), which connects the partition_search with
1072*77c1e3ccSAndroid Build Coastguard Worker \ref architecture_enc_inter_modes and \ref architecture_enc_intra_modes. To
1073*77c1e3ccSAndroid Build Coastguard Worker make good decisions, reconstruction is also required in order to build
1074*77c1e3ccSAndroid Build Coastguard Worker references and contexts. This is implemented by \ref encode_sb() at the
1075*77c1e3ccSAndroid Build Coastguard Worker sub-tree level and \ref encode_b() at coding block level.
1076*77c1e3ccSAndroid Build Coastguard Worker
1077*77c1e3ccSAndroid Build Coastguard Worker See also \ref partition_search
1078*77c1e3ccSAndroid Build Coastguard Worker
1079*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_intra_modes Intra Mode Search
1080*77c1e3ccSAndroid Build Coastguard Worker
1081*77c1e3ccSAndroid Build Coastguard WorkerAV1 also provides 71 different intra prediction modes, i.e. modes that predict
1082*77c1e3ccSAndroid Build Coastguard Workeronly based upon information in the current frame with no dependency on
1083*77c1e3ccSAndroid Build Coastguard Workerprevious or future frames. For key frames, where this independence from any
1084*77c1e3ccSAndroid Build Coastguard Workerother frame is a defining requirement and for other cases where intra only
1085*77c1e3ccSAndroid Build Coastguard Workerframes are required, the encoder need only considers these modes in the rate
1086*77c1e3ccSAndroid Build Coastguard Workerdistortion loop.
1087*77c1e3ccSAndroid Build Coastguard Worker
1088*77c1e3ccSAndroid Build Coastguard WorkerEven so, in most use cases, searching all possible intra prediction modes for
1089*77c1e3ccSAndroid Build Coastguard Workerevery block and partition size is not practical and some pruning of the search
1090*77c1e3ccSAndroid Build Coastguard Workertree is necessary.
1091*77c1e3ccSAndroid Build Coastguard Worker
1092*77c1e3ccSAndroid Build Coastguard WorkerFor the Rate distortion optimized case, the main top level function
1093*77c1e3ccSAndroid Build Coastguard Workerresponsible for selecting the intra prediction mode for a given block is
1094*77c1e3ccSAndroid Build Coastguard Worker\ref av1_rd_pick_intra_mode_sb(). The readers attention is also drawn to the
1095*77c1e3ccSAndroid Build Coastguard Workerfunctions \ref hybrid_intra_mode_search() and \ref av1_nonrd_pick_intra_mode()
1096*77c1e3ccSAndroid Build Coastguard Workerwhich may be used where encode speed is critical. The choice between the
1097*77c1e3ccSAndroid Build Coastguard Workerrd path and the non rd or hybrid paths depends on the encoder use case and the
1098*77c1e3ccSAndroid Build Coastguard Worker\ref AV1_COMP.speed parameter. Further fine control of the speed vs quality
1099*77c1e3ccSAndroid Build Coastguard Workertrade off is provided by means of fields in \ref AV1_COMP.sf (which has type
1100*77c1e3ccSAndroid Build Coastguard Worker\ref SPEED_FEATURES).
1101*77c1e3ccSAndroid Build Coastguard Worker
1102*77c1e3ccSAndroid Build Coastguard WorkerNote that some intra modes are only considered for specific use cases or
1103*77c1e3ccSAndroid Build Coastguard Workertypes of video. For example the palette based prediction modes are often
1104*77c1e3ccSAndroid Build Coastguard Workervalueable for graphics or screen share content but not for natural video.
1105*77c1e3ccSAndroid Build Coastguard Worker(See \ref av1_search_palette_mode())
1106*77c1e3ccSAndroid Build Coastguard Worker
1107*77c1e3ccSAndroid Build Coastguard WorkerSee also \ref intra_mode_search for more details.
1108*77c1e3ccSAndroid Build Coastguard Worker
1109*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_inter_modes Inter Prediction Mode Search
1110*77c1e3ccSAndroid Build Coastguard Worker
1111*77c1e3ccSAndroid Build Coastguard WorkerFor inter frames, where we also allow prediction using one or more previously
1112*77c1e3ccSAndroid Build Coastguard Workercoded frames (which may chronologically speaking be past or future frames or
1113*77c1e3ccSAndroid Build Coastguard Workernon-display reference buffers such as ARF frames), the size of the search tree
1114*77c1e3ccSAndroid Build Coastguard Workerthat needs to be traversed, to select a prediction mode, is considerably more
1115*77c1e3ccSAndroid Build Coastguard Workermassive.
1116*77c1e3ccSAndroid Build Coastguard Worker
1117*77c1e3ccSAndroid Build Coastguard WorkerIn addition to the 71 possible intra modes we also need to consider 56 single
1118*77c1e3ccSAndroid Build Coastguard Workerframe inter prediction modes (7 reference frames x 4 modes x 2 for OBMC
1119*77c1e3ccSAndroid Build Coastguard Worker(overlapped block motion compensation)), 12768 compound inter prediction modes
1120*77c1e3ccSAndroid Build Coastguard Worker(these are modes that combine inter predictors from two reference frames) and
1121*77c1e3ccSAndroid Build Coastguard Worker36708 compound inter / intra prediction modes.
1122*77c1e3ccSAndroid Build Coastguard Worker
1123*77c1e3ccSAndroid Build Coastguard WorkerAs with the intra mode search, libaom supports an RD based pathway and a non
1124*77c1e3ccSAndroid Build Coastguard Workerrd pathway for speed critical use cases.  The entry points for these two cases
1125*77c1e3ccSAndroid Build Coastguard Workerare \ref av1_rd_pick_inter_mode() and \ref av1_nonrd_pick_inter_mode_sb()
1126*77c1e3ccSAndroid Build Coastguard Workerrespectively.
1127*77c1e3ccSAndroid Build Coastguard Worker
1128*77c1e3ccSAndroid Build Coastguard WorkerVarious heuristics and predictive strategies are used to prune the search tree
1129*77c1e3ccSAndroid Build Coastguard Workerwith fine control provided through the speed features parameter in the main
1130*77c1e3ccSAndroid Build Coastguard Workercompressor instance data structure \ref AV1_COMP.sf.
1131*77c1e3ccSAndroid Build Coastguard Worker
1132*77c1e3ccSAndroid Build Coastguard WorkerIt is worth noting, that some prediction modes incurr a much larger rate cost
1133*77c1e3ccSAndroid Build Coastguard Workerthan others (ignoring for now the cost of coding the error residual). For
1134*77c1e3ccSAndroid Build Coastguard Workerexample, a compound mode that requires the encoder to specify two reference
1135*77c1e3ccSAndroid Build Coastguard Workerframes and two new motion vectors will almost inevitable have a higher rate
1136*77c1e3ccSAndroid Build Coastguard Workercost than a simple inter prediction mode that uses a predicted or 0,0 motion
1137*77c1e3ccSAndroid Build Coastguard Workervector. As such, if we have already found a mode for the current block that
1138*77c1e3ccSAndroid Build Coastguard Workerhas a low RD cost, we can skip a large number of the possible modes on the
1139*77c1e3ccSAndroid Build Coastguard Workerbasis that even if the error residual is 0 the inherent rate cost of the
1140*77c1e3ccSAndroid Build Coastguard Workermode itself will garauntee that it is not chosen.
1141*77c1e3ccSAndroid Build Coastguard Worker
1142*77c1e3ccSAndroid Build Coastguard WorkerSee also \ref inter_mode_search for more details.
1143*77c1e3ccSAndroid Build Coastguard Worker
1144*77c1e3ccSAndroid Build Coastguard Worker\section architecture_enc_tx_search Transform Search
1145*77c1e3ccSAndroid Build Coastguard Worker
1146*77c1e3ccSAndroid Build Coastguard WorkerAV1 implements the transform stage using 4 seperable 1-d transforms (DCT,
1147*77c1e3ccSAndroid Build Coastguard WorkerADST, FLIPADST and IDTX, where FLIPADST is the reversed version of ADST
1148*77c1e3ccSAndroid Build Coastguard Workerand IDTX is the identity transform) which can be combined to give 16 2-d
1149*77c1e3ccSAndroid Build Coastguard Workercombinations.
1150*77c1e3ccSAndroid Build Coastguard Worker
1151*77c1e3ccSAndroid Build Coastguard WorkerThese combinations can be applied at 19 different scales from 64x64 pixels
1152*77c1e3ccSAndroid Build Coastguard Workerdown to 4x4 pixels.
1153*77c1e3ccSAndroid Build Coastguard Worker
1154*77c1e3ccSAndroid Build Coastguard WorkerThis gives rise to a large number of possible candidate transform options
1155*77c1e3ccSAndroid Build Coastguard Workerfor coding the residual error after prediction. An exhaustive rate-distortion
1156*77c1e3ccSAndroid Build Coastguard Workerbased evaluation of all candidates would not be practical from a speed
1157*77c1e3ccSAndroid Build Coastguard Workerperspective in a production encoder implementation. Hence libaom addopts a
1158*77c1e3ccSAndroid Build Coastguard Workernumber of strategies to prune the selection of both the transform size and
1159*77c1e3ccSAndroid Build Coastguard Workertransform type.
1160*77c1e3ccSAndroid Build Coastguard Worker
1161*77c1e3ccSAndroid Build Coastguard WorkerThere are a number of strategies that have been tested and implememnted in
1162*77c1e3ccSAndroid Build Coastguard Workerlibaom including:
1163*77c1e3ccSAndroid Build Coastguard Worker
1164*77c1e3ccSAndroid Build Coastguard Worker- A statistics based approach that looks at the frequency with which certain
1165*77c1e3ccSAndroid Build Coastguard Worker  combinations are used in a given context and prunes out very unlikely
1166*77c1e3ccSAndroid Build Coastguard Worker  candidates. It is worth noting here that some size candidates can be pruned
1167*77c1e3ccSAndroid Build Coastguard Worker  out immediately based on the size of the prediction partition. For example it
1168*77c1e3ccSAndroid Build Coastguard Worker  does not make sense to use a transform size that is larger than the
1169*77c1e3ccSAndroid Build Coastguard Worker  prediction partition size but also a very large prediction partition size is
1170*77c1e3ccSAndroid Build Coastguard Worker  unlikely to be optimally pared with small transforms.
1171*77c1e3ccSAndroid Build Coastguard Worker
1172*77c1e3ccSAndroid Build Coastguard Worker- A Machine learning based model
1173*77c1e3ccSAndroid Build Coastguard Worker
1174*77c1e3ccSAndroid Build Coastguard Worker- A method that initially tests candidates using a fast algorithm that skips
1175*77c1e3ccSAndroid Build Coastguard Worker  entropy encoding and uses an estimated cost model to choose a reduced subset
1176*77c1e3ccSAndroid Build Coastguard Worker  for full RD analysis. This subject is covered more fully in a paper authored
1177*77c1e3ccSAndroid Build Coastguard Worker  by Bohan Li, Jingning Han, and Yaowu Xu titled: <b>Fast Transform Type
1178*77c1e3ccSAndroid Build Coastguard Worker  Selection Using Conditional Laplace Distribution Based Rate Estimation</b>
1179*77c1e3ccSAndroid Build Coastguard Worker
1180*77c1e3ccSAndroid Build Coastguard Worker<b>TODO Add link to paper when available</b>
1181*77c1e3ccSAndroid Build Coastguard Worker
1182*77c1e3ccSAndroid Build Coastguard WorkerSee also \ref transform_search for more details.
1183*77c1e3ccSAndroid Build Coastguard Worker
1184*77c1e3ccSAndroid Build Coastguard Worker\section architecture_post_enc_filt Post Encode Loop Filtering
1185*77c1e3ccSAndroid Build Coastguard Worker
1186*77c1e3ccSAndroid Build Coastguard WorkerAV1 supports three types of post encode <b>in loop</b> filtering to improve
1187*77c1e3ccSAndroid Build Coastguard Workerthe quality of the reconstructed video.
1188*77c1e3ccSAndroid Build Coastguard Worker
1189*77c1e3ccSAndroid Build Coastguard Worker- <b>Deblocking Filter</b> The first of these is a farily traditional boundary
1190*77c1e3ccSAndroid Build Coastguard Worker  deblocking filter that attempts to smooth discontinuities that may occur at
1191*77c1e3ccSAndroid Build Coastguard Worker  the boundaries between blocks. See also \ref in_loop_filter.
1192*77c1e3ccSAndroid Build Coastguard Worker
1193*77c1e3ccSAndroid Build Coastguard Worker- <b>CDEF Filter</b> The constrained directional enhancement filter (CDEF)
1194*77c1e3ccSAndroid Build Coastguard Worker  allows the codec to apply a non-linear deringing filter along certain
1195*77c1e3ccSAndroid Build Coastguard Worker  (potentially oblique) directions. A primary filter is applied along the
1196*77c1e3ccSAndroid Build Coastguard Worker  selected direction, whilst a secondary filter is applied at 45 degrees to
1197*77c1e3ccSAndroid Build Coastguard Worker  the primary direction. (See also \ref in_loop_cdef and
1198*77c1e3ccSAndroid Build Coastguard Worker  <a href="https://arxiv.org/abs/2008.06091"> A Technical Overview of AV1</a>.
1199*77c1e3ccSAndroid Build Coastguard Worker
1200*77c1e3ccSAndroid Build Coastguard Worker- <b>Loop Restoration Filter</b> The loop restoration filter is applied after
1201*77c1e3ccSAndroid Build Coastguard Worker  any prior post filtering stages. It acts on units of either 64 x 64,
1202*77c1e3ccSAndroid Build Coastguard Worker  128 x 128, or 256 x 256 pixel blocks, refered to as loop restoration units.
1203*77c1e3ccSAndroid Build Coastguard Worker  Each unit can independently select either to bypass filtering, use a Wiener
1204*77c1e3ccSAndroid Build Coastguard Worker  filter, or use a self-guided filter. (See also \ref in_loop_restoration and
1205*77c1e3ccSAndroid Build Coastguard Worker  <a href="https://arxiv.org/abs/2008.06091"> A Technical Overview of AV1</a>.
1206*77c1e3ccSAndroid Build Coastguard Worker
1207*77c1e3ccSAndroid Build Coastguard Worker\section architecture_entropy Entropy Coding
1208*77c1e3ccSAndroid Build Coastguard Worker
1209*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_entropy_aritmetic Arithmetic Coder
1210*77c1e3ccSAndroid Build Coastguard Worker
1211*77c1e3ccSAndroid Build Coastguard WorkerVP9, used a binary arithmetic coder to encode symbols, where the propability
1212*77c1e3ccSAndroid Build Coastguard Workerof a 1 or 0 at each descision node was based on a context model that took
1213*77c1e3ccSAndroid Build Coastguard Workerinto account recently coded values (for example previously coded coefficients
1214*77c1e3ccSAndroid Build Coastguard Workerin the current block). A mechanism existed to update the context model each
1215*77c1e3ccSAndroid Build Coastguard Workerframe, either explicitly in the bitstream, or implicitly at both the encoder
1216*77c1e3ccSAndroid Build Coastguard Workerand decoder based on the observed frequency of different outcomes in the
1217*77c1e3ccSAndroid Build Coastguard Workerprevious frame. VP9 also supported seperate context models for different types
1218*77c1e3ccSAndroid Build Coastguard Workerof frame (e.g. inter coded frames and key frames).
1219*77c1e3ccSAndroid Build Coastguard Worker
1220*77c1e3ccSAndroid Build Coastguard WorkerIn contrast, AV1 uses an M-ary symbol arithmetic coder to compress the syntax
1221*77c1e3ccSAndroid Build Coastguard Workerelements, where integer \f$M\in[2, 14]\f$. This approach is based upon the entropy
1222*77c1e3ccSAndroid Build Coastguard Workercoding strategy used in the Daala video codec and allows for some bit-level
1223*77c1e3ccSAndroid Build Coastguard Workerparallelism in its implementation. AV1 also has an extended context model and
1224*77c1e3ccSAndroid Build Coastguard Workerallows for updates to the probabilities on a per symbol basis as opposed to
1225*77c1e3ccSAndroid Build Coastguard Workerthe per frame strategy in VP9.
1226*77c1e3ccSAndroid Build Coastguard Worker
1227*77c1e3ccSAndroid Build Coastguard WorkerTo improve the performance / throughput of the arithmetic encoder, especially
1228*77c1e3ccSAndroid Build Coastguard Workerin hardware implementations, the probability model is updated and maintained
1229*77c1e3ccSAndroid Build Coastguard Workerat 15-bit precision, but the arithmetic encoder only uses the most significant
1230*77c1e3ccSAndroid Build Coastguard Worker9 bits when encoding a symbol. A more detailed discussion of the algorithm
1231*77c1e3ccSAndroid Build Coastguard Workerand design constraints can be found in
1232*77c1e3ccSAndroid Build Coastguard Worker<a href="https://arxiv.org/abs/2008.06091"> A Technical Overview of AV1</a>.
1233*77c1e3ccSAndroid Build Coastguard Worker
1234*77c1e3ccSAndroid Build Coastguard WorkerTODO add references to key functions / files.
1235*77c1e3ccSAndroid Build Coastguard Worker
1236*77c1e3ccSAndroid Build Coastguard WorkerAs with VP9, a mechanism exists in AV1 to encode some elements into the
1237*77c1e3ccSAndroid Build Coastguard Workerbitstream as uncrompresed bits or literal values, without using the arithmetic
1238*77c1e3ccSAndroid Build Coastguard Workercoder. For example, some frame and sequence header values, where it is
1239*77c1e3ccSAndroid Build Coastguard Workerbeneficial to be able to read the values directly.
1240*77c1e3ccSAndroid Build Coastguard Worker
1241*77c1e3ccSAndroid Build Coastguard WorkerTODO add references to key functions / files.
1242*77c1e3ccSAndroid Build Coastguard Worker
1243*77c1e3ccSAndroid Build Coastguard Worker\subsection architecture_entropy_coef Transform Coefficient Coding and Optimization
1244*77c1e3ccSAndroid Build Coastguard Worker\image html coeff_coding.png "" width=70%
1245*77c1e3ccSAndroid Build Coastguard Worker
1246*77c1e3ccSAndroid Build Coastguard Worker\subsubsection architecture_entropy_coef_what Transform coefficient coding
1247*77c1e3ccSAndroid Build Coastguard WorkerTransform coefficient coding is where the encoder compresses a quantized version
1248*77c1e3ccSAndroid Build Coastguard Workerof prediction residue into the bitstream.
1249*77c1e3ccSAndroid Build Coastguard Worker
1250*77c1e3ccSAndroid Build Coastguard Worker\paragraph architecture_entropy_coef_prepare Preparation - transform and quantize
1251*77c1e3ccSAndroid Build Coastguard WorkerBefore the entropy coding stage, the encoder decouple the pixel-to-pixel
1252*77c1e3ccSAndroid Build Coastguard Workercorrelation of the prediction residue by transforming the residue from the
1253*77c1e3ccSAndroid Build Coastguard Workerspatial domain to the frequency domain. Then the encoder quantizes the transform
1254*77c1e3ccSAndroid Build Coastguard Workercoefficients to make the coefficients ready for entropy coding.
1255*77c1e3ccSAndroid Build Coastguard Worker
1256*77c1e3ccSAndroid Build Coastguard Worker\paragraph architecture_entropy_coef_coding The coding process
1257*77c1e3ccSAndroid Build Coastguard WorkerThe encoder uses \ref av1_write_coeffs_txb() to write the coefficients of
1258*77c1e3ccSAndroid Build Coastguard Workera transform block into the bitstream.
1259*77c1e3ccSAndroid Build Coastguard WorkerThe coding process has three stages.
1260*77c1e3ccSAndroid Build Coastguard Worker1. The encoder will code transform block skip flag (txb_skip). If the skip flag is
1261*77c1e3ccSAndroid Build Coastguard Workeroff, then the encoder will code the end of block position (eob) which is the scan
1262*77c1e3ccSAndroid Build Coastguard Workerindex of the last non-zero coefficient plus one.
1263*77c1e3ccSAndroid Build Coastguard Worker2. Second, the encoder will code lower magnitude levels of each coefficient in
1264*77c1e3ccSAndroid Build Coastguard Workerreverse scan order.
1265*77c1e3ccSAndroid Build Coastguard Worker3. Finally, the encoder will code the sign and higher magnitude levels for each
1266*77c1e3ccSAndroid Build Coastguard Workercoefficient if they are available.
1267*77c1e3ccSAndroid Build Coastguard Worker
1268*77c1e3ccSAndroid Build Coastguard WorkerRelated functions:
1269*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_write_coeffs_txb()
1270*77c1e3ccSAndroid Build Coastguard Worker- write_inter_txb_coeff()
1271*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_write_intra_coeffs_mb()
1272*77c1e3ccSAndroid Build Coastguard Worker
1273*77c1e3ccSAndroid Build Coastguard Worker\paragraph architecture_entropy_coef_context Context information
1274*77c1e3ccSAndroid Build Coastguard WorkerTo improve the compression efficiency, the encoder uses several context models
1275*77c1e3ccSAndroid Build Coastguard Workertailored for transform coefficients to capture the correlations between coding
1276*77c1e3ccSAndroid Build Coastguard Workersymbols. Most of the context models are built to capture the correlations
1277*77c1e3ccSAndroid Build Coastguard Workerbetween the coefficients within the same transform block. However, transform
1278*77c1e3ccSAndroid Build Coastguard Workerblock skip flag (txb_skip) and the sign of dc coefficient (dc_sign) require
1279*77c1e3ccSAndroid Build Coastguard Workercontext info from neighboring transform blocks.
1280*77c1e3ccSAndroid Build Coastguard Worker
1281*77c1e3ccSAndroid Build Coastguard WorkerHere is how context info spread between transform blocks. Before coding a
1282*77c1e3ccSAndroid Build Coastguard Workertransform block, the encoder will use get_txb_ctx() to collect the context
1283*77c1e3ccSAndroid Build Coastguard Workerinformation from neighboring transform blocks. Then the context information
1284*77c1e3ccSAndroid Build Coastguard Workerwill be used for coding transform block skip flag (txb_skip) and the sign of
1285*77c1e3ccSAndroid Build Coastguard Workerdc coefficient (dc_sign). After the transform block is coded, the encoder will
1286*77c1e3ccSAndroid Build Coastguard Workerextract the context info from the current block using
1287*77c1e3ccSAndroid Build Coastguard Worker\ref av1_get_txb_entropy_context(). Then encoder will store the context info
1288*77c1e3ccSAndroid Build Coastguard Workerinto a byte (uint8_t) using av1_set_entropy_contexts(). The encoder will use
1289*77c1e3ccSAndroid Build Coastguard Workerthe context info to code other transform blocks.
1290*77c1e3ccSAndroid Build Coastguard Worker
1291*77c1e3ccSAndroid Build Coastguard WorkerRelated functions:
1292*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_get_txb_entropy_context()
1293*77c1e3ccSAndroid Build Coastguard Worker- av1_set_entropy_contexts()
1294*77c1e3ccSAndroid Build Coastguard Worker- get_txb_ctx()
1295*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_update_intra_mb_txb_context()
1296*77c1e3ccSAndroid Build Coastguard Worker
1297*77c1e3ccSAndroid Build Coastguard Worker\subsubsection architecture_entropy_coef_rd RD optimization
1298*77c1e3ccSAndroid Build Coastguard WorkerBeside the actual entropy coding, the encoder uses several utility functions
1299*77c1e3ccSAndroid Build Coastguard Workerto make optimal RD decisions.
1300*77c1e3ccSAndroid Build Coastguard Worker
1301*77c1e3ccSAndroid Build Coastguard Worker\paragraph architecture_entropy_coef_cost Entropy cost
1302*77c1e3ccSAndroid Build Coastguard WorkerThe encoder uses \ref av1_cost_coeffs_txb() or \ref av1_cost_coeffs_txb_laplacian()
1303*77c1e3ccSAndroid Build Coastguard Workerto estimate the entropy cost of a transform block. Note that
1304*77c1e3ccSAndroid Build Coastguard Worker\ref av1_cost_coeffs_txb() is slower but accurate whereas
1305*77c1e3ccSAndroid Build Coastguard Worker\ref av1_cost_coeffs_txb_laplacian() is faster but less accurate.
1306*77c1e3ccSAndroid Build Coastguard Worker
1307*77c1e3ccSAndroid Build Coastguard WorkerRelated functions:
1308*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_cost_coeffs_txb()
1309*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_cost_coeffs_txb_laplacian()
1310*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_cost_coeffs_txb_estimate()
1311*77c1e3ccSAndroid Build Coastguard Worker
1312*77c1e3ccSAndroid Build Coastguard Worker\paragraph architecture_entropy_coef_opt Quantized level optimization
1313*77c1e3ccSAndroid Build Coastguard WorkerBeside computing entropy cost, the encoder also uses \ref av1_optimize_txb()
1314*77c1e3ccSAndroid Build Coastguard Workerto adjust the coefficient’s quantized levels to achieve optimal RD trade-off.
1315*77c1e3ccSAndroid Build Coastguard WorkerIn \ref av1_optimize_txb(), the encoder goes through each quantized
1316*77c1e3ccSAndroid Build Coastguard Workercoefficient and lowers the quantized coefficient level by one if the action
1317*77c1e3ccSAndroid Build Coastguard Workeryields a better RD score.
1318*77c1e3ccSAndroid Build Coastguard Worker
1319*77c1e3ccSAndroid Build Coastguard WorkerRelated functions:
1320*77c1e3ccSAndroid Build Coastguard Worker- \ref av1_optimize_txb()
1321*77c1e3ccSAndroid Build Coastguard Worker
1322*77c1e3ccSAndroid Build Coastguard WorkerAll the related functions are listed in \ref coefficient_coding.
1323*77c1e3ccSAndroid Build Coastguard Worker
1324*77c1e3ccSAndroid Build Coastguard Worker\section architecture_simd SIMD usage
1325*77c1e3ccSAndroid Build Coastguard Worker
1326*77c1e3ccSAndroid Build Coastguard WorkerIn order to efficiently encode video on modern platforms, it is necessary to
1327*77c1e3ccSAndroid Build Coastguard Workerimplement optimized versions of many core encoding and decoding functions using
1328*77c1e3ccSAndroid Build Coastguard Workerarchitecture-specific SIMD instructions.
1329*77c1e3ccSAndroid Build Coastguard Worker
1330*77c1e3ccSAndroid Build Coastguard WorkerFunctions which have optimized implementations will have multiple variants
1331*77c1e3ccSAndroid Build Coastguard Workerin the code, each suffixed with the name of the appropriate instruction set.
1332*77c1e3ccSAndroid Build Coastguard WorkerThere will additionally be an `_c` version, which acts as a reference
1333*77c1e3ccSAndroid Build Coastguard Workerimplementation which the SIMD variants can be tested against.
1334*77c1e3ccSAndroid Build Coastguard Worker
1335*77c1e3ccSAndroid Build Coastguard WorkerAs different machines with the same nominal architecture may support different
1336*77c1e3ccSAndroid Build Coastguard Workersubsets of SIMD instructions, we have dynamic CPU detection logic which chooses
1337*77c1e3ccSAndroid Build Coastguard Workerthe appropriate functions to use at run time. This process is handled by
1338*77c1e3ccSAndroid Build Coastguard Worker`build/cmake/rtcd.pl`, with function definitions in the files
1339*77c1e3ccSAndroid Build Coastguard Worker`*_rtcd_defs.pl` elsewhere in the codebase.
1340*77c1e3ccSAndroid Build Coastguard Worker
1341*77c1e3ccSAndroid Build Coastguard WorkerCurrently SIMD is supported on the following platforms:
1342*77c1e3ccSAndroid Build Coastguard Worker
1343*77c1e3ccSAndroid Build Coastguard Worker- x86: Requires SSE4.1 or above
1344*77c1e3ccSAndroid Build Coastguard Worker
1345*77c1e3ccSAndroid Build Coastguard Worker- Arm: Requires Neon (Armv7-A and above)
1346*77c1e3ccSAndroid Build Coastguard Worker
1347*77c1e3ccSAndroid Build Coastguard WorkerWe aim to provide implementations of all performance-critical functions which
1348*77c1e3ccSAndroid Build Coastguard Workerare compatible with the instruction sets listed above. Additional SIMD
1349*77c1e3ccSAndroid Build Coastguard Workerextensions (e.g. AVX on x86, SVE on Arm) are also used to provide even
1350*77c1e3ccSAndroid Build Coastguard Workergreater performance where available.
1351*77c1e3ccSAndroid Build Coastguard Worker
1352*77c1e3ccSAndroid Build Coastguard Worker*/
1353*77c1e3ccSAndroid Build Coastguard Worker
1354*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup encoder_algo Encoder Algorithm
1355*77c1e3ccSAndroid Build Coastguard Worker *
1356*77c1e3ccSAndroid Build Coastguard Worker * The encoder algorithm describes how a sequence is encoded, including high
1357*77c1e3ccSAndroid Build Coastguard Worker * level decision as well as algorithm used at every encoding stage.
1358*77c1e3ccSAndroid Build Coastguard Worker */
1359*77c1e3ccSAndroid Build Coastguard Worker
1360*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup high_level_algo High-level Algorithm
1361*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1362*77c1e3ccSAndroid Build Coastguard Worker * This module describes sequence level/frame level algorithm in AV1.
1363*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1364*77c1e3ccSAndroid Build Coastguard Worker * @{
1365*77c1e3ccSAndroid Build Coastguard Worker */
1366*77c1e3ccSAndroid Build Coastguard Worker
1367*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup speed_features Speed vs Quality Trade Off
1368*77c1e3ccSAndroid Build Coastguard Worker * \ingroup high_level_algo
1369*77c1e3ccSAndroid Build Coastguard Worker * This module describes the encode speed vs quality tradeoff
1370*77c1e3ccSAndroid Build Coastguard Worker * @{
1371*77c1e3ccSAndroid Build Coastguard Worker */
1372*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup speed_features */
1373*77c1e3ccSAndroid Build Coastguard Worker
1374*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup src_frame_proc Source Frame Processing
1375*77c1e3ccSAndroid Build Coastguard Worker * \ingroup high_level_algo
1376*77c1e3ccSAndroid Build Coastguard Worker * This module describes algorithms in AV1 assosciated with the
1377*77c1e3ccSAndroid Build Coastguard Worker * pre-processing of source frames. See also \ref architecture_enc_src_proc
1378*77c1e3ccSAndroid Build Coastguard Worker *
1379*77c1e3ccSAndroid Build Coastguard Worker * @{
1380*77c1e3ccSAndroid Build Coastguard Worker */
1381*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup src_frame_proc */
1382*77c1e3ccSAndroid Build Coastguard Worker
1383*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup rate_control Rate Control
1384*77c1e3ccSAndroid Build Coastguard Worker * \ingroup high_level_algo
1385*77c1e3ccSAndroid Build Coastguard Worker * This module describes rate control algorithm in AV1.
1386*77c1e3ccSAndroid Build Coastguard Worker *  See also \ref architecture_enc_rate_ctrl
1387*77c1e3ccSAndroid Build Coastguard Worker * @{
1388*77c1e3ccSAndroid Build Coastguard Worker */
1389*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup rate_control */
1390*77c1e3ccSAndroid Build Coastguard Worker
1391*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup tpl_modelling Temporal Dependency Modelling
1392*77c1e3ccSAndroid Build Coastguard Worker * \ingroup high_level_algo
1393*77c1e3ccSAndroid Build Coastguard Worker * This module includes algorithms to implement temporal dependency modelling.
1394*77c1e3ccSAndroid Build Coastguard Worker *  See also \ref architecture_enc_tpl
1395*77c1e3ccSAndroid Build Coastguard Worker * @{
1396*77c1e3ccSAndroid Build Coastguard Worker */
1397*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup tpl_modelling */
1398*77c1e3ccSAndroid Build Coastguard Worker
1399*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup two_pass_algo Two Pass Mode
1400*77c1e3ccSAndroid Build Coastguard Worker   \ingroup high_level_algo
1401*77c1e3ccSAndroid Build Coastguard Worker
1402*77c1e3ccSAndroid Build Coastguard Worker In two pass mode, the input file is passed into the encoder for a quick
1403*77c1e3ccSAndroid Build Coastguard Worker first pass, where statistics are gathered. These statistics and the input
1404*77c1e3ccSAndroid Build Coastguard Worker file are then passed back into the encoder for a second pass. The statistics
1405*77c1e3ccSAndroid Build Coastguard Worker help the encoder reach the desired bitrate without as much overshooting or
1406*77c1e3ccSAndroid Build Coastguard Worker undershooting.
1407*77c1e3ccSAndroid Build Coastguard Worker
1408*77c1e3ccSAndroid Build Coastguard Worker During the first pass, the codec will return "stats" packets that contain
1409*77c1e3ccSAndroid Build Coastguard Worker information useful for the second pass. The caller should concatenate these
1410*77c1e3ccSAndroid Build Coastguard Worker packets as they are received. In the second pass, the concatenated packets
1411*77c1e3ccSAndroid Build Coastguard Worker are passed in, along with the frames to encode. During the second pass,
1412*77c1e3ccSAndroid Build Coastguard Worker "frame" packets are returned that represent the compressed video.
1413*77c1e3ccSAndroid Build Coastguard Worker
1414*77c1e3ccSAndroid Build Coastguard Worker A complete example can be found in `examples/twopass_encoder.c`. Pseudocode
1415*77c1e3ccSAndroid Build Coastguard Worker is provided below to illustrate the core parts.
1416*77c1e3ccSAndroid Build Coastguard Worker
1417*77c1e3ccSAndroid Build Coastguard Worker During the first pass, the uncompressed frames are passed in and stats
1418*77c1e3ccSAndroid Build Coastguard Worker information is appended to a byte array.
1419*77c1e3ccSAndroid Build Coastguard Worker
1420*77c1e3ccSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~{.c}
1421*77c1e3ccSAndroid Build Coastguard Worker// For simplicity, assume that there is enough memory in the stats buffer.
1422*77c1e3ccSAndroid Build Coastguard Worker// Actual code will want to use a resizable array. stats_len represents
1423*77c1e3ccSAndroid Build Coastguard Worker// the length of data already present in the buffer.
1424*77c1e3ccSAndroid Build Coastguard Workervoid get_stats_data(aom_codec_ctx_t *encoder, char *stats,
1425*77c1e3ccSAndroid Build Coastguard Worker                    size_t *stats_len, bool *got_data) {
1426*77c1e3ccSAndroid Build Coastguard Worker  const aom_codec_cx_pkt_t *pkt;
1427*77c1e3ccSAndroid Build Coastguard Worker  aom_codec_iter_t iter = NULL;
1428*77c1e3ccSAndroid Build Coastguard Worker  while ((pkt = aom_codec_get_cx_data(encoder, &iter))) {
1429*77c1e3ccSAndroid Build Coastguard Worker    *got_data = true;
1430*77c1e3ccSAndroid Build Coastguard Worker    if (pkt->kind != AOM_CODEC_STATS_PKT) continue;
1431*77c1e3ccSAndroid Build Coastguard Worker    memcpy(stats + *stats_len, pkt->data.twopass_stats.buf,
1432*77c1e3ccSAndroid Build Coastguard Worker           pkt->data.twopass_stats.sz);
1433*77c1e3ccSAndroid Build Coastguard Worker    *stats_len += pkt->data.twopass_stats.sz;
1434*77c1e3ccSAndroid Build Coastguard Worker  }
1435*77c1e3ccSAndroid Build Coastguard Worker}
1436*77c1e3ccSAndroid Build Coastguard Worker
1437*77c1e3ccSAndroid Build Coastguard Workervoid first_pass(char *stats, size_t *stats_len) {
1438*77c1e3ccSAndroid Build Coastguard Worker  struct aom_codec_enc_cfg first_pass_cfg;
1439*77c1e3ccSAndroid Build Coastguard Worker  ... // Initialize the config as needed.
1440*77c1e3ccSAndroid Build Coastguard Worker  first_pass_cfg.g_pass = AOM_RC_FIRST_PASS;
1441*77c1e3ccSAndroid Build Coastguard Worker  aom_codec_ctx_t first_pass_encoder;
1442*77c1e3ccSAndroid Build Coastguard Worker  ... // Initialize the encoder.
1443*77c1e3ccSAndroid Build Coastguard Worker
1444*77c1e3ccSAndroid Build Coastguard Worker  while (frame_available) {
1445*77c1e3ccSAndroid Build Coastguard Worker    // Read in the uncompressed frame, update frame_available
1446*77c1e3ccSAndroid Build Coastguard Worker    aom_image_t *frame_to_encode = ...;
1447*77c1e3ccSAndroid Build Coastguard Worker    aom_codec_encode(&first_pass_encoder, img, pts, duration, flags);
1448*77c1e3ccSAndroid Build Coastguard Worker    get_stats_data(&first_pass_encoder, stats, stats_len);
1449*77c1e3ccSAndroid Build Coastguard Worker  }
1450*77c1e3ccSAndroid Build Coastguard Worker  // After all frames have been processed, call aom_codec_encode with
1451*77c1e3ccSAndroid Build Coastguard Worker  // a NULL ptr repeatedly, until no more data is returned. The NULL
1452*77c1e3ccSAndroid Build Coastguard Worker  // ptr tells the encoder that no more frames are available.
1453*77c1e3ccSAndroid Build Coastguard Worker  bool got_data;
1454*77c1e3ccSAndroid Build Coastguard Worker  do {
1455*77c1e3ccSAndroid Build Coastguard Worker    got_data = false;
1456*77c1e3ccSAndroid Build Coastguard Worker    aom_codec_encode(&first_pass_encoder, NULL, pts, duration, flags);
1457*77c1e3ccSAndroid Build Coastguard Worker    get_stats_data(&first_pass_encoder, stats, stats_len, &got_data);
1458*77c1e3ccSAndroid Build Coastguard Worker  } while (got_data);
1459*77c1e3ccSAndroid Build Coastguard Worker
1460*77c1e3ccSAndroid Build Coastguard Worker  aom_codec_destroy(&first_pass_encoder);
1461*77c1e3ccSAndroid Build Coastguard Worker}
1462*77c1e3ccSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~
1463*77c1e3ccSAndroid Build Coastguard Worker
1464*77c1e3ccSAndroid Build Coastguard Worker During the second pass, the uncompressed frames and the stats are
1465*77c1e3ccSAndroid Build Coastguard Worker passed into the encoder.
1466*77c1e3ccSAndroid Build Coastguard Worker
1467*77c1e3ccSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~{.c}
1468*77c1e3ccSAndroid Build Coastguard Worker// Write out each encoded frame to the file.
1469*77c1e3ccSAndroid Build Coastguard Workervoid get_cx_data(aom_codec_ctx_t *encoder, FILE *file,
1470*77c1e3ccSAndroid Build Coastguard Worker                 bool *got_data) {
1471*77c1e3ccSAndroid Build Coastguard Worker  const aom_codec_cx_pkt_t *pkt;
1472*77c1e3ccSAndroid Build Coastguard Worker  aom_codec_iter_t iter = NULL;
1473*77c1e3ccSAndroid Build Coastguard Worker  while ((pkt = aom_codec_get_cx_data(encoder, &iter))) {
1474*77c1e3ccSAndroid Build Coastguard Worker   *got_data = true;
1475*77c1e3ccSAndroid Build Coastguard Worker   if (pkt->kind != AOM_CODEC_CX_FRAME_PKT) continue;
1476*77c1e3ccSAndroid Build Coastguard Worker   fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, file);
1477*77c1e3ccSAndroid Build Coastguard Worker  }
1478*77c1e3ccSAndroid Build Coastguard Worker}
1479*77c1e3ccSAndroid Build Coastguard Worker
1480*77c1e3ccSAndroid Build Coastguard Workervoid second_pass(char *stats, size_t stats_len) {
1481*77c1e3ccSAndroid Build Coastguard Worker  struct aom_codec_enc_cfg second_pass_cfg;
1482*77c1e3ccSAndroid Build Coastguard Worker  ... // Initialize the config file as needed.
1483*77c1e3ccSAndroid Build Coastguard Worker  second_pass_cfg.g_pass = AOM_RC_LAST_PASS;
1484*77c1e3ccSAndroid Build Coastguard Worker  cfg.rc_twopass_stats_in.buf = stats;
1485*77c1e3ccSAndroid Build Coastguard Worker  cfg.rc_twopass_stats_in.sz = stats_len;
1486*77c1e3ccSAndroid Build Coastguard Worker  aom_codec_ctx_t second_pass_encoder;
1487*77c1e3ccSAndroid Build Coastguard Worker  ... // Initialize the encoder from the config.
1488*77c1e3ccSAndroid Build Coastguard Worker
1489*77c1e3ccSAndroid Build Coastguard Worker  FILE *output = fopen("output.obu", "wb");
1490*77c1e3ccSAndroid Build Coastguard Worker  while (frame_available) {
1491*77c1e3ccSAndroid Build Coastguard Worker    // Read in the uncompressed frame, update frame_available
1492*77c1e3ccSAndroid Build Coastguard Worker    aom_image_t *frame_to_encode = ...;
1493*77c1e3ccSAndroid Build Coastguard Worker    aom_codec_encode(&second_pass_encoder, img, pts, duration, flags);
1494*77c1e3ccSAndroid Build Coastguard Worker    get_cx_data(&second_pass_encoder, output);
1495*77c1e3ccSAndroid Build Coastguard Worker  }
1496*77c1e3ccSAndroid Build Coastguard Worker  // Pass in NULL to flush the encoder.
1497*77c1e3ccSAndroid Build Coastguard Worker  bool got_data;
1498*77c1e3ccSAndroid Build Coastguard Worker  do {
1499*77c1e3ccSAndroid Build Coastguard Worker    got_data = false;
1500*77c1e3ccSAndroid Build Coastguard Worker    aom_codec_encode(&second_pass_encoder, NULL, pts, duration, flags);
1501*77c1e3ccSAndroid Build Coastguard Worker    get_cx_data(&second_pass_encoder, output, &got_data);
1502*77c1e3ccSAndroid Build Coastguard Worker  } while (got_data);
1503*77c1e3ccSAndroid Build Coastguard Worker
1504*77c1e3ccSAndroid Build Coastguard Worker  aom_codec_destroy(&second_pass_encoder);
1505*77c1e3ccSAndroid Build Coastguard Worker}
1506*77c1e3ccSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~
1507*77c1e3ccSAndroid Build Coastguard Worker */
1508*77c1e3ccSAndroid Build Coastguard Worker
1509*77c1e3ccSAndroid Build Coastguard Worker /*!\defgroup look_ahead_buffer The Look-Ahead Buffer
1510*77c1e3ccSAndroid Build Coastguard Worker    \ingroup high_level_algo
1511*77c1e3ccSAndroid Build Coastguard Worker
1512*77c1e3ccSAndroid Build Coastguard Worker A program should call \ref aom_codec_encode() for each frame that needs
1513*77c1e3ccSAndroid Build Coastguard Worker processing. These frames are internally copied and stored in a fixed-size
1514*77c1e3ccSAndroid Build Coastguard Worker circular buffer, known as the look-ahead buffer. Other parts of the code
1515*77c1e3ccSAndroid Build Coastguard Worker will use future frame information to inform current frame decisions;
1516*77c1e3ccSAndroid Build Coastguard Worker examples include the first-pass algorithm, TPL model, and temporal filter.
1517*77c1e3ccSAndroid Build Coastguard Worker Note that this buffer also keeps a reference to the last source frame.
1518*77c1e3ccSAndroid Build Coastguard Worker
1519*77c1e3ccSAndroid Build Coastguard Worker The look-ahead buffer is defined in \ref av1/encoder/lookahead.h. It acts as an
1520*77c1e3ccSAndroid Build Coastguard Worker opaque structure, with an interface to create and free memory associated with
1521*77c1e3ccSAndroid Build Coastguard Worker it. It supports pushing and popping frames onto the structure in a FIFO
1522*77c1e3ccSAndroid Build Coastguard Worker fashion. It also allows look-ahead when using the \ref av1_lookahead_peek()
1523*77c1e3ccSAndroid Build Coastguard Worker function with a non-negative number, and look-behind when -1 is passed in (for
1524*77c1e3ccSAndroid Build Coastguard Worker the last source frame; e.g., firstpass will use this for motion estimation).
1525*77c1e3ccSAndroid Build Coastguard Worker The \ref av1_lookahead_depth() function returns the current number of frames
1526*77c1e3ccSAndroid Build Coastguard Worker stored in it. Note that \ref av1_lookahead_pop() is a bit of a misnomer - it
1527*77c1e3ccSAndroid Build Coastguard Worker only pops if either the "flush" variable is set, or the buffer is at maximum
1528*77c1e3ccSAndroid Build Coastguard Worker capacity.
1529*77c1e3ccSAndroid Build Coastguard Worker
1530*77c1e3ccSAndroid Build Coastguard Worker The buffer is stored in the \ref AV1_PRIMARY::lookahead field.
1531*77c1e3ccSAndroid Build Coastguard Worker It is initialized in the first call to \ref aom_codec_encode(), in the
1532*77c1e3ccSAndroid Build Coastguard Worker \ref av1_receive_raw_frame() sub-routine. The buffer size is defined by
1533*77c1e3ccSAndroid Build Coastguard Worker the g_lag_in_frames parameter set in the
1534*77c1e3ccSAndroid Build Coastguard Worker \ref aom_codec_enc_cfg_t::g_lag_in_frames struct.
1535*77c1e3ccSAndroid Build Coastguard Worker This can be modified manually but should only be set once. On the command
1536*77c1e3ccSAndroid Build Coastguard Worker line, the flag "--lag-in-frames" controls it. The default size is 19 for
1537*77c1e3ccSAndroid Build Coastguard Worker non-realtime usage and 1 for realtime. Note that a maximum value of 35 is
1538*77c1e3ccSAndroid Build Coastguard Worker enforced.
1539*77c1e3ccSAndroid Build Coastguard Worker
1540*77c1e3ccSAndroid Build Coastguard Worker A frame will stay in the buffer as long as possible. As mentioned above,
1541*77c1e3ccSAndroid Build Coastguard Worker the \ref av1_lookahead_pop() only removes a frame when either flush is set,
1542*77c1e3ccSAndroid Build Coastguard Worker or the buffer is full. Note that each call to \ref aom_codec_encode() inserts
1543*77c1e3ccSAndroid Build Coastguard Worker another frame into the buffer, and pop is called by the sub-function
1544*77c1e3ccSAndroid Build Coastguard Worker \ref av1_encode_strategy(). The buffer is told to flush when
1545*77c1e3ccSAndroid Build Coastguard Worker \ref aom_codec_encode() is passed a NULL image pointer. Note that the caller
1546*77c1e3ccSAndroid Build Coastguard Worker must repeatedly call \ref aom_codec_encode() with a NULL image pointer, until
1547*77c1e3ccSAndroid Build Coastguard Worker no more packets are available, in order to fully flush the buffer.
1548*77c1e3ccSAndroid Build Coastguard Worker
1549*77c1e3ccSAndroid Build Coastguard Worker */
1550*77c1e3ccSAndroid Build Coastguard Worker
1551*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup high_level_algo */
1552*77c1e3ccSAndroid Build Coastguard Worker
1553*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup partition_search Partition Search
1554*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1555*77c1e3ccSAndroid Build Coastguard Worker * For and overview of the partition search see \ref architecture_enc_partitions
1556*77c1e3ccSAndroid Build Coastguard Worker * @{
1557*77c1e3ccSAndroid Build Coastguard Worker */
1558*77c1e3ccSAndroid Build Coastguard Worker
1559*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup partition_search */
1560*77c1e3ccSAndroid Build Coastguard Worker
1561*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup intra_mode_search Intra Mode Search
1562*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1563*77c1e3ccSAndroid Build Coastguard Worker * This module describes intra mode search algorithm in AV1.
1564*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1565*77c1e3ccSAndroid Build Coastguard Worker * @{
1566*77c1e3ccSAndroid Build Coastguard Worker */
1567*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup intra_mode_search */
1568*77c1e3ccSAndroid Build Coastguard Worker
1569*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup inter_mode_search Inter Mode Search
1570*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1571*77c1e3ccSAndroid Build Coastguard Worker * This module describes inter mode search algorithm in AV1.
1572*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1573*77c1e3ccSAndroid Build Coastguard Worker * @{
1574*77c1e3ccSAndroid Build Coastguard Worker */
1575*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup inter_mode_search */
1576*77c1e3ccSAndroid Build Coastguard Worker
1577*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup palette_mode_search Palette Mode Search
1578*77c1e3ccSAndroid Build Coastguard Worker * \ingroup intra_mode_search
1579*77c1e3ccSAndroid Build Coastguard Worker * This module describes palette mode search algorithm in AV1.
1580*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1581*77c1e3ccSAndroid Build Coastguard Worker * @{
1582*77c1e3ccSAndroid Build Coastguard Worker */
1583*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup palette_mode_search */
1584*77c1e3ccSAndroid Build Coastguard Worker
1585*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup transform_search Transform Search
1586*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1587*77c1e3ccSAndroid Build Coastguard Worker * This module describes transform search algorithm in AV1.
1588*77c1e3ccSAndroid Build Coastguard Worker * @{
1589*77c1e3ccSAndroid Build Coastguard Worker */
1590*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup transform_search */
1591*77c1e3ccSAndroid Build Coastguard Worker
1592*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup coefficient_coding Transform Coefficient Coding and Optimization
1593*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1594*77c1e3ccSAndroid Build Coastguard Worker * This module describes the algorithms of transform coefficient coding and optimization in AV1.
1595*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1596*77c1e3ccSAndroid Build Coastguard Worker * @{
1597*77c1e3ccSAndroid Build Coastguard Worker */
1598*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup coefficient_coding */
1599*77c1e3ccSAndroid Build Coastguard Worker
1600*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup in_loop_filter In-loop Filter
1601*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1602*77c1e3ccSAndroid Build Coastguard Worker * This module describes in-loop filter algorithm in AV1.
1603*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1604*77c1e3ccSAndroid Build Coastguard Worker * @{
1605*77c1e3ccSAndroid Build Coastguard Worker */
1606*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup in_loop_filter */
1607*77c1e3ccSAndroid Build Coastguard Worker
1608*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup in_loop_cdef CDEF
1609*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1610*77c1e3ccSAndroid Build Coastguard Worker * This module describes the CDEF parameter search algorithm
1611*77c1e3ccSAndroid Build Coastguard Worker * in AV1. More details will be added.
1612*77c1e3ccSAndroid Build Coastguard Worker * @{
1613*77c1e3ccSAndroid Build Coastguard Worker */
1614*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup in_loop_restoration */
1615*77c1e3ccSAndroid Build Coastguard Worker
1616*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup in_loop_restoration Loop Restoration
1617*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1618*77c1e3ccSAndroid Build Coastguard Worker * This module describes the loop restoration search
1619*77c1e3ccSAndroid Build Coastguard Worker * and estimation algorithm in AV1.
1620*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1621*77c1e3ccSAndroid Build Coastguard Worker * @{
1622*77c1e3ccSAndroid Build Coastguard Worker */
1623*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup in_loop_restoration */
1624*77c1e3ccSAndroid Build Coastguard Worker
1625*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup cyclic_refresh Cyclic Refresh
1626*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1627*77c1e3ccSAndroid Build Coastguard Worker * This module describes the cyclic refresh (aq-mode=3) in AV1.
1628*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1629*77c1e3ccSAndroid Build Coastguard Worker * @{
1630*77c1e3ccSAndroid Build Coastguard Worker */
1631*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup cyclic_refresh */
1632*77c1e3ccSAndroid Build Coastguard Worker
1633*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup SVC Scalable Video Coding
1634*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1635*77c1e3ccSAndroid Build Coastguard Worker * This module describes scalable video coding algorithm in AV1.
1636*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1637*77c1e3ccSAndroid Build Coastguard Worker * @{
1638*77c1e3ccSAndroid Build Coastguard Worker */
1639*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup SVC */
1640*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup variance_partition Variance Partition
1641*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1642*77c1e3ccSAndroid Build Coastguard Worker * This module describes variance partition algorithm in AV1.
1643*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1644*77c1e3ccSAndroid Build Coastguard Worker * @{
1645*77c1e3ccSAndroid Build Coastguard Worker */
1646*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup variance_partition */
1647*77c1e3ccSAndroid Build Coastguard Worker/*!\defgroup nonrd_mode_search NonRD Optimized Mode Search
1648*77c1e3ccSAndroid Build Coastguard Worker * \ingroup encoder_algo
1649*77c1e3ccSAndroid Build Coastguard Worker * This module describes NonRD Optimized Mode Search used in Real-Time mode.
1650*77c1e3ccSAndroid Build Coastguard Worker * More details will be added.
1651*77c1e3ccSAndroid Build Coastguard Worker * @{
1652*77c1e3ccSAndroid Build Coastguard Worker */
1653*77c1e3ccSAndroid Build Coastguard Worker/*! @} - end defgroup nonrd_mode_search */
1654