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