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.

266 lines
6.2KB

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