quant.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2010 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. // Quantizer initialization
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./vp8i.h"
  14. static WEBP_INLINE int clip(int v, int M) {
  15. return v < 0 ? 0 : v > M ? M : v;
  16. }
  17. // Paragraph 14.1
  18. static const uint8_t kDcTable[128] = {
  19. 4, 5, 6, 7, 8, 9, 10, 10,
  20. 11, 12, 13, 14, 15, 16, 17, 17,
  21. 18, 19, 20, 20, 21, 21, 22, 22,
  22. 23, 23, 24, 25, 25, 26, 27, 28,
  23. 29, 30, 31, 32, 33, 34, 35, 36,
  24. 37, 37, 38, 39, 40, 41, 42, 43,
  25. 44, 45, 46, 46, 47, 48, 49, 50,
  26. 51, 52, 53, 54, 55, 56, 57, 58,
  27. 59, 60, 61, 62, 63, 64, 65, 66,
  28. 67, 68, 69, 70, 71, 72, 73, 74,
  29. 75, 76, 76, 77, 78, 79, 80, 81,
  30. 82, 83, 84, 85, 86, 87, 88, 89,
  31. 91, 93, 95, 96, 98, 100, 101, 102,
  32. 104, 106, 108, 110, 112, 114, 116, 118,
  33. 122, 124, 126, 128, 130, 132, 134, 136,
  34. 138, 140, 143, 145, 148, 151, 154, 157
  35. };
  36. static const uint16_t kAcTable[128] = {
  37. 4, 5, 6, 7, 8, 9, 10, 11,
  38. 12, 13, 14, 15, 16, 17, 18, 19,
  39. 20, 21, 22, 23, 24, 25, 26, 27,
  40. 28, 29, 30, 31, 32, 33, 34, 35,
  41. 36, 37, 38, 39, 40, 41, 42, 43,
  42. 44, 45, 46, 47, 48, 49, 50, 51,
  43. 52, 53, 54, 55, 56, 57, 58, 60,
  44. 62, 64, 66, 68, 70, 72, 74, 76,
  45. 78, 80, 82, 84, 86, 88, 90, 92,
  46. 94, 96, 98, 100, 102, 104, 106, 108,
  47. 110, 112, 114, 116, 119, 122, 125, 128,
  48. 131, 134, 137, 140, 143, 146, 149, 152,
  49. 155, 158, 161, 164, 167, 170, 173, 177,
  50. 181, 185, 189, 193, 197, 201, 205, 209,
  51. 213, 217, 221, 225, 229, 234, 239, 245,
  52. 249, 254, 259, 264, 269, 274, 279, 284
  53. };
  54. //------------------------------------------------------------------------------
  55. // Paragraph 9.6
  56. void VP8ParseQuant(VP8Decoder* const dec) {
  57. VP8BitReader* const br = &dec->br_;
  58. const int base_q0 = VP8GetValue(br, 7);
  59. const int dqy1_dc = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0;
  60. const int dqy2_dc = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0;
  61. const int dqy2_ac = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0;
  62. const int dquv_dc = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0;
  63. const int dquv_ac = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0;
  64. const VP8SegmentHeader* const hdr = &dec->segment_hdr_;
  65. int i;
  66. for (i = 0; i < NUM_MB_SEGMENTS; ++i) {
  67. int q;
  68. if (hdr->use_segment_) {
  69. q = hdr->quantizer_[i];
  70. if (!hdr->absolute_delta_) {
  71. q += base_q0;
  72. }
  73. } else {
  74. if (i > 0) {
  75. dec->dqm_[i] = dec->dqm_[0];
  76. continue;
  77. } else {
  78. q = base_q0;
  79. }
  80. }
  81. {
  82. VP8QuantMatrix* const m = &dec->dqm_[i];
  83. m->y1_mat_[0] = kDcTable[clip(q + dqy1_dc, 127)];
  84. m->y1_mat_[1] = kAcTable[clip(q + 0, 127)];
  85. m->y2_mat_[0] = kDcTable[clip(q + dqy2_dc, 127)] * 2;
  86. // For all x in [0..284], x*155/100 is bitwise equal to (x*101581) >> 16.
  87. // The smallest precision for that is '(x*6349) >> 12' but 16 is a good
  88. // word size.
  89. m->y2_mat_[1] = (kAcTable[clip(q + dqy2_ac, 127)] * 101581) >> 16;
  90. if (m->y2_mat_[1] < 8) m->y2_mat_[1] = 8;
  91. m->uv_mat_[0] = kDcTable[clip(q + dquv_dc, 117)];
  92. m->uv_mat_[1] = kAcTable[clip(q + dquv_ac, 127)];
  93. m->uv_quant_ = q + dquv_ac; // for dithering strength evaluation
  94. }
  95. }
  96. }
  97. //------------------------------------------------------------------------------