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.

840 lines
25KB

  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 partitions;
  55. int * partition_to_class;
  56. int nclasses;
  57. floor_class_t * classes;
  58. int multiplier;
  59. int rangebits;
  60. int values;
  61. struct { int x; } * list;
  62. } floor_t;
  63. typedef struct {
  64. int type;
  65. int begin;
  66. int end;
  67. int partition_size;
  68. int classifications;
  69. int classbook;
  70. int (*books)[8];
  71. } residue_t;
  72. typedef struct {
  73. int submaps;
  74. int * mux;
  75. int * floor;
  76. int * residue;
  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 blocksize[2]; // in (1<<n) format
  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. int ncodebooks;
  94. codebook_t * codebooks;
  95. int nfloors;
  96. floor_t * floors;
  97. int nresidues;
  98. residue_t * residues;
  99. int nmappings;
  100. mapping_t * mappings;
  101. int nmodes;
  102. vorbis_mode_t * modes;
  103. } venc_context_t;
  104. typedef struct {
  105. int total;
  106. int total_pos;
  107. int pos;
  108. uint8_t * buf_ptr;
  109. } PutBitContext;
  110. static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
  111. pb->total = buffer_len * 8;
  112. pb->total_pos = 0;
  113. pb->pos = 0;
  114. pb->buf_ptr = buf;
  115. }
  116. static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
  117. if ((pb->total_pos += bits) >= pb->total) return;
  118. if (!bits) return;
  119. if (pb->pos) {
  120. if (pb->pos > bits) {
  121. *pb->buf_ptr |= val << (8 - pb->pos);
  122. pb->pos -= bits;
  123. bits = 0;
  124. } else {
  125. *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
  126. val >>= pb->pos;
  127. bits -= pb->pos;
  128. pb->pos = 0;
  129. }
  130. }
  131. for (; bits >= 8; bits -= 8) {
  132. *pb->buf_ptr++ = val & 0xFF;
  133. val >>= 8;
  134. }
  135. if (bits) {
  136. *pb->buf_ptr = val;
  137. pb->pos = 8 - bits;
  138. }
  139. }
  140. static inline void flush_put_bits(PutBitContext * pb) {
  141. }
  142. static inline int put_bits_count(PutBitContext * pb) {
  143. return pb->total_pos;
  144. }
  145. static int cb_lookup_vals(int lookup, int dimentions, int entries) {
  146. if (lookup == 1) {
  147. int tmp, i;
  148. for (tmp = 0; ; tmp++) {
  149. int n = 1;
  150. for (i = 0; i < dimentions; i++) n *= tmp;
  151. if (n > entries) break;
  152. }
  153. return tmp - 1;
  154. } else if (lookup == 2) return dimentions * entries;
  155. return 0;
  156. }
  157. static void ready_codebook(codebook_t * cb) {
  158. 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 };
  159. int i;
  160. for (i = 0; i < cb->nentries; i++) {
  161. cb_entry_t * e = &cb->entries[i];
  162. int j = 0;
  163. if (h[0]) h[0] = 0;
  164. else for (j = e->len; !h[j]; j--) assert(j);
  165. e->codeword = h[j];
  166. h[j] = 0;
  167. for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
  168. }
  169. for (i = 0; i < 33; i++) assert(!h[i]);
  170. if (!cb->lookup) cb->dimentions = NULL;
  171. else {
  172. int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  173. cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
  174. for (i = 0; i < cb->nentries; i++) {
  175. float last = 0;
  176. int j;
  177. int div = 1;
  178. for (j = 0; j < cb->ndimentions; j++) {
  179. int off;
  180. if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
  181. else off = i * cb->ndimentions + j; // lookup type 2
  182. cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
  183. if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
  184. div *= vals;
  185. }
  186. }
  187. }
  188. }
  189. static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
  190. codebook_t * cb;
  191. floor_t * fc;
  192. residue_t * rc;
  193. mapping_t * mc;
  194. int i, book;
  195. venc->channels = avccontext->channels;
  196. venc->sample_rate = avccontext->sample_rate;
  197. venc->blocksize[0] = venc->blocksize[1] = 8;
  198. venc->ncodebooks = 10;
  199. venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
  200. // codebook 0 - floor1 book, values 0..255
  201. cb = &venc->codebooks[0];
  202. cb->nentries = 256;
  203. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  204. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
  205. cb->ndimentions = 0;
  206. cb->min = 0.;
  207. cb->delta = 0.;
  208. cb->seq_p = 0;
  209. cb->lookup = 0;
  210. cb->quantlist = NULL;
  211. ready_codebook(cb);
  212. // codebook 1 - residue classbook, values 0..1, dimentions 4
  213. cb = &venc->codebooks[1];
  214. cb->nentries = 2;
  215. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  216. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
  217. cb->ndimentions = 4;
  218. cb->min = 0.;
  219. cb->delta = 0.;
  220. cb->seq_p = 0;
  221. cb->lookup = 0;
  222. cb->quantlist = NULL;
  223. ready_codebook(cb);
  224. // codebook 2..9 - vector, for the residue, values -32767..32767, dimentions 1
  225. for (book = 0; book < 8; book++) {
  226. cb = &venc->codebooks[2 + book];
  227. cb->nentries = 5;
  228. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  229. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
  230. cb->ndimentions = 1;
  231. cb->delta = 1 << ((7 - book) * 2);
  232. cb->min = -cb->delta*2;
  233. cb->seq_p = 0;
  234. cb->lookup = 2;
  235. cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
  236. for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
  237. ready_codebook(cb);
  238. }
  239. venc->nfloors = 1;
  240. venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
  241. // just 1 floor
  242. fc = &venc->floors[0];
  243. fc->partitions = 1;
  244. fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
  245. for (i = 0; i < fc->partitions; i++) fc->partition_to_class[i] = 0;
  246. fc->nclasses = 1;
  247. fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
  248. for (i = 0; i < fc->nclasses; i++) {
  249. floor_class_t * c = &fc->classes[i];
  250. int j, books;
  251. c->dim = 1;
  252. c->subclass = 0;
  253. c->masterbook = 0;
  254. books = (1 << c->subclass);
  255. c->books = av_malloc(sizeof(int) * books);
  256. for (j = 0; j < books; j++) c->books[j] = 0;
  257. }
  258. fc->multiplier = 1;
  259. fc->rangebits = venc->blocksize[0];
  260. fc->values = 2;
  261. for (i = 0; i < fc->partitions; i++)
  262. fc->values += fc->classes[fc->partition_to_class[i]].dim;
  263. fc->list = av_malloc(sizeof(*fc->list) * fc->values);
  264. fc->list[0].x = 0;
  265. fc->list[1].x = 1 << fc->rangebits;
  266. for (i = 2; i < fc->values; i++) fc->list[i].x = i * 5;
  267. venc->nresidues = 1;
  268. venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
  269. // single residue
  270. rc = &venc->residues[0];
  271. rc->type = 0;
  272. rc->begin = 0;
  273. rc->end = 1 << (venc->blocksize[0] - 1);
  274. rc->partition_size = 64;
  275. rc->classifications = 1;
  276. rc->classbook = 1;
  277. rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
  278. for (i = 0; i < 8; i++) rc->books[0][i] = 2 + i;
  279. venc->nmappings = 1;
  280. venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
  281. // single mapping
  282. mc = &venc->mappings[0];
  283. mc->submaps = 1;
  284. mc->mux = av_malloc(sizeof(int) * venc->channels);
  285. for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
  286. mc->floor = av_malloc(sizeof(int) * mc->submaps);
  287. mc->residue = av_malloc(sizeof(int) * mc->submaps);
  288. for (i = 0; i < mc->submaps; i++) {
  289. mc->floor[i] = 0;
  290. mc->residue[i] = 0;
  291. }
  292. venc->nmodes = 1;
  293. venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
  294. // single mode
  295. venc->modes[0].blockflag = 0;
  296. venc->modes[0].mapping = 0;
  297. venc->have_saved = 0;
  298. venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  299. venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
  300. venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  301. venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  302. {
  303. const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
  304. venc->win[0] = vwin[venc->blocksize[0] - 6];
  305. venc->win[1] = vwin[venc->blocksize[1] - 6];
  306. }
  307. ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
  308. ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
  309. }
  310. static inline int ilog(unsigned int a) {
  311. int i;
  312. for (i = 0; a >> i; i++);
  313. return i;
  314. }
  315. static void put_float(PutBitContext * pb, float f) {
  316. int exp, mant;
  317. uint32_t res = 0;
  318. mant = (int)ldexp(frexp(f, &exp), 20);
  319. exp += 788 - 20;
  320. if (mant < 0) { res |= (1 << 31); mant = -mant; }
  321. res |= mant | (exp << 21);
  322. put_bits(pb, 32, res);
  323. }
  324. static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
  325. int i;
  326. int ordered = 0;
  327. put_bits(pb, 24, 0x564342); //magic
  328. put_bits(pb, 16, cb->ndimentions);
  329. put_bits(pb, 24, cb->nentries);
  330. for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
  331. if (i == cb->nentries) ordered = 1;
  332. put_bits(pb, 1, ordered);
  333. if (ordered) {
  334. int len = cb->entries[0].len;
  335. put_bits(pb, 5, len);
  336. i = 0;
  337. while (i < cb->nentries) {
  338. int j;
  339. for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
  340. put_bits(pb, ilog(cb->nentries - i), j);
  341. i += j;
  342. len++;
  343. }
  344. } else {
  345. int sparse = 0;
  346. for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
  347. if (i != cb->nentries) sparse = 1;
  348. put_bits(pb, 1, sparse);
  349. for (i = 0; i < cb->nentries; i++) {
  350. if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
  351. if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
  352. }
  353. }
  354. put_bits(pb, 4, cb->lookup);
  355. if (cb->lookup) {
  356. int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  357. int bits = ilog(cb->quantlist[0]);
  358. for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
  359. put_float(pb, cb->min);
  360. put_float(pb, cb->delta);
  361. put_bits(pb, 4, bits - 1);
  362. put_bits(pb, 1, cb->seq_p);
  363. for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
  364. }
  365. }
  366. static void put_floor_header(PutBitContext * pb, floor_t * fc) {
  367. int i;
  368. put_bits(pb, 16, 1); // type, only floor1 is supported
  369. put_bits(pb, 5, fc->partitions);
  370. for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
  371. for (i = 0; i < fc->nclasses; i++) {
  372. int j, books;
  373. put_bits(pb, 3, fc->classes[i].dim - 1);
  374. put_bits(pb, 2, fc->classes[i].subclass);
  375. if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
  376. books = (1 << fc->classes[i].subclass);
  377. for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
  378. }
  379. put_bits(pb, 2, fc->multiplier - 1);
  380. put_bits(pb, 4, fc->rangebits);
  381. for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
  382. }
  383. static void put_residue_header(PutBitContext * pb, residue_t * rc) {
  384. int i;
  385. put_bits(pb, 16, rc->type);
  386. put_bits(pb, 24, rc->begin);
  387. put_bits(pb, 24, rc->end);
  388. put_bits(pb, 24, rc->partition_size - 1);
  389. put_bits(pb, 6, rc->classifications - 1);
  390. put_bits(pb, 8, rc->classbook);
  391. for (i = 0; i < rc->classifications; i++) {
  392. int j, tmp = 0;
  393. for (j = 0; j < 8; j++) tmp |= (!!rc->books[i][j]) << j;
  394. put_bits(pb, 3, tmp & 7);
  395. put_bits(pb, 1, tmp > 7);
  396. if (tmp > 7) put_bits(pb, 5, tmp >> 3);
  397. }
  398. for (i = 0; i < rc->classifications; i++) {
  399. int j;
  400. for (j = 0; j < 8; j++)
  401. if (rc->books[i][j])
  402. put_bits(pb, 8, rc->books[i][j]);
  403. }
  404. }
  405. static int put_main_header(venc_context_t * venc, uint8_t ** out) {
  406. int i;
  407. PutBitContext pb;
  408. uint8_t buffer[50000] = {0}, * p = buffer;
  409. int buffer_len = sizeof buffer;
  410. int len, hlens[3];
  411. // identification header
  412. init_put_bits(&pb, p, buffer_len);
  413. put_bits(&pb, 8, 1); //magic
  414. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  415. put_bits(&pb, 32, 0); // version
  416. put_bits(&pb, 8, venc->channels);
  417. put_bits(&pb, 32, venc->sample_rate);
  418. put_bits(&pb, 32, 0); // bitrate
  419. put_bits(&pb, 32, 0); // bitrate
  420. put_bits(&pb, 32, 0); // bitrate
  421. put_bits(&pb, 4, venc->blocksize[0]);
  422. put_bits(&pb, 4, venc->blocksize[1]);
  423. put_bits(&pb, 1, 1); // framing
  424. flush_put_bits(&pb);
  425. hlens[0] = (put_bits_count(&pb) + 7) / 8;
  426. buffer_len -= hlens[0];
  427. p += hlens[0];
  428. // comment header
  429. init_put_bits(&pb, p, buffer_len);
  430. put_bits(&pb, 8, 3); //magic
  431. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  432. put_bits(&pb, 32, 0); // vendor length TODO
  433. put_bits(&pb, 32, 0); // amount of comments
  434. put_bits(&pb, 1, 1); // framing
  435. flush_put_bits(&pb);
  436. hlens[1] = (put_bits_count(&pb) + 7) / 8;
  437. buffer_len -= hlens[1];
  438. p += hlens[1];
  439. // setup header
  440. init_put_bits(&pb, p, buffer_len);
  441. put_bits(&pb, 8, 5); //magic
  442. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  443. // codebooks
  444. put_bits(&pb, 8, venc->ncodebooks - 1);
  445. for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
  446. // time domain, reserved, zero
  447. put_bits(&pb, 6, 0);
  448. put_bits(&pb, 16, 0);
  449. // floors
  450. put_bits(&pb, 6, venc->nfloors - 1);
  451. for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
  452. // residues
  453. put_bits(&pb, 6, venc->nresidues - 1);
  454. for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
  455. // mappings
  456. put_bits(&pb, 6, venc->nmappings - 1);
  457. for (i = 0; i < venc->nmappings; i++) {
  458. mapping_t * mc = &venc->mappings[i];
  459. int j;
  460. put_bits(&pb, 16, 0); // mapping type
  461. put_bits(&pb, 1, mc->submaps > 1);
  462. if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
  463. put_bits(&pb, 1, 0); // channel coupling
  464. put_bits(&pb, 2, 0); // reserved
  465. if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
  466. for (j = 0; j < mc->submaps; j++) {
  467. put_bits(&pb, 8, 0); // reserved time configuration
  468. put_bits(&pb, 8, mc->floor[j]);
  469. put_bits(&pb, 8, mc->residue[j]);
  470. }
  471. }
  472. // modes
  473. put_bits(&pb, 6, venc->nmodes - 1);
  474. for (i = 0; i < venc->nmodes; i++) {
  475. put_bits(&pb, 1, venc->modes[i].blockflag);
  476. put_bits(&pb, 16, 0); // reserved window type
  477. put_bits(&pb, 16, 0); // reserved transform type
  478. put_bits(&pb, 8, venc->modes[i].mapping);
  479. }
  480. put_bits(&pb, 1, 1); // framing
  481. flush_put_bits(&pb);
  482. hlens[2] = (put_bits_count(&pb) + 7) / 8;
  483. len = hlens[0] + hlens[1] + hlens[2];
  484. p = *out = av_mallocz(64 + len + len/255);
  485. *p++ = 2;
  486. p += av_xiphlacing(p, hlens[0]);
  487. p += av_xiphlacing(p, hlens[1]);
  488. buffer_len = 0;
  489. for (i = 0; i < 3; i++) {
  490. memcpy(p, buffer + buffer_len, hlens[i]);
  491. p += hlens[i];
  492. buffer_len += hlens[i];
  493. }
  494. return p - *out;
  495. }
  496. static int vorbis_encode_init(AVCodecContext * avccontext)
  497. {
  498. venc_context_t * venc = avccontext->priv_data;
  499. create_vorbis_context(venc, avccontext);
  500. //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
  501. //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
  502. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  503. avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
  504. avccontext->coded_frame = avcodec_alloc_frame();
  505. avccontext->coded_frame->key_frame = 1;
  506. return 0;
  507. }
  508. static int window(venc_context_t * venc, signed short * audio, int samples) {
  509. int i, j, channel;
  510. const float * win = venc->win[0];
  511. int window_len = 1 << (venc->blocksize[0] - 1);
  512. // FIXME use dsp
  513. if (!venc->have_saved && !samples) return 0;
  514. if (venc->have_saved) {
  515. for (channel = 0; channel < venc->channels; channel++) {
  516. memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
  517. }
  518. } else {
  519. for (channel = 0; channel < venc->channels; channel++) {
  520. memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
  521. }
  522. }
  523. if (samples) {
  524. for (channel = 0; channel < venc->channels; channel++) {
  525. float * offset = venc->samples + channel*window_len*2 + window_len;
  526. j = channel;
  527. for (i = 0; i < samples; i++, j += venc->channels)
  528. offset[i] = audio[j] / 32768. * win[window_len - i];
  529. }
  530. } else {
  531. for (channel = 0; channel < venc->channels; channel++) {
  532. memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
  533. }
  534. }
  535. for (channel = 0; channel < venc->channels; channel++) {
  536. ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
  537. }
  538. if (samples) {
  539. for (channel = 0; channel < venc->channels; channel++) {
  540. float * offset = venc->saved + channel*window_len;
  541. j = channel;
  542. for (i = 0; i < samples; i++, j += venc->channels)
  543. offset[i] = audio[j] / 32768. * win[i];
  544. }
  545. venc->have_saved = 1;
  546. } else {
  547. venc->have_saved = 0;
  548. }
  549. return 1;
  550. }
  551. static float put_vector(codebook_t * book, PutBitContext * pb, float num) {
  552. int i;
  553. int entry = -1;
  554. float distance = 0;
  555. assert(book->dimentions);
  556. assert(book->ndimentions == 1);
  557. for (i = 0; i < book->nentries; i++) {
  558. float d = (book->dimentions[i] - num)*(book->dimentions[i] - num);
  559. if (entry == -1 || distance > d) {
  560. entry = i;
  561. distance = d;
  562. }
  563. }
  564. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  565. return book->dimentions[entry];
  566. }
  567. static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
  568. int pass, i, j, p, k;
  569. int psize = rc->partition_size;
  570. int partitions = (rc->end - rc->begin) / psize;
  571. int classes[channels][partitions];
  572. int classwords = venc->codebooks[rc->classbook].ndimentions;
  573. for (pass = 0; pass < 8; pass++) {
  574. p = 0;
  575. while (p < partitions) {
  576. if (pass == 0) for (j = 0; j < channels; j++) {
  577. codebook_t * book = &venc->codebooks[rc->classbook];
  578. int entry = 0;
  579. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  580. for (i = classwords; i--; ) {
  581. classes[j][p + i] = entry % rc->classifications;
  582. entry /= rc->classifications;
  583. }
  584. }
  585. for (i = 0; i < classwords && p < partitions; i++, p++) {
  586. for (j = 0; j < channels; j++) {
  587. int nbook = rc->books[classes[j][p]][pass];
  588. codebook_t * book = &venc->codebooks[nbook];
  589. float * buf = coeffs + samples*j + rc->begin + p*psize;
  590. assert(rc->type == 0);
  591. assert(book->ndimentions == 1);
  592. for (k = 0; k < psize; k++) {
  593. buf[k] -= put_vector(book, pb, buf[k]);
  594. }
  595. }
  596. }
  597. }
  598. }
  599. }
  600. static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
  601. {
  602. venc_context_t * venc = avccontext->priv_data;
  603. signed short * audio = data;
  604. int samples = data ? avccontext->frame_size : 0;
  605. vorbis_mode_t * mode;
  606. mapping_t * mapping;
  607. PutBitContext pb;
  608. int i;
  609. if (!window(venc, audio, samples)) return 0;
  610. init_put_bits(&pb, packets, buf_size);
  611. put_bits(&pb, 1, 0); // magic bit
  612. put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
  613. mode = &venc->modes[0];
  614. mapping = &venc->mappings[mode->mapping];
  615. if (mode->blockflag) {
  616. put_bits(&pb, 1, 0);
  617. put_bits(&pb, 1, 0);
  618. }
  619. for (i = 0; i < venc->channels; i++) {
  620. floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
  621. int range = 255 / fc->multiplier + 1;
  622. int j;
  623. put_bits(&pb, 1, 1); // non zero
  624. put_bits(&pb, ilog(range - 1), 113); // magic value - 3.7180282E-05
  625. put_bits(&pb, ilog(range - 1), 113); // both sides of X
  626. for (j = 0; j < fc->partitions; j++) {
  627. floor_class_t * c = &fc->classes[fc->partition_to_class[j]];
  628. codebook_t * book = &venc->codebooks[c->books[0]];
  629. int entry = 0;
  630. int k;
  631. for (k = 0; k < c->dim; k++) {
  632. put_bits(&pb, book->entries[entry].len, book->entries[entry].codeword);
  633. }
  634. }
  635. for (j = 0; j < samples; j++) {
  636. venc->floor[i * samples + j] = floor1_inverse_db_table[220];
  637. }
  638. }
  639. for (i = 0; i < venc->channels; i++) {
  640. int j;
  641. for (j = 0; j < samples; j++) {
  642. venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
  643. }
  644. }
  645. residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
  646. return (put_bits_count(&pb) + 7) / 8;
  647. }
  648. static int vorbis_encode_close(AVCodecContext * avccontext)
  649. {
  650. venc_context_t * venc = avccontext->priv_data;
  651. int i;
  652. if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
  653. av_freep(&venc->codebooks[i].entries);
  654. av_freep(&venc->codebooks[i].quantlist);
  655. av_freep(&venc->codebooks[i].dimentions);
  656. }
  657. av_freep(&venc->codebooks);
  658. if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
  659. int j;
  660. av_freep(&venc->floors[i].classes);
  661. if (venc->floors[i].classes)
  662. for (j = 0; j < venc->floors[i].nclasses; j++)
  663. av_freep(&venc->floors[i].classes[j].books);
  664. av_freep(&venc->floors[i].partition_to_class);
  665. av_freep(&venc->floors[i].list);
  666. }
  667. av_freep(&venc->floors);
  668. if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
  669. av_freep(&venc->residues[i].books);
  670. }
  671. av_freep(&venc->residues);
  672. if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
  673. av_freep(&venc->mappings[i].mux);
  674. av_freep(&venc->mappings[i].floor);
  675. av_freep(&venc->mappings[i].residue);
  676. }
  677. av_freep(&venc->mappings);
  678. av_freep(&venc->modes);
  679. av_freep(&venc->saved);
  680. av_freep(&venc->samples);
  681. av_freep(&venc->floor);
  682. av_freep(&venc->coeffs);
  683. ff_mdct_end(&venc->mdct[0]);
  684. ff_mdct_end(&venc->mdct[1]);
  685. av_freep(&avccontext->coded_frame);
  686. av_freep(&avccontext->extradata);
  687. return 0 ;
  688. }
  689. AVCodec oggvorbis_encoder = {
  690. "vorbis",
  691. CODEC_TYPE_AUDIO,
  692. CODEC_ID_VORBIS,
  693. sizeof(venc_context_t),
  694. vorbis_encode_init,
  695. vorbis_encode_frame,
  696. vorbis_encode_close,
  697. .capabilities= CODEC_CAP_DELAY,
  698. };