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