vorbis.c 6.2 KB

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