| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <float.h> |
| 19 |
|
| 20 |
#include "src/dsp/lossless_common.h" |
| 21 |
#include "src/enc/backward_references_enc.h" |
| 22 |
#include "src/enc/histogram_enc.h" |
| 23 |
#include "src/utils/color_cache_utils.h" |
| 24 |
#include "src/utils/utils.h" |
| 25 |
|
| 26 |
#define VALUES_IN_BYTE 256 |
| 27 |
|
| 28 |
extern void VP8LClearBackwardRefs(VP8LBackwardRefs* const refs); |
| 29 |
extern int VP8LDistanceToPlaneCode(int xsize, int dist); |
| 30 |
extern void VP8LBackwardRefsCursorAdd(VP8LBackwardRefs* const refs, |
| 31 |
const PixOrCopy v); |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
float alpha_[VALUES_IN_BYTE]; |
| 35 |
float red_[VALUES_IN_BYTE]; |
| 36 |
float blue_[VALUES_IN_BYTE]; |
| 37 |
float distance_[NUM_DISTANCE_CODES]; |
| 38 |
float* literal_; |
| 39 |
} CostModel; |
| 40 |
|
| 41 |
static void ConvertPopulationCountTableToBitEstimates( |
| 42 |
int num_symbols, const uint32_t population_counts[], float output[]) { |
| 43 |
uint32_t sum = 0; |
| 44 |
int nonzeros = 0; |
| 45 |
int i; |
| 46 |
for (i = 0; i < num_symbols; ++i) { |
| 47 |
sum += population_counts[i]; |
| 48 |
if (population_counts[i] > 0) { |
| 49 |
++nonzeros; |
| 50 |
} |
| 51 |
} |
| 52 |
if (nonzeros <= 1) { |
| 53 |
memset(output, 0, num_symbols * sizeof(*output)); |
| 54 |
} else { |
| 55 |
const float logsum = VP8LFastLog2(sum); |
| 56 |
for (i = 0; i < num_symbols; ++i) { |
| 57 |
output[i] = logsum - VP8LFastLog2(population_counts[i]); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
static int CostModelBuild(CostModel* const m, int xsize, int cache_bits, |
| 63 |
const VP8LBackwardRefs* const refs) { |
| 64 |
int ok = 0; |
| 65 |
VP8LRefsCursor c = VP8LRefsCursorInit(refs); |
| 66 |
VP8LHistogram* const histo = VP8LAllocateHistogram(cache_bits); |
| 67 |
if (histo == NULL) goto Error; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
VP8LHistogramInit(histo, cache_bits, 1); |
| 72 |
while (VP8LRefsCursorOk(&c)) { |
| 73 |
VP8LHistogramAddSinglePixOrCopy(histo, c.cur_pos, VP8LDistanceToPlaneCode, |
| 74 |
xsize); |
| 75 |
VP8LRefsCursorNext(&c); |
| 76 |
} |
| 77 |
|
| 78 |
ConvertPopulationCountTableToBitEstimates( |
| 79 |
VP8LHistogramNumCodes(histo->palette_code_bits_), histo->literal_, |
| 80 |
m->literal_); |
| 81 |
ConvertPopulationCountTableToBitEstimates( |
| 82 |
VALUES_IN_BYTE, histo->red_, m->red_); |
| 83 |
ConvertPopulationCountTableToBitEstimates( |
| 84 |
VALUES_IN_BYTE, histo->blue_, m->blue_); |
| 85 |
ConvertPopulationCountTableToBitEstimates( |
| 86 |
VALUES_IN_BYTE, histo->alpha_, m->alpha_); |
| 87 |
ConvertPopulationCountTableToBitEstimates( |
| 88 |
NUM_DISTANCE_CODES, histo->distance_, m->distance_); |
| 89 |
ok = 1; |
| 90 |
|
| 91 |
Error: |
| 92 |
VP8LFreeHistogram(histo); |
| 93 |
return ok; |
| 94 |
} |
| 95 |
|
| 96 |
static WEBP_INLINE float GetLiteralCost(const CostModel* const m, uint32_t v) { |
| 97 |
return m->alpha_[v >> 24] + |
| 98 |
m->red_[(v >> 16) & 0xff] + |
| 99 |
m->literal_[(v >> 8) & 0xff] + |
| 100 |
m->blue_[v & 0xff]; |
| 101 |
} |
| 102 |
|
| 103 |
static WEBP_INLINE float GetCacheCost(const CostModel* const m, uint32_t idx) { |
| 104 |
const int literal_idx = VALUES_IN_BYTE + NUM_LENGTH_CODES + idx; |
| 105 |
return m->literal_[literal_idx]; |
| 106 |
} |
| 107 |
|
| 108 |
static WEBP_INLINE float GetLengthCost(const CostModel* const m, |
| 109 |
uint32_t length) { |
| 110 |
int code, extra_bits; |
| 111 |
VP8LPrefixEncodeBits(length, &code, &extra_bits); |
| 112 |
return m->literal_[VALUES_IN_BYTE + code] + extra_bits; |
| 113 |
} |
| 114 |
|
| 115 |
static WEBP_INLINE float GetDistanceCost(const CostModel* const m, |
| 116 |
uint32_t distance) { |
| 117 |
int code, extra_bits; |
| 118 |
VP8LPrefixEncodeBits(distance, &code, &extra_bits); |
| 119 |
return m->distance_[code] + extra_bits; |
| 120 |
} |
| 121 |
|
| 122 |
static WEBP_INLINE void AddSingleLiteralWithCostModel( |
| 123 |
const uint32_t* const argb, VP8LColorCache* const hashers, |
| 124 |
const CostModel* const cost_model, int idx, int use_color_cache, |
| 125 |
float prev_cost, float* const cost, uint16_t* const dist_array) { |
| 126 |
float cost_val = prev_cost; |
| 127 |
const uint32_t color = argb[idx]; |
| 128 |
const int ix = use_color_cache ? VP8LColorCacheContains(hashers, color) : -1; |
| 129 |
if (ix >= 0) { |
| 130 |
|
| 131 |
const float mul0 = 0.68f; |
| 132 |
cost_val += GetCacheCost(cost_model, ix) * mul0; |
| 133 |
} else { |
| 134 |
const float mul1 = 0.82f; |
| 135 |
if (use_color_cache) VP8LColorCacheInsert(hashers, color); |
| 136 |
cost_val += GetLiteralCost(cost_model, color) * mul1; |
| 137 |
} |
| 138 |
if (cost[idx] > cost_val) { |
| 139 |
cost[idx] = cost_val; |
| 140 |
dist_array[idx] = 1; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#define COST_CACHE_INTERVAL_SIZE_MAX 500 |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
typedef struct CostInterval CostInterval; |
| 165 |
struct CostInterval { |
| 166 |
float cost_; |
| 167 |
int start_; |
| 168 |
int end_; |
| 169 |
int index_; |
| 170 |
CostInterval* previous_; |
| 171 |
CostInterval* next_; |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
typedef struct { |
| 176 |
float cost_; |
| 177 |
int start_; |
| 178 |
int end_; |
| 179 |
} CostCacheInterval; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
#define COST_MANAGER_MAX_FREE_LIST 10 |
| 186 |
typedef struct { |
| 187 |
CostInterval* head_; |
| 188 |
int count_; |
| 189 |
CostCacheInterval* cache_intervals_; |
| 190 |
size_t cache_intervals_size_; |
| 191 |
float cost_cache_[MAX_LENGTH]; |
| 192 |
float* costs_; |
| 193 |
uint16_t* dist_array_; |
| 194 |
|
| 195 |
|
| 196 |
CostInterval intervals_[COST_MANAGER_MAX_FREE_LIST]; |
| 197 |
CostInterval* free_intervals_; |
| 198 |
|
| 199 |
|
| 200 |
CostInterval* recycled_intervals_; |
| 201 |
} CostManager; |
| 202 |
|
| 203 |
static void CostIntervalAddToFreeList(CostManager* const manager, |
| 204 |
CostInterval* const interval) { |
| 205 |
interval->next_ = manager->free_intervals_; |
| 206 |
manager->free_intervals_ = interval; |
| 207 |
} |
| 208 |
|
| 209 |
static int CostIntervalIsInFreeList(const CostManager* const manager, |
| 210 |
const CostInterval* const interval) { |
| 211 |
return (interval >= &manager->intervals_[0] && |
| 212 |
interval <= &manager->intervals_[COST_MANAGER_MAX_FREE_LIST - 1]); |
| 213 |
} |
| 214 |
|
| 215 |
static void CostManagerInitFreeList(CostManager* const manager) { |
| 216 |
int i; |
| 217 |
manager->free_intervals_ = NULL; |
| 218 |
for (i = 0; i < COST_MANAGER_MAX_FREE_LIST; ++i) { |
| 219 |
CostIntervalAddToFreeList(manager, &manager->intervals_[i]); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
static void DeleteIntervalList(CostManager* const manager, |
| 224 |
const CostInterval* interval) { |
| 225 |
while (interval != NULL) { |
| 226 |
const CostInterval* const next = interval->next_; |
| 227 |
if (!CostIntervalIsInFreeList(manager, interval)) { |
| 228 |
WebPSafeFree((void*)interval); |
| 229 |
} |
| 230 |
interval = next; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static void CostManagerClear(CostManager* const manager) { |
| 235 |
if (manager == NULL) return; |
| 236 |
|
| 237 |
WebPSafeFree(manager->costs_); |
| 238 |
WebPSafeFree(manager->cache_intervals_); |
| 239 |
|
| 240 |
|
| 241 |
DeleteIntervalList(manager, manager->head_); |
| 242 |
manager->head_ = NULL; |
| 243 |
DeleteIntervalList(manager, manager->recycled_intervals_); |
| 244 |
manager->recycled_intervals_ = NULL; |
| 245 |
|
| 246 |
|
| 247 |
memset(manager, 0, sizeof(*manager)); |
| 248 |
CostManagerInitFreeList(manager); |
| 249 |
} |
| 250 |
|
| 251 |
static int CostManagerInit(CostManager* const manager, |
| 252 |
uint16_t* const dist_array, int pix_count, |
| 253 |
const CostModel* const cost_model) { |
| 254 |
int i; |
| 255 |
const int cost_cache_size = (pix_count > MAX_LENGTH) ? MAX_LENGTH : pix_count; |
| 256 |
|
| 257 |
manager->costs_ = NULL; |
| 258 |
manager->cache_intervals_ = NULL; |
| 259 |
manager->head_ = NULL; |
| 260 |
manager->recycled_intervals_ = NULL; |
| 261 |
manager->count_ = 0; |
| 262 |
manager->dist_array_ = dist_array; |
| 263 |
CostManagerInitFreeList(manager); |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
for (i = 0; i < cost_cache_size; ++i) { |
| 269 |
manager->cost_cache_[i] = GetLengthCost(cost_model, i); |
| 270 |
} |
| 271 |
manager->cache_intervals_size_ = 1; |
| 272 |
for (i = 1; i < cost_cache_size; ++i) { |
| 273 |
|
| 274 |
if (manager->cost_cache_[i] != manager->cost_cache_[i - 1]) { |
| 275 |
++manager->cache_intervals_size_; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
assert(manager->cache_intervals_size_ <= MAX_LENGTH); |
| 284 |
manager->cache_intervals_ = (CostCacheInterval*)WebPSafeMalloc( |
| 285 |
manager->cache_intervals_size_, sizeof(*manager->cache_intervals_)); |
| 286 |
if (manager->cache_intervals_ == NULL) { |
| 287 |
CostManagerClear(manager); |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
{ |
| 293 |
CostCacheInterval* cur = manager->cache_intervals_; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
cur->start_ = 0; |
| 298 |
cur->end_ = 1; |
| 299 |
cur->cost_ = manager->cost_cache_[0]; |
| 300 |
for (i = 1; i < cost_cache_size; ++i) { |
| 301 |
const float cost_val = manager->cost_cache_[i]; |
| 302 |
if (cost_val != cur->cost_) { |
| 303 |
++cur; |
| 304 |
|
| 305 |
cur->start_ = i; |
| 306 |
cur->cost_ = cost_val; |
| 307 |
} |
| 308 |
cur->end_ = i + 1; |
| 309 |
} |
| 310 |
assert((size_t)(cur - manager->cache_intervals_) + 1 == |
| 311 |
manager->cache_intervals_size_); |
| 312 |
} |
| 313 |
|
| 314 |
manager->costs_ = (float*)WebPSafeMalloc(pix_count, sizeof(*manager->costs_)); |
| 315 |
if (manager->costs_ == NULL) { |
| 316 |
CostManagerClear(manager); |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
for (i = 0; i < pix_count; ++i) manager->costs_[i] = FLT_MAX; |
| 321 |
|
| 322 |
return 1; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
static WEBP_INLINE void UpdateCost(CostManager* const manager, int i, |
| 328 |
int position, float cost) { |
| 329 |
const int k = i - position; |
| 330 |
assert(k >= 0 && k < MAX_LENGTH); |
| 331 |
|
| 332 |
if (manager->costs_[i] > cost) { |
| 333 |
manager->costs_[i] = cost; |
| 334 |
manager->dist_array_[i] = k + 1; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
static WEBP_INLINE void UpdateCostPerInterval(CostManager* const manager, |
| 341 |
int start, int end, int position, |
| 342 |
float cost) { |
| 343 |
int i; |
| 344 |
for (i = start; i < end; ++i) UpdateCost(manager, i, position, cost); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
static WEBP_INLINE void ConnectIntervals(CostManager* const manager, |
| 349 |
CostInterval* const prev, |
| 350 |
CostInterval* const next) { |
| 351 |
if (prev != NULL) { |
| 352 |
prev->next_ = next; |
| 353 |
} else { |
| 354 |
manager->head_ = next; |
| 355 |
} |
| 356 |
|
| 357 |
if (next != NULL) next->previous_ = prev; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
static WEBP_INLINE void PopInterval(CostManager* const manager, |
| 362 |
CostInterval* const interval) { |
| 363 |
if (interval == NULL) return; |
| 364 |
|
| 365 |
ConnectIntervals(manager, interval->previous_, interval->next_); |
| 366 |
if (CostIntervalIsInFreeList(manager, interval)) { |
| 367 |
CostIntervalAddToFreeList(manager, interval); |
| 368 |
} else { |
| 369 |
interval->next_ = manager->recycled_intervals_; |
| 370 |
manager->recycled_intervals_ = interval; |
| 371 |
} |
| 372 |
--manager->count_; |
| 373 |
assert(manager->count_ >= 0); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
static WEBP_INLINE void UpdateCostAtIndex(CostManager* const manager, int i, |
| 381 |
int do_clean_intervals) { |
| 382 |
CostInterval* current = manager->head_; |
| 383 |
|
| 384 |
while (current != NULL && current->start_ <= i) { |
| 385 |
CostInterval* const next = current->next_; |
| 386 |
if (current->end_ <= i) { |
| 387 |
if (do_clean_intervals) { |
| 388 |
|
| 389 |
PopInterval(manager, current); |
| 390 |
} |
| 391 |
} else { |
| 392 |
UpdateCost(manager, i, current->index_, current->cost_); |
| 393 |
} |
| 394 |
current = next; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
static WEBP_INLINE void PositionOrphanInterval(CostManager* const manager, |
| 402 |
CostInterval* const current, |
| 403 |
CostInterval* previous) { |
| 404 |
assert(current != NULL); |
| 405 |
|
| 406 |
if (previous == NULL) previous = manager->head_; |
| 407 |
while (previous != NULL && current->start_ < previous->start_) { |
| 408 |
previous = previous->previous_; |
| 409 |
} |
| 410 |
while (previous != NULL && previous->next_ != NULL && |
| 411 |
previous->next_->start_ < current->start_) { |
| 412 |
previous = previous->next_; |
| 413 |
} |
| 414 |
|
| 415 |
if (previous != NULL) { |
| 416 |
ConnectIntervals(manager, current, previous->next_); |
| 417 |
} else { |
| 418 |
ConnectIntervals(manager, current, manager->head_); |
| 419 |
} |
| 420 |
ConnectIntervals(manager, previous, current); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
static WEBP_INLINE void InsertInterval(CostManager* const manager, |
| 426 |
CostInterval* const interval_in, |
| 427 |
float cost, int position, int start, |
| 428 |
int end) { |
| 429 |
CostInterval* interval_new; |
| 430 |
|
| 431 |
if (start >= end) return; |
| 432 |
if (manager->count_ >= COST_CACHE_INTERVAL_SIZE_MAX) { |
| 433 |
|
| 434 |
UpdateCostPerInterval(manager, start, end, position, cost); |
| 435 |
return; |
| 436 |
} |
| 437 |
if (manager->free_intervals_ != NULL) { |
| 438 |
interval_new = manager->free_intervals_; |
| 439 |
manager->free_intervals_ = interval_new->next_; |
| 440 |
} else if (manager->recycled_intervals_ != NULL) { |
| 441 |
interval_new = manager->recycled_intervals_; |
| 442 |
manager->recycled_intervals_ = interval_new->next_; |
| 443 |
} else { |
| 444 |
interval_new = (CostInterval*)WebPSafeMalloc(1, sizeof(*interval_new)); |
| 445 |
if (interval_new == NULL) { |
| 446 |
|
| 447 |
UpdateCostPerInterval(manager, start, end, position, cost); |
| 448 |
return; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
interval_new->cost_ = cost; |
| 453 |
interval_new->index_ = position; |
| 454 |
interval_new->start_ = start; |
| 455 |
interval_new->end_ = end; |
| 456 |
PositionOrphanInterval(manager, interval_new, interval_in); |
| 457 |
|
| 458 |
++manager->count_; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
static WEBP_INLINE void PushInterval(CostManager* const manager, |
| 466 |
float distance_cost, int position, |
| 467 |
int len) { |
| 468 |
size_t i; |
| 469 |
CostInterval* interval = manager->head_; |
| 470 |
CostInterval* interval_next; |
| 471 |
const CostCacheInterval* const cost_cache_intervals = |
| 472 |
manager->cache_intervals_; |
| 473 |
|
| 474 |
|
| 475 |
const int kSkipDistance = 10; |
| 476 |
|
| 477 |
if (len < kSkipDistance) { |
| 478 |
int j; |
| 479 |
for (j = position; j < position + len; ++j) { |
| 480 |
const int k = j - position; |
| 481 |
float cost_tmp; |
| 482 |
assert(k >= 0 && k < MAX_LENGTH); |
| 483 |
cost_tmp = distance_cost + manager->cost_cache_[k]; |
| 484 |
|
| 485 |
if (manager->costs_[j] > cost_tmp) { |
| 486 |
manager->costs_[j] = cost_tmp; |
| 487 |
manager->dist_array_[j] = k + 1; |
| 488 |
} |
| 489 |
} |
| 490 |
return; |
| 491 |
} |
| 492 |
|
| 493 |
for (i = 0; i < manager->cache_intervals_size_ && |
| 494 |
cost_cache_intervals[i].start_ < len; |
| 495 |
++i) { |
| 496 |
|
| 497 |
int start = position + cost_cache_intervals[i].start_; |
| 498 |
const int end = position + (cost_cache_intervals[i].end_ > len |
| 499 |
? len |
| 500 |
: cost_cache_intervals[i].end_); |
| 501 |
const float cost = distance_cost + cost_cache_intervals[i].cost_; |
| 502 |
|
| 503 |
for (; interval != NULL && interval->start_ < end; |
| 504 |
interval = interval_next) { |
| 505 |
interval_next = interval->next_; |
| 506 |
|
| 507 |
|
| 508 |
if (start >= interval->end_) continue; |
| 509 |
|
| 510 |
if (cost >= interval->cost_) { |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
const int start_new = interval->end_; |
| 519 |
InsertInterval(manager, interval, cost, position, start, |
| 520 |
interval->start_); |
| 521 |
start = start_new; |
| 522 |
if (start >= end) break; |
| 523 |
continue; |
| 524 |
} |
| 525 |
|
| 526 |
if (start <= interval->start_) { |
| 527 |
if (interval->end_ <= end) { |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
PopInterval(manager, interval); |
| 534 |
} else { |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
interval->start_ = end; |
| 540 |
break; |
| 541 |
} |
| 542 |
} else { |
| 543 |
if (end < interval->end_) { |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
const int end_original = interval->end_; |
| 550 |
interval->end_ = start; |
| 551 |
InsertInterval(manager, interval, interval->cost_, interval->index_, |
| 552 |
end, end_original); |
| 553 |
interval = interval->next_; |
| 554 |
break; |
| 555 |
} else { |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
interval->end_ = start; |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
InsertInterval(manager, interval, cost, position, start, end); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
static int BackwardReferencesHashChainDistanceOnly( |
| 570 |
int xsize, int ysize, const uint32_t* const argb, int cache_bits, |
| 571 |
const VP8LHashChain* const hash_chain, const VP8LBackwardRefs* const refs, |
| 572 |
uint16_t* const dist_array) { |
| 573 |
int i; |
| 574 |
int ok = 0; |
| 575 |
int cc_init = 0; |
| 576 |
const int pix_count = xsize * ysize; |
| 577 |
const int use_color_cache = (cache_bits > 0); |
| 578 |
const size_t literal_array_size = |
| 579 |
sizeof(float) * (VP8LHistogramNumCodes(cache_bits)); |
| 580 |
const size_t cost_model_size = sizeof(CostModel) + literal_array_size; |
| 581 |
CostModel* const cost_model = |
| 582 |
(CostModel*)WebPSafeCalloc(1ULL, cost_model_size); |
| 583 |
VP8LColorCache hashers; |
| 584 |
CostManager* cost_manager = |
| 585 |
(CostManager*)WebPSafeCalloc(1ULL, sizeof(*cost_manager)); |
| 586 |
int offset_prev = -1, len_prev = -1; |
| 587 |
float offset_cost = -1.f; |
| 588 |
int first_offset_is_constant = -1; |
| 589 |
int reach = 0; |
| 590 |
|
| 591 |
if (cost_model == NULL || cost_manager == NULL) goto Error; |
| 592 |
|
| 593 |
cost_model->literal_ = (float*)(cost_model + 1); |
| 594 |
if (use_color_cache) { |
| 595 |
cc_init = VP8LColorCacheInit(&hashers, cache_bits); |
| 596 |
if (!cc_init) goto Error; |
| 597 |
} |
| 598 |
|
| 599 |
if (!CostModelBuild(cost_model, xsize, cache_bits, refs)) { |
| 600 |
goto Error; |
| 601 |
} |
| 602 |
|
| 603 |
if (!CostManagerInit(cost_manager, dist_array, pix_count, cost_model)) { |
| 604 |
goto Error; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
dist_array[0] = 0; |
| 610 |
|
| 611 |
AddSingleLiteralWithCostModel(argb, &hashers, cost_model, 0, use_color_cache, |
| 612 |
0.f, cost_manager->costs_, dist_array); |
| 613 |
|
| 614 |
for (i = 1; i < pix_count; ++i) { |
| 615 |
const float prev_cost = cost_manager->costs_[i - 1]; |
| 616 |
int offset, len; |
| 617 |
VP8LHashChainFindCopy(hash_chain, i, &offset, &len); |
| 618 |
|
| 619 |
|
| 620 |
AddSingleLiteralWithCostModel(argb, &hashers, cost_model, i, |
| 621 |
use_color_cache, prev_cost, |
| 622 |
cost_manager->costs_, dist_array); |
| 623 |
|
| 624 |
|
| 625 |
if (len >= 2) { |
| 626 |
if (offset != offset_prev) { |
| 627 |
const int code = VP8LDistanceToPlaneCode(xsize, offset); |
| 628 |
offset_cost = GetDistanceCost(cost_model, code); |
| 629 |
first_offset_is_constant = 1; |
| 630 |
PushInterval(cost_manager, prev_cost + offset_cost, i, len); |
| 631 |
} else { |
| 632 |
assert(offset_cost >= 0); |
| 633 |
assert(len_prev >= 0); |
| 634 |
assert(first_offset_is_constant == 0 || first_offset_is_constant == 1); |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
if (first_offset_is_constant) { |
| 641 |
reach = i - 1 + len_prev - 1; |
| 642 |
first_offset_is_constant = 0; |
| 643 |
} |
| 644 |
|
| 645 |
if (i + len - 1 > reach) { |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
int offset_j, len_j = 0; |
| 655 |
int j; |
| 656 |
assert(len == MAX_LENGTH || len == pix_count - i); |
| 657 |
|
| 658 |
|
| 659 |
for (j = i; j <= reach; ++j) { |
| 660 |
VP8LHashChainFindCopy(hash_chain, j + 1, &offset_j, &len_j); |
| 661 |
if (offset_j != offset) { |
| 662 |
VP8LHashChainFindCopy(hash_chain, j, &offset_j, &len_j); |
| 663 |
break; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
UpdateCostAtIndex(cost_manager, j - 1, 0); |
| 668 |
UpdateCostAtIndex(cost_manager, j, 0); |
| 669 |
|
| 670 |
PushInterval(cost_manager, cost_manager->costs_[j - 1] + offset_cost, |
| 671 |
j, len_j); |
| 672 |
reach = j + len_j - 1; |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
UpdateCostAtIndex(cost_manager, i, 1); |
| 678 |
offset_prev = offset; |
| 679 |
len_prev = len; |
| 680 |
} |
| 681 |
|
| 682 |
ok = !refs->error_; |
| 683 |
Error: |
| 684 |
if (cc_init) VP8LColorCacheClear(&hashers); |
| 685 |
CostManagerClear(cost_manager); |
| 686 |
WebPSafeFree(cost_model); |
| 687 |
WebPSafeFree(cost_manager); |
| 688 |
return ok; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
static void TraceBackwards(uint16_t* const dist_array, |
| 695 |
int dist_array_size, |
| 696 |
uint16_t** const chosen_path, |
| 697 |
int* const chosen_path_size) { |
| 698 |
uint16_t* path = dist_array + dist_array_size; |
| 699 |
uint16_t* cur = dist_array + dist_array_size - 1; |
| 700 |
while (cur >= dist_array) { |
| 701 |
const int k = *cur; |
| 702 |
--path; |
| 703 |
*path = k; |
| 704 |
cur -= k; |
| 705 |
} |
| 706 |
*chosen_path = path; |
| 707 |
*chosen_path_size = (int)(dist_array + dist_array_size - path); |
| 708 |
} |
| 709 |
|
| 710 |
static int BackwardReferencesHashChainFollowChosenPath( |
| 711 |
const uint32_t* const argb, int cache_bits, |
| 712 |
const uint16_t* const chosen_path, int chosen_path_size, |
| 713 |
const VP8LHashChain* const hash_chain, VP8LBackwardRefs* const refs) { |
| 714 |
const int use_color_cache = (cache_bits > 0); |
| 715 |
int ix; |
| 716 |
int i = 0; |
| 717 |
int ok = 0; |
| 718 |
int cc_init = 0; |
| 719 |
VP8LColorCache hashers; |
| 720 |
|
| 721 |
if (use_color_cache) { |
| 722 |
cc_init = VP8LColorCacheInit(&hashers, cache_bits); |
| 723 |
if (!cc_init) goto Error; |
| 724 |
} |
| 725 |
|
| 726 |
VP8LClearBackwardRefs(refs); |
| 727 |
for (ix = 0; ix < chosen_path_size; ++ix) { |
| 728 |
const int len = chosen_path[ix]; |
| 729 |
if (len != 1) { |
| 730 |
int k; |
| 731 |
const int offset = VP8LHashChainFindOffset(hash_chain, i); |
| 732 |
VP8LBackwardRefsCursorAdd(refs, PixOrCopyCreateCopy(offset, len)); |
| 733 |
if (use_color_cache) { |
| 734 |
for (k = 0; k < len; ++k) { |
| 735 |
VP8LColorCacheInsert(&hashers, argb[i + k]); |
| 736 |
} |
| 737 |
} |
| 738 |
i += len; |
| 739 |
} else { |
| 740 |
PixOrCopy v; |
| 741 |
const int idx = |
| 742 |
use_color_cache ? VP8LColorCacheContains(&hashers, argb[i]) : -1; |
| 743 |
if (idx >= 0) { |
| 744 |
|
| 745 |
|
| 746 |
v = PixOrCopyCreateCacheIdx(idx); |
| 747 |
} else { |
| 748 |
if (use_color_cache) VP8LColorCacheInsert(&hashers, argb[i]); |
| 749 |
v = PixOrCopyCreateLiteral(argb[i]); |
| 750 |
} |
| 751 |
VP8LBackwardRefsCursorAdd(refs, v); |
| 752 |
++i; |
| 753 |
} |
| 754 |
} |
| 755 |
ok = !refs->error_; |
| 756 |
Error: |
| 757 |
if (cc_init) VP8LColorCacheClear(&hashers); |
| 758 |
return ok; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
extern int VP8LBackwardReferencesTraceBackwards( |
| 763 |
int xsize, int ysize, const uint32_t* const argb, int cache_bits, |
| 764 |
const VP8LHashChain* const hash_chain, |
| 765 |
const VP8LBackwardRefs* const refs_src, VP8LBackwardRefs* const refs_dst); |
| 766 |
int VP8LBackwardReferencesTraceBackwards(int xsize, int ysize, |
| 767 |
const uint32_t* const argb, |
| 768 |
int cache_bits, |
| 769 |
const VP8LHashChain* const hash_chain, |
| 770 |
const VP8LBackwardRefs* const refs_src, |
| 771 |
VP8LBackwardRefs* const refs_dst) { |
| 772 |
int ok = 0; |
| 773 |
const int dist_array_size = xsize * ysize; |
| 774 |
uint16_t* chosen_path = NULL; |
| 775 |
int chosen_path_size = 0; |
| 776 |
uint16_t* dist_array = |
| 777 |
(uint16_t*)WebPSafeMalloc(dist_array_size, sizeof(*dist_array)); |
| 778 |
|
| 779 |
if (dist_array == NULL) goto Error; |
| 780 |
|
| 781 |
if (!BackwardReferencesHashChainDistanceOnly( |
| 782 |
xsize, ysize, argb, cache_bits, hash_chain, refs_src, dist_array)) { |
| 783 |
goto Error; |
| 784 |
} |
| 785 |
TraceBackwards(dist_array, dist_array_size, &chosen_path, &chosen_path_size); |
| 786 |
if (!BackwardReferencesHashChainFollowChosenPath( |
| 787 |
argb, cache_bits, chosen_path, chosen_path_size, hash_chain, |
| 788 |
refs_dst)) { |
| 789 |
goto Error; |
| 790 |
} |
| 791 |
ok = 1; |
| 792 |
Error: |
| 793 |
WebPSafeFree(dist_array); |
| 794 |
return ok; |
| 795 |
} |