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