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