Coverage Report

Created: 2024-07-27 06:27

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