From 3e1604750707e993294cebd652bf067f7676615f Mon Sep 17 00:00:00 2001 From: Darryl Wallace Date: Fri, 15 Mar 2013 00:37:07 -0400 Subject: [PATCH] s302m encoder Signed-off-by: Paul B Mahol --- doc/general.texi | 2 +- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 2 +- libavcodec/s302menc.c | 176 +++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 4 +- 5 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 libavcodec/s302menc.c diff --git a/doc/general.texi b/doc/general.texi index 89c0d32c12..41dc2140cc 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -891,7 +891,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 -@item SMPTE 302M AES3 audio @tab @tab X +@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 1a58c69ac5..c0d165a6b4 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -375,6 +375,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 a15162d8ff..a6b3e720fb 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -239,7 +239,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 0000000000..325c1ed634 --- /dev/null +++ b/libavcodec/s302menc.c @@ -0,0 +1,176 @@ +/* + * SMPTE 302M encoder + * Copyright (c) 2010 Google, Inc. + * Copyright (c) 2013 Darryl Wallace + * + * 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 "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); + } + + switch (avctx->sample_fmt) { + case AV_SAMPLE_FMT_S16: + avctx->bits_per_raw_sample = 16; + break; + case AV_SAMPLE_FMT_S32: + if (avctx->bits_per_raw_sample > 24) { + av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n"); + avctx->bits_per_raw_sample = 24; + } else if (!avctx->bits_per_raw_sample) { + avctx->bits_per_raw_sample = 24; + } else if (avctx->bits_per_raw_sample <= 20) { + avctx->bits_per_raw_sample = 20; + } + } + + avctx->frame_size = 0; + avctx->bit_rate = 48000 * avctx->channels * + (avctx->bits_per_raw_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_raw_sample + 4)) / 8; + int ret, c, channels; + uint8_t *o; + PutBitContext pb; + + if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)) < 0) + return ret; + + o = avpkt->data; + init_put_bits(&pb, o, buf_size * 8); + put_bits(&pb, 16, buf_size - AES3_HEADER_LEN); + put_bits(&pb, 2, (avctx->channels - 2) >> 1); // number of channels + put_bits(&pb, 8, 0); // channel ID + put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit) + put_bits(&pb, 4, 0); // alignments + flush_put_bits(&pb); + o += AES3_HEADER_LEN; + + if (avctx->bits_per_raw_sample == 24) { + const uint32_t *samples = (uint32_t *)frame->data[0]; + + for (c = 0; c < frame->nb_samples; c++) { + uint8_t vucf = s->framing_index == 0 ? 0x10: 0; + + for (channels = 0; channels < avctx->channels; channels += 2) { + o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8]; + o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16]; + o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24]; + o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf; + o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12]; + o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20]; + o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28]; + o += 7; + samples += 2; + } + + s->framing_index++; + if (s->framing_index >= 192) + s->framing_index = 0; + } + } else if (avctx->bits_per_raw_sample == 20) { + const uint32_t *samples = (uint32_t *)frame->data[0]; + + for (c = 0; c < frame->nb_samples; c++) { + uint8_t vucf = s->framing_index == 0 ? 0x80: 0; + + for (channels = 0; channels < avctx->channels; channels += 2) { + o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12]; + o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20]; + o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf]; + o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12]; + o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20]; + o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28]; + o += 6; + samples += 2; + } + + s->framing_index++; + if (s->framing_index >= 192) + s->framing_index = 0; + } + } else if (avctx->bits_per_raw_sample == 16) { + const uint16_t *samples = (uint16_t *)frame->data[0]; + + for (c = 0; c < frame->nb_samples; c++) { + uint8_t vucf = s->framing_index == 0 ? 0x10 : 0; + + for (channels = 0; channels < avctx->channels; channels += 2) { + o[0] = ff_reverse[ samples[0] & 0xFF]; + o[1] = ff_reverse[(samples[0] & 0xFF00) >> 8]; + o[2] = ff_reverse[(samples[1] & 0x0F) << 4] | vucf; + o[3] = ff_reverse[(samples[1] & 0x0FF0) >> 4]; + o[4] = ff_reverse[(samples[1] & 0xF000) >> 12]; + o += 5; + samples += 2; + + } + + s->framing_index++; + if (s->framing_index >= 192) + s->framing_index = 0; + } + } + + *got_packet_ptr = 1; + + return 0; +} + +AVCodec ff_s302m_encoder = { + .name = "s302m", + .type = AVMEDIA_TYPE_AUDIO, + .id = CODEC_ID_S302M, + .priv_data_size = sizeof(S302MEncContext), + .init = s302m_encode_init, + .encode2 = s302m_encode2_frame, + .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"), + .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32, + AV_SAMPLE_FMT_S16, + AV_SAMPLE_FMT_NONE }, + .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE | CODEC_CAP_EXPERIMENTAL, + .supported_samplerates = (const int[]) { 48000, 0 }, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index be9a580cfb..7d34f8a6cb 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -29,8 +29,8 @@ #include "libavutil/avutil.h" #define LIBAVCODEC_VERSION_MAJOR 55 -#define LIBAVCODEC_VERSION_MINOR 10 -#define LIBAVCODEC_VERSION_MICRO 101 +#define LIBAVCODEC_VERSION_MINOR 11 +#define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \