00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "avcodec.h"
00020 #include "bitstream.h"
00021 #include "golomb.h"
00022
00038 #define MAX_CHANNELS 2
00039
00040 #define MID_SIDE 0
00041 #define LEFT_SIDE 1
00042 #define RIGHT_SIDE 2
00043
00044 typedef struct SonicContext {
00045 int lossless, decorrelation;
00046
00047 int num_taps, downsampling;
00048 double quantization;
00049
00050 int channels, samplerate, block_align, frame_size;
00051
00052 int *tap_quant;
00053 int *int_samples;
00054 int *coded_samples[MAX_CHANNELS];
00055
00056
00057 int *tail;
00058 int tail_size;
00059 int *window;
00060 int window_size;
00061
00062
00063 int *predictor_k;
00064 int *predictor_state[MAX_CHANNELS];
00065 } SonicContext;
00066
00067 #define LATTICE_SHIFT 10
00068 #define SAMPLE_SHIFT 4
00069 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
00070 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
00071
00072 #define BASE_QUANT 0.6
00073 #define RATE_VARIATION 3.0
00074
00075 static inline int divide(int a, int b)
00076 {
00077 if (a < 0)
00078 return -( (-a + b/2)/b );
00079 else
00080 return (a + b/2)/b;
00081 }
00082
00083 static inline int shift(int a,int b)
00084 {
00085 return (a+(1<<(b-1))) >> b;
00086 }
00087
00088 static inline int shift_down(int a,int b)
00089 {
00090 return (a>>b)+((a<0)?1:0);
00091 }
00092
00093 #if 1
00094 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
00095 {
00096 int i;
00097
00098 for (i = 0; i < entries; i++)
00099 set_se_golomb(pb, buf[i]);
00100
00101 return 1;
00102 }
00103
00104 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
00105 {
00106 int i;
00107
00108 for (i = 0; i < entries; i++)
00109 buf[i] = get_se_golomb(gb);
00110
00111 return 1;
00112 }
00113
00114 #else
00115
00116 #define ADAPT_LEVEL 8
00117
00118 static int bits_to_store(uint64_t x)
00119 {
00120 int res = 0;
00121
00122 while(x)
00123 {
00124 res++;
00125 x >>= 1;
00126 }
00127 return res;
00128 }
00129
00130 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
00131 {
00132 int i, bits;
00133
00134 if (!max)
00135 return;
00136
00137 bits = bits_to_store(max);
00138
00139 for (i = 0; i < bits-1; i++)
00140 put_bits(pb, 1, value & (1 << i));
00141
00142 if ( (value | (1 << (bits-1))) <= max)
00143 put_bits(pb, 1, value & (1 << (bits-1)));
00144 }
00145
00146 static unsigned int read_uint_max(GetBitContext *gb, int max)
00147 {
00148 int i, bits, value = 0;
00149
00150 if (!max)
00151 return 0;
00152
00153 bits = bits_to_store(max);
00154
00155 for (i = 0; i < bits-1; i++)
00156 if (get_bits1(gb))
00157 value += 1 << i;
00158
00159 if ( (value | (1<<(bits-1))) <= max)
00160 if (get_bits1(gb))
00161 value += 1 << (bits-1);
00162
00163 return value;
00164 }
00165
00166 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
00167 {
00168 int i, j, x = 0, low_bits = 0, max = 0;
00169 int step = 256, pos = 0, dominant = 0, any = 0;
00170 int *copy, *bits;
00171
00172 copy = av_mallocz(4* entries);
00173 if (!copy)
00174 return -1;
00175
00176 if (base_2_part)
00177 {
00178 int energy = 0;
00179
00180 for (i = 0; i < entries; i++)
00181 energy += abs(buf[i]);
00182
00183 low_bits = bits_to_store(energy / (entries * 2));
00184 if (low_bits > 15)
00185 low_bits = 15;
00186
00187 put_bits(pb, 4, low_bits);
00188 }
00189
00190 for (i = 0; i < entries; i++)
00191 {
00192 put_bits(pb, low_bits, abs(buf[i]));
00193 copy[i] = abs(buf[i]) >> low_bits;
00194 if (copy[i] > max)
00195 max = abs(copy[i]);
00196 }
00197
00198 bits = av_mallocz(4* entries*max);
00199 if (!bits)
00200 {
00201
00202 return -1;
00203 }
00204
00205 for (i = 0; i <= max; i++)
00206 {
00207 for (j = 0; j < entries; j++)
00208 if (copy[j] >= i)
00209 bits[x++] = copy[j] > i;
00210 }
00211
00212
00213 while (pos < x)
00214 {
00215 int steplet = step >> 8;
00216
00217 if (pos + steplet > x)
00218 steplet = x - pos;
00219
00220 for (i = 0; i < steplet; i++)
00221 if (bits[i+pos] != dominant)
00222 any = 1;
00223
00224 put_bits(pb, 1, any);
00225
00226 if (!any)
00227 {
00228 pos += steplet;
00229 step += step / ADAPT_LEVEL;
00230 }
00231 else
00232 {
00233 int interloper = 0;
00234
00235 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
00236 interloper++;
00237
00238
00239 write_uint_max(pb, interloper, (step >> 8) - 1);
00240
00241 pos += interloper + 1;
00242 step -= step / ADAPT_LEVEL;
00243 }
00244
00245 if (step < 256)
00246 {
00247 step = 65536 / step;
00248 dominant = !dominant;
00249 }
00250 }
00251
00252
00253 for (i = 0; i < entries; i++)
00254 if (buf[i])
00255 put_bits(pb, 1, buf[i] < 0);
00256
00257
00258
00259
00260 return 0;
00261 }
00262
00263 static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
00264 {
00265 int i, low_bits = 0, x = 0;
00266 int n_zeros = 0, step = 256, dominant = 0;
00267 int pos = 0, level = 0;
00268 int *bits = av_mallocz(4* entries);
00269
00270 if (!bits)
00271 return -1;
00272
00273 if (base_2_part)
00274 {
00275 low_bits = get_bits(gb, 4);
00276
00277 if (low_bits)
00278 for (i = 0; i < entries; i++)
00279 buf[i] = get_bits(gb, low_bits);
00280 }
00281
00282
00283
00284 while (n_zeros < entries)
00285 {
00286 int steplet = step >> 8;
00287
00288 if (!get_bits1(gb))
00289 {
00290 for (i = 0; i < steplet; i++)
00291 bits[x++] = dominant;
00292
00293 if (!dominant)
00294 n_zeros += steplet;
00295
00296 step += step / ADAPT_LEVEL;
00297 }
00298 else
00299 {
00300 int actual_run = read_uint_max(gb, steplet-1);
00301
00302
00303
00304 for (i = 0; i < actual_run; i++)
00305 bits[x++] = dominant;
00306
00307 bits[x++] = !dominant;
00308
00309 if (!dominant)
00310 n_zeros += actual_run;
00311 else
00312 n_zeros++;
00313
00314 step -= step / ADAPT_LEVEL;
00315 }
00316
00317 if (step < 256)
00318 {
00319 step = 65536 / step;
00320 dominant = !dominant;
00321 }
00322 }
00323
00324
00325 n_zeros = 0;
00326 for (i = 0; n_zeros < entries; i++)
00327 {
00328 while(1)
00329 {
00330 if (pos >= entries)
00331 {
00332 pos = 0;
00333 level += 1 << low_bits;
00334 }
00335
00336 if (buf[pos] >= level)
00337 break;
00338
00339 pos++;
00340 }
00341
00342 if (bits[i])
00343 buf[pos] += 1 << low_bits;
00344 else
00345 n_zeros++;
00346
00347 pos++;
00348 }
00349
00350
00351
00352 for (i = 0; i < entries; i++)
00353 if (buf[i] && get_bits1(gb))
00354 buf[i] = -buf[i];
00355
00356
00357
00358 return 0;
00359 }
00360 #endif
00361
00362 static void predictor_init_state(int *k, int *state, int order)
00363 {
00364 int i;
00365
00366 for (i = order-2; i >= 0; i--)
00367 {
00368 int j, p, x = state[i];
00369
00370 for (j = 0, p = i+1; p < order; j++,p++)
00371 {
00372 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
00373 state[p] += shift_down(k[j]*x, LATTICE_SHIFT);
00374 x = tmp;
00375 }
00376 }
00377 }
00378
00379 static int predictor_calc_error(int *k, int *state, int order, int error)
00380 {
00381 int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT);
00382
00383 #if 1
00384 int *k_ptr = &(k[order-2]),
00385 *state_ptr = &(state[order-2]);
00386 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
00387 {
00388 int k_value = *k_ptr, state_value = *state_ptr;
00389 x -= shift_down(k_value * state_value, LATTICE_SHIFT);
00390 state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
00391 }
00392 #else
00393 for (i = order-2; i >= 0; i--)
00394 {
00395 x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
00396 state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
00397 }
00398 #endif
00399
00400
00401 if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
00402 if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16);
00403
00404 state[0] = x;
00405
00406 return x;
00407 }
00408
00409
00410
00411
00412
00413 static void modified_levinson_durbin(int *window, int window_entries,
00414 int *out, int out_entries, int channels, int *tap_quant)
00415 {
00416 int i;
00417 int *state = av_mallocz(4* window_entries);
00418
00419 memcpy(state, window, 4* window_entries);
00420
00421 for (i = 0; i < out_entries; i++)
00422 {
00423 int step = (i+1)*channels, k, j;
00424 double xx = 0.0, xy = 0.0;
00425 #if 1
00426 int *x_ptr = &(window[step]), *state_ptr = &(state[0]);
00427 j = window_entries - step;
00428 for (;j>=0;j--,x_ptr++,state_ptr++)
00429 {
00430 double x_value = *x_ptr, state_value = *state_ptr;
00431 xx += state_value*state_value;
00432 xy += x_value*state_value;
00433 }
00434 #else
00435 for (j = 0; j <= (window_entries - step); j++);
00436 {
00437 double stepval = window[step+j], stateval = window[j];
00438
00439
00440 xx += stateval*stateval;
00441 xy += stepval*stateval;
00442 }
00443 #endif
00444 if (xx == 0.0)
00445 k = 0;
00446 else
00447 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
00448
00449 if (k > (LATTICE_FACTOR/tap_quant[i]))
00450 k = LATTICE_FACTOR/tap_quant[i];
00451 if (-k > (LATTICE_FACTOR/tap_quant[i]))
00452 k = -(LATTICE_FACTOR/tap_quant[i]);
00453
00454 out[i] = k;
00455 k *= tap_quant[i];
00456
00457 #if 1
00458 x_ptr = &(window[step]);
00459 state_ptr = &(state[0]);
00460 j = window_entries - step;
00461 for (;j>=0;j--,x_ptr++,state_ptr++)
00462 {
00463 int x_value = *x_ptr, state_value = *state_ptr;
00464 *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
00465 *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
00466 }
00467 #else
00468 for (j=0; j <= (window_entries - step); j++)
00469 {
00470 int stepval = window[step+j], stateval=state[j];
00471 window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
00472 state[j] += shift_down(k * stepval, LATTICE_SHIFT);
00473 }
00474 #endif
00475 }
00476
00477 av_free(state);
00478 }
00479
00480 static int samplerate_table[] =
00481 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
00482
00483 #ifdef CONFIG_ENCODERS
00484
00485 static inline int code_samplerate(int samplerate)
00486 {
00487 switch (samplerate)
00488 {
00489 case 44100: return 0;
00490 case 22050: return 1;
00491 case 11025: return 2;
00492 case 96000: return 3;
00493 case 48000: return 4;
00494 case 32000: return 5;
00495 case 24000: return 6;
00496 case 16000: return 7;
00497 case 8000: return 8;
00498 }
00499 return -1;
00500 }
00501
00502 static int sonic_encode_init(AVCodecContext *avctx)
00503 {
00504 SonicContext *s = avctx->priv_data;
00505 PutBitContext pb;
00506 int i, version = 0;
00507
00508 if (avctx->channels > MAX_CHANNELS)
00509 {
00510 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
00511 return -1;
00512 }
00513
00514 if (avctx->channels == 2)
00515 s->decorrelation = MID_SIDE;
00516
00517 if (avctx->codec->id == CODEC_ID_SONIC_LS)
00518 {
00519 s->lossless = 1;
00520 s->num_taps = 32;
00521 s->downsampling = 1;
00522 s->quantization = 0.0;
00523 }
00524 else
00525 {
00526 s->num_taps = 128;
00527 s->downsampling = 2;
00528 s->quantization = 1.0;
00529 }
00530
00531
00532 if ((s->num_taps < 32) || (s->num_taps > 1024) ||
00533 ((s->num_taps>>5)<<5 != s->num_taps))
00534 {
00535 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
00536 return -1;
00537 }
00538
00539
00540 s->tap_quant = av_mallocz(4* s->num_taps);
00541 for (i = 0; i < s->num_taps; i++)
00542 s->tap_quant[i] = (int)(sqrt(i+1));
00543
00544 s->channels = avctx->channels;
00545 s->samplerate = avctx->sample_rate;
00546
00547 s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling;
00548 s->frame_size = s->channels*s->block_align*s->downsampling;
00549
00550 s->tail = av_mallocz(4* s->num_taps*s->channels);
00551 if (!s->tail)
00552 return -1;
00553 s->tail_size = s->num_taps*s->channels;
00554
00555 s->predictor_k = av_mallocz(4 * s->num_taps);
00556 if (!s->predictor_k)
00557 return -1;
00558
00559 for (i = 0; i < s->channels; i++)
00560 {
00561 s->coded_samples[i] = av_mallocz(4* s->block_align);
00562 if (!s->coded_samples[i])
00563 return -1;
00564 }
00565
00566 s->int_samples = av_mallocz(4* s->frame_size);
00567
00568 s->window_size = ((2*s->tail_size)+s->frame_size);
00569 s->window = av_mallocz(4* s->window_size);
00570 if (!s->window)
00571 return -1;
00572
00573 avctx->extradata = av_mallocz(16);
00574 if (!avctx->extradata)
00575 return -1;
00576 init_put_bits(&pb, avctx->extradata, 16*8);
00577
00578 put_bits(&pb, 2, version);
00579 if (version == 1)
00580 {
00581 put_bits(&pb, 2, s->channels);
00582 put_bits(&pb, 4, code_samplerate(s->samplerate));
00583 }
00584 put_bits(&pb, 1, s->lossless);
00585 if (!s->lossless)
00586 put_bits(&pb, 3, SAMPLE_SHIFT);
00587 put_bits(&pb, 2, s->decorrelation);
00588 put_bits(&pb, 2, s->downsampling);
00589 put_bits(&pb, 5, (s->num_taps >> 5)-1);
00590 put_bits(&pb, 1, 0);
00591
00592 flush_put_bits(&pb);
00593 avctx->extradata_size = put_bits_count(&pb)/8;
00594
00595 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
00596 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
00597
00598 avctx->coded_frame = avcodec_alloc_frame();
00599 if (!avctx->coded_frame)
00600 return -ENOMEM;
00601 avctx->coded_frame->key_frame = 1;
00602 avctx->frame_size = s->block_align*s->downsampling;
00603
00604 return 0;
00605 }
00606
00607 static int sonic_encode_close(AVCodecContext *avctx)
00608 {
00609 SonicContext *s = avctx->priv_data;
00610 int i;
00611
00612 av_freep(&avctx->coded_frame);
00613
00614 for (i = 0; i < s->channels; i++)
00615 av_free(s->coded_samples[i]);
00616
00617 av_free(s->predictor_k);
00618 av_free(s->tail);
00619 av_free(s->tap_quant);
00620 av_free(s->window);
00621 av_free(s->int_samples);
00622
00623 return 0;
00624 }
00625
00626 static int sonic_encode_frame(AVCodecContext *avctx,
00627 uint8_t *buf, int buf_size, void *data)
00628 {
00629 SonicContext *s = avctx->priv_data;
00630 PutBitContext pb;
00631 int i, j, ch, quant = 0, x = 0;
00632 short *samples = data;
00633
00634 init_put_bits(&pb, buf, buf_size*8);
00635
00636
00637 for (i = 0; i < s->frame_size; i++)
00638 s->int_samples[i] = samples[i];
00639
00640 if (!s->lossless)
00641 for (i = 0; i < s->frame_size; i++)
00642 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
00643
00644 switch(s->decorrelation)
00645 {
00646 case MID_SIDE:
00647 for (i = 0; i < s->frame_size; i += s->channels)
00648 {
00649 s->int_samples[i] += s->int_samples[i+1];
00650 s->int_samples[i+1] -= shift(s->int_samples[i], 1);
00651 }
00652 break;
00653 case LEFT_SIDE:
00654 for (i = 0; i < s->frame_size; i += s->channels)
00655 s->int_samples[i+1] -= s->int_samples[i];
00656 break;
00657 case RIGHT_SIDE:
00658 for (i = 0; i < s->frame_size; i += s->channels)
00659 s->int_samples[i] -= s->int_samples[i+1];
00660 break;
00661 }
00662
00663 memset(s->window, 0, 4* s->window_size);
00664
00665 for (i = 0; i < s->tail_size; i++)
00666 s->window[x++] = s->tail[i];
00667
00668 for (i = 0; i < s->frame_size; i++)
00669 s->window[x++] = s->int_samples[i];
00670
00671 for (i = 0; i < s->tail_size; i++)
00672 s->window[x++] = 0;
00673
00674 for (i = 0; i < s->tail_size; i++)
00675 s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
00676
00677
00678 modified_levinson_durbin(s->window, s->window_size,
00679 s->predictor_k, s->num_taps, s->channels, s->tap_quant);
00680 if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0)
00681 return -1;
00682
00683 for (ch = 0; ch < s->channels; ch++)
00684 {
00685 x = s->tail_size+ch;
00686 for (i = 0; i < s->block_align; i++)
00687 {
00688 int sum = 0;
00689 for (j = 0; j < s->downsampling; j++, x += s->channels)
00690 sum += s->window[x];
00691 s->coded_samples[ch][i] = sum;
00692 }
00693 }
00694
00695
00696 if (!s->lossless)
00697 {
00698 double energy1 = 0.0, energy2 = 0.0;
00699 for (ch = 0; ch < s->channels; ch++)
00700 {
00701 for (i = 0; i < s->block_align; i++)
00702 {
00703 double sample = s->coded_samples[ch][i];
00704 energy2 += sample*sample;
00705 energy1 += fabs(sample);
00706 }
00707 }
00708
00709 energy2 = sqrt(energy2/(s->channels*s->block_align));
00710 energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align);
00711
00712
00713
00714
00715 if (energy2 > energy1)
00716 energy2 += (energy2-energy1)*RATE_VARIATION;
00717
00718 quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
00719
00720
00721 if (quant < 1)
00722 quant = 1;
00723 if (quant > 65535)
00724 quant = 65535;
00725
00726 set_ue_golomb(&pb, quant);
00727
00728 quant *= SAMPLE_FACTOR;
00729 }
00730
00731
00732 for (ch = 0; ch < s->channels; ch++)
00733 {
00734 if (!s->lossless)
00735 for (i = 0; i < s->block_align; i++)
00736 s->coded_samples[ch][i] = divide(s->coded_samples[ch][i], quant);
00737
00738 if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0)
00739 return -1;
00740 }
00741
00742
00743
00744 flush_put_bits(&pb);
00745 return (put_bits_count(&pb)+7)/8;
00746 }
00747 #endif //CONFIG_ENCODERS
00748
00749 static int sonic_decode_init(AVCodecContext *avctx)
00750 {
00751 SonicContext *s = avctx->priv_data;
00752 GetBitContext gb;
00753 int i, version;
00754
00755 s->channels = avctx->channels;
00756 s->samplerate = avctx->sample_rate;
00757
00758 if (!avctx->extradata)
00759 {
00760 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
00761 return -1;
00762 }
00763
00764 init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
00765
00766 version = get_bits(&gb, 2);
00767 if (version > 1)
00768 {
00769 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
00770 return -1;
00771 }
00772
00773 if (version == 1)
00774 {
00775 s->channels = get_bits(&gb, 2);
00776 s->samplerate = samplerate_table[get_bits(&gb, 4)];
00777 av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
00778 s->channels, s->samplerate);
00779 }
00780
00781 if (s->channels > MAX_CHANNELS)
00782 {
00783 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
00784 return -1;
00785 }
00786
00787 s->lossless = get_bits1(&gb);
00788 if (!s->lossless)
00789 skip_bits(&gb, 3);
00790 s->decorrelation = get_bits(&gb, 2);
00791
00792 s->downsampling = get_bits(&gb, 2);
00793 s->num_taps = (get_bits(&gb, 5)+1)<<5;
00794 if (get_bits1(&gb))
00795 av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
00796
00797 s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling;
00798 s->frame_size = s->channels*s->block_align*s->downsampling;
00799
00800
00801 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
00802 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
00803
00804
00805 s->tap_quant = av_mallocz(4* s->num_taps);
00806 for (i = 0; i < s->num_taps; i++)
00807 s->tap_quant[i] = (int)(sqrt(i+1));
00808
00809 s->predictor_k = av_mallocz(4* s->num_taps);
00810
00811 for (i = 0; i < s->channels; i++)
00812 {
00813 s->predictor_state[i] = av_mallocz(4* s->num_taps);
00814 if (!s->predictor_state[i])
00815 return -1;
00816 }
00817
00818 for (i = 0; i < s->channels; i++)
00819 {
00820 s->coded_samples[i] = av_mallocz(4* s->block_align);
00821 if (!s->coded_samples[i])
00822 return -1;
00823 }
00824 s->int_samples = av_mallocz(4* s->frame_size);
00825
00826 return 0;
00827 }
00828
00829 static int sonic_decode_close(AVCodecContext *avctx)
00830 {
00831 SonicContext *s = avctx->priv_data;
00832 int i;
00833
00834 av_free(s->int_samples);
00835 av_free(s->tap_quant);
00836 av_free(s->predictor_k);
00837
00838 for (i = 0; i < s->channels; i++)
00839 {
00840 av_free(s->predictor_state[i]);
00841 av_free(s->coded_samples[i]);
00842 }
00843
00844 return 0;
00845 }
00846
00847 static int sonic_decode_frame(AVCodecContext *avctx,
00848 void *data, int *data_size,
00849 uint8_t *buf, int buf_size)
00850 {
00851 SonicContext *s = avctx->priv_data;
00852 GetBitContext gb;
00853 int i, quant, ch, j;
00854 short *samples = data;
00855
00856 if (buf_size == 0) return 0;
00857
00858
00859
00860 init_get_bits(&gb, buf, buf_size*8);
00861
00862 intlist_read(&gb, s->predictor_k, s->num_taps, 0);
00863
00864
00865 for (i = 0; i < s->num_taps; i++)
00866 s->predictor_k[i] *= s->tap_quant[i];
00867
00868 if (s->lossless)
00869 quant = 1;
00870 else
00871 quant = get_ue_golomb(&gb) * SAMPLE_FACTOR;
00872
00873
00874
00875 for (ch = 0; ch < s->channels; ch++)
00876 {
00877 int x = ch;
00878
00879 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
00880
00881 intlist_read(&gb, s->coded_samples[ch], s->block_align, 1);
00882
00883 for (i = 0; i < s->block_align; i++)
00884 {
00885 for (j = 0; j < s->downsampling - 1; j++)
00886 {
00887 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
00888 x += s->channels;
00889 }
00890
00891 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
00892 x += s->channels;
00893 }
00894
00895 for (i = 0; i < s->num_taps; i++)
00896 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
00897 }
00898
00899 switch(s->decorrelation)
00900 {
00901 case MID_SIDE:
00902 for (i = 0; i < s->frame_size; i += s->channels)
00903 {
00904 s->int_samples[i+1] += shift(s->int_samples[i], 1);
00905 s->int_samples[i] -= s->int_samples[i+1];
00906 }
00907 break;
00908 case LEFT_SIDE:
00909 for (i = 0; i < s->frame_size; i += s->channels)
00910 s->int_samples[i+1] += s->int_samples[i];
00911 break;
00912 case RIGHT_SIDE:
00913 for (i = 0; i < s->frame_size; i += s->channels)
00914 s->int_samples[i] += s->int_samples[i+1];
00915 break;
00916 }
00917
00918 if (!s->lossless)
00919 for (i = 0; i < s->frame_size; i++)
00920 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
00921
00922
00923 for (i = 0; i < s->frame_size; i++)
00924 {
00925 if (s->int_samples[i] > 32767)
00926 samples[i] = 32767;
00927 else if (s->int_samples[i] < -32768)
00928 samples[i] = -32768;
00929 else
00930 samples[i] = s->int_samples[i];
00931 }
00932
00933 align_get_bits(&gb);
00934
00935 *data_size = s->frame_size * 2;
00936
00937 return (get_bits_count(&gb)+7)/8;
00938 }
00939
00940 #ifdef CONFIG_ENCODERS
00941 AVCodec sonic_encoder = {
00942 "sonic",
00943 CODEC_TYPE_AUDIO,
00944 CODEC_ID_SONIC,
00945 sizeof(SonicContext),
00946 sonic_encode_init,
00947 sonic_encode_frame,
00948 sonic_encode_close,
00949 NULL,
00950 };
00951
00952 AVCodec sonic_ls_encoder = {
00953 "sonicls",
00954 CODEC_TYPE_AUDIO,
00955 CODEC_ID_SONIC_LS,
00956 sizeof(SonicContext),
00957 sonic_encode_init,
00958 sonic_encode_frame,
00959 sonic_encode_close,
00960 NULL,
00961 };
00962 #endif
00963
00964 #ifdef CONFIG_DECODERS
00965 AVCodec sonic_decoder = {
00966 "sonic",
00967 CODEC_TYPE_AUDIO,
00968 CODEC_ID_SONIC,
00969 sizeof(SonicContext),
00970 sonic_decode_init,
00971 NULL,
00972 sonic_decode_close,
00973 sonic_decode_frame,
00974 };
00975 #endif