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.

571 lines
21KB

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