[FFmpeg-devel] [PATCH] libshine fixed point MP3 encoding support

Nicolas George nicolas.george at normalesup.org
Mon Jul 30 11:02:13 CEST 2012


Le tridi 13 thermidor, an CCXX, Paul B Mahol a écrit :
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
> This add support for liquidsoap libshine version,
> available at https://github.com/savonet/shine
> 
> Handling of last frame is hacky due library limitations and does not work as it should.

That is frequent, I do not think there is cause to worry.

> ___
> 
>  configure              |    5 ++
>  libavcodec/Makefile    |    1 +
>  libavcodec/allcodecs.c |    1 +
>  libavcodec/libshine.c  |  140 ++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 147 insertions(+), 0 deletions(-)
>  create mode 100644 libavcodec/libshine.c
> 
> diff --git a/configure b/configure
> index 1445b82..7eac069 100755
> --- a/configure
> +++ b/configure
> @@ -197,6 +197,7 @@ External library support:
>    --enable-libpulse        enable Pulseaudio input via libpulse [no]
>    --enable-librtmp         enable RTMP[E] support via librtmp [no]
>    --enable-libschroedinger enable Dirac de/encoding via libschroedinger [no]
> +  --enable-libshine        enable fixed-point MP3 encoding via libshine [no]
>    --enable-libspeex        enable Speex de/encoding via libspeex [no]
>    --enable-libstagefright-h264  enable H.264 decoding via libstagefright [no]
>    --enable-libtheora       enable Theora encoding via libtheora [no]
> @@ -1093,6 +1094,7 @@ CONFIG_LIST="
>      libpulse
>      librtmp
>      libschroedinger
> +    libshine
>      libspeex
>      libstagefright_h264
>      libtheora
> @@ -1667,6 +1669,7 @@ libopenjpeg_encoder_deps="libopenjpeg"
>  libopus_decoder_deps="libopus"
>  libschroedinger_decoder_deps="libschroedinger"
>  libschroedinger_encoder_deps="libschroedinger"
> +libshine_encoder_deps="libshine"
>  libspeex_decoder_deps="libspeex"
>  libspeex_encoder_deps="libspeex"
>  libstagefright_h264_decoder_deps="libstagefright_h264"
> @@ -3421,6 +3424,7 @@ enabled libopus    && require_pkg_config opus opus_multistream.h opus_multistrea
>  enabled libpulse && require_pkg_config libpulse-simple pulse/simple.h pa_simple_new
>  enabled librtmp    && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
>  enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init
> +enabled libshine   && require_pkg_config shine shine/layer3.h L3_encode_frame
>  enabled libspeex   && require  libspeex speex/speex.h speex_decoder_init -lspeex
>  enabled libstagefright_h264  && require_cpp libstagefright_h264 "binder/ProcessState.h media/stagefright/MetaData.h
>      media/stagefright/MediaBufferGroup.h media/stagefright/MediaDebug.h media/stagefright/MediaDefs.h
> @@ -3808,6 +3812,7 @@ echo "libopus enabled           ${libopus-no}"
>  echo "libpulse enabled          ${libpulse-no}"
>  echo "librtmp enabled           ${librtmp-no}"
>  echo "libschroedinger enabled   ${libschroedinger-no}"
> +echo "libshine enabled          ${libshine-no}"
>  echo "libspeex enabled          ${libspeex-no}"
>  echo "libstagefright-h264 enabled    ${libstagefright_h264-no}"
>  echo "libtheora enabled         ${libtheora-no}"
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index b4138e8..44c15fc 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -690,6 +690,7 @@ OBJS-$(CONFIG_LIBSCHROEDINGER_DECODER)    += libschroedingerdec.o \
>                                               libschroedinger.o
>  OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER)    += libschroedingerenc.o \
>                                               libschroedinger.o
> +OBJS-$(CONFIG_LIBSHINE_ENCODER)           += libshine.o
>  OBJS-$(CONFIG_LIBSPEEX_DECODER)           += libspeexdec.o
>  OBJS-$(CONFIG_LIBSPEEX_ENCODER)           += libspeexenc.o audio_frame_queue.o
>  OBJS-$(CONFIG_LIBSTAGEFRIGHT_H264_DECODER)+= libstagefright.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index 6bcc3c2..d87d171 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -424,6 +424,7 @@ void avcodec_register_all(void)
>      REGISTER_ENCDEC  (LIBGSM, libgsm);
>      REGISTER_ENCDEC  (LIBGSM_MS, libgsm_ms);
>      REGISTER_ENCDEC  (LIBILBC, libilbc);
> +    REGISTER_ENCODER (LIBSHINE, libshine);
>      REGISTER_ENCODER (LIBMP3LAME, libmp3lame);
>      REGISTER_ENCDEC  (LIBOPENCORE_AMRNB, libopencore_amrnb);
>      REGISTER_DECODER (LIBOPENCORE_AMRWB, libopencore_amrwb);
> diff --git a/libavcodec/libshine.c b/libavcodec/libshine.c
> new file mode 100644
> index 0000000..42fa760
> --- /dev/null
> +++ b/libavcodec/libshine.c
> @@ -0,0 +1,140 @@
> +/*
> + * Interface to libshine for mp3 encoding
> + * Copyright (c) 2012 Paul B Mahol
> + *
> + * 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 <shine/layer3.h>

> +#include <libavutil/intreadwrite.h>

#include "libavutil/intreadwrite.h"

> +
> +#include "avcodec.h"
> +#include "audio_frame_queue.h"
> +#include "internal.h"
> +#include "mpegaudio.h"
> +#include "mpegaudiodecheader.h"
> +
> +#define BUFFER_SIZE (4096 * 4)
> +
> +typedef struct SHINEContext {
> +    shine_config_t  config;
> +    shine_t         shine;
> +    uint8_t         buffer[BUFFER_SIZE];
> +    int             buffer_index;
> +    int16_t         zero[2][samp_per_frame];
> +    AudioFrameQueue afq;
> +} SHINEContext;
> +
> +static av_cold int shine_encode_init(AVCodecContext *avctx)
> +{
> +    SHINEContext *s = avctx->priv_data;
> +
> +    L3_set_config_mpeg_defaults(&s->config.mpeg);
> +    if (avctx->bit_rate)
> +        s->config.mpeg.bitr = avctx->bit_rate / 1000;

> +    if (L3_find_bitrate_index(s->config.mpeg.bitr) < 1) {

"< 0"? Or is there any reason to exclude 32 kbps?

> +        av_log(avctx, AV_LOG_ERROR, "invalid bitrate\n");
> +        return AVERROR(EINVAL);
> +    }
> +    s->config.mpeg.mode = avctx->channels == 2 ? STEREO : MONO;
> +    s->config.wave.samplerate = avctx->sample_rate;
> +    s->config.wave.channels   = avctx->channels == 2 ? PCM_STEREO : PCM_MONO;
> +    s->shine = L3_initialise(&s->config);
> +    if (!s->shine)
> +        return AVERROR(ENOMEM);
> +    avctx->frame_size = samp_per_frame;
> +    ff_af_queue_init(avctx, &s->afq);
> +    return 0;
> +}
> +
> +static int shine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
> +                              const AVFrame *frame, int *got_packet_ptr)
> +{
> +    SHINEContext *s = avctx->priv_data;
> +    MPADecodeHeader hdr;
> +    unsigned char *data;
> +    int16_t *input;
> +    long written;
> +    int ret, len;
> +

> +    input = frame ? frame->data[0] : &s->zero;

No call to L3_flush()?

> +    data = L3_encode_frame(s->shine, input, &written);
> +    if (written) {
> +        if (s->buffer_index + written > BUFFER_SIZE) {
> +            av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
> +            return AVERROR_BUG;
> +        }
> +        memcpy(s->buffer + s->buffer_index, data, written);
> +        s->buffer_index += written;
> +    }
> +    if (frame) {

> +        if ((ret = ff_af_queue_add(&s->afq, frame) < 0))

Wrong parentheses: (ret = fn()) < 0.

> +            return ret;
> +    }
> +    if (s->buffer_index < 4 || !s->afq.frame_count)
> +        return 0;
> +    if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
> +        av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
> +        return -1;
> +    }
> +    len = hdr.frame_size;
> +    if (len <= s->buffer_index) {
> +        if ((ret = ff_alloc_packet2(avctx, avpkt, len)))
> +            return ret;
> +        memcpy(avpkt->data, s->buffer, len);
> +        s->buffer_index -= len;
> +        memmove(s->buffer, s->buffer + len, s->buffer_index);
> +
> +        ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
> +                           &avpkt->duration);
> +
> +        avpkt->size = len;
> +        *got_packet_ptr = 1;
> +    }
> +    return 0;
> +}
> +
> +static av_cold int shine_encode_close(AVCodecContext *avctx)
> +{
> +    SHINEContext *s = avctx->priv_data;
> +
> +    ff_af_queue_close(&s->afq);
> +    L3_close(s->shine);
> +    return 0;
> +}
> +
> +static const int libshine_sample_rates[] = {
> +    44100, 48000, 32000, 0
> +};
> +
> +AVCodec ff_libshine_encoder = {
> +    .name                  = "libshine",
> +    .type                  = AVMEDIA_TYPE_AUDIO,
> +    .id                    = CODEC_ID_MP3,
> +    .priv_data_size        = sizeof(SHINEContext),
> +    .init                  = shine_encode_init,
> +    .encode2               = shine_encode_frame,
> +    .close                 = shine_encode_close,
> +    .capabilities          = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,

You never check frame->nb_samples: are you sure about SMALL_LAST_FRAME?

> +    .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
> +                                                            AV_SAMPLE_FMT_NONE },
> +    .supported_samplerates = libshine_sample_rates,
> +    .channel_layouts       = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
> +                                                  AV_CH_LAYOUT_STEREO,
> +                                                  0 },
> +    .long_name             = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"),
> +};

Great job of making good of a poor API, thanks.

Regards,

-- 
  Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120730/af3f2f1f/attachment.asc>


More information about the ffmpeg-devel mailing list