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.

257 lines
6.1KB

  1. /*
  2. * RealAudio 2.0 (28.8K)
  3. * Copyright (c) 2003 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #define ALT_BITSTREAM_READER_LE
  23. #include "bitstream.h"
  24. #include "ra288.h"
  25. typedef struct {
  26. float history[8];
  27. float output[40];
  28. float pr1[36];
  29. float pr2[10];
  30. int phase, phasep;
  31. float st1a[111], st1b[37], st1[37];
  32. float st2a[38], st2b[11], st2[11];
  33. float sb[41];
  34. float lhist[10];
  35. } Real288_internal;
  36. static inline float scalar_product_float(float * v1, float * v2, int size)
  37. {
  38. float res = 0.;
  39. while (size--)
  40. res += *v1++ * *v2++;
  41. return res;
  42. }
  43. /* Decode and produce output */
  44. static void decode(Real288_internal *glob, float gain, int cb_coef)
  45. {
  46. int x, y;
  47. double sum, sumsum;
  48. float buffer[5];
  49. memmove(glob->sb + 5, glob->sb, 36 * sizeof(*glob->sb));
  50. for (x=4; x >= 0; x--)
  51. glob->sb[x] = -scalar_product_float(glob->sb + x + 1, glob->pr1, 36);
  52. /* convert log and do rms */
  53. sum = 32. - scalar_product_float(glob->pr2, glob->lhist, 10);
  54. if (sum < 0)
  55. sum = 0;
  56. else if (sum > 60)
  57. sum = 60;
  58. sumsum = exp(sum * 0.1151292546497) * gain; /* pow(10.0,sum/20)*f */
  59. sum = 0;
  60. for (x=0; x < 5; x++) {
  61. buffer[x] = codetable[cb_coef][x] * sumsum;
  62. sum += buffer[x] * buffer[x];
  63. }
  64. sum /= 5;
  65. if (sum < 1)
  66. sum = 1;
  67. /* shift and store */
  68. memmove(glob->lhist, glob->lhist - 1, 10 * sizeof(*glob->lhist));
  69. *glob->lhist = glob->history[glob->phase] = 10 * log10(sum) - 32;
  70. for (x=1; x < 5; x++)
  71. for (y=x-1; y >= 0; y--)
  72. buffer[x] -= glob->pr1[x-y-1] * buffer[y];
  73. /* output */
  74. for (x=0; x < 5; x++) {
  75. float f = glob->sb[4-x] + buffer[x];
  76. if (f > 4095)
  77. f = 4095;
  78. else if (f < -4095)
  79. f = -4095;
  80. glob->output[glob->phasep+x] = glob->sb[4-x] = f;
  81. }
  82. }
  83. /* column multiply */
  84. static void colmult(float *tgt, float *m1, const float *m2, int n)
  85. {
  86. while (n--)
  87. *(tgt++) = (*(m1++)) * (*(m2++));
  88. }
  89. static int pred(float *in, float *tgt, int n)
  90. {
  91. int x, y;
  92. double f0, f1, f2;
  93. if (in[n] == 0)
  94. return 0;
  95. if ((f0 = *in) <= 0)
  96. return 0;
  97. for (x=1 ; ; x++) {
  98. float *p1 = in + x;
  99. float *p2 = tgt;
  100. if (n < x)
  101. return 1;
  102. f1 = *(p1--);
  103. for (y=0; y < x - 1; y++)
  104. f1 += (*(p1--))*(*(p2++));
  105. p1 = tgt + x - 1;
  106. p2 = tgt;
  107. *(p1--) = f2 = -f1/f0;
  108. for (y=x >> 1; y--;) {
  109. float temp = *p2 + *p1 * f2;
  110. *(p1--) += *p2 * f2;
  111. *(p2++) = temp;
  112. }
  113. if ((f0 += f1*f2) < 0)
  114. return 0;
  115. }
  116. }
  117. /* product sum (lsf) */
  118. static void prodsum(float *tgt, float *src, int len, int n)
  119. {
  120. for (; n >= 0; n--)
  121. tgt[n] = scalar_product_float(src, src - n, len);
  122. }
  123. static void co(int n, int i, int j, float *in, float *out, float *st1,
  124. float *st2, const float *table)
  125. {
  126. int a, b, c;
  127. unsigned int x;
  128. float *fp;
  129. float buffer1[37];
  130. float buffer2[37];
  131. float work[111];
  132. /* rotate and multiply */
  133. c = (b = (a = n + i) + j) - i;
  134. fp = st1 + i;
  135. for (x=0; x < b; x++) {
  136. if (x == c)
  137. fp=in;
  138. work[x] = *(table++) * (*(st1++) = *(fp++));
  139. }
  140. prodsum(buffer1, work + n, i, n);
  141. prodsum(buffer2, work + a, j, n);
  142. for (x=0;x<=n;x++) {
  143. *st2 = *st2 * (0.5625) + buffer1[x];
  144. out[x] = *(st2++) + buffer2[x];
  145. }
  146. *out *= 1.00390625; /* to prevent clipping */
  147. }
  148. static void update(Real288_internal *glob)
  149. {
  150. int x,y;
  151. float buffer1[40], temp1[37];
  152. float buffer2[8], temp2[11];
  153. y = glob->phasep+5;
  154. for (x=0; x < 40; x++)
  155. buffer1[x] = glob->output[(y++)%40];
  156. co(36, 40, 35, buffer1, temp1, glob->st1a, glob->st1b, table1);
  157. if (pred(temp1, glob->st1, 36))
  158. colmult(glob->pr1, glob->st1, table1a, 36);
  159. y = glob->phase + 1;
  160. for (x=0; x < 8; x++)
  161. buffer2[x] = glob->history[(y++) % 8];
  162. co(10, 8, 20, buffer2, temp2, glob->st2a, glob->st2b, table2);
  163. if (pred(temp2, glob->st2, 10))
  164. colmult(glob->pr2, glob->st2, table2a, 10);
  165. }
  166. /* Decode a block (celp) */
  167. static int ra288_decode_frame(AVCodecContext * avctx, void *data,
  168. int *data_size, const uint8_t * buf,
  169. int buf_size)
  170. {
  171. int16_t *out = data;
  172. int x, y;
  173. Real288_internal *glob = avctx->priv_data;
  174. GetBitContext gb;
  175. if (buf_size < avctx->block_align) {
  176. av_log(avctx, AV_LOG_ERROR,
  177. "Error! Input buffer is too small [%d<%d]\n",
  178. buf_size, avctx->block_align);
  179. return 0;
  180. }
  181. init_get_bits(&gb, buf, avctx->block_align * 8);
  182. for (x=0; x < 32; x++) {
  183. float gain = amptable[get_bits(&gb, 3)];
  184. int cb_coef = get_bits(&gb, 6 + (x&1));
  185. glob->phasep = (glob->phase = x & 7) * 5;
  186. decode(glob, gain, cb_coef);
  187. for (y=0; y < 5; y++)
  188. *(out++) = 8 * glob->output[glob->phasep + y];
  189. if (glob->phase == 3)
  190. update(glob);
  191. }
  192. *data_size = (char *)out - (char *)data;
  193. return avctx->block_align;
  194. }
  195. AVCodec ra_288_decoder =
  196. {
  197. "real_288",
  198. CODEC_TYPE_AUDIO,
  199. CODEC_ID_RA_288,
  200. sizeof(Real288_internal),
  201. NULL,
  202. NULL,
  203. NULL,
  204. ra288_decode_frame,
  205. .long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),
  206. };