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
23 #define ALT_BITSTREAM_READER_LE
24 #include "bitstream.h"
34 float st1a[111], st1b[37], st1[37];
35 float st2a[38], st2b[11], st2[11];
40 static inline float scalar_product_float(float * v1, float * v2, int size)
50 /* Decode and produce output */
51 static void decode(Real288_internal *glob, float gain, int cb_coef)
57 memmove(glob->sb + 5, glob->sb, 36 * sizeof(*glob->sb));
59 for (x=4; x >= 0; x--)
60 glob->sb[x] = -scalar_product_float(glob->sb + x + 1, glob->pr1, 36);
62 /* convert log and do rms */
63 sum = 32. - scalar_product_float(glob->pr2, glob->lhist, 10);
70 sumsum = exp(sum * 0.1151292546497) * gain; /* pow(10.0,sum/20)*f */
73 buffer[x] = codetable[cb_coef][x] * sumsum;
75 sum = scalar_product_float(buffer, buffer, 5) / 5;
81 memmove(glob->lhist, glob->lhist - 1, 10 * sizeof(*glob->lhist));
83 *glob->lhist = glob->history[glob->phase] = 10 * log10(sum) - 32;
86 for (y=x-1; y >= 0; y--)
87 buffer[x] -= glob->pr1[x-y-1] * buffer[y];
90 for (x=0; x < 5; x++) {
91 float f = glob->sb[4-x] + buffer[x];
98 glob->output[glob->phase*5+x] = glob->sb[4-x] = f;
102 /* column multiply */
103 static void colmult(float *tgt, float *m1, const float *m2, int n)
106 *(tgt++) = (*(m1++)) * (*(m2++));
109 static int pred(float *in, float *tgt, int n)
129 for (y=0; y < x - 1; y++)
130 f1 += (*(p1--))*(*(p2++));
134 *(p1--) = f2 = -f1/f0;
135 for (y=x >> 1; y--;) {
136 float temp = *p2 + *p1 * f2;
140 if ((f0 += f1*f2) < 0)
145 /* product sum (lsf) */
146 static void prodsum(float *tgt, float *src, int len, int n)
149 tgt[n] = scalar_product_float(src, src - n, len);
153 static void co(int n, int i, int j, float *in, float *out, float *st1,
154 float *st2, const float *table)
163 /* rotate and multiply */
164 c = (b = (a = n + i) + j) - i;
166 for (x=0; x < b; x++) {
169 work[x] = *(table++) * (*(st1++) = *(fp++));
172 prodsum(buffer1, work + n, i, n);
173 prodsum(buffer2, work + a, j, n);
176 *st2 = *st2 * (0.5625) + buffer1[x];
177 out[x] = *(st2++) + buffer2[x];
179 *out *= 1.00390625; /* to prevent clipping */
182 static void update(Real288_internal *glob)
184 float buffer1[40], temp1[37];
185 float buffer2[8], temp2[11];
187 memcpy(buffer1 , glob->output + 20, 20*sizeof(*buffer1));
188 memcpy(buffer1 + 20, glob->output , 20*sizeof(*buffer1));
190 co(36, 40, 35, buffer1, temp1, glob->st1a, glob->st1b, table1);
192 if (pred(temp1, glob->st1, 36))
193 colmult(glob->pr1, glob->st1, table1a, 36);
195 memcpy(buffer2 , glob->history + 4, 4*sizeof(*buffer2));
196 memcpy(buffer2 + 4, glob->history , 4*sizeof(*buffer2));
198 co(10, 8, 20, buffer2, temp2, glob->st2a, glob->st2b, table2);
200 if (pred(temp2, glob->st2, 10))
201 colmult(glob->pr2, glob->st2, table2a, 10);
204 /* Decode a block (celp) */
205 static int ra288_decode_frame(AVCodecContext * avctx, void *data,
206 int *data_size, const uint8_t * buf,
211 Real288_internal *glob = avctx->priv_data;
214 if (buf_size < avctx->block_align) {
215 av_log(avctx, AV_LOG_ERROR,
216 "Error! Input buffer is too small [%d<%d]\n",
217 buf_size, avctx->block_align);
221 init_get_bits(&gb, buf, avctx->block_align * 8);
223 for (x=0; x < 32; x++) {
224 float gain = amptable[get_bits(&gb, 3)];
225 int cb_coef = get_bits(&gb, 6 + (x&1));
227 decode(glob, gain, cb_coef);
229 for (y=0; y < 5; y++)
230 *(out++) = 8 * glob->output[glob->phase*5 + y];
232 if (glob->phase == 3)
236 *data_size = (char *)out - (char *)data;
237 return avctx->block_align;
240 AVCodec ra_288_decoder =
245 sizeof(Real288_internal),
250 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),