Mercurial > libavcodec.hg
comparison vorbis_enc.c @ 10316:7bfcc66f807d libavcodec
whitespace cosmetics, prettyprinting, K&R coding style
author | diego |
---|---|
date | Tue, 29 Sep 2009 11:43:58 +0000 |
parents | d3644248015a |
children | b10c3bc8ea3e |
comparison
equal
deleted
inserted
replaced
10315:27cb9c2c0c6b | 10316:7bfcc66f807d |
---|---|
36 #undef NDEBUG | 36 #undef NDEBUG |
37 #include <assert.h> | 37 #include <assert.h> |
38 | 38 |
39 typedef struct { | 39 typedef struct { |
40 int nentries; | 40 int nentries; |
41 uint8_t * lens; | 41 uint8_t *lens; |
42 uint32_t * codewords; | 42 uint32_t *codewords; |
43 int ndimentions; | 43 int ndimentions; |
44 float min; | 44 float min; |
45 float delta; | 45 float delta; |
46 int seq_p; | 46 int seq_p; |
47 int lookup; | 47 int lookup; |
48 int * quantlist; | 48 int *quantlist; |
49 float * dimentions; | 49 float *dimentions; |
50 float * pow2; | 50 float *pow2; |
51 } vorbis_enc_codebook; | 51 } vorbis_enc_codebook; |
52 | 52 |
53 typedef struct { | 53 typedef struct { |
54 int dim; | 54 int dim; |
55 int subclass; | 55 int subclass; |
56 int masterbook; | 56 int masterbook; |
57 int * books; | 57 int *books; |
58 } vorbis_enc_floor_class; | 58 } vorbis_enc_floor_class; |
59 | 59 |
60 typedef struct { | 60 typedef struct { |
61 int partitions; | 61 int partitions; |
62 int * partition_to_class; | 62 int *partition_to_class; |
63 int nclasses; | 63 int nclasses; |
64 vorbis_enc_floor_class * classes; | 64 vorbis_enc_floor_class *classes; |
65 int multiplier; | 65 int multiplier; |
66 int rangebits; | 66 int rangebits; |
67 int values; | 67 int values; |
68 vorbis_floor1_entry * list; | 68 vorbis_floor1_entry *list; |
69 } vorbis_enc_floor; | 69 } vorbis_enc_floor; |
70 | 70 |
71 typedef struct { | 71 typedef struct { |
72 int type; | 72 int type; |
73 int begin; | 73 int begin; |
79 float (*maxes)[2]; | 79 float (*maxes)[2]; |
80 } vorbis_enc_residue; | 80 } vorbis_enc_residue; |
81 | 81 |
82 typedef struct { | 82 typedef struct { |
83 int submaps; | 83 int submaps; |
84 int * mux; | 84 int *mux; |
85 int * floor; | 85 int *floor; |
86 int * residue; | 86 int *residue; |
87 int coupling_steps; | 87 int coupling_steps; |
88 int * magnitude; | 88 int *magnitude; |
89 int * angle; | 89 int *angle; |
90 } vorbis_enc_mapping; | 90 } vorbis_enc_mapping; |
91 | 91 |
92 typedef struct { | 92 typedef struct { |
93 int blockflag; | 93 int blockflag; |
94 int mapping; | 94 int mapping; |
97 typedef struct { | 97 typedef struct { |
98 int channels; | 98 int channels; |
99 int sample_rate; | 99 int sample_rate; |
100 int log2_blocksize[2]; | 100 int log2_blocksize[2]; |
101 FFTContext mdct[2]; | 101 FFTContext mdct[2]; |
102 const float * win[2]; | 102 const float *win[2]; |
103 int have_saved; | 103 int have_saved; |
104 float * saved; | 104 float *saved; |
105 float * samples; | 105 float *samples; |
106 float * floor; // also used for tmp values for mdct | 106 float *floor; // also used for tmp values for mdct |
107 float * coeffs; // also used for residue after floor | 107 float *coeffs; // also used for residue after floor |
108 float quality; | 108 float quality; |
109 | 109 |
110 int ncodebooks; | 110 int ncodebooks; |
111 vorbis_enc_codebook * codebooks; | 111 vorbis_enc_codebook *codebooks; |
112 | 112 |
113 int nfloors; | 113 int nfloors; |
114 vorbis_enc_floor * floors; | 114 vorbis_enc_floor *floors; |
115 | 115 |
116 int nresidues; | 116 int nresidues; |
117 vorbis_enc_residue * residues; | 117 vorbis_enc_residue *residues; |
118 | 118 |
119 int nmappings; | 119 int nmappings; |
120 vorbis_enc_mapping * mappings; | 120 vorbis_enc_mapping *mappings; |
121 | 121 |
122 int nmodes; | 122 int nmodes; |
123 vorbis_enc_mode * modes; | 123 vorbis_enc_mode *modes; |
124 | 124 |
125 int64_t sample_count; | 125 int64_t sample_count; |
126 } vorbis_enc_context; | 126 } vorbis_enc_context; |
127 | 127 |
128 static inline void put_codeword(PutBitContext * pb, vorbis_enc_codebook * cb, | 128 static inline void put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb, |
129 int entry) | 129 int entry) |
130 { | 130 { |
131 assert(entry >= 0); | 131 assert(entry >= 0); |
132 assert(entry < cb->nentries); | 132 assert(entry < cb->nentries); |
133 assert(cb->lens[entry]); | 133 assert(cb->lens[entry]); |
137 static int cb_lookup_vals(int lookup, int dimentions, int entries) | 137 static int cb_lookup_vals(int lookup, int dimentions, int entries) |
138 { | 138 { |
139 if (lookup == 1) | 139 if (lookup == 1) |
140 return ff_vorbis_nth_root(entries, dimentions); | 140 return ff_vorbis_nth_root(entries, dimentions); |
141 else if (lookup == 2) | 141 else if (lookup == 2) |
142 return dimentions * entries; | 142 return dimentions *entries; |
143 return 0; | 143 return 0; |
144 } | 144 } |
145 | 145 |
146 static void ready_codebook(vorbis_enc_codebook * cb) | 146 static void ready_codebook(vorbis_enc_codebook *cb) |
147 { | 147 { |
148 int i; | 148 int i; |
149 | 149 |
150 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries); | 150 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries); |
151 | 151 |
152 if (!cb->lookup) | 152 if (!cb->lookup) { |
153 cb->pow2 = cb->dimentions = NULL; | 153 cb->pow2 = cb->dimentions = NULL; |
154 else { | 154 } else { |
155 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); | 155 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); |
156 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions); | 156 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions); |
157 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries); | 157 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries); |
158 for (i = 0; i < cb->nentries; i++) { | 158 for (i = 0; i < cb->nentries; i++) { |
159 float last = 0; | 159 float last = 0; |
167 off = i * cb->ndimentions + j; // lookup type 2 | 167 off = i * cb->ndimentions + j; // lookup type 2 |
168 | 168 |
169 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta; | 169 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta; |
170 if (cb->seq_p) | 170 if (cb->seq_p) |
171 last = cb->dimentions[i * cb->ndimentions + j]; | 171 last = cb->dimentions[i * cb->ndimentions + j]; |
172 cb->pow2[i] += cb->dimentions[i * cb->ndimentions + j]*cb->dimentions[i * cb->ndimentions + j]; | 172 cb->pow2[i] += cb->dimentions[i * cb->ndimentions + j] * cb->dimentions[i * cb->ndimentions + j]; |
173 div *= vals; | 173 div *= vals; |
174 } | 174 } |
175 cb->pow2[i] /= 2.; | 175 cb->pow2[i] /= 2.; |
176 } | 176 } |
177 } | 177 } |
178 } | 178 } |
179 | 179 |
180 static void ready_residue(vorbis_enc_residue * rc, vorbis_enc_context * venc) | 180 static void ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc) |
181 { | 181 { |
182 int i; | 182 int i; |
183 assert(rc->type == 2); | 183 assert(rc->type == 2); |
184 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications); | 184 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications); |
185 for (i = 0; i < rc->classifications; i++) { | 185 for (i = 0; i < rc->classifications; i++) { |
186 int j; | 186 int j; |
187 vorbis_enc_codebook * cb; | 187 vorbis_enc_codebook * cb; |
188 for (j = 0; j < 8; j++) | 188 for (j = 0; j < 8; j++) |
189 if (rc->books[i][j] != -1) | 189 if (rc->books[i][j] != -1) |
190 break; | 190 break; |
191 if (j == 8) | 191 if (j == 8) // zero |
192 continue; // zero | 192 continue; |
193 cb = &venc->codebooks[rc->books[i][j]]; | 193 cb = &venc->codebooks[rc->books[i][j]]; |
194 assert(cb->ndimentions >= 2); | 194 assert(cb->ndimentions >= 2); |
195 assert(cb->lookup); | 195 assert(cb->lookup); |
196 | 196 |
197 for (j = 0; j < cb->nentries; j++) { | 197 for (j = 0; j < cb->nentries; j++) { |
211 rc->maxes[i][0] += 0.8; | 211 rc->maxes[i][0] += 0.8; |
212 rc->maxes[i][1] += 0.8; | 212 rc->maxes[i][1] += 0.8; |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 static void create_vorbis_context(vorbis_enc_context * venc, | 216 static void create_vorbis_context(vorbis_enc_context *venc, |
217 AVCodecContext * avccontext) | 217 AVCodecContext *avccontext) |
218 { | 218 { |
219 vorbis_enc_floor * fc; | 219 vorbis_enc_floor *fc; |
220 vorbis_enc_residue * rc; | 220 vorbis_enc_residue *rc; |
221 vorbis_enc_mapping * mc; | 221 vorbis_enc_mapping *mc; |
222 int i, book; | 222 int i, book; |
223 | 223 |
224 venc->channels = avccontext->channels; | 224 venc->channels = avccontext->channels; |
225 venc->sample_rate = avccontext->sample_rate; | 225 venc->sample_rate = avccontext->sample_rate; |
226 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11; | 226 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11; |
227 | 227 |
228 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors); | 228 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors); |
229 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks); | 229 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks); |
230 | 230 |
231 // codebook 0..14 - floor1 book, values 0..255 | 231 // codebook 0..14 - floor1 book, values 0..255 |
232 // codebook 15 residue masterbook | 232 // codebook 15 residue masterbook |
233 // codebook 16..29 residue | 233 // codebook 16..29 residue |
234 for (book = 0; book < venc->ncodebooks; book++) { | 234 for (book = 0; book < venc->ncodebooks; book++) { |
235 vorbis_enc_codebook * cb = &venc->codebooks[book]; | 235 vorbis_enc_codebook *cb = &venc->codebooks[book]; |
236 int vals; | 236 int vals; |
237 cb->ndimentions = cvectors[book].dim; | 237 cb->ndimentions = cvectors[book].dim; |
238 cb->nentries = cvectors[book].real_len; | 238 cb->nentries = cvectors[book].real_len; |
239 cb->min = cvectors[book].min; | 239 cb->min = cvectors[book].min; |
240 cb->delta = cvectors[book].delta; | 240 cb->delta = cvectors[book].delta; |
241 cb->lookup = cvectors[book].lookup; | 241 cb->lookup = cvectors[book].lookup; |
242 cb->seq_p = 0; | 242 cb->seq_p = 0; |
243 | 243 |
244 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries); | 244 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries); |
245 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries); | 245 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries); |
246 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len); | 246 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len); |
247 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len); | 247 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len); |
248 | 248 |
249 if (cb->lookup) { | 249 if (cb->lookup) { |
256 } | 256 } |
257 ready_codebook(cb); | 257 ready_codebook(cb); |
258 } | 258 } |
259 | 259 |
260 venc->nfloors = 1; | 260 venc->nfloors = 1; |
261 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors); | 261 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors); |
262 | 262 |
263 // just 1 floor | 263 // just 1 floor |
264 fc = &venc->floors[0]; | 264 fc = &venc->floors[0]; |
265 fc->partitions = 8; | 265 fc->partitions = 8; |
266 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions); | 266 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions); |
267 fc->nclasses = 0; | 267 fc->nclasses = 0; |
268 for (i = 0; i < fc->partitions; i++) { | 268 for (i = 0; i < fc->partitions; i++) { |
269 static const int a[] = {0,1,2,2,3,3,4,4}; | 269 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4}; |
270 fc->partition_to_class[i] = a[i]; | 270 fc->partition_to_class[i] = a[i]; |
271 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]); | 271 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]); |
272 } | 272 } |
273 fc->nclasses++; | 273 fc->nclasses++; |
274 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses); | 274 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses); |
275 for (i = 0; i < fc->nclasses; i++) { | 275 for (i = 0; i < fc->nclasses; i++) { |
276 vorbis_enc_floor_class * c = &fc->classes[i]; | 276 vorbis_enc_floor_class * c = &fc->classes[i]; |
277 int j, books; | 277 int j, books; |
278 c->dim = floor_classes[i].dim; | 278 c->dim = floor_classes[i].dim; |
279 c->subclass = floor_classes[i].subclass; | 279 c->subclass = floor_classes[i].subclass; |
280 c->masterbook = floor_classes[i].masterbook; | 280 c->masterbook = floor_classes[i].masterbook; |
281 books = (1 << c->subclass); | 281 books = (1 << c->subclass); |
282 c->books = av_malloc(sizeof(int) * books); | 282 c->books = av_malloc(sizeof(int) * books); |
283 for (j = 0; j < books; j++) | 283 for (j = 0; j < books; j++) |
284 c->books[j] = floor_classes[i].nbooks[j]; | 284 c->books[j] = floor_classes[i].nbooks[j]; |
285 } | 285 } |
286 fc->multiplier = 2; | 286 fc->multiplier = 2; |
287 fc->rangebits = venc->log2_blocksize[0] - 1; | 287 fc->rangebits = venc->log2_blocksize[0] - 1; |
288 | 288 |
289 fc->values = 2; | 289 fc->values = 2; |
290 for (i = 0; i < fc->partitions; i++) | 290 for (i = 0; i < fc->partitions; i++) |
291 fc->values += fc->classes[fc->partition_to_class[i]].dim; | 291 fc->values += fc->classes[fc->partition_to_class[i]].dim; |
292 | 292 |
302 fc->list[i].x = a[i - 2]; | 302 fc->list[i].x = a[i - 2]; |
303 } | 303 } |
304 ff_vorbis_ready_floor1_list(fc->list, fc->values); | 304 ff_vorbis_ready_floor1_list(fc->list, fc->values); |
305 | 305 |
306 venc->nresidues = 1; | 306 venc->nresidues = 1; |
307 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues); | 307 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues); |
308 | 308 |
309 // single residue | 309 // single residue |
310 rc = &venc->residues[0]; | 310 rc = &venc->residues[0]; |
311 rc->type = 2; | 311 rc->type = 2; |
312 rc->begin = 0; | 312 rc->begin = 0; |
313 rc->end = 1600; | 313 rc->end = 1600; |
314 rc->partition_size = 32; | 314 rc->partition_size = 32; |
315 rc->classifications = 10; | 315 rc->classifications = 10; |
316 rc->classbook = 15; | 316 rc->classbook = 15; |
317 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications); | 317 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications); |
318 { | 318 { |
319 static const int8_t a[10][8] = { | 319 static const int8_t a[10][8] = { |
320 { -1, -1, -1, -1, -1, -1, -1, -1, }, | 320 { -1, -1, -1, -1, -1, -1, -1, -1, }, |
321 { -1, -1, 16, -1, -1, -1, -1, -1, }, | 321 { -1, -1, 16, -1, -1, -1, -1, -1, }, |
322 { -1, -1, 17, -1, -1, -1, -1, -1, }, | 322 { -1, -1, 17, -1, -1, -1, -1, -1, }, |
331 memcpy(rc->books, a, sizeof a); | 331 memcpy(rc->books, a, sizeof a); |
332 } | 332 } |
333 ready_residue(rc, venc); | 333 ready_residue(rc, venc); |
334 | 334 |
335 venc->nmappings = 1; | 335 venc->nmappings = 1; |
336 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings); | 336 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings); |
337 | 337 |
338 // single mapping | 338 // single mapping |
339 mc = &venc->mappings[0]; | 339 mc = &venc->mappings[0]; |
340 mc->submaps = 1; | 340 mc->submaps = 1; |
341 mc->mux = av_malloc(sizeof(int) * venc->channels); | 341 mc->mux = av_malloc(sizeof(int) * venc->channels); |
342 for (i = 0; i < venc->channels; i++) | 342 for (i = 0; i < venc->channels; i++) |
343 mc->mux[i] = 0; | 343 mc->mux[i] = 0; |
344 mc->floor = av_malloc(sizeof(int) * mc->submaps); | 344 mc->floor = av_malloc(sizeof(int) * mc->submaps); |
345 mc->residue = av_malloc(sizeof(int) * mc->submaps); | 345 mc->residue = av_malloc(sizeof(int) * mc->submaps); |
346 for (i = 0; i < mc->submaps; i++) { | 346 for (i = 0; i < mc->submaps; i++) { |
347 mc->floor[i] = 0; | 347 mc->floor[i] = 0; |
348 mc->residue[i] = 0; | 348 mc->residue[i] = 0; |
349 } | 349 } |
350 mc->coupling_steps = venc->channels == 2 ? 1 : 0; | 350 mc->coupling_steps = venc->channels == 2 ? 1 : 0; |
351 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps); | 351 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps); |
352 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps); | 352 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps); |
353 if (mc->coupling_steps) { | 353 if (mc->coupling_steps) { |
354 mc->magnitude[0] = 0; | 354 mc->magnitude[0] = 0; |
355 mc->angle[0] = 1; | 355 mc->angle[0] = 1; |
356 } | 356 } |
357 | 357 |
358 venc->nmodes = 1; | 358 venc->nmodes = 1; |
359 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes); | 359 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes); |
360 | 360 |
361 // single mode | 361 // single mode |
362 venc->modes[0].blockflag = 0; | 362 venc->modes[0].blockflag = 0; |
363 venc->modes[0].mapping = 0; | 363 venc->modes[0].mapping = 0; |
364 | 364 |
365 venc->have_saved = 0; | 365 venc->have_saved = 0; |
366 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); | 366 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); |
367 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1])); | 367 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1])); |
368 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); | 368 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); |
369 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); | 369 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); |
370 | 370 |
371 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6]; | 371 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6]; |
372 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6]; | 372 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6]; |
373 | 373 |
374 ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0); | 374 ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0); |
375 ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0); | 375 ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0); |
376 } | 376 } |
377 | 377 |
378 static void put_float(PutBitContext * pb, float f) | 378 static void put_float(PutBitContext *pb, float f) |
379 { | 379 { |
380 int exp, mant; | 380 int exp, mant; |
381 uint32_t res = 0; | 381 uint32_t res = 0; |
382 mant = (int)ldexp(frexp(f, &exp), 20); | 382 mant = (int)ldexp(frexp(f, &exp), 20); |
383 exp += 788 - 20; | 383 exp += 788 - 20; |
387 } | 387 } |
388 res |= mant | (exp << 21); | 388 res |= mant | (exp << 21); |
389 put_bits(pb, 32, res); | 389 put_bits(pb, 32, res); |
390 } | 390 } |
391 | 391 |
392 static void put_codebook_header(PutBitContext * pb, vorbis_enc_codebook * cb) | 392 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb) |
393 { | 393 { |
394 int i; | 394 int i; |
395 int ordered = 0; | 395 int ordered = 0; |
396 | 396 |
397 put_bits(pb, 24, 0x564342); //magic | 397 put_bits(pb, 24, 0x564342); //magic |
435 } | 435 } |
436 } | 436 } |
437 | 437 |
438 put_bits(pb, 4, cb->lookup); | 438 put_bits(pb, 4, cb->lookup); |
439 if (cb->lookup) { | 439 if (cb->lookup) { |
440 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); | 440 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); |
441 int bits = ilog(cb->quantlist[0]); | 441 int bits = ilog(cb->quantlist[0]); |
442 | 442 |
443 for (i = 1; i < tmp; i++) | 443 for (i = 1; i < tmp; i++) |
444 bits = FFMAX(bits, ilog(cb->quantlist[i])); | 444 bits = FFMAX(bits, ilog(cb->quantlist[i])); |
445 | 445 |
452 for (i = 0; i < tmp; i++) | 452 for (i = 0; i < tmp; i++) |
453 put_bits(pb, bits, cb->quantlist[i]); | 453 put_bits(pb, bits, cb->quantlist[i]); |
454 } | 454 } |
455 } | 455 } |
456 | 456 |
457 static void put_floor_header(PutBitContext * pb, vorbis_enc_floor * fc) | 457 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc) |
458 { | 458 { |
459 int i; | 459 int i; |
460 | 460 |
461 put_bits(pb, 16, 1); // type, only floor1 is supported | 461 put_bits(pb, 16, 1); // type, only floor1 is supported |
462 | 462 |
485 | 485 |
486 for (i = 2; i < fc->values; i++) | 486 for (i = 2; i < fc->values; i++) |
487 put_bits(pb, fc->rangebits, fc->list[i].x); | 487 put_bits(pb, fc->rangebits, fc->list[i].x); |
488 } | 488 } |
489 | 489 |
490 static void put_residue_header(PutBitContext * pb, vorbis_enc_residue * rc) | 490 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc) |
491 { | 491 { |
492 int i; | 492 int i; |
493 | 493 |
494 put_bits(pb, 16, rc->type); | 494 put_bits(pb, 16, rc->type); |
495 | 495 |
517 if (rc->books[i][j] != -1) | 517 if (rc->books[i][j] != -1) |
518 put_bits(pb, 8, rc->books[i][j]); | 518 put_bits(pb, 8, rc->books[i][j]); |
519 } | 519 } |
520 } | 520 } |
521 | 521 |
522 static int put_main_header(vorbis_enc_context * venc, uint8_t ** out) | 522 static int put_main_header(vorbis_enc_context *venc, uint8_t **out) |
523 { | 523 { |
524 int i; | 524 int i; |
525 PutBitContext pb; | 525 PutBitContext pb; |
526 uint8_t buffer[50000] = {0}, * p = buffer; | 526 uint8_t buffer[50000] = {0}, *p = buffer; |
527 int buffer_len = sizeof buffer; | 527 int buffer_len = sizeof buffer; |
528 int len, hlens[3]; | 528 int len, hlens[3]; |
529 | 529 |
530 // identification header | 530 // identification header |
531 init_put_bits(&pb, p, buffer_len); | 531 init_put_bits(&pb, p, buffer_len); |
532 put_bits(&pb, 8, 1); //magic | 532 put_bits(&pb, 8, 1); //magic |
533 for (i = 0; "vorbis"[i]; i++) | 533 for (i = 0; "vorbis"[i]; i++) |
534 put_bits(&pb, 8, "vorbis"[i]); | 534 put_bits(&pb, 8, "vorbis"[i]); |
535 put_bits(&pb, 32, 0); // version | 535 put_bits(&pb, 32, 0); // version |
536 put_bits(&pb, 8, venc->channels); | 536 put_bits(&pb, 8, venc->channels); |
537 put_bits(&pb, 32, venc->sample_rate); | 537 put_bits(&pb, 32, venc->sample_rate); |
538 put_bits(&pb, 32, 0); // bitrate | 538 put_bits(&pb, 32, 0); // bitrate |
539 put_bits(&pb, 32, 0); // bitrate | 539 put_bits(&pb, 32, 0); // bitrate |
540 put_bits(&pb, 32, 0); // bitrate | 540 put_bits(&pb, 32, 0); // bitrate |
541 put_bits(&pb, 4, venc->log2_blocksize[0]); | 541 put_bits(&pb, 4, venc->log2_blocksize[0]); |
542 put_bits(&pb, 4, venc->log2_blocksize[1]); | 542 put_bits(&pb, 4, venc->log2_blocksize[1]); |
543 put_bits(&pb, 1, 1); // framing | 543 put_bits(&pb, 1, 1); // framing |
544 | 544 |
545 flush_put_bits(&pb); | 545 flush_put_bits(&pb); |
546 hlens[0] = (put_bits_count(&pb) + 7) / 8; | 546 hlens[0] = (put_bits_count(&pb) + 7) / 8; |
547 buffer_len -= hlens[0]; | 547 buffer_len -= hlens[0]; |
548 p += hlens[0]; | 548 p += hlens[0]; |
552 put_bits(&pb, 8, 3); //magic | 552 put_bits(&pb, 8, 3); //magic |
553 for (i = 0; "vorbis"[i]; i++) | 553 for (i = 0; "vorbis"[i]; i++) |
554 put_bits(&pb, 8, "vorbis"[i]); | 554 put_bits(&pb, 8, "vorbis"[i]); |
555 put_bits(&pb, 32, 0); // vendor length TODO | 555 put_bits(&pb, 32, 0); // vendor length TODO |
556 put_bits(&pb, 32, 0); // amount of comments | 556 put_bits(&pb, 32, 0); // amount of comments |
557 put_bits(&pb, 1, 1); // framing | 557 put_bits(&pb, 1, 1); // framing |
558 | 558 |
559 flush_put_bits(&pb); | 559 flush_put_bits(&pb); |
560 hlens[1] = (put_bits_count(&pb) + 7) / 8; | 560 hlens[1] = (put_bits_count(&pb) + 7) / 8; |
561 buffer_len -= hlens[1]; | 561 buffer_len -= hlens[1]; |
562 p += hlens[1]; | 562 p += hlens[1]; |
571 put_bits(&pb, 8, venc->ncodebooks - 1); | 571 put_bits(&pb, 8, venc->ncodebooks - 1); |
572 for (i = 0; i < venc->ncodebooks; i++) | 572 for (i = 0; i < venc->ncodebooks; i++) |
573 put_codebook_header(&pb, &venc->codebooks[i]); | 573 put_codebook_header(&pb, &venc->codebooks[i]); |
574 | 574 |
575 // time domain, reserved, zero | 575 // time domain, reserved, zero |
576 put_bits(&pb, 6, 0); | 576 put_bits(&pb, 6, 0); |
577 put_bits(&pb, 16, 0); | 577 put_bits(&pb, 16, 0); |
578 | 578 |
579 // floors | 579 // floors |
580 put_bits(&pb, 6, venc->nfloors - 1); | 580 put_bits(&pb, 6, venc->nfloors - 1); |
581 for (i = 0; i < venc->nfloors; i++) | 581 for (i = 0; i < venc->nfloors; i++) |
587 put_residue_header(&pb, &venc->residues[i]); | 587 put_residue_header(&pb, &venc->residues[i]); |
588 | 588 |
589 // mappings | 589 // mappings |
590 put_bits(&pb, 6, venc->nmappings - 1); | 590 put_bits(&pb, 6, venc->nmappings - 1); |
591 for (i = 0; i < venc->nmappings; i++) { | 591 for (i = 0; i < venc->nmappings; i++) { |
592 vorbis_enc_mapping * mc = &venc->mappings[i]; | 592 vorbis_enc_mapping *mc = &venc->mappings[i]; |
593 int j; | 593 int j; |
594 put_bits(&pb, 16, 0); // mapping type | 594 put_bits(&pb, 16, 0); // mapping type |
595 | 595 |
596 put_bits(&pb, 1, mc->submaps > 1); | 596 put_bits(&pb, 1, mc->submaps > 1); |
597 if (mc->submaps > 1) | 597 if (mc->submaps > 1) |
647 } | 647 } |
648 | 648 |
649 return p - *out; | 649 return p - *out; |
650 } | 650 } |
651 | 651 |
652 static float get_floor_average(vorbis_enc_floor * fc, float * coeffs, int i) | 652 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i) |
653 { | 653 { |
654 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x; | 654 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x; |
655 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x; | 655 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x; |
656 int j; | 656 int j; |
657 float average = 0; | 657 float average = 0; |
659 for (j = begin; j < end; j++) | 659 for (j = begin; j < end; j++) |
660 average += fabs(coeffs[j]); | 660 average += fabs(coeffs[j]); |
661 return average / (end - begin); | 661 return average / (end - begin); |
662 } | 662 } |
663 | 663 |
664 static void floor_fit(vorbis_enc_context * venc, vorbis_enc_floor * fc, | 664 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc, |
665 float * coeffs, uint_fast16_t * posts, int samples) | 665 float *coeffs, uint_fast16_t *posts, int samples) |
666 { | 666 { |
667 int range = 255 / fc->multiplier + 1; | 667 int range = 255 / fc->multiplier + 1; |
668 int i; | 668 int i; |
669 float tot_average = 0.; | 669 float tot_average = 0.; |
670 float averages[fc->values]; | 670 float averages[fc->values]; |
671 for (i = 0; i < fc->values; i++){ | 671 for (i = 0; i < fc->values; i++) { |
672 averages[i] = get_floor_average(fc, coeffs, i); | 672 averages[i] = get_floor_average(fc, coeffs, i); |
673 tot_average += averages[i]; | 673 tot_average += averages[i]; |
674 } | 674 } |
675 tot_average /= fc->values; | 675 tot_average /= fc->values; |
676 tot_average /= venc->quality; | 676 tot_average /= venc->quality; |
677 | 677 |
678 for (i = 0; i < fc->values; i++) { | 678 for (i = 0; i < fc->values; i++) { |
679 int position = fc->list[fc->list[i].sort].x; | 679 int position = fc->list[fc->list[i].sort].x; |
680 float average = averages[i]; | 680 float average = averages[i]; |
681 int j; | 681 int j; |
682 | 682 |
683 average *= pow(tot_average / average, 0.5) * pow(1.25, position/200.); // MAGIC! | 683 average *= pow(tot_average / average, 0.5) * pow(1.25, position/200.); // MAGIC! |
684 for (j = 0; j < range - 1; j++) | 684 for (j = 0; j < range - 1; j++) |
691 static int render_point(int x0, int y0, int x1, int y1, int x) | 691 static int render_point(int x0, int y0, int x1, int y1, int x) |
692 { | 692 { |
693 return y0 + (x - x0) * (y1 - y0) / (x1 - x0); | 693 return y0 + (x - x0) * (y1 - y0) / (x1 - x0); |
694 } | 694 } |
695 | 695 |
696 static void floor_encode(vorbis_enc_context * venc, vorbis_enc_floor * fc, | 696 static void floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc, |
697 PutBitContext * pb, uint_fast16_t * posts, | 697 PutBitContext *pb, uint_fast16_t *posts, |
698 float * floor, int samples) | 698 float *floor, int samples) |
699 { | 699 { |
700 int range = 255 / fc->multiplier + 1; | 700 int range = 255 / fc->multiplier + 1; |
701 int coded[fc->values]; // first 2 values are unused | 701 int coded[fc->values]; // first 2 values are unused |
702 int i, counter; | 702 int i, counter; |
703 | 703 |
753 // coded could be -1, but this still works, cause that is 0 | 753 // coded could be -1, but this still works, cause that is 0 |
754 if (coded[counter + k] < maxval) | 754 if (coded[counter + k] < maxval) |
755 break; | 755 break; |
756 } | 756 } |
757 assert(l != csub); | 757 assert(l != csub); |
758 cval |= l << cshift; | 758 cval |= l << cshift; |
759 cshift += c->subclass; | 759 cshift += c->subclass; |
760 } | 760 } |
761 put_codeword(pb, book, cval); | 761 put_codeword(pb, book, cval); |
762 } | 762 } |
763 for (k = 0; k < c->dim; k++) { | 763 for (k = 0; k < c->dim; k++) { |
764 int book = c->books[cval & (csub-1)]; | 764 int book = c->books[cval & (csub-1)]; |
765 int entry = coded[counter++]; | 765 int entry = coded[counter++]; |
766 cval >>= c->subclass; | 766 cval >>= c->subclass; |
767 if (book == -1) | 767 if (book == -1) |
768 continue; | 768 continue; |
769 if (entry == -1) | 769 if (entry == -1) |
770 entry = 0; | 770 entry = 0; |
771 put_codeword(pb, &venc->codebooks[book], entry); | 771 put_codeword(pb, &venc->codebooks[book], entry); |
772 } | 772 } |
773 } | 773 } |
774 | 774 |
775 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded, fc->multiplier, floor, samples); | 775 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded, |
776 } | 776 fc->multiplier, floor, samples); |
777 | 777 } |
778 static float * put_vector(vorbis_enc_codebook * book, PutBitContext * pb, float * num) | 778 |
779 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb, | |
780 float *num) | |
779 { | 781 { |
780 int i, entry = -1; | 782 int i, entry = -1; |
781 float distance = FLT_MAX; | 783 float distance = FLT_MAX; |
782 assert(book->dimentions); | 784 assert(book->dimentions); |
783 for (i = 0; i < book->nentries; i++) { | 785 for (i = 0; i < book->nentries; i++) { |
786 if (!book->lens[i]) | 788 if (!book->lens[i]) |
787 continue; | 789 continue; |
788 for (j = 0; j < book->ndimentions; j++) | 790 for (j = 0; j < book->ndimentions; j++) |
789 d -= vec[j] * num[j]; | 791 d -= vec[j] * num[j]; |
790 if (distance > d) { | 792 if (distance > d) { |
791 entry = i; | 793 entry = i; |
792 distance = d; | 794 distance = d; |
793 } | 795 } |
794 } | 796 } |
795 put_codeword(pb, book, entry); | 797 put_codeword(pb, book, entry); |
796 return &book->dimentions[entry * book->ndimentions]; | 798 return &book->dimentions[entry * book->ndimentions]; |
797 } | 799 } |
798 | 800 |
799 static void residue_encode(vorbis_enc_context * venc, vorbis_enc_residue * rc, | 801 static void residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc, |
800 PutBitContext * pb, float * coeffs, int samples, | 802 PutBitContext *pb, float *coeffs, int samples, |
801 int real_ch) | 803 int real_ch) |
802 { | 804 { |
803 int pass, i, j, p, k; | 805 int pass, i, j, p, k; |
804 int psize = rc->partition_size; | 806 int psize = rc->partition_size; |
805 int partitions = (rc->end - rc->begin) / psize; | 807 int partitions = (rc->end - rc->begin) / psize; |
806 int channels = (rc->type == 2) ? 1 : real_ch; | 808 int channels = (rc->type == 2) ? 1 : real_ch; |
807 int classes[channels][partitions]; | 809 int classes[channels][partitions]; |
808 int classwords = venc->codebooks[rc->classbook].ndimentions; | 810 int classwords = venc->codebooks[rc->classbook].ndimentions; |
809 | 811 |
810 assert(rc->type == 2); | 812 assert(rc->type == 2); |
811 assert(real_ch == 2); | 813 assert(real_ch == 2); |
838 } | 840 } |
839 for (i = 0; i < classwords && p < partitions; i++, p++) { | 841 for (i = 0; i < classwords && p < partitions; i++, p++) { |
840 for (j = 0; j < channels; j++) { | 842 for (j = 0; j < channels; j++) { |
841 int nbook = rc->books[classes[j][p]][pass]; | 843 int nbook = rc->books[classes[j][p]][pass]; |
842 vorbis_enc_codebook * book = &venc->codebooks[nbook]; | 844 vorbis_enc_codebook * book = &venc->codebooks[nbook]; |
843 float * buf = coeffs + samples*j + rc->begin + p*psize; | 845 float *buf = coeffs + samples*j + rc->begin + p*psize; |
844 if (nbook == -1) | 846 if (nbook == -1) |
845 continue; | 847 continue; |
846 | 848 |
847 assert(rc->type == 0 || rc->type == 2); | 849 assert(rc->type == 0 || rc->type == 2); |
848 assert(!(psize % book->ndimentions)); | 850 assert(!(psize % book->ndimentions)); |
849 | 851 |
850 if (rc->type == 0) { | 852 if (rc->type == 0) { |
851 for (k = 0; k < psize; k += book->ndimentions) { | 853 for (k = 0; k < psize; k += book->ndimentions) { |
852 float * a = put_vector(book, pb, &buf[k]); | 854 float *a = put_vector(book, pb, &buf[k]); |
853 int l; | 855 int l; |
854 for (l = 0; l < book->ndimentions; l++) | 856 for (l = 0; l < book->ndimentions; l++) |
855 buf[k + l] -= a[l]; | 857 buf[k + l] -= a[l]; |
856 } | 858 } |
857 } else { | 859 } else { |
858 int s = rc->begin + p * psize, a1, b1; | 860 int s = rc->begin + p * psize, a1, b1; |
859 a1 = (s % real_ch) * samples; | 861 a1 = (s % real_ch) * samples; |
860 b1 = s / real_ch; | 862 b1 = s / real_ch; |
861 s = real_ch * samples; | 863 s = real_ch * samples; |
862 for (k = 0; k < psize; k += book->ndimentions) { | 864 for (k = 0; k < psize; k += book->ndimentions) { |
863 int dim, a2 = a1, b2 = b1; | 865 int dim, a2 = a1, b2 = b1; |
864 float vec[book->ndimentions], * pv = vec; | 866 float vec[book->ndimentions], *pv = vec; |
865 for (dim = book->ndimentions; dim--; ) { | 867 for (dim = book->ndimentions; dim--; ) { |
866 *pv++ = coeffs[a2 + b2]; | 868 *pv++ = coeffs[a2 + b2]; |
867 if ((a2 += samples) == s) { | 869 if ((a2 += samples) == s) { |
868 a2=0; | 870 a2 = 0; |
869 b2++; | 871 b2++; |
870 } | 872 } |
871 } | 873 } |
872 pv = put_vector(book, pb, vec); | 874 pv = put_vector(book, pb, vec); |
873 for (dim = book->ndimentions; dim--; ) { | 875 for (dim = book->ndimentions; dim--; ) { |
874 coeffs[a1 + b1] -= *pv++; | 876 coeffs[a1 + b1] -= *pv++; |
875 if ((a1 += samples) == s) { | 877 if ((a1 += samples) == s) { |
876 a1=0; | 878 a1 = 0; |
877 b1++; | 879 b1++; |
878 } | 880 } |
879 } | 881 } |
880 } | 882 } |
881 } | 883 } |
883 } | 885 } |
884 } | 886 } |
885 } | 887 } |
886 } | 888 } |
887 | 889 |
888 static int apply_window_and_mdct(vorbis_enc_context * venc, | 890 static int apply_window_and_mdct(vorbis_enc_context *venc, signed short *audio, |
889 signed short * audio, int samples) | 891 int samples) |
890 { | 892 { |
891 int i, j, channel; | 893 int i, j, channel; |
892 const float * win = venc->win[0]; | 894 const float * win = venc->win[0]; |
893 int window_len = 1 << (venc->log2_blocksize[0] - 1); | 895 int window_len = 1 << (venc->log2_blocksize[0] - 1); |
894 float n = (float)(1 << venc->log2_blocksize[0]) / 4.; | 896 float n = (float)(1 << venc->log2_blocksize[0]) / 4.; |
895 // FIXME use dsp | 897 // FIXME use dsp |
896 | 898 |
897 if (!venc->have_saved && !samples) | 899 if (!venc->have_saved && !samples) |
898 return 0; | 900 return 0; |
899 | 901 |
900 if (venc->have_saved) | 902 if (venc->have_saved) { |
901 for (channel = 0; channel < venc->channels; channel++) | 903 for (channel = 0; channel < venc->channels; channel++) |
902 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len); | 904 memcpy(venc->samples + channel * window_len * 2, |
903 else | 905 venc->saved + channel * window_len, sizeof(float) * window_len); |
906 } else { | |
904 for (channel = 0; channel < venc->channels; channel++) | 907 for (channel = 0; channel < venc->channels; channel++) |
905 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len); | 908 memset(venc->samples + channel * window_len * 2, 0, |
909 sizeof(float) * window_len); | |
910 } | |
906 | 911 |
907 if (samples) { | 912 if (samples) { |
908 for (channel = 0; channel < venc->channels; channel++) { | 913 for (channel = 0; channel < venc->channels; channel++) { |
909 float * offset = venc->samples + channel*window_len*2 + window_len; | 914 float * offset = venc->samples + channel*window_len*2 + window_len; |
910 j = channel; | 915 j = channel; |
911 for (i = 0; i < samples; i++, j += venc->channels) | 916 for (i = 0; i < samples; i++, j += venc->channels) |
912 offset[i] = -audio[j] / 32768. / n * win[window_len - i - 1]; //FIXME find out why the sign has to be fliped | 917 offset[i] = -audio[j] / 32768. / n * win[window_len - i - 1]; //FIXME find out why the sign has to be fliped |
913 } | 918 } |
914 } else { | 919 } else { |
915 for (channel = 0; channel < venc->channels; channel++) | 920 for (channel = 0; channel < venc->channels; channel++) |
916 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len); | 921 memset(venc->samples + channel * window_len * 2 + window_len, |
922 0, sizeof(float) * window_len); | |
917 } | 923 } |
918 | 924 |
919 for (channel = 0; channel < venc->channels; channel++) | 925 for (channel = 0; channel < venc->channels; channel++) |
920 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2); | 926 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len, |
927 venc->samples + channel * window_len * 2); | |
921 | 928 |
922 if (samples) { | 929 if (samples) { |
923 for (channel = 0; channel < venc->channels; channel++) { | 930 for (channel = 0; channel < venc->channels; channel++) { |
924 float * offset = venc->saved + channel*window_len; | 931 float *offset = venc->saved + channel * window_len; |
925 j = channel; | 932 j = channel; |
926 for (i = 0; i < samples; i++, j += venc->channels) | 933 for (i = 0; i < samples; i++, j += venc->channels) |
927 offset[i] = -audio[j] / 32768. / n * win[i]; //FIXME find out why the sign has to be fliped | 934 offset[i] = -audio[j] / 32768. / n * win[i]; //FIXME find out why the sign has to be fliped |
928 } | 935 } |
929 venc->have_saved = 1; | 936 venc->have_saved = 1; |
931 venc->have_saved = 0; | 938 venc->have_saved = 0; |
932 } | 939 } |
933 return 1; | 940 return 1; |
934 } | 941 } |
935 | 942 |
936 static av_cold int vorbis_encode_init(AVCodecContext * avccontext) | 943 static av_cold int vorbis_encode_init(AVCodecContext *avccontext) |
937 { | 944 { |
938 vorbis_enc_context * venc = avccontext->priv_data; | 945 vorbis_enc_context *venc = avccontext->priv_data; |
939 | 946 |
940 if (avccontext->channels != 2) { | 947 if (avccontext->channels != 2) { |
941 av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n"); | 948 av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n"); |
942 return -1; | 949 return -1; |
943 } | 950 } |
950 venc->quality = 1.; | 957 venc->quality = 1.; |
951 venc->quality *= venc->quality; | 958 venc->quality *= venc->quality; |
952 | 959 |
953 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata); | 960 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata); |
954 | 961 |
955 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1); | 962 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1); |
956 | 963 |
957 avccontext->coded_frame = avcodec_alloc_frame(); | 964 avccontext->coded_frame = avcodec_alloc_frame(); |
958 avccontext->coded_frame->key_frame = 1; | 965 avccontext->coded_frame->key_frame = 1; |
959 | 966 |
960 return 0; | 967 return 0; |
961 } | 968 } |
962 | 969 |
963 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data) | 970 static int vorbis_encode_frame(AVCodecContext *avccontext, |
964 { | 971 unsigned char *packets, |
965 vorbis_enc_context * venc = avccontext->priv_data; | 972 int buf_size, void *data) |
966 signed short * audio = data; | 973 { |
974 vorbis_enc_context *venc = avccontext->priv_data; | |
975 signed short *audio = data; | |
967 int samples = data ? avccontext->frame_size : 0; | 976 int samples = data ? avccontext->frame_size : 0; |
968 vorbis_enc_mode * mode; | 977 vorbis_enc_mode *mode; |
969 vorbis_enc_mapping * mapping; | 978 vorbis_enc_mapping *mapping; |
970 PutBitContext pb; | 979 PutBitContext pb; |
971 int i; | 980 int i; |
972 | 981 |
973 if (!apply_window_and_mdct(venc, audio, samples)) | 982 if (!apply_window_and_mdct(venc, audio, samples)) |
974 return 0; | 983 return 0; |
978 | 987 |
979 put_bits(&pb, 1, 0); // magic bit | 988 put_bits(&pb, 1, 0); // magic bit |
980 | 989 |
981 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode | 990 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode |
982 | 991 |
983 mode = &venc->modes[0]; | 992 mode = &venc->modes[0]; |
984 mapping = &venc->mappings[mode->mapping]; | 993 mapping = &venc->mappings[mode->mapping]; |
985 if (mode->blockflag) { | 994 if (mode->blockflag) { |
986 put_bits(&pb, 1, 0); | 995 put_bits(&pb, 1, 0); |
987 put_bits(&pb, 1, 0); | 996 put_bits(&pb, 1, 0); |
988 } | 997 } |
989 | 998 |
990 for (i = 0; i < venc->channels; i++) { | 999 for (i = 0; i < venc->channels; i++) { |
991 vorbis_enc_floor * fc = &venc->floors[mapping->floor[mapping->mux[i]]]; | 1000 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]]; |
992 uint_fast16_t posts[fc->values]; | 1001 uint_fast16_t posts[fc->values]; |
993 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples); | 1002 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples); |
994 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples); | 1003 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples); |
995 } | 1004 } |
996 | 1005 |
997 for (i = 0; i < venc->channels * samples; i++) | 1006 for (i = 0; i < venc->channels * samples; i++) |
998 venc->coeffs[i] /= venc->floor[i]; | 1007 venc->coeffs[i] /= venc->floor[i]; |
999 | 1008 |
1000 for (i = 0; i < mapping->coupling_steps; i++) { | 1009 for (i = 0; i < mapping->coupling_steps; i++) { |
1001 float * mag = venc->coeffs + mapping->magnitude[i] * samples; | 1010 float *mag = venc->coeffs + mapping->magnitude[i] * samples; |
1002 float * ang = venc->coeffs + mapping->angle[i] * samples; | 1011 float *ang = venc->coeffs + mapping->angle[i] * samples; |
1003 int j; | 1012 int j; |
1004 for (j = 0; j < samples; j++) { | 1013 for (j = 0; j < samples; j++) { |
1005 float a = ang[j]; | 1014 float a = ang[j]; |
1006 ang[j] -= mag[j]; | 1015 ang[j] -= mag[j]; |
1007 if (mag[j] > 0) | 1016 if (mag[j] > 0) |
1009 if (ang[j] < 0) | 1018 if (ang[j] < 0) |
1010 mag[j] = a; | 1019 mag[j] = a; |
1011 } | 1020 } |
1012 } | 1021 } |
1013 | 1022 |
1014 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels); | 1023 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], |
1024 &pb, venc->coeffs, samples, venc->channels); | |
1015 | 1025 |
1016 avccontext->coded_frame->pts = venc->sample_count; | 1026 avccontext->coded_frame->pts = venc->sample_count; |
1017 venc->sample_count += avccontext->frame_size; | 1027 venc->sample_count += avccontext->frame_size; |
1018 flush_put_bits(&pb); | 1028 flush_put_bits(&pb); |
1019 return (put_bits_count(&pb) + 7) / 8; | 1029 return (put_bits_count(&pb) + 7) / 8; |
1020 } | 1030 } |
1021 | 1031 |
1022 | 1032 |
1023 static av_cold int vorbis_encode_close(AVCodecContext * avccontext) | 1033 static av_cold int vorbis_encode_close(AVCodecContext *avccontext) |
1024 { | 1034 { |
1025 vorbis_enc_context * venc = avccontext->priv_data; | 1035 vorbis_enc_context *venc = avccontext->priv_data; |
1026 int i; | 1036 int i; |
1027 | 1037 |
1028 if (venc->codebooks) | 1038 if (venc->codebooks) |
1029 for (i = 0; i < venc->ncodebooks; i++) { | 1039 for (i = 0; i < venc->ncodebooks; i++) { |
1030 av_freep(&venc->codebooks[i].lens); | 1040 av_freep(&venc->codebooks[i].lens); |