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 
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "avio.h"
26 
27 typedef struct {
28  int version;
31 } SMUSHContext;
32 
34 {
35  if (((AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M') &&
36  AV_RL32(p->buf + 8) == MKTAG('S', 'H', 'D', 'R')) ||
37  (AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M') &&
38  AV_RL32(p->buf + 8) == MKTAG('A', 'H', 'D', 'R')))) {
39  return AVPROBE_SCORE_MAX;
40  }
41 
42  return 0;
43 }
44 
46 {
47  SMUSHContext *smush = ctx->priv_data;
48  AVIOContext *pb = ctx->pb;
49  AVStream *vst, *ast;
50  uint32_t magic, nframes, size, subversion, i;
51  uint32_t width = 0, height = 0, got_audio = 0, read = 0;
52  uint32_t sample_rate, channels, palette[256];
53 
54  magic = avio_rb32(pb);
55  avio_skip(pb, 4); // skip movie size
56 
57  if (magic == MKBETAG('A', 'N', 'I', 'M')) {
58  if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
59  return AVERROR_INVALIDDATA;
60 
61  size = avio_rb32(pb);
62  if (size < 3 * 256 + 6)
63  return AVERROR_INVALIDDATA;
64 
65  smush->version = 0;
66  subversion = avio_rl16(pb);
67  nframes = avio_rl16(pb);
68 
69  avio_skip(pb, 2); // skip pad
70 
71  for (i = 0; i < 256; i++)
72  palette[i] = avio_rb24(pb);
73 
74  avio_skip(pb, size - (3 * 256 + 6));
75  } else if (magic == MKBETAG('S', 'A', 'N', 'M') ) {
76  if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
77  return AVERROR_INVALIDDATA;
78 
79  size = avio_rb32(pb);
80  if (size < 14)
81  return AVERROR_INVALIDDATA;
82 
83  smush->version = 1;
84  subversion = avio_rl16(pb);
85  nframes = avio_rl32(pb);
86  avio_skip(pb, 2); // skip pad
87  width = avio_rl16(pb);
88  height = avio_rl16(pb);
89  avio_skip(pb, 2); // skip pad
90  avio_skip(pb, size - 14);
91 
92  if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
93  return AVERROR_INVALIDDATA;
94 
95  size = avio_rb32(pb);
96  while (!got_audio && ((read + 8) < size)) {
97  uint32_t sig, chunk_size;
98 
99  if (url_feof(pb))
100  return AVERROR_EOF;
101 
102  sig = avio_rb32(pb);
103  chunk_size = avio_rb32(pb);
104  read += 8;
105  switch (sig) {
106  case MKBETAG('W', 'a', 'v', 'e'):
107  got_audio = 1;
108  sample_rate = avio_rl32(pb);
109  channels = avio_rl32(pb);
110  avio_skip(pb, chunk_size - 8);
111  read += chunk_size;
112  break;
113  case MKBETAG('B', 'l', '1', '6'):
114  case MKBETAG('A', 'N', 'N', 'O'):
115  avio_skip(pb, chunk_size);
116  read += chunk_size;
117  break;
118  default:
119  return AVERROR_INVALIDDATA;
120  break;
121  }
122  }
123 
124  avio_skip(pb, size - read);
125  } else {
126  av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
127  return AVERROR_INVALIDDATA;
128  }
129 
130  vst = avformat_new_stream(ctx, 0);
131  if (!vst)
132  return AVERROR(ENOMEM);
133 
134  smush->video_stream_index = vst->index;
135 
136  vst->start_time = 0;
137  vst->duration =
138  vst->nb_frames = nframes;
141  vst->codec->codec_tag = 0;
142  vst->codec->width = width;
143  vst->codec->height = height;
144 
145  avpriv_set_pts_info(vst, 64, 66667, 1000000);
146 
147  if (!smush->version) {
149  if (!vst->codec->extradata)
150  return AVERROR(ENOMEM);
151 
152  vst->codec->extradata_size = 1024 + 2;
153  AV_WL16(vst->codec->extradata, subversion);
154  for (i = 0; i < 256; i++)
155  AV_WL32(vst->codec->extradata + 2 + i * 4, palette[i]);
156  }
157 
158  if (got_audio) {
159  ast = avformat_new_stream(ctx, 0);
160  if (!ast)
161  return AVERROR(ENOMEM);
162 
163  smush->audio_stream_index = ast->index;
164 
165  ast->start_time = 0;
168  ast->codec->codec_tag = 0;
169  ast->codec->sample_rate = sample_rate;
170  ast->codec->channels = channels;
171 
172  avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
173  }
174 
175  return 0;
176 }
177 
179 {
180  SMUSHContext *smush = ctx->priv_data;
181  AVIOContext *pb = ctx->pb;
182  int done = 0;
183 
184  while (!done) {
185  uint32_t sig, size;
186 
187  if (url_feof(pb))
188  return AVERROR_EOF;
189 
190  sig = avio_rb32(pb);
191  size = avio_rb32(pb);
192 
193  switch (sig) {
194  case MKBETAG('F', 'R', 'M', 'E'):
195  if (smush->version)
196  break;
197  if (av_get_packet(pb, pkt, size) < 0)
198  return AVERROR(EIO);
199 
200  pkt->stream_index = smush->video_stream_index;
201  done = 1;
202  break;
203  case MKBETAG('B', 'l', '1', '6'):
204  if (av_get_packet(pb, pkt, size) < 0)
205  return AVERROR(EIO);
206 
207  pkt->stream_index = smush->video_stream_index;
208  pkt->duration = 1;
209  done = 1;
210  break;
211  case MKBETAG('W', 'a', 'v', 'e'):
212  if (size < 13)
213  return AVERROR_INVALIDDATA;
214  if (av_get_packet(pb, pkt, size) < 13)
215  return AVERROR(EIO);
216 
217  pkt->stream_index = smush->audio_stream_index;
218  pkt->flags |= AV_PKT_FLAG_KEY;
219  pkt->duration = AV_RB32(pkt->data);
220  if (pkt->duration == 0xFFFFFFFFu)
221  pkt->duration = AV_RB32(pkt->data + 8);
222  done = 1;
223  break;
224  default:
225  avio_skip(pb, size);
226  break;
227  }
228  }
229 
230  return 0;
231 }
232 
234  .name = "smush",
235  .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
236  .priv_data_size = sizeof(SMUSHContext),
240 };
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: smush.c:178
int index
stream index in AVFormatContext
Definition: avformat.h:644
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
AVInputFormat ff_smush_demuxer
Definition: smush.c:233
Format I/O context.
Definition: avformat.h:944
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
#define AV_RB32
static AVPacket pkt
Definition: demuxing.c:56
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
#define AVERROR_EOF
End of file.
Definition: error.h:55
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.
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
#define AV_WL16(p, darg)
Definition: intreadwrite.h:250
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int video_stream_index
Definition: smush.c:30
int size
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int version
Definition: smush.c:28
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:603
normalize analysis window nframes
Definition: stft.m:13
static int read_probe(AVProbeData *pd)
int width
picture width / height.
#define AV_RL32
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
static int width
Definition: tests/utils.c:158
sample_rate
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
static int smush_read_header(AVFormatContext *ctx)
Definition: smush.c:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
int palette
Definition: v4l.c:61
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:689
static int smush_read_probe(AVProbeData *p)
Definition: smush.c:33
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:698
#define MKBETAG(a, b, c, d)
Definition: common.h:283
int audio_stream_index
Definition: smush.c:29
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.