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.

1117 lines
35KB

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