00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "avcodec.h"
00021 #include "ra144.h"
00022
00023 #define DATABLOCK1 20
00024 #define DATACHUNK1 1440
00025 #define AUDIOBLOCK 160
00026 #define AUDIOBUFFER 12288
00027
00028 #define NBLOCKS 4
00029 #define BLOCKSIZE 40
00030 #define HALFBLOCK 20
00031 #define BUFFERSIZE 146
00032
00033
00034
00035 typedef struct {
00036 unsigned int resetflag, val, oldval;
00037 unsigned int unpacked[28];
00038 unsigned int *iptr;
00039 unsigned int gval;
00040 unsigned short *gsp;
00041 unsigned int gbuf1[8];
00042 unsigned short gbuf2[120];
00043 signed short output_buffer[40];
00044 unsigned int *decptr;
00045 signed short *decsp;
00046
00047
00048 unsigned int swapb1a[10];
00049 unsigned int swapb2a[10];
00050 unsigned int swapb1b[10];
00051 unsigned int swapb2b[10];
00052 unsigned int *swapbuf1;
00053 unsigned int *swapbuf2;
00054 unsigned int *swapbuf1alt;
00055 unsigned int *swapbuf2alt;
00056
00057 unsigned int buffer[5];
00058 unsigned short int buffer_2[148];
00059 unsigned short int buffer_a[40];
00060 unsigned short int buffer_b[40];
00061 unsigned short int buffer_c[40];
00062 unsigned short int buffer_d[40];
00063
00064 unsigned short int work[50];
00065 unsigned short *sptr;
00066
00067 int buffer1[10];
00068 int buffer2[10];
00069
00070 signed short wavtable1[2304];
00071 unsigned short wavtable2[2304];
00072 } Real144_internal;
00073
00074 static int ra144_decode_init(AVCodecContext * avctx)
00075 {
00076 Real144_internal *glob=avctx->priv_data;
00077
00078 memset(glob,0,sizeof(Real144_internal));
00079 glob->resetflag=1;
00080 glob->swapbuf1=glob->swapb1a;
00081 glob->swapbuf2=glob->swapb2a;
00082 glob->swapbuf1alt=glob->swapb1b;
00083 glob->swapbuf2alt=glob->swapb2b;
00084
00085 memcpy(glob->wavtable1,wavtable1,sizeof(wavtable1));
00086 memcpy(glob->wavtable2,wavtable2,sizeof(wavtable2));
00087
00088 return 0;
00089 }
00090
00091 static void final(Real144_internal *glob, short *i1, short *i2, void *out, int *statbuf, int len);
00092 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);
00093 static int irms(short *data, int factor);
00094 static void rotate_block(short *source, short *target, int offset);
00095
00096 static int t_sqrt(unsigned int x)
00097 {
00098 int s=0;
00099 while (x>0xfff) { s++; x=x>>2; }
00100 return (sqrt_table[x]<<s)<<2;
00101 }
00102
00103
00104 static void do_voice(int *a1, int *a2)
00105 {
00106 int buffer[10];
00107 int *b1,*b2;
00108 int x,y;
00109 int *ptr,*tmp;
00110
00111 b1=buffer;
00112 b2=a2;
00113
00114 for (x=0;x<10;x++) {
00115 b1[x]=(*a1)<<4;
00116
00117 if(x>0) {
00118 ptr=b2+x;
00119 for (y=0;y<=x-1;y++)
00120 b1[y]=(((*a1)*(*(--ptr)))>>12)+b2[y];
00121 }
00122 tmp=b1;
00123 b1=b2;
00124 b2=tmp;
00125 a1++;
00126 }
00127 ptr=a2+10;
00128 while (ptr>a2) (*a2++)>>=4;
00129 }
00130
00131
00132
00133 static void do_output_subblock(Real144_internal *glob, unsigned int x)
00134 {
00135 int a,b,c,d,e,f,g;
00136
00137 if (x==1) memset(glob->buffer,0,20);
00138 if ((*glob->iptr)==0) a=0;
00139 else a=(*glob->iptr)+HALFBLOCK-1;
00140 glob->iptr++;
00141 b=*(glob->iptr++);
00142 c=*(glob->iptr++);
00143 d=*(glob->iptr++);
00144 if (a) rotate_block(glob->buffer_2,glob->buffer_a,a);
00145 memcpy(glob->buffer_b,etable1+b*BLOCKSIZE,BLOCKSIZE*2);
00146 e=((ftable1[b]>>4)*glob->gval)>>8;
00147 memcpy(glob->buffer_c,etable2+c*BLOCKSIZE,BLOCKSIZE*2);
00148 f=((ftable2[c]>>4)*glob->gval)>>8;
00149 if (a) g=irms(glob->buffer_a,glob->gval)>>12;
00150 else g=0;
00151 add_wav(glob,d,a,g,e,f,glob->buffer_a,glob->buffer_b,glob->buffer_c,glob->buffer_d);
00152 memmove(glob->buffer_2,glob->buffer_2+BLOCKSIZE,(BUFFERSIZE-BLOCKSIZE)*2);
00153 memcpy(glob->buffer_2+BUFFERSIZE-BLOCKSIZE,glob->buffer_d,BLOCKSIZE*2);
00154 final(glob,glob->gsp,glob->buffer_d,glob->output_buffer,glob->buffer,BLOCKSIZE);
00155 }
00156
00157
00158 static void rotate_block(short *source, short *target, int offset)
00159 {
00160 short *end;
00161 short *ptr1;
00162 short *ptr2;
00163 short *ptr3;
00164 ptr2=source+BUFFERSIZE;
00165 ptr3=ptr1=ptr2-offset;
00166 end=target+BLOCKSIZE;
00167 while (target<end) {
00168 *(target++)=*(ptr3++);
00169 if (ptr3==ptr2) ptr3=ptr1;
00170 }
00171 }
00172
00173
00174 static int irms(short *data, int factor)
00175 {
00176 short *p1,*p2;
00177 unsigned int sum;
00178 p2=(p1=data)+BLOCKSIZE;
00179 for (sum=0;p2>p1;p1++) sum+=(*p1)*(*p1);
00180 if (sum==0) return 0;
00181 return (0x20000000/(t_sqrt(sum)>>8))*factor;
00182 }
00183
00184
00185 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)
00186 {
00187 int a,b,c;
00188 short *ptr,*ptr2;
00189
00190 ptr=glob->wavtable1+n*9;
00191 ptr2=glob->wavtable2+n*9;
00192 if (f!=0) {
00193 a=((*ptr)*m1)>>((*ptr2)+1);
00194 } else {
00195 a=0;
00196 }
00197 ptr++;ptr2++;
00198 b=((*ptr)*m2)>>((*ptr2)+1);
00199 ptr++;ptr2++;
00200 c=((*ptr)*m3)>>((*ptr2)+1);
00201 ptr2=(ptr=dest)+BLOCKSIZE;
00202 if (f!=0)
00203 while (ptr<ptr2)
00204 *(ptr++)=((*(s1++))*a+(*(s2++))*b+(*(s3++))*c)>>12;
00205 else
00206 while (ptr<ptr2)
00207 *(ptr++)=((*(s2++))*b+(*(s3++))*c)>>12;
00208 }
00209
00210
00211 static void final(Real144_internal *glob, short *i1, short *i2, void *out, int *statbuf, int len)
00212 {
00213 int x,sum;
00214 int buffer[10];
00215 short *ptr;
00216 short *ptr2;
00217
00218 memcpy(glob->work,statbuf,20);
00219 memcpy(glob->work+10,i2,len*2);
00220
00221 buffer[9]=i1[0];
00222 buffer[8]=i1[1];
00223 buffer[7]=i1[2];
00224 buffer[6]=i1[3];
00225 buffer[5]=i1[4];
00226 buffer[4]=i1[5];
00227 buffer[3]=i1[6];
00228 buffer[2]=i1[7];
00229 buffer[1]=i1[8];
00230 buffer[0]=i1[9];
00231
00232 ptr2=(ptr=glob->work)+len;
00233 while (ptr<ptr2) {
00234 for(sum=0,x=0;x<=9;x++)
00235 sum+=buffer[x]*(ptr[x]);
00236 sum=sum>>12;
00237 x=ptr[10]-sum;
00238 if (x<-32768 || x>32767)
00239 {
00240 memset(out,0,len*2);
00241 memset(statbuf,0,20);
00242 return;
00243 }
00244 ptr[10]=x;
00245 ptr++;
00246 }
00247 memcpy(out,ptr+10-len,len*2);
00248 memcpy(statbuf,ptr,20);
00249 }
00250
00251
00252 static void unpack_input(unsigned char *input, unsigned int *output)
00253 {
00254 unsigned int outbuffer[28];
00255 unsigned short inbuffer[10];
00256 unsigned int x;
00257 unsigned int *ptr;
00258
00259
00260 for (x=0;x<20;x+=2)
00261 inbuffer[x/2]=(input[x]<<8)+input[x+1];
00262
00263
00264 ptr=outbuffer;
00265 *(ptr++)=27;
00266 *(ptr++)=(inbuffer[0]>>10)&0x3f;
00267 *(ptr++)=(inbuffer[0]>>5)&0x1f;
00268 *(ptr++)=inbuffer[0]&0x1f;
00269 *(ptr++)=(inbuffer[1]>>12)&0xf;
00270 *(ptr++)=(inbuffer[1]>>8)&0xf;
00271 *(ptr++)=(inbuffer[1]>>5)&7;
00272 *(ptr++)=(inbuffer[1]>>2)&7;
00273 *(ptr++)=((inbuffer[1]<<1)&6)|((inbuffer[2]>>15)&1);
00274 *(ptr++)=(inbuffer[2]>>12)&7;
00275 *(ptr++)=(inbuffer[2]>>10)&3;
00276 *(ptr++)=(inbuffer[2]>>5)&0x1f;
00277 *(ptr++)=((inbuffer[2]<<2)&0x7c)|((inbuffer[3]>>14)&3);
00278 *(ptr++)=(inbuffer[3]>>6)&0xff;
00279 *(ptr++)=((inbuffer[3]<<1)&0x7e)|((inbuffer[4]>>15)&1);
00280 *(ptr++)=(inbuffer[4]>>8)&0x7f;
00281 *(ptr++)=(inbuffer[4]>>1)&0x7f;
00282 *(ptr++)=((inbuffer[4]<<7)&0x80)|((inbuffer[5]>>9)&0x7f);
00283 *(ptr++)=(inbuffer[5]>>2)&0x7f;
00284 *(ptr++)=((inbuffer[5]<<5)&0x60)|((inbuffer[6]>>11)&0x1f);
00285 *(ptr++)=(inbuffer[6]>>4)&0x7f;
00286 *(ptr++)=((inbuffer[6]<<4)&0xf0)|((inbuffer[7]>>12)&0xf);
00287 *(ptr++)=(inbuffer[7]>>5)&0x7f;
00288 *(ptr++)=((inbuffer[7]<<2)&0x7c)|((inbuffer[8]>>14)&3);
00289 *(ptr++)=(inbuffer[8]>>7)&0x7f;
00290 *(ptr++)=((inbuffer[8]<<1)&0xfe)|((inbuffer[9]>>15)&1);
00291 *(ptr++)=(inbuffer[9]>>8)&0x7f;
00292 *(ptr++)=(inbuffer[9]>>1)&0x7f;
00293
00294 *(output++)=outbuffer[11];
00295 for (x=1;x<11;*(output++)=outbuffer[x++]);
00296 ptr=outbuffer+12;
00297 for (x=0;x<16;x+=4)
00298 {
00299 *(output++)=ptr[x];
00300 *(output++)=ptr[x+2];
00301 *(output++)=ptr[x+3];
00302 *(output++)=ptr[x+1];
00303 }
00304 }
00305
00306 static unsigned int rms(int *data, int f)
00307 {
00308 int *c;
00309 int x;
00310 unsigned int res;
00311 int b;
00312
00313 c=data;
00314 b=0;
00315 res=0x10000;
00316 for (x=0;x<10;x++)
00317 {
00318 res=(((0x1000000-(*c)*(*c))>>12)*res)>>12;
00319 if (res==0) return 0;
00320 if (res<=0x3fff)
00321 {
00322 while (res<=0x3fff)
00323 {
00324 b++;
00325 res<<=2;
00326 }
00327 } else {
00328 if (res>0x10000)
00329 return 0;
00330 }
00331 c++;
00332 }
00333 if (res>0) res=t_sqrt(res);
00334
00335 res>>=(b+10);
00336 res=(res*f)>>10;
00337 return res;
00338 }
00339
00340 static void dec1(Real144_internal *glob, int *data, int *inp, int n, int f)
00341 {
00342 short *ptr,*end;
00343
00344 *(glob->decptr++)=rms(data,f);
00345 glob->decptr++;
00346 end=(ptr=glob->decsp)+(n*10);
00347 while (ptr<end) *(ptr++)=*(inp++);
00348 }
00349
00350 static int eq(Real144_internal *glob, short *in, int *target)
00351 {
00352 int retval;
00353 int a;
00354 int b;
00355 int c;
00356 unsigned int u;
00357 short *sptr;
00358 int *ptr1,*ptr2,*ptr3;
00359 int *bp1,*bp2,*temp;
00360
00361 retval=0;
00362 bp1=glob->buffer1;
00363 bp2=glob->buffer2;
00364 ptr2=(ptr3=glob->buffer2)+9;
00365 sptr=in;
00366 while (ptr2>=ptr3)
00367 *(ptr3++)=*(sptr++);
00368
00369 target+=9;
00370 a=bp2[9];
00371 *target=a;
00372 if (a+0x1000>0x1fff)
00373 return 0;
00374 c=8;u=a;
00375 while (c>=0)
00376 {
00377 if (u==0x1000) u++;
00378 if (u==0xfffff000) u--;
00379 b=0x1000-((u*u)>>12);
00380 if (b==0) b++;
00381 ptr2=bp1;
00382 ptr1=(ptr3=bp2)+c;
00383 for (u=0;u<=c;u++)
00384 *(ptr2++)=((*(ptr3++)-(((*target)*(*(ptr1--)))>>12))*(0x1000000/b))>>12;
00385 *(--target)=u=bp1[(c--)];
00386 if ((u+0x1000)>0x1fff) retval=1;
00387 temp=bp2;
00388 bp2=bp1;
00389 bp1=temp;
00390 }
00391 return retval;
00392 }
00393
00394 static void dec2(Real144_internal *glob, int *data, int *inp, int n, int f, int *inp2, int l)
00395 {
00396 unsigned int *ptr1,*ptr2;
00397 int work[10];
00398 int a,b;
00399 int x;
00400 int result;
00401
00402 if(l+1<NBLOCKS/2) a=NBLOCKS-(l+1);
00403 else a=l+1;
00404 b=NBLOCKS-a;
00405 if (l==0)
00406 {
00407 glob->decsp=glob->sptr=glob->gbuf2;
00408 glob->decptr=glob->gbuf1;
00409 }
00410 ptr1=inp;
00411 ptr2=inp2;
00412 for (x=0;x<10*n;x++)
00413 *(glob->sptr++)=(a*(*ptr1++)+b*(*ptr2++))>>2;
00414 result=eq(glob,glob->decsp,work);
00415 if (result==1)
00416 {
00417 dec1(glob,data,inp,n,f);
00418 } else {
00419 *(glob->decptr++)=rms(work,f);
00420 glob->decptr++;
00421 }
00422 glob->decsp+=n*10;
00423 }
00424
00425
00426 static int ra144_decode_frame(AVCodecContext * avctx,
00427 void *vdata, int *data_size,
00428 uint8_t * buf, int buf_size)
00429 {
00430 unsigned int a,b,c;
00431 long s;
00432 signed short *shptr;
00433 unsigned int *lptr,*temp;
00434 const short **dptr;
00435 int16_t *datao;
00436 int16_t *data = vdata;
00437 Real144_internal *glob=avctx->priv_data;
00438
00439 if(buf_size==0)
00440 return 0;
00441
00442 datao = data;
00443 unpack_input(buf,glob->unpacked);
00444
00445 glob->iptr=glob->unpacked;
00446 glob->val=decodetable[0][(*(glob->iptr++))<<1];
00447
00448 dptr=decodetable+1;
00449 lptr=glob->swapbuf1;
00450 while (lptr<glob->swapbuf1+10)
00451 *(lptr++)=(*(dptr++))[(*(glob->iptr++))<<1];
00452
00453 do_voice(glob->swapbuf1,glob->swapbuf2);
00454
00455 a=t_sqrt(glob->val*glob->oldval)>>12;
00456
00457 for (c=0;c<NBLOCKS;c++) {
00458 if (c==(NBLOCKS-1)) {
00459 dec1(glob,glob->swapbuf1,glob->swapbuf2,3,glob->val);
00460 } else {
00461 if (c*2==(NBLOCKS-2)) {
00462 if (glob->oldval<glob->val) {
00463 dec2(glob,glob->swapbuf1,glob->swapbuf2,3,a,glob->swapbuf2alt,c);
00464 } else {
00465 dec2(glob,glob->swapbuf1alt,glob->swapbuf2alt,3,a,glob->swapbuf2,c);
00466 }
00467 } else {
00468 if (c*2<(NBLOCKS-2)) {
00469 dec2(glob,glob->swapbuf1alt,glob->swapbuf2alt,3,glob->oldval,glob->swapbuf2,c);
00470 } else {
00471 dec2(glob,glob->swapbuf1,glob->swapbuf2,3,glob->val,glob->swapbuf2alt,c);
00472 }
00473 }
00474 }
00475 }
00476
00477
00478 for (b=0,c=0;c<4;c++) {
00479 glob->gval=glob->gbuf1[c*2];
00480 glob->gsp=glob->gbuf2+b;
00481 do_output_subblock(glob,glob->resetflag);
00482 glob->resetflag=0;
00483
00484 shptr=glob->output_buffer;
00485 while (shptr<glob->output_buffer+BLOCKSIZE) {
00486 s=*(shptr++)<<2;
00487 *data=s;
00488 if (s>32767) *data=32767;
00489 if (s<-32767) *data=-32768;
00490 data++;
00491 }
00492 b+=30;
00493 }
00494
00495 glob->oldval=glob->val;
00496 temp=glob->swapbuf1alt;
00497 glob->swapbuf1alt=glob->swapbuf1;
00498 glob->swapbuf1=temp;
00499 temp=glob->swapbuf2alt;
00500 glob->swapbuf2alt=glob->swapbuf2;
00501 glob->swapbuf2=temp;
00502 *data_size=(data-datao)*sizeof(*data);
00503 return 20;
00504 }
00505
00506
00507 AVCodec ra_144_decoder =
00508 {
00509 "real_144",
00510 CODEC_TYPE_AUDIO,
00511 CODEC_ID_RA_144,
00512 sizeof(Real144_internal),
00513 ra144_decode_init,
00514 NULL,
00515 NULL,
00516 ra144_decode_frame,
00517 };