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.

585 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 "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. opus->opts.packet_size =
  244. avctx->frame_size = frame_size * avctx->sample_rate / 48000;
  245. break;
  246. default:
  247. av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
  248. "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40 or 60.\n",
  249. opus->opts.frame_duration);
  250. return AVERROR(EINVAL);
  251. }
  252. if (avctx->compression_level < 0 || avctx->compression_level > 10) {
  253. av_log(avctx, AV_LOG_WARNING,
  254. "Compression level must be in the range 0 to 10. "
  255. "Defaulting to 10.\n");
  256. opus->opts.complexity = 10;
  257. } else {
  258. opus->opts.complexity = avctx->compression_level;
  259. }
  260. if (avctx->cutoff) {
  261. switch (avctx->cutoff) {
  262. case 4000:
  263. opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
  264. break;
  265. case 6000:
  266. opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
  267. break;
  268. case 8000:
  269. opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
  270. break;
  271. case 12000:
  272. opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
  273. break;
  274. case 20000:
  275. opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
  276. break;
  277. default:
  278. av_log(avctx, AV_LOG_WARNING,
  279. "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
  280. "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
  281. avctx->cutoff);
  282. avctx->cutoff = 0;
  283. }
  284. }
  285. /* Channels may need to be reordered to match opus mapping. */
  286. av_ret = libopus_validate_layout_and_get_channel_map(avctx, opus->opts.mapping_family,
  287. &opus->encoder_channel_map);
  288. if (av_ret) {
  289. return av_ret;
  290. }
  291. if (opus->opts.mapping_family == -1) {
  292. /* By default, use mapping family 1 for the header but use the older
  293. * libopus multistream API to avoid surround masking. */
  294. /* Set the mapping family so that the value is correct in the header */
  295. mapping_family = avctx->channels > 2 ? 1 : 0;
  296. coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
  297. opus->stream_count = avctx->channels - coupled_stream_count;
  298. memcpy(libopus_channel_mapping,
  299. opus_vorbis_channel_map[avctx->channels - 1],
  300. avctx->channels * sizeof(*libopus_channel_mapping));
  301. enc = opus_multistream_encoder_create(
  302. avctx->sample_rate, avctx->channels, opus->stream_count,
  303. coupled_stream_count,
  304. libavcodec_libopus_channel_map[avctx->channels - 1],
  305. opus->opts.application, &ret);
  306. } else {
  307. /* Use the newer multistream API. The encoder will set the channel
  308. * mapping and coupled stream counts to its internal defaults and will
  309. * use surround masking analysis to save bits. */
  310. mapping_family = opus->opts.mapping_family;
  311. enc = opus_multistream_surround_encoder_create(
  312. avctx->sample_rate, avctx->channels, mapping_family,
  313. &opus->stream_count, &coupled_stream_count, libopus_channel_mapping,
  314. opus->opts.application, &ret);
  315. }
  316. if (ret != OPUS_OK) {
  317. av_log(avctx, AV_LOG_ERROR,
  318. "Failed to create encoder: %s\n", opus_strerror(ret));
  319. return ff_opus_error_to_averror(ret);
  320. }
  321. if (!avctx->bit_rate) {
  322. /* Sane default copied from opusenc */
  323. avctx->bit_rate = 64000 * opus->stream_count +
  324. 32000 * coupled_stream_count;
  325. av_log(avctx, AV_LOG_WARNING,
  326. "No bit rate set. Defaulting to %"PRId64" bps.\n", avctx->bit_rate);
  327. }
  328. if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
  329. av_log(avctx, AV_LOG_ERROR, "The bit rate %"PRId64" bps is unsupported. "
  330. "Please choose a value between 500 and %d.\n", avctx->bit_rate,
  331. 256000 * avctx->channels);
  332. ret = AVERROR(EINVAL);
  333. goto fail;
  334. }
  335. ret = libopus_configure_encoder(avctx, enc, &opus->opts);
  336. if (ret != OPUS_OK) {
  337. ret = ff_opus_error_to_averror(ret);
  338. goto fail;
  339. }
  340. /* Header includes channel mapping table if and only if mapping family is NOT 0 */
  341. header_size = 19 + (mapping_family == 0 ? 0 : 2 + avctx->channels);
  342. avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE);
  343. if (!avctx->extradata) {
  344. av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
  345. ret = AVERROR(ENOMEM);
  346. goto fail;
  347. }
  348. avctx->extradata_size = header_size;
  349. opus->samples = av_mallocz_array(frame_size, avctx->channels *
  350. av_get_bytes_per_sample(avctx->sample_fmt));
  351. if (!opus->samples) {
  352. av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
  353. ret = AVERROR(ENOMEM);
  354. goto fail;
  355. }
  356. ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->initial_padding));
  357. if (ret != OPUS_OK)
  358. av_log(avctx, AV_LOG_WARNING,
  359. "Unable to get number of lookahead samples: %s\n",
  360. opus_strerror(ret));
  361. libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
  362. mapping_family, libopus_channel_mapping);
  363. ff_af_queue_init(avctx, &opus->afq);
  364. opus->enc = enc;
  365. return 0;
  366. fail:
  367. opus_multistream_encoder_destroy(enc);
  368. av_freep(&avctx->extradata);
  369. return ret;
  370. }
  371. static void libopus_copy_samples_with_channel_map(
  372. uint8_t *dst, const uint8_t *src, const uint8_t *channel_map,
  373. int nb_channels, int nb_samples, int bytes_per_sample) {
  374. int sample, channel;
  375. for (sample = 0; sample < nb_samples; ++sample) {
  376. for (channel = 0; channel < nb_channels; ++channel) {
  377. const size_t src_pos = bytes_per_sample * (nb_channels * sample + channel);
  378. const size_t dst_pos = bytes_per_sample * (nb_channels * sample + channel_map[channel]);
  379. memcpy(&dst[dst_pos], &src[src_pos], bytes_per_sample);
  380. }
  381. }
  382. }
  383. static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
  384. const AVFrame *frame, int *got_packet_ptr)
  385. {
  386. LibopusEncContext *opus = avctx->priv_data;
  387. const int bytes_per_sample = av_get_bytes_per_sample(avctx->sample_fmt);
  388. const int sample_size = avctx->channels * bytes_per_sample;
  389. uint8_t *audio;
  390. int ret;
  391. int discard_padding;
  392. if (frame) {
  393. ret = ff_af_queue_add(&opus->afq, frame);
  394. if (ret < 0)
  395. return ret;
  396. if (opus->encoder_channel_map != NULL) {
  397. audio = opus->samples;
  398. libopus_copy_samples_with_channel_map(
  399. audio, frame->data[0], opus->encoder_channel_map,
  400. avctx->channels, frame->nb_samples, bytes_per_sample);
  401. } else if (frame->nb_samples < opus->opts.packet_size) {
  402. audio = opus->samples;
  403. memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
  404. } else
  405. audio = frame->data[0];
  406. } else {
  407. if (!opus->afq.remaining_samples || (!opus->afq.frame_alloc && !opus->afq.frame_count))
  408. return 0;
  409. audio = opus->samples;
  410. memset(audio, 0, opus->opts.packet_size * sample_size);
  411. }
  412. /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
  413. * consist of 3 frames in one packet. The maximum frame size is 1275
  414. * bytes along with the largest possible packet header of 7 bytes. */
  415. if ((ret = ff_alloc_packet2(avctx, avpkt, (1275 * 3 + 7) * opus->stream_count, 0)) < 0)
  416. return ret;
  417. if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
  418. ret = opus_multistream_encode_float(opus->enc, (float *)audio,
  419. opus->opts.packet_size,
  420. avpkt->data, avpkt->size);
  421. else
  422. ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
  423. opus->opts.packet_size,
  424. avpkt->data, avpkt->size);
  425. if (ret < 0) {
  426. av_log(avctx, AV_LOG_ERROR,
  427. "Error encoding frame: %s\n", opus_strerror(ret));
  428. return ff_opus_error_to_averror(ret);
  429. }
  430. av_shrink_packet(avpkt, ret);
  431. ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
  432. &avpkt->pts, &avpkt->duration);
  433. discard_padding = opus->opts.packet_size - avpkt->duration;
  434. // Check if subtraction resulted in an overflow
  435. if ((discard_padding < opus->opts.packet_size) != (avpkt->duration > 0)) {
  436. av_packet_unref(avpkt);
  437. av_free(avpkt);
  438. return AVERROR(EINVAL);
  439. }
  440. if (discard_padding > 0) {
  441. uint8_t* side_data = av_packet_new_side_data(avpkt,
  442. AV_PKT_DATA_SKIP_SAMPLES,
  443. 10);
  444. if(!side_data) {
  445. av_packet_unref(avpkt);
  446. av_free(avpkt);
  447. return AVERROR(ENOMEM);
  448. }
  449. AV_WL32(side_data + 4, discard_padding);
  450. }
  451. *got_packet_ptr = 1;
  452. return 0;
  453. }
  454. static av_cold int libopus_encode_close(AVCodecContext *avctx)
  455. {
  456. LibopusEncContext *opus = avctx->priv_data;
  457. opus_multistream_encoder_destroy(opus->enc);
  458. ff_af_queue_close(&opus->afq);
  459. av_freep(&opus->samples);
  460. av_freep(&avctx->extradata);
  461. return 0;
  462. }
  463. #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
  464. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  465. static const AVOption libopus_options[] = {
  466. { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
  467. { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
  468. { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
  469. { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
  470. { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 60.0, FLAGS },
  471. { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
  472. { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
  473. { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
  474. { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
  475. { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
  476. { "mapping_family", "Channel Mapping Family", OFFSET(mapping_family), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, FLAGS, "mapping_family" },
  477. #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
  478. { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  479. #endif
  480. { NULL },
  481. };
  482. static const AVClass libopus_class = {
  483. .class_name = "libopus",
  484. .item_name = av_default_item_name,
  485. .option = libopus_options,
  486. .version = LIBAVUTIL_VERSION_INT,
  487. };
  488. static const AVCodecDefault libopus_defaults[] = {
  489. { "b", "0" },
  490. { "compression_level", "10" },
  491. { NULL },
  492. };
  493. static const int libopus_sample_rates[] = {
  494. 48000, 24000, 16000, 12000, 8000, 0,
  495. };
  496. AVCodec ff_libopus_encoder = {
  497. .name = "libopus",
  498. .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
  499. .type = AVMEDIA_TYPE_AUDIO,
  500. .id = AV_CODEC_ID_OPUS,
  501. .priv_data_size = sizeof(LibopusEncContext),
  502. .init = libopus_encode_init,
  503. .encode2 = libopus_encode,
  504. .close = libopus_encode_close,
  505. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SMALL_LAST_FRAME,
  506. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  507. AV_SAMPLE_FMT_FLT,
  508. AV_SAMPLE_FMT_NONE },
  509. .supported_samplerates = libopus_sample_rates,
  510. .priv_class = &libopus_class,
  511. .defaults = libopus_defaults,
  512. .wrapper_name = "libopus",
  513. };