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.

229 lines
6.3KB

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