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.

233 lines
6.6KB

  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. int ff_vorbis_ready_floor1_list(AVCodecContext *avccontext,
  100. vorbis_floor1_entry *list, int values)
  101. {
  102. int i;
  103. list[0].sort = 0;
  104. list[1].sort = 1;
  105. for (i = 2; i < values; i++) {
  106. int j;
  107. list[i].low = 0;
  108. list[i].high = 1;
  109. list[i].sort = i;
  110. for (j = 2; j < i; j++) {
  111. int tmp = list[j].x;
  112. if (tmp < list[i].x) {
  113. if (tmp > list[list[i].low].x)
  114. list[i].low = j;
  115. } else {
  116. if (tmp < list[list[i].high].x)
  117. list[i].high = j;
  118. }
  119. }
  120. }
  121. for (i = 0; i < values - 1; i++) {
  122. int j;
  123. for (j = i + 1; j < values; j++) {
  124. if (list[i].x == list[j].x) {
  125. av_log(avccontext, AV_LOG_ERROR,
  126. "Duplicate value found in floor 1 X coordinates\n");
  127. return AVERROR_INVALIDDATA;
  128. }
  129. if (list[list[i].sort].x > list[list[j].sort].x) {
  130. int tmp = list[i].sort;
  131. list[i].sort = list[j].sort;
  132. list[j].sort = tmp;
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. static inline void render_line_unrolled(intptr_t x, int y, int x1,
  139. intptr_t sy, int ady, int adx,
  140. float *buf)
  141. {
  142. int err = -adx;
  143. x -= x1 - 1;
  144. buf += x1 - 1;
  145. while (++x < 0) {
  146. err += ady;
  147. if (err >= 0) {
  148. err += ady - adx;
  149. y += sy;
  150. buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  151. }
  152. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  153. }
  154. if (x <= 0) {
  155. if (err + ady >= 0)
  156. y += sy;
  157. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  158. }
  159. }
  160. static void render_line(int x0, int y0, int x1, int y1, float *buf)
  161. {
  162. int dy = y1 - y0;
  163. int adx = x1 - x0;
  164. int ady = FFABS(dy);
  165. int sy = dy < 0 ? -1 : 1;
  166. buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
  167. if (ady*2 <= adx) { // optimized common case
  168. render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
  169. } else {
  170. int base = dy / adx;
  171. int x = x0;
  172. int y = y0;
  173. int err = -adx;
  174. ady -= FFABS(base) * adx;
  175. while (++x < x1) {
  176. y += base;
  177. err += ady;
  178. if (err >= 0) {
  179. err -= adx;
  180. y += sy;
  181. }
  182. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  183. }
  184. }
  185. }
  186. void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
  187. uint16_t *y_list, int *flag,
  188. int multiplier, float *out, int samples)
  189. {
  190. int lx, ly, i;
  191. lx = 0;
  192. ly = y_list[0] * multiplier;
  193. for (i = 1; i < values; i++) {
  194. int pos = list[i].sort;
  195. if (flag[pos]) {
  196. int x1 = list[pos].x;
  197. int y1 = y_list[pos] * multiplier;
  198. if (lx < samples)
  199. render_line(lx, ly, FFMIN(x1,samples), y1, out);
  200. lx = x1;
  201. ly = y1;
  202. }
  203. if (lx >= samples)
  204. break;
  205. }
  206. if (lx < samples)
  207. render_line(lx, ly, samples, ly, out);
  208. }