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.

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