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