[FFmpeg-devel] [PATCH 3/3] LucasArts SMUSH demuxer

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


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/general.texi         |    2 +
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    1 +
 libavformat/smush.c      |  293 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 297 insertions(+), 0 deletions(-)
 create mode 100644 libavformat/smush.c

diff --git a/doc/general.texi b/doc/general.texi
index bc8214e..07eac4c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -322,6 +322,8 @@ library:
     @tab Multimedia format used by many games.
 @item SMJPEG                    @tab X @tab X
     @tab Used in certain Loki game ports.
+ at item Smush
+    @tab Multimedia format used in some LucasArts games.
 @item Sony OpenMG (OMA)         @tab X @tab X
     @tab Audio format used in Sony Sonic Stage and Sony Vegas.
 @item Sony PlayStation STR      @tab   @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 212c9d7..f96c163 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -299,6 +299,7 @@ OBJS-$(CONFIG_SIFF_DEMUXER)              += siff.o
 OBJS-$(CONFIG_SMACKER_DEMUXER)           += smacker.o
 OBJS-$(CONFIG_SMJPEG_DEMUXER)            += smjpegdec.o smjpeg.o
 OBJS-$(CONFIG_SMJPEG_MUXER)              += smjpegenc.o smjpeg.o
+OBJS-$(CONFIG_SMUSH_DEMUXER)             += smush.o
 OBJS-$(CONFIG_SOL_DEMUXER)               += sol.o pcm.o
 OBJS-$(CONFIG_SOX_DEMUXER)               += soxdec.o pcm.o
 OBJS-$(CONFIG_SOX_MUXER)                 += soxenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 905a165..fa61b10 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -217,6 +217,7 @@ void av_register_all(void)
     REGISTER_DEMUXER  (SIFF, siff);
     REGISTER_DEMUXER  (SMACKER, smacker);
     REGISTER_MUXDEMUX (SMJPEG, smjpeg);
+    REGISTER_DEMUXER  (SMUSH, smush);
     REGISTER_DEMUXER  (SOL, sol);
     REGISTER_MUXDEMUX (SOX, sox);
     REGISTER_MUXDEMUX (SPDIF, spdif);
diff --git a/libavformat/smush.c b/libavformat/smush.c
new file mode 100644
index 0000000..ee5742a
--- /dev/null
+++ b/libavformat/smush.c
@@ -0,0 +1,293 @@
+/*
+ * LucasArts Smush demuxer
+ * Copyright (c) 2006 Cyril Zorin
+ *
+ * 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/intreadwrite.h"
+#include "avformat.h"
+#include "internal.h"
+#include "avio.h"
+
+typedef struct {
+    int        width, height;
+
+    int        nframes;
+    int        stream_index;
+    int        subversion;
+    uint32_t   palette[256];
+} smush_vinfo;
+
+typedef struct {
+    int        freq, nchannels;
+    int        stream_index;
+} smush_ainfo;
+
+typedef struct {
+    smush_vinfo vinfo;
+    smush_ainfo ainfo;
+    int        version;
+} SMUSHContext;
+
+static int smush_read_probe(AVProbeData *p)
+{
+    if (p->buf_size >= 4
+        && (AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M')
+         || AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M'))) {
+        return AVPROBE_SCORE_MAX;
+    }
+
+    return 0;
+}
+
+static av_cold int read_vinfo0(AVIOContext *pb, smush_vinfo *vinfo)
+{
+    uint32_t size, sig;
+    int i;
+
+    sig = avio_rb32(pb);
+    if (sig != MKBETAG('A', 'H', 'D', 'R'))
+        return AVERROR_INVALIDDATA;
+
+    size   = avio_rb32(pb);
+    if (size < 3 * 256 + 6)
+        return AVERROR_INVALIDDATA;
+
+    vinfo->subversion = avio_rl16(pb);
+    vinfo->nframes    = avio_rl16(pb);
+
+    avio_skip(pb, 2); // skip pad
+    vinfo->width  = 0;
+    vinfo->height = 0;
+
+    for (i = 0; i < 256; i++)
+        vinfo->palette[i] = avio_rb24(pb);
+
+    avio_skip(pb, size - (3 * 256 + 6));
+
+    return 0;
+}
+
+static av_cold int read_vinfo1(AVIOContext *pb, smush_vinfo *vinfo)
+{
+    uint32_t size, sig;
+
+    sig = avio_rb32(pb);
+    if (sig != MKBETAG('S', 'H', 'D', 'R'))
+        return AVERROR_INVALIDDATA;
+
+    size = avio_rb32(pb);
+    if (size < 14)
+        return AVERROR_INVALIDDATA;
+
+    avio_skip(pb, 2); // skip version
+
+    vinfo->nframes = avio_rl32(pb);
+
+    avio_skip(pb, 2); // skip pad
+    vinfo->width  = avio_rl16(pb);
+    vinfo->height = avio_rl16(pb);
+    avio_skip(pb, 2); // skip pad
+
+    avio_skip(pb, size - 14);
+
+    return 0;
+}
+
+static av_cold int read_ainfo1(AVIOContext *pb, smush_ainfo *ainfo)
+{
+    uint32_t sig, size, chunk_size, read = 0;
+    int got_wave = 0;
+
+    sig = avio_rb32(pb);
+    if (sig != MKBETAG('F', 'L', 'H', 'D'))
+        return AVERROR_INVALIDDATA;
+
+    size = avio_rb32(pb);
+    while (!got_wave && ((read + 8) < size)) {
+        sig        = avio_rb32(pb);
+        chunk_size = avio_rb32(pb);
+
+        read += 8;
+        switch (sig) {
+        case MKBETAG('W', 'a', 'v', 'e'):
+            got_wave         = 1;
+            ainfo->freq      = avio_rl32(pb);
+            ainfo->nchannels = avio_rl32(pb);
+            avio_skip(pb, chunk_size - 8);
+            read += chunk_size;
+            break;
+        case MKBETAG('B', 'l', '1', '6'):
+        case MKBETAG('A', 'N', 'N', 'O'):
+            avio_skip(pb, chunk_size);
+            read += chunk_size;
+            break;
+        }
+    }
+
+    avio_skip(pb, size - read);
+
+    return got_wave;
+}
+
+static int smush_read_header(AVFormatContext *ctx)
+{
+    AVIOContext *pb = ctx->pb;
+    SMUSHContext *smush = ctx->priv_data;
+    AVStream *vstream;
+    AVStream *astream;
+    smush_vinfo *vinfo = &smush->vinfo;
+    smush_ainfo *ainfo = &smush->ainfo;
+    uint32_t movie_sig = avio_rb32(pb);
+
+    switch (movie_sig) {
+    case MKBETAG('A', 'N', 'I', 'M'):
+        smush->version = 0;
+        break;
+    case MKBETAG('S', 'A', 'N', 'M'):
+        smush->version = 1;
+        break;
+    default:
+        av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    avio_rb32(pb); // skip movie size
+
+    if (!smush->version && read_vinfo0(pb, &smush->vinfo)) {
+        av_log(ctx, AV_LOG_ERROR, "Wrong video stream header\n");
+        return AVERROR_INVALIDDATA;
+    }
+    if (smush->version && read_vinfo1(pb, &smush->vinfo)) {
+        av_log(ctx, AV_LOG_ERROR, "Wrong video stream header\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    vstream = avformat_new_stream(ctx, 0);
+    if (!vstream)
+        return AVERROR(ENOMEM);
+
+    vinfo->stream_index        = vstream->index;
+
+    vstream->start_time        = 0;
+    vstream->duration          = vinfo->nframes;
+    vstream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+    vstream->codec->codec_id   = CODEC_ID_SANM;
+    vstream->codec->codec_tag  = 0;
+    vstream->codec->width      = vinfo->width;
+    vstream->codec->height     = vinfo->height;
+
+    avpriv_set_pts_info(vstream, 64, 1, 15);
+
+    if (!smush->version) {
+        int i;
+
+        vstream->codec->extradata = av_malloc(1024 + 2);
+        if (!vstream->codec->extradata)
+            return AVERROR(ENOMEM);
+        vstream->codec->extradata_size = 1024 + 2;
+        AV_WL16(vstream->codec->extradata, vinfo->subversion);
+        for (i = 0; i < 256; i++)
+            AV_WL32(vstream->codec->extradata + 2 + i * 4, vinfo->palette[i]);
+    }
+
+    if (smush->version) {
+        int ret;
+
+        if ((ret = read_ainfo1(pb, &smush->ainfo)) < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Invalid audio information\n");
+            return AVERROR_INVALIDDATA;
+        } else if (ret) {
+            astream = avformat_new_stream(ctx, 0);
+            if (!astream)
+                return AVERROR(ENOMEM);
+
+            ainfo->stream_index         = astream->index;
+
+            astream->start_time         = 0;
+            astream->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
+            astream->codec->codec_id    = CODEC_ID_VIMA;
+            astream->codec->codec_tag   = 0;
+            astream->codec->channels    = ainfo->nchannels;
+            astream->codec->sample_rate = ainfo->freq;
+
+            avpriv_set_pts_info(astream, 64, 1, ainfo->freq);
+        }
+    }
+
+    return 0;
+}
+
+static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
+{
+    SMUSHContext *smush = ctx->priv_data;
+    AVIOContext *pb = ctx->pb;
+    uint32_t sig, size;
+    int done = 0;
+
+    while (!done) {
+        if (pb->eof_reached)
+            return AVERROR_EOF;
+
+        sig    = avio_rb32(pb);
+        size   = avio_rb32(pb);
+
+        switch (sig) {
+        case MKBETAG('F', 'R', 'M', 'E'):
+            if (!smush->version && size) {
+                if (av_get_packet(pb, pkt, size) != size)
+                    return AVERROR(EIO);
+
+                pkt->stream_index = smush->vinfo.stream_index;
+
+                done = 1;
+            }
+            break;
+        case MKBETAG('B', 'l', '1', '6'):
+            if (av_get_packet(pb, pkt, size) != size)
+                return AVERROR(EIO);
+
+            pkt->stream_index = smush->vinfo.stream_index;
+
+            done = 1;
+            break;
+        case MKBETAG('W','a','v','e'):
+            if (av_get_packet(pb, pkt, size) != size)
+                return AVERROR(EIO);
+
+            av_grow_packet(pkt, 2);
+            pkt->stream_index = smush->ainfo.stream_index;
+            done = 1;
+            break;
+        default:
+            avio_skip(pb, size);
+            break;
+        }
+    }
+
+    return 0;
+}
+
+AVInputFormat ff_smush_demuxer = {
+    .name           = "smush",
+    .long_name      = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
+    .priv_data_size = sizeof(SMUSHContext),
+    .read_probe     = smush_read_probe,
+    .read_header    = smush_read_header,
+    .read_packet    = smush_read_packet,
+};
-- 
1.7.7



More information about the ffmpeg-devel mailing list