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