FFmpeg
Loading...
Searching...
No Matches
smush.c
Go to the documentation of this file.
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
23
24#include "avformat.h"
25#include "avio.h"
26#include "demux.h"
27#include "internal.h"
28
34
35static int smush_read_probe(const AVProbeData *p)
36{
37 if (((AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M') &&
38 AV_RL32(p->buf + 8) == MKTAG('S', 'H', 'D', 'R')) ||
39 (AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M') &&
40 AV_RL32(p->buf + 8) == MKTAG('A', 'H', 'D', 'R')))) {
41 return AVPROBE_SCORE_MAX;
42 }
43
44 return 0;
45}
46
48{
49 SMUSHContext *smush = ctx->priv_data;
50 AVIOContext *pb = ctx->pb;
51 AVStream *vst, *ast;
52 uint32_t magic, nframes, size, subversion, i;
53 uint32_t width = 0, height = 0, got_audio = 0, read = 0;
54 uint32_t sample_rate, channels, palette[256], frame_rate = 15;
55 int ret;
56
57 magic = avio_rb32(pb);
58 avio_skip(pb, 4); // skip movie size
59
60 if (magic == MKBETAG('A', 'N', 'I', 'M')) {
61 if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
63
64 size = avio_rb32(pb);
65 if (size < 3 * 256 + 6)
67 size -= 3 * 256 + 6;
68
69 smush->version = 0;
70 subversion = avio_rl16(pb);
71 nframes = avio_rl16(pb);
72 if (!nframes)
74
75 avio_skip(pb, 2); // skip pad
76
77 for (i = 0; i < 256; i++)
78 palette[i] = avio_rb24(pb);
79
80 if (subversion > 1) {
81 if (size < 12)
83 size -= 12;
84 frame_rate = avio_rl32(pb);
85 avio_skip(pb, 4); // max size of FRME chunk in file
86 sample_rate = avio_rl32(pb);
87 if (frame_rate < 1 || frame_rate > 70)
88 frame_rate = 12;
89 }
90 avio_skip(pb, size);
91 } else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
92 if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
94
95 size = avio_rb32(pb);
96 if (size < 14)
98
99 smush->version = 1;
100 subversion = avio_rl16(pb);
101 nframes = avio_rl32(pb);
102 if (!nframes)
103 return AVERROR_INVALIDDATA;
104
105 avio_skip(pb, 2); // skip pad
106 width = avio_rl16(pb);
107 height = avio_rl16(pb);
108 avio_skip(pb, 2); // skip pad
109 avio_skip(pb, size - 14);
110
111 if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
112 return AVERROR_INVALIDDATA;
113
114 size = avio_rb32(pb);
115 while (!got_audio && ((read + 8) < size)) {
116 uint32_t sig, chunk_size;
117
118 if (avio_feof(pb))
119 return AVERROR_EOF;
120
121 sig = avio_rb32(pb);
122 chunk_size = avio_rb32(pb);
123 read += 8;
124 switch (sig) {
125 case MKBETAG('W', 'a', 'v', 'e'):
126 got_audio = 1;
127 sample_rate = avio_rl32(pb);
128 if (!sample_rate)
129 return AVERROR_INVALIDDATA;
130
131 channels = avio_rl32(pb);
132 if (!channels)
133 return AVERROR_INVALIDDATA;
134
135 avio_skip(pb, chunk_size - 8);
136 read += chunk_size;
137 break;
138 case MKBETAG('B', 'l', '1', '6'):
139 case MKBETAG('A', 'N', 'N', 'O'):
140 avio_skip(pb, chunk_size);
141 read += chunk_size;
142 break;
143 default:
144 return AVERROR_INVALIDDATA;
145 }
146 }
147
148 avio_skip(pb, size - read);
149 } else {
150 av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
151 return AVERROR_INVALIDDATA;
152 }
153
154 vst = avformat_new_stream(ctx, 0);
155 if (!vst)
156 return AVERROR(ENOMEM);
157
158 smush->video_stream_index = vst->index;
159
160 avpriv_set_pts_info(vst, 64, 1, frame_rate);
161
162 vst->start_time = 0;
163 vst->duration =
164 vst->nb_frames = nframes;
165 vst->avg_frame_rate = av_inv_q(vst->time_base);
168 vst->codecpar->codec_tag = 0;
169 vst->codecpar->width = width;
170 vst->codecpar->height = height;
171
172 if (!smush->version) {
173 if ((ret = ff_alloc_extradata(vst->codecpar, 1024 + 2)) < 0)
174 return ret;
175
176 AV_WL16(vst->codecpar->extradata, subversion);
177 for (i = 0; i < 256; i++)
178 AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
179 }
180
181 if (got_audio) {
182 ast = avformat_new_stream(ctx, 0);
183 if (!ast)
184 return AVERROR(ENOMEM);
185
186 smush->audio_stream_index = ast->index;
187
188 ast->start_time = 0;
191 ast->codecpar->codec_tag = 0;
192 ast->codecpar->sample_rate = sample_rate;
194 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
195 }
196
197 return 0;
198}
199
201{
202 SMUSHContext *smush = ctx->priv_data;
203 AVIOContext *pb = ctx->pb;
204 int done = 0;
205 int ret;
206
207 while (!done) {
208 uint32_t sig, size;
209
210 if (avio_feof(pb))
211 return AVERROR_EOF;
212
213 sig = avio_rb32(pb);
214 size = avio_rb32(pb);
215
216 switch (sig) {
217 case MKBETAG('F', 'R', 'M', 'E'):
218 if (smush->version)
219 break;
220 if ((ret = av_get_packet(pb, pkt, size)) < 0)
221 return ret;
222
223 pkt->stream_index = smush->video_stream_index;
224 done = 1;
225 break;
226 case MKBETAG('B', 'l', '1', '6'):
227 if ((ret = av_get_packet(pb, pkt, size)) < 0)
228 return ret;
229
230 pkt->stream_index = smush->video_stream_index;
231 pkt->duration = 1;
232 done = 1;
233 break;
234 case MKBETAG('W', 'a', 'v', 'e'):
235 if (size < 13)
236 return AVERROR_INVALIDDATA;
237 if (av_get_packet(pb, pkt, size) < 13)
238 return AVERROR_INVALIDDATA;
239
240 pkt->stream_index = smush->audio_stream_index;
241 pkt->flags |= AV_PKT_FLAG_KEY;
242 pkt->duration = AV_RB32(pkt->data);
243 if (pkt->duration == 0xFFFFFFFFu)
244 pkt->duration = AV_RB32(pkt->data + 8);
245 done = 1;
246 break;
247 default:
248 avio_skip(pb, size);
249 break;
250 }
251 }
252
253 return 0;
254}
255
257 .p.name = "smush",
258 .p.long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
259 .priv_data_size = sizeof(SMUSHContext),
263};
const FFInputFormat ff_smush_demuxer
Definition smush.c:256
static AVFormatContext * ctx
channels
Definition aptx.h:31
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
Buffered I/O operations.
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
unsigned int avio_rb24(AVIOContext *s)
Definition aviobuf.c:757
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_SANM
Definition codec_id.h:231
@ AV_CODEC_ID_ADPCM_VIMA
Definition codec_id.h:400
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_WL16(p, v)
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define MKBETAG(a, b, c, d)
Definition macros.h:56
static volatile sig_atomic_t sig
Definition signal.c:48
static int smush_read_probe(const AVProbeData *p)
Definition smush.c:35
static int smush_read_header(AVFormatContext *ctx)
Definition smush.c:47
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition smush.c:200
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
int version
Definition smush.c:30
int video_stream_index
Definition smush.c:32
int audio_stream_index
Definition smush.c:31
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int size