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