]> git.sesse.net Git - ffmpeg/blob - libavformat/smush.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / smush.c
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 #include "avformat.h"
24 #include "internal.h"
25 #include "avio.h"
26
27 typedef struct {
28     int version;
29     int audio_stream_index;
30     int video_stream_index;
31 } SMUSHContext;
32
33 static int smush_read_probe(AVProbeData *p)
34 {
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;
38     }
39
40     return 0;
41 }
42
43 static int smush_read_header(AVFormatContext *ctx)
44 {
45     SMUSHContext *smush = ctx->priv_data;
46     AVIOContext *pb = ctx->pb;
47     AVStream *vst, *ast;
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];
51
52     magic = avio_rb32(pb);
53     avio_skip(pb, 4); // skip movie size
54
55     if (magic == MKBETAG('A', 'N', 'I', 'M')) {
56         if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
57             return AVERROR_INVALIDDATA;
58
59         size = avio_rb32(pb);
60         if (size < 3 * 256 + 6)
61             return AVERROR_INVALIDDATA;
62
63         smush->version = 0;
64         subversion     = avio_rl16(pb);
65         nframes        = avio_rl16(pb);
66
67         avio_skip(pb, 2); // skip pad
68
69         for (i = 0; i < 256; i++)
70             palette[i] = avio_rb24(pb);
71
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;
76
77         size = avio_rb32(pb);
78         if (size < 14)
79             return AVERROR_INVALIDDATA;
80
81         smush->version = 1;
82         avio_skip(pb, 2); // skip version
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);
89
90         if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
91             return AVERROR_INVALIDDATA;
92
93         size = avio_rb32(pb);
94         while (!got_audio && ((read + 8) < size)) {
95             uint32_t sig, chunk_size;
96
97             if (url_feof(pb))
98                 return AVERROR_EOF;
99
100             sig        = avio_rb32(pb);
101             chunk_size = avio_rb32(pb);
102             read += 8;
103             switch (sig) {
104             case MKBETAG('W', 'a', 'v', 'e'):
105                 got_audio = 1;
106                 sample_rate = avio_rl32(pb);
107                 channels    = avio_rl32(pb);
108                 avio_skip(pb, chunk_size - 8);
109                 read += chunk_size;
110                 break;
111             case MKBETAG('B', 'l', '1', '6'):
112             case MKBETAG('A', 'N', 'N', 'O'):
113                 avio_skip(pb, chunk_size);
114                 read += chunk_size;
115                 break;
116             default:
117                 return AVERROR_INVALIDDATA;
118                 break;
119             }
120         }
121
122         avio_skip(pb, size - read);
123     } else {
124         av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
125         return AVERROR_INVALIDDATA;
126     }
127
128     vst = avformat_new_stream(ctx, 0);
129     if (!vst)
130         return AVERROR(ENOMEM);
131
132     smush->video_stream_index = vst->index;
133
134     vst->start_time        = 0;
135     vst->duration          =
136     vst->nb_frames         = nframes;
137     vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
138     vst->codec->codec_id   = CODEC_ID_SANM;
139     vst->codec->codec_tag  = 0;
140     vst->codec->width      = width;
141     vst->codec->height     = height;
142
143     avpriv_set_pts_info(vst, 64, 66667, 1000000);
144
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);
149
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]);
154     }
155
156     if (got_audio) {
157         ast = avformat_new_stream(ctx, 0);
158         if (!ast)
159             return AVERROR(ENOMEM);
160
161         smush->audio_stream_index = ast->index;
162
163         ast->start_time         = 0;
164         ast->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
165         ast->codec->codec_id    = CODEC_ID_VIMA;
166         ast->codec->codec_tag   = 0;
167         ast->codec->sample_rate = sample_rate;
168         ast->codec->channels    = channels;
169
170         avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
171     }
172
173     return 0;
174 }
175
176 static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
177 {
178     SMUSHContext *smush = ctx->priv_data;
179     AVIOContext *pb = ctx->pb;
180     int done = 0;
181
182     while (!done) {
183         uint32_t sig, size;
184
185         if (url_feof(pb))
186             return AVERROR_EOF;
187
188         sig    = avio_rb32(pb);
189         size   = avio_rb32(pb);
190
191         switch (sig) {
192         case MKBETAG('F', 'R', 'M', 'E'):
193             if (smush->version)
194                 break;
195             if (av_get_packet(pb, pkt, size) < 0)
196                 return AVERROR(EIO);
197
198             pkt->stream_index = smush->video_stream_index;
199             done = 1;
200             break;
201         case MKBETAG('B', 'l', '1', '6'):
202             if (av_get_packet(pb, pkt, size) < 0)
203                 return AVERROR(EIO);
204
205             pkt->stream_index = smush->video_stream_index;
206             pkt->duration = 1;
207             done = 1;
208             break;
209         case MKBETAG('W', 'a', 'v', 'e'):
210             if (size < 13)
211                 return AVERROR_INVALIDDATA;
212             if (av_get_packet(pb, pkt, size) < 0)
213                 return AVERROR(EIO);
214
215             pkt->stream_index = smush->audio_stream_index;
216             pkt->duration = AV_RB32(pkt->data);
217             if (pkt->duration == 0xFFFFFFFFu)
218                 pkt->duration = AV_RB32(pkt->data + 8);
219             done = 1;
220             break;
221         default:
222             avio_skip(pb, size);
223             break;
224         }
225     }
226
227     return 0;
228 }
229
230 AVInputFormat ff_smush_demuxer = {
231     .name           = "smush",
232     .long_name      = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
233     .priv_data_size = sizeof(SMUSHContext),
234     .read_probe     = smush_read_probe,
235     .read_header    = smush_read_header,
236     .read_packet    = smush_read_packet,
237 };