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.

1086 lines
34KB

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