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.

1309 lines
40KB

  1. /*
  2. * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Native Vorbis encoder.
  23. * @author Oded Shimon <ods15@ods15.dyndns.org>
  24. */
  25. #include <float.h>
  26. #include "libavutil/float_dsp.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "fft.h"
  30. #include "mathops.h"
  31. #include "vorbis.h"
  32. #include "vorbis_enc_data.h"
  33. #include "audio_frame_queue.h"
  34. #include "libavfilter/bufferqueue.h"
  35. #define BITSTREAM_WRITER_LE
  36. #include "put_bits.h"
  37. #undef NDEBUG
  38. #include <assert.h>
  39. typedef struct vorbis_enc_codebook {
  40. int nentries;
  41. uint8_t *lens;
  42. uint32_t *codewords;
  43. int ndimensions;
  44. float min;
  45. float delta;
  46. int seq_p;
  47. int lookup;
  48. int *quantlist;
  49. float *dimensions;
  50. float *pow2;
  51. } vorbis_enc_codebook;
  52. typedef struct vorbis_enc_floor_class {
  53. int dim;
  54. int subclass;
  55. int masterbook;
  56. int *books;
  57. } vorbis_enc_floor_class;
  58. typedef struct vorbis_enc_floor {
  59. int partitions;
  60. int *partition_to_class;
  61. int nclasses;
  62. vorbis_enc_floor_class *classes;
  63. int multiplier;
  64. int rangebits;
  65. int values;
  66. vorbis_floor1_entry *list;
  67. } vorbis_enc_floor;
  68. typedef struct vorbis_enc_residue {
  69. int type;
  70. int begin;
  71. int end;
  72. int partition_size;
  73. int classifications;
  74. int classbook;
  75. int8_t (*books)[8];
  76. float (*maxes)[2];
  77. } vorbis_enc_residue;
  78. typedef struct vorbis_enc_mapping {
  79. int submaps;
  80. int *mux;
  81. int *floor;
  82. int *residue;
  83. int coupling_steps;
  84. int *magnitude;
  85. int *angle;
  86. } vorbis_enc_mapping;
  87. typedef struct vorbis_enc_mode {
  88. int blockflag;
  89. int mapping;
  90. } vorbis_enc_mode;
  91. typedef struct vorbis_enc_context {
  92. int channels;
  93. int sample_rate;
  94. int log2_blocksize[2];
  95. FFTContext mdct[2];
  96. const float *win[2];
  97. int have_saved;
  98. float *saved;
  99. float *samples;
  100. float *floor; // also used for tmp values for mdct
  101. float *coeffs; // also used for residue after floor
  102. float *scratch; // used for tmp values for psy model
  103. float quality;
  104. AudioFrameQueue afq;
  105. struct FFBufQueue bufqueue;
  106. int ncodebooks;
  107. vorbis_enc_codebook *codebooks;
  108. int nfloors;
  109. vorbis_enc_floor *floors;
  110. int nresidues;
  111. vorbis_enc_residue *residues;
  112. int nmappings;
  113. vorbis_enc_mapping *mappings;
  114. int nmodes;
  115. vorbis_enc_mode *modes;
  116. int64_t next_pts;
  117. AVFloatDSPContext *fdsp;
  118. } vorbis_enc_context;
  119. #define MAX_CHANNELS 2
  120. #define MAX_CODEBOOK_DIM 8
  121. #define MAX_FLOOR_CLASS_DIM 4
  122. #define NUM_FLOOR_PARTITIONS 8
  123. #define MAX_FLOOR_VALUES (MAX_FLOOR_CLASS_DIM*NUM_FLOOR_PARTITIONS+2)
  124. #define RESIDUE_SIZE 1600
  125. #define RESIDUE_PART_SIZE 32
  126. #define NUM_RESIDUE_PARTITIONS (RESIDUE_SIZE/RESIDUE_PART_SIZE)
  127. static inline int put_codeword(PutBitContext *pb, vorbis_enc_codebook *cb,
  128. int entry)
  129. {
  130. av_assert2(entry >= 0);
  131. av_assert2(entry < cb->nentries);
  132. av_assert2(cb->lens[entry]);
  133. if (pb->size_in_bits - put_bits_count(pb) < cb->lens[entry])
  134. return AVERROR(EINVAL);
  135. put_bits(pb, cb->lens[entry], cb->codewords[entry]);
  136. return 0;
  137. }
  138. static int cb_lookup_vals(int lookup, int dimensions, int entries)
  139. {
  140. if (lookup == 1)
  141. return ff_vorbis_nth_root(entries, dimensions);
  142. else if (lookup == 2)
  143. return dimensions *entries;
  144. return 0;
  145. }
  146. static int ready_codebook(vorbis_enc_codebook *cb)
  147. {
  148. int i;
  149. ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
  150. if (!cb->lookup) {
  151. cb->pow2 = cb->dimensions = NULL;
  152. } else {
  153. int vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
  154. cb->dimensions = av_malloc_array(cb->nentries, sizeof(float) * cb->ndimensions);
  155. cb->pow2 = av_mallocz_array(cb->nentries, sizeof(float));
  156. if (!cb->dimensions || !cb->pow2)
  157. return AVERROR(ENOMEM);
  158. for (i = 0; i < cb->nentries; i++) {
  159. float last = 0;
  160. int j;
  161. int div = 1;
  162. for (j = 0; j < cb->ndimensions; j++) {
  163. int off;
  164. if (cb->lookup == 1)
  165. off = (i / div) % vals; // lookup type 1
  166. else
  167. off = i * cb->ndimensions + j; // lookup type 2
  168. cb->dimensions[i * cb->ndimensions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
  169. if (cb->seq_p)
  170. last = cb->dimensions[i * cb->ndimensions + j];
  171. cb->pow2[i] += cb->dimensions[i * cb->ndimensions + j] * cb->dimensions[i * cb->ndimensions + j];
  172. div *= vals;
  173. }
  174. cb->pow2[i] /= 2.0;
  175. }
  176. }
  177. return 0;
  178. }
  179. static int ready_residue(vorbis_enc_residue *rc, vorbis_enc_context *venc)
  180. {
  181. int i;
  182. av_assert0(rc->type == 2);
  183. rc->maxes = av_mallocz_array(rc->classifications, sizeof(float[2]));
  184. if (!rc->maxes)
  185. return AVERROR(ENOMEM);
  186. for (i = 0; i < rc->classifications; i++) {
  187. int j;
  188. vorbis_enc_codebook * cb;
  189. for (j = 0; j < 8; j++)
  190. if (rc->books[i][j] != -1)
  191. break;
  192. if (j == 8) // zero
  193. continue;
  194. cb = &venc->codebooks[rc->books[i][j]];
  195. assert(cb->ndimensions >= 2);
  196. assert(cb->lookup);
  197. for (j = 0; j < cb->nentries; j++) {
  198. float a;
  199. if (!cb->lens[j])
  200. continue;
  201. a = fabs(cb->dimensions[j * cb->ndimensions]);
  202. if (a > rc->maxes[i][0])
  203. rc->maxes[i][0] = a;
  204. a = fabs(cb->dimensions[j * cb->ndimensions + 1]);
  205. if (a > rc->maxes[i][1])
  206. rc->maxes[i][1] = a;
  207. }
  208. }
  209. // small bias
  210. for (i = 0; i < rc->classifications; i++) {
  211. rc->maxes[i][0] += 0.8;
  212. rc->maxes[i][1] += 0.8;
  213. }
  214. return 0;
  215. }
  216. static av_cold int dsp_init(AVCodecContext *avctx, vorbis_enc_context *venc)
  217. {
  218. int ret = 0;
  219. venc->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  220. if (!venc->fdsp)
  221. return AVERROR(ENOMEM);
  222. // init windows
  223. venc->win[0] = ff_vorbis_vwin[venc->log2_blocksize[0] - 6];
  224. venc->win[1] = ff_vorbis_vwin[venc->log2_blocksize[1] - 6];
  225. if ((ret = ff_mdct_init(&venc->mdct[0], venc->log2_blocksize[0], 0, 1.0)) < 0)
  226. return ret;
  227. if ((ret = ff_mdct_init(&venc->mdct[1], venc->log2_blocksize[1], 0, 1.0)) < 0)
  228. return ret;
  229. return 0;
  230. }
  231. static int create_vorbis_context(vorbis_enc_context *venc,
  232. AVCodecContext *avctx)
  233. {
  234. vorbis_enc_floor *fc;
  235. vorbis_enc_residue *rc;
  236. vorbis_enc_mapping *mc;
  237. int i, book, ret;
  238. venc->channels = avctx->channels;
  239. venc->sample_rate = avctx->sample_rate;
  240. venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
  241. venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
  242. venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
  243. if (!venc->codebooks)
  244. return AVERROR(ENOMEM);
  245. // codebook 0..14 - floor1 book, values 0..255
  246. // codebook 15 residue masterbook
  247. // codebook 16..29 residue
  248. for (book = 0; book < venc->ncodebooks; book++) {
  249. vorbis_enc_codebook *cb = &venc->codebooks[book];
  250. int vals;
  251. cb->ndimensions = cvectors[book].dim;
  252. cb->nentries = cvectors[book].real_len;
  253. cb->min = cvectors[book].min;
  254. cb->delta = cvectors[book].delta;
  255. cb->lookup = cvectors[book].lookup;
  256. cb->seq_p = 0;
  257. cb->lens = av_malloc_array(cb->nentries, sizeof(uint8_t));
  258. cb->codewords = av_malloc_array(cb->nentries, sizeof(uint32_t));
  259. if (!cb->lens || !cb->codewords)
  260. return AVERROR(ENOMEM);
  261. memcpy(cb->lens, cvectors[book].clens, cvectors[book].len);
  262. memset(cb->lens + cvectors[book].len, 0, cb->nentries - cvectors[book].len);
  263. if (cb->lookup) {
  264. vals = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
  265. cb->quantlist = av_malloc_array(vals, sizeof(int));
  266. if (!cb->quantlist)
  267. return AVERROR(ENOMEM);
  268. for (i = 0; i < vals; i++)
  269. cb->quantlist[i] = cvectors[book].quant[i];
  270. } else {
  271. cb->quantlist = NULL;
  272. }
  273. if ((ret = ready_codebook(cb)) < 0)
  274. return ret;
  275. }
  276. venc->nfloors = 1;
  277. venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
  278. if (!venc->floors)
  279. return AVERROR(ENOMEM);
  280. // just 1 floor
  281. fc = &venc->floors[0];
  282. fc->partitions = NUM_FLOOR_PARTITIONS;
  283. fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
  284. if (!fc->partition_to_class)
  285. return AVERROR(ENOMEM);
  286. fc->nclasses = 0;
  287. for (i = 0; i < fc->partitions; i++) {
  288. static const int a[] = {0, 1, 2, 2, 3, 3, 4, 4};
  289. fc->partition_to_class[i] = a[i];
  290. fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
  291. }
  292. fc->nclasses++;
  293. fc->classes = av_malloc_array(fc->nclasses, sizeof(vorbis_enc_floor_class));
  294. if (!fc->classes)
  295. return AVERROR(ENOMEM);
  296. for (i = 0; i < fc->nclasses; i++) {
  297. vorbis_enc_floor_class * c = &fc->classes[i];
  298. int j, books;
  299. c->dim = floor_classes[i].dim;
  300. c->subclass = floor_classes[i].subclass;
  301. c->masterbook = floor_classes[i].masterbook;
  302. books = (1 << c->subclass);
  303. c->books = av_malloc_array(books, sizeof(int));
  304. if (!c->books)
  305. return AVERROR(ENOMEM);
  306. for (j = 0; j < books; j++)
  307. c->books[j] = floor_classes[i].nbooks[j];
  308. }
  309. fc->multiplier = 2;
  310. fc->rangebits = venc->log2_blocksize[1] - 1;
  311. fc->values = 2;
  312. for (i = 0; i < fc->partitions; i++)
  313. fc->values += fc->classes[fc->partition_to_class[i]].dim;
  314. fc->list = av_malloc_array(fc->values, sizeof(vorbis_floor1_entry));
  315. if (!fc->list)
  316. return AVERROR(ENOMEM);
  317. fc->list[0].x = 0;
  318. fc->list[1].x = 1 << fc->rangebits;
  319. for (i = 2; i < fc->values; i++) {
  320. static const int a[] = {
  321. 93, 23,372, 6, 46,186,750, 14, 33, 65,
  322. 130,260,556, 3, 10, 18, 28, 39, 55, 79,
  323. 111,158,220,312,464,650,850
  324. };
  325. fc->list[i].x = a[i - 2];
  326. }
  327. if (ff_vorbis_ready_floor1_list(avctx, fc->list, fc->values))
  328. return AVERROR_BUG;
  329. venc->nresidues = 1;
  330. venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
  331. if (!venc->residues)
  332. return AVERROR(ENOMEM);
  333. // single residue
  334. rc = &venc->residues[0];
  335. rc->type = 2;
  336. rc->begin = 0;
  337. rc->end = 1600;
  338. rc->partition_size = 32;
  339. rc->classifications = 10;
  340. rc->classbook = 15;
  341. rc->books = av_malloc(sizeof(*rc->books) * rc->classifications);
  342. if (!rc->books)
  343. return AVERROR(ENOMEM);
  344. {
  345. static const int8_t a[10][8] = {
  346. { -1, -1, -1, -1, -1, -1, -1, -1, },
  347. { -1, -1, 16, -1, -1, -1, -1, -1, },
  348. { -1, -1, 17, -1, -1, -1, -1, -1, },
  349. { -1, -1, 18, -1, -1, -1, -1, -1, },
  350. { -1, -1, 19, -1, -1, -1, -1, -1, },
  351. { -1, -1, 20, -1, -1, -1, -1, -1, },
  352. { -1, -1, 21, -1, -1, -1, -1, -1, },
  353. { 22, 23, -1, -1, -1, -1, -1, -1, },
  354. { 24, 25, -1, -1, -1, -1, -1, -1, },
  355. { 26, 27, 28, -1, -1, -1, -1, -1, },
  356. };
  357. memcpy(rc->books, a, sizeof a);
  358. }
  359. if ((ret = ready_residue(rc, venc)) < 0)
  360. return ret;
  361. venc->nmappings = 1;
  362. venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
  363. if (!venc->mappings)
  364. return AVERROR(ENOMEM);
  365. // single mapping
  366. mc = &venc->mappings[0];
  367. mc->submaps = 1;
  368. mc->mux = av_malloc(sizeof(int) * venc->channels);
  369. if (!mc->mux)
  370. return AVERROR(ENOMEM);
  371. for (i = 0; i < venc->channels; i++)
  372. mc->mux[i] = 0;
  373. mc->floor = av_malloc(sizeof(int) * mc->submaps);
  374. mc->residue = av_malloc(sizeof(int) * mc->submaps);
  375. if (!mc->floor || !mc->residue)
  376. return AVERROR(ENOMEM);
  377. for (i = 0; i < mc->submaps; i++) {
  378. mc->floor[i] = 0;
  379. mc->residue[i] = 0;
  380. }
  381. mc->coupling_steps = venc->channels == 2 ? 1 : 0;
  382. mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
  383. mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
  384. if (!mc->magnitude || !mc->angle)
  385. return AVERROR(ENOMEM);
  386. if (mc->coupling_steps) {
  387. mc->magnitude[0] = 0;
  388. mc->angle[0] = 1;
  389. }
  390. venc->nmodes = 2;
  391. venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
  392. if (!venc->modes)
  393. return AVERROR(ENOMEM);
  394. // Short block
  395. venc->modes[0].blockflag = 0;
  396. venc->modes[0].mapping = 0;
  397. // Long block
  398. venc->modes[1].blockflag = 1;
  399. venc->modes[1].mapping = 0;
  400. venc->have_saved = 0;
  401. venc->saved = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
  402. venc->samples = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
  403. venc->floor = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
  404. venc->coeffs = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]) / 2);
  405. venc->scratch = av_malloc_array(sizeof(float) * venc->channels, (1 << venc->log2_blocksize[1]));
  406. if (!venc->saved || !venc->samples || !venc->floor || !venc->coeffs || !venc->scratch)
  407. return AVERROR(ENOMEM);
  408. if ((ret = dsp_init(avctx, venc)) < 0)
  409. return ret;
  410. return 0;
  411. }
  412. static void put_float(PutBitContext *pb, float f)
  413. {
  414. int exp, mant;
  415. uint32_t res = 0;
  416. mant = (int)ldexp(frexp(f, &exp), 20);
  417. exp += 788 - 20;
  418. if (mant < 0) {
  419. res |= (1U << 31);
  420. mant = -mant;
  421. }
  422. res |= mant | (exp << 21);
  423. put_bits32(pb, res);
  424. }
  425. static void put_codebook_header(PutBitContext *pb, vorbis_enc_codebook *cb)
  426. {
  427. int i;
  428. int ordered = 0;
  429. put_bits(pb, 24, 0x564342); //magic
  430. put_bits(pb, 16, cb->ndimensions);
  431. put_bits(pb, 24, cb->nentries);
  432. for (i = 1; i < cb->nentries; i++)
  433. if (cb->lens[i] < cb->lens[i-1])
  434. break;
  435. if (i == cb->nentries)
  436. ordered = 1;
  437. put_bits(pb, 1, ordered);
  438. if (ordered) {
  439. int len = cb->lens[0];
  440. put_bits(pb, 5, len - 1);
  441. i = 0;
  442. while (i < cb->nentries) {
  443. int j;
  444. for (j = 0; j+i < cb->nentries; j++)
  445. if (cb->lens[j+i] != len)
  446. break;
  447. put_bits(pb, ilog(cb->nentries - i), j);
  448. i += j;
  449. len++;
  450. }
  451. } else {
  452. int sparse = 0;
  453. for (i = 0; i < cb->nentries; i++)
  454. if (!cb->lens[i])
  455. break;
  456. if (i != cb->nentries)
  457. sparse = 1;
  458. put_bits(pb, 1, sparse);
  459. for (i = 0; i < cb->nentries; i++) {
  460. if (sparse)
  461. put_bits(pb, 1, !!cb->lens[i]);
  462. if (cb->lens[i])
  463. put_bits(pb, 5, cb->lens[i] - 1);
  464. }
  465. }
  466. put_bits(pb, 4, cb->lookup);
  467. if (cb->lookup) {
  468. int tmp = cb_lookup_vals(cb->lookup, cb->ndimensions, cb->nentries);
  469. int bits = ilog(cb->quantlist[0]);
  470. for (i = 1; i < tmp; i++)
  471. bits = FFMAX(bits, ilog(cb->quantlist[i]));
  472. put_float(pb, cb->min);
  473. put_float(pb, cb->delta);
  474. put_bits(pb, 4, bits - 1);
  475. put_bits(pb, 1, cb->seq_p);
  476. for (i = 0; i < tmp; i++)
  477. put_bits(pb, bits, cb->quantlist[i]);
  478. }
  479. }
  480. static void put_floor_header(PutBitContext *pb, vorbis_enc_floor *fc)
  481. {
  482. int i;
  483. put_bits(pb, 16, 1); // type, only floor1 is supported
  484. put_bits(pb, 5, fc->partitions);
  485. for (i = 0; i < fc->partitions; i++)
  486. put_bits(pb, 4, fc->partition_to_class[i]);
  487. for (i = 0; i < fc->nclasses; i++) {
  488. int j, books;
  489. put_bits(pb, 3, fc->classes[i].dim - 1);
  490. put_bits(pb, 2, fc->classes[i].subclass);
  491. if (fc->classes[i].subclass)
  492. put_bits(pb, 8, fc->classes[i].masterbook);
  493. books = (1 << fc->classes[i].subclass);
  494. for (j = 0; j < books; j++)
  495. put_bits(pb, 8, fc->classes[i].books[j] + 1);
  496. }
  497. put_bits(pb, 2, fc->multiplier - 1);
  498. put_bits(pb, 4, fc->rangebits);
  499. for (i = 2; i < fc->values; i++)
  500. put_bits(pb, fc->rangebits, fc->list[i].x);
  501. }
  502. static void put_residue_header(PutBitContext *pb, vorbis_enc_residue *rc)
  503. {
  504. int i;
  505. put_bits(pb, 16, rc->type);
  506. put_bits(pb, 24, rc->begin);
  507. put_bits(pb, 24, rc->end);
  508. put_bits(pb, 24, rc->partition_size - 1);
  509. put_bits(pb, 6, rc->classifications - 1);
  510. put_bits(pb, 8, rc->classbook);
  511. for (i = 0; i < rc->classifications; i++) {
  512. int j, tmp = 0;
  513. for (j = 0; j < 8; j++)
  514. tmp |= (rc->books[i][j] != -1) << j;
  515. put_bits(pb, 3, tmp & 7);
  516. put_bits(pb, 1, tmp > 7);
  517. if (tmp > 7)
  518. put_bits(pb, 5, tmp >> 3);
  519. }
  520. for (i = 0; i < rc->classifications; i++) {
  521. int j;
  522. for (j = 0; j < 8; j++)
  523. if (rc->books[i][j] != -1)
  524. put_bits(pb, 8, rc->books[i][j]);
  525. }
  526. }
  527. static int put_main_header(vorbis_enc_context *venc, uint8_t **out)
  528. {
  529. int i;
  530. PutBitContext pb;
  531. int len, hlens[3];
  532. int buffer_len = 50000;
  533. uint8_t *buffer = av_mallocz(buffer_len), *p = buffer;
  534. if (!buffer)
  535. return AVERROR(ENOMEM);
  536. // identification header
  537. init_put_bits(&pb, p, buffer_len);
  538. put_bits(&pb, 8, 1); //magic
  539. for (i = 0; "vorbis"[i]; i++)
  540. put_bits(&pb, 8, "vorbis"[i]);
  541. put_bits32(&pb, 0); // version
  542. put_bits(&pb, 8, venc->channels);
  543. put_bits32(&pb, venc->sample_rate);
  544. put_bits32(&pb, 0); // bitrate
  545. put_bits32(&pb, 0); // bitrate
  546. put_bits32(&pb, 0); // bitrate
  547. put_bits(&pb, 4, venc->log2_blocksize[0]);
  548. put_bits(&pb, 4, venc->log2_blocksize[1]);
  549. put_bits(&pb, 1, 1); // framing
  550. flush_put_bits(&pb);
  551. hlens[0] = put_bits_count(&pb) >> 3;
  552. buffer_len -= hlens[0];
  553. p += hlens[0];
  554. // comment header
  555. init_put_bits(&pb, p, buffer_len);
  556. put_bits(&pb, 8, 3); //magic
  557. for (i = 0; "vorbis"[i]; i++)
  558. put_bits(&pb, 8, "vorbis"[i]);
  559. put_bits32(&pb, 0); // vendor length TODO
  560. put_bits32(&pb, 0); // amount of comments
  561. put_bits(&pb, 1, 1); // framing
  562. flush_put_bits(&pb);
  563. hlens[1] = put_bits_count(&pb) >> 3;
  564. buffer_len -= hlens[1];
  565. p += hlens[1];
  566. // setup header
  567. init_put_bits(&pb, p, buffer_len);
  568. put_bits(&pb, 8, 5); //magic
  569. for (i = 0; "vorbis"[i]; i++)
  570. put_bits(&pb, 8, "vorbis"[i]);
  571. // codebooks
  572. put_bits(&pb, 8, venc->ncodebooks - 1);
  573. for (i = 0; i < venc->ncodebooks; i++)
  574. put_codebook_header(&pb, &venc->codebooks[i]);
  575. // time domain, reserved, zero
  576. put_bits(&pb, 6, 0);
  577. put_bits(&pb, 16, 0);
  578. // floors
  579. put_bits(&pb, 6, venc->nfloors - 1);
  580. for (i = 0; i < venc->nfloors; i++)
  581. put_floor_header(&pb, &venc->floors[i]);
  582. // residues
  583. put_bits(&pb, 6, venc->nresidues - 1);
  584. for (i = 0; i < venc->nresidues; i++)
  585. put_residue_header(&pb, &venc->residues[i]);
  586. // mappings
  587. put_bits(&pb, 6, venc->nmappings - 1);
  588. for (i = 0; i < venc->nmappings; i++) {
  589. vorbis_enc_mapping *mc = &venc->mappings[i];
  590. int j;
  591. put_bits(&pb, 16, 0); // mapping type
  592. put_bits(&pb, 1, mc->submaps > 1);
  593. if (mc->submaps > 1)
  594. put_bits(&pb, 4, mc->submaps - 1);
  595. put_bits(&pb, 1, !!mc->coupling_steps);
  596. if (mc->coupling_steps) {
  597. put_bits(&pb, 8, mc->coupling_steps - 1);
  598. for (j = 0; j < mc->coupling_steps; j++) {
  599. put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
  600. put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
  601. }
  602. }
  603. put_bits(&pb, 2, 0); // reserved
  604. if (mc->submaps > 1)
  605. for (j = 0; j < venc->channels; j++)
  606. put_bits(&pb, 4, mc->mux[j]);
  607. for (j = 0; j < mc->submaps; j++) {
  608. put_bits(&pb, 8, 0); // reserved time configuration
  609. put_bits(&pb, 8, mc->floor[j]);
  610. put_bits(&pb, 8, mc->residue[j]);
  611. }
  612. }
  613. // modes
  614. put_bits(&pb, 6, venc->nmodes - 1);
  615. for (i = 0; i < venc->nmodes; i++) {
  616. put_bits(&pb, 1, venc->modes[i].blockflag);
  617. put_bits(&pb, 16, 0); // reserved window type
  618. put_bits(&pb, 16, 0); // reserved transform type
  619. put_bits(&pb, 8, venc->modes[i].mapping);
  620. }
  621. put_bits(&pb, 1, 1); // framing
  622. flush_put_bits(&pb);
  623. hlens[2] = put_bits_count(&pb) >> 3;
  624. len = hlens[0] + hlens[1] + hlens[2];
  625. p = *out = av_mallocz(64 + len + len/255);
  626. if (!p)
  627. return AVERROR(ENOMEM);
  628. *p++ = 2;
  629. p += av_xiphlacing(p, hlens[0]);
  630. p += av_xiphlacing(p, hlens[1]);
  631. buffer_len = 0;
  632. for (i = 0; i < 3; i++) {
  633. memcpy(p, buffer + buffer_len, hlens[i]);
  634. p += hlens[i];
  635. buffer_len += hlens[i];
  636. }
  637. av_freep(&buffer);
  638. return p - *out;
  639. }
  640. static float get_floor_average(vorbis_enc_floor * fc, float *coeffs, int i)
  641. {
  642. int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
  643. int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
  644. int j;
  645. float average = 0;
  646. for (j = begin; j < end; j++)
  647. average += fabs(coeffs[j]);
  648. return average / (end - begin);
  649. }
  650. static void floor_fit(vorbis_enc_context *venc, vorbis_enc_floor *fc,
  651. float *coeffs, uint16_t *posts, int samples)
  652. {
  653. int range = 255 / fc->multiplier + 1;
  654. int i;
  655. float tot_average = 0.0;
  656. float averages[MAX_FLOOR_VALUES];
  657. for (i = 0; i < fc->values; i++) {
  658. averages[i] = get_floor_average(fc, coeffs, i);
  659. tot_average += averages[i];
  660. }
  661. tot_average /= fc->values;
  662. tot_average /= venc->quality;
  663. for (i = 0; i < fc->values; i++) {
  664. int position = fc->list[fc->list[i].sort].x;
  665. float average = averages[i];
  666. int j;
  667. average = sqrt(tot_average * average) * pow(1.25f, position*0.005f); // MAGIC!
  668. for (j = 0; j < range - 1; j++)
  669. if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average)
  670. break;
  671. posts[fc->list[i].sort] = j;
  672. }
  673. }
  674. static int render_point(int x0, int y0, int x1, int y1, int x)
  675. {
  676. return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
  677. }
  678. static int floor_encode(vorbis_enc_context *venc, vorbis_enc_floor *fc,
  679. PutBitContext *pb, uint16_t *posts,
  680. float *floor, int samples)
  681. {
  682. int range = 255 / fc->multiplier + 1;
  683. int coded[MAX_FLOOR_VALUES]; // first 2 values are unused
  684. int i, counter;
  685. if (pb->size_in_bits - put_bits_count(pb) < 1 + 2 * ilog(range - 1))
  686. return AVERROR(EINVAL);
  687. put_bits(pb, 1, 1); // non zero
  688. put_bits(pb, ilog(range - 1), posts[0]);
  689. put_bits(pb, ilog(range - 1), posts[1]);
  690. coded[0] = coded[1] = 1;
  691. for (i = 2; i < fc->values; i++) {
  692. int predicted = render_point(fc->list[fc->list[i].low].x,
  693. posts[fc->list[i].low],
  694. fc->list[fc->list[i].high].x,
  695. posts[fc->list[i].high],
  696. fc->list[i].x);
  697. int highroom = range - predicted;
  698. int lowroom = predicted;
  699. int room = FFMIN(highroom, lowroom);
  700. if (predicted == posts[i]) {
  701. coded[i] = 0; // must be used later as flag!
  702. continue;
  703. } else {
  704. if (!coded[fc->list[i].low ])
  705. coded[fc->list[i].low ] = -1;
  706. if (!coded[fc->list[i].high])
  707. coded[fc->list[i].high] = -1;
  708. }
  709. if (posts[i] > predicted) {
  710. if (posts[i] - predicted > room)
  711. coded[i] = posts[i] - predicted + lowroom;
  712. else
  713. coded[i] = (posts[i] - predicted) << 1;
  714. } else {
  715. if (predicted - posts[i] > room)
  716. coded[i] = predicted - posts[i] + highroom - 1;
  717. else
  718. coded[i] = ((predicted - posts[i]) << 1) - 1;
  719. }
  720. }
  721. counter = 2;
  722. for (i = 0; i < fc->partitions; i++) {
  723. vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
  724. int k, cval = 0, csub = 1<<c->subclass;
  725. if (c->subclass) {
  726. vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
  727. int cshift = 0;
  728. for (k = 0; k < c->dim; k++) {
  729. int l;
  730. for (l = 0; l < csub; l++) {
  731. int maxval = 1;
  732. if (c->books[l] != -1)
  733. maxval = venc->codebooks[c->books[l]].nentries;
  734. // coded could be -1, but this still works, cause that is 0
  735. if (coded[counter + k] < maxval)
  736. break;
  737. }
  738. assert(l != csub);
  739. cval |= l << cshift;
  740. cshift += c->subclass;
  741. }
  742. if (put_codeword(pb, book, cval))
  743. return AVERROR(EINVAL);
  744. }
  745. for (k = 0; k < c->dim; k++) {
  746. int book = c->books[cval & (csub-1)];
  747. int entry = coded[counter++];
  748. cval >>= c->subclass;
  749. if (book == -1)
  750. continue;
  751. if (entry == -1)
  752. entry = 0;
  753. if (put_codeword(pb, &venc->codebooks[book], entry))
  754. return AVERROR(EINVAL);
  755. }
  756. }
  757. ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded,
  758. fc->multiplier, floor, samples);
  759. return 0;
  760. }
  761. static float *put_vector(vorbis_enc_codebook *book, PutBitContext *pb,
  762. float *num)
  763. {
  764. int i, entry = -1;
  765. float distance = FLT_MAX;
  766. assert(book->dimensions);
  767. for (i = 0; i < book->nentries; i++) {
  768. float * vec = book->dimensions + i * book->ndimensions, d = book->pow2[i];
  769. int j;
  770. if (!book->lens[i])
  771. continue;
  772. for (j = 0; j < book->ndimensions; j++)
  773. d -= vec[j] * num[j];
  774. if (distance > d) {
  775. entry = i;
  776. distance = d;
  777. }
  778. }
  779. if (put_codeword(pb, book, entry))
  780. return NULL;
  781. return &book->dimensions[entry * book->ndimensions];
  782. }
  783. static int residue_encode(vorbis_enc_context *venc, vorbis_enc_residue *rc,
  784. PutBitContext *pb, float *coeffs, int samples,
  785. int real_ch)
  786. {
  787. int pass, i, j, p, k;
  788. int psize = rc->partition_size;
  789. int partitions = (rc->end - rc->begin) / psize;
  790. int channels = (rc->type == 2) ? 1 : real_ch;
  791. int classes[MAX_CHANNELS][NUM_RESIDUE_PARTITIONS];
  792. int classwords = venc->codebooks[rc->classbook].ndimensions;
  793. av_assert0(rc->type == 2);
  794. av_assert0(real_ch == 2);
  795. for (p = 0; p < partitions; p++) {
  796. float max1 = 0.0, max2 = 0.0;
  797. int s = rc->begin + p * psize;
  798. for (k = s; k < s + psize; k += 2) {
  799. max1 = FFMAX(max1, fabs(coeffs[ k / real_ch]));
  800. max2 = FFMAX(max2, fabs(coeffs[samples + k / real_ch]));
  801. }
  802. for (i = 0; i < rc->classifications - 1; i++)
  803. if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1])
  804. break;
  805. classes[0][p] = i;
  806. }
  807. for (pass = 0; pass < 8; pass++) {
  808. p = 0;
  809. while (p < partitions) {
  810. if (pass == 0)
  811. for (j = 0; j < channels; j++) {
  812. vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
  813. int entry = 0;
  814. for (i = 0; i < classwords; i++) {
  815. entry *= rc->classifications;
  816. entry += classes[j][p + i];
  817. }
  818. if (put_codeword(pb, book, entry))
  819. return AVERROR(EINVAL);
  820. }
  821. for (i = 0; i < classwords && p < partitions; i++, p++) {
  822. for (j = 0; j < channels; j++) {
  823. int nbook = rc->books[classes[j][p]][pass];
  824. vorbis_enc_codebook * book = &venc->codebooks[nbook];
  825. float *buf = coeffs + samples*j + rc->begin + p*psize;
  826. if (nbook == -1)
  827. continue;
  828. assert(rc->type == 0 || rc->type == 2);
  829. assert(!(psize % book->ndimensions));
  830. if (rc->type == 0) {
  831. for (k = 0; k < psize; k += book->ndimensions) {
  832. int l;
  833. float *a = put_vector(book, pb, &buf[k]);
  834. if (!a)
  835. return AVERROR(EINVAL);
  836. for (l = 0; l < book->ndimensions; l++)
  837. buf[k + l] -= a[l];
  838. }
  839. } else {
  840. int s = rc->begin + p * psize, a1, b1;
  841. a1 = (s % real_ch) * samples;
  842. b1 = s / real_ch;
  843. s = real_ch * samples;
  844. for (k = 0; k < psize; k += book->ndimensions) {
  845. int dim, a2 = a1, b2 = b1;
  846. float vec[MAX_CODEBOOK_DIM], *pv = vec;
  847. for (dim = book->ndimensions; dim--; ) {
  848. *pv++ = coeffs[a2 + b2];
  849. if ((a2 += samples) == s) {
  850. a2 = 0;
  851. b2++;
  852. }
  853. }
  854. pv = put_vector(book, pb, vec);
  855. if (!pv)
  856. return AVERROR(EINVAL);
  857. for (dim = book->ndimensions; dim--; ) {
  858. coeffs[a1 + b1] -= *pv++;
  859. if ((a1 += samples) == s) {
  860. a1 = 0;
  861. b1++;
  862. }
  863. }
  864. }
  865. }
  866. }
  867. }
  868. }
  869. }
  870. return 0;
  871. }
  872. static int apply_window_and_mdct(vorbis_enc_context *venc)
  873. {
  874. int channel;
  875. const float * win = venc->win[1];
  876. int window_len = 1 << (venc->log2_blocksize[1] - 1);
  877. float n = (float)(1 << venc->log2_blocksize[1]) / 4.0;
  878. AVFloatDSPContext *fdsp = venc->fdsp;
  879. for (channel = 0; channel < venc->channels; channel++) {
  880. float *offset = venc->samples + channel * window_len * 2;
  881. fdsp->vector_fmul(offset, offset, win, window_len);
  882. fdsp->vector_fmul_scalar(offset, offset, 1/n, window_len);
  883. offset += window_len;
  884. fdsp->vector_fmul_reverse(offset, offset, win, window_len);
  885. fdsp->vector_fmul_scalar(offset, offset, 1/n, window_len);
  886. venc->mdct[1].mdct_calc(&venc->mdct[1], venc->coeffs + channel * window_len,
  887. venc->samples + channel * window_len * 2);
  888. }
  889. return 1;
  890. }
  891. /* Used for padding the last encoded packet */
  892. static AVFrame *spawn_empty_frame(AVCodecContext *avctx, int channels)
  893. {
  894. AVFrame *f = av_frame_alloc();
  895. int ch;
  896. if (!f)
  897. return NULL;
  898. f->format = avctx->sample_fmt;
  899. f->nb_samples = avctx->frame_size;
  900. f->channel_layout = avctx->channel_layout;
  901. if (av_frame_get_buffer(f, 4)) {
  902. av_frame_free(&f);
  903. return NULL;
  904. }
  905. for (ch = 0; ch < channels; ch++) {
  906. size_t bps = av_get_bytes_per_sample(f->format);
  907. memset(f->extended_data[ch], 0, bps * f->nb_samples);
  908. }
  909. return f;
  910. }
  911. /* Set up audio samples for psy analysis and window/mdct */
  912. static void move_audio(vorbis_enc_context *venc, int sf_size)
  913. {
  914. AVFrame *cur = NULL;
  915. int frame_size = 1 << (venc->log2_blocksize[1] - 1);
  916. int subframes = frame_size / sf_size;
  917. int sf, ch;
  918. /* Copy samples from last frame into current frame */
  919. if (venc->have_saved)
  920. for (ch = 0; ch < venc->channels; ch++)
  921. memcpy(venc->samples + 2 * ch * frame_size,
  922. venc->saved + ch * frame_size, sizeof(float) * frame_size);
  923. else
  924. for (ch = 0; ch < venc->channels; ch++)
  925. memset(venc->samples + 2 * ch * frame_size, 0, sizeof(float) * frame_size);
  926. for (sf = 0; sf < subframes; sf++) {
  927. cur = ff_bufqueue_get(&venc->bufqueue);
  928. for (ch = 0; ch < venc->channels; ch++) {
  929. float *offset = venc->samples + 2 * ch * frame_size + frame_size;
  930. float *save = venc->saved + ch * frame_size;
  931. const float *input = (float *) cur->extended_data[ch];
  932. const size_t len = cur->nb_samples * sizeof(float);
  933. memcpy(offset + sf*sf_size, input, len);
  934. memcpy(save + sf*sf_size, input, len); // Move samples for next frame
  935. }
  936. av_frame_free(&cur);
  937. }
  938. venc->have_saved = 1;
  939. memcpy(venc->scratch, venc->samples, 2 * venc->channels * frame_size);
  940. }
  941. static int vorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  942. const AVFrame *frame, int *got_packet_ptr)
  943. {
  944. vorbis_enc_context *venc = avctx->priv_data;
  945. int i, ret, need_more;
  946. int frame_size = 1 << (venc->log2_blocksize[1] - 1);
  947. vorbis_enc_mode *mode;
  948. vorbis_enc_mapping *mapping;
  949. PutBitContext pb;
  950. if (frame) {
  951. AVFrame *clone;
  952. if ((ret = ff_af_queue_add(&venc->afq, frame)) < 0)
  953. return ret;
  954. clone = av_frame_clone(frame);
  955. if (!clone)
  956. return AVERROR(ENOMEM);
  957. ff_bufqueue_add(avctx, &venc->bufqueue, clone);
  958. } else
  959. if (!venc->afq.remaining_samples)
  960. return 0;
  961. need_more = venc->bufqueue.available * avctx->frame_size < frame_size;
  962. need_more = frame && need_more;
  963. if (need_more)
  964. return 0;
  965. /* Pad the bufqueue with empty frames for encoding the last packet. */
  966. if (!frame) {
  967. if (venc->bufqueue.available * avctx->frame_size < frame_size) {
  968. int frames_needed = (frame_size/avctx->frame_size) - venc->bufqueue.available;
  969. int i;
  970. for (i = 0; i < frames_needed; i++) {
  971. AVFrame *empty = spawn_empty_frame(avctx, venc->channels);
  972. if (!empty)
  973. return AVERROR(ENOMEM);
  974. ff_bufqueue_add(avctx, &venc->bufqueue, empty);
  975. }
  976. }
  977. }
  978. move_audio(venc, avctx->frame_size);
  979. if (!apply_window_and_mdct(venc))
  980. return 0;
  981. if ((ret = ff_alloc_packet2(avctx, avpkt, 8192, 0)) < 0)
  982. return ret;
  983. init_put_bits(&pb, avpkt->data, avpkt->size);
  984. put_bits(&pb, 1, 0); // magic bit
  985. put_bits(&pb, ilog(venc->nmodes - 1), 1); // Mode for current frame
  986. mode = &venc->modes[1];
  987. mapping = &venc->mappings[mode->mapping];
  988. if (mode->blockflag) {
  989. put_bits(&pb, 1, 1); // Previous windowflag
  990. put_bits(&pb, 1, 1); // Next windowflag
  991. }
  992. for (i = 0; i < venc->channels; i++) {
  993. vorbis_enc_floor *fc = &venc->floors[mapping->floor[mapping->mux[i]]];
  994. uint16_t posts[MAX_FLOOR_VALUES];
  995. floor_fit(venc, fc, &venc->coeffs[i * frame_size], posts, frame_size);
  996. if (floor_encode(venc, fc, &pb, posts, &venc->floor[i * frame_size], frame_size)) {
  997. av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
  998. return AVERROR(EINVAL);
  999. }
  1000. }
  1001. for (i = 0; i < venc->channels * frame_size; i++)
  1002. venc->coeffs[i] /= venc->floor[i];
  1003. for (i = 0; i < mapping->coupling_steps; i++) {
  1004. float *mag = venc->coeffs + mapping->magnitude[i] * frame_size;
  1005. float *ang = venc->coeffs + mapping->angle[i] * frame_size;
  1006. int j;
  1007. for (j = 0; j < frame_size; j++) {
  1008. float a = ang[j];
  1009. ang[j] -= mag[j];
  1010. if (mag[j] > 0)
  1011. ang[j] = -ang[j];
  1012. if (ang[j] < 0)
  1013. mag[j] = a;
  1014. }
  1015. }
  1016. if (residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]],
  1017. &pb, venc->coeffs, frame_size, venc->channels)) {
  1018. av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
  1019. return AVERROR(EINVAL);
  1020. }
  1021. flush_put_bits(&pb);
  1022. avpkt->size = put_bits_count(&pb) >> 3;
  1023. ff_af_queue_remove(&venc->afq, frame_size, &avpkt->pts, &avpkt->duration);
  1024. if (frame_size > avpkt->duration) {
  1025. uint8_t *side = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
  1026. if (!side)
  1027. return AVERROR(ENOMEM);
  1028. AV_WL32(&side[4], frame_size - avpkt->duration);
  1029. }
  1030. *got_packet_ptr = 1;
  1031. return 0;
  1032. }
  1033. static av_cold int vorbis_encode_close(AVCodecContext *avctx)
  1034. {
  1035. vorbis_enc_context *venc = avctx->priv_data;
  1036. int i;
  1037. if (venc->codebooks)
  1038. for (i = 0; i < venc->ncodebooks; i++) {
  1039. av_freep(&venc->codebooks[i].lens);
  1040. av_freep(&venc->codebooks[i].codewords);
  1041. av_freep(&venc->codebooks[i].quantlist);
  1042. av_freep(&venc->codebooks[i].dimensions);
  1043. av_freep(&venc->codebooks[i].pow2);
  1044. }
  1045. av_freep(&venc->codebooks);
  1046. if (venc->floors)
  1047. for (i = 0; i < venc->nfloors; i++) {
  1048. int j;
  1049. if (venc->floors[i].classes)
  1050. for (j = 0; j < venc->floors[i].nclasses; j++)
  1051. av_freep(&venc->floors[i].classes[j].books);
  1052. av_freep(&venc->floors[i].classes);
  1053. av_freep(&venc->floors[i].partition_to_class);
  1054. av_freep(&venc->floors[i].list);
  1055. }
  1056. av_freep(&venc->floors);
  1057. if (venc->residues)
  1058. for (i = 0; i < venc->nresidues; i++) {
  1059. av_freep(&venc->residues[i].books);
  1060. av_freep(&venc->residues[i].maxes);
  1061. }
  1062. av_freep(&venc->residues);
  1063. if (venc->mappings)
  1064. for (i = 0; i < venc->nmappings; i++) {
  1065. av_freep(&venc->mappings[i].mux);
  1066. av_freep(&venc->mappings[i].floor);
  1067. av_freep(&venc->mappings[i].residue);
  1068. av_freep(&venc->mappings[i].magnitude);
  1069. av_freep(&venc->mappings[i].angle);
  1070. }
  1071. av_freep(&venc->mappings);
  1072. av_freep(&venc->modes);
  1073. av_freep(&venc->saved);
  1074. av_freep(&venc->samples);
  1075. av_freep(&venc->floor);
  1076. av_freep(&venc->coeffs);
  1077. av_freep(&venc->scratch);
  1078. av_freep(&venc->fdsp);
  1079. ff_mdct_end(&venc->mdct[0]);
  1080. ff_mdct_end(&venc->mdct[1]);
  1081. ff_af_queue_close(&venc->afq);
  1082. ff_bufqueue_discard_all(&venc->bufqueue);
  1083. av_freep(&avctx->extradata);
  1084. return 0 ;
  1085. }
  1086. static av_cold int vorbis_encode_init(AVCodecContext *avctx)
  1087. {
  1088. vorbis_enc_context *venc = avctx->priv_data;
  1089. int ret;
  1090. if (avctx->channels != 2) {
  1091. av_log(avctx, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
  1092. return -1;
  1093. }
  1094. if ((ret = create_vorbis_context(venc, avctx)) < 0)
  1095. goto error;
  1096. avctx->bit_rate = 0;
  1097. if (avctx->flags & AV_CODEC_FLAG_QSCALE)
  1098. venc->quality = avctx->global_quality / (float)FF_QP2LAMBDA;
  1099. else
  1100. venc->quality = 8;
  1101. venc->quality *= venc->quality;
  1102. if ((ret = put_main_header(venc, (uint8_t**)&avctx->extradata)) < 0)
  1103. goto error;
  1104. avctx->extradata_size = ret;
  1105. avctx->frame_size = 64;
  1106. ff_af_queue_init(avctx, &venc->afq);
  1107. return 0;
  1108. error:
  1109. vorbis_encode_close(avctx);
  1110. return ret;
  1111. }
  1112. AVCodec ff_vorbis_encoder = {
  1113. .name = "vorbis",
  1114. .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
  1115. .type = AVMEDIA_TYPE_AUDIO,
  1116. .id = AV_CODEC_ID_VORBIS,
  1117. .priv_data_size = sizeof(vorbis_enc_context),
  1118. .init = vorbis_encode_init,
  1119. .encode2 = vorbis_encode_frame,
  1120. .close = vorbis_encode_close,
  1121. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
  1122. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  1123. AV_SAMPLE_FMT_NONE },
  1124. };