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