1 /*
2 * LucasArts Smush demuxer
3 * Copyright (c) 2006 Cyril Zorin
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavutil/intreadwrite.h"
23
24 #include "avformat.h"
25 #include "avio.h"
26 #include "internal.h"
27
28 typedef struct SMUSHContext {
29 int version;
30 int audio_stream_index;
31 int video_stream_index;
32 } SMUSHContext;
33
smush_read_probe(const AVProbeData *p)34 static int smush_read_probe(const AVProbeData *p)
35 {
36 if (((AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M') &&
37 AV_RL32(p->buf + 8) == MKTAG('S', 'H', 'D', 'R')) ||
38 (AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M') &&
39 AV_RL32(p->buf + 8) == MKTAG('A', 'H', 'D', 'R')))) {
40 return AVPROBE_SCORE_MAX;
41 }
42
43 return 0;
44 }
45
smush_read_header(AVFormatContext *ctx)46 static int smush_read_header(AVFormatContext *ctx)
47 {
48 SMUSHContext *smush = ctx->priv_data;
49 AVIOContext *pb = ctx->pb;
50 AVStream *vst, *ast;
51 uint32_t magic, nframes, size, subversion, i;
52 uint32_t width = 0, height = 0, got_audio = 0, read = 0;
53 uint32_t sample_rate, channels, palette[256];
54 int ret;
55
56 magic = avio_rb32(pb);
57 avio_skip(pb, 4); // skip movie size
58
59 if (magic == MKBETAG('A', 'N', 'I', 'M')) {
60 if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
61 return AVERROR_INVALIDDATA;
62
63 size = avio_rb32(pb);
64 if (size < 3 * 256 + 6)
65 return AVERROR_INVALIDDATA;
66
67 smush->version = 0;
68 subversion = avio_rl16(pb);
69 nframes = avio_rl16(pb);
70 if (!nframes)
71 return AVERROR_INVALIDDATA;
72
73 avio_skip(pb, 2); // skip pad
74
75 for (i = 0; i < 256; i++)
76 palette[i] = avio_rb24(pb);
77
78 avio_skip(pb, size - (3 * 256 + 6));
79 } else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
80 if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
81 return AVERROR_INVALIDDATA;
82
83 size = avio_rb32(pb);
84 if (size < 14)
85 return AVERROR_INVALIDDATA;
86
87 smush->version = 1;
88 subversion = avio_rl16(pb);
89 nframes = avio_rl32(pb);
90 if (!nframes)
91 return AVERROR_INVALIDDATA;
92
93 avio_skip(pb, 2); // skip pad
94 width = avio_rl16(pb);
95 height = avio_rl16(pb);
96 avio_skip(pb, 2); // skip pad
97 avio_skip(pb, size - 14);
98
99 if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
100 return AVERROR_INVALIDDATA;
101
102 size = avio_rb32(pb);
103 while (!got_audio && ((read + 8) < size)) {
104 uint32_t sig, chunk_size;
105
106 if (avio_feof(pb))
107 return AVERROR_EOF;
108
109 sig = avio_rb32(pb);
110 chunk_size = avio_rb32(pb);
111 read += 8;
112 switch (sig) {
113 case MKBETAG('W', 'a', 'v', 'e'):
114 got_audio = 1;
115 sample_rate = avio_rl32(pb);
116 if (!sample_rate)
117 return AVERROR_INVALIDDATA;
118
119 channels = avio_rl32(pb);
120 if (!channels)
121 return AVERROR_INVALIDDATA;
122
123 avio_skip(pb, chunk_size - 8);
124 read += chunk_size;
125 break;
126 case MKBETAG('B', 'l', '1', '6'):
127 case MKBETAG('A', 'N', 'N', 'O'):
128 avio_skip(pb, chunk_size);
129 read += chunk_size;
130 break;
131 default:
132 return AVERROR_INVALIDDATA;
133 }
134 }
135
136 avio_skip(pb, size - read);
137 } else {
138 av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
139 return AVERROR_INVALIDDATA;
140 }
141
142 vst = avformat_new_stream(ctx, 0);
143 if (!vst)
144 return AVERROR(ENOMEM);
145
146 smush->video_stream_index = vst->index;
147
148 avpriv_set_pts_info(vst, 64, 1, 15);
149
150 vst->start_time = 0;
151 vst->duration =
152 vst->nb_frames = nframes;
153 vst->avg_frame_rate = av_inv_q(vst->time_base);
154 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
155 vst->codecpar->codec_id = AV_CODEC_ID_SANM;
156 vst->codecpar->codec_tag = 0;
157 vst->codecpar->width = width;
158 vst->codecpar->height = height;
159
160 if (!smush->version) {
161 if ((ret = ff_alloc_extradata(vst->codecpar, 1024 + 2)) < 0)
162 return ret;
163
164 AV_WL16(vst->codecpar->extradata, subversion);
165 for (i = 0; i < 256; i++)
166 AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
167 }
168
169 if (got_audio) {
170 ast = avformat_new_stream(ctx, 0);
171 if (!ast)
172 return AVERROR(ENOMEM);
173
174 smush->audio_stream_index = ast->index;
175
176 ast->start_time = 0;
177 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
178 ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_VIMA;
179 ast->codecpar->codec_tag = 0;
180 ast->codecpar->sample_rate = sample_rate;
181 ast->codecpar->ch_layout.nb_channels = channels;
182 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
183 }
184
185 return 0;
186 }
187
smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)188 static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
189 {
190 SMUSHContext *smush = ctx->priv_data;
191 AVIOContext *pb = ctx->pb;
192 int done = 0;
193 int ret;
194
195 while (!done) {
196 uint32_t sig, size;
197
198 if (avio_feof(pb))
199 return AVERROR_EOF;
200
201 sig = avio_rb32(pb);
202 size = avio_rb32(pb);
203
204 switch (sig) {
205 case MKBETAG('F', 'R', 'M', 'E'):
206 if (smush->version)
207 break;
208 if ((ret = av_get_packet(pb, pkt, size)) < 0)
209 return ret;
210
211 pkt->stream_index = smush->video_stream_index;
212 done = 1;
213 break;
214 case MKBETAG('B', 'l', '1', '6'):
215 if ((ret = av_get_packet(pb, pkt, size)) < 0)
216 return ret;
217
218 pkt->stream_index = smush->video_stream_index;
219 pkt->duration = 1;
220 done = 1;
221 break;
222 case MKBETAG('W', 'a', 'v', 'e'):
223 if (size < 13)
224 return AVERROR_INVALIDDATA;
225 if (av_get_packet(pb, pkt, size) < 13)
226 return AVERROR(EIO);
227
228 pkt->stream_index = smush->audio_stream_index;
229 pkt->flags |= AV_PKT_FLAG_KEY;
230 pkt->duration = AV_RB32(pkt->data);
231 if (pkt->duration == 0xFFFFFFFFu)
232 pkt->duration = AV_RB32(pkt->data + 8);
233 done = 1;
234 break;
235 default:
236 avio_skip(pb, size);
237 break;
238 }
239 }
240
241 return 0;
242 }
243
244 const AVInputFormat ff_smush_demuxer = {
245 .name = "smush",
246 .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
247 .priv_data_size = sizeof(SMUSHContext),
248 .read_probe = smush_read_probe,
249 .read_header = smush_read_header,
250 .read_packet = smush_read_packet,
251 };
252