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 "demux.h"
27 #include "internal.h"
28 
29 typedef struct SMUSHContext {
30  int version;
33 } SMUSHContext;
34 
35 static 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];
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'))
62  return AVERROR_INVALIDDATA;
63 
64  size = avio_rb32(pb);
65  if (size < 3 * 256 + 6)
66  return AVERROR_INVALIDDATA;
67 
68  smush->version = 0;
69  subversion = avio_rl16(pb);
70  nframes = avio_rl16(pb);
71  if (!nframes)
72  return AVERROR_INVALIDDATA;
73 
74  avio_skip(pb, 2); // skip pad
75 
76  for (i = 0; i < 256; i++)
77  palette[i] = avio_rb24(pb);
78 
79  avio_skip(pb, size - (3 * 256 + 6));
80  } else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
81  if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
82  return AVERROR_INVALIDDATA;
83 
84  size = avio_rb32(pb);
85  if (size < 14)
86  return AVERROR_INVALIDDATA;
87 
88  smush->version = 1;
89  subversion = avio_rl16(pb);
90  nframes = avio_rl32(pb);
91  if (!nframes)
92  return AVERROR_INVALIDDATA;
93 
94  avio_skip(pb, 2); // skip pad
95  width = avio_rl16(pb);
96  height = avio_rl16(pb);
97  avio_skip(pb, 2); // skip pad
98  avio_skip(pb, size - 14);
99 
100  if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
101  return AVERROR_INVALIDDATA;
102 
103  size = avio_rb32(pb);
104  while (!got_audio && ((read + 8) < size)) {
105  uint32_t sig, chunk_size;
106 
107  if (avio_feof(pb))
108  return AVERROR_EOF;
109 
110  sig = avio_rb32(pb);
111  chunk_size = avio_rb32(pb);
112  read += 8;
113  switch (sig) {
114  case MKBETAG('W', 'a', 'v', 'e'):
115  got_audio = 1;
116  sample_rate = avio_rl32(pb);
117  if (!sample_rate)
118  return AVERROR_INVALIDDATA;
119 
120  channels = avio_rl32(pb);
121  if (!channels)
122  return AVERROR_INVALIDDATA;
123 
124  avio_skip(pb, chunk_size - 8);
125  read += chunk_size;
126  break;
127  case MKBETAG('B', 'l', '1', '6'):
128  case MKBETAG('A', 'N', 'N', 'O'):
129  avio_skip(pb, chunk_size);
130  read += chunk_size;
131  break;
132  default:
133  return AVERROR_INVALIDDATA;
134  }
135  }
136 
137  avio_skip(pb, size - read);
138  } else {
139  av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
140  return AVERROR_INVALIDDATA;
141  }
142 
143  vst = avformat_new_stream(ctx, 0);
144  if (!vst)
145  return AVERROR(ENOMEM);
146 
147  smush->video_stream_index = vst->index;
148 
149  avpriv_set_pts_info(vst, 64, 1, 15);
150 
151  vst->start_time = 0;
152  vst->duration =
153  vst->nb_frames = nframes;
154  vst->avg_frame_rate = av_inv_q(vst->time_base);
157  vst->codecpar->codec_tag = 0;
158  vst->codecpar->width = width;
159  vst->codecpar->height = height;
160 
161  if (!smush->version) {
162  if ((ret = ff_alloc_extradata(vst->codecpar, 1024 + 2)) < 0)
163  return ret;
164 
165  AV_WL16(vst->codecpar->extradata, subversion);
166  for (i = 0; i < 256; i++)
167  AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
168  }
169 
170  if (got_audio) {
171  ast = avformat_new_stream(ctx, 0);
172  if (!ast)
173  return AVERROR(ENOMEM);
174 
175  smush->audio_stream_index = ast->index;
176 
177  ast->start_time = 0;
180  ast->codecpar->codec_tag = 0;
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 
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 
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 
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  .p.name = "smush",
247  .p.long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
248  .priv_data_size = sizeof(SMUSHContext),
252 };
AV_CODEC_ID_SANM
@ AV_CODEC_ID_SANM
Definition: codec_id.h:234
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVERROR
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
smush_read_probe
static int smush_read_probe(const AVProbeData *p)
Definition: smush.c:35
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:832
SMUSHContext::version
int version
Definition: smush.c:30
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:577
sample_rate
sample_rate
Definition: ffmpeg_filter.c:409
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:853
smush_read_packet
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: smush.c:189
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
SMUSHContext
Definition: smush.c:29
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:713
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:760
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
width
#define width
intreadwrite.h
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:782
AV_CODEC_ID_ADPCM_VIMA
@ AV_CODEC_ID_ADPCM_VIMA
Definition: codec_id.h:397
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1297
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:804
ff_smush_demuxer
const FFInputFormat ff_smush_demuxer
Definition: smush.c:245
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:729
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:753
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
SMUSHContext::video_stream_index
int video_stream_index
Definition: smush.c:32
size
int size
Definition: twinvq_data.h:10344
avio.h
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_RB32
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:96
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:410
height
#define height
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVCodecParameters::height
int height
Definition: codec_par.h:135
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
demux.h
av_get_packet
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:103
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:749
smush_read_header
static int smush_read_header(AVFormatContext *ctx)
Definition: smush.c:47
AVPacket::stream_index
int stream_index
Definition: packet.h:524
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:317
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
FFInputFormat
Definition: demux.h:37
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:792
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1283
SMUSHContext::audio_stream_index
int audio_stream_index
Definition: smush.c:31
read
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.
Definition: bitstream_template.h:231
ff_alloc_extradata
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:239
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345