23 #include <opus_multistream.h>
43 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
59 0, 1, 1, 2, 2, 2, 2, 3
70 { 0, 4, 1, 2, 3, 5, 6 },
71 { 0, 6, 1, 2, 3, 4, 5, 7 },
82 { 0, 1, 5, 6, 2, 4, 3 },
83 { 0, 1, 6, 7, 4, 5, 2, 3 },
87 int coupled_stream_count,
95 bytestream_put_byte(&p, 1);
99 bytestream_put_le16(&p, 0);
102 bytestream_put_byte(&p, mapping_family);
103 if (mapping_family != 0) {
104 bytestream_put_byte(&p, stream_count);
105 bytestream_put_byte(&p, coupled_stream_count);
117 "Quality-based encoding not supported, "
118 "please specify a bitrate and VBR setting.\n");
122 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->
bit_rate));
123 if (ret != OPUS_OK) {
125 "Failed to set bitrate: %s\n", opus_strerror(ret));
129 ret = opus_multistream_encoder_ctl(enc,
130 OPUS_SET_COMPLEXITY(
opts->complexity));
133 "Unable to set complexity: %s\n", opus_strerror(ret));
135 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!
opts->vbr));
138 "Unable to set VBR: %s\n", opus_strerror(ret));
140 ret = opus_multistream_encoder_ctl(enc,
141 OPUS_SET_VBR_CONSTRAINT(
opts->vbr == 2));
144 "Unable to set constrained VBR: %s\n", opus_strerror(ret));
146 ret = opus_multistream_encoder_ctl(enc,
147 OPUS_SET_PACKET_LOSS_PERC(
opts->packet_loss));
150 "Unable to set expected packet loss percentage: %s\n",
153 ret = opus_multistream_encoder_ctl(enc,
154 OPUS_SET_INBAND_FEC(
opts->fec));
157 "Unable to set inband FEC: %s\n",
161 ret = opus_multistream_encoder_ctl(enc,
162 OPUS_SET_MAX_BANDWIDTH(
opts->max_bandwidth));
165 "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
168 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
169 ret = opus_multistream_encoder_ctl(enc,
170 OPUS_SET_PHASE_INVERSION_DISABLED(!
opts->apply_phase_inv));
173 "Unable to set phase inversion: %s\n",
181 if (avctx->
channels > max_channels) {
195 "No channel layout specified. Opus encoder will use Vorbis "
196 "channel layout for %d channels.\n", avctx->
channels);
202 "Invalid channel layout %s for specified mapping family %d.\n",
203 name, mapping_family);
214 const uint8_t ** channel_map_result)
219 switch (mapping_family) {
247 "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
252 *channel_map_result = channel_map;
260 uint8_t libopus_channel_mapping[255];
263 int coupled_stream_count, header_size,
frame_size;
272 "LPC mode cannot be used with a frame duration of less "
273 "than 10ms. Enabling restricted low-delay mode.\n"
274 "Use a longer frame duration if this is not what you want.\n");
282 #ifdef OPUS_FRAMESIZE_120_MS
292 "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40"
293 #ifdef OPUS_FRAMESIZE_120_MS
294 ", 60, 80, 100 or 120.\n",
304 "Compression level must be in the range 0 to 10. "
305 "Defaulting to 10.\n");
330 "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
331 "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
349 mapping_family = avctx->
channels > 2 ? 1 : 0;
352 memcpy(libopus_channel_mapping,
354 avctx->
channels *
sizeof(*libopus_channel_mapping));
356 enc = opus_multistream_encoder_create(
358 coupled_stream_count,
366 enc = opus_multistream_surround_encoder_create(
368 &opus->
stream_count, &coupled_stream_count, libopus_channel_mapping,
372 if (ret != OPUS_OK) {
374 "Failed to create encoder: %s\n", opus_strerror(ret));
381 32000 * coupled_stream_count;
383 "No bit rate set. Defaulting to %"PRId64
" bps.\n", avctx->
bit_rate);
388 "Please choose a value between 500 and %d.\n", avctx->
bit_rate,
395 if (ret != OPUS_OK) {
401 header_size = 19 + (mapping_family == 0 ? 0 : 2 + avctx->
channels);
418 ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->
initial_padding));
421 "Unable to get number of lookahead samples: %s\n",
425 mapping_family, libopus_channel_mapping);
434 opus_multistream_encoder_destroy(enc);
441 int nb_channels,
int nb_samples,
int bytes_per_sample) {
448 memcpy(&dst[dst_pos], &
src[src_pos], bytes_per_sample);
458 const int sample_size = avctx->
channels * bytes_per_sample;
491 ret = opus_multistream_encode_float(opus->
enc, (
float *)audio,
495 ret = opus_multistream_encode(opus->
enc, (opus_int16 *)audio,
501 "Error encoding frame: %s\n", opus_strerror(ret));
512 if ((discard_padding < opus->
opts.packet_size) != (avpkt->
duration > 0)) {
516 if (discard_padding > 0) {
524 AV_WL32(side_data + 4, discard_padding);
536 opus_multistream_encoder_destroy(opus->
enc);
546 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
547 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
549 {
"application",
"Intended application type",
OFFSET(application),
AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY,
FLAGS,
"application" },
550 {
"voip",
"Favor improved speech intelligibility", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0,
FLAGS,
"application" },
551 {
"audio",
"Favor faithfulness to the input", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0,
FLAGS,
"application" },
552 {
"lowdelay",
"Restrict to only the lowest delay modes", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0,
FLAGS,
"application" },
553 {
"frame_duration",
"Duration of a frame in milliseconds",
OFFSET(frame_duration),
AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 120.0,
FLAGS },
560 {
"mapping_family",
"Channel Mapping Family",
OFFSET(mapping_family),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255,
FLAGS,
"mapping_family" },
561 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
562 {
"apply_phase_inv",
"Apply intensity stereo phase inversion",
OFFSET(apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
FLAGS },
576 {
"compression_level",
"10" },
581 48000, 24000, 16000, 12000, 8000, 0,
600 .wrapper_name =
"libopus",
static enum AVSampleFormat sample_fmts[]
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
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.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
static av_cold int init(AVCodecContext *avctx)
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
channel
Use these values when setting the channel map with ebur128_set_channel().
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
#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_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_packet_unref(AVPacket *pkt)
Wipe the packet.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
#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.
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S16
signed 16 bits
#define LIBAVUTIL_VERSION_INT
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int ff_opus_error_to_averror(int err)
static const AVCodecDefault libopus_defaults[]
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 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[]
AVCodec ff_libopus_encoder
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)
@ OPUS_BANDWIDTH_NARROWBAND
@ OPUS_BANDWIDTH_MEDIUMBAND
@ OPUS_BANDWIDTH_FULLBAND
@ OPUS_BANDWIDTH_SUPERWIDEBAND
@ OPUS_BANDWIDTH_WIDEBAND
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
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
some codecs need / can use extradata like Huffman tables.
int channels
number of audio channels
int cutoff
Audio cutoff bandwidth (0 means "automatic")
uint64_t channel_layout
Audio channel layout.
int frame_size
Number of samples per channel in an audio frame.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
This structure stores compressed data.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
const uint8_t * encoder_channel_map
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
const uint64_t ff_vorbis_channel_layouts[9]