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.0KB

  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;
  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(const float * v1, const float * v2,
  37. int size)
  38. {
  39. float res = 0.;
  40. while (size--)
  41. res += *v1++ * *v2++;
  42. return res;
  43. }
  44. /* Decode and produce output */
  45. static void decode(Real288_internal *glob, float gain, int cb_coef)
  46. {
  47. int x, y;
  48. double sumsum;
  49. float sum, buffer[5];
  50. memmove(glob->sb + 5, glob->sb, 36 * sizeof(*glob->sb));
  51. for (x=4; x >= 0; x--)
  52. glob->sb[x] = -scalar_product_float(glob->sb + x + 1, glob->pr1, 36);
  53. /* convert log and do rms */
  54. sum = 32. - scalar_product_float(glob->pr2, glob->lhist, 10);
  55. sum = av_clipf(sum, 0, 60);
  56. sumsum = exp(sum * 0.1151292546497) * gain; /* pow(10.0,sum/20)*f */
  57. for (x=0; x < 5; x++)
  58. buffer[x] = codetable[cb_coef][x] * sumsum;
  59. sum = scalar_product_float(buffer, buffer, 5) / 5;
  60. sum = FFMAX(sum, 1);
  61. /* shift and store */
  62. memmove(glob->lhist, glob->lhist - 1, 10 * sizeof(*glob->lhist));
  63. *glob->lhist = glob->history[glob->phase] = 10 * log10(sum) - 32;
  64. for (x=1; x < 5; x++)
  65. for (y=x-1; y >= 0; y--)
  66. buffer[x] -= glob->pr1[x-y-1] * buffer[y];
  67. /* output */
  68. for (x=0; x < 5; x++) {
  69. glob->output[glob->phase*5+x] = glob->sb[4-x] =
  70. av_clipf(glob->sb[4-x] + buffer[x], -4095, 4095);
  71. }
  72. }
  73. /* column multiply */
  74. static void colmult(float *tgt, const float *m1, const float *m2, int n)
  75. {
  76. while (n--)
  77. *(tgt++) = (*(m1++)) * (*(m2++));
  78. }
  79. static int pred(const float *in, float *tgt, int n)
  80. {
  81. int x, y;
  82. double f0, f1, f2;
  83. if (in[n] == 0)
  84. return 0;
  85. if ((f0 = *in) <= 0)
  86. return 0;
  87. in--; // To avoid a -1 subtraction in the inner loop
  88. for (x=1; x <= n; x++) {
  89. f1 = in[x+1];
  90. for (y=0; y < x - 1; y++)
  91. f1 += in[x-y]*tgt[y];
  92. tgt[x-1] = f2 = -f1/f0;
  93. for (y=0; y < x >> 1; y++) {
  94. float temp = tgt[y] + tgt[x-y-2]*f2;
  95. tgt[x-y-2] += tgt[y]*f2;
  96. tgt[y] = temp;
  97. }
  98. if ((f0 += f1*f2) < 0)
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. /* product sum (lsf) */
  104. static void prodsum(float *tgt, const float *src, int len, int n)
  105. {
  106. for (; n >= 0; n--)
  107. tgt[n] = scalar_product_float(src, src - n, len);
  108. }
  109. static void co(int n, int i, int j, const float *in, float *out, float *st1,
  110. float *st2, const float *table)
  111. {
  112. int a, b, c;
  113. unsigned int x;
  114. const float *fp;
  115. float buffer1[37];
  116. float buffer2[37];
  117. float work[111];
  118. /* rotate and multiply */
  119. c = (b = (a = n + i) + j) - i;
  120. fp = st1 + i;
  121. for (x=0; x < b; x++) {
  122. if (x == c)
  123. fp=in;
  124. work[x] = *(table++) * (*(st1++) = *(fp++));
  125. }
  126. prodsum(buffer1, work + n, i, n);
  127. prodsum(buffer2, work + a, j, n);
  128. for (x=0;x<=n;x++) {
  129. *st2 = *st2 * (0.5625) + buffer1[x];
  130. out[x] = *(st2++) + buffer2[x];
  131. }
  132. *out *= 1.00390625; /* to prevent clipping */
  133. }
  134. static void update(Real288_internal *glob)
  135. {
  136. float buffer1[40], temp1[37];
  137. float buffer2[8], temp2[11];
  138. memcpy(buffer1 , glob->output + 20, 20*sizeof(*buffer1));
  139. memcpy(buffer1 + 20, glob->output , 20*sizeof(*buffer1));
  140. co(36, 40, 35, buffer1, temp1, glob->st1a, glob->st1b, table1);
  141. if (pred(temp1, glob->st1, 36))
  142. colmult(glob->pr1, glob->st1, table1a, 36);
  143. memcpy(buffer2 , glob->history + 4, 4*sizeof(*buffer2));
  144. memcpy(buffer2 + 4, glob->history , 4*sizeof(*buffer2));
  145. co(10, 8, 20, buffer2, temp2, glob->st2a, glob->st2b, table2);
  146. if (pred(temp2, glob->st2, 10))
  147. colmult(glob->pr2, glob->st2, table2a, 10);
  148. }
  149. /* Decode a block (celp) */
  150. static int ra288_decode_frame(AVCodecContext * avctx, void *data,
  151. int *data_size, const uint8_t * buf,
  152. int buf_size)
  153. {
  154. int16_t *out = data;
  155. int x, y;
  156. Real288_internal *glob = avctx->priv_data;
  157. GetBitContext gb;
  158. if (buf_size < avctx->block_align) {
  159. av_log(avctx, AV_LOG_ERROR,
  160. "Error! Input buffer is too small [%d<%d]\n",
  161. buf_size, avctx->block_align);
  162. return 0;
  163. }
  164. init_get_bits(&gb, buf, avctx->block_align * 8);
  165. for (x=0; x < 32; x++) {
  166. float gain = amptable[get_bits(&gb, 3)];
  167. int cb_coef = get_bits(&gb, 6 + (x&1));
  168. glob->phase = x & 7;
  169. decode(glob, gain, cb_coef);
  170. for (y=0; y < 5; y++)
  171. *(out++) = 8 * glob->output[glob->phase*5 + y];
  172. if (glob->phase == 3)
  173. update(glob);
  174. }
  175. *data_size = (char *)out - (char *)data;
  176. return avctx->block_align;
  177. }
  178. AVCodec ra_288_decoder =
  179. {
  180. "real_288",
  181. CODEC_TYPE_AUDIO,
  182. CODEC_ID_RA_288,
  183. sizeof(Real288_internal),
  184. NULL,
  185. NULL,
  186. NULL,
  187. ra288_decode_frame,
  188. .long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),
  189. };