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.

235 lines
6.6KB

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