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.

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