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.

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