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.

210 lines
5.9KB

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