2 * LucasArts Smush demuxer
3 * Copyright (c) 2006 Cyril Zorin
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
29 int audio_stream_index;
30 int video_stream_index;
33 static int smush_read_probe(AVProbeData *p)
35 if ((AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M') ||
36 AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M'))) {
37 return AVPROBE_SCORE_MAX;
43 static int smush_read_header(AVFormatContext *ctx)
45 SMUSHContext *smush = ctx->priv_data;
46 AVIOContext *pb = ctx->pb;
48 uint32_t magic, nframes, size, subversion, i;
49 uint32_t width = 0, height = 0, got_audio = 0, read = 0;
50 uint32_t sample_rate, channels, palette[256];
52 magic = avio_rb32(pb);
53 avio_skip(pb, 4); // skip movie size
55 if (magic == MKBETAG('A', 'N', 'I', 'M')) {
56 if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
57 return AVERROR_INVALIDDATA;
60 if (size < 3 * 256 + 6)
61 return AVERROR_INVALIDDATA;
64 subversion = avio_rl16(pb);
65 nframes = avio_rl16(pb);
67 avio_skip(pb, 2); // skip pad
69 for (i = 0; i < 256; i++)
70 palette[i] = avio_rb24(pb);
72 avio_skip(pb, size - (3 * 256 + 6));
73 } else if (magic == MKBETAG('S', 'A', 'N', 'M') ) {
74 if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
75 return AVERROR_INVALIDDATA;
79 return AVERROR_INVALIDDATA;
82 subversion = avio_rl16(pb);
83 nframes = avio_rl32(pb);
84 avio_skip(pb, 2); // skip pad
85 width = avio_rl16(pb);
86 height = avio_rl16(pb);
87 avio_skip(pb, 2); // skip pad
88 avio_skip(pb, size - 14);
90 if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
91 return AVERROR_INVALIDDATA;
94 while (!got_audio && ((read + 8) < size)) {
95 uint32_t sig, chunk_size;
101 chunk_size = avio_rb32(pb);
104 case MKBETAG('W', 'a', 'v', 'e'):
106 sample_rate = avio_rl32(pb);
107 channels = avio_rl32(pb);
108 avio_skip(pb, chunk_size - 8);
111 case MKBETAG('B', 'l', '1', '6'):
112 case MKBETAG('A', 'N', 'N', 'O'):
113 avio_skip(pb, chunk_size);
117 return AVERROR_INVALIDDATA;
122 avio_skip(pb, size - read);
124 av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
125 return AVERROR_INVALIDDATA;
128 vst = avformat_new_stream(ctx, 0);
130 return AVERROR(ENOMEM);
132 smush->video_stream_index = vst->index;
136 vst->nb_frames = nframes;
137 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
138 vst->codec->codec_id = AV_CODEC_ID_SANM;
139 vst->codec->codec_tag = 0;
140 vst->codec->width = width;
141 vst->codec->height = height;
143 avpriv_set_pts_info(vst, 64, 66667, 1000000);
145 if (!smush->version) {
146 vst->codec->extradata = av_malloc(1024 + 2 + FF_INPUT_BUFFER_PADDING_SIZE);
147 if (!vst->codec->extradata)
148 return AVERROR(ENOMEM);
150 vst->codec->extradata_size = 1024 + 2;
151 AV_WL16(vst->codec->extradata, subversion);
152 for (i = 0; i < 256; i++)
153 AV_WL32(vst->codec->extradata + 2 + i * 4, palette[i]);
157 ast = avformat_new_stream(ctx, 0);
159 return AVERROR(ENOMEM);
161 smush->audio_stream_index = ast->index;
164 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
165 ast->codec->codec_id = AV_CODEC_ID_VIMA;
166 ast->codec->codec_tag = 0;
167 ast->codec->sample_rate = sample_rate;
168 ast->codec->channels = channels;
170 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
176 static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
178 SMUSHContext *smush = ctx->priv_data;
179 AVIOContext *pb = ctx->pb;
189 size = avio_rb32(pb);
192 case MKBETAG('F', 'R', 'M', 'E'):
195 if (av_get_packet(pb, pkt, size) < 0)
198 pkt->stream_index = smush->video_stream_index;
201 case MKBETAG('B', 'l', '1', '6'):
202 if (av_get_packet(pb, pkt, size) < 0)
205 pkt->stream_index = smush->video_stream_index;
209 case MKBETAG('W', 'a', 'v', 'e'):
211 return AVERROR_INVALIDDATA;
212 if (av_get_packet(pb, pkt, size) < 0)
215 pkt->stream_index = smush->audio_stream_index;
216 pkt->flags |= AV_PKT_FLAG_KEY;
217 pkt->duration = AV_RB32(pkt->data);
218 if (pkt->duration == 0xFFFFFFFFu)
219 pkt->duration = AV_RB32(pkt->data + 8);
231 AVInputFormat ff_smush_demuxer = {
233 .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
234 .priv_data_size = sizeof(SMUSHContext),
235 .read_probe = smush_read_probe,
236 .read_header = smush_read_header,
237 .read_packet = smush_read_packet,