quant.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package vp8
  5. // This file implements parsing the quantization factors.
  6. // quant are DC/AC quantization factors.
  7. type quant struct {
  8. y1 [2]uint16
  9. y2 [2]uint16
  10. uv [2]uint16
  11. }
  12. // clip clips x to the range [min, max] inclusive.
  13. func clip(x, min, max int32) int32 {
  14. if x < min {
  15. return min
  16. }
  17. if x > max {
  18. return max
  19. }
  20. return x
  21. }
  22. // parseQuant parses the quantization factors, as specified in section 9.6.
  23. func (d *Decoder) parseQuant() {
  24. baseQ0 := d.fp.readUint(uniformProb, 7)
  25. dqy1DC := d.fp.readOptionalInt(uniformProb, 4)
  26. const dqy1AC = 0
  27. dqy2DC := d.fp.readOptionalInt(uniformProb, 4)
  28. dqy2AC := d.fp.readOptionalInt(uniformProb, 4)
  29. dquvDC := d.fp.readOptionalInt(uniformProb, 4)
  30. dquvAC := d.fp.readOptionalInt(uniformProb, 4)
  31. for i := 0; i < nSegment; i++ {
  32. q := int32(baseQ0)
  33. if d.segmentHeader.useSegment {
  34. if d.segmentHeader.relativeDelta {
  35. q += int32(d.segmentHeader.quantizer[i])
  36. } else {
  37. q = int32(d.segmentHeader.quantizer[i])
  38. }
  39. }
  40. d.quant[i].y1[0] = dequantTableDC[clip(q+dqy1DC, 0, 127)]
  41. d.quant[i].y1[1] = dequantTableAC[clip(q+dqy1AC, 0, 127)]
  42. d.quant[i].y2[0] = dequantTableDC[clip(q+dqy2DC, 0, 127)] * 2
  43. d.quant[i].y2[1] = dequantTableAC[clip(q+dqy2AC, 0, 127)] * 155 / 100
  44. if d.quant[i].y2[1] < 8 {
  45. d.quant[i].y2[1] = 8
  46. }
  47. // The 117 is not a typo. The dequant_init function in the spec's Reference
  48. // Decoder Source Code (http://tools.ietf.org/html/rfc6386#section-9.6 Page 145)
  49. // says to clamp the LHS value at 132, which is equal to dequantTableDC[117].
  50. d.quant[i].uv[0] = dequantTableDC[clip(q+dquvDC, 0, 117)]
  51. d.quant[i].uv[1] = dequantTableAC[clip(q+dquvAC, 0, 127)]
  52. }
  53. }
  54. // The dequantization tables are specified in section 14.1.
  55. var (
  56. dequantTableDC = [128]uint16{
  57. 4, 5, 6, 7, 8, 9, 10, 10,
  58. 11, 12, 13, 14, 15, 16, 17, 17,
  59. 18, 19, 20, 20, 21, 21, 22, 22,
  60. 23, 23, 24, 25, 25, 26, 27, 28,
  61. 29, 30, 31, 32, 33, 34, 35, 36,
  62. 37, 37, 38, 39, 40, 41, 42, 43,
  63. 44, 45, 46, 46, 47, 48, 49, 50,
  64. 51, 52, 53, 54, 55, 56, 57, 58,
  65. 59, 60, 61, 62, 63, 64, 65, 66,
  66. 67, 68, 69, 70, 71, 72, 73, 74,
  67. 75, 76, 76, 77, 78, 79, 80, 81,
  68. 82, 83, 84, 85, 86, 87, 88, 89,
  69. 91, 93, 95, 96, 98, 100, 101, 102,
  70. 104, 106, 108, 110, 112, 114, 116, 118,
  71. 122, 124, 126, 128, 130, 132, 134, 136,
  72. 138, 140, 143, 145, 148, 151, 154, 157,
  73. }
  74. dequantTableAC = [128]uint16{
  75. 4, 5, 6, 7, 8, 9, 10, 11,
  76. 12, 13, 14, 15, 16, 17, 18, 19,
  77. 20, 21, 22, 23, 24, 25, 26, 27,
  78. 28, 29, 30, 31, 32, 33, 34, 35,
  79. 36, 37, 38, 39, 40, 41, 42, 43,
  80. 44, 45, 46, 47, 48, 49, 50, 51,
  81. 52, 53, 54, 55, 56, 57, 58, 60,
  82. 62, 64, 66, 68, 70, 72, 74, 76,
  83. 78, 80, 82, 84, 86, 88, 90, 92,
  84. 94, 96, 98, 100, 102, 104, 106, 108,
  85. 110, 112, 114, 116, 119, 122, 125, 128,
  86. 131, 134, 137, 140, 143, 146, 149, 152,
  87. 155, 158, 161, 164, 167, 170, 173, 177,
  88. 181, 185, 189, 193, 197, 201, 205, 209,
  89. 213, 217, 221, 225, 229, 234, 239, 245,
  90. 249, 254, 259, 264, 269, 274, 279, 284,
  91. }
  92. )