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))
146 while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
149 return AVERROR_INVALIDDATA;
153 for(i=e-1; i>=0; i--){
154 a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
157 e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
163 static inline int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
167 for (i = 0; i < entries; i++)
168 put_symbol(c, state, buf[i], 1, NULL, NULL);
173 static inline int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
177 for (i = 0; i < entries; i++)
178 buf[i] = get_symbol(c, state, 1);
183 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
187 for (i = 0; i < entries; i++)
188 set_se_golomb(pb, buf[i]);
193 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
197 for (i = 0; i < entries; i++)
198 buf[i] = get_se_golomb(gb);
205 #define ADAPT_LEVEL 8
207 static int bits_to_store(uint64_t x)
219 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
226 bits = bits_to_store(max);
228 for (i = 0; i < bits-1; i++)
229 put_bits(pb, 1, value & (1 << i));
231 if ( (value | (1 << (bits-1))) <= max)
232 put_bits(pb, 1, value & (1 << (bits-1)));
235 static unsigned int read_uint_max(GetBitContext *gb, int max)
237 int i, bits, value = 0;
242 bits = bits_to_store(max);
244 for (i = 0; i < bits-1; i++)
248 if ( (value | (1<<(bits-1))) <= max)
250 value += 1 << (bits-1);
255 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
257 int i, j, x = 0, low_bits = 0, max = 0;
258 int step = 256, pos = 0, dominant = 0, any = 0;
261 copy = av_calloc(entries, sizeof(*copy));
263 return AVERROR(ENOMEM);
269 for (i = 0; i < entries; i++)
270 energy += abs(buf[i]);
272 low_bits = bits_to_store(energy / (entries * 2));
276 put_bits(pb, 4, low_bits);
279 for (i = 0; i < entries; i++)
281 put_bits(pb, low_bits, abs(buf[i]));
282 copy[i] = abs(buf[i]) >> low_bits;
287 bits = av_calloc(entries*max, sizeof(*bits));
291 return AVERROR(ENOMEM);
294 for (i = 0; i <= max; i++)
296 for (j = 0; j < entries; j++)
298 bits[x++] = copy[j] > i;
304 int steplet = step >> 8;
306 if (pos + steplet > x)
309 for (i = 0; i < steplet; i++)
310 if (bits[i+pos] != dominant)
313 put_bits(pb, 1, any);
318 step += step / ADAPT_LEVEL;
324 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
328 write_uint_max(pb, interloper, (step >> 8) - 1);
330 pos += interloper + 1;
331 step -= step / ADAPT_LEVEL;
337 dominant = !dominant;
342 for (i = 0; i < entries; i++)
344 put_bits(pb, 1, buf[i] < 0);
352 static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
354 int i, low_bits = 0, x = 0;
355 int n_zeros = 0, step = 256, dominant = 0;
356 int pos = 0, level = 0;
357 int *bits = av_calloc(entries, sizeof(*bits));
360 return AVERROR(ENOMEM);
364 low_bits = get_bits(gb, 4);
367 for (i = 0; i < entries; i++)
368 buf[i] = get_bits(gb, low_bits);
371 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
373 while (n_zeros < entries)
375 int steplet = step >> 8;
379 for (i = 0; i < steplet; i++)
380 bits[x++] = dominant;
385 step += step / ADAPT_LEVEL;
389 int actual_run = read_uint_max(gb, steplet-1);
391 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
393 for (i = 0; i < actual_run; i++)
394 bits[x++] = dominant;
396 bits[x++] = !dominant;
399 n_zeros += actual_run;
403 step -= step / ADAPT_LEVEL;
409 dominant = !dominant;
413 // reconstruct unsigned values
415 for (i = 0; n_zeros < entries; i++)
422 level += 1 << low_bits;
425 if (buf[pos] >= level)
432 buf[pos] += 1 << low_bits;
441 for (i = 0; i < entries; i++)
442 if (buf[i] && get_bits1(gb))
445 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
451 static void predictor_init_state(int *k, int *state, int order)
455 for (i = order-2; i >= 0; i--)
457 int j, p, x = state[i];
459 for (j = 0, p = i+1; p < order; j++,p++)
461 int tmp = x + shift_down(k[j] * (unsigned)state[p], LATTICE_SHIFT);
462 state[p] += shift_down(k[j]* (unsigned)x, LATTICE_SHIFT);
468 static int predictor_calc_error(int *k, int *state, int order, int error)
470 int i, x = error - shift_down(k[order-1] * (unsigned)state[order-1], LATTICE_SHIFT);
473 int *k_ptr = &(k[order-2]),
474 *state_ptr = &(state[order-2]);
475 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
477 int k_value = *k_ptr, state_value = *state_ptr;
478 x -= shift_down(k_value * (unsigned)state_value, LATTICE_SHIFT);
479 state_ptr[1] = state_value + shift_down(k_value * (unsigned)x, LATTICE_SHIFT);
482 for (i = order-2; i >= 0; i--)
484 x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
485 state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
489 // don't drift too far, to avoid overflows
490 if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
491 if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16);
498 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
499 // Heavily modified Levinson-Durbin algorithm which
500 // copes better with quantization, and calculates the
501 // actual whitened result as it goes.
503 static int modified_levinson_durbin(int *window, int window_entries,
504 int *out, int out_entries, int channels, int *tap_quant)
507 int *state = av_calloc(window_entries, sizeof(*state));
510 return AVERROR(ENOMEM);
512 memcpy(state, window, 4* window_entries);
514 for (i = 0; i < out_entries; i++)
516 int step = (i+1)*channels, k, j;
517 double xx = 0.0, xy = 0.0;
519 int *x_ptr = &(window[step]);
520 int *state_ptr = &(state[0]);
521 j = window_entries - step;
522 for (;j>0;j--,x_ptr++,state_ptr++)
524 double x_value = *x_ptr;
525 double state_value = *state_ptr;
526 xx += state_value*state_value;
527 xy += x_value*state_value;
530 for (j = 0; j <= (window_entries - step); j++);
532 double stepval = window[step+j];
533 double stateval = window[j];
534 // xx += (double)window[j]*(double)window[j];
535 // xy += (double)window[step+j]*(double)window[j];
536 xx += stateval*stateval;
537 xy += stepval*stateval;
543 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
545 if (k > (LATTICE_FACTOR/tap_quant[i]))
546 k = LATTICE_FACTOR/tap_quant[i];
547 if (-k > (LATTICE_FACTOR/tap_quant[i]))
548 k = -(LATTICE_FACTOR/tap_quant[i]);
554 x_ptr = &(window[step]);
555 state_ptr = &(state[0]);
556 j = window_entries - step;
557 for (;j>0;j--,x_ptr++,state_ptr++)
559 int x_value = *x_ptr;
560 int state_value = *state_ptr;
561 *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
562 *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
565 for (j=0; j <= (window_entries - step); j++)
567 int stepval = window[step+j];
568 int stateval=state[j];
569 window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
570 state[j] += shift_down(k * stepval, LATTICE_SHIFT);
579 static inline int code_samplerate(int samplerate)
583 case 44100: return 0;
584 case 22050: return 1;
585 case 11025: return 2;
586 case 96000: return 3;
587 case 48000: return 4;
588 case 32000: return 5;
589 case 24000: return 6;
590 case 16000: return 7;
593 return AVERROR(EINVAL);
596 static av_cold int sonic_encode_init(AVCodecContext *avctx)
598 SonicContext *s = avctx->priv_data;
604 if (avctx->channels > MAX_CHANNELS)
606 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
607 return AVERROR(EINVAL); /* only stereo or mono for now */
610 if (avctx->channels == 2)
611 s->decorrelation = MID_SIDE;
613 s->decorrelation = 3;
615 if (avctx->codec->id == AV_CODEC_ID_SONIC_LS)
620 s->quantization = 0.0;
626 s->quantization = 1.0;
630 if (s->num_taps < 32 || s->num_taps > 1024 || s->num_taps % 32) {
631 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
632 return AVERROR_INVALIDDATA;
636 s->tap_quant = av_calloc(s->num_taps, sizeof(*s->tap_quant));
638 return AVERROR(ENOMEM);
640 for (i = 0; i < s->num_taps; i++)
641 s->tap_quant[i] = ff_sqrt(i+1);
643 s->channels = avctx->channels;
644 s->samplerate = avctx->sample_rate;
646 s->block_align = 2048LL*s->samplerate/(44100*s->downsampling);
647 s->frame_size = s->channels*s->block_align*s->downsampling;
649 s->tail_size = s->num_taps*s->channels;
650 s->tail = av_calloc(s->tail_size, sizeof(*s->tail));
652 return AVERROR(ENOMEM);
654 s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k) );
656 return AVERROR(ENOMEM);
658 for (i = 0; i < s->channels; i++)
660 s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples));
661 if (!s->coded_samples[i])
662 return AVERROR(ENOMEM);
665 s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
667 s->window_size = ((2*s->tail_size)+s->frame_size);
668 s->window = av_calloc(s->window_size, sizeof(*s->window));
669 if (!s->window || !s->int_samples)
670 return AVERROR(ENOMEM);
672 avctx->extradata = av_mallocz(16);
673 if (!avctx->extradata)
674 return AVERROR(ENOMEM);
675 init_put_bits(&pb, avctx->extradata, 16*8);
677 put_bits(&pb, 2, s->version); // version
680 if (s->version >= 2) {
681 put_bits(&pb, 8, s->version);
682 put_bits(&pb, 8, s->minor_version);
684 put_bits(&pb, 2, s->channels);
685 put_bits(&pb, 4, code_samplerate(s->samplerate));
687 put_bits(&pb, 1, s->lossless);
689 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
690 put_bits(&pb, 2, s->decorrelation);
691 put_bits(&pb, 2, s->downsampling);
692 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
693 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
696 avctx->extradata_size = put_bits_count(&pb)/8;
698 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
699 s->version, s->minor_version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
701 avctx->frame_size = s->block_align*s->downsampling;
706 static av_cold int sonic_encode_close(AVCodecContext *avctx)
708 SonicContext *s = avctx->priv_data;
711 for (i = 0; i < s->channels; i++)
712 av_freep(&s->coded_samples[i]);
714 av_freep(&s->predictor_k);
716 av_freep(&s->tap_quant);
717 av_freep(&s->window);
718 av_freep(&s->int_samples);
723 static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
724 const AVFrame *frame, int *got_packet_ptr)
726 SonicContext *s = avctx->priv_data;
728 int i, j, ch, quant = 0, x = 0;
730 const short *samples = (const int16_t*)frame->data[0];
733 if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000, 0)) < 0)
736 ff_init_range_encoder(&c, avpkt->data, avpkt->size);
737 ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);
738 memset(state, 128, sizeof(state));
741 for (i = 0; i < s->frame_size; i++)
742 s->int_samples[i] = samples[i];
745 for (i = 0; i < s->frame_size; i++)
746 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
748 switch(s->decorrelation)
751 for (i = 0; i < s->frame_size; i += s->channels)
753 s->int_samples[i] += s->int_samples[i+1];
754 s->int_samples[i+1] -= shift(s->int_samples[i], 1);
758 for (i = 0; i < s->frame_size; i += s->channels)
759 s->int_samples[i+1] -= s->int_samples[i];
762 for (i = 0; i < s->frame_size; i += s->channels)
763 s->int_samples[i] -= s->int_samples[i+1];
767 memset(s->window, 0, 4* s->window_size);
769 for (i = 0; i < s->tail_size; i++)
770 s->window[x++] = s->tail[i];
772 for (i = 0; i < s->frame_size; i++)
773 s->window[x++] = s->int_samples[i];
775 for (i = 0; i < s->tail_size; i++)
778 for (i = 0; i < s->tail_size; i++)
779 s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
782 ret = modified_levinson_durbin(s->window, s->window_size,
783 s->predictor_k, s->num_taps, s->channels, s->tap_quant);
787 if ((ret = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)
790 for (ch = 0; ch < s->channels; ch++)
793 for (i = 0; i < s->block_align; i++)
796 for (j = 0; j < s->downsampling; j++, x += s->channels)
798 s->coded_samples[ch][i] = sum;
802 // simple rate control code
805 double energy1 = 0.0, energy2 = 0.0;
806 for (ch = 0; ch < s->channels; ch++)
808 for (i = 0; i < s->block_align; i++)
810 double sample = s->coded_samples[ch][i];
811 energy2 += sample*sample;
812 energy1 += fabs(sample);
816 energy2 = sqrt(energy2/(s->channels*s->block_align));
817 energy1 = M_SQRT2*energy1/(s->channels*s->block_align);
819 // increase bitrate when samples are like a gaussian distribution
820 // reduce bitrate when samples are like a two-tailed exponential distribution
822 if (energy2 > energy1)
823 energy2 += (energy2-energy1)*RATE_VARIATION;
825 quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
826 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
828 quant = av_clip(quant, 1, 65534);
830 put_symbol(&c, state, quant, 0, NULL, NULL);
832 quant *= SAMPLE_FACTOR;
835 // write out coded samples
836 for (ch = 0; ch < s->channels; ch++)
839 for (i = 0; i < s->block_align; i++)
840 s->coded_samples[ch][i] = ROUNDED_DIV(s->coded_samples[ch][i], quant);
842 if ((ret = intlist_write(&c, state, s->coded_samples[ch], s->block_align, 1)) < 0)
846 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
848 avpkt->size = ff_rac_terminate(&c, 0);
853 #endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
855 #if CONFIG_SONIC_DECODER
856 static const int samplerate_table[] =
857 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
859 static av_cold int sonic_decode_init(AVCodecContext *avctx)
861 SonicContext *s = avctx->priv_data;
866 s->channels = avctx->channels;
867 s->samplerate = avctx->sample_rate;
869 if (!avctx->extradata)
871 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
872 return AVERROR_INVALIDDATA;
875 ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
879 s->version = get_bits(&gb, 2);
880 if (s->version >= 2) {
881 s->version = get_bits(&gb, 8);
882 s->minor_version = get_bits(&gb, 8);
886 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
887 return AVERROR_INVALIDDATA;
892 int sample_rate_index;
893 s->channels = get_bits(&gb, 2);
894 sample_rate_index = get_bits(&gb, 4);
895 if (sample_rate_index >= FF_ARRAY_ELEMS(samplerate_table)) {
896 av_log(avctx, AV_LOG_ERROR, "Invalid sample_rate_index %d\n", sample_rate_index);
897 return AVERROR_INVALIDDATA;
899 s->samplerate = samplerate_table[sample_rate_index];
900 av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
901 s->channels, s->samplerate);
904 if (s->channels > MAX_CHANNELS || s->channels < 1)
906 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
907 return AVERROR_INVALIDDATA;
909 avctx->channels = s->channels;
911 s->lossless = get_bits1(&gb);
913 skip_bits(&gb, 3); // XXX FIXME
914 s->decorrelation = get_bits(&gb, 2);
915 if (s->decorrelation != 3 && s->channels != 2) {
916 av_log(avctx, AV_LOG_ERROR, "invalid decorrelation %d\n", s->decorrelation);
917 return AVERROR_INVALIDDATA;
920 s->downsampling = get_bits(&gb, 2);
921 if (!s->downsampling) {
922 av_log(avctx, AV_LOG_ERROR, "invalid downsampling value\n");
923 return AVERROR_INVALIDDATA;
926 s->num_taps = (get_bits(&gb, 5)+1)<<5;
927 if (get_bits1(&gb)) // XXX FIXME
928 av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
930 s->block_align = 2048LL*s->samplerate/(44100*s->downsampling);
931 s->frame_size = s->channels*s->block_align*s->downsampling;
932 // avctx->frame_size = s->block_align;
934 if (s->num_taps * s->channels > s->frame_size) {
935 av_log(avctx, AV_LOG_ERROR,
936 "number of taps times channels (%d * %d) larger than frame size %d\n",
937 s->num_taps, s->channels, s->frame_size);
938 return AVERROR_INVALIDDATA;
941 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
942 s->version, s->minor_version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
945 s->tap_quant = av_calloc(s->num_taps, sizeof(*s->tap_quant));
947 return AVERROR(ENOMEM);
949 for (i = 0; i < s->num_taps; i++)
950 s->tap_quant[i] = ff_sqrt(i+1);
952 s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k));
954 for (i = 0; i < s->channels; i++)
956 s->predictor_state[i] = av_calloc(s->num_taps, sizeof(**s->predictor_state));
957 if (!s->predictor_state[i])
958 return AVERROR(ENOMEM);
961 for (i = 0; i < s->channels; i++)
963 s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples));
964 if (!s->coded_samples[i])
965 return AVERROR(ENOMEM);
967 s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
969 return AVERROR(ENOMEM);
971 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
975 static av_cold int sonic_decode_close(AVCodecContext *avctx)
977 SonicContext *s = avctx->priv_data;
980 av_freep(&s->int_samples);
981 av_freep(&s->tap_quant);
982 av_freep(&s->predictor_k);
984 for (i = 0; i < s->channels; i++)
986 av_freep(&s->predictor_state[i]);
987 av_freep(&s->coded_samples[i]);
993 static int sonic_decode_frame(AVCodecContext *avctx,
994 void *data, int *got_frame_ptr,
997 const uint8_t *buf = avpkt->data;
998 int buf_size = avpkt->size;
999 SonicContext *s = avctx->priv_data;
1002 int i, quant, ch, j, ret;
1004 AVFrame *frame = data;
1006 if (buf_size == 0) return 0;
1008 frame->nb_samples = s->frame_size / avctx->channels;
1009 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1011 samples = (int16_t *)frame->data[0];
1013 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
1015 memset(state, 128, sizeof(state));
1016 ff_init_range_decoder(&c, buf, buf_size);
1017 ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);
1019 intlist_read(&c, state, s->predictor_k, s->num_taps, 0);
1022 for (i = 0; i < s->num_taps; i++)
1023 s->predictor_k[i] *= s->tap_quant[i];
1028 quant = get_symbol(&c, state, 0) * SAMPLE_FACTOR;
1030 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
1032 for (ch = 0; ch < s->channels; ch++)
1036 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
1038 intlist_read(&c, state, s->coded_samples[ch], s->block_align, 1);
1040 for (i = 0; i < s->block_align; i++)
1042 for (j = 0; j < s->downsampling - 1; j++)
1044 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
1048 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * (unsigned)quant);
1052 for (i = 0; i < s->num_taps; i++)
1053 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
1056 switch(s->decorrelation)
1059 for (i = 0; i < s->frame_size; i += s->channels)
1061 s->int_samples[i+1] += shift(s->int_samples[i], 1);
1062 s->int_samples[i] -= s->int_samples[i+1];
1066 for (i = 0; i < s->frame_size; i += s->channels)
1067 s->int_samples[i+1] += s->int_samples[i];
1070 for (i = 0; i < s->frame_size; i += s->channels)
1071 s->int_samples[i] += s->int_samples[i+1];
1076 for (i = 0; i < s->frame_size; i++)
1077 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
1079 // internal -> short
1080 for (i = 0; i < s->frame_size; i++)
1081 samples[i] = av_clip_int16(s->int_samples[i]);
1088 AVCodec ff_sonic_decoder = {
1090 .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
1091 .type = AVMEDIA_TYPE_AUDIO,
1092 .id = AV_CODEC_ID_SONIC,
1093 .priv_data_size = sizeof(SonicContext),
1094 .init = sonic_decode_init,
1095 .close = sonic_decode_close,
1096 .decode = sonic_decode_frame,
1097 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_EXPERIMENTAL,
1099 #endif /* CONFIG_SONIC_DECODER */
1101 #if CONFIG_SONIC_ENCODER
1102 AVCodec ff_sonic_encoder = {
1104 .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
1105 .type = AVMEDIA_TYPE_AUDIO,
1106 .id = AV_CODEC_ID_SONIC,
1107 .priv_data_size = sizeof(SonicContext),
1108 .init = sonic_encode_init,
1109 .encode2 = sonic_encode_frame,
1110 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
1111 .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1112 .close = sonic_encode_close,
1116 #if CONFIG_SONIC_LS_ENCODER
1117 AVCodec ff_sonic_ls_encoder = {
1119 .long_name = NULL_IF_CONFIG_SMALL("Sonic lossless"),
1120 .type = AVMEDIA_TYPE_AUDIO,
1121 .id = AV_CODEC_ID_SONIC_LS,
1122 .priv_data_size = sizeof(SonicContext),
1123 .init = sonic_encode_init,
1124 .encode2 = sonic_encode_frame,
1125 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
1126 .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1127 .close = sonic_encode_close,