ac.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Direct Stream Transfer (DST) codec
  3. * ISO/IEC 14496-3 Part 3 Subpart 10: Technical description of lossless coding of oversampled audio
  4. */
  5. #ifndef AC_H
  6. #define AC_H
  7. #include <stdint.h>
  8. #include "common.h"
  9. namespace dst {
  10. constexpr int PBITS = AC_BITS; // number of bits for Probabilities
  11. constexpr int NBITS = 4; // number of overhead bits: must be at least 2!
  12. // maximum "variable shift length" is (NBITS-1)
  13. constexpr int PSUM = 1 << PBITS;
  14. constexpr int ABITS = PBITS + NBITS; // must be at least PBITS+2
  15. constexpr int MB = 0; // if (MB) print max buffer use
  16. constexpr int ONE = 1 << ABITS;
  17. constexpr int HALF = 1 << (ABITS - 1);
  18. class ac_t {
  19. unsigned int Init;
  20. unsigned int C;
  21. unsigned int A;
  22. int cbptr;
  23. public:
  24. unsigned int getPtableIndex(int PredicVal, int PtableLen) {
  25. int j;
  26. j = (PredicVal > 0 ? PredicVal : -PredicVal) >> AC_QSTEP;
  27. if (j >= PtableLen) {
  28. j = PtableLen - 1;
  29. }
  30. return (unsigned int)j;
  31. }
  32. void decodeBit(uint8_t& b, int p, uint8_t* cb, int fs, int flush) {
  33. unsigned int ap;
  34. unsigned int h;
  35. if (Init == 1) {
  36. Init = 0;
  37. A = ONE - 1;
  38. C = 0;
  39. for (cbptr = 1; cbptr <= ABITS; cbptr++) {
  40. C <<= 1;
  41. if (cbptr < fs) {
  42. C |= cb[cbptr];
  43. }
  44. }
  45. }
  46. if (flush == 0) {
  47. // approximate (A * p) with "partial rounding"
  48. ap = ((A >> PBITS) | ((A >> (PBITS - 1)) & 1))* p;
  49. h = A - ap;
  50. if (C >= h) {
  51. b = 0;
  52. C -= h;
  53. A = ap;
  54. }
  55. else {
  56. b = 1;
  57. A = h;
  58. }
  59. while (A < HALF) {
  60. A <<= 1;
  61. // Use new flushing technique; insert zero in LSB of C if reading past the end of the arithmetic code
  62. C <<= 1;
  63. if (cbptr < fs) {
  64. C |= cb[cbptr];
  65. }
  66. cbptr++;
  67. }
  68. }
  69. else {
  70. Init = 1;
  71. if (cbptr < fs - 7) {
  72. b = 0;
  73. }
  74. else {
  75. b = 1;
  76. while ((cbptr < fs) && (b == 1)) {
  77. if (cb[cbptr] != 0) {
  78. b = 1;
  79. }
  80. cbptr++;
  81. }
  82. }
  83. }
  84. }
  85. void decodeBit_Init(uint8_t* cb, int fs) {
  86. Init = 0;
  87. A = ONE - 1;
  88. C = 0;
  89. for (cbptr = 1; cbptr <= ABITS; cbptr++) {
  90. C <<= 1;
  91. if (cbptr < fs) {
  92. C |= GET_BIT(cb, cbptr);
  93. }
  94. }
  95. }
  96. void decodeBit_Decode(uint8_t* b, int p, uint8_t* cb, int fs) {
  97. unsigned int ap;
  98. unsigned int h;
  99. // approximate (A * p) with "partial rounding"
  100. ap = ((A >> PBITS) | ((A >> (PBITS - 1)) & 1))* p;
  101. h = A - ap;
  102. if (C >= h) {
  103. *b = 0;
  104. C -= h;
  105. A = ap;
  106. }
  107. else {
  108. *b = 1;
  109. A = h;
  110. }
  111. while (A < HALF) {
  112. A <<= 1;
  113. // Use new flushing technique; insert zero in LSB of C if reading past the end of the arithmetic code
  114. C <<= 1;
  115. if (cbptr < fs) {
  116. C |= GET_BIT(cb, cbptr);
  117. }
  118. cbptr++;
  119. }
  120. }
  121. void decodeBit_Flush(uint8_t* b, int p, uint8_t* cb, int fs) {
  122. (void)p;
  123. Init = 1;
  124. if (cbptr < fs - 7) {
  125. *b = 0;
  126. }
  127. else {
  128. *b = 1;
  129. while ((cbptr < fs) && (*b == 1)) {
  130. if (GET_BIT(cb, cbptr) != 0) {
  131. *b = 1;
  132. }
  133. cbptr++;
  134. }
  135. }
  136. }
  137. };
  138. }
  139. #endif