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