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.3KB

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