[FFmpeg-devel] [PATCH] s302m encoder

Michael Niedermayer michaelni at gmx.at
Sat May 18 22:06:55 CEST 2013


On Fri, Apr 19, 2013 at 10:11:56PM +0000, Paul B Mahol wrote:
> From: Darryl Wallace <wallacdj at gmail.com>
> 
> 
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
> 
> TODO: proper frame->nb_samples calculation, current one does not follow specification.
> 
> ---
>  doc/general.texi       |   2 +-
>  libavcodec/Makefile    |   1 +
>  libavcodec/allcodecs.c |   2 +-
>  libavcodec/s302menc.c  | 187 +++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 190 insertions(+), 2 deletions(-)
>  create mode 100644 libavcodec/s302menc.c
> 
> diff --git a/doc/general.texi b/doc/general.texi
> index 39b9360..0b1cade 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -886,7 +886,7 @@ following image formats are supported:
>  @item Sierra VMD audio       @tab     @tab  X
>      @tab Used in Sierra VMD files.
>  @item Smacker audio          @tab     @tab  X
> - at item SMPTE 302M AES3 audio  @tab     @tab  X
> + at item SMPTE 302M AES3 audio  @tab  X  @tab  X
>  @item Sonic                  @tab  X  @tab  X
>      @tab experimental codec
>  @item Sonic lossless         @tab  X  @tab  X
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index ac342b1..59c756d 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -372,6 +372,7 @@ OBJS-$(CONFIG_RV30_DECODER)            += rv30.o rv34.o rv30dsp.o rv34dsp.o
>  OBJS-$(CONFIG_RV40_DECODER)            += rv40.o rv34.o rv34dsp.o rv40dsp.o
>  OBJS-$(CONFIG_SAMI_DECODER)            += samidec.o ass.o
>  OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
> +OBJS-$(CONFIG_S302M_ENCODER)           += s302menc.o
>  OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
>  OBJS-$(CONFIG_SGI_DECODER)             += sgidec.o
>  OBJS-$(CONFIG_SGI_ENCODER)             += sgienc.o rle.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index 19a638c..eeb6324 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -237,7 +237,7 @@ void avcodec_register_all(void)
>      REGISTER_ENCDEC (RV20,              rv20);
>      REGISTER_DECODER(RV30,              rv30);
>      REGISTER_DECODER(RV40,              rv40);
> -    REGISTER_DECODER(S302M,             s302m);
> +    REGISTER_ENCDEC (S302M,             s302m);
>      REGISTER_DECODER(SANM,              sanm);
>      REGISTER_ENCDEC (SGI,               sgi);
>      REGISTER_DECODER(SGIRLE,            sgirle);
> diff --git a/libavcodec/s302menc.c b/libavcodec/s302menc.c
> new file mode 100644
> index 0000000..dbfbbff
> --- /dev/null
> +++ b/libavcodec/s302menc.c
> @@ -0,0 +1,187 @@
> +/*
> + * SMPTE 302M encoder
> + * Copyright (c) 2010, Google, Inc.
> + * Copyright (c) 2013, Darryl Wallace <wallacdj at gmail.com>
> + *

> + * This file is part of Libav.

hmm


> + *
> + * 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
> + */
> +
> +#include "avcodec.h"
> +#include "internal.h"
> +#include "put_bits.h"
> +
> +#define AES3_HEADER_LEN 4
> +
> +typedef struct S302MEncContext {
> +    uint8_t framing_index; /* Set for even channels on multiple of 192 samples */
> +} S302MEncContext;
> +
> +static av_cold int s302m_encode_init(AVCodecContext *avctx)
> +{
> +    S302MEncContext *s = avctx->priv_data;
> +
> +    if (avctx->channels & 1 || avctx->channels > 8) {
> +        av_log(avctx, AV_LOG_ERROR,
> +               "Encoding %d channel(s) is not allowed. Only 2, 4, 6 and 8 channels are supported.\n",
> +               avctx->channels);
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if (avctx->bits_per_coded_sample == 0) {
> +        if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
> +            avctx->bits_per_coded_sample = 24;
> +        } else if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
> +            avctx->bits_per_coded_sample = 16;
> +        }

> +    } else if (avctx->bits_per_coded_sample == 24 ||
> +               avctx->bits_per_coded_sample == 20) {
> +        avctx->sample_fmt = AV_SAMPLE_FMT_S32;
> +    } else if(avctx->bits_per_coded_sample == 16) {
> +        avctx->sample_fmt = AV_SAMPLE_FMT_S16;

these look wrong
the sample format is set by the user for encoders


> +    } else {
> +        av_log(avctx, AV_LOG_ERROR,
> +               "Unsupported bits per coded sample of %d.\n",
> +               avctx->bits_per_coded_sample);
> +        return AVERROR(EINVAL);
> +    }
> +
> +    avctx->frame_size = 0;
> +    avctx->bit_rate   = 48000 * avctx->channels *
> +                       (avctx->bits_per_coded_sample + 4);
> +    s->framing_index  = 0;
> +
> +    return 0;
> +}
> +
> +static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt,
> +                               const AVFrame *frame, int *got_packet_ptr)
> +{
> +    S302MEncContext *s = avctx->priv_data;

> +    const int buf_size = AES3_HEADER_LEN +
> +                        (frame->nb_samples *
> +                         avctx->channels *
> +                        (avctx->bits_per_coded_sample + 4)) / 8;

should check for integer overflow

also a regression test should be added
rest should be ok if it works

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Observe your enemies, for they first find out your faults. -- Antisthenes
-------------- 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/20130518/608be077/attachment.asc>


More information about the ffmpeg-devel mailing list