00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <opus.h>
00023 #include <opus_multistream.h>
00024
00025 #include "libavutil/opt.h"
00026 #include "avcodec.h"
00027 #include "bytestream.h"
00028 #include "internal.h"
00029 #include "libopus.h"
00030 #include "vorbis.h"
00031 #include "audio_frame_queue.h"
00032
00033 typedef struct LibopusEncOpts {
00034 int vbr;
00035 int application;
00036 int packet_loss;
00037 int complexity;
00038 float frame_duration;
00039 int packet_size;
00040 int max_bandwidth;
00041 } LibopusEncOpts;
00042
00043 typedef struct LibopusEncContext {
00044 AVClass *class;
00045 OpusMSEncoder *enc;
00046 int stream_count;
00047 uint8_t *samples;
00048 LibopusEncOpts opts;
00049 AudioFrameQueue afq;
00050 } LibopusEncContext;
00051
00052 static const uint8_t opus_coupled_streams[8] = {
00053 0, 1, 1, 2, 2, 2, 2, 3
00054 };
00055
00056
00057 static const uint8_t opus_vorbis_channel_map[8][8] = {
00058 { 0 },
00059 { 0, 1 },
00060 { 0, 2, 1 },
00061 { 0, 1, 2, 3 },
00062 { 0, 4, 1, 2, 3 },
00063 { 0, 4, 1, 2, 3, 5 },
00064 { 0, 4, 1, 2, 3, 5, 6 },
00065 { 0, 6, 1, 2, 3, 4, 5, 7 },
00066 };
00067
00068
00069 static const uint8_t libavcodec_libopus_channel_map[8][8] = {
00070 { 0 },
00071 { 0, 1 },
00072 { 0, 1, 2 },
00073 { 0, 1, 2, 3 },
00074 { 0, 1, 3, 4, 2 },
00075 { 0, 1, 4, 5, 2, 3 },
00076 { 0, 1, 5, 6, 2, 4, 3 },
00077 { 0, 1, 6, 7, 4, 5, 2, 3 },
00078 };
00079
00080 static void libopus_write_header(AVCodecContext *avctx, int stream_count,
00081 int coupled_stream_count,
00082 const uint8_t *channel_mapping)
00083 {
00084 uint8_t *p = avctx->extradata;
00085 int channels = avctx->channels;
00086
00087 bytestream_put_buffer(&p, "OpusHead", 8);
00088 bytestream_put_byte(&p, 1);
00089 bytestream_put_byte(&p, channels);
00090 bytestream_put_le16(&p, avctx->delay);
00091 bytestream_put_le32(&p, avctx->sample_rate);
00092 bytestream_put_le16(&p, 0);
00093
00094
00095 if (channels > 2) {
00096 bytestream_put_byte(&p, channels <= 8 ? 1 : 255);
00097 bytestream_put_byte(&p, stream_count);
00098 bytestream_put_byte(&p, coupled_stream_count);
00099 bytestream_put_buffer(&p, channel_mapping, channels);
00100 } else {
00101 bytestream_put_byte(&p, 0);
00102 }
00103 }
00104
00105 static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
00106 LibopusEncOpts *opts)
00107 {
00108 int ret;
00109
00110 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
00111 if (ret != OPUS_OK) {
00112 av_log(avctx, AV_LOG_ERROR,
00113 "Failed to set bitrate: %s\n", opus_strerror(ret));
00114 return ret;
00115 }
00116
00117 ret = opus_multistream_encoder_ctl(enc,
00118 OPUS_SET_COMPLEXITY(opts->complexity));
00119 if (ret != OPUS_OK)
00120 av_log(avctx, AV_LOG_WARNING,
00121 "Unable to set complexity: %s\n", opus_strerror(ret));
00122
00123 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
00124 if (ret != OPUS_OK)
00125 av_log(avctx, AV_LOG_WARNING,
00126 "Unable to set VBR: %s\n", opus_strerror(ret));
00127
00128 ret = opus_multistream_encoder_ctl(enc,
00129 OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
00130 if (ret != OPUS_OK)
00131 av_log(avctx, AV_LOG_WARNING,
00132 "Unable to set constrained VBR: %s\n", opus_strerror(ret));
00133
00134 ret = opus_multistream_encoder_ctl(enc,
00135 OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
00136 if (ret != OPUS_OK)
00137 av_log(avctx, AV_LOG_WARNING,
00138 "Unable to set expected packet loss percentage: %s\n",
00139 opus_strerror(ret));
00140
00141 if (avctx->cutoff) {
00142 ret = opus_multistream_encoder_ctl(enc,
00143 OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
00144 if (ret != OPUS_OK)
00145 av_log(avctx, AV_LOG_WARNING,
00146 "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
00147 }
00148
00149 return OPUS_OK;
00150 }
00151
00152 static int av_cold libopus_encode_init(AVCodecContext *avctx)
00153 {
00154 LibopusEncContext *opus = avctx->priv_data;
00155 const uint8_t *channel_mapping;
00156 OpusMSEncoder *enc;
00157 int ret = OPUS_OK;
00158 int coupled_stream_count, header_size, frame_size;
00159
00160 coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
00161 opus->stream_count = avctx->channels - coupled_stream_count;
00162 channel_mapping = libavcodec_libopus_channel_map[avctx->channels - 1];
00163
00164
00165
00166 if (avctx->channels > 8)
00167 av_log(avctx, AV_LOG_WARNING,
00168 "Channel layout undefined for %d channels.\n", avctx->channels);
00169
00170 if (!avctx->bit_rate) {
00171
00172 avctx->bit_rate = 64000 * opus->stream_count +
00173 32000 * coupled_stream_count;
00174 av_log(avctx, AV_LOG_WARNING,
00175 "No bit rate set. Defaulting to %d bps.\n", avctx->bit_rate);
00176 }
00177
00178 if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
00179 av_log(avctx, AV_LOG_ERROR, "The bit rate %d bps is unsupported. "
00180 "Please choose a value between 500 and %d.\n", avctx->bit_rate,
00181 256000 * avctx->channels);
00182 return AVERROR(EINVAL);
00183 }
00184
00185 frame_size = opus->opts.frame_duration * 48000 / 1000;
00186 switch (frame_size) {
00187 case 120:
00188 case 240:
00189 if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
00190 av_log(avctx, AV_LOG_WARNING,
00191 "LPC mode cannot be used with a frame duration of less "
00192 "than 10ms. Enabling restricted low-delay mode.\n"
00193 "Use a longer frame duration if this is not what you want.\n");
00194
00195
00196 opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
00197 case 480:
00198 case 960:
00199 case 1920:
00200 case 2880:
00201 opus->opts.packet_size =
00202 avctx->frame_size = frame_size * avctx->sample_rate / 48000;
00203 break;
00204 default:
00205 av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
00206 "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40 or 60.\n",
00207 opus->opts.frame_duration);
00208 return AVERROR(EINVAL);
00209 }
00210
00211 if (avctx->compression_level < 0 || avctx->compression_level > 10) {
00212 av_log(avctx, AV_LOG_WARNING,
00213 "Compression level must be in the range 0 to 10. "
00214 "Defaulting to 10.\n");
00215 opus->opts.complexity = 10;
00216 } else {
00217 opus->opts.complexity = avctx->compression_level;
00218 }
00219
00220 if (avctx->cutoff) {
00221 switch (avctx->cutoff) {
00222 case 4000:
00223 opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
00224 break;
00225 case 6000:
00226 opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
00227 break;
00228 case 8000:
00229 opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
00230 break;
00231 case 12000:
00232 opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
00233 break;
00234 case 20000:
00235 opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
00236 break;
00237 default:
00238 av_log(avctx, AV_LOG_WARNING,
00239 "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
00240 "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
00241 avctx->cutoff);
00242 avctx->cutoff = 0;
00243 }
00244 }
00245
00246 enc = opus_multistream_encoder_create(avctx->sample_rate, avctx->channels,
00247 opus->stream_count,
00248 coupled_stream_count,
00249 channel_mapping,
00250 opus->opts.application, &ret);
00251 if (ret != OPUS_OK) {
00252 av_log(avctx, AV_LOG_ERROR,
00253 "Failed to create encoder: %s\n", opus_strerror(ret));
00254 return ff_opus_error_to_averror(ret);
00255 }
00256
00257 ret = libopus_configure_encoder(avctx, enc, &opus->opts);
00258 if (ret != OPUS_OK) {
00259 ret = ff_opus_error_to_averror(ret);
00260 goto fail;
00261 }
00262
00263 header_size = 19 + (avctx->channels > 2 ? 2 + avctx->channels : 0);
00264 avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
00265 if (!avctx->extradata) {
00266 av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
00267 ret = AVERROR(ENOMEM);
00268 goto fail;
00269 }
00270 avctx->extradata_size = header_size;
00271
00272 opus->samples = av_mallocz(frame_size * avctx->channels *
00273 av_get_bytes_per_sample(avctx->sample_fmt));
00274 if (!opus->samples) {
00275 av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
00276 ret = AVERROR(ENOMEM);
00277 goto fail;
00278 }
00279
00280 ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->delay));
00281 if (ret != OPUS_OK)
00282 av_log(avctx, AV_LOG_WARNING,
00283 "Unable to get number of lookahead samples: %s\n",
00284 opus_strerror(ret));
00285
00286 libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
00287 opus_vorbis_channel_map[avctx->channels - 1]);
00288
00289 ff_af_queue_init(avctx, &opus->afq);
00290
00291 opus->enc = enc;
00292
00293 return 0;
00294
00295 fail:
00296 opus_multistream_encoder_destroy(enc);
00297 av_freep(&avctx->extradata);
00298 return ret;
00299 }
00300
00301 static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
00302 const AVFrame *frame, int *got_packet_ptr)
00303 {
00304 LibopusEncContext *opus = avctx->priv_data;
00305 const int sample_size = avctx->channels *
00306 av_get_bytes_per_sample(avctx->sample_fmt);
00307 uint8_t *audio;
00308 int ret;
00309
00310 if (frame) {
00311 ff_af_queue_add(&opus->afq, frame);
00312 if (frame->nb_samples < opus->opts.packet_size) {
00313 audio = opus->samples;
00314 memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
00315 } else
00316 audio = frame->data[0];
00317 } else {
00318 if (!opus->afq.remaining_samples)
00319 return 0;
00320 audio = opus->samples;
00321 memset(audio, 0, opus->opts.packet_size * sample_size);
00322 }
00323
00324
00325
00326
00327 if (ret = ff_alloc_packet(avpkt, (1275 * 3 + 7) * opus->stream_count)) {
00328 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
00329 return ret;
00330 }
00331
00332 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
00333 ret = opus_multistream_encode_float(opus->enc, (float *)audio,
00334 opus->opts.packet_size,
00335 avpkt->data, avpkt->size);
00336 else
00337 ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
00338 opus->opts.packet_size,
00339 avpkt->data, avpkt->size);
00340
00341 if (ret < 0) {
00342 av_log(avctx, AV_LOG_ERROR,
00343 "Error encoding frame: %s\n", opus_strerror(ret));
00344 return ff_opus_error_to_averror(ret);
00345 }
00346
00347 av_shrink_packet(avpkt, ret);
00348
00349 ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
00350 &avpkt->pts, &avpkt->duration);
00351
00352 *got_packet_ptr = 1;
00353
00354 return 0;
00355 }
00356
00357 static int av_cold libopus_encode_close(AVCodecContext *avctx)
00358 {
00359 LibopusEncContext *opus = avctx->priv_data;
00360
00361 opus_multistream_encoder_destroy(opus->enc);
00362
00363 ff_af_queue_close(&opus->afq);
00364
00365 av_freep(&opus->samples);
00366 av_freep(&avctx->extradata);
00367
00368 return 0;
00369 }
00370
00371 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
00372 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00373 static const AVOption libopus_options[] = {
00374 { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
00375 { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
00376 { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
00377 { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
00378 { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 10.0 }, 2.5, 60.0, FLAGS },
00379 { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
00380 { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
00381 { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
00382 { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
00383 { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
00384 { NULL },
00385 };
00386
00387 static const AVClass libopus_class = {
00388 .class_name = "libopus",
00389 .item_name = av_default_item_name,
00390 .option = libopus_options,
00391 .version = LIBAVUTIL_VERSION_INT,
00392 };
00393
00394 static const AVCodecDefault libopus_defaults[] = {
00395 { "b", "0" },
00396 { "compression_level", "10" },
00397 { NULL },
00398 };
00399
00400 static const int libopus_sample_rates[] = {
00401 48000, 24000, 16000, 12000, 8000, 0,
00402 };
00403
00404 AVCodec ff_libopus_encoder = {
00405 .name = "libopus",
00406 .type = AVMEDIA_TYPE_AUDIO,
00407 .id = AV_CODEC_ID_OPUS,
00408 .priv_data_size = sizeof(LibopusEncContext),
00409 .init = libopus_encode_init,
00410 .encode2 = libopus_encode,
00411 .close = libopus_encode_close,
00412 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
00413 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00414 AV_SAMPLE_FMT_FLT,
00415 AV_SAMPLE_FMT_NONE },
00416 .channel_layouts = ff_vorbis_channel_layouts,
00417 .supported_samplerates = libopus_sample_rates,
00418 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
00419 .priv_class = &libopus_class,
00420 .defaults = libopus_defaults,
00421 };