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.

187 lines
5.2KB

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