Libav
|
00001 /* 00002 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org> 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * FFmpeg is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with FFmpeg; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00027 #include <float.h> 00028 #include "avcodec.h" 00029 #include "dsputil.h" 00030 #include "fft.h" 00031 #include "vorbis.h" 00032 #include "vorbis_enc_data.h" 00033 00034 #define BITSTREAM_WRITER_LE 00035 #include "put_bits.h" 00036 00037 #undef NDEBUG 00038 #include <assert.h> 00039 00040 typedef struct { 00041 int nentries; 00042 uint8_t *lens; 00043 uint32_t *codewords; 00044 int ndimentions; 00045 float min; 00046 float delta; 00047 int seq_p; 00048 int lookup; 00049 int *quantlist; 00050 float *dimentions; 00051 float *pow2; 00052 } vorbis_enc_codebook; 00053 00054 typedef struct { 00055 int dim; 00056 int subclass; 00057 int masterbook; 00058 int *books; 00059 } vorbis_enc_floor_class; 00060 00061 typedef struct { 00062 int partitions; 00063 int *partition_to_class; 00064 int nclasses; 00065 vorbis_enc_floor_class *classes; 00066 int multiplier; 00067 int rangebits; 00068 int values; 00069 vorbis_floor1_entry *list; 00070 } vorbis_enc_floor; 00071 00072 typedef struct { 00073 int type; 00074 int begin; 00075 int end; 00076 int partition_size; 00077 int classifications; 00078 int classbook; 00079 int8_t (*books)[8]; 00080 float (*maxes)[2]; 00081 } vorbis_enc_residue; 00082 00083 typedef struct { 00084 int submaps; 00085 int *mux; 00086 int *floor; 00087 int *residue; 00088 int coupling_steps; 00089 int *magnitude; 00090 int *angle; 00091 } vorbis_enc_mapping; 00092 00093 typedef struct { 00094 int blockflag; 00095 int mapping; 00096 } vorbis_enc_mode; 00097 00098 typedef struct { 00099 int channels; 00100 int sample_rate; 00101 int log2_blocksize[2]; 00102 FFTContext mdct[2]; 00103 const float *win[2]; 00104 int have_saved; 00105 float *saved; 00106 float *samples; 00107 float *floor; // also used for tmp values for mdct 00108 float *coeffs; // also used for residue after floor 00109 float quality; 00110 00111 int ncodebooks; 00112 vorbis_enc_codebook *codebooks; 00113 00114 int nfloors; 00115 vorbis_enc_floor *floors; 00116 00117 int nresidues; 00118 vorbis_enc_residue *residues; 00119 00120 int nmappings; 00121 vorbis_enc_mapping *mappings; 00122 00123 int nmodes; 00124 vorbis_enc_mode *modes; 00125 00126 int64_t sample_count; 00127 } vorbis_enc_context; 00128 00129 static inline void put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb, 00130 int entry) 00131 { 00132 assert(entry >= 0); 00133 assert(entry < cb->nentries); 00134 assert(cb->lens[entry]); 00135 put_bits(pb, cb->lens[entry], cb->codewords[entry]); 00136 } 00137 00138 static int cb_lookup_vals(int lookup, int dimentions, int entries) 00139 { 00140 if (lookup == 1) 00141 return ff_vorbis_nth_root(entries, dimentions); 00142 else if (lookup == 2) 00143 return dimentions *entries; 00144 return 0; 00145 } 00146 00147 static void ready_codebook(vorbis_enc_codebook *cb) 00148 { 00149 int i; 00150 00151 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries); 00152 00153 if (!cb->lookup) { 00154 cb->pow2 = cb->dimentions = NULL; 00155 } else { 00156 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); 00157 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions); 00158 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries); 00159 for (i = 0; i < cb->nentries; i++) { 00160 float last = 0; 00161 int j; 00162 int div = 1; 00163 for (j = 0; j < cb->ndimentions; j++) { 00164 int off; 00165 if (cb->lookup == 1) 00166 off = (i / div) % vals; // lookup type 1 00167 else 00168 off = i * cb->ndimentions + j; // lookup type 2 00169 00170 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta; 00171 if (cb->seq_p) 00172 last = cb->dimentions[i * cb->ndimentions + j]; 00173 cb->pow2[i] += cb->dimentions[i * cb->ndimentions + j] * cb->dimentions[i * cb->ndimentions + j]; 00174 div *= vals; 00175 } 00176 cb->pow2[i] /= 2.; 00177 } 00178 } 00179 } 00180 00181 static void ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc) 00182 { 00183 int i; 00184 assert(rc->type == 2); 00185 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications); 00186 for (i = 0; i < rc->classifications; i++) { 00187 int j; 00188 vorbis_enc_codebook * cb; 00189 for (j = 0; j < 8; j++) 00190 if (rc->books[i][j] != -1) 00191 break; 00192 if (j == 8) // zero 00193 continue; 00194 cb = &venc->codebooks[rc->books[i][j]]; 00195 assert(cb->ndimentions >= 2); 00196 assert(cb->lookup); 00197 00198 for (j = 0; j < cb->nentries; j++) { 00199 float a; 00200 if (!cb->lens[j]) 00201 continue; 00202 a = fabs(cb->dimentions[j * cb->ndimentions]); 00203 if (a > rc->maxes[i][0]) 00204 rc->maxes[i][0] = a; 00205 a = fabs(cb->dimentions[j * cb->ndimentions + 1]); 00206 if (a > rc->maxes[i][1]) 00207 rc->maxes[i][1] = a; 00208 } 00209 } 00210 // small bias 00211 for (i = 0; i < rc->classifications; i++) { 00212 rc->maxes[i][0] += 0.8; 00213 rc->maxes[i][1] += 0.8; 00214 } 00215 } 00216 00217 static void create_vorbis_context(vorbis_enc_context *venc, 00218 AVCodecContext *avccontext) 00219 { 00220 vorbis_enc_floor *fc; 00221 vorbis_enc_residue *rc; 00222 vorbis_enc_mapping *mc; 00223 int i, book; 00224 00225 venc->channels = avccontext->channels; 00226 venc->sample_rate = avccontext->sample_rate; 00227 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11; 00228 00229 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors); 00230 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks); 00231 00232 // codebook 0..14 - floor1 book, values 0..255 00233 // codebook 15 residue masterbook 00234 // codebook 16..29 residue 00235 for (book = 0; book < venc->ncodebooks; book++) { 00236 vorbis_enc_codebook *cb = &venc->codebooks[book]; 00237 int vals; 00238 cb->ndimentions = cvectors[book].dim; 00239 cb->nentries = cvectors[book].real_len; 00240 cb->min = cvectors[book].min; 00241 cb->delta = cvectors[book].delta; 00242 cb->lookup = cvectors[book].lookup; 00243 cb->seq_p = 0; 00244 00245 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries); 00246 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries); 00247 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len); 00248 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len); 00249 00250 if (cb->lookup) { 00251 vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); 00252 cb->quantlist = av_malloc(sizeof(int) * vals); 00253 for (i = 0; i < vals; i++) 00254 cb->quantlist[i] = cvectors[book].quant[i]; 00255 } else { 00256 cb->quantlist = NULL; 00257 } 00258 ready_codebook(cb); 00259 } 00260 00261 venc->nfloors = 1; 00262 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors); 00263 00264 // just 1 floor 00265 fc = &venc->floors[0]; 00266 fc->partitions = 8; 00267 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions); 00268 fc->nclasses = 0; 00269 for (i = 0; i < fc->partitions; i++) { 00270 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4}; 00271 fc->partition_to_class[i] = a[i]; 00272 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]); 00273 } 00274 fc->nclasses++; 00275 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses); 00276 for (i = 0; i < fc->nclasses; i++) { 00277 vorbis_enc_floor_class * c = &fc->classes[i]; 00278 int j, books; 00279 c->dim = floor_classes[i].dim; 00280 c->subclass = floor_classes[i].subclass; 00281 c->masterbook = floor_classes[i].masterbook; 00282 books = (1 << c->subclass); 00283 c->books = av_malloc(sizeof(int) * books); 00284 for (j = 0; j < books; j++) 00285 c->books[j] = floor_classes[i].nbooks[j]; 00286 } 00287 fc->multiplier = 2; 00288 fc->rangebits = venc->log2_blocksize[0] - 1; 00289 00290 fc->values = 2; 00291 for (i = 0; i < fc->partitions; i++) 00292 fc->values += fc->classes[fc->partition_to_class[i]].dim; 00293 00294 fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values); 00295 fc->list[0].x = 0; 00296 fc->list[1].x = 1 << fc->rangebits; 00297 for (i = 2; i < fc->values; i++) { 00298 static const int a[] = { 00299 93, 23,372, 6, 46,186,750, 14, 33, 65, 00300 130,260,556, 3, 10, 18, 28, 39, 55, 79, 00301 111,158,220,312,464,650,850 00302 }; 00303 fc->list[i].x = a[i - 2]; 00304 } 00305 ff_vorbis_ready_floor1_list(fc->list, fc->values); 00306 00307 venc->nresidues = 1; 00308 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues); 00309 00310 // single residue 00311 rc = &venc->residues[0]; 00312 rc->type = 2; 00313 rc->begin = 0; 00314 rc->end = 1600; 00315 rc->partition_size = 32; 00316 rc->classifications = 10; 00317 rc->classbook = 15; 00318 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications); 00319 { 00320 static const int8_t a[10][8] = { 00321 { -1, -1, -1, -1, -1, -1, -1, -1, }, 00322 { -1, -1, 16, -1, -1, -1, -1, -1, }, 00323 { -1, -1, 17, -1, -1, -1, -1, -1, }, 00324 { -1, -1, 18, -1, -1, -1, -1, -1, }, 00325 { -1, -1, 19, -1, -1, -1, -1, -1, }, 00326 { -1, -1, 20, -1, -1, -1, -1, -1, }, 00327 { -1, -1, 21, -1, -1, -1, -1, -1, }, 00328 { 22, 23, -1, -1, -1, -1, -1, -1, }, 00329 { 24, 25, -1, -1, -1, -1, -1, -1, }, 00330 { 26, 27, 28, -1, -1, -1, -1, -1, }, 00331 }; 00332 memcpy(rc->books, a, sizeof a); 00333 } 00334 ready_residue(rc, venc); 00335 00336 venc->nmappings = 1; 00337 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings); 00338 00339 // single mapping 00340 mc = &venc->mappings[0]; 00341 mc->submaps = 1; 00342 mc->mux = av_malloc(sizeof(int) * venc->channels); 00343 for (i = 0; i < venc->channels; i++) 00344 mc->mux[i] = 0; 00345 mc->floor = av_malloc(sizeof(int) * mc->submaps); 00346 mc->residue = av_malloc(sizeof(int) * mc->submaps); 00347 for (i = 0; i < mc->submaps; i++) { 00348 mc->floor[i] = 0; 00349 mc->residue[i] = 0; 00350 } 00351 mc->coupling_steps = venc->channels == 2 ? 1 : 0; 00352 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps); 00353 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps); 00354 if (mc->coupling_steps) { 00355 mc->magnitude[0] = 0; 00356 mc->angle[0] = 1; 00357 } 00358 00359 venc->nmodes = 1; 00360 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes); 00361 00362 // single mode 00363 venc->modes[0].blockflag = 0; 00364 venc->modes[0].mapping = 0; 00365 00366 venc->have_saved = 0; 00367 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); 00368 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1])); 00369 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); 00370 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2); 00371 00372 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6]; 00373 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6]; 00374 00375 ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0); 00376 ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0); 00377 } 00378 00379 static void put_float(PutBitContext *pb, float f) 00380 { 00381 int exp, mant; 00382 uint32_t res = 0; 00383 mant = (int)ldexp(frexp(f, &exp), 20); 00384 exp += 788 - 20; 00385 if (mant < 0) { 00386 res |= (1 << 31); 00387 mant = -mant; 00388 } 00389 res |= mant | (exp << 21); 00390 put_bits32(pb, res); 00391 } 00392 00393 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb) 00394 { 00395 int i; 00396 int ordered = 0; 00397 00398 put_bits(pb, 24, 0x564342); //magic 00399 put_bits(pb, 16, cb->ndimentions); 00400 put_bits(pb, 24, cb->nentries); 00401 00402 for (i = 1; i < cb->nentries; i++) 00403 if (cb->lens[i] < cb->lens[i-1]) 00404 break; 00405 if (i == cb->nentries) 00406 ordered = 1; 00407 00408 put_bits(pb, 1, ordered); 00409 if (ordered) { 00410 int len = cb->lens[0]; 00411 put_bits(pb, 5, len - 1); 00412 i = 0; 00413 while (i < cb->nentries) { 00414 int j; 00415 for (j = 0; j+i < cb->nentries; j++) 00416 if (cb->lens[j+i] != len) 00417 break; 00418 put_bits(pb, ilog(cb->nentries - i), j); 00419 i += j; 00420 len++; 00421 } 00422 } else { 00423 int sparse = 0; 00424 for (i = 0; i < cb->nentries; i++) 00425 if (!cb->lens[i]) 00426 break; 00427 if (i != cb->nentries) 00428 sparse = 1; 00429 put_bits(pb, 1, sparse); 00430 00431 for (i = 0; i < cb->nentries; i++) { 00432 if (sparse) 00433 put_bits(pb, 1, !!cb->lens[i]); 00434 if (cb->lens[i]) 00435 put_bits(pb, 5, cb->lens[i] - 1); 00436 } 00437 } 00438 00439 put_bits(pb, 4, cb->lookup); 00440 if (cb->lookup) { 00441 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries); 00442 int bits = ilog(cb->quantlist[0]); 00443 00444 for (i = 1; i < tmp; i++) 00445 bits = FFMAX(bits, ilog(cb->quantlist[i])); 00446 00447 put_float(pb, cb->min); 00448 put_float(pb, cb->delta); 00449 00450 put_bits(pb, 4, bits - 1); 00451 put_bits(pb, 1, cb->seq_p); 00452 00453 for (i = 0; i < tmp; i++) 00454 put_bits(pb, bits, cb->quantlist[i]); 00455 } 00456 } 00457 00458 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc) 00459 { 00460 int i; 00461 00462 put_bits(pb, 16, 1); // type, only floor1 is supported 00463 00464 put_bits(pb, 5, fc->partitions); 00465 00466 for (i = 0; i < fc->partitions; i++) 00467 put_bits(pb, 4, fc->partition_to_class[i]); 00468 00469 for (i = 0; i < fc->nclasses; i++) { 00470 int j, books; 00471 00472 put_bits(pb, 3, fc->classes[i].dim - 1); 00473 put_bits(pb, 2, fc->classes[i].subclass); 00474 00475 if (fc->classes[i].subclass) 00476 put_bits(pb, 8, fc->classes[i].masterbook); 00477 00478 books = (1 << fc->classes[i].subclass); 00479 00480 for (j = 0; j < books; j++) 00481 put_bits(pb, 8, fc->classes[i].books[j] + 1); 00482 } 00483 00484 put_bits(pb, 2, fc->multiplier - 1); 00485 put_bits(pb, 4, fc->rangebits); 00486 00487 for (i = 2; i < fc->values; i++) 00488 put_bits(pb, fc->rangebits, fc->list[i].x); 00489 } 00490 00491 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc) 00492 { 00493 int i; 00494 00495 put_bits(pb, 16, rc->type); 00496 00497 put_bits(pb, 24, rc->begin); 00498 put_bits(pb, 24, rc->end); 00499 put_bits(pb, 24, rc->partition_size - 1); 00500 put_bits(pb, 6, rc->classifications - 1); 00501 put_bits(pb, 8, rc->classbook); 00502 00503 for (i = 0; i < rc->classifications; i++) { 00504 int j, tmp = 0; 00505 for (j = 0; j < 8; j++) 00506 tmp |= (rc->books[i][j] != -1) << j; 00507 00508 put_bits(pb, 3, tmp & 7); 00509 put_bits(pb, 1, tmp > 7); 00510 00511 if (tmp > 7) 00512 put_bits(pb, 5, tmp >> 3); 00513 } 00514 00515 for (i = 0; i < rc->classifications; i++) { 00516 int j; 00517 for (j = 0; j < 8; j++) 00518 if (rc->books[i][j] != -1) 00519 put_bits(pb, 8, rc->books[i][j]); 00520 } 00521 } 00522 00523 static int put_main_header(vorbis_enc_context *venc, uint8_t **out) 00524 { 00525 int i; 00526 PutBitContext pb; 00527 uint8_t buffer[50000] = {0}, *p = buffer; 00528 int buffer_len = sizeof buffer; 00529 int len, hlens[3]; 00530 00531 // identification header 00532 init_put_bits(&pb, p, buffer_len); 00533 put_bits(&pb, 8, 1); //magic 00534 for (i = 0; "vorbis"[i]; i++) 00535 put_bits(&pb, 8, "vorbis"[i]); 00536 put_bits32(&pb, 0); // version 00537 put_bits(&pb, 8, venc->channels); 00538 put_bits32(&pb, venc->sample_rate); 00539 put_bits32(&pb, 0); // bitrate 00540 put_bits32(&pb, 0); // bitrate 00541 put_bits32(&pb, 0); // bitrate 00542 put_bits(&pb, 4, venc->log2_blocksize[0]); 00543 put_bits(&pb, 4, venc->log2_blocksize[1]); 00544 put_bits(&pb, 1, 1); // framing 00545 00546 flush_put_bits(&pb); 00547 hlens[0] = put_bits_count(&pb) >> 3; 00548 buffer_len -= hlens[0]; 00549 p += hlens[0]; 00550 00551 // comment header 00552 init_put_bits(&pb, p, buffer_len); 00553 put_bits(&pb, 8, 3); //magic 00554 for (i = 0; "vorbis"[i]; i++) 00555 put_bits(&pb, 8, "vorbis"[i]); 00556 put_bits32(&pb, 0); // vendor length TODO 00557 put_bits32(&pb, 0); // amount of comments 00558 put_bits(&pb, 1, 1); // framing 00559 00560 flush_put_bits(&pb); 00561 hlens[1] = put_bits_count(&pb) >> 3; 00562 buffer_len -= hlens[1]; 00563 p += hlens[1]; 00564 00565 // setup header 00566 init_put_bits(&pb, p, buffer_len); 00567 put_bits(&pb, 8, 5); //magic 00568 for (i = 0; "vorbis"[i]; i++) 00569 put_bits(&pb, 8, "vorbis"[i]); 00570 00571 // codebooks 00572 put_bits(&pb, 8, venc->ncodebooks - 1); 00573 for (i = 0; i < venc->ncodebooks; i++) 00574 put_codebook_header(&pb, &venc->codebooks[i]); 00575 00576 // time domain, reserved, zero 00577 put_bits(&pb, 6, 0); 00578 put_bits(&pb, 16, 0); 00579 00580 // floors 00581 put_bits(&pb, 6, venc->nfloors - 1); 00582 for (i = 0; i < venc->nfloors; i++) 00583 put_floor_header(&pb, &venc->floors[i]); 00584 00585 // residues 00586 put_bits(&pb, 6, venc->nresidues - 1); 00587 for (i = 0; i < venc->nresidues; i++) 00588 put_residue_header(&pb, &venc->residues[i]); 00589 00590 // mappings 00591 put_bits(&pb, 6, venc->nmappings - 1); 00592 for (i = 0; i < venc->nmappings; i++) { 00593 vorbis_enc_mapping *mc = &venc->mappings[i]; 00594 int j; 00595 put_bits(&pb, 16, 0); // mapping type 00596 00597 put_bits(&pb, 1, mc->submaps > 1); 00598 if (mc->submaps > 1) 00599 put_bits(&pb, 4, mc->submaps - 1); 00600 00601 put_bits(&pb, 1, !!mc->coupling_steps); 00602 if (mc->coupling_steps) { 00603 put_bits(&pb, 8, mc->coupling_steps - 1); 00604 for (j = 0; j < mc->coupling_steps; j++) { 00605 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]); 00606 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]); 00607 } 00608 } 00609 00610 put_bits(&pb, 2, 0); // reserved 00611 00612 if (mc->submaps > 1) 00613 for (j = 0; j < venc->channels; j++) 00614 put_bits(&pb, 4, mc->mux[j]); 00615 00616 for (j = 0; j < mc->submaps; j++) { 00617 put_bits(&pb, 8, 0); // reserved time configuration 00618 put_bits(&pb, 8, mc->floor[j]); 00619 put_bits(&pb, 8, mc->residue[j]); 00620 } 00621 } 00622 00623 // modes 00624 put_bits(&pb, 6, venc->nmodes - 1); 00625 for (i = 0; i < venc->nmodes; i++) { 00626 put_bits(&pb, 1, venc->modes[i].blockflag); 00627 put_bits(&pb, 16, 0); // reserved window type 00628 put_bits(&pb, 16, 0); // reserved transform type 00629 put_bits(&pb, 8, venc->modes[i].mapping); 00630 } 00631 00632 put_bits(&pb, 1, 1); // framing 00633 00634 flush_put_bits(&pb); 00635 hlens[2] = put_bits_count(&pb) >> 3; 00636 00637 len = hlens[0] + hlens[1] + hlens[2]; 00638 p = *out = av_mallocz(64 + len + len/255); 00639 00640 *p++ = 2; 00641 p += av_xiphlacing(p, hlens[0]); 00642 p += av_xiphlacing(p, hlens[1]); 00643 buffer_len = 0; 00644 for (i = 0; i < 3; i++) { 00645 memcpy(p, buffer + buffer_len, hlens[i]); 00646 p += hlens[i]; 00647 buffer_len += hlens[i]; 00648 } 00649 00650 return p - *out; 00651 } 00652 00653 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i) 00654 { 00655 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x; 00656 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x; 00657 int j; 00658 float average = 0; 00659 00660 for (j = begin; j < end; j++) 00661 average += fabs(coeffs[j]); 00662 return average / (end - begin); 00663 } 00664 00665 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc, 00666 float *coeffs, uint_fast16_t *posts, int samples) 00667 { 00668 int range = 255 / fc->multiplier + 1; 00669 int i; 00670 float tot_average = 0.; 00671 float averages[fc->values]; 00672 for (i = 0; i < fc->values; i++) { 00673 averages[i] = get_floor_average(fc, coeffs, i); 00674 tot_average += averages[i]; 00675 } 00676 tot_average /= fc->values; 00677 tot_average /= venc->quality; 00678 00679 for (i = 0; i < fc->values; i++) { 00680 int position = fc->list[fc->list[i].sort].x; 00681 float average = averages[i]; 00682 int j; 00683 00684 average *= pow(tot_average / average, 0.5) * pow(1.25, position/200.); // MAGIC! 00685 for (j = 0; j < range - 1; j++) 00686 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average) 00687 break; 00688 posts[fc->list[i].sort] = j; 00689 } 00690 } 00691 00692 static int render_point(int x0, int y0, int x1, int y1, int x) 00693 { 00694 return y0 + (x - x0) * (y1 - y0) / (x1 - x0); 00695 } 00696 00697 static void floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc, 00698 PutBitContext *pb, uint_fast16_t *posts, 00699 float *floor, int samples) 00700 { 00701 int range = 255 / fc->multiplier + 1; 00702 int coded[fc->values]; // first 2 values are unused 00703 int i, counter; 00704 00705 put_bits(pb, 1, 1); // non zero 00706 put_bits(pb, ilog(range - 1), posts[0]); 00707 put_bits(pb, ilog(range - 1), posts[1]); 00708 coded[0] = coded[1] = 1; 00709 00710 for (i = 2; i < fc->values; i++) { 00711 int predicted = render_point(fc->list[fc->list[i].low].x, 00712 posts[fc->list[i].low], 00713 fc->list[fc->list[i].high].x, 00714 posts[fc->list[i].high], 00715 fc->list[i].x); 00716 int highroom = range - predicted; 00717 int lowroom = predicted; 00718 int room = FFMIN(highroom, lowroom); 00719 if (predicted == posts[i]) { 00720 coded[i] = 0; // must be used later as flag! 00721 continue; 00722 } else { 00723 if (!coded[fc->list[i].low ]) 00724 coded[fc->list[i].low ] = -1; 00725 if (!coded[fc->list[i].high]) 00726 coded[fc->list[i].high] = -1; 00727 } 00728 if (posts[i] > predicted) { 00729 if (posts[i] - predicted > room) 00730 coded[i] = posts[i] - predicted + lowroom; 00731 else 00732 coded[i] = (posts[i] - predicted) << 1; 00733 } else { 00734 if (predicted - posts[i] > room) 00735 coded[i] = predicted - posts[i] + highroom - 1; 00736 else 00737 coded[i] = ((predicted - posts[i]) << 1) - 1; 00738 } 00739 } 00740 00741 counter = 2; 00742 for (i = 0; i < fc->partitions; i++) { 00743 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]]; 00744 int k, cval = 0, csub = 1<<c->subclass; 00745 if (c->subclass) { 00746 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook]; 00747 int cshift = 0; 00748 for (k = 0; k < c->dim; k++) { 00749 int l; 00750 for (l = 0; l < csub; l++) { 00751 int maxval = 1; 00752 if (c->books[l] != -1) 00753 maxval = venc->codebooks[c->books[l]].nentries; 00754 // coded could be -1, but this still works, cause that is 0 00755 if (coded[counter + k] < maxval) 00756 break; 00757 } 00758 assert(l != csub); 00759 cval |= l << cshift; 00760 cshift += c->subclass; 00761 } 00762 put_codeword(pb, book, cval); 00763 } 00764 for (k = 0; k < c->dim; k++) { 00765 int book = c->books[cval & (csub-1)]; 00766 int entry = coded[counter++]; 00767 cval >>= c->subclass; 00768 if (book == -1) 00769 continue; 00770 if (entry == -1) 00771 entry = 0; 00772 put_codeword(pb, &venc->codebooks[book], entry); 00773 } 00774 } 00775 00776 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded, 00777 fc->multiplier, floor, samples); 00778 } 00779 00780 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb, 00781 float *num) 00782 { 00783 int i, entry = -1; 00784 float distance = FLT_MAX; 00785 assert(book->dimentions); 00786 for (i = 0; i < book->nentries; i++) { 00787 float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i]; 00788 int j; 00789 if (!book->lens[i]) 00790 continue; 00791 for (j = 0; j < book->ndimentions; j++) 00792 d -= vec[j] * num[j]; 00793 if (distance > d) { 00794 entry = i; 00795 distance = d; 00796 } 00797 } 00798 put_codeword(pb, book, entry); 00799 return &book->dimentions[entry * book->ndimentions]; 00800 } 00801 00802 static void residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc, 00803 PutBitContext *pb, float *coeffs, int samples, 00804 int real_ch) 00805 { 00806 int pass, i, j, p, k; 00807 int psize = rc->partition_size; 00808 int partitions = (rc->end - rc->begin) / psize; 00809 int channels = (rc->type == 2) ? 1 : real_ch; 00810 int classes[channels][partitions]; 00811 int classwords = venc->codebooks[rc->classbook].ndimentions; 00812 00813 assert(rc->type == 2); 00814 assert(real_ch == 2); 00815 for (p = 0; p < partitions; p++) { 00816 float max1 = 0., max2 = 0.; 00817 int s = rc->begin + p * psize; 00818 for (k = s; k < s + psize; k += 2) { 00819 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch])); 00820 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch])); 00821 } 00822 00823 for (i = 0; i < rc->classifications - 1; i++) 00824 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) 00825 break; 00826 classes[0][p] = i; 00827 } 00828 00829 for (pass = 0; pass < 8; pass++) { 00830 p = 0; 00831 while (p < partitions) { 00832 if (pass == 0) 00833 for (j = 0; j < channels; j++) { 00834 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook]; 00835 int entry = 0; 00836 for (i = 0; i < classwords; i++) { 00837 entry *= rc->classifications; 00838 entry += classes[j][p + i]; 00839 } 00840 put_codeword(pb, book, entry); 00841 } 00842 for (i = 0; i < classwords && p < partitions; i++, p++) { 00843 for (j = 0; j < channels; j++) { 00844 int nbook = rc->books[classes[j][p]][pass]; 00845 vorbis_enc_codebook * book = &venc->codebooks[nbook]; 00846 float *buf = coeffs + samples*j + rc->begin + p*psize; 00847 if (nbook == -1) 00848 continue; 00849 00850 assert(rc->type == 0 || rc->type == 2); 00851 assert(!(psize % book->ndimentions)); 00852 00853 if (rc->type == 0) { 00854 for (k = 0; k < psize; k += book->ndimentions) { 00855 float *a = put_vector(book, pb, &buf[k]); 00856 int l; 00857 for (l = 0; l < book->ndimentions; l++) 00858 buf[k + l] -= a[l]; 00859 } 00860 } else { 00861 int s = rc->begin + p * psize, a1, b1; 00862 a1 = (s % real_ch) * samples; 00863 b1 = s / real_ch; 00864 s = real_ch * samples; 00865 for (k = 0; k < psize; k += book->ndimentions) { 00866 int dim, a2 = a1, b2 = b1; 00867 float vec[book->ndimentions], *pv = vec; 00868 for (dim = book->ndimentions; dim--; ) { 00869 *pv++ = coeffs[a2 + b2]; 00870 if ((a2 += samples) == s) { 00871 a2 = 0; 00872 b2++; 00873 } 00874 } 00875 pv = put_vector(book, pb, vec); 00876 for (dim = book->ndimentions; dim--; ) { 00877 coeffs[a1 + b1] -= *pv++; 00878 if ((a1 += samples) == s) { 00879 a1 = 0; 00880 b1++; 00881 } 00882 } 00883 } 00884 } 00885 } 00886 } 00887 } 00888 } 00889 } 00890 00891 static int apply_window_and_mdct(vorbis_enc_context *venc, signed short *audio, 00892 int samples) 00893 { 00894 int i, j, channel; 00895 const float * win = venc->win[0]; 00896 int window_len = 1 << (venc->log2_blocksize[0] - 1); 00897 float n = (float)(1 << venc->log2_blocksize[0]) / 4.; 00898 // FIXME use dsp 00899 00900 if (!venc->have_saved && !samples) 00901 return 0; 00902 00903 if (venc->have_saved) { 00904 for (channel = 0; channel < venc->channels; channel++) 00905 memcpy(venc->samples + channel * window_len * 2, 00906 venc->saved + channel * window_len, sizeof(float) * window_len); 00907 } else { 00908 for (channel = 0; channel < venc->channels; channel++) 00909 memset(venc->samples + channel * window_len * 2, 0, 00910 sizeof(float) * window_len); 00911 } 00912 00913 if (samples) { 00914 for (channel = 0; channel < venc->channels; channel++) { 00915 float * offset = venc->samples + channel*window_len*2 + window_len; 00916 j = channel; 00917 for (i = 0; i < samples; i++, j += venc->channels) 00918 offset[i] = -audio[j] / 32768. / n * win[window_len - i - 1]; //FIXME find out why the sign has to be fliped 00919 } 00920 } else { 00921 for (channel = 0; channel < venc->channels; channel++) 00922 memset(venc->samples + channel * window_len * 2 + window_len, 00923 0, sizeof(float) * window_len); 00924 } 00925 00926 for (channel = 0; channel < venc->channels; channel++) 00927 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len, 00928 venc->samples + channel * window_len * 2); 00929 00930 if (samples) { 00931 for (channel = 0; channel < venc->channels; channel++) { 00932 float *offset = venc->saved + channel * window_len; 00933 j = channel; 00934 for (i = 0; i < samples; i++, j += venc->channels) 00935 offset[i] = -audio[j] / 32768. / n * win[i]; //FIXME find out why the sign has to be fliped 00936 } 00937 venc->have_saved = 1; 00938 } else { 00939 venc->have_saved = 0; 00940 } 00941 return 1; 00942 } 00943 00944 static av_cold int vorbis_encode_init(AVCodecContext *avccontext) 00945 { 00946 vorbis_enc_context *venc = avccontext->priv_data; 00947 00948 if (avccontext->channels != 2) { 00949 av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n"); 00950 return -1; 00951 } 00952 00953 create_vorbis_context(venc, avccontext); 00954 00955 if (avccontext->flags & CODEC_FLAG_QSCALE) 00956 venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.; 00957 else 00958 venc->quality = 1.; 00959 venc->quality *= venc->quality; 00960 00961 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata); 00962 00963 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1); 00964 00965 avccontext->coded_frame = avcodec_alloc_frame(); 00966 avccontext->coded_frame->key_frame = 1; 00967 00968 return 0; 00969 } 00970 00971 static int vorbis_encode_frame(AVCodecContext *avccontext, 00972 unsigned char *packets, 00973 int buf_size, void *data) 00974 { 00975 vorbis_enc_context *venc = avccontext->priv_data; 00976 signed short *audio = data; 00977 int samples = data ? avccontext->frame_size : 0; 00978 vorbis_enc_mode *mode; 00979 vorbis_enc_mapping *mapping; 00980 PutBitContext pb; 00981 int i; 00982 00983 if (!apply_window_and_mdct(venc, audio, samples)) 00984 return 0; 00985 samples = 1 << (venc->log2_blocksize[0] - 1); 00986 00987 init_put_bits(&pb, packets, buf_size); 00988 00989 put_bits(&pb, 1, 0); // magic bit 00990 00991 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode 00992 00993 mode = &venc->modes[0]; 00994 mapping = &venc->mappings[mode->mapping]; 00995 if (mode->blockflag) { 00996 put_bits(&pb, 1, 0); 00997 put_bits(&pb, 1, 0); 00998 } 00999 01000 for (i = 0; i < venc->channels; i++) { 01001 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]]; 01002 uint_fast16_t posts[fc->values]; 01003 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples); 01004 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples); 01005 } 01006 01007 for (i = 0; i < venc->channels * samples; i++) 01008 venc->coeffs[i] /= venc->floor[i]; 01009 01010 for (i = 0; i < mapping->coupling_steps; i++) { 01011 float *mag = venc->coeffs + mapping->magnitude[i] * samples; 01012 float *ang = venc->coeffs + mapping->angle[i] * samples; 01013 int j; 01014 for (j = 0; j < samples; j++) { 01015 float a = ang[j]; 01016 ang[j] -= mag[j]; 01017 if (mag[j] > 0) 01018 ang[j] = -ang[j]; 01019 if (ang[j] < 0) 01020 mag[j] = a; 01021 } 01022 } 01023 01024 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], 01025 &pb, venc->coeffs, samples, venc->channels); 01026 01027 avccontext->coded_frame->pts = venc->sample_count; 01028 venc->sample_count += avccontext->frame_size; 01029 flush_put_bits(&pb); 01030 return put_bits_count(&pb) >> 3; 01031 } 01032 01033 01034 static av_cold int vorbis_encode_close(AVCodecContext *avccontext) 01035 { 01036 vorbis_enc_context *venc = avccontext->priv_data; 01037 int i; 01038 01039 if (venc->codebooks) 01040 for (i = 0; i < venc->ncodebooks; i++) { 01041 av_freep(&venc->codebooks[i].lens); 01042 av_freep(&venc->codebooks[i].codewords); 01043 av_freep(&venc->codebooks[i].quantlist); 01044 av_freep(&venc->codebooks[i].dimentions); 01045 av_freep(&venc->codebooks[i].pow2); 01046 } 01047 av_freep(&venc->codebooks); 01048 01049 if (venc->floors) 01050 for (i = 0; i < venc->nfloors; i++) { 01051 int j; 01052 if (venc->floors[i].classes) 01053 for (j = 0; j < venc->floors[i].nclasses; j++) 01054 av_freep(&venc->floors[i].classes[j].books); 01055 av_freep(&venc->floors[i].classes); 01056 av_freep(&venc->floors[i].partition_to_class); 01057 av_freep(&venc->floors[i].list); 01058 } 01059 av_freep(&venc->floors); 01060 01061 if (venc->residues) 01062 for (i = 0; i < venc->nresidues; i++) { 01063 av_freep(&venc->residues[i].books); 01064 av_freep(&venc->residues[i].maxes); 01065 } 01066 av_freep(&venc->residues); 01067 01068 if (venc->mappings) 01069 for (i = 0; i < venc->nmappings; i++) { 01070 av_freep(&venc->mappings[i].mux); 01071 av_freep(&venc->mappings[i].floor); 01072 av_freep(&venc->mappings[i].residue); 01073 av_freep(&venc->mappings[i].magnitude); 01074 av_freep(&venc->mappings[i].angle); 01075 } 01076 av_freep(&venc->mappings); 01077 01078 av_freep(&venc->modes); 01079 01080 av_freep(&venc->saved); 01081 av_freep(&venc->samples); 01082 av_freep(&venc->floor); 01083 av_freep(&venc->coeffs); 01084 01085 ff_mdct_end(&venc->mdct[0]); 01086 ff_mdct_end(&venc->mdct[1]); 01087 01088 av_freep(&avccontext->coded_frame); 01089 av_freep(&avccontext->extradata); 01090 01091 return 0 ; 01092 } 01093 01094 AVCodec vorbis_encoder = { 01095 "vorbis", 01096 AVMEDIA_TYPE_AUDIO, 01097 CODEC_ID_VORBIS, 01098 sizeof(vorbis_enc_context), 01099 vorbis_encode_init, 01100 vorbis_encode_frame, 01101 vorbis_encode_close, 01102 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, 01103 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, 01104 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"), 01105 };