[FFmpeg-devel] [PATCH] AFC demuxer

Paul B Mahol onemda at gmail.com
Mon Nov 26 21:46:29 CET 2012


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/general.texi         |  2 ++
 libavcodec/adpcm.c       | 19 ++++++++++--
 libavformat/Makefile     |  1 +
 libavformat/afc.c        | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/allformats.c |  1 +
 5 files changed, 102 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/afc.c

diff --git a/doc/general.texi b/doc/general.texi
index 366b315..0e10d30 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -147,6 +147,8 @@ library:
     @tab Multimedia format used in game Heart Of Darkness.
 @item Apple HTTP Live Streaming @tab   @tab X
 @item Artworx Data Format       @tab   @tab X
+ at item AFC                       @tab   @tab X
+    @tab Audio format used on the Nintendo Gamecube.
 @item ASF                       @tab X @tab X
 @item AST                       @tab   @tab X
     @tab Audio format used on the Nintendo Wii.
diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index df8aba9..c182e6f 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -1268,13 +1268,26 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
         }
         break;
     case AV_CODEC_ID_ADPCM_AFC:
+    {
+        int samples_per_block;
+        int blocks;
+
+        if (avctx->extradata && avctx->extradata_size == 1 && avctx->extradata[0]) {
+            samples_per_block = avctx->extradata[0] / 16;
+            blocks = nb_samples / avctx->extradata[0];
+        } else {
+            samples_per_block = nb_samples / 16;
+            blocks = 1;
+        }
+
+        for (m = 0; m < blocks; m++) {
         for (channel = 0; channel < avctx->channels; channel++) {
             int prev1 = c->status[channel].sample1;
             int prev2 = c->status[channel].sample2;
 
-            samples = samples_p[channel];
+            samples = samples_p[channel] + m * 16;
             /* Read in every sample for this channel.  */
-            for (i = 0; i < nb_samples / 16; i++) {
+            for (i = 0; i < samples_per_block; i++) {
                 int byte = bytestream2_get_byteu(&gb);
                 int scale = 1 << (byte >> 4);
                 int index = byte & 0xf;
@@ -1303,8 +1316,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
             c->status[channel].sample1 = prev1;
             c->status[channel].sample2 = prev2;
         }
+        }
         bytestream2_seek(&gb, 0, SEEK_END);
         break;
+    }
     case AV_CODEC_ID_ADPCM_THP:
     {
         int table[6][16];
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 136ada8..463ce32 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -37,6 +37,7 @@ OBJS-$(CONFIG_ADX_DEMUXER)               += adxdec.o
 OBJS-$(CONFIG_ADX_MUXER)                 += rawenc.o
 OBJS-$(CONFIG_ADTS_MUXER)                += adtsenc.o
 OBJS-$(CONFIG_AEA_DEMUXER)               += aea.o pcm.o
+OBJS-$(CONFIG_AFC_DEMUXER)               += afc.o
 OBJS-$(CONFIG_AIFF_DEMUXER)              += aiffdec.o pcm.o isom.o \
                                             mov_chan.o
 OBJS-$(CONFIG_AIFF_MUXER)                += aiffenc.o isom.o
diff --git a/libavformat/afc.c b/libavformat/afc.c
new file mode 100644
index 0000000..ec8bf03
--- /dev/null
+++ b/libavformat/afc.c
@@ -0,0 +1,81 @@
+/*
+ * AFC demuxer
+ * 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 "libavutil/channel_layout.h"
+#include "libavutil/intreadwrite.h"
+#include "avformat.h"
+#include "internal.h"
+
+typedef struct AFCDemuxContext {
+    int64_t    data_end;
+} AFCDemuxContext;
+
+static int afc_read_header(AVFormatContext *s)
+{
+    AFCDemuxContext *c = s->priv_data;
+    AVStream *st;
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
+    st->codec->codec_id   = AV_CODEC_ID_ADPCM_AFC;
+    st->codec->channels   = 2;
+    st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
+    st->codec->extradata_size = 1;
+
+    st->codec->extradata = av_mallocz(1 + FF_INPUT_BUFFER_PADDING_SIZE);
+    if (!st->codec->extradata)
+        return AVERROR(ENOMEM);
+    st->codec->extradata[0] = 8 * st->codec->channels;
+
+    c->data_end = avio_rb32(s->pb) + 32;
+    st->duration = avio_rb32(s->pb);
+    st->codec->sample_rate = avio_rb16(s->pb);
+    avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
+    avio_skip(s->pb, 22);
+
+    return 0;
+}
+
+static int afc_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AFCDemuxContext *c = s->priv_data;
+    int64_t size;
+    int ret;
+
+    size = FFMIN(c->data_end - avio_tell(s->pb), 18 * 128);
+    if (size <= 0)
+        return AVERROR_EOF;
+
+    ret = av_get_packet(s->pb, pkt, size);
+    pkt->stream_index = 0;
+    return ret;
+}
+
+AVInputFormat ff_afc_demuxer = {
+    .name           = "afc",
+    .long_name      = NULL_IF_CONFIG_SMALL("AFC"),
+    .priv_data_size = sizeof(AFCDemuxContext),
+    .read_header    = afc_read_header,
+    .read_packet    = afc_read_packet,
+    .extensions     = "afc",
+};
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index eaeb51a..6c721c4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -57,6 +57,7 @@ void av_register_all(void)
     REGISTER_MUXER    (ADTS, adts);
     REGISTER_MUXDEMUX (ADX, adx);
     REGISTER_DEMUXER  (AEA, aea);
+    REGISTER_DEMUXER  (AFC, afc);
     REGISTER_MUXDEMUX (AIFF, aiff);
     REGISTER_MUXDEMUX (AMR, amr);
     REGISTER_DEMUXER  (ANM, anm);
-- 
1.7.11.4



More information about the ffmpeg-devel mailing list