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