cost_enc.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Cost tables for level and modes.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #ifndef WEBP_ENC_COST_ENC_H_
  14. #define WEBP_ENC_COST_ENC_H_
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include "./vp8i_enc.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. // On-the-fly info about the current set of residuals. Handy to avoid
  22. // passing zillions of params.
  23. typedef struct VP8Residual VP8Residual;
  24. struct VP8Residual {
  25. int first;
  26. int last;
  27. const int16_t* coeffs;
  28. int coeff_type;
  29. ProbaArray* prob;
  30. StatsArray* stats;
  31. CostArrayPtr costs;
  32. };
  33. void VP8InitResidual(int first, int coeff_type,
  34. VP8Encoder* const enc, VP8Residual* const res);
  35. int VP8RecordCoeffs(int ctx, const VP8Residual* const res);
  36. // Record proba context used.
  37. static WEBP_INLINE int VP8RecordStats(int bit, proba_t* const stats) {
  38. proba_t p = *stats;
  39. // An overflow is inbound. Note we handle this at 0xfffe0000u instead of
  40. // 0xffff0000u to make sure p + 1u does not overflow.
  41. if (p >= 0xfffe0000u) {
  42. p = ((p + 1u) >> 1) & 0x7fff7fffu; // -> divide the stats by 2.
  43. }
  44. // record bit count (lower 16 bits) and increment total count (upper 16 bits).
  45. p += 0x00010000u + bit;
  46. *stats = p;
  47. return bit;
  48. }
  49. // Cost of coding one event with probability 'proba'.
  50. static WEBP_INLINE int VP8BitCost(int bit, uint8_t proba) {
  51. return !bit ? VP8EntropyCost[proba] : VP8EntropyCost[255 - proba];
  52. }
  53. // Level cost calculations
  54. extern const uint16_t VP8LevelCodes[MAX_VARIABLE_LEVEL][2];
  55. void VP8CalculateLevelCosts(VP8EncProba* const proba);
  56. static WEBP_INLINE int VP8LevelCost(const uint16_t* const table, int level) {
  57. return VP8LevelFixedCosts[level]
  58. + table[(level > MAX_VARIABLE_LEVEL) ? MAX_VARIABLE_LEVEL : level];
  59. }
  60. // Mode costs
  61. extern const uint16_t VP8FixedCostsUV[4];
  62. extern const uint16_t VP8FixedCostsI16[4];
  63. extern const uint16_t VP8FixedCostsI4[NUM_BMODES][NUM_BMODES][NUM_BMODES];
  64. //------------------------------------------------------------------------------
  65. #ifdef __cplusplus
  66. } // extern "C"
  67. #endif
  68. #endif // WEBP_ENC_COST_ENC_H_