FFmpeg  2.1.1
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) {
148  if (ff_alloc_extradata(vst->codec, 1024 + 2))
149  return AVERROR(ENOMEM);
150 
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;
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 
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) < 13)
213  return AVERROR(EIO);
214 
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);
220  done = 1;
221  break;
222  default:
223  avio_skip(pb, size);
224  break;
225  }
226  }
227 
228  return 0;
229 }
230 
232  .name = "smush",
233  .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
234  .priv_data_size = sizeof(SMUSHContext),
238 };
Bytestream IO Context.
Definition: avio.h:68
int size
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
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.
Definition: utils.c:3922
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: smush.c:176
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:593
int index
stream index in AVFormatContext
Definition: avformat.h:668
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:624
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
AVInputFormat ff_smush_demuxer
Definition: smush.c:231
Format I/O context.
Definition: avformat.h:968
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:617
int duration
Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.
Definition: avcodec.h:1085
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:200
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void * priv_data
Format private data.
Definition: avformat.h:988
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:577
int video_stream_index
Definition: smush.c:30
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
int version
Definition: smush.c:28
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
int width
picture width / height.
Definition: avcodec.h:1314
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
#define AV_WL16(p, darg)
Definition: intreadwrite.h:250
static int width
Definition: utils.c:158
sample_rate
Buffered I/O operations.
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
int sample_rate
samples per second
Definition: avcodec.h:1873
static int smush_read_header(AVFormatContext *ctx)
Definition: smush.c:45
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
int ff_alloc_extradata(AVCodecContext *avctx, int size)
Allocate extradata with additional FF_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:2690
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
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
#define MKTAG(a, b, c, d)
uint8_t * data
Definition: avcodec.h:1063
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
#define MKBETAG(a, b, c, d)
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
#define AVERROR_EOF
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:342
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:720
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
int palette
Definition: v4l.c:60
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:713
static int smush_read_probe(AVProbeData *p)
Definition: smush.c:33
Main libavformat public API header.
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:722
int audio_stream_index
Definition: smush.c:29
#define AV_RB32(x)
Definition: intreadwrite.h:258
#define AVERROR_INVALIDDATA
#define AV_RL32(x)
Definition: intreadwrite.h:275
int channels
number of audio channels
Definition: avcodec.h:1874
#define AVERROR(e)
static AVPacket pkt
Definition: demuxing.c:52
int stream_index
Definition: avcodec.h:1065
This structure stores compressed data.
Definition: avcodec.h:1040