2 * RealAudio 2.0 (28.8K)
3 * Copyright (c) 2003 the ffmpeg project
5 * This file is part of FFmpeg.
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.
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.
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
32 float st1a[111], st1b[37], st1[37];
33 float st2a[38], st2b[11], st2[11];
38 static int ra288_decode_init(AVCodecContext * avctx)
40 Real288_internal *glob = avctx->priv_data;
41 memset(glob, 0, sizeof(Real288_internal));
45 static void prodsum(float *tgt, float *src, int len, int n);
46 static void co(int n, int i, int j, float *in, float *out, float *st1, float *st2, const float *table);
47 static int pred(float *in, float *tgt, int n);
48 static void colmult(float *tgt, float *m1, const float *m2, int n);
52 static void unpack(unsigned short *tgt, const unsigned char *src,
59 for (x=0; x < len; tgt[x++] = 0)
60 buffer[x] = 9 + (x & 1);
62 for (x=y=z=0; x < len/*was 38*/; x++) {
67 temp &= 255 >> (8 - n);
72 tgt[++y] += src[x] >> n;
79 static void update(Real288_internal *glob)
82 float buffer1[40], temp1[37];
83 float buffer2[8], temp2[11];
85 for (x=0, y=glob->phasep+5; x < 40; buffer1[x++] = glob->output[(y++)%40]);
87 co(36, 40, 35, buffer1, temp1, glob->st1a, glob->st1b, table1);
89 if (pred(temp1, glob->st1, 36))
90 colmult(glob->pr1, glob->st1, table1a, 36);
92 for (x=0, y=glob->phase + 1; x < 8; buffer2[x++] = glob->history[(y++) % 8]);
94 co(10, 8, 20, buffer2, temp2, glob->st2a, glob->st2b, table2);
96 if (pred(temp2, glob->st2, 10))
97 colmult(glob->pr2, glob->st2, table2a, 10);
100 /* Decode and produce output */
101 static void decode(Real288_internal *glob, unsigned int input)
110 for (x=36; x--; glob->sb[x+5] = glob->sb[x]);
115 for (sum=0, y=36; y--; sum -= (*(++p1))*(*(p2++)));
120 f = amptable[input & 7];
121 table = codetable + (input >> 3) * 5;
123 /* convert log and do rms */
124 for (sum=32, x=10; x--; sum -= glob->pr2[x] * glob->lhist[x]);
131 sumsum = exp(sum * 0.1151292546497) * f; /* pow(10.0,sum/20)*f */
133 for (sum=0, x=5; x--;) {
134 buffer[x] = table[x] * sumsum;
135 sum += buffer[x] * buffer[x];
141 /* shift and store */
142 for (x=10; --x; glob->lhist[x] = glob->lhist[x-1]);
144 *glob->lhist = glob->history[glob->phase] = 10 * log10(sum) - 32;
146 for (x=1; x < 5; x++)
147 for (y=x; y--; buffer[x] -= glob->pr1[x-y-1] * buffer[y]);
150 for (x=0; x < 5; x++) {
151 f = glob->sb[4-x] + buffer[x];
158 glob->output[glob->phasep+x] = glob->sb[4-x] = f;
162 /* column multiply */
163 static void colmult(float *tgt, float *m1, const float *m2, int n)
166 *(tgt++) = (*(m1++)) * (*(m2++));
169 static int pred(float *in, float *tgt, int n)
189 for (y=x; --y; f1 += (*(p1--))*(*(p2++)));
193 *(p1--) = f2 = -f1/f0;
194 for (y=x >> 1; y--;) {
195 temp = *p2 + *p1 * f2;
199 if ((f0 += f1*f2) < 0)
204 static void co(int n, int i, int j, float *in, float *out, float *st1,
205 float *st2, const float *table)
214 /* rotate and multiply */
215 c = (b = (a = n + i) + j) - i;
217 for (x=0; x < b; x++) {
220 work[x] = *(table++) * (*(st1++) = *(fp++));
223 prodsum(buffer1, work + n, i, n);
224 prodsum(buffer2, work + a, j, n);
227 *st2 = *st2 * (0.5625) + buffer1[x];
228 out[x] = *(st2++) + buffer2[x];
230 *out *= 1.00390625; /* to prevent clipping */
233 /* product sum (lsf) */
234 static void prodsum(float *tgt, float *src, int len, int n)
242 for (sum=0, x=len; x--; sum += (*p1++) * (*p2++));
247 static void * decode_block(AVCodecContext * avctx, const unsigned char *in,
248 signed short int *out, unsigned len)
251 Real288_internal *glob = avctx->priv_data;
252 unsigned short int buffer[len];
254 unpack(buffer, in, len);
256 for (x=0; x < 32; x++) {
257 glob->phasep = (glob->phase = x & 7) * 5;
258 decode(glob, buffer[x]);
260 for (y=0; y<5; *(out++) = 8 * glob->output[glob->phasep+(y++)]);
262 if (glob->phase == 3)
269 /* Decode a block (celp) */
270 static int ra288_decode_frame(AVCodecContext * avctx, void *data,
271 int *data_size, const uint8_t * buf,
276 if (buf_size < avctx->block_align) {
277 av_log(avctx, AV_LOG_ERROR,
278 "ffra288: Error! Input buffer is too small [%d<%d]\n",
279 buf_size, avctx->block_align);
284 data = decode_block(avctx, buf, (signed short *)data, avctx->block_align);
286 *data_size = (char *)data - (char *)datao;
287 return avctx->block_align;
290 AVCodec ra_288_decoder =
295 sizeof(Real288_internal),
300 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),