2
0

libopusenc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Opus encoder using libopus
  3. * Copyright (c) 2012 Nathan Caldwell
  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.h>
  22. #include <opus_multistream.h>
  23. #include "libavutil/opt.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "libopus.h"
  28. #include "vorbis.h"
  29. #include "audio_frame_queue.h"
  30. typedef struct LibopusEncOpts {
  31. int vbr;
  32. int application;
  33. int packet_loss;
  34. int complexity;
  35. float frame_duration;
  36. int packet_size;
  37. int max_bandwidth;
  38. int mapping_family;
  39. #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
  40. int apply_phase_inv;
  41. #endif
  42. } LibopusEncOpts;
  43. typedef struct LibopusEncContext {
  44. AVClass *class;
  45. OpusMSEncoder *enc;
  46. int stream_count;
  47. uint8_t *samples;
  48. LibopusEncOpts opts;
  49. AudioFrameQueue afq;
  50. const uint8_t *encoder_channel_map;
  51. } LibopusEncContext;
  52. static const uint8_t opus_coupled_streams[8] = {
  53. 0, 1, 1, 2, 2, 2, 2, 3
  54. };
  55. /* Opus internal to Vorbis channel order mapping written in the header */
  56. static const uint8_t opus_vorbis_channel_map[8][8] = {
  57. { 0 },
  58. { 0, 1 },
  59. { 0, 2, 1 },
  60. { 0, 1, 2, 3 },
  61. { 0, 4, 1, 2, 3 },
  62. { 0, 4, 1, 2, 3, 5 },
  63. { 0, 4, 1, 2, 3, 5, 6 },
  64. { 0, 6, 1, 2, 3, 4, 5, 7 },
  65. };
  66. /* libavcodec to libopus channel order mapping, passed to libopus */
  67. static const uint8_t libavcodec_libopus_channel_map[8][8] = {
  68. { 0 },
  69. { 0, 1 },
  70. { 0, 1, 2 },
  71. { 0, 1, 2, 3 },
  72. { 0, 1, 3, 4, 2 },
  73. { 0, 1, 4, 5, 2, 3 },
  74. { 0, 1, 5, 6, 2, 4, 3 },
  75. { 0, 1, 6, 7, 4, 5, 2, 3 },
  76. };
  77. static void libopus_write_header(AVCodecContext *avctx, int stream_count,
  78. int coupled_stream_count,
  79. int mapping_family,
  80. const uint8_t *channel_mapping)
  81. {
  82. uint8_t *p = avctx->extradata;
  83. int channels = avctx->channels;
  84. bytestream_put_buffer(&p, "OpusHead", 8);
  85. bytestream_put_byte(&p, 1); /* Version */
  86. bytestream_put_byte(&p, channels);
  87. bytestream_put_le16(&p, avctx->initial_padding); /* Lookahead samples at 48kHz */
  88. bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
  89. bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
  90. /* Channel mapping */
  91. bytestream_put_byte(&p, mapping_family);
  92. if (mapping_family != 0) {
  93. bytestream_put_byte(&p, stream_count);
  94. bytestream_put_byte(&p, coupled_stream_count);
  95. bytestream_put_buffer(&p, channel_mapping, channels);
  96. }
  97. }
  98. static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
  99. LibopusEncOpts *opts)
  100. {
  101. int ret;
  102. if (avctx->global_quality) {
  103. av_log(avctx, AV_LOG_ERROR,
  104. "Quality-based encoding not supported, "
  105. "please specify a bitrate and VBR setting.\n");
  106. return AVERROR(EINVAL);
  107. }
  108. ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
  109. if (ret != OPUS_OK) {
  110. av_log(avctx, AV_LOG_ERROR,
  111. "Failed to set bitrate: %s\n", opus_strerror(ret));
  112. return ret;
  113. }
  114. ret = opus_multistream_encoder_ctl(enc,
  115. OPUS_SET_COMPLEXITY(opts->complexity));
  116. if (ret != OPUS_OK)
  117. av_log(avctx, AV_LOG_WARNING,
  118. "Unable to set complexity: %s\n", opus_strerror(ret));
  119. ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
  120. if (ret != OPUS_OK)
  121. av_log(avctx, AV_LOG_WARNING,
  122. "Unable to set VBR: %s\n", opus_strerror(ret));
  123. ret = opus_multistream_encoder_ctl(enc,
  124. OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
  125. if (ret != OPUS_OK)
  126. av_log(avctx, AV_LOG_WARNING,
  127. "Unable to set constrained VBR: %s\n", opus_strerror(ret));
  128. ret = opus_multistream_encoder_ctl(enc,
  129. OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
  130. if (ret != OPUS_OK)
  131. av_log(avctx, AV_LOG_WARNING,
  132. "Unable to set expected packet loss percentage: %s\n",
  133. opus_strerror(ret));
  134. if (avctx->cutoff) {
  135. ret = opus_multistream_encoder_ctl(enc,
  136. OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
  137. if (ret != OPUS_OK)
  138. av_log(avctx, AV_LOG_WARNING,
  139. "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
  140. }
  141. #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
  142. ret = opus_multistream_encoder_ctl(enc,
  143. OPUS_SET_PHASE_INVERSION_DISABLED(!opts->apply_phase_inv));
  144. if (ret != OPUS_OK)
  145. av_log(avctx, AV_LOG_WARNING,
  146. "Unable to set phase inversion: %s\n",
  147. opus_strerror(ret));
  148. #endif
  149. return OPUS_OK;
  150. }
  151. static int libopus_check_max_channels(AVCodecContext *avctx,
  152. int max_channels) {
  153. if (avctx->channels > max_channels) {
  154. av_log(avctx, AV_LOG_ERROR, "Opus mapping family undefined for %d channels.\n",
  155. avctx->channels);
  156. return AVERROR(EINVAL);
  157. }
  158. return 0;
  159. }
  160. static int libopus_check_vorbis_layout(AVCodecContext *avctx, int mapping_family) {
  161. av_assert2(avctx->channels < FF_ARRAY_ELEMS(ff_vorbis_channel_layouts));
  162. if (!avctx->channel_layout) {
  163. av_log(avctx, AV_LOG_WARNING,
  164. "No channel layout specified. Opus encoder will use Vorbis "
  165. "channel layout for %d channels.\n", avctx->channels);
  166. } else if (avctx->channel_layout != ff_vorbis_channel_layouts[avctx->channels - 1]) {
  167. char name[32];
  168. av_get_channel_layout_string(name, sizeof(name), avctx->channels,
  169. avctx->channel_layout);
  170. av_log(avctx, AV_LOG_ERROR,
  171. "Invalid channel layout %s for specified mapping family %d.\n",
  172. name, mapping_family);
  173. return AVERROR(EINVAL);
  174. }
  175. return 0;
  176. }
  177. static int libopus_validate_layout_and_get_channel_map(
  178. AVCodecContext *avctx,
  179. int mapping_family,
  180. const uint8_t ** channel_map_result)
  181. {
  182. const uint8_t * channel_map = NULL;
  183. int ret;
  184. switch (mapping_family) {
  185. case -1:
  186. ret = libopus_check_max_channels(avctx, 8);
  187. if (ret == 0) {
  188. ret = libopus_check_vorbis_layout(avctx, mapping_family);
  189. /* Channels do not need to be reordered. */
  190. }
  191. break;
  192. case 0:
  193. ret = libopus_check_max_channels(avctx, 2);
  194. if (ret == 0) {
  195. ret = libopus_check_vorbis_layout(avctx, mapping_family);
  196. }
  197. break;
  198. case 1:
  199. /* Opus expects channels to be in Vorbis order. */
  200. ret = libopus_check_max_channels(avctx, 8);
  201. if (ret == 0) {
  202. ret = libopus_check_vorbis_layout(avctx, mapping_family);
  203. channel_map = ff_vorbis_channel_layout_offsets[avctx->channels - 1];
  204. }
  205. break;
  206. case 255:
  207. ret = libopus_check_max_channels(avctx, 254);
  208. break;
  209. default:
  210. av_log(avctx, AV_LOG_WARNING,
  211. "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
  212. mapping_family);
  213. ret = 0;
  214. }
  215. *channel_map_result = channel_map;
  216. return ret;
  217. }
  218. static av_cold int libopus_encode_init(AVCodecContext *avctx)
  219. {
  220. LibopusEncContext *opus = avctx->priv_data;
  221. OpusMSEncoder *enc;
  222. uint8_t libopus_channel_mapping[255];
  223. int ret = OPUS_OK;
  224. int av_ret;
  225. int coupled_stream_count, header_size, frame_size;
  226. int mapping_family;
  227. frame_size = opus->opts.frame_duration * 48000 / 1000;
  228. switch (frame_size) {
  229. case 120:
  230. case 240:
  231. if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
  232. av_log(avctx, AV_LOG_WARNING,
  233. "LPC mode cannot be used with a frame duration of less "
  234. "than 10ms. Enabling restricted low-delay mode.\n"
  235. "Use a longer frame duration if this is not what you want.\n");
  236. /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
  237. * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
  238. opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
  239. case 480:
  240. case 960:
  241. case 1920:
  242. case 2880:
  243. #ifdef OPUS_FRAMESIZE_120_MS
  244. case 3840:
  245. case 4800:
  246. case 5760:
  247. #endif
  248. opus->opts.packet_size =
  249. avctx->frame_size = frame_size * avctx->sample_rate / 48000;
  250. break;
  251. default:
  252. av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
  253. "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40"
  254. #ifdef OPUS_FRAMESIZE_120_MS
  255. ", 60, 80, 100 or 120.\n",
  256. #else
  257. " or 60.\n",
  258. #endif
  259. opus->opts.frame_duration);
  260. return AVERROR(EINVAL);
  261. }
  262. if (avctx->compression_level < 0 || avctx->compression_level > 10) {
  263. av_log(avctx, AV_LOG_WARNING,
  264. "Compression level must be in the range 0 to 10. "
  265. "Defaulting to 10.\n");
  266. opus->opts.complexity = 10;
  267. } else {
  268. opus->opts.complexity = avctx->compression_level;
  269. }
  270. if (avctx->cutoff) {
  271. switch (avctx->cutoff) {
  272. case 4000:
  273. opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
  274. break;
  275. case 6000:
  276. opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
  277. break;
  278. case 8000:
  279. opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
  280. break;
  281. case 12000:
  282. opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
  283. break;
  284. case 20000:
  285. opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
  286. break;
  287. default:
  288. av_log(avctx, AV_LOG_WARNING,
  289. "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
  290. "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
  291. avctx->cutoff);
  292. avctx->cutoff = 0;
  293. }
  294. }
  295. /* Channels may need to be reordered to match opus mapping. */
  296. av_ret = libopus_validate_layout_and_get_channel_map(avctx, opus->opts.mapping_family,
  297. &opus->encoder_channel_map);
  298. if (av_ret) {
  299. return av_ret;
  300. }
  301. if (opus->opts.mapping_family == -1) {
  302. /* By default, use mapping family 1 for the header but use the older
  303. * libopus multistream API to avoid surround masking. */
  304. /* Set the mapping family so that the value is correct in the header */
  305. mapping_family = avctx->channels > 2 ? 1 : 0;
  306. coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
  307. opus->stream_count = avctx->channels - coupled_stream_count;
  308. memcpy(libopus_channel_mapping,
  309. opus_vorbis_channel_map[avctx->channels - 1],
  310. avctx->channels * sizeof(*libopus_channel_mapping));
  311. enc = opus_multistream_encoder_create(
  312. avctx->sample_rate, avctx->channels, opus->stream_count,
  313. coupled_stream_count,
  314. libavcodec_libopus_channel_map[avctx->channels - 1],
  315. opus->opts.application, &ret);
  316. } else {
  317. /* Use the newer multistream API. The encoder will set the channel
  318. * mapping and coupled stream counts to its internal defaults and will
  319. * use surround masking analysis to save bits. */
  320. mapping_family = opus->opts.mapping_family;
  321. enc = opus_multistream_surround_encoder_create(
  322. avctx->sample_rate, avctx->channels, mapping_family,
  323. &opus->stream_count, &coupled_stream_count, libopus_channel_mapping,
  324. opus->opts.application, &ret);
  325. }
  326. if (ret != OPUS_OK) {
  327. av_log(avctx, AV_LOG_ERROR,
  328. "Failed to create encoder: %s\n", opus_strerror(ret));
  329. return ff_opus_error_to_averror(ret);
  330. }
  331. if (!avctx->bit_rate) {
  332. /* Sane default copied from opusenc */
  333. avctx->bit_rate = 64000 * opus->stream_count +
  334. 32000 * coupled_stream_count;
  335. av_log(avctx, AV_LOG_WARNING,
  336. "No bit rate set. Defaulting to %"PRId64" bps.\n", avctx->bit_rate);
  337. }
  338. if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
  339. av_log(avctx, AV_LOG_ERROR, "The bit rate %"PRId64" bps is unsupported. "
  340. "Please choose a value between 500 and %d.\n", avctx->bit_rate,
  341. 256000 * avctx->channels);
  342. ret = AVERROR(EINVAL);
  343. goto fail;
  344. }
  345. ret = libopus_configure_encoder(avctx, enc, &opus->opts);
  346. if (ret != OPUS_OK) {
  347. ret = ff_opus_error_to_averror(ret);
  348. goto fail;
  349. }
  350. /* Header includes channel mapping table if and only if mapping family is NOT 0 */
  351. header_size = 19 + (mapping_family == 0 ? 0 : 2 + avctx->channels);
  352. avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE);
  353. if (!avctx->extradata) {
  354. av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
  355. ret = AVERROR(ENOMEM);
  356. goto fail;
  357. }
  358. avctx->extradata_size = header_size;
  359. opus->samples = av_mallocz_array(frame_size, avctx->channels *
  360. av_get_bytes_per_sample(avctx->sample_fmt));
  361. if (!opus->samples) {
  362. av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
  363. ret = AVERROR(ENOMEM);
  364. goto fail;
  365. }
  366. ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->initial_padding));
  367. if (ret != OPUS_OK)
  368. av_log(avctx, AV_LOG_WARNING,
  369. "Unable to get number of lookahead samples: %s\n",
  370. opus_strerror(ret));
  371. libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
  372. mapping_family, libopus_channel_mapping);
  373. ff_af_queue_init(avctx, &opus->afq);
  374. opus->enc = enc;
  375. return 0;
  376. fail:
  377. opus_multistream_encoder_destroy(enc);
  378. av_freep(&avctx->extradata);
  379. return ret;
  380. }
  381. static void libopus_copy_samples_with_channel_map(
  382. uint8_t *dst, const uint8_t *src, const uint8_t *channel_map,
  383. int nb_channels, int nb_samples, int bytes_per_sample) {
  384. int sample, channel;
  385. for (sample = 0; sample < nb_samples; ++sample) {
  386. for (channel = 0; channel < nb_channels; ++channel) {
  387. const size_t src_pos = bytes_per_sample * (nb_channels * sample + channel);
  388. const size_t dst_pos = bytes_per_sample * (nb_channels * sample + channel_map[channel]);
  389. memcpy(&dst[dst_pos], &src[src_pos], bytes_per_sample);
  390. }
  391. }
  392. }
  393. static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
  394. const AVFrame *frame, int *got_packet_ptr)
  395. {
  396. LibopusEncContext *opus = avctx->priv_data;
  397. const int bytes_per_sample = av_get_bytes_per_sample(avctx->sample_fmt);
  398. const int sample_size = avctx->channels * bytes_per_sample;
  399. uint8_t *audio;
  400. int ret;
  401. int discard_padding;
  402. if (frame) {
  403. ret = ff_af_queue_add(&opus->afq, frame);
  404. if (ret < 0)
  405. return ret;
  406. if (opus->encoder_channel_map != NULL) {
  407. audio = opus->samples;
  408. libopus_copy_samples_with_channel_map(
  409. audio, frame->data[0], opus->encoder_channel_map,
  410. avctx->channels, frame->nb_samples, bytes_per_sample);
  411. } else if (frame->nb_samples < opus->opts.packet_size) {
  412. audio = opus->samples;
  413. memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
  414. } else
  415. audio = frame->data[0];
  416. } else {
  417. if (!opus->afq.remaining_samples || (!opus->afq.frame_alloc && !opus->afq.frame_count))
  418. return 0;
  419. audio = opus->samples;
  420. memset(audio, 0, opus->opts.packet_size * sample_size);
  421. }
  422. /* Maximum packet size taken from opusenc in opus-tools. 120ms packets
  423. * consist of 6 frames in one packet. The maximum frame size is 1275
  424. * bytes along with the largest possible packet header of 7 bytes. */
  425. if ((ret = ff_alloc_packet2(avctx, avpkt, (1275 * 6 + 7) * opus->stream_count, 0)) < 0)
  426. return ret;
  427. if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
  428. ret = opus_multistream_encode_float(opus->enc, (float *)audio,
  429. opus->opts.packet_size,
  430. avpkt->data, avpkt->size);
  431. else
  432. ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
  433. opus->opts.packet_size,
  434. avpkt->data, avpkt->size);
  435. if (ret < 0) {
  436. av_log(avctx, AV_LOG_ERROR,
  437. "Error encoding frame: %s\n", opus_strerror(ret));
  438. return ff_opus_error_to_averror(ret);
  439. }
  440. av_shrink_packet(avpkt, ret);
  441. ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
  442. &avpkt->pts, &avpkt->duration);
  443. discard_padding = opus->opts.packet_size - avpkt->duration;
  444. // Check if subtraction resulted in an overflow
  445. if ((discard_padding < opus->opts.packet_size) != (avpkt->duration > 0)) {
  446. av_packet_unref(avpkt);
  447. return AVERROR(EINVAL);
  448. }
  449. if (discard_padding > 0) {
  450. uint8_t* side_data = av_packet_new_side_data(avpkt,
  451. AV_PKT_DATA_SKIP_SAMPLES,
  452. 10);
  453. if(!side_data) {
  454. av_packet_unref(avpkt);
  455. return AVERROR(ENOMEM);
  456. }
  457. AV_WL32(side_data + 4, discard_padding);
  458. }
  459. *got_packet_ptr = 1;
  460. return 0;
  461. }
  462. static av_cold int libopus_encode_close(AVCodecContext *avctx)
  463. {
  464. LibopusEncContext *opus = avctx->priv_data;
  465. opus_multistream_encoder_destroy(opus->enc);
  466. ff_af_queue_close(&opus->afq);
  467. av_freep(&opus->samples);
  468. av_freep(&avctx->extradata);
  469. return 0;
  470. }
  471. #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
  472. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  473. static const AVOption libopus_options[] = {
  474. { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
  475. { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
  476. { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
  477. { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
  478. { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 120.0, FLAGS },
  479. { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
  480. { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
  481. { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
  482. { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
  483. { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
  484. { "mapping_family", "Channel Mapping Family", OFFSET(mapping_family), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, FLAGS, "mapping_family" },
  485. #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
  486. { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  487. #endif
  488. { NULL },
  489. };
  490. static const AVClass libopus_class = {
  491. .class_name = "libopus",
  492. .item_name = av_default_item_name,
  493. .option = libopus_options,
  494. .version = LIBAVUTIL_VERSION_INT,
  495. };
  496. static const AVCodecDefault libopus_defaults[] = {
  497. { "b", "0" },
  498. { "compression_level", "10" },
  499. { NULL },
  500. };
  501. static const int libopus_sample_rates[] = {
  502. 48000, 24000, 16000, 12000, 8000, 0,
  503. };
  504. AVCodec ff_libopus_encoder = {
  505. .name = "libopus",
  506. .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
  507. .type = AVMEDIA_TYPE_AUDIO,
  508. .id = AV_CODEC_ID_OPUS,
  509. .priv_data_size = sizeof(LibopusEncContext),
  510. .init = libopus_encode_init,
  511. .encode2 = libopus_encode,
  512. .close = libopus_encode_close,
  513. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SMALL_LAST_FRAME,
  514. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  515. AV_SAMPLE_FMT_FLT,
  516. AV_SAMPLE_FMT_NONE },
  517. .supported_samplerates = libopus_sample_rates,
  518. .priv_class = &libopus_class,
  519. .defaults = libopus_defaults,
  520. .wrapper_name = "libopus",
  521. };