xref: /aosp_15_r20/external/webp/src/enc/frame_enc.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2011 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li //   frame coding and analysis
11*b2055c35SXin Li //
12*b2055c35SXin Li // Author: Skal ([email protected])
13*b2055c35SXin Li 
14*b2055c35SXin Li #include <string.h>
15*b2055c35SXin Li #include <math.h>
16*b2055c35SXin Li 
17*b2055c35SXin Li #include "src/enc/cost_enc.h"
18*b2055c35SXin Li #include "src/enc/vp8i_enc.h"
19*b2055c35SXin Li #include "src/dsp/dsp.h"
20*b2055c35SXin Li #include "src/webp/format_constants.h"  // RIFF constants
21*b2055c35SXin Li 
22*b2055c35SXin Li #define SEGMENT_VISU 0
23*b2055c35SXin Li #define DEBUG_SEARCH 0    // useful to track search convergence
24*b2055c35SXin Li 
25*b2055c35SXin Li //------------------------------------------------------------------------------
26*b2055c35SXin Li // multi-pass convergence
27*b2055c35SXin Li 
28*b2055c35SXin Li #define HEADER_SIZE_ESTIMATE (RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE +  \
29*b2055c35SXin Li                               VP8_FRAME_HEADER_SIZE)
30*b2055c35SXin Li #define DQ_LIMIT 0.4  // convergence is considered reached if dq < DQ_LIMIT
31*b2055c35SXin Li // we allow 2k of extra head-room in PARTITION0 limit.
32*b2055c35SXin Li #define PARTITION0_SIZE_LIMIT ((VP8_MAX_PARTITION0_SIZE - 2048ULL) << 11)
33*b2055c35SXin Li 
Clamp(float v,float min,float max)34*b2055c35SXin Li static float Clamp(float v, float min, float max) {
35*b2055c35SXin Li   return (v < min) ? min : (v > max) ? max : v;
36*b2055c35SXin Li }
37*b2055c35SXin Li 
38*b2055c35SXin Li typedef struct {  // struct for organizing convergence in either size or PSNR
39*b2055c35SXin Li   int is_first;
40*b2055c35SXin Li   float dq;
41*b2055c35SXin Li   float q, last_q;
42*b2055c35SXin Li   float qmin, qmax;
43*b2055c35SXin Li   double value, last_value;   // PSNR or size
44*b2055c35SXin Li   double target;
45*b2055c35SXin Li   int do_size_search;
46*b2055c35SXin Li } PassStats;
47*b2055c35SXin Li 
InitPassStats(const VP8Encoder * const enc,PassStats * const s)48*b2055c35SXin Li static int InitPassStats(const VP8Encoder* const enc, PassStats* const s) {
49*b2055c35SXin Li   const uint64_t target_size = (uint64_t)enc->config_->target_size;
50*b2055c35SXin Li   const int do_size_search = (target_size != 0);
51*b2055c35SXin Li   const float target_PSNR = enc->config_->target_PSNR;
52*b2055c35SXin Li 
53*b2055c35SXin Li   s->is_first = 1;
54*b2055c35SXin Li   s->dq = 10.f;
55*b2055c35SXin Li   s->qmin = 1.f * enc->config_->qmin;
56*b2055c35SXin Li   s->qmax = 1.f * enc->config_->qmax;
57*b2055c35SXin Li   s->q = s->last_q = Clamp(enc->config_->quality, s->qmin, s->qmax);
58*b2055c35SXin Li   s->target = do_size_search ? (double)target_size
59*b2055c35SXin Li             : (target_PSNR > 0.) ? target_PSNR
60*b2055c35SXin Li             : 40.;   // default, just in case
61*b2055c35SXin Li   s->value = s->last_value = 0.;
62*b2055c35SXin Li   s->do_size_search = do_size_search;
63*b2055c35SXin Li   return do_size_search;
64*b2055c35SXin Li }
65*b2055c35SXin Li 
ComputeNextQ(PassStats * const s)66*b2055c35SXin Li static float ComputeNextQ(PassStats* const s) {
67*b2055c35SXin Li   float dq;
68*b2055c35SXin Li   if (s->is_first) {
69*b2055c35SXin Li     dq = (s->value > s->target) ? -s->dq : s->dq;
70*b2055c35SXin Li     s->is_first = 0;
71*b2055c35SXin Li   } else if (s->value != s->last_value) {
72*b2055c35SXin Li     const double slope = (s->target - s->value) / (s->last_value - s->value);
73*b2055c35SXin Li     dq = (float)(slope * (s->last_q - s->q));
74*b2055c35SXin Li   } else {
75*b2055c35SXin Li     dq = 0.;  // we're done?!
76*b2055c35SXin Li   }
77*b2055c35SXin Li   // Limit variable to avoid large swings.
78*b2055c35SXin Li   s->dq = Clamp(dq, -30.f, 30.f);
79*b2055c35SXin Li   s->last_q = s->q;
80*b2055c35SXin Li   s->last_value = s->value;
81*b2055c35SXin Li   s->q = Clamp(s->q + s->dq, s->qmin, s->qmax);
82*b2055c35SXin Li   return s->q;
83*b2055c35SXin Li }
84*b2055c35SXin Li 
85*b2055c35SXin Li //------------------------------------------------------------------------------
86*b2055c35SXin Li // Tables for level coding
87*b2055c35SXin Li 
88*b2055c35SXin Li const uint8_t VP8Cat3[] = { 173, 148, 140 };
89*b2055c35SXin Li const uint8_t VP8Cat4[] = { 176, 155, 140, 135 };
90*b2055c35SXin Li const uint8_t VP8Cat5[] = { 180, 157, 141, 134, 130 };
91*b2055c35SXin Li const uint8_t VP8Cat6[] =
92*b2055c35SXin Li     { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129 };
93*b2055c35SXin Li 
94*b2055c35SXin Li //------------------------------------------------------------------------------
95*b2055c35SXin Li // Reset the statistics about: number of skips, token proba, level cost,...
96*b2055c35SXin Li 
ResetStats(VP8Encoder * const enc)97*b2055c35SXin Li static void ResetStats(VP8Encoder* const enc) {
98*b2055c35SXin Li   VP8EncProba* const proba = &enc->proba_;
99*b2055c35SXin Li   VP8CalculateLevelCosts(proba);
100*b2055c35SXin Li   proba->nb_skip_ = 0;
101*b2055c35SXin Li }
102*b2055c35SXin Li 
103*b2055c35SXin Li //------------------------------------------------------------------------------
104*b2055c35SXin Li // Skip decision probability
105*b2055c35SXin Li 
106*b2055c35SXin Li #define SKIP_PROBA_THRESHOLD 250  // value below which using skip_proba is OK.
107*b2055c35SXin Li 
CalcSkipProba(uint64_t nb,uint64_t total)108*b2055c35SXin Li static int CalcSkipProba(uint64_t nb, uint64_t total) {
109*b2055c35SXin Li   return (int)(total ? (total - nb) * 255 / total : 255);
110*b2055c35SXin Li }
111*b2055c35SXin Li 
112*b2055c35SXin Li // Returns the bit-cost for coding the skip probability.
FinalizeSkipProba(VP8Encoder * const enc)113*b2055c35SXin Li static int FinalizeSkipProba(VP8Encoder* const enc) {
114*b2055c35SXin Li   VP8EncProba* const proba = &enc->proba_;
115*b2055c35SXin Li   const int nb_mbs = enc->mb_w_ * enc->mb_h_;
116*b2055c35SXin Li   const int nb_events = proba->nb_skip_;
117*b2055c35SXin Li   int size;
118*b2055c35SXin Li   proba->skip_proba_ = CalcSkipProba(nb_events, nb_mbs);
119*b2055c35SXin Li   proba->use_skip_proba_ = (proba->skip_proba_ < SKIP_PROBA_THRESHOLD);
120*b2055c35SXin Li   size = 256;   // 'use_skip_proba' bit
121*b2055c35SXin Li   if (proba->use_skip_proba_) {
122*b2055c35SXin Li     size +=  nb_events * VP8BitCost(1, proba->skip_proba_)
123*b2055c35SXin Li          + (nb_mbs - nb_events) * VP8BitCost(0, proba->skip_proba_);
124*b2055c35SXin Li     size += 8 * 256;   // cost of signaling the skip_proba_ itself.
125*b2055c35SXin Li   }
126*b2055c35SXin Li   return size;
127*b2055c35SXin Li }
128*b2055c35SXin Li 
129*b2055c35SXin Li // Collect statistics and deduce probabilities for next coding pass.
130*b2055c35SXin Li // Return the total bit-cost for coding the probability updates.
CalcTokenProba(int nb,int total)131*b2055c35SXin Li static int CalcTokenProba(int nb, int total) {
132*b2055c35SXin Li   assert(nb <= total);
133*b2055c35SXin Li   return nb ? (255 - nb * 255 / total) : 255;
134*b2055c35SXin Li }
135*b2055c35SXin Li 
136*b2055c35SXin Li // Cost of coding 'nb' 1's and 'total-nb' 0's using 'proba' probability.
BranchCost(int nb,int total,int proba)137*b2055c35SXin Li static int BranchCost(int nb, int total, int proba) {
138*b2055c35SXin Li   return nb * VP8BitCost(1, proba) + (total - nb) * VP8BitCost(0, proba);
139*b2055c35SXin Li }
140*b2055c35SXin Li 
ResetTokenStats(VP8Encoder * const enc)141*b2055c35SXin Li static void ResetTokenStats(VP8Encoder* const enc) {
142*b2055c35SXin Li   VP8EncProba* const proba = &enc->proba_;
143*b2055c35SXin Li   memset(proba->stats_, 0, sizeof(proba->stats_));
144*b2055c35SXin Li }
145*b2055c35SXin Li 
FinalizeTokenProbas(VP8EncProba * const proba)146*b2055c35SXin Li static int FinalizeTokenProbas(VP8EncProba* const proba) {
147*b2055c35SXin Li   int has_changed = 0;
148*b2055c35SXin Li   int size = 0;
149*b2055c35SXin Li   int t, b, c, p;
150*b2055c35SXin Li   for (t = 0; t < NUM_TYPES; ++t) {
151*b2055c35SXin Li     for (b = 0; b < NUM_BANDS; ++b) {
152*b2055c35SXin Li       for (c = 0; c < NUM_CTX; ++c) {
153*b2055c35SXin Li         for (p = 0; p < NUM_PROBAS; ++p) {
154*b2055c35SXin Li           const proba_t stats = proba->stats_[t][b][c][p];
155*b2055c35SXin Li           const int nb = (stats >> 0) & 0xffff;
156*b2055c35SXin Li           const int total = (stats >> 16) & 0xffff;
157*b2055c35SXin Li           const int update_proba = VP8CoeffsUpdateProba[t][b][c][p];
158*b2055c35SXin Li           const int old_p = VP8CoeffsProba0[t][b][c][p];
159*b2055c35SXin Li           const int new_p = CalcTokenProba(nb, total);
160*b2055c35SXin Li           const int old_cost = BranchCost(nb, total, old_p)
161*b2055c35SXin Li                              + VP8BitCost(0, update_proba);
162*b2055c35SXin Li           const int new_cost = BranchCost(nb, total, new_p)
163*b2055c35SXin Li                              + VP8BitCost(1, update_proba)
164*b2055c35SXin Li                              + 8 * 256;
165*b2055c35SXin Li           const int use_new_p = (old_cost > new_cost);
166*b2055c35SXin Li           size += VP8BitCost(use_new_p, update_proba);
167*b2055c35SXin Li           if (use_new_p) {  // only use proba that seem meaningful enough.
168*b2055c35SXin Li             proba->coeffs_[t][b][c][p] = new_p;
169*b2055c35SXin Li             has_changed |= (new_p != old_p);
170*b2055c35SXin Li             size += 8 * 256;
171*b2055c35SXin Li           } else {
172*b2055c35SXin Li             proba->coeffs_[t][b][c][p] = old_p;
173*b2055c35SXin Li           }
174*b2055c35SXin Li         }
175*b2055c35SXin Li       }
176*b2055c35SXin Li     }
177*b2055c35SXin Li   }
178*b2055c35SXin Li   proba->dirty_ = has_changed;
179*b2055c35SXin Li   return size;
180*b2055c35SXin Li }
181*b2055c35SXin Li 
182*b2055c35SXin Li //------------------------------------------------------------------------------
183*b2055c35SXin Li // Finalize Segment probability based on the coding tree
184*b2055c35SXin Li 
GetProba(int a,int b)185*b2055c35SXin Li static int GetProba(int a, int b) {
186*b2055c35SXin Li   const int total = a + b;
187*b2055c35SXin Li   return (total == 0) ? 255     // that's the default probability.
188*b2055c35SXin Li                       : (255 * a + total / 2) / total;  // rounded proba
189*b2055c35SXin Li }
190*b2055c35SXin Li 
ResetSegments(VP8Encoder * const enc)191*b2055c35SXin Li static void ResetSegments(VP8Encoder* const enc) {
192*b2055c35SXin Li   int n;
193*b2055c35SXin Li   for (n = 0; n < enc->mb_w_ * enc->mb_h_; ++n) {
194*b2055c35SXin Li     enc->mb_info_[n].segment_ = 0;
195*b2055c35SXin Li   }
196*b2055c35SXin Li }
197*b2055c35SXin Li 
SetSegmentProbas(VP8Encoder * const enc)198*b2055c35SXin Li static void SetSegmentProbas(VP8Encoder* const enc) {
199*b2055c35SXin Li   int p[NUM_MB_SEGMENTS] = { 0 };
200*b2055c35SXin Li   int n;
201*b2055c35SXin Li 
202*b2055c35SXin Li   for (n = 0; n < enc->mb_w_ * enc->mb_h_; ++n) {
203*b2055c35SXin Li     const VP8MBInfo* const mb = &enc->mb_info_[n];
204*b2055c35SXin Li     ++p[mb->segment_];
205*b2055c35SXin Li   }
206*b2055c35SXin Li #if !defined(WEBP_DISABLE_STATS)
207*b2055c35SXin Li   if (enc->pic_->stats != NULL) {
208*b2055c35SXin Li     for (n = 0; n < NUM_MB_SEGMENTS; ++n) {
209*b2055c35SXin Li       enc->pic_->stats->segment_size[n] = p[n];
210*b2055c35SXin Li     }
211*b2055c35SXin Li   }
212*b2055c35SXin Li #endif
213*b2055c35SXin Li   if (enc->segment_hdr_.num_segments_ > 1) {
214*b2055c35SXin Li     uint8_t* const probas = enc->proba_.segments_;
215*b2055c35SXin Li     probas[0] = GetProba(p[0] + p[1], p[2] + p[3]);
216*b2055c35SXin Li     probas[1] = GetProba(p[0], p[1]);
217*b2055c35SXin Li     probas[2] = GetProba(p[2], p[3]);
218*b2055c35SXin Li 
219*b2055c35SXin Li     enc->segment_hdr_.update_map_ =
220*b2055c35SXin Li         (probas[0] != 255) || (probas[1] != 255) || (probas[2] != 255);
221*b2055c35SXin Li     if (!enc->segment_hdr_.update_map_) ResetSegments(enc);
222*b2055c35SXin Li     enc->segment_hdr_.size_ =
223*b2055c35SXin Li         p[0] * (VP8BitCost(0, probas[0]) + VP8BitCost(0, probas[1])) +
224*b2055c35SXin Li         p[1] * (VP8BitCost(0, probas[0]) + VP8BitCost(1, probas[1])) +
225*b2055c35SXin Li         p[2] * (VP8BitCost(1, probas[0]) + VP8BitCost(0, probas[2])) +
226*b2055c35SXin Li         p[3] * (VP8BitCost(1, probas[0]) + VP8BitCost(1, probas[2]));
227*b2055c35SXin Li   } else {
228*b2055c35SXin Li     enc->segment_hdr_.update_map_ = 0;
229*b2055c35SXin Li     enc->segment_hdr_.size_ = 0;
230*b2055c35SXin Li   }
231*b2055c35SXin Li }
232*b2055c35SXin Li 
233*b2055c35SXin Li //------------------------------------------------------------------------------
234*b2055c35SXin Li // Coefficient coding
235*b2055c35SXin Li 
PutCoeffs(VP8BitWriter * const bw,int ctx,const VP8Residual * res)236*b2055c35SXin Li static int PutCoeffs(VP8BitWriter* const bw, int ctx, const VP8Residual* res) {
237*b2055c35SXin Li   int n = res->first;
238*b2055c35SXin Li   // should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 1
239*b2055c35SXin Li   const uint8_t* p = res->prob[n][ctx];
240*b2055c35SXin Li   if (!VP8PutBit(bw, res->last >= 0, p[0])) {
241*b2055c35SXin Li     return 0;
242*b2055c35SXin Li   }
243*b2055c35SXin Li 
244*b2055c35SXin Li   while (n < 16) {
245*b2055c35SXin Li     const int c = res->coeffs[n++];
246*b2055c35SXin Li     const int sign = c < 0;
247*b2055c35SXin Li     int v = sign ? -c : c;
248*b2055c35SXin Li     if (!VP8PutBit(bw, v != 0, p[1])) {
249*b2055c35SXin Li       p = res->prob[VP8EncBands[n]][0];
250*b2055c35SXin Li       continue;
251*b2055c35SXin Li     }
252*b2055c35SXin Li     if (!VP8PutBit(bw, v > 1, p[2])) {
253*b2055c35SXin Li       p = res->prob[VP8EncBands[n]][1];
254*b2055c35SXin Li     } else {
255*b2055c35SXin Li       if (!VP8PutBit(bw, v > 4, p[3])) {
256*b2055c35SXin Li         if (VP8PutBit(bw, v != 2, p[4])) {
257*b2055c35SXin Li           VP8PutBit(bw, v == 4, p[5]);
258*b2055c35SXin Li         }
259*b2055c35SXin Li       } else if (!VP8PutBit(bw, v > 10, p[6])) {
260*b2055c35SXin Li         if (!VP8PutBit(bw, v > 6, p[7])) {
261*b2055c35SXin Li           VP8PutBit(bw, v == 6, 159);
262*b2055c35SXin Li         } else {
263*b2055c35SXin Li           VP8PutBit(bw, v >= 9, 165);
264*b2055c35SXin Li           VP8PutBit(bw, !(v & 1), 145);
265*b2055c35SXin Li         }
266*b2055c35SXin Li       } else {
267*b2055c35SXin Li         int mask;
268*b2055c35SXin Li         const uint8_t* tab;
269*b2055c35SXin Li         if (v < 3 + (8 << 1)) {          // VP8Cat3  (3b)
270*b2055c35SXin Li           VP8PutBit(bw, 0, p[8]);
271*b2055c35SXin Li           VP8PutBit(bw, 0, p[9]);
272*b2055c35SXin Li           v -= 3 + (8 << 0);
273*b2055c35SXin Li           mask = 1 << 2;
274*b2055c35SXin Li           tab = VP8Cat3;
275*b2055c35SXin Li         } else if (v < 3 + (8 << 2)) {   // VP8Cat4  (4b)
276*b2055c35SXin Li           VP8PutBit(bw, 0, p[8]);
277*b2055c35SXin Li           VP8PutBit(bw, 1, p[9]);
278*b2055c35SXin Li           v -= 3 + (8 << 1);
279*b2055c35SXin Li           mask = 1 << 3;
280*b2055c35SXin Li           tab = VP8Cat4;
281*b2055c35SXin Li         } else if (v < 3 + (8 << 3)) {   // VP8Cat5  (5b)
282*b2055c35SXin Li           VP8PutBit(bw, 1, p[8]);
283*b2055c35SXin Li           VP8PutBit(bw, 0, p[10]);
284*b2055c35SXin Li           v -= 3 + (8 << 2);
285*b2055c35SXin Li           mask = 1 << 4;
286*b2055c35SXin Li           tab = VP8Cat5;
287*b2055c35SXin Li         } else {                         // VP8Cat6 (11b)
288*b2055c35SXin Li           VP8PutBit(bw, 1, p[8]);
289*b2055c35SXin Li           VP8PutBit(bw, 1, p[10]);
290*b2055c35SXin Li           v -= 3 + (8 << 3);
291*b2055c35SXin Li           mask = 1 << 10;
292*b2055c35SXin Li           tab = VP8Cat6;
293*b2055c35SXin Li         }
294*b2055c35SXin Li         while (mask) {
295*b2055c35SXin Li           VP8PutBit(bw, !!(v & mask), *tab++);
296*b2055c35SXin Li           mask >>= 1;
297*b2055c35SXin Li         }
298*b2055c35SXin Li       }
299*b2055c35SXin Li       p = res->prob[VP8EncBands[n]][2];
300*b2055c35SXin Li     }
301*b2055c35SXin Li     VP8PutBitUniform(bw, sign);
302*b2055c35SXin Li     if (n == 16 || !VP8PutBit(bw, n <= res->last, p[0])) {
303*b2055c35SXin Li       return 1;   // EOB
304*b2055c35SXin Li     }
305*b2055c35SXin Li   }
306*b2055c35SXin Li   return 1;
307*b2055c35SXin Li }
308*b2055c35SXin Li 
CodeResiduals(VP8BitWriter * const bw,VP8EncIterator * const it,const VP8ModeScore * const rd)309*b2055c35SXin Li static void CodeResiduals(VP8BitWriter* const bw, VP8EncIterator* const it,
310*b2055c35SXin Li                           const VP8ModeScore* const rd) {
311*b2055c35SXin Li   int x, y, ch;
312*b2055c35SXin Li   VP8Residual res;
313*b2055c35SXin Li   uint64_t pos1, pos2, pos3;
314*b2055c35SXin Li   const int i16 = (it->mb_->type_ == 1);
315*b2055c35SXin Li   const int segment = it->mb_->segment_;
316*b2055c35SXin Li   VP8Encoder* const enc = it->enc_;
317*b2055c35SXin Li 
318*b2055c35SXin Li   VP8IteratorNzToBytes(it);
319*b2055c35SXin Li 
320*b2055c35SXin Li   pos1 = VP8BitWriterPos(bw);
321*b2055c35SXin Li   if (i16) {
322*b2055c35SXin Li     VP8InitResidual(0, 1, enc, &res);
323*b2055c35SXin Li     VP8SetResidualCoeffs(rd->y_dc_levels, &res);
324*b2055c35SXin Li     it->top_nz_[8] = it->left_nz_[8] =
325*b2055c35SXin Li       PutCoeffs(bw, it->top_nz_[8] + it->left_nz_[8], &res);
326*b2055c35SXin Li     VP8InitResidual(1, 0, enc, &res);
327*b2055c35SXin Li   } else {
328*b2055c35SXin Li     VP8InitResidual(0, 3, enc, &res);
329*b2055c35SXin Li   }
330*b2055c35SXin Li 
331*b2055c35SXin Li   // luma-AC
332*b2055c35SXin Li   for (y = 0; y < 4; ++y) {
333*b2055c35SXin Li     for (x = 0; x < 4; ++x) {
334*b2055c35SXin Li       const int ctx = it->top_nz_[x] + it->left_nz_[y];
335*b2055c35SXin Li       VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res);
336*b2055c35SXin Li       it->top_nz_[x] = it->left_nz_[y] = PutCoeffs(bw, ctx, &res);
337*b2055c35SXin Li     }
338*b2055c35SXin Li   }
339*b2055c35SXin Li   pos2 = VP8BitWriterPos(bw);
340*b2055c35SXin Li 
341*b2055c35SXin Li   // U/V
342*b2055c35SXin Li   VP8InitResidual(0, 2, enc, &res);
343*b2055c35SXin Li   for (ch = 0; ch <= 2; ch += 2) {
344*b2055c35SXin Li     for (y = 0; y < 2; ++y) {
345*b2055c35SXin Li       for (x = 0; x < 2; ++x) {
346*b2055c35SXin Li         const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y];
347*b2055c35SXin Li         VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res);
348*b2055c35SXin Li         it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] =
349*b2055c35SXin Li             PutCoeffs(bw, ctx, &res);
350*b2055c35SXin Li       }
351*b2055c35SXin Li     }
352*b2055c35SXin Li   }
353*b2055c35SXin Li   pos3 = VP8BitWriterPos(bw);
354*b2055c35SXin Li   it->luma_bits_ = pos2 - pos1;
355*b2055c35SXin Li   it->uv_bits_ = pos3 - pos2;
356*b2055c35SXin Li   it->bit_count_[segment][i16] += it->luma_bits_;
357*b2055c35SXin Li   it->bit_count_[segment][2] += it->uv_bits_;
358*b2055c35SXin Li   VP8IteratorBytesToNz(it);
359*b2055c35SXin Li }
360*b2055c35SXin Li 
361*b2055c35SXin Li // Same as CodeResiduals, but doesn't actually write anything.
362*b2055c35SXin Li // Instead, it just records the event distribution.
RecordResiduals(VP8EncIterator * const it,const VP8ModeScore * const rd)363*b2055c35SXin Li static void RecordResiduals(VP8EncIterator* const it,
364*b2055c35SXin Li                             const VP8ModeScore* const rd) {
365*b2055c35SXin Li   int x, y, ch;
366*b2055c35SXin Li   VP8Residual res;
367*b2055c35SXin Li   VP8Encoder* const enc = it->enc_;
368*b2055c35SXin Li 
369*b2055c35SXin Li   VP8IteratorNzToBytes(it);
370*b2055c35SXin Li 
371*b2055c35SXin Li   if (it->mb_->type_ == 1) {   // i16x16
372*b2055c35SXin Li     VP8InitResidual(0, 1, enc, &res);
373*b2055c35SXin Li     VP8SetResidualCoeffs(rd->y_dc_levels, &res);
374*b2055c35SXin Li     it->top_nz_[8] = it->left_nz_[8] =
375*b2055c35SXin Li       VP8RecordCoeffs(it->top_nz_[8] + it->left_nz_[8], &res);
376*b2055c35SXin Li     VP8InitResidual(1, 0, enc, &res);
377*b2055c35SXin Li   } else {
378*b2055c35SXin Li     VP8InitResidual(0, 3, enc, &res);
379*b2055c35SXin Li   }
380*b2055c35SXin Li 
381*b2055c35SXin Li   // luma-AC
382*b2055c35SXin Li   for (y = 0; y < 4; ++y) {
383*b2055c35SXin Li     for (x = 0; x < 4; ++x) {
384*b2055c35SXin Li       const int ctx = it->top_nz_[x] + it->left_nz_[y];
385*b2055c35SXin Li       VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res);
386*b2055c35SXin Li       it->top_nz_[x] = it->left_nz_[y] = VP8RecordCoeffs(ctx, &res);
387*b2055c35SXin Li     }
388*b2055c35SXin Li   }
389*b2055c35SXin Li 
390*b2055c35SXin Li   // U/V
391*b2055c35SXin Li   VP8InitResidual(0, 2, enc, &res);
392*b2055c35SXin Li   for (ch = 0; ch <= 2; ch += 2) {
393*b2055c35SXin Li     for (y = 0; y < 2; ++y) {
394*b2055c35SXin Li       for (x = 0; x < 2; ++x) {
395*b2055c35SXin Li         const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y];
396*b2055c35SXin Li         VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res);
397*b2055c35SXin Li         it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] =
398*b2055c35SXin Li             VP8RecordCoeffs(ctx, &res);
399*b2055c35SXin Li       }
400*b2055c35SXin Li     }
401*b2055c35SXin Li   }
402*b2055c35SXin Li 
403*b2055c35SXin Li   VP8IteratorBytesToNz(it);
404*b2055c35SXin Li }
405*b2055c35SXin Li 
406*b2055c35SXin Li //------------------------------------------------------------------------------
407*b2055c35SXin Li // Token buffer
408*b2055c35SXin Li 
409*b2055c35SXin Li #if !defined(DISABLE_TOKEN_BUFFER)
410*b2055c35SXin Li 
RecordTokens(VP8EncIterator * const it,const VP8ModeScore * const rd,VP8TBuffer * const tokens)411*b2055c35SXin Li static int RecordTokens(VP8EncIterator* const it, const VP8ModeScore* const rd,
412*b2055c35SXin Li                         VP8TBuffer* const tokens) {
413*b2055c35SXin Li   int x, y, ch;
414*b2055c35SXin Li   VP8Residual res;
415*b2055c35SXin Li   VP8Encoder* const enc = it->enc_;
416*b2055c35SXin Li 
417*b2055c35SXin Li   VP8IteratorNzToBytes(it);
418*b2055c35SXin Li   if (it->mb_->type_ == 1) {   // i16x16
419*b2055c35SXin Li     const int ctx = it->top_nz_[8] + it->left_nz_[8];
420*b2055c35SXin Li     VP8InitResidual(0, 1, enc, &res);
421*b2055c35SXin Li     VP8SetResidualCoeffs(rd->y_dc_levels, &res);
422*b2055c35SXin Li     it->top_nz_[8] = it->left_nz_[8] =
423*b2055c35SXin Li         VP8RecordCoeffTokens(ctx, &res, tokens);
424*b2055c35SXin Li     VP8InitResidual(1, 0, enc, &res);
425*b2055c35SXin Li   } else {
426*b2055c35SXin Li     VP8InitResidual(0, 3, enc, &res);
427*b2055c35SXin Li   }
428*b2055c35SXin Li 
429*b2055c35SXin Li   // luma-AC
430*b2055c35SXin Li   for (y = 0; y < 4; ++y) {
431*b2055c35SXin Li     for (x = 0; x < 4; ++x) {
432*b2055c35SXin Li       const int ctx = it->top_nz_[x] + it->left_nz_[y];
433*b2055c35SXin Li       VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res);
434*b2055c35SXin Li       it->top_nz_[x] = it->left_nz_[y] =
435*b2055c35SXin Li           VP8RecordCoeffTokens(ctx, &res, tokens);
436*b2055c35SXin Li     }
437*b2055c35SXin Li   }
438*b2055c35SXin Li 
439*b2055c35SXin Li   // U/V
440*b2055c35SXin Li   VP8InitResidual(0, 2, enc, &res);
441*b2055c35SXin Li   for (ch = 0; ch <= 2; ch += 2) {
442*b2055c35SXin Li     for (y = 0; y < 2; ++y) {
443*b2055c35SXin Li       for (x = 0; x < 2; ++x) {
444*b2055c35SXin Li         const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y];
445*b2055c35SXin Li         VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res);
446*b2055c35SXin Li         it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] =
447*b2055c35SXin Li             VP8RecordCoeffTokens(ctx, &res, tokens);
448*b2055c35SXin Li       }
449*b2055c35SXin Li     }
450*b2055c35SXin Li   }
451*b2055c35SXin Li   VP8IteratorBytesToNz(it);
452*b2055c35SXin Li   return !tokens->error_;
453*b2055c35SXin Li }
454*b2055c35SXin Li 
455*b2055c35SXin Li #endif    // !DISABLE_TOKEN_BUFFER
456*b2055c35SXin Li 
457*b2055c35SXin Li //------------------------------------------------------------------------------
458*b2055c35SXin Li // ExtraInfo map / Debug function
459*b2055c35SXin Li 
460*b2055c35SXin Li #if !defined(WEBP_DISABLE_STATS)
461*b2055c35SXin Li 
462*b2055c35SXin Li #if SEGMENT_VISU
SetBlock(uint8_t * p,int value,int size)463*b2055c35SXin Li static void SetBlock(uint8_t* p, int value, int size) {
464*b2055c35SXin Li   int y;
465*b2055c35SXin Li   for (y = 0; y < size; ++y) {
466*b2055c35SXin Li     memset(p, value, size);
467*b2055c35SXin Li     p += BPS;
468*b2055c35SXin Li   }
469*b2055c35SXin Li }
470*b2055c35SXin Li #endif
471*b2055c35SXin Li 
ResetSSE(VP8Encoder * const enc)472*b2055c35SXin Li static void ResetSSE(VP8Encoder* const enc) {
473*b2055c35SXin Li   enc->sse_[0] = 0;
474*b2055c35SXin Li   enc->sse_[1] = 0;
475*b2055c35SXin Li   enc->sse_[2] = 0;
476*b2055c35SXin Li   // Note: enc->sse_[3] is managed by alpha.c
477*b2055c35SXin Li   enc->sse_count_ = 0;
478*b2055c35SXin Li }
479*b2055c35SXin Li 
StoreSSE(const VP8EncIterator * const it)480*b2055c35SXin Li static void StoreSSE(const VP8EncIterator* const it) {
481*b2055c35SXin Li   VP8Encoder* const enc = it->enc_;
482*b2055c35SXin Li   const uint8_t* const in = it->yuv_in_;
483*b2055c35SXin Li   const uint8_t* const out = it->yuv_out_;
484*b2055c35SXin Li   // Note: not totally accurate at boundary. And doesn't include in-loop filter.
485*b2055c35SXin Li   enc->sse_[0] += VP8SSE16x16(in + Y_OFF_ENC, out + Y_OFF_ENC);
486*b2055c35SXin Li   enc->sse_[1] += VP8SSE8x8(in + U_OFF_ENC, out + U_OFF_ENC);
487*b2055c35SXin Li   enc->sse_[2] += VP8SSE8x8(in + V_OFF_ENC, out + V_OFF_ENC);
488*b2055c35SXin Li   enc->sse_count_ += 16 * 16;
489*b2055c35SXin Li }
490*b2055c35SXin Li 
StoreSideInfo(const VP8EncIterator * const it)491*b2055c35SXin Li static void StoreSideInfo(const VP8EncIterator* const it) {
492*b2055c35SXin Li   VP8Encoder* const enc = it->enc_;
493*b2055c35SXin Li   const VP8MBInfo* const mb = it->mb_;
494*b2055c35SXin Li   WebPPicture* const pic = enc->pic_;
495*b2055c35SXin Li 
496*b2055c35SXin Li   if (pic->stats != NULL) {
497*b2055c35SXin Li     StoreSSE(it);
498*b2055c35SXin Li     enc->block_count_[0] += (mb->type_ == 0);
499*b2055c35SXin Li     enc->block_count_[1] += (mb->type_ == 1);
500*b2055c35SXin Li     enc->block_count_[2] += (mb->skip_ != 0);
501*b2055c35SXin Li   }
502*b2055c35SXin Li 
503*b2055c35SXin Li   if (pic->extra_info != NULL) {
504*b2055c35SXin Li     uint8_t* const info = &pic->extra_info[it->x_ + it->y_ * enc->mb_w_];
505*b2055c35SXin Li     switch (pic->extra_info_type) {
506*b2055c35SXin Li       case 1: *info = mb->type_; break;
507*b2055c35SXin Li       case 2: *info = mb->segment_; break;
508*b2055c35SXin Li       case 3: *info = enc->dqm_[mb->segment_].quant_; break;
509*b2055c35SXin Li       case 4: *info = (mb->type_ == 1) ? it->preds_[0] : 0xff; break;
510*b2055c35SXin Li       case 5: *info = mb->uv_mode_; break;
511*b2055c35SXin Li       case 6: {
512*b2055c35SXin Li         const int b = (int)((it->luma_bits_ + it->uv_bits_ + 7) >> 3);
513*b2055c35SXin Li         *info = (b > 255) ? 255 : b; break;
514*b2055c35SXin Li       }
515*b2055c35SXin Li       case 7: *info = mb->alpha_; break;
516*b2055c35SXin Li       default: *info = 0; break;
517*b2055c35SXin Li     }
518*b2055c35SXin Li   }
519*b2055c35SXin Li #if SEGMENT_VISU  // visualize segments and prediction modes
520*b2055c35SXin Li   SetBlock(it->yuv_out_ + Y_OFF_ENC, mb->segment_ * 64, 16);
521*b2055c35SXin Li   SetBlock(it->yuv_out_ + U_OFF_ENC, it->preds_[0] * 64, 8);
522*b2055c35SXin Li   SetBlock(it->yuv_out_ + V_OFF_ENC, mb->uv_mode_ * 64, 8);
523*b2055c35SXin Li #endif
524*b2055c35SXin Li }
525*b2055c35SXin Li 
ResetSideInfo(const VP8EncIterator * const it)526*b2055c35SXin Li static void ResetSideInfo(const VP8EncIterator* const it) {
527*b2055c35SXin Li   VP8Encoder* const enc = it->enc_;
528*b2055c35SXin Li   WebPPicture* const pic = enc->pic_;
529*b2055c35SXin Li   if (pic->stats != NULL) {
530*b2055c35SXin Li     memset(enc->block_count_, 0, sizeof(enc->block_count_));
531*b2055c35SXin Li   }
532*b2055c35SXin Li   ResetSSE(enc);
533*b2055c35SXin Li }
534*b2055c35SXin Li #else  // defined(WEBP_DISABLE_STATS)
ResetSSE(VP8Encoder * const enc)535*b2055c35SXin Li static void ResetSSE(VP8Encoder* const enc) {
536*b2055c35SXin Li   (void)enc;
537*b2055c35SXin Li }
StoreSideInfo(const VP8EncIterator * const it)538*b2055c35SXin Li static void StoreSideInfo(const VP8EncIterator* const it) {
539*b2055c35SXin Li   VP8Encoder* const enc = it->enc_;
540*b2055c35SXin Li   WebPPicture* const pic = enc->pic_;
541*b2055c35SXin Li   if (pic->extra_info != NULL) {
542*b2055c35SXin Li     if (it->x_ == 0 && it->y_ == 0) {   // only do it once, at start
543*b2055c35SXin Li       memset(pic->extra_info, 0,
544*b2055c35SXin Li              enc->mb_w_ * enc->mb_h_ * sizeof(*pic->extra_info));
545*b2055c35SXin Li     }
546*b2055c35SXin Li   }
547*b2055c35SXin Li }
548*b2055c35SXin Li 
ResetSideInfo(const VP8EncIterator * const it)549*b2055c35SXin Li static void ResetSideInfo(const VP8EncIterator* const it) {
550*b2055c35SXin Li   (void)it;
551*b2055c35SXin Li }
552*b2055c35SXin Li #endif  // !defined(WEBP_DISABLE_STATS)
553*b2055c35SXin Li 
GetPSNR(uint64_t mse,uint64_t size)554*b2055c35SXin Li static double GetPSNR(uint64_t mse, uint64_t size) {
555*b2055c35SXin Li   return (mse > 0 && size > 0) ? 10. * log10(255. * 255. * size / mse) : 99;
556*b2055c35SXin Li }
557*b2055c35SXin Li 
558*b2055c35SXin Li //------------------------------------------------------------------------------
559*b2055c35SXin Li //  StatLoop(): only collect statistics (number of skips, token usage, ...).
560*b2055c35SXin Li //  This is used for deciding optimal probabilities. It also modifies the
561*b2055c35SXin Li //  quantizer value if some target (size, PSNR) was specified.
562*b2055c35SXin Li 
SetLoopParams(VP8Encoder * const enc,float q)563*b2055c35SXin Li static void SetLoopParams(VP8Encoder* const enc, float q) {
564*b2055c35SXin Li   // Make sure the quality parameter is inside valid bounds
565*b2055c35SXin Li   q = Clamp(q, 0.f, 100.f);
566*b2055c35SXin Li 
567*b2055c35SXin Li   VP8SetSegmentParams(enc, q);      // setup segment quantizations and filters
568*b2055c35SXin Li   SetSegmentProbas(enc);            // compute segment probabilities
569*b2055c35SXin Li 
570*b2055c35SXin Li   ResetStats(enc);
571*b2055c35SXin Li   ResetSSE(enc);
572*b2055c35SXin Li }
573*b2055c35SXin Li 
OneStatPass(VP8Encoder * const enc,VP8RDLevel rd_opt,int nb_mbs,int percent_delta,PassStats * const s)574*b2055c35SXin Li static uint64_t OneStatPass(VP8Encoder* const enc, VP8RDLevel rd_opt,
575*b2055c35SXin Li                             int nb_mbs, int percent_delta,
576*b2055c35SXin Li                             PassStats* const s) {
577*b2055c35SXin Li   VP8EncIterator it;
578*b2055c35SXin Li   uint64_t size = 0;
579*b2055c35SXin Li   uint64_t size_p0 = 0;
580*b2055c35SXin Li   uint64_t distortion = 0;
581*b2055c35SXin Li   const uint64_t pixel_count = (uint64_t)nb_mbs * 384;
582*b2055c35SXin Li 
583*b2055c35SXin Li   VP8IteratorInit(enc, &it);
584*b2055c35SXin Li   SetLoopParams(enc, s->q);
585*b2055c35SXin Li   do {
586*b2055c35SXin Li     VP8ModeScore info;
587*b2055c35SXin Li     VP8IteratorImport(&it, NULL);
588*b2055c35SXin Li     if (VP8Decimate(&it, &info, rd_opt)) {
589*b2055c35SXin Li       // Just record the number of skips and act like skip_proba is not used.
590*b2055c35SXin Li       ++enc->proba_.nb_skip_;
591*b2055c35SXin Li     }
592*b2055c35SXin Li     RecordResiduals(&it, &info);
593*b2055c35SXin Li     size += info.R + info.H;
594*b2055c35SXin Li     size_p0 += info.H;
595*b2055c35SXin Li     distortion += info.D;
596*b2055c35SXin Li     if (percent_delta && !VP8IteratorProgress(&it, percent_delta)) {
597*b2055c35SXin Li       return 0;
598*b2055c35SXin Li     }
599*b2055c35SXin Li     VP8IteratorSaveBoundary(&it);
600*b2055c35SXin Li   } while (VP8IteratorNext(&it) && --nb_mbs > 0);
601*b2055c35SXin Li 
602*b2055c35SXin Li   size_p0 += enc->segment_hdr_.size_;
603*b2055c35SXin Li   if (s->do_size_search) {
604*b2055c35SXin Li     size += FinalizeSkipProba(enc);
605*b2055c35SXin Li     size += FinalizeTokenProbas(&enc->proba_);
606*b2055c35SXin Li     size = ((size + size_p0 + 1024) >> 11) + HEADER_SIZE_ESTIMATE;
607*b2055c35SXin Li     s->value = (double)size;
608*b2055c35SXin Li   } else {
609*b2055c35SXin Li     s->value = GetPSNR(distortion, pixel_count);
610*b2055c35SXin Li   }
611*b2055c35SXin Li   return size_p0;
612*b2055c35SXin Li }
613*b2055c35SXin Li 
StatLoop(VP8Encoder * const enc)614*b2055c35SXin Li static int StatLoop(VP8Encoder* const enc) {
615*b2055c35SXin Li   const int method = enc->method_;
616*b2055c35SXin Li   const int do_search = enc->do_search_;
617*b2055c35SXin Li   const int fast_probe = ((method == 0 || method == 3) && !do_search);
618*b2055c35SXin Li   int num_pass_left = enc->config_->pass;
619*b2055c35SXin Li   const int task_percent = 20;
620*b2055c35SXin Li   const int percent_per_pass =
621*b2055c35SXin Li       (task_percent + num_pass_left / 2) / num_pass_left;
622*b2055c35SXin Li   const int final_percent = enc->percent_ + task_percent;
623*b2055c35SXin Li   const VP8RDLevel rd_opt =
624*b2055c35SXin Li       (method >= 3 || do_search) ? RD_OPT_BASIC : RD_OPT_NONE;
625*b2055c35SXin Li   int nb_mbs = enc->mb_w_ * enc->mb_h_;
626*b2055c35SXin Li   PassStats stats;
627*b2055c35SXin Li 
628*b2055c35SXin Li   InitPassStats(enc, &stats);
629*b2055c35SXin Li   ResetTokenStats(enc);
630*b2055c35SXin Li 
631*b2055c35SXin Li   // Fast mode: quick analysis pass over few mbs. Better than nothing.
632*b2055c35SXin Li   if (fast_probe) {
633*b2055c35SXin Li     if (method == 3) {  // we need more stats for method 3 to be reliable.
634*b2055c35SXin Li       nb_mbs = (nb_mbs > 200) ? nb_mbs >> 1 : 100;
635*b2055c35SXin Li     } else {
636*b2055c35SXin Li       nb_mbs = (nb_mbs > 200) ? nb_mbs >> 2 : 50;
637*b2055c35SXin Li     }
638*b2055c35SXin Li   }
639*b2055c35SXin Li 
640*b2055c35SXin Li   while (num_pass_left-- > 0) {
641*b2055c35SXin Li     const int is_last_pass = (fabs(stats.dq) <= DQ_LIMIT) ||
642*b2055c35SXin Li                              (num_pass_left == 0) ||
643*b2055c35SXin Li                              (enc->max_i4_header_bits_ == 0);
644*b2055c35SXin Li     const uint64_t size_p0 =
645*b2055c35SXin Li         OneStatPass(enc, rd_opt, nb_mbs, percent_per_pass, &stats);
646*b2055c35SXin Li     if (size_p0 == 0) return 0;
647*b2055c35SXin Li #if (DEBUG_SEARCH > 0)
648*b2055c35SXin Li     printf("#%d value:%.1lf -> %.1lf   q:%.2f -> %.2f\n",
649*b2055c35SXin Li            num_pass_left, stats.last_value, stats.value, stats.last_q, stats.q);
650*b2055c35SXin Li #endif
651*b2055c35SXin Li     if (enc->max_i4_header_bits_ > 0 && size_p0 > PARTITION0_SIZE_LIMIT) {
652*b2055c35SXin Li       ++num_pass_left;
653*b2055c35SXin Li       enc->max_i4_header_bits_ >>= 1;  // strengthen header bit limitation...
654*b2055c35SXin Li       continue;                        // ...and start over
655*b2055c35SXin Li     }
656*b2055c35SXin Li     if (is_last_pass) {
657*b2055c35SXin Li       break;
658*b2055c35SXin Li     }
659*b2055c35SXin Li     // If no target size: just do several pass without changing 'q'
660*b2055c35SXin Li     if (do_search) {
661*b2055c35SXin Li       ComputeNextQ(&stats);
662*b2055c35SXin Li       if (fabs(stats.dq) <= DQ_LIMIT) break;
663*b2055c35SXin Li     }
664*b2055c35SXin Li   }
665*b2055c35SXin Li   if (!do_search || !stats.do_size_search) {
666*b2055c35SXin Li     // Need to finalize probas now, since it wasn't done during the search.
667*b2055c35SXin Li     FinalizeSkipProba(enc);
668*b2055c35SXin Li     FinalizeTokenProbas(&enc->proba_);
669*b2055c35SXin Li   }
670*b2055c35SXin Li   VP8CalculateLevelCosts(&enc->proba_);  // finalize costs
671*b2055c35SXin Li   return WebPReportProgress(enc->pic_, final_percent, &enc->percent_);
672*b2055c35SXin Li }
673*b2055c35SXin Li 
674*b2055c35SXin Li //------------------------------------------------------------------------------
675*b2055c35SXin Li // Main loops
676*b2055c35SXin Li //
677*b2055c35SXin Li 
678*b2055c35SXin Li static const uint8_t kAverageBytesPerMB[8] = { 50, 24, 16, 9, 7, 5, 3, 2 };
679*b2055c35SXin Li 
PreLoopInitialize(VP8Encoder * const enc)680*b2055c35SXin Li static int PreLoopInitialize(VP8Encoder* const enc) {
681*b2055c35SXin Li   int p;
682*b2055c35SXin Li   int ok = 1;
683*b2055c35SXin Li   const int average_bytes_per_MB = kAverageBytesPerMB[enc->base_quant_ >> 4];
684*b2055c35SXin Li   const int bytes_per_parts =
685*b2055c35SXin Li       enc->mb_w_ * enc->mb_h_ * average_bytes_per_MB / enc->num_parts_;
686*b2055c35SXin Li   // Initialize the bit-writers
687*b2055c35SXin Li   for (p = 0; ok && p < enc->num_parts_; ++p) {
688*b2055c35SXin Li     ok = VP8BitWriterInit(enc->parts_ + p, bytes_per_parts);
689*b2055c35SXin Li   }
690*b2055c35SXin Li   if (!ok) {
691*b2055c35SXin Li     VP8EncFreeBitWriters(enc);  // malloc error occurred
692*b2055c35SXin Li     return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY);
693*b2055c35SXin Li   }
694*b2055c35SXin Li   return ok;
695*b2055c35SXin Li }
696*b2055c35SXin Li 
PostLoopFinalize(VP8EncIterator * const it,int ok)697*b2055c35SXin Li static int PostLoopFinalize(VP8EncIterator* const it, int ok) {
698*b2055c35SXin Li   VP8Encoder* const enc = it->enc_;
699*b2055c35SXin Li   if (ok) {      // Finalize the partitions, check for extra errors.
700*b2055c35SXin Li     int p;
701*b2055c35SXin Li     for (p = 0; p < enc->num_parts_; ++p) {
702*b2055c35SXin Li       VP8BitWriterFinish(enc->parts_ + p);
703*b2055c35SXin Li       ok &= !enc->parts_[p].error_;
704*b2055c35SXin Li     }
705*b2055c35SXin Li   }
706*b2055c35SXin Li 
707*b2055c35SXin Li   if (ok) {      // All good. Finish up.
708*b2055c35SXin Li #if !defined(WEBP_DISABLE_STATS)
709*b2055c35SXin Li     if (enc->pic_->stats != NULL) {  // finalize byte counters...
710*b2055c35SXin Li       int i, s;
711*b2055c35SXin Li       for (i = 0; i <= 2; ++i) {
712*b2055c35SXin Li         for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
713*b2055c35SXin Li           enc->residual_bytes_[i][s] = (int)((it->bit_count_[s][i] + 7) >> 3);
714*b2055c35SXin Li         }
715*b2055c35SXin Li       }
716*b2055c35SXin Li     }
717*b2055c35SXin Li #endif
718*b2055c35SXin Li     VP8AdjustFilterStrength(it);     // ...and store filter stats.
719*b2055c35SXin Li   } else {
720*b2055c35SXin Li     // Something bad happened -> need to do some memory cleanup.
721*b2055c35SXin Li     VP8EncFreeBitWriters(enc);
722*b2055c35SXin Li     return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY);
723*b2055c35SXin Li   }
724*b2055c35SXin Li   return ok;
725*b2055c35SXin Li }
726*b2055c35SXin Li 
727*b2055c35SXin Li //------------------------------------------------------------------------------
728*b2055c35SXin Li //  VP8EncLoop(): does the final bitstream coding.
729*b2055c35SXin Li 
ResetAfterSkip(VP8EncIterator * const it)730*b2055c35SXin Li static void ResetAfterSkip(VP8EncIterator* const it) {
731*b2055c35SXin Li   if (it->mb_->type_ == 1) {
732*b2055c35SXin Li     *it->nz_ = 0;  // reset all predictors
733*b2055c35SXin Li     it->left_nz_[8] = 0;
734*b2055c35SXin Li   } else {
735*b2055c35SXin Li     *it->nz_ &= (1 << 24);  // preserve the dc_nz bit
736*b2055c35SXin Li   }
737*b2055c35SXin Li }
738*b2055c35SXin Li 
VP8EncLoop(VP8Encoder * const enc)739*b2055c35SXin Li int VP8EncLoop(VP8Encoder* const enc) {
740*b2055c35SXin Li   VP8EncIterator it;
741*b2055c35SXin Li   int ok = PreLoopInitialize(enc);
742*b2055c35SXin Li   if (!ok) return 0;
743*b2055c35SXin Li 
744*b2055c35SXin Li   StatLoop(enc);  // stats-collection loop
745*b2055c35SXin Li 
746*b2055c35SXin Li   VP8IteratorInit(enc, &it);
747*b2055c35SXin Li   VP8InitFilter(&it);
748*b2055c35SXin Li   do {
749*b2055c35SXin Li     VP8ModeScore info;
750*b2055c35SXin Li     const int dont_use_skip = !enc->proba_.use_skip_proba_;
751*b2055c35SXin Li     const VP8RDLevel rd_opt = enc->rd_opt_level_;
752*b2055c35SXin Li 
753*b2055c35SXin Li     VP8IteratorImport(&it, NULL);
754*b2055c35SXin Li     // Warning! order is important: first call VP8Decimate() and
755*b2055c35SXin Li     // *then* decide how to code the skip decision if there's one.
756*b2055c35SXin Li     if (!VP8Decimate(&it, &info, rd_opt) || dont_use_skip) {
757*b2055c35SXin Li       CodeResiduals(it.bw_, &it, &info);
758*b2055c35SXin Li       if (it.bw_->error_) {
759*b2055c35SXin Li         // enc->pic_->error_code is set in PostLoopFinalize().
760*b2055c35SXin Li         ok = 0;
761*b2055c35SXin Li         break;
762*b2055c35SXin Li       }
763*b2055c35SXin Li     } else {   // reset predictors after a skip
764*b2055c35SXin Li       ResetAfterSkip(&it);
765*b2055c35SXin Li     }
766*b2055c35SXin Li     StoreSideInfo(&it);
767*b2055c35SXin Li     VP8StoreFilterStats(&it);
768*b2055c35SXin Li     VP8IteratorExport(&it);
769*b2055c35SXin Li     ok = VP8IteratorProgress(&it, 20);
770*b2055c35SXin Li     VP8IteratorSaveBoundary(&it);
771*b2055c35SXin Li   } while (ok && VP8IteratorNext(&it));
772*b2055c35SXin Li 
773*b2055c35SXin Li   return PostLoopFinalize(&it, ok);
774*b2055c35SXin Li }
775*b2055c35SXin Li 
776*b2055c35SXin Li //------------------------------------------------------------------------------
777*b2055c35SXin Li // Single pass using Token Buffer.
778*b2055c35SXin Li 
779*b2055c35SXin Li #if !defined(DISABLE_TOKEN_BUFFER)
780*b2055c35SXin Li 
781*b2055c35SXin Li #define MIN_COUNT 96  // minimum number of macroblocks before updating stats
782*b2055c35SXin Li 
VP8EncTokenLoop(VP8Encoder * const enc)783*b2055c35SXin Li int VP8EncTokenLoop(VP8Encoder* const enc) {
784*b2055c35SXin Li   // Roughly refresh the proba eight times per pass
785*b2055c35SXin Li   int max_count = (enc->mb_w_ * enc->mb_h_) >> 3;
786*b2055c35SXin Li   int num_pass_left = enc->config_->pass;
787*b2055c35SXin Li   int remaining_progress = 40;  // percents
788*b2055c35SXin Li   const int do_search = enc->do_search_;
789*b2055c35SXin Li   VP8EncIterator it;
790*b2055c35SXin Li   VP8EncProba* const proba = &enc->proba_;
791*b2055c35SXin Li   const VP8RDLevel rd_opt = enc->rd_opt_level_;
792*b2055c35SXin Li   const uint64_t pixel_count = (uint64_t)enc->mb_w_ * enc->mb_h_ * 384;
793*b2055c35SXin Li   PassStats stats;
794*b2055c35SXin Li   int ok;
795*b2055c35SXin Li 
796*b2055c35SXin Li   InitPassStats(enc, &stats);
797*b2055c35SXin Li   ok = PreLoopInitialize(enc);
798*b2055c35SXin Li   if (!ok) return 0;
799*b2055c35SXin Li 
800*b2055c35SXin Li   if (max_count < MIN_COUNT) max_count = MIN_COUNT;
801*b2055c35SXin Li 
802*b2055c35SXin Li   assert(enc->num_parts_ == 1);
803*b2055c35SXin Li   assert(enc->use_tokens_);
804*b2055c35SXin Li   assert(proba->use_skip_proba_ == 0);
805*b2055c35SXin Li   assert(rd_opt >= RD_OPT_BASIC);   // otherwise, token-buffer won't be useful
806*b2055c35SXin Li   assert(num_pass_left > 0);
807*b2055c35SXin Li 
808*b2055c35SXin Li   while (ok && num_pass_left-- > 0) {
809*b2055c35SXin Li     const int is_last_pass = (fabs(stats.dq) <= DQ_LIMIT) ||
810*b2055c35SXin Li                              (num_pass_left == 0) ||
811*b2055c35SXin Li                              (enc->max_i4_header_bits_ == 0);
812*b2055c35SXin Li     uint64_t size_p0 = 0;
813*b2055c35SXin Li     uint64_t distortion = 0;
814*b2055c35SXin Li     int cnt = max_count;
815*b2055c35SXin Li     // The final number of passes is not trivial to know in advance.
816*b2055c35SXin Li     const int pass_progress = remaining_progress / (2 + num_pass_left);
817*b2055c35SXin Li     remaining_progress -= pass_progress;
818*b2055c35SXin Li     VP8IteratorInit(enc, &it);
819*b2055c35SXin Li     SetLoopParams(enc, stats.q);
820*b2055c35SXin Li     if (is_last_pass) {
821*b2055c35SXin Li       ResetTokenStats(enc);
822*b2055c35SXin Li       VP8InitFilter(&it);  // don't collect stats until last pass (too costly)
823*b2055c35SXin Li     }
824*b2055c35SXin Li     VP8TBufferClear(&enc->tokens_);
825*b2055c35SXin Li     do {
826*b2055c35SXin Li       VP8ModeScore info;
827*b2055c35SXin Li       VP8IteratorImport(&it, NULL);
828*b2055c35SXin Li       if (--cnt < 0) {
829*b2055c35SXin Li         FinalizeTokenProbas(proba);
830*b2055c35SXin Li         VP8CalculateLevelCosts(proba);  // refresh cost tables for rd-opt
831*b2055c35SXin Li         cnt = max_count;
832*b2055c35SXin Li       }
833*b2055c35SXin Li       VP8Decimate(&it, &info, rd_opt);
834*b2055c35SXin Li       ok = RecordTokens(&it, &info, &enc->tokens_);
835*b2055c35SXin Li       if (!ok) {
836*b2055c35SXin Li         WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY);
837*b2055c35SXin Li         break;
838*b2055c35SXin Li       }
839*b2055c35SXin Li       size_p0 += info.H;
840*b2055c35SXin Li       distortion += info.D;
841*b2055c35SXin Li       if (is_last_pass) {
842*b2055c35SXin Li         StoreSideInfo(&it);
843*b2055c35SXin Li         VP8StoreFilterStats(&it);
844*b2055c35SXin Li         VP8IteratorExport(&it);
845*b2055c35SXin Li         ok = VP8IteratorProgress(&it, pass_progress);
846*b2055c35SXin Li       }
847*b2055c35SXin Li       VP8IteratorSaveBoundary(&it);
848*b2055c35SXin Li     } while (ok && VP8IteratorNext(&it));
849*b2055c35SXin Li     if (!ok) break;
850*b2055c35SXin Li 
851*b2055c35SXin Li     size_p0 += enc->segment_hdr_.size_;
852*b2055c35SXin Li     if (stats.do_size_search) {
853*b2055c35SXin Li       uint64_t size = FinalizeTokenProbas(&enc->proba_);
854*b2055c35SXin Li       size += VP8EstimateTokenSize(&enc->tokens_,
855*b2055c35SXin Li                                    (const uint8_t*)proba->coeffs_);
856*b2055c35SXin Li       size = (size + size_p0 + 1024) >> 11;  // -> size in bytes
857*b2055c35SXin Li       size += HEADER_SIZE_ESTIMATE;
858*b2055c35SXin Li       stats.value = (double)size;
859*b2055c35SXin Li     } else {  // compute and store PSNR
860*b2055c35SXin Li       stats.value = GetPSNR(distortion, pixel_count);
861*b2055c35SXin Li     }
862*b2055c35SXin Li 
863*b2055c35SXin Li #if (DEBUG_SEARCH > 0)
864*b2055c35SXin Li     printf("#%2d metric:%.1lf -> %.1lf   last_q=%.2lf q=%.2lf dq=%.2lf "
865*b2055c35SXin Li            " range:[%.1f, %.1f]\n",
866*b2055c35SXin Li            num_pass_left, stats.last_value, stats.value,
867*b2055c35SXin Li            stats.last_q, stats.q, stats.dq, stats.qmin, stats.qmax);
868*b2055c35SXin Li #endif
869*b2055c35SXin Li     if (enc->max_i4_header_bits_ > 0 && size_p0 > PARTITION0_SIZE_LIMIT) {
870*b2055c35SXin Li       ++num_pass_left;
871*b2055c35SXin Li       enc->max_i4_header_bits_ >>= 1;  // strengthen header bit limitation...
872*b2055c35SXin Li       if (is_last_pass) {
873*b2055c35SXin Li         ResetSideInfo(&it);
874*b2055c35SXin Li       }
875*b2055c35SXin Li       continue;                        // ...and start over
876*b2055c35SXin Li     }
877*b2055c35SXin Li     if (is_last_pass) {
878*b2055c35SXin Li       break;   // done
879*b2055c35SXin Li     }
880*b2055c35SXin Li     if (do_search) {
881*b2055c35SXin Li       ComputeNextQ(&stats);  // Adjust q
882*b2055c35SXin Li     }
883*b2055c35SXin Li   }
884*b2055c35SXin Li   if (ok) {
885*b2055c35SXin Li     if (!stats.do_size_search) {
886*b2055c35SXin Li       FinalizeTokenProbas(&enc->proba_);
887*b2055c35SXin Li     }
888*b2055c35SXin Li     ok = VP8EmitTokens(&enc->tokens_, enc->parts_ + 0,
889*b2055c35SXin Li                        (const uint8_t*)proba->coeffs_, 1);
890*b2055c35SXin Li   }
891*b2055c35SXin Li   ok = ok && WebPReportProgress(enc->pic_, enc->percent_ + remaining_progress,
892*b2055c35SXin Li                                 &enc->percent_);
893*b2055c35SXin Li   return PostLoopFinalize(&it, ok);
894*b2055c35SXin Li }
895*b2055c35SXin Li 
896*b2055c35SXin Li #else
897*b2055c35SXin Li 
VP8EncTokenLoop(VP8Encoder * const enc)898*b2055c35SXin Li int VP8EncTokenLoop(VP8Encoder* const enc) {
899*b2055c35SXin Li   (void)enc;
900*b2055c35SXin Li   return 0;   // we shouldn't be here.
901*b2055c35SXin Li }
902*b2055c35SXin Li 
903*b2055c35SXin Li #endif    // DISABLE_TOKEN_BUFFER
904*b2055c35SXin Li 
905*b2055c35SXin Li //------------------------------------------------------------------------------
906