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.

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