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// WebP encoder: main entry point
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#include <assert.h>
15#include <stdlib.h>
16#include <string.h>
17#include <math.h>
18
19#include "./cost.h"
20#include "./vp8enci.h"
21#include "./vp8li.h"
22#include "../utils/utils.h"
23
24// #define PRINT_MEMORY_INFO
25
26#ifdef PRINT_MEMORY_INFO
27#include <stdio.h>
28#endif
29
30//------------------------------------------------------------------------------
31
32int WebPGetEncoderVersion(void) {
33  return (ENC_MAJ_VERSION << 16) | (ENC_MIN_VERSION << 8) | ENC_REV_VERSION;
34}
35
36//------------------------------------------------------------------------------
37// VP8Encoder
38//------------------------------------------------------------------------------
39
40static void ResetSegmentHeader(VP8Encoder* const enc) {
41  VP8EncSegmentHeader* const hdr = &enc->segment_hdr_;
42  hdr->num_segments_ = enc->config_->segments;
43  hdr->update_map_  = (hdr->num_segments_ > 1);
44  hdr->size_ = 0;
45}
46
47static void ResetFilterHeader(VP8Encoder* const enc) {
48  VP8EncFilterHeader* const hdr = &enc->filter_hdr_;
49  hdr->simple_ = 1;
50  hdr->level_ = 0;
51  hdr->sharpness_ = 0;
52  hdr->i4x4_lf_delta_ = 0;
53}
54
55static void ResetBoundaryPredictions(VP8Encoder* const enc) {
56  // init boundary values once for all
57  // Note: actually, initializing the preds_[] is only needed for intra4.
58  int i;
59  uint8_t* const top = enc->preds_ - enc->preds_w_;
60  uint8_t* const left = enc->preds_ - 1;
61  for (i = -1; i < 4 * enc->mb_w_; ++i) {
62    top[i] = B_DC_PRED;
63  }
64  for (i = 0; i < 4 * enc->mb_h_; ++i) {
65    left[i * enc->preds_w_] = B_DC_PRED;
66  }
67  enc->nz_[-1] = 0;   // constant
68}
69
70// Mapping from config->method_ to coding tools used.
71//-------------------+---+---+---+---+---+---+---+
72//   Method          | 0 | 1 | 2 | 3 |(4)| 5 | 6 |
73//-------------------+---+---+---+---+---+---+---+
74// fast probe        | x |   |   | x |   |   |   |
75//-------------------+---+---+---+---+---+---+---+
76// dynamic proba     | ~ | x | x | x | x | x | x |
77//-------------------+---+---+---+---+---+---+---+
78// fast mode analysis|   |   |   |   | x | x | x |
79//-------------------+---+---+---+---+---+---+---+
80// basic rd-opt      |   |   |   | x | x | x | x |
81//-------------------+---+---+---+---+---+---+---+
82// disto-refine i4/16| x | x | x |   |   |   |   |
83//-------------------+---+---+---+---+---+---+---+
84// disto-refine uv   |   | x | x |   |   |   |   |
85//-------------------+---+---+---+---+---+---+---+
86// rd-opt i4/16      |   |   | ~ | x | x | x | x |
87//-------------------+---+---+---+---+---+---+---+
88// token buffer (opt)|   |   |   | x | x | x | x |
89//-------------------+---+---+---+---+---+---+---+
90// Trellis           |   |   |   |   |   | x |Ful|
91//-------------------+---+---+---+---+---+---+---+
92// full-SNS          |   |   |   |   | x | x | x |
93//-------------------+---+---+---+---+---+---+---+
94
95static void MapConfigToTools(VP8Encoder* const enc) {
96  const WebPConfig* const config = enc->config_;
97  const int method = config->method;
98  const int limit = 100 - config->partition_limit;
99  enc->method_ = method;
100  enc->rd_opt_level_ = (method >= 6) ? RD_OPT_TRELLIS_ALL
101                     : (method >= 5) ? RD_OPT_TRELLIS
102                     : (method >= 3) ? RD_OPT_BASIC
103                     : RD_OPT_NONE;
104  enc->max_i4_header_bits_ =
105      256 * 16 * 16 *                 // upper bound: up to 16bit per 4x4 block
106      (limit * limit) / (100 * 100);  // ... modulated with a quadratic curve.
107
108  enc->thread_level_ = config->thread_level;
109
110  enc->do_search_ = (config->target_size > 0 || config->target_PSNR > 0);
111  if (!config->low_memory) {
112#if !defined(DISABLE_TOKEN_BUFFER)
113    enc->use_tokens_ = (enc->rd_opt_level_ >= RD_OPT_BASIC);  // need rd stats
114#endif
115    if (enc->use_tokens_) {
116      enc->num_parts_ = 1;   // doesn't work with multi-partition
117    }
118  }
119}
120
121// Memory scaling with dimensions:
122//  memory (bytes) ~= 2.25 * w + 0.0625 * w * h
123//
124// Typical memory footprint (614x440 picture)
125//              encoder: 22111
126//                 info: 4368
127//                preds: 17741
128//          top samples: 1263
129//             non-zero: 175
130//             lf-stats: 0
131//                total: 45658
132// Transient object sizes:
133//       VP8EncIterator: 3360
134//         VP8ModeScore: 872
135//       VP8SegmentInfo: 732
136//          VP8EncProba: 18352
137//              LFStats: 2048
138// Picture size (yuv): 419328
139
140static VP8Encoder* InitVP8Encoder(const WebPConfig* const config,
141                                  WebPPicture* const picture) {
142  VP8Encoder* enc;
143  const int use_filter =
144      (config->filter_strength > 0) || (config->autofilter > 0);
145  const int mb_w = (picture->width + 15) >> 4;
146  const int mb_h = (picture->height + 15) >> 4;
147  const int preds_w = 4 * mb_w + 1;
148  const int preds_h = 4 * mb_h + 1;
149  const size_t preds_size = preds_w * preds_h * sizeof(*enc->preds_);
150  const int top_stride = mb_w * 16;
151  const size_t nz_size = (mb_w + 1) * sizeof(*enc->nz_) + WEBP_ALIGN_CST;
152  const size_t info_size = mb_w * mb_h * sizeof(*enc->mb_info_);
153  const size_t samples_size =
154      2 * top_stride * sizeof(*enc->y_top_)  // top-luma/u/v
155      + WEBP_ALIGN_CST;                      // align all
156  const size_t lf_stats_size =
157      config->autofilter ? sizeof(*enc->lf_stats_) + WEBP_ALIGN_CST : 0;
158  uint8_t* mem;
159  const uint64_t size = (uint64_t)sizeof(*enc)   // main struct
160                      + WEBP_ALIGN_CST           // cache alignment
161                      + info_size                // modes info
162                      + preds_size               // prediction modes
163                      + samples_size             // top/left samples
164                      + nz_size                  // coeff context bits
165                      + lf_stats_size;           // autofilter stats
166
167#ifdef PRINT_MEMORY_INFO
168  printf("===================================\n");
169  printf("Memory used:\n"
170         "             encoder: %ld\n"
171         "                info: %ld\n"
172         "               preds: %ld\n"
173         "         top samples: %ld\n"
174         "            non-zero: %ld\n"
175         "            lf-stats: %ld\n"
176         "               total: %ld\n",
177         sizeof(*enc) + WEBP_ALIGN_CST, info_size,
178         preds_size, samples_size, nz_size, lf_stats_size, size);
179  printf("Transient object sizes:\n"
180         "      VP8EncIterator: %ld\n"
181         "        VP8ModeScore: %ld\n"
182         "      VP8SegmentInfo: %ld\n"
183         "         VP8EncProba: %ld\n"
184         "             LFStats: %ld\n",
185         sizeof(VP8EncIterator), sizeof(VP8ModeScore),
186         sizeof(VP8SegmentInfo), sizeof(VP8EncProba),
187         sizeof(LFStats));
188  printf("Picture size (yuv): %ld\n",
189         mb_w * mb_h * 384 * sizeof(uint8_t));
190  printf("===================================\n");
191#endif
192  mem = (uint8_t*)WebPSafeMalloc(size, sizeof(*mem));
193  if (mem == NULL) {
194    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
195    return NULL;
196  }
197  enc = (VP8Encoder*)mem;
198  mem = (uint8_t*)WEBP_ALIGN(mem + sizeof(*enc));
199  memset(enc, 0, sizeof(*enc));
200  enc->num_parts_ = 1 << config->partitions;
201  enc->mb_w_ = mb_w;
202  enc->mb_h_ = mb_h;
203  enc->preds_w_ = preds_w;
204  enc->mb_info_ = (VP8MBInfo*)mem;
205  mem += info_size;
206  enc->preds_ = ((uint8_t*)mem) + 1 + enc->preds_w_;
207  mem += preds_size;
208  enc->nz_ = 1 + (uint32_t*)WEBP_ALIGN(mem);
209  mem += nz_size;
210  enc->lf_stats_ = lf_stats_size ? (LFStats*)WEBP_ALIGN(mem) : NULL;
211  mem += lf_stats_size;
212
213  // top samples (all 16-aligned)
214  mem = (uint8_t*)WEBP_ALIGN(mem);
215  enc->y_top_ = (uint8_t*)mem;
216  enc->uv_top_ = enc->y_top_ + top_stride;
217  mem += 2 * top_stride;
218  assert(mem <= (uint8_t*)enc + size);
219
220  enc->config_ = config;
221  enc->profile_ = use_filter ? ((config->filter_type == 1) ? 0 : 1) : 2;
222  enc->pic_ = picture;
223  enc->percent_ = 0;
224
225  MapConfigToTools(enc);
226  VP8EncDspInit();
227  VP8DefaultProbas(enc);
228  ResetSegmentHeader(enc);
229  ResetFilterHeader(enc);
230  ResetBoundaryPredictions(enc);
231  VP8EncDspCostInit();
232  VP8EncInitAlpha(enc);
233
234  // lower quality means smaller output -> we modulate a little the page
235  // size based on quality. This is just a crude 1rst-order prediction.
236  {
237    const float scale = 1.f + config->quality * 5.f / 100.f;  // in [1,6]
238    VP8TBufferInit(&enc->tokens_, (int)(mb_w * mb_h * 4 * scale));
239  }
240  return enc;
241}
242
243static int DeleteVP8Encoder(VP8Encoder* enc) {
244  int ok = 1;
245  if (enc != NULL) {
246    ok = VP8EncDeleteAlpha(enc);
247    VP8TBufferClear(&enc->tokens_);
248    WebPSafeFree(enc);
249  }
250  return ok;
251}
252
253//------------------------------------------------------------------------------
254
255static double GetPSNR(uint64_t err, uint64_t size) {
256  return (err > 0 && size > 0) ? 10. * log10(255. * 255. * size / err) : 99.;
257}
258
259static void FinalizePSNR(const VP8Encoder* const enc) {
260  WebPAuxStats* stats = enc->pic_->stats;
261  const uint64_t size = enc->sse_count_;
262  const uint64_t* const sse = enc->sse_;
263  stats->PSNR[0] = (float)GetPSNR(sse[0], size);
264  stats->PSNR[1] = (float)GetPSNR(sse[1], size / 4);
265  stats->PSNR[2] = (float)GetPSNR(sse[2], size / 4);
266  stats->PSNR[3] = (float)GetPSNR(sse[0] + sse[1] + sse[2], size * 3 / 2);
267  stats->PSNR[4] = (float)GetPSNR(sse[3], size);
268}
269
270static void StoreStats(VP8Encoder* const enc) {
271  WebPAuxStats* const stats = enc->pic_->stats;
272  if (stats != NULL) {
273    int i, s;
274    for (i = 0; i < NUM_MB_SEGMENTS; ++i) {
275      stats->segment_level[i] = enc->dqm_[i].fstrength_;
276      stats->segment_quant[i] = enc->dqm_[i].quant_;
277      for (s = 0; s <= 2; ++s) {
278        stats->residual_bytes[s][i] = enc->residual_bytes_[s][i];
279      }
280    }
281    FinalizePSNR(enc);
282    stats->coded_size = enc->coded_size_;
283    for (i = 0; i < 3; ++i) {
284      stats->block_count[i] = enc->block_count_[i];
285    }
286  }
287  WebPReportProgress(enc->pic_, 100, &enc->percent_);  // done!
288}
289
290int WebPEncodingSetError(const WebPPicture* const pic,
291                         WebPEncodingError error) {
292  assert((int)error < VP8_ENC_ERROR_LAST);
293  assert((int)error >= VP8_ENC_OK);
294  ((WebPPicture*)pic)->error_code = error;
295  return 0;
296}
297
298int WebPReportProgress(const WebPPicture* const pic,
299                       int percent, int* const percent_store) {
300  if (percent_store != NULL && percent != *percent_store) {
301    *percent_store = percent;
302    if (pic->progress_hook && !pic->progress_hook(percent, pic)) {
303      // user abort requested
304      WebPEncodingSetError(pic, VP8_ENC_ERROR_USER_ABORT);
305      return 0;
306    }
307  }
308  return 1;  // ok
309}
310//------------------------------------------------------------------------------
311
312int WebPEncode(const WebPConfig* config, WebPPicture* pic) {
313  int ok = 0;
314
315  if (pic == NULL)
316    return 0;
317  WebPEncodingSetError(pic, VP8_ENC_OK);  // all ok so far
318  if (config == NULL)  // bad params
319    return WebPEncodingSetError(pic, VP8_ENC_ERROR_NULL_PARAMETER);
320  if (!WebPValidateConfig(config))
321    return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
322  if (pic->width <= 0 || pic->height <= 0)
323    return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_DIMENSION);
324  if (pic->width > WEBP_MAX_DIMENSION || pic->height > WEBP_MAX_DIMENSION)
325    return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_DIMENSION);
326
327  if (pic->stats != NULL) memset(pic->stats, 0, sizeof(*pic->stats));
328
329  if (!config->lossless) {
330    VP8Encoder* enc = NULL;
331
332    if (!config->exact) {
333      WebPCleanupTransparentArea(pic);
334    }
335
336    if (pic->use_argb || pic->y == NULL || pic->u == NULL || pic->v == NULL) {
337      // Make sure we have YUVA samples.
338      if (config->preprocessing & 4) {
339        if (!WebPPictureSmartARGBToYUVA(pic)) {
340          return 0;
341        }
342      } else {
343        float dithering = 0.f;
344        if (config->preprocessing & 2) {
345          const float x = config->quality / 100.f;
346          const float x2 = x * x;
347          // slowly decreasing from max dithering at low quality (q->0)
348          // to 0.5 dithering amplitude at high quality (q->100)
349          dithering = 1.0f + (0.5f - 1.0f) * x2 * x2;
350        }
351        if (!WebPPictureARGBToYUVADithered(pic, WEBP_YUV420, dithering)) {
352          return 0;
353        }
354      }
355    }
356
357    enc = InitVP8Encoder(config, pic);
358    if (enc == NULL) return 0;  // pic->error is already set.
359    // Note: each of the tasks below account for 20% in the progress report.
360    ok = VP8EncAnalyze(enc);
361
362    // Analysis is done, proceed to actual coding.
363    ok = ok && VP8EncStartAlpha(enc);   // possibly done in parallel
364    if (!enc->use_tokens_) {
365      ok = ok && VP8EncLoop(enc);
366    } else {
367      ok = ok && VP8EncTokenLoop(enc);
368    }
369    ok = ok && VP8EncFinishAlpha(enc);
370
371    ok = ok && VP8EncWrite(enc);
372    StoreStats(enc);
373    if (!ok) {
374      VP8EncFreeBitWriters(enc);
375    }
376    ok &= DeleteVP8Encoder(enc);  // must always be called, even if !ok
377  } else {
378    // Make sure we have ARGB samples.
379    if (pic->argb == NULL && !WebPPictureYUVAToARGB(pic)) {
380      return 0;
381    }
382
383    if (!config->exact) {
384      WebPCleanupTransparentAreaLossless(pic);
385    }
386
387    ok = VP8LEncodeImage(config, pic);  // Sets pic->error in case of problem.
388  }
389
390  return ok;
391}
392