[FFmpeg-devel] [PATCH 1/3] LucasArts Smush VIMA audio decoder

Paul B Mahol onemda at gmail.com
Tue Mar 27 19:28:43 CEST 2012


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/general.texi       |    2 +
 libavcodec/Makefile    |    1 +
 libavcodec/allcodecs.c |    1 +
 libavcodec/avcodec.h   |    1 +
 libavcodec/vima.c      |  273 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 278 insertions(+), 0 deletions(-)
 create mode 100644 libavcodec/vima.c

diff --git a/doc/general.texi b/doc/general.texi
index 5143f83..a810e5b 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -810,6 +810,8 @@ following image formats are supported:
 @item TrueHD                 @tab     @tab  X
     @tab Used in HD-DVD and Blu-Ray discs.
 @item TwinVQ (VQF flavor)    @tab     @tab  X
+ at item VIMA
+    @tab Used in SANM variant of LucasArts SMUSH animations.
 @item Vorbis                 @tab  E  @tab  X
     @tab A native but very primitive encoder exists.
 @item WavPack                @tab     @tab  X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ba7b4b8..eb24595 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -592,6 +592,7 @@ OBJS-$(CONFIG_ADPCM_THP_DECODER)          += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_XA_DECODER)           += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_YAMAHA_DECODER)       += adpcm.o adpcm_data.o
 OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)       += adpcmenc.o adpcm_data.o
+OBJS-$(CONFIG_VIMA_DECODER)               += vima.o
 
 # libavformat dependencies
 OBJS-$(CONFIG_ADTS_MUXER)              += mpeg4audio.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 5e3024d..779474e 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -391,6 +391,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (ADPCM_THP, adpcm_thp);
     REGISTER_DECODER (ADPCM_XA, adpcm_xa);
     REGISTER_ENCDEC  (ADPCM_YAMAHA, adpcm_yamaha);
+    REGISTER_DECODER (VIMA, vima);
 
     /* subtitles */
     REGISTER_ENCDEC  (ASS, ass);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 28c3a50..eb22c1c 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -321,6 +321,7 @@ enum CodecID {
     CODEC_ID_ADPCM_IMA_ISS,
     CODEC_ID_ADPCM_G722,
     CODEC_ID_ADPCM_IMA_APC,
+    CODEC_ID_VIMA,
 
     /* AMR */
     CODEC_ID_AMR_NB = 0x12000,
diff --git a/libavcodec/vima.c b/libavcodec/vima.c
new file mode 100644
index 0000000..fa1bf61
--- /dev/null
+++ b/libavcodec/vima.c
@@ -0,0 +1,273 @@
+/*
+ * LucasArt VIMA decoder
+ * 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/audioconvert.h"
+#include "avcodec.h"
+#include "bytestream.h"
+
+typedef struct {
+    AVFrame     frame;
+    uint16_t    *predict_table;
+} VimaContext;
+
+static const int16_t step_table[] =
+{
+        7,     8,     9,    10,    11,    12,    13,    14,    16,    17,
+       19,    21,    23,    25,    28,    31,    34,    37,    41,    45,
+       50,    55,    60,    66,    73,    80,    88,    97,   107,   118,
+      130,   143,   157,   173,   190,   209,   230,   253,   279,   307,
+      337,   371,   408,   449,   494,   544,   598,   658,   724,   796,
+      876,   963,  1060,  1166,  1282,  1411,  1552,  1707,  1878,  2066,
+     2272,  2499,  2749,  3024,  3327,  3660,  4026,  4428,  4871,  5358,
+     5894,  6484,  7132,  7845,  8630,  9493, 10442, 11487, 12635, 13899,
+    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
+};
+
+static const uint8_t size_table[] =
+{
+    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5,
+    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+    6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
+};
+
+static const int8_t index_table1[] =
+{
+    -1, 4, -1, 4
+};
+
+static const int8_t index_table2[] =
+{
+    -1, -1, 2, 6, -1, -1, 2, 6
+};
+
+static const int8_t index_table3[] =
+{
+    -1, -1, -1, -1, 1, 2, 4, 6,
+    -1, -1, -1, -1, 1, 2, 4, 6
+};
+
+static const int8_t index_table4[] =
+{
+    -1, -1, -1, -1, -1, -1, -1, -1,
+     1,  1,  1,  2,  2,  4,  5,  6,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+     1,  1,  1,  2,  2,  4,  5,  6
+};
+
+static const int8_t index_table5[] =
+{
+    -1, -1, -1, -1, -1, -1, -1, -1,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+     1,  1,  1,  1,  1,  2,  2,  2,
+     2,  4,  4,  4,  5,  5,  6,  6,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+     1,  1,  1,  1,  1,  2,  2,  2,
+     2,  4,  4,  4,  5,  5,  6,  6
+};
+
+static const int8_t index_table6[] =
+{
+    -1, -1, -1, -1, -1, -1, -1, -1,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+     1,  1,  1,  1,  1,  1,  1,  1,
+     1,  1,  2,  2,  2,  2,  2,  2,
+     2,  2,  4,  4,  4,  4,  4,  4,
+     5,  5,  5,  5,  6,  6,  6,  6,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+    -1, -1, -1, -1, -1, -1, -1, -1,
+     1,  1,  1,  1,  1,  1,  1,  1,
+     1,  1,  2,  2,  2,  2,  2,  2,
+     2,  2,  4,  4,  4,  4,  4,  4,
+     5,  5,  5,  5,  6,  6,  6,  6
+};
+
+static const int8_t* step_index_tables[] =
+{
+    index_table1, index_table2, index_table3,
+    index_table4, index_table5, index_table6
+};
+
+static av_cold int decode_init(AVCodecContext *avctx)
+{
+    VimaContext *vima = avctx->priv_data;
+    int         start_pos, i;
+
+    vima->predict_table = av_mallocz(5786 * 2);
+    if (!vima->predict_table) {
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate predict table.\n");
+        return AVERROR(ENOMEM);
+    }
+
+    for (start_pos = 0, i = 0; start_pos < 64; start_pos++, i++) {
+        unsigned int dest_pos, table_pos;
+
+        for (table_pos = 0, dest_pos = start_pos;
+             table_pos < FF_ARRAY_ELEMS(step_table);
+             table_pos++, dest_pos += 64) {
+            int put = 0, count, table_value;
+
+            for (count = 32, table_value = step_table[table_pos]; count != 0;
+                 count >>= 1, table_value >>= 1) {
+                if (i & count)
+                    put += table_value;
+            }
+            vima->predict_table[dest_pos] = put;
+        }
+    }
+
+    avcodec_get_frame_defaults(&vima->frame);
+    avctx->coded_frame = &vima->frame;
+    avctx->sample_fmt  = AV_SAMPLE_FMT_S16;
+
+    return 0;
+}
+
+static int decode_frame(AVCodecContext *avctx, void *data,
+                        int *got_frame_ptr, AVPacket *pkt)
+{
+    GetByteContext gb;
+    VimaContext    *vima = avctx->priv_data;
+    int16_t        pcm_data[2];
+    int8_t         channel_hint[2];
+    int            bits, bitPtr = 0, ret, chan, samples, channels = 1;
+
+    bytestream2_init(&gb, pkt->data, pkt->size);
+
+    if (bytestream2_get_bytes_left(&gb) < 13)
+        return AVERROR_INVALIDDATA;
+
+    samples = bytestream2_get_be32u(&gb);
+    if (samples < 0) {
+        bytestream2_skip(&gb, 4);
+        samples = bytestream2_get_be32u(&gb);
+    }
+
+    channel_hint[0] = bytestream2_get_byteu(&gb);
+    if (channel_hint[0] & 0x80) {
+        channel_hint[0] = ~channel_hint[0];
+        channels = 2;
+    }
+    avctx->channels = channels;
+    avctx->channel_layout = (channels == 2) ? AV_CH_LAYOUT_STEREO :
+                                              AV_CH_LAYOUT_MONO;
+    pcm_data[0] = bytestream2_get_be16u(&gb);
+    if (channels > 1) {
+        channel_hint[1] = bytestream2_get_byte(&gb);
+        pcm_data[1] = bytestream2_get_be16(&gb);
+    }
+    bits = bytestream2_get_be16(&gb);
+
+    vima->frame.nb_samples = samples;
+    if ((ret = avctx->get_buffer(avctx, &vima->frame)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return ret;
+    }
+
+    for (chan = 0; chan < channels; chan++) {
+        uint16_t *dest = (uint16_t*)vima->frame.data[0] + chan;
+        int step_index = channel_hint[chan];
+        int outputWord = pcm_data[chan];
+        int sample;
+
+        for (sample = 0; sample < samples; sample++) {
+            int lookup_size, highBit, lowBits, val;
+
+            step_index  = av_clip(step_index, 0, 88);
+            lookup_size = size_table[step_index];
+            highBit     = 1 << (lookup_size - 1);
+            lowBits     = highBit - 1;
+            bitPtr     += lookup_size;
+            val         = (bits >> (16 - bitPtr)) & (highBit | lowBits);
+            if (bitPtr > 7) {
+                if (bytestream2_get_bytes_left(&gb) < 1)
+                    return -1;
+
+                bits = ((bits & 0xff) << 8) | bytestream2_get_byteu(&gb);
+                bitPtr -= 8;
+            }
+
+            if (val & highBit)
+                val ^= highBit;
+            else
+                highBit = 0;
+
+            if (val == lowBits) {
+                if (bytestream2_get_bytes_left(&gb) < 2)
+                    return -1;
+
+                outputWord  = (int16_t)(bits << bitPtr) & 0xffffff00;
+                bits        = ((bits & 0xff) << 8) | bytestream2_get_byteu(&gb);
+                outputWord |= (bits >> (8 - bitPtr)) & 0xff;
+                bits        = ((bits & 0xff) << 8) | bytestream2_get_byteu(&gb);
+            } else {
+                int predict_index, delta;
+
+                predict_index = (val << (7 - lookup_size)) | (step_index << 6);
+                predict_index = av_clip(predict_index, 0, 5785);
+                delta         = vima->predict_table[predict_index];
+                if (val)
+                    delta += step_table[step_index] >> (lookup_size - 1);
+                if (highBit)
+                    delta  = -delta;
+
+                outputWord += delta;
+                outputWord  = av_clip(outputWord, -0x8000, 0x7fff);
+            }
+
+            *dest = outputWord;
+            dest += channels;
+
+            step_index += step_index_tables[lookup_size - 2][val];
+        }
+    }
+
+    *got_frame_ptr   = 1;
+    *(AVFrame *)data = vima->frame;
+
+    return pkt->size;
+}
+
+static av_cold int decode_end(AVCodecContext *avctx)
+{
+    VimaContext *vima = avctx->priv_data;
+
+    av_free(vima->predict_table);
+
+    return 0;
+}
+
+AVCodec ff_vima_decoder = {
+    .name           = "vima",
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = CODEC_ID_VIMA,
+    .priv_data_size = sizeof(VimaContext),
+    .init           = decode_init,
+    .close          = decode_end,
+    .decode         = decode_frame,
+    .capabilities   = CODEC_CAP_DR1,
+    .long_name      = NULL_IF_CONFIG_SMALL("LucasArts SMUSH audio"),
+};
-- 
1.7.7



More information about the ffmpeg-devel mailing list