2 * Opus encoder using libopus
3 * Copyright (c) 2012 Nathan Caldwell
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <opus_multistream.h>
25 #include "libavutil/opt.h"
27 #include "bytestream.h"
31 #include "audio_frame_queue.h"
33 typedef struct LibopusEncOpts {
43 typedef struct LibopusEncContext {
52 static const uint8_t opus_coupled_streams[8] = {
53 0, 1, 1, 2, 2, 2, 2, 3
56 /* Opus internal to Vorbis channel order mapping written in the header */
57 static const uint8_t opus_vorbis_channel_map[8][8] = {
64 { 0, 4, 1, 2, 3, 5, 6 },
65 { 0, 6, 1, 2, 3, 4, 5, 7 },
68 /* libav to libopus channel order mapping, passed to libopus */
69 static const uint8_t libav_libopus_channel_map[8][8] = {
76 { 0, 1, 5, 6, 2, 4, 3 },
77 { 0, 1, 6, 7, 4, 5, 2, 3 },
80 static void libopus_write_header(AVCodecContext *avctx, int stream_count,
81 int coupled_stream_count,
82 const uint8_t *channel_mapping)
84 uint8_t *p = avctx->extradata;
85 int channels = avctx->channels;
87 bytestream_put_buffer(&p, "OpusHead", 8);
88 bytestream_put_byte(&p, 1); /* Version */
89 bytestream_put_byte(&p, channels);
90 bytestream_put_le16(&p, avctx->initial_padding); /* Lookahead samples at 48kHz */
91 bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
92 bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
96 bytestream_put_byte(&p, channels <= 8 ? 1 : 255);
97 bytestream_put_byte(&p, stream_count);
98 bytestream_put_byte(&p, coupled_stream_count);
99 bytestream_put_buffer(&p, channel_mapping, channels);
101 bytestream_put_byte(&p, 0);
105 static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
106 LibopusEncOpts *opts)
110 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
111 if (ret != OPUS_OK) {
112 av_log(avctx, AV_LOG_ERROR,
113 "Failed to set bitrate: %s\n", opus_strerror(ret));
117 ret = opus_multistream_encoder_ctl(enc,
118 OPUS_SET_COMPLEXITY(opts->complexity));
120 av_log(avctx, AV_LOG_WARNING,
121 "Unable to set complexity: %s\n", opus_strerror(ret));
123 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
125 av_log(avctx, AV_LOG_WARNING,
126 "Unable to set VBR: %s\n", opus_strerror(ret));
128 ret = opus_multistream_encoder_ctl(enc,
129 OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
131 av_log(avctx, AV_LOG_WARNING,
132 "Unable to set constrained VBR: %s\n", opus_strerror(ret));
134 ret = opus_multistream_encoder_ctl(enc,
135 OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
137 av_log(avctx, AV_LOG_WARNING,
138 "Unable to set expected packet loss percentage: %s\n",
142 ret = opus_multistream_encoder_ctl(enc,
143 OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
145 av_log(avctx, AV_LOG_WARNING,
146 "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
152 static int av_cold libopus_encode_init(AVCodecContext *avctx)
154 LibopusEncContext *opus = avctx->priv_data;
155 const uint8_t *channel_mapping;
158 int coupled_stream_count, header_size, frame_size;
160 coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
161 opus->stream_count = avctx->channels - coupled_stream_count;
162 channel_mapping = libav_libopus_channel_map[avctx->channels - 1];
164 /* FIXME: Opus can handle up to 255 channels. However, the mapping for
165 * anything greater than 8 is undefined. */
166 if (avctx->channels > 8) {
167 av_log(avctx, AV_LOG_ERROR,
168 "Channel layout undefined for %d channels.\n", avctx->channels);
169 return AVERROR_PATCHWELCOME;
171 if (!avctx->bit_rate) {
172 /* Sane default copied from opusenc */
173 avctx->bit_rate = 64000 * opus->stream_count +
174 32000 * coupled_stream_count;
175 av_log(avctx, AV_LOG_WARNING,
176 "No bit rate set. Defaulting to %d bps.\n", avctx->bit_rate);
179 if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
180 av_log(avctx, AV_LOG_ERROR, "The bit rate %d bps is unsupported. "
181 "Please choose a value between 500 and %d.\n", avctx->bit_rate,
182 256000 * avctx->channels);
183 return AVERROR(EINVAL);
186 frame_size = opus->opts.frame_duration * 48000 / 1000;
187 switch (frame_size) {
190 if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
191 av_log(avctx, AV_LOG_WARNING,
192 "LPC mode cannot be used with a frame duration of less "
193 "than 10ms. Enabling restricted low-delay mode.\n"
194 "Use a longer frame duration if this is not what you want.\n");
195 /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
196 * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
197 opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
202 opus->opts.packet_size =
203 avctx->frame_size = frame_size * avctx->sample_rate / 48000;
206 av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
207 "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40 or 60.\n",
208 opus->opts.frame_duration);
209 return AVERROR(EINVAL);
212 if (avctx->compression_level < 0 || avctx->compression_level > 10) {
213 av_log(avctx, AV_LOG_WARNING,
214 "Compression level must be in the range 0 to 10. "
215 "Defaulting to 10.\n");
216 opus->opts.complexity = 10;
218 opus->opts.complexity = avctx->compression_level;
222 switch (avctx->cutoff) {
224 opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
227 opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
230 opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
233 opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
236 opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
239 av_log(avctx, AV_LOG_WARNING,
240 "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
241 "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
247 enc = opus_multistream_encoder_create(avctx->sample_rate, avctx->channels,
249 coupled_stream_count,
251 opus->opts.application, &ret);
252 if (ret != OPUS_OK) {
253 av_log(avctx, AV_LOG_ERROR,
254 "Failed to create encoder: %s\n", opus_strerror(ret));
255 return ff_opus_error_to_averror(ret);
258 ret = libopus_configure_encoder(avctx, enc, &opus->opts);
259 if (ret != OPUS_OK) {
260 ret = ff_opus_error_to_averror(ret);
264 header_size = 19 + (avctx->channels > 2 ? 2 + avctx->channels : 0);
265 avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE);
266 if (!avctx->extradata) {
267 av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
268 ret = AVERROR(ENOMEM);
271 avctx->extradata_size = header_size;
273 opus->samples = av_mallocz(frame_size * avctx->channels *
274 av_get_bytes_per_sample(avctx->sample_fmt));
275 if (!opus->samples) {
276 av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
277 ret = AVERROR(ENOMEM);
281 ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->initial_padding));
283 av_log(avctx, AV_LOG_WARNING,
284 "Unable to get number of lookahead samples: %s\n",
287 libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
288 opus_vorbis_channel_map[avctx->channels - 1]);
290 ff_af_queue_init(avctx, &opus->afq);
297 opus_multistream_encoder_destroy(enc);
298 av_freep(&avctx->extradata);
302 static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
303 const AVFrame *frame, int *got_packet_ptr)
305 LibopusEncContext *opus = avctx->priv_data;
306 const int sample_size = avctx->channels *
307 av_get_bytes_per_sample(avctx->sample_fmt);
312 ret = ff_af_queue_add(&opus->afq, frame);
315 if (frame->nb_samples < opus->opts.packet_size) {
316 audio = opus->samples;
317 memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
319 audio = frame->data[0];
321 if (!opus->afq.remaining_samples)
323 audio = opus->samples;
324 memset(audio, 0, opus->opts.packet_size * sample_size);
327 /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
328 * consist of 3 frames in one packet. The maximum frame size is 1275
329 * bytes along with the largest possible packet header of 7 bytes. */
330 if (ret = ff_alloc_packet(avpkt, (1275 * 3 + 7) * opus->stream_count)) {
331 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
335 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
336 ret = opus_multistream_encode_float(opus->enc, (float *)audio,
337 opus->opts.packet_size,
338 avpkt->data, avpkt->size);
340 ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
341 opus->opts.packet_size,
342 avpkt->data, avpkt->size);
345 av_log(avctx, AV_LOG_ERROR,
346 "Error encoding frame: %s\n", opus_strerror(ret));
347 return ff_opus_error_to_averror(ret);
350 av_shrink_packet(avpkt, ret);
352 ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
353 &avpkt->pts, &avpkt->duration);
360 static int av_cold libopus_encode_close(AVCodecContext *avctx)
362 LibopusEncContext *opus = avctx->priv_data;
364 opus_multistream_encoder_destroy(opus->enc);
366 ff_af_queue_close(&opus->afq);
368 av_freep(&opus->samples);
369 av_freep(&avctx->extradata);
374 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
375 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
376 static const AVOption libopus_options[] = {
377 { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
378 { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
379 { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
380 { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
381 { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 60.0, FLAGS },
382 { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
383 { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
384 { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
385 { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
386 { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
390 static const AVClass libopus_class = {
391 .class_name = "libopus",
392 .item_name = av_default_item_name,
393 .option = libopus_options,
394 .version = LIBAVUTIL_VERSION_INT,
397 static const AVCodecDefault libopus_defaults[] = {
399 { "compression_level", "10" },
403 static const int libopus_sample_rates[] = {
404 48000, 24000, 16000, 12000, 8000, 0,
407 AVCodec ff_libopus_encoder = {
409 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
410 .type = AVMEDIA_TYPE_AUDIO,
411 .id = AV_CODEC_ID_OPUS,
412 .priv_data_size = sizeof(LibopusEncContext),
413 .init = libopus_encode_init,
414 .encode2 = libopus_encode,
415 .close = libopus_encode_close,
416 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SMALL_LAST_FRAME,
417 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
419 AV_SAMPLE_FMT_NONE },
420 .channel_layouts = ff_vorbis_channel_layouts,
421 .supported_samplerates = libopus_sample_rates,
422 .priv_class = &libopus_class,
423 .defaults = libopus_defaults,