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.

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