Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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;
32 } SMUSHContext;
33 
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 
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 
55  magic = avio_rb32(pb);
56  avio_skip(pb, 4); // skip movie size
57 
58  if (magic == MKBETAG('A', 'N', 'I', 'M')) {
59  if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
60  return AVERROR_INVALIDDATA;
61 
62  size = avio_rb32(pb);
63  if (size < 3 * 256 + 6)
64  return AVERROR_INVALIDDATA;
65 
66  smush->version = 0;
67  subversion = avio_rl16(pb);
68  nframes = avio_rl16(pb);
69  if (!nframes)
70  return AVERROR_INVALIDDATA;
71 
72  avio_skip(pb, 2); // skip pad
73 
74  for (i = 0; i < 256; i++)
75  palette[i] = avio_rb24(pb);
76 
77  avio_skip(pb, size - (3 * 256 + 6));
78  } else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
79  if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
80  return AVERROR_INVALIDDATA;
81 
82  size = avio_rb32(pb);
83  if (size < 14)
84  return AVERROR_INVALIDDATA;
85 
86  smush->version = 1;
87  subversion = avio_rl16(pb);
88  nframes = avio_rl32(pb);
89  if (!nframes)
90  return AVERROR_INVALIDDATA;
91 
92  avio_skip(pb, 2); // skip pad
93  width = avio_rl16(pb);
94  height = avio_rl16(pb);
95  avio_skip(pb, 2); // skip pad
96  avio_skip(pb, size - 14);
97 
98  if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
99  return AVERROR_INVALIDDATA;
100 
101  size = avio_rb32(pb);
102  while (!got_audio && ((read + 8) < size)) {
103  uint32_t sig, chunk_size;
104 
105  if (pb->eof_reached)
106  return AVERROR_EOF;
107 
108  sig = avio_rb32(pb);
109  chunk_size = avio_rb32(pb);
110  read += 8;
111  switch (sig) {
112  case MKBETAG('W', 'a', 'v', 'e'):
113  got_audio = 1;
114  sample_rate = avio_rl32(pb);
115  if (!sample_rate)
116  return AVERROR_INVALIDDATA;
117 
118  channels = avio_rl32(pb);
119  if (!channels)
120  return AVERROR_INVALIDDATA;
121 
122  avio_skip(pb, chunk_size - 8);
123  read += chunk_size;
124  break;
125  case MKBETAG('B', 'l', '1', '6'):
126  case MKBETAG('A', 'N', 'N', 'O'):
127  avio_skip(pb, chunk_size);
128  read += chunk_size;
129  break;
130  default:
131  return AVERROR_INVALIDDATA;
132  break;
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);
156  vst->codec->codec_tag = 0;
157  vst->codec->width = width;
158  vst->codec->height = height;
159 
160  if (!smush->version) {
161  av_free(vst->codec->extradata);
162  vst->codec->extradata_size = 1024 + 2;
165  if (!vst->codec->extradata)
166  return AVERROR(ENOMEM);
167 
168  AV_WL16(vst->codec->extradata, subversion);
169  for (i = 0; i < 256; i++)
170  AV_WL32(vst->codec->extradata + 2 + i * 4, palette[i]);
171  }
172 
173  if (got_audio) {
174  ast = avformat_new_stream(ctx, 0);
175  if (!ast)
176  return AVERROR(ENOMEM);
177 
178  smush->audio_stream_index = ast->index;
179 
180  ast->start_time = 0;
183  ast->codec->codec_tag = 0;
184  ast->codec->sample_rate = sample_rate;
185  ast->codec->channels = channels;
186 
187  avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
188  }
189 
190  return 0;
191 }
192 
194 {
195  SMUSHContext *smush = ctx->priv_data;
196  AVIOContext *pb = ctx->pb;
197  int done = 0;
198  int ret;
199 
200  while (!done) {
201  uint32_t sig, size;
202 
203  if (pb->eof_reached)
204  return AVERROR_EOF;
205 
206  sig = avio_rb32(pb);
207  size = avio_rb32(pb);
208 
209  switch (sig) {
210  case MKBETAG('F', 'R', 'M', 'E'):
211  if (smush->version)
212  break;
213  if ((ret = av_get_packet(pb, pkt, size)) < 0)
214  return ret;
215 
216  pkt->stream_index = smush->video_stream_index;
217  done = 1;
218  break;
219  case MKBETAG('B', 'l', '1', '6'):
220  if ((ret = av_get_packet(pb, pkt, size)) < 0)
221  return ret;
222 
223  pkt->stream_index = smush->video_stream_index;
224  pkt->duration = 1;
225  done = 1;
226  break;
227  case MKBETAG('W', 'a', 'v', 'e'):
228  if (size < 13)
229  return AVERROR_INVALIDDATA;
230  if (av_get_packet(pb, pkt, size) < 13)
231  return AVERROR(EIO);
232 
233  pkt->stream_index = smush->audio_stream_index;
234  pkt->flags |= AV_PKT_FLAG_KEY;
235  pkt->duration = AV_RB32(pkt->data);
236  if (pkt->duration == 0xFFFFFFFFu)
237  pkt->duration = AV_RB32(pkt->data + 8);
238  done = 1;
239  break;
240  default:
241  avio_skip(pb, size);
242  break;
243  }
244  }
245 
246  return 0;
247 }
248 
250  .name = "smush",
251  .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
252  .priv_data_size = sizeof(SMUSHContext),
256 };
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:62
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Buffered I/O operations.
int size
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:2800
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: smush.c:193
int index
stream index in AVFormatContext
Definition: avformat.h:700
AVInputFormat ff_smush_demuxer
Definition: smush.c:249
Format I/O context.
Definition: avformat.h:922
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
#define AV_RB32
Definition: intreadwrite.h:130
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1158
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2497
uint8_t * data
Definition: avcodec.h:967
#define AVERROR_EOF
End of file.
Definition: error.h:51
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:117
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:985
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1013
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:780
int video_stream_index
Definition: smush.c:31
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:973
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
int version
Definition: smush.c:29
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:588
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1218
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define AV_RL32
Definition: intreadwrite.h:146
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
static int width
Definition: utils.c:156
enum AVMediaType codec_type
Definition: avcodec.h:1052
enum AVCodecID codec_id
Definition: avcodec.h:1061
int sample_rate
samples per second
Definition: avcodec.h:1785
AVIOContext * pb
I/O context.
Definition: avformat.h:964
static int smush_read_header(AVFormatContext *ctx)
Definition: smush.c:46
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1076
int extradata_size
Definition: avcodec.h:1159
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:122
int height
Definition: gxfenc.c:72
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:756
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:548
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:749
static int smush_read_probe(AVProbeData *p)
Definition: smush.c:34
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:758
#define MKBETAG(a, b, c, d)
Definition: common.h:239
int audio_stream_index
Definition: smush.c:30
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1786
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
#define AV_WL16(p, d)
Definition: intreadwrite.h:225
int stream_index
Definition: avcodec.h:969
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:944