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.

241 lines
6.8KB

  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 AVERROR_INVALIDDATA;
  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 (i = p; (bits[i] == 0) && (i < num); ++i)
  72. ;
  73. if (i == num)
  74. return 0;
  75. for (; p < num; ++p) {
  76. if (bits[p] > 32)
  77. return AVERROR_INVALIDDATA;
  78. if (bits[p] == 0)
  79. continue;
  80. // find corresponding exit(node which the tree can grow further from)
  81. for (i = bits[p]; i > 0; --i)
  82. if (exit_at_level[i])
  83. break;
  84. if (!i) // overspecified tree
  85. return AVERROR_INVALIDDATA;
  86. code = exit_at_level[i];
  87. exit_at_level[i] = 0;
  88. // construct code (append 0s to end) and introduce new exits
  89. for (j = i + 1 ;j <= bits[p]; ++j)
  90. exit_at_level[j] = code + (1 << (j - 1));
  91. codes[p] = code;
  92. #ifdef DEBUG
  93. av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
  94. init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
  95. for (i = 0; i < bits[p]; ++i)
  96. av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
  97. av_log(NULL, AV_LOG_INFO, "\n");
  98. #endif
  99. }
  100. //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
  101. for (p = 1; p < 33; p++)
  102. if (exit_at_level[p])
  103. return AVERROR_INVALIDDATA;
  104. return 0;
  105. }
  106. int ff_vorbis_ready_floor1_list(AVCodecContext *avctx,
  107. vorbis_floor1_entry *list, int values)
  108. {
  109. int i;
  110. list[0].sort = 0;
  111. list[1].sort = 1;
  112. for (i = 2; i < values; i++) {
  113. int j;
  114. list[i].low = 0;
  115. list[i].high = 1;
  116. list[i].sort = i;
  117. for (j = 2; j < i; j++) {
  118. int tmp = list[j].x;
  119. if (tmp < list[i].x) {
  120. if (tmp > list[list[i].low].x)
  121. list[i].low = j;
  122. } else {
  123. if (tmp < list[list[i].high].x)
  124. list[i].high = j;
  125. }
  126. }
  127. }
  128. for (i = 0; i < values - 1; i++) {
  129. int j;
  130. for (j = i + 1; j < values; j++) {
  131. if (list[i].x == list[j].x) {
  132. av_log(avctx, AV_LOG_ERROR,
  133. "Duplicate value found in floor 1 X coordinates\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. if (list[list[i].sort].x > list[list[j].sort].x) {
  137. int tmp = list[i].sort;
  138. list[i].sort = list[j].sort;
  139. list[j].sort = tmp;
  140. }
  141. }
  142. }
  143. return 0;
  144. }
  145. static inline void render_line_unrolled(intptr_t x, int y, int x1,
  146. intptr_t sy, int ady, int adx,
  147. float *buf)
  148. {
  149. int err = -adx;
  150. x -= x1 - 1;
  151. buf += x1 - 1;
  152. while (++x < 0) {
  153. err += ady;
  154. if (err >= 0) {
  155. err += ady - adx;
  156. y += sy;
  157. buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  158. }
  159. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  160. }
  161. if (x <= 0) {
  162. if (err + ady >= 0)
  163. y += sy;
  164. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  165. }
  166. }
  167. static void render_line(int x0, int y0, int x1, int y1, float *buf)
  168. {
  169. int dy = y1 - y0;
  170. int adx = x1 - x0;
  171. int ady = FFABS(dy);
  172. int sy = dy < 0 ? -1 : 1;
  173. buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
  174. if (ady*2 <= adx) { // optimized common case
  175. render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
  176. } else {
  177. int base = dy / adx;
  178. int x = x0;
  179. int y = y0;
  180. int err = -adx;
  181. ady -= FFABS(base) * adx;
  182. while (++x < x1) {
  183. y += base;
  184. err += ady;
  185. if (err >= 0) {
  186. err -= adx;
  187. y += sy;
  188. }
  189. buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
  190. }
  191. }
  192. }
  193. void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
  194. uint16_t *y_list, int *flag,
  195. int multiplier, float *out, int samples)
  196. {
  197. int lx, ly, i;
  198. lx = 0;
  199. ly = y_list[0] * multiplier;
  200. for (i = 1; i < values; i++) {
  201. int pos = list[i].sort;
  202. if (flag[pos]) {
  203. int x1 = list[pos].x;
  204. int y1 = y_list[pos] * multiplier;
  205. if (lx < samples)
  206. render_line(lx, ly, FFMIN(x1,samples), y1, out);
  207. lx = x1;
  208. ly = y1;
  209. }
  210. if (lx >= samples)
  211. break;
  212. }
  213. if (lx < samples)
  214. render_line(lx, ly, samples, ly, out);
  215. }