Cosmetics: indentation
[ffmpeg-lucabe.git] / libavcodec / ra144.c
blobe1ac53f4b02af02720c823e84124029cfb043165
1 /*
2 * Real Audio 1.0 (14.4K)
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
22 #include "avcodec.h"
23 #include "bitstream.h"
24 #include "ra144.h"
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 */
32 /* internal globals */
33 typedef struct {
34 unsigned int oldval;
35 unsigned int gbuf1[4];
36 unsigned short gbuf2[120];
37 unsigned int *decptr; /* decoder ptr */
38 signed short *decsp;
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;
47 unsigned int buffer[5];
48 unsigned short int buffer_2[148];
50 unsigned short *sptr;
51 } Real144_internal;
53 static int ra144_decode_init(AVCodecContext * avctx)
55 Real144_internal *glob = avctx->priv_data;
57 glob->swapbuf1 = glob->swapbuffers[0];
58 glob->swapbuf2 = glob->swapbuffers[1];
59 glob->swapbuf1alt = glob->swapbuffers[2];
60 glob->swapbuf2alt = glob->swapbuffers[3];
62 return 0;
65 /* lookup square roots in table */
66 static int t_sqrt(unsigned int x)
68 int s = 0;
69 while (x > 0xfff) {
70 s++;
71 x = x >> 2;
74 return (ff_sqrt(x << 20) << s) << 2;
77 /* do 'voice' */
78 static void do_voice(const int *a1, int *a2)
80 int buffer[10];
81 int *b1 = buffer;
82 int *b2 = a2;
83 int x, y;
85 for (x=0; x < 10; x++) {
86 b1[x] = a1[x] << 4;
88 for (y=0; y < x; y++)
89 b1[y] = ((a1[x] * b2[x-y-1]) >> 12) + b2[y];
91 FFSWAP(int *, b1, b2);
94 for (x=0; x < 10; x++)
95 a2[x] >>= 4;
98 /* rotate block */
99 static void rotate_block(const short *source, short *target, int offset)
101 int i=0, k=0;
102 source += BUFFERSIZE - offset;
104 while (i<BLOCKSIZE) {
105 target[i++] = source[k++];
107 if (k == offset)
108 k = 0;
112 /* inverse root mean square */
113 static int irms(const short *data, int factor)
115 unsigned int i, sum = 0;
117 for (i=0; i < BLOCKSIZE; i++)
118 sum += data[i] * data[i];
120 if (sum == 0)
121 return 0; /* OOPS - division by zero */
123 return (0x20000000 / (t_sqrt(sum) >> 8)) * factor;
126 /* multiply/add wavetable */
127 static void add_wav(int n, int f, int m1, int m2, int m3, const short *s1,
128 const short *s2, const short *s3, short *dest)
130 int a = 0;
131 int b, c, i;
132 const short *ptr, *ptr2;
134 ptr = wavtable1 + n * 9;
135 ptr2 = wavtable2 + n * 9;
137 if (f)
138 a = (ptr[0] * m1) >> (ptr2[0] + 1);
140 b = (ptr[1] * m2) >> (ptr2[1] + 1);
141 c = (ptr[2] * m3) >> (ptr2[2] + 1);
143 for (i=0; i < BLOCKSIZE; i++)
144 dest[i] = ((*(s1++)) * a + (*(s2++)) * b + (*(s3++)) * c) >> 12;
148 static void final(const short *i1, const short *i2,
149 void *out, int *statbuf, int len)
151 int x, sum, i;
152 int buffer[10];
153 short *ptr;
154 short *ptr2;
155 unsigned short int work[50];
157 memcpy(work, statbuf,20);
158 memcpy(work + 10, i2, len * 2);
160 for(i=0; i<10; i++)
161 buffer[9-i] = i1[i];
163 ptr2 = (ptr = work) + len;
165 while (ptr < ptr2) {
166 for(sum=0, x=0; x<=9; x++)
167 sum += buffer[x] * (ptr[x]);
169 sum = sum >> 12;
170 x = ptr[10] - sum;
172 if (x<-32768 || x>32767) {
173 memset(out, 0, len * 2);
174 memset(statbuf, 0, 20);
175 return;
178 ptr[10] = x;
179 ptr++;
181 memcpy(out, ptr+10 - len, len * 2);
182 memcpy(statbuf, ptr, 20);
185 static unsigned int rms(const int *data, int f)
187 const int *c;
188 int x;
189 unsigned int res;
190 int b;
192 c = data;
193 b = 0;
194 res = 0x10000;
195 for (x=0; x<10; x++) {
196 res = (((0x1000000 - (*c) * (*c)) >> 12) * res) >> 12;
198 if (res == 0)
199 return 0;
201 if (res <= 0x3fff) {
202 while (res <= 0x3fff) {
203 b++;
204 res <<= 2;
206 } else {
207 if (res > 0x10000)
208 return 0; /* We're screwed, might as well go out with a bang. :P */
210 c++;
213 if (res > 0)
214 res = t_sqrt(res);
216 res >>= (b + 10);
217 res = (res * f) >> 10;
218 return res;
221 /* do quarter-block output */
222 static void do_output_subblock(Real144_internal *glob, const unsigned short *gsp, unsigned int gval, signed short *output_buffer, GetBitContext *gb)
224 unsigned short int buffer_a[40];
225 unsigned short int *block;
226 int e, f, g;
227 int a = get_bits(gb, 7);
228 int d = get_bits(gb, 8);
229 int b = get_bits(gb, 7);
230 int c = get_bits(gb, 7);
232 if (a) {
233 a += HALFBLOCK - 1;
234 rotate_block(glob->buffer_2, buffer_a, a);
237 e = ((ftable1[b] >> 4) * gval) >> 8;
238 f = ((ftable2[c] >> 4) * gval) >> 8;
240 if (a)
241 g = irms(buffer_a, gval) >> 12;
242 else
243 g = 0;
245 memmove(glob->buffer_2, glob->buffer_2 + BLOCKSIZE, (BUFFERSIZE - BLOCKSIZE) * 2);
246 block = glob->buffer_2 + BUFFERSIZE - BLOCKSIZE;
248 add_wav(d, a, g, e, f, buffer_a, etable1[b],
249 etable2[c], block);
251 final(gsp, block, output_buffer, glob->buffer, BLOCKSIZE);
254 static void dec1(Real144_internal *glob, const int *data, const int *inp,
255 int n, int f)
257 short *ptr,*end;
259 *(glob->decptr++) = rms(data, f);
260 end = (ptr = glob->decsp) + (n * 10);
262 while (ptr < end)
263 *(ptr++) = *(inp++);
266 static int eq(const short *in, int *target)
268 int retval;
269 int a;
270 int b;
271 int c;
272 unsigned int u;
273 const short *sptr;
274 int *ptr1, *ptr2, *ptr3;
275 int *bp1, *bp2;
276 int buffer1[10];
277 int buffer2[10];
279 retval = 0;
280 bp1 = buffer1;
281 bp2 = buffer2;
282 ptr2 = (ptr3 = buffer2) + 9;
283 sptr = in;
285 while (ptr2 >= ptr3)
286 *(ptr3++) = *(sptr++);
288 target += 9;
289 a = bp2[9];
290 *target = a;
292 if (a + 0x1000 > 0x1fff)
293 return 0; /* We're screwed, might as well go out with a bang. :P */
295 c = 8;
296 u = a;
298 while (c >= 0) {
299 if (u == 0x1000)
300 u++;
302 if (u == 0xfffff000)
303 u--;
305 b = 0x1000-((u * u) >> 12);
307 if (b == 0)
308 b++;
310 ptr2 = bp1;
311 ptr1 = (ptr3 = bp2) + c;
313 for (u=0; u<=c; u++)
314 *(ptr2++) = ((*(ptr3++) - (((*target) * (*(ptr1--))) >> 12)) * (0x1000000 / b)) >> 12;
316 *(--target) = u = bp1[(c--)];
318 if ((u + 0x1000) > 0x1fff)
319 retval = 1;
321 FFSWAP(int *, bp1, bp2);
323 return retval;
326 static void dec2(Real144_internal *glob, const int *data, const int *inp,
327 int n, int f, const int *inp2, int l)
329 unsigned const int *ptr1,*ptr2;
330 int work[10];
331 int a,b;
332 int x;
333 int result;
335 if(l + 1 < NBLOCKS / 2)
336 a = NBLOCKS - (l + 1);
337 else
338 a = l + 1;
340 b = NBLOCKS - a;
342 if (l == 0) {
343 glob->decsp = glob->sptr = glob->gbuf2;
344 glob->decptr = glob->gbuf1;
346 ptr1 = inp;
347 ptr2 = inp2;
349 for (x=0; x<10*n; x++)
350 *(glob->sptr++) = (a * (*ptr1++) + b * (*ptr2++)) >> 2;
352 result = eq(glob->decsp, work);
354 if (result == 1) {
355 dec1(glob, data, inp, n, f);
356 } else {
357 *(glob->decptr++) = rms(work, f);
359 glob->decsp += n * 10;
362 /* Uncompress one block (20 bytes -> 160*2 bytes) */
363 static int ra144_decode_frame(AVCodecContext * avctx,
364 void *vdata, int *data_size,
365 const uint8_t * buf, int buf_size)
367 static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
368 unsigned int a, b, c;
369 int i;
370 signed short *shptr;
371 int16_t *data = vdata;
372 unsigned int val;
374 Real144_internal *glob = avctx->priv_data;
375 GetBitContext gb;
377 if(buf_size == 0)
378 return 0;
380 init_get_bits(&gb, buf, 20 * 8);
382 for (i=0; i<10; i++)
383 // "<< 1"? Doesn't this make one value out of two of the table useless?
384 glob->swapbuf1[i] = decodetable[i][get_bits(&gb, sizes[i]) << 1];
386 do_voice(glob->swapbuf1, glob->swapbuf2);
388 val = decodeval[get_bits(&gb, 5) << 1]; // Useless table entries?
389 a = t_sqrt(val*glob->oldval) >> 12;
391 dec2(glob, glob->swapbuf1alt, glob->swapbuf2alt, 3, glob->oldval, glob->swapbuf2, 0);
392 if (glob->oldval < val) {
393 dec2(glob, glob->swapbuf1, glob->swapbuf2, 3, a, glob->swapbuf2alt, 1);
394 } else {
395 dec2(glob, glob->swapbuf1alt, glob->swapbuf2alt, 3, a, glob->swapbuf2, 1);
397 dec2(glob, glob->swapbuf1, glob->swapbuf2, 3, val, glob->swapbuf2alt, 2);
398 dec1(glob, glob->swapbuf1, glob->swapbuf2, 3, val);
400 /* do output */
401 for (b=0, c=0; c<4; c++) {
402 unsigned int gval = glob->gbuf1[c];
403 unsigned short *gsp = glob->gbuf2 + b;
404 signed short output_buffer[40];
406 do_output_subblock(glob, gsp, gval, output_buffer, &gb);
408 shptr = output_buffer;
409 while (shptr < output_buffer + BLOCKSIZE)
410 *data++ = av_clip_int16(*(shptr++) << 2);
411 b += 30;
414 glob->oldval = val;
416 FFSWAP(unsigned int *, glob->swapbuf1alt, glob->swapbuf1);
417 FFSWAP(unsigned int *, glob->swapbuf2alt, glob->swapbuf2);
419 *data_size = 2*160;
420 return 20;
424 AVCodec ra_144_decoder =
426 "real_144",
427 CODEC_TYPE_AUDIO,
428 CODEC_ID_RA_144,
429 sizeof(Real144_internal),
430 ra144_decode_init,
431 NULL,
432 NULL,
433 ra144_decode_frame,
434 .long_name = "RealAudio 1.0 (14.4K)",