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.

1175 lines
36KB

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