vorbis_enc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Native Vorbis encoder.
  23. * @author Oded Shimon <ods15@ods15.dyndns.org>
  24. */
  25. #include <float.h>
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "fft.h"
  29. #include "vorbis.h"
  30. #include "vorbis_enc_data.h"
  31. #define BITSTREAM_WRITER_LE
  32. #include "put_bits.h"
  33. #undef NDEBUG
  34. #include <assert.h>
  35. typedef struct {
  36. int nentries;
  37. uint8_t *lens;
  38. uint32_t *codewords;
  39. int ndimentions;
  40. float min;
  41. float delta;
  42. int seq_p;
  43. int lookup;
  44. int *quantlist;
  45. float *dimentions;
  46. float *pow2;
  47. } vorbis_enc_codebook;
  48. typedef struct {
  49. int dim;
  50. int subclass;
  51. int masterbook;
  52. int *books;
  53. } vorbis_enc_floor_class;
  54. typedef struct {
  55. int partitions;
  56. int *partition_to_class;
  57. int nclasses;
  58. vorbis_enc_floor_class *classes;
  59. int multiplier;
  60. int rangebits;
  61. int values;
  62. vorbis_floor1_entry *list;
  63. } vorbis_enc_floor;
  64. typedef struct {
  65. int type;
  66. int begin;
  67. int end;
  68. int partition_size;
  69. int classifications;
  70. int classbook;
  71. int8_t (*books)[8];
  72. float (*maxes)[2];
  73. } vorbis_enc_residue;
  74. typedef struct {
  75. int submaps;
  76. int *mux;
  77. int *floor;
  78. int *residue;
  79. int coupling_steps;
  80. int *magnitude;
  81. int *angle;
  82. } vorbis_enc_mapping;
  83. typedef struct {
  84. int blockflag;
  85. int mapping;
  86. } vorbis_enc_mode;
  87. typedef struct {
  88. int channels;
  89. int sample_rate;
  90. int log2_blocksize[2];
  91. FFTContext mdct[2];
  92. const float *win[2];
  93. int have_saved;
  94. float *saved;
  95. float *samples;
  96. float *floor; // also used for tmp values for mdct
  97. float *coeffs; // also used for residue after floor
  98. float quality;
  99. int ncodebooks;
  100. vorbis_enc_codebook *codebooks;
  101. int nfloors;
  102. vorbis_enc_floor *floors;
  103. int nresidues;
  104. vorbis_enc_residue *residues;
  105. int nmappings;
  106. vorbis_enc_mapping *mappings;
  107. int nmodes;
  108. vorbis_enc_mode *modes;
  109. int64_t sample_count;
  110. } vorbis_enc_context;
  111. static inline void put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
  112. int entry)
  113. {
  114. assert(entry >= 0);
  115. assert(entry < cb->nentries);
  116. assert(cb->lens[entry]);
  117. put_bits(pb, cb->lens[entry], cb->codewords[entry]);
  118. }
  119. static int cb_lookup_vals(int lookup, int dimentions, int entries)
  120. {
  121. if (lookup == 1)
  122. return ff_vorbis_nth_root(entries, dimentions);
  123. else if (lookup == 2)
  124. return dimentions *entries;
  125. return 0;
  126. }
  127. static void ready_codebook(vorbis_enc_codebook *cb)
  128. {
  129. int i;
  130. ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
  131. if (!cb->lookup) {
  132. cb->pow2 = cb->dimentions = NULL;
  133. } else {
  134. int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  135. cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
  136. cb->pow2 = av_mallocz(sizeof(float) * cb->nentries);
  137. for (i = 0; i < cb->nentries; i++) {
  138. float last = 0;
  139. int j;
  140. int div = 1;
  141. for (j = 0; j < cb->ndimentions; j++) {
  142. int off;
  143. if (cb->lookup == 1)
  144. off = (i / div) % vals; // lookup type 1
  145. else
  146. off = i * cb->ndimentions + j; // lookup type 2
  147. cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
  148. if (cb->seq_p)
  149. last = cb->dimentions[i * cb->ndimentions + j];
  150. cb->pow2[i] += cb->dimentions[i * cb->ndimentions + j] * cb->dimentions[i * cb->ndimentions + j];
  151. div *= vals;
  152. }
  153. cb->pow2[i] /= 2.;
  154. }
  155. }
  156. }
  157. static void ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
  158. {
  159. int i;
  160. assert(rc->type == 2);
  161. rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
  162. for (i = 0; i < rc->classifications; i++) {
  163. int j;
  164. vorbis_enc_codebook * cb;
  165. for (j = 0; j < 8; j++)
  166. if (rc->books[i][j] != -1)
  167. break;
  168. if (j == 8) // zero
  169. continue;
  170. cb = &venc->codebooks[rc->books[i][j]];
  171. assert(cb->ndimentions >= 2);
  172. assert(cb->lookup);
  173. for (j = 0; j < cb->nentries; j++) {
  174. float a;
  175. if (!cb->lens[j])
  176. continue;
  177. a = fabs(cb->dimentions[j * cb->ndimentions]);
  178. if (a > rc->maxes[i][0])
  179. rc->maxes[i][0] = a;
  180. a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
  181. if (a > rc->maxes[i][1])
  182. rc->maxes[i][1] = a;
  183. }
  184. }
  185. // small bias
  186. for (i = 0; i < rc->classifications; i++) {
  187. rc->maxes[i][0] += 0.8;
  188. rc->maxes[i][1] += 0.8;
  189. }
  190. }
  191. static void create_vorbis_context(vorbis_enc_context *venc,
  192. AVCodecContext *avccontext)
  193. {
  194. vorbis_enc_floor *fc;
  195. vorbis_enc_residue *rc;
  196. vorbis_enc_mapping *mc;
  197. int i, book;
  198. venc->channels = avccontext->channels;
  199. venc->sample_rate = avccontext->sample_rate;
  200. venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
  201. venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
  202. venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
  203. // codebook 0..14 - floor1 book, values 0..255
  204. // codebook 15 residue masterbook
  205. // codebook 16..29 residue
  206. for (book = 0; book < venc->ncodebooks; book++) {
  207. vorbis_enc_codebook *cb = &venc->codebooks[book];
  208. int vals;
  209. cb->ndimentions = cvectors[book].dim;
  210. cb->nentries = cvectors[book].real_len;
  211. cb->min = cvectors[book].min;
  212. cb->delta = cvectors[book].delta;
  213. cb->lookup = cvectors[book].lookup;
  214. cb->seq_p = 0;
  215. cb->lens = av_malloc(sizeof(uint8_t) * cb->nentries);
  216. cb->codewords = av_malloc(sizeof(uint32_t) * cb->nentries);
  217. memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
  218. memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
  219. if (cb->lookup) {
  220. vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  221. cb->quantlist = av_malloc(sizeof(int) * vals);
  222. for (i = 0; i < vals; i++)
  223. cb->quantlist[i] = cvectors[book].quant[i];
  224. } else {
  225. cb->quantlist = NULL;
  226. }
  227. ready_codebook(cb);
  228. }
  229. venc->nfloors = 1;
  230. venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
  231. // just 1 floor
  232. fc = &venc->floors[0];
  233. fc->partitions = 8;
  234. fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
  235. fc->nclasses = 0;
  236. for (i = 0; i < fc->partitions; i++) {
  237. static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
  238. fc->partition_to_class[i] = a[i];
  239. fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
  240. }
  241. fc->nclasses++;
  242. fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses);
  243. for (i = 0; i < fc->nclasses; i++) {
  244. vorbis_enc_floor_class * c = &fc->classes[i];
  245. int j, books;
  246. c->dim = floor_classes[i].dim;
  247. c->subclass = floor_classes[i].subclass;
  248. c->masterbook = floor_classes[i].masterbook;
  249. books = (1 << c->subclass);
  250. c->books = av_malloc(sizeof(int) * books);
  251. for (j = 0; j < books; j++)
  252. c->books[j] = floor_classes[i].nbooks[j];
  253. }
  254. fc->multiplier = 2;
  255. fc->rangebits = venc->log2_blocksize[0] - 1;
  256. fc->values = 2;
  257. for (i = 0; i < fc->partitions; i++)
  258. fc->values += fc->classes[fc->partition_to_class[i]].dim;
  259. fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values);
  260. fc->list[0].x = 0;
  261. fc->list[1].x = 1 << fc->rangebits;
  262. for (i = 2; i < fc->values; i++) {
  263. static const int a[] = {
  264. 93, 23,372, 6, 46,186,750, 14, 33, 65,
  265. 130,260,556, 3, 10, 18, 28, 39, 55, 79,
  266. 111,158,220,312,464,650,850
  267. };
  268. fc->list[i].x = a[i - 2];
  269. }
  270. ff_vorbis_ready_floor1_list(fc->list, fc->values);
  271. venc->nresidues = 1;
  272. venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
  273. // single residue
  274. rc = &venc->residues[0];
  275. rc->type = 2;
  276. rc->begin = 0;
  277. rc->end = 1600;
  278. rc->partition_size = 32;
  279. rc->classifications = 10;
  280. rc->classbook = 15;
  281. rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
  282. {
  283. static const int8_t a[10][8] = {
  284. { -1, -1, -1, -1, -1, -1, -1, -1, },
  285. { -1, -1, 16, -1, -1, -1, -1, -1, },
  286. { -1, -1, 17, -1, -1, -1, -1, -1, },
  287. { -1, -1, 18, -1, -1, -1, -1, -1, },
  288. { -1, -1, 19, -1, -1, -1, -1, -1, },
  289. { -1, -1, 20, -1, -1, -1, -1, -1, },
  290. { -1, -1, 21, -1, -1, -1, -1, -1, },
  291. { 22, 23, -1, -1, -1, -1, -1, -1, },
  292. { 24, 25, -1, -1, -1, -1, -1, -1, },
  293. { 26, 27, 28, -1, -1, -1, -1, -1, },
  294. };
  295. memcpy(rc->books, a, sizeof a);
  296. }
  297. ready_residue(rc, venc);
  298. venc->nmappings = 1;
  299. venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
  300. // single mapping
  301. mc = &venc->mappings[0];
  302. mc->submaps = 1;
  303. mc->mux = av_malloc(sizeof(int) * venc->channels);
  304. for (i = 0; i < venc->channels; i++)
  305. mc->mux[i] = 0;
  306. mc->floor = av_malloc(sizeof(int) * mc->submaps);
  307. mc->residue = av_malloc(sizeof(int) * mc->submaps);
  308. for (i = 0; i < mc->submaps; i++) {
  309. mc->floor[i] = 0;
  310. mc->residue[i] = 0;
  311. }
  312. mc->coupling_steps = venc->channels == 2 ? 1 : 0;
  313. mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
  314. mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
  315. if (mc->coupling_steps) {
  316. mc->magnitude[0] = 0;
  317. mc->angle[0] = 1;
  318. }
  319. venc->nmodes = 1;
  320. venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
  321. // single mode
  322. venc->modes[0].blockflag = 0;
  323. venc->modes[0].mapping = 0;
  324. venc->have_saved = 0;
  325. venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
  326. venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]));
  327. venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
  328. venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->log2_blocksize[1]) / 2);
  329. venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
  330. venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
  331. ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0);
  332. ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0);
  333. }
  334. static void put_float(PutBitContext *pb, float f)
  335. {
  336. int exp, mant;
  337. uint32_t res = 0;
  338. mant = (int)ldexp(frexp(f, &exp), 20);
  339. exp += 788 - 20;
  340. if (mant < 0) {
  341. res |= (1 << 31);
  342. mant = -mant;
  343. }
  344. res |= mant | (exp << 21);
  345. put_bits32(pb, res);
  346. }
  347. static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
  348. {
  349. int i;
  350. int ordered = 0;
  351. put_bits(pb, 24, 0x564342); //magic
  352. put_bits(pb, 16, cb->ndimentions);
  353. put_bits(pb, 24, cb->nentries);
  354. for (i = 1; i < cb->nentries; i++)
  355. if (cb->lens[i] < cb->lens[i-1])
  356. break;
  357. if (i == cb->nentries)
  358. ordered = 1;
  359. put_bits(pb, 1, ordered);
  360. if (ordered) {
  361. int len = cb->lens[0];
  362. put_bits(pb, 5, len - 1);
  363. i = 0;
  364. while (i < cb->nentries) {
  365. int j;
  366. for (j = 0; j+i < cb->nentries; j++)
  367. if (cb->lens[j+i] != len)
  368. break;
  369. put_bits(pb, ilog(cb->nentries - i), j);
  370. i += j;
  371. len++;
  372. }
  373. } else {
  374. int sparse = 0;
  375. for (i = 0; i < cb->nentries; i++)
  376. if (!cb->lens[i])
  377. break;
  378. if (i != cb->nentries)
  379. sparse = 1;
  380. put_bits(pb, 1, sparse);
  381. for (i = 0; i < cb->nentries; i++) {
  382. if (sparse)
  383. put_bits(pb, 1, !!cb->lens[i]);
  384. if (cb->lens[i])
  385. put_bits(pb, 5, cb->lens[i] - 1);
  386. }
  387. }
  388. put_bits(pb, 4, cb->lookup);
  389. if (cb->lookup) {
  390. int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  391. int bits = ilog(cb->quantlist[0]);
  392. for (i = 1; i < tmp; i++)
  393. bits = FFMAX(bits, ilog(cb->quantlist[i]));
  394. put_float(pb, cb->min);
  395. put_float(pb, cb->delta);
  396. put_bits(pb, 4, bits - 1);
  397. put_bits(pb, 1, cb->seq_p);
  398. for (i = 0; i < tmp; i++)
  399. put_bits(pb, bits, cb->quantlist[i]);
  400. }
  401. }
  402. static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
  403. {
  404. int i;
  405. put_bits(pb, 16, 1); // type, only floor1 is supported
  406. put_bits(pb, 5, fc->partitions);
  407. for (i = 0; i < fc->partitions; i++)
  408. put_bits(pb, 4, fc->partition_to_class[i]);
  409. for (i = 0; i < fc->nclasses; i++) {
  410. int j, books;
  411. put_bits(pb, 3, fc->classes[i].dim - 1);
  412. put_bits(pb, 2, fc->classes[i].subclass);
  413. if (fc->classes[i].subclass)
  414. put_bits(pb, 8, fc->classes[i].masterbook);
  415. books = (1 << fc->classes[i].subclass);
  416. for (j = 0; j < books; j++)
  417. put_bits(pb, 8, fc->classes[i].books[j] + 1);
  418. }
  419. put_bits(pb, 2, fc->multiplier - 1);
  420. put_bits(pb, 4, fc->rangebits);
  421. for (i = 2; i < fc->values; i++)
  422. put_bits(pb, fc->rangebits, fc->list[i].x);
  423. }
  424. static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
  425. {
  426. int i;
  427. put_bits(pb, 16, rc->type);
  428. put_bits(pb, 24, rc->begin);
  429. put_bits(pb, 24, rc->end);
  430. put_bits(pb, 24, rc->partition_size - 1);
  431. put_bits(pb, 6, rc->classifications - 1);
  432. put_bits(pb, 8, rc->classbook);
  433. for (i = 0; i < rc->classifications; i++) {
  434. int j, tmp = 0;
  435. for (j = 0; j < 8; j++)
  436. tmp |= (rc->books[i][j] != -1) << j;
  437. put_bits(pb, 3, tmp & 7);
  438. put_bits(pb, 1, tmp > 7);
  439. if (tmp > 7)
  440. put_bits(pb, 5, tmp >> 3);
  441. }
  442. for (i = 0; i < rc->classifications; i++) {
  443. int j;
  444. for (j = 0; j < 8; j++)
  445. if (rc->books[i][j] != -1)
  446. put_bits(pb, 8, rc->books[i][j]);
  447. }
  448. }
  449. static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
  450. {
  451. int i;
  452. PutBitContext pb;
  453. uint8_t buffer[50000] = {0}, *p = buffer;
  454. int buffer_len = sizeof buffer;
  455. int len, hlens[3];
  456. // identification header
  457. init_put_bits(&pb, p, buffer_len);
  458. put_bits(&pb, 8, 1); //magic
  459. for (i = 0; "vorbis"[i]; i++)
  460. put_bits(&pb, 8, "vorbis"[i]);
  461. put_bits32(&pb, 0); // version
  462. put_bits(&pb, 8, venc->channels);
  463. put_bits32(&pb, venc->sample_rate);
  464. put_bits32(&pb, 0); // bitrate
  465. put_bits32(&pb, 0); // bitrate
  466. put_bits32(&pb, 0); // bitrate
  467. put_bits(&pb, 4, venc->log2_blocksize[0]);
  468. put_bits(&pb, 4, venc->log2_blocksize[1]);
  469. put_bits(&pb, 1, 1); // framing
  470. flush_put_bits(&pb);
  471. hlens[0] = put_bits_count(&pb) >> 3;
  472. buffer_len -= hlens[0];
  473. p += hlens[0];
  474. // comment header
  475. init_put_bits(&pb, p, buffer_len);
  476. put_bits(&pb, 8, 3); //magic
  477. for (i = 0; "vorbis"[i]; i++)
  478. put_bits(&pb, 8, "vorbis"[i]);
  479. put_bits32(&pb, 0); // vendor length TODO
  480. put_bits32(&pb, 0); // amount of comments
  481. put_bits(&pb, 1, 1); // framing
  482. flush_put_bits(&pb);
  483. hlens[1] = put_bits_count(&pb) >> 3;
  484. buffer_len -= hlens[1];
  485. p += hlens[1];
  486. // setup header
  487. init_put_bits(&pb, p, buffer_len);
  488. put_bits(&pb, 8, 5); //magic
  489. for (i = 0; "vorbis"[i]; i++)
  490. put_bits(&pb, 8, "vorbis"[i]);
  491. // codebooks
  492. put_bits(&pb, 8, venc->ncodebooks - 1);
  493. for (i = 0; i < venc->ncodebooks; i++)
  494. put_codebook_header(&pb, &venc->codebooks[i]);
  495. // time domain, reserved, zero
  496. put_bits(&pb, 6, 0);
  497. put_bits(&pb, 16, 0);
  498. // floors
  499. put_bits(&pb, 6, venc->nfloors - 1);
  500. for (i = 0; i < venc->nfloors; i++)
  501. put_floor_header(&pb, &venc->floors[i]);
  502. // residues
  503. put_bits(&pb, 6, venc->nresidues - 1);
  504. for (i = 0; i < venc->nresidues; i++)
  505. put_residue_header(&pb, &venc->residues[i]);
  506. // mappings
  507. put_bits(&pb, 6, venc->nmappings - 1);
  508. for (i = 0; i < venc->nmappings; i++) {
  509. vorbis_enc_mapping *mc = &venc->mappings[i];
  510. int j;
  511. put_bits(&pb, 16, 0); // mapping type
  512. put_bits(&pb, 1, mc->submaps > 1);
  513. if (mc->submaps > 1)
  514. put_bits(&pb, 4, mc->submaps - 1);
  515. put_bits(&pb, 1, !!mc->coupling_steps);
  516. if (mc->coupling_steps) {
  517. put_bits(&pb, 8, mc->coupling_steps - 1);
  518. for (j = 0; j < mc->coupling_steps; j++) {
  519. put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
  520. put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
  521. }
  522. }
  523. put_bits(&pb, 2, 0); // reserved
  524. if (mc->submaps > 1)
  525. for (j = 0; j < venc->channels; j++)
  526. put_bits(&pb, 4, mc->mux[j]);
  527. for (j = 0; j < mc->submaps; j++) {
  528. put_bits(&pb, 8, 0); // reserved time configuration
  529. put_bits(&pb, 8, mc->floor[j]);
  530. put_bits(&pb, 8, mc->residue[j]);
  531. }
  532. }
  533. // modes
  534. put_bits(&pb, 6, venc->nmodes - 1);
  535. for (i = 0; i < venc->nmodes; i++) {
  536. put_bits(&pb, 1, venc->modes[i].blockflag);
  537. put_bits(&pb, 16, 0); // reserved window type
  538. put_bits(&pb, 16, 0); // reserved transform type
  539. put_bits(&pb, 8, venc->modes[i].mapping);
  540. }
  541. put_bits(&pb, 1, 1); // framing
  542. flush_put_bits(&pb);
  543. hlens[2] = put_bits_count(&pb) >> 3;
  544. len = hlens[0] + hlens[1] + hlens[2];
  545. p = *out = av_mallocz(64 + len + len/255);
  546. *p++ = 2;
  547. p += av_xiphlacing(p, hlens[0]);
  548. p += av_xiphlacing(p, hlens[1]);
  549. buffer_len = 0;
  550. for (i = 0; i < 3; i++) {
  551. memcpy(p, buffer + buffer_len, hlens[i]);
  552. p += hlens[i];
  553. buffer_len += hlens[i];
  554. }
  555. return p - *out;
  556. }
  557. static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
  558. {
  559. int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
  560. int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
  561. int j;
  562. float average = 0;
  563. for (j = begin; j < end; j++)
  564. average += fabs(coeffs[j]);
  565. return average / (end - begin);
  566. }
  567. static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
  568. float *coeffs, uint_fast16_t *posts, int samples)
  569. {
  570. int range = 255 / fc->multiplier + 1;
  571. int i;
  572. float tot_average = 0.;
  573. float averages[fc->values];
  574. for (i = 0; i < fc->values; i++) {
  575. averages[i] = get_floor_average(fc, coeffs, i);
  576. tot_average += averages[i];
  577. }
  578. tot_average /= fc->values;
  579. tot_average /= venc->quality;
  580. for (i = 0; i < fc->values; i++) {
  581. int position = fc->list[fc->list[i].sort].x;
  582. float average = averages[i];
  583. int j;
  584. average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
  585. for (j = 0; j < range - 1; j++)
  586. if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
  587. break;
  588. posts[fc->list[i].sort] = j;
  589. }
  590. }
  591. static int render_point(int x0, int y0, int x1, int y1, int x)
  592. {
  593. return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
  594. }
  595. static void floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
  596. PutBitContext *pb, uint_fast16_t *posts,
  597. float *floor, int samples)
  598. {
  599. int range = 255 / fc->multiplier + 1;
  600. int coded[fc->values]; // first 2 values are unused
  601. int i, counter;
  602. put_bits(pb, 1, 1); // non zero
  603. put_bits(pb, ilog(range - 1), posts[0]);
  604. put_bits(pb, ilog(range - 1), posts[1]);
  605. coded[0] = coded[1] = 1;
  606. for (i = 2; i < fc->values; i++) {
  607. int predicted = render_point(fc->list[fc->list[i].low].x,
  608. posts[fc->list[i].low],
  609. fc->list[fc->list[i].high].x,
  610. posts[fc->list[i].high],
  611. fc->list[i].x);
  612. int highroom = range - predicted;
  613. int lowroom = predicted;
  614. int room = FFMIN(highroom, lowroom);
  615. if (predicted == posts[i]) {
  616. coded[i] = 0; // must be used later as flag!
  617. continue;
  618. } else {
  619. if (!coded[fc->list[i].low ])
  620. coded[fc->list[i].low ] = -1;
  621. if (!coded[fc->list[i].high])
  622. coded[fc->list[i].high] = -1;
  623. }
  624. if (posts[i] > predicted) {
  625. if (posts[i] - predicted > room)
  626. coded[i] = posts[i] - predicted + lowroom;
  627. else
  628. coded[i] = (posts[i] - predicted) << 1;
  629. } else {
  630. if (predicted - posts[i] > room)
  631. coded[i] = predicted - posts[i] + highroom - 1;
  632. else
  633. coded[i] = ((predicted - posts[i]) << 1) - 1;
  634. }
  635. }
  636. counter = 2;
  637. for (i = 0; i < fc->partitions; i++) {
  638. vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
  639. int k, cval = 0, csub = 1<<c->subclass;
  640. if (c->subclass) {
  641. vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
  642. int cshift = 0;
  643. for (k = 0; k < c->dim; k++) {
  644. int l;
  645. for (l = 0; l < csub; l++) {
  646. int maxval = 1;
  647. if (c->books[l] != -1)
  648. maxval = venc->codebooks[c->books[l]].nentries;
  649. // coded could be -1, but this still works, cause that is 0
  650. if (coded[counter + k] < maxval)
  651. break;
  652. }
  653. assert(l != csub);
  654. cval |= l << cshift;
  655. cshift += c->subclass;
  656. }
  657. put_codeword(pb, book, cval);
  658. }
  659. for (k = 0; k < c->dim; k++) {
  660. int book = c->books[cval & (csub-1)];
  661. int entry = coded[counter++];
  662. cval >>= c->subclass;
  663. if (book == -1)
  664. continue;
  665. if (entry == -1)
  666. entry = 0;
  667. put_codeword(pb, &venc->codebooks[book], entry);
  668. }
  669. }
  670. ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
  671. fc->multiplier, floor, samples);
  672. }
  673. static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
  674. float *num)
  675. {
  676. int i, entry = -1;
  677. float distance = FLT_MAX;
  678. assert(book->dimentions);
  679. for (i = 0; i < book->nentries; i++) {
  680. float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
  681. int j;
  682. if (!book->lens[i])
  683. continue;
  684. for (j = 0; j < book->ndimentions; j++)
  685. d -= vec[j] * num[j];
  686. if (distance > d) {
  687. entry = i;
  688. distance = d;
  689. }
  690. }
  691. put_codeword(pb, book, entry);
  692. return &book->dimentions[entry * book->ndimentions];
  693. }
  694. static void residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
  695. PutBitContext *pb, float *coeffs, int samples,
  696. int real_ch)
  697. {
  698. int pass, i, j, p, k;
  699. int psize = rc->partition_size;
  700. int partitions = (rc->end - rc->begin) / psize;
  701. int channels = (rc->type == 2) ? 1 : real_ch;
  702. int classes[channels][partitions];
  703. int classwords = venc->codebooks[rc->classbook].ndimentions;
  704. assert(rc->type == 2);
  705. assert(real_ch == 2);
  706. for (p = 0; p < partitions; p++) {
  707. float max1 = 0., max2 = 0.;
  708. int s = rc->begin + p * psize;
  709. for (k = s; k < s + psize; k += 2) {
  710. max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
  711. max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
  712. }
  713. for (i = 0; i < rc->classifications - 1; i++)
  714. if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
  715. break;
  716. classes[0][p] = i;
  717. }
  718. for (pass = 0; pass < 8; pass++) {
  719. p = 0;
  720. while (p < partitions) {
  721. if (pass == 0)
  722. for (j = 0; j < channels; j++) {
  723. vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
  724. int entry = 0;
  725. for (i = 0; i < classwords; i++) {
  726. entry *= rc->classifications;
  727. entry += classes[j][p + i];
  728. }
  729. put_codeword(pb, book, entry);
  730. }
  731. for (i = 0; i < classwords && p < partitions; i++, p++) {
  732. for (j = 0; j < channels; j++) {
  733. int nbook = rc->books[classes[j][p]][pass];
  734. vorbis_enc_codebook * book = &venc->codebooks[nbook];
  735. float *buf = coeffs + samples*j + rc->begin + p*psize;
  736. if (nbook == -1)
  737. continue;
  738. assert(rc->type == 0 || rc->type == 2);
  739. assert(!(psize % book->ndimentions));
  740. if (rc->type == 0) {
  741. for (k = 0; k < psize; k += book->ndimentions) {
  742. float *a = put_vector(book, pb, &buf[k]);
  743. int l;
  744. for (l = 0; l < book->ndimentions; l++)
  745. buf[k + l] -= a[l];
  746. }
  747. } else {
  748. int s = rc->begin + p * psize, a1, b1;
  749. a1 = (s % real_ch) * samples;
  750. b1 = s / real_ch;
  751. s = real_ch * samples;
  752. for (k = 0; k < psize; k += book->ndimentions) {
  753. int dim, a2 = a1, b2 = b1;
  754. float vec[book->ndimentions], *pv = vec;
  755. for (dim = book->ndimentions; dim--; ) {
  756. *pv++ = coeffs[a2 + b2];
  757. if ((a2 += samples) == s) {
  758. a2 = 0;
  759. b2++;
  760. }
  761. }
  762. pv = put_vector(book, pb, vec);
  763. for (dim = book->ndimentions; dim--; ) {
  764. coeffs[a1 + b1] -= *pv++;
  765. if ((a1 += samples) == s) {
  766. a1 = 0;
  767. b1++;
  768. }
  769. }
  770. }
  771. }
  772. }
  773. }
  774. }
  775. }
  776. }
  777. static int apply_window_and_mdct(vorbis_enc_context *venc, const signed short *audio,
  778. int samples)
  779. {
  780. int i, j, channel;
  781. const float * win = venc->win[0];
  782. int window_len = 1 << (venc->log2_blocksize[0] - 1);
  783. float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
  784. // FIXME use dsp
  785. if (!venc->have_saved && !samples)
  786. return 0;
  787. if (venc->have_saved) {
  788. for (channel = 0; channel < venc->channels; channel++)
  789. memcpy(venc->samples + channel * window_len * 2,
  790. venc->saved + channel * window_len, sizeof(float) * window_len);
  791. } else {
  792. for (channel = 0; channel < venc->channels; channel++)
  793. memset(venc->samples + channel * window_len * 2, 0,
  794. sizeof(float) * window_len);
  795. }
  796. if (samples) {
  797. for (channel = 0; channel < venc->channels; channel++) {
  798. float * offset = venc->samples + channel*window_len*2 + window_len;
  799. j = channel;
  800. for (i = 0; i < samples; i++, j += venc->channels)
  801. offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
  802. }
  803. } else {
  804. for (channel = 0; channel < venc->channels; channel++)
  805. memset(venc->samples + channel * window_len * 2 + window_len,
  806. 0, sizeof(float) * window_len);
  807. }
  808. for (channel = 0; channel < venc->channels; channel++)
  809. ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
  810. venc->samples + channel * window_len * 2);
  811. if (samples) {
  812. for (channel = 0; channel < venc->channels; channel++) {
  813. float *offset = venc->saved + channel * window_len;
  814. j = channel;
  815. for (i = 0; i < samples; i++, j += venc->channels)
  816. offset[i] = audio[j] / 32768. / n * win[i];
  817. }
  818. venc->have_saved = 1;
  819. } else {
  820. venc->have_saved = 0;
  821. }
  822. return 1;
  823. }
  824. static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
  825. {
  826. vorbis_enc_context *venc = avccontext->priv_data;
  827. if (avccontext->channels != 2) {
  828. av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
  829. return -1;
  830. }
  831. create_vorbis_context(venc, avccontext);
  832. if (avccontext->flags & CODEC_FLAG_QSCALE)
  833. venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.;
  834. else
  835. venc->quality = 0.03;
  836. venc->quality *= venc->quality;
  837. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  838. avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);
  839. avccontext->coded_frame = avcodec_alloc_frame();
  840. avccontext->coded_frame->key_frame = 1;
  841. return 0;
  842. }
  843. static int vorbis_encode_frame(AVCodecContext *avccontext,
  844. unsigned char *packets,
  845. int buf_size, void *data)
  846. {
  847. vorbis_enc_context *venc = avccontext->priv_data;
  848. const signed short *audio = data;
  849. int samples = data ? avccontext->frame_size : 0;
  850. vorbis_enc_mode *mode;
  851. vorbis_enc_mapping *mapping;
  852. PutBitContext pb;
  853. int i;
  854. if (!apply_window_and_mdct(venc, audio, samples))
  855. return 0;
  856. samples = 1 << (venc->log2_blocksize[0] - 1);
  857. init_put_bits(&pb, packets, buf_size);
  858. put_bits(&pb, 1, 0); // magic bit
  859. put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
  860. mode = &venc->modes[0];
  861. mapping = &venc->mappings[mode->mapping];
  862. if (mode->blockflag) {
  863. put_bits(&pb, 1, 0);
  864. put_bits(&pb, 1, 0);
  865. }
  866. for (i = 0; i < venc->channels; i++) {
  867. vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
  868. uint_fast16_t posts[fc->values];
  869. floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
  870. floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
  871. }
  872. for (i = 0; i < venc->channels * samples; i++)
  873. venc->coeffs[i] /= venc->floor[i];
  874. for (i = 0; i < mapping->coupling_steps; i++) {
  875. float *mag = venc->coeffs + mapping->magnitude[i] * samples;
  876. float *ang = venc->coeffs + mapping->angle[i] * samples;
  877. int j;
  878. for (j = 0; j < samples; j++) {
  879. float a = ang[j];
  880. ang[j] -= mag[j];
  881. if (mag[j] > 0)
  882. ang[j] = -ang[j];
  883. if (ang[j] < 0)
  884. mag[j] = a;
  885. }
  886. }
  887. residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
  888. &pb, venc->coeffs, samples, venc->channels);
  889. avccontext->coded_frame->pts = venc->sample_count;
  890. venc->sample_count += avccontext->frame_size;
  891. flush_put_bits(&pb);
  892. return put_bits_count(&pb) >> 3;
  893. }
  894. static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
  895. {
  896. vorbis_enc_context *venc = avccontext->priv_data;
  897. int i;
  898. if (venc->codebooks)
  899. for (i = 0; i < venc->ncodebooks; i++) {
  900. av_freep(&venc->codebooks[i].lens);
  901. av_freep(&venc->codebooks[i].codewords);
  902. av_freep(&venc->codebooks[i].quantlist);
  903. av_freep(&venc->codebooks[i].dimentions);
  904. av_freep(&venc->codebooks[i].pow2);
  905. }
  906. av_freep(&venc->codebooks);
  907. if (venc->floors)
  908. for (i = 0; i < venc->nfloors; i++) {
  909. int j;
  910. if (venc->floors[i].classes)
  911. for (j = 0; j < venc->floors[i].nclasses; j++)
  912. av_freep(&venc->floors[i].classes[j].books);
  913. av_freep(&venc->floors[i].classes);
  914. av_freep(&venc->floors[i].partition_to_class);
  915. av_freep(&venc->floors[i].list);
  916. }
  917. av_freep(&venc->floors);
  918. if (venc->residues)
  919. for (i = 0; i < venc->nresidues; i++) {
  920. av_freep(&venc->residues[i].books);
  921. av_freep(&venc->residues[i].maxes);
  922. }
  923. av_freep(&venc->residues);
  924. if (venc->mappings)
  925. for (i = 0; i < venc->nmappings; i++) {
  926. av_freep(&venc->mappings[i].mux);
  927. av_freep(&venc->mappings[i].floor);
  928. av_freep(&venc->mappings[i].residue);
  929. av_freep(&venc->mappings[i].magnitude);
  930. av_freep(&venc->mappings[i].angle);
  931. }
  932. av_freep(&venc->mappings);
  933. av_freep(&venc->modes);
  934. av_freep(&venc->saved);
  935. av_freep(&venc->samples);
  936. av_freep(&venc->floor);
  937. av_freep(&venc->coeffs);
  938. ff_mdct_end(&venc->mdct[0]);
  939. ff_mdct_end(&venc->mdct[1]);
  940. av_freep(&avccontext->coded_frame);
  941. av_freep(&avccontext->extradata);
  942. return 0 ;
  943. }
  944. AVCodec vorbis_encoder = {
  945. "vorbis",
  946. AVMEDIA_TYPE_AUDIO,
  947. CODEC_ID_VORBIS,
  948. sizeof(vorbis_enc_context),
  949. vorbis_encode_init,
  950. vorbis_encode_frame,
  951. vorbis_encode_close,
  952. .capabilities= CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL,
  953. .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  954. .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
  955. };