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
22 #include "bitstream.h"
26 * @file libavcodec/sonic.c
27 * Simple free lossless/lossy audio codec
28 * Based on Paul Francis Harrison's Bonk (http://www.logarithmic.net/pfh/bonk)
29 * Written and designed by Alex Beregszaszi
32 * - CABAC put/get_symbol
33 * - independent quantizer for channels
34 * - >2 channels support
35 * - more decorrelation types
36 * - more tap_quant tests
37 * - selectable intlist writers/readers (bonk-style, golomb, cabac)
40 #define MAX_CHANNELS 2
46 typedef struct SonicContext {
47 int lossless, decorrelation;
49 int num_taps, downsampling;
52 int channels, samplerate, block_align, frame_size;
56 int *coded_samples[MAX_CHANNELS];
66 int *predictor_state[MAX_CHANNELS];
69 #define LATTICE_SHIFT 10
70 #define SAMPLE_SHIFT 4
71 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
72 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
74 #define BASE_QUANT 0.6
75 #define RATE_VARIATION 3.0
77 static inline int divide(int a, int b)
80 return -( (-a + b/2)/b );
85 static inline int shift(int a,int b)
87 return (a+(1<<(b-1))) >> b;
90 static inline int shift_down(int a,int b)
92 return (a>>b)+((a<0)?1:0);
96 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
100 for (i = 0; i < entries; i++)
101 set_se_golomb(pb, buf[i]);
106 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
110 for (i = 0; i < entries; i++)
111 buf[i] = get_se_golomb(gb);
118 #define ADAPT_LEVEL 8
120 static int bits_to_store(uint64_t x)
132 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
139 bits = bits_to_store(max);
141 for (i = 0; i < bits-1; i++)
142 put_bits(pb, 1, value & (1 << i));
144 if ( (value | (1 << (bits-1))) <= max)
145 put_bits(pb, 1, value & (1 << (bits-1)));
148 static unsigned int read_uint_max(GetBitContext *gb, int max)
150 int i, bits, value = 0;
155 bits = bits_to_store(max);
157 for (i = 0; i < bits-1; i++)
161 if ( (value | (1<<(bits-1))) <= max)
163 value += 1 << (bits-1);
168 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
170 int i, j, x = 0, low_bits = 0, max = 0;
171 int step = 256, pos = 0, dominant = 0, any = 0;
174 copy = av_mallocz(4* entries);
182 for (i = 0; i < entries; i++)
183 energy += abs(buf[i]);
185 low_bits = bits_to_store(energy / (entries * 2));
189 put_bits(pb, 4, low_bits);
192 for (i = 0; i < entries; i++)
194 put_bits(pb, low_bits, abs(buf[i]));
195 copy[i] = abs(buf[i]) >> low_bits;
200 bits = av_mallocz(4* entries*max);
207 for (i = 0; i <= max; i++)
209 for (j = 0; j < entries; j++)
211 bits[x++] = copy[j] > i;
217 int steplet = step >> 8;
219 if (pos + steplet > x)
222 for (i = 0; i < steplet; i++)
223 if (bits[i+pos] != dominant)
226 put_bits(pb, 1, any);
231 step += step / ADAPT_LEVEL;
237 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
241 write_uint_max(pb, interloper, (step >> 8) - 1);
243 pos += interloper + 1;
244 step -= step / ADAPT_LEVEL;
250 dominant = !dominant;
255 for (i = 0; i < entries; i++)
257 put_bits(pb, 1, buf[i] < 0);
265 static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
267 int i, low_bits = 0, x = 0;
268 int n_zeros = 0, step = 256, dominant = 0;
269 int pos = 0, level = 0;
270 int *bits = av_mallocz(4* entries);
277 low_bits = get_bits(gb, 4);
280 for (i = 0; i < entries; i++)
281 buf[i] = get_bits(gb, low_bits);
284 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
286 while (n_zeros < entries)
288 int steplet = step >> 8;
292 for (i = 0; i < steplet; i++)
293 bits[x++] = dominant;
298 step += step / ADAPT_LEVEL;
302 int actual_run = read_uint_max(gb, steplet-1);
304 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
306 for (i = 0; i < actual_run; i++)
307 bits[x++] = dominant;
309 bits[x++] = !dominant;
312 n_zeros += actual_run;
316 step -= step / ADAPT_LEVEL;
322 dominant = !dominant;
326 // reconstruct unsigned values
328 for (i = 0; n_zeros < entries; i++)
335 level += 1 << low_bits;
338 if (buf[pos] >= level)
345 buf[pos] += 1 << low_bits;
354 for (i = 0; i < entries; i++)
355 if (buf[i] && get_bits1(gb))
358 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
364 static void predictor_init_state(int *k, int *state, int order)
368 for (i = order-2; i >= 0; i--)
370 int j, p, x = state[i];
372 for (j = 0, p = i+1; p < order; j++,p++)
374 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
375 state[p] += shift_down(k[j]*x, LATTICE_SHIFT);
381 static int predictor_calc_error(int *k, int *state, int order, int error)
383 int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT);
386 int *k_ptr = &(k[order-2]),
387 *state_ptr = &(state[order-2]);
388 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
390 int k_value = *k_ptr, state_value = *state_ptr;
391 x -= shift_down(k_value * state_value, LATTICE_SHIFT);
392 state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
395 for (i = order-2; i >= 0; i--)
397 x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
398 state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
402 // don't drift too far, to avoid overflows
403 if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
404 if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16);
411 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
412 // Heavily modified Levinson-Durbin algorithm which
413 // copes better with quantization, and calculates the
414 // actual whitened result as it goes.
416 static void modified_levinson_durbin(int *window, int window_entries,
417 int *out, int out_entries, int channels, int *tap_quant)
420 int *state = av_mallocz(4* window_entries);
422 memcpy(state, window, 4* window_entries);
424 for (i = 0; i < out_entries; i++)
426 int step = (i+1)*channels, k, j;
427 double xx = 0.0, xy = 0.0;
429 int *x_ptr = &(window[step]), *state_ptr = &(state[0]);
430 j = window_entries - step;
431 for (;j>=0;j--,x_ptr++,state_ptr++)
433 double x_value = *x_ptr, state_value = *state_ptr;
434 xx += state_value*state_value;
435 xy += x_value*state_value;
438 for (j = 0; j <= (window_entries - step); j++);
440 double stepval = window[step+j], stateval = window[j];
441 // xx += (double)window[j]*(double)window[j];
442 // xy += (double)window[step+j]*(double)window[j];
443 xx += stateval*stateval;
444 xy += stepval*stateval;
450 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
452 if (k > (LATTICE_FACTOR/tap_quant[i]))
453 k = LATTICE_FACTOR/tap_quant[i];
454 if (-k > (LATTICE_FACTOR/tap_quant[i]))
455 k = -(LATTICE_FACTOR/tap_quant[i]);
461 x_ptr = &(window[step]);
462 state_ptr = &(state[0]);
463 j = window_entries - step;
464 for (;j>=0;j--,x_ptr++,state_ptr++)
466 int x_value = *x_ptr, state_value = *state_ptr;
467 *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
468 *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
471 for (j=0; j <= (window_entries - step); j++)
473 int stepval = window[step+j], stateval=state[j];
474 window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
475 state[j] += shift_down(k * stepval, LATTICE_SHIFT);
482 #endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
484 static const int samplerate_table[] =
485 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
487 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
488 static inline int code_samplerate(int samplerate)
492 case 44100: return 0;
493 case 22050: return 1;
494 case 11025: return 2;
495 case 96000: return 3;
496 case 48000: return 4;
497 case 32000: return 5;
498 case 24000: return 6;
499 case 16000: return 7;
505 static av_cold int sonic_encode_init(AVCodecContext *avctx)
507 SonicContext *s = avctx->priv_data;
511 if (avctx->channels > MAX_CHANNELS)
513 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
514 return -1; /* only stereo or mono for now */
517 if (avctx->channels == 2)
518 s->decorrelation = MID_SIDE;
520 if (avctx->codec->id == CODEC_ID_SONIC_LS)
525 s->quantization = 0.0;
531 s->quantization = 1.0;
535 if ((s->num_taps < 32) || (s->num_taps > 1024) ||
536 ((s->num_taps>>5)<<5 != s->num_taps))
538 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
543 s->tap_quant = av_mallocz(4* s->num_taps);
544 for (i = 0; i < s->num_taps; i++)
545 s->tap_quant[i] = (int)(sqrt(i+1));
547 s->channels = avctx->channels;
548 s->samplerate = avctx->sample_rate;
550 s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling;
551 s->frame_size = s->channels*s->block_align*s->downsampling;
553 s->tail = av_mallocz(4* s->num_taps*s->channels);
556 s->tail_size = s->num_taps*s->channels;
558 s->predictor_k = av_mallocz(4 * s->num_taps);
562 for (i = 0; i < s->channels; i++)
564 s->coded_samples[i] = av_mallocz(4* s->block_align);
565 if (!s->coded_samples[i])
569 s->int_samples = av_mallocz(4* s->frame_size);
571 s->window_size = ((2*s->tail_size)+s->frame_size);
572 s->window = av_mallocz(4* s->window_size);
576 avctx->extradata = av_mallocz(16);
577 if (!avctx->extradata)
579 init_put_bits(&pb, avctx->extradata, 16*8);
581 put_bits(&pb, 2, version); // version
584 put_bits(&pb, 2, s->channels);
585 put_bits(&pb, 4, code_samplerate(s->samplerate));
587 put_bits(&pb, 1, s->lossless);
589 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
590 put_bits(&pb, 2, s->decorrelation);
591 put_bits(&pb, 2, s->downsampling);
592 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
593 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
596 avctx->extradata_size = put_bits_count(&pb)/8;
598 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
599 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
601 avctx->coded_frame = avcodec_alloc_frame();
602 if (!avctx->coded_frame)
603 return AVERROR(ENOMEM);
604 avctx->coded_frame->key_frame = 1;
605 avctx->frame_size = s->block_align*s->downsampling;
610 static av_cold int sonic_encode_close(AVCodecContext *avctx)
612 SonicContext *s = avctx->priv_data;
615 av_freep(&avctx->coded_frame);
617 for (i = 0; i < s->channels; i++)
618 av_free(s->coded_samples[i]);
620 av_free(s->predictor_k);
622 av_free(s->tap_quant);
624 av_free(s->int_samples);
629 static int sonic_encode_frame(AVCodecContext *avctx,
630 uint8_t *buf, int buf_size, void *data)
632 SonicContext *s = avctx->priv_data;
634 int i, j, ch, quant = 0, x = 0;
635 short *samples = data;
637 init_put_bits(&pb, buf, buf_size*8);
640 for (i = 0; i < s->frame_size; i++)
641 s->int_samples[i] = samples[i];
644 for (i = 0; i < s->frame_size; i++)
645 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
647 switch(s->decorrelation)
650 for (i = 0; i < s->frame_size; i += s->channels)
652 s->int_samples[i] += s->int_samples[i+1];
653 s->int_samples[i+1] -= shift(s->int_samples[i], 1);
657 for (i = 0; i < s->frame_size; i += s->channels)
658 s->int_samples[i+1] -= s->int_samples[i];
661 for (i = 0; i < s->frame_size; i += s->channels)
662 s->int_samples[i] -= s->int_samples[i+1];
666 memset(s->window, 0, 4* s->window_size);
668 for (i = 0; i < s->tail_size; i++)
669 s->window[x++] = s->tail[i];
671 for (i = 0; i < s->frame_size; i++)
672 s->window[x++] = s->int_samples[i];
674 for (i = 0; i < s->tail_size; i++)
677 for (i = 0; i < s->tail_size; i++)
678 s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
681 modified_levinson_durbin(s->window, s->window_size,
682 s->predictor_k, s->num_taps, s->channels, s->tap_quant);
683 if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0)
686 for (ch = 0; ch < s->channels; ch++)
689 for (i = 0; i < s->block_align; i++)
692 for (j = 0; j < s->downsampling; j++, x += s->channels)
694 s->coded_samples[ch][i] = sum;
698 // simple rate control code
701 double energy1 = 0.0, energy2 = 0.0;
702 for (ch = 0; ch < s->channels; ch++)
704 for (i = 0; i < s->block_align; i++)
706 double sample = s->coded_samples[ch][i];
707 energy2 += sample*sample;
708 energy1 += fabs(sample);
712 energy2 = sqrt(energy2/(s->channels*s->block_align));
713 energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align);
715 // increase bitrate when samples are like a gaussian distribution
716 // reduce bitrate when samples are like a two-tailed exponential distribution
718 if (energy2 > energy1)
719 energy2 += (energy2-energy1)*RATE_VARIATION;
721 quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
722 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
729 set_ue_golomb(&pb, quant);
731 quant *= SAMPLE_FACTOR;
734 // write out coded samples
735 for (ch = 0; ch < s->channels; ch++)
738 for (i = 0; i < s->block_align; i++)
739 s->coded_samples[ch][i] = divide(s->coded_samples[ch][i], quant);
741 if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0)
745 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
748 return (put_bits_count(&pb)+7)/8;
750 #endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
752 #if CONFIG_SONIC_DECODER
753 static av_cold int sonic_decode_init(AVCodecContext *avctx)
755 SonicContext *s = avctx->priv_data;
759 s->channels = avctx->channels;
760 s->samplerate = avctx->sample_rate;
762 if (!avctx->extradata)
764 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
768 init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
770 version = get_bits(&gb, 2);
773 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
779 s->channels = get_bits(&gb, 2);
780 s->samplerate = samplerate_table[get_bits(&gb, 4)];
781 av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
782 s->channels, s->samplerate);
785 if (s->channels > MAX_CHANNELS)
787 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
791 s->lossless = get_bits1(&gb);
793 skip_bits(&gb, 3); // XXX FIXME
794 s->decorrelation = get_bits(&gb, 2);
796 s->downsampling = get_bits(&gb, 2);
797 s->num_taps = (get_bits(&gb, 5)+1)<<5;
798 if (get_bits1(&gb)) // XXX FIXME
799 av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
801 s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling;
802 s->frame_size = s->channels*s->block_align*s->downsampling;
803 // avctx->frame_size = s->block_align;
805 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
806 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
809 s->tap_quant = av_mallocz(4* s->num_taps);
810 for (i = 0; i < s->num_taps; i++)
811 s->tap_quant[i] = (int)(sqrt(i+1));
813 s->predictor_k = av_mallocz(4* s->num_taps);
815 for (i = 0; i < s->channels; i++)
817 s->predictor_state[i] = av_mallocz(4* s->num_taps);
818 if (!s->predictor_state[i])
822 for (i = 0; i < s->channels; i++)
824 s->coded_samples[i] = av_mallocz(4* s->block_align);
825 if (!s->coded_samples[i])
828 s->int_samples = av_mallocz(4* s->frame_size);
830 avctx->sample_fmt = SAMPLE_FMT_S16;
834 static av_cold int sonic_decode_close(AVCodecContext *avctx)
836 SonicContext *s = avctx->priv_data;
839 av_free(s->int_samples);
840 av_free(s->tap_quant);
841 av_free(s->predictor_k);
843 for (i = 0; i < s->channels; i++)
845 av_free(s->predictor_state[i]);
846 av_free(s->coded_samples[i]);
852 static int sonic_decode_frame(AVCodecContext *avctx,
853 void *data, int *data_size,
856 const uint8_t *buf = avpkt->data;
857 int buf_size = avpkt->size;
858 SonicContext *s = avctx->priv_data;
861 short *samples = data;
863 if (buf_size == 0) return 0;
865 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
867 init_get_bits(&gb, buf, buf_size*8);
869 intlist_read(&gb, s->predictor_k, s->num_taps, 0);
872 for (i = 0; i < s->num_taps; i++)
873 s->predictor_k[i] *= s->tap_quant[i];
878 quant = get_ue_golomb(&gb) * SAMPLE_FACTOR;
880 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
882 for (ch = 0; ch < s->channels; ch++)
886 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
888 intlist_read(&gb, s->coded_samples[ch], s->block_align, 1);
890 for (i = 0; i < s->block_align; i++)
892 for (j = 0; j < s->downsampling - 1; j++)
894 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
898 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
902 for (i = 0; i < s->num_taps; i++)
903 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
906 switch(s->decorrelation)
909 for (i = 0; i < s->frame_size; i += s->channels)
911 s->int_samples[i+1] += shift(s->int_samples[i], 1);
912 s->int_samples[i] -= s->int_samples[i+1];
916 for (i = 0; i < s->frame_size; i += s->channels)
917 s->int_samples[i+1] += s->int_samples[i];
920 for (i = 0; i < s->frame_size; i += s->channels)
921 s->int_samples[i] += s->int_samples[i+1];
926 for (i = 0; i < s->frame_size; i++)
927 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
930 for (i = 0; i < s->frame_size; i++)
931 samples[i] = av_clip_int16(s->int_samples[i]);
935 *data_size = s->frame_size * 2;
937 return (get_bits_count(&gb)+7)/8;
939 #endif /* CONFIG_SONIC_DECODER */
941 #if CONFIG_SONIC_ENCODER
942 AVCodec sonic_encoder = {
946 sizeof(SonicContext),
951 .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
955 #if CONFIG_SONIC_LS_ENCODER
956 AVCodec sonic_ls_encoder = {
960 sizeof(SonicContext),
965 .long_name = NULL_IF_CONFIG_SMALL("Sonic lossless"),
969 #if CONFIG_SONIC_DECODER
970 AVCodec sonic_decoder = {
974 sizeof(SonicContext),
979 .long_name = NULL_IF_CONFIG_SMALL("Sonic"),