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.

844 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 = 1;
  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 = 2;
  276. rc->classbook = 1;
  277. rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
  278. for (i = 0; i < rc->classifications; i++) {
  279. int j;
  280. for (j = 0; j < 8; j++) rc->books[i][j] = 2 + j;
  281. }
  282. venc->nmappings = 1;
  283. venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
  284. // single mapping
  285. mc = &venc->mappings[0];
  286. mc->submaps = 1;
  287. mc->mux = av_malloc(sizeof(int) * venc->channels);
  288. for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
  289. mc->floor = av_malloc(sizeof(int) * mc->submaps);
  290. mc->residue = av_malloc(sizeof(int) * mc->submaps);
  291. for (i = 0; i < mc->submaps; i++) {
  292. mc->floor[i] = 0;
  293. mc->residue[i] = 0;
  294. }
  295. venc->nmodes = 1;
  296. venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
  297. // single mode
  298. venc->modes[0].blockflag = 0;
  299. venc->modes[0].mapping = 0;
  300. venc->have_saved = 0;
  301. venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  302. venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
  303. venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  304. venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
  305. {
  306. const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
  307. venc->win[0] = vwin[venc->blocksize[0] - 6];
  308. venc->win[1] = vwin[venc->blocksize[1] - 6];
  309. }
  310. ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
  311. ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
  312. }
  313. static inline int ilog(unsigned int a) {
  314. int i;
  315. for (i = 0; a >> i; i++);
  316. return i;
  317. }
  318. static void put_float(PutBitContext * pb, float f) {
  319. int exp, mant;
  320. uint32_t res = 0;
  321. mant = (int)ldexp(frexp(f, &exp), 20);
  322. exp += 788 - 20;
  323. if (mant < 0) { res |= (1 << 31); mant = -mant; }
  324. res |= mant | (exp << 21);
  325. put_bits(pb, 32, res);
  326. }
  327. static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
  328. int i;
  329. int ordered = 0;
  330. put_bits(pb, 24, 0x564342); //magic
  331. put_bits(pb, 16, cb->ndimentions);
  332. put_bits(pb, 24, cb->nentries);
  333. for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
  334. if (i == cb->nentries) ordered = 1;
  335. put_bits(pb, 1, ordered);
  336. if (ordered) {
  337. int len = cb->entries[0].len;
  338. put_bits(pb, 5, len);
  339. i = 0;
  340. while (i < cb->nentries) {
  341. int j;
  342. for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
  343. put_bits(pb, ilog(cb->nentries - i), j);
  344. i += j;
  345. len++;
  346. }
  347. } else {
  348. int sparse = 0;
  349. for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
  350. if (i != cb->nentries) sparse = 1;
  351. put_bits(pb, 1, sparse);
  352. for (i = 0; i < cb->nentries; i++) {
  353. if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
  354. if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
  355. }
  356. }
  357. put_bits(pb, 4, cb->lookup);
  358. if (cb->lookup) {
  359. int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  360. int bits = ilog(cb->quantlist[0]);
  361. for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
  362. put_float(pb, cb->min);
  363. put_float(pb, cb->delta);
  364. put_bits(pb, 4, bits - 1);
  365. put_bits(pb, 1, cb->seq_p);
  366. for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
  367. }
  368. }
  369. static void put_floor_header(PutBitContext * pb, floor_t * fc) {
  370. int i;
  371. put_bits(pb, 16, 1); // type, only floor1 is supported
  372. put_bits(pb, 5, fc->partitions);
  373. for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
  374. for (i = 0; i < fc->nclasses; i++) {
  375. int j, books;
  376. put_bits(pb, 3, fc->classes[i].dim - 1);
  377. put_bits(pb, 2, fc->classes[i].subclass);
  378. if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
  379. books = (1 << fc->classes[i].subclass);
  380. for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
  381. }
  382. put_bits(pb, 2, fc->multiplier - 1);
  383. put_bits(pb, 4, fc->rangebits);
  384. for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
  385. }
  386. static void put_residue_header(PutBitContext * pb, residue_t * rc) {
  387. int i;
  388. put_bits(pb, 16, rc->type);
  389. put_bits(pb, 24, rc->begin);
  390. put_bits(pb, 24, rc->end);
  391. put_bits(pb, 24, rc->partition_size - 1);
  392. put_bits(pb, 6, rc->classifications - 1);
  393. put_bits(pb, 8, rc->classbook);
  394. for (i = 0; i < rc->classifications; i++) {
  395. int j, tmp = 0;
  396. for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
  397. put_bits(pb, 3, tmp & 7);
  398. put_bits(pb, 1, tmp > 7);
  399. if (tmp > 7) put_bits(pb, 5, tmp >> 3);
  400. }
  401. for (i = 0; i < rc->classifications; i++) {
  402. int j;
  403. for (j = 0; j < 8; j++)
  404. if (rc->books[i][j])
  405. put_bits(pb, 8, rc->books[i][j]);
  406. }
  407. }
  408. static int put_main_header(venc_context_t * venc, uint8_t ** out) {
  409. int i;
  410. PutBitContext pb;
  411. uint8_t buffer[50000] = {0}, * p = buffer;
  412. int buffer_len = sizeof buffer;
  413. int len, hlens[3];
  414. // identification header
  415. init_put_bits(&pb, p, buffer_len);
  416. put_bits(&pb, 8, 1); //magic
  417. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  418. put_bits(&pb, 32, 0); // version
  419. put_bits(&pb, 8, venc->channels);
  420. put_bits(&pb, 32, venc->sample_rate);
  421. put_bits(&pb, 32, 0); // bitrate
  422. put_bits(&pb, 32, 0); // bitrate
  423. put_bits(&pb, 32, 0); // bitrate
  424. put_bits(&pb, 4, venc->blocksize[0]);
  425. put_bits(&pb, 4, venc->blocksize[1]);
  426. put_bits(&pb, 1, 1); // framing
  427. flush_put_bits(&pb);
  428. hlens[0] = (put_bits_count(&pb) + 7) / 8;
  429. buffer_len -= hlens[0];
  430. p += hlens[0];
  431. // comment header
  432. init_put_bits(&pb, p, buffer_len);
  433. put_bits(&pb, 8, 3); //magic
  434. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  435. put_bits(&pb, 32, 0); // vendor length TODO
  436. put_bits(&pb, 32, 0); // amount of comments
  437. put_bits(&pb, 1, 1); // framing
  438. flush_put_bits(&pb);
  439. hlens[1] = (put_bits_count(&pb) + 7) / 8;
  440. buffer_len -= hlens[1];
  441. p += hlens[1];
  442. // setup header
  443. init_put_bits(&pb, p, buffer_len);
  444. put_bits(&pb, 8, 5); //magic
  445. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  446. // codebooks
  447. put_bits(&pb, 8, venc->ncodebooks - 1);
  448. for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
  449. // time domain, reserved, zero
  450. put_bits(&pb, 6, 0);
  451. put_bits(&pb, 16, 0);
  452. // floors
  453. put_bits(&pb, 6, venc->nfloors - 1);
  454. for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
  455. // residues
  456. put_bits(&pb, 6, venc->nresidues - 1);
  457. for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
  458. // mappings
  459. put_bits(&pb, 6, venc->nmappings - 1);
  460. for (i = 0; i < venc->nmappings; i++) {
  461. mapping_t * mc = &venc->mappings[i];
  462. int j;
  463. put_bits(&pb, 16, 0); // mapping type
  464. put_bits(&pb, 1, mc->submaps > 1);
  465. if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
  466. put_bits(&pb, 1, 0); // channel coupling
  467. put_bits(&pb, 2, 0); // reserved
  468. if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
  469. for (j = 0; j < mc->submaps; j++) {
  470. put_bits(&pb, 8, 0); // reserved time configuration
  471. put_bits(&pb, 8, mc->floor[j]);
  472. put_bits(&pb, 8, mc->residue[j]);
  473. }
  474. }
  475. // modes
  476. put_bits(&pb, 6, venc->nmodes - 1);
  477. for (i = 0; i < venc->nmodes; i++) {
  478. put_bits(&pb, 1, venc->modes[i].blockflag);
  479. put_bits(&pb, 16, 0); // reserved window type
  480. put_bits(&pb, 16, 0); // reserved transform type
  481. put_bits(&pb, 8, venc->modes[i].mapping);
  482. }
  483. put_bits(&pb, 1, 1); // framing
  484. flush_put_bits(&pb);
  485. hlens[2] = (put_bits_count(&pb) + 7) / 8;
  486. len = hlens[0] + hlens[1] + hlens[2];
  487. p = *out = av_mallocz(64 + len + len/255);
  488. *p++ = 2;
  489. p += av_xiphlacing(p, hlens[0]);
  490. p += av_xiphlacing(p, hlens[1]);
  491. buffer_len = 0;
  492. for (i = 0; i < 3; i++) {
  493. memcpy(p, buffer + buffer_len, hlens[i]);
  494. p += hlens[i];
  495. buffer_len += hlens[i];
  496. }
  497. return p - *out;
  498. }
  499. static int vorbis_encode_init(AVCodecContext * avccontext)
  500. {
  501. venc_context_t * venc = avccontext->priv_data;
  502. create_vorbis_context(venc, avccontext);
  503. //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
  504. //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
  505. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  506. avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
  507. avccontext->coded_frame = avcodec_alloc_frame();
  508. avccontext->coded_frame->key_frame = 1;
  509. return 0;
  510. }
  511. static int window(venc_context_t * venc, signed short * audio, int samples) {
  512. int i, j, channel;
  513. const float * win = venc->win[0];
  514. int window_len = 1 << (venc->blocksize[0] - 1);
  515. float n = (float)(1 << venc->blocksize[0]) / 4.;
  516. // FIXME use dsp
  517. if (!venc->have_saved && !samples) return 0;
  518. if (venc->have_saved) {
  519. for (channel = 0; channel < venc->channels; channel++) {
  520. memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
  521. }
  522. } else {
  523. for (channel = 0; channel < venc->channels; channel++) {
  524. memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
  525. }
  526. }
  527. if (samples) {
  528. for (channel = 0; channel < venc->channels; channel++) {
  529. float * offset = venc->samples + channel*window_len*2 + window_len;
  530. j = channel;
  531. for (i = 0; i < samples; i++, j += venc->channels)
  532. offset[i] = audio[j] / 32768. * win[window_len - i] / n;
  533. }
  534. } else {
  535. for (channel = 0; channel < venc->channels; channel++) {
  536. memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
  537. }
  538. }
  539. for (channel = 0; channel < venc->channels; channel++) {
  540. ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
  541. }
  542. if (samples) {
  543. for (channel = 0; channel < venc->channels; channel++) {
  544. float * offset = venc->saved + channel*window_len;
  545. j = channel;
  546. for (i = 0; i < samples; i++, j += venc->channels)
  547. offset[i] = audio[j] / 32768. * win[i] / n;
  548. }
  549. venc->have_saved = 1;
  550. } else {
  551. venc->have_saved = 0;
  552. }
  553. return 1;
  554. }
  555. static float put_vector(codebook_t * book, PutBitContext * pb, float num) {
  556. int i;
  557. int entry = -1;
  558. float distance = 0;
  559. assert(book->dimentions);
  560. assert(book->ndimentions == 1);
  561. for (i = 0; i < book->nentries; i++) {
  562. float d = (book->dimentions[i] - num)*(book->dimentions[i] - num);
  563. if (entry == -1 || distance > d) {
  564. entry = i;
  565. distance = d;
  566. }
  567. }
  568. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  569. return book->dimentions[entry];
  570. }
  571. static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int channels) {
  572. int pass, i, j, p, k;
  573. int psize = rc->partition_size;
  574. int partitions = (rc->end - rc->begin) / psize;
  575. int classes[channels][partitions];
  576. int classwords = venc->codebooks[rc->classbook].ndimentions;
  577. for (pass = 0; pass < 8; pass++) {
  578. p = 0;
  579. while (p < partitions) {
  580. if (pass == 0) for (j = 0; j < channels; j++) {
  581. codebook_t * book = &venc->codebooks[rc->classbook];
  582. int entry = 0;
  583. put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
  584. for (i = classwords; i--; ) {
  585. classes[j][p + i] = entry % rc->classifications;
  586. entry /= rc->classifications;
  587. }
  588. }
  589. for (i = 0; i < classwords && p < partitions; i++, p++) {
  590. for (j = 0; j < channels; j++) {
  591. int nbook = rc->books[classes[j][p]][pass];
  592. codebook_t * book = &venc->codebooks[nbook];
  593. float * buf = coeffs + samples*j + rc->begin + p*psize;
  594. assert(rc->type == 0);
  595. assert(book->ndimentions == 1);
  596. for (k = 0; k < psize; k++) {
  597. buf[k] -= put_vector(book, pb, buf[k]);
  598. }
  599. }
  600. }
  601. }
  602. }
  603. }
  604. static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
  605. {
  606. venc_context_t * venc = avccontext->priv_data;
  607. signed short * audio = data;
  608. int samples = data ? avccontext->frame_size : 0;
  609. vorbis_mode_t * mode;
  610. mapping_t * mapping;
  611. PutBitContext pb;
  612. int i;
  613. if (!window(venc, audio, samples)) return 0;
  614. init_put_bits(&pb, packets, buf_size);
  615. put_bits(&pb, 1, 0); // magic bit
  616. put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
  617. mode = &venc->modes[0];
  618. mapping = &venc->mappings[mode->mapping];
  619. if (mode->blockflag) {
  620. put_bits(&pb, 1, 0);
  621. put_bits(&pb, 1, 0);
  622. }
  623. for (i = 0; i < venc->channels; i++) {
  624. floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
  625. int range = 255 / fc->multiplier + 1;
  626. int j;
  627. put_bits(&pb, 1, 1); // non zero
  628. put_bits(&pb, ilog(range - 1), 113); // magic value - 3.7180282E-05
  629. put_bits(&pb, ilog(range - 1), 113); // both sides of X
  630. for (j = 0; j < fc->partitions; j++) {
  631. floor_class_t * c = &fc->classes[fc->partition_to_class[j]];
  632. codebook_t * book = &venc->codebooks[c->books[0]];
  633. int entry = 0;
  634. int k;
  635. for (k = 0; k < c->dim; k++) {
  636. put_bits(&pb, book->entries[entry].len, book->entries[entry].codeword);
  637. }
  638. }
  639. for (j = 0; j < samples; j++) {
  640. venc->floor[i * samples + j] = floor1_inverse_db_table[113];
  641. }
  642. }
  643. for (i = 0; i < venc->channels; i++) {
  644. int j;
  645. for (j = 0; j < samples; j++) {
  646. venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
  647. }
  648. }
  649. residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
  650. return (put_bits_count(&pb) + 7) / 8;
  651. }
  652. static int vorbis_encode_close(AVCodecContext * avccontext)
  653. {
  654. venc_context_t * venc = avccontext->priv_data;
  655. int i;
  656. if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
  657. av_freep(&venc->codebooks[i].entries);
  658. av_freep(&venc->codebooks[i].quantlist);
  659. av_freep(&venc->codebooks[i].dimentions);
  660. }
  661. av_freep(&venc->codebooks);
  662. if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
  663. int j;
  664. av_freep(&venc->floors[i].classes);
  665. if (venc->floors[i].classes)
  666. for (j = 0; j < venc->floors[i].nclasses; j++)
  667. av_freep(&venc->floors[i].classes[j].books);
  668. av_freep(&venc->floors[i].partition_to_class);
  669. av_freep(&venc->floors[i].list);
  670. }
  671. av_freep(&venc->floors);
  672. if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
  673. av_freep(&venc->residues[i].books);
  674. }
  675. av_freep(&venc->residues);
  676. if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
  677. av_freep(&venc->mappings[i].mux);
  678. av_freep(&venc->mappings[i].floor);
  679. av_freep(&venc->mappings[i].residue);
  680. }
  681. av_freep(&venc->mappings);
  682. av_freep(&venc->modes);
  683. av_freep(&venc->saved);
  684. av_freep(&venc->samples);
  685. av_freep(&venc->floor);
  686. av_freep(&venc->coeffs);
  687. ff_mdct_end(&venc->mdct[0]);
  688. ff_mdct_end(&venc->mdct[1]);
  689. av_freep(&avccontext->coded_frame);
  690. av_freep(&avccontext->extradata);
  691. return 0 ;
  692. }
  693. AVCodec oggvorbis_encoder = {
  694. "vorbis",
  695. CODEC_TYPE_AUDIO,
  696. CODEC_ID_VORBIS,
  697. sizeof(venc_context_t),
  698. vorbis_encode_init,
  699. vorbis_encode_frame,
  700. vorbis_encode_close,
  701. .capabilities= CODEC_CAP_DELAY,
  702. };