comparison sonic.c @ 2979:bfabfdf9ce55 libavcodec

COSMETICS: tabs --> spaces, some prettyprinting
author diego
date Thu, 22 Dec 2005 01:10:11 +0000
parents ef2149182f1c
children 0b546eab515d
comparison
equal deleted inserted replaced
2978:403183bbb505 2979:bfabfdf9ce55
62 // for decoding 62 // for decoding
63 int *predictor_k; 63 int *predictor_k;
64 int *predictor_state[MAX_CHANNELS]; 64 int *predictor_state[MAX_CHANNELS];
65 } SonicContext; 65 } SonicContext;
66 66
67 #define LATTICE_SHIFT 10 67 #define LATTICE_SHIFT 10
68 #define SAMPLE_SHIFT 4 68 #define SAMPLE_SHIFT 4
69 #define LATTICE_FACTOR (1 << LATTICE_SHIFT) 69 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
70 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT) 70 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
71 71
72 #define BASE_QUANT 0.6 72 #define BASE_QUANT 0.6
73 #define RATE_VARIATION 3.0 73 #define RATE_VARIATION 3.0
74 74
75 static inline int divide(int a, int b) 75 static inline int divide(int a, int b)
76 { 76 {
77 if (a < 0) 77 if (a < 0)
78 return -( (-a + b/2)/b ); 78 return -( (-a + b/2)/b );
79 else 79 else
80 return (a + b/2)/b; 80 return (a + b/2)/b;
81 } 81 }
82 82
83 static inline int shift(int a,int b) 83 static inline int shift(int a,int b)
84 { 84 {
85 return (a+(1<<(b-1))) >> b; 85 return (a+(1<<(b-1))) >> b;
94 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part) 94 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
95 { 95 {
96 int i; 96 int i;
97 97
98 for (i = 0; i < entries; i++) 98 for (i = 0; i < entries; i++)
99 set_se_golomb(pb, buf[i]); 99 set_se_golomb(pb, buf[i]);
100 100
101 return 1; 101 return 1;
102 } 102 }
103 103
104 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part) 104 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
105 { 105 {
106 int i; 106 int i;
107 107
108 for (i = 0; i < entries; i++) 108 for (i = 0; i < entries; i++)
109 buf[i] = get_se_golomb(gb); 109 buf[i] = get_se_golomb(gb);
110 110
111 return 1; 111 return 1;
112 } 112 }
113 113
114 #else 114 #else
119 { 119 {
120 int res = 0; 120 int res = 0;
121 121
122 while(x) 122 while(x)
123 { 123 {
124 res++; 124 res++;
125 x >>= 1; 125 x >>= 1;
126 } 126 }
127 return res; 127 return res;
128 } 128 }
129 129
130 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max) 130 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
131 { 131 {
132 int i, bits; 132 int i, bits;
133 133
134 if (!max) 134 if (!max)
135 return; 135 return;
136 136
137 bits = bits_to_store(max); 137 bits = bits_to_store(max);
138 138
139 for (i = 0; i < bits-1; i++) 139 for (i = 0; i < bits-1; i++)
140 put_bits(pb, 1, value & (1 << i)); 140 put_bits(pb, 1, value & (1 << i));
141 141
142 if ( (value | (1 << (bits-1))) <= max) 142 if ( (value | (1 << (bits-1))) <= max)
143 put_bits(pb, 1, value & (1 << (bits-1))); 143 put_bits(pb, 1, value & (1 << (bits-1)));
144 } 144 }
145 145
146 static unsigned int read_uint_max(GetBitContext *gb, int max) 146 static unsigned int read_uint_max(GetBitContext *gb, int max)
147 { 147 {
148 int i, bits, value = 0; 148 int i, bits, value = 0;
149 149
150 if (!max) 150 if (!max)
151 return 0; 151 return 0;
152 152
153 bits = bits_to_store(max); 153 bits = bits_to_store(max);
154 154
155 for (i = 0; i < bits-1; i++) 155 for (i = 0; i < bits-1; i++)
156 if (get_bits1(gb)) 156 if (get_bits1(gb))
157 value += 1 << i; 157 value += 1 << i;
158 158
159 if ( (value | (1<<(bits-1))) <= max) 159 if ( (value | (1<<(bits-1))) <= max)
160 if (get_bits1(gb)) 160 if (get_bits1(gb))
161 value += 1 << (bits-1); 161 value += 1 << (bits-1);
162 162
163 return value; 163 return value;
164 } 164 }
165 165
166 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part) 166 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
169 int step = 256, pos = 0, dominant = 0, any = 0; 169 int step = 256, pos = 0, dominant = 0, any = 0;
170 int *copy, *bits; 170 int *copy, *bits;
171 171
172 copy = av_mallocz(4* entries); 172 copy = av_mallocz(4* entries);
173 if (!copy) 173 if (!copy)
174 return -1; 174 return -1;
175 175
176 if (base_2_part) 176 if (base_2_part)
177 { 177 {
178 int energy = 0; 178 int energy = 0;
179 179
180 for (i = 0; i < entries; i++) 180 for (i = 0; i < entries; i++)
181 energy += abs(buf[i]); 181 energy += abs(buf[i]);
182 182
183 low_bits = bits_to_store(energy / (entries * 2)); 183 low_bits = bits_to_store(energy / (entries * 2));
184 if (low_bits > 15) 184 if (low_bits > 15)
185 low_bits = 15; 185 low_bits = 15;
186 186
187 put_bits(pb, 4, low_bits); 187 put_bits(pb, 4, low_bits);
188 } 188 }
189 189
190 for (i = 0; i < entries; i++) 190 for (i = 0; i < entries; i++)
191 { 191 {
192 put_bits(pb, low_bits, abs(buf[i])); 192 put_bits(pb, low_bits, abs(buf[i]));
193 copy[i] = abs(buf[i]) >> low_bits; 193 copy[i] = abs(buf[i]) >> low_bits;
194 if (copy[i] > max) 194 if (copy[i] > max)
195 max = abs(copy[i]); 195 max = abs(copy[i]);
196 } 196 }
197 197
198 bits = av_mallocz(4* entries*max); 198 bits = av_mallocz(4* entries*max);
199 if (!bits) 199 if (!bits)
200 { 200 {
201 // av_free(copy); 201 // av_free(copy);
202 return -1; 202 return -1;
203 } 203 }
204 204
205 for (i = 0; i <= max; i++) 205 for (i = 0; i <= max; i++)
206 { 206 {
207 for (j = 0; j < entries; j++) 207 for (j = 0; j < entries; j++)
208 if (copy[j] >= i) 208 if (copy[j] >= i)
209 bits[x++] = copy[j] > i; 209 bits[x++] = copy[j] > i;
210 } 210 }
211 211
212 // store bitstream 212 // store bitstream
213 while (pos < x) 213 while (pos < x)
214 { 214 {
215 int steplet = step >> 8; 215 int steplet = step >> 8;
216 216
217 if (pos + steplet > x) 217 if (pos + steplet > x)
218 steplet = x - pos; 218 steplet = x - pos;
219 219
220 for (i = 0; i < steplet; i++) 220 for (i = 0; i < steplet; i++)
221 if (bits[i+pos] != dominant) 221 if (bits[i+pos] != dominant)
222 any = 1; 222 any = 1;
223 223
224 put_bits(pb, 1, any); 224 put_bits(pb, 1, any);
225 225
226 if (!any) 226 if (!any)
227 { 227 {
228 pos += steplet; 228 pos += steplet;
229 step += step / ADAPT_LEVEL; 229 step += step / ADAPT_LEVEL;
230 } 230 }
231 else 231 else
232 { 232 {
233 int interloper = 0; 233 int interloper = 0;
234 234
235 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant)) 235 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
236 interloper++; 236 interloper++;
237 237
238 // note change 238 // note change
239 write_uint_max(pb, interloper, (step >> 8) - 1); 239 write_uint_max(pb, interloper, (step >> 8) - 1);
240 240
241 pos += interloper + 1; 241 pos += interloper + 1;
242 step -= step / ADAPT_LEVEL; 242 step -= step / ADAPT_LEVEL;
243 } 243 }
244 244
245 if (step < 256) 245 if (step < 256)
246 { 246 {
247 step = 65536 / step; 247 step = 65536 / step;
248 dominant = !dominant; 248 dominant = !dominant;
249 } 249 }
250 } 250 }
251 251
252 // store signs 252 // store signs
253 for (i = 0; i < entries; i++) 253 for (i = 0; i < entries; i++)
254 if (buf[i]) 254 if (buf[i])
255 put_bits(pb, 1, buf[i] < 0); 255 put_bits(pb, 1, buf[i] < 0);
256 256
257 // av_free(bits); 257 // av_free(bits);
258 // av_free(copy); 258 // av_free(copy);
259 259
260 return 0; 260 return 0;
266 int n_zeros = 0, step = 256, dominant = 0; 266 int n_zeros = 0, step = 256, dominant = 0;
267 int pos = 0, level = 0; 267 int pos = 0, level = 0;
268 int *bits = av_mallocz(4* entries); 268 int *bits = av_mallocz(4* entries);
269 269
270 if (!bits) 270 if (!bits)
271 return -1; 271 return -1;
272 272
273 if (base_2_part) 273 if (base_2_part)
274 { 274 {
275 low_bits = get_bits(gb, 4); 275 low_bits = get_bits(gb, 4);
276 276
277 if (low_bits) 277 if (low_bits)
278 for (i = 0; i < entries; i++) 278 for (i = 0; i < entries; i++)
279 buf[i] = get_bits(gb, low_bits); 279 buf[i] = get_bits(gb, low_bits);
280 } 280 }
281 281
282 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits); 282 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
283 283
284 while (n_zeros < entries) 284 while (n_zeros < entries)
285 { 285 {
286 int steplet = step >> 8; 286 int steplet = step >> 8;
287 287
288 if (!get_bits1(gb)) 288 if (!get_bits1(gb))
289 { 289 {
290 for (i = 0; i < steplet; i++) 290 for (i = 0; i < steplet; i++)
291 bits[x++] = dominant; 291 bits[x++] = dominant;
292 292
293 if (!dominant) 293 if (!dominant)
294 n_zeros += steplet; 294 n_zeros += steplet;
295 295
296 step += step / ADAPT_LEVEL; 296 step += step / ADAPT_LEVEL;
297 } 297 }
298 else 298 else
299 { 299 {
300 int actual_run = read_uint_max(gb, steplet-1); 300 int actual_run = read_uint_max(gb, steplet-1);
301 301
302 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run); 302 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
303 303
304 for (i = 0; i < actual_run; i++) 304 for (i = 0; i < actual_run; i++)
305 bits[x++] = dominant; 305 bits[x++] = dominant;
306 306
307 bits[x++] = !dominant; 307 bits[x++] = !dominant;
308 308
309 if (!dominant) 309 if (!dominant)
310 n_zeros += actual_run; 310 n_zeros += actual_run;
311 else 311 else
312 n_zeros++; 312 n_zeros++;
313 313
314 step -= step / ADAPT_LEVEL; 314 step -= step / ADAPT_LEVEL;
315 } 315 }
316 316
317 if (step < 256) 317 if (step < 256)
318 { 318 {
319 step = 65536 / step; 319 step = 65536 / step;
320 dominant = !dominant; 320 dominant = !dominant;
321 } 321 }
322 } 322 }
323 323
324 // reconstruct unsigned values 324 // reconstruct unsigned values
325 n_zeros = 0; 325 n_zeros = 0;
326 for (i = 0; n_zeros < entries; i++) 326 for (i = 0; n_zeros < entries; i++)
327 { 327 {
328 while(1) 328 while(1)
329 { 329 {
330 if (pos >= entries) 330 if (pos >= entries)
331 { 331 {
332 pos = 0; 332 pos = 0;
333 level += 1 << low_bits; 333 level += 1 << low_bits;
334 } 334 }
335 335
336 if (buf[pos] >= level) 336 if (buf[pos] >= level)
337 break; 337 break;
338 338
339 pos++; 339 pos++;
340 } 340 }
341 341
342 if (bits[i]) 342 if (bits[i])
343 buf[pos] += 1 << low_bits; 343 buf[pos] += 1 << low_bits;
344 else 344 else
345 n_zeros++; 345 n_zeros++;
346 346
347 pos++; 347 pos++;
348 } 348 }
349 // av_free(bits); 349 // av_free(bits);
350 350
351 // read signs 351 // read signs
352 for (i = 0; i < entries; i++) 352 for (i = 0; i < entries; i++)
353 if (buf[i] && get_bits1(gb)) 353 if (buf[i] && get_bits1(gb))
354 buf[i] = -buf[i]; 354 buf[i] = -buf[i];
355 355
356 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos); 356 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
357 357
358 return 0; 358 return 0;
359 } 359 }
363 { 363 {
364 int i; 364 int i;
365 365
366 for (i = order-2; i >= 0; i--) 366 for (i = order-2; i >= 0; i--)
367 { 367 {
368 int j, p, x = state[i]; 368 int j, p, x = state[i];
369 369
370 for (j = 0, p = i+1; p < order; j++,p++) 370 for (j = 0, p = i+1; p < order; j++,p++)
371 { 371 {
372 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT); 372 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
373 state[p] += shift_down(k[j]*x, LATTICE_SHIFT); 373 state[p] += shift_down(k[j]*x, LATTICE_SHIFT);
374 x = tmp; 374 x = tmp;
375 } 375 }
376 } 376 }
377 } 377 }
378 378
379 static int predictor_calc_error(int *k, int *state, int order, int error) 379 static int predictor_calc_error(int *k, int *state, int order, int error)
380 { 380 {
381 int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT); 381 int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT);
382 382
383 #if 1 383 #if 1
384 int *k_ptr = &(k[order-2]), 384 int *k_ptr = &(k[order-2]),
385 *state_ptr = &(state[order-2]); 385 *state_ptr = &(state[order-2]);
386 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--) 386 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
387 { 387 {
388 int k_value = *k_ptr, state_value = *state_ptr; 388 int k_value = *k_ptr, state_value = *state_ptr;
389 x -= shift_down(k_value * state_value, LATTICE_SHIFT); 389 x -= shift_down(k_value * state_value, LATTICE_SHIFT);
390 state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT); 390 state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
391 } 391 }
392 #else 392 #else
393 for (i = order-2; i >= 0; i--) 393 for (i = order-2; i >= 0; i--)
394 { 394 {
395 x -= shift_down(k[i] * state[i], LATTICE_SHIFT); 395 x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
396 state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT); 396 state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
397 } 397 }
398 #endif 398 #endif
399 399
400 // don't drift too far, to avoid overflows 400 // don't drift too far, to avoid overflows
401 if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16); 401 if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
409 // Heavily modified Levinson-Durbin algorithm which 409 // Heavily modified Levinson-Durbin algorithm which
410 // copes better with quantization, and calculates the 410 // copes better with quantization, and calculates the
411 // actual whitened result as it goes. 411 // actual whitened result as it goes.
412 412
413 static void modified_levinson_durbin(int *window, int window_entries, 413 static void modified_levinson_durbin(int *window, int window_entries,
414 int *out, int out_entries, int channels, int *tap_quant) 414 int *out, int out_entries, int channels, int *tap_quant)
415 { 415 {
416 int i; 416 int i;
417 int *state = av_mallocz(4* window_entries); 417 int *state = av_mallocz(4* window_entries);
418 418
419 memcpy(state, window, 4* window_entries); 419 memcpy(state, window, 4* window_entries);
420 420
421 for (i = 0; i < out_entries; i++) 421 for (i = 0; i < out_entries; i++)
422 { 422 {
423 int step = (i+1)*channels, k, j; 423 int step = (i+1)*channels, k, j;
424 double xx = 0.0, xy = 0.0; 424 double xx = 0.0, xy = 0.0;
425 #if 1 425 #if 1
426 int *x_ptr = &(window[step]), *state_ptr = &(state[0]); 426 int *x_ptr = &(window[step]), *state_ptr = &(state[0]);
427 j = window_entries - step; 427 j = window_entries - step;
428 for (;j>=0;j--,x_ptr++,state_ptr++) 428 for (;j>=0;j--,x_ptr++,state_ptr++)
429 { 429 {
430 double x_value = *x_ptr, state_value = *state_ptr; 430 double x_value = *x_ptr, state_value = *state_ptr;
431 xx += state_value*state_value; 431 xx += state_value*state_value;
432 xy += x_value*state_value; 432 xy += x_value*state_value;
433 } 433 }
434 #else 434 #else
435 for (j = 0; j <= (window_entries - step); j++); 435 for (j = 0; j <= (window_entries - step); j++);
436 { 436 {
437 double stepval = window[step+j], stateval = window[j]; 437 double stepval = window[step+j], stateval = window[j];
438 // xx += (double)window[j]*(double)window[j]; 438 // xx += (double)window[j]*(double)window[j];
439 // xy += (double)window[step+j]*(double)window[j]; 439 // xy += (double)window[step+j]*(double)window[j];
440 xx += stateval*stateval; 440 xx += stateval*stateval;
441 xy += stepval*stateval; 441 xy += stepval*stateval;
442 } 442 }
443 #endif 443 #endif
444 if (xx == 0.0) 444 if (xx == 0.0)
445 k = 0; 445 k = 0;
446 else 446 else
447 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5)); 447 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
448 448
449 if (k > (LATTICE_FACTOR/tap_quant[i])) 449 if (k > (LATTICE_FACTOR/tap_quant[i]))
450 k = LATTICE_FACTOR/tap_quant[i]; 450 k = LATTICE_FACTOR/tap_quant[i];
451 if (-k > (LATTICE_FACTOR/tap_quant[i])) 451 if (-k > (LATTICE_FACTOR/tap_quant[i]))
452 k = -(LATTICE_FACTOR/tap_quant[i]); 452 k = -(LATTICE_FACTOR/tap_quant[i]);
453 453
454 out[i] = k; 454 out[i] = k;
455 k *= tap_quant[i]; 455 k *= tap_quant[i];
456 456
457 #if 1 457 #if 1
458 x_ptr = &(window[step]); 458 x_ptr = &(window[step]);
459 state_ptr = &(state[0]); 459 state_ptr = &(state[0]);
460 j = window_entries - step; 460 j = window_entries - step;
461 for (;j>=0;j--,x_ptr++,state_ptr++) 461 for (;j>=0;j--,x_ptr++,state_ptr++)
462 { 462 {
463 int x_value = *x_ptr, state_value = *state_ptr; 463 int x_value = *x_ptr, state_value = *state_ptr;
464 *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT); 464 *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
465 *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT); 465 *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
466 } 466 }
467 #else 467 #else
468 for (j=0; j <= (window_entries - step); j++) 468 for (j=0; j <= (window_entries - step); j++)
469 { 469 {
470 int stepval = window[step+j], stateval=state[j]; 470 int stepval = window[step+j], stateval=state[j];
471 window[step+j] += shift_down(k * stateval, LATTICE_SHIFT); 471 window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
472 state[j] += shift_down(k * stepval, LATTICE_SHIFT); 472 state[j] += shift_down(k * stepval, LATTICE_SHIFT);
473 } 473 }
474 #endif 474 #endif
475 } 475 }
476 476
477 av_free(state); 477 av_free(state);
478 } 478 }
484 484
485 static inline int code_samplerate(int samplerate) 485 static inline int code_samplerate(int samplerate)
486 { 486 {
487 switch (samplerate) 487 switch (samplerate)
488 { 488 {
489 case 44100: return 0; 489 case 44100: return 0;
490 case 22050: return 1; 490 case 22050: return 1;
491 case 11025: return 2; 491 case 11025: return 2;
492 case 96000: return 3; 492 case 96000: return 3;
493 case 48000: return 4; 493 case 48000: return 4;
494 case 32000: return 5; 494 case 32000: return 5;
495 case 24000: return 6; 495 case 24000: return 6;
496 case 16000: return 7; 496 case 16000: return 7;
497 case 8000: return 8; 497 case 8000: return 8;
498 } 498 }
499 return -1; 499 return -1;
500 } 500 }
501 501
502 static int sonic_encode_init(AVCodecContext *avctx) 502 static int sonic_encode_init(AVCodecContext *avctx)
505 PutBitContext pb; 505 PutBitContext pb;
506 int i, version = 0; 506 int i, version = 0;
507 507
508 if (avctx->channels > MAX_CHANNELS) 508 if (avctx->channels > MAX_CHANNELS)
509 { 509 {
510 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n"); 510 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
511 return -1; /* only stereo or mono for now */ 511 return -1; /* only stereo or mono for now */
512 } 512 }
513 513
514 if (avctx->channels == 2) 514 if (avctx->channels == 2)
515 s->decorrelation = MID_SIDE; 515 s->decorrelation = MID_SIDE;
516 516
517 if (avctx->codec->id == CODEC_ID_SONIC_LS) 517 if (avctx->codec->id == CODEC_ID_SONIC_LS)
518 { 518 {
519 s->lossless = 1; 519 s->lossless = 1;
520 s->num_taps = 32; 520 s->num_taps = 32;
521 s->downsampling = 1; 521 s->downsampling = 1;
522 s->quantization = 0.0; 522 s->quantization = 0.0;
523 } 523 }
524 else 524 else
525 { 525 {
526 s->num_taps = 128; 526 s->num_taps = 128;
527 s->downsampling = 2; 527 s->downsampling = 2;
528 s->quantization = 1.0; 528 s->quantization = 1.0;
529 } 529 }
530 530
531 // max tap 2048 531 // max tap 2048
532 if ((s->num_taps < 32) || (s->num_taps > 1024) || 532 if ((s->num_taps < 32) || (s->num_taps > 1024) ||
533 ((s->num_taps>>5)<<5 != s->num_taps)) 533 ((s->num_taps>>5)<<5 != s->num_taps))
534 { 534 {
535 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n"); 535 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
536 return -1; 536 return -1;
537 } 537 }
538 538
539 // generate taps 539 // generate taps
540 s->tap_quant = av_mallocz(4* s->num_taps); 540 s->tap_quant = av_mallocz(4* s->num_taps);
541 for (i = 0; i < s->num_taps; i++) 541 for (i = 0; i < s->num_taps; i++)
542 s->tap_quant[i] = (int)(sqrt(i+1)); 542 s->tap_quant[i] = (int)(sqrt(i+1));
543 543
544 s->channels = avctx->channels; 544 s->channels = avctx->channels;
545 s->samplerate = avctx->sample_rate; 545 s->samplerate = avctx->sample_rate;
546 546
547 s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling; 547 s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling;
548 s->frame_size = s->channels*s->block_align*s->downsampling; 548 s->frame_size = s->channels*s->block_align*s->downsampling;
549 549
550 s->tail = av_mallocz(4* s->num_taps*s->channels); 550 s->tail = av_mallocz(4* s->num_taps*s->channels);
551 if (!s->tail) 551 if (!s->tail)
552 return -1; 552 return -1;
553 s->tail_size = s->num_taps*s->channels; 553 s->tail_size = s->num_taps*s->channels;
554 554
555 s->predictor_k = av_mallocz(4 * s->num_taps); 555 s->predictor_k = av_mallocz(4 * s->num_taps);
556 if (!s->predictor_k) 556 if (!s->predictor_k)
557 return -1; 557 return -1;
558 558
559 for (i = 0; i < s->channels; i++) 559 for (i = 0; i < s->channels; i++)
560 { 560 {
561 s->coded_samples[i] = av_mallocz(4* s->block_align); 561 s->coded_samples[i] = av_mallocz(4* s->block_align);
562 if (!s->coded_samples[i]) 562 if (!s->coded_samples[i])
563 return -1; 563 return -1;
564 } 564 }
565 565
566 s->int_samples = av_mallocz(4* s->frame_size); 566 s->int_samples = av_mallocz(4* s->frame_size);
567 567
568 s->window_size = ((2*s->tail_size)+s->frame_size); 568 s->window_size = ((2*s->tail_size)+s->frame_size);
569 s->window = av_mallocz(4* s->window_size); 569 s->window = av_mallocz(4* s->window_size);
570 if (!s->window) 570 if (!s->window)
571 return -1; 571 return -1;
572 572
573 avctx->extradata = av_mallocz(16); 573 avctx->extradata = av_mallocz(16);
574 if (!avctx->extradata) 574 if (!avctx->extradata)
575 return -1; 575 return -1;
576 init_put_bits(&pb, avctx->extradata, 16*8); 576 init_put_bits(&pb, avctx->extradata, 16*8);
577 577
578 put_bits(&pb, 2, version); // version 578 put_bits(&pb, 2, version); // version
579 if (version == 1) 579 if (version == 1)
580 { 580 {
581 put_bits(&pb, 2, s->channels); 581 put_bits(&pb, 2, s->channels);
582 put_bits(&pb, 4, code_samplerate(s->samplerate)); 582 put_bits(&pb, 4, code_samplerate(s->samplerate));
583 } 583 }
584 put_bits(&pb, 1, s->lossless); 584 put_bits(&pb, 1, s->lossless);
585 if (!s->lossless) 585 if (!s->lossless)
586 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision 586 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
587 put_bits(&pb, 2, s->decorrelation); 587 put_bits(&pb, 2, s->decorrelation);
588 put_bits(&pb, 2, s->downsampling); 588 put_bits(&pb, 2, s->downsampling);
589 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024 589 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
590 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table 590 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
591 591
592 flush_put_bits(&pb); 592 flush_put_bits(&pb);
593 avctx->extradata_size = put_bits_count(&pb)/8; 593 avctx->extradata_size = put_bits_count(&pb)/8;
594 594
595 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n", 595 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
596 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling); 596 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
597 597
598 avctx->coded_frame = avcodec_alloc_frame(); 598 avctx->coded_frame = avcodec_alloc_frame();
599 if (!avctx->coded_frame) 599 if (!avctx->coded_frame)
600 return -ENOMEM; 600 return -ENOMEM;
601 avctx->coded_frame->key_frame = 1; 601 avctx->coded_frame->key_frame = 1;
602 avctx->frame_size = s->block_align*s->downsampling; 602 avctx->frame_size = s->block_align*s->downsampling;
603 603
604 return 0; 604 return 0;
605 } 605 }
610 int i; 610 int i;
611 611
612 av_freep(&avctx->coded_frame); 612 av_freep(&avctx->coded_frame);
613 613
614 for (i = 0; i < s->channels; i++) 614 for (i = 0; i < s->channels; i++)
615 av_free(s->coded_samples[i]); 615 av_free(s->coded_samples[i]);
616 616
617 av_free(s->predictor_k); 617 av_free(s->predictor_k);
618 av_free(s->tail); 618 av_free(s->tail);
619 av_free(s->tap_quant); 619 av_free(s->tap_quant);
620 av_free(s->window); 620 av_free(s->window);
622 622
623 return 0; 623 return 0;
624 } 624 }
625 625
626 static int sonic_encode_frame(AVCodecContext *avctx, 626 static int sonic_encode_frame(AVCodecContext *avctx,
627 uint8_t *buf, int buf_size, void *data) 627 uint8_t *buf, int buf_size, void *data)
628 { 628 {
629 SonicContext *s = avctx->priv_data; 629 SonicContext *s = avctx->priv_data;
630 PutBitContext pb; 630 PutBitContext pb;
631 int i, j, ch, quant = 0, x = 0; 631 int i, j, ch, quant = 0, x = 0;
632 short *samples = data; 632 short *samples = data;
633 633
634 init_put_bits(&pb, buf, buf_size*8); 634 init_put_bits(&pb, buf, buf_size*8);
635 635
636 // short -> internal 636 // short -> internal
637 for (i = 0; i < s->frame_size; i++) 637 for (i = 0; i < s->frame_size; i++)
638 s->int_samples[i] = samples[i]; 638 s->int_samples[i] = samples[i];
639 639
640 if (!s->lossless) 640 if (!s->lossless)
641 for (i = 0; i < s->frame_size; i++) 641 for (i = 0; i < s->frame_size; i++)
642 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT; 642 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
643 643
644 switch(s->decorrelation) 644 switch(s->decorrelation)
645 { 645 {
646 case MID_SIDE: 646 case MID_SIDE:
647 for (i = 0; i < s->frame_size; i += s->channels) 647 for (i = 0; i < s->frame_size; i += s->channels)
648 { 648 {
649 s->int_samples[i] += s->int_samples[i+1]; 649 s->int_samples[i] += s->int_samples[i+1];
650 s->int_samples[i+1] -= shift(s->int_samples[i], 1); 650 s->int_samples[i+1] -= shift(s->int_samples[i], 1);
651 } 651 }
652 break; 652 break;
653 case LEFT_SIDE: 653 case LEFT_SIDE:
654 for (i = 0; i < s->frame_size; i += s->channels) 654 for (i = 0; i < s->frame_size; i += s->channels)
655 s->int_samples[i+1] -= s->int_samples[i]; 655 s->int_samples[i+1] -= s->int_samples[i];
656 break; 656 break;
657 case RIGHT_SIDE: 657 case RIGHT_SIDE:
658 for (i = 0; i < s->frame_size; i += s->channels) 658 for (i = 0; i < s->frame_size; i += s->channels)
659 s->int_samples[i] -= s->int_samples[i+1]; 659 s->int_samples[i] -= s->int_samples[i+1];
660 break; 660 break;
661 } 661 }
662 662
663 memset(s->window, 0, 4* s->window_size); 663 memset(s->window, 0, 4* s->window_size);
664 664
665 for (i = 0; i < s->tail_size; i++) 665 for (i = 0; i < s->tail_size; i++)
666 s->window[x++] = s->tail[i]; 666 s->window[x++] = s->tail[i];
667 667
668 for (i = 0; i < s->frame_size; i++) 668 for (i = 0; i < s->frame_size; i++)
669 s->window[x++] = s->int_samples[i]; 669 s->window[x++] = s->int_samples[i];
670 670
671 for (i = 0; i < s->tail_size; i++) 671 for (i = 0; i < s->tail_size; i++)
672 s->window[x++] = 0; 672 s->window[x++] = 0;
673 673
674 for (i = 0; i < s->tail_size; i++) 674 for (i = 0; i < s->tail_size; i++)
675 s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i]; 675 s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
676 676
677 // generate taps 677 // generate taps
678 modified_levinson_durbin(s->window, s->window_size, 678 modified_levinson_durbin(s->window, s->window_size,
679 s->predictor_k, s->num_taps, s->channels, s->tap_quant); 679 s->predictor_k, s->num_taps, s->channels, s->tap_quant);
680 if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0) 680 if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0)
681 return -1; 681 return -1;
682 682
683 for (ch = 0; ch < s->channels; ch++) 683 for (ch = 0; ch < s->channels; ch++)
684 { 684 {
685 x = s->tail_size+ch; 685 x = s->tail_size+ch;
686 for (i = 0; i < s->block_align; i++) 686 for (i = 0; i < s->block_align; i++)
687 { 687 {
688 int sum = 0; 688 int sum = 0;
689 for (j = 0; j < s->downsampling; j++, x += s->channels) 689 for (j = 0; j < s->downsampling; j++, x += s->channels)
690 sum += s->window[x]; 690 sum += s->window[x];
691 s->coded_samples[ch][i] = sum; 691 s->coded_samples[ch][i] = sum;
692 } 692 }
693 } 693 }
694 694
695 // simple rate control code 695 // simple rate control code
696 if (!s->lossless) 696 if (!s->lossless)
697 { 697 {
698 double energy1 = 0.0, energy2 = 0.0; 698 double energy1 = 0.0, energy2 = 0.0;
699 for (ch = 0; ch < s->channels; ch++) 699 for (ch = 0; ch < s->channels; ch++)
700 { 700 {
701 for (i = 0; i < s->block_align; i++) 701 for (i = 0; i < s->block_align; i++)
702 { 702 {
703 double sample = s->coded_samples[ch][i]; 703 double sample = s->coded_samples[ch][i];
704 energy2 += sample*sample; 704 energy2 += sample*sample;
705 energy1 += fabs(sample); 705 energy1 += fabs(sample);
706 } 706 }
707 } 707 }
708 708
709 energy2 = sqrt(energy2/(s->channels*s->block_align)); 709 energy2 = sqrt(energy2/(s->channels*s->block_align));
710 energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align); 710 energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align);
711 711
712 // increase bitrate when samples are like a gaussian distribution 712 // increase bitrate when samples are like a gaussian distribution
713 // reduce bitrate when samples are like a two-tailed exponential distribution 713 // reduce bitrate when samples are like a two-tailed exponential distribution
714 714
715 if (energy2 > energy1) 715 if (energy2 > energy1)
716 energy2 += (energy2-energy1)*RATE_VARIATION; 716 energy2 += (energy2-energy1)*RATE_VARIATION;
717 717
718 quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR); 718 quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
719 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2); 719 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
720 720
721 if (quant < 1) 721 if (quant < 1)
722 quant = 1; 722 quant = 1;
723 if (quant > 65535) 723 if (quant > 65535)
724 quant = 65535; 724 quant = 65535;
725 725
726 set_ue_golomb(&pb, quant); 726 set_ue_golomb(&pb, quant);
727 727
728 quant *= SAMPLE_FACTOR; 728 quant *= SAMPLE_FACTOR;
729 } 729 }
730 730
731 // write out coded samples 731 // write out coded samples
732 for (ch = 0; ch < s->channels; ch++) 732 for (ch = 0; ch < s->channels; ch++)
733 { 733 {
734 if (!s->lossless) 734 if (!s->lossless)
735 for (i = 0; i < s->block_align; i++) 735 for (i = 0; i < s->block_align; i++)
736 s->coded_samples[ch][i] = divide(s->coded_samples[ch][i], quant); 736 s->coded_samples[ch][i] = divide(s->coded_samples[ch][i], quant);
737 737
738 if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0) 738 if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0)
739 return -1; 739 return -1;
740 } 740 }
741 741
742 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8); 742 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
743 743
744 flush_put_bits(&pb); 744 flush_put_bits(&pb);
755 s->channels = avctx->channels; 755 s->channels = avctx->channels;
756 s->samplerate = avctx->sample_rate; 756 s->samplerate = avctx->sample_rate;
757 757
758 if (!avctx->extradata) 758 if (!avctx->extradata)
759 { 759 {
760 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n"); 760 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
761 return -1; 761 return -1;
762 } 762 }
763 763
764 init_get_bits(&gb, avctx->extradata, avctx->extradata_size); 764 init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
765 765
766 version = get_bits(&gb, 2); 766 version = get_bits(&gb, 2);
767 if (version > 1) 767 if (version > 1)
768 { 768 {
769 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n"); 769 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
770 return -1; 770 return -1;
771 } 771 }
772 772
773 if (version == 1) 773 if (version == 1)
774 { 774 {
775 s->channels = get_bits(&gb, 2); 775 s->channels = get_bits(&gb, 2);
776 s->samplerate = samplerate_table[get_bits(&gb, 4)]; 776 s->samplerate = samplerate_table[get_bits(&gb, 4)];
777 av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n", 777 av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
778 s->channels, s->samplerate); 778 s->channels, s->samplerate);
779 } 779 }
780 780
781 if (s->channels > MAX_CHANNELS) 781 if (s->channels > MAX_CHANNELS)
782 { 782 {
783 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n"); 783 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
784 return -1; 784 return -1;
785 } 785 }
786 786
787 s->lossless = get_bits1(&gb); 787 s->lossless = get_bits1(&gb);
788 if (!s->lossless) 788 if (!s->lossless)
789 skip_bits(&gb, 3); // XXX FIXME 789 skip_bits(&gb, 3); // XXX FIXME
790 s->decorrelation = get_bits(&gb, 2); 790 s->decorrelation = get_bits(&gb, 2);
791 791
792 s->downsampling = get_bits(&gb, 2); 792 s->downsampling = get_bits(&gb, 2);
793 s->num_taps = (get_bits(&gb, 5)+1)<<5; 793 s->num_taps = (get_bits(&gb, 5)+1)<<5;
794 if (get_bits1(&gb)) // XXX FIXME 794 if (get_bits1(&gb)) // XXX FIXME
795 av_log(avctx, AV_LOG_INFO, "Custom quant table\n"); 795 av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
796 796
797 s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling; 797 s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling;
798 s->frame_size = s->channels*s->block_align*s->downsampling; 798 s->frame_size = s->channels*s->block_align*s->downsampling;
799 // avctx->frame_size = s->block_align; 799 // avctx->frame_size = s->block_align;
800 800
801 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n", 801 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
802 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling); 802 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
803 803
804 // generate taps 804 // generate taps
805 s->tap_quant = av_mallocz(4* s->num_taps); 805 s->tap_quant = av_mallocz(4* s->num_taps);
806 for (i = 0; i < s->num_taps; i++) 806 for (i = 0; i < s->num_taps; i++)
807 s->tap_quant[i] = (int)(sqrt(i+1)); 807 s->tap_quant[i] = (int)(sqrt(i+1));
808 808
809 s->predictor_k = av_mallocz(4* s->num_taps); 809 s->predictor_k = av_mallocz(4* s->num_taps);
810 810
811 for (i = 0; i < s->channels; i++) 811 for (i = 0; i < s->channels; i++)
812 { 812 {
813 s->predictor_state[i] = av_mallocz(4* s->num_taps); 813 s->predictor_state[i] = av_mallocz(4* s->num_taps);
814 if (!s->predictor_state[i]) 814 if (!s->predictor_state[i])
815 return -1; 815 return -1;
816 } 816 }
817 817
818 for (i = 0; i < s->channels; i++) 818 for (i = 0; i < s->channels; i++)
819 { 819 {
820 s->coded_samples[i] = av_mallocz(4* s->block_align); 820 s->coded_samples[i] = av_mallocz(4* s->block_align);
821 if (!s->coded_samples[i]) 821 if (!s->coded_samples[i])
822 return -1; 822 return -1;
823 } 823 }
824 s->int_samples = av_mallocz(4* s->frame_size); 824 s->int_samples = av_mallocz(4* s->frame_size);
825 825
826 return 0; 826 return 0;
827 } 827 }
835 av_free(s->tap_quant); 835 av_free(s->tap_quant);
836 av_free(s->predictor_k); 836 av_free(s->predictor_k);
837 837
838 for (i = 0; i < s->channels; i++) 838 for (i = 0; i < s->channels; i++)
839 { 839 {
840 av_free(s->predictor_state[i]); 840 av_free(s->predictor_state[i]);
841 av_free(s->coded_samples[i]); 841 av_free(s->coded_samples[i]);
842 } 842 }
843 843
844 return 0; 844 return 0;
845 } 845 }
846 846
847 static int sonic_decode_frame(AVCodecContext *avctx, 847 static int sonic_decode_frame(AVCodecContext *avctx,
848 void *data, int *data_size, 848 void *data, int *data_size,
849 uint8_t *buf, int buf_size) 849 uint8_t *buf, int buf_size)
850 { 850 {
851 SonicContext *s = avctx->priv_data; 851 SonicContext *s = avctx->priv_data;
852 GetBitContext gb; 852 GetBitContext gb;
853 int i, quant, ch, j; 853 int i, quant, ch, j;
854 short *samples = data; 854 short *samples = data;
861 861
862 intlist_read(&gb, s->predictor_k, s->num_taps, 0); 862 intlist_read(&gb, s->predictor_k, s->num_taps, 0);
863 863
864 // dequantize 864 // dequantize
865 for (i = 0; i < s->num_taps; i++) 865 for (i = 0; i < s->num_taps; i++)
866 s->predictor_k[i] *= s->tap_quant[i]; 866 s->predictor_k[i] *= s->tap_quant[i];
867 867
868 if (s->lossless) 868 if (s->lossless)
869 quant = 1; 869 quant = 1;
870 else 870 else
871 quant = get_ue_golomb(&gb) * SAMPLE_FACTOR; 871 quant = get_ue_golomb(&gb) * SAMPLE_FACTOR;
872 872
873 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant); 873 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
874 874
875 for (ch = 0; ch < s->channels; ch++) 875 for (ch = 0; ch < s->channels; ch++)
876 { 876 {
877 int x = ch; 877 int x = ch;
878 878
879 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps); 879 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
880 880
881 intlist_read(&gb, s->coded_samples[ch], s->block_align, 1); 881 intlist_read(&gb, s->coded_samples[ch], s->block_align, 1);
882 882
883 for (i = 0; i < s->block_align; i++) 883 for (i = 0; i < s->block_align; i++)
884 { 884 {
885 for (j = 0; j < s->downsampling - 1; j++) 885 for (j = 0; j < s->downsampling - 1; j++)
886 { 886 {
887 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0); 887 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
888 x += s->channels; 888 x += s->channels;
889 } 889 }
890 890
891 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant); 891 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
892 x += s->channels; 892 x += s->channels;
893 } 893 }
894 894
895 for (i = 0; i < s->num_taps; i++) 895 for (i = 0; i < s->num_taps; i++)
896 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels]; 896 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
897 } 897 }
898 898
899 switch(s->decorrelation) 899 switch(s->decorrelation)
900 { 900 {
901 case MID_SIDE: 901 case MID_SIDE:
902 for (i = 0; i < s->frame_size; i += s->channels) 902 for (i = 0; i < s->frame_size; i += s->channels)
903 { 903 {
904 s->int_samples[i+1] += shift(s->int_samples[i], 1); 904 s->int_samples[i+1] += shift(s->int_samples[i], 1);
905 s->int_samples[i] -= s->int_samples[i+1]; 905 s->int_samples[i] -= s->int_samples[i+1];
906 } 906 }
907 break; 907 break;
908 case LEFT_SIDE: 908 case LEFT_SIDE:
909 for (i = 0; i < s->frame_size; i += s->channels) 909 for (i = 0; i < s->frame_size; i += s->channels)
910 s->int_samples[i+1] += s->int_samples[i]; 910 s->int_samples[i+1] += s->int_samples[i];
911 break; 911 break;
912 case RIGHT_SIDE: 912 case RIGHT_SIDE:
913 for (i = 0; i < s->frame_size; i += s->channels) 913 for (i = 0; i < s->frame_size; i += s->channels)
914 s->int_samples[i] += s->int_samples[i+1]; 914 s->int_samples[i] += s->int_samples[i+1];
915 break; 915 break;
916 } 916 }
917 917
918 if (!s->lossless) 918 if (!s->lossless)
919 for (i = 0; i < s->frame_size; i++) 919 for (i = 0; i < s->frame_size; i++)
920 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT); 920 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
921 921
922 // internal -> short 922 // internal -> short
923 for (i = 0; i < s->frame_size; i++) 923 for (i = 0; i < s->frame_size; i++)
924 { 924 {
925 if (s->int_samples[i] > 32767) 925 if (s->int_samples[i] > 32767)
926 samples[i] = 32767; 926 samples[i] = 32767;
927 else if (s->int_samples[i] < -32768) 927 else if (s->int_samples[i] < -32768)
928 samples[i] = -32768; 928 samples[i] = -32768;
929 else 929 else
930 samples[i] = s->int_samples[i]; 930 samples[i] = s->int_samples[i];
931 } 931 }
932 932
933 align_get_bits(&gb); 933 align_get_bits(&gb);
934 934
935 *data_size = s->frame_size * 2; 935 *data_size = s->frame_size * 2;