aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2012-10-02 14:38:01 +0200
committerMichael Niedermayer <michaelni@gmx.at>2012-10-02 14:58:19 +0200
commit82db8ee3211014a38db6b8cae03f1c3246938eee (patch)
treeacac8aa9b5b38bcbcb5c34a77f076adee3c28465
parentf7f5370b4b413c05f6ed848bffb85d28c5f44b9c (diff)
parentfd41cb43702498948ff14ba8c284fd5c15fc729d (diff)
downloadffmpeg-82db8ee3211014a38db6b8cae03f1c3246938eee.tar.gz
Merge commit 'fd41cb43702498948ff14ba8c284fd5c15fc729d'
* commit 'fd41cb43702498948ff14ba8c284fd5c15fc729d': avconv: improve sample format negotiation for decoder request Opus encoder using libopus mpegts: Drop pointless casting of hex_dump_debug arguments avformat: const correctness for av_hex_dump / av_hex_dump_log wmadec: Adjust debug printf argument length modifier Conflicts: Changelog ffmpeg.c libavcodec/libopusdec.c libavcodec/version.h Merged-by: Michael Niedermayer <michaelni@gmx.at>
-rw-r--r--Changelog1
-rwxr-xr-xconfigure1
-rw-r--r--doc/general.texi2
-rw-r--r--libavcodec/Makefile5
-rw-r--r--libavcodec/allcodecs.c2
-rw-r--r--libavcodec/libopus.c48
-rw-r--r--libavcodec/libopus.h27
-rw-r--r--libavcodec/libopusdec.c20
-rw-r--r--libavcodec/libopusenc.c421
-rw-r--r--libavcodec/version.h2
-rw-r--r--libavcodec/wmadec.c2
-rw-r--r--libavformat/avformat.h4
-rw-r--r--libavformat/mpegts.c6
-rw-r--r--libavformat/utils.c7
14 files changed, 518 insertions, 30 deletions
diff --git a/Changelog b/Changelog
index d65674f5ea..1a78f0f953 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
version next:
- stream disposition information printing in ffprobe
- filter for loudness analysis following EBU R128
+- Opus encoder using libopus
version 1.0:
diff --git a/configure b/configure
index 829b3ed653..65394080da 100755
--- a/configure
+++ b/configure
@@ -1768,6 +1768,7 @@ libopencore_amrwb_decoder_deps="libopencore_amrwb"
libopenjpeg_decoder_deps="libopenjpeg"
libopenjpeg_encoder_deps="libopenjpeg"
libopus_decoder_deps="libopus"
+libopus_encoder_deps="libopus"
libschroedinger_decoder_deps="libschroedinger"
libschroedinger_encoder_deps="libschroedinger"
libspeex_decoder_deps="libspeex"
diff --git a/doc/general.texi b/doc/general.texi
index b5f4c99d76..659226a156 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -813,7 +813,7 @@ following image formats are supported:
@item Musepack SV7 @tab @tab X
@item Musepack SV8 @tab @tab X
@item Nellymoser Asao @tab X @tab X
-@item Opus @tab @tab E
+@item Opus @tab E @tab E
@tab supported through external library libopus
@item PCM A-law @tab X @tab X
@item PCM mu-law @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 34737f27bb..8471b63f9d 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -657,7 +657,10 @@ OBJS-$(CONFIG_LIBOPENCORE_AMRNB_ENCODER) += libopencore-amr.o \
OBJS-$(CONFIG_LIBOPENCORE_AMRWB_DECODER) += libopencore-amr.o
OBJS-$(CONFIG_LIBOPENJPEG_DECODER) += libopenjpegdec.o
OBJS-$(CONFIG_LIBOPENJPEG_ENCODER) += libopenjpegenc.o
-OBJS-$(CONFIG_LIBOPUS_DECODER) += libopusdec.o vorbis_data.o
+OBJS-$(CONFIG_LIBOPUS_DECODER) += libopusdec.o libopus.o \
+ vorbis_data.o
+OBJS-$(CONFIG_LIBOPUS_ENCODER) += libopusenc.o libopus.o \
+ vorbis_data.o audio_frame_queue.o
OBJS-$(CONFIG_LIBSCHROEDINGER_DECODER) += libschroedingerdec.o \
libschroedinger.o
OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER) += libschroedingerenc.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index cba953f825..33fe1ea2e1 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -436,7 +436,7 @@ void avcodec_register_all(void)
REGISTER_ENCDEC (LIBOPENCORE_AMRNB, libopencore_amrnb);
REGISTER_DECODER (LIBOPENCORE_AMRWB, libopencore_amrwb);
REGISTER_ENCDEC (LIBOPENJPEG, libopenjpeg);
- REGISTER_DECODER (LIBOPUS, libopus);
+ REGISTER_ENCDEC (LIBOPUS, libopus);
REGISTER_ENCDEC (LIBSCHROEDINGER, libschroedinger);
REGISTER_ENCDEC (LIBSPEEX, libspeex);
REGISTER_DECODER (LIBSTAGEFRIGHT_H264, libstagefright_h264);
diff --git a/libavcodec/libopus.c b/libavcodec/libopus.c
new file mode 100644
index 0000000000..16395c73df
--- /dev/null
+++ b/libavcodec/libopus.c
@@ -0,0 +1,48 @@
+/*
+ * libopus encoder/decoder common code
+ * Copyright (c) 2012 Nicolas George
+ *
+ * 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
+ */
+
+#include <opus_defines.h>
+
+#include "libavutil/common.h"
+#include "libavutil/error.h"
+#include "libopus.h"
+
+int ff_opus_error_to_averror(int err)
+{
+ switch (err) {
+ case OPUS_BAD_ARG:
+ return AVERROR(EINVAL);
+ case OPUS_BUFFER_TOO_SMALL:
+ return AVERROR_UNKNOWN;
+ case OPUS_INTERNAL_ERROR:
+ return AVERROR(EFAULT);
+ case OPUS_INVALID_PACKET:
+ return AVERROR_INVALIDDATA;
+ case OPUS_UNIMPLEMENTED:
+ return AVERROR(ENOSYS);
+ case OPUS_INVALID_STATE:
+ return AVERROR_UNKNOWN;
+ case OPUS_ALLOC_FAIL:
+ return AVERROR(ENOMEM);
+ default:
+ return AVERROR(EINVAL);
+ }
+}
diff --git a/libavcodec/libopus.h b/libavcodec/libopus.h
new file mode 100644
index 0000000000..a8223d1d6f
--- /dev/null
+++ b/libavcodec/libopus.h
@@ -0,0 +1,27 @@
+/*
+ * libopus encoder/decoder common code
+ * Copyright (c) 2012 Nicolas George
+ *
+ * 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
+ */
+
+#ifndef AVCODEC_LIBOPUS_H
+#define AVCODEC_LIBOPUS_H
+
+int ff_opus_error_to_averror(int err);
+
+#endif /* AVCODEC_LIBOPUS_H */
diff --git a/libavcodec/libopusdec.c b/libavcodec/libopusdec.c
index 24488fe9dd..f3cad25f76 100644
--- a/libavcodec/libopusdec.c
+++ b/libavcodec/libopusdec.c
@@ -22,13 +22,13 @@
#include <opus.h>
#include <opus_multistream.h>
-#include "libavutil/common.h"
#include "libavutil/avassert.h"
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "internal.h"
#include "vorbis.h"
#include "mathops.h"
+#include "libopus.h"
struct libopus_context {
OpusMSDecoder *dec;
@@ -39,20 +39,6 @@ struct libopus_context {
#endif
};
-static int opus_error_to_averror(int err)
-{
- switch (err) {
- case OPUS_BAD_ARG: return AVERROR(EINVAL);
- case OPUS_BUFFER_TOO_SMALL: return AVERROR_BUFFER_TOO_SMALL;
- case OPUS_INTERNAL_ERROR: return AVERROR(EFAULT);
- case OPUS_INVALID_PACKET: return AVERROR_INVALIDDATA;
- case OPUS_UNIMPLEMENTED: return AVERROR(ENOSYS);
- case OPUS_INVALID_STATE: return AVERROR_EXTERNAL;
- case OPUS_ALLOC_FAIL: return AVERROR(ENOMEM);
- default: return AVERROR(EINVAL);
- }
-}
-
#define OPUS_HEAD_SIZE 19
static av_cold int libopus_decode_init(AVCodecContext *avc)
@@ -105,7 +91,7 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
if (!opus->dec) {
av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
opus_strerror(ret));
- return opus_error_to_averror(ret);
+ return ff_opus_error_to_averror(ret);
}
#ifdef OPUS_SET_GAIN
@@ -165,7 +151,7 @@ static int libopus_decode(AVCodecContext *avc, void *frame,
if (nb_samples < 0) {
av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
opus_strerror(nb_samples));
- return opus_error_to_averror(nb_samples);
+ return ff_opus_error_to_averror(nb_samples);
}
#ifndef OPUS_SET_GAIN
diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c
new file mode 100644
index 0000000000..1ae92a99c1
--- /dev/null
+++ b/libavcodec/libopusenc.c
@@ -0,0 +1,421 @@
+/*
+ * 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
+ */
+
+#include <opus.h>
+#include <opus_multistream.h>
+
+#include "libavutil/opt.h"
+#include "avcodec.h"
+#include "bytestream.h"
+#include "internal.h"
+#include "libopus.h"
+#include "vorbis.h"
+#include "audio_frame_queue.h"
+
+typedef struct LibopusEncOpts {
+ int vbr;
+ int application;
+ int packet_loss;
+ int complexity;
+ float frame_duration;
+ int packet_size;
+ int max_bandwidth;
+} LibopusEncOpts;
+
+typedef struct LibopusEncContext {
+ AVClass *class;
+ OpusMSEncoder *enc;
+ int stream_count;
+ uint8_t *samples;
+ LibopusEncOpts opts;
+ AudioFrameQueue afq;
+} LibopusEncContext;
+
+static const uint8_t opus_coupled_streams[8] = {
+ 0, 1, 1, 2, 2, 2, 2, 3
+};
+
+/* Opus internal to Vorbis channel order mapping written in the header */
+static const uint8_t opus_vorbis_channel_map[8][8] = {
+ { 0 },
+ { 0, 1 },
+ { 0, 2, 1 },
+ { 0, 1, 2, 3 },
+ { 0, 4, 1, 2, 3 },
+ { 0, 4, 1, 2, 3, 5 },
+ { 0, 4, 1, 2, 3, 5, 6 },
+ { 0, 6, 1, 2, 3, 4, 5, 7 },
+};
+
+/* libav to libopus channel order mapping, passed to libopus */
+static const uint8_t libav_libopus_channel_map[8][8] = {
+ { 0 },
+ { 0, 1 },
+ { 0, 1, 2 },
+ { 0, 1, 2, 3 },
+ { 0, 1, 3, 4, 2 },
+ { 0, 1, 4, 5, 2, 3 },
+ { 0, 1, 5, 6, 2, 4, 3 },
+ { 0, 1, 6, 7, 4, 5, 2, 3 },
+};
+
+static void libopus_write_header(AVCodecContext *avctx, int stream_count,
+ int coupled_stream_count,
+ const uint8_t *channel_mapping)
+{
+ uint8_t *p = avctx->extradata;
+ int channels = avctx->channels;
+
+ bytestream_put_buffer(&p, "OpusHead", 8);
+ bytestream_put_byte(&p, 1); /* Version */
+ bytestream_put_byte(&p, channels);
+ bytestream_put_le16(&p, avctx->delay); /* Lookahead samples at 48kHz */
+ bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
+ bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
+
+ /* Channel mapping */
+ if (channels > 2) {
+ bytestream_put_byte(&p, channels <= 8 ? 1 : 255);
+ bytestream_put_byte(&p, stream_count);
+ bytestream_put_byte(&p, coupled_stream_count);
+ bytestream_put_buffer(&p, channel_mapping, channels);
+ } else {
+ bytestream_put_byte(&p, 0);
+ }
+}
+
+static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
+ LibopusEncOpts *opts)
+{
+ int ret;
+
+ 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));
+
+ 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));
+ }
+
+ return OPUS_OK;
+}
+
+static int av_cold libopus_encode_init(AVCodecContext *avctx)
+{
+ LibopusEncContext *opus = avctx->priv_data;
+ const uint8_t *channel_mapping;
+ OpusMSEncoder *enc;
+ int ret = OPUS_OK;
+ int coupled_stream_count, header_size, frame_size;
+
+ coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
+ opus->stream_count = avctx->channels - coupled_stream_count;
+ channel_mapping = libav_libopus_channel_map[avctx->channels - 1];
+
+ /* FIXME: Opus can handle up to 255 channels. However, the mapping for
+ * anything greater than 8 is undefined. */
+ if (avctx->channels > 8)
+ av_log(avctx, AV_LOG_WARNING,
+ "Channel layout undefined for %d channels.\n", avctx->channels);
+
+ 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 %d bps.\n", avctx->bit_rate);
+ }
+
+ if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
+ av_log(avctx, AV_LOG_ERROR, "The bit rate %d bps is unsupported. "
+ "Please choose a value between 500 and %d.\n", avctx->bit_rate,
+ 256000 * avctx->channels);
+ return AVERROR(EINVAL);
+ }
+
+ 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:
+ 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 or 60.\n",
+ 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;
+ }
+ }
+
+ enc = opus_multistream_encoder_create(avctx->sample_rate, avctx->channels,
+ opus->stream_count,
+ coupled_stream_count,
+ 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);
+ }
+
+ ret = libopus_configure_encoder(avctx, enc, &opus->opts);
+ if (ret != OPUS_OK) {
+ ret = ff_opus_error_to_averror(ret);
+ goto fail;
+ }
+
+ header_size = 19 + (avctx->channels > 2 ? 2 + avctx->channels : 0);
+ avctx->extradata = av_malloc(header_size + FF_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_mallocz(frame_size * avctx->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->delay));
+ if (ret != OPUS_OK)
+ av_log(avctx, AV_LOG_WARNING,
+ "Unable to get number of lookahead samples: %s\n",
+ opus_strerror(ret));
+
+ libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
+ opus_vorbis_channel_map[avctx->channels - 1]);
+
+ ff_af_queue_init(avctx, &opus->afq);
+
+ opus->enc = enc;
+
+ return 0;
+
+fail:
+ opus_multistream_encoder_destroy(enc);
+ av_freep(&avctx->extradata);
+ return ret;
+}
+
+static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
+ const AVFrame *frame, int *got_packet_ptr)
+{
+ LibopusEncContext *opus = avctx->priv_data;
+ const int sample_size = avctx->channels *
+ av_get_bytes_per_sample(avctx->sample_fmt);
+ uint8_t *audio;
+ int ret;
+
+ if (frame) {
+ ff_af_queue_add(&opus->afq, frame);
+ if (frame->nb_samples < opus->opts.packet_size) {
+ audio = opus->samples;
+ memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
+ } else
+ audio = frame->data[0];
+ } else {
+ if (!opus->afq.remaining_samples)
+ return 0;
+ audio = opus->samples;
+ memset(audio, 0, opus->opts.packet_size * sample_size);
+ }
+
+ /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
+ * consist of 3 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(avpkt, (1275 * 3 + 7) * opus->stream_count)) {
+ av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
+ return ret;
+ }
+
+ if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
+ ret = opus_multistream_encode_float(opus->enc, (float *)audio,
+ opus->opts.packet_size,
+ avpkt->data, avpkt->size);
+ else
+ ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
+ opus->opts.packet_size,
+ avpkt->data, avpkt->size);
+
+ if (ret < 0) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Error encoding frame: %s\n", opus_strerror(ret));
+ return ff_opus_error_to_averror(ret);
+ }
+
+ av_shrink_packet(avpkt, ret);
+
+ ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
+ &avpkt->pts, &avpkt->duration);
+
+ *got_packet_ptr = 1;
+
+ return 0;
+}
+
+static int av_cold libopus_encode_close(AVCodecContext *avctx)
+{
+ LibopusEncContext *opus = avctx->priv_data;
+
+ opus_multistream_encoder_destroy(opus->enc);
+
+ ff_af_queue_close(&opus->afq);
+
+ av_freep(&opus->samples);
+ av_freep(&avctx->extradata);
+
+ return 0;
+}
+
+#define OFFSET(x) offsetof(LibopusEncContext, opts.x)
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption libopus_options[] = {
+ { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
+ { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
+ { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
+ { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
+ { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 10.0 }, 2.5, 60.0, FLAGS },
+ { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
+ { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
+ { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
+ { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
+ { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
+ { NULL },
+};
+
+static const AVClass libopus_class = {
+ .class_name = "libopus",
+ .item_name = av_default_item_name,
+ .option = libopus_options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVCodecDefault libopus_defaults[] = {
+ { "b", "0" },
+ { "compression_level", "10" },
+ { NULL },
+};
+
+static const int libopus_sample_rates[] = {
+ 48000, 24000, 16000, 12000, 8000, 0,
+};
+
+AVCodec ff_libopus_encoder = {
+ .name = "libopus",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .id = AV_CODEC_ID_OPUS,
+ .priv_data_size = sizeof(LibopusEncContext),
+ .init = libopus_encode_init,
+ .encode2 = libopus_encode,
+ .close = libopus_encode_close,
+ .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
+ .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
+ AV_SAMPLE_FMT_FLT,
+ AV_SAMPLE_FMT_NONE },
+ .channel_layouts = ff_vorbis_channel_layouts,
+ .supported_samplerates = libopus_sample_rates,
+ .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
+ .priv_class = &libopus_class,
+ .defaults = libopus_defaults,
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 7e0b96b408..1ac16bc7a1 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
#include "libavutil/avutil.h"
#define LIBAVCODEC_VERSION_MAJOR 54
-#define LIBAVCODEC_VERSION_MINOR 62
+#define LIBAVCODEC_VERSION_MINOR 63
#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/libavcodec/wmadec.c b/libavcodec/wmadec.c
index 6bd1a821fd..1bcf0da453 100644
--- a/libavcodec/wmadec.c
+++ b/libavcodec/wmadec.c
@@ -926,7 +926,7 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
samples += s->nb_channels * s->frame_len;
}
- av_dlog(s->avctx, "%d %d %d %d outbytes:%d eaten:%d\n",
+ av_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\n",
s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,
(int8_t *)samples - (int8_t *)data, s->block_align);
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index f7779e7808..1ee44dfd7c 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1813,7 +1813,7 @@ int av_get_output_timestamp(struct AVFormatContext *s, int stream,
*
* @see av_hex_dump_log, av_pkt_dump2, av_pkt_dump_log2
*/
-void av_hex_dump(FILE *f, uint8_t *buf, int size);
+void av_hex_dump(FILE *f, const uint8_t *buf, int size);
/**
* Send a nice hexadecimal dump of a buffer to the log.
@@ -1827,7 +1827,7 @@ void av_hex_dump(FILE *f, uint8_t *buf, int size);
*
* @see av_hex_dump, av_pkt_dump2, av_pkt_dump_log2
*/
-void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size);
+void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size);
/**
* Send a nice dump of a packet to the specified file stream.
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 6316d6178e..aed3cea69c 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -1414,7 +1414,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
int i;
av_dlog(ts->stream, "PMT: len %i\n", section_len);
- hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
+ hex_dump_debug(ts->stream, section, section_len);
p_end = section + section_len - 4;
p = section;
@@ -1553,7 +1553,7 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
AVProgram *program;
av_dlog(ts->stream, "PAT:\n");
- hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
+ hex_dump_debug(ts->stream, section, section_len);
p_end = section + section_len - 4;
p = section;
@@ -1601,7 +1601,7 @@ static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
char *name, *provider_name;
av_dlog(ts->stream, "SDT:\n");
- hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
+ hex_dump_debug(ts->stream, section, section_len);
p_end = section + section_len - 4;
p = section;
diff --git a/libavformat/utils.c b/libavformat/utils.c
index b3298f8fbd..92dc6850ab 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -4146,7 +4146,8 @@ int av_get_frame_filename(char *buf, int buf_size,
return -1;
}
-static void hex_dump_internal(void *avcl, FILE *f, int level, uint8_t *buf, int size)
+static void hex_dump_internal(void *avcl, FILE *f, int level,
+ const uint8_t *buf, int size)
{
int len, i, j, c;
#undef fprintf
@@ -4175,12 +4176,12 @@ static void hex_dump_internal(void *avcl, FILE *f, int level, uint8_t *buf, int
#undef PRINT
}
-void av_hex_dump(FILE *f, uint8_t *buf, int size)
+void av_hex_dump(FILE *f, const uint8_t *buf, int size)
{
hex_dump_internal(NULL, f, 0, buf, size);
}
-void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size)
+void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
{
hex_dump_internal(avcl, NULL, level, buf, size);
}