| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#include <math.h> |
| 18 |
|
| 19 |
#include "src/enc/cost_enc.h" |
| 20 |
#include "src/enc/vp8i_enc.h" |
| 21 |
#include "src/enc/vp8li_enc.h" |
| 22 |
#include "src/utils/utils.h" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifdef PRINT_MEMORY_INFO |
| 27 |
#include <stdio.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
int WebPGetEncoderVersion(void) { |
| 33 |
return (ENC_MAJ_VERSION << 16) | (ENC_MIN_VERSION << 8) | ENC_REV_VERSION; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static 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 |
|
| 47 |
static 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 |
|
| 55 |
static void ResetBoundaryPredictions(VP8Encoder* const enc) { |
| 56 |
|
| 57 |
|
| 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; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static 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 * |
| 106 |
(limit * limit) / (100 * 100); |
| 107 |
|
| 108 |
|
| 109 |
enc->mb_header_limit_ = |
| 110 |
(score_t)256 * 510 * 8 * 1024 / (enc->mb_w_ * enc->mb_h_); |
| 111 |
|
| 112 |
enc->thread_level_ = config->thread_level; |
| 113 |
|
| 114 |
enc->do_search_ = (config->target_size > 0 || config->target_PSNR > 0); |
| 115 |
if (!config->low_memory) { |
| 116 |
#if !defined(DISABLE_TOKEN_BUFFER) |
| 117 |
enc->use_tokens_ = (enc->rd_opt_level_ >= RD_OPT_BASIC); |
| 118 |
#endif |
| 119 |
if (enc->use_tokens_) { |
| 120 |
enc->num_parts_ = 1; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static VP8Encoder* InitVP8Encoder(const WebPConfig* const config, |
| 145 |
WebPPicture* const picture) { |
| 146 |
VP8Encoder* enc; |
| 147 |
const int use_filter = |
| 148 |
(config->filter_strength > 0) || (config->autofilter > 0); |
| 149 |
const int mb_w = (picture->width + 15) >> 4; |
| 150 |
const int mb_h = (picture->height + 15) >> 4; |
| 151 |
const int preds_w = 4 * mb_w + 1; |
| 152 |
const int preds_h = 4 * mb_h + 1; |
| 153 |
const size_t preds_size = preds_w * preds_h * sizeof(*enc->preds_); |
| 154 |
const int top_stride = mb_w * 16; |
| 155 |
const size_t nz_size = (mb_w + 1) * sizeof(*enc->nz_) + WEBP_ALIGN_CST; |
| 156 |
const size_t info_size = mb_w * mb_h * sizeof(*enc->mb_info_); |
| 157 |
const size_t samples_size = |
| 158 |
2 * top_stride * sizeof(*enc->y_top_) |
| 159 |
+ WEBP_ALIGN_CST; |
| 160 |
const size_t lf_stats_size = |
| 161 |
config->autofilter ? sizeof(*enc->lf_stats_) + WEBP_ALIGN_CST : 0; |
| 162 |
const size_t top_derr_size = |
| 163 |
(config->quality <= ERROR_DIFFUSION_QUALITY || config->pass > 1) ? |
| 164 |
mb_w * sizeof(*enc->top_derr_) : 0; |
| 165 |
uint8_t* mem; |
| 166 |
const uint64_t size = (uint64_t)sizeof(*enc) |
| 167 |
+ WEBP_ALIGN_CST |
| 168 |
+ info_size |
| 169 |
+ preds_size |
| 170 |
+ samples_size |
| 171 |
+ top_derr_size |
| 172 |
+ nz_size |
| 173 |
+ lf_stats_size; |
| 174 |
|
| 175 |
#ifdef PRINT_MEMORY_INFO |
| 176 |
printf("===================================\n"); |
| 177 |
printf("Memory used:\n" |
| 178 |
" encoder: %ld\n" |
| 179 |
" info: %ld\n" |
| 180 |
" preds: %ld\n" |
| 181 |
" top samples: %ld\n" |
| 182 |
" top diffusion: %ld\n" |
| 183 |
" non-zero: %ld\n" |
| 184 |
" lf-stats: %ld\n" |
| 185 |
" total: %ld\n", |
| 186 |
sizeof(*enc) + WEBP_ALIGN_CST, info_size, |
| 187 |
preds_size, samples_size, top_derr_size, nz_size, lf_stats_size, size); |
| 188 |
printf("Transient object sizes:\n" |
| 189 |
" VP8EncIterator: %ld\n" |
| 190 |
" VP8ModeScore: %ld\n" |
| 191 |
" VP8SegmentInfo: %ld\n" |
| 192 |
" VP8EncProba: %ld\n" |
| 193 |
" LFStats: %ld\n", |
| 194 |
sizeof(VP8EncIterator), sizeof(VP8ModeScore), |
| 195 |
sizeof(VP8SegmentInfo), sizeof(VP8EncProba), |
| 196 |
sizeof(LFStats)); |
| 197 |
printf("Picture size (yuv): %ld\n", |
| 198 |
mb_w * mb_h * 384 * sizeof(uint8_t)); |
| 199 |
printf("===================================\n"); |
| 200 |
#endif |
| 201 |
mem = (uint8_t*)WebPSafeMalloc(size, sizeof(*mem)); |
| 202 |
if (mem == NULL) { |
| 203 |
WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
enc = (VP8Encoder*)mem; |
| 207 |
mem = (uint8_t*)WEBP_ALIGN(mem + sizeof(*enc)); |
| 208 |
memset(enc, 0, sizeof(*enc)); |
| 209 |
enc->num_parts_ = 1 << config->partitions; |
| 210 |
enc->mb_w_ = mb_w; |
| 211 |
enc->mb_h_ = mb_h; |
| 212 |
enc->preds_w_ = preds_w; |
| 213 |
enc->mb_info_ = (VP8MBInfo*)mem; |
| 214 |
mem += info_size; |
| 215 |
enc->preds_ = mem + 1 + enc->preds_w_; |
| 216 |
mem += preds_size; |
| 217 |
enc->nz_ = 1 + (uint32_t*)WEBP_ALIGN(mem); |
| 218 |
mem += nz_size; |
| 219 |
enc->lf_stats_ = lf_stats_size ? (LFStats*)WEBP_ALIGN(mem) : NULL; |
| 220 |
mem += lf_stats_size; |
| 221 |
|
| 222 |
|
| 223 |
mem = (uint8_t*)WEBP_ALIGN(mem); |
| 224 |
enc->y_top_ = mem; |
| 225 |
enc->uv_top_ = enc->y_top_ + top_stride; |
| 226 |
mem += 2 * top_stride; |
| 227 |
enc->top_derr_ = top_derr_size ? (DError*)mem : NULL; |
| 228 |
mem += top_derr_size; |
| 229 |
assert(mem <= (uint8_t*)enc + size); |
| 230 |
|
| 231 |
enc->config_ = config; |
| 232 |
enc->profile_ = use_filter ? ((config->filter_type == 1) ? 0 : 1) : 2; |
| 233 |
enc->pic_ = picture; |
| 234 |
enc->percent_ = 0; |
| 235 |
|
| 236 |
MapConfigToTools(enc); |
| 237 |
VP8EncDspInit(); |
| 238 |
VP8DefaultProbas(enc); |
| 239 |
ResetSegmentHeader(enc); |
| 240 |
ResetFilterHeader(enc); |
| 241 |
ResetBoundaryPredictions(enc); |
| 242 |
VP8EncDspCostInit(); |
| 243 |
VP8EncInitAlpha(enc); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
{ |
| 248 |
const float scale = 1.f + config->quality * 5.f / 100.f; |
| 249 |
VP8TBufferInit(&enc->tokens_, (int)(mb_w * mb_h * 4 * scale)); |
| 250 |
} |
| 251 |
return enc; |
| 252 |
} |
| 253 |
|
| 254 |
static int DeleteVP8Encoder(VP8Encoder* enc) { |
| 255 |
int ok = 1; |
| 256 |
if (enc != NULL) { |
| 257 |
ok = VP8EncDeleteAlpha(enc); |
| 258 |
VP8TBufferClear(&enc->tokens_); |
| 259 |
WebPSafeFree(enc); |
| 260 |
} |
| 261 |
return ok; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
#if !defined(WEBP_DISABLE_STATS) |
| 267 |
static double GetPSNR(uint64_t err, uint64_t size) { |
| 268 |
return (err > 0 && size > 0) ? 10. * log10(255. * 255. * size / err) : 99.; |
| 269 |
} |
| 270 |
|
| 271 |
static void FinalizePSNR(const VP8Encoder* const enc) { |
| 272 |
WebPAuxStats* stats = enc->pic_->stats; |
| 273 |
const uint64_t size = enc->sse_count_; |
| 274 |
const uint64_t* const sse = enc->sse_; |
| 275 |
stats->PSNR[0] = (float)GetPSNR(sse[0], size); |
| 276 |
stats->PSNR[1] = (float)GetPSNR(sse[1], size / 4); |
| 277 |
stats->PSNR[2] = (float)GetPSNR(sse[2], size / 4); |
| 278 |
stats->PSNR[3] = (float)GetPSNR(sse[0] + sse[1] + sse[2], size * 3 / 2); |
| 279 |
stats->PSNR[4] = (float)GetPSNR(sse[3], size); |
| 280 |
} |
| 281 |
#endif |
| 282 |
|
| 283 |
static void StoreStats(VP8Encoder* const enc) { |
| 284 |
#if !defined(WEBP_DISABLE_STATS) |
| 285 |
WebPAuxStats* const stats = enc->pic_->stats; |
| 286 |
if (stats != NULL) { |
| 287 |
int i, s; |
| 288 |
for (i = 0; i < NUM_MB_SEGMENTS; ++i) { |
| 289 |
stats->segment_level[i] = enc->dqm_[i].fstrength_; |
| 290 |
stats->segment_quant[i] = enc->dqm_[i].quant_; |
| 291 |
for (s = 0; s <= 2; ++s) { |
| 292 |
stats->residual_bytes[s][i] = enc->residual_bytes_[s][i]; |
| 293 |
} |
| 294 |
} |
| 295 |
FinalizePSNR(enc); |
| 296 |
stats->coded_size = enc->coded_size_; |
| 297 |
for (i = 0; i < 3; ++i) { |
| 298 |
stats->block_count[i] = enc->block_count_[i]; |
| 299 |
} |
| 300 |
} |
| 301 |
#else |
| 302 |
WebPReportProgress(enc->pic_, 100, &enc->percent_); |
| 303 |
#endif |
| 304 |
} |
| 305 |
|
| 306 |
int WebPEncodingSetError(const WebPPicture* const pic, |
| 307 |
WebPEncodingError error) { |
| 308 |
assert((int)error < VP8_ENC_ERROR_LAST); |
| 309 |
assert((int)error >= VP8_ENC_OK); |
| 310 |
|
| 311 |
if (pic->error_code == VP8_ENC_OK) { |
| 312 |
((WebPPicture*)pic)->error_code = error; |
| 313 |
} |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
int WebPReportProgress(const WebPPicture* const pic, |
| 318 |
int percent, int* const percent_store) { |
| 319 |
if (percent_store != NULL && percent != *percent_store) { |
| 320 |
*percent_store = percent; |
| 321 |
if (pic->progress_hook && !pic->progress_hook(percent, pic)) { |
| 322 |
|
| 323 |
return WebPEncodingSetError(pic, VP8_ENC_ERROR_USER_ABORT); |
| 324 |
} |
| 325 |
} |
| 326 |
return 1; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
int WebPEncode(const WebPConfig* config, WebPPicture* pic) { |
| 331 |
int ok = 0; |
| 332 |
if (pic == NULL) return 0; |
| 333 |
|
| 334 |
pic->error_code = VP8_ENC_OK; |
| 335 |
if (config == NULL) { |
| 336 |
return WebPEncodingSetError(pic, VP8_ENC_ERROR_NULL_PARAMETER); |
| 337 |
} |
| 338 |
if (!WebPValidateConfig(config)) { |
| 339 |
return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION); |
| 340 |
} |
| 341 |
if (!WebPValidatePicture(pic)) return 0; |
| 342 |
if (pic->width > WEBP_MAX_DIMENSION || pic->height > WEBP_MAX_DIMENSION) { |
| 343 |
return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_DIMENSION); |
| 344 |
} |
| 345 |
|
| 346 |
if (pic->stats != NULL) memset(pic->stats, 0, sizeof(*pic->stats)); |
| 347 |
|
| 348 |
if (!config->lossless) { |
| 349 |
VP8Encoder* enc = NULL; |
| 350 |
|
| 351 |
if (pic->use_argb || pic->y == NULL || pic->u == NULL || pic->v == NULL) { |
| 352 |
|
| 353 |
if (config->use_sharp_yuv || (config->preprocessing & 4)) { |
| 354 |
if (!WebPPictureSharpARGBToYUVA(pic)) { |
| 355 |
return 0; |
| 356 |
} |
| 357 |
} else { |
| 358 |
float dithering = 0.f; |
| 359 |
if (config->preprocessing & 2) { |
| 360 |
const float x = config->quality / 100.f; |
| 361 |
const float x2 = x * x; |
| 362 |
|
| 363 |
|
| 364 |
dithering = 1.0f + (0.5f - 1.0f) * x2 * x2; |
| 365 |
} |
| 366 |
if (!WebPPictureARGBToYUVADithered(pic, WEBP_YUV420, dithering)) { |
| 367 |
return 0; |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if (!config->exact) { |
| 373 |
WebPCleanupTransparentArea(pic); |
| 374 |
} |
| 375 |
|
| 376 |
enc = InitVP8Encoder(config, pic); |
| 377 |
if (enc == NULL) return 0; |
| 378 |
|
| 379 |
ok = VP8EncAnalyze(enc); |
| 380 |
|
| 381 |
|
| 382 |
ok = ok && VP8EncStartAlpha(enc); |
| 383 |
if (!enc->use_tokens_) { |
| 384 |
ok = ok && VP8EncLoop(enc); |
| 385 |
} else { |
| 386 |
ok = ok && VP8EncTokenLoop(enc); |
| 387 |
} |
| 388 |
ok = ok && VP8EncFinishAlpha(enc); |
| 389 |
|
| 390 |
ok = ok && VP8EncWrite(enc); |
| 391 |
StoreStats(enc); |
| 392 |
if (!ok) { |
| 393 |
VP8EncFreeBitWriters(enc); |
| 394 |
} |
| 395 |
ok &= DeleteVP8Encoder(enc); |
| 396 |
} else { |
| 397 |
|
| 398 |
if (pic->argb == NULL && !WebPPictureYUVAToARGB(pic)) { |
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
if (!config->exact) { |
| 403 |
WebPReplaceTransparentPixels(pic, 0x000000); |
| 404 |
} |
| 405 |
|
| 406 |
ok = VP8LEncodeImage(config, pic); |
| 407 |
} |
| 408 |
|
| 409 |
return ok; |
| 410 |
} |