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.

613 lines
17KB

  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. #undef NDEBUG
  25. #include <assert.h>
  26. #define ALT_BITSTREAM_READER_LE
  27. #include "bitstream.h"
  28. #define VORBIS_FRAME_SIZE 64
  29. #define BUFFER_SIZE (1024*64)
  30. typedef struct {
  31. int len;
  32. uint32_t codeword;
  33. } cb_entry_t;
  34. typedef struct {
  35. int nentries;
  36. cb_entry_t * entries;
  37. int ndimentions;
  38. float min;
  39. float delta;
  40. int seq_p;
  41. int lookup;
  42. int * quantlist;
  43. float * dimentions;
  44. } codebook_t;
  45. typedef struct {
  46. int dim;
  47. int subclass;
  48. int masterbook;
  49. int * books;
  50. } floor_class_t;
  51. typedef struct {
  52. int partitions;
  53. int * partition_to_class;
  54. int nclasses;
  55. floor_class_t * classes;
  56. int multiplier;
  57. int rangebits;
  58. int values;
  59. struct { int x; } * list;
  60. } floor_t;
  61. typedef struct {
  62. int type;
  63. int begin;
  64. int end;
  65. int partition_size;
  66. int classifications;
  67. int classbook;
  68. int (*books)[8];
  69. } residue_t;
  70. typedef struct {
  71. int submaps;
  72. int * mux;
  73. int * floor;
  74. int * residue;
  75. } mapping_t;
  76. typedef struct {
  77. int blockflag;
  78. int mapping;
  79. } vorbis_mode_t;
  80. typedef struct {
  81. int channels;
  82. int sample_rate;
  83. int blocksize[2]; // in (1<<n) format
  84. int ncodebooks;
  85. codebook_t * codebooks;
  86. int nfloors;
  87. floor_t * floors;
  88. int nresidues;
  89. residue_t * residues;
  90. int nmappings;
  91. mapping_t * mappings;
  92. int nmodes;
  93. vorbis_mode_t * modes;
  94. } venc_context_t;
  95. static int cb_lookup_vals(int lookup, int dimentions, int entries) {
  96. if (lookup == 1) {
  97. int tmp, i;
  98. for (tmp = 0; ; tmp++) {
  99. int n = 1;
  100. for (i = 0; i < dimentions; i++) n *= tmp;
  101. if (n > entries) break;
  102. }
  103. return tmp - 1;
  104. } else if (lookup == 2) return dimentions * entries;
  105. return 0;
  106. }
  107. static void ready_codebook(codebook_t * cb) {
  108. 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 };
  109. int i;
  110. for (i = 0; i < cb->nentries; i++) {
  111. cb_entry_t * e = &cb->entries[i];
  112. int j = 0;
  113. if (h[0]) h[0] = 0;
  114. else for (j = e->len; !h[j]; j--) assert(j);
  115. e->codeword = h[j];
  116. h[j] = 0;
  117. for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
  118. }
  119. for (i = 0; i < 33; i++) assert(!h[i]);
  120. if (!cb->lookup) cb->dimentions = NULL;
  121. else {
  122. int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  123. cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
  124. for (i = 0; i < cb->nentries; i++) {
  125. float last = 0;
  126. int j;
  127. int div = 1;
  128. for (j = 0; j < cb->ndimentions; j++) {
  129. int off;
  130. if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
  131. else off = i * cb->ndimentions + j; // lookup type 2
  132. cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
  133. if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
  134. div *= vals;
  135. }
  136. }
  137. }
  138. }
  139. static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
  140. codebook_t * cb;
  141. floor_t * fc;
  142. residue_t * rc;
  143. mapping_t * mc;
  144. int i, book;
  145. venc->channels = avccontext->channels;
  146. venc->sample_rate = avccontext->sample_rate;
  147. venc->blocksize[0] = venc->blocksize[1] = 8;
  148. venc->ncodebooks = 10;
  149. venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
  150. // codebook 1 - floor1 book, values 0..255
  151. cb = &venc->codebooks[0];
  152. cb->nentries = 256;
  153. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  154. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 8;
  155. cb->ndimentions = 0;
  156. cb->min = 0.;
  157. cb->delta = 0.;
  158. cb->seq_p = 0;
  159. cb->lookup = 0;
  160. cb->quantlist = NULL;
  161. ready_codebook(cb);
  162. // codebook 2 - residue classbook, values 0..1, dimentions 200
  163. cb = &venc->codebooks[1];
  164. cb->nentries = 2;
  165. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  166. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = 1;
  167. cb->ndimentions = 200;
  168. cb->min = 0.;
  169. cb->delta = 0.;
  170. cb->seq_p = 0;
  171. cb->lookup = 0;
  172. cb->quantlist = NULL;
  173. ready_codebook(cb);
  174. // codebook 3..10 - vector, for the residue, values -32767..32767, dimentions 1
  175. for (book = 0; book < 8; book++) {
  176. cb = &venc->codebooks[2 + book];
  177. cb->nentries = 5;
  178. cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
  179. for (i = 0; i < cb->nentries; i++) cb->entries[i].len = i == 2 ? 1 : 3;
  180. cb->ndimentions = 1;
  181. cb->delta = 1 << ((7 - book) * 2);
  182. cb->min = -cb->delta*2;
  183. cb->seq_p = 0;
  184. cb->lookup = 2;
  185. cb->quantlist = av_malloc(sizeof(int) * cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries));
  186. for (i = 0; i < cb->nentries; i++) cb->quantlist[i] = i;
  187. ready_codebook(cb);
  188. }
  189. venc->nfloors = 1;
  190. venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
  191. // just 1 floor
  192. fc = &venc->floors[0];
  193. fc->partitions = 1;
  194. fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
  195. for (i = 0; i < fc->partitions; i++) fc->partition_to_class = 0;
  196. fc->nclasses = 1;
  197. fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
  198. for (i = 0; i < fc->nclasses; i++) {
  199. floor_class_t * c = &fc->classes[i];
  200. int j, books;
  201. c->dim = 1;
  202. c->subclass = 0;
  203. c->masterbook = 0;
  204. books = (1 << c->subclass);
  205. c->books = av_malloc(sizeof(int) * books);
  206. for (j = 0; j < books; j++) c->books[j] = 0;
  207. }
  208. fc->multiplier = 1;
  209. fc->rangebits = venc->blocksize[0];
  210. fc->values = 2;
  211. for (i = 0; i < fc->partitions; i++)
  212. fc->values += fc->classes[fc->partition_to_class[i]].dim;
  213. fc->list = av_malloc(sizeof(*fc->list) * fc->values);
  214. fc->list[0].x = 0;
  215. fc->list[1].x = 1 << fc->rangebits;
  216. for (i = 2; i < fc->values; i++) fc->list[i].x = i * 5;
  217. venc->nresidues = 1;
  218. venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
  219. // single residue
  220. rc = &venc->residues[0];
  221. rc->type = 0;
  222. rc->begin = 0;
  223. rc->end = 1 << venc->blocksize[0];
  224. rc->partition_size = 64;
  225. rc->classifications = 1;
  226. rc->classbook = 1;
  227. rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
  228. for (i = 0; i < 8; i++) rc->books[0][i] = 2 + i;
  229. venc->nmappings = 1;
  230. venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
  231. // single mapping
  232. mc = &venc->mappings[0];
  233. mc->submaps = 1;
  234. mc->mux = av_malloc(sizeof(int) * venc->channels);
  235. for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
  236. mc->floor = av_malloc(sizeof(int) * mc->submaps);
  237. mc->residue = av_malloc(sizeof(int) * mc->submaps);
  238. for (i = 0; i < mc->submaps; i++) {
  239. mc->floor[i] = 0;
  240. mc->residue[i] = 0;
  241. }
  242. venc->nmodes = 1;
  243. venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
  244. // single mode
  245. venc->modes[0].blockflag = 0;
  246. venc->modes[0].mapping = 0;
  247. }
  248. static inline int ilog(unsigned int a) {
  249. int i;
  250. for (i = 0; a >> i; i++);
  251. return i;
  252. }
  253. static void put_float(PutBitContext * pb, float f) {
  254. int exp, mant;
  255. uint32_t res = 0;
  256. mant = (int)ldexp(frexp(f, &exp), 20);
  257. exp += 788 - 20;
  258. if (mant < 0) { res |= (1 << 31); mant = -mant; }
  259. res |= mant | (exp << 21);
  260. put_bits(pb, 32, res);
  261. }
  262. static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
  263. int i;
  264. int ordered = 0;
  265. put_bits(pb, 24, 0x564342); //magic
  266. put_bits(pb, 16, cb->ndimentions);
  267. put_bits(pb, 24, cb->nentries);
  268. for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
  269. if (i == cb->nentries) ordered = 1;
  270. put_bits(pb, 1, ordered);
  271. if (ordered) {
  272. int len = cb->entries[0].len;
  273. put_bits(pb, 5, len);
  274. i = 0;
  275. while (i < cb->nentries) {
  276. int j;
  277. for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
  278. put_bits(pb, ilog(cb->nentries - i), j);
  279. i += j;
  280. len++;
  281. }
  282. } else {
  283. int sparse = 0;
  284. for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
  285. if (i != cb->nentries) sparse = 1;
  286. put_bits(pb, 1, sparse);
  287. for (i = 0; i < cb->nentries; i++) {
  288. if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
  289. if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len);
  290. }
  291. }
  292. put_bits(pb, 4, cb->lookup);
  293. if (cb->lookup) {
  294. int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
  295. int bits = ilog(cb->quantlist[0]);
  296. for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
  297. put_float(pb, cb->min);
  298. put_float(pb, cb->delta);
  299. put_bits(pb, 4, bits - 1);
  300. put_bits(pb, 1, cb->seq_p);
  301. for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
  302. }
  303. }
  304. static void put_floor_header(PutBitContext * pb, floor_t * fc) {
  305. int i;
  306. put_bits(pb, 16, 1); // type, only floor1 is supported
  307. put_bits(pb, 5, fc->partitions);
  308. for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
  309. for (i = 0; i < fc->nclasses; i++) {
  310. int j, books;
  311. put_bits(pb, 3, fc->classes[i].dim - 1);
  312. put_bits(pb, 2, fc->classes[i].subclass);
  313. if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
  314. books = (1 << fc->classes[i].subclass);
  315. for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
  316. }
  317. put_bits(pb, 2, fc->multiplier - 1);
  318. put_bits(pb, 4, fc->rangebits);
  319. for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
  320. }
  321. static void put_residue_header(PutBitContext * pb, residue_t * rc) {
  322. int i;
  323. put_bits(pb, 16, rc->type);
  324. put_bits(pb, 24, rc->begin);
  325. put_bits(pb, 24, rc->end);
  326. put_bits(pb, 24, rc->partition_size - 1);
  327. put_bits(pb, 6, rc->classifications);
  328. put_bits(pb, 8, rc->classbook);
  329. for (i = 0; i < rc->classifications; i++) {
  330. int j, tmp = 0;
  331. for (j = 0; j < 8; j++) tmp |= (!!rc->books[i][j]) << j;
  332. put_bits(pb, 3, tmp & 7);
  333. put_bits(pb, 1, tmp > 7);
  334. if (tmp > 7) put_bits(pb, 5, tmp >> 3);
  335. }
  336. for (i = 0; i < rc->classifications; i++) {
  337. int j;
  338. for (j = 0; j < 8; j++)
  339. if (rc->books[i][j])
  340. put_bits(pb, 8, rc->books[i][j]);
  341. }
  342. }
  343. static int put_main_header(venc_context_t * venc, uint8_t ** out) {
  344. int i;
  345. PutBitContext pb;
  346. uint8_t buffer[50000] = {0}, * p = buffer;
  347. int buffer_len = sizeof buffer;
  348. int len, hlens[3];
  349. // identification header
  350. init_put_bits(&pb, p, buffer_len);
  351. put_bits(&pb, 8, 1); //magic
  352. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  353. put_bits(&pb, 32, 0); // version
  354. put_bits(&pb, 8, venc->channels);
  355. put_bits(&pb, 32, venc->sample_rate);
  356. put_bits(&pb, 32, 0); // bitrate
  357. put_bits(&pb, 32, 0); // bitrate
  358. put_bits(&pb, 32, 0); // bitrate
  359. put_bits(&pb, 4, venc->blocksize[0]);
  360. put_bits(&pb, 4, venc->blocksize[1]);
  361. put_bits(&pb, 1, 1); // framing
  362. flush_put_bits(&pb);
  363. hlens[0] = (put_bits_count(&pb) + 7) / 8;
  364. buffer_len -= hlens[0];
  365. p += hlens[0];
  366. // comment header
  367. init_put_bits(&pb, p, buffer_len);
  368. put_bits(&pb, 8, 3); //magic
  369. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  370. put_bits(&pb, 32, 0); // vendor length TODO
  371. put_bits(&pb, 32, 0); // amount of comments
  372. put_bits(&pb, 1, 1); // framing
  373. flush_put_bits(&pb);
  374. hlens[1] = (put_bits_count(&pb) + 7) / 8;
  375. buffer_len -= hlens[1];
  376. p += hlens[1];
  377. // setup header
  378. init_put_bits(&pb, p, buffer_len);
  379. put_bits(&pb, 8, 5); //magic
  380. for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
  381. // codebooks
  382. put_bits(&pb, 8, venc->ncodebooks - 1);
  383. for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
  384. // time domain, reserved, zero
  385. put_bits(&pb, 6, 0);
  386. put_bits(&pb, 16, 0);
  387. // floors
  388. put_bits(&pb, 6, venc->nfloors - 1);
  389. for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
  390. // residues
  391. put_bits(&pb, 6, venc->nresidues - 1);
  392. for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
  393. // mappings
  394. put_bits(&pb, 6, venc->nmappings - 1);
  395. for (i = 0; i < venc->nmappings; i++) {
  396. mapping_t * mc = &venc->mappings[i];
  397. int j;
  398. put_bits(&pb, 16, 0); // mapping type
  399. put_bits(&pb, 1, mc->submaps > 1);
  400. if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
  401. put_bits(&pb, 1, 0); // channel coupling
  402. put_bits(&pb, 2, 0); // reserved
  403. if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
  404. for (j = 0; j < mc->submaps; j++) {
  405. put_bits(&pb, 8, 0); // reserved time configuration
  406. put_bits(&pb, 8, mc->floor[j]);
  407. put_bits(&pb, 8, mc->residue[j]);
  408. }
  409. }
  410. // modes
  411. put_bits(&pb, 6, venc->nmodes - 1);
  412. for (i = 0; i < venc->nmodes; i++) {
  413. put_bits(&pb, 1, venc->modes[i].blockflag);
  414. put_bits(&pb, 16, 0); // reserved window type
  415. put_bits(&pb, 16, 0); // reserved transform type
  416. put_bits(&pb, 8, venc->modes[i].mapping);
  417. }
  418. flush_put_bits(&pb);
  419. hlens[2] = (put_bits_count(&pb) + 7) / 8;
  420. len = hlens[0] + hlens[1] + hlens[2];
  421. p = *out = av_mallocz(64 + len + len/255);
  422. *p++ = 2;
  423. p += av_xiphlacing(p, hlens[0]);
  424. p += av_xiphlacing(p, hlens[1]);
  425. buffer_len = 0;
  426. for (i = 0; i < 3; i++) {
  427. memcpy(p, buffer + buffer_len, hlens[i]);
  428. p += hlens[i];
  429. buffer_len += hlens[i];
  430. }
  431. return p - *out;
  432. }
  433. static int vorbis_encode_init(AVCodecContext * avccontext)
  434. {
  435. venc_context_t * venc = avccontext->priv_data;
  436. create_vorbis_context(venc, avccontext);
  437. //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
  438. //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
  439. avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
  440. avccontext->frame_size = VORBIS_FRAME_SIZE;
  441. avccontext->coded_frame = avcodec_alloc_frame();
  442. avccontext->coded_frame->key_frame = 1;
  443. return 0;
  444. }
  445. static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
  446. {
  447. #if 0
  448. venc_context_t * venc = avccontext->priv_data;
  449. signed short * audio = data;
  450. int samples = data ? VORBIS_FRAME_SIZE : 0;
  451. avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
  452. memcpy(packets, compressed_frame, l);
  453. #endif
  454. return data ? 50 : 0;
  455. }
  456. static int vorbis_encode_close(AVCodecContext * avccontext)
  457. {
  458. venc_context_t * venc = avccontext->priv_data;
  459. int i;
  460. if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
  461. av_freep(&venc->codebooks[i].entries);
  462. av_freep(&venc->codebooks[i].quantlist);
  463. av_freep(&venc->codebooks[i].dimentions);
  464. }
  465. av_freep(&venc->codebooks);
  466. if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
  467. int j;
  468. av_freep(&venc->floors[i].classes);
  469. if (venc->floors[i].classes)
  470. for (j = 0; j < venc->floors[i].nclasses; j++)
  471. av_freep(&venc->floors[i].classes[j].books);
  472. av_freep(&venc->floors[i].partition_to_class);
  473. av_freep(&venc->floors[i].list);
  474. }
  475. av_freep(&venc->floors);
  476. if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
  477. av_freep(&venc->residues[i].books);
  478. }
  479. av_freep(&venc->residues);
  480. if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
  481. av_freep(&venc->mappings[i].mux);
  482. av_freep(&venc->mappings[i].floor);
  483. av_freep(&venc->mappings[i].residue);
  484. }
  485. av_freep(&venc->mappings);
  486. av_freep(&venc->modes);
  487. av_freep(&avccontext->coded_frame);
  488. av_freep(&avccontext->extradata);
  489. return 0 ;
  490. }
  491. AVCodec oggvorbis_encoder = {
  492. "vorbis",
  493. CODEC_TYPE_AUDIO,
  494. CODEC_ID_VORBIS,
  495. sizeof(venc_context_t),
  496. vorbis_encode_init,
  497. vorbis_encode_frame,
  498. vorbis_encode_close,
  499. .capabilities= CODEC_CAP_DELAY,
  500. };