From: Justin Ruggles <[email protected]>

---
 Changelog                |    1 +
 configure                |    3 +-
 doc/general.texi         |    2 +-
 libavcodec/Makefile      |    1 +
 libavcodec/allcodecs.c   |    2 +-
 libavcodec/libspeexenc.c |  306 ++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/version.h     |    2 +-
 7 files changed, 313 insertions(+), 4 deletions(-)
 create mode 100644 libavcodec/libspeexenc.c

diff --git a/Changelog b/Changelog
index a70a3db..fe84683 100644
--- a/Changelog
+++ b/Changelog
@@ -52,6 +52,7 @@ easier to use. The changes are:
 - VC-1 interlaced decoding
 - lut, lutrgb, and lutyuv filters
 - boxblur filter
+- Speex encoding via libspeex
 
 
 version 0.7:
diff --git a/configure b/configure
index a036873..1860dc8 100755
--- a/configure
+++ b/configure
@@ -178,7 +178,7 @@ External library support:
   --enable-libopenjpeg     enable JPEG 2000 decoding via OpenJPEG [no]
   --enable-librtmp         enable RTMP[E] support via librtmp [no]
   --enable-libschroedinger enable Dirac support via libschroedinger [no]
-  --enable-libspeex        enable Speex decoding via libspeex [no]
+  --enable-libspeex        enable Speex support via libspeex [no]
   --enable-libtheora       enable Theora encoding via libtheora [no]
   --enable-libvo-aacenc    enable AAC encoding via libvo-aacenc [no]
   --enable-libvo-amrwbenc  enable AMR-WB encoding via libvo-amrwbenc [no]
@@ -1406,6 +1406,7 @@ libopenjpeg_decoder_deps="libopenjpeg"
 libschroedinger_decoder_deps="libschroedinger"
 libschroedinger_encoder_deps="libschroedinger"
 libspeex_decoder_deps="libspeex"
+libspeex_encoder_deps="libspeex"
 libtheora_encoder_deps="libtheora"
 libvo_aacenc_encoder_deps="libvo_aacenc"
 libvo_amrwbenc_encoder_deps="libvo_amrwbenc"
diff --git a/doc/general.texi b/doc/general.texi
index 2c463cb..8090e1c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -681,7 +681,7 @@ following image formats are supported:
     @tab Used in Sierra VMD files.
 @item Smacker audio          @tab     @tab  X
 @item SMPTE 302M AES3 audio  @tab     @tab  X
-@item Speex                  @tab     @tab  E
+@item Speex                  @tab  E  @tab  E
     @tab supported through external library libspeex
 @item True Audio (TTA)       @tab     @tab  X
 @item TrueHD                 @tab     @tab  X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 4c3eca3..4dde2e5 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -575,6 +575,7 @@ OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER)    += 
libschroedingerenc.o \
                                              libschroedinger.o    \
                                              libdirac_libschro.o
 OBJS-$(CONFIG_LIBSPEEX_DECODER)           += libspeexdec.o
+OBJS-$(CONFIG_LIBSPEEX_ENCODER)           += libspeexenc.o
 OBJS-$(CONFIG_LIBTHEORA_ENCODER)          += libtheoraenc.o
 OBJS-$(CONFIG_LIBVO_AACENC_ENCODER)       += libvo-aacenc.o mpeg4audio.o
 OBJS-$(CONFIG_LIBVO_AMRWBENC_ENCODER)     += libvo-amrwbenc.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fdb0a9c..2f8a38b 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -369,7 +369,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (LIBOPENCORE_AMRWB, libopencore_amrwb);
     REGISTER_DECODER (LIBOPENJPEG, libopenjpeg);
     REGISTER_ENCDEC  (LIBSCHROEDINGER, libschroedinger);
-    REGISTER_DECODER (LIBSPEEX, libspeex);
+    REGISTER_ENCDEC  (LIBSPEEX, libspeex);
     REGISTER_ENCODER (LIBTHEORA, libtheora);
     REGISTER_ENCODER (LIBVO_AACENC, libvo_aacenc);
     REGISTER_ENCODER (LIBVO_AMRWBENC, libvo_amrwbenc);
diff --git a/libavcodec/libspeexenc.c b/libavcodec/libspeexenc.c
new file mode 100644
index 0000000..627f588
--- /dev/null
+++ b/libavcodec/libspeexenc.c
@@ -0,0 +1,306 @@
+/*
+ * Copyright (C) 2009 Justin Ruggles
+ * Copyright (c) 2009 Xuggle Incorporated
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * libspeex Speex audio encoder
+ *
+ * Usage Guide
+ * This explains the values that need to be set prior to initialization in
+ * order to control various encoding parameters.
+ *
+ * Channels
+ *     Speex only supports mono or stereo, so avctx->channels must be set to
+ *     1 or 2.
+ *
+ * Sample Rate / Encoding Mode
+ *     Speex has 3 modes, each of which uses a specific sample rate.
+ *         narrowband     :  8 kHz
+ *         wideband       : 16 kHz
+ *         ultra-wideband : 32 kHz
+ *     avctx->sample_rate must be set to one of these 3 values.  This will be
+ *     used to set the encoding mode.
+ *
+ * Rate Control
+ *     VBR mode is turned on by setting CODEC_FLAG_QSCALE in avctx->flags.
+ *     avctx->global_quality is used to set the encoding quality.
+ *     For CBR mode, avctx->bit_rate is used to set the constant bitrate.
+ *     Approx. Bitrate Range:
+ *         narrowband     : 2400 - 25600 bps
+ *         wideband       : 4000 - 43200 bps
+ *         ultra-wideband : 4400 - 45200 bps
+ *
+ * Complexity
+ *     Encoding complexity is controlled by setting avctx->compression_level.
+ *     The valid range is 0 to 10.  A higher setting gives generally better
+ *     quality at the expense of encoding speed.  This does not affect the
+ *     bit rate.
+ *
+ * Frames-per-Packet
+ *     The encoder defaults to using 1 frame-per-packet.  However, it is
+ *     sometimes desirable to use multiple frames-per-packet to reduce the
+ *     amount of container overhead.  This can be done by setting
+ *     avctx->frame_size to a multiple of a single frame.  Each encoding mode
+ *     uses a different number of samples per frame.
+ *         narrowband     : 160 samples
+ *         wideband       : 320 samples
+ *         ultra-wideband : 640 samples
+ *     For example, if you want to set frames-per-packet to 5 in wideband mode,
+ *     you need to set avctx->frame_size to 1600.
+ */
+
+#include "avcodec.h"
+#include <speex/speex.h>
+#include <speex/speex_header.h>
+#include <speex/speex_stereo.h>
+
+typedef struct {
+    SpeexBits bits;             ///< libspeex bitwriter context
+    SpeexHeader header;         ///< libspeex header struct
+    void *enc_state;            ///< libspeex encoder state
+    float vbr_quality;          ///< VBR quality 0.0 to 10.0
+} LibSpeexEncContext;
+
+static av_cold void print_enc_params(AVCodecContext *avctx,
+                                     LibSpeexEncContext *s)
+{
+    const char *mode_str = "unknown";
+
+    av_log(avctx, AV_LOG_DEBUG, "channels: %d\n", avctx->channels);
+    switch (s->header.mode) {
+    case SPEEX_MODEID_NB:  mode_str = "narrowband";     break;
+    case SPEEX_MODEID_WB:  mode_str = "wideband";       break;
+    case SPEEX_MODEID_UWB: mode_str = "ultra-wideband"; break;
+    }
+    av_log(avctx, AV_LOG_DEBUG, "mode: %s\n", mode_str);
+    if (s->header.vbr) {
+        av_log(avctx, AV_LOG_DEBUG, "rate control: VBR\n");
+        av_log(avctx, AV_LOG_DEBUG, "  quality: %f\n", s->vbr_quality);
+    } else {
+        av_log(avctx, AV_LOG_DEBUG, "rate control: CBR\n");
+        av_log(avctx, AV_LOG_DEBUG, "  bitrate: %d bps\n", avctx->bit_rate);
+    }
+    av_log(avctx, AV_LOG_DEBUG, "complexity: %d\n", avctx->compression_level);
+    av_log(avctx, AV_LOG_DEBUG, "frame size: %"PRId32" samples\n",
+           s->header.frame_size);
+    av_log(avctx, AV_LOG_DEBUG, "frames per packet: %"PRId32"\n",
+           s->header.frames_per_packet);
+    av_log(avctx, AV_LOG_DEBUG, "packet size: %d\n", avctx->frame_size);
+}
+
+static av_cold int encode_init(AVCodecContext *avctx)
+{
+    LibSpeexEncContext *s = avctx->priv_data;
+    const SpeexMode *mode;
+    uint8_t *header_data;
+    int header_size;
+    int32_t complexity;
+
+    /* channels */
+    if (avctx->channels < 1 || avctx->channels > 2) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid channels (%d). Only stereo and "
+               "mono are supported\n", avctx->channels);
+        return AVERROR(EINVAL);
+    }
+
+    /* sample rate and encoding mode */
+    switch (avctx->sample_rate) {
+    case  8000: mode = &speex_nb_mode;  break;
+    case 16000: mode = &speex_wb_mode;  break;
+    case 32000: mode = &speex_uwb_mode; break;
+    default:
+        av_log(avctx, AV_LOG_ERROR, "Sample rate of %d Hz is not supported. "
+               "Resample to 8, 16, or 32 kHz.\n", avctx->sample_rate);
+        return AVERROR(EINVAL);
+    }
+
+    /* initialize libspeex */
+    s->enc_state = speex_encoder_init(mode);
+    if (!s->enc_state) {
+        av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex\n");
+        return -1;
+    }
+    speex_init_header(&s->header, avctx->sample_rate, avctx->channels, mode);
+
+    /* rate control method and parameters */
+    if (avctx->flags & CODEC_FLAG_QSCALE) {
+        /* VBR */
+        s->header.vbr = 1;
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR, &s->header.vbr);
+        s->vbr_quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
+                                  0.0f, 10.0f);
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR_QUALITY, 
&s->vbr_quality);
+        avctx->bit_rate = 0;
+    } else {
+        /* CBR */
+        s->header.bitrate = avctx->bit_rate;
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE, &s->header.bitrate);
+        speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, &s->header.bitrate);
+        /* stereo side information adds about 800 bps to the base bitrate */
+        avctx->bit_rate = s->header.bitrate + (avctx->channels == 2 ? 800 : 0);
+    }
+
+    /* set encoding complexity */
+    if (avctx->compression_level > FF_COMPRESSION_DEFAULT) {
+        complexity = av_clip(avctx->compression_level, 0, 10);
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
+    }
+    speex_encoder_ctl(s->enc_state, SPEEX_GET_COMPLEXITY, &complexity);
+    avctx->compression_level = complexity;
+
+    /* set packet size */
+    s->header.frames_per_packet = 1;
+    if (avctx->frame_size > 0) {
+        /* libspeex doesn't fail cleanly if frames_per_packet is too high, so
+           we're limiting it to 100 */
+        s->header.frames_per_packet = av_clip(avctx->frame_size /
+                                              s->header.frame_size, 1, 100);
+    }
+    avctx->frame_size = s->header.frame_size * s->header.frames_per_packet;
+
+    /* create header packet bytes from header struct */
+    /* note: libspeex allocates the memory for header_data, which is freed
+             below with speex_header_free() */
+    header_data = speex_header_to_packet(&s->header, &header_size);
+
+    /* allocate extradata and coded_frame */
+    avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
+    avctx->coded_frame = avcodec_alloc_frame();
+    if (!avctx->extradata || !avctx->coded_frame) {
+        speex_header_free(header_data);
+        speex_encoder_destroy(s->enc_state);
+        av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
+        return AVERROR(ENOMEM);
+    }
+    avctx->coded_frame->key_frame = 1;
+
+    /* copy header packet to extradata */
+    memcpy(avctx->extradata, header_data, header_size);
+    avctx->extradata_size = header_size;
+    speex_header_free(header_data);
+
+    /* init libspeex bitwriter */
+    speex_bits_init(&s->bits);
+
+    print_enc_params(avctx, s);
+    return 0;
+}
+
+static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size,
+                        void *data)
+{
+    LibSpeexEncContext *s = avctx->priv_data;
+    void *samples = data;
+    int nframes, i;
+
+    if (!avctx->frame_size)
+        return 0;
+
+    /* handle last packet, which may have fewer frames-per-packet and/or
+       fewer samples in the last frame */
+    nframes = s->header.frames_per_packet;
+    if (avctx->frame_size < nframes * s->header.frame_size) {
+        nframes = (avctx->frame_size + s->header.frame_size - 1) /
+                  s->header.frame_size;
+        if (avctx->frame_size != s->header.frame_size * nframes) {
+            /* allocate new buffer to pad last frame */
+            int new_samples_size;
+            avctx->frame_size = nframes * s->header.frame_size;
+            new_samples_size  = avctx->frame_size * avctx->channels *
+                                (avctx->sample_fmt == SAMPLE_FMT_FLT ?
+                                sizeof(float) : sizeof(int16_t));
+            samples = av_mallocz(new_samples_size);
+            if (!samples)
+                return AVERROR(ENOMEM);
+            memcpy(samples, data, new_samples_size);
+        }
+    }
+
+    /* encode Speex frames */
+    speex_bits_reset(&s->bits);
+    if (avctx->sample_fmt == SAMPLE_FMT_FLT) {
+        float *samples_flt = samples;
+
+        /* scale floating point samples to 16-bit range as required by 
libspeex */
+        if (avctx->sample_fmt == SAMPLE_FMT_FLT)
+            for (i = 0; i < avctx->frame_size * avctx->channels; i++)
+                samples_flt[i] *= 32767.0;
+
+        for (i = 0; i < nframes; i++) {
+            if (avctx->channels == 2)
+                speex_encode_stereo(samples_flt, s->header.frame_size, 
&s->bits);
+            speex_encode(s->enc_state, samples_flt, &s->bits);
+            samples_flt += s->header.frame_size * avctx->channels;
+        }
+    } else {
+        int16_t *samples_s16 = samples;
+
+        for (i = 0; i < nframes; i++) {
+            if (avctx->channels == 2)
+                speex_encode_stereo_int(samples_s16, s->header.frame_size, 
&s->bits);
+            speex_encode_int(s->enc_state, samples_s16, &s->bits);
+            samples_s16 += s->header.frame_size * avctx->channels;
+        }
+    }
+
+    /* add extra terminator codes for unused frames in last packet */
+    for (; i < s->header.frames_per_packet; i++)
+        speex_bits_pack(&s->bits, 15, 5);
+
+    /* free samples for small last frame if needed */
+    if (samples != data)
+        av_freep(&samples);
+
+    /* write output */
+    if (buf_size > speex_bits_nbytes(&s->bits)) {
+        return speex_bits_write(&s->bits, frame, buf_size);
+    } else {
+        av_log(avctx, AV_LOG_ERROR, "output buffer too small");
+        return -1;
+    }
+}
+
+static av_cold int encode_close(AVCodecContext *avctx)
+{
+    LibSpeexEncContext *s = avctx->priv_data;
+
+    speex_bits_destroy(&s->bits);
+    speex_encoder_destroy(s->enc_state);
+
+    av_freep(&avctx->coded_frame);
+    av_freep(&avctx->extradata);
+
+    return 0;
+}
+
+AVCodec ff_libspeex_encoder = {
+    .name = "libspeex",
+    .type = AVMEDIA_TYPE_AUDIO,
+    .id = CODEC_ID_SPEEX,
+    .priv_data_size = sizeof(LibSpeexEncContext),
+    .init = encode_init,
+    .encode = encode_frame,
+    .close = encode_close,
+    .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
+    .sample_fmts = (const enum SampleFormat[]){ SAMPLE_FMT_S16, SAMPLE_FMT_FLT,
+                                                SAMPLE_FMT_NONE },
+    .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index ee647be..c1157ec 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -21,7 +21,7 @@
 #define AVCODEC_VERSION_H
 
 #define LIBAVCODEC_VERSION_MAJOR 53
-#define LIBAVCODEC_VERSION_MINOR  12
+#define LIBAVCODEC_VERSION_MINOR  13
 #define LIBAVCODEC_VERSION_MICRO  2
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
-- 
1.7.6.1

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to