Mercurial > libavcodec.hg
annotate sonic.c @ 3418:ecdc24320e47 libavcodec
spelling typo
author | diego |
---|---|
date | Wed, 05 Jul 2006 22:56:45 +0000 |
parents | 0b546eab515d |
children | 20545fbb6f7c |
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 * |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
5 * This library is free software; you can redistribute it and/or |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
6 * 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
|
7 * License as published by the Free Software Foundation; either |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
8 * version 2 of the License, or (at your option) any later version. |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
9 * |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
10 * This library is distributed in the hope that it will be useful, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
11 * 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
|
12 * 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
|
13 * 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
|
14 * |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
15 * You should have received a copy 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
|
16 * License along with this library; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
17 * 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
|
18 */ |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
19 #include "avcodec.h" |
2398
582e635cfa08
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
michael
parents:
2205
diff
changeset
|
20 #include "bitstream.h" |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
21 #include "golomb.h" |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
22 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
23 /** |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
24 * @file sonic.c |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
25 * 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
|
26 * 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
|
27 * 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
|
28 * |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
29 * TODO: |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
30 * - CABAC put/get_symbol |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
31 * - independent quantizer for channels |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
32 * - >2 channels support |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
33 * - more decorrelation types |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
34 * - more tap_quant tests |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
35 * - 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
|
36 */ |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
37 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
38 #define MAX_CHANNELS 2 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
39 |
2205 | 40 #define MID_SIDE 0 |
41 #define LEFT_SIDE 1 | |
42 #define RIGHT_SIDE 2 | |
43 | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
44 typedef struct SonicContext { |
2205 | 45 int lossless, decorrelation; |
2967 | 46 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
47 int num_taps, downsampling; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
48 double quantization; |
2967 | 49 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
50 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
|
51 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
52 int *tap_quant; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
53 int *int_samples; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
54 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
|
55 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
56 // for encoding |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
57 int *tail; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
58 int tail_size; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
59 int *window; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
60 int window_size; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
61 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
62 // for decoding |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
63 int *predictor_k; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
64 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
|
65 } SonicContext; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
66 |
2979 | 67 #define LATTICE_SHIFT 10 |
68 #define SAMPLE_SHIFT 4 | |
69 #define LATTICE_FACTOR (1 << LATTICE_SHIFT) | |
70 #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
|
71 |
2979 | 72 #define BASE_QUANT 0.6 |
73 #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
|
74 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
75 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
|
76 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
77 if (a < 0) |
2979 | 78 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
|
79 else |
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 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
82 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
83 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
|
84 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
85 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
|
86 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
87 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
88 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
|
89 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
90 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
|
91 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
92 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
93 #if 1 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
94 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
|
95 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
96 int i; |
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 for (i = 0; i < entries; i++) |
2979 | 99 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
|
100 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
101 return 1; |
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 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
104 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
|
105 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
106 int i; |
2967 | 107 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
108 for (i = 0; i < entries; i++) |
2979 | 109 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
|
110 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
111 return 1; |
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 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
114 #else |
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 #define ADAPT_LEVEL 8 |
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 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
|
119 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
120 int res = 0; |
2967 | 121 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
122 while(x) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
123 { |
2979 | 124 res++; |
125 x >>= 1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
126 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
127 return res; |
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 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
130 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
|
131 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
132 int i, bits; |
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 if (!max) |
2979 | 135 return; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
136 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
137 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
|
138 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
139 for (i = 0; i < bits-1; i++) |
2979 | 140 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
|
141 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
142 if ( (value | (1 << (bits-1))) <= max) |
2979 | 143 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
|
144 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
145 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
146 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
|
147 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
148 int i, bits, value = 0; |
2967 | 149 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
150 if (!max) |
2979 | 151 return 0; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
152 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
153 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
|
154 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
155 for (i = 0; i < bits-1; i++) |
2979 | 156 if (get_bits1(gb)) |
157 value += 1 << i; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
158 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
159 if ( (value | (1<<(bits-1))) <= max) |
2979 | 160 if (get_bits1(gb)) |
161 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
|
162 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
163 return value; |
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 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
166 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
|
167 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
168 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
|
169 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
|
170 int *copy, *bits; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
171 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
172 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
|
173 if (!copy) |
2979 | 174 return -1; |
2967 | 175 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
176 if (base_2_part) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
177 { |
2979 | 178 int energy = 0; |
2967 | 179 |
2979 | 180 for (i = 0; i < entries; i++) |
181 energy += abs(buf[i]); | |
2967 | 182 |
2979 | 183 low_bits = bits_to_store(energy / (entries * 2)); |
184 if (low_bits > 15) | |
185 low_bits = 15; | |
2967 | 186 |
2979 | 187 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
|
188 } |
2967 | 189 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
190 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
|
191 { |
2979 | 192 put_bits(pb, low_bits, abs(buf[i])); |
193 copy[i] = abs(buf[i]) >> low_bits; | |
194 if (copy[i] > max) | |
195 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
|
196 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
197 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
198 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
|
199 if (!bits) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
200 { |
2979 | 201 // av_free(copy); |
202 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
203 } |
2967 | 204 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
205 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
|
206 { |
2979 | 207 for (j = 0; j < entries; j++) |
208 if (copy[j] >= i) | |
209 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
|
210 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
211 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
212 // store bitstream |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
213 while (pos < x) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
214 { |
2979 | 215 int steplet = step >> 8; |
2967 | 216 |
2979 | 217 if (pos + steplet > x) |
218 steplet = x - pos; | |
2967 | 219 |
2979 | 220 for (i = 0; i < steplet; i++) |
221 if (bits[i+pos] != dominant) | |
222 any = 1; | |
2967 | 223 |
2979 | 224 put_bits(pb, 1, any); |
2967 | 225 |
2979 | 226 if (!any) |
227 { | |
228 pos += steplet; | |
229 step += step / ADAPT_LEVEL; | |
230 } | |
231 else | |
232 { | |
233 int interloper = 0; | |
2967 | 234 |
2979 | 235 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant)) |
236 interloper++; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
237 |
2979 | 238 // note change |
239 write_uint_max(pb, interloper, (step >> 8) - 1); | |
2967 | 240 |
2979 | 241 pos += interloper + 1; |
242 step -= step / ADAPT_LEVEL; | |
243 } | |
2967 | 244 |
2979 | 245 if (step < 256) |
246 { | |
247 step = 65536 / step; | |
248 dominant = !dominant; | |
249 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
250 } |
2967 | 251 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
252 // store signs |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
253 for (i = 0; i < entries; i++) |
2979 | 254 if (buf[i]) |
255 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
|
256 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
257 // av_free(bits); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
258 // av_free(copy); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
259 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
260 return 0; |
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 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
263 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
|
264 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
265 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
|
266 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
|
267 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
|
268 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
|
269 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
270 if (!bits) |
2979 | 271 return -1; |
2967 | 272 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
273 if (base_2_part) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
274 { |
2979 | 275 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
|
276 |
2979 | 277 if (low_bits) |
278 for (i = 0; i < entries; i++) | |
279 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
|
280 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
281 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
282 // 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
|
283 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
284 while (n_zeros < entries) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
285 { |
2979 | 286 int steplet = step >> 8; |
2967 | 287 |
2979 | 288 if (!get_bits1(gb)) |
289 { | |
290 for (i = 0; i < steplet; i++) | |
291 bits[x++] = dominant; | |
2967 | 292 |
2979 | 293 if (!dominant) |
294 n_zeros += steplet; | |
2967 | 295 |
2979 | 296 step += step / ADAPT_LEVEL; |
297 } | |
298 else | |
299 { | |
300 int actual_run = read_uint_max(gb, steplet-1); | |
2967 | 301 |
2979 | 302 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run); |
2967 | 303 |
2979 | 304 for (i = 0; i < actual_run; i++) |
305 bits[x++] = dominant; | |
2967 | 306 |
2979 | 307 bits[x++] = !dominant; |
2967 | 308 |
2979 | 309 if (!dominant) |
310 n_zeros += actual_run; | |
311 else | |
312 n_zeros++; | |
2967 | 313 |
2979 | 314 step -= step / ADAPT_LEVEL; |
315 } | |
2967 | 316 |
2979 | 317 if (step < 256) |
318 { | |
319 step = 65536 / step; | |
320 dominant = !dominant; | |
321 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
322 } |
2967 | 323 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
324 // reconstruct unsigned values |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
325 n_zeros = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
326 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
|
327 { |
2979 | 328 while(1) |
329 { | |
330 if (pos >= entries) | |
331 { | |
332 pos = 0; | |
333 level += 1 << low_bits; | |
334 } | |
2967 | 335 |
2979 | 336 if (buf[pos] >= level) |
337 break; | |
2967 | 338 |
2979 | 339 pos++; |
340 } | |
2967 | 341 |
2979 | 342 if (bits[i]) |
343 buf[pos] += 1 << low_bits; | |
344 else | |
345 n_zeros++; | |
2967 | 346 |
2979 | 347 pos++; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
348 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
349 // av_free(bits); |
2967 | 350 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
351 // read signs |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
352 for (i = 0; i < entries; i++) |
2979 | 353 if (buf[i] && get_bits1(gb)) |
354 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
|
355 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
356 // 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
|
357 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
358 return 0; |
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 #endif |
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 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
|
363 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
364 int i; |
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 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
|
367 { |
2979 | 368 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
|
369 |
2979 | 370 for (j = 0, p = i+1; p < order; j++,p++) |
371 { | |
372 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT); | |
373 state[p] += shift_down(k[j]*x, LATTICE_SHIFT); | |
374 x = tmp; | |
375 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
376 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
377 } |
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 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
|
380 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
381 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
|
382 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
383 #if 1 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
384 int *k_ptr = &(k[order-2]), |
2979 | 385 *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
|
386 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
|
387 { |
2979 | 388 int k_value = *k_ptr, state_value = *state_ptr; |
389 x -= shift_down(k_value * state_value, LATTICE_SHIFT); | |
390 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
|
391 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
392 #else |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
393 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
|
394 { |
2979 | 395 x -= shift_down(k[i] * state[i], LATTICE_SHIFT); |
396 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
|
397 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
398 #endif |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
399 |
2967 | 400 // 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
|
401 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
|
402 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
|
403 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
404 state[0] = x; |
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 return 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 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
409 // 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
|
410 // 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
|
411 // 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
|
412 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
413 static void modified_levinson_durbin(int *window, int window_entries, |
2979 | 414 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
|
415 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
416 int i; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
417 int *state = av_mallocz(4* window_entries); |
2967 | 418 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
419 memcpy(state, window, 4* window_entries); |
2967 | 420 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
421 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
|
422 { |
2979 | 423 int step = (i+1)*channels, k, j; |
424 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
|
425 #if 1 |
2979 | 426 int *x_ptr = &(window[step]), *state_ptr = &(state[0]); |
427 j = window_entries - step; | |
428 for (;j>=0;j--,x_ptr++,state_ptr++) | |
429 { | |
430 double x_value = *x_ptr, state_value = *state_ptr; | |
431 xx += state_value*state_value; | |
432 xy += x_value*state_value; | |
433 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
434 #else |
2979 | 435 for (j = 0; j <= (window_entries - step); j++); |
436 { | |
437 double stepval = window[step+j], stateval = window[j]; | |
438 // xx += (double)window[j]*(double)window[j]; | |
439 // xy += (double)window[step+j]*(double)window[j]; | |
440 xx += stateval*stateval; | |
441 xy += stepval*stateval; | |
442 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
443 #endif |
2979 | 444 if (xx == 0.0) |
445 k = 0; | |
446 else | |
447 k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5)); | |
2967 | 448 |
2979 | 449 if (k > (LATTICE_FACTOR/tap_quant[i])) |
450 k = LATTICE_FACTOR/tap_quant[i]; | |
451 if (-k > (LATTICE_FACTOR/tap_quant[i])) | |
452 k = -(LATTICE_FACTOR/tap_quant[i]); | |
2967 | 453 |
2979 | 454 out[i] = k; |
455 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
|
456 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
457 #if 1 |
2979 | 458 x_ptr = &(window[step]); |
459 state_ptr = &(state[0]); | |
460 j = window_entries - step; | |
461 for (;j>=0;j--,x_ptr++,state_ptr++) | |
462 { | |
463 int x_value = *x_ptr, state_value = *state_ptr; | |
464 *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT); | |
465 *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT); | |
466 } | |
2967 | 467 #else |
2979 | 468 for (j=0; j <= (window_entries - step); j++) |
469 { | |
470 int stepval = window[step+j], stateval=state[j]; | |
471 window[step+j] += shift_down(k * stateval, LATTICE_SHIFT); | |
472 state[j] += shift_down(k * stepval, LATTICE_SHIFT); | |
473 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
474 #endif |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
475 } |
2967 | 476 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
477 av_free(state); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
478 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
479 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
480 static int samplerate_table[] = |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
481 { 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
|
482 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
483 #ifdef CONFIG_ENCODERS |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
484 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
485 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
|
486 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
487 switch (samplerate) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
488 { |
2979 | 489 case 44100: return 0; |
490 case 22050: return 1; | |
491 case 11025: return 2; | |
492 case 96000: return 3; | |
493 case 48000: return 4; | |
494 case 32000: return 5; | |
495 case 24000: return 6; | |
496 case 16000: return 7; | |
497 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
|
498 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
499 return -1; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
500 } |
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 static int sonic_encode_init(AVCodecContext *avctx) |
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 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
|
505 PutBitContext pb; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
506 int i, version = 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
507 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
508 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
|
509 { |
2979 | 510 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
|
511 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
|
512 } |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
513 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
514 if (avctx->channels == 2) |
2979 | 515 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
|
516 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
517 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
|
518 { |
2979 | 519 s->lossless = 1; |
520 s->num_taps = 32; | |
521 s->downsampling = 1; | |
522 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
|
523 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
524 else |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
525 { |
2979 | 526 s->num_taps = 128; |
527 s->downsampling = 2; | |
528 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
|
529 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
530 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
531 // max tap 2048 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
532 if ((s->num_taps < 32) || (s->num_taps > 1024) || |
2979 | 533 ((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
|
534 { |
2979 | 535 av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n"); |
536 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
537 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
538 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
539 // generate taps |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
540 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
|
541 for (i = 0; i < s->num_taps; i++) |
2979 | 542 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
|
543 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
544 s->channels = avctx->channels; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
545 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
|
546 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
547 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
|
548 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
|
549 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
550 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
|
551 if (!s->tail) |
2979 | 552 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
553 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
|
554 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
555 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
|
556 if (!s->predictor_k) |
2979 | 557 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
558 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
559 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
|
560 { |
2979 | 561 s->coded_samples[i] = av_mallocz(4* s->block_align); |
562 if (!s->coded_samples[i]) | |
563 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
564 } |
2967 | 565 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
566 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
|
567 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
568 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
|
569 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
|
570 if (!s->window) |
2979 | 571 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
572 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
573 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
|
574 if (!avctx->extradata) |
2979 | 575 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
576 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
|
577 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
578 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
|
579 if (version == 1) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
580 { |
2979 | 581 put_bits(&pb, 2, s->channels); |
582 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
|
583 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
584 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
|
585 if (!s->lossless) |
2979 | 586 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision |
2205 | 587 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
|
588 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
|
589 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
|
590 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
|
591 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
592 flush_put_bits(&pb); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
593 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
|
594 |
2205 | 595 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n", |
2979 | 596 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
|
597 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
598 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
|
599 if (!avctx->coded_frame) |
2979 | 600 return -ENOMEM; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
601 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
|
602 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
|
603 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
604 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
605 } |
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 static int sonic_encode_close(AVCodecContext *avctx) |
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 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
|
610 int i; |
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 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
|
613 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
614 for (i = 0; i < s->channels; i++) |
2979 | 615 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
|
616 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
617 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
|
618 av_free(s->tail); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
619 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
|
620 av_free(s->window); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
621 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
|
622 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
623 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
624 } |
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 static int sonic_encode_frame(AVCodecContext *avctx, |
2979 | 627 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
|
628 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
629 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
|
630 PutBitContext pb; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
631 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
|
632 short *samples = data; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
633 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
634 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
|
635 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
636 // short -> internal |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
637 for (i = 0; i < s->frame_size; i++) |
2979 | 638 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
|
639 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
640 if (!s->lossless) |
2979 | 641 for (i = 0; i < s->frame_size; i++) |
642 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
|
643 |
2205 | 644 switch(s->decorrelation) |
645 { | |
2979 | 646 case MID_SIDE: |
647 for (i = 0; i < s->frame_size; i += s->channels) | |
648 { | |
649 s->int_samples[i] += s->int_samples[i+1]; | |
650 s->int_samples[i+1] -= shift(s->int_samples[i], 1); | |
651 } | |
652 break; | |
653 case LEFT_SIDE: | |
654 for (i = 0; i < s->frame_size; i += s->channels) | |
655 s->int_samples[i+1] -= s->int_samples[i]; | |
656 break; | |
657 case RIGHT_SIDE: | |
658 for (i = 0; i < s->frame_size; i += s->channels) | |
659 s->int_samples[i] -= s->int_samples[i+1]; | |
660 break; | |
2205 | 661 } |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
662 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
663 memset(s->window, 0, 4* s->window_size); |
2967 | 664 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
665 for (i = 0; i < s->tail_size; i++) |
2979 | 666 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
|
667 |
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->frame_size; i++) |
2979 | 669 s->window[x++] = s->int_samples[i]; |
2967 | 670 |
2182
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->tail_size; i++) |
2979 | 672 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
|
673 |
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->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
|
676 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
677 // generate taps |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
678 modified_levinson_durbin(s->window, s->window_size, |
2979 | 679 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
|
680 if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0) |
2979 | 681 return -1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
682 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
683 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
|
684 { |
2979 | 685 x = s->tail_size+ch; |
686 for (i = 0; i < s->block_align; i++) | |
687 { | |
688 int sum = 0; | |
689 for (j = 0; j < s->downsampling; j++, x += s->channels) | |
690 sum += s->window[x]; | |
691 s->coded_samples[ch][i] = sum; | |
692 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
693 } |
2967 | 694 |
695 // 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
|
696 if (!s->lossless) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
697 { |
2979 | 698 double energy1 = 0.0, energy2 = 0.0; |
699 for (ch = 0; ch < s->channels; ch++) | |
700 { | |
701 for (i = 0; i < s->block_align; i++) | |
702 { | |
703 double sample = s->coded_samples[ch][i]; | |
704 energy2 += sample*sample; | |
705 energy1 += fabs(sample); | |
706 } | |
707 } | |
2967 | 708 |
2979 | 709 energy2 = sqrt(energy2/(s->channels*s->block_align)); |
710 energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align); | |
2967 | 711 |
2979 | 712 // increase bitrate when samples are like a gaussian distribution |
713 // reduce bitrate when samples are like a two-tailed exponential distribution | |
2967 | 714 |
2979 | 715 if (energy2 > energy1) |
716 energy2 += (energy2-energy1)*RATE_VARIATION; | |
2967 | 717 |
2979 | 718 quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR); |
719 // 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
|
720 |
2979 | 721 if (quant < 1) |
722 quant = 1; | |
723 if (quant > 65535) | |
724 quant = 65535; | |
2967 | 725 |
2979 | 726 set_ue_golomb(&pb, quant); |
2967 | 727 |
2979 | 728 quant *= SAMPLE_FACTOR; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
729 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
730 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
731 // write out coded samples |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
732 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
|
733 { |
2979 | 734 if (!s->lossless) |
735 for (i = 0; i < s->block_align; i++) | |
736 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
|
737 |
2979 | 738 if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0) |
739 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
740 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
741 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
742 // 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
|
743 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
744 flush_put_bits(&pb); |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
745 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
|
746 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
747 #endif //CONFIG_ENCODERS |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
748 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
749 static int sonic_decode_init(AVCodecContext *avctx) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
750 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
751 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
|
752 GetBitContext gb; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
753 int i, version; |
2967 | 754 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
755 s->channels = avctx->channels; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
756 s->samplerate = avctx->sample_rate; |
2967 | 757 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
758 if (!avctx->extradata) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
759 { |
2979 | 760 av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n"); |
761 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
762 } |
2967 | 763 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
764 init_get_bits(&gb, avctx->extradata, avctx->extradata_size); |
2967 | 765 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
766 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
|
767 if (version > 1) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
768 { |
2979 | 769 av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n"); |
770 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
771 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
772 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
773 if (version == 1) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
774 { |
2979 | 775 s->channels = get_bits(&gb, 2); |
776 s->samplerate = samplerate_table[get_bits(&gb, 4)]; | |
777 av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n", | |
778 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
|
779 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
780 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
781 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
|
782 { |
2979 | 783 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n"); |
784 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
785 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
786 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
787 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
|
788 if (!s->lossless) |
2979 | 789 skip_bits(&gb, 3); // XXX FIXME |
2205 | 790 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
|
791 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
792 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
|
793 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
|
794 if (get_bits1(&gb)) // XXX FIXME |
2979 | 795 av_log(avctx, AV_LOG_INFO, "Custom quant table\n"); |
2967 | 796 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
797 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
|
798 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
|
799 // 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
|
800 |
2205 | 801 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n", |
2979 | 802 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
|
803 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
804 // generate taps |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
805 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
|
806 for (i = 0; i < s->num_taps; i++) |
2979 | 807 s->tap_quant[i] = (int)(sqrt(i+1)); |
2967 | 808 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
809 s->predictor_k = av_mallocz(4* s->num_taps); |
2967 | 810 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
811 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
|
812 { |
2979 | 813 s->predictor_state[i] = av_mallocz(4* s->num_taps); |
814 if (!s->predictor_state[i]) | |
815 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
816 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
817 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
818 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
|
819 { |
2979 | 820 s->coded_samples[i] = av_mallocz(4* s->block_align); |
821 if (!s->coded_samples[i]) | |
822 return -1; | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
823 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
824 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
|
825 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
826 return 0; |
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 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
829 static int sonic_decode_close(AVCodecContext *avctx) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
830 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
831 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
|
832 int i; |
2967 | 833 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
834 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
|
835 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
|
836 av_free(s->predictor_k); |
2967 | 837 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
838 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
|
839 { |
2979 | 840 av_free(s->predictor_state[i]); |
841 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
|
842 } |
2967 | 843 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
844 return 0; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
845 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
846 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
847 static int sonic_decode_frame(AVCodecContext *avctx, |
2979 | 848 void *data, int *data_size, |
849 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
|
850 { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
851 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
|
852 GetBitContext gb; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
853 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
|
854 short *samples = data; |
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 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
|
857 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
858 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size); |
2967 | 859 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
860 init_get_bits(&gb, buf, buf_size*8); |
2967 | 861 |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
862 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
|
863 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
864 // dequantize |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
865 for (i = 0; i < s->num_taps; i++) |
2979 | 866 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
|
867 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
868 if (s->lossless) |
2979 | 869 quant = 1; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
870 else |
2979 | 871 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
|
872 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
873 // 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
|
874 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
875 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
|
876 { |
2979 | 877 int x = ch; |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
878 |
2979 | 879 predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps); |
2967 | 880 |
2979 | 881 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
|
882 |
2979 | 883 for (i = 0; i < s->block_align; i++) |
884 { | |
885 for (j = 0; j < s->downsampling - 1; j++) | |
886 { | |
887 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0); | |
888 x += s->channels; | |
889 } | |
2967 | 890 |
2979 | 891 s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant); |
892 x += s->channels; | |
893 } | |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
894 |
2979 | 895 for (i = 0; i < s->num_taps; i++) |
896 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
|
897 } |
2967 | 898 |
2205 | 899 switch(s->decorrelation) |
900 { | |
2979 | 901 case MID_SIDE: |
902 for (i = 0; i < s->frame_size; i += s->channels) | |
903 { | |
904 s->int_samples[i+1] += shift(s->int_samples[i], 1); | |
905 s->int_samples[i] -= s->int_samples[i+1]; | |
906 } | |
907 break; | |
908 case LEFT_SIDE: | |
909 for (i = 0; i < s->frame_size; i += s->channels) | |
910 s->int_samples[i+1] += s->int_samples[i]; | |
911 break; | |
912 case RIGHT_SIDE: | |
913 for (i = 0; i < s->frame_size; i += s->channels) | |
914 s->int_samples[i] += s->int_samples[i+1]; | |
915 break; | |
2205 | 916 } |
2182
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
917 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
918 if (!s->lossless) |
2979 | 919 for (i = 0; i < s->frame_size; i++) |
920 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
|
921 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
922 // internal -> short |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
923 for (i = 0; i < s->frame_size; i++) |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
924 { |
2979 | 925 if (s->int_samples[i] > 32767) |
926 samples[i] = 32767; | |
927 else if (s->int_samples[i] < -32768) | |
928 samples[i] = -32768; | |
929 else | |
930 samples[i] = 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
|
931 } |
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 align_get_bits(&gb); |
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 *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
|
936 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
937 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
|
938 } |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
939 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
940 #ifdef CONFIG_ENCODERS |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
941 AVCodec sonic_encoder = { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
942 "sonic", |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
943 CODEC_TYPE_AUDIO, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
944 CODEC_ID_SONIC, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
945 sizeof(SonicContext), |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
946 sonic_encode_init, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
947 sonic_encode_frame, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
948 sonic_encode_close, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
949 NULL, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
950 }; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
951 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
952 AVCodec sonic_ls_encoder = { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
953 "sonicls", |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
954 CODEC_TYPE_AUDIO, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
955 CODEC_ID_SONIC_LS, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
956 sizeof(SonicContext), |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
957 sonic_encode_init, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
958 sonic_encode_frame, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
959 sonic_encode_close, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
960 NULL, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
961 }; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
962 #endif |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
963 |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
964 #ifdef CONFIG_DECODERS |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
965 AVCodec sonic_decoder = { |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
966 "sonic", |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
967 CODEC_TYPE_AUDIO, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
968 CODEC_ID_SONIC, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
969 sizeof(SonicContext), |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
970 sonic_decode_init, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
971 NULL, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
972 sonic_decode_close, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
973 sonic_decode_frame, |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
974 }; |
113732773bda
new opensource lossy/lossless audio codec based on speech compression techniques (actually based on bonk)
alex
parents:
diff
changeset
|
975 #endif |