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.

183 lines
5.1KB

  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. unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n) { // x^(1/n)
  29. unsigned int ret=0, i, j;
  30. do {
  31. ++ret;
  32. for(i=0,j=ret;i<n-1;i++) j*=ret;
  33. } while (j<=x);
  34. return ret - 1;
  35. }
  36. // Generate vlc codes from vorbis huffman code lengths
  37. int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, uint_fast32_t num) {
  38. uint_fast32_t exit_at_level[33]={404,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  39. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  40. uint_fast8_t i,j;
  41. uint_fast32_t code,p;
  42. #ifdef V_DEBUG
  43. GetBitContext gb;
  44. #endif
  45. for(p=0;(bits[p]==0) && (p<num);++p);
  46. if (p==num) {
  47. // av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
  48. return 0;
  49. }
  50. codes[p]=0;
  51. for(i=0;i<bits[p];++i) {
  52. exit_at_level[i+1]=1<<i;
  53. }
  54. #ifdef V_DEBUG
  55. av_log(NULL, AV_LOG_INFO, " %d. of %d code len %d code %d - ", p, num, bits[p], codes[p]);
  56. init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
  57. for(i=0;i<bits[p];++i) {
  58. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  59. }
  60. av_log(NULL, AV_LOG_INFO, "\n");
  61. #endif
  62. ++p;
  63. for(;p<num;++p) {
  64. if (bits[p]==0) continue;
  65. // find corresponding exit(node which the tree can grow further from)
  66. for(i=bits[p];i>0;--i) {
  67. if (exit_at_level[i]) break;
  68. }
  69. if (!i) return 1; // overspecified tree
  70. code=exit_at_level[i];
  71. exit_at_level[i]=0;
  72. // construct code (append 0s to end) and introduce new exits
  73. for(j=i+1;j<=bits[p];++j) {
  74. exit_at_level[j]=code+(1<<(j-1));
  75. }
  76. codes[p]=code;
  77. #ifdef V_DEBUG
  78. av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
  79. init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
  80. for(i=0;i<bits[p];++i) {
  81. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  82. }
  83. av_log(NULL, AV_LOG_INFO, "\n");
  84. #endif
  85. }
  86. //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
  87. for (p=1; p<33; p++)
  88. if (exit_at_level[p]) return 1;
  89. return 0;
  90. }
  91. void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values) {
  92. int i;
  93. list[0].sort = 0;
  94. list[1].sort = 1;
  95. for (i = 2; i < values; i++) {
  96. int j;
  97. list[i].low = 0;
  98. list[i].high = 1;
  99. list[i].sort = i;
  100. for (j = 2; j < i; j++) {
  101. int tmp = list[j].x;
  102. if (tmp < list[i].x) {
  103. if (tmp > list[list[i].low].x) list[i].low = j;
  104. } else {
  105. if (tmp < list[list[i].high].x) list[i].high = j;
  106. }
  107. }
  108. }
  109. for (i = 0; i < values - 1; i++) {
  110. int j;
  111. for (j = i + 1; j < values; j++) {
  112. if (list[list[i].sort].x > list[list[j].sort].x) {
  113. int tmp = list[i].sort;
  114. list[i].sort = list[j].sort;
  115. list[j].sort = tmp;
  116. }
  117. }
  118. }
  119. }
  120. static void render_line(int x0, int y0, int x1, int y1, float * buf) {
  121. int dy = y1 - y0;
  122. int adx = x1 - x0;
  123. int base = dy / adx;
  124. int ady = FFABS(dy) - FFABS(base) * adx;
  125. int x = x0;
  126. int y = y0;
  127. int err = 0;
  128. int sy = dy<0 ? -1 : 1;
  129. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  130. while (++x < x1) {
  131. err += ady;
  132. if (err >= adx) {
  133. err -= adx;
  134. y += sy;
  135. }
  136. y += base;
  137. buf[x] = ff_vorbis_floor1_inverse_db_table[y];
  138. }
  139. }
  140. void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values, uint_fast16_t * y_list, int * flag, int multiplier, float * out, int samples) {
  141. int lx, ly, i;
  142. lx = 0;
  143. ly = y_list[0] * multiplier;
  144. for (i = 1; i < values; i++) {
  145. int pos = list[i].sort;
  146. if (flag[pos]) {
  147. int x1 = list[pos].x;
  148. int y1 = y_list[pos] * multiplier;
  149. if (lx < samples)
  150. render_line(lx, ly, FFMIN(x1,samples), y1, out);
  151. lx = x1;
  152. ly = y1;
  153. }
  154. if (lx >= samples) break;
  155. }
  156. if (lx < samples) render_line(lx, ly, samples, ly, out);
  157. }