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 "config_components.h"
25 #include "codec_internal.h"
30 #include "put_golomb.h"
31 #include "rangecoder.h"
36 * Simple free lossless/lossy audio codec
37 * Based on Paul Francis Harrison's Bonk (http://www.logarithmic.net/pfh/bonk)
38 * Written and designed by Alex Beregszaszi
41 * - CABAC put/get_symbol
42 * - independent quantizer for channels
43 * - >2 channels support
44 * - more decorrelation types
45 * - more tap_quant tests
46 * - selectable intlist writers/readers (bonk-style, golomb, cabac)
49 #define MAX_CHANNELS 2
55 typedef struct SonicContext
{
58 int lossless
, decorrelation
;
60 int num_taps
, downsampling
;
63 int channels
, samplerate
, block_align
, frame_size
;
67 int *coded_samples
[MAX_CHANNELS
];
77 int *predictor_state
[MAX_CHANNELS
];
80 #define LATTICE_SHIFT 10
81 #define SAMPLE_SHIFT 4
82 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
83 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
85 #define BASE_QUANT 0.6
86 #define RATE_VARIATION 3.0
88 static inline int shift(int a
,int b
)
90 return (a
+(1<<(b
-1))) >> b
;
93 static inline int shift_down(int a
,int b
)
98 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]){
101 #define put_rac(C,S,B) \
105 rc_stat2[(S)-state][B]++;\
111 const int a
= FFABS(v
);
112 const int e
= av_log2(a
);
113 put_rac(c
, state
+0, 0);
116 put_rac(c
, state
+1+i
, 1); //1..10
118 put_rac(c
, state
+1+i
, 0);
120 for(i
=e
-1; i
>=0; i
--){
121 put_rac(c
, state
+22+i
, (a
>>i
)&1); //22..31
125 put_rac(c
, state
+11 + e
, v
< 0); //11..21
128 put_rac(c
, state
+1+FFMIN(i
,9), 1); //1..10
130 put_rac(c
, state
+1+9, 0);
132 for(i
=e
-1; i
>=0; i
--){
133 put_rac(c
, state
+22+FFMIN(i
,9), (a
>>i
)&1); //22..31
137 put_rac(c
, state
+11 + 10, v
< 0); //11..21
140 put_rac(c
, state
+0, 1);
145 static inline av_flatten
int get_symbol(RangeCoder
*c
, uint8_t *state
, int is_signed
){
146 if(get_rac(c
, state
+0))
152 while(get_rac(c
, state
+1 + FFMIN(e
,9))){ //1..10
155 return AVERROR_INVALIDDATA
;
159 for(i
=e
-1; i
>=0; i
--){
160 a
+= a
+ get_rac(c
, state
+22 + FFMIN(i
,9)); //22..31
163 e
= -(is_signed
&& get_rac(c
, state
+11 + FFMIN(e
, 10))); //11..21
169 static inline int intlist_write(RangeCoder
*c
, uint8_t *state
, int *buf
, int entries
, int base_2_part
)
173 for (i
= 0; i
< entries
; i
++)
174 put_symbol(c
, state
, buf
[i
], 1, NULL
, NULL
);
179 static inline int intlist_read(RangeCoder
*c
, uint8_t *state
, int *buf
, int entries
, int base_2_part
)
183 for (i
= 0; i
< entries
; i
++)
184 buf
[i
] = get_symbol(c
, state
, 1);
189 static inline int intlist_write(PutBitContext
*pb
, int *buf
, int entries
, int base_2_part
)
193 for (i
= 0; i
< entries
; i
++)
194 set_se_golomb(pb
, buf
[i
]);
199 static inline int intlist_read(GetBitContext
*gb
, int *buf
, int entries
, int base_2_part
)
203 for (i
= 0; i
< entries
; i
++)
204 buf
[i
] = get_se_golomb(gb
);
211 #define ADAPT_LEVEL 8
213 static int bits_to_store(uint64_t x
)
225 static void write_uint_max(PutBitContext
*pb
, unsigned int value
, unsigned int max
)
232 bits
= bits_to_store(max
);
234 for (i
= 0; i
< bits
-1; i
++)
235 put_bits(pb
, 1, value
& (1 << i
));
237 if ( (value
| (1 << (bits
-1))) <= max
)
238 put_bits(pb
, 1, value
& (1 << (bits
-1)));
241 static unsigned int read_uint_max(GetBitContext
*gb
, int max
)
243 int i
, bits
, value
= 0;
248 bits
= bits_to_store(max
);
250 for (i
= 0; i
< bits
-1; i
++)
254 if ( (value
| (1<<(bits
-1))) <= max
)
256 value
+= 1 << (bits
-1);
261 static int intlist_write(PutBitContext
*pb
, int *buf
, int entries
, int base_2_part
)
263 int i
, j
, x
= 0, low_bits
= 0, max
= 0;
264 int step
= 256, pos
= 0, dominant
= 0, any
= 0;
267 copy
= av_calloc(entries
, sizeof(*copy
));
269 return AVERROR(ENOMEM
);
275 for (i
= 0; i
< entries
; i
++)
276 energy
+= abs(buf
[i
]);
278 low_bits
= bits_to_store(energy
/ (entries
* 2));
282 put_bits(pb
, 4, low_bits
);
285 for (i
= 0; i
< entries
; i
++)
287 put_bits(pb
, low_bits
, abs(buf
[i
]));
288 copy
[i
] = abs(buf
[i
]) >> low_bits
;
293 bits
= av_calloc(entries
*max
, sizeof(*bits
));
297 return AVERROR(ENOMEM
);
300 for (i
= 0; i
<= max
; i
++)
302 for (j
= 0; j
< entries
; j
++)
304 bits
[x
++] = copy
[j
] > i
;
310 int steplet
= step
>> 8;
312 if (pos
+ steplet
> x
)
315 for (i
= 0; i
< steplet
; i
++)
316 if (bits
[i
+pos
] != dominant
)
319 put_bits(pb
, 1, any
);
324 step
+= step
/ ADAPT_LEVEL
;
330 while (((pos
+ interloper
) < x
) && (bits
[pos
+ interloper
] == dominant
))
334 write_uint_max(pb
, interloper
, (step
>> 8) - 1);
336 pos
+= interloper
+ 1;
337 step
-= step
/ ADAPT_LEVEL
;
343 dominant
= !dominant
;
348 for (i
= 0; i
< entries
; i
++)
350 put_bits(pb
, 1, buf
[i
] < 0);
358 static int intlist_read(GetBitContext
*gb
, int *buf
, int entries
, int base_2_part
)
360 int i
, low_bits
= 0, x
= 0;
361 int n_zeros
= 0, step
= 256, dominant
= 0;
362 int pos
= 0, level
= 0;
363 int *bits
= av_calloc(entries
, sizeof(*bits
));
366 return AVERROR(ENOMEM
);
370 low_bits
= get_bits(gb
, 4);
373 for (i
= 0; i
< entries
; i
++)
374 buf
[i
] = get_bits(gb
, low_bits
);
377 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
379 while (n_zeros
< entries
)
381 int steplet
= step
>> 8;
385 for (i
= 0; i
< steplet
; i
++)
386 bits
[x
++] = dominant
;
391 step
+= step
/ ADAPT_LEVEL
;
395 int actual_run
= read_uint_max(gb
, steplet
-1);
397 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
399 for (i
= 0; i
< actual_run
; i
++)
400 bits
[x
++] = dominant
;
402 bits
[x
++] = !dominant
;
405 n_zeros
+= actual_run
;
409 step
-= step
/ ADAPT_LEVEL
;
415 dominant
= !dominant
;
419 // reconstruct unsigned values
421 for (i
= 0; n_zeros
< entries
; i
++)
428 level
+= 1 << low_bits
;
431 if (buf
[pos
] >= level
)
438 buf
[pos
] += 1 << low_bits
;
447 for (i
= 0; i
< entries
; i
++)
448 if (buf
[i
] && get_bits1(gb
))
451 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
457 static void predictor_init_state(int *k
, int *state
, int order
)
461 for (i
= order
-2; i
>= 0; i
--)
463 int j
, p
, x
= state
[i
];
465 for (j
= 0, p
= i
+1; p
< order
; j
++,p
++)
467 int tmp
= x
+ shift_down(k
[j
] * (unsigned)state
[p
], LATTICE_SHIFT
);
468 state
[p
] += shift_down(k
[j
]* (unsigned)x
, LATTICE_SHIFT
);
474 static int predictor_calc_error(int *k
, int *state
, int order
, int error
)
476 int i
, x
= error
- (unsigned)shift_down(k
[order
-1] * (unsigned)state
[order
-1], LATTICE_SHIFT
);
479 int *k_ptr
= &(k
[order
-2]),
480 *state_ptr
= &(state
[order
-2]);
481 for (i
= order
-2; i
>= 0; i
--, k_ptr
--, state_ptr
--)
483 int k_value
= *k_ptr
, state_value
= *state_ptr
;
484 x
-= (unsigned)shift_down(k_value
* (unsigned)state_value
, LATTICE_SHIFT
);
485 state_ptr
[1] = state_value
+ shift_down(k_value
* (unsigned)x
, LATTICE_SHIFT
);
488 for (i
= order
-2; i
>= 0; i
--)
490 x
-= (unsigned)shift_down(k
[i
] * state
[i
], LATTICE_SHIFT
);
491 state
[i
+1] = state
[i
] + shift_down(k
[i
] * x
, LATTICE_SHIFT
);
495 // don't drift too far, to avoid overflows
496 if (x
> (SAMPLE_FACTOR
<<16)) x
= (SAMPLE_FACTOR
<<16);
497 if (x
< -(SAMPLE_FACTOR
<<16)) x
= -(SAMPLE_FACTOR
<<16);
504 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
505 // Heavily modified Levinson-Durbin algorithm which
506 // copes better with quantization, and calculates the
507 // actual whitened result as it goes.
509 static void modified_levinson_durbin(int *window
, int window_entries
,
510 int *out
, int out_entries
, int channels
, int *tap_quant
)
513 int *state
= window
+ window_entries
;
515 memcpy(state
, window
, window_entries
* sizeof(*state
));
517 for (i
= 0; i
< out_entries
; i
++)
519 int step
= (i
+1)*channels
, k
, j
;
520 double xx
= 0.0, xy
= 0.0;
522 int *x_ptr
= &(window
[step
]);
523 int *state_ptr
= &(state
[0]);
524 j
= window_entries
- step
;
525 for (;j
>0;j
--,x_ptr
++,state_ptr
++)
527 double x_value
= *x_ptr
;
528 double state_value
= *state_ptr
;
529 xx
+= state_value
*state_value
;
530 xy
+= x_value
*state_value
;
533 for (j
= 0; j
<= (window_entries
- step
); j
++);
535 double stepval
= window
[step
+j
];
536 double stateval
= window
[j
];
537 // xx += (double)window[j]*(double)window[j];
538 // xy += (double)window[step+j]*(double)window[j];
539 xx
+= stateval
*stateval
;
540 xy
+= stepval
*stateval
;
546 k
= (int)(floor(-xy
/xx
* (double)LATTICE_FACTOR
/ (double)(tap_quant
[i
]) + 0.5));
548 if (k
> (LATTICE_FACTOR
/tap_quant
[i
]))
549 k
= LATTICE_FACTOR
/tap_quant
[i
];
550 if (-k
> (LATTICE_FACTOR
/tap_quant
[i
]))
551 k
= -(LATTICE_FACTOR
/tap_quant
[i
]);
557 x_ptr
= &(window
[step
]);
558 state_ptr
= &(state
[0]);
559 j
= window_entries
- step
;
560 for (;j
>0;j
--,x_ptr
++,state_ptr
++)
562 int x_value
= *x_ptr
;
563 int state_value
= *state_ptr
;
564 *x_ptr
= x_value
+ shift_down(k
*state_value
,LATTICE_SHIFT
);
565 *state_ptr
= state_value
+ shift_down(k
*x_value
, LATTICE_SHIFT
);
568 for (j
=0; j
<= (window_entries
- step
); j
++)
570 int stepval
= window
[step
+j
];
571 int stateval
=state
[j
];
572 window
[step
+j
] += shift_down(k
* stateval
, LATTICE_SHIFT
);
573 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
;
605 if (avctx
->ch_layout
.nb_channels
> MAX_CHANNELS
)
607 av_log(avctx
, AV_LOG_ERROR
, "Only mono and stereo streams are supported by now\n");
608 return AVERROR(EINVAL
); /* only stereo or mono for now */
611 if (avctx
->ch_layout
.nb_channels
== 2)
612 s
->decorrelation
= MID_SIDE
;
614 s
->decorrelation
= 3;
616 if (avctx
->codec
->id
== AV_CODEC_ID_SONIC_LS
)
621 s
->quantization
= 0.0;
627 s
->quantization
= 1.0;
631 if (s
->num_taps
< 32 || s
->num_taps
> 1024 || s
->num_taps
% 32) {
632 av_log(avctx
, AV_LOG_ERROR
, "Invalid number of taps\n");
633 return AVERROR_INVALIDDATA
;
637 s
->tap_quant
= av_calloc(s
->num_taps
, sizeof(*s
->tap_quant
));
639 return AVERROR(ENOMEM
);
641 for (i
= 0; i
< s
->num_taps
; i
++)
642 s
->tap_quant
[i
] = ff_sqrt(i
+1);
644 s
->channels
= avctx
->ch_layout
.nb_channels
;
645 s
->samplerate
= avctx
->sample_rate
;
647 s
->block_align
= 2048LL*s
->samplerate
/(44100*s
->downsampling
);
648 s
->frame_size
= s
->channels
*s
->block_align
*s
->downsampling
;
650 s
->tail_size
= s
->num_taps
*s
->channels
;
651 s
->tail
= av_calloc(s
->tail_size
, sizeof(*s
->tail
));
653 return AVERROR(ENOMEM
);
655 s
->predictor_k
= av_calloc(s
->num_taps
, sizeof(*s
->predictor_k
) );
657 return AVERROR(ENOMEM
);
659 coded_samples
= av_calloc(s
->block_align
, s
->channels
* sizeof(**s
->coded_samples
));
661 return AVERROR(ENOMEM
);
662 for (i
= 0; i
< s
->channels
; i
++, coded_samples
+= s
->block_align
)
663 s
->coded_samples
[i
] = coded_samples
;
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
, 2 * 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_bytes_output(&pb
);
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
;
710 av_freep(&s
->coded_samples
[0]);
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_packet(avctx
, avpkt
, s
->frame_size
* 5 + 1000)) < 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, s
->window_size
* sizeof(*s
->window
));
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 modified_levinson_durbin(s
->window
, s
->window_size
,
780 s
->predictor_k
, s
->num_taps
, s
->channels
, s
->tap_quant
);
782 if ((ret
= intlist_write(&c
, state
, s
->predictor_k
, s
->num_taps
, 0)) < 0)
785 for (ch
= 0; ch
< s
->channels
; ch
++)
788 for (i
= 0; i
< s
->block_align
; i
++)
791 for (j
= 0; j
< s
->downsampling
; j
++, x
+= s
->channels
)
793 s
->coded_samples
[ch
][i
] = sum
;
797 // simple rate control code
800 double energy1
= 0.0, energy2
= 0.0;
801 for (ch
= 0; ch
< s
->channels
; ch
++)
803 for (i
= 0; i
< s
->block_align
; i
++)
805 double sample
= s
->coded_samples
[ch
][i
];
806 energy2
+= sample
*sample
;
807 energy1
+= fabs(sample
);
811 energy2
= sqrt(energy2
/(s
->channels
*s
->block_align
));
812 energy1
= M_SQRT2
*energy1
/(s
->channels
*s
->block_align
);
814 // increase bitrate when samples are like a gaussian distribution
815 // reduce bitrate when samples are like a two-tailed exponential distribution
817 if (energy2
> energy1
)
818 energy2
+= (energy2
-energy1
)*RATE_VARIATION
;
820 quant
= (int)(BASE_QUANT
*s
->quantization
*energy2
/SAMPLE_FACTOR
);
821 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
823 quant
= av_clip(quant
, 1, 65534);
825 put_symbol(&c
, state
, quant
, 0, NULL
, NULL
);
827 quant
*= SAMPLE_FACTOR
;
830 // write out coded samples
831 for (ch
= 0; ch
< s
->channels
; ch
++)
834 for (i
= 0; i
< s
->block_align
; i
++)
835 s
->coded_samples
[ch
][i
] = ROUNDED_DIV(s
->coded_samples
[ch
][i
], quant
);
837 if ((ret
= intlist_write(&c
, state
, s
->coded_samples
[ch
], s
->block_align
, 1)) < 0)
841 avpkt
->size
= ff_rac_terminate(&c
, 0);
846 #endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
848 #if CONFIG_SONIC_DECODER
849 static const int samplerate_table
[] =
850 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
852 static av_cold
int sonic_decode_init(AVCodecContext
*avctx
)
854 SonicContext
*s
= avctx
->priv_data
;
860 s
->channels
= avctx
->ch_layout
.nb_channels
;
861 s
->samplerate
= avctx
->sample_rate
;
863 if (!avctx
->extradata
)
865 av_log(avctx
, AV_LOG_ERROR
, "No mandatory headers present\n");
866 return AVERROR_INVALIDDATA
;
869 ret
= init_get_bits8(&gb
, avctx
->extradata
, avctx
->extradata_size
);
873 s
->version
= get_bits(&gb
, 2);
874 if (s
->version
>= 2) {
875 s
->version
= get_bits(&gb
, 8);
876 s
->minor_version
= get_bits(&gb
, 8);
880 av_log(avctx
, AV_LOG_ERROR
, "Unsupported Sonic version, please report\n");
881 return AVERROR_INVALIDDATA
;
886 int sample_rate_index
;
887 s
->channels
= get_bits(&gb
, 2);
888 sample_rate_index
= get_bits(&gb
, 4);
889 if (sample_rate_index
>= FF_ARRAY_ELEMS(samplerate_table
)) {
890 av_log(avctx
, AV_LOG_ERROR
, "Invalid sample_rate_index %d\n", sample_rate_index
);
891 return AVERROR_INVALIDDATA
;
893 s
->samplerate
= samplerate_table
[sample_rate_index
];
894 av_log(avctx
, AV_LOG_INFO
, "Sonicv2 chans: %d samprate: %d\n",
895 s
->channels
, s
->samplerate
);
898 if (s
->channels
> MAX_CHANNELS
|| s
->channels
< 1)
900 av_log(avctx
, AV_LOG_ERROR
, "Only mono and stereo streams are supported by now\n");
901 return AVERROR_INVALIDDATA
;
903 av_channel_layout_uninit(&avctx
->ch_layout
);
904 avctx
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
905 avctx
->ch_layout
.nb_channels
= s
->channels
;
907 s
->lossless
= get_bits1(&gb
);
909 skip_bits(&gb
, 3); // XXX FIXME
910 s
->decorrelation
= get_bits(&gb
, 2);
911 if (s
->decorrelation
!= 3 && s
->channels
!= 2) {
912 av_log(avctx
, AV_LOG_ERROR
, "invalid decorrelation %d\n", s
->decorrelation
);
913 return AVERROR_INVALIDDATA
;
916 s
->downsampling
= get_bits(&gb
, 2);
917 if (!s
->downsampling
) {
918 av_log(avctx
, AV_LOG_ERROR
, "invalid downsampling value\n");
919 return AVERROR_INVALIDDATA
;
922 s
->num_taps
= (get_bits(&gb
, 5)+1)<<5;
923 if (get_bits1(&gb
)) // XXX FIXME
924 av_log(avctx
, AV_LOG_INFO
, "Custom quant table\n");
926 if (s
->num_taps
> 128)
927 return AVERROR_INVALIDDATA
;
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 tmp
= av_calloc(s
->num_taps
, s
->channels
* sizeof(**s
->predictor_state
));
955 return AVERROR(ENOMEM
);
956 for (i
= 0; i
< s
->channels
; i
++, tmp
+= s
->num_taps
)
957 s
->predictor_state
[i
] = tmp
;
959 tmp
= av_calloc(s
->block_align
, s
->channels
* sizeof(**s
->coded_samples
));
961 return AVERROR(ENOMEM
);
962 for (i
= 0; i
< s
->channels
; i
++, tmp
+= s
->block_align
)
963 s
->coded_samples
[i
] = tmp
;
965 s
->int_samples
= av_calloc(s
->frame_size
, sizeof(*s
->int_samples
));
967 return AVERROR(ENOMEM
);
969 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16
;
973 static av_cold
int sonic_decode_close(AVCodecContext
*avctx
)
975 SonicContext
*s
= avctx
->priv_data
;
977 av_freep(&s
->int_samples
);
978 av_freep(&s
->tap_quant
);
979 av_freep(&s
->predictor_k
);
980 av_freep(&s
->predictor_state
[0]);
981 av_freep(&s
->coded_samples
[0]);
986 static int sonic_decode_frame(AVCodecContext
*avctx
, AVFrame
*frame
,
987 int *got_frame_ptr
, AVPacket
*avpkt
)
989 const uint8_t *buf
= avpkt
->data
;
990 int buf_size
= avpkt
->size
;
991 SonicContext
*s
= avctx
->priv_data
;
994 int i
, quant
, ch
, j
, ret
;
997 if (buf_size
== 0) return 0;
999 frame
->nb_samples
= s
->frame_size
/ avctx
->ch_layout
.nb_channels
;
1000 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
1002 samples
= (int16_t *)frame
->data
[0];
1004 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
1006 memset(state
, 128, sizeof(state
));
1007 ff_init_range_decoder(&c
, buf
, buf_size
);
1008 ff_build_rac_states(&c
, 0.05*(1LL<<32), 256-8);
1010 intlist_read(&c
, state
, s
->predictor_k
, s
->num_taps
, 0);
1013 for (i
= 0; i
< s
->num_taps
; i
++)
1014 s
->predictor_k
[i
] *= (unsigned) s
->tap_quant
[i
];
1019 quant
= get_symbol(&c
, state
, 0) * (unsigned)SAMPLE_FACTOR
;
1021 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
1023 for (ch
= 0; ch
< s
->channels
; ch
++)
1027 if (c
.overread
> MAX_OVERREAD
)
1028 return AVERROR_INVALIDDATA
;
1030 predictor_init_state(s
->predictor_k
, s
->predictor_state
[ch
], s
->num_taps
);
1032 intlist_read(&c
, state
, s
->coded_samples
[ch
], s
->block_align
, 1);
1034 for (i
= 0; i
< s
->block_align
; i
++)
1036 for (j
= 0; j
< s
->downsampling
- 1; j
++)
1038 s
->int_samples
[x
] = predictor_calc_error(s
->predictor_k
, s
->predictor_state
[ch
], s
->num_taps
, 0);
1042 s
->int_samples
[x
] = predictor_calc_error(s
->predictor_k
, s
->predictor_state
[ch
], s
->num_taps
, s
->coded_samples
[ch
][i
] * (unsigned)quant
);
1046 for (i
= 0; i
< s
->num_taps
; i
++)
1047 s
->predictor_state
[ch
][i
] = s
->int_samples
[s
->frame_size
- s
->channels
+ ch
- i
*s
->channels
];
1050 switch(s
->decorrelation
)
1053 for (i
= 0; i
< s
->frame_size
; i
+= s
->channels
)
1055 s
->int_samples
[i
+1] += shift(s
->int_samples
[i
], 1);
1056 s
->int_samples
[i
] -= s
->int_samples
[i
+1];
1060 for (i
= 0; i
< s
->frame_size
; i
+= s
->channels
)
1061 s
->int_samples
[i
+1] += s
->int_samples
[i
];
1064 for (i
= 0; i
< s
->frame_size
; i
+= s
->channels
)
1065 s
->int_samples
[i
] += s
->int_samples
[i
+1];
1070 for (i
= 0; i
< s
->frame_size
; i
++)
1071 s
->int_samples
[i
] = shift(s
->int_samples
[i
], SAMPLE_SHIFT
);
1073 // internal -> short
1074 for (i
= 0; i
< s
->frame_size
; i
++)
1075 samples
[i
] = av_clip_int16(s
->int_samples
[i
]);
1082 const FFCodec ff_sonic_decoder
= {
1084 .p
.long_name
= NULL_IF_CONFIG_SMALL("Sonic"),
1085 .p
.type
= AVMEDIA_TYPE_AUDIO
,
1086 .p
.id
= AV_CODEC_ID_SONIC
,
1087 .priv_data_size
= sizeof(SonicContext
),
1088 .init
= sonic_decode_init
,
1089 .close
= sonic_decode_close
,
1090 FF_CODEC_DECODE_CB(sonic_decode_frame
),
1091 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_EXPERIMENTAL
| AV_CODEC_CAP_CHANNEL_CONF
,
1092 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
| FF_CODEC_CAP_INIT_CLEANUP
,
1094 #endif /* CONFIG_SONIC_DECODER */
1096 #if CONFIG_SONIC_ENCODER
1097 const FFCodec ff_sonic_encoder
= {
1099 .p
.long_name
= NULL_IF_CONFIG_SMALL("Sonic"),
1100 .p
.type
= AVMEDIA_TYPE_AUDIO
,
1101 .p
.id
= AV_CODEC_ID_SONIC
,
1102 .priv_data_size
= sizeof(SonicContext
),
1103 .init
= sonic_encode_init
,
1104 FF_CODEC_ENCODE_CB(sonic_encode_frame
),
1105 .p
.sample_fmts
= (const enum AVSampleFormat
[]){ AV_SAMPLE_FMT_S16
, AV_SAMPLE_FMT_NONE
},
1106 .p
.capabilities
= AV_CODEC_CAP_EXPERIMENTAL
,
1107 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
| FF_CODEC_CAP_INIT_CLEANUP
,
1108 .close
= sonic_encode_close
,
1112 #if CONFIG_SONIC_LS_ENCODER
1113 const FFCodec ff_sonic_ls_encoder
= {
1114 .p
.name
= "sonicls",
1115 .p
.long_name
= NULL_IF_CONFIG_SMALL("Sonic lossless"),
1116 .p
.type
= AVMEDIA_TYPE_AUDIO
,
1117 .p
.id
= AV_CODEC_ID_SONIC_LS
,
1118 .priv_data_size
= sizeof(SonicContext
),
1119 .init
= sonic_encode_init
,
1120 FF_CODEC_ENCODE_CB(sonic_encode_frame
),
1121 .p
.sample_fmts
= (const enum AVSampleFormat
[]){ AV_SAMPLE_FMT_S16
, AV_SAMPLE_FMT_NONE
},
1122 .p
.capabilities
= AV_CODEC_CAP_EXPERIMENTAL
,
1123 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
| FF_CODEC_CAP_INIT_CLEANUP
,
1124 .close
= sonic_encode_close
,