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.

1002 lines
30KB

  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 "avcodec.h"
  24. #define BITSTREAM_H // don't include this
  25. typedef int VLC;
  26. typedef int GetBitContext;
  27. #include "vorbis.h"
  28. #undef NDEBUG
  29. #include <assert.h>
  30. //#define ALT_BITSTREAM_WRITER
  31. //#include "bitstream.h"
  32. typedef struct {
  33. int len;
  34. uint32_t codeword;
  35. } cb_entry_t;
  36. typedef struct {
  37. int nentries;
  38. cb_entry_t * entries;
  39. int ndimentions;
  40. float min;
  41. float delta;
  42. int seq_p;
  43. int lookup;
  44. int * quantlist;
  45. float * dimentions;
  46. } codebook_t;
  47. typedef struct {
  48. int dim;
  49. int subclass;
  50. int masterbook;
  51. int * books;
  52. } floor_class_t;
  53. typedef struct {
  54. int x;
  55. int low;
  56. int high;
  57. int sort;
  58. } floor_entry_t;
  59. typedef struct {
  60. int partitions;
  61. int * partition_to_class;
  62. int nclasses;
  63. floor_class_t * classes;
  64. int multiplier;
  65. int rangebits;
  66. int values;
  67. floor_entry_t * list;
  68. } floor_t;
  69. typedef struct {
  70. int type;
  71. int begin;
  72. int end;
  73. int partition_size;
  74. int classifications;
  75. int classbook;
  76. int (*books)[8];
  77. } residue_t;
  78. typedef struct {
  79. int submaps;
  80. int * mux;
  81. int * floor;
  82. int * residue;
  83. } mapping_t;
  84. typedef struct {
  85. int blockflag;
  86. int mapping;
  87. } vorbis_mode_t;
  88. typedef struct {
  89. int channels;
  90. int sample_rate;
  91. int blocksize[2]; // in (1<<n) format
  92. MDCTContext mdct[2];
  93. const float * win[2];
  94. int have_saved;
  95. float * saved;
  96. float * samples;
  97. float * floor; // also used for tmp values for mdct
  98. float * coeffs; // also used for residue after floor
  99. int ncodebooks;
  100. codebook_t * codebooks;
  101. int nfloors;
  102. floor_t * floors;
  103. int nresidues;
  104. residue_t * residues;
  105. int nmappings;
  106. mapping_t * mappings;
  107. int nmodes;
  108. vorbis_mode_t * modes;
  109. } venc_context_t;
  110. typedef struct {
  111. int total;
  112. int total_pos;
  113. int pos;
  114. uint8_t * buf_ptr;
  115. } PutBitContext;
  116. #define ilog(i) av_log2(2*(i))
  117. static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
  118. pb->total = buffer_len * 8;
  119. pb->total_pos = 0;
  120. pb->pos = 0;
  121. pb->buf_ptr = buf;
  122. }
  123. static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
  124. if ((pb->total_pos += bits) >= pb->total) return;
  125. if (!bits) return;
  126. if (pb->pos) {
  127. if (pb->pos > bits) {
  128. *pb->buf_ptr |= val << (8 - pb->pos);
  129. pb->pos -= bits;
  130. bits = 0;
  131. } else {
  132. *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
  133. val >>= pb->pos;
  134. bits -= pb->pos;
  135. pb->pos = 0;
  136. }
  137. }
  138. for (; bits >= 8; bits -= 8) {
  139. *pb->buf_ptr++ = val & 0xFF;
  140. val >>= 8;
  141. }
  142. if (bits) {
  143. *pb->buf_ptr = val;
  144. pb->pos = 8 - bits;
  145. }
  146. }
  147. static inline void flush_put_bits(PutBitContext * pb) {
  148. }
  149. static inline int put_bits_count(PutBitContext * pb) {
  150. return pb->total_pos;
  151. }
  152. static int cb_lookup_vals(int lookup, int dimentions, int entries) {
  153. if (lookup == 1) {
  154. int tmp, i;
  155. for (tmp = 0; ; tmp++) {
  156. int n = 1;
  157. for (i = 0; i < dimentions; i++) n *= tmp;
  158. if (n > entries) break;
  159. }
  160. return tmp - 1;
  161. } else if (lookup == 2) return dimentions * entries;
  162. return 0;
  163. }
  164. static void ready_codebook(codebook_t * cb) {
  165. int h[33] = { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  166. int i;
  167. for (i = 0; i < cb->nentries; i++) {
  168. cb_entry_t * e = &cb->entries[i];
  169. int j = 0;
  170. if (h[0]) h[0] = 0;
  171. else {
  172. for (j = e->len; j; j--)
  173. if (h[j]) break;
  174. assert(j);
  175. }
  176. e->codeword = h[j];
  177. h[j] = 0;
  178. for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
  179. }
  180. for (i = 0; i < 33; i++) assert(!h[i]);
  181. if (!cb->lookup) cb->dimentions = NULL;
  182. else {
  183. int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  184. cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
  185. for (i = 0; i < cb->nentries; i++) {
  186. float last = 0;
  187. int j;
  188. int div = 1;
  189. for (j = 0; j < cb->ndimentions; j++) {
  190. int off;
  191. if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
  192. else off = i * cb->ndimentions + j; // lookup type 2
  193. cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
  194. if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
  195. div *= vals;
  196. }
  197. }
  198. }
  199. }
  200. static void ready_floor(floor_t * fc) {
  201. int i;
  202. fc->list[0].sort = 0;
  203. fc->list[1].sort = 1;
  204. for (i = 2; i < fc->values; i++) {
  205. int j;
  206. fc->list[i].low = 0;
  207. fc->list[i].high = 1;
  208. fc->list[i].sort = i;
  209. for (j = 2; j < i; j++) {
  210. int tmp = fc->list[j].x;
  211. if (tmp < fc->list[i].x) {
  212. if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
  213. } else {
  214. if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
  215. }
  216. }
  217. }
  218. for (i = 0; i < fc->values - 1; i++) {
  219. int j;
  220. for (j = i + 1; j < fc->values; j++) {
  221. if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
  222. int tmp = fc->list[i].sort;
  223. fc->list[i].sort = fc->list[j].sort;
  224. fc->list[j].sort = tmp;
  225. }
  226. }
  227. }
  228. }
  229. static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
  230. codebook_t * cb;
  231. floor_t * fc;
  232. residue_t * rc;
  233. mapping_t * mc;
  234. int i, book;
  235. venc->channels = avccontext->channels;
  236. venc->sample_rate = avccontext->sample_rate;
  237. venc->blocksize[0] = venc->blocksize[1] = 9;
  238. venc->ncodebooks = 10;
  239. venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
  240. // codebook 0 - floor1 book, values 0..255
  241. cb = &venc->codebooks[0];
  242. cb->nentries = 512;
  243. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  244. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 9;
  245. cb->ndimentions = 0;
  246. cb->min = 0.;
  247. cb->delta = 0.;
  248. cb->seq_p = 0;
  249. cb->lookup = 0;
  250. cb->quantlist = NULL;
  251. ready_codebook(cb);
  252. // codebook 1 - residue classbook, values 0..1, dimentions 4
  253. cb = &venc->codebooks[1];
  254. cb->nentries = 2;
  255. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  256. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
  257. cb->ndimentions = 4;
  258. cb->min = 0.;
  259. cb->delta = 0.;
  260. cb->seq_p = 0;
  261. cb->lookup = 0;
  262. cb->quantlist = NULL;
  263. ready_codebook(cb);
  264. // codebook 2..9 - vector, for the residue, values -32767..32767, dimentions 1
  265. for (book = 0; book < 8; book++) {
  266. cb = &venc->codebooks[2 + book];
  267. cb->nentries = 5;
  268. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  269. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
  270. cb->ndimentions = 1;
  271. cb->delta = 1 << ((7 - book) * 2);
  272. cb->min = -cb->delta*2;
  273. cb->seq_p = 0;
  274. cb->lookup = 1;
  275. cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
  276. for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
  277. ready_codebook(cb);
  278. }
  279. venc->nfloors = 1;
  280. venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
  281. // just 1 floor
  282. fc = &venc->floors[0];
  283. fc->partitions = 3;
  284. fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
  285. for (i = 0; i < fc->partitions; i++) fc->partition_to_class[i] = 0;
  286. fc->nclasses = 1;
  287. fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
  288. for (i = 0; i < fc->nclasses; i++) {
  289. floor_class_t * c = &fc->classes[i];
  290. int j, books;
  291. c->dim = 2;
  292. c->subclass = 0;
  293. c->masterbook = 0;
  294. books = (1 << c->subclass);
  295. c->books = av_malloc(sizeof(int) * books);
  296. for (j = 0; j < books; j++) c->books[j] = 0;
  297. }
  298. fc->multiplier = 1;
  299. fc->rangebits = venc->blocksize[0] - 1;
  300. fc->values = 2;
  301. for (i = 0; i < fc->partitions; i++)
  302. fc->values += fc->classes[fc->partition_to_class[i]].dim;
  303. fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
  304. fc->list[0].x = 0;
  305. fc->list[1].x = 1 << fc->rangebits;
  306. for (i = 2; i < fc->values; i++) {
  307. /*int a = i - 1;
  308. int g = ilog(a);
  309. assert(g <= fc->rangebits);
  310. a ^= 1 << (g-1);
  311. g = 1 << (fc->rangebits - g);
  312. fc->list[i].x = g + a*2*g;*/
  313. int a[] = {14, 4, 58, 2, 8, 28, 90};
  314. fc->list[i].x = a[i - 2];
  315. }
  316. ready_floor(fc);
  317. venc->nresidues = 1;
  318. venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
  319. // single residue
  320. rc = &venc->residues[0];
  321. rc->type = 0;
  322. rc->begin = 0;
  323. rc->end = 1 << (venc->blocksize[0] - 1);
  324. rc->partition_size = 64;
  325. rc->classifications = 2;
  326. rc->classbook = 1;
  327. rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
  328. for (i = 0; i < rc->classifications; i++) {
  329. int j;
  330. for (j = 0; j < 8; j++) rc->books[i][j] = 2 + j;
  331. rc->books[i][0] = rc->books[i][1] = rc->books[i][2] = rc->books[i][3] = -1;
  332. }
  333. venc->nmappings = 1;
  334. venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
  335. // single mapping
  336. mc = &venc->mappings[0];
  337. mc->submaps = 1;
  338. mc->mux = av_malloc(sizeof(int) * venc->channels);
  339. for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
  340. mc->floor = av_malloc(sizeof(int) * mc->submaps);
  341. mc->residue = av_malloc(sizeof(int) * mc->submaps);
  342. for (i = 0; i < mc->submaps; i++) {
  343. mc->floor[i] = 0;
  344. mc->residue[i] = 0;
  345. }
  346. venc->nmodes = 1;
  347. venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
  348. // single mode
  349. venc->modes[0].blockflag = 0;
  350. venc->modes[0].mapping = 0;
  351. venc->have_saved = 0;
  352. venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  353. venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
  354. venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  355. venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  356. {
  357. const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
  358. venc->win[0] = vwin[venc->blocksize[0] - 6];
  359. venc->win[1] = vwin[venc->blocksize[1] - 6];
  360. }
  361. ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
  362. ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
  363. }
  364. static void put_float(PutBitContext * pb, float f) {
  365. int exp, mant;
  366. uint32_t res = 0;
  367. mant = (int)ldexp(frexp(f, &exp), 20);
  368. exp += 788 - 20;
  369. if (mant < 0) { res |= (1 << 31); mant = -mant; }
  370. res |= mant | (exp << 21);
  371. put_bits(pb, 32, res);
  372. }
  373. static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
  374. int i;
  375. int ordered = 0;
  376. put_bits(pb, 24, 0x564342); //magic
  377. put_bits(pb, 16, cb->ndimentions);
  378. put_bits(pb, 24, cb->nentries);
  379. for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
  380. if (i == cb->nentries) ordered = 1;
  381. put_bits(pb, 1, ordered);
  382. if (ordered) {
  383. int len = cb->entries[0].len;
  384. put_bits(pb, 5, len - 1);
  385. i = 0;
  386. while (i < cb->nentries) {
  387. int j;
  388. for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
  389. put_bits(pb, ilog(cb->nentries - i), j);
  390. i += j;
  391. len++;
  392. }
  393. } else {
  394. int sparse = 0;
  395. for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
  396. if (i != cb->nentries) sparse = 1;
  397. put_bits(pb, 1, sparse);
  398. for (i = 0; i < cb->nentries; i++) {
  399. if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
  400. if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
  401. }
  402. }
  403. put_bits(pb, 4, cb->lookup);
  404. if (cb->lookup) {
  405. int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  406. int bits = ilog(cb->quantlist[0]);
  407. for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
  408. put_float(pb, cb->min);
  409. put_float(pb, cb->delta);
  410. put_bits(pb, 4, bits - 1);
  411. put_bits(pb, 1, cb->seq_p);
  412. for (i = 0; i < tmp; i++) 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++) put_bits(pb, 4, fc->partition_to_class[i]);
  420. for (i = 0; i < fc->nclasses; i++) {
  421. int j, books;
  422. put_bits(pb, 3, fc->classes[i].dim - 1);
  423. put_bits(pb, 2, fc->classes[i].subclass);
  424. if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
  425. books = (1 << fc->classes[i].subclass);
  426. for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
  427. }
  428. put_bits(pb, 2, fc->multiplier - 1);
  429. put_bits(pb, 4, fc->rangebits);
  430. for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
  431. }
  432. static void put_residue_header(PutBitContext * pb, residue_t * rc) {
  433. int i;
  434. put_bits(pb, 16, rc->type);
  435. put_bits(pb, 24, rc->begin);
  436. put_bits(pb, 24, rc->end);
  437. put_bits(pb, 24, rc->partition_size - 1);
  438. put_bits(pb, 6, rc->classifications - 1);
  439. put_bits(pb, 8, rc->classbook);
  440. for (i = 0; i < rc->classifications; i++) {
  441. int j, tmp = 0;
  442. for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
  443. put_bits(pb, 3, tmp & 7);
  444. put_bits(pb, 1, tmp > 7);
  445. if (tmp > 7) put_bits(pb, 5, tmp >> 3);
  446. }
  447. for (i = 0; i < rc->classifications; i++) {
  448. int j;
  449. for (j = 0; j < 8; j++)
  450. if (rc->books[i][j] != -1)
  451. put_bits(pb, 8, rc->books[i][j]);
  452. }
  453. }
  454. static int put_main_header(venc_context_t * venc, uint8_t ** out) {
  455. int i;
  456. PutBitContext pb;
  457. uint8_t buffer[50000] = {0}, * p = buffer;
  458. int buffer_len = sizeof buffer;
  459. int len, hlens[3];
  460. // identification header
  461. init_put_bits(&pb, p, buffer_len);
  462. put_bits(&pb, 8, 1); //magic
  463. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  464. put_bits(&pb, 32, 0); // version
  465. put_bits(&pb, 8, venc->channels);
  466. put_bits(&pb, 32, venc->sample_rate);
  467. put_bits(&pb, 32, 0); // bitrate
  468. put_bits(&pb, 32, 0); // bitrate
  469. put_bits(&pb, 32, 0); // bitrate
  470. put_bits(&pb, 4, venc->blocksize[0]);
  471. put_bits(&pb, 4, venc->blocksize[1]);
  472. put_bits(&pb, 1, 1); // framing
  473. flush_put_bits(&pb);
  474. hlens[0] = (put_bits_count(&pb) + 7) / 8;
  475. buffer_len -= hlens[0];
  476. p += hlens[0];
  477. // comment header
  478. init_put_bits(&pb, p, buffer_len);
  479. put_bits(&pb, 8, 3); //magic
  480. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  481. put_bits(&pb, 32, 0); // vendor length TODO
  482. put_bits(&pb, 32, 0); // amount of comments
  483. put_bits(&pb, 1, 1); // framing
  484. flush_put_bits(&pb);
  485. hlens[1] = (put_bits_count(&pb) + 7) / 8;
  486. buffer_len -= hlens[1];
  487. p += hlens[1];
  488. // setup header
  489. init_put_bits(&pb, p, buffer_len);
  490. put_bits(&pb, 8, 5); //magic
  491. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  492. // codebooks
  493. put_bits(&pb, 8, venc->ncodebooks - 1);
  494. for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
  495. // time domain, reserved, zero
  496. put_bits(&pb, 6, 0);
  497. put_bits(&pb, 16, 0);
  498. // floors
  499. put_bits(&pb, 6, venc->nfloors - 1);
  500. for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
  501. // residues
  502. put_bits(&pb, 6, venc->nresidues - 1);
  503. for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
  504. // mappings
  505. put_bits(&pb, 6, venc->nmappings - 1);
  506. for (i = 0; i < venc->nmappings; i++) {
  507. mapping_t * mc = &venc->mappings[i];
  508. int j;
  509. put_bits(&pb, 16, 0); // mapping type
  510. put_bits(&pb, 1, mc->submaps > 1);
  511. if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
  512. put_bits(&pb, 1, 0); // channel coupling
  513. put_bits(&pb, 2, 0); // reserved
  514. if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
  515. for (j = 0; j < mc->submaps; j++) {
  516. put_bits(&pb, 8, 0); // reserved time configuration
  517. put_bits(&pb, 8, mc->floor[j]);
  518. put_bits(&pb, 8, mc->residue[j]);
  519. }
  520. }
  521. // modes
  522. put_bits(&pb, 6, venc->nmodes - 1);
  523. for (i = 0; i < venc->nmodes; i++) {
  524. put_bits(&pb, 1, venc->modes[i].blockflag);
  525. put_bits(&pb, 16, 0); // reserved window type
  526. put_bits(&pb, 16, 0); // reserved transform type
  527. put_bits(&pb, 8, venc->modes[i].mapping);
  528. }
  529. put_bits(&pb, 1, 1); // framing
  530. flush_put_bits(&pb);
  531. hlens[2] = (put_bits_count(&pb) + 7) / 8;
  532. len = hlens[0] + hlens[1] + hlens[2];
  533. p = *out = av_mallocz(64 + len + len/255);
  534. *p++ = 2;
  535. p += av_xiphlacing(p, hlens[0]);
  536. p += av_xiphlacing(p, hlens[1]);
  537. buffer_len = 0;
  538. for (i = 0; i < 3; i++) {
  539. memcpy(p, buffer + buffer_len, hlens[i]);
  540. p += hlens[i];
  541. buffer_len += hlens[i];
  542. }
  543. return p - *out;
  544. }
  545. static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
  546. int range = 255 / fc->multiplier + 1;
  547. int i;
  548. for (i = 0; i < fc->values; i++) {
  549. int position = fc->list[fc->list[i].sort].x;
  550. int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
  551. int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
  552. int j;
  553. float average = 0;
  554. begin = (position + begin) / 2;
  555. end = (position + end ) / 2;
  556. assert(end <= samples);
  557. for (j = begin; j < end; j++) average += fabs(coeffs[j]);
  558. average /= end - begin;
  559. average /= 32; // MAGIC!
  560. for (j = 0; j < range - 1; j++) if (floor1_inverse_db_table[j * fc->multiplier] > average) break;
  561. posts[fc->list[i].sort] = j;
  562. }
  563. }
  564. static int render_point(int x0, int y0, int x1, int y1, int x) {
  565. return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
  566. }
  567. static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
  568. int dy = y1 - y0;
  569. int adx = x1 - x0;
  570. int ady = FFMAX(dy, -dy);
  571. int base = dy / adx;
  572. int x = x0;
  573. int y = y0;
  574. int err = 0;
  575. int sy;
  576. if (dy < 0) sy = base - 1;
  577. else sy = base + 1;
  578. ady = ady - FFMAX(base, -base) * adx;
  579. if (x >= n) return;
  580. buf[x] = floor1_inverse_db_table[y];
  581. for (x = x0 + 1; x < x1; x++) {
  582. if (x >= n) return;
  583. err += ady;
  584. if (err >= adx) {
  585. err -= adx;
  586. y += sy;
  587. } else {
  588. y += base;
  589. }
  590. buf[x] = floor1_inverse_db_table[y];
  591. }
  592. }
  593. static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
  594. int range = 255 / fc->multiplier + 1;
  595. int coded[fc->values]; // first 2 values are unused
  596. int i, counter;
  597. int lx, ly;
  598. put_bits(pb, 1, 1); // non zero
  599. put_bits(pb, ilog(range - 1), posts[0]);
  600. put_bits(pb, ilog(range - 1), posts[1]);
  601. for (i = 2; i < fc->values; i++) {
  602. int predicted = render_point(fc->list[fc->list[i].low].x,
  603. posts[fc->list[i].low],
  604. fc->list[fc->list[i].high].x,
  605. posts[fc->list[i].high],
  606. fc->list[i].x);
  607. int highroom = range - predicted;
  608. int lowroom = predicted;
  609. int room = FFMIN(highroom, lowroom);
  610. if (predicted == posts[i]) {
  611. coded[i] = 0; // must be used later as flag!
  612. continue;
  613. } else {
  614. if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
  615. if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
  616. }
  617. if (posts[i] > predicted) {
  618. if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
  619. else coded[i] = (posts[i] - predicted) << 1;
  620. } else {
  621. if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
  622. else coded[i] = ((predicted - posts[i]) << 1) - 1;
  623. }
  624. }
  625. counter = 2;
  626. for (i = 0; i < fc->partitions; i++) {
  627. floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
  628. codebook_t * book = &venc->codebooks[c->books[0]];
  629. int k;
  630. assert(!c->subclass);
  631. for (k = 0; k < c->dim; k++) {
  632. int entry = coded[counter++];
  633. if (entry == -1) entry = 0;
  634. assert(entry < book->nentries);
  635. assert(entry >= 0);
  636. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  637. }
  638. }
  639. lx = 0;
  640. ly = posts[0] * fc->multiplier; // sorted 0 is still 0
  641. coded[0] = coded[1] = 1;
  642. for (i = 1; i < fc->values; i++) {
  643. int pos = fc->list[i].sort;
  644. if (coded[pos]) {
  645. render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
  646. lx = fc->list[pos].x;
  647. ly = posts[pos] * fc->multiplier;
  648. }
  649. if (lx >= samples) break;
  650. }
  651. if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
  652. }
  653. static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
  654. int i;
  655. int entry = -1;
  656. float distance = 0;
  657. assert(book->dimentions);
  658. for (i = 0; i < book->nentries; i++) {
  659. float d = 0.;
  660. int j;
  661. for (j = 0; j < book->ndimentions; j++) {
  662. float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
  663. d += a*a;
  664. }
  665. if (entry == -1 || distance > d) {
  666. entry = i;
  667. distance = d;
  668. }
  669. }
  670. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  671. return &book->dimentions[entry * book->ndimentions];
  672. }
  673. static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
  674. int pass, i, j, p, k;
  675. int psize = rc->partition_size;
  676. int partitions = (rc->end - rc->begin) / psize;
  677. int classes[channels][partitions];
  678. int classwords = venc->codebooks[rc->classbook].ndimentions;
  679. for (pass = 0; pass < 8; pass++) {
  680. p = 0;
  681. while (p < partitions) {
  682. if (pass == 0) for (j = 0; j < channels; j++) {
  683. codebook_t * book = &venc->codebooks[rc->classbook];
  684. int entry = 0;
  685. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  686. for (i = classwords; i--; ) {
  687. classes[j][p + i] = entry % rc->classifications;
  688. entry /= rc->classifications;
  689. }
  690. }
  691. for (i = 0; i < classwords && p < partitions; i++, p++) {
  692. for (j = 0; j < channels; j++) {
  693. int nbook = rc->books[classes[j][p]][pass];
  694. codebook_t * book = &venc->codebooks[nbook];
  695. float * buf = coeffs + samples*j + rc->begin + p*psize;
  696. if (nbook == -1) continue;
  697. assert(rc->type == 0);
  698. assert(!(psize % book->ndimentions));
  699. for (k = 0; k < psize; k += book->ndimentions) {
  700. float * a = put_vector(book, pb, &buf[k]);
  701. int l;
  702. for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
  703. }
  704. }
  705. }
  706. }
  707. }
  708. }
  709. static int window(venc_context_t * venc, signed short * audio, int samples) {
  710. int i, j, channel;
  711. const float * win = venc->win[0];
  712. int window_len = 1 << (venc->blocksize[0] - 1);
  713. float n = (float)(1 << venc->blocksize[0]) / 4.;
  714. // FIXME use dsp
  715. if (!venc->have_saved && !samples) return 0;
  716. if (venc->have_saved) {
  717. for (channel = 0; channel < venc->channels; channel++) {
  718. memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
  719. }
  720. } else {
  721. for (channel = 0; channel < venc->channels; channel++) {
  722. memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
  723. }
  724. }
  725. if (samples) {
  726. for (channel = 0; channel < venc->channels; channel++) {
  727. float * offset = venc->samples + channel*window_len*2 + window_len;
  728. j = channel;
  729. for (i = 0; i < samples; i++, j += venc->channels)
  730. offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
  731. }
  732. } else {
  733. for (channel = 0; channel < venc->channels; channel++) {
  734. memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
  735. }
  736. }
  737. for (channel = 0; channel < venc->channels; channel++) {
  738. ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
  739. }
  740. if (samples) {
  741. for (channel = 0; channel < venc->channels; channel++) {
  742. float * offset = venc->saved + channel*window_len;
  743. j = channel;
  744. for (i = 0; i < samples; i++, j += venc->channels)
  745. offset[i] = audio[j] / 32768. / n * win[i];
  746. }
  747. venc->have_saved = 1;
  748. } else {
  749. venc->have_saved = 0;
  750. }
  751. return 1;
  752. }
  753. static int vorbis_encode_init(AVCodecContext * avccontext)
  754. {
  755. venc_context_t * venc = avccontext->priv_data;
  756. create_vorbis_context(venc, avccontext);
  757. //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
  758. //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
  759. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  760. avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
  761. avccontext->coded_frame = avcodec_alloc_frame();
  762. avccontext->coded_frame->key_frame = 1;
  763. return 0;
  764. }
  765. static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
  766. {
  767. venc_context_t * venc = avccontext->priv_data;
  768. signed short * audio = data;
  769. int samples = data ? avccontext->frame_size : 0;
  770. vorbis_mode_t * mode;
  771. mapping_t * mapping;
  772. PutBitContext pb;
  773. int i;
  774. if (!window(venc, audio, samples)) return 0;
  775. samples = 1 << (venc->blocksize[0] - 1);
  776. init_put_bits(&pb, packets, buf_size);
  777. put_bits(&pb, 1, 0); // magic bit
  778. put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
  779. mode = &venc->modes[0];
  780. mapping = &venc->mappings[mode->mapping];
  781. if (mode->blockflag) {
  782. put_bits(&pb, 1, 0);
  783. put_bits(&pb, 1, 0);
  784. }
  785. for (i = 0; i < venc->channels; i++) {
  786. floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
  787. int posts[fc->values];
  788. floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
  789. floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
  790. }
  791. for (i = 0; i < venc->channels; i++) {
  792. int j;
  793. for (j = 0; j < samples; j++) {
  794. venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
  795. }
  796. }
  797. residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
  798. return (put_bits_count(&pb) + 7) / 8;
  799. }
  800. static int vorbis_encode_close(AVCodecContext * avccontext)
  801. {
  802. venc_context_t * venc = avccontext->priv_data;
  803. int i;
  804. if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
  805. av_freep(&venc->codebooks[i].entries);
  806. av_freep(&venc->codebooks[i].quantlist);
  807. av_freep(&venc->codebooks[i].dimentions);
  808. }
  809. av_freep(&venc->codebooks);
  810. if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
  811. int j;
  812. av_freep(&venc->floors[i].classes);
  813. if (venc->floors[i].classes)
  814. for (j = 0; j < venc->floors[i].nclasses; j++)
  815. av_freep(&venc->floors[i].classes[j].books);
  816. av_freep(&venc->floors[i].partition_to_class);
  817. av_freep(&venc->floors[i].list);
  818. }
  819. av_freep(&venc->floors);
  820. if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
  821. av_freep(&venc->residues[i].books);
  822. }
  823. av_freep(&venc->residues);
  824. if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
  825. av_freep(&venc->mappings[i].mux);
  826. av_freep(&venc->mappings[i].floor);
  827. av_freep(&venc->mappings[i].residue);
  828. }
  829. av_freep(&venc->mappings);
  830. av_freep(&venc->modes);
  831. av_freep(&venc->saved);
  832. av_freep(&venc->samples);
  833. av_freep(&venc->floor);
  834. av_freep(&venc->coeffs);
  835. ff_mdct_end(&venc->mdct[0]);
  836. ff_mdct_end(&venc->mdct[1]);
  837. av_freep(&avccontext->coded_frame);
  838. av_freep(&avccontext->extradata);
  839. return 0 ;
  840. }
  841. AVCodec vorbis_encoder = {
  842. "vorbis",
  843. CODEC_TYPE_AUDIO,
  844. CODEC_ID_VORBIS,
  845. sizeof(venc_context_t),
  846. vorbis_encode_init,
  847. vorbis_encode_frame,
  848. vorbis_encode_close,
  849. .capabilities= CODEC_CAP_DELAY,
  850. };