Go to the documentation of this file.
23 #include <opus_multistream.h>
46 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
62 0, 1, 1, 2, 2, 2, 2, 3
73 { 0, 4, 1, 2, 3, 5, 6 },
74 { 0, 6, 1, 2, 3, 4, 5, 7 },
85 { 0, 1, 5, 6, 2, 4, 3 },
86 { 0, 1, 6, 7, 4, 5, 2, 3 },
90 int coupled_stream_count,
92 const uint8_t *channel_mapping)
98 bytestream_put_byte(&p, 1);
102 bytestream_put_le16(&p, 0);
105 bytestream_put_byte(&p, mapping_family);
106 if (mapping_family != 0) {
107 bytestream_put_byte(&p, stream_count);
108 bytestream_put_byte(&p, coupled_stream_count);
120 "Quality-based encoding not supported, "
121 "please specify a bitrate and VBR setting.\n");
125 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->
bit_rate));
126 if (
ret != OPUS_OK) {
128 "Failed to set bitrate: %s\n", opus_strerror(
ret));
132 ret = opus_multistream_encoder_ctl(enc,
133 OPUS_SET_COMPLEXITY(
opts->complexity));
136 "Unable to set complexity: %s\n", opus_strerror(
ret));
138 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!
opts->vbr));
141 "Unable to set VBR: %s\n", opus_strerror(
ret));
143 ret = opus_multistream_encoder_ctl(enc,
144 OPUS_SET_VBR_CONSTRAINT(
opts->vbr == 2));
147 "Unable to set constrained VBR: %s\n", opus_strerror(
ret));
149 ret = opus_multistream_encoder_ctl(enc,
150 OPUS_SET_PACKET_LOSS_PERC(
opts->packet_loss));
153 "Unable to set expected packet loss percentage: %s\n",
156 ret = opus_multistream_encoder_ctl(enc,
157 OPUS_SET_INBAND_FEC(
opts->fec));
160 "Unable to set inband FEC: %s\n",
164 ret = opus_multistream_encoder_ctl(enc,
165 OPUS_SET_MAX_BANDWIDTH(
opts->max_bandwidth));
168 "Unable to set maximum bandwidth: %s\n", opus_strerror(
ret));
171 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
172 ret = opus_multistream_encoder_ctl(enc,
173 OPUS_SET_PHASE_INVERSION_DISABLED(!
opts->apply_phase_inv));
176 "Unable to set phase inversion: %s\n",
198 "No channel layout specified. Opus encoder will use Vorbis "
205 "Invalid channel layout %s for specified mapping family %d.\n",
206 name, mapping_family);
217 const uint8_t ** channel_map_result)
222 switch (mapping_family) {
250 "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
263 uint8_t libopus_channel_mapping[255];
267 int coupled_stream_count, header_size,
frame_size;
276 "LPC mode cannot be used with a frame duration of less "
277 "than 10ms. Enabling restricted low-delay mode.\n"
278 "Use a longer frame duration if this is not what you want.\n");
286 #ifdef OPUS_FRAMESIZE_120_MS
296 "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40"
297 #ifdef OPUS_FRAMESIZE_120_MS
298 ", 60, 80, 100 or 120.\n",
308 "Compression level must be in the range 0 to 10. "
309 "Defaulting to 10.\n");
334 "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
335 "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
353 mapping_family =
channels > 2 ? 1 : 0;
356 memcpy(libopus_channel_mapping,
358 channels *
sizeof(*libopus_channel_mapping));
360 enc = opus_multistream_encoder_create(
362 coupled_stream_count,
370 enc = opus_multistream_surround_encoder_create(
372 &opus->
stream_count, &coupled_stream_count, libopus_channel_mapping,
376 if (
ret != OPUS_OK) {
378 "Failed to create encoder: %s\n", opus_strerror(
ret));
385 32000 * coupled_stream_count;
387 "No bit rate set. Defaulting to %"PRId64
" bps.\n", avctx->
bit_rate);
392 "Please choose a value between 500 and %d.\n", avctx->
bit_rate,
399 if (
ret != OPUS_OK) {
405 header_size = 19 + (mapping_family == 0 ? 0 : 2 +
channels);
425 "Unable to get number of lookahead samples: %s\n",
429 mapping_family, libopus_channel_mapping);
438 opus_multistream_encoder_destroy(enc);
444 int nb_channels,
int nb_samples,
int bytes_per_sample) {
448 const size_t src_pos = bytes_per_sample * (nb_channels *
sample +
channel);
451 memcpy(&dst[dst_pos], &
src[src_pos], bytes_per_sample);
462 const int sample_size =
channels * bytes_per_sample;
463 const uint8_t *audio;
480 audio =
frame->data[0];
495 ret = opus_multistream_encode_float(opus->
enc, (
const float *)audio,
499 ret = opus_multistream_encode(opus->
enc, (
const opus_int16 *)audio,
505 "Error encoding frame: %s\n", opus_strerror(
ret));
516 if ((discard_padding < opus->
opts.packet_size) != (avpkt->
duration > 0))
518 if (discard_padding > 0) {
524 AV_WL32(side_data + 4, discard_padding);
536 opus_multistream_encoder_destroy(opus->
enc);
545 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
546 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
548 {
"application",
"Intended application type",
OFFSET(application),
AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY,
FLAGS, .unit =
"application" },
549 {
"voip",
"Favor improved speech intelligibility", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0,
FLAGS, .unit =
"application" },
550 {
"audio",
"Favor faithfulness to the input", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0,
FLAGS, .unit =
"application" },
551 {
"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" },
552 {
"frame_duration",
"Duration of a frame in milliseconds",
OFFSET(frame_duration),
AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 120.0,
FLAGS },
559 {
"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 },
575 {
"compression_level",
"10" },
580 48000, 24000, 16000, 12000, 8000, 0,
601 .p.wrapper_name =
"libopus",
const FFCodec ff_libopus_encoder
static const AVClass libopus_class
int frame_size
Number of samples per channel in an audio frame.
#define AV_LOG_WARNING
Something somehow does not look correct.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
@ OPUS_BANDWIDTH_NARROWBAND
int sample_rate
samples per second
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
This structure describes decoded (raw) audio or video data.
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
@ OPUS_BANDWIDTH_FULLBAND
enum AVChannelOrder order
Channel order used in this layout.
static const uint8_t opus_vorbis_channel_map[8][8]
int nb_channels
Number of channels in this layout.
static av_cold int libopus_encode_init(AVCodecContext *avctx)
AVCodec p
The public AVCodec.
AVChannelLayout ch_layout
Audio channel layout.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
int initial_padding
Audio only.
#define FF_CODEC_ENCODE_CB(func)
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
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.
static av_cold int libopus_encode_close(AVCodecContext *avctx)
int global_quality
Global quality for codecs which cannot change it per frame.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
static void libopus_write_header(AVCodecContext *avctx, int stream_count, int coupled_stream_count, int mapping_family, const uint8_t *channel_mapping)
static const uint8_t channel_map[8][8]
int ff_opus_error_to_averror(int err)
#define CODEC_LONG_NAME(str)
@ OPUS_BANDWIDTH_WIDEBAND
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc, LibopusEncOpts *opts)
int64_t bit_rate
the average bitrate
const char * av_default_item_name(void *ptr)
Return the context name.
static const AVOption libopus_options[]
@ OPUS_BANDWIDTH_SUPERWIDEBAND
int(* init)(AVBSFContext *ctx)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
enum AVSampleFormat sample_fmt
audio sample format
const uint8_t * encoder_channel_map
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int cutoff
Audio cutoff bandwidth (0 means "automatic")
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S16
signed 16 bits
const char * name
Name of the codec implementation.
void * av_calloc(size_t nmemb, size_t size)
static int libopus_check_max_channels(AVCodecContext *avctx, int max_channels)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
static const FFCodecDefault libopus_defaults[]
#define AV_INPUT_BUFFER_PADDING_SIZE
static int libopus_check_vorbis_layout(AVCodecContext *avctx, int mapping_family)
main external API structure.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
const AVChannelLayout ff_vorbis_ch_layouts[9]
@ OPUS_BANDWIDTH_MEDIUMBAND
This structure stores compressed data.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
static int libopus_validate_layout_and_get_channel_map(AVCodecContext *avctx, int mapping_family, const uint8_t **channel_map_result)
static const uint8_t libavcodec_libopus_channel_map[8][8]
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
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 uint8_t opus_coupled_streams[8]
static const int libopus_sample_rates[]