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