You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
8.9KB

  1. /*
  2. * bit_allocate.c
  3. *
  4. * Copyright (C) Aaron Holtzman - May 1999
  5. *
  6. * This file is part of ac3dec, a free Dolby AC-3 stream decoder.
  7. *
  8. * ac3dec is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * ac3dec is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Make; see the file COPYING. If not, write to
  20. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. */
  23. #include "../common.h"
  24. #include "ac3.h"
  25. #include "ac3_internal.h"
  26. static int hthtab[3][50] = {
  27. {0x730, 0x730, 0x7c0, 0x800, 0x820, 0x840, 0x850, 0x850, 0x860, 0x860,
  28. 0x860, 0x860, 0x860, 0x870, 0x870, 0x870, 0x880, 0x880, 0x890, 0x890,
  29. 0x8a0, 0x8a0, 0x8b0, 0x8b0, 0x8c0, 0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x900,
  30. 0x910, 0x910, 0x910, 0x910, 0x900, 0x8f0, 0x8c0, 0x870, 0x820, 0x7e0,
  31. 0x7a0, 0x770, 0x760, 0x7a0, 0x7c0, 0x7c0, 0x6e0, 0x400, 0x3c0, 0x3c0},
  32. {0x710, 0x710, 0x7a0, 0x7f0, 0x820, 0x830, 0x840, 0x850, 0x850, 0x860,
  33. 0x860, 0x860, 0x860, 0x860, 0x870, 0x870, 0x870, 0x880, 0x880, 0x880,
  34. 0x890, 0x890, 0x8a0, 0x8a0, 0x8b0, 0x8b0, 0x8c0, 0x8c0, 0x8e0, 0x8f0,
  35. 0x900, 0x910, 0x910, 0x910, 0x910, 0x900, 0x8e0, 0x8b0, 0x870, 0x820,
  36. 0x7e0, 0x7b0, 0x760, 0x770, 0x7a0, 0x7c0, 0x780, 0x5d0, 0x3c0, 0x3c0},
  37. {0x680, 0x680, 0x750, 0x7b0, 0x7e0, 0x810, 0x820, 0x830, 0x840, 0x850,
  38. 0x850, 0x850, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860,
  39. 0x870, 0x870, 0x870, 0x870, 0x880, 0x880, 0x880, 0x890, 0x8a0, 0x8b0,
  40. 0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x900, 0x910, 0x910, 0x910, 0x900, 0x8f0,
  41. 0x8d0, 0x8b0, 0x840, 0x7f0, 0x790, 0x760, 0x7a0, 0x7c0, 0x7b0, 0x720}
  42. };
  43. static int8_t baptab[305] = {
  44. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  45. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  46. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  47. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  48. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  49. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, // 93 padding entries
  50. 16, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14,
  51. 14, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9,
  52. 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5,
  53. 5, 4, 4, -3, -3, 3, 3, 3, -2, -2, -1, -1, -1, -1, -1, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  63. 0, 0, 0, 0 // 148 padding entries
  64. };
  65. static int bndtab[30] = {21, 22, 23, 24, 25, 26, 27, 28, 31, 34,
  66. 37, 40, 43, 46, 49, 55, 61, 67, 73, 79,
  67. 85, 97, 109, 121, 133, 157, 181, 205, 229, 253};
  68. static int8_t latab[256] = {
  69. -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53,
  70. -52, -52, -51, -50, -49, -48, -47, -47, -46, -45, -44, -44,
  71. -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35,
  72. -35, -34, -33, -33, -32, -32, -31, -30, -30, -29, -29, -28,
  73. -28, -27, -27, -26, -26, -25, -25, -24, -24, -23, -23, -22,
  74. -22, -21, -21, -21, -20, -20, -19, -19, -19, -18, -18, -18,
  75. -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14,
  76. -13, -13, -13, -13, -12, -12, -12, -12, -11, -11, -11, -11,
  77. -10, -10, -10, -10, -10, -9, -9, -9, -9, -9, -8, -8,
  78. -8, -8, -8, -8, -7, -7, -7, -7, -7, -7, -6, -6,
  79. -6, -6, -6, -6, -6, -6, -5, -5, -5, -5, -5, -5,
  80. -5, -5, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  81. -4, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
  82. -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  83. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1,
  84. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  85. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  86. -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  90. 0, 0, 0, 0
  91. };
  92. #define UPDATE_LEAK() \
  93. do { \
  94. fastleak += fdecay; \
  95. if (fastleak > psd + fgain) \
  96. fastleak = psd + fgain; \
  97. slowleak += sdecay; \
  98. if (slowleak > psd + sgain) \
  99. slowleak = psd + sgain; \
  100. } while (0)
  101. #define COMPUTE_MASK() \
  102. do { \
  103. if (psd > dbknee) \
  104. mask -= (psd - dbknee) >> 2; \
  105. if (mask > hth [i >> halfrate]) \
  106. mask = hth [i >> halfrate]; \
  107. mask -= snroffset + 128 * deltba[i]; \
  108. mask = (mask > 0) ? 0 : ((-mask) >> 5); \
  109. mask -= floor; \
  110. } while (0)
  111. void bit_allocate (ac3_state_t * state, ac3_ba_t * ba, int bndstart,
  112. int start, int end, int fastleak, int slowleak,
  113. uint8_t * exp, int8_t * bap)
  114. {
  115. static int slowgain[4] = {0x540, 0x4d8, 0x478, 0x410};
  116. static int dbpbtab[4] = {0xc00, 0x500, 0x300, 0x100};
  117. static int floortab[8] = {0x910, 0x950, 0x990, 0x9d0,
  118. 0xa10, 0xa90, 0xb10, 0x1400};
  119. int i, j;
  120. int fdecay, fgain, sdecay, sgain, dbknee, floor, snroffset;
  121. int psd, mask;
  122. int8_t * deltba;
  123. int * hth;
  124. int halfrate;
  125. halfrate = state->halfrate;
  126. fdecay = (63 + 20 * state->fdcycod) >> halfrate;
  127. fgain = 128 + 128 * ba->fgaincod;
  128. sdecay = (15 + 2 * state->sdcycod) >> halfrate;
  129. sgain = slowgain[state->sgaincod];
  130. dbknee = dbpbtab[state->dbpbcod];
  131. hth = hthtab[state->fscod];
  132. /*
  133. * if there is no delta bit allocation, make deltba point to an area
  134. * known to contain zeroes. baptab+156 here.
  135. */
  136. deltba = (ba->deltbae == DELTA_BIT_NONE) ? baptab + 156 : ba->deltba;
  137. floor = floortab[state->floorcod];
  138. snroffset = 960 - 64 * state->csnroffst - 4 * ba->fsnroffst + floor;
  139. floor >>= 5;
  140. i = bndstart;
  141. j = start;
  142. if (start == 0) { // not the coupling channel
  143. int lowcomp;
  144. lowcomp = 0;
  145. j = end - 1;
  146. do {
  147. if (i < j) {
  148. if (exp[i+1] == exp[i] - 2)
  149. lowcomp = 384;
  150. else if (lowcomp && (exp[i+1] > exp[i]))
  151. lowcomp -= 64;
  152. }
  153. psd = 128 * exp[i];
  154. mask = psd + fgain + lowcomp;
  155. COMPUTE_MASK ();
  156. bap[i++] = (baptab+156)[mask + 4 * exp[i]];
  157. } while ((i < 3) || ((i < 7) && (exp[i] > exp[i-1])));
  158. fastleak = psd + fgain;
  159. slowleak = psd + sgain;
  160. while (i < 7) {
  161. if (i < j) {
  162. if (exp[i+1] == exp[i] - 2)
  163. lowcomp = 384;
  164. else if (lowcomp && (exp[i+1] > exp[i]))
  165. lowcomp -= 64;
  166. }
  167. psd = 128 * exp[i];
  168. UPDATE_LEAK ();
  169. mask = ((fastleak + lowcomp < slowleak) ?
  170. fastleak + lowcomp : slowleak);
  171. COMPUTE_MASK ();
  172. bap[i++] = (baptab+156)[mask + 4 * exp[i]];
  173. }
  174. if (end == 7) // lfe channel
  175. return;
  176. do {
  177. if (exp[i+1] == exp[i] - 2)
  178. lowcomp = 320;
  179. else if (lowcomp && (exp[i+1] > exp[i]))
  180. lowcomp -= 64;
  181. psd = 128 * exp[i];
  182. UPDATE_LEAK ();
  183. mask = ((fastleak + lowcomp < slowleak) ?
  184. fastleak + lowcomp : slowleak);
  185. COMPUTE_MASK ();
  186. bap[i++] = (baptab+156)[mask + 4 * exp[i]];
  187. } while (i < 20);
  188. while (lowcomp > 128) { // two iterations maximum
  189. lowcomp -= 128;
  190. psd = 128 * exp[i];
  191. UPDATE_LEAK ();
  192. mask = ((fastleak + lowcomp < slowleak) ?
  193. fastleak + lowcomp : slowleak);
  194. COMPUTE_MASK ();
  195. bap[i++] = (baptab+156)[mask + 4 * exp[i]];
  196. }
  197. j = i;
  198. }
  199. do {
  200. int startband, endband;
  201. startband = j;
  202. endband = ((bndtab-20)[i] < end) ? (bndtab-20)[i] : end;
  203. psd = 128 * exp[j++];
  204. while (j < endband) {
  205. int next, delta;
  206. next = 128 * exp[j++];
  207. delta = next - psd;
  208. switch (delta >> 9) {
  209. case -6: case -5: case -4: case -3: case -2:
  210. psd = next;
  211. break;
  212. case -1:
  213. psd = next + latab[(-delta) >> 1];
  214. break;
  215. case 0:
  216. psd += latab[delta >> 1];
  217. break;
  218. }
  219. }
  220. // minpsd = -289
  221. UPDATE_LEAK ();
  222. mask = (fastleak < slowleak) ? fastleak : slowleak;
  223. COMPUTE_MASK ();
  224. i++;
  225. j = startband;
  226. do {
  227. // max(mask+4*exp)=147=-(minpsd+fgain-deltba-snroffset)>>5+4*exp
  228. // min(mask+4*exp)=-156=-(sgain-deltba-snroffset)>>5
  229. bap[j++] = (baptab+156)[mask + 4 * exp[j]];
  230. } while (j < endband);
  231. } while (j < end);
  232. }