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