]> git.sesse.net Git - ffmpeg/blob - libavcodec/ra144.c
Simplify rms()
[ffmpeg] / libavcodec / ra144.c
1 /*
2  * Real Audio 1.0 (14.4K)
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 #include "bitstream.h"
24 #include "ra144.h"
25
26 #define NBLOCKS         4       /* number of segments within a block */
27 #define BLOCKSIZE       40      /* (quarter) block size in 16-bit words (80 bytes) */
28 #define HALFBLOCK       20      /* BLOCKSIZE/2 */
29 #define BUFFERSIZE      146     /* for do_output */
30
31
32 /* internal globals */
33 typedef struct {
34     unsigned int     oldval;
35     unsigned int     gbuf1[4];
36     unsigned short   gbuf2[4][30];
37     unsigned int    *decptr;                /* decoder ptr */
38
39     /* the swapped buffers */
40     unsigned int     swapbuffers[4][10];
41     unsigned int    *swapbuf1;
42     unsigned int    *swapbuf2;
43     unsigned int    *swapbuf1alt;
44     unsigned int    *swapbuf2alt;
45
46     unsigned int buffer[5];
47     unsigned short int buffer_2[148];
48 } Real144_internal;
49
50 static int ra144_decode_init(AVCodecContext * avctx)
51 {
52     Real144_internal *glob = avctx->priv_data;
53
54     glob->swapbuf1    = glob->swapbuffers[0];
55     glob->swapbuf2    = glob->swapbuffers[1];
56     glob->swapbuf1alt = glob->swapbuffers[2];
57     glob->swapbuf2alt = glob->swapbuffers[3];
58
59     return 0;
60 }
61
62 /* lookup square roots in table */
63 static int t_sqrt(unsigned int x)
64 {
65     int s = 0;
66     while (x > 0xfff) {
67         s++;
68         x = x >> 2;
69     }
70
71     return (ff_sqrt(x << 20) << s) << 2;
72 }
73
74 /* do 'voice' */
75 static void do_voice(const int *a1, int *a2)
76 {
77     int buffer[10];
78     int *b1 = buffer;
79     int *b2 = a2;
80     int x, y;
81
82     for (x=0; x < 10; x++) {
83         b1[x] = a1[x] << 4;
84
85         for (y=0; y < x; y++)
86             b1[y] = ((a1[x] * b2[x-y-1]) >> 12) + b2[y];
87
88         FFSWAP(int *, b1, b2);
89     }
90
91     for (x=0; x < 10; x++)
92         a2[x] >>= 4;
93 }
94
95 /* rotate block */
96 static void rotate_block(const short *source, short *target, int offset)
97 {
98     int i=0, k=0;
99     source += BUFFERSIZE - offset;
100
101     while (i<BLOCKSIZE) {
102         target[i++] = source[k++];
103
104         if (k == offset)
105             k = 0;
106     }
107 }
108
109 /* inverse root mean square */
110 static int irms(const short *data, int factor)
111 {
112     unsigned int i, sum = 0;
113
114     for (i=0; i < BLOCKSIZE; i++)
115         sum += data[i] * data[i];
116
117     if (sum == 0)
118         return 0; /* OOPS - division by zero */
119
120     return (0x20000000 / (t_sqrt(sum) >> 8)) * factor;
121 }
122
123 /* multiply/add wavetable */
124 static void add_wav(int n, int f, int m1, int m2, int m3, const short *s1,
125                     const short *s2, const short *s3, short *dest)
126 {
127     int a = 0;
128     int b, c, i;
129     const short *ptr, *ptr2;
130
131     ptr  = wavtable1[n];
132     ptr2 = wavtable2[n];
133
134     if (f)
135         a = (ptr[0] * m1) >> (ptr2[0] + 1);
136
137     b = (ptr[1] * m2) >> (ptr2[1] + 1);
138     c = (ptr[2] * m3) >> (ptr2[2] + 1);
139
140     for (i=0; i < BLOCKSIZE; i++)
141         dest[i] = ((*(s1++)) * a + (*(s2++)) * b + (*(s3++)) * c) >> 12;
142 }
143
144
145 static void final(const short *i1, const short *i2,
146                   void *out, int *statbuf, int len)
147 {
148     int x, sum, i;
149     int buffer[10];
150     short *ptr;
151     short *ptr2;
152     unsigned short int work[50];
153
154     memcpy(work, statbuf,20);
155     memcpy(work + 10, i2, len * 2);
156
157     for(i=0; i<10; i++)
158         buffer[9-i] = i1[i];
159
160     ptr2 = (ptr = work) + len;
161
162     while (ptr < ptr2) {
163         for(sum=0, x=0; x<=9; x++)
164             sum += buffer[x] * (ptr[x]);
165
166         sum = sum >> 12;
167         x = ptr[10] - sum;
168
169         if (x<-32768 || x>32767) {
170             memset(out, 0, len * 2);
171             memset(statbuf, 0, 20);
172             return;
173         }
174
175         ptr[10] = x;
176         ptr++;
177     }
178     memcpy(out, ptr+10 - len, len * 2);
179     memcpy(statbuf, ptr, 20);
180 }
181
182 static unsigned int rms(const int *data, int f)
183 {
184     int x;
185     unsigned int res = 0x10000;
186     int b = 0;
187
188     for (x=0; x<10; x++) {
189         res = (((0x1000000 - (*data) * (*data)) >> 12) * res) >> 12;
190
191         if (res == 0)
192             return 0;
193
194             if (res > 0x10000)
195                 return 0; /* We're screwed, might as well go out with a bang. :P */
196
197         while (res <= 0x3fff) {
198             b++;
199             res <<= 2;
200         }
201         data++;
202     }
203
204     if (res > 0)
205         res = t_sqrt(res);
206
207     res >>= (b + 10);
208     res = (res * f) >> 10;
209     return res;
210 }
211
212 /* do quarter-block output */
213 static void do_output_subblock(Real144_internal *glob, const unsigned short  *gsp, unsigned int gval, signed short *output_buffer, GetBitContext *gb)
214 {
215     unsigned short int buffer_a[40];
216     unsigned short int *block;
217     int e, f, g;
218     int a = get_bits(gb, 7);
219     int d = get_bits(gb, 8);
220     int b = get_bits(gb, 7);
221     int c = get_bits(gb, 7);
222
223     if (a) {
224         a += HALFBLOCK - 1;
225         rotate_block(glob->buffer_2, buffer_a, a);
226     }
227
228     e = ((ftable1[b] >> 4) * gval) >> 8;
229     f = ((ftable2[c] >> 4) * gval) >> 8;
230
231     if (a)
232         g = irms(buffer_a, gval) >> 12;
233     else
234         g = 0;
235
236     memmove(glob->buffer_2, glob->buffer_2 + BLOCKSIZE, (BUFFERSIZE - BLOCKSIZE) * 2);
237     block = glob->buffer_2 + BUFFERSIZE - BLOCKSIZE;
238
239     add_wav(d, a, g, e, f, buffer_a, etable1[b],
240             etable2[c], block);
241
242     final(gsp, block, output_buffer, glob->buffer, BLOCKSIZE);
243 }
244
245 static void dec1(Real144_internal *glob, const int *data, const int *inp,
246                  int n, int f, int block_idx)
247 {
248     short *ptr,*end;
249     signed   short  *decsp = glob->gbuf2[block_idx];
250
251      *(glob->decptr++) = rms(data, f);
252     end = (ptr = decsp) + (n * 10);
253
254     while (ptr < end)
255         *(ptr++) = *(inp++);
256 }
257
258 static int eq(const short *in, int *target)
259 {
260     int retval;
261     int a;
262     int b;
263     int c;
264     unsigned int u;
265     const short *sptr;
266     int *ptr1, *ptr2, *ptr3;
267     int *bp1, *bp2;
268     int buffer1[10];
269     int buffer2[10];
270
271     retval = 0;
272     bp1 = buffer1;
273     bp2 = buffer2;
274     ptr2 = (ptr3 = buffer2) + 9;
275     sptr = in;
276
277     while (ptr2 >= ptr3)
278         *(ptr3++) = *(sptr++);
279
280     target += 9;
281     a = bp2[9];
282     *target = a;
283
284     if (a + 0x1000 > 0x1fff)
285         return 0; /* We're screwed, might as well go out with a bang. :P */
286
287     c = 8;
288     u = a;
289
290     while (c >= 0) {
291         if (u == 0x1000)
292             u++;
293
294         if (u == 0xfffff000)
295             u--;
296
297         b = 0x1000-((u * u) >> 12);
298
299         if (b == 0)
300             b++;
301
302         ptr2 = bp1;
303         ptr1 = (ptr3 = bp2) + c;
304
305         for (u=0; u<=c; u++)
306             *(ptr2++) = ((*(ptr3++) - (((*target) * (*(ptr1--))) >> 12)) * (0x1000000 / b)) >> 12;
307
308         *(--target) = u = bp1[(c--)];
309
310         if ((u + 0x1000) > 0x1fff)
311             retval = 1;
312
313         FFSWAP(int *, bp1, bp2);
314     }
315     return retval;
316 }
317
318 static void dec2(Real144_internal *glob, const int *data, const int *inp,
319                  int n, int f, const int *inp2, int l)
320 {
321     unsigned const int *ptr1,*ptr2;
322     int work[10];
323     int a,b;
324     int x;
325     int result;
326     signed   short *decsp = glob->gbuf2[l];
327     unsigned short *sptr  = decsp;
328
329     if(l + 1 < NBLOCKS / 2)
330         a = NBLOCKS - (l + 1);
331     else
332         a = l + 1;
333
334     b = NBLOCKS - a;
335
336     if (l == 0) {
337         glob->decptr = glob->gbuf1;
338     }
339     ptr1 = inp;
340     ptr2 = inp2;
341
342     for (x=0; x<10*n; x++)
343         *(sptr++) = (a * (*ptr1++) + b * (*ptr2++)) >> 2;
344
345     result = eq(decsp, work);
346
347     if (result == 1) {
348         dec1(glob, data, inp, n, f, l);
349     } else {
350         *(glob->decptr++) = rms(work, f);
351     }
352 }
353
354 /* Uncompress one block (20 bytes -> 160*2 bytes) */
355 static int ra144_decode_frame(AVCodecContext * avctx,
356             void *vdata, int *data_size,
357             const uint8_t * buf, int buf_size)
358 {
359     static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
360     unsigned int a, c;
361     int i;
362     int16_t *data = vdata;
363     unsigned int val;
364
365     Real144_internal *glob = avctx->priv_data;
366     GetBitContext gb;
367
368     if(buf_size == 0)
369         return 0;
370
371     init_get_bits(&gb, buf, 20 * 8);
372
373     for (i=0; i<10; i++)
374         // "<< 1"? Doesn't this make one value out of two of the table useless?
375         glob->swapbuf1[i] = decodetable[i][get_bits(&gb, sizes[i]) << 1];
376
377     do_voice(glob->swapbuf1, glob->swapbuf2);
378
379     val = decodeval[get_bits(&gb, 5) << 1]; // Useless table entries?
380     a = t_sqrt(val*glob->oldval) >> 12;
381
382     dec2(glob, glob->swapbuf1alt, glob->swapbuf2alt, 3, glob->oldval, glob->swapbuf2, 0);
383     if (glob->oldval < val) {
384         dec2(glob, glob->swapbuf1, glob->swapbuf2, 3, a, glob->swapbuf2alt, 1);
385     } else {
386         dec2(glob, glob->swapbuf1alt, glob->swapbuf2alt, 3, a, glob->swapbuf2, 1);
387     }
388     dec2(glob, glob->swapbuf1, glob->swapbuf2, 3, val, glob->swapbuf2alt, 2);
389     dec1(glob, glob->swapbuf1, glob->swapbuf2, 3, val, 3);
390
391     /* do output */
392     for (c=0; c<4; c++) {
393         do_output_subblock(glob, glob->gbuf2[c], glob->gbuf1[c], data, &gb);
394
395         for (i=0; i<BLOCKSIZE; i++) {
396             *data = av_clip_int16(*data << 2);
397             data++;
398         }
399     }
400
401     glob->oldval = val;
402
403     FFSWAP(unsigned int *, glob->swapbuf1alt, glob->swapbuf1);
404     FFSWAP(unsigned int *, glob->swapbuf2alt, glob->swapbuf2);
405
406     *data_size = 2*160;
407     return 20;
408 }
409
410
411 AVCodec ra_144_decoder =
412 {
413     "real_144",
414     CODEC_TYPE_AUDIO,
415     CODEC_ID_RA_144,
416     sizeof(Real144_internal),
417     ra144_decode_init,
418     NULL,
419     NULL,
420     ra144_decode_frame,
421     .long_name = "RealAudio 1.0 (14.4K)",
422 };