OSDN Git Service

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