| 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 |
|
| 18 |
#include "src/enc/vp8i_enc.h" |
| 19 |
#include "src/dsp/dsp.h" |
| 20 |
#include "src/utils/filters_utils.h" |
| 21 |
#include "src/utils/quant_levels_utils.h" |
| 22 |
#include "src/utils/utils.h" |
| 23 |
#include "src/webp/encode.h" |
| 24 |
#include "src/webp/format_constants.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#include "src/enc/vp8li_enc.h" |
| 50 |
|
| 51 |
static int EncodeLossless(const uint8_t* const data, int width, int height, |
| 52 |
int effort_level, |
| 53 |
int use_quality_100, VP8LBitWriter* const bw, |
| 54 |
WebPAuxStats* const stats) { |
| 55 |
int ok = 0; |
| 56 |
WebPConfig config; |
| 57 |
WebPPicture picture; |
| 58 |
|
| 59 |
if (!WebPPictureInit(&picture)) return 0; |
| 60 |
picture.width = width; |
| 61 |
picture.height = height; |
| 62 |
picture.use_argb = 1; |
| 63 |
picture.stats = stats; |
| 64 |
if (!WebPPictureAlloc(&picture)) return 0; |
| 65 |
|
| 66 |
|
| 67 |
WebPDispatchAlphaToGreen(data, width, picture.width, picture.height, |
| 68 |
picture.argb, picture.argb_stride); |
| 69 |
|
| 70 |
if (!WebPConfigInit(&config)) return 0; |
| 71 |
config.lossless = 1; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
config.exact = 1; |
| 77 |
config.method = effort_level; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
config.quality = |
| 84 |
(use_quality_100 && effort_level == 6) ? 100 : 8.f * effort_level; |
| 85 |
assert(config.quality >= 0 && config.quality <= 100.f); |
| 86 |
|
| 87 |
ok = VP8LEncodeStream(&config, &picture, bw); |
| 88 |
WebPPictureFree(&picture); |
| 89 |
ok = ok && !bw->error_; |
| 90 |
if (!ok) { |
| 91 |
VP8LBitWriterWipeOut(bw); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
typedef struct { |
| 101 |
size_t score; |
| 102 |
VP8BitWriter bw; |
| 103 |
WebPAuxStats stats; |
| 104 |
} FilterTrial; |
| 105 |
|
| 106 |
|
| 107 |
static int EncodeAlphaInternal(const uint8_t* const data, int width, int height, |
| 108 |
int method, int filter, int reduce_levels, |
| 109 |
int effort_level, |
| 110 |
uint8_t* const tmp_alpha, |
| 111 |
FilterTrial* result) { |
| 112 |
int ok = 0; |
| 113 |
const uint8_t* alpha_src; |
| 114 |
WebPFilterFunc filter_func; |
| 115 |
uint8_t header; |
| 116 |
const size_t data_size = width * height; |
| 117 |
const uint8_t* output = NULL; |
| 118 |
size_t output_size = 0; |
| 119 |
VP8LBitWriter tmp_bw; |
| 120 |
|
| 121 |
assert((uint64_t)data_size == (uint64_t)width * height); |
| 122 |
assert(filter >= 0 && filter < WEBP_FILTER_LAST); |
| 123 |
assert(method >= ALPHA_NO_COMPRESSION); |
| 124 |
assert(method <= ALPHA_LOSSLESS_COMPRESSION); |
| 125 |
assert(sizeof(header) == ALPHA_HEADER_LEN); |
| 126 |
|
| 127 |
filter_func = WebPFilters[filter]; |
| 128 |
if (filter_func != NULL) { |
| 129 |
filter_func(data, width, height, width, tmp_alpha); |
| 130 |
alpha_src = tmp_alpha; |
| 131 |
} else { |
| 132 |
alpha_src = data; |
| 133 |
} |
| 134 |
|
| 135 |
if (method != ALPHA_NO_COMPRESSION) { |
| 136 |
ok = VP8LBitWriterInit(&tmp_bw, data_size >> 3); |
| 137 |
ok = ok && EncodeLossless(alpha_src, width, height, effort_level, |
| 138 |
!reduce_levels, &tmp_bw, &result->stats); |
| 139 |
if (ok) { |
| 140 |
output = VP8LBitWriterFinish(&tmp_bw); |
| 141 |
if (tmp_bw.error_) { |
| 142 |
VP8LBitWriterWipeOut(&tmp_bw); |
| 143 |
memset(&result->bw, 0, sizeof(result->bw)); |
| 144 |
return 0; |
| 145 |
} |
| 146 |
output_size = VP8LBitWriterNumBytes(&tmp_bw); |
| 147 |
if (output_size > data_size) { |
| 148 |
|
| 149 |
method = ALPHA_NO_COMPRESSION; |
| 150 |
VP8LBitWriterWipeOut(&tmp_bw); |
| 151 |
} |
| 152 |
} else { |
| 153 |
VP8LBitWriterWipeOut(&tmp_bw); |
| 154 |
memset(&result->bw, 0, sizeof(result->bw)); |
| 155 |
return 0; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if (method == ALPHA_NO_COMPRESSION) { |
| 160 |
output = alpha_src; |
| 161 |
output_size = data_size; |
| 162 |
ok = 1; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
header = method | (filter << 2); |
| 167 |
if (reduce_levels) header |= ALPHA_PREPROCESSED_LEVELS << 4; |
| 168 |
|
| 169 |
if (!VP8BitWriterInit(&result->bw, ALPHA_HEADER_LEN + output_size)) ok = 0; |
| 170 |
ok = ok && VP8BitWriterAppend(&result->bw, &header, ALPHA_HEADER_LEN); |
| 171 |
ok = ok && VP8BitWriterAppend(&result->bw, output, output_size); |
| 172 |
|
| 173 |
if (method != ALPHA_NO_COMPRESSION) { |
| 174 |
VP8LBitWriterWipeOut(&tmp_bw); |
| 175 |
} |
| 176 |
ok = ok && !result->bw.error_; |
| 177 |
result->score = VP8BitWriterSize(&result->bw); |
| 178 |
return ok; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
static int GetNumColors(const uint8_t* data, int width, int height, |
| 184 |
int stride) { |
| 185 |
int j; |
| 186 |
int colors = 0; |
| 187 |
uint8_t color[256] = { 0 }; |
| 188 |
|
| 189 |
for (j = 0; j < height; ++j) { |
| 190 |
int i; |
| 191 |
const uint8_t* const p = data + j * stride; |
| 192 |
for (i = 0; i < width; ++i) { |
| 193 |
color[p[i]] = 1; |
| 194 |
} |
| 195 |
} |
| 196 |
for (j = 0; j < 256; ++j) { |
| 197 |
if (color[j] > 0) ++colors; |
| 198 |
} |
| 199 |
return colors; |
| 200 |
} |
| 201 |
|
| 202 |
#define FILTER_TRY_NONE (1 << WEBP_FILTER_NONE) |
| 203 |
#define FILTER_TRY_ALL ((1 << WEBP_FILTER_LAST) - 1) |
| 204 |
|
| 205 |
|
| 206 |
static uint32_t GetFilterMap(const uint8_t* alpha, int width, int height, |
| 207 |
int filter, int effort_level) { |
| 208 |
uint32_t bit_map = 0U; |
| 209 |
if (filter == WEBP_FILTER_FAST) { |
| 210 |
|
| 211 |
int try_filter_none = (effort_level > 3); |
| 212 |
const int kMinColorsForFilterNone = 16; |
| 213 |
const int kMaxColorsForFilterNone = 192; |
| 214 |
const int num_colors = GetNumColors(alpha, width, height, width); |
| 215 |
|
| 216 |
filter = (num_colors <= kMinColorsForFilterNone) |
| 217 |
? WEBP_FILTER_NONE |
| 218 |
: WebPEstimateBestFilter(alpha, width, height, width); |
| 219 |
bit_map |= 1 << filter; |
| 220 |
|
| 221 |
|
| 222 |
if (try_filter_none || num_colors > kMaxColorsForFilterNone) { |
| 223 |
bit_map |= FILTER_TRY_NONE; |
| 224 |
} |
| 225 |
} else if (filter == WEBP_FILTER_NONE) { |
| 226 |
bit_map = FILTER_TRY_NONE; |
| 227 |
} else { |
| 228 |
bit_map = FILTER_TRY_ALL; |
| 229 |
} |
| 230 |
return bit_map; |
| 231 |
} |
| 232 |
|
| 233 |
static void InitFilterTrial(FilterTrial* const score) { |
| 234 |
score->score = (size_t)~0U; |
| 235 |
VP8BitWriterInit(&score->bw, 0); |
| 236 |
} |
| 237 |
|
| 238 |
static int ApplyFiltersAndEncode(const uint8_t* alpha, int width, int height, |
| 239 |
size_t data_size, int method, int filter, |
| 240 |
int reduce_levels, int effort_level, |
| 241 |
uint8_t** const output, |
| 242 |
size_t* const output_size, |
| 243 |
WebPAuxStats* const stats) { |
| 244 |
int ok = 1; |
| 245 |
FilterTrial best; |
| 246 |
uint32_t try_map = |
| 247 |
GetFilterMap(alpha, width, height, filter, effort_level); |
| 248 |
InitFilterTrial(&best); |
| 249 |
|
| 250 |
if (try_map != FILTER_TRY_NONE) { |
| 251 |
uint8_t* filtered_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size); |
| 252 |
if (filtered_alpha == NULL) return 0; |
| 253 |
|
| 254 |
for (filter = WEBP_FILTER_NONE; ok && try_map; ++filter, try_map >>= 1) { |
| 255 |
if (try_map & 1) { |
| 256 |
FilterTrial trial; |
| 257 |
ok = EncodeAlphaInternal(alpha, width, height, method, filter, |
| 258 |
reduce_levels, effort_level, filtered_alpha, |
| 259 |
&trial); |
| 260 |
if (ok && trial.score < best.score) { |
| 261 |
VP8BitWriterWipeOut(&best.bw); |
| 262 |
best = trial; |
| 263 |
} else { |
| 264 |
VP8BitWriterWipeOut(&trial.bw); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
WebPSafeFree(filtered_alpha); |
| 269 |
} else { |
| 270 |
ok = EncodeAlphaInternal(alpha, width, height, method, WEBP_FILTER_NONE, |
| 271 |
reduce_levels, effort_level, NULL, &best); |
| 272 |
} |
| 273 |
if (ok) { |
| 274 |
#if !defined(WEBP_DISABLE_STATS) |
| 275 |
if (stats != NULL) { |
| 276 |
stats->lossless_features = best.stats.lossless_features; |
| 277 |
stats->histogram_bits = best.stats.histogram_bits; |
| 278 |
stats->transform_bits = best.stats.transform_bits; |
| 279 |
stats->cache_bits = best.stats.cache_bits; |
| 280 |
stats->palette_size = best.stats.palette_size; |
| 281 |
stats->lossless_size = best.stats.lossless_size; |
| 282 |
stats->lossless_hdr_size = best.stats.lossless_hdr_size; |
| 283 |
stats->lossless_data_size = best.stats.lossless_data_size; |
| 284 |
} |
| 285 |
#else |
| 286 |
(void)stats; |
| 287 |
#endif |
| 288 |
*output_size = VP8BitWriterSize(&best.bw); |
| 289 |
*output = VP8BitWriterBuf(&best.bw); |
| 290 |
} else { |
| 291 |
VP8BitWriterWipeOut(&best.bw); |
| 292 |
} |
| 293 |
return ok; |
| 294 |
} |
| 295 |
|
| 296 |
static int EncodeAlpha(VP8Encoder* const enc, |
| 297 |
int quality, int method, int filter, |
| 298 |
int effort_level, |
| 299 |
uint8_t** const output, size_t* const output_size) { |
| 300 |
const WebPPicture* const pic = enc->pic_; |
| 301 |
const int width = pic->width; |
| 302 |
const int height = pic->height; |
| 303 |
|
| 304 |
uint8_t* quant_alpha = NULL; |
| 305 |
const size_t data_size = width * height; |
| 306 |
uint64_t sse = 0; |
| 307 |
int ok = 1; |
| 308 |
const int reduce_levels = (quality < 100); |
| 309 |
|
| 310 |
|
| 311 |
assert((uint64_t)data_size == (uint64_t)width * height); |
| 312 |
assert(enc != NULL && pic != NULL && pic->a != NULL); |
| 313 |
assert(output != NULL && output_size != NULL); |
| 314 |
assert(width > 0 && height > 0); |
| 315 |
assert(pic->a_stride >= width); |
| 316 |
assert(filter >= WEBP_FILTER_NONE && filter <= WEBP_FILTER_FAST); |
| 317 |
|
| 318 |
if (quality < 0 || quality > 100) { |
| 319 |
return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION); |
| 320 |
} |
| 321 |
|
| 322 |
if (method < ALPHA_NO_COMPRESSION || method > ALPHA_LOSSLESS_COMPRESSION) { |
| 323 |
return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION); |
| 324 |
} |
| 325 |
|
| 326 |
if (method == ALPHA_NO_COMPRESSION) { |
| 327 |
|
| 328 |
filter = WEBP_FILTER_NONE; |
| 329 |
} |
| 330 |
|
| 331 |
quant_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size); |
| 332 |
if (quant_alpha == NULL) { |
| 333 |
return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
WebPCopyPlane(pic->a, pic->a_stride, quant_alpha, width, width, height); |
| 338 |
|
| 339 |
if (reduce_levels) { |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
const int alpha_levels = (quality <= 70) ? (2 + quality / 5) |
| 344 |
: (16 + (quality - 70) * 8); |
| 345 |
ok = QuantizeLevels(quant_alpha, width, height, alpha_levels, &sse); |
| 346 |
} |
| 347 |
|
| 348 |
if (ok) { |
| 349 |
VP8FiltersInit(); |
| 350 |
ok = ApplyFiltersAndEncode(quant_alpha, width, height, data_size, method, |
| 351 |
filter, reduce_levels, effort_level, output, |
| 352 |
output_size, pic->stats); |
| 353 |
if (!ok) { |
| 354 |
WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 355 |
} |
| 356 |
#if !defined(WEBP_DISABLE_STATS) |
| 357 |
if (pic->stats != NULL) { |
| 358 |
pic->stats->coded_size += (int)(*output_size); |
| 359 |
enc->sse_[3] = sse; |
| 360 |
} |
| 361 |
#endif |
| 362 |
} |
| 363 |
|
| 364 |
WebPSafeFree(quant_alpha); |
| 365 |
return ok; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
static int CompressAlphaJob(void* arg1, void* unused) { |
| 372 |
VP8Encoder* const enc = (VP8Encoder*)arg1; |
| 373 |
const WebPConfig* config = enc->config_; |
| 374 |
uint8_t* alpha_data = NULL; |
| 375 |
size_t alpha_size = 0; |
| 376 |
const int effort_level = config->method; |
| 377 |
const WEBP_FILTER_TYPE filter = |
| 378 |
(config->alpha_filtering == 0) ? WEBP_FILTER_NONE : |
| 379 |
(config->alpha_filtering == 1) ? WEBP_FILTER_FAST : |
| 380 |
WEBP_FILTER_BEST; |
| 381 |
if (!EncodeAlpha(enc, config->alpha_quality, config->alpha_compression, |
| 382 |
filter, effort_level, &alpha_data, &alpha_size)) { |
| 383 |
return 0; |
| 384 |
} |
| 385 |
if (alpha_size != (uint32_t)alpha_size) { |
| 386 |
WebPSafeFree(alpha_data); |
| 387 |
return 0; |
| 388 |
} |
| 389 |
enc->alpha_data_size_ = (uint32_t)alpha_size; |
| 390 |
enc->alpha_data_ = alpha_data; |
| 391 |
(void)unused; |
| 392 |
return 1; |
| 393 |
} |
| 394 |
|
| 395 |
void VP8EncInitAlpha(VP8Encoder* const enc) { |
| 396 |
WebPInitAlphaProcessing(); |
| 397 |
enc->has_alpha_ = WebPPictureHasTransparency(enc->pic_); |
| 398 |
enc->alpha_data_ = NULL; |
| 399 |
enc->alpha_data_size_ = 0; |
| 400 |
if (enc->thread_level_ > 0) { |
| 401 |
WebPWorker* const worker = &enc->alpha_worker_; |
| 402 |
WebPGetWorkerInterface()->Init(worker); |
| 403 |
worker->data1 = enc; |
| 404 |
worker->data2 = NULL; |
| 405 |
worker->hook = CompressAlphaJob; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
int VP8EncStartAlpha(VP8Encoder* const enc) { |
| 410 |
if (enc->has_alpha_) { |
| 411 |
if (enc->thread_level_ > 0) { |
| 412 |
WebPWorker* const worker = &enc->alpha_worker_; |
| 413 |
|
| 414 |
if (!WebPGetWorkerInterface()->Reset(worker)) { |
| 415 |
return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY); |
| 416 |
} |
| 417 |
WebPGetWorkerInterface()->Launch(worker); |
| 418 |
return 1; |
| 419 |
} else { |
| 420 |
return CompressAlphaJob(enc, NULL); |
| 421 |
} |
| 422 |
} |
| 423 |
return 1; |
| 424 |
} |
| 425 |
|
| 426 |
int VP8EncFinishAlpha(VP8Encoder* const enc) { |
| 427 |
if (enc->has_alpha_) { |
| 428 |
if (enc->thread_level_ > 0) { |
| 429 |
WebPWorker* const worker = &enc->alpha_worker_; |
| 430 |
if (!WebPGetWorkerInterface()->Sync(worker)) return 0; |
| 431 |
} |
| 432 |
} |
| 433 |
return WebPReportProgress(enc->pic_, enc->percent_ + 20, &enc->percent_); |
| 434 |
} |
| 435 |
|
| 436 |
int VP8EncDeleteAlpha(VP8Encoder* const enc) { |
| 437 |
int ok = 1; |
| 438 |
if (enc->thread_level_ > 0) { |
| 439 |
WebPWorker* const worker = &enc->alpha_worker_; |
| 440 |
|
| 441 |
ok = WebPGetWorkerInterface()->Sync(worker); |
| 442 |
|
| 443 |
WebPGetWorkerInterface()->End(worker); |
| 444 |
} |
| 445 |
WebPSafeFree(enc->alpha_data_); |
| 446 |
enc->alpha_data_ = NULL; |
| 447 |
enc->alpha_data_size_ = 0; |
| 448 |
enc->has_alpha_ = 0; |
| 449 |
return ok; |
| 450 |
} |