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.

237 lines
6.7KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Common code for Vorbis I encoder and decoder
  25. * @author Denes Balatoni ( dbalatoni programozo hu )
  26. */
  27. #define BITSTREAM_READER_LE
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "vorbis.h"
  31. /* Helper functions */
  32. // x^(1/n)
  33. unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
  34. {
  35. unsigned int ret = 0, i, j;
  36. do {
  37. ++ret;
  38. for (i = 0, j = ret; i < n - 1; i++)
  39. j *= ret;
  40. } while (j <= x);
  41. return ret - 1;
  42. }
  43. // Generate vlc codes from vorbis huffman code lengths
  44. // the two bits[p] > 32 checks should be redundant, all calling code should
  45. // already ensure that, but since it allows overwriting the stack it seems
  46. // reasonable to check redundantly.
  47. int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
  48. {
  49. uint32_t exit_at_level[33] = { 404 };
  50. unsigned i, j, p, code;
  51. #ifdef DEBUG
  52. GetBitContext gb;
  53. #endif
  54. for (p = 0; (bits[p] == 0) && (p < num); ++p)
  55. ;
  56. if (p == num)
  57. return 0;
  58. codes[p] = 0;
  59. if (bits[p] > 32)
  60. return 1;
  61. for (i = 0; i < bits[p]; ++i)
  62. exit_at_level[i+1] = 1 << i;
  63. #ifdef DEBUG
  64. av_log(NULL, AV_LOG_INFO, " %u. of %u code len %d code %d - ", p, num, bits[p], codes[p]);
  65. init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
  66. for (i = 0; i < bits[p]; ++i)
  67. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  68. av_log(NULL, AV_LOG_INFO, "\n");
  69. #endif
  70. ++p;
  71. for (; p < num; ++p) {
  72. if (bits[p] > 32)
  73. return 1;
  74. if (bits[p] == 0)
  75. continue;
  76. // find corresponding exit(node which the tree can grow further from)
  77. for (i = bits[p]; i > 0; --i)
  78. if (exit_at_level[i])
  79. break;
  80. if (!i) // overspecified tree
  81. return 1;
  82. code = exit_at_level[i];
  83. exit_at_level[i] = 0;
  84. // construct code (append 0s to end) and introduce new exits
  85. for (j = i + 1 ;j <= bits[p]; ++j)
  86. exit_at_level[j] = code + (1 << (j - 1));
  87. codes[p] = code;
  88. #ifdef DEBUG
  89. av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
  90. init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
  91. for (i = 0; i < bits[p]; ++i)
  92. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  93. av_log(NULL, AV_LOG_INFO, "\n");
  94. #endif
  95. }
  96. //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
  97. for (p = 1; p < 33; p++)
  98. if (exit_at_level[p])
  99. return 1;
  100. return 0;
  101. }
  102. int ff_vorbis_ready_floor1_list(AVCodecContext *avccontext,
  103. vorbis_floor1_entry *list, int values)
  104. {
  105. int i;
  106. list[0].sort = 0;
  107. list[1].sort = 1;
  108. for (i = 2; i < values; i++) {
  109. int j;
  110. list[i].low = 0;
  111. list[i].high = 1;
  112. list[i].sort = i;
  113. for (j = 2; j < i; j++) {
  114. int tmp = list[j].x;
  115. if (tmp < list[i].x) {
  116. if (tmp > list[list[i].low].x)
  117. list[i].low = j;
  118. } else {
  119. if (tmp < list[list[i].high].x)
  120. list[i].high = j;
  121. }
  122. }
  123. }
  124. for (i = 0; i < values - 1; i++) {
  125. int j;
  126. for (j = i + 1; j < values; j++) {
  127. if (list[i].x == list[j].x) {
  128. av_log(avccontext, AV_LOG_ERROR,
  129. "Duplicate value found in floor 1 X coordinates\n");
  130. return AVERROR_INVALIDDATA;
  131. }
  132. if (list[list[i].sort].x > list[list[j].sort].x) {
  133. int tmp = list[i].sort;
  134. list[i].sort = list[j].sort;
  135. list[j].sort = tmp;
  136. }
  137. }
  138. }
  139. return 0;
  140. }
  141. static inline void render_line_unrolled(intptr_t x, int y, int x1,
  142. intptr_t sy, int ady, int adx,
  143. float *buf)
  144. {
  145. int err = -adx;
  146. x -= x1 - 1;
  147. buf += x1 - 1;
  148. while (++x < 0) {
  149. err += ady;
  150. if (err >= 0) {
  151. err += ady - adx;
  152. y += sy;
  153. buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  154. }
  155. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  156. }
  157. if (x <= 0) {
  158. if (err + ady >= 0)
  159. y += sy;
  160. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  161. }
  162. }
  163. static void render_line(int x0, int y0, int x1, int y1, float *buf)
  164. {
  165. int dy = y1 - y0;
  166. int adx = x1 - x0;
  167. int ady = FFABS(dy);
  168. int sy = dy < 0 ? -1 : 1;
  169. buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
  170. if (ady*2 <= adx) { // optimized common case
  171. render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
  172. } else {
  173. int base = dy / adx;
  174. int x = x0;
  175. int y = y0;
  176. int err = -adx;
  177. ady -= FFABS(base) * adx;
  178. while (++x < x1) {
  179. y += base;
  180. err += ady;
  181. if (err >= 0) {
  182. err -= adx;
  183. y += sy;
  184. }
  185. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  186. }
  187. }
  188. }
  189. void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
  190. uint16_t *y_list, int *flag,
  191. int multiplier, float *out, int samples)
  192. {
  193. int lx, ly, i;
  194. lx = 0;
  195. ly = y_list[0] * multiplier;
  196. for (i = 1; i < values; i++) {
  197. int pos = list[i].sort;
  198. if (flag[pos]) {
  199. int x1 = list[pos].x;
  200. int y1 = y_list[pos] * multiplier;
  201. if (lx < samples)
  202. render_line(lx, ly, FFMIN(x1,samples), y1, out);
  203. lx = x1;
  204. ly = y1;
  205. }
  206. if (lx >= samples)
  207. break;
  208. }
  209. if (lx < samples)
  210. render_line(lx, ly, samples, ly, out);
  211. }