ffmpeg / libavcodec / vorbis_enc.c @ 9127d7c3
History  View  Annotate  Download (37.7 KB)
1 
/*


2 
* copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>

3 
*

4 
* This library is free software; you can redistribute it and/or

5 
* modify it under the terms of the GNU Lesser General Public

6 
* License as published by the Free Software Foundation; either

7 
* version 2 of the License, or (at your option) any later version.

8 
*

9 
* This library is distributed in the hope that it will be useful,

10 
* but WITHOUT ANY WARRANTY; without even the implied warranty of

11 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

12 
* Lesser General Public License for more details.

13 
*

14 
* You should have received a copy of the GNU Lesser General Public

15 
* License along with this library; if not, write to the Free Software

16 
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 021101301 USA

17 
*/

18  
19 
/**

20 
* @file vorbis_enc.c

21 
* Native Vorbis encoder.

22 
* @author Oded Shimon <ods15@ods15.dyndns.org>

23 
*/

24  
25 
#include "avcodec.h" 
26  
27 
#define BITSTREAM_H // don't include this 
28 
typedef int VLC; 
29 
typedef int GetBitContext; 
30 
#include "vorbis.h" 
31  
32 
#undef NDEBUG

33 
#include <assert.h> 
34  
35 
//#define ALT_BITSTREAM_WRITER

36 
//#include "bitstream.h"

37  
38 
typedef struct { 
39 
int len;

40 
uint32_t codeword; 
41 
} cb_entry_t; 
42  
43 
typedef struct { 
44 
int nentries;

45 
cb_entry_t * entries; 
46 
int ndimentions;

47 
float min;

48 
float delta;

49 
int seq_p;

50 
int lookup;

51 
int * quantlist;

52 
float * dimentions;

53 
} codebook_t; 
54  
55 
typedef struct { 
56 
int dim;

57 
int subclass;

58 
int masterbook;

59 
int * books;

60 
} floor_class_t; 
61  
62 
typedef struct { 
63 
int x;

64 
int low;

65 
int high;

66 
int sort;

67 
} floor_entry_t; 
68  
69 
typedef struct { 
70 
int partitions;

71 
int * partition_to_class;

72 
int nclasses;

73 
floor_class_t * classes; 
74 
int multiplier;

75 
int rangebits;

76 
int values;

77 
floor_entry_t * list; 
78 
} floor_t; 
79  
80 
typedef struct { 
81 
int type;

82 
int begin;

83 
int end;

84 
int partition_size;

85 
int classifications;

86 
int classbook;

87 
int (*books)[8]; 
88 
} residue_t; 
89  
90 
typedef struct { 
91 
int submaps;

92 
int * mux;

93 
int * floor;

94 
int * residue;

95 
int coupling_steps;

96 
int * magnitude;

97 
int * angle;

98 
} mapping_t; 
99  
100 
typedef struct { 
101 
int blockflag;

102 
int mapping;

103 
} vorbis_mode_t; 
104  
105 
typedef struct { 
106 
int channels;

107 
int sample_rate;

108 
int blocksize[2]; // in (1<<n) format 
109 
MDCTContext mdct[2];

110 
const float * win[2]; 
111 
int have_saved;

112 
float * saved;

113 
float * samples;

114 
float * floor; // also used for tmp values for mdct 
115 
float * coeffs; // also used for residue after floor 
116  
117 
int ncodebooks;

118 
codebook_t * codebooks; 
119  
120 
int nfloors;

121 
floor_t * floors; 
122  
123 
int nresidues;

124 
residue_t * residues; 
125  
126 
int nmappings;

127 
mapping_t * mappings; 
128  
129 
int nmodes;

130 
vorbis_mode_t * modes; 
131 
} venc_context_t; 
132  
133 
typedef struct { 
134 
int total;

135 
int total_pos;

136 
int pos;

137 
uint8_t * buf_ptr; 
138 
} PutBitContext; 
139  
140 
#define ilog(i) av_log2(2*(i)) 
141  
142 
static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) { 
143 
pb>total = buffer_len * 8;

144 
pb>total_pos = 0;

145 
pb>pos = 0;

146 
pb>buf_ptr = buf; 
147 
} 
148  
149 
static void put_bits(PutBitContext * pb, int bits, uint64_t val) { 
150 
if ((pb>total_pos += bits) >= pb>total) return; 
151 
if (!bits) return; 
152 
if (pb>pos) {

153 
if (pb>pos > bits) {

154 
*pb>buf_ptr = val << (8  pb>pos);

155 
pb>pos = bits; 
156 
bits = 0;

157 
} else {

158 
*pb>buf_ptr++ = (val << (8  pb>pos)) & 0xFF; 
159 
val >>= pb>pos; 
160 
bits = pb>pos; 
161 
pb>pos = 0;

162 
} 
163 
} 
164 
for (; bits >= 8; bits = 8) { 
165 
*pb>buf_ptr++ = val & 0xFF;

166 
val >>= 8;

167 
} 
168 
if (bits) {

169 
*pb>buf_ptr = val; 
170 
pb>pos = 8  bits;

171 
} 
172 
} 
173  
174 
static inline void flush_put_bits(PutBitContext * pb) { 
175 
} 
176  
177 
static inline int put_bits_count(PutBitContext * pb) { 
178 
return pb>total_pos;

179 
} 
180  
181 
static int cb_lookup_vals(int lookup, int dimentions, int entries) { 
182 
if (lookup == 1) { 
183 
int tmp, i;

184 
for (tmp = 0; ; tmp++) { 
185 
int n = 1; 
186 
for (i = 0; i < dimentions; i++) n *= tmp; 
187 
if (n > entries) break; 
188 
} 
189 
return tmp  1; 
190 
} else if (lookup == 2) return dimentions * entries; 
191 
return 0; 
192 
} 
193  
194 
static void ready_codebook(codebook_t * cb) { 
195 
int h[33] = { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; 
196 
int i;

197  
198 
for (i = 0; i < cb>nentries; i++) { 
199 
cb_entry_t * e = &cb>entries[i]; 
200 
int j = 0; 
201 
if (h[0]) h[0] = 0; 
202 
else {

203 
for (j = e>len; j; j)

204 
if (h[j]) break; 
205 
assert(j); 
206 
} 
207 
e>codeword = h[j]; 
208 
h[j] = 0;

209 
for (j++; j <= e>len; j++) h[j] = e>codeword  (1 << (j  1)); 
210 
} 
211 
for (i = 0; i < 33; i++) assert(!h[i]); 
212  
213 
if (!cb>lookup) cb>dimentions = NULL; 
214 
else {

215 
int vals = cb_lookup_vals(cb>lookup, cb>ndimentions, cb>nentries);

216 
cb>dimentions = av_malloc(sizeof(float) * cb>nentries * cb>ndimentions); 
217 
for (i = 0; i < cb>nentries; i++) { 
218 
float last = 0; 
219 
int j;

220 
int div = 1; 
221 
for (j = 0; j < cb>ndimentions; j++) { 
222 
int off;

223 
if (cb>lookup == 1) off = (i / div) % vals; // lookup type 1 
224 
else off = i * cb>ndimentions + j; // lookup type 2 
225  
226 
cb>dimentions[i * cb>ndimentions + j] = last + cb>min + cb>quantlist[off] * cb>delta; 
227 
if (cb>seq_p) last = cb>dimentions[i * cb>ndimentions + j];

228 
div *= vals; 
229 
} 
230 
} 
231 
} 
232  
233 
} 
234  
235 
static void ready_floor(floor_t * fc) { 
236 
int i;

237 
fc>list[0].sort = 0; 
238 
fc>list[1].sort = 1; 
239 
for (i = 2; i < fc>values; i++) { 
240 
int j;

241 
fc>list[i].low = 0;

242 
fc>list[i].high = 1;

243 
fc>list[i].sort = i; 
244 
for (j = 2; j < i; j++) { 
245 
int tmp = fc>list[j].x;

246 
if (tmp < fc>list[i].x) {

247 
if (tmp > fc>list[fc>list[i].low].x) fc>list[i].low = j;

248 
} else {

249 
if (tmp < fc>list[fc>list[i].high].x) fc>list[i].high = j;

250 
} 
251 
} 
252 
} 
253 
for (i = 0; i < fc>values  1; i++) { 
254 
int j;

255 
for (j = i + 1; j < fc>values; j++) { 
256 
if (fc>list[fc>list[i].sort].x > fc>list[fc>list[j].sort].x) {

257 
int tmp = fc>list[i].sort;

258 
fc>list[i].sort = fc>list[j].sort; 
259 
fc>list[j].sort = tmp; 
260 
} 
261 
} 
262 
} 
263 
} 
264  
265 
static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) { 
266 
codebook_t * cb; 
267 
floor_t * fc; 
268 
residue_t * rc; 
269 
mapping_t * mc; 
270 
int i, book;

271  
272 
venc>channels = avccontext>channels; 
273 
venc>sample_rate = avccontext>sample_rate; 
274 
venc>blocksize[0] = venc>blocksize[1] = 11; 
275  
276 
venc>ncodebooks = 15 + 1 + 8; 
277 
venc>codebooks = av_malloc(sizeof(codebook_t) * venc>ncodebooks);

278  
279 
int codebook0[] = { 2, 10, 8, 14, 7, 12, 11, 14, 1, 5, 3, 7, 4, 9, 7, 13, }; 
280 
int codebook1[] = { 1, 4, 2, 6, 3, 7, 5, 7, }; 
281 
int codebook2[] = { 1, 5, 7, 21, 5, 8, 9, 21, 10, 9, 12, 20, 20, 16, 20, 20, 4, 8, 9, 20, 6, 8, 9, 20, 11, 11, 13, 20, 20, 15, 17, 20, 9, 11, 14, 20, 8, 10, 15, 20, 11, 13, 15, 20, 20, 20, 20, 20, 20, 20, 20, 20, 13, 20, 20, 20, 18, 18, 20, 20, 20, 20, 20, 20, 3, 6, 8, 20, 6, 7, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 5, 7, 9, 20, 6, 6, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 8, 10, 13, 20, 8, 9, 12, 20, 11, 10, 12, 20, 20, 20, 20, 20, 18, 20, 20, 20, 15, 17, 18, 20, 18, 17, 18, 20, 20, 20, 20, 20, 7, 10, 12, 20, 8, 9, 11, 20, 14, 13, 14, 20, 20, 20, 20, 20, 6, 9, 12, 20, 7, 8, 11, 20, 12, 11, 13, 20, 20, 20, 20, 20, 9, 11, 15, 20, 8, 10, 14, 20, 12, 11, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 11, 16, 18, 20, 15, 15, 17, 20, 20, 17, 20, 20, 20, 20, 20, 20, 9, 14, 16, 20, 12, 12, 15, 20, 17, 15, 18, 20, 20, 20, 20, 20, 16, 19, 18, 20, 15, 16, 20, 20, 17, 17, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, }; 
282 
int codebook3[] = { 2, 3, 7, 13, 4, 4, 7, 15, 8, 6, 9, 17, 21, 16, 15, 21, 2, 5, 7, 11, 5, 5, 7, 14, 9, 7, 10, 16, 17, 15, 16, 21, 4, 7, 10, 17, 7, 7, 9, 15, 11, 9, 11, 16, 21, 18, 15, 21, 18, 21, 21, 21, 15, 17, 17, 19, 21, 19, 18, 20, 21, 21, 21, 20, }; 
283 
int codebook4[] = { 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 5, 7, 5, 7, 5, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, 10, 6, 10, 6, 11, 6, 11, 7, 11, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 8, 13, 8, 12, 8, 12, 8, 13, 8, 13, 9, 13, 9, 13, 9, 13, 9, 12, 10, 12, 10, 13, 10, 14, 11, 14, 12, 14, 13, 14, 13, 14, 14, 15, 16, 15, 15, 15, 14, 15, 17, 21, 22, 22, 21, 22, 22, 22, 22, 22, 22, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, }; 
284 
int codebook5[] = { 2, 5, 5, 4, 5, 4, 5, 4, 5, 4, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, }; 
285 
int codebook6[] = { 8, 5, 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 8, 4, 8, 4, 9, 5, 9, 5, 9, 5, 9, 5, 9, 6, 10, 6, 10, 7, 10, 8, 11, 9, 11, 11, 12, 13, 12, 14, 13, 15, 13, 15, 14, 16, 14, 17, 15, 17, 15, 15, 16, 16, 15, 16, 16, 16, 15, 18, 16, 15, 17, 17, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, }; 
286 
int codebook7[] = { 1, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 9, 8, 10, 9, 10, 9, }; 
287 
int codebook8[] = { 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 5, 5, 6, 5, 6, 5, 7, 5, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 9, 8, 9, 9, 9, 9, 10, 10, 10, 11, 9, 12, 9, 12, 9, 15, 10, 14, 9, 13, 10, 13, 10, 12, 10, 12, 10, 13, 10, 12, 11, 13, 11, 14, 12, 13, 13, 14, 14, 13, 14, 15, 14, 16, 13, 13, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, }; 
288 
int codebook9[] = { 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5, 5, 5, }; 
289 
int codebook10[] = { 3, 3, 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 5, 7, 5, 8, 6, 8, 6, 9, 7, 10, 7, 10, 8, 10, 8, 11, 9, 11, }; 
290 
int codebook11[] = { 3, 7, 3, 8, 3, 10, 3, 8, 3, 9, 3, 8, 4, 9, 4, 9, 5, 9, 6, 10, 6, 9, 7, 11, 7, 12, 9, 13, 10, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, }; 
291 
int codebook12[] = { 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 5, 4, }; 
292 
int codebook13[] = { 4, 2, 4, 2, 5, 3, 5, 4, 6, 6, 6, 7, 7, 8, 7, 8, 7, 8, 7, 9, 8, 9, 8, 9, 8, 10, 8, 11, 9, 12, 9, 12, }; 
293 
int codebook14[] = { 2, 5, 2, 6, 3, 6, 4, 7, 4, 7, 5, 9, 5, 11, 6, 11, 6, 11, 7, 11, 6, 11, 6, 11, 9, 11, 8, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, }; 
294  
295 
int codebook_sizes[] = { 16, 8, 256, 64, 128, 32, 96, 32, 96, 17, 32, 78, 17, 32, 78 }; 
296 
int * codebook_lens[] = { codebook0, codebook1, codebook2, codebook3, codebook4, codebook5, codebook6, codebook7,

297 
codebook8, codebook9, codebook10, codebook11, codebook12, codebook13, codebook14 }; 
298  
299 
// codebook 0..14  floor1 book, values 0..255

300 
for (book = 0; book < 15; book++) { 
301 
cb = &venc>codebooks[book]; 
302 
cb>nentries = codebook_sizes[book]; 
303 
cb>entries = av_malloc(sizeof(cb_entry_t) * cb>nentries);

304 
for (i = 0; i < cb>nentries; i++) cb>entries[i].len = codebook_lens[book][i]; 
305 
cb>ndimentions = 0;

306 
cb>min = 0.;

307 
cb>delta = 0.;

308 
cb>seq_p = 0;

309 
cb>lookup = 0;

310 
cb>quantlist = NULL;

311 
ready_codebook(cb); 
312 
} 
313  
314 
// codebook 15  residue classbook, values 0..1, dimentions 4

315 
cb = &venc>codebooks[15];

316 
cb>nentries = 2;

317 
cb>entries = av_malloc(sizeof(cb_entry_t) * cb>nentries);

318 
for (i = 0; i < cb>nentries; i++) cb>entries[i].len = 1; 
319 
cb>ndimentions = 4;

320 
cb>min = 0.;

321 
cb>delta = 0.;

322 
cb>seq_p = 0;

323 
cb>lookup = 0;

324 
cb>quantlist = NULL;

325 
ready_codebook(cb); 
326  
327 
// codebook 16..23  vector, for the residue, values 32767..32767, dimentions 1

328 
for (book = 0; book < 8; book++) { 
329 
cb = &venc>codebooks[16 + book];

330 
cb>nentries = 5;

331 
cb>entries = av_malloc(sizeof(cb_entry_t) * cb>nentries);

332 
for (i = 0; i < cb>nentries; i++) cb>entries[i].len = i == 2 ? 1 : 3; 
333 
cb>ndimentions = 1;

334 
cb>delta = 1 << ((7  book) * 2); 
335 
cb>min = cb>delta*2;

336 
cb>seq_p = 0;

337 
cb>lookup = 1;

338 
cb>quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb>lookup, cb>ndimentions, cb>nentries)); 
339 
for (i = 0; i < cb>nentries; i++) cb>quantlist[i] = i; 
340 
ready_codebook(cb); 
341 
} 
342  
343 
venc>nfloors = 1;

344 
venc>floors = av_malloc(sizeof(floor_t) * venc>nfloors);

345  
346 
// just 1 floor

347 
fc = &venc>floors[0];

348 
fc>partitions = 8;

349 
fc>partition_to_class = av_malloc(sizeof(int) * fc>partitions); 
350 
fc>nclasses = 0;

351 
for (i = 0; i < fc>partitions; i++) { 
352 
int a[] = {0,1,2,2,3,3,4,4}; 
353 
fc>partition_to_class[i] = a[i]; 
354 
fc>nclasses = FFMAX(fc>nclasses, fc>partition_to_class[i]); 
355 
} 
356 
fc>nclasses++; 
357 
fc>classes = av_malloc(sizeof(floor_class_t) * fc>nclasses);

358 
for (i = 0; i < fc>nclasses; i++) { 
359 
floor_class_t * c = &fc>classes[i]; 
360 
int j, books;

361 
int dim[] = {3,4,3,4,3}; 
362 
int subclass[] = {0,1,1,2,2}; 
363 
int masterbook[] = {0/*none*/,0,1,2,3}; 
364 
int * nbooks[] = {

365 
(int[]){ 4 }, 
366 
(int[]){ 5, 6 }, 
367 
(int[]){ 7, 8 }, 
368 
(int[]){ 1, 9, 10, 11 }, 
369 
(int[]){ 1, 12, 13, 14 }, 
370 
}; 
371 
c>dim = dim[i]; 
372 
c>subclass = subclass[i]; 
373 
c>masterbook = masterbook[i]; 
374 
books = (1 << c>subclass);

375 
c>books = av_malloc(sizeof(int) * books); 
376 
for (j = 0; j < books; j++) c>books[j] = nbooks[i][j]; 
377 
} 
378 
fc>multiplier = 2;

379 
fc>rangebits = venc>blocksize[0]  1; 
380  
381 
fc>values = 2;

382 
for (i = 0; i < fc>partitions; i++) 
383 
fc>values += fc>classes[fc>partition_to_class[i]].dim; 
384  
385 
fc>list = av_malloc(sizeof(floor_entry_t) * fc>values);

386 
fc>list[0].x = 0; 
387 
fc>list[1].x = 1 << fc>rangebits; 
388 
for (i = 2; i < fc>values; i++) { 
389 
/*int a = i  1;

390 
int g = ilog(a);

391 
assert(g <= fc>rangebits);

392 
a ^= 1 << (g1);

393 
g = 1 << (fc>rangebits  g);

394 
fc>list[i].x = g + a*2*g;*/

395 
//int a[] = {14, 4, 58, 2, 8, 28, 90};

396 
int a[] = {93,23,372,6,46,186,750,14,33,65,130,260,556,3,10,18,28,39,55,79,111,158,220,312,464,650,850}; 
397 
fc>list[i].x = a[i  2];

398 
} 
399 
ready_floor(fc); 
400  
401 
venc>nresidues = 1;

402 
venc>residues = av_malloc(sizeof(residue_t) * venc>nresidues);

403  
404 
// single residue

405 
rc = &venc>residues[0];

406 
rc>type = 0;

407 
rc>begin = 0;

408 
rc>end = 1 << (venc>blocksize[0]  1); 
409 
rc>partition_size = 64;

410 
rc>classifications = 2;

411 
rc>classbook = 15;

412 
rc>books = av_malloc(sizeof(int[8]) * rc>classifications); 
413 
for (i = 0; i < rc>classifications; i++) { 
414 
int j;

415 
for (j = 0; j < 8; j++) rc>books[i][j] = 16 + j; 
416 
rc>books[i][0] = rc>books[i][1] = rc>books[i][2] = rc>books[i][3] = 1; 
417 
} 
418  
419 
venc>nmappings = 1;

420 
venc>mappings = av_malloc(sizeof(mapping_t) * venc>nmappings);

421  
422 
// single mapping

423 
mc = &venc>mappings[0];

424 
mc>submaps = 1;

425 
mc>mux = av_malloc(sizeof(int) * venc>channels); 
426 
for (i = 0; i < venc>channels; i++) mc>mux[i] = 0; 
427 
mc>floor = av_malloc(sizeof(int) * mc>submaps); 
428 
mc>residue = av_malloc(sizeof(int) * mc>submaps); 
429 
for (i = 0; i < mc>submaps; i++) { 
430 
mc>floor[i] = 0;

431 
mc>residue[i] = 0;

432 
} 
433 
mc>coupling_steps = venc>channels == 2 ? 1 : 0; 
434 
mc>magnitude = av_malloc(sizeof(int) * mc>coupling_steps); 
435 
mc>angle = av_malloc(sizeof(int) * mc>coupling_steps); 
436 
if (mc>coupling_steps) {

437 
mc>magnitude[0] = 0; 
438 
mc>angle[0] = 1; 
439 
} 
440  
441 
venc>nmodes = 1;

442 
venc>modes = av_malloc(sizeof(vorbis_mode_t) * venc>nmodes);

443  
444 
// single mode

445 
venc>modes[0].blockflag = 0; 
446 
venc>modes[0].mapping = 0; 
447  
448 
venc>have_saved = 0;

449 
venc>saved = av_malloc(sizeof(float) * venc>channels * (1 << venc>blocksize[1]) / 2); 
450 
venc>samples = av_malloc(sizeof(float) * venc>channels * (1 << venc>blocksize[1])); 
451 
venc>floor = av_malloc(sizeof(float) * venc>channels * (1 << venc>blocksize[1]) / 2); 
452 
venc>coeffs = av_malloc(sizeof(float) * venc>channels * (1 << venc>blocksize[1]) / 2); 
453  
454 
{ 
455 
const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 }; 
456 
venc>win[0] = vwin[venc>blocksize[0]  6]; 
457 
venc>win[1] = vwin[venc>blocksize[1]  6]; 
458 
} 
459  
460 
ff_mdct_init(&venc>mdct[0], venc>blocksize[0], 0); 
461 
ff_mdct_init(&venc>mdct[1], venc>blocksize[1], 0); 
462 
} 
463  
464 
static void put_float(PutBitContext * pb, float f) { 
465 
int exp, mant;

466 
uint32_t res = 0;

467 
mant = (int)ldexp(frexp(f, &exp), 20); 
468 
exp += 788  20; 
469 
if (mant < 0) { res = (1 << 31); mant = mant; } 
470 
res = mant  (exp << 21);

471 
put_bits(pb, 32, res);

472 
} 
473  
474 
static void put_codebook_header(PutBitContext * pb, codebook_t * cb) { 
475 
int i;

476 
int ordered = 0; 
477  
478 
put_bits(pb, 24, 0x564342); //magic 
479 
put_bits(pb, 16, cb>ndimentions);

480 
put_bits(pb, 24, cb>nentries);

481  
482 
for (i = 1; i < cb>nentries; i++) if (cb>entries[i].len < cb>entries[i1].len) break; 
483 
if (i == cb>nentries) ordered = 1; 
484  
485 
put_bits(pb, 1, ordered);

486 
if (ordered) {

487 
int len = cb>entries[0].len; 
488 
put_bits(pb, 5, len  1); 
489 
i = 0;

490 
while (i < cb>nentries) {

491 
int j;

492 
for (j = 0; j+i < cb>nentries; j++) if (cb>entries[j+i].len != len) break; 
493 
put_bits(pb, ilog(cb>nentries  i), j); 
494 
i += j; 
495 
len++; 
496 
} 
497 
} else {

498 
int sparse = 0; 
499 
for (i = 0; i < cb>nentries; i++) if (!cb>entries[i].len) break; 
500 
if (i != cb>nentries) sparse = 1; 
501 
put_bits(pb, 1, sparse);

502  
503 
for (i = 0; i < cb>nentries; i++) { 
504 
if (sparse) put_bits(pb, 1, !!cb>entries[i].len); 
505 
if (cb>entries[i].len) put_bits(pb, 5, cb>entries[i].len  1); 
506 
} 
507 
} 
508  
509 
put_bits(pb, 4, cb>lookup);

510 
if (cb>lookup) {

511 
int tmp = cb_lookup_vals(cb>lookup, cb>ndimentions, cb>nentries);

512 
int bits = ilog(cb>quantlist[0]); 
513  
514 
for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb>quantlist[i])); 
515  
516 
put_float(pb, cb>min); 
517 
put_float(pb, cb>delta); 
518  
519 
put_bits(pb, 4, bits  1); 
520 
put_bits(pb, 1, cb>seq_p);

521  
522 
for (i = 0; i < tmp; i++) put_bits(pb, bits, cb>quantlist[i]); 
523 
} 
524 
} 
525  
526 
static void put_floor_header(PutBitContext * pb, floor_t * fc) { 
527 
int i;

528  
529 
put_bits(pb, 16, 1); // type, only floor1 is supported 
530  
531 
put_bits(pb, 5, fc>partitions);

532  
533 
for (i = 0; i < fc>partitions; i++) put_bits(pb, 4, fc>partition_to_class[i]); 
534  
535 
for (i = 0; i < fc>nclasses; i++) { 
536 
int j, books;

537  
538 
put_bits(pb, 3, fc>classes[i].dim  1); 
539 
put_bits(pb, 2, fc>classes[i].subclass);

540  
541 
if (fc>classes[i].subclass) put_bits(pb, 8, fc>classes[i].masterbook); 
542  
543 
books = (1 << fc>classes[i].subclass);

544  
545 
for (j = 0; j < books; j++) put_bits(pb, 8, fc>classes[i].books[j] + 1); 
546 
} 
547  
548 
put_bits(pb, 2, fc>multiplier  1); 
549 
put_bits(pb, 4, fc>rangebits);

550  
551 
for (i = 2; i < fc>values; i++) put_bits(pb, fc>rangebits, fc>list[i].x); 
552 
} 
553  
554 
static void put_residue_header(PutBitContext * pb, residue_t * rc) { 
555 
int i;

556  
557 
put_bits(pb, 16, rc>type);

558  
559 
put_bits(pb, 24, rc>begin);

560 
put_bits(pb, 24, rc>end);

561 
put_bits(pb, 24, rc>partition_size  1); 
562 
put_bits(pb, 6, rc>classifications  1); 
563 
put_bits(pb, 8, rc>classbook);

564  
565 
for (i = 0; i < rc>classifications; i++) { 
566 
int j, tmp = 0; 
567 
for (j = 0; j < 8; j++) tmp = (rc>books[i][j] != 1) << j; 
568  
569 
put_bits(pb, 3, tmp & 7); 
570 
put_bits(pb, 1, tmp > 7); 
571  
572 
if (tmp > 7) put_bits(pb, 5, tmp >> 3); 
573 
} 
574  
575 
for (i = 0; i < rc>classifications; i++) { 
576 
int j;

577 
for (j = 0; j < 8; j++) 
578 
if (rc>books[i][j] != 1) 
579 
put_bits(pb, 8, rc>books[i][j]);

580 
} 
581 
} 
582  
583 
static int put_main_header(venc_context_t * venc, uint8_t ** out) { 
584 
int i;

585 
PutBitContext pb; 
586 
uint8_t buffer[50000] = {0}, * p = buffer; 
587 
int buffer_len = sizeof buffer; 
588 
int len, hlens[3]; 
589  
590 
// identification header

591 
init_put_bits(&pb, p, buffer_len); 
592 
put_bits(&pb, 8, 1); //magic 
593 
for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]); 
594 
put_bits(&pb, 32, 0); // version 
595 
put_bits(&pb, 8, venc>channels);

596 
put_bits(&pb, 32, venc>sample_rate);

597 
put_bits(&pb, 32, 0); // bitrate 
598 
put_bits(&pb, 32, 0); // bitrate 
599 
put_bits(&pb, 32, 0); // bitrate 
600 
put_bits(&pb, 4, venc>blocksize[0]); 
601 
put_bits(&pb, 4, venc>blocksize[1]); 
602 
put_bits(&pb, 1, 1); // framing 
603  
604 
flush_put_bits(&pb); 
605 
hlens[0] = (put_bits_count(&pb) + 7) / 8; 
606 
buffer_len = hlens[0];

607 
p += hlens[0];

608  
609 
// comment header

610 
init_put_bits(&pb, p, buffer_len); 
611 
put_bits(&pb, 8, 3); //magic 
612 
for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]); 
613 
put_bits(&pb, 32, 0); // vendor length TODO 
614 
put_bits(&pb, 32, 0); // amount of comments 
615 
put_bits(&pb, 1, 1); // framing 
616  
617 
flush_put_bits(&pb); 
618 
hlens[1] = (put_bits_count(&pb) + 7) / 8; 
619 
buffer_len = hlens[1];

620 
p += hlens[1];

621  
622 
// setup header

623 
init_put_bits(&pb, p, buffer_len); 
624 
put_bits(&pb, 8, 5); //magic 
625 
for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]); 
626  
627 
// codebooks

628 
put_bits(&pb, 8, venc>ncodebooks  1); 
629 
for (i = 0; i < venc>ncodebooks; i++) put_codebook_header(&pb, &venc>codebooks[i]); 
630  
631 
// time domain, reserved, zero

632 
put_bits(&pb, 6, 0); 
633 
put_bits(&pb, 16, 0); 
634  
635 
// floors

636 
put_bits(&pb, 6, venc>nfloors  1); 
637 
for (i = 0; i < venc>nfloors; i++) put_floor_header(&pb, &venc>floors[i]); 
638  
639 
// residues

640 
put_bits(&pb, 6, venc>nresidues  1); 
641 
for (i = 0; i < venc>nresidues; i++) put_residue_header(&pb, &venc>residues[i]); 
642  
643 
// mappings

644 
put_bits(&pb, 6, venc>nmappings  1); 
645 
for (i = 0; i < venc>nmappings; i++) { 
646 
mapping_t * mc = &venc>mappings[i]; 
647 
int j;

648 
put_bits(&pb, 16, 0); // mapping type 
649  
650 
put_bits(&pb, 1, mc>submaps > 1); 
651 
if (mc>submaps > 1) put_bits(&pb, 4, mc>submaps  1); 
652  
653 
put_bits(&pb, 1, !!mc>coupling_steps);

654 
if (mc>coupling_steps) {

655 
put_bits(&pb, 8, mc>coupling_steps  1); 
656 
for (j = 0; j < mc>coupling_steps; j++) { 
657 
put_bits(&pb, ilog(venc>channels  1), mc>magnitude[j]);

658 
put_bits(&pb, ilog(venc>channels  1), mc>angle[j]);

659 
} 
660 
} 
661  
662 
put_bits(&pb, 2, 0); // reserved 
663  
664 
if (mc>submaps > 1) for (j = 0; j < venc>channels; j++) put_bits(&pb, 4, mc>mux[j]); 
665  
666 
for (j = 0; j < mc>submaps; j++) { 
667 
put_bits(&pb, 8, 0); // reserved time configuration 
668 
put_bits(&pb, 8, mc>floor[j]);

669 
put_bits(&pb, 8, mc>residue[j]);

670 
} 
671 
} 
672  
673 
// modes

674 
put_bits(&pb, 6, venc>nmodes  1); 
675 
for (i = 0; i < venc>nmodes; i++) { 
676 
put_bits(&pb, 1, venc>modes[i].blockflag);

677 
put_bits(&pb, 16, 0); // reserved window type 
678 
put_bits(&pb, 16, 0); // reserved transform type 
679 
put_bits(&pb, 8, venc>modes[i].mapping);

680 
} 
681  
682 
put_bits(&pb, 1, 1); // framing 
683  
684 
flush_put_bits(&pb); 
685 
hlens[2] = (put_bits_count(&pb) + 7) / 8; 
686  
687 
len = hlens[0] + hlens[1] + hlens[2]; 
688 
p = *out = av_mallocz(64 + len + len/255); 
689  
690 
*p++ = 2;

691 
p += av_xiphlacing(p, hlens[0]);

692 
p += av_xiphlacing(p, hlens[1]);

693 
buffer_len = 0;

694 
for (i = 0; i < 3; i++) { 
695 
memcpy(p, buffer + buffer_len, hlens[i]); 
696 
p += hlens[i]; 
697 
buffer_len += hlens[i]; 
698 
} 
699  
700 
return p  *out;

701 
} 
702  
703 
static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) { 
704 
int range = 255 / fc>multiplier + 1; 
705 
int i;

706 
for (i = 0; i < fc>values; i++) { 
707 
int position = fc>list[fc>list[i].sort].x;

708 
int begin = fc>list[fc>list[FFMAX(i1, 0)].sort].x; 
709 
int end = fc>list[fc>list[FFMIN(i+1, fc>values  1)].sort].x; 
710 
int j;

711 
float average = 0; 
712 
begin = (position + begin) / 2;

713 
end = (position + end ) / 2;

714  
715 
assert(end <= samples); 
716 
for (j = begin; j < end; j++) average += fabs(coeffs[j]);

717 
average /= end  begin; 
718 
average /= 32; // MAGIC! 
719 
for (j = 0; j < range  1; j++) if (floor1_inverse_db_table[j * fc>multiplier] > average) break; 
720 
posts[fc>list[i].sort] = j; 
721 
} 
722 
} 
723  
724 
static int render_point(int x0, int y0, int x1, int y1, int x) { 
725 
return y0 + (x  x0) * (y1  y0) / (x1  x0);

726 
} 
727  
728 
static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) { 
729 
int dy = y1  y0;

730 
int adx = x1  x0;

731 
int ady = FFMAX(dy, dy);

732 
int base = dy / adx;

733 
int x = x0;

734 
int y = y0;

735 
int err = 0; 
736 
int sy;

737 
if (dy < 0) sy = base  1; 
738 
else sy = base + 1; 
739 
ady = ady  FFMAX(base, base) * adx; 
740 
if (x >= n) return; 
741 
buf[x] = floor1_inverse_db_table[y]; 
742 
for (x = x0 + 1; x < x1; x++) { 
743 
if (x >= n) return; 
744 
err += ady; 
745 
if (err >= adx) {

746 
err = adx; 
747 
y += sy; 
748 
} else {

749 
y += base; 
750 
} 
751 
buf[x] = floor1_inverse_db_table[y]; 
752 
} 
753 
} 
754  
755 
static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) { 
756 
int range = 255 / fc>multiplier + 1; 
757 
int coded[fc>values]; // first 2 values are unused 
758 
int i, counter;

759 
int lx, ly;

760  
761 
put_bits(pb, 1, 1); // non zero 
762 
put_bits(pb, ilog(range  1), posts[0]); 
763 
put_bits(pb, ilog(range  1), posts[1]); 
764  
765 
for (i = 2; i < fc>values; i++) { 
766 
int predicted = render_point(fc>list[fc>list[i].low].x,

767 
posts[fc>list[i].low], 
768 
fc>list[fc>list[i].high].x, 
769 
posts[fc>list[i].high], 
770 
fc>list[i].x); 
771 
int highroom = range  predicted;

772 
int lowroom = predicted;

773 
int room = FFMIN(highroom, lowroom);

774 
if (predicted == posts[i]) {

775 
coded[i] = 0; // must be used later as flag! 
776 
continue;

777 
} else {

778 
if (!coded[fc>list[i].low]) coded[fc>list[i].low] = 1; 
779 
if (!coded[fc>list[i].high]) coded[fc>list[i].high] = 1; 
780 
} 
781 
if (posts[i] > predicted) {

782 
if (posts[i]  predicted > room) coded[i] = posts[i]  predicted + lowroom;

783 
else coded[i] = (posts[i]  predicted) << 1; 
784 
} else {

785 
if (predicted  posts[i] > room) coded[i] = predicted  posts[i] + highroom  1; 
786 
else coded[i] = ((predicted  posts[i]) << 1)  1; 
787 
} 
788 
} 
789  
790 
counter = 2;

791 
for (i = 0; i < fc>partitions; i++) { 
792 
floor_class_t * c = &fc>classes[fc>partition_to_class[i]]; 
793 
int k, cval = 0, csub = 1<<c>subclass; 
794 
if (c>subclass) {

795 
codebook_t * book = &venc>codebooks[c>masterbook]; 
796 
int cshift = 0; 
797 
for (k = 0; k < c>dim; k++) { 
798 
int l;

799 
for (l = 0; l < csub; l++) { 
800 
int maxval = 1; 
801 
if (c>books[l] != 1) maxval = venc>codebooks[c>books[l]].nentries; 
802 
// coded could be 1, but this still works, cause thats 0

803 
if (coded[counter + k] < maxval) break; 
804 
} 
805 
assert(l != csub); 
806 
cval = l << cshift; 
807 
cshift += c>subclass; 
808 
} 
809 
assert(cval < book>nentries); 
810 
put_bits(pb, book>entries[cval].len, book>entries[cval].codeword); 
811 
} 
812 
for (k = 0; k < c>dim; k++) { 
813 
int book = c>books[cval & (csub1)]; 
814 
int entry = coded[counter++];

815 
cval >>= c>subclass; 
816 
if (book == 1) continue; 
817 
if (entry == 1) entry = 0; 
818 
assert(entry < venc>codebooks[book].nentries); 
819 
assert(entry >= 0);

820 
put_bits(pb, venc>codebooks[book].entries[entry].len, venc>codebooks[book].entries[entry].codeword); 
821 
} 
822 
} 
823  
824 
lx = 0;

825 
ly = posts[0] * fc>multiplier; // sorted 0 is still 0 
826 
coded[0] = coded[1] = 1; 
827 
for (i = 1; i < fc>values; i++) { 
828 
int pos = fc>list[i].sort;

829 
if (coded[pos]) {

830 
render_line(lx, ly, fc>list[pos].x, posts[pos] * fc>multiplier, floor, samples); 
831 
lx = fc>list[pos].x; 
832 
ly = posts[pos] * fc>multiplier; 
833 
} 
834 
if (lx >= samples) break; 
835 
} 
836 
if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);

837 
} 
838  
839 
static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) { 
840 
int i;

841 
int entry = 1; 
842 
float distance = 0; 
843 
assert(book>dimentions); 
844 
for (i = 0; i < book>nentries; i++) { 
845 
float d = 0.; 
846 
int j;

847 
for (j = 0; j < book>ndimentions; j++) { 
848 
float a = (book>dimentions[i * book>ndimentions + j]  num[j]);

849 
d += a*a; 
850 
} 
851 
if (entry == 1  distance > d) { 
852 
entry = i; 
853 
distance = d; 
854 
} 
855 
} 
856 
put_bits(pb, book>entries[entry].len, book>entries[entry].codeword); 
857 
return &book>dimentions[entry * book>ndimentions];

858 
} 
859  
860 
static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) { 
861 
int pass, i, j, p, k;

862 
int psize = rc>partition_size;

863 
int partitions = (rc>end  rc>begin) / psize;

864 
int classes[channels][partitions];

865 
int classwords = venc>codebooks[rc>classbook].ndimentions;

866 
int real_ch = channels;

867  
868 
if (rc>type == 2) channels = 1; 
869  
870 
for (pass = 0; pass < 8; pass++) { 
871 
p = 0;

872 
while (p < partitions) {

873 
if (pass == 0) for (j = 0; j < channels; j++) { 
874 
codebook_t * book = &venc>codebooks[rc>classbook]; 
875 
int entry = 0; 
876 
put_bits(pb, book>entries[entry].len, book>entries[entry].codeword); 
877 
for (i = classwords; i; ) {

878 
classes[j][p + i] = entry % rc>classifications; 
879 
entry /= rc>classifications; 
880 
} 
881 
} 
882 
for (i = 0; i < classwords && p < partitions; i++, p++) { 
883 
for (j = 0; j < channels; j++) { 
884 
int nbook = rc>books[classes[j][p]][pass];

885 
codebook_t * book = &venc>codebooks[nbook]; 
886 
float * buf = coeffs + samples*j + rc>begin + p*psize;

887 
if (nbook == 1) continue; 
888  
889 
assert(rc>type == 0  rc>type == 2); 
890 
assert(!(psize % book>ndimentions)); 
891  
892 
if (rc>type == 0) { 
893 
for (k = 0; k < psize; k += book>ndimentions) { 
894 
float * a = put_vector(book, pb, &buf[k]);

895 
int l;

896 
for (l = 0; l < book>ndimentions; l++) buf[k + l] = a[l]; 
897 
} 
898 
} else {

899 
for (k = 0; k < psize; k += book>ndimentions) { 
900 
int dim = book>ndimentions, s = rc>begin + p * psize, l;

901 
float vec[dim], * a = vec;

902 
for (l = s + k; l < s + k + dim; l++)

903 
*a++ = coeffs[(l % real_ch) * samples + l / real_ch]; 
904 
a = put_vector(book, pb, vec); 
905 
for (l = s + k; l < s + k + dim; l++)

906 
coeffs[(l % real_ch) * samples + l / real_ch] = *a++; 
907 
} 
908 
} 
909 
} 
910 
} 
911 
} 
912 
} 
913 
} 
914  
915 
static int window(venc_context_t * venc, signed short * audio, int samples) { 
916 
int i, j, channel;

917 
const float * win = venc>win[0]; 
918 
int window_len = 1 << (venc>blocksize[0]  1); 
919 
float n = (float)(1 << venc>blocksize[0]) / 4.; 
920 
// FIXME use dsp

921  
922 
if (!venc>have_saved && !samples) return 0; 
923  
924 
if (venc>have_saved) {

925 
for (channel = 0; channel < venc>channels; channel++) { 
926 
memcpy(venc>samples + channel*window_len*2, venc>saved + channel*window_len, sizeof(float)*window_len); 
927 
} 
928 
} else {

929 
for (channel = 0; channel < venc>channels; channel++) { 
930 
memset(venc>samples + channel*window_len*2, 0, sizeof(float)*window_len); 
931 
} 
932 
} 
933  
934 
if (samples) {

935 
for (channel = 0; channel < venc>channels; channel++) { 
936 
float * offset = venc>samples + channel*window_len*2 + window_len; 
937 
j = channel; 
938 
for (i = 0; i < samples; i++, j += venc>channels) 
939 
offset[i] = audio[j] / 32768. / n * win[window_len  i  1]; 
940 
} 
941 
} else {

942 
for (channel = 0; channel < venc>channels; channel++) { 
943 
memset(venc>samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len); 
944 
} 
945 
} 
946  
947 
for (channel = 0; channel < venc>channels; channel++) { 
948 
ff_mdct_calc(&venc>mdct[0], venc>coeffs + channel*window_len, venc>samples + channel*window_len*2, venc>floor/*tmp*/); 
949 
} 
950  
951 
if (samples) {

952 
for (channel = 0; channel < venc>channels; channel++) { 
953 
float * offset = venc>saved + channel*window_len;

954 
j = channel; 
955 
for (i = 0; i < samples; i++, j += venc>channels) 
956 
offset[i] = audio[j] / 32768. / n * win[i]; 
957 
} 
958 
venc>have_saved = 1;

959 
} else {

960 
venc>have_saved = 0;

961 
} 
962 
return 1; 
963 
} 
964  
965 
static int vorbis_encode_init(AVCodecContext * avccontext) 
966 
{ 
967 
venc_context_t * venc = avccontext>priv_data; 
968  
969 
create_vorbis_context(venc, avccontext); 
970  
971 
//if (avccontext>flags & CODEC_FLAG_QSCALE) avccontext>global_quality / (float)FF_QP2LAMBDA); else avccontext>bit_rate;

972 
//if(avccontext>cutoff > 0) cfreq = avccontext>cutoff / 1000.0;

973  
974 
avccontext>extradata_size = put_main_header(venc, (uint8_t**)&avccontext>extradata); 
975  
976 
avccontext>frame_size = 1 << (venc>blocksize[0]  1); 
977  
978 
avccontext>coded_frame = avcodec_alloc_frame(); 
979 
avccontext>coded_frame>key_frame = 1;

980  
981 
return 0; 
982 
} 
983  
984 
static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data) 
985 
{ 
986 
venc_context_t * venc = avccontext>priv_data; 
987 
signed short * audio = data; 
988 
int samples = data ? avccontext>frame_size : 0; 
989 
vorbis_mode_t * mode; 
990 
mapping_t * mapping; 
991 
PutBitContext pb; 
992 
int i;

993  
994 
if (!window(venc, audio, samples)) return 0; 
995 
samples = 1 << (venc>blocksize[0]  1); 
996  
997 
init_put_bits(&pb, packets, buf_size); 
998  
999 
put_bits(&pb, 1, 0); // magic bit 
1000  
1001 
put_bits(&pb, ilog(venc>nmodes  1), 0); // 0 bits, the mode 
1002  
1003 
mode = &venc>modes[0];

1004 
mapping = &venc>mappings[mode>mapping]; 
1005 
if (mode>blockflag) {

1006 
put_bits(&pb, 1, 0); 
1007 
put_bits(&pb, 1, 0); 
1008 
} 
1009  
1010 
for (i = 0; i < venc>channels; i++) { 
1011 
floor_t * fc = &venc>floors[mapping>floor[mapping>mux[i]]]; 
1012 
int posts[fc>values];

1013 
floor_fit(venc, fc, &venc>coeffs[i * samples], posts, samples); 
1014 
floor_encode(venc, fc, &pb, posts, &venc>floor[i * samples], samples); 
1015 
} 
1016  
1017 
for (i = 0; i < venc>channels; i++) { 
1018 
int j;

1019 
for (j = 0; j < samples; j++) { 
1020 
venc>coeffs[i * samples + j] /= venc>floor[i * samples + j]; 
1021 
} 
1022 
} 
1023  
1024 
for (i = 0; i < mapping>coupling_steps; i++) { 
1025 
float * mag = venc>coeffs + mapping>magnitude[i] * samples;

1026 
float * ang = venc>coeffs + mapping>angle[i] * samples;

1027 
int j;

1028 
for (j = 0; j < samples; j++) { 
1029 
float m = mag[j];

1030 
float a = ang[j];

1031 
if (m > 0) { 
1032 
ang[j] = m  a; 
1033 
if (a > m) mag[j] = a;

1034 
else mag[j] = m;

1035 
} else {

1036 
ang[j] = a  m; 
1037 
if (a > m) mag[j] = m;

1038 
else mag[j] = a;

1039 
} 
1040 
} 
1041 
} 
1042  
1043 
residue_encode(venc, &venc>residues[mapping>residue[mapping>mux[0]]], &pb, venc>coeffs, samples, venc>channels);

1044  
1045 
return (put_bits_count(&pb) + 7) / 8; 
1046 
} 
1047  
1048  
1049 
static int vorbis_encode_close(AVCodecContext * avccontext) 
1050 
{ 
1051 
venc_context_t * venc = avccontext>priv_data; 
1052 
int i;

1053  
1054 
if (venc>codebooks) for (i = 0; i < venc>ncodebooks; i++) { 
1055 
av_freep(&venc>codebooks[i].entries); 
1056 
av_freep(&venc>codebooks[i].quantlist); 
1057 
av_freep(&venc>codebooks[i].dimentions); 
1058 
} 
1059 
av_freep(&venc>codebooks); 
1060  
1061 
if (venc>floors) for (i = 0; i < venc>nfloors; i++) { 
1062 
int j;

1063 
av_freep(&venc>floors[i].classes); 
1064 
if (venc>floors[i].classes)

1065 
for (j = 0; j < venc>floors[i].nclasses; j++) 
1066 
av_freep(&venc>floors[i].classes[j].books); 
1067 
av_freep(&venc>floors[i].partition_to_class); 
1068 
av_freep(&venc>floors[i].list); 
1069 
} 
1070 
av_freep(&venc>floors); 
1071  
1072 
if (venc>residues) for (i = 0; i < venc>nresidues; i++) { 
1073 
av_freep(&venc>residues[i].books); 
1074 
} 
1075 
av_freep(&venc>residues); 
1076  
1077 
if (venc>mappings) for (i = 0; i < venc>nmappings; i++) { 
1078 
av_freep(&venc>mappings[i].mux); 
1079 
av_freep(&venc>mappings[i].floor); 
1080 
av_freep(&venc>mappings[i].residue); 
1081 
} 
1082 
av_freep(&venc>mappings); 
1083  
1084 
av_freep(&venc>modes); 
1085  
1086 
av_freep(&venc>saved); 
1087 
av_freep(&venc>samples); 
1088 
av_freep(&venc>floor); 
1089 
av_freep(&venc>coeffs); 
1090  
1091 
ff_mdct_end(&venc>mdct[0]);

1092 
ff_mdct_end(&venc>mdct[1]);

1093  
1094 
av_freep(&avccontext>coded_frame); 
1095 
av_freep(&avccontext>extradata); 
1096  
1097 
return 0 ; 
1098 
} 
1099  
1100 
AVCodec vorbis_encoder = { 
1101 
"vorbis",

1102 
CODEC_TYPE_AUDIO, 
1103 
CODEC_ID_VORBIS, 
1104 
sizeof(venc_context_t),

1105 
vorbis_encode_init, 
1106 
vorbis_encode_frame, 
1107 
vorbis_encode_close, 
1108 
.capabilities= CODEC_CAP_DELAY, 
1109 
}; 