• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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