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.

617 lines
22KB

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