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.

232 lines
6.5KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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, uint_fast32_t 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. uint_fast8_t i, j;
  50. uint_fast32_t code, p;
  51. #ifdef V_DEBUG
  52. GetBitContext gb;
  53. #endif
  54. for (p = 0; (bits[p] == 0) && (p < num); ++p)
  55. ;
  56. if (p == num) {
  57. // av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
  58. return 0;
  59. }
  60. codes[p] = 0;
  61. if (bits[p] > 32)
  62. return 1;
  63. for (i = 0; i < bits[p]; ++i)
  64. exit_at_level[i+1] = 1 << i;
  65. #ifdef V_DEBUG
  66. av_log(NULL, AV_LOG_INFO, " %d. of %d code len %d code %d - ", p, num, bits[p], codes[p]);
  67. init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
  68. for (i = 0; i < bits[p]; ++i)
  69. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  70. av_log(NULL, AV_LOG_INFO, "\n");
  71. #endif
  72. ++p;
  73. for (; p < num; ++p) {
  74. if (bits[p] > 32)
  75. return 1;
  76. if (bits[p] == 0)
  77. continue;
  78. // find corresponding exit(node which the tree can grow further from)
  79. for (i = bits[p]; i > 0; --i)
  80. if (exit_at_level[i])
  81. break;
  82. if (!i) // overspecified tree
  83. return 1;
  84. code = exit_at_level[i];
  85. exit_at_level[i] = 0;
  86. // construct code (append 0s to end) and introduce new exits
  87. for (j = i + 1 ;j <= bits[p]; ++j)
  88. exit_at_level[j] = code + (1 << (j - 1));
  89. codes[p] = code;
  90. #ifdef V_DEBUG
  91. av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
  92. init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
  93. for (i = 0; i < bits[p]; ++i)
  94. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  95. av_log(NULL, AV_LOG_INFO, "\n");
  96. #endif
  97. }
  98. //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
  99. for (p = 1; p < 33; p++)
  100. if (exit_at_level[p])
  101. return 1;
  102. return 0;
  103. }
  104. void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values)
  105. {
  106. int i;
  107. list[0].sort = 0;
  108. list[1].sort = 1;
  109. for (i = 2; i < values; i++) {
  110. int j;
  111. list[i].low = 0;
  112. list[i].high = 1;
  113. list[i].sort = i;
  114. for (j = 2; j < i; j++) {
  115. int tmp = list[j].x;
  116. if (tmp < list[i].x) {
  117. if (tmp > list[list[i].low].x)
  118. list[i].low = j;
  119. } else {
  120. if (tmp < list[list[i].high].x)
  121. list[i].high = j;
  122. }
  123. }
  124. }
  125. for (i = 0; i < values - 1; i++) {
  126. int j;
  127. for (j = i + 1; j < values; j++) {
  128. if (list[list[i].sort].x > list[list[j].sort].x) {
  129. int tmp = list[i].sort;
  130. list[i].sort = list[j].sort;
  131. list[j].sort = tmp;
  132. }
  133. }
  134. }
  135. }
  136. static inline void render_line_unrolled(intptr_t x, intptr_t y, int x1,
  137. intptr_t sy, int ady, int adx,
  138. float *buf)
  139. {
  140. int err = -adx;
  141. x -= x1 - 1;
  142. buf += x1 - 1;
  143. while (++x < 0) {
  144. err += ady;
  145. if (err >= 0) {
  146. err += ady - adx;
  147. y += sy;
  148. buf[x++] = ff_vorbis_floor1_inverse_db_table[y];
  149. }
  150. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  151. }
  152. if (x <= 0) {
  153. if (err + ady >= 0)
  154. y += sy;
  155. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  156. }
  157. }
  158. static void render_line(int x0, int y0, int x1, int y1, float *buf)
  159. {
  160. int dy = y1 - y0;
  161. int adx = x1 - x0;
  162. int ady = FFABS(dy);
  163. int sy = dy < 0 ? -1 : 1;
  164. buf[x0] = ff_vorbis_floor1_inverse_db_table[y0];
  165. if (ady*2 <= adx) { // optimized common case
  166. render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
  167. } else {
  168. int base = dy / adx;
  169. int x = x0;
  170. int y = y0;
  171. int err = -adx;
  172. ady -= FFABS(base) * adx;
  173. while (++x < x1) {
  174. y += base;
  175. err += ady;
  176. if (err >= 0) {
  177. err -= adx;
  178. y += sy;
  179. }
  180. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  181. }
  182. }
  183. }
  184. void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
  185. uint_fast16_t *y_list, int *flag,
  186. int multiplier, float *out, int samples)
  187. {
  188. int lx, ly, i;
  189. lx = 0;
  190. ly = y_list[0] * multiplier;
  191. for (i = 1; i < values; i++) {
  192. int pos = list[i].sort;
  193. if (flag[pos]) {
  194. int x1 = list[pos].x;
  195. int y1 = y_list[pos] * multiplier;
  196. if (lx < samples)
  197. render_line(lx, ly, FFMIN(x1,samples), y1, out);
  198. lx = x1;
  199. ly = y1;
  200. }
  201. if (lx >= samples)
  202. break;
  203. }
  204. if (lx < samples)
  205. render_line(lx, ly, samples, ly, out);
  206. }