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 "ra288.h"
00022
00023 typedef struct {
00024 float history[8];
00025 float output[40];
00026 float pr1[36];
00027 float pr2[10];
00028 int phase, phasep;
00029
00030 float st1a[111],st1b[37],st1[37];
00031 float st2a[38],st2b[11],st2[11];
00032 float sb[41];
00033 float lhist[10];
00034 } Real288_internal;
00035
00036 static int ra288_decode_init(AVCodecContext * avctx)
00037 {
00038 Real288_internal *glob=avctx->priv_data;
00039 memset(glob,0,sizeof(Real288_internal));
00040 return 0;
00041 }
00042
00043 static void prodsum(float *tgt, float *src, int len, int n);
00044 static void co(int n, int i, int j, float *in, float *out, float *st1, float *st2, const float *table);
00045 static int pred(float *in, float *tgt, int n);
00046 static void colmult(float *tgt, float *m1, const float *m2, int n);
00047
00048
00049
00050 static void unpack(unsigned short *tgt, unsigned char *src, unsigned int len)
00051 {
00052 int x,y,z;
00053 int n,temp;
00054 int buffer[len];
00055
00056 for (x=0;x<len;tgt[x++]=0)
00057 buffer[x]=9+(x&1);
00058
00059 for (x=y=z=0;x<len;x++) {
00060 n=buffer[y]-z;
00061 temp=src[x];
00062 if (n<8) temp&=255>>(8-n);
00063 tgt[y]+=temp<<z;
00064 if (n<=8) {
00065 tgt[++y]+=src[x]>>n;
00066 z=8-n;
00067 } else z+=8;
00068 }
00069 }
00070
00071 static void update(Real288_internal *glob)
00072 {
00073 int x,y;
00074 float buffer1[40],temp1[37];
00075 float buffer2[8],temp2[11];
00076
00077 for (x=0,y=glob->phasep+5;x<40;buffer1[x++]=glob->output[(y++)%40]);
00078 co(36,40,35,buffer1,temp1,glob->st1a,glob->st1b,table1);
00079 if (pred(temp1,glob->st1,36))
00080 colmult(glob->pr1,glob->st1,table1a,36);
00081
00082 for (x=0,y=glob->phase+1;x<8;buffer2[x++]=glob->history[(y++)%8]);
00083 co(10,8,20,buffer2,temp2,glob->st2a,glob->st2b,table2);
00084 if (pred(temp2,glob->st2,10))
00085 colmult(glob->pr2,glob->st2,table2a,10);
00086 }
00087
00088
00089 static void decode(Real288_internal *glob, unsigned int input)
00090 {
00091 unsigned int x,y;
00092 float f;
00093 double sum,sumsum;
00094 float *p1,*p2;
00095 float buffer[5];
00096 const float *table;
00097
00098 for (x=36;x--;glob->sb[x+5]=glob->sb[x]);
00099 for (x=5;x--;) {
00100 p1=glob->sb+x;p2=glob->pr1;
00101 for (sum=0,y=36;y--;sum-=(*(++p1))*(*(p2++)));
00102 glob->sb[x]=sum;
00103 }
00104
00105 f=amptable[input&7];
00106 table=codetable+(input>>3)*5;
00107
00108
00109 for (sum=32,x=10;x--;sum-=glob->pr2[x]*glob->lhist[x]);
00110 if (sum<0) sum=0; else if (sum>60) sum=60;
00111
00112 sumsum=exp(sum*0.1151292546497)*f;
00113 for (sum=0,x=5;x--;) { buffer[x]=table[x]*sumsum; sum+=buffer[x]*buffer[x]; }
00114 if ((sum/=5)<1) sum=1;
00115
00116
00117 for (x=10;--x;glob->lhist[x]=glob->lhist[x-1]);
00118 *glob->lhist=glob->history[glob->phase]=10*log10(sum)-32;
00119
00120 for (x=1;x<5;x++) for (y=x;y--;buffer[x]-=glob->pr1[x-y-1]*buffer[y]);
00121
00122
00123 for (x=0;x<5;x++) {
00124 f=glob->sb[4-x]+buffer[x];
00125 if (f>4095) f=4095; else if (f<-4095) f=-4095;
00126 glob->output[glob->phasep+x]=glob->sb[4-x]=f;
00127 }
00128 }
00129
00130
00131 static void colmult(float *tgt, float *m1, const float *m2, int n)
00132 {
00133 while (n--)
00134 *(tgt++)=(*(m1++))*(*(m2++));
00135 }
00136
00137 static int pred(float *in, float *tgt, int n)
00138 {
00139 int x,y;
00140 float *p1,*p2;
00141 double f0,f1,f2;
00142 float temp;
00143
00144 if (in[n]==0) return 0;
00145 if ((f0=*in)<=0) return 0;
00146
00147 for (x=1;;x++) {
00148 if (n<x) return 1;
00149
00150 p1=in+x;
00151 p2=tgt;
00152 f1=*(p1--);
00153 for (y=x;--y;f1+=(*(p1--))*(*(p2++)));
00154
00155 p1=tgt+x-1;
00156 p2=tgt;
00157 *(p1--)=f2=-f1/f0;
00158 for (y=x>>1;y--;) {
00159 temp=*p2+*p1*f2;
00160 *(p1--)+=*p2*f2;
00161 *(p2++)=temp;
00162 }
00163 if ((f0+=f1*f2)<0) return 0;
00164 }
00165 }
00166
00167 static void co(int n, int i, int j, float *in, float *out, float *st1, float *st2, const float *table)
00168 {
00169 int a,b,c;
00170 unsigned int x;
00171 float *fp;
00172 float buffer1[37];
00173 float buffer2[37];
00174 float work[111];
00175
00176
00177 c=(b=(a=n+i)+j)-i;
00178 fp=st1+i;
00179 for (x=0;x<b;x++) {
00180 if (x==c) fp=in;
00181 work[x]=*(table++)*(*(st1++)=*(fp++));
00182 }
00183
00184 prodsum(buffer1,work+n,i,n);
00185 prodsum(buffer2,work+a,j,n);
00186
00187 for (x=0;x<=n;x++) {
00188 *st2=*st2*(0.5625)+buffer1[x];
00189 out[x]=*(st2++)+buffer2[x];
00190 }
00191 *out*=1.00390625;
00192 }
00193
00194
00195 static void prodsum(float *tgt, float *src, int len, int n)
00196 {
00197 unsigned int x;
00198 float *p1,*p2;
00199 double sum;
00200
00201 while (n>=0)
00202 {
00203 p1=(p2=src)-n;
00204 for (sum=0,x=len;x--;sum+=(*p1++)*(*p2++));
00205 tgt[n--]=sum;
00206 }
00207 }
00208
00209 static void * decode_block(AVCodecContext * avctx, unsigned char *in, signed short int *out,unsigned len)
00210 {
00211 int x,y;
00212 Real288_internal *glob=avctx->priv_data;
00213 unsigned short int buffer[len];
00214
00215 unpack(buffer,in,len);
00216 for (x=0;x<32;x++)
00217 {
00218 glob->phasep=(glob->phase=x&7)*5;
00219 decode(glob,buffer[x]);
00220 for (y=0;y<5;*(out++)=8*glob->output[glob->phasep+(y++)]);
00221 if (glob->phase==3) update(glob);
00222 }
00223 return out;
00224 }
00225
00226
00227 static int ra288_decode_frame(AVCodecContext * avctx,
00228 void *data, int *data_size,
00229 uint8_t * buf, int buf_size)
00230 {
00231 if(avctx->extradata_size>=6)
00232 {
00233
00234
00235
00236
00237
00238
00239 int bret;
00240 void *datao;
00241 int w=avctx->block_align;
00242 int h=((short*)(avctx->extradata))[1];
00243 int cfs=((short*)(avctx->extradata))[3];
00244 int i,j;
00245 if(buf_size<w*h)
00246 {
00247 av_log(avctx, AV_LOG_ERROR, "ffra288: Error! Input buffer is too small [%d<%d]\n",buf_size,w*h);
00248 return 0;
00249 }
00250 datao = data;
00251 bret = 0;
00252 for (j = 0; j < h/2; j++)
00253 for (i = 0; i < h; i++)
00254 {
00255 data=decode_block(avctx,&buf[j*cfs+cfs*i*h/2],(signed short *)data,cfs);
00256 bret += cfs;
00257 }
00258 *data_size = (char *)data - (char *)datao;
00259 return bret;
00260 }
00261 else
00262 {
00263 av_log(avctx, AV_LOG_ERROR, "ffra288: Error: need extra data!!!\n");
00264 return 0;
00265 }
00266 }
00267
00268 AVCodec ra_288_decoder =
00269 {
00270 "real_288",
00271 CODEC_TYPE_AUDIO,
00272 CODEC_ID_RA_288,
00273 sizeof(Real288_internal),
00274 ra288_decode_init,
00275 NULL,
00276 NULL,
00277 ra288_decode_frame,
00278 };