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