FFmpeg
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 
24 #include "avformat.h"
25 #include "avio.h"
26 #include "internal.h"
27 
28 typedef struct SMUSHContext {
29  int version;
32 } SMUSHContext;
33 
34 static int smush_read_probe(const AVProbeData *p)
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  int ret;
55 
56  magic = avio_rb32(pb);
57  avio_skip(pb, 4); // skip movie size
58 
59  if (magic == MKBETAG('A', 'N', 'I', 'M')) {
60  if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
61  return AVERROR_INVALIDDATA;
62 
63  size = avio_rb32(pb);
64  if (size < 3 * 256 + 6)
65  return AVERROR_INVALIDDATA;
66 
67  smush->version = 0;
68  subversion = avio_rl16(pb);
69  nframes = avio_rl16(pb);
70  if (!nframes)
71  return AVERROR_INVALIDDATA;
72 
73  avio_skip(pb, 2); // skip pad
74 
75  for (i = 0; i < 256; i++)
76  palette[i] = avio_rb24(pb);
77 
78  avio_skip(pb, size - (3 * 256 + 6));
79  } else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
80  if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
81  return AVERROR_INVALIDDATA;
82 
83  size = avio_rb32(pb);
84  if (size < 14)
85  return AVERROR_INVALIDDATA;
86 
87  smush->version = 1;
88  subversion = avio_rl16(pb);
89  nframes = avio_rl32(pb);
90  if (!nframes)
91  return AVERROR_INVALIDDATA;
92 
93  avio_skip(pb, 2); // skip pad
94  width = avio_rl16(pb);
95  height = avio_rl16(pb);
96  avio_skip(pb, 2); // skip pad
97  avio_skip(pb, size - 14);
98 
99  if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
100  return AVERROR_INVALIDDATA;
101 
102  size = avio_rb32(pb);
103  while (!got_audio && ((read + 8) < size)) {
104  uint32_t sig, chunk_size;
105 
106  if (avio_feof(pb))
107  return AVERROR_EOF;
108 
109  sig = avio_rb32(pb);
110  chunk_size = avio_rb32(pb);
111  read += 8;
112  switch (sig) {
113  case MKBETAG('W', 'a', 'v', 'e'):
114  got_audio = 1;
115  sample_rate = avio_rl32(pb);
116  if (!sample_rate)
117  return AVERROR_INVALIDDATA;
118 
119  channels = avio_rl32(pb);
120  if (!channels)
121  return AVERROR_INVALIDDATA;
122 
123  avio_skip(pb, chunk_size - 8);
124  read += chunk_size;
125  break;
126  case MKBETAG('B', 'l', '1', '6'):
127  case MKBETAG('A', 'N', 'N', 'O'):
128  avio_skip(pb, chunk_size);
129  read += chunk_size;
130  break;
131  default:
132  return AVERROR_INVALIDDATA;
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->codecpar->codec_tag = 0;
157  vst->codecpar->width = width;
158  vst->codecpar->height = height;
159 
160  if (!smush->version) {
161  if ((ret = ff_alloc_extradata(vst->codecpar, 1024 + 2)) < 0)
162  return ret;
163 
164  AV_WL16(vst->codecpar->extradata, subversion);
165  for (i = 0; i < 256; i++)
166  AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
167  }
168 
169  if (got_audio) {
170  ast = avformat_new_stream(ctx, 0);
171  if (!ast)
172  return AVERROR(ENOMEM);
173 
174  smush->audio_stream_index = ast->index;
175 
176  ast->start_time = 0;
179  ast->codecpar->codec_tag = 0;
181  ast->codecpar->channels = channels;
182 
183  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
184  }
185 
186  return 0;
187 }
188 
190 {
191  SMUSHContext *smush = ctx->priv_data;
192  AVIOContext *pb = ctx->pb;
193  int done = 0;
194  int ret;
195 
196  while (!done) {
197  uint32_t sig, size;
198 
199  if (avio_feof(pb))
200  return AVERROR_EOF;
201 
202  sig = avio_rb32(pb);
203  size = avio_rb32(pb);
204 
205  switch (sig) {
206  case MKBETAG('F', 'R', 'M', 'E'):
207  if (smush->version)
208  break;
209  if ((ret = av_get_packet(pb, pkt, size)) < 0)
210  return ret;
211 
212  pkt->stream_index = smush->video_stream_index;
213  done = 1;
214  break;
215  case MKBETAG('B', 'l', '1', '6'):
216  if ((ret = av_get_packet(pb, pkt, size)) < 0)
217  return ret;
218 
219  pkt->stream_index = smush->video_stream_index;
220  pkt->duration = 1;
221  done = 1;
222  break;
223  case MKBETAG('W', 'a', 'v', 'e'):
224  if (size < 13)
225  return AVERROR_INVALIDDATA;
226  if (av_get_packet(pb, pkt, size) < 13)
227  return AVERROR(EIO);
228 
229  pkt->stream_index = smush->audio_stream_index;
230  pkt->flags |= AV_PKT_FLAG_KEY;
231  pkt->duration = AV_RB32(pkt->data);
232  if (pkt->duration == 0xFFFFFFFFu)
233  pkt->duration = AV_RB32(pkt->data + 8);
234  done = 1;
235  break;
236  default:
237  avio_skip(pb, size);
238  break;
239  }
240  }
241 
242  return 0;
243 }
244 
246  .name = "smush",
247  .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
248  .priv_data_size = sizeof(SMUSHContext),
252 };
Bytestream IO Context.
Definition: avio.h:161
#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.
Definition: utils.c:4928
channels
Definition: aptx.c:30
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: smush.c:189
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3981
int index
stream index in AVFormatContext
Definition: avformat.h:881
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:334
static AVPacket pkt
AVInputFormat ff_smush_demuxer
Definition: smush.c:245
Format I/O context.
Definition: avformat.h:1357
int width
Video only.
Definition: avcodec.h:4047
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:803
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1511
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4501
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
#define height
uint8_t * data
Definition: avcodec.h:1493
#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.
Definition: utils.c:308
ptrdiff_t size
Definition: opengl_enc.c:100
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1525
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:772
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3977
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:953
int video_stream_index
Definition: smush.c:31
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1499
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
int version
Definition: smush.c:29
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:3320
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:796
#define width
static int smush_read_probe(const AVProbeData *p)
Definition: smush.c:34
AVFormatContext * ctx
Definition: movenc.c:48
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
Stream structure.
Definition: avformat.h:880
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
sample_rate
AVIOContext * pb
I/O context.
Definition: avformat.h:1399
static int smush_read_header(AVFormatContext *ctx)
Definition: smush.c:46
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:929
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int sample_rate
Audio only.
Definition: avcodec.h:4091
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:756
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:919
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:931
#define MKBETAG(a, b, c, d)
Definition: common.h:367
int audio_stream_index
Definition: smush.c:30
void * priv_data
Format private data.
Definition: avformat.h:1385
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3995
int channels
Audio only.
Definition: avcodec.h:4087
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1027
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:361
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3985
int stream_index
Definition: avcodec.h:1495
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:909
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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.Frame references ownership and permissions
#define MKTAG(a, b, c, d)
Definition: common.h:366
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1470
#define AV_WL32(p, v)
Definition: intreadwrite.h:426