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.

1131 lines
41KB

  1. /*
  2. * Opus encoder
  3. * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "opus_celt.h"
  22. #include "opus_pvq.h"
  23. #include "opustab.h"
  24. #include "libavutil/float_dsp.h"
  25. #include "libavutil/opt.h"
  26. #include "internal.h"
  27. #include "bytestream.h"
  28. #include "audio_frame_queue.h"
  29. /* Determines the maximum delay the psychoacoustic system will use for lookahead */
  30. #define FF_BUFQUEUE_SIZE 145
  31. #include "libavfilter/bufferqueue.h"
  32. #define OPUS_MAX_LOOKAHEAD ((FF_BUFQUEUE_SIZE - 1)*2.5f)
  33. #define OPUS_MAX_CHANNELS 2
  34. /* 120 ms / 2.5 ms = 48 frames (extremely improbable, but the encoder'll work) */
  35. #define OPUS_MAX_FRAMES_PER_PACKET 48
  36. #define OPUS_BLOCK_SIZE(x) (2 * 15 * (1 << ((x) + 2)))
  37. #define OPUS_SAMPLES_TO_BLOCK_SIZE(x) (ff_log2((x) / (2 * 15)) - 2)
  38. typedef struct OpusEncOptions {
  39. float max_delay_ms;
  40. } OpusEncOptions;
  41. typedef struct OpusEncContext {
  42. AVClass *av_class;
  43. OpusEncOptions options;
  44. AVCodecContext *avctx;
  45. AudioFrameQueue afq;
  46. AVFloatDSPContext *dsp;
  47. MDCT15Context *mdct[CELT_BLOCK_NB];
  48. struct FFBufQueue bufqueue;
  49. enum OpusMode mode;
  50. enum OpusBandwidth bandwidth;
  51. int pkt_framesize;
  52. int pkt_frames;
  53. int channels;
  54. CeltFrame *frame;
  55. OpusRangeCoder *rc;
  56. /* Actual energy the decoder will have */
  57. float last_quantized_energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS];
  58. DECLARE_ALIGNED(32, float, scratch)[2048];
  59. } OpusEncContext;
  60. static void opus_write_extradata(AVCodecContext *avctx)
  61. {
  62. uint8_t *bs = avctx->extradata;
  63. bytestream_put_buffer(&bs, "OpusHead", 8);
  64. bytestream_put_byte (&bs, 0x1);
  65. bytestream_put_byte (&bs, avctx->channels);
  66. bytestream_put_le16 (&bs, avctx->initial_padding);
  67. bytestream_put_le32 (&bs, avctx->sample_rate);
  68. bytestream_put_le16 (&bs, 0x0);
  69. bytestream_put_byte (&bs, 0x0); /* Default layout */
  70. }
  71. static int opus_gen_toc(OpusEncContext *s, uint8_t *toc, int *size, int *fsize_needed)
  72. {
  73. int i, tmp = 0x0, extended_toc = 0;
  74. static const int toc_cfg[][OPUS_MODE_NB][OPUS_BANDWITH_NB] = {
  75. /* Silk Hybrid Celt Layer */
  76. /* NB MB WB SWB FB NB MB WB SWB FB NB MB WB SWB FB Bandwidth */
  77. { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 17, 0, 21, 25, 29 } }, /* 2.5 ms */
  78. { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 18, 0, 22, 26, 30 } }, /* 5 ms */
  79. { { 1, 5, 9, 0, 0 }, { 0, 0, 0, 13, 15 }, { 19, 0, 23, 27, 31 } }, /* 10 ms */
  80. { { 2, 6, 10, 0, 0 }, { 0, 0, 0, 14, 16 }, { 20, 0, 24, 28, 32 } }, /* 20 ms */
  81. { { 3, 7, 11, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }, /* 40 ms */
  82. { { 4, 8, 12, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }, /* 60 ms */
  83. };
  84. int cfg = toc_cfg[s->pkt_framesize][s->mode][s->bandwidth];
  85. *fsize_needed = 0;
  86. if (!cfg)
  87. return 1;
  88. if (s->pkt_frames == 2) { /* 2 packets */
  89. if (s->frame[0].framebits == s->frame[1].framebits) { /* same size */
  90. tmp = 0x1;
  91. } else { /* different size */
  92. tmp = 0x2;
  93. *fsize_needed = 1; /* put frame sizes in the packet */
  94. }
  95. } else if (s->pkt_frames > 2) {
  96. tmp = 0x3;
  97. extended_toc = 1;
  98. }
  99. tmp |= (s->channels > 1) << 2; /* Stereo or mono */
  100. tmp |= (cfg - 1) << 3; /* codec configuration */
  101. *toc++ = tmp;
  102. if (extended_toc) {
  103. for (i = 0; i < (s->pkt_frames - 1); i++)
  104. *fsize_needed |= (s->frame[i].framebits != s->frame[i + 1].framebits);
  105. tmp = (*fsize_needed) << 7; /* vbr flag */
  106. tmp |= s->pkt_frames; /* frame number - can be 0 as well */
  107. *toc++ = tmp;
  108. }
  109. *size = 1 + extended_toc;
  110. return 0;
  111. }
  112. static void celt_frame_setup_input(OpusEncContext *s, CeltFrame *f)
  113. {
  114. int sf, ch;
  115. AVFrame *cur = NULL;
  116. const int subframesize = s->avctx->frame_size;
  117. int subframes = OPUS_BLOCK_SIZE(s->pkt_framesize) / subframesize;
  118. cur = ff_bufqueue_get(&s->bufqueue);
  119. for (ch = 0; ch < f->channels; ch++) {
  120. CeltBlock *b = &f->block[ch];
  121. const void *input = cur->extended_data[ch];
  122. size_t bps = av_get_bytes_per_sample(cur->format);
  123. memcpy(b->overlap, input, bps*cur->nb_samples);
  124. }
  125. av_frame_free(&cur);
  126. for (sf = 0; sf < subframes; sf++) {
  127. if (sf != (subframes - 1))
  128. cur = ff_bufqueue_get(&s->bufqueue);
  129. else
  130. cur = ff_bufqueue_peek(&s->bufqueue, 0);
  131. for (ch = 0; ch < f->channels; ch++) {
  132. CeltBlock *b = &f->block[ch];
  133. const void *input = cur->extended_data[ch];
  134. const size_t bps = av_get_bytes_per_sample(cur->format);
  135. const size_t left = (subframesize - cur->nb_samples)*bps;
  136. const size_t len = FFMIN(subframesize, cur->nb_samples)*bps;
  137. memcpy(&b->samples[sf*subframesize], input, len);
  138. memset(&b->samples[cur->nb_samples], 0, left);
  139. }
  140. /* Last frame isn't popped off and freed yet - we need it for overlap */
  141. if (sf != (subframes - 1))
  142. av_frame_free(&cur);
  143. }
  144. }
  145. /* Apply the pre emphasis filter */
  146. static void celt_apply_preemph_filter(OpusEncContext *s, CeltFrame *f)
  147. {
  148. int i, sf, ch;
  149. const int subframesize = s->avctx->frame_size;
  150. const int subframes = OPUS_BLOCK_SIZE(s->pkt_framesize) / subframesize;
  151. /* Filter overlap */
  152. for (ch = 0; ch < f->channels; ch++) {
  153. CeltBlock *b = &f->block[ch];
  154. float m = b->emph_coeff;
  155. for (i = 0; i < CELT_OVERLAP; i++) {
  156. float sample = b->overlap[i];
  157. b->overlap[i] = sample - m;
  158. m = sample * CELT_EMPH_COEFF;
  159. }
  160. b->emph_coeff = m;
  161. }
  162. /* Filter the samples but do not update the last subframe's coeff - overlap ^^^ */
  163. for (sf = 0; sf < subframes; sf++) {
  164. for (ch = 0; ch < f->channels; ch++) {
  165. CeltBlock *b = &f->block[ch];
  166. float m = b->emph_coeff;
  167. for (i = 0; i < subframesize; i++) {
  168. float sample = b->samples[sf*subframesize + i];
  169. b->samples[sf*subframesize + i] = sample - m;
  170. m = sample * CELT_EMPH_COEFF;
  171. }
  172. if (sf != (subframes - 1))
  173. b->emph_coeff = m;
  174. }
  175. }
  176. }
  177. /* Create the window and do the mdct */
  178. static void celt_frame_mdct(OpusEncContext *s, CeltFrame *f)
  179. {
  180. int i, t, ch;
  181. float *win = s->scratch;
  182. /* I think I can use s->dsp->vector_fmul_window for transients at least */
  183. if (f->transient) {
  184. for (ch = 0; ch < f->channels; ch++) {
  185. CeltBlock *b = &f->block[ch];
  186. float *src1 = b->overlap;
  187. for (t = 0; t < f->blocks; t++) {
  188. float *src2 = &b->samples[CELT_OVERLAP*t];
  189. for (i = 0; i < CELT_OVERLAP; i++) {
  190. win[ i] = src1[i]*ff_celt_window[i];
  191. win[CELT_OVERLAP + i] = src2[i]*ff_celt_window[CELT_OVERLAP - i - 1];
  192. }
  193. src1 = src2;
  194. s->mdct[0]->mdct(s->mdct[0], b->coeffs + t, win, f->blocks);
  195. }
  196. }
  197. } else {
  198. int blk_len = OPUS_BLOCK_SIZE(f->size), wlen = OPUS_BLOCK_SIZE(f->size + 1);
  199. int rwin = blk_len - CELT_OVERLAP, lap_dst = (wlen - blk_len - CELT_OVERLAP) >> 1;
  200. for (ch = 0; ch < f->channels; ch++) {
  201. CeltBlock *b = &f->block[ch];
  202. memset(win, 0, wlen*sizeof(float));
  203. memcpy(&win[lap_dst + CELT_OVERLAP], b->samples, rwin*sizeof(float));
  204. /* Alignment fucks me over */
  205. //s->dsp->vector_fmul(&dst[lap_dst], b->overlap, ff_celt_window, CELT_OVERLAP);
  206. //s->dsp->vector_fmul_reverse(&dst[lap_dst + blk_len - CELT_OVERLAP], b->samples, ff_celt_window, CELT_OVERLAP);
  207. for (i = 0; i < CELT_OVERLAP; i++) {
  208. win[lap_dst + i] = b->overlap[i] *ff_celt_window[i];
  209. win[lap_dst + blk_len + i] = b->samples[rwin + i]*ff_celt_window[CELT_OVERLAP - i - 1];
  210. }
  211. s->mdct[f->size]->mdct(s->mdct[f->size], b->coeffs, win, 1);
  212. }
  213. }
  214. }
  215. /* Fills the bands and normalizes them */
  216. static int celt_frame_map_norm_bands(OpusEncContext *s, CeltFrame *f)
  217. {
  218. int i, j, ch, noise = 0;
  219. for (ch = 0; ch < f->channels; ch++) {
  220. CeltBlock *block = &f->block[ch];
  221. float *start = block->coeffs;
  222. for (i = 0; i < CELT_MAX_BANDS; i++) {
  223. float ener = 0.0f;
  224. /* Calculate band bins */
  225. block->band_bins[i] = ff_celt_freq_range[i] << f->size;
  226. block->band_coeffs[i] = start;
  227. start += block->band_bins[i];
  228. /* Normalize band energy */
  229. for (j = 0; j < block->band_bins[i]; j++)
  230. ener += block->band_coeffs[i][j]*block->band_coeffs[i][j];
  231. block->lin_energy[i] = sqrtf(ener) + FLT_EPSILON;
  232. ener = 1.0f/block->lin_energy[i];
  233. for (j = 0; j < block->band_bins[i]; j++)
  234. block->band_coeffs[i][j] *= ener;
  235. block->energy[i] = log2f(block->lin_energy[i]) - ff_celt_mean_energy[i];
  236. /* CELT_ENERGY_SILENCE is what the decoder uses and its not -infinity */
  237. block->energy[i] = FFMAX(block->energy[i], CELT_ENERGY_SILENCE);
  238. noise |= block->energy[i] > CELT_ENERGY_SILENCE;
  239. }
  240. }
  241. return !noise;
  242. }
  243. static void celt_enc_tf(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f)
  244. {
  245. int i, tf_select = 0, diff = 0, tf_changed = 0, tf_select_needed;
  246. int bits = f->transient ? 2 : 4;
  247. tf_select_needed = ((f->size && (opus_rc_tell(rc) + bits + 1) <= f->framebits));
  248. for (i = f->start_band; i < f->end_band; i++) {
  249. if ((opus_rc_tell(rc) + bits + tf_select_needed) <= f->framebits) {
  250. const int tbit = (diff ^ 1) == f->tf_change[i];
  251. ff_opus_rc_enc_log(rc, tbit, bits);
  252. diff ^= tbit;
  253. tf_changed |= diff;
  254. }
  255. bits = f->transient ? 4 : 5;
  256. }
  257. if (tf_select_needed && ff_celt_tf_select[f->size][f->transient][0][tf_changed] !=
  258. ff_celt_tf_select[f->size][f->transient][1][tf_changed]) {
  259. ff_opus_rc_enc_log(rc, f->tf_select, 1);
  260. tf_select = f->tf_select;
  261. }
  262. for (i = f->start_band; i < f->end_band; i++)
  263. f->tf_change[i] = ff_celt_tf_select[f->size][f->transient][tf_select][f->tf_change[i]];
  264. }
  265. static void celt_bitalloc(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f)
  266. {
  267. int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
  268. int skip_startband = f->start_band;
  269. int skip_bit = 0;
  270. int intensitystereo_bit = 0;
  271. int dualstereo_bit = 0;
  272. int dynalloc = 6;
  273. int extrabits = 0;
  274. int *cap = f->caps;
  275. int boost[CELT_MAX_BANDS];
  276. int trim_offset[CELT_MAX_BANDS];
  277. int threshold[CELT_MAX_BANDS];
  278. int bits1[CELT_MAX_BANDS];
  279. int bits2[CELT_MAX_BANDS];
  280. /* Tell the spread to the decoder */
  281. if (opus_rc_tell(rc) + 4 <= f->framebits)
  282. ff_opus_rc_enc_cdf(rc, f->spread, ff_celt_model_spread);
  283. /* Generate static allocation caps */
  284. for (i = 0; i < CELT_MAX_BANDS; i++) {
  285. cap[i] = (ff_celt_static_caps[f->size][f->channels - 1][i] + 64)
  286. * ff_celt_freq_range[i] << (f->channels - 1) << f->size >> 2;
  287. }
  288. /* Band boosts */
  289. tbits_8ths = f->framebits << 3;
  290. for (i = f->start_band; i < f->end_band; i++) {
  291. int quanta, b_dynalloc, boost_amount = f->alloc_boost[i];
  292. boost[i] = 0;
  293. quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
  294. quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
  295. b_dynalloc = dynalloc;
  296. while (opus_rc_tell_frac(rc) + (b_dynalloc << 3) < tbits_8ths && boost[i] < cap[i]) {
  297. int is_boost = boost_amount--;
  298. ff_opus_rc_enc_log(rc, is_boost, b_dynalloc);
  299. if (!is_boost)
  300. break;
  301. boost[i] += quanta;
  302. tbits_8ths -= quanta;
  303. b_dynalloc = 1;
  304. }
  305. if (boost[i])
  306. dynalloc = FFMAX(2, dynalloc - 1);
  307. }
  308. /* Put allocation trim */
  309. if (opus_rc_tell_frac(rc) + (6 << 3) <= tbits_8ths)
  310. ff_opus_rc_enc_cdf(rc, f->alloc_trim, ff_celt_model_alloc_trim);
  311. /* Anti-collapse bit reservation */
  312. tbits_8ths = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
  313. f->anticollapse_needed = 0;
  314. if (f->transient && f->size >= 2 && tbits_8ths >= ((f->size + 2) << 3))
  315. f->anticollapse_needed = 1 << 3;
  316. tbits_8ths -= f->anticollapse_needed;
  317. /* Band skip bit reservation */
  318. if (tbits_8ths >= 1 << 3)
  319. skip_bit = 1 << 3;
  320. tbits_8ths -= skip_bit;
  321. /* Intensity/dual stereo bit reservation */
  322. if (f->channels == 2) {
  323. intensitystereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
  324. if (intensitystereo_bit <= tbits_8ths) {
  325. tbits_8ths -= intensitystereo_bit;
  326. if (tbits_8ths >= 1 << 3) {
  327. dualstereo_bit = 1 << 3;
  328. tbits_8ths -= 1 << 3;
  329. }
  330. } else {
  331. intensitystereo_bit = 0;
  332. }
  333. }
  334. /* Trim offsets */
  335. for (i = f->start_band; i < f->end_band; i++) {
  336. int trim = f->alloc_trim - 5 - f->size;
  337. int band = ff_celt_freq_range[i] * (f->end_band - i - 1);
  338. int duration = f->size + 3;
  339. int scale = duration + f->channels - 1;
  340. /* PVQ minimum allocation threshold, below this value the band is
  341. * skipped */
  342. threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
  343. f->channels << 3);
  344. trim_offset[i] = trim * (band << scale) >> 6;
  345. if (ff_celt_freq_range[i] << f->size == 1)
  346. trim_offset[i] -= f->channels << 3;
  347. }
  348. /* Bisection */
  349. low = 1;
  350. high = CELT_VECTORS - 1;
  351. while (low <= high) {
  352. int center = (low + high) >> 1;
  353. done = total = 0;
  354. for (i = f->end_band - 1; i >= f->start_band; i--) {
  355. bandbits = ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]
  356. << (f->channels - 1) << f->size >> 2;
  357. if (bandbits)
  358. bandbits = FFMAX(0, bandbits + trim_offset[i]);
  359. bandbits += boost[i];
  360. if (bandbits >= threshold[i] || done) {
  361. done = 1;
  362. total += FFMIN(bandbits, cap[i]);
  363. } else if (bandbits >= f->channels << 3)
  364. total += f->channels << 3;
  365. }
  366. if (total > tbits_8ths)
  367. high = center - 1;
  368. else
  369. low = center + 1;
  370. }
  371. high = low--;
  372. /* Bisection */
  373. for (i = f->start_band; i < f->end_band; i++) {
  374. bits1[i] = ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]
  375. << (f->channels - 1) << f->size >> 2;
  376. bits2[i] = high >= CELT_VECTORS ? cap[i] :
  377. ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]
  378. << (f->channels - 1) << f->size >> 2;
  379. if (bits1[i])
  380. bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]);
  381. if (bits2[i])
  382. bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]);
  383. if (low)
  384. bits1[i] += boost[i];
  385. bits2[i] += boost[i];
  386. if (boost[i])
  387. skip_startband = i;
  388. bits2[i] = FFMAX(0, bits2[i] - bits1[i]);
  389. }
  390. /* Bisection */
  391. low = 0;
  392. high = 1 << CELT_ALLOC_STEPS;
  393. for (i = 0; i < CELT_ALLOC_STEPS; i++) {
  394. int center = (low + high) >> 1;
  395. done = total = 0;
  396. for (j = f->end_band - 1; j >= f->start_band; j--) {
  397. bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
  398. if (bandbits >= threshold[j] || done) {
  399. done = 1;
  400. total += FFMIN(bandbits, cap[j]);
  401. } else if (bandbits >= f->channels << 3)
  402. total += f->channels << 3;
  403. }
  404. if (total > tbits_8ths)
  405. high = center;
  406. else
  407. low = center;
  408. }
  409. /* Bisection */
  410. done = total = 0;
  411. for (i = f->end_band - 1; i >= f->start_band; i--) {
  412. bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
  413. if (bandbits >= threshold[i] || done)
  414. done = 1;
  415. else
  416. bandbits = (bandbits >= f->channels << 3) ?
  417. f->channels << 3 : 0;
  418. bandbits = FFMIN(bandbits, cap[i]);
  419. f->pulses[i] = bandbits;
  420. total += bandbits;
  421. }
  422. /* Band skipping */
  423. for (f->coded_bands = f->end_band; ; f->coded_bands--) {
  424. int allocation;
  425. j = f->coded_bands - 1;
  426. if (j == skip_startband) {
  427. /* all remaining bands are not skipped */
  428. tbits_8ths += skip_bit;
  429. break;
  430. }
  431. /* determine the number of bits available for coding "do not skip" markers */
  432. remaining = tbits_8ths - total;
  433. bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
  434. remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
  435. allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j]
  436. + FFMAX(0, remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]));
  437. /* a "do not skip" marker is only coded if the allocation is
  438. above the chosen threshold */
  439. if (allocation >= FFMAX(threshold[j], (f->channels + 1) << 3)) {
  440. const int do_not_skip = f->coded_bands <= f->skip_band_floor;
  441. ff_opus_rc_enc_log(rc, do_not_skip, 1);
  442. if (do_not_skip)
  443. break;
  444. total += 1 << 3;
  445. allocation -= 1 << 3;
  446. }
  447. /* the band is skipped, so reclaim its bits */
  448. total -= f->pulses[j];
  449. if (intensitystereo_bit) {
  450. total -= intensitystereo_bit;
  451. intensitystereo_bit = ff_celt_log2_frac[j - f->start_band];
  452. total += intensitystereo_bit;
  453. }
  454. total += f->pulses[j] = (allocation >= f->channels << 3) ? f->channels << 3 : 0;
  455. }
  456. /* Encode stereo flags */
  457. if (intensitystereo_bit) {
  458. f->intensity_stereo = FFMIN(f->intensity_stereo, f->coded_bands);
  459. ff_opus_rc_enc_uint(rc, f->intensity_stereo, f->coded_bands + 1 - f->start_band);
  460. }
  461. if (f->intensity_stereo <= f->start_band)
  462. tbits_8ths += dualstereo_bit; /* no intensity stereo means no dual stereo */
  463. else if (dualstereo_bit)
  464. ff_opus_rc_enc_log(rc, f->dual_stereo, 1);
  465. /* Supply the remaining bits in this frame to lower bands */
  466. remaining = tbits_8ths - total;
  467. bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
  468. remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
  469. for (i = f->start_band; i < f->coded_bands; i++) {
  470. int bits = FFMIN(remaining, ff_celt_freq_range[i]);
  471. f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
  472. remaining -= bits;
  473. }
  474. /* Finally determine the allocation */
  475. for (i = f->start_band; i < f->coded_bands; i++) {
  476. int N = ff_celt_freq_range[i] << f->size;
  477. int prev_extra = extrabits;
  478. f->pulses[i] += extrabits;
  479. if (N > 1) {
  480. int dof; // degrees of freedom
  481. int temp; // dof * channels * log(dof)
  482. int offset; // fine energy quantization offset, i.e.
  483. // extra bits assigned over the standard
  484. // totalbits/dof
  485. int fine_bits, max_bits;
  486. extrabits = FFMAX(0, f->pulses[i] - cap[i]);
  487. f->pulses[i] -= extrabits;
  488. /* intensity stereo makes use of an extra degree of freedom */
  489. dof = N * f->channels + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
  490. temp = dof * (ff_celt_log_freq_range[i] + (f->size << 3));
  491. offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
  492. if (N == 2) /* dof=2 is the only case that doesn't fit the model */
  493. offset += dof << 1;
  494. /* grant an additional bias for the first and second pulses */
  495. if (f->pulses[i] + offset < 2 * (dof << 3))
  496. offset += temp >> 2;
  497. else if (f->pulses[i] + offset < 3 * (dof << 3))
  498. offset += temp >> 3;
  499. fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
  500. max_bits = FFMIN((f->pulses[i] >> 3) >> (f->channels - 1), CELT_MAX_FINE_BITS);
  501. max_bits = FFMAX(max_bits, 0);
  502. f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
  503. /* if fine_bits was rounded down or capped,
  504. give priority for the final fine energy pass */
  505. f->fine_priority[i] = (f->fine_bits[i] * (dof << 3) >= f->pulses[i] + offset);
  506. /* the remaining bits are assigned to PVQ */
  507. f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
  508. } else {
  509. /* all bits go to fine energy except for the sign bit */
  510. extrabits = FFMAX(0, f->pulses[i] - (f->channels << 3));
  511. f->pulses[i] -= extrabits;
  512. f->fine_bits[i] = 0;
  513. f->fine_priority[i] = 1;
  514. }
  515. /* hand back a limited number of extra fine energy bits to this band */
  516. if (extrabits > 0) {
  517. int fineextra = FFMIN(extrabits >> (f->channels + 2),
  518. CELT_MAX_FINE_BITS - f->fine_bits[i]);
  519. f->fine_bits[i] += fineextra;
  520. fineextra <<= f->channels + 2;
  521. f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
  522. extrabits -= fineextra;
  523. }
  524. }
  525. f->remaining = extrabits;
  526. /* skipped bands dedicate all of their bits for fine energy */
  527. for (; i < f->end_band; i++) {
  528. f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3;
  529. f->pulses[i] = 0;
  530. f->fine_priority[i] = f->fine_bits[i] < 1;
  531. }
  532. }
  533. static void celt_quant_coarse(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f)
  534. {
  535. int i, ch;
  536. float alpha, beta, prev[2] = { 0, 0 };
  537. const uint8_t *pmod = ff_celt_coarse_energy_dist[f->size][f->intra];
  538. /* Inter is really just differential coding */
  539. if (opus_rc_tell(rc) + 3 <= f->framebits)
  540. ff_opus_rc_enc_log(rc, f->intra, 3);
  541. else
  542. f->intra = 0;
  543. if (f->intra) {
  544. alpha = 0.0f;
  545. beta = 1.0f - 4915.0f/32768.0f;
  546. } else {
  547. alpha = ff_celt_alpha_coef[f->size];
  548. beta = 1.0f - ff_celt_beta_coef[f->size];
  549. }
  550. for (i = f->start_band; i < f->end_band; i++) {
  551. for (ch = 0; ch < f->channels; ch++) {
  552. CeltBlock *block = &f->block[ch];
  553. const int left = f->framebits - opus_rc_tell(rc);
  554. const float last = FFMAX(-9.0f, s->last_quantized_energy[ch][i]);
  555. float diff = block->energy[i] - prev[ch] - last*alpha;
  556. int q_en = lrintf(diff);
  557. if (left >= 15) {
  558. ff_opus_rc_enc_laplace(rc, &q_en, pmod[i << 1] << 7, pmod[(i << 1) + 1] << 6);
  559. } else if (left >= 2) {
  560. q_en = av_clip(q_en, -1, 1);
  561. ff_opus_rc_enc_cdf(rc, 2*q_en + 3*(q_en < 0), ff_celt_model_energy_small);
  562. } else if (left >= 1) {
  563. q_en = av_clip(q_en, -1, 0);
  564. ff_opus_rc_enc_log(rc, (q_en & 1), 1);
  565. } else q_en = -1;
  566. block->error_energy[i] = q_en - diff;
  567. prev[ch] += beta * q_en;
  568. }
  569. }
  570. }
  571. static void celt_quant_fine(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f)
  572. {
  573. int i, ch;
  574. for (i = f->start_band; i < f->end_band; i++) {
  575. if (!f->fine_bits[i])
  576. continue;
  577. for (ch = 0; ch < f->channels; ch++) {
  578. CeltBlock *block = &f->block[ch];
  579. int quant, lim = (1 << f->fine_bits[i]);
  580. float offset, diff = 0.5f - block->error_energy[i];
  581. quant = av_clip(floor(diff*lim), 0, lim - 1);
  582. ff_opus_rc_put_raw(rc, quant, f->fine_bits[i]);
  583. offset = 0.5f - ((quant + 0.5f) * (1 << (14 - f->fine_bits[i])) / 16384.0f);
  584. block->error_energy[i] -= offset;
  585. }
  586. }
  587. }
  588. static void celt_quant_final(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f)
  589. {
  590. int i, ch, priority;
  591. for (priority = 0; priority < 2; priority++) {
  592. for (i = f->start_band; i < f->end_band && (f->framebits - opus_rc_tell(rc)) >= f->channels; i++) {
  593. if (f->fine_priority[i] != priority || f->fine_bits[i] >= CELT_MAX_FINE_BITS)
  594. continue;
  595. for (ch = 0; ch < f->channels; ch++) {
  596. CeltBlock *block = &f->block[ch];
  597. const float err = block->error_energy[i];
  598. const float offset = 0.5f * (1 << (14 - f->fine_bits[i] - 1)) / 16384.0f;
  599. const int sign = FFABS(err + offset) < FFABS(err - offset);
  600. ff_opus_rc_put_raw(rc, sign, 1);
  601. block->error_energy[i] -= offset*(1 - 2*sign);
  602. }
  603. }
  604. }
  605. }
  606. static void celt_quant_bands(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f)
  607. {
  608. float lowband_scratch[8 * 22];
  609. float norm[2 * 8 * 100];
  610. int totalbits = (f->framebits << 3) - f->anticollapse_needed;
  611. int update_lowband = 1;
  612. int lowband_offset = 0;
  613. int i, j;
  614. for (i = f->start_band; i < f->end_band; i++) {
  615. int band_offset = ff_celt_freq_bands[i] << f->size;
  616. int band_size = ff_celt_freq_range[i] << f->size;
  617. float *X = f->block[0].coeffs + band_offset;
  618. float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL;
  619. int consumed = opus_rc_tell_frac(rc);
  620. float *norm2 = norm + 8 * 100;
  621. int effective_lowband = -1;
  622. unsigned int cm[2];
  623. int b;
  624. /* Compute how many bits we want to allocate to this band */
  625. if (i != f->start_band)
  626. f->remaining -= consumed;
  627. f->remaining2 = totalbits - consumed - 1;
  628. if (i <= f->coded_bands - 1) {
  629. int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i);
  630. b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14);
  631. } else
  632. b = 0;
  633. if (ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] &&
  634. (update_lowband || lowband_offset == 0))
  635. lowband_offset = i;
  636. /* Get a conservative estimate of the collapse_mask's for the bands we're
  637. going to be folding from. */
  638. if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE ||
  639. f->blocks > 1 || f->tf_change[i] < 0)) {
  640. int foldstart, foldend;
  641. /* This ensures we never repeat spectral content within one band */
  642. effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band],
  643. ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]);
  644. foldstart = lowband_offset;
  645. while (ff_celt_freq_bands[--foldstart] > effective_lowband);
  646. foldend = lowband_offset - 1;
  647. while (ff_celt_freq_bands[++foldend] < effective_lowband + ff_celt_freq_range[i]);
  648. cm[0] = cm[1] = 0;
  649. for (j = foldstart; j < foldend; j++) {
  650. cm[0] |= f->block[0].collapse_masks[j];
  651. cm[1] |= f->block[f->channels - 1].collapse_masks[j];
  652. }
  653. } else
  654. /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
  655. always) be non-zero.*/
  656. cm[0] = cm[1] = (1 << f->blocks) - 1;
  657. if (f->dual_stereo && i == f->intensity_stereo) {
  658. /* Switch off dual stereo to do intensity */
  659. f->dual_stereo = 0;
  660. for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++)
  661. norm[j] = (norm[j] + norm2[j]) / 2;
  662. }
  663. if (f->dual_stereo) {
  664. cm[0] = ff_celt_encode_band(f, rc, i, X, NULL, band_size, b / 2, f->blocks,
  665. effective_lowband != -1 ? norm + (effective_lowband << f->size) : NULL, f->size,
  666. norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]);
  667. cm[1] = ff_celt_encode_band(f, rc, i, Y, NULL, band_size, b/2, f->blocks,
  668. effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL, f->size,
  669. norm2 + band_offset, 0, 1.0f, lowband_scratch, cm[1]);
  670. } else {
  671. cm[0] = ff_celt_encode_band(f, rc, i, X, Y, band_size, b, f->blocks,
  672. effective_lowband != -1 ? norm + (effective_lowband << f->size) : NULL, f->size,
  673. norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]|cm[1]);
  674. cm[1] = cm[0];
  675. }
  676. f->block[0].collapse_masks[i] = (uint8_t)cm[0];
  677. f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1];
  678. f->remaining += f->pulses[i] + consumed;
  679. /* Update the folding position only as long as we have 1 bit/sample depth */
  680. update_lowband = (b > band_size << 3);
  681. }
  682. }
  683. static void celt_encode_frame(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f)
  684. {
  685. int i, ch;
  686. celt_frame_setup_input(s, f);
  687. celt_apply_preemph_filter(s, f);
  688. if (f->pfilter) {
  689. /* Not implemented */
  690. }
  691. celt_frame_mdct(s, f);
  692. f->silence = celt_frame_map_norm_bands(s, f);
  693. if (f->silence) {
  694. f->framebits = 1;
  695. return;
  696. }
  697. ff_opus_rc_enc_log(rc, f->silence, 15);
  698. if (!f->start_band && opus_rc_tell(rc) + 16 <= f->framebits)
  699. ff_opus_rc_enc_log(rc, f->pfilter, 1);
  700. if (f->pfilter) {
  701. /* Not implemented */
  702. }
  703. if (f->size && opus_rc_tell(rc) + 3 <= f->framebits)
  704. ff_opus_rc_enc_log(rc, f->transient, 3);
  705. celt_quant_coarse (s, rc, f);
  706. celt_enc_tf (s, rc, f);
  707. celt_bitalloc (s, rc, f);
  708. celt_quant_fine (s, rc, f);
  709. celt_quant_bands (s, rc, f);
  710. if (f->anticollapse_needed)
  711. ff_opus_rc_put_raw(rc, f->anticollapse, 1);
  712. celt_quant_final(s, rc, f);
  713. for (ch = 0; ch < f->channels; ch++) {
  714. CeltBlock *block = &f->block[ch];
  715. for (i = 0; i < CELT_MAX_BANDS; i++)
  716. s->last_quantized_energy[ch][i] = block->energy[i] + block->error_energy[i];
  717. }
  718. }
  719. static void ff_opus_psy_process(OpusEncContext *s, int end, int *need_more)
  720. {
  721. int max_delay_samples = (s->options.max_delay_ms*s->avctx->sample_rate)/1000;
  722. int max_bsize = FFMIN(OPUS_SAMPLES_TO_BLOCK_SIZE(max_delay_samples), CELT_BLOCK_960);
  723. s->pkt_frames = 1;
  724. s->pkt_framesize = max_bsize;
  725. s->mode = OPUS_MODE_CELT;
  726. s->bandwidth = OPUS_BANDWIDTH_FULLBAND;
  727. *need_more = s->bufqueue.available*s->avctx->frame_size < (max_delay_samples + CELT_OVERLAP);
  728. /* Don't request more if we start being flushed with NULL frames */
  729. *need_more = !end && *need_more;
  730. }
  731. static void ff_opus_psy_celt_frame_setup(OpusEncContext *s, CeltFrame *f, int index)
  732. {
  733. int frame_size = OPUS_BLOCK_SIZE(s->pkt_framesize);
  734. f->avctx = s->avctx;
  735. f->dsp = s->dsp;
  736. f->start_band = (s->mode == OPUS_MODE_HYBRID) ? 17 : 0;
  737. f->end_band = ff_celt_band_end[s->bandwidth];
  738. f->channels = s->channels;
  739. f->size = s->pkt_framesize;
  740. /* Decisions */
  741. f->silence = 0;
  742. f->pfilter = 0;
  743. f->transient = 0;
  744. f->intra = 1;
  745. f->tf_select = 0;
  746. f->anticollapse = 0;
  747. f->alloc_trim = 5;
  748. f->skip_band_floor = f->end_band;
  749. f->intensity_stereo = f->end_band;
  750. f->dual_stereo = 0;
  751. f->spread = CELT_SPREAD_NORMAL;
  752. memset(f->tf_change, 0, sizeof(int)*CELT_MAX_BANDS);
  753. memset(f->alloc_boost, 0, sizeof(int)*CELT_MAX_BANDS);
  754. f->blocks = f->transient ? frame_size/CELT_OVERLAP : 1;
  755. f->framebits = FFALIGN(lrintf((double)s->avctx->bit_rate/(s->avctx->sample_rate/frame_size)), 8);
  756. }
  757. static void opus_packet_assembler(OpusEncContext *s, AVPacket *avpkt)
  758. {
  759. int i, offset, fsize_needed;
  760. /* Write toc */
  761. opus_gen_toc(s, avpkt->data, &offset, &fsize_needed);
  762. for (i = 0; i < s->pkt_frames; i++) {
  763. ff_opus_rc_enc_end(&s->rc[i], avpkt->data + offset, s->frame[i].framebits >> 3);
  764. offset += s->frame[i].framebits >> 3;
  765. }
  766. avpkt->size = offset;
  767. }
  768. /* Used as overlap for the first frame and padding for the last encoded packet */
  769. static AVFrame *spawn_empty_frame(OpusEncContext *s)
  770. {
  771. int i;
  772. AVFrame *f = av_frame_alloc();
  773. if (!f)
  774. return NULL;
  775. f->format = s->avctx->sample_fmt;
  776. f->nb_samples = s->avctx->frame_size;
  777. f->channel_layout = s->avctx->channel_layout;
  778. if (av_frame_get_buffer(f, 4)) {
  779. av_frame_free(&f);
  780. return NULL;
  781. }
  782. for (i = 0; i < s->channels; i++) {
  783. size_t bps = av_get_bytes_per_sample(f->format);
  784. memset(f->extended_data[i], 0, bps*f->nb_samples);
  785. }
  786. return f;
  787. }
  788. static int opus_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  789. const AVFrame *frame, int *got_packet_ptr)
  790. {
  791. OpusEncContext *s = avctx->priv_data;
  792. int i, ret, frame_size, need_more, alloc_size = 0;
  793. if (frame) { /* Add new frame to queue */
  794. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  795. return ret;
  796. ff_bufqueue_add(avctx, &s->bufqueue, av_frame_clone(frame));
  797. } else {
  798. if (!s->afq.remaining_samples)
  799. return 0; /* We've been flushed and there's nothing left to encode */
  800. }
  801. /* Run the psychoacoustic system */
  802. ff_opus_psy_process(s, !frame, &need_more);
  803. /* Get more samples for lookahead/encoding */
  804. if (need_more)
  805. return 0;
  806. frame_size = OPUS_BLOCK_SIZE(s->pkt_framesize);
  807. if (!frame) {
  808. /* This can go negative, that's not a problem, we only pad if positive */
  809. int pad_empty = s->pkt_frames*(frame_size/s->avctx->frame_size) - s->bufqueue.available + 1;
  810. /* Pad with empty 2.5 ms frames to whatever framesize was decided,
  811. * this should only happen at the very last flush frame. The frames
  812. * allocated here will be freed (because they have no other references)
  813. * after they get used by celt_frame_setup_input() */
  814. for (i = 0; i < pad_empty; i++) {
  815. AVFrame *empty = spawn_empty_frame(s);
  816. if (!empty)
  817. return AVERROR(ENOMEM);
  818. ff_bufqueue_add(avctx, &s->bufqueue, empty);
  819. }
  820. }
  821. for (i = 0; i < s->pkt_frames; i++) {
  822. ff_opus_rc_enc_init(&s->rc[i]);
  823. ff_opus_psy_celt_frame_setup(s, &s->frame[i], i);
  824. celt_encode_frame(s, &s->rc[i], &s->frame[i]);
  825. alloc_size += s->frame[i].framebits >> 3;
  826. }
  827. /* Worst case toc + the frame lengths if needed */
  828. alloc_size += 2 + s->pkt_frames*2;
  829. if ((ret = ff_alloc_packet2(avctx, avpkt, alloc_size, 0)) < 0)
  830. return ret;
  831. /* Assemble packet */
  832. opus_packet_assembler(s, avpkt);
  833. /* Remove samples from queue and skip if needed */
  834. ff_af_queue_remove(&s->afq, s->pkt_frames*frame_size, &avpkt->pts, &avpkt->duration);
  835. if (s->pkt_frames*frame_size > avpkt->duration) {
  836. uint8_t *side = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
  837. if (!side)
  838. return AVERROR(ENOMEM);
  839. AV_WL32(&side[4], s->pkt_frames*frame_size - avpkt->duration + 120);
  840. }
  841. *got_packet_ptr = 1;
  842. return 0;
  843. }
  844. static av_cold int opus_encode_end(AVCodecContext *avctx)
  845. {
  846. int i;
  847. OpusEncContext *s = avctx->priv_data;
  848. for (i = 0; i < CELT_BLOCK_NB; i++)
  849. ff_mdct15_uninit(&s->mdct[i]);
  850. av_freep(&s->dsp);
  851. av_freep(&s->frame);
  852. av_freep(&s->rc);
  853. ff_af_queue_close(&s->afq);
  854. ff_bufqueue_discard_all(&s->bufqueue);
  855. av_freep(&avctx->extradata);
  856. return 0;
  857. }
  858. static av_cold int opus_encode_init(AVCodecContext *avctx)
  859. {
  860. int i, ch, ret;
  861. OpusEncContext *s = avctx->priv_data;
  862. s->avctx = avctx;
  863. s->channels = avctx->channels;
  864. /* Opus allows us to change the framesize on each packet (and each packet may
  865. * have multiple frames in it) but we can't change the codec's frame size on
  866. * runtime, so fix it to the lowest possible number of samples and use a queue
  867. * to accumulate AVFrames until we have enough to encode whatever the encoder
  868. * decides is the best */
  869. avctx->frame_size = 120;
  870. /* Initial padding will change if SILK is ever supported */
  871. avctx->initial_padding = 120;
  872. avctx->cutoff = !avctx->cutoff ? 20000 : avctx->cutoff;
  873. if (!avctx->bit_rate) {
  874. int coupled = ff_opus_default_coupled_streams[s->channels - 1];
  875. avctx->bit_rate = coupled*(96000) + (s->channels - coupled*2)*(48000);
  876. } else if (avctx->bit_rate < 6000 || avctx->bit_rate > 255000 * s->channels) {
  877. int64_t clipped_rate = av_clip(avctx->bit_rate, 6000, 255000 * s->channels);
  878. av_log(avctx, AV_LOG_ERROR, "Unsupported bitrate %li kbps, clipping to %li kbps\n",
  879. avctx->bit_rate/1000, clipped_rate/1000);
  880. avctx->bit_rate = clipped_rate;
  881. }
  882. /* Frame structs and range coder buffers */
  883. s->frame = av_malloc(OPUS_MAX_FRAMES_PER_PACKET*sizeof(CeltFrame));
  884. if (!s->frame)
  885. return AVERROR(ENOMEM);
  886. s->rc = av_malloc(OPUS_MAX_FRAMES_PER_PACKET*sizeof(OpusRangeCoder));
  887. if (!s->rc)
  888. return AVERROR(ENOMEM);
  889. /* Extradata */
  890. avctx->extradata_size = 19;
  891. avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  892. if (!avctx->extradata)
  893. return AVERROR(ENOMEM);
  894. opus_write_extradata(avctx);
  895. ff_af_queue_init(avctx, &s->afq);
  896. if (!(s->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT)))
  897. return AVERROR(ENOMEM);
  898. /* I have no idea why a base scaling factor of 68 works, could be the twiddles */
  899. for (i = 0; i < CELT_BLOCK_NB; i++)
  900. if ((ret = ff_mdct15_init(&s->mdct[i], 0, i + 3, 68 << (CELT_BLOCK_NB - 1 - i))))
  901. return AVERROR(ENOMEM);
  902. /* Zero out previous energy (matters for inter first frame) */
  903. for (ch = 0; ch < s->channels; ch++)
  904. for (i = 0; i < CELT_MAX_BANDS; i++)
  905. s->last_quantized_energy[ch][i] = 0.0f;
  906. /* Allocate an empty frame to use as overlap for the first frame of audio */
  907. ff_bufqueue_add(avctx, &s->bufqueue, spawn_empty_frame(s));
  908. if (!ff_bufqueue_peek(&s->bufqueue, 0))
  909. return AVERROR(ENOMEM);
  910. return 0;
  911. }
  912. #define OPUSENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  913. static const AVOption opusenc_options[] = {
  914. { "opus_delay", "Maximum delay (and lookahead) in milliseconds", offsetof(OpusEncContext, options.max_delay_ms), AV_OPT_TYPE_FLOAT, { .dbl = OPUS_MAX_LOOKAHEAD }, 2.5f, OPUS_MAX_LOOKAHEAD, OPUSENC_FLAGS },
  915. { NULL },
  916. };
  917. static const AVClass opusenc_class = {
  918. .class_name = "Opus encoder",
  919. .item_name = av_default_item_name,
  920. .option = opusenc_options,
  921. .version = LIBAVUTIL_VERSION_INT,
  922. };
  923. static const AVCodecDefault opusenc_defaults[] = {
  924. { "b", "0" },
  925. { "compression_level", "10" },
  926. { NULL },
  927. };
  928. AVCodec ff_opus_encoder = {
  929. .name = "opus",
  930. .long_name = NULL_IF_CONFIG_SMALL("Opus"),
  931. .type = AVMEDIA_TYPE_AUDIO,
  932. .id = AV_CODEC_ID_OPUS,
  933. .defaults = opusenc_defaults,
  934. .priv_class = &opusenc_class,
  935. .priv_data_size = sizeof(OpusEncContext),
  936. .init = opus_encode_init,
  937. .encode2 = opus_encode_frame,
  938. .close = opus_encode_end,
  939. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  940. .capabilities = AV_CODEC_CAP_EXPERIMENTAL | AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY,
  941. .supported_samplerates = (const int []){ 48000, 0 },
  942. .channel_layouts = (const uint64_t []){ AV_CH_LAYOUT_MONO,
  943. AV_CH_LAYOUT_STEREO, 0 },
  944. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  945. AV_SAMPLE_FMT_NONE },
  946. };