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
150 for(i=e-1; i>=0; i--){
151 a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
154 e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
160 static inline int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
164 for (i = 0; i < entries; i++)
165 put_symbol(c, state, buf[i], 1, NULL, NULL);
170 static inline int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
174 for (i = 0; i < entries; i++)
175 buf[i] = get_symbol(c, state, 1);
180 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
184 for (i = 0; i < entries; i++)
185 set_se_golomb(pb, buf[i]);
190 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
194 for (i = 0; i < entries; i++)
195 buf[i] = get_se_golomb(gb);
202 #define ADAPT_LEVEL 8
204 static int bits_to_store(uint64_t x)
216 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
223 bits = bits_to_store(max);
225 for (i = 0; i < bits-1; i++)
226 put_bits(pb, 1, value & (1 << i));
228 if ( (value | (1 << (bits-1))) <= max)
229 put_bits(pb, 1, value & (1 << (bits-1)));
232 static unsigned int read_uint_max(GetBitContext *gb, int max)
234 int i, bits, value = 0;
239 bits = bits_to_store(max);
241 for (i = 0; i < bits-1; i++)
245 if ( (value | (1<<(bits-1))) <= max)
247 value += 1 << (bits-1);
252 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
254 int i, j, x = 0, low_bits = 0, max = 0;
255 int step = 256, pos = 0, dominant = 0, any = 0;
258 copy = av_calloc(entries, sizeof(*copy));
260 return AVERROR(ENOMEM);
266 for (i = 0; i < entries; i++)
267 energy += abs(buf[i]);
269 low_bits = bits_to_store(energy / (entries * 2));
273 put_bits(pb, 4, low_bits);
276 for (i = 0; i < entries; i++)
278 put_bits(pb, low_bits, abs(buf[i]));
279 copy[i] = abs(buf[i]) >> low_bits;
284 bits = av_calloc(entries*max, sizeof(*bits));
288 return AVERROR(ENOMEM);
291 for (i = 0; i <= max; i++)
293 for (j = 0; j < entries; j++)
295 bits[x++] = copy[j] > i;
301 int steplet = step >> 8;
303 if (pos + steplet > x)
306 for (i = 0; i < steplet; i++)
307 if (bits[i+pos] != dominant)
310 put_bits(pb, 1, any);
315 step += step / ADAPT_LEVEL;
321 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
325 write_uint_max(pb, interloper, (step >> 8) - 1);
327 pos += interloper + 1;
328 step -= step / ADAPT_LEVEL;
334 dominant = !dominant;
339 for (i = 0; i < entries; i++)
341 put_bits(pb, 1, buf[i] < 0);
349 static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
351 int i, low_bits = 0, x = 0;
352 int n_zeros = 0, step = 256, dominant = 0;
353 int pos = 0, level = 0;
354 int *bits = av_calloc(entries, sizeof(*bits));
357 return AVERROR(ENOMEM);
361 low_bits = get_bits(gb, 4);
364 for (i = 0; i < entries; i++)
365 buf[i] = get_bits(gb, low_bits);
368 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
370 while (n_zeros < entries)
372 int steplet = step >> 8;
376 for (i = 0; i < steplet; i++)
377 bits[x++] = dominant;
382 step += step / ADAPT_LEVEL;
386 int actual_run = read_uint_max(gb, steplet-1);
388 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
390 for (i = 0; i < actual_run; i++)
391 bits[x++] = dominant;
393 bits[x++] = !dominant;
396 n_zeros += actual_run;
400 step -= step / ADAPT_LEVEL;
406 dominant = !dominant;
410 // reconstruct unsigned values
412 for (i = 0; n_zeros < entries; i++)
419 level += 1 << low_bits;
422 if (buf[pos] >= level)
429 buf[pos] += 1 << low_bits;
438 for (i = 0; i < entries; i++)
439 if (buf[i] && get_bits1(gb))
442 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
448 static void predictor_init_state(int *k, int *state, int order)
452 for (i = order-2; i >= 0; i--)
454 int j, p, x = state[i];
456 for (j = 0, p = i+1; p < order; j++,p++)
458 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
459 state[p] += shift_down(k[j]*x, LATTICE_SHIFT);
465 static int predictor_calc_error(int *k, int *state, int order, int error)
467 int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT);
470 int *k_ptr = &(k[order-2]),
471 *state_ptr = &(state[order-2]);
472 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
474 int k_value = *k_ptr, state_value = *state_ptr;
475 x -= shift_down(k_value * state_value, LATTICE_SHIFT);
476 state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
479 for (i = order-2; i >= 0; i--)
481 x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
482 state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
486 // don't drift too far, to avoid overflows
487 if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
488 if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16);
495 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
496 // Heavily modified Levinson-Durbin algorithm which
497 // copes better with quantization, and calculates the
498 // actual whitened result as it goes.
500 static int modified_levinson_durbin(int *window, int window_entries,
501 int *out, int out_entries, int channels, int *tap_quant)
504 int *state = av_calloc(window_entries, sizeof(*state));
507 return AVERROR(ENOMEM);
509 memcpy(state, window, 4* window_entries);
511 for (i = 0; i < out_entries; i++)
513 int step = (i+1)*channels, k, j;
514 double xx = 0.0, xy = 0.0;
516 int *x_ptr = &(window[step]);
517 int *state_ptr = &(state[0]);
518 j = window_entries - step;
519 for (;j>0;j--,x_ptr++,state_ptr++)
521 double x_value = *x_ptr;
522 double state_value = *state_ptr;
523 xx += state_value*state_value;
524 xy += x_value*state_value;
527 for (j = 0; j <= (window_entries - step); j++);
529 double stepval = window[step+j];
530 double stateval = window[j];
531 // xx += (double)window[j]*(double)window[j];
532 // xy += (double)window[step+j]*(double)window[j];
533 xx += stateval*stateval;
534 xy += stepval*stateval;
540 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
542 if (k > (LATTICE_FACTOR/tap_quant[i]))
543 k = LATTICE_FACTOR/tap_quant[i];
544 if (-k > (LATTICE_FACTOR/tap_quant[i]))
545 k = -(LATTICE_FACTOR/tap_quant[i]);
551 x_ptr = &(window[step]);
552 state_ptr = &(state[0]);
553 j = window_entries - step;
554 for (;j>0;j--,x_ptr++,state_ptr++)
556 int x_value = *x_ptr;
557 int state_value = *state_ptr;
558 *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
559 *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
562 for (j=0; j <= (window_entries - step); j++)
564 int stepval = window[step+j];
565 int stateval=state[j];
566 window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
567 state[j] += shift_down(k * stepval, LATTICE_SHIFT);
576 static inline int code_samplerate(int samplerate)
580 case 44100: return 0;
581 case 22050: return 1;
582 case 11025: return 2;
583 case 96000: return 3;
584 case 48000: return 4;
585 case 32000: return 5;
586 case 24000: return 6;
587 case 16000: return 7;
590 return AVERROR(EINVAL);
593 static av_cold int sonic_encode_init(AVCodecContext *avctx)
595 SonicContext *s = avctx->priv_data;
601 if (avctx->channels > MAX_CHANNELS)
603 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
604 return AVERROR(EINVAL); /* only stereo or mono for now */
607 if (avctx->channels == 2)
608 s->decorrelation = MID_SIDE;
610 s->decorrelation = 3;
612 if (avctx->codec->id == AV_CODEC_ID_SONIC_LS)
617 s->quantization = 0.0;
623 s->quantization = 1.0;
627 if (s->num_taps < 32 || s->num_taps > 1024 || s->num_taps % 32) {
628 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
629 return AVERROR_INVALIDDATA;
633 s->tap_quant = av_calloc(s->num_taps, sizeof(*s->tap_quant));
635 return AVERROR(ENOMEM);
637 for (i = 0; i < s->num_taps; i++)
638 s->tap_quant[i] = ff_sqrt(i+1);
640 s->channels = avctx->channels;
641 s->samplerate = avctx->sample_rate;
643 s->block_align = 2048LL*s->samplerate/(44100*s->downsampling);
644 s->frame_size = s->channels*s->block_align*s->downsampling;
646 s->tail_size = s->num_taps*s->channels;
647 s->tail = av_calloc(s->tail_size, sizeof(*s->tail));
649 return AVERROR(ENOMEM);
651 s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k) );
653 return AVERROR(ENOMEM);
655 for (i = 0; i < s->channels; i++)
657 s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples));
658 if (!s->coded_samples[i])
659 return AVERROR(ENOMEM);
662 s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
664 s->window_size = ((2*s->tail_size)+s->frame_size);
665 s->window = av_calloc(s->window_size, sizeof(*s->window));
666 if (!s->window || !s->int_samples)
667 return AVERROR(ENOMEM);
669 avctx->extradata = av_mallocz(16);
670 if (!avctx->extradata)
671 return AVERROR(ENOMEM);
672 init_put_bits(&pb, avctx->extradata, 16*8);
674 put_bits(&pb, 2, s->version); // version
677 if (s->version >= 2) {
678 put_bits(&pb, 8, s->version);
679 put_bits(&pb, 8, s->minor_version);
681 put_bits(&pb, 2, s->channels);
682 put_bits(&pb, 4, code_samplerate(s->samplerate));
684 put_bits(&pb, 1, s->lossless);
686 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
687 put_bits(&pb, 2, s->decorrelation);
688 put_bits(&pb, 2, s->downsampling);
689 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
690 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
693 avctx->extradata_size = put_bits_count(&pb)/8;
695 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
696 s->version, s->minor_version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
698 avctx->frame_size = s->block_align*s->downsampling;
703 static av_cold int sonic_encode_close(AVCodecContext *avctx)
705 SonicContext *s = avctx->priv_data;
708 for (i = 0; i < s->channels; i++)
709 av_freep(&s->coded_samples[i]);
711 av_freep(&s->predictor_k);
713 av_freep(&s->tap_quant);
714 av_freep(&s->window);
715 av_freep(&s->int_samples);
720 static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
721 const AVFrame *frame, int *got_packet_ptr)
723 SonicContext *s = avctx->priv_data;
725 int i, j, ch, quant = 0, x = 0;
727 const short *samples = (const int16_t*)frame->data[0];
730 if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000, 0)) < 0)
733 ff_init_range_encoder(&c, avpkt->data, avpkt->size);
734 ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);
735 memset(state, 128, sizeof(state));
738 for (i = 0; i < s->frame_size; i++)
739 s->int_samples[i] = samples[i];
742 for (i = 0; i < s->frame_size; i++)
743 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
745 switch(s->decorrelation)
748 for (i = 0; i < s->frame_size; i += s->channels)
750 s->int_samples[i] += s->int_samples[i+1];
751 s->int_samples[i+1] -= shift(s->int_samples[i], 1);
755 for (i = 0; i < s->frame_size; i += s->channels)
756 s->int_samples[i+1] -= s->int_samples[i];
759 for (i = 0; i < s->frame_size; i += s->channels)
760 s->int_samples[i] -= s->int_samples[i+1];
764 memset(s->window, 0, 4* s->window_size);
766 for (i = 0; i < s->tail_size; i++)
767 s->window[x++] = s->tail[i];
769 for (i = 0; i < s->frame_size; i++)
770 s->window[x++] = s->int_samples[i];
772 for (i = 0; i < s->tail_size; i++)
775 for (i = 0; i < s->tail_size; i++)
776 s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
779 ret = modified_levinson_durbin(s->window, s->window_size,
780 s->predictor_k, s->num_taps, s->channels, s->tap_quant);
784 if ((ret = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)
787 for (ch = 0; ch < s->channels; ch++)
790 for (i = 0; i < s->block_align; i++)
793 for (j = 0; j < s->downsampling; j++, x += s->channels)
795 s->coded_samples[ch][i] = sum;
799 // simple rate control code
802 double energy1 = 0.0, energy2 = 0.0;
803 for (ch = 0; ch < s->channels; ch++)
805 for (i = 0; i < s->block_align; i++)
807 double sample = s->coded_samples[ch][i];
808 energy2 += sample*sample;
809 energy1 += fabs(sample);
813 energy2 = sqrt(energy2/(s->channels*s->block_align));
814 energy1 = M_SQRT2*energy1/(s->channels*s->block_align);
816 // increase bitrate when samples are like a gaussian distribution
817 // reduce bitrate when samples are like a two-tailed exponential distribution
819 if (energy2 > energy1)
820 energy2 += (energy2-energy1)*RATE_VARIATION;
822 quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
823 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
825 quant = av_clip(quant, 1, 65534);
827 put_symbol(&c, state, quant, 0, NULL, NULL);
829 quant *= SAMPLE_FACTOR;
832 // write out coded samples
833 for (ch = 0; ch < s->channels; ch++)
836 for (i = 0; i < s->block_align; i++)
837 s->coded_samples[ch][i] = ROUNDED_DIV(s->coded_samples[ch][i], quant);
839 if ((ret = intlist_write(&c, state, s->coded_samples[ch], s->block_align, 1)) < 0)
843 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
845 avpkt->size = ff_rac_terminate(&c);
850 #endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
852 #if CONFIG_SONIC_DECODER
853 static const int samplerate_table[] =
854 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
856 static av_cold int sonic_decode_init(AVCodecContext *avctx)
858 SonicContext *s = avctx->priv_data;
863 s->channels = avctx->channels;
864 s->samplerate = avctx->sample_rate;
866 if (!avctx->extradata)
868 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
869 return AVERROR_INVALIDDATA;
872 ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
876 s->version = get_bits(&gb, 2);
877 if (s->version >= 2) {
878 s->version = get_bits(&gb, 8);
879 s->minor_version = get_bits(&gb, 8);
883 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
884 return AVERROR_INVALIDDATA;
889 int sample_rate_index;
890 s->channels = get_bits(&gb, 2);
891 sample_rate_index = get_bits(&gb, 4);
892 if (sample_rate_index >= FF_ARRAY_ELEMS(samplerate_table)) {
893 av_log(avctx, AV_LOG_ERROR, "Invalid sample_rate_index %d\n", sample_rate_index);
894 return AVERROR_INVALIDDATA;
896 s->samplerate = samplerate_table[sample_rate_index];
897 av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
898 s->channels, s->samplerate);
901 if (s->channels > MAX_CHANNELS || s->channels < 1)
903 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
904 return AVERROR_INVALIDDATA;
906 avctx->channels = s->channels;
908 s->lossless = get_bits1(&gb);
910 skip_bits(&gb, 3); // XXX FIXME
911 s->decorrelation = get_bits(&gb, 2);
912 if (s->decorrelation != 3 && s->channels != 2) {
913 av_log(avctx, AV_LOG_ERROR, "invalid decorrelation %d\n", s->decorrelation);
914 return AVERROR_INVALIDDATA;
917 s->downsampling = get_bits(&gb, 2);
918 if (!s->downsampling) {
919 av_log(avctx, AV_LOG_ERROR, "invalid downsampling value\n");
920 return AVERROR_INVALIDDATA;
923 s->num_taps = (get_bits(&gb, 5)+1)<<5;
924 if (get_bits1(&gb)) // XXX FIXME
925 av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
927 s->block_align = 2048LL*s->samplerate/(44100*s->downsampling);
928 s->frame_size = s->channels*s->block_align*s->downsampling;
929 // avctx->frame_size = s->block_align;
931 if (s->num_taps * s->channels > s->frame_size) {
932 av_log(avctx, AV_LOG_ERROR,
933 "number of taps times channels (%d * %d) larger than frame size %d\n",
934 s->num_taps, s->channels, s->frame_size);
935 return AVERROR_INVALIDDATA;
938 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
939 s->version, s->minor_version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
942 s->tap_quant = av_calloc(s->num_taps, sizeof(*s->tap_quant));
944 return AVERROR(ENOMEM);
946 for (i = 0; i < s->num_taps; i++)
947 s->tap_quant[i] = ff_sqrt(i+1);
949 s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k));
951 for (i = 0; i < s->channels; i++)
953 s->predictor_state[i] = av_calloc(s->num_taps, sizeof(**s->predictor_state));
954 if (!s->predictor_state[i])
955 return AVERROR(ENOMEM);
958 for (i = 0; i < s->channels; i++)
960 s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples));
961 if (!s->coded_samples[i])
962 return AVERROR(ENOMEM);
964 s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
966 return AVERROR(ENOMEM);
968 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
972 static av_cold int sonic_decode_close(AVCodecContext *avctx)
974 SonicContext *s = avctx->priv_data;
977 av_freep(&s->int_samples);
978 av_freep(&s->tap_quant);
979 av_freep(&s->predictor_k);
981 for (i = 0; i < s->channels; i++)
983 av_freep(&s->predictor_state[i]);
984 av_freep(&s->coded_samples[i]);
990 static int sonic_decode_frame(AVCodecContext *avctx,
991 void *data, int *got_frame_ptr,
994 const uint8_t *buf = avpkt->data;
995 int buf_size = avpkt->size;
996 SonicContext *s = avctx->priv_data;
999 int i, quant, ch, j, ret;
1001 AVFrame *frame = data;
1003 if (buf_size == 0) return 0;
1005 frame->nb_samples = s->frame_size / avctx->channels;
1006 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1008 samples = (int16_t *)frame->data[0];
1010 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
1012 memset(state, 128, sizeof(state));
1013 ff_init_range_decoder(&c, buf, buf_size);
1014 ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);
1016 intlist_read(&c, state, s->predictor_k, s->num_taps, 0);
1019 for (i = 0; i < s->num_taps; i++)
1020 s->predictor_k[i] *= s->tap_quant[i];
1025 quant = get_symbol(&c, state, 0) * SAMPLE_FACTOR;
1027 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
1029 for (ch = 0; ch < s->channels; ch++)
1033 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
1035 intlist_read(&c, state, s->coded_samples[ch], s->block_align, 1);
1037 for (i = 0; i < s->block_align; i++)
1039 for (j = 0; j < s->downsampling - 1; j++)
1041 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
1045 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
1049 for (i = 0; i < s->num_taps; i++)
1050 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
1053 switch(s->decorrelation)
1056 for (i = 0; i < s->frame_size; i += s->channels)
1058 s->int_samples[i+1] += shift(s->int_samples[i], 1);
1059 s->int_samples[i] -= s->int_samples[i+1];
1063 for (i = 0; i < s->frame_size; i += s->channels)
1064 s->int_samples[i+1] += s->int_samples[i];
1067 for (i = 0; i < s->frame_size; i += s->channels)
1068 s->int_samples[i] += s->int_samples[i+1];
1073 for (i = 0; i < s->frame_size; i++)
1074 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
1076 // internal -> short
1077 for (i = 0; i < s->frame_size; i++)
1078 samples[i] = av_clip_int16(s->int_samples[i]);
1085 AVCodec ff_sonic_decoder = {
1087 .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
1088 .type = AVMEDIA_TYPE_AUDIO,
1089 .id = AV_CODEC_ID_SONIC,
1090 .priv_data_size = sizeof(SonicContext),
1091 .init = sonic_decode_init,
1092 .close = sonic_decode_close,
1093 .decode = sonic_decode_frame,
1094 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_EXPERIMENTAL,
1096 #endif /* CONFIG_SONIC_DECODER */
1098 #if CONFIG_SONIC_ENCODER
1099 AVCodec ff_sonic_encoder = {
1101 .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
1102 .type = AVMEDIA_TYPE_AUDIO,
1103 .id = AV_CODEC_ID_SONIC,
1104 .priv_data_size = sizeof(SonicContext),
1105 .init = sonic_encode_init,
1106 .encode2 = sonic_encode_frame,
1107 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
1108 .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1109 .close = sonic_encode_close,
1113 #if CONFIG_SONIC_LS_ENCODER
1114 AVCodec ff_sonic_ls_encoder = {
1116 .long_name = NULL_IF_CONFIG_SMALL("Sonic lossless"),
1117 .type = AVMEDIA_TYPE_AUDIO,
1118 .id = AV_CODEC_ID_SONIC_LS,
1119 .priv_data_size = sizeof(SonicContext),
1120 .init = sonic_encode_init,
1121 .encode2 = sonic_encode_frame,
1122 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
1123 .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1124 .close = sonic_encode_close,