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.

231 lines
6.4KB

  1. /**
  2. * @file
  3. * Common code for Vorbis I encoder and decoder
  4. * @author Denes Balatoni ( dbalatoni programozo hu )
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav 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 GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #undef V_DEBUG
  23. //#define V_DEBUG
  24. #define ALT_BITSTREAM_READER_LE
  25. #include "avcodec.h"
  26. #include "get_bits.h"
  27. #include "vorbis.h"
  28. /* Helper functions */
  29. // x^(1/n)
  30. unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
  31. {
  32. unsigned int ret = 0, i, j;
  33. do {
  34. ++ret;
  35. for (i = 0, j = ret; i < n - 1; i++)
  36. j *= ret;
  37. } while (j <= x);
  38. return ret - 1;
  39. }
  40. // Generate vlc codes from vorbis huffman code lengths
  41. // the two bits[p] > 32 checks should be redundant, all calling code should
  42. // already ensure that, but since it allows overwriting the stack it seems
  43. // reasonable to check redundantly.
  44. int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
  45. {
  46. uint_fast32_t exit_at_level[33] = {
  47. 404, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  49. unsigned i, j, p, code;
  50. #ifdef V_DEBUG
  51. GetBitContext gb;
  52. #endif
  53. for (p = 0; (bits[p] == 0) && (p < num); ++p)
  54. ;
  55. if (p == num) {
  56. // av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
  57. return 0;
  58. }
  59. codes[p] = 0;
  60. if (bits[p] > 32)
  61. return 1;
  62. for (i = 0; i < bits[p]; ++i)
  63. exit_at_level[i+1] = 1 << i;
  64. #ifdef V_DEBUG
  65. av_log(NULL, AV_LOG_INFO, " %u. of %u code len %d code %d - ", p, num, bits[p], codes[p]);
  66. init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
  67. for (i = 0; i < bits[p]; ++i)
  68. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  69. av_log(NULL, AV_LOG_INFO, "\n");
  70. #endif
  71. ++p;
  72. for (; p < num; ++p) {
  73. if (bits[p] > 32)
  74. return 1;
  75. if (bits[p] == 0)
  76. continue;
  77. // find corresponding exit(node which the tree can grow further from)
  78. for (i = bits[p]; i > 0; --i)
  79. if (exit_at_level[i])
  80. break;
  81. if (!i) // overspecified tree
  82. return 1;
  83. code = exit_at_level[i];
  84. exit_at_level[i] = 0;
  85. // construct code (append 0s to end) and introduce new exits
  86. for (j = i + 1 ;j <= bits[p]; ++j)
  87. exit_at_level[j] = code + (1 << (j - 1));
  88. codes[p] = code;
  89. #ifdef V_DEBUG
  90. av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
  91. init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
  92. for (i = 0; i < bits[p]; ++i)
  93. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  94. av_log(NULL, AV_LOG_INFO, "\n");
  95. #endif
  96. }
  97. //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
  98. for (p = 1; p < 33; p++)
  99. if (exit_at_level[p])
  100. return 1;
  101. return 0;
  102. }
  103. void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values)
  104. {
  105. int i;
  106. list[0].sort = 0;
  107. list[1].sort = 1;
  108. for (i = 2; i < values; i++) {
  109. int j;
  110. list[i].low = 0;
  111. list[i].high = 1;
  112. list[i].sort = i;
  113. for (j = 2; j < i; j++) {
  114. int tmp = list[j].x;
  115. if (tmp < list[i].x) {
  116. if (tmp > list[list[i].low].x)
  117. list[i].low = j;
  118. } else {
  119. if (tmp < list[list[i].high].x)
  120. list[i].high = j;
  121. }
  122. }
  123. }
  124. for (i = 0; i < values - 1; i++) {
  125. int j;
  126. for (j = i + 1; j < values; j++) {
  127. if (list[list[i].sort].x > list[list[j].sort].x) {
  128. int tmp = list[i].sort;
  129. list[i].sort = list[j].sort;
  130. list[j].sort = tmp;
  131. }
  132. }
  133. }
  134. }
  135. static inline void render_line_unrolled(intptr_t x, intptr_t y, int x1,
  136. intptr_t sy, int ady, int adx,
  137. float *buf)
  138. {
  139. int err = -adx;
  140. x -= x1 - 1;
  141. buf += x1 - 1;
  142. while (++x < 0) {
  143. err += ady;
  144. if (err >= 0) {
  145. err += ady - adx;
  146. y += sy;
  147. buf[x++] = ff_vorbis_floor1_inverse_db_table[y];
  148. }
  149. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  150. }
  151. if (x <= 0) {
  152. if (err + ady >= 0)
  153. y += sy;
  154. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  155. }
  156. }
  157. static void render_line(int x0, int y0, int x1, int y1, float *buf)
  158. {
  159. int dy = y1 - y0;
  160. int adx = x1 - x0;
  161. int ady = FFABS(dy);
  162. int sy = dy < 0 ? -1 : 1;
  163. buf[x0] = ff_vorbis_floor1_inverse_db_table[y0];
  164. if (ady*2 <= adx) { // optimized common case
  165. render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
  166. } else {
  167. int base = dy / adx;
  168. int x = x0;
  169. int y = y0;
  170. int err = -adx;
  171. ady -= FFABS(base) * adx;
  172. while (++x < x1) {
  173. y += base;
  174. err += ady;
  175. if (err >= 0) {
  176. err -= adx;
  177. y += sy;
  178. }
  179. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  180. }
  181. }
  182. }
  183. void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
  184. uint_fast16_t *y_list, int *flag,
  185. int multiplier, float *out, int samples)
  186. {
  187. int lx, ly, i;
  188. lx = 0;
  189. ly = y_list[0] * multiplier;
  190. for (i = 1; i < values; i++) {
  191. int pos = list[i].sort;
  192. if (flag[pos]) {
  193. int x1 = list[pos].x;
  194. int y1 = y_list[pos] * multiplier;
  195. if (lx < samples)
  196. render_line(lx, ly, FFMIN(x1,samples), y1, out);
  197. lx = x1;
  198. ly = y1;
  199. }
  200. if (lx >= samples)
  201. break;
  202. }
  203. if (lx < samples)
  204. render_line(lx, ly, samples, ly, out);
  205. }