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.

1105 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. for (i = 0; "vorbis"[i]; i++)
  459. put_bits(&pb, 8, "vorbis"[i]);
  460. put_bits32(&pb, 0); // version
  461. put_bits(&pb, 8, venc->channels);
  462. put_bits32(&pb, venc->sample_rate);
  463. put_bits32(&pb, 0); // bitrate
  464. put_bits32(&pb, 0); // bitrate
  465. put_bits32(&pb, 0); // bitrate
  466. put_bits(&pb, 4, venc->log2_blocksize[0]);
  467. put_bits(&pb, 4, venc->log2_blocksize[1]);
  468. put_bits(&pb, 1, 1); // framing
  469. flush_put_bits(&pb);
  470. hlens[0] = put_bits_count(&pb) >> 3;
  471. buffer_len -= hlens[0];
  472. p += hlens[0];
  473. // comment header
  474. init_put_bits(&pb, p, buffer_len);
  475. put_bits(&pb, 8, 3); //magic
  476. for (i = 0; "vorbis"[i]; i++)
  477. put_bits(&pb, 8, "vorbis"[i]);
  478. put_bits32(&pb, 0); // vendor length TODO
  479. put_bits32(&pb, 0); // amount of comments
  480. put_bits(&pb, 1, 1); // framing
  481. flush_put_bits(&pb);
  482. hlens[1] = put_bits_count(&pb) >> 3;
  483. buffer_len -= hlens[1];
  484. p += hlens[1];
  485. // setup header
  486. init_put_bits(&pb, p, buffer_len);
  487. put_bits(&pb, 8, 5); //magic
  488. for (i = 0; "vorbis"[i]; i++)
  489. put_bits(&pb, 8, "vorbis"[i]);
  490. // codebooks
  491. put_bits(&pb, 8, venc->ncodebooks - 1);
  492. for (i = 0; i < venc->ncodebooks; i++)
  493. put_codebook_header(&pb, &venc->codebooks[i]);
  494. // time domain, reserved, zero
  495. put_bits(&pb, 6, 0);
  496. put_bits(&pb, 16, 0);
  497. // floors
  498. put_bits(&pb, 6, venc->nfloors - 1);
  499. for (i = 0; i < venc->nfloors; i++)
  500. put_floor_header(&pb, &venc->floors[i]);
  501. // residues
  502. put_bits(&pb, 6, venc->nresidues - 1);
  503. for (i = 0; i < venc->nresidues; i++)
  504. put_residue_header(&pb, &venc->residues[i]);
  505. // mappings
  506. put_bits(&pb, 6, venc->nmappings - 1);
  507. for (i = 0; i < venc->nmappings; i++) {
  508. vorbis_enc_mapping *mc = &venc->mappings[i];
  509. int j;
  510. put_bits(&pb, 16, 0); // mapping type
  511. put_bits(&pb, 1, mc->submaps > 1);
  512. if (mc->submaps > 1)
  513. put_bits(&pb, 4, mc->submaps - 1);
  514. put_bits(&pb, 1, !!mc->coupling_steps);
  515. if (mc->coupling_steps) {
  516. put_bits(&pb, 8, mc->coupling_steps - 1);
  517. for (j = 0; j < mc->coupling_steps; j++) {
  518. put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
  519. put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
  520. }
  521. }
  522. put_bits(&pb, 2, 0); // reserved
  523. if (mc->submaps > 1)
  524. for (j = 0; j < venc->channels; j++)
  525. put_bits(&pb, 4, mc->mux[j]);
  526. for (j = 0; j < mc->submaps; j++) {
  527. put_bits(&pb, 8, 0); // reserved time configuration
  528. put_bits(&pb, 8, mc->floor[j]);
  529. put_bits(&pb, 8, mc->residue[j]);
  530. }
  531. }
  532. // modes
  533. put_bits(&pb, 6, venc->nmodes - 1);
  534. for (i = 0; i < venc->nmodes; i++) {
  535. put_bits(&pb, 1, venc->modes[i].blockflag);
  536. put_bits(&pb, 16, 0); // reserved window type
  537. put_bits(&pb, 16, 0); // reserved transform type
  538. put_bits(&pb, 8, venc->modes[i].mapping);
  539. }
  540. put_bits(&pb, 1, 1); // framing
  541. flush_put_bits(&pb);
  542. hlens[2] = put_bits_count(&pb) >> 3;
  543. len = hlens[0] + hlens[1] + hlens[2];
  544. p = *out = av_mallocz(64 + len + len/255);
  545. *p++ = 2;
  546. p += av_xiphlacing(p, hlens[0]);
  547. p += av_xiphlacing(p, hlens[1]);
  548. buffer_len = 0;
  549. for (i = 0; i < 3; i++) {
  550. memcpy(p, buffer + buffer_len, hlens[i]);
  551. p += hlens[i];
  552. buffer_len += hlens[i];
  553. }
  554. return p - *out;
  555. }
  556. static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
  557. {
  558. int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
  559. int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
  560. int j;
  561. float average = 0;
  562. for (j = begin; j < end; j++)
  563. average += fabs(coeffs[j]);
  564. return average / (end - begin);
  565. }
  566. static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
  567. float *coeffs, uint_fast16_t *posts, int samples)
  568. {
  569. int range = 255 / fc->multiplier + 1;
  570. int i;
  571. float tot_average = 0.;
  572. float averages[fc->values];
  573. for (i = 0; i < fc->values; i++) {
  574. averages[i] = get_floor_average(fc, coeffs, i);
  575. tot_average += averages[i];
  576. }
  577. tot_average /= fc->values;
  578. tot_average /= venc->quality;
  579. for (i = 0; i < fc->values; i++) {
  580. int position = fc->list[fc->list[i].sort].x;
  581. float average = averages[i];
  582. int j;
  583. average *= pow(tot_average / average, 0.5) * pow(1.25, position/200.); // MAGIC!
  584. for (j = 0; j < range - 1; j++)
  585. if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
  586. break;
  587. posts[fc->list[i].sort] = j;
  588. }
  589. }
  590. static int render_point(int x0, int y0, int x1, int y1, int x)
  591. {
  592. return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
  593. }
  594. static void floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
  595. PutBitContext *pb, uint_fast16_t *posts,
  596. float *floor, int samples)
  597. {
  598. int range = 255 / fc->multiplier + 1;
  599. int coded[fc->values]; // first 2 values are unused
  600. int i, counter;
  601. put_bits(pb, 1, 1); // non zero
  602. put_bits(pb, ilog(range - 1), posts[0]);
  603. put_bits(pb, ilog(range - 1), posts[1]);
  604. coded[0] = coded[1] = 1;
  605. for (i = 2; i < fc->values; i++) {
  606. int predicted = render_point(fc->list[fc->list[i].low].x,
  607. posts[fc->list[i].low],
  608. fc->list[fc->list[i].high].x,
  609. posts[fc->list[i].high],
  610. fc->list[i].x);
  611. int highroom = range - predicted;
  612. int lowroom = predicted;
  613. int room = FFMIN(highroom, lowroom);
  614. if (predicted == posts[i]) {
  615. coded[i] = 0; // must be used later as flag!
  616. continue;
  617. } else {
  618. if (!coded[fc->list[i].low ])
  619. coded[fc->list[i].low ] = -1;
  620. if (!coded[fc->list[i].high])
  621. coded[fc->list[i].high] = -1;
  622. }
  623. if (posts[i] > predicted) {
  624. if (posts[i] - predicted > room)
  625. coded[i] = posts[i] - predicted + lowroom;
  626. else
  627. coded[i] = (posts[i] - predicted) << 1;
  628. } else {
  629. if (predicted - posts[i] > room)
  630. coded[i] = predicted - posts[i] + highroom - 1;
  631. else
  632. coded[i] = ((predicted - posts[i]) << 1) - 1;
  633. }
  634. }
  635. counter = 2;
  636. for (i = 0; i < fc->partitions; i++) {
  637. vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
  638. int k, cval = 0, csub = 1<<c->subclass;
  639. if (c->subclass) {
  640. vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
  641. int cshift = 0;
  642. for (k = 0; k < c->dim; k++) {
  643. int l;
  644. for (l = 0; l < csub; l++) {
  645. int maxval = 1;
  646. if (c->books[l] != -1)
  647. maxval = venc->codebooks[c->books[l]].nentries;
  648. // coded could be -1, but this still works, cause that is 0
  649. if (coded[counter + k] < maxval)
  650. break;
  651. }
  652. assert(l != csub);
  653. cval |= l << cshift;
  654. cshift += c->subclass;
  655. }
  656. put_codeword(pb, book, cval);
  657. }
  658. for (k = 0; k < c->dim; k++) {
  659. int book = c->books[cval & (csub-1)];
  660. int entry = coded[counter++];
  661. cval >>= c->subclass;
  662. if (book == -1)
  663. continue;
  664. if (entry == -1)
  665. entry = 0;
  666. put_codeword(pb, &venc->codebooks[book], entry);
  667. }
  668. }
  669. ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
  670. fc->multiplier, floor, samples);
  671. }
  672. static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
  673. float *num)
  674. {
  675. int i, entry = -1;
  676. float distance = FLT_MAX;
  677. assert(book->dimentions);
  678. for (i = 0; i < book->nentries; i++) {
  679. float * vec = book->dimentions + i * book->ndimentions, d = book->pow2[i];
  680. int j;
  681. if (!book->lens[i])
  682. continue;
  683. for (j = 0; j < book->ndimentions; j++)
  684. d -= vec[j] * num[j];
  685. if (distance > d) {
  686. entry = i;
  687. distance = d;
  688. }
  689. }
  690. put_codeword(pb, book, entry);
  691. return &book->dimentions[entry * book->ndimentions];
  692. }
  693. static void residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
  694. PutBitContext *pb, float *coeffs, int samples,
  695. int real_ch)
  696. {
  697. int pass, i, j, p, k;
  698. int psize = rc->partition_size;
  699. int partitions = (rc->end - rc->begin) / psize;
  700. int channels = (rc->type == 2) ? 1 : real_ch;
  701. int classes[channels][partitions];
  702. int classwords = venc->codebooks[rc->classbook].ndimentions;
  703. assert(rc->type == 2);
  704. assert(real_ch == 2);
  705. for (p = 0; p < partitions; p++) {
  706. float max1 = 0., max2 = 0.;
  707. int s = rc->begin + p * psize;
  708. for (k = s; k < s + psize; k += 2) {
  709. max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
  710. max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
  711. }
  712. for (i = 0; i < rc->classifications - 1; i++)
  713. if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
  714. break;
  715. classes[0][p] = i;
  716. }
  717. for (pass = 0; pass < 8; pass++) {
  718. p = 0;
  719. while (p < partitions) {
  720. if (pass == 0)
  721. for (j = 0; j < channels; j++) {
  722. vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
  723. int entry = 0;
  724. for (i = 0; i < classwords; i++) {
  725. entry *= rc->classifications;
  726. entry += classes[j][p + i];
  727. }
  728. put_codeword(pb, book, entry);
  729. }
  730. for (i = 0; i < classwords && p < partitions; i++, p++) {
  731. for (j = 0; j < channels; j++) {
  732. int nbook = rc->books[classes[j][p]][pass];
  733. vorbis_enc_codebook * book = &venc->codebooks[nbook];
  734. float *buf = coeffs + samples*j + rc->begin + p*psize;
  735. if (nbook == -1)
  736. continue;
  737. assert(rc->type == 0 || rc->type == 2);
  738. assert(!(psize % book->ndimentions));
  739. if (rc->type == 0) {
  740. for (k = 0; k < psize; k += book->ndimentions) {
  741. float *a = put_vector(book, pb, &buf[k]);
  742. int l;
  743. for (l = 0; l < book->ndimentions; l++)
  744. buf[k + l] -= a[l];
  745. }
  746. } else {
  747. int s = rc->begin + p * psize, a1, b1;
  748. a1 = (s % real_ch) * samples;
  749. b1 = s / real_ch;
  750. s = real_ch * samples;
  751. for (k = 0; k < psize; k += book->ndimentions) {
  752. int dim, a2 = a1, b2 = b1;
  753. float vec[book->ndimentions], *pv = vec;
  754. for (dim = book->ndimentions; dim--; ) {
  755. *pv++ = coeffs[a2 + b2];
  756. if ((a2 += samples) == s) {
  757. a2 = 0;
  758. b2++;
  759. }
  760. }
  761. pv = put_vector(book, pb, vec);
  762. for (dim = book->ndimentions; dim--; ) {
  763. coeffs[a1 + b1] -= *pv++;
  764. if ((a1 += samples) == s) {
  765. a1 = 0;
  766. b1++;
  767. }
  768. }
  769. }
  770. }
  771. }
  772. }
  773. }
  774. }
  775. }
  776. static int apply_window_and_mdct(vorbis_enc_context *venc, signed short *audio,
  777. int samples)
  778. {
  779. int i, j, channel;
  780. const float * win = venc->win[0];
  781. int window_len = 1 << (venc->log2_blocksize[0] - 1);
  782. float n = (float)(1 << venc->log2_blocksize[0]) / 4.;
  783. // FIXME use dsp
  784. if (!venc->have_saved && !samples)
  785. return 0;
  786. if (venc->have_saved) {
  787. for (channel = 0; channel < venc->channels; channel++)
  788. memcpy(venc->samples + channel * window_len * 2,
  789. venc->saved + channel * window_len, sizeof(float) * window_len);
  790. } else {
  791. for (channel = 0; channel < venc->channels; channel++)
  792. memset(venc->samples + channel * window_len * 2, 0,
  793. sizeof(float) * window_len);
  794. }
  795. if (samples) {
  796. for (channel = 0; channel < venc->channels; channel++) {
  797. float * offset = venc->samples + channel*window_len*2 + window_len;
  798. j = channel;
  799. for (i = 0; i < samples; i++, j += venc->channels)
  800. offset[i] = -audio[j] / 32768. / n * win[window_len - i - 1]; //FIXME find out why the sign has to be fliped
  801. }
  802. } else {
  803. for (channel = 0; channel < venc->channels; channel++)
  804. memset(venc->samples + channel * window_len * 2 + window_len,
  805. 0, sizeof(float) * window_len);
  806. }
  807. for (channel = 0; channel < venc->channels; channel++)
  808. ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel * window_len,
  809. venc->samples + channel * window_len * 2);
  810. if (samples) {
  811. for (channel = 0; channel < venc->channels; channel++) {
  812. float *offset = venc->saved + channel * window_len;
  813. j = channel;
  814. for (i = 0; i < samples; i++, j += venc->channels)
  815. offset[i] = -audio[j] / 32768. / n * win[i]; //FIXME find out why the sign has to be fliped
  816. }
  817. venc->have_saved = 1;
  818. } else {
  819. venc->have_saved = 0;
  820. }
  821. return 1;
  822. }
  823. static av_cold int vorbis_encode_init(AVCodecContext *avccontext)
  824. {
  825. vorbis_enc_context *venc = avccontext->priv_data;
  826. if (avccontext->channels != 2) {
  827. av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
  828. return -1;
  829. }
  830. create_vorbis_context(venc, avccontext);
  831. if (avccontext->flags & CODEC_FLAG_QSCALE)
  832. venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 10.;
  833. else
  834. venc->quality = 1.;
  835. venc->quality *= venc->quality;
  836. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  837. avccontext->frame_size = 1 << (venc->log2_blocksize[0] - 1);
  838. avccontext->coded_frame = avcodec_alloc_frame();
  839. avccontext->coded_frame->key_frame = 1;
  840. return 0;
  841. }
  842. static int vorbis_encode_frame(AVCodecContext *avccontext,
  843. unsigned char *packets,
  844. int buf_size, void *data)
  845. {
  846. vorbis_enc_context *venc = avccontext->priv_data;
  847. signed short *audio = data;
  848. int samples = data ? avccontext->frame_size : 0;
  849. vorbis_enc_mode *mode;
  850. vorbis_enc_mapping *mapping;
  851. PutBitContext pb;
  852. int i;
  853. if (!apply_window_and_mdct(venc, audio, samples))
  854. return 0;
  855. samples = 1 << (venc->log2_blocksize[0] - 1);
  856. init_put_bits(&pb, packets, buf_size);
  857. put_bits(&pb, 1, 0); // magic bit
  858. put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
  859. mode = &venc->modes[0];
  860. mapping = &venc->mappings[mode->mapping];
  861. if (mode->blockflag) {
  862. put_bits(&pb, 1, 0);
  863. put_bits(&pb, 1, 0);
  864. }
  865. for (i = 0; i < venc->channels; i++) {
  866. vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
  867. uint_fast16_t posts[fc->values];
  868. floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
  869. floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
  870. }
  871. for (i = 0; i < venc->channels * samples; i++)
  872. venc->coeffs[i] /= venc->floor[i];
  873. for (i = 0; i < mapping->coupling_steps; i++) {
  874. float *mag = venc->coeffs + mapping->magnitude[i] * samples;
  875. float *ang = venc->coeffs + mapping->angle[i] * samples;
  876. int j;
  877. for (j = 0; j < samples; j++) {
  878. float a = ang[j];
  879. ang[j] -= mag[j];
  880. if (mag[j] > 0)
  881. ang[j] = -ang[j];
  882. if (ang[j] < 0)
  883. mag[j] = a;
  884. }
  885. }
  886. residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
  887. &pb, venc->coeffs, samples, venc->channels);
  888. avccontext->coded_frame->pts = venc->sample_count;
  889. venc->sample_count += avccontext->frame_size;
  890. flush_put_bits(&pb);
  891. return put_bits_count(&pb) >> 3;
  892. }
  893. static av_cold int vorbis_encode_close(AVCodecContext *avccontext)
  894. {
  895. vorbis_enc_context *venc = avccontext->priv_data;
  896. int i;
  897. if (venc->codebooks)
  898. for (i = 0; i < venc->ncodebooks; i++) {
  899. av_freep(&venc->codebooks[i].lens);
  900. av_freep(&venc->codebooks[i].codewords);
  901. av_freep(&venc->codebooks[i].quantlist);
  902. av_freep(&venc->codebooks[i].dimentions);
  903. av_freep(&venc->codebooks[i].pow2);
  904. }
  905. av_freep(&venc->codebooks);
  906. if (venc->floors)
  907. for (i = 0; i < venc->nfloors; i++) {
  908. int j;
  909. if (venc->floors[i].classes)
  910. for (j = 0; j < venc->floors[i].nclasses; j++)
  911. av_freep(&venc->floors[i].classes[j].books);
  912. av_freep(&venc->floors[i].classes);
  913. av_freep(&venc->floors[i].partition_to_class);
  914. av_freep(&venc->floors[i].list);
  915. }
  916. av_freep(&venc->floors);
  917. if (venc->residues)
  918. for (i = 0; i < venc->nresidues; i++) {
  919. av_freep(&venc->residues[i].books);
  920. av_freep(&venc->residues[i].maxes);
  921. }
  922. av_freep(&venc->residues);
  923. if (venc->mappings)
  924. for (i = 0; i < venc->nmappings; i++) {
  925. av_freep(&venc->mappings[i].mux);
  926. av_freep(&venc->mappings[i].floor);
  927. av_freep(&venc->mappings[i].residue);
  928. av_freep(&venc->mappings[i].magnitude);
  929. av_freep(&venc->mappings[i].angle);
  930. }
  931. av_freep(&venc->mappings);
  932. av_freep(&venc->modes);
  933. av_freep(&venc->saved);
  934. av_freep(&venc->samples);
  935. av_freep(&venc->floor);
  936. av_freep(&venc->coeffs);
  937. ff_mdct_end(&venc->mdct[0]);
  938. ff_mdct_end(&venc->mdct[1]);
  939. av_freep(&avccontext->coded_frame);
  940. av_freep(&avccontext->extradata);
  941. return 0 ;
  942. }
  943. AVCodec vorbis_encoder = {
  944. "vorbis",
  945. CODEC_TYPE_AUDIO,
  946. CODEC_ID_VORBIS,
  947. sizeof(vorbis_enc_context),
  948. vorbis_encode_init,
  949. vorbis_encode_frame,
  950. vorbis_encode_close,
  951. .capabilities= CODEC_CAP_DELAY,
  952. .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  953. .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
  954. };