annotate ffmpeg/libavcodec/libopusenc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Opus encoder using libopus
yading@10 3 * Copyright (c) 2012 Nathan Caldwell
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include <opus.h>
yading@10 23 #include <opus_multistream.h>
yading@10 24
yading@10 25 #include "libavutil/opt.h"
yading@10 26 #include "avcodec.h"
yading@10 27 #include "bytestream.h"
yading@10 28 #include "internal.h"
yading@10 29 #include "libopus.h"
yading@10 30 #include "vorbis.h"
yading@10 31 #include "audio_frame_queue.h"
yading@10 32
yading@10 33 typedef struct LibopusEncOpts {
yading@10 34 int vbr;
yading@10 35 int application;
yading@10 36 int packet_loss;
yading@10 37 int complexity;
yading@10 38 float frame_duration;
yading@10 39 int packet_size;
yading@10 40 int max_bandwidth;
yading@10 41 } LibopusEncOpts;
yading@10 42
yading@10 43 typedef struct LibopusEncContext {
yading@10 44 AVClass *class;
yading@10 45 OpusMSEncoder *enc;
yading@10 46 int stream_count;
yading@10 47 uint8_t *samples;
yading@10 48 LibopusEncOpts opts;
yading@10 49 AudioFrameQueue afq;
yading@10 50 } LibopusEncContext;
yading@10 51
yading@10 52 static const uint8_t opus_coupled_streams[8] = {
yading@10 53 0, 1, 1, 2, 2, 2, 2, 3
yading@10 54 };
yading@10 55
yading@10 56 /* Opus internal to Vorbis channel order mapping written in the header */
yading@10 57 static const uint8_t opus_vorbis_channel_map[8][8] = {
yading@10 58 { 0 },
yading@10 59 { 0, 1 },
yading@10 60 { 0, 2, 1 },
yading@10 61 { 0, 1, 2, 3 },
yading@10 62 { 0, 4, 1, 2, 3 },
yading@10 63 { 0, 4, 1, 2, 3, 5 },
yading@10 64 { 0, 4, 1, 2, 3, 5, 6 },
yading@10 65 { 0, 6, 1, 2, 3, 4, 5, 7 },
yading@10 66 };
yading@10 67
yading@10 68 /* libavcodec to libopus channel order mapping, passed to libopus */
yading@10 69 static const uint8_t libavcodec_libopus_channel_map[8][8] = {
yading@10 70 { 0 },
yading@10 71 { 0, 1 },
yading@10 72 { 0, 1, 2 },
yading@10 73 { 0, 1, 2, 3 },
yading@10 74 { 0, 1, 3, 4, 2 },
yading@10 75 { 0, 1, 4, 5, 2, 3 },
yading@10 76 { 0, 1, 5, 6, 2, 4, 3 },
yading@10 77 { 0, 1, 6, 7, 4, 5, 2, 3 },
yading@10 78 };
yading@10 79
yading@10 80 static void libopus_write_header(AVCodecContext *avctx, int stream_count,
yading@10 81 int coupled_stream_count,
yading@10 82 const uint8_t *channel_mapping)
yading@10 83 {
yading@10 84 uint8_t *p = avctx->extradata;
yading@10 85 int channels = avctx->channels;
yading@10 86
yading@10 87 bytestream_put_buffer(&p, "OpusHead", 8);
yading@10 88 bytestream_put_byte(&p, 1); /* Version */
yading@10 89 bytestream_put_byte(&p, channels);
yading@10 90 bytestream_put_le16(&p, avctx->delay); /* Lookahead samples at 48kHz */
yading@10 91 bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
yading@10 92 bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
yading@10 93
yading@10 94 /* Channel mapping */
yading@10 95 if (channels > 2) {
yading@10 96 bytestream_put_byte(&p, channels <= 8 ? 1 : 255);
yading@10 97 bytestream_put_byte(&p, stream_count);
yading@10 98 bytestream_put_byte(&p, coupled_stream_count);
yading@10 99 bytestream_put_buffer(&p, channel_mapping, channels);
yading@10 100 } else {
yading@10 101 bytestream_put_byte(&p, 0);
yading@10 102 }
yading@10 103 }
yading@10 104
yading@10 105 static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
yading@10 106 LibopusEncOpts *opts)
yading@10 107 {
yading@10 108 int ret;
yading@10 109
yading@10 110 if (avctx->global_quality) {
yading@10 111 av_log(avctx, AV_LOG_ERROR,
yading@10 112 "Quality-based encoding not supported, "
yading@10 113 "please specify a bitrate and VBR setting.\n");
yading@10 114 return AVERROR(EINVAL);
yading@10 115 }
yading@10 116
yading@10 117 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
yading@10 118 if (ret != OPUS_OK) {
yading@10 119 av_log(avctx, AV_LOG_ERROR,
yading@10 120 "Failed to set bitrate: %s\n", opus_strerror(ret));
yading@10 121 return ret;
yading@10 122 }
yading@10 123
yading@10 124 ret = opus_multistream_encoder_ctl(enc,
yading@10 125 OPUS_SET_COMPLEXITY(opts->complexity));
yading@10 126 if (ret != OPUS_OK)
yading@10 127 av_log(avctx, AV_LOG_WARNING,
yading@10 128 "Unable to set complexity: %s\n", opus_strerror(ret));
yading@10 129
yading@10 130 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
yading@10 131 if (ret != OPUS_OK)
yading@10 132 av_log(avctx, AV_LOG_WARNING,
yading@10 133 "Unable to set VBR: %s\n", opus_strerror(ret));
yading@10 134
yading@10 135 ret = opus_multistream_encoder_ctl(enc,
yading@10 136 OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
yading@10 137 if (ret != OPUS_OK)
yading@10 138 av_log(avctx, AV_LOG_WARNING,
yading@10 139 "Unable to set constrained VBR: %s\n", opus_strerror(ret));
yading@10 140
yading@10 141 ret = opus_multistream_encoder_ctl(enc,
yading@10 142 OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
yading@10 143 if (ret != OPUS_OK)
yading@10 144 av_log(avctx, AV_LOG_WARNING,
yading@10 145 "Unable to set expected packet loss percentage: %s\n",
yading@10 146 opus_strerror(ret));
yading@10 147
yading@10 148 if (avctx->cutoff) {
yading@10 149 ret = opus_multistream_encoder_ctl(enc,
yading@10 150 OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
yading@10 151 if (ret != OPUS_OK)
yading@10 152 av_log(avctx, AV_LOG_WARNING,
yading@10 153 "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
yading@10 154 }
yading@10 155
yading@10 156 return OPUS_OK;
yading@10 157 }
yading@10 158
yading@10 159 static int av_cold libopus_encode_init(AVCodecContext *avctx)
yading@10 160 {
yading@10 161 LibopusEncContext *opus = avctx->priv_data;
yading@10 162 const uint8_t *channel_mapping;
yading@10 163 OpusMSEncoder *enc;
yading@10 164 int ret = OPUS_OK;
yading@10 165 int coupled_stream_count, header_size, frame_size;
yading@10 166
yading@10 167 coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
yading@10 168 opus->stream_count = avctx->channels - coupled_stream_count;
yading@10 169 channel_mapping = libavcodec_libopus_channel_map[avctx->channels - 1];
yading@10 170
yading@10 171 /* FIXME: Opus can handle up to 255 channels. However, the mapping for
yading@10 172 * anything greater than 8 is undefined. */
yading@10 173 if (avctx->channels > 8)
yading@10 174 av_log(avctx, AV_LOG_WARNING,
yading@10 175 "Channel layout undefined for %d channels.\n", avctx->channels);
yading@10 176
yading@10 177 if (!avctx->bit_rate) {
yading@10 178 /* Sane default copied from opusenc */
yading@10 179 avctx->bit_rate = 64000 * opus->stream_count +
yading@10 180 32000 * coupled_stream_count;
yading@10 181 av_log(avctx, AV_LOG_WARNING,
yading@10 182 "No bit rate set. Defaulting to %d bps.\n", avctx->bit_rate);
yading@10 183 }
yading@10 184
yading@10 185 if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
yading@10 186 av_log(avctx, AV_LOG_ERROR, "The bit rate %d bps is unsupported. "
yading@10 187 "Please choose a value between 500 and %d.\n", avctx->bit_rate,
yading@10 188 256000 * avctx->channels);
yading@10 189 return AVERROR(EINVAL);
yading@10 190 }
yading@10 191
yading@10 192 frame_size = opus->opts.frame_duration * 48000 / 1000;
yading@10 193 switch (frame_size) {
yading@10 194 case 120:
yading@10 195 case 240:
yading@10 196 if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
yading@10 197 av_log(avctx, AV_LOG_WARNING,
yading@10 198 "LPC mode cannot be used with a frame duration of less "
yading@10 199 "than 10ms. Enabling restricted low-delay mode.\n"
yading@10 200 "Use a longer frame duration if this is not what you want.\n");
yading@10 201 /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
yading@10 202 * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
yading@10 203 opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
yading@10 204 case 480:
yading@10 205 case 960:
yading@10 206 case 1920:
yading@10 207 case 2880:
yading@10 208 opus->opts.packet_size =
yading@10 209 avctx->frame_size = frame_size * avctx->sample_rate / 48000;
yading@10 210 break;
yading@10 211 default:
yading@10 212 av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
yading@10 213 "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40 or 60.\n",
yading@10 214 opus->opts.frame_duration);
yading@10 215 return AVERROR(EINVAL);
yading@10 216 }
yading@10 217
yading@10 218 if (avctx->compression_level < 0 || avctx->compression_level > 10) {
yading@10 219 av_log(avctx, AV_LOG_WARNING,
yading@10 220 "Compression level must be in the range 0 to 10. "
yading@10 221 "Defaulting to 10.\n");
yading@10 222 opus->opts.complexity = 10;
yading@10 223 } else {
yading@10 224 opus->opts.complexity = avctx->compression_level;
yading@10 225 }
yading@10 226
yading@10 227 if (avctx->cutoff) {
yading@10 228 switch (avctx->cutoff) {
yading@10 229 case 4000:
yading@10 230 opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
yading@10 231 break;
yading@10 232 case 6000:
yading@10 233 opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
yading@10 234 break;
yading@10 235 case 8000:
yading@10 236 opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
yading@10 237 break;
yading@10 238 case 12000:
yading@10 239 opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
yading@10 240 break;
yading@10 241 case 20000:
yading@10 242 opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
yading@10 243 break;
yading@10 244 default:
yading@10 245 av_log(avctx, AV_LOG_WARNING,
yading@10 246 "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
yading@10 247 "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
yading@10 248 avctx->cutoff);
yading@10 249 avctx->cutoff = 0;
yading@10 250 }
yading@10 251 }
yading@10 252
yading@10 253 enc = opus_multistream_encoder_create(avctx->sample_rate, avctx->channels,
yading@10 254 opus->stream_count,
yading@10 255 coupled_stream_count,
yading@10 256 channel_mapping,
yading@10 257 opus->opts.application, &ret);
yading@10 258 if (ret != OPUS_OK) {
yading@10 259 av_log(avctx, AV_LOG_ERROR,
yading@10 260 "Failed to create encoder: %s\n", opus_strerror(ret));
yading@10 261 return ff_opus_error_to_averror(ret);
yading@10 262 }
yading@10 263
yading@10 264 ret = libopus_configure_encoder(avctx, enc, &opus->opts);
yading@10 265 if (ret != OPUS_OK) {
yading@10 266 ret = ff_opus_error_to_averror(ret);
yading@10 267 goto fail;
yading@10 268 }
yading@10 269
yading@10 270 header_size = 19 + (avctx->channels > 2 ? 2 + avctx->channels : 0);
yading@10 271 avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
yading@10 272 if (!avctx->extradata) {
yading@10 273 av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
yading@10 274 ret = AVERROR(ENOMEM);
yading@10 275 goto fail;
yading@10 276 }
yading@10 277 avctx->extradata_size = header_size;
yading@10 278
yading@10 279 opus->samples = av_mallocz(frame_size * avctx->channels *
yading@10 280 av_get_bytes_per_sample(avctx->sample_fmt));
yading@10 281 if (!opus->samples) {
yading@10 282 av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
yading@10 283 ret = AVERROR(ENOMEM);
yading@10 284 goto fail;
yading@10 285 }
yading@10 286
yading@10 287 ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->delay));
yading@10 288 if (ret != OPUS_OK)
yading@10 289 av_log(avctx, AV_LOG_WARNING,
yading@10 290 "Unable to get number of lookahead samples: %s\n",
yading@10 291 opus_strerror(ret));
yading@10 292
yading@10 293 libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
yading@10 294 opus_vorbis_channel_map[avctx->channels - 1]);
yading@10 295
yading@10 296 ff_af_queue_init(avctx, &opus->afq);
yading@10 297
yading@10 298 opus->enc = enc;
yading@10 299
yading@10 300 return 0;
yading@10 301
yading@10 302 fail:
yading@10 303 opus_multistream_encoder_destroy(enc);
yading@10 304 av_freep(&avctx->extradata);
yading@10 305 return ret;
yading@10 306 }
yading@10 307
yading@10 308 static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
yading@10 309 const AVFrame *frame, int *got_packet_ptr)
yading@10 310 {
yading@10 311 LibopusEncContext *opus = avctx->priv_data;
yading@10 312 const int sample_size = avctx->channels *
yading@10 313 av_get_bytes_per_sample(avctx->sample_fmt);
yading@10 314 uint8_t *audio;
yading@10 315 int ret;
yading@10 316
yading@10 317 if (frame) {
yading@10 318 ff_af_queue_add(&opus->afq, frame);
yading@10 319 if (frame->nb_samples < opus->opts.packet_size) {
yading@10 320 audio = opus->samples;
yading@10 321 memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
yading@10 322 } else
yading@10 323 audio = frame->data[0];
yading@10 324 } else {
yading@10 325 if (!opus->afq.remaining_samples)
yading@10 326 return 0;
yading@10 327 audio = opus->samples;
yading@10 328 memset(audio, 0, opus->opts.packet_size * sample_size);
yading@10 329 }
yading@10 330
yading@10 331 /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
yading@10 332 * consist of 3 frames in one packet. The maximum frame size is 1275
yading@10 333 * bytes along with the largest possible packet header of 7 bytes. */
yading@10 334 if ((ret = ff_alloc_packet2(avctx, avpkt, (1275 * 3 + 7) * opus->stream_count)) < 0)
yading@10 335 return ret;
yading@10 336
yading@10 337 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
yading@10 338 ret = opus_multistream_encode_float(opus->enc, (float *)audio,
yading@10 339 opus->opts.packet_size,
yading@10 340 avpkt->data, avpkt->size);
yading@10 341 else
yading@10 342 ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
yading@10 343 opus->opts.packet_size,
yading@10 344 avpkt->data, avpkt->size);
yading@10 345
yading@10 346 if (ret < 0) {
yading@10 347 av_log(avctx, AV_LOG_ERROR,
yading@10 348 "Error encoding frame: %s\n", opus_strerror(ret));
yading@10 349 return ff_opus_error_to_averror(ret);
yading@10 350 }
yading@10 351
yading@10 352 av_shrink_packet(avpkt, ret);
yading@10 353
yading@10 354 ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
yading@10 355 &avpkt->pts, &avpkt->duration);
yading@10 356
yading@10 357 *got_packet_ptr = 1;
yading@10 358
yading@10 359 return 0;
yading@10 360 }
yading@10 361
yading@10 362 static int av_cold libopus_encode_close(AVCodecContext *avctx)
yading@10 363 {
yading@10 364 LibopusEncContext *opus = avctx->priv_data;
yading@10 365
yading@10 366 opus_multistream_encoder_destroy(opus->enc);
yading@10 367
yading@10 368 ff_af_queue_close(&opus->afq);
yading@10 369
yading@10 370 av_freep(&opus->samples);
yading@10 371 av_freep(&avctx->extradata);
yading@10 372
yading@10 373 return 0;
yading@10 374 }
yading@10 375
yading@10 376 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
yading@10 377 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
yading@10 378 static const AVOption libopus_options[] = {
yading@10 379 { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
yading@10 380 { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
yading@10 381 { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
yading@10 382 { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
yading@10 383 { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 10.0 }, 2.5, 60.0, FLAGS },
yading@10 384 { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
yading@10 385 { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
yading@10 386 { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
yading@10 387 { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
yading@10 388 { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
yading@10 389 { NULL },
yading@10 390 };
yading@10 391
yading@10 392 static const AVClass libopus_class = {
yading@10 393 .class_name = "libopus",
yading@10 394 .item_name = av_default_item_name,
yading@10 395 .option = libopus_options,
yading@10 396 .version = LIBAVUTIL_VERSION_INT,
yading@10 397 };
yading@10 398
yading@10 399 static const AVCodecDefault libopus_defaults[] = {
yading@10 400 { "b", "0" },
yading@10 401 { "compression_level", "10" },
yading@10 402 { NULL },
yading@10 403 };
yading@10 404
yading@10 405 static const int libopus_sample_rates[] = {
yading@10 406 48000, 24000, 16000, 12000, 8000, 0,
yading@10 407 };
yading@10 408
yading@10 409 AVCodec ff_libopus_encoder = {
yading@10 410 .name = "libopus",
yading@10 411 .type = AVMEDIA_TYPE_AUDIO,
yading@10 412 .id = AV_CODEC_ID_OPUS,
yading@10 413 .priv_data_size = sizeof(LibopusEncContext),
yading@10 414 .init = libopus_encode_init,
yading@10 415 .encode2 = libopus_encode,
yading@10 416 .close = libopus_encode_close,
yading@10 417 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
yading@10 418 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
yading@10 419 AV_SAMPLE_FMT_FLT,
yading@10 420 AV_SAMPLE_FMT_NONE },
yading@10 421 .channel_layouts = ff_vorbis_channel_layouts,
yading@10 422 .supported_samplerates = libopus_sample_rates,
yading@10 423 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
yading@10 424 .priv_class = &libopus_class,
yading@10 425 .defaults = libopus_defaults,
yading@10 426 };