00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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;
00108 float *coeffs;
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;
00167 else
00168 off = i * cb->ndimentions + j;
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)
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
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
00233
00234
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
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 if (ff_vorbis_ready_floor1_list(avccontext, fc->list, fc->values))
00306 return AVERROR(EINVAL);
00307
00308 venc->nresidues = 1;
00309 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
00310
00311
00312 rc = &venc->residues[0];
00313 rc->type = 2;
00314 rc->begin = 0;
00315 rc->end = 1600;
00316 rc->partition_size = 32;
00317 rc->classifications = 10;
00318 rc->classbook = 15;
00319 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
00320 {
00321 static const int8_t a[10][8] = {
00322 { -1, -1, -1, -1, -1, -1, -1, -1, },
00323 { -1, -1, 16, -1, -1, -1, -1, -1, },
00324 { -1, -1, 17, -1, -1, -1, -1, -1, },
00325 { -1, -1, 18, -1, -1, -1, -1, -1, },
00326 { -1, -1, 19, -1, -1, -1, -1, -1, },
00327 { -1, -1, 20, -1, -1, -1, -1, -1, },
00328 { -1, -1, 21, -1, -1, -1, -1, -1, },
00329 { 22, 23, -1, -1, -1, -1, -1, -1, },
00330 { 24, 25, -1, -1, -1, -1, -1, -1, },
00331 { 26, 27, 28, -1, -1, -1, -1, -1, },
00332 };
00333 memcpy(rc->books, a, sizeof a);
00334 }
00335 ready_residue(rc, venc);
00336
00337 venc->nmappings = 1;
00338 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
00339
00340
00341 mc = &venc->mappings[0];
00342 mc->submaps = 1;
00343 mc->mux = av_malloc(sizeof(int) * venc->channels);
00344 for (i = 0; i < venc->channels; i++)
00345 mc->mux[i] = 0;
00346 mc->floor = av_malloc(sizeof(int) * mc->submaps);
00347 mc->residue = av_malloc(sizeof(int) * mc->submaps);
00348 for (i = 0; i < mc->submaps; i++) {
00349 mc->floor[i] = 0;
00350 mc->residue[i] = 0;
00351 }
00352 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
00353 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
00354 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
00355 if (mc->coupling_steps) {
00356 mc->magnitude[0] = 0;
00357 mc->angle[0] = 1;
00358 }
00359
00360 venc->nmodes = 1;
00361 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
00362
00363
00364 venc->modes[0].blockflag = 0;
00365 venc->modes[0].mapping = 0;
00366
00367 venc->have_saved = 0;
00368 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
00369 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
00370 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
00371 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
00372
00373 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
00374 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
00375
00376 ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0);
00377 ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0);
00378 }
00379
00380 static void put_float(PutBitContext *pb, float f)
00381 {
00382 int exp, mant;
00383 uint32_t res = 0;
00384 mant = (int)ldexp(frexp(f, &exp), 20);
00385 exp += 788 - 20;
00386 if (mant < 0) {
00387 res |= (1 << 31);
00388 mant = -mant;
00389 }
00390 res |= mant | (exp << 21);
00391 put_bits32(pb, res);
00392 }
00393
00394 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
00395 {
00396 int i;
00397 int ordered = 0;
00398
00399 put_bits(pb, 24, 0x564342);
00400 put_bits(pb, 16, cb->ndimentions);
00401 put_bits(pb, 24, cb->nentries);
00402
00403 for (i = 1; i < cb->nentries; i++)
00404 if (cb->lens[i] < cb->lens[i-1])
00405 break;
00406 if (i == cb->nentries)
00407 ordered = 1;
00408
00409 put_bits(pb, 1, ordered);
00410 if (ordered) {
00411 int len = cb->lens[0];
00412 put_bits(pb, 5, len - 1);
00413 i = 0;
00414 while (i < cb->nentries) {
00415 int j;
00416 for (j = 0; j+i < cb->nentries; j++)
00417 if (cb->lens[j+i] != len)
00418 break;
00419 put_bits(pb, ilog(cb->nentries - i), j);
00420 i += j;
00421 len++;
00422 }
00423 } else {
00424 int sparse = 0;
00425 for (i = 0; i < cb->nentries; i++)
00426 if (!cb->lens[i])
00427 break;
00428 if (i != cb->nentries)
00429 sparse = 1;
00430 put_bits(pb, 1, sparse);
00431
00432 for (i = 0; i < cb->nentries; i++) {
00433 if (sparse)
00434 put_bits(pb, 1, !!cb->lens[i]);
00435 if (cb->lens[i])
00436 put_bits(pb, 5, cb->lens[i] - 1);
00437 }
00438 }
00439
00440 put_bits(pb, 4, cb->lookup);
00441 if (cb->lookup) {
00442 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
00443 int bits = ilog(cb->quantlist[0]);
00444
00445 for (i = 1; i < tmp; i++)
00446 bits = FFMAX(bits, ilog(cb->quantlist[i]));
00447
00448 put_float(pb, cb->min);
00449 put_float(pb, cb->delta);
00450
00451 put_bits(pb, 4, bits - 1);
00452 put_bits(pb, 1, cb->seq_p);
00453
00454 for (i = 0; i < tmp; i++)
00455 put_bits(pb, bits, cb->quantlist[i]);
00456 }
00457 }
00458
00459 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
00460 {
00461 int i;
00462
00463 put_bits(pb, 16, 1);
00464
00465 put_bits(pb, 5, fc->partitions);
00466
00467 for (i = 0; i < fc->partitions; i++)
00468 put_bits(pb, 4, fc->partition_to_class[i]);
00469
00470 for (i = 0; i < fc->nclasses; i++) {
00471 int j, books;
00472
00473 put_bits(pb, 3, fc->classes[i].dim - 1);
00474 put_bits(pb, 2, fc->classes[i].subclass);
00475
00476 if (fc->classes[i].subclass)
00477 put_bits(pb, 8, fc->classes[i].masterbook);
00478
00479 books = (1 << fc->classes[i].subclass);
00480
00481 for (j = 0; j < books; j++)
00482 put_bits(pb, 8, fc->classes[i].books[j] + 1);
00483 }
00484
00485 put_bits(pb, 2, fc->multiplier - 1);
00486 put_bits(pb, 4, fc->rangebits);
00487
00488 for (i = 2; i < fc->values; i++)
00489 put_bits(pb, fc->rangebits, fc->list[i].x);
00490 }
00491
00492 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
00493 {
00494 int i;
00495
00496 put_bits(pb, 16, rc->type);
00497
00498 put_bits(pb, 24, rc->begin);
00499 put_bits(pb, 24, rc->end);
00500 put_bits(pb, 24, rc->partition_size - 1);
00501 put_bits(pb, 6, rc->classifications - 1);
00502 put_bits(pb, 8, rc->classbook);
00503
00504 for (i = 0; i < rc->classifications; i++) {
00505 int j, tmp = 0;
00506 for (j = 0; j < 8; j++)
00507 tmp |= (rc->books[i][j] != -1) << j;
00508
00509 put_bits(pb, 3, tmp & 7);
00510 put_bits(pb, 1, tmp > 7);
00511
00512 if (tmp > 7)
00513 put_bits(pb, 5, tmp >> 3);
00514 }
00515
00516 for (i = 0; i < rc->classifications; i++) {
00517 int j;
00518 for (j = 0; j < 8; j++)
00519 if (rc->books[i][j] != -1)
00520 put_bits(pb, 8, rc->books[i][j]);
00521 }
00522 }
00523
00524 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
00525 {
00526 int i;
00527 PutBitContext pb;
00528 uint8_t buffer[50000] = {0}, *p = buffer;
00529 int buffer_len = sizeof buffer;
00530 int len, hlens[3];
00531
00532
00533 init_put_bits(&pb, p, buffer_len);
00534 put_bits(&pb, 8, 1);
00535 for (i = 0; "vorbis"[i]; i++)
00536 put_bits(&pb, 8, "vorbis"[i]);
00537 put_bits32(&pb, 0);
00538 put_bits(&pb, 8, venc->channels);
00539 put_bits32(&pb, venc->sample_rate);
00540 put_bits32(&pb, 0);
00541 put_bits32(&pb, 0);
00542 put_bits32(&pb, 0);
00543 put_bits(&pb, 4, venc->log2_blocksize[0]);
00544 put_bits(&pb, 4, venc->log2_blocksize[1]);
00545 put_bits(&pb, 1, 1);
00546
00547 flush_put_bits(&pb);
00548 hlens[0] = put_bits_count(&pb) >> 3;
00549 buffer_len -= hlens[0];
00550 p += hlens[0];
00551
00552
00553 init_put_bits(&pb, p, buffer_len);
00554 put_bits(&pb, 8, 3);
00555 for (i = 0; "vorbis"[i]; i++)
00556 put_bits(&pb, 8, "vorbis"[i]);
00557 put_bits32(&pb, 0);
00558 put_bits32(&pb, 0);
00559 put_bits(&pb, 1, 1);
00560
00561 flush_put_bits(&pb);
00562 hlens[1] = put_bits_count(&pb) >> 3;
00563 buffer_len -= hlens[1];
00564 p += hlens[1];
00565
00566
00567 init_put_bits(&pb, p, buffer_len);
00568 put_bits(&pb, 8, 5);
00569 for (i = 0; "vorbis"[i]; i++)
00570 put_bits(&pb, 8, "vorbis"[i]);
00571
00572
00573 put_bits(&pb, 8, venc->ncodebooks - 1);
00574 for (i = 0; i < venc->ncodebooks; i++)
00575 put_codebook_header(&pb, &venc->codebooks[i]);
00576
00577
00578 put_bits(&pb, 6, 0);
00579 put_bits(&pb, 16, 0);
00580
00581
00582 put_bits(&pb, 6, venc->nfloors - 1);
00583 for (i = 0; i < venc->nfloors; i++)
00584 put_floor_header(&pb, &venc->floors[i]);
00585
00586
00587 put_bits(&pb, 6, venc->nresidues - 1);
00588 for (i = 0; i < venc->nresidues; i++)
00589 put_residue_header(&pb, &venc->residues[i]);
00590
00591
00592 put_bits(&pb, 6, venc->nmappings - 1);
00593 for (i = 0; i < venc->nmappings; i++) {
00594 vorbis_enc_mapping *mc = &venc->mappings[i];
00595 int j;
00596 put_bits(&pb, 16, 0);
00597
00598 put_bits(&pb, 1, mc->submaps > 1);
00599 if (mc->submaps > 1)
00600 put_bits(&pb, 4, mc->submaps - 1);
00601
00602 put_bits(&pb, 1, !!mc->coupling_steps);
00603 if (mc->coupling_steps) {
00604 put_bits(&pb, 8, mc->coupling_steps - 1);
00605 for (j = 0; j < mc->coupling_steps; j++) {
00606 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
00607 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
00608 }
00609 }
00610
00611 put_bits(&pb, 2, 0);
00612
00613 if (mc->submaps > 1)
00614 for (j = 0; j < venc->channels; j++)
00615 put_bits(&pb, 4, mc->mux[j]);
00616
00617 for (j = 0; j < mc->submaps; j++) {
00618 put_bits(&pb, 8, 0);
00619 put_bits(&pb, 8, mc->floor[j]);
00620 put_bits(&pb, 8, mc->residue[j]);
00621 }
00622 }
00623
00624
00625 put_bits(&pb, 6, venc->nmodes - 1);
00626 for (i = 0; i < venc->nmodes; i++) {
00627 put_bits(&pb, 1, venc->modes[i].blockflag);
00628 put_bits(&pb, 16, 0);
00629 put_bits(&pb, 16, 0);
00630 put_bits(&pb, 8, venc->modes[i].mapping);
00631 }
00632
00633 put_bits(&pb, 1, 1);
00634
00635 flush_put_bits(&pb);
00636 hlens[2] = put_bits_count(&pb) >> 3;
00637
00638 len = hlens[0] + hlens[1] + hlens[2];
00639 p = *out = av_mallocz(64 + len + len/255);
00640
00641 *p++ = 2;
00642 p += av_xiphlacing(p, hlens[0]);
00643 p += av_xiphlacing(p, hlens[1]);
00644 buffer_len = 0;
00645 for (i = 0; i < 3; i++) {
00646 memcpy(p, buffer + buffer_len, hlens[i]);
00647 p += hlens[i];
00648 buffer_len += hlens[i];
00649 }
00650
00651 return p - *out;
00652 }
00653
00654 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
00655 {
00656 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
00657 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
00658 int j;
00659 float average = 0;
00660
00661 for (j = begin; j < end; j++)
00662 average += fabs(coeffs[j]);
00663 return average / (end - begin);
00664 }
00665
00666 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
00667 float *coeffs, uint_fast16_t *posts, int samples)
00668 {
00669 int range = 255 / fc->multiplier + 1;
00670 int i;
00671 float tot_average = 0.;
00672 float averages[fc->values];
00673 for (i = 0; i < fc->values; i++) {
00674 averages[i] = get_floor_average(fc, coeffs, i);
00675 tot_average += averages[i];
00676 }
00677 tot_average /= fc->values;
00678 tot_average /= venc->quality;
00679
00680 for (i = 0; i < fc->values; i++) {
00681 int position = fc->list[fc->list[i].sort].x;
00682 float average = averages[i];
00683 int j;
00684
00685 average *= pow(tot_average / average, 0.5) * pow(1.25, position/200.);
00686 for (j = 0; j < range - 1; j++)
00687 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
00688 break;
00689 posts[fc->list[i].sort] = j;
00690 }
00691 }
00692
00693 static int render_point(int x0, int y0, int x1, int y1, int x)
00694 {
00695 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
00696 }
00697
00698 static void floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
00699 PutBitContext *pb, uint_fast16_t *posts,
00700 float *floor, int samples)
00701 {
00702 int range = 255 / fc->multiplier + 1;
00703 int coded[fc->values];
00704 int i, counter;
00705
00706 put_bits(pb, 1, 1);
00707 put_bits(pb, ilog(range - 1), posts[0]);
00708 put_bits(pb, ilog(range - 1), posts[1]);
00709 coded[0] = coded[1] = 1;
00710
00711 for (i = 2; i < fc->values; i++) {
00712 int predicted = render_point(fc->list[fc->list[i].low].x,
00713 posts[fc->list[i].low],
00714 fc->list[fc->list[i].high].x,
00715 posts[fc->list[i].high],
00716 fc->list[i].x);
00717 int highroom = range - predicted;
00718 int lowroom = predicted;
00719 int room = FFMIN(highroom, lowroom);
00720 if (predicted == posts[i]) {
00721 coded[i] = 0;
00722 continue;
00723 } else {
00724 if (!coded[fc->list[i].low ])
00725 coded[fc->list[i].low ] = -1;
00726 if (!coded[fc->list[i].high])
00727 coded[fc->list[i].high] = -1;
00728 }
00729 if (posts[i] > predicted) {
00730 if (posts[i] - predicted > room)
00731 coded[i] = posts[i] - predicted + lowroom;
00732 else
00733 coded[i] = (posts[i] - predicted) << 1;
00734 } else {
00735 if (predicted - posts[i] > room)
00736 coded[i] = predicted - posts[i] + highroom - 1;
00737 else
00738 coded[i] = ((predicted - posts[i]) << 1) - 1;
00739 }
00740 }
00741
00742 counter = 2;
00743 for (i = 0; i < fc->partitions; i++) {
00744 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
00745 int k, cval = 0, csub = 1<<c->subclass;
00746 if (c->subclass) {
00747 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
00748 int cshift = 0;
00749 for (k = 0; k < c->dim; k++) {
00750 int l;
00751 for (l = 0; l < csub; l++) {
00752 int maxval = 1;
00753 if (c->books[l] != -1)
00754 maxval = venc->codebooks[c->books[l]].nentries;
00755
00756 if (coded[counter + k] < maxval)
00757 break;
00758 }
00759 assert(l != csub);
00760 cval |= l << cshift;
00761 cshift += c->subclass;
00762 }
00763 put_codeword(pb, book, cval);
00764 }
00765 for (k = 0; k < c->dim; k++) {
00766 int book = c->books[cval & (csub-1)];
00767 int entry = coded[counter++];
00768 cval >>= c->subclass;
00769 if (book == -1)
00770 continue;
00771 if (entry == -1)
00772 entry = 0;
00773 put_codeword(pb, &venc->codebooks[book], entry);
00774 }
00775 }
00776
00777 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
00778 fc->multiplier, floor, samples);
00779 }
00780
00781 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
00782 float *num)
00783 {
00784 int i, entry = -1;
00785 float distance = FLT_MAX;
00786 assert(book->dimentions);
00787 for (i = 0; i < book->nentries; i++) {
00788 float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
00789 int j;
00790 if (!book->lens[i])
00791 continue;
00792 for (j = 0; j < book->ndimentions; j++)
00793 d -= vec[j] * num[j];
00794 if (distance > d) {
00795 entry = i;
00796 distance = d;
00797 }
00798 }
00799 put_codeword(pb, book, entry);
00800 return &book->dimentions[entry * book->ndimentions];
00801 }
00802
00803 static void residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
00804 PutBitContext *pb, float *coeffs, int samples,
00805 int real_ch)
00806 {
00807 int pass, i, j, p, k;
00808 int psize = rc->partition_size;
00809 int partitions = (rc->end - rc->begin) / psize;
00810 int channels = (rc->type == 2) ? 1 : real_ch;
00811 int classes[channels][partitions];
00812 int classwords = venc->codebooks[rc->classbook].ndimentions;
00813
00814 assert(rc->type == 2);
00815 assert(real_ch == 2);
00816 for (p = 0; p < partitions; p++) {
00817 float max1 = 0., max2 = 0.;
00818 int s = rc->begin + p * psize;
00819 for (k = s; k < s + psize; k += 2) {
00820 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
00821 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
00822 }
00823
00824 for (i = 0; i < rc->classifications - 1; i++)
00825 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
00826 break;
00827 classes[0][p] = i;
00828 }
00829
00830 for (pass = 0; pass < 8; pass++) {
00831 p = 0;
00832 while (p < partitions) {
00833 if (pass == 0)
00834 for (j = 0; j < channels; j++) {
00835 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
00836 int entry = 0;
00837 for (i = 0; i < classwords; i++) {
00838 entry *= rc->classifications;
00839 entry += classes[j][p + i];
00840 }
00841 put_codeword(pb, book, entry);
00842 }
00843 for (i = 0; i < classwords && p < partitions; i++, p++) {
00844 for (j = 0; j < channels; j++) {
00845 int nbook = rc->books[classes[j][p]][pass];
00846 vorbis_enc_codebook * book = &venc->codebooks[nbook];
00847 float *buf = coeffs + samples*j + rc->begin + p*psize;
00848 if (nbook == -1)
00849 continue;
00850
00851 assert(rc->type == 0 || rc->type == 2);
00852 assert(!(psize % book->ndimentions));
00853
00854 if (rc->type == 0) {
00855 for (k = 0; k < psize; k += book->ndimentions) {
00856 float *a = put_vector(book, pb, &buf[k]);
00857 int l;
00858 for (l = 0; l < book->ndimentions; l++)
00859 buf[k + l] -= a[l];
00860 }
00861 } else {
00862 int s = rc->begin + p * psize, a1, b1;
00863 a1 = (s % real_ch) * samples;
00864 b1 = s / real_ch;
00865 s = real_ch * samples;
00866 for (k = 0; k < psize; k += book->ndimentions) {
00867 int dim, a2 = a1, b2 = b1;
00868 float vec[book->ndimentions], *pv = vec;
00869 for (dim = book->ndimentions; dim--; ) {
00870 *pv++ = coeffs[a2 + b2];
00871 if ((a2 += samples) == s) {
00872 a2 = 0;
00873 b2++;
00874 }
00875 }
00876 pv = put_vector(book, pb, vec);
00877 for (dim = book->ndimentions; dim--; ) {
00878 coeffs[a1 + b1] -= *pv++;
00879 if ((a1 += samples) == s) {
00880 a1 = 0;
00881 b1++;
00882 }
00883 }
00884 }
00885 }
00886 }
00887 }
00888 }
00889 }
00890 }
00891
00892 static int apply_window_and_mdct(vorbis_enc_context *venc, signed short *audio,
00893 int samples)
00894 {
00895 int i, j, channel;
00896 const float * win = venc->win[0];
00897 int window_len = 1 << (venc->log2_blocksize[0] - 1);
00898 float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
00899
00900
00901 if (!venc->have_saved && !samples)
00902 return 0;
00903
00904 if (venc->have_saved) {
00905 for (channel = 0; channel < venc->channels; channel++)
00906 memcpy(venc->samples + channel * window_len * 2,
00907 venc->saved + channel * window_len, sizeof(float) * window_len);
00908 } else {
00909 for (channel = 0; channel < venc->channels; channel++)
00910 memset(venc->samples + channel * window_len * 2, 0,
00911 sizeof(float) * window_len);
00912 }
00913
00914 if (samples) {
00915 for (channel = 0; channel < venc->channels; channel++) {
00916 float * offset = venc->samples + channel*window_len*2 + window_len;
00917 j = channel;
00918 for (i = 0; i < samples; i++, j += venc->channels)
00919 offset[i] = -audio[j] / 32768. / n * win[window_len - i - 1];
00920 }
00921 } else {
00922 for (channel = 0; channel < venc->channels; channel++)
00923 memset(venc->samples + channel * window_len * 2 + window_len,
00924 0, sizeof(float) * window_len);
00925 }
00926
00927 for (channel = 0; channel < venc->channels; channel++)
00928 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
00929 venc->samples + channel * window_len * 2);
00930
00931 if (samples) {
00932 for (channel = 0; channel < venc->channels; channel++) {
00933 float *offset = venc->saved + channel * window_len;
00934 j = channel;
00935 for (i = 0; i < samples; i++, j += venc->channels)
00936 offset[i] = -audio[j] / 32768. / n * win[i];
00937 }
00938 venc->have_saved = 1;
00939 } else {
00940 venc->have_saved = 0;
00941 }
00942 return 1;
00943 }
00944
00945 static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
00946 {
00947 vorbis_enc_context *venc = avccontext->priv_data;
00948
00949 if (avccontext->channels != 2) {
00950 av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
00951 return -1;
00952 }
00953
00954 create_vorbis_context(venc, avccontext);
00955
00956 if (avccontext->flags & CODEC_FLAG_QSCALE)
00957 venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.;
00958 else
00959 venc->quality = 1.;
00960 venc->quality *= venc->quality;
00961
00962 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
00963
00964 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);
00965
00966 avccontext->coded_frame = avcodec_alloc_frame();
00967 avccontext->coded_frame->key_frame = 1;
00968
00969 return 0;
00970 }
00971
00972 static int vorbis_encode_frame(AVCodecContext *avccontext,
00973 unsigned char *packets,
00974 int buf_size, void *data)
00975 {
00976 vorbis_enc_context *venc = avccontext->priv_data;
00977 signed short *audio = data;
00978 int samples = data ? avccontext->frame_size : 0;
00979 vorbis_enc_mode *mode;
00980 vorbis_enc_mapping *mapping;
00981 PutBitContext pb;
00982 int i;
00983
00984 if (!apply_window_and_mdct(venc, audio, samples))
00985 return 0;
00986 samples = 1 << (venc->log2_blocksize[0] - 1);
00987
00988 init_put_bits(&pb, packets, buf_size);
00989
00990 put_bits(&pb, 1, 0);
00991
00992 put_bits(&pb, ilog(venc->nmodes - 1), 0);
00993
00994 mode = &venc->modes[0];
00995 mapping = &venc->mappings[mode->mapping];
00996 if (mode->blockflag) {
00997 put_bits(&pb, 1, 0);
00998 put_bits(&pb, 1, 0);
00999 }
01000
01001 for (i = 0; i < venc->channels; i++) {
01002 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
01003 uint_fast16_t posts[fc->values];
01004 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
01005 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
01006 }
01007
01008 for (i = 0; i < venc->channels * samples; i++)
01009 venc->coeffs[i] /= venc->floor[i];
01010
01011 for (i = 0; i < mapping->coupling_steps; i++) {
01012 float *mag = venc->coeffs + mapping->magnitude[i] * samples;
01013 float *ang = venc->coeffs + mapping->angle[i] * samples;
01014 int j;
01015 for (j = 0; j < samples; j++) {
01016 float a = ang[j];
01017 ang[j] -= mag[j];
01018 if (mag[j] > 0)
01019 ang[j] = -ang[j];
01020 if (ang[j] < 0)
01021 mag[j] = a;
01022 }
01023 }
01024
01025 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
01026 &pb, venc->coeffs, samples, venc->channels);
01027
01028 avccontext->coded_frame->pts = venc->sample_count;
01029 venc->sample_count += avccontext->frame_size;
01030 flush_put_bits(&pb);
01031 return put_bits_count(&pb) >> 3;
01032 }
01033
01034
01035 static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
01036 {
01037 vorbis_enc_context *venc = avccontext->priv_data;
01038 int i;
01039
01040 if (venc->codebooks)
01041 for (i = 0; i < venc->ncodebooks; i++) {
01042 av_freep(&venc->codebooks[i].lens);
01043 av_freep(&venc->codebooks[i].codewords);
01044 av_freep(&venc->codebooks[i].quantlist);
01045 av_freep(&venc->codebooks[i].dimentions);
01046 av_freep(&venc->codebooks[i].pow2);
01047 }
01048 av_freep(&venc->codebooks);
01049
01050 if (venc->floors)
01051 for (i = 0; i < venc->nfloors; i++) {
01052 int j;
01053 if (venc->floors[i].classes)
01054 for (j = 0; j < venc->floors[i].nclasses; j++)
01055 av_freep(&venc->floors[i].classes[j].books);
01056 av_freep(&venc->floors[i].classes);
01057 av_freep(&venc->floors[i].partition_to_class);
01058 av_freep(&venc->floors[i].list);
01059 }
01060 av_freep(&venc->floors);
01061
01062 if (venc->residues)
01063 for (i = 0; i < venc->nresidues; i++) {
01064 av_freep(&venc->residues[i].books);
01065 av_freep(&venc->residues[i].maxes);
01066 }
01067 av_freep(&venc->residues);
01068
01069 if (venc->mappings)
01070 for (i = 0; i < venc->nmappings; i++) {
01071 av_freep(&venc->mappings[i].mux);
01072 av_freep(&venc->mappings[i].floor);
01073 av_freep(&venc->mappings[i].residue);
01074 av_freep(&venc->mappings[i].magnitude);
01075 av_freep(&venc->mappings[i].angle);
01076 }
01077 av_freep(&venc->mappings);
01078
01079 av_freep(&venc->modes);
01080
01081 av_freep(&venc->saved);
01082 av_freep(&venc->samples);
01083 av_freep(&venc->floor);
01084 av_freep(&venc->coeffs);
01085
01086 ff_mdct_end(&venc->mdct[0]);
01087 ff_mdct_end(&venc->mdct[1]);
01088
01089 av_freep(&avccontext->coded_frame);
01090 av_freep(&avccontext->extradata);
01091
01092 return 0 ;
01093 }
01094
01095 AVCodec vorbis_encoder = {
01096 "vorbis",
01097 AVMEDIA_TYPE_AUDIO,
01098 CODEC_ID_VORBIS,
01099 sizeof(vorbis_enc_context),
01100 vorbis_encode_init,
01101 vorbis_encode_frame,
01102 vorbis_encode_close,
01103 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
01104 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
01105 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
01106 };