/* * Opus encoder using libopus * Copyright (c) 2012 Nathan Caldwell * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
typedefstruct LibopusEncOpts { int vbr; int application; int packet_loss; int fec; int complexity; float frame_duration; int packet_size; int max_bandwidth; int mapping_family; int dtx; #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST int apply_phase_inv; #endif
} LibopusEncOpts;
staticint libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
LibopusEncOpts *opts)
{ int ret;
if (avctx->global_quality) {
av_log(avctx, AV_LOG_ERROR, "Quality-based encoding not supported, " "please specify a bitrate and VBR setting.\n"); return AVERROR(EINVAL);
}
ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate)); if (ret != OPUS_OK) {
av_log(avctx, AV_LOG_ERROR, "Failed to set bitrate: %s\n", opus_strerror(ret)); return ret;
}
ret = opus_multistream_encoder_ctl(enc,
OPUS_SET_COMPLEXITY(opts->complexity)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to set complexity: %s\n", opus_strerror(ret));
ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to set VBR: %s\n", opus_strerror(ret));
ret = opus_multistream_encoder_ctl(enc,
OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to set constrained VBR: %s\n", opus_strerror(ret));
ret = opus_multistream_encoder_ctl(enc,
OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to set expected packet loss percentage: %s\n",
opus_strerror(ret));
ret = opus_multistream_encoder_ctl(enc,
OPUS_SET_INBAND_FEC(opts->fec)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to set inband FEC: %s\n",
opus_strerror(ret));
ret = opus_multistream_encoder_ctl(enc,
OPUS_SET_DTX(opts->dtx)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to set DTX: %s\n",
opus_strerror(ret));
if (avctx->cutoff) {
ret = opus_multistream_encoder_ctl(enc,
OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
}
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
ret = opus_multistream_encoder_ctl(enc,
OPUS_SET_PHASE_INVERSION_DISABLED(!opts->apply_phase_inv)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to set phase inversion: %s\n",
opus_strerror(ret)); #endif return OPUS_OK;
}
staticint libopus_check_max_channels(AVCodecContext *avctx, int max_channels) { if (avctx->ch_layout.nb_channels > max_channels) {
av_log(avctx, AV_LOG_ERROR, "Opus mapping family undefined for %d channels.\n",
avctx->ch_layout.nb_channels); return AVERROR(EINVAL);
}
return 0;
}
staticint libopus_check_vorbis_layout(AVCodecContext *avctx, int mapping_family) {
av_assert2(avctx->ch_layout.nb_channels < FF_ARRAY_ELEMS(ff_vorbis_ch_layouts));
if (avctx->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
av_log(avctx, AV_LOG_WARNING, "No channel layout specified. Opus encoder will use Vorbis " "channel layout for %d channels.\n", avctx->ch_layout.nb_channels);
} elseif (av_channel_layout_compare(&avctx->ch_layout, &ff_vorbis_ch_layouts[avctx->ch_layout.nb_channels - 1])) { char name[32];
av_channel_layout_describe(&avctx->ch_layout, name, sizeof(name));
av_log(avctx, AV_LOG_ERROR, "Invalid channel layout %s for specified mapping family %d.\n",
name, mapping_family);
return AVERROR(EINVAL);
}
return 0;
}
staticint libopus_validate_layout_and_get_channel_map(
AVCodecContext *avctx, int mapping_family, const uint8_t ** channel_map_result)
{ const uint8_t * channel_map = NULL; int ret;
switch (mapping_family) { case -1:
ret = libopus_check_max_channels(avctx, 8); if (ret == 0) {
ret = libopus_check_vorbis_layout(avctx, mapping_family); /* Channels do not need to be reordered. */
}
break; case 0:
ret = libopus_check_max_channels(avctx, 2); if (ret == 0) {
ret = libopus_check_vorbis_layout(avctx, mapping_family);
} break; case 1: /* Opus expects channels to be in Vorbis order. */
ret = libopus_check_max_channels(avctx, 8); if (ret == 0) {
ret = libopus_check_vorbis_layout(avctx, mapping_family);
channel_map = ff_vorbis_channel_layout_offsets[avctx->ch_layout.nb_channels - 1];
} break; case 255:
ret = libopus_check_max_channels(avctx, 254); break; default:
av_log(avctx, AV_LOG_WARNING, "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
mapping_family);
ret = 0;
}
*channel_map_result = channel_map; return ret;
}
static av_cold int libopus_encode_init(AVCodecContext *avctx)
{
LibopusEncContext *opus = avctx->priv_data;
OpusMSEncoder *enc;
uint8_t libopus_channel_mapping[255]; int ret = OPUS_OK; int channels = avctx->ch_layout.nb_channels; int av_ret; int coupled_stream_count, header_size, frame_size; int mapping_family;
frame_size = opus->opts.frame_duration * 48000 / 1000; switch (frame_size) { case 120: case 240: if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
av_log(avctx, AV_LOG_WARNING, "LPC mode cannot be used with a frame duration of less " "than 10ms. Enabling restricted low-delay mode.\n" "Use a longer frame duration if this is not what you want.\n"); /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
* RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY; case 480: case 960: case 1920: case 2880: #ifdef OPUS_FRAMESIZE_120_MS case 3840: case 4800: case 5760: #endif
opus->opts.packet_size =
avctx->frame_size = frame_size * avctx->sample_rate / 48000; break; default:
av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n" "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40" #ifdef OPUS_FRAMESIZE_120_MS ", 60, 80, 100 or 120.\n", #else " or 60.\n", #endif
opus->opts.frame_duration); return AVERROR(EINVAL);
}
if (avctx->compression_level < 0 || avctx->compression_level > 10) {
av_log(avctx, AV_LOG_WARNING, "Compression level must be in the range 0 to 10. " "Defaulting to 10.\n");
opus->opts.complexity = 10;
} else {
opus->opts.complexity = avctx->compression_level;
}
if (avctx->cutoff) { switch (avctx->cutoff) { case 4000:
opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND; break; case 6000:
opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; break; case 8000:
opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND; break; case 12000:
opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; break; case 20000:
opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND; break; default:
av_log(avctx, AV_LOG_WARNING, "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n" "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
avctx->cutoff);
avctx->cutoff = 0;
}
}
/* Channels may need to be reordered to match opus mapping. */
av_ret = libopus_validate_layout_and_get_channel_map(avctx, opus->opts.mapping_family,
&opus->encoder_channel_map); if (av_ret) { return av_ret;
}
if (opus->opts.mapping_family == -1) { /* By default, use mapping family 1 for the header but use the older
* libopus multistream API to avoid surround masking. */
/* Set the mapping family so that the value is correct in the header */
mapping_family = channels > 2 ? 1 : 0;
coupled_stream_count = opus_coupled_streams[channels - 1];
opus->stream_count = channels - coupled_stream_count;
memcpy(libopus_channel_mapping,
opus_vorbis_channel_map[channels - 1],
channels * sizeof(*libopus_channel_mapping));
enc = opus_multistream_encoder_create(
avctx->sample_rate, channels, opus->stream_count,
coupled_stream_count,
libavcodec_libopus_channel_map[channels - 1],
opus->opts.application, &ret);
} else { /* Use the newer multistream API. The encoder will set the channel * mapping and coupled stream counts to its internal defaults and will
* use surround masking analysis to save bits. */
mapping_family = opus->opts.mapping_family;
enc = opus_multistream_surround_encoder_create(
avctx->sample_rate, channels, mapping_family,
&opus->stream_count, &coupled_stream_count, libopus_channel_mapping,
opus->opts.application, &ret);
}
if (ret != OPUS_OK) {
av_log(avctx, AV_LOG_ERROR, "Failed to create encoder: %s\n", opus_strerror(ret)); return ff_opus_error_to_averror(ret);
}
if (!avctx->bit_rate) { /* Sane default copied from opusenc */
avctx->bit_rate = 64000 * opus->stream_count +
32000 * coupled_stream_count;
av_log(avctx, AV_LOG_WARNING, "No bit rate set. Defaulting to %"PRId64" bps.\n", avctx->bit_rate);
}
if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * channels) {
av_log(avctx, AV_LOG_ERROR, "The bit rate %"PRId64" bps is unsupported. " "Please choose a value between 500 and %d.\n", avctx->bit_rate,
256000 * channels);
ret = AVERROR(EINVAL); goto fail;
}
ret = libopus_configure_encoder(avctx, enc, &opus->opts); if (ret != OPUS_OK) {
ret = ff_opus_error_to_averror(ret); goto fail;
}
/* Header includes channel mapping table if and only if mapping family is NOT 0 */
header_size = 19 + (mapping_family == 0 ? 0 : 2 + channels);
avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE); if (!avctx->extradata) {
av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
ret = AVERROR(ENOMEM); goto fail;
}
avctx->extradata_size = header_size;
opus->samples = av_calloc(frame_size, channels *
av_get_bytes_per_sample(avctx->sample_fmt)); if (!opus->samples) {
av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
ret = AVERROR(ENOMEM); goto fail;
}
ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->initial_padding)); if (ret != OPUS_OK)
av_log(avctx, AV_LOG_WARNING, "Unable to get number of lookahead samples: %s\n",
opus_strerror(ret));
/* Maximum packet size taken from opusenc in opus-tools. 120ms packets * consist of 6 frames in one packet. The maximum frame size is 1275
* bytes along with the largest possible packet header of 7 bytes. */ if ((ret = ff_alloc_packet(avctx, avpkt, (1275 * 6 + 7) * opus->stream_count)) < 0) return ret;
if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
ret = opus_multistream_encode_float(opus->enc, (constfloat *)audio,
opus->opts.packet_size,
avpkt->data, avpkt->size); else
ret = opus_multistream_encode(opus->enc, (const opus_int16 *)audio,
opus->opts.packet_size,
avpkt->data, avpkt->size);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.