You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1102 lines
34KB

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