23#include <opus_multistream.h>
48#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
64 0, 1, 1, 2, 2, 2, 2, 3
75 { 0, 4, 1, 2, 3, 5, 6 },
76 { 0, 6, 1, 2, 3, 4, 5, 7 },
87 { 0, 1, 5, 6, 2, 4, 3 },
88 { 0, 1, 6, 7, 4, 5, 2, 3 },
92 int coupled_stream_count,
94 const uint8_t *channel_mapping)
100 bytestream_put_byte(&p, 1);
105 bytestream_put_le16(&p, 0);
108 bytestream_put_byte(&p, mapping_family);
109 if (mapping_family != 0) {
110 bytestream_put_byte(&p, stream_count);
111 bytestream_put_byte(&p, coupled_stream_count);
123 "Quality-based encoding not supported, "
124 "please specify a bitrate and VBR setting.\n");
128 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->
bit_rate));
129 if (
ret != OPUS_OK) {
131 "Failed to set bitrate: %s\n", opus_strerror(
ret));
135 ret = opus_multistream_encoder_ctl(enc,
136 OPUS_SET_COMPLEXITY(
opts->complexity));
139 "Unable to set complexity: %s\n", opus_strerror(
ret));
141 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!
opts->vbr));
144 "Unable to set VBR: %s\n", opus_strerror(
ret));
146 ret = opus_multistream_encoder_ctl(enc,
147 OPUS_SET_VBR_CONSTRAINT(
opts->vbr == 2));
150 "Unable to set constrained VBR: %s\n", opus_strerror(
ret));
152 ret = opus_multistream_encoder_ctl(enc,
153 OPUS_SET_PACKET_LOSS_PERC(
opts->packet_loss));
156 "Unable to set expected packet loss percentage: %s\n",
159 ret = opus_multistream_encoder_ctl(enc,
160 OPUS_SET_INBAND_FEC(
opts->fec));
163 "Unable to set inband FEC: %s\n",
166 ret = opus_multistream_encoder_ctl(enc,
167 OPUS_SET_DTX(
opts->dtx));
170 "Unable to set DTX: %s\n",
174 ret = opus_multistream_encoder_ctl(enc,
175 OPUS_SET_MAX_BANDWIDTH(
opts->max_bandwidth));
178 "Unable to set maximum bandwidth: %s\n", opus_strerror(
ret));
181#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
182 ret = opus_multistream_encoder_ctl(enc,
183 OPUS_SET_PHASE_INVERSION_DISABLED(!
opts->apply_phase_inv));
186 "Unable to set phase inversion: %s\n",
208 "No channel layout specified. Opus encoder will use Vorbis "
215 "Invalid channel layout %s for specified mapping family %d.\n",
216 name, mapping_family);
227 const uint8_t ** channel_map_result)
232 switch (mapping_family) {
260 "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
273 uint8_t libopus_channel_mapping[255];
277 int coupled_stream_count, header_size,
frame_size;
286 "LPC mode cannot be used with a frame duration of less "
287 "than 10ms. Enabling restricted low-delay mode.\n"
288 "Use a longer frame duration if this is not what you want.\n");
297#ifdef OPUS_FRAMESIZE_120_MS
308 "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40"
309#ifdef OPUS_FRAMESIZE_120_MS
310 ", 60, 80, 100 or 120.\n",
320 "Compression level must be in the range 0 to 10. "
321 "Defaulting to 10.\n");
346 "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
347 "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
365 mapping_family =
channels > 2 ? 1 : 0;
368 memcpy(libopus_channel_mapping,
370 channels *
sizeof(*libopus_channel_mapping));
372 enc = opus_multistream_encoder_create(
374 coupled_stream_count,
382 enc = opus_multistream_surround_encoder_create(
384 &opus->
stream_count, &coupled_stream_count, libopus_channel_mapping,
388 if (
ret != OPUS_OK) {
390 "Failed to create encoder: %s\n", opus_strerror(
ret));
397 32000 * coupled_stream_count;
399 "No bit rate set. Defaulting to %"PRId64
" bps.\n", avctx->
bit_rate);
404 "Please choose a value between 500 and %d.\n", avctx->
bit_rate,
411 if (
ret != OPUS_OK) {
417 header_size = 19 + (mapping_family == 0 ? 0 : 2 +
channels);
437 "Unable to get number of lookahead samples: %s\n",
441 mapping_family, libopus_channel_mapping);
450 opus_multistream_encoder_destroy(enc);
456 int nb_channels,
int nb_samples,
int bytes_per_sample) {
460 const size_t src_pos = bytes_per_sample * (nb_channels *
sample +
channel);
463 memcpy(&
dst[dst_pos], &
src[src_pos], bytes_per_sample);
474 const int sample_size =
channels * bytes_per_sample;
475 const uint8_t *audio;
491 audio =
frame->data[0];
506 ret = opus_multistream_encode_float(opus->
enc, (
const float *)audio,
510 ret = opus_multistream_encode(opus->
enc, (
const opus_int16 *)audio,
516 "Error encoding frame: %s\n", opus_strerror(
ret));
535 opus_multistream_encoder_destroy(opus->
enc);
544#define OFFSET(x) offsetof(LibopusEncContext, opts.x)
545#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
547 {
"application",
"Intended application type",
OFFSET(application),
AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY,
FLAGS, .unit =
"application" },
548 {
"voip",
"Favor improved speech intelligibility", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0,
FLAGS, .unit =
"application" },
549 {
"audio",
"Favor faithfulness to the input", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0,
FLAGS, .unit =
"application" },
550 {
"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" },
551 {
"frame_duration",
"Duration of a frame in milliseconds",
OFFSET(frame_duration),
AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 120.0,
FLAGS },
558 {
"mapping_family",
"Channel Mapping Family",
OFFSET(mapping_family),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255,
FLAGS, .unit =
"mapping_family" },
560#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
561 {
"apply_phase_inv",
"Apply intensity stereo phase inversion",
OFFSET(apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
FLAGS },
567 .class_name =
"libopus",
575 {
"compression_level",
"10" },
580 48000, 24000, 16000, 12000, 8000, 0,
599 .p.wrapper_name =
"libopus",
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
const FFCodec ff_libopus_encoder
static AVDictionary * opts
static const uint8_t channel_map[8][8]
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Libavcodec external API header.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Public libavutil channel layout APIs header.
#define CODEC_SAMPLERATES_ARRAY(array)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
int(* init)(AVBSFContext *ctx)
channel
Use these values when setting the channel map with ebur128_set_channel().
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
static const uint8_t frame_size[4]
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
@ AV_SAMPLE_FMT_S16
signed 16 bits
#define LIBAVUTIL_VERSION_INT
int ff_opus_error_to_averror(int err)
static const uint8_t libavcodec_libopus_channel_map[8][8]
static av_cold int libopus_encode_init(AVCodecContext *avctx)
static void libopus_copy_samples_with_channel_map(uint8_t *dst, const uint8_t *src, const uint8_t *channel_map, int nb_channels, int nb_samples, int bytes_per_sample)
static const int libopus_sample_rates[]
static int libopus_check_vorbis_layout(AVCodecContext *avctx, int mapping_family)
static av_cold int libopus_encode_close(AVCodecContext *avctx)
static const uint8_t opus_vorbis_channel_map[8][8]
static const FFCodecDefault libopus_defaults[]
static int libopus_validate_layout_and_get_channel_map(AVCodecContext *avctx, int mapping_family, const uint8_t **channel_map_result)
static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static const AVOption libopus_options[]
static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc, LibopusEncOpts *opts)
static const AVClass libopus_class
static const uint8_t opus_coupled_streams[8]
static void libopus_write_header(AVCodecContext *avctx, int stream_count, int coupled_stream_count, int mapping_family, const uint8_t *channel_mapping)
static int libopus_check_max_channels(AVCodecContext *avctx, int max_channels)
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
@ OPUS_BANDWIDTH_NARROWBAND
@ OPUS_BANDWIDTH_MEDIUMBAND
@ OPUS_BANDWIDTH_FULLBAND
@ OPUS_BANDWIDTH_SUPERWIDEBAND
@ OPUS_BANDWIDTH_WIDEBAND
#define FF_ARRAY_ELEMS(a)
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int global_quality
Global quality for codecs which cannot change it per frame.
int64_t bit_rate
the average bitrate
int initial_padding
Audio only.
int sample_rate
samples per second
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int cutoff
Audio cutoff bandwidth (0 means "automatic")
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
const uint8_t * encoder_channel_map
#define OPUS_TIMESTAMP_RATE
The clock of Opus: packet lengths, pre-skip and trimming count samples at 48 kHz whatever the sample ...
const AVChannelLayout ff_vorbis_ch_layouts[9]
const uint8_t ff_vorbis_channel_layout_offsets[8][8]