[FFmpeg-devel] [PATCH] s302menc: Add S302M encoder.

Thierry Foucu tfoucu at gmail.com
Tue Apr 10 23:45:17 CEST 2012


---
 libavcodec/Makefile    |    1 +
 libavcodec/allcodecs.c |    2 +-
 libavcodec/s302menc.c  |  115 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+), 1 deletions(-)
 create mode 100644 libavcodec/s302menc.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index d554df5..14cb1de 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -376,6 +376,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 \
                                           mpegvideo.o error_resilience.o
 OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
+OBJS-$(CONFIG_S302M_ENCODER)           += s302menc.o
 OBJS-$(CONFIG_SGI_DECODER)             += sgidec.o
 OBJS-$(CONFIG_SGI_ENCODER)             += sgienc.o rle.o
 OBJS-$(CONFIG_SHORTEN_DECODER)         += shorten.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 76daafd..238bc62 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -198,7 +198,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_ENCDEC  (SGI, sgi);
     REGISTER_DECODER (SMACKER, smacker);
     REGISTER_DECODER (SMC, smc);
diff --git a/libavcodec/s302menc.c b/libavcodec/s302menc.c
new file mode 100644
index 0000000..604266f
--- /dev/null
+++ b/libavcodec/s302menc.c
@@ -0,0 +1,115 @@
+/*
+ * SMPTE 302M Encoder
+ * Copyright (c) 2010, Google, Inc.
+ *
+ * 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
+ */
+
+/**
+ * @file libavcodec/s302menc.c
+ * Simple S302M PCM encapsulation.
+ */
+
+
+#include "avcodec.h"
+#include "put_bits.h"
+#include "libavutil/intreadwrite.h"
+
+#define AES3_HEADER_LEN 4
+/* The number has to be a multiple of 192 */
+#define S302M_NUMBER_SAMPLES 4800 /* Is 100ms of audio too big? */
+typedef struct S302MContext {
+    AVFrame frame;
+    /* Set for even channels on multiple of 192 samples */
+    uint8_t framing_index;
+} S302MContext;
+
+static int s302m_encode_init(AVCodecContext *avctx)
+{
+    S302MContext *s = avctx->priv_data;
+
+    if (avctx->channels != 2 && avctx->channels != 4 && avctx->channels != 6 & 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 -1;
+    }
+    avctx->bits_per_coded_sample = 16;
+    avctx->frame_size = S302M_NUMBER_SAMPLES;
+    avctx->coded_frame= &s->frame;
+    avctx->coded_frame->key_frame= 1;
+    avctx->bit_rate = 48000*avctx->channels*(avctx->bits_per_coded_sample+4);
+    s->framing_index = 0;
+
+    return 0;
+}
+
+static int s302m_encode_frame(AVCodecContext *avctx,
+                              uint8_t *frame, int buf_size, void *data)
+{
+    S302MContext *s = avctx->priv_data;
+    uint8_t *o = frame;
+    const int s302m_frame_size = S302M_NUMBER_SAMPLES * avctx->channels * 5 >> 1;
+    int num_samples;
+    int channels;
+    const uint16_t *i = data;
+    uint8_t vucf;
+
+    if (buf_size < s302m_frame_size + AES3_HEADER_LEN) {
+      av_log(avctx, AV_LOG_ERROR, "Buffer too small %d to encode %d Bytes.\n",
+             buf_size, s302m_frame_size + AES3_HEADER_LEN);
+      return -1;
+    }
+
+    PutBitContext pb;
+    init_put_bits(&pb, o, buf_size*8);
+    put_bits(&pb, 16, s302m_frame_size);  // Frame size
+    put_bits(&pb, 2, (avctx->channels - 2) >> 1);  // Number channels
+    put_bits(&pb, 8, 0);  // Channel id
+    put_bits(&pb, 2, 0);  // bits per samples (16bit)
+    put_bits(&pb, 4, 0);  // alignments
+    flush_put_bits(&pb);
+
+    o += AES3_HEADER_LEN;
+
+    num_samples = S302M_NUMBER_SAMPLES * avctx->channels;
+    for (; num_samples > 0; num_samples -= avctx->channels) {
+        vucf = (s->framing_index == 0)? 0x10 : 0;
+        for (channels = 0; channels < avctx->channels; channels += 2) {
+            vucf = (s->framing_index == 0)? 0x10 : 0;
+            *o++ = av_reverse[i[0] & 0xff];
+            *o++ = av_reverse[(i[0] & 0xff00) >> 8];
+            *o++ = av_reverse[(i[1] & 0x0f) << 4] | vucf;
+            *o++ = av_reverse[(i[1] & 0x0ff0) >> 4];
+            *o++ = av_reverse[(i[1] & 0xf000) >> 12];
+            i += 2;
+        }
+        s->framing_index++;
+        if (s->framing_index >= 192) s->framing_index = 0;
+    }
+    return o - frame;
+}
+
+AVCodec ff_s302m_encoder = {
+    .name           = "s302m",
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = CODEC_ID_S302M,
+    .priv_data_size = sizeof(S302MContext),
+    .init           = s302m_encode_init,
+    .encode         = s302m_encode_frame,
+    .long_name      = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
+    .sample_fmts    = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
+    .supported_samplerates = (const int[]){48000, 0},
+};
-- 
1.7.7.3



More information about the ffmpeg-devel mailing list