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 #define MAX_CHANNELS 2
00130 #define MAX_CODEBOOK_DIM 8
00131
00132 #define MAX_FLOOR_CLASS_DIM 4
00133 #define NUM_FLOOR_PARTITIONS 8
00134 #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
00135
00136 #define RESIDUE_SIZE 1600
00137 #define RESIDUE_PART_SIZE 32
00138 #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
00139
00140 static inline void put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
00141 int entry)
00142 {
00143 assert(entry >= 0);
00144 assert(entry < cb->nentries);
00145 assert(cb->lens[entry]);
00146 put_bits(pb, cb->lens[entry], cb->codewords[entry]);
00147 }
00148
00149 static int cb_lookup_vals(int lookup, int dimentions, int entries)
00150 {
00151 if (lookup == 1)
00152 return ff_vorbis_nth_root(entries, dimentions);
00153 else if (lookup == 2)
00154 return dimentions *entries;
00155 return 0;
00156 }
00157
00158 static int ready_codebook(vorbis_enc_codebook *cb)
00159 {
00160 int i;
00161
00162 ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
00163
00164 if (!cb->lookup) {
00165 cb->pow2 = cb->dimentions = NULL;
00166 } else {
00167 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
00168 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
00169 cb->pow2 = av_mallocz(sizeof(float) * cb->nentries);
00170 if (!cb->dimentions || !cb->pow2)
00171 return AVERROR(ENOMEM);
00172 for (i = 0; i < cb->nentries; i++) {
00173 float last = 0;
00174 int j;
00175 int div = 1;
00176 for (j = 0; j < cb->ndimentions; j++) {
00177 int off;
00178 if (cb->lookup == 1)
00179 off = (i / div) % vals;
00180 else
00181 off = i * cb->ndimentions + j;
00182
00183 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
00184 if (cb->seq_p)
00185 last = cb->dimentions[i * cb->ndimentions + j];
00186 cb->pow2[i] += cb->dimentions[i * cb->ndimentions + j] * cb->dimentions[i * cb->ndimentions + j];
00187 div *= vals;
00188 }
00189 cb->pow2[i] /= 2.;
00190 }
00191 }
00192 return 0;
00193 }
00194
00195 static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
00196 {
00197 int i;
00198 assert(rc->type == 2);
00199 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
00200 if (!rc->maxes)
00201 return AVERROR(ENOMEM);
00202 for (i = 0; i < rc->classifications; i++) {
00203 int j;
00204 vorbis_enc_codebook * cb;
00205 for (j = 0; j < 8; j++)
00206 if (rc->books[i][j] != -1)
00207 break;
00208 if (j == 8)
00209 continue;
00210 cb = &venc->codebooks[rc->books[i][j]];
00211 assert(cb->ndimentions >= 2);
00212 assert(cb->lookup);
00213
00214 for (j = 0; j < cb->nentries; j++) {
00215 float a;
00216 if (!cb->lens[j])
00217 continue;
00218 a = fabs(cb->dimentions[j * cb->ndimentions]);
00219 if (a > rc->maxes[i][0])
00220 rc->maxes[i][0] = a;
00221 a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
00222 if (a > rc->maxes[i][1])
00223 rc->maxes[i][1] = a;
00224 }
00225 }
00226
00227 for (i = 0; i < rc->classifications; i++) {
00228 rc->maxes[i][0] += 0.8;
00229 rc->maxes[i][1] += 0.8;
00230 }
00231 return 0;
00232 }
00233
00234 static int create_vorbis_context(vorbis_enc_context *venc,
00235 AVCodecContext *avccontext)
00236 {
00237 vorbis_enc_floor *fc;
00238 vorbis_enc_residue *rc;
00239 vorbis_enc_mapping *mc;
00240 int i, book, ret;
00241
00242 venc->channels = avccontext->channels;
00243 venc->sample_rate = avccontext->sample_rate;
00244 venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
00245
00246 venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
00247 venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
00248 if (!venc->codebooks)
00249 return AVERROR(ENOMEM);
00250
00251
00252
00253
00254 for (book = 0; book < venc->ncodebooks; book++) {
00255 vorbis_enc_codebook *cb = &venc->codebooks[book];
00256 int vals;
00257 cb->ndimentions = cvectors[book].dim;
00258 cb->nentries = cvectors[book].real_len;
00259 cb->min = cvectors[book].min;
00260 cb->delta = cvectors[book].delta;
00261 cb->lookup = cvectors[book].lookup;
00262 cb->seq_p = 0;
00263
00264 cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
00265 cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
00266 if (!cb->lens || !cb->codewords)
00267 return AVERROR(ENOMEM);
00268 memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
00269 memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
00270
00271 if (cb->lookup) {
00272 vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
00273 cb->quantlist = av_malloc(sizeof(int) * vals);
00274 if (!cb->quantlist)
00275 return AVERROR(ENOMEM);
00276 for (i = 0; i < vals; i++)
00277 cb->quantlist[i] = cvectors[book].quant[i];
00278 } else {
00279 cb->quantlist = NULL;
00280 }
00281 if ((ret = ready_codebook(cb)) < 0)
00282 return ret;
00283 }
00284
00285 venc->nfloors = 1;
00286 venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
00287 if (!venc->floors)
00288 return AVERROR(ENOMEM);
00289
00290
00291 fc = &venc->floors[0];
00292 fc->partitions = NUM_FLOOR_PARTITIONS;
00293 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
00294 if (!fc->partition_to_class)
00295 return AVERROR(ENOMEM);
00296 fc->nclasses = 0;
00297 for (i = 0; i < fc->partitions; i++) {
00298 static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
00299 fc->partition_to_class[i] = a[i];
00300 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
00301 }
00302 fc->nclasses++;
00303 fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses);
00304 if (!fc->classes)
00305 return AVERROR(ENOMEM);
00306 for (i = 0; i < fc->nclasses; i++) {
00307 vorbis_enc_floor_class * c = &fc->classes[i];
00308 int j, books;
00309 c->dim = floor_classes[i].dim;
00310 c->subclass = floor_classes[i].subclass;
00311 c->masterbook = floor_classes[i].masterbook;
00312 books = (1 << c->subclass);
00313 c->books = av_malloc(sizeof(int) * books);
00314 if (!c->books)
00315 return AVERROR(ENOMEM);
00316 for (j = 0; j < books; j++)
00317 c->books[j] = floor_classes[i].nbooks[j];
00318 }
00319 fc->multiplier = 2;
00320 fc->rangebits = venc->log2_blocksize[0] - 1;
00321
00322 fc->values = 2;
00323 for (i = 0; i < fc->partitions; i++)
00324 fc->values += fc->classes[fc->partition_to_class[i]].dim;
00325
00326 fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values);
00327 if (!fc->list)
00328 return AVERROR(ENOMEM);
00329 fc->list[0].x = 0;
00330 fc->list[1].x = 1 << fc->rangebits;
00331 for (i = 2; i < fc->values; i++) {
00332 static const int a[] = {
00333 93, 23,372, 6, 46,186,750, 14, 33, 65,
00334 130,260,556, 3, 10, 18, 28, 39, 55, 79,
00335 111,158,220,312,464,650,850
00336 };
00337 fc->list[i].x = a[i - 2];
00338 }
00339 if (ff_vorbis_ready_floor1_list(avccontext, fc->list, fc->values))
00340 return AVERROR_BUG;
00341
00342 venc->nresidues = 1;
00343 venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
00344 if (!venc->residues)
00345 return AVERROR(ENOMEM);
00346
00347
00348 rc = &venc->residues[0];
00349 rc->type = 2;
00350 rc->begin = 0;
00351 rc->end = 1600;
00352 rc->partition_size = 32;
00353 rc->classifications = 10;
00354 rc->classbook = 15;
00355 rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
00356 if (!rc->books)
00357 return AVERROR(ENOMEM);
00358 {
00359 static const int8_t a[10][8] = {
00360 { -1, -1, -1, -1, -1, -1, -1, -1, },
00361 { -1, -1, 16, -1, -1, -1, -1, -1, },
00362 { -1, -1, 17, -1, -1, -1, -1, -1, },
00363 { -1, -1, 18, -1, -1, -1, -1, -1, },
00364 { -1, -1, 19, -1, -1, -1, -1, -1, },
00365 { -1, -1, 20, -1, -1, -1, -1, -1, },
00366 { -1, -1, 21, -1, -1, -1, -1, -1, },
00367 { 22, 23, -1, -1, -1, -1, -1, -1, },
00368 { 24, 25, -1, -1, -1, -1, -1, -1, },
00369 { 26, 27, 28, -1, -1, -1, -1, -1, },
00370 };
00371 memcpy(rc->books, a, sizeof a);
00372 }
00373 if ((ret = ready_residue(rc, venc)) < 0)
00374 return ret;
00375
00376 venc->nmappings = 1;
00377 venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
00378 if (!venc->mappings)
00379 return AVERROR(ENOMEM);
00380
00381
00382 mc = &venc->mappings[0];
00383 mc->submaps = 1;
00384 mc->mux = av_malloc(sizeof(int) * venc->channels);
00385 if (!mc->mux)
00386 return AVERROR(ENOMEM);
00387 for (i = 0; i < venc->channels; i++)
00388 mc->mux[i] = 0;
00389 mc->floor = av_malloc(sizeof(int) * mc->submaps);
00390 mc->residue = av_malloc(sizeof(int) * mc->submaps);
00391 if (!mc->floor || !mc->residue)
00392 return AVERROR(ENOMEM);
00393 for (i = 0; i < mc->submaps; i++) {
00394 mc->floor[i] = 0;
00395 mc->residue[i] = 0;
00396 }
00397 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
00398 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
00399 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
00400 if (!mc->magnitude || !mc->angle)
00401 return AVERROR(ENOMEM);
00402 if (mc->coupling_steps) {
00403 mc->magnitude[0] = 0;
00404 mc->angle[0] = 1;
00405 }
00406
00407 venc->nmodes = 1;
00408 venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
00409 if (!venc->modes)
00410 return AVERROR(ENOMEM);
00411
00412
00413 venc->modes[0].blockflag = 0;
00414 venc->modes[0].mapping = 0;
00415
00416 venc->have_saved = 0;
00417 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
00418 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
00419 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
00420 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
00421 if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs)
00422 return AVERROR(ENOMEM);
00423
00424 venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
00425 venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
00426
00427 if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
00428 return ret;
00429 if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
00430 return ret;
00431
00432 return 0;
00433 }
00434
00435 static void put_float(PutBitContext *pb, float f)
00436 {
00437 int exp, mant;
00438 uint32_t res = 0;
00439 mant = (int)ldexp(frexp(f, &exp), 20);
00440 exp += 788 - 20;
00441 if (mant < 0) {
00442 res |= (1U << 31);
00443 mant = -mant;
00444 }
00445 res |= mant | (exp << 21);
00446 put_bits32(pb, res);
00447 }
00448
00449 static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
00450 {
00451 int i;
00452 int ordered = 0;
00453
00454 put_bits(pb, 24, 0x564342);
00455 put_bits(pb, 16, cb->ndimentions);
00456 put_bits(pb, 24, cb->nentries);
00457
00458 for (i = 1; i < cb->nentries; i++)
00459 if (cb->lens[i] < cb->lens[i-1])
00460 break;
00461 if (i == cb->nentries)
00462 ordered = 1;
00463
00464 put_bits(pb, 1, ordered);
00465 if (ordered) {
00466 int len = cb->lens[0];
00467 put_bits(pb, 5, len - 1);
00468 i = 0;
00469 while (i < cb->nentries) {
00470 int j;
00471 for (j = 0; j+i < cb->nentries; j++)
00472 if (cb->lens[j+i] != len)
00473 break;
00474 put_bits(pb, ilog(cb->nentries - i), j);
00475 i += j;
00476 len++;
00477 }
00478 } else {
00479 int sparse = 0;
00480 for (i = 0; i < cb->nentries; i++)
00481 if (!cb->lens[i])
00482 break;
00483 if (i != cb->nentries)
00484 sparse = 1;
00485 put_bits(pb, 1, sparse);
00486
00487 for (i = 0; i < cb->nentries; i++) {
00488 if (sparse)
00489 put_bits(pb, 1, !!cb->lens[i]);
00490 if (cb->lens[i])
00491 put_bits(pb, 5, cb->lens[i] - 1);
00492 }
00493 }
00494
00495 put_bits(pb, 4, cb->lookup);
00496 if (cb->lookup) {
00497 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
00498 int bits = ilog(cb->quantlist[0]);
00499
00500 for (i = 1; i < tmp; i++)
00501 bits = FFMAX(bits, ilog(cb->quantlist[i]));
00502
00503 put_float(pb, cb->min);
00504 put_float(pb, cb->delta);
00505
00506 put_bits(pb, 4, bits - 1);
00507 put_bits(pb, 1, cb->seq_p);
00508
00509 for (i = 0; i < tmp; i++)
00510 put_bits(pb, bits, cb->quantlist[i]);
00511 }
00512 }
00513
00514 static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
00515 {
00516 int i;
00517
00518 put_bits(pb, 16, 1);
00519
00520 put_bits(pb, 5, fc->partitions);
00521
00522 for (i = 0; i < fc->partitions; i++)
00523 put_bits(pb, 4, fc->partition_to_class[i]);
00524
00525 for (i = 0; i < fc->nclasses; i++) {
00526 int j, books;
00527
00528 put_bits(pb, 3, fc->classes[i].dim - 1);
00529 put_bits(pb, 2, fc->classes[i].subclass);
00530
00531 if (fc->classes[i].subclass)
00532 put_bits(pb, 8, fc->classes[i].masterbook);
00533
00534 books = (1 << fc->classes[i].subclass);
00535
00536 for (j = 0; j < books; j++)
00537 put_bits(pb, 8, fc->classes[i].books[j] + 1);
00538 }
00539
00540 put_bits(pb, 2, fc->multiplier - 1);
00541 put_bits(pb, 4, fc->rangebits);
00542
00543 for (i = 2; i < fc->values; i++)
00544 put_bits(pb, fc->rangebits, fc->list[i].x);
00545 }
00546
00547 static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
00548 {
00549 int i;
00550
00551 put_bits(pb, 16, rc->type);
00552
00553 put_bits(pb, 24, rc->begin);
00554 put_bits(pb, 24, rc->end);
00555 put_bits(pb, 24, rc->partition_size - 1);
00556 put_bits(pb, 6, rc->classifications - 1);
00557 put_bits(pb, 8, rc->classbook);
00558
00559 for (i = 0; i < rc->classifications; i++) {
00560 int j, tmp = 0;
00561 for (j = 0; j < 8; j++)
00562 tmp |= (rc->books[i][j] != -1) << j;
00563
00564 put_bits(pb, 3, tmp & 7);
00565 put_bits(pb, 1, tmp > 7);
00566
00567 if (tmp > 7)
00568 put_bits(pb, 5, tmp >> 3);
00569 }
00570
00571 for (i = 0; i < rc->classifications; i++) {
00572 int j;
00573 for (j = 0; j < 8; j++)
00574 if (rc->books[i][j] != -1)
00575 put_bits(pb, 8, rc->books[i][j]);
00576 }
00577 }
00578
00579 static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
00580 {
00581 int i;
00582 PutBitContext pb;
00583 uint8_t buffer[50000] = {0}, *p = buffer;
00584 int buffer_len = sizeof buffer;
00585 int len, hlens[3];
00586
00587
00588 init_put_bits(&pb, p, buffer_len);
00589 put_bits(&pb, 8, 1);
00590 for (i = 0; "vorbis"[i]; i++)
00591 put_bits(&pb, 8, "vorbis"[i]);
00592 put_bits32(&pb, 0);
00593 put_bits(&pb, 8, venc->channels);
00594 put_bits32(&pb, venc->sample_rate);
00595 put_bits32(&pb, 0);
00596 put_bits32(&pb, 0);
00597 put_bits32(&pb, 0);
00598 put_bits(&pb, 4, venc->log2_blocksize[0]);
00599 put_bits(&pb, 4, venc->log2_blocksize[1]);
00600 put_bits(&pb, 1, 1);
00601
00602 flush_put_bits(&pb);
00603 hlens[0] = put_bits_count(&pb) >> 3;
00604 buffer_len -= hlens[0];
00605 p += hlens[0];
00606
00607
00608 init_put_bits(&pb, p, buffer_len);
00609 put_bits(&pb, 8, 3);
00610 for (i = 0; "vorbis"[i]; i++)
00611 put_bits(&pb, 8, "vorbis"[i]);
00612 put_bits32(&pb, 0);
00613 put_bits32(&pb, 0);
00614 put_bits(&pb, 1, 1);
00615
00616 flush_put_bits(&pb);
00617 hlens[1] = put_bits_count(&pb) >> 3;
00618 buffer_len -= hlens[1];
00619 p += hlens[1];
00620
00621
00622 init_put_bits(&pb, p, buffer_len);
00623 put_bits(&pb, 8, 5);
00624 for (i = 0; "vorbis"[i]; i++)
00625 put_bits(&pb, 8, "vorbis"[i]);
00626
00627
00628 put_bits(&pb, 8, venc->ncodebooks - 1);
00629 for (i = 0; i < venc->ncodebooks; i++)
00630 put_codebook_header(&pb, &venc->codebooks[i]);
00631
00632
00633 put_bits(&pb, 6, 0);
00634 put_bits(&pb, 16, 0);
00635
00636
00637 put_bits(&pb, 6, venc->nfloors - 1);
00638 for (i = 0; i < venc->nfloors; i++)
00639 put_floor_header(&pb, &venc->floors[i]);
00640
00641
00642 put_bits(&pb, 6, venc->nresidues - 1);
00643 for (i = 0; i < venc->nresidues; i++)
00644 put_residue_header(&pb, &venc->residues[i]);
00645
00646
00647 put_bits(&pb, 6, venc->nmappings - 1);
00648 for (i = 0; i < venc->nmappings; i++) {
00649 vorbis_enc_mapping *mc = &venc->mappings[i];
00650 int j;
00651 put_bits(&pb, 16, 0);
00652
00653 put_bits(&pb, 1, mc->submaps > 1);
00654 if (mc->submaps > 1)
00655 put_bits(&pb, 4, mc->submaps - 1);
00656
00657 put_bits(&pb, 1, !!mc->coupling_steps);
00658 if (mc->coupling_steps) {
00659 put_bits(&pb, 8, mc->coupling_steps - 1);
00660 for (j = 0; j < mc->coupling_steps; j++) {
00661 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
00662 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
00663 }
00664 }
00665
00666 put_bits(&pb, 2, 0);
00667
00668 if (mc->submaps > 1)
00669 for (j = 0; j < venc->channels; j++)
00670 put_bits(&pb, 4, mc->mux[j]);
00671
00672 for (j = 0; j < mc->submaps; j++) {
00673 put_bits(&pb, 8, 0);
00674 put_bits(&pb, 8, mc->floor[j]);
00675 put_bits(&pb, 8, mc->residue[j]);
00676 }
00677 }
00678
00679
00680 put_bits(&pb, 6, venc->nmodes - 1);
00681 for (i = 0; i < venc->nmodes; i++) {
00682 put_bits(&pb, 1, venc->modes[i].blockflag);
00683 put_bits(&pb, 16, 0);
00684 put_bits(&pb, 16, 0);
00685 put_bits(&pb, 8, venc->modes[i].mapping);
00686 }
00687
00688 put_bits(&pb, 1, 1);
00689
00690 flush_put_bits(&pb);
00691 hlens[2] = put_bits_count(&pb) >> 3;
00692
00693 len = hlens[0] + hlens[1] + hlens[2];
00694 p = *out = av_mallocz(64 + len + len/255);
00695 if (!p)
00696 return AVERROR(ENOMEM);
00697
00698 *p++ = 2;
00699 p += av_xiphlacing(p, hlens[0]);
00700 p += av_xiphlacing(p, hlens[1]);
00701 buffer_len = 0;
00702 for (i = 0; i < 3; i++) {
00703 memcpy(p, buffer + buffer_len, hlens[i]);
00704 p += hlens[i];
00705 buffer_len += hlens[i];
00706 }
00707
00708 return p - *out;
00709 }
00710
00711 static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
00712 {
00713 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
00714 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
00715 int j;
00716 float average = 0;
00717
00718 for (j = begin; j < end; j++)
00719 average += fabs(coeffs[j]);
00720 return average / (end - begin);
00721 }
00722
00723 static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
00724 float *coeffs, uint16_t *posts, int samples)
00725 {
00726 int range = 255 / fc->multiplier + 1;
00727 int i;
00728 float tot_average = 0.;
00729 float averages[MAX_FLOOR_VALUES];
00730 for (i = 0; i < fc->values; i++) {
00731 averages[i] = get_floor_average(fc, coeffs, i);
00732 tot_average += averages[i];
00733 }
00734 tot_average /= fc->values;
00735 tot_average /= venc->quality;
00736
00737 for (i = 0; i < fc->values; i++) {
00738 int position = fc->list[fc->list[i].sort].x;
00739 float average = averages[i];
00740 int j;
00741
00742 average = sqrt(tot_average * average) * pow(1.25f, position*0.005f);
00743 for (j = 0; j < range - 1; j++)
00744 if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
00745 break;
00746 posts[fc->list[i].sort] = j;
00747 }
00748 }
00749
00750 static int render_point(int x0, int y0, int x1, int y1, int x)
00751 {
00752 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
00753 }
00754
00755 static void floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
00756 PutBitContext *pb, uint16_t *posts,
00757 float *floor, int samples)
00758 {
00759 int range = 255 / fc->multiplier + 1;
00760 int coded[MAX_FLOOR_VALUES];
00761 int i, counter;
00762
00763 put_bits(pb, 1, 1);
00764 put_bits(pb, ilog(range - 1), posts[0]);
00765 put_bits(pb, ilog(range - 1), posts[1]);
00766 coded[0] = coded[1] = 1;
00767
00768 for (i = 2; i < fc->values; i++) {
00769 int predicted = render_point(fc->list[fc->list[i].low].x,
00770 posts[fc->list[i].low],
00771 fc->list[fc->list[i].high].x,
00772 posts[fc->list[i].high],
00773 fc->list[i].x);
00774 int highroom = range - predicted;
00775 int lowroom = predicted;
00776 int room = FFMIN(highroom, lowroom);
00777 if (predicted == posts[i]) {
00778 coded[i] = 0;
00779 continue;
00780 } else {
00781 if (!coded[fc->list[i].low ])
00782 coded[fc->list[i].low ] = -1;
00783 if (!coded[fc->list[i].high])
00784 coded[fc->list[i].high] = -1;
00785 }
00786 if (posts[i] > predicted) {
00787 if (posts[i] - predicted > room)
00788 coded[i] = posts[i] - predicted + lowroom;
00789 else
00790 coded[i] = (posts[i] - predicted) << 1;
00791 } else {
00792 if (predicted - posts[i] > room)
00793 coded[i] = predicted - posts[i] + highroom - 1;
00794 else
00795 coded[i] = ((predicted - posts[i]) << 1) - 1;
00796 }
00797 }
00798
00799 counter = 2;
00800 for (i = 0; i < fc->partitions; i++) {
00801 vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
00802 int k, cval = 0, csub = 1<<c->subclass;
00803 if (c->subclass) {
00804 vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
00805 int cshift = 0;
00806 for (k = 0; k < c->dim; k++) {
00807 int l;
00808 for (l = 0; l < csub; l++) {
00809 int maxval = 1;
00810 if (c->books[l] != -1)
00811 maxval = venc->codebooks[c->books[l]].nentries;
00812
00813 if (coded[counter + k] < maxval)
00814 break;
00815 }
00816 assert(l != csub);
00817 cval |= l << cshift;
00818 cshift += c->subclass;
00819 }
00820 put_codeword(pb, book, cval);
00821 }
00822 for (k = 0; k < c->dim; k++) {
00823 int book = c->books[cval & (csub-1)];
00824 int entry = coded[counter++];
00825 cval >>= c->subclass;
00826 if (book == -1)
00827 continue;
00828 if (entry == -1)
00829 entry = 0;
00830 put_codeword(pb, &venc->codebooks[book], entry);
00831 }
00832 }
00833
00834 ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
00835 fc->multiplier, floor, samples);
00836 }
00837
00838 static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
00839 float *num)
00840 {
00841 int i, entry = -1;
00842 float distance = FLT_MAX;
00843 assert(book->dimentions);
00844 for (i = 0; i < book->nentries; i++) {
00845 float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
00846 int j;
00847 if (!book->lens[i])
00848 continue;
00849 for (j = 0; j < book->ndimentions; j++)
00850 d -= vec[j] * num[j];
00851 if (distance > d) {
00852 entry = i;
00853 distance = d;
00854 }
00855 }
00856 put_codeword(pb, book, entry);
00857 return &book->dimentions[entry * book->ndimentions];
00858 }
00859
00860 static void residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
00861 PutBitContext *pb, float *coeffs, int samples,
00862 int real_ch)
00863 {
00864 int pass, i, j, p, k;
00865 int psize = rc->partition_size;
00866 int partitions = (rc->end - rc->begin) / psize;
00867 int channels = (rc->type == 2) ? 1 : real_ch;
00868 int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
00869 int classwords = venc->codebooks[rc->classbook].ndimentions;
00870
00871 assert(rc->type == 2);
00872 assert(real_ch == 2);
00873 for (p = 0; p < partitions; p++) {
00874 float max1 = 0., max2 = 0.;
00875 int s = rc->begin + p * psize;
00876 for (k = s; k < s + psize; k += 2) {
00877 max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
00878 max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
00879 }
00880
00881 for (i = 0; i < rc->classifications - 1; i++)
00882 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
00883 break;
00884 classes[0][p] = i;
00885 }
00886
00887 for (pass = 0; pass < 8; pass++) {
00888 p = 0;
00889 while (p < partitions) {
00890 if (pass == 0)
00891 for (j = 0; j < channels; j++) {
00892 vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
00893 int entry = 0;
00894 for (i = 0; i < classwords; i++) {
00895 entry *= rc->classifications;
00896 entry += classes[j][p + i];
00897 }
00898 put_codeword(pb, book, entry);
00899 }
00900 for (i = 0; i < classwords && p < partitions; i++, p++) {
00901 for (j = 0; j < channels; j++) {
00902 int nbook = rc->books[classes[j][p]][pass];
00903 vorbis_enc_codebook * book = &venc->codebooks[nbook];
00904 float *buf = coeffs + samples*j + rc->begin + p*psize;
00905 if (nbook == -1)
00906 continue;
00907
00908 assert(rc->type == 0 || rc->type == 2);
00909 assert(!(psize % book->ndimentions));
00910
00911 if (rc->type == 0) {
00912 for (k = 0; k < psize; k += book->ndimentions) {
00913 float *a = put_vector(book, pb, &buf[k]);
00914 int l;
00915 for (l = 0; l < book->ndimentions; l++)
00916 buf[k + l] -= a[l];
00917 }
00918 } else {
00919 int s = rc->begin + p * psize, a1, b1;
00920 a1 = (s % real_ch) * samples;
00921 b1 = s / real_ch;
00922 s = real_ch * samples;
00923 for (k = 0; k < psize; k += book->ndimentions) {
00924 int dim, a2 = a1, b2 = b1;
00925 float vec[MAX_CODEBOOK_DIM], *pv = vec;
00926 for (dim = book->ndimentions; dim--; ) {
00927 *pv++ = coeffs[a2 + b2];
00928 if ((a2 += samples) == s) {
00929 a2 = 0;
00930 b2++;
00931 }
00932 }
00933 pv = put_vector(book, pb, vec);
00934 for (dim = book->ndimentions; dim--; ) {
00935 coeffs[a1 + b1] -= *pv++;
00936 if ((a1 += samples) == s) {
00937 a1 = 0;
00938 b1++;
00939 }
00940 }
00941 }
00942 }
00943 }
00944 }
00945 }
00946 }
00947 }
00948
00949 static int apply_window_and_mdct(vorbis_enc_context *venc, const signed short *audio,
00950 int samples)
00951 {
00952 int i, j, channel;
00953 const float * win = venc->win[0];
00954 int window_len = 1 << (venc->log2_blocksize[0] - 1);
00955 float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
00956
00957
00958 if (!venc->have_saved && !samples)
00959 return 0;
00960
00961 if (venc->have_saved) {
00962 for (channel = 0; channel < venc->channels; channel++)
00963 memcpy(venc->samples + channel * window_len * 2,
00964 venc->saved + channel * window_len, sizeof(float) * window_len);
00965 } else {
00966 for (channel = 0; channel < venc->channels; channel++)
00967 memset(venc->samples + channel * window_len * 2, 0,
00968 sizeof(float) * window_len);
00969 }
00970
00971 if (samples) {
00972 for (channel = 0; channel < venc->channels; channel++) {
00973 float * offset = venc->samples + channel*window_len*2 + window_len;
00974 j = channel;
00975 for (i = 0; i < samples; i++, j += venc->channels)
00976 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
00977 }
00978 } else {
00979 for (channel = 0; channel < venc->channels; channel++)
00980 memset(venc->samples + channel * window_len * 2 + window_len,
00981 0, sizeof(float) * window_len);
00982 }
00983
00984 for (channel = 0; channel < venc->channels; channel++)
00985 venc->mdct[0].mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
00986 venc->samples + channel * window_len * 2);
00987
00988 if (samples) {
00989 for (channel = 0; channel < venc->channels; channel++) {
00990 float *offset = venc->saved + channel * window_len;
00991 j = channel;
00992 for (i = 0; i < samples; i++, j += venc->channels)
00993 offset[i] = audio[j] / 32768. / n * win[i];
00994 }
00995 venc->have_saved = 1;
00996 } else {
00997 venc->have_saved = 0;
00998 }
00999 return 1;
01000 }
01001
01002 static int vorbis_encode_frame(AVCodecContext *avccontext,
01003 unsigned char *packets,
01004 int buf_size, void *data)
01005 {
01006 vorbis_enc_context *venc = avccontext->priv_data;
01007 const signed short *audio = data;
01008 int samples = data ? avccontext->frame_size : 0;
01009 vorbis_enc_mode *mode;
01010 vorbis_enc_mapping *mapping;
01011 PutBitContext pb;
01012 int i;
01013
01014 if (!apply_window_and_mdct(venc, audio, samples))
01015 return 0;
01016 samples = 1 << (venc->log2_blocksize[0] - 1);
01017
01018 init_put_bits(&pb, packets, buf_size);
01019
01020 put_bits(&pb, 1, 0);
01021
01022 put_bits(&pb, ilog(venc->nmodes - 1), 0);
01023
01024 mode = &venc->modes[0];
01025 mapping = &venc->mappings[mode->mapping];
01026 if (mode->blockflag) {
01027 put_bits(&pb, 1, 0);
01028 put_bits(&pb, 1, 0);
01029 }
01030
01031 for (i = 0; i < venc->channels; i++) {
01032 vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
01033 uint16_t posts[MAX_FLOOR_VALUES];
01034 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
01035 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
01036 }
01037
01038 for (i = 0; i < venc->channels * samples; i++)
01039 venc->coeffs[i] /= venc->floor[i];
01040
01041 for (i = 0; i < mapping->coupling_steps; i++) {
01042 float *mag = venc->coeffs + mapping->magnitude[i] * samples;
01043 float *ang = venc->coeffs + mapping->angle[i] * samples;
01044 int j;
01045 for (j = 0; j < samples; j++) {
01046 float a = ang[j];
01047 ang[j] -= mag[j];
01048 if (mag[j] > 0)
01049 ang[j] = -ang[j];
01050 if (ang[j] < 0)
01051 mag[j] = a;
01052 }
01053 }
01054
01055 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
01056 &pb, venc->coeffs, samples, venc->channels);
01057
01058 avccontext->coded_frame->pts = venc->sample_count;
01059 venc->sample_count += avccontext->frame_size;
01060 flush_put_bits(&pb);
01061 return put_bits_count(&pb) >> 3;
01062 }
01063
01064
01065 static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
01066 {
01067 vorbis_enc_context *venc = avccontext->priv_data;
01068 int i;
01069
01070 if (venc->codebooks)
01071 for (i = 0; i < venc->ncodebooks; i++) {
01072 av_freep(&venc->codebooks[i].lens);
01073 av_freep(&venc->codebooks[i].codewords);
01074 av_freep(&venc->codebooks[i].quantlist);
01075 av_freep(&venc->codebooks[i].dimentions);
01076 av_freep(&venc->codebooks[i].pow2);
01077 }
01078 av_freep(&venc->codebooks);
01079
01080 if (venc->floors)
01081 for (i = 0; i < venc->nfloors; i++) {
01082 int j;
01083 if (venc->floors[i].classes)
01084 for (j = 0; j < venc->floors[i].nclasses; j++)
01085 av_freep(&venc->floors[i].classes[j].books);
01086 av_freep(&venc->floors[i].classes);
01087 av_freep(&venc->floors[i].partition_to_class);
01088 av_freep(&venc->floors[i].list);
01089 }
01090 av_freep(&venc->floors);
01091
01092 if (venc->residues)
01093 for (i = 0; i < venc->nresidues; i++) {
01094 av_freep(&venc->residues[i].books);
01095 av_freep(&venc->residues[i].maxes);
01096 }
01097 av_freep(&venc->residues);
01098
01099 if (venc->mappings)
01100 for (i = 0; i < venc->nmappings; i++) {
01101 av_freep(&venc->mappings[i].mux);
01102 av_freep(&venc->mappings[i].floor);
01103 av_freep(&venc->mappings[i].residue);
01104 av_freep(&venc->mappings[i].magnitude);
01105 av_freep(&venc->mappings[i].angle);
01106 }
01107 av_freep(&venc->mappings);
01108
01109 av_freep(&venc->modes);
01110
01111 av_freep(&venc->saved);
01112 av_freep(&venc->samples);
01113 av_freep(&venc->floor);
01114 av_freep(&venc->coeffs);
01115
01116 ff_mdct_end(&venc->mdct[0]);
01117 ff_mdct_end(&venc->mdct[1]);
01118
01119 av_freep(&avccontext->coded_frame);
01120 av_freep(&avccontext->extradata);
01121
01122 return 0 ;
01123 }
01124
01125 static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
01126 {
01127 vorbis_enc_context *venc = avccontext->priv_data;
01128 int ret;
01129
01130 if (avccontext->channels != 2) {
01131 av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
01132 return -1;
01133 }
01134
01135 if ((ret = create_vorbis_context(venc, avccontext)) < 0)
01136 goto error;
01137
01138 if (avccontext->flags & CODEC_FLAG_QSCALE)
01139 venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.;
01140 else
01141 venc->quality = 0.03;
01142 venc->quality *= venc->quality;
01143
01144 if ((ret = put_main_header(venc, (uint8_t**)&avccontext->extradata)) < 0)
01145 goto error;
01146 avccontext->extradata_size = ret;
01147
01148 avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);
01149
01150 avccontext->coded_frame = avcodec_alloc_frame();
01151 if (!avccontext->coded_frame) {
01152 ret = AVERROR(ENOMEM);
01153 goto error;
01154 }
01155
01156 return 0;
01157 error:
01158 vorbis_encode_close(avccontext);
01159 return ret;
01160 }
01161
01162 AVCodec ff_vorbis_encoder = {
01163 .name = "vorbis",
01164 .type = AVMEDIA_TYPE_AUDIO,
01165 .id = CODEC_ID_VORBIS,
01166 .priv_data_size = sizeof(vorbis_enc_context),
01167 .init = vorbis_encode_init,
01168 .encode = vorbis_encode_frame,
01169 .close = vorbis_encode_close,
01170 .capabilities= CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
01171 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
01172 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
01173 };