2 * Simple free lossless/lossy audio codec
3 * Copyright (c) 2004 Alex Beregszaszi
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "rangecoder.h"
30 * Simple free lossless/lossy audio codec
31 * Based on Paul Francis Harrison's Bonk (http://www.logarithmic.net/pfh/bonk)
32 * Written and designed by Alex Beregszaszi
35 * - CABAC put/get_symbol
36 * - independent quantizer for channels
37 * - >2 channels support
38 * - more decorrelation types
39 * - more tap_quant tests
40 * - selectable intlist writers/readers (bonk-style, golomb, cabac)
43 #define MAX_CHANNELS 2
49 typedef struct SonicContext {
52 int lossless, decorrelation;
54 int num_taps, downsampling;
57 int channels, samplerate, block_align, frame_size;
61 int *coded_samples[MAX_CHANNELS];
71 int *predictor_state[MAX_CHANNELS];
74 #define LATTICE_SHIFT 10
75 #define SAMPLE_SHIFT 4
76 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
77 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
79 #define BASE_QUANT 0.6
80 #define RATE_VARIATION 3.0
82 static inline int shift(int a,int b)
84 return (a+(1<<(b-1))) >> b;
87 static inline int shift_down(int a,int b)
92 static av_always_inline av_flatten void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2]){
95 #define put_rac(C,S,B) \
99 rc_stat2[(S)-state][B]++;\
105 const int a= FFABS(v);
106 const int e= av_log2(a);
107 put_rac(c, state+0, 0);
110 put_rac(c, state+1+i, 1); //1..10
112 put_rac(c, state+1+i, 0);
114 for(i=e-1; i>=0; i--){
115 put_rac(c, state+22+i, (a>>i)&1); //22..31
119 put_rac(c, state+11 + e, v < 0); //11..21
122 put_rac(c, state+1+FFMIN(i,9), 1); //1..10
124 put_rac(c, state+1+9, 0);
126 for(i=e-1; i>=0; i--){
127 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
131 put_rac(c, state+11 + 10, v < 0); //11..21
134 put_rac(c, state+0, 1);
139 static inline av_flatten int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
140 if(get_rac(c, state+0))
145 while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
148 return AVERROR_INVALIDDATA;
152 for(i=e-1; i>=0; i--){
153 a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
156 e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
162 static inline int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
166 for (i = 0; i < entries; i++)
167 put_symbol(c, state, buf[i], 1, NULL, NULL);
172 static inline int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
176 for (i = 0; i < entries; i++)
177 buf[i] = get_symbol(c, state, 1);
182 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
186 for (i = 0; i < entries; i++)
187 set_se_golomb(pb, buf[i]);
192 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
196 for (i = 0; i < entries; i++)
197 buf[i] = get_se_golomb(gb);
204 #define ADAPT_LEVEL 8
206 static int bits_to_store(uint64_t x)
218 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
225 bits = bits_to_store(max);
227 for (i = 0; i < bits-1; i++)
228 put_bits(pb, 1, value & (1 << i));
230 if ( (value | (1 << (bits-1))) <= max)
231 put_bits(pb, 1, value & (1 << (bits-1)));
234 static unsigned int read_uint_max(GetBitContext *gb, int max)
236 int i, bits, value = 0;
241 bits = bits_to_store(max);
243 for (i = 0; i < bits-1; i++)
247 if ( (value | (1<<(bits-1))) <= max)
249 value += 1 << (bits-1);
254 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
256 int i, j, x = 0, low_bits = 0, max = 0;
257 int step = 256, pos = 0, dominant = 0, any = 0;
260 copy = av_calloc(entries, sizeof(*copy));
262 return AVERROR(ENOMEM);
268 for (i = 0; i < entries; i++)
269 energy += abs(buf[i]);
271 low_bits = bits_to_store(energy / (entries * 2));
275 put_bits(pb, 4, low_bits);
278 for (i = 0; i < entries; i++)
280 put_bits(pb, low_bits, abs(buf[i]));
281 copy[i] = abs(buf[i]) >> low_bits;
286 bits = av_calloc(entries*max, sizeof(*bits));
290 return AVERROR(ENOMEM);
293 for (i = 0; i <= max; i++)
295 for (j = 0; j < entries; j++)
297 bits[x++] = copy[j] > i;
303 int steplet = step >> 8;
305 if (pos + steplet > x)
308 for (i = 0; i < steplet; i++)
309 if (bits[i+pos] != dominant)
312 put_bits(pb, 1, any);
317 step += step / ADAPT_LEVEL;
323 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
327 write_uint_max(pb, interloper, (step >> 8) - 1);
329 pos += interloper + 1;
330 step -= step / ADAPT_LEVEL;
336 dominant = !dominant;
341 for (i = 0; i < entries; i++)
343 put_bits(pb, 1, buf[i] < 0);
351 static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
353 int i, low_bits = 0, x = 0;
354 int n_zeros = 0, step = 256, dominant = 0;
355 int pos = 0, level = 0;
356 int *bits = av_calloc(entries, sizeof(*bits));
359 return AVERROR(ENOMEM);
363 low_bits = get_bits(gb, 4);
366 for (i = 0; i < entries; i++)
367 buf[i] = get_bits(gb, low_bits);
370 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
372 while (n_zeros < entries)
374 int steplet = step >> 8;
378 for (i = 0; i < steplet; i++)
379 bits[x++] = dominant;
384 step += step / ADAPT_LEVEL;
388 int actual_run = read_uint_max(gb, steplet-1);
390 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
392 for (i = 0; i < actual_run; i++)
393 bits[x++] = dominant;
395 bits[x++] = !dominant;
398 n_zeros += actual_run;
402 step -= step / ADAPT_LEVEL;
408 dominant = !dominant;
412 // reconstruct unsigned values
414 for (i = 0; n_zeros < entries; i++)
421 level += 1 << low_bits;
424 if (buf[pos] >= level)
431 buf[pos] += 1 << low_bits;
440 for (i = 0; i < entries; i++)
441 if (buf[i] && get_bits1(gb))
444 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
450 static void predictor_init_state(int *k, int *state, int order)
454 for (i = order-2; i >= 0; i--)
456 int j, p, x = state[i];
458 for (j = 0, p = i+1; p < order; j++,p++)
460 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
461 state[p] += shift_down(k[j]*x, LATTICE_SHIFT);
467 static int predictor_calc_error(int *k, int *state, int order, int error)
469 int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT);
472 int *k_ptr = &(k[order-2]),
473 *state_ptr = &(state[order-2]);
474 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
476 int k_value = *k_ptr, state_value = *state_ptr;
477 x -= shift_down(k_value * state_value, LATTICE_SHIFT);
478 state_ptr[1] = state_value + shift_down(k_value * (unsigned)x, LATTICE_SHIFT);
481 for (i = order-2; i >= 0; i--)
483 x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
484 state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
488 // don't drift too far, to avoid overflows
489 if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
490 if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16);
497 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
498 // Heavily modified Levinson-Durbin algorithm which
499 // copes better with quantization, and calculates the
500 // actual whitened result as it goes.
502 static int modified_levinson_durbin(int *window, int window_entries,
503 int *out, int out_entries, int channels, int *tap_quant)
506 int *state = av_calloc(window_entries, sizeof(*state));
509 return AVERROR(ENOMEM);
511 memcpy(state, window, 4* window_entries);
513 for (i = 0; i < out_entries; i++)
515 int step = (i+1)*channels, k, j;
516 double xx = 0.0, xy = 0.0;
518 int *x_ptr = &(window[step]);
519 int *state_ptr = &(state[0]);
520 j = window_entries - step;
521 for (;j>0;j--,x_ptr++,state_ptr++)
523 double x_value = *x_ptr;
524 double state_value = *state_ptr;
525 xx += state_value*state_value;
526 xy += x_value*state_value;
529 for (j = 0; j <= (window_entries - step); j++);
531 double stepval = window[step+j];
532 double stateval = window[j];
533 // xx += (double)window[j]*(double)window[j];
534 // xy += (double)window[step+j]*(double)window[j];
535 xx += stateval*stateval;
536 xy += stepval*stateval;
542 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
544 if (k > (LATTICE_FACTOR/tap_quant[i]))
545 k = LATTICE_FACTOR/tap_quant[i];
546 if (-k > (LATTICE_FACTOR/tap_quant[i]))
547 k = -(LATTICE_FACTOR/tap_quant[i]);
553 x_ptr = &(window[step]);
554 state_ptr = &(state[0]);
555 j = window_entries - step;
556 for (;j>0;j--,x_ptr++,state_ptr++)
558 int x_value = *x_ptr;
559 int state_value = *state_ptr;
560 *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
561 *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
564 for (j=0; j <= (window_entries - step); j++)
566 int stepval = window[step+j];
567 int stateval=state[j];
568 window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
569 state[j] += shift_down(k * stepval, LATTICE_SHIFT);
578 static inline int code_samplerate(int samplerate)
582 case 44100: return 0;
583 case 22050: return 1;
584 case 11025: return 2;
585 case 96000: return 3;
586 case 48000: return 4;
587 case 32000: return 5;
588 case 24000: return 6;
589 case 16000: return 7;
592 return AVERROR(EINVAL);
595 static av_cold int sonic_encode_init(AVCodecContext *avctx)
597 SonicContext *s = avctx->priv_data;
603 if (avctx->channels > MAX_CHANNELS)
605 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
606 return AVERROR(EINVAL); /* only stereo or mono for now */
609 if (avctx->channels == 2)
610 s->decorrelation = MID_SIDE;
612 s->decorrelation = 3;
614 if (avctx->codec->id == AV_CODEC_ID_SONIC_LS)
619 s->quantization = 0.0;
625 s->quantization = 1.0;
629 if (s->num_taps < 32 || s->num_taps > 1024 || s->num_taps % 32) {
630 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
631 return AVERROR_INVALIDDATA;
635 s->tap_quant = av_calloc(s->num_taps, sizeof(*s->tap_quant));
637 return AVERROR(ENOMEM);
639 for (i = 0; i < s->num_taps; i++)
640 s->tap_quant[i] = ff_sqrt(i+1);
642 s->channels = avctx->channels;
643 s->samplerate = avctx->sample_rate;
645 s->block_align = 2048LL*s->samplerate/(44100*s->downsampling);
646 s->frame_size = s->channels*s->block_align*s->downsampling;
648 s->tail_size = s->num_taps*s->channels;
649 s->tail = av_calloc(s->tail_size, sizeof(*s->tail));
651 return AVERROR(ENOMEM);
653 s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k) );
655 return AVERROR(ENOMEM);
657 for (i = 0; i < s->channels; i++)
659 s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples));
660 if (!s->coded_samples[i])
661 return AVERROR(ENOMEM);
664 s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
666 s->window_size = ((2*s->tail_size)+s->frame_size);
667 s->window = av_calloc(s->window_size, sizeof(*s->window));
668 if (!s->window || !s->int_samples)
669 return AVERROR(ENOMEM);
671 avctx->extradata = av_mallocz(16);
672 if (!avctx->extradata)
673 return AVERROR(ENOMEM);
674 init_put_bits(&pb, avctx->extradata, 16*8);
676 put_bits(&pb, 2, s->version); // version
679 if (s->version >= 2) {
680 put_bits(&pb, 8, s->version);
681 put_bits(&pb, 8, s->minor_version);
683 put_bits(&pb, 2, s->channels);
684 put_bits(&pb, 4, code_samplerate(s->samplerate));
686 put_bits(&pb, 1, s->lossless);
688 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
689 put_bits(&pb, 2, s->decorrelation);
690 put_bits(&pb, 2, s->downsampling);
691 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
692 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
695 avctx->extradata_size = put_bits_count(&pb)/8;
697 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
698 s->version, s->minor_version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
700 avctx->frame_size = s->block_align*s->downsampling;
705 static av_cold int sonic_encode_close(AVCodecContext *avctx)
707 SonicContext *s = avctx->priv_data;
710 for (i = 0; i < s->channels; i++)
711 av_freep(&s->coded_samples[i]);
713 av_freep(&s->predictor_k);
715 av_freep(&s->tap_quant);
716 av_freep(&s->window);
717 av_freep(&s->int_samples);
722 static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
723 const AVFrame *frame, int *got_packet_ptr)
725 SonicContext *s = avctx->priv_data;
727 int i, j, ch, quant = 0, x = 0;
729 const short *samples = (const int16_t*)frame->data[0];
732 if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000, 0)) < 0)
735 ff_init_range_encoder(&c, avpkt->data, avpkt->size);
736 ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);
737 memset(state, 128, sizeof(state));
740 for (i = 0; i < s->frame_size; i++)
741 s->int_samples[i] = samples[i];
744 for (i = 0; i < s->frame_size; i++)
745 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
747 switch(s->decorrelation)
750 for (i = 0; i < s->frame_size; i += s->channels)
752 s->int_samples[i] += s->int_samples[i+1];
753 s->int_samples[i+1] -= shift(s->int_samples[i], 1);
757 for (i = 0; i < s->frame_size; i += s->channels)
758 s->int_samples[i+1] -= s->int_samples[i];
761 for (i = 0; i < s->frame_size; i += s->channels)
762 s->int_samples[i] -= s->int_samples[i+1];
766 memset(s->window, 0, 4* s->window_size);
768 for (i = 0; i < s->tail_size; i++)
769 s->window[x++] = s->tail[i];
771 for (i = 0; i < s->frame_size; i++)
772 s->window[x++] = s->int_samples[i];
774 for (i = 0; i < s->tail_size; i++)
777 for (i = 0; i < s->tail_size; i++)
778 s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
781 ret = modified_levinson_durbin(s->window, s->window_size,
782 s->predictor_k, s->num_taps, s->channels, s->tap_quant);
786 if ((ret = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)
789 for (ch = 0; ch < s->channels; ch++)
792 for (i = 0; i < s->block_align; i++)
795 for (j = 0; j < s->downsampling; j++, x += s->channels)
797 s->coded_samples[ch][i] = sum;
801 // simple rate control code
804 double energy1 = 0.0, energy2 = 0.0;
805 for (ch = 0; ch < s->channels; ch++)
807 for (i = 0; i < s->block_align; i++)
809 double sample = s->coded_samples[ch][i];
810 energy2 += sample*sample;
811 energy1 += fabs(sample);
815 energy2 = sqrt(energy2/(s->channels*s->block_align));
816 energy1 = M_SQRT2*energy1/(s->channels*s->block_align);
818 // increase bitrate when samples are like a gaussian distribution
819 // reduce bitrate when samples are like a two-tailed exponential distribution
821 if (energy2 > energy1)
822 energy2 += (energy2-energy1)*RATE_VARIATION;
824 quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
825 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
827 quant = av_clip(quant, 1, 65534);
829 put_symbol(&c, state, quant, 0, NULL, NULL);
831 quant *= SAMPLE_FACTOR;
834 // write out coded samples
835 for (ch = 0; ch < s->channels; ch++)
838 for (i = 0; i < s->block_align; i++)
839 s->coded_samples[ch][i] = ROUNDED_DIV(s->coded_samples[ch][i], quant);
841 if ((ret = intlist_write(&c, state, s->coded_samples[ch], s->block_align, 1)) < 0)
845 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
847 avpkt->size = ff_rac_terminate(&c, 0);
852 #endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
854 #if CONFIG_SONIC_DECODER
855 static const int samplerate_table[] =
856 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
858 static av_cold int sonic_decode_init(AVCodecContext *avctx)
860 SonicContext *s = avctx->priv_data;
865 s->channels = avctx->channels;
866 s->samplerate = avctx->sample_rate;
868 if (!avctx->extradata)
870 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
871 return AVERROR_INVALIDDATA;
874 ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
878 s->version = get_bits(&gb, 2);
879 if (s->version >= 2) {
880 s->version = get_bits(&gb, 8);
881 s->minor_version = get_bits(&gb, 8);
885 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
886 return AVERROR_INVALIDDATA;
891 int sample_rate_index;
892 s->channels = get_bits(&gb, 2);
893 sample_rate_index = get_bits(&gb, 4);
894 if (sample_rate_index >= FF_ARRAY_ELEMS(samplerate_table)) {
895 av_log(avctx, AV_LOG_ERROR, "Invalid sample_rate_index %d\n", sample_rate_index);
896 return AVERROR_INVALIDDATA;
898 s->samplerate = samplerate_table[sample_rate_index];
899 av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
900 s->channels, s->samplerate);
903 if (s->channels > MAX_CHANNELS || s->channels < 1)
905 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
906 return AVERROR_INVALIDDATA;
908 avctx->channels = s->channels;
910 s->lossless = get_bits1(&gb);
912 skip_bits(&gb, 3); // XXX FIXME
913 s->decorrelation = get_bits(&gb, 2);
914 if (s->decorrelation != 3 && s->channels != 2) {
915 av_log(avctx, AV_LOG_ERROR, "invalid decorrelation %d\n", s->decorrelation);
916 return AVERROR_INVALIDDATA;
919 s->downsampling = get_bits(&gb, 2);
920 if (!s->downsampling) {
921 av_log(avctx, AV_LOG_ERROR, "invalid downsampling value\n");
922 return AVERROR_INVALIDDATA;
925 s->num_taps = (get_bits(&gb, 5)+1)<<5;
926 if (get_bits1(&gb)) // XXX FIXME
927 av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
929 s->block_align = 2048LL*s->samplerate/(44100*s->downsampling);
930 s->frame_size = s->channels*s->block_align*s->downsampling;
931 // avctx->frame_size = s->block_align;
933 if (s->num_taps * s->channels > s->frame_size) {
934 av_log(avctx, AV_LOG_ERROR,
935 "number of taps times channels (%d * %d) larger than frame size %d\n",
936 s->num_taps, s->channels, s->frame_size);
937 return AVERROR_INVALIDDATA;
940 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
941 s->version, s->minor_version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
944 s->tap_quant = av_calloc(s->num_taps, sizeof(*s->tap_quant));
946 return AVERROR(ENOMEM);
948 for (i = 0; i < s->num_taps; i++)
949 s->tap_quant[i] = ff_sqrt(i+1);
951 s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k));
953 for (i = 0; i < s->channels; i++)
955 s->predictor_state[i] = av_calloc(s->num_taps, sizeof(**s->predictor_state));
956 if (!s->predictor_state[i])
957 return AVERROR(ENOMEM);
960 for (i = 0; i < s->channels; i++)
962 s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples));
963 if (!s->coded_samples[i])
964 return AVERROR(ENOMEM);
966 s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
968 return AVERROR(ENOMEM);
970 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
974 static av_cold int sonic_decode_close(AVCodecContext *avctx)
976 SonicContext *s = avctx->priv_data;
979 av_freep(&s->int_samples);
980 av_freep(&s->tap_quant);
981 av_freep(&s->predictor_k);
983 for (i = 0; i < s->channels; i++)
985 av_freep(&s->predictor_state[i]);
986 av_freep(&s->coded_samples[i]);
992 static int sonic_decode_frame(AVCodecContext *avctx,
993 void *data, int *got_frame_ptr,
996 const uint8_t *buf = avpkt->data;
997 int buf_size = avpkt->size;
998 SonicContext *s = avctx->priv_data;
1001 int i, quant, ch, j, ret;
1003 AVFrame *frame = data;
1005 if (buf_size == 0) return 0;
1007 frame->nb_samples = s->frame_size / avctx->channels;
1008 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1010 samples = (int16_t *)frame->data[0];
1012 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
1014 memset(state, 128, sizeof(state));
1015 ff_init_range_decoder(&c, buf, buf_size);
1016 ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);
1018 intlist_read(&c, state, s->predictor_k, s->num_taps, 0);
1021 for (i = 0; i < s->num_taps; i++)
1022 s->predictor_k[i] *= s->tap_quant[i];
1027 quant = get_symbol(&c, state, 0) * SAMPLE_FACTOR;
1029 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
1031 for (ch = 0; ch < s->channels; ch++)
1035 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
1037 intlist_read(&c, state, s->coded_samples[ch], s->block_align, 1);
1039 for (i = 0; i < s->block_align; i++)
1041 for (j = 0; j < s->downsampling - 1; j++)
1043 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
1047 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
1051 for (i = 0; i < s->num_taps; i++)
1052 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
1055 switch(s->decorrelation)
1058 for (i = 0; i < s->frame_size; i += s->channels)
1060 s->int_samples[i+1] += shift(s->int_samples[i], 1);
1061 s->int_samples[i] -= s->int_samples[i+1];
1065 for (i = 0; i < s->frame_size; i += s->channels)
1066 s->int_samples[i+1] += s->int_samples[i];
1069 for (i = 0; i < s->frame_size; i += s->channels)
1070 s->int_samples[i] += s->int_samples[i+1];
1075 for (i = 0; i < s->frame_size; i++)
1076 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
1078 // internal -> short
1079 for (i = 0; i < s->frame_size; i++)
1080 samples[i] = av_clip_int16(s->int_samples[i]);
1087 AVCodec ff_sonic_decoder = {
1089 .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
1090 .type = AVMEDIA_TYPE_AUDIO,
1091 .id = AV_CODEC_ID_SONIC,
1092 .priv_data_size = sizeof(SonicContext),
1093 .init = sonic_decode_init,
1094 .close = sonic_decode_close,
1095 .decode = sonic_decode_frame,
1096 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_EXPERIMENTAL,
1098 #endif /* CONFIG_SONIC_DECODER */
1100 #if CONFIG_SONIC_ENCODER
1101 AVCodec ff_sonic_encoder = {
1103 .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
1104 .type = AVMEDIA_TYPE_AUDIO,
1105 .id = AV_CODEC_ID_SONIC,
1106 .priv_data_size = sizeof(SonicContext),
1107 .init = sonic_encode_init,
1108 .encode2 = sonic_encode_frame,
1109 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
1110 .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1111 .close = sonic_encode_close,
1115 #if CONFIG_SONIC_LS_ENCODER
1116 AVCodec ff_sonic_ls_encoder = {
1118 .long_name = NULL_IF_CONFIG_SMALL("Sonic lossless"),
1119 .type = AVMEDIA_TYPE_AUDIO,
1120 .id = AV_CODEC_ID_SONIC_LS,
1121 .priv_data_size = sizeof(SonicContext),
1122 .init = sonic_encode_init,
1123 .encode2 = sonic_encode_frame,
1124 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
1125 .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1126 .close = sonic_encode_close,