Mercurial > libavcodec.hg
annotate sonic.c @ 8136:3085502c4f33 libavcodec
add support for spectral extension
author | jbr |
---|---|
date | Thu, 13 Nov 2008 03:18:13 +0000 |
parents | 6efb15a24e91 |
children | 7a463923ecd1 |
rev | line source |
---|---|
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
1 /* |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
2 * Simple free lossless/lossy audio codec |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
3 * Copyright (c) 2004 Alex Beregszaszi |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
4 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
9 * License as published by the Free Software Foundation; either |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
15 * Lesser General Public License for more details. |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
16 * |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
20 */ |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
21 #include "avcodec.h" |
2398
582e635cfa08
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
michael
parents:
2205
diff
changeset
|
22 #include "bitstream.h" |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
23 #include "golomb.h" |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
24 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
25 /** |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
26 * @file sonic.c |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
27 * Simple free lossless/lossy audio codec |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
28 * Based on Paul Francis Harrison's Bonk (http://www.logarithmic.net/pfh/bonk) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
29 * Written and designed by Alex Beregszaszi |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
30 * |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
31 * TODO: |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
32 * - CABAC put/get_symbol |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
33 * - independent quantizer for channels |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
34 * - >2 channels support |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
35 * - more decorrelation types |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
36 * - more tap_quant tests |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
37 * - selectable intlist writers/readers (bonk-style, golomb, cabac) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
38 */ |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
39 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
40 #define MAX_CHANNELS 2 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
41 |
2205 | 42 #define MID_SIDE 0 |
43 #define LEFT_SIDE 1 | |
44 #define RIGHT_SIDE 2 | |
45 | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
46 typedef struct SonicContext { |
2205 | 47 int lossless, decorrelation; |
2967 | 48 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
49 int num_taps, downsampling; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
50 double quantization; |
2967 | 51 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
52 int channels, samplerate, block_align, frame_size; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
53 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
54 int *tap_quant; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
55 int *int_samples; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
56 int *coded_samples[MAX_CHANNELS]; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
57 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
58 // for encoding |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
59 int *tail; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
60 int tail_size; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
61 int *window; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
62 int window_size; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
63 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
64 // for decoding |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
65 int *predictor_k; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
66 int *predictor_state[MAX_CHANNELS]; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
67 } SonicContext; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
68 |
2979 | 69 #define LATTICE_SHIFT 10 |
70 #define SAMPLE_SHIFT 4 | |
71 #define LATTICE_FACTOR (1 << LATTICE_SHIFT) | |
72 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT) | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
73 |
2979 | 74 #define BASE_QUANT 0.6 |
75 #define RATE_VARIATION 3.0 | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
76 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
77 static inline int divide(int a, int b) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
78 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
79 if (a < 0) |
2979 | 80 return -( (-a + b/2)/b ); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
81 else |
2979 | 82 return (a + b/2)/b; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
83 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
84 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
85 static inline int shift(int a,int b) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
86 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
87 return (a+(1<<(b-1))) >> b; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
88 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
89 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
90 static inline int shift_down(int a,int b) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
91 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
92 return (a>>b)+((a<0)?1:0); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
93 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
94 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
95 #if 1 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
96 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
97 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
98 int i; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
99 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
100 for (i = 0; i < entries; i++) |
2979 | 101 set_se_golomb(pb, buf[i]); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
102 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
103 return 1; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
104 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
105 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
106 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
107 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
108 int i; |
2967 | 109 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
110 for (i = 0; i < entries; i++) |
2979 | 111 buf[i] = get_se_golomb(gb); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
112 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
113 return 1; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
114 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
115 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
116 #else |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
117 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
118 #define ADAPT_LEVEL 8 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
119 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
120 static int bits_to_store(uint64_t x) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
121 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
122 int res = 0; |
2967 | 123 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
124 while(x) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
125 { |
2979 | 126 res++; |
127 x >>= 1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
128 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
129 return res; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
130 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
131 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
132 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
133 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
134 int i, bits; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
135 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
136 if (!max) |
2979 | 137 return; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
138 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
139 bits = bits_to_store(max); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
140 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
141 for (i = 0; i < bits-1; i++) |
2979 | 142 put_bits(pb, 1, value & (1 << i)); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
143 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
144 if ( (value | (1 << (bits-1))) <= max) |
2979 | 145 put_bits(pb, 1, value & (1 << (bits-1))); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
146 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
147 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
148 static unsigned int read_uint_max(GetBitContext *gb, int max) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
149 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
150 int i, bits, value = 0; |
2967 | 151 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
152 if (!max) |
2979 | 153 return 0; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
154 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
155 bits = bits_to_store(max); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
156 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
157 for (i = 0; i < bits-1; i++) |
2979 | 158 if (get_bits1(gb)) |
159 value += 1 << i; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
160 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
161 if ( (value | (1<<(bits-1))) <= max) |
2979 | 162 if (get_bits1(gb)) |
163 value += 1 << (bits-1); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
164 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
165 return value; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
166 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
167 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
168 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
169 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
170 int i, j, x = 0, low_bits = 0, max = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
171 int step = 256, pos = 0, dominant = 0, any = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
172 int *copy, *bits; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
173 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
174 copy = av_mallocz(4* entries); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
175 if (!copy) |
2979 | 176 return -1; |
2967 | 177 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
178 if (base_2_part) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
179 { |
2979 | 180 int energy = 0; |
2967 | 181 |
2979 | 182 for (i = 0; i < entries; i++) |
183 energy += abs(buf[i]); | |
2967 | 184 |
2979 | 185 low_bits = bits_to_store(energy / (entries * 2)); |
186 if (low_bits > 15) | |
187 low_bits = 15; | |
2967 | 188 |
2979 | 189 put_bits(pb, 4, low_bits); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
190 } |
2967 | 191 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
192 for (i = 0; i < entries; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
193 { |
2979 | 194 put_bits(pb, low_bits, abs(buf[i])); |
195 copy[i] = abs(buf[i]) >> low_bits; | |
196 if (copy[i] > max) | |
197 max = abs(copy[i]); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
198 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
199 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
200 bits = av_mallocz(4* entries*max); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
201 if (!bits) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
202 { |
2979 | 203 // av_free(copy); |
204 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
205 } |
2967 | 206 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
207 for (i = 0; i <= max; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
208 { |
2979 | 209 for (j = 0; j < entries; j++) |
210 if (copy[j] >= i) | |
211 bits[x++] = copy[j] > i; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
212 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
213 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
214 // store bitstream |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
215 while (pos < x) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
216 { |
2979 | 217 int steplet = step >> 8; |
2967 | 218 |
2979 | 219 if (pos + steplet > x) |
220 steplet = x - pos; | |
2967 | 221 |
2979 | 222 for (i = 0; i < steplet; i++) |
223 if (bits[i+pos] != dominant) | |
224 any = 1; | |
2967 | 225 |
2979 | 226 put_bits(pb, 1, any); |
2967 | 227 |
2979 | 228 if (!any) |
229 { | |
230 pos += steplet; | |
231 step += step / ADAPT_LEVEL; | |
232 } | |
233 else | |
234 { | |
235 int interloper = 0; | |
2967 | 236 |
2979 | 237 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant)) |
238 interloper++; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
239 |
2979 | 240 // note change |
241 write_uint_max(pb, interloper, (step >> 8) - 1); | |
2967 | 242 |
2979 | 243 pos += interloper + 1; |
244 step -= step / ADAPT_LEVEL; | |
245 } | |
2967 | 246 |
2979 | 247 if (step < 256) |
248 { | |
249 step = 65536 / step; | |
250 dominant = !dominant; | |
251 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
252 } |
2967 | 253 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
254 // store signs |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
255 for (i = 0; i < entries; i++) |
2979 | 256 if (buf[i]) |
257 put_bits(pb, 1, buf[i] < 0); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
258 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
259 // av_free(bits); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
260 // av_free(copy); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
261 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
262 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
263 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
264 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
265 static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
266 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
267 int i, low_bits = 0, x = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
268 int n_zeros = 0, step = 256, dominant = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
269 int pos = 0, level = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
270 int *bits = av_mallocz(4* entries); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
271 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
272 if (!bits) |
2979 | 273 return -1; |
2967 | 274 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
275 if (base_2_part) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
276 { |
2979 | 277 low_bits = get_bits(gb, 4); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
278 |
2979 | 279 if (low_bits) |
280 for (i = 0; i < entries; i++) | |
281 buf[i] = get_bits(gb, low_bits); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
282 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
283 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
284 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
285 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
286 while (n_zeros < entries) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
287 { |
2979 | 288 int steplet = step >> 8; |
2967 | 289 |
2979 | 290 if (!get_bits1(gb)) |
291 { | |
292 for (i = 0; i < steplet; i++) | |
293 bits[x++] = dominant; | |
2967 | 294 |
2979 | 295 if (!dominant) |
296 n_zeros += steplet; | |
2967 | 297 |
2979 | 298 step += step / ADAPT_LEVEL; |
299 } | |
300 else | |
301 { | |
302 int actual_run = read_uint_max(gb, steplet-1); | |
2967 | 303 |
2979 | 304 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run); |
2967 | 305 |
2979 | 306 for (i = 0; i < actual_run; i++) |
307 bits[x++] = dominant; | |
2967 | 308 |
2979 | 309 bits[x++] = !dominant; |
2967 | 310 |
2979 | 311 if (!dominant) |
312 n_zeros += actual_run; | |
313 else | |
314 n_zeros++; | |
2967 | 315 |
2979 | 316 step -= step / ADAPT_LEVEL; |
317 } | |
2967 | 318 |
2979 | 319 if (step < 256) |
320 { | |
321 step = 65536 / step; | |
322 dominant = !dominant; | |
323 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
324 } |
2967 | 325 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
326 // reconstruct unsigned values |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
327 n_zeros = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
328 for (i = 0; n_zeros < entries; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
329 { |
2979 | 330 while(1) |
331 { | |
332 if (pos >= entries) | |
333 { | |
334 pos = 0; | |
335 level += 1 << low_bits; | |
336 } | |
2967 | 337 |
2979 | 338 if (buf[pos] >= level) |
339 break; | |
2967 | 340 |
2979 | 341 pos++; |
342 } | |
2967 | 343 |
2979 | 344 if (bits[i]) |
345 buf[pos] += 1 << low_bits; | |
346 else | |
347 n_zeros++; | |
2967 | 348 |
2979 | 349 pos++; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
350 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
351 // av_free(bits); |
2967 | 352 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
353 // read signs |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
354 for (i = 0; i < entries; i++) |
2979 | 355 if (buf[i] && get_bits1(gb)) |
356 buf[i] = -buf[i]; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
357 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
358 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
359 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
360 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
361 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
362 #endif |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
363 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
364 static void predictor_init_state(int *k, int *state, int order) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
365 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
366 int i; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
367 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
368 for (i = order-2; i >= 0; i--) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
369 { |
2979 | 370 int j, p, x = state[i]; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
371 |
2979 | 372 for (j = 0, p = i+1; p < order; j++,p++) |
373 { | |
374 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT); | |
375 state[p] += shift_down(k[j]*x, LATTICE_SHIFT); | |
376 x = tmp; | |
377 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
378 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
379 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
380 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
381 static int predictor_calc_error(int *k, int *state, int order, int error) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
382 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
383 int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
384 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
385 #if 1 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
386 int *k_ptr = &(k[order-2]), |
2979 | 387 *state_ptr = &(state[order-2]); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
388 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
389 { |
2979 | 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); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
393 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
394 #else |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
395 for (i = order-2; i >= 0; i--) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
396 { |
2979 | 397 x -= shift_down(k[i] * state[i], LATTICE_SHIFT); |
398 state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
399 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
400 #endif |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
401 |
2967 | 402 // don't drift too far, to avoid overflows |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
403 if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
404 if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
405 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
406 state[0] = x; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
407 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
408 return x; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
409 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
410 |
7782
6efb15a24e91
Replace generic CONFIG_ENCODERS preprocessor conditionals by more specific
diego
parents:
7776
diff
changeset
|
411 #if defined(CONFIG_SONIC_ENCODER) || defined(CONFIG_SONIC_LS_ENCODER) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
412 // Heavily modified Levinson-Durbin algorithm which |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
413 // copes better with quantization, and calculates the |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
414 // actual whitened result as it goes. |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
415 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
416 static void modified_levinson_durbin(int *window, int window_entries, |
2979 | 417 int *out, int out_entries, int channels, int *tap_quant) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
418 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
419 int i; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
420 int *state = av_mallocz(4* window_entries); |
2967 | 421 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
422 memcpy(state, window, 4* window_entries); |
2967 | 423 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
424 for (i = 0; i < out_entries; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
425 { |
2979 | 426 int step = (i+1)*channels, k, j; |
427 double xx = 0.0, xy = 0.0; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
428 #if 1 |
2979 | 429 int *x_ptr = &(window[step]), *state_ptr = &(state[0]); |
430 j = window_entries - step; | |
431 for (;j>=0;j--,x_ptr++,state_ptr++) | |
432 { | |
433 double x_value = *x_ptr, state_value = *state_ptr; | |
434 xx += state_value*state_value; | |
435 xy += x_value*state_value; | |
436 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
437 #else |
2979 | 438 for (j = 0; j <= (window_entries - step); j++); |
439 { | |
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; | |
445 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
446 #endif |
2979 | 447 if (xx == 0.0) |
448 k = 0; | |
449 else | |
450 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5)); | |
2967 | 451 |
2979 | 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]); | |
2967 | 456 |
2979 | 457 out[i] = k; |
458 k *= tap_quant[i]; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
459 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
460 #if 1 |
2979 | 461 x_ptr = &(window[step]); |
462 state_ptr = &(state[0]); | |
463 j = window_entries - step; | |
464 for (;j>=0;j--,x_ptr++,state_ptr++) | |
465 { | |
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); | |
469 } | |
2967 | 470 #else |
2979 | 471 for (j=0; j <= (window_entries - step); j++) |
472 { | |
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); | |
476 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
477 #endif |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
478 } |
2967 | 479 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
480 av_free(state); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
481 } |
7782
6efb15a24e91
Replace generic CONFIG_ENCODERS preprocessor conditionals by more specific
diego
parents:
7776
diff
changeset
|
482 #endif /* defined(CONFIG_SONIC_ENCODER) || defined(CONFIG_SONIC_LS_ENCODER) */ |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
483 |
7129 | 484 static const int samplerate_table[] = |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
485 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 }; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
486 |
7782
6efb15a24e91
Replace generic CONFIG_ENCODERS preprocessor conditionals by more specific
diego
parents:
7776
diff
changeset
|
487 #if defined(CONFIG_SONIC_ENCODER) || defined(CONFIG_SONIC_LS_ENCODER) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
488 static inline int code_samplerate(int samplerate) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
489 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
490 switch (samplerate) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
491 { |
2979 | 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; | |
500 case 8000: return 8; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
501 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
502 return -1; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
503 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
504 |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6253
diff
changeset
|
505 static av_cold int sonic_encode_init(AVCodecContext *avctx) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
506 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
507 SonicContext *s = avctx->priv_data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
508 PutBitContext pb; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
509 int i, version = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
510 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
511 if (avctx->channels > MAX_CHANNELS) |
2183
6d40885b03ad
ffmpeg short conversion got fixed somewhere, no need to workaround it - fixes amplitude bug
alex
parents:
2182
diff
changeset
|
512 { |
2979 | 513 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n"); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
514 return -1; /* only stereo or mono for now */ |
2183
6d40885b03ad
ffmpeg short conversion got fixed somewhere, no need to workaround it - fixes amplitude bug
alex
parents:
2182
diff
changeset
|
515 } |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
516 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
517 if (avctx->channels == 2) |
2979 | 518 s->decorrelation = MID_SIDE; |
2183
6d40885b03ad
ffmpeg short conversion got fixed somewhere, no need to workaround it - fixes amplitude bug
alex
parents:
2182
diff
changeset
|
519 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
520 if (avctx->codec->id == CODEC_ID_SONIC_LS) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
521 { |
2979 | 522 s->lossless = 1; |
523 s->num_taps = 32; | |
524 s->downsampling = 1; | |
525 s->quantization = 0.0; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
526 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
527 else |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
528 { |
2979 | 529 s->num_taps = 128; |
530 s->downsampling = 2; | |
531 s->quantization = 1.0; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
532 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
533 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
534 // max tap 2048 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
535 if ((s->num_taps < 32) || (s->num_taps > 1024) || |
2979 | 536 ((s->num_taps>>5)<<5 != s->num_taps)) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
537 { |
2979 | 538 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n"); |
539 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
540 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
541 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
542 // generate taps |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
543 s->tap_quant = av_mallocz(4* s->num_taps); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
544 for (i = 0; i < s->num_taps; i++) |
2979 | 545 s->tap_quant[i] = (int)(sqrt(i+1)); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
546 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
547 s->channels = avctx->channels; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
548 s->samplerate = avctx->sample_rate; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
549 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
550 s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
551 s->frame_size = s->channels*s->block_align*s->downsampling; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
552 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
553 s->tail = av_mallocz(4* s->num_taps*s->channels); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
554 if (!s->tail) |
2979 | 555 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
556 s->tail_size = s->num_taps*s->channels; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
557 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
558 s->predictor_k = av_mallocz(4 * s->num_taps); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
559 if (!s->predictor_k) |
2979 | 560 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
561 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
562 for (i = 0; i < s->channels; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
563 { |
2979 | 564 s->coded_samples[i] = av_mallocz(4* s->block_align); |
565 if (!s->coded_samples[i]) | |
566 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
567 } |
2967 | 568 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
569 s->int_samples = av_mallocz(4* s->frame_size); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
570 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
571 s->window_size = ((2*s->tail_size)+s->frame_size); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
572 s->window = av_mallocz(4* s->window_size); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
573 if (!s->window) |
2979 | 574 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
575 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
576 avctx->extradata = av_mallocz(16); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
577 if (!avctx->extradata) |
2979 | 578 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
579 init_put_bits(&pb, avctx->extradata, 16*8); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
580 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
581 put_bits(&pb, 2, version); // version |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
582 if (version == 1) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
583 { |
2979 | 584 put_bits(&pb, 2, s->channels); |
585 put_bits(&pb, 4, code_samplerate(s->samplerate)); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
586 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
587 put_bits(&pb, 1, s->lossless); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
588 if (!s->lossless) |
2979 | 589 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision |
2205 | 590 put_bits(&pb, 2, s->decorrelation); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
591 put_bits(&pb, 2, s->downsampling); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
592 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
593 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
594 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
595 flush_put_bits(&pb); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
596 avctx->extradata_size = put_bits_count(&pb)/8; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
597 |
2205 | 598 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n", |
2979 | 599 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
600 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
601 avctx->coded_frame = avcodec_alloc_frame(); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
602 if (!avctx->coded_frame) |
4520
9bf957e669f0
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
3947
diff
changeset
|
603 return AVERROR(ENOMEM); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
604 avctx->coded_frame->key_frame = 1; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
605 avctx->frame_size = s->block_align*s->downsampling; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
606 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
607 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
608 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
609 |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6253
diff
changeset
|
610 static av_cold int sonic_encode_close(AVCodecContext *avctx) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
611 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
612 SonicContext *s = avctx->priv_data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
613 int i; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
614 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
615 av_freep(&avctx->coded_frame); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
616 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
617 for (i = 0; i < s->channels; i++) |
2979 | 618 av_free(s->coded_samples[i]); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
619 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
620 av_free(s->predictor_k); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
621 av_free(s->tail); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
622 av_free(s->tap_quant); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
623 av_free(s->window); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
624 av_free(s->int_samples); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
625 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
626 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
627 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
628 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
629 static int sonic_encode_frame(AVCodecContext *avctx, |
2979 | 630 uint8_t *buf, int buf_size, void *data) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
631 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
632 SonicContext *s = avctx->priv_data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
633 PutBitContext pb; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
634 int i, j, ch, quant = 0, x = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
635 short *samples = data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
636 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
637 init_put_bits(&pb, buf, buf_size*8); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
638 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
639 // short -> internal |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
640 for (i = 0; i < s->frame_size; i++) |
2979 | 641 s->int_samples[i] = samples[i]; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
642 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
643 if (!s->lossless) |
2979 | 644 for (i = 0; i < s->frame_size; i++) |
645 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
646 |
2205 | 647 switch(s->decorrelation) |
648 { | |
2979 | 649 case MID_SIDE: |
650 for (i = 0; i < s->frame_size; i += s->channels) | |
651 { | |
652 s->int_samples[i] += s->int_samples[i+1]; | |
653 s->int_samples[i+1] -= shift(s->int_samples[i], 1); | |
654 } | |
655 break; | |
656 case LEFT_SIDE: | |
657 for (i = 0; i < s->frame_size; i += s->channels) | |
658 s->int_samples[i+1] -= s->int_samples[i]; | |
659 break; | |
660 case RIGHT_SIDE: | |
661 for (i = 0; i < s->frame_size; i += s->channels) | |
662 s->int_samples[i] -= s->int_samples[i+1]; | |
663 break; | |
2205 | 664 } |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
665 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
666 memset(s->window, 0, 4* s->window_size); |
2967 | 667 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
668 for (i = 0; i < s->tail_size; i++) |
2979 | 669 s->window[x++] = s->tail[i]; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
670 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
671 for (i = 0; i < s->frame_size; i++) |
2979 | 672 s->window[x++] = s->int_samples[i]; |
2967 | 673 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
674 for (i = 0; i < s->tail_size; i++) |
2979 | 675 s->window[x++] = 0; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
676 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
677 for (i = 0; i < s->tail_size; i++) |
2979 | 678 s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i]; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
679 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
680 // generate taps |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
681 modified_levinson_durbin(s->window, s->window_size, |
2979 | 682 s->predictor_k, s->num_taps, s->channels, s->tap_quant); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
683 if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0) |
2979 | 684 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
685 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
686 for (ch = 0; ch < s->channels; ch++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
687 { |
2979 | 688 x = s->tail_size+ch; |
689 for (i = 0; i < s->block_align; i++) | |
690 { | |
691 int sum = 0; | |
692 for (j = 0; j < s->downsampling; j++, x += s->channels) | |
693 sum += s->window[x]; | |
694 s->coded_samples[ch][i] = sum; | |
695 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
696 } |
2967 | 697 |
698 // simple rate control code | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
699 if (!s->lossless) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
700 { |
2979 | 701 double energy1 = 0.0, energy2 = 0.0; |
702 for (ch = 0; ch < s->channels; ch++) | |
703 { | |
704 for (i = 0; i < s->block_align; i++) | |
705 { | |
706 double sample = s->coded_samples[ch][i]; | |
707 energy2 += sample*sample; | |
708 energy1 += fabs(sample); | |
709 } | |
710 } | |
2967 | 711 |
2979 | 712 energy2 = sqrt(energy2/(s->channels*s->block_align)); |
713 energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align); | |
2967 | 714 |
2979 | 715 // increase bitrate when samples are like a gaussian distribution |
716 // reduce bitrate when samples are like a two-tailed exponential distribution | |
2967 | 717 |
2979 | 718 if (energy2 > energy1) |
719 energy2 += (energy2-energy1)*RATE_VARIATION; | |
2967 | 720 |
2979 | 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); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
723 |
2979 | 724 if (quant < 1) |
725 quant = 1; | |
726 if (quant > 65535) | |
727 quant = 65535; | |
2967 | 728 |
2979 | 729 set_ue_golomb(&pb, quant); |
2967 | 730 |
2979 | 731 quant *= SAMPLE_FACTOR; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
732 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
733 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
734 // write out coded samples |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
735 for (ch = 0; ch < s->channels; ch++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
736 { |
2979 | 737 if (!s->lossless) |
738 for (i = 0; i < s->block_align; i++) | |
739 s->coded_samples[ch][i] = divide(s->coded_samples[ch][i], quant); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
740 |
2979 | 741 if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0) |
742 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
743 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
744 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
745 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
746 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
747 flush_put_bits(&pb); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
748 return (put_bits_count(&pb)+7)/8; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
749 } |
7782
6efb15a24e91
Replace generic CONFIG_ENCODERS preprocessor conditionals by more specific
diego
parents:
7776
diff
changeset
|
750 #endif /* defined(CONFIG_SONIC_ENCODER) || defined(CONFIG_SONIC_LS_ENCODER) */ |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
751 |
7776
dbcdd0165e55
Replace generic CONFIG_DECODERS preprocessor conditionals by more specific
diego
parents:
7451
diff
changeset
|
752 #ifdef CONFIG_SONIC_DECODER |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6253
diff
changeset
|
753 static av_cold int sonic_decode_init(AVCodecContext *avctx) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
754 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
755 SonicContext *s = avctx->priv_data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
756 GetBitContext gb; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
757 int i, version; |
2967 | 758 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
759 s->channels = avctx->channels; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
760 s->samplerate = avctx->sample_rate; |
2967 | 761 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
762 if (!avctx->extradata) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
763 { |
2979 | 764 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n"); |
765 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
766 } |
2967 | 767 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
768 init_get_bits(&gb, avctx->extradata, avctx->extradata_size); |
2967 | 769 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
770 version = get_bits(&gb, 2); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
771 if (version > 1) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
772 { |
2979 | 773 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n"); |
774 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
775 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
776 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
777 if (version == 1) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
778 { |
2979 | 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); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
783 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
784 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
785 if (s->channels > MAX_CHANNELS) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
786 { |
2979 | 787 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n"); |
788 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
789 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
790 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
791 s->lossless = get_bits1(&gb); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
792 if (!s->lossless) |
2979 | 793 skip_bits(&gb, 3); // XXX FIXME |
2205 | 794 s->decorrelation = get_bits(&gb, 2); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
795 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
796 s->downsampling = get_bits(&gb, 2); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
797 s->num_taps = (get_bits(&gb, 5)+1)<<5; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
798 if (get_bits1(&gb)) // XXX FIXME |
2979 | 799 av_log(avctx, AV_LOG_INFO, "Custom quant table\n"); |
2967 | 800 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
801 s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
802 s->frame_size = s->channels*s->block_align*s->downsampling; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
803 // avctx->frame_size = s->block_align; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
804 |
2205 | 805 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n", |
2979 | 806 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
807 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
808 // generate taps |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
809 s->tap_quant = av_mallocz(4* s->num_taps); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
810 for (i = 0; i < s->num_taps; i++) |
2979 | 811 s->tap_quant[i] = (int)(sqrt(i+1)); |
2967 | 812 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
813 s->predictor_k = av_mallocz(4* s->num_taps); |
2967 | 814 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
815 for (i = 0; i < s->channels; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
816 { |
2979 | 817 s->predictor_state[i] = av_mallocz(4* s->num_taps); |
818 if (!s->predictor_state[i]) | |
819 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
820 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
821 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
822 for (i = 0; i < s->channels; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
823 { |
2979 | 824 s->coded_samples[i] = av_mallocz(4* s->block_align); |
825 if (!s->coded_samples[i]) | |
826 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
827 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
828 s->int_samples = av_mallocz(4* s->frame_size); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
829 |
7451
85ab7655ad4d
Modify all codecs to report their supported input and output sample format(s).
pross
parents:
7129
diff
changeset
|
830 avctx->sample_fmt = SAMPLE_FMT_S16; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
831 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
832 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
833 |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6253
diff
changeset
|
834 static av_cold int sonic_decode_close(AVCodecContext *avctx) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
835 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
836 SonicContext *s = avctx->priv_data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
837 int i; |
2967 | 838 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
839 av_free(s->int_samples); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
840 av_free(s->tap_quant); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
841 av_free(s->predictor_k); |
2967 | 842 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
843 for (i = 0; i < s->channels; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
844 { |
2979 | 845 av_free(s->predictor_state[i]); |
846 av_free(s->coded_samples[i]); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
847 } |
2967 | 848 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
849 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
850 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
851 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
852 static int sonic_decode_frame(AVCodecContext *avctx, |
2979 | 853 void *data, int *data_size, |
6253 | 854 const uint8_t *buf, int buf_size) |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
855 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
856 SonicContext *s = avctx->priv_data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
857 GetBitContext gb; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
858 int i, quant, ch, j; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
859 short *samples = data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
860 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
861 if (buf_size == 0) return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
862 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
863 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size); |
2967 | 864 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
865 init_get_bits(&gb, buf, buf_size*8); |
2967 | 866 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
867 intlist_read(&gb, s->predictor_k, s->num_taps, 0); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
868 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
869 // dequantize |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
870 for (i = 0; i < s->num_taps; i++) |
2979 | 871 s->predictor_k[i] *= s->tap_quant[i]; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
872 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
873 if (s->lossless) |
2979 | 874 quant = 1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
875 else |
2979 | 876 quant = get_ue_golomb(&gb) * SAMPLE_FACTOR; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
877 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
878 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
879 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
880 for (ch = 0; ch < s->channels; ch++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
881 { |
2979 | 882 int x = ch; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
883 |
2979 | 884 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps); |
2967 | 885 |
2979 | 886 intlist_read(&gb, s->coded_samples[ch], s->block_align, 1); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
887 |
2979 | 888 for (i = 0; i < s->block_align; i++) |
889 { | |
890 for (j = 0; j < s->downsampling - 1; j++) | |
891 { | |
892 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0); | |
893 x += s->channels; | |
894 } | |
2967 | 895 |
2979 | 896 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant); |
897 x += s->channels; | |
898 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
899 |
2979 | 900 for (i = 0; i < s->num_taps; i++) |
901 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels]; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
902 } |
2967 | 903 |
2205 | 904 switch(s->decorrelation) |
905 { | |
2979 | 906 case MID_SIDE: |
907 for (i = 0; i < s->frame_size; i += s->channels) | |
908 { | |
909 s->int_samples[i+1] += shift(s->int_samples[i], 1); | |
910 s->int_samples[i] -= s->int_samples[i+1]; | |
911 } | |
912 break; | |
913 case LEFT_SIDE: | |
914 for (i = 0; i < s->frame_size; i += s->channels) | |
915 s->int_samples[i+1] += s->int_samples[i]; | |
916 break; | |
917 case RIGHT_SIDE: | |
918 for (i = 0; i < s->frame_size; i += s->channels) | |
919 s->int_samples[i] += s->int_samples[i+1]; | |
920 break; | |
2205 | 921 } |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
922 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
923 if (!s->lossless) |
2979 | 924 for (i = 0; i < s->frame_size; i++) |
925 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT); | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
926 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
927 // internal -> short |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
928 for (i = 0; i < s->frame_size; i++) |
5523 | 929 samples[i] = av_clip_int16(s->int_samples[i]); |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
930 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
931 align_get_bits(&gb); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
932 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
933 *data_size = s->frame_size * 2; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
934 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
935 return (get_bits_count(&gb)+7)/8; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
936 } |
7776
dbcdd0165e55
Replace generic CONFIG_DECODERS preprocessor conditionals by more specific
diego
parents:
7451
diff
changeset
|
937 #endif /* CONFIG_SONIC_DECODER */ |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
938 |
7782
6efb15a24e91
Replace generic CONFIG_ENCODERS preprocessor conditionals by more specific
diego
parents:
7776
diff
changeset
|
939 #ifdef CONFIG_SONIC_ENCODER |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
940 AVCodec sonic_encoder = { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
941 "sonic", |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
942 CODEC_TYPE_AUDIO, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
943 CODEC_ID_SONIC, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
944 sizeof(SonicContext), |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
945 sonic_encode_init, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
946 sonic_encode_frame, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
947 sonic_encode_close, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
948 NULL, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6713
diff
changeset
|
949 .long_name = NULL_IF_CONFIG_SMALL("Sonic"), |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
950 }; |
7782
6efb15a24e91
Replace generic CONFIG_ENCODERS preprocessor conditionals by more specific
diego
parents:
7776
diff
changeset
|
951 #endif |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
952 |
7782
6efb15a24e91
Replace generic CONFIG_ENCODERS preprocessor conditionals by more specific
diego
parents:
7776
diff
changeset
|
953 #ifdef CONFIG_SONIC_LS_ENCODER |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
954 AVCodec sonic_ls_encoder = { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
955 "sonicls", |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
956 CODEC_TYPE_AUDIO, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
957 CODEC_ID_SONIC_LS, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
958 sizeof(SonicContext), |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
959 sonic_encode_init, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
960 sonic_encode_frame, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
961 sonic_encode_close, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
962 NULL, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6713
diff
changeset
|
963 .long_name = NULL_IF_CONFIG_SMALL("Sonic lossless"), |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
964 }; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
965 #endif |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
966 |
7776
dbcdd0165e55
Replace generic CONFIG_DECODERS preprocessor conditionals by more specific
diego
parents:
7451
diff
changeset
|
967 #ifdef CONFIG_SONIC_DECODER |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
968 AVCodec sonic_decoder = { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
969 "sonic", |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
970 CODEC_TYPE_AUDIO, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
971 CODEC_ID_SONIC, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
972 sizeof(SonicContext), |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
973 sonic_decode_init, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
974 NULL, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
975 sonic_decode_close, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
976 sonic_decode_frame, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6713
diff
changeset
|
977 .long_name = NULL_IF_CONFIG_SMALL("Sonic"), |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
978 }; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
979 #endif |