vorbis_enc.c

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