Coverage Report

Created: 2024-07-27 06:28

/src/libwebp/src/enc/analysis_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
// Macroblock analysis
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <stdlib.h>
15
#include <string.h>
16
#include <assert.h>
17
18
#include "src/enc/vp8i_enc.h"
19
#include "src/enc/cost_enc.h"
20
#include "src/utils/utils.h"
21
22
0
#define MAX_ITERS_K_MEANS  6
23
24
//------------------------------------------------------------------------------
25
// Smooth the segment map by replacing isolated block by the majority of its
26
// neighbours.
27
28
0
static void SmoothSegmentMap(VP8Encoder* const enc) {
29
0
  int n, x, y;
30
0
  const int w = enc->mb_w_;
31
0
  const int h = enc->mb_h_;
32
0
  const int majority_cnt_3_x_3_grid = 5;
33
0
  uint8_t* const tmp = (uint8_t*)WebPSafeMalloc(w * h, sizeof(*tmp));
34
0
  assert((uint64_t)(w * h) == (uint64_t)w * h);   // no overflow, as per spec
35
36
0
  if (tmp == NULL) return;
37
0
  for (y = 1; y < h - 1; ++y) {
38
0
    for (x = 1; x < w - 1; ++x) {
39
0
      int cnt[NUM_MB_SEGMENTS] = { 0 };
40
0
      const VP8MBInfo* const mb = &enc->mb_info_[x + w * y];
41
0
      int majority_seg = mb->segment_;
42
      // Check the 8 neighbouring segment values.
43
0
      cnt[mb[-w - 1].segment_]++;  // top-left
44
0
      cnt[mb[-w + 0].segment_]++;  // top
45
0
      cnt[mb[-w + 1].segment_]++;  // top-right
46
0
      cnt[mb[   - 1].segment_]++;  // left
47
0
      cnt[mb[   + 1].segment_]++;  // right
48
0
      cnt[mb[ w - 1].segment_]++;  // bottom-left
49
0
      cnt[mb[ w + 0].segment_]++;  // bottom
50
0
      cnt[mb[ w + 1].segment_]++;  // bottom-right
51
0
      for (n = 0; n < NUM_MB_SEGMENTS; ++n) {
52
0
        if (cnt[n] >= majority_cnt_3_x_3_grid) {
53
0
          majority_seg = n;
54
0
          break;
55
0
        }
56
0
      }
57
0
      tmp[x + y * w] = majority_seg;
58
0
    }
59
0
  }
60
0
  for (y = 1; y < h - 1; ++y) {
61
0
    for (x = 1; x < w - 1; ++x) {
62
0
      VP8MBInfo* const mb = &enc->mb_info_[x + w * y];
63
0
      mb->segment_ = tmp[x + y * w];
64
0
    }
65
0
  }
66
0
  WebPSafeFree(tmp);
67
0
}
68
69
//------------------------------------------------------------------------------
70
// set segment susceptibility alpha_ / beta_
71
72
0
static WEBP_INLINE int clip(int v, int m, int M) {
73
0
  return (v < m) ? m : (v > M) ? M : v;
74
0
}
75
76
static void SetSegmentAlphas(VP8Encoder* const enc,
77
                             const int centers[NUM_MB_SEGMENTS],
78
0
                             int mid) {
79
0
  const int nb = enc->segment_hdr_.num_segments_;
80
0
  int min = centers[0], max = centers[0];
81
0
  int n;
82
83
0
  if (nb > 1) {
84
0
    for (n = 0; n < nb; ++n) {
85
0
      if (min > centers[n]) min = centers[n];
86
0
      if (max < centers[n]) max = centers[n];
87
0
    }
88
0
  }
89
0
  if (max == min) max = min + 1;
90
0
  assert(mid <= max && mid >= min);
91
0
  for (n = 0; n < nb; ++n) {
92
0
    const int alpha = 255 * (centers[n] - mid) / (max - min);
93
0
    const int beta = 255 * (centers[n] - min) / (max - min);
94
0
    enc->dqm_[n].alpha_ = clip(alpha, -127, 127);
95
0
    enc->dqm_[n].beta_ = clip(beta, 0, 255);
96
0
  }
97
0
}
98
99
//------------------------------------------------------------------------------
100
// Compute susceptibility based on DCT-coeff histograms:
101
// the higher, the "easier" the macroblock is to compress.
102
103
0
#define MAX_ALPHA 255                // 8b of precision for susceptibilities.
104
0
#define ALPHA_SCALE (2 * MAX_ALPHA)  // scaling factor for alpha.
105
0
#define DEFAULT_ALPHA (-1)
106
0
#define IS_BETTER_ALPHA(alpha, best_alpha) ((alpha) > (best_alpha))
107
108
0
static int FinalAlphaValue(int alpha) {
109
0
  alpha = MAX_ALPHA - alpha;
110
0
  return clip(alpha, 0, MAX_ALPHA);
111
0
}
112
113
0
static int GetAlpha(const VP8Histogram* const histo) {
114
  // 'alpha' will later be clipped to [0..MAX_ALPHA] range, clamping outer
115
  // values which happen to be mostly noise. This leaves the maximum precision
116
  // for handling the useful small values which contribute most.
117
0
  const int max_value = histo->max_value;
118
0
  const int last_non_zero = histo->last_non_zero;
119
0
  const int alpha =
120
0
      (max_value > 1) ? ALPHA_SCALE * last_non_zero / max_value : 0;
121
0
  return alpha;
122
0
}
123
124
0
static void InitHistogram(VP8Histogram* const histo) {
125
0
  histo->max_value = 0;
126
0
  histo->last_non_zero = 1;
127
0
}
128
129
//------------------------------------------------------------------------------
130
// Simplified k-Means, to assign Nb segments based on alpha-histogram
131
132
static void AssignSegments(VP8Encoder* const enc,
133
0
                           const int alphas[MAX_ALPHA + 1]) {
134
  // 'num_segments_' is previously validated and <= NUM_MB_SEGMENTS, but an
135
  // explicit check is needed to avoid spurious warning about 'n + 1' exceeding
136
  // array bounds of 'centers' with some compilers (noticed with gcc-4.9).
137
0
  const int nb = (enc->segment_hdr_.num_segments_ < NUM_MB_SEGMENTS) ?
138
0
                 enc->segment_hdr_.num_segments_ : NUM_MB_SEGMENTS;
139
0
  int centers[NUM_MB_SEGMENTS];
140
0
  int weighted_average = 0;
141
0
  int map[MAX_ALPHA + 1];
142
0
  int a, n, k;
143
0
  int min_a = 0, max_a = MAX_ALPHA, range_a;
144
  // 'int' type is ok for histo, and won't overflow
145
0
  int accum[NUM_MB_SEGMENTS], dist_accum[NUM_MB_SEGMENTS];
146
147
0
  assert(nb >= 1);
148
0
  assert(nb <= NUM_MB_SEGMENTS);
149
150
  // bracket the input
151
0
  for (n = 0; n <= MAX_ALPHA && alphas[n] == 0; ++n) {}
152
0
  min_a = n;
153
0
  for (n = MAX_ALPHA; n > min_a && alphas[n] == 0; --n) {}
154
0
  max_a = n;
155
0
  range_a = max_a - min_a;
156
157
  // Spread initial centers evenly
158
0
  for (k = 0, n = 1; k < nb; ++k, n += 2) {
159
0
    assert(n < 2 * nb);
160
0
    centers[k] = min_a + (n * range_a) / (2 * nb);
161
0
  }
162
163
0
  for (k = 0; k < MAX_ITERS_K_MEANS; ++k) {     // few iters are enough
164
0
    int total_weight;
165
0
    int displaced;
166
    // Reset stats
167
0
    for (n = 0; n < nb; ++n) {
168
0
      accum[n] = 0;
169
0
      dist_accum[n] = 0;
170
0
    }
171
    // Assign nearest center for each 'a'
172
0
    n = 0;    // track the nearest center for current 'a'
173
0
    for (a = min_a; a <= max_a; ++a) {
174
0
      if (alphas[a]) {
175
0
        while (n + 1 < nb && abs(a - centers[n + 1]) < abs(a - centers[n])) {
176
0
          n++;
177
0
        }
178
0
        map[a] = n;
179
        // accumulate contribution into best centroid
180
0
        dist_accum[n] += a * alphas[a];
181
0
        accum[n] += alphas[a];
182
0
      }
183
0
    }
184
    // All point are classified. Move the centroids to the
185
    // center of their respective cloud.
186
0
    displaced = 0;
187
0
    weighted_average = 0;
188
0
    total_weight = 0;
189
0
    for (n = 0; n < nb; ++n) {
190
0
      if (accum[n]) {
191
0
        const int new_center = (dist_accum[n] + accum[n] / 2) / accum[n];
192
0
        displaced += abs(centers[n] - new_center);
193
0
        centers[n] = new_center;
194
0
        weighted_average += new_center * accum[n];
195
0
        total_weight += accum[n];
196
0
      }
197
0
    }
198
0
    weighted_average = (weighted_average + total_weight / 2) / total_weight;
199
0
    if (displaced < 5) break;   // no need to keep on looping...
200
0
  }
201
202
  // Map each original value to the closest centroid
203
0
  for (n = 0; n < enc->mb_w_ * enc->mb_h_; ++n) {
204
0
    VP8MBInfo* const mb = &enc->mb_info_[n];
205
0
    const int alpha = mb->alpha_;
206
0
    mb->segment_ = map[alpha];
207
0
    mb->alpha_ = centers[map[alpha]];  // for the record.
208
0
  }
209
210
0
  if (nb > 1) {
211
0
    const int smooth = (enc->config_->preprocessing & 1);
212
0
    if (smooth) SmoothSegmentMap(enc);
213
0
  }
214
215
0
  SetSegmentAlphas(enc, centers, weighted_average);  // pick some alphas.
216
0
}
217
218
//------------------------------------------------------------------------------
219
// Macroblock analysis: collect histogram for each mode, deduce the maximal
220
// susceptibility and set best modes for this macroblock.
221
// Segment assignment is done later.
222
223
// Number of modes to inspect for alpha_ evaluation. We don't need to test all
224
// the possible modes during the analysis phase: we risk falling into a local
225
// optimum, or be subject to boundary effect
226
0
#define MAX_INTRA16_MODE 2
227
#define MAX_INTRA4_MODE  2
228
0
#define MAX_UV_MODE      2
229
230
0
static int MBAnalyzeBestIntra16Mode(VP8EncIterator* const it) {
231
0
  const int max_mode = MAX_INTRA16_MODE;
232
0
  int mode;
233
0
  int best_alpha = DEFAULT_ALPHA;
234
0
  int best_mode = 0;
235
236
0
  VP8MakeLuma16Preds(it);
237
0
  for (mode = 0; mode < max_mode; ++mode) {
238
0
    VP8Histogram histo;
239
0
    int alpha;
240
241
0
    InitHistogram(&histo);
242
0
    VP8CollectHistogram(it->yuv_in_ + Y_OFF_ENC,
243
0
                        it->yuv_p_ + VP8I16ModeOffsets[mode],
244
0
                        0, 16, &histo);
245
0
    alpha = GetAlpha(&histo);
246
0
    if (IS_BETTER_ALPHA(alpha, best_alpha)) {
247
0
      best_alpha = alpha;
248
0
      best_mode = mode;
249
0
    }
250
0
  }
251
0
  VP8SetIntra16Mode(it, best_mode);
252
0
  return best_alpha;
253
0
}
254
255
0
static int FastMBAnalyze(VP8EncIterator* const it) {
256
  // Empirical cut-off value, should be around 16 (~=block size). We use the
257
  // [8-17] range and favor intra4 at high quality, intra16 for low quality.
258
0
  const int q = (int)it->enc_->config_->quality;
259
0
  const uint32_t kThreshold = 8 + (17 - 8) * q / 100;
260
0
  int k;
261
0
  uint32_t dc[16], m, m2;
262
0
  for (k = 0; k < 16; k += 4) {
263
0
    VP8Mean16x4(it->yuv_in_ + Y_OFF_ENC + k * BPS, &dc[k]);
264
0
  }
265
0
  for (m = 0, m2 = 0, k = 0; k < 16; ++k) {
266
0
    m += dc[k];
267
0
    m2 += dc[k] * dc[k];
268
0
  }
269
0
  if (kThreshold * m2 < m * m) {
270
0
    VP8SetIntra16Mode(it, 0);   // DC16
271
0
  } else {
272
0
    const uint8_t modes[16] = { 0 };  // DC4
273
0
    VP8SetIntra4Mode(it, modes);
274
0
  }
275
0
  return 0;
276
0
}
277
278
0
static int MBAnalyzeBestUVMode(VP8EncIterator* const it) {
279
0
  int best_alpha = DEFAULT_ALPHA;
280
0
  int smallest_alpha = 0;
281
0
  int best_mode = 0;
282
0
  const int max_mode = MAX_UV_MODE;
283
0
  int mode;
284
285
0
  VP8MakeChroma8Preds(it);
286
0
  for (mode = 0; mode < max_mode; ++mode) {
287
0
    VP8Histogram histo;
288
0
    int alpha;
289
0
    InitHistogram(&histo);
290
0
    VP8CollectHistogram(it->yuv_in_ + U_OFF_ENC,
291
0
                        it->yuv_p_ + VP8UVModeOffsets[mode],
292
0
                        16, 16 + 4 + 4, &histo);
293
0
    alpha = GetAlpha(&histo);
294
0
    if (IS_BETTER_ALPHA(alpha, best_alpha)) {
295
0
      best_alpha = alpha;
296
0
    }
297
    // The best prediction mode tends to be the one with the smallest alpha.
298
0
    if (mode == 0 || alpha < smallest_alpha) {
299
0
      smallest_alpha = alpha;
300
0
      best_mode = mode;
301
0
    }
302
0
  }
303
0
  VP8SetIntraUVMode(it, best_mode);
304
0
  return best_alpha;
305
0
}
306
307
static void MBAnalyze(VP8EncIterator* const it,
308
                      int alphas[MAX_ALPHA + 1],
309
0
                      int* const alpha, int* const uv_alpha) {
310
0
  const VP8Encoder* const enc = it->enc_;
311
0
  int best_alpha, best_uv_alpha;
312
313
0
  VP8SetIntra16Mode(it, 0);  // default: Intra16, DC_PRED
314
0
  VP8SetSkip(it, 0);         // not skipped
315
0
  VP8SetSegment(it, 0);      // default segment, spec-wise.
316
317
0
  if (enc->method_ <= 1) {
318
0
    best_alpha = FastMBAnalyze(it);
319
0
  } else {
320
0
    best_alpha = MBAnalyzeBestIntra16Mode(it);
321
0
  }
322
0
  best_uv_alpha = MBAnalyzeBestUVMode(it);
323
324
  // Final susceptibility mix
325
0
  best_alpha = (3 * best_alpha + best_uv_alpha + 2) >> 2;
326
0
  best_alpha = FinalAlphaValue(best_alpha);
327
0
  alphas[best_alpha]++;
328
0
  it->mb_->alpha_ = best_alpha;   // for later remapping.
329
330
  // Accumulate for later complexity analysis.
331
0
  *alpha += best_alpha;   // mixed susceptibility (not just luma)
332
0
  *uv_alpha += best_uv_alpha;
333
0
}
334
335
0
static void DefaultMBInfo(VP8MBInfo* const mb) {
336
0
  mb->type_ = 1;     // I16x16
337
0
  mb->uv_mode_ = 0;
338
0
  mb->skip_ = 0;     // not skipped
339
0
  mb->segment_ = 0;  // default segment
340
0
  mb->alpha_ = 0;
341
0
}
342
343
//------------------------------------------------------------------------------
344
// Main analysis loop:
345
// Collect all susceptibilities for each macroblock and record their
346
// distribution in alphas[]. Segments is assigned a-posteriori, based on
347
// this histogram.
348
// We also pick an intra16 prediction mode, which shouldn't be considered
349
// final except for fast-encode settings. We can also pick some intra4 modes
350
// and decide intra4/intra16, but that's usually almost always a bad choice at
351
// this stage.
352
353
0
static void ResetAllMBInfo(VP8Encoder* const enc) {
354
0
  int n;
355
0
  for (n = 0; n < enc->mb_w_ * enc->mb_h_; ++n) {
356
0
    DefaultMBInfo(&enc->mb_info_[n]);
357
0
  }
358
  // Default susceptibilities.
359
0
  enc->dqm_[0].alpha_ = 0;
360
0
  enc->dqm_[0].beta_ = 0;
361
  // Note: we can't compute this alpha_ / uv_alpha_ -> set to default value.
362
0
  enc->alpha_ = 0;
363
0
  enc->uv_alpha_ = 0;
364
0
  WebPReportProgress(enc->pic_, enc->percent_ + 20, &enc->percent_);
365
0
}
366
367
// struct used to collect job result
368
typedef struct {
369
  WebPWorker worker;
370
  int alphas[MAX_ALPHA + 1];
371
  int alpha, uv_alpha;
372
  VP8EncIterator it;
373
  int delta_progress;
374
} SegmentJob;
375
376
// main work call
377
0
static int DoSegmentsJob(void* arg1, void* arg2) {
378
0
  SegmentJob* const job = (SegmentJob*)arg1;
379
0
  VP8EncIterator* const it = (VP8EncIterator*)arg2;
380
0
  int ok = 1;
381
0
  if (!VP8IteratorIsDone(it)) {
382
0
    uint8_t tmp[32 + WEBP_ALIGN_CST];
383
0
    uint8_t* const scratch = (uint8_t*)WEBP_ALIGN(tmp);
384
0
    do {
385
      // Let's pretend we have perfect lossless reconstruction.
386
0
      VP8IteratorImport(it, scratch);
387
0
      MBAnalyze(it, job->alphas, &job->alpha, &job->uv_alpha);
388
0
      ok = VP8IteratorProgress(it, job->delta_progress);
389
0
    } while (ok && VP8IteratorNext(it));
390
0
  }
391
0
  return ok;
392
0
}
393
394
#ifdef WEBP_USE_THREAD
395
0
static void MergeJobs(const SegmentJob* const src, SegmentJob* const dst) {
396
0
  int i;
397
0
  for (i = 0; i <= MAX_ALPHA; ++i) dst->alphas[i] += src->alphas[i];
398
0
  dst->alpha += src->alpha;
399
0
  dst->uv_alpha += src->uv_alpha;
400
0
}
401
#endif
402
403
// initialize the job struct with some tasks to perform
404
static void InitSegmentJob(VP8Encoder* const enc, SegmentJob* const job,
405
0
                           int start_row, int end_row) {
406
0
  WebPGetWorkerInterface()->Init(&job->worker);
407
0
  job->worker.data1 = job;
408
0
  job->worker.data2 = &job->it;
409
0
  job->worker.hook = DoSegmentsJob;
410
0
  VP8IteratorInit(enc, &job->it);
411
0
  VP8IteratorSetRow(&job->it, start_row);
412
0
  VP8IteratorSetCountDown(&job->it, (end_row - start_row) * enc->mb_w_);
413
0
  memset(job->alphas, 0, sizeof(job->alphas));
414
0
  job->alpha = 0;
415
0
  job->uv_alpha = 0;
416
  // only one of both jobs can record the progress, since we don't
417
  // expect the user's hook to be multi-thread safe
418
0
  job->delta_progress = (start_row == 0) ? 20 : 0;
419
0
}
420
421
// main entry point
422
0
int VP8EncAnalyze(VP8Encoder* const enc) {
423
0
  int ok = 1;
424
0
  const int do_segments =
425
0
      enc->config_->emulate_jpeg_size ||   // We need the complexity evaluation.
426
0
      (enc->segment_hdr_.num_segments_ > 1) ||
427
0
      (enc->method_ <= 1);  // for method 0 - 1, we need preds_[] to be filled.
428
0
  if (do_segments) {
429
0
    const int last_row = enc->mb_h_;
430
0
    const int total_mb = last_row * enc->mb_w_;
431
0
#ifdef WEBP_USE_THREAD
432
    // We give a little more than a half work to the main thread.
433
0
    const int split_row = (9 * last_row + 15) >> 4;
434
0
    const int kMinSplitRow = 2;  // minimal rows needed for mt to be worth it
435
0
    const int do_mt = (enc->thread_level_ > 0) && (split_row >= kMinSplitRow);
436
#else
437
    const int do_mt = 0;
438
#endif
439
0
    const WebPWorkerInterface* const worker_interface =
440
0
        WebPGetWorkerInterface();
441
0
    SegmentJob main_job;
442
0
    if (do_mt) {
443
0
#ifdef WEBP_USE_THREAD
444
0
      SegmentJob side_job;
445
      // Note the use of '&' instead of '&&' because we must call the functions
446
      // no matter what.
447
0
      InitSegmentJob(enc, &main_job, 0, split_row);
448
0
      InitSegmentJob(enc, &side_job, split_row, last_row);
449
      // we don't need to call Reset() on main_job.worker, since we're calling
450
      // WebPWorkerExecute() on it
451
0
      ok &= worker_interface->Reset(&side_job.worker);
452
      // launch the two jobs in parallel
453
0
      if (ok) {
454
0
        worker_interface->Launch(&side_job.worker);
455
0
        worker_interface->Execute(&main_job.worker);
456
0
        ok &= worker_interface->Sync(&side_job.worker);
457
0
        ok &= worker_interface->Sync(&main_job.worker);
458
0
      }
459
0
      worker_interface->End(&side_job.worker);
460
0
      if (ok) MergeJobs(&side_job, &main_job);  // merge results together
461
0
#endif  // WEBP_USE_THREAD
462
0
    } else {
463
      // Even for single-thread case, we use the generic Worker tools.
464
0
      InitSegmentJob(enc, &main_job, 0, last_row);
465
0
      worker_interface->Execute(&main_job.worker);
466
0
      ok &= worker_interface->Sync(&main_job.worker);
467
0
    }
468
0
    worker_interface->End(&main_job.worker);
469
0
    if (ok) {
470
0
      enc->alpha_ = main_job.alpha / total_mb;
471
0
      enc->uv_alpha_ = main_job.uv_alpha / total_mb;
472
0
      AssignSegments(enc, main_job.alphas);
473
0
    }
474
0
  } else {   // Use only one default segment.
475
0
    ResetAllMBInfo(enc);
476
0
  }
477
0
  if (!ok) {
478
0
    return WebPEncodingSetError(enc->pic_,
479
0
                                VP8_ENC_ERROR_OUT_OF_MEMORY);  // imprecise
480
0
  }
481
0
  return ok;
482
0
}
483