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 
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 (avio_feof(pb))
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  }
133  }
134 
135  avio_skip(pb, size - read);
136  } else {
137  av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
138  return AVERROR_INVALIDDATA;
139  }
140 
141  vst = avformat_new_stream(ctx, 0);
142  if (!vst)
143  return AVERROR(ENOMEM);
144 
145  smush->video_stream_index = vst->index;
146 
147  avpriv_set_pts_info(vst, 64, 1, 15);
148 
149  vst->start_time = 0;
150  vst->duration =
151  vst->nb_frames = nframes;
152  vst->avg_frame_rate = av_inv_q(vst->time_base);
155  vst->codecpar->codec_tag = 0;
156  vst->codecpar->width = width;
157  vst->codecpar->height = height;
158 
159  if (!smush->version) {
160  if (ff_alloc_extradata(vst->codecpar, 1024 + 2))
161  return AVERROR(ENOMEM);
162 
163  AV_WL16(vst->codecpar->extradata, subversion);
164  for (i = 0; i < 256; i++)
165  AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
166  }
167 
168  if (got_audio) {
169  ast = avformat_new_stream(ctx, 0);
170  if (!ast)
171  return AVERROR(ENOMEM);
172 
173  smush->audio_stream_index = ast->index;
174 
175  ast->start_time = 0;
178  ast->codecpar->codec_tag = 0;
180  ast->codecpar->channels = channels;
181 
182  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
183  }
184 
185  return 0;
186 }
187 
189 {
190  SMUSHContext *smush = ctx->priv_data;
191  AVIOContext *pb = ctx->pb;
192  int done = 0;
193  int ret;
194 
195  while (!done) {
196  uint32_t sig, size;
197 
198  if (avio_feof(pb))
199  return AVERROR_EOF;
200 
201  sig = avio_rb32(pb);
202  size = avio_rb32(pb);
203 
204  switch (sig) {
205  case MKBETAG('F', 'R', 'M', 'E'):
206  if (smush->version)
207  break;
208  if ((ret = av_get_packet(pb, pkt, size)) < 0)
209  return ret;
210 
212  done = 1;
213  break;
214  case MKBETAG('B', 'l', '1', '6'):
215  if ((ret = av_get_packet(pb, pkt, size)) < 0)
216  return ret;
217 
219  pkt->duration = 1;
220  done = 1;
221  break;
222  case MKBETAG('W', 'a', 'v', 'e'):
223  if (size < 13)
224  return AVERROR_INVALIDDATA;
225  if (av_get_packet(pb, pkt, size) < 13)
226  return AVERROR(EIO);
227 
230  pkt->duration = AV_RB32(pkt->data);
231  if (pkt->duration == 0xFFFFFFFFu)
232  pkt->duration = AV_RB32(pkt->data + 8);
233  done = 1;
234  break;
235  default:
236  avio_skip(pb, size);
237  break;
238  }
239  }
240 
241  return 0;
242 }
243 
245  .name = "smush",
246  .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
247  .priv_data_size = sizeof(SMUSHContext),
251 };
AV_CODEC_ID_SANM
@ AV_CODEC_ID_SANM
Definition: avcodec.h:400
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3971
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
smush_read_probe
static int smush_read_probe(const AVProbeData *p)
Definition: smush.c:34
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:366
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:943
SMUSHContext::version
int version
Definition: smush.c:29
channels
channels
Definition: aptx.c:30
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1495
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3961
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
sample_rate
sample_rate
Definition: ffmpeg_filter.c:191
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:4063
smush_read_packet
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: smush.c:188
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:919
SMUSHContext
Definition: smush.c:28
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:753
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:800
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVInputFormat
Definition: avformat.h:640
width
#define width
intreadwrite.h
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:4023
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
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:899
ff_smush_demuxer
AVInputFormat ff_smush_demuxer
Definition: smush.c:244
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AV_CODEC_ID_ADPCM_VIMA
@ AV_CODEC_ID_ADPCM_VIMA
Definition: avcodec.h:532
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:4067
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:921
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:769
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:793
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:188
SMUSHContext::video_stream_index
int video_stream_index
Definition: smush.c:31
avpriv_set_pts_info
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:4910
size
int size
Definition: twinvq_data.h:11134
avio.h
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:92
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: common.h:367
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
height
#define height
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVCodecParameters::height
int height
Definition: avcodec.h:4024
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
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:313
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:870
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:871
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
smush_read_header
static int smush_read_header(AVFormatContext *ctx)
Definition: smush.c:46
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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:909
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1370
SMUSHContext::audio_stream_index
int audio_stream_index
Definition: smush.c:30
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:3309
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:358