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