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.

188 lines
5.4KB

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