FFmpeg
smacker.c
Go to the documentation of this file.
1 /*
2  * Smacker demuxer
3  * Copyright (c) 2006 Konstantin Shishkov
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 /*
23  * Based on http://wiki.multimedia.cx/index.php?title=Smacker
24  */
25 
26 #include <inttypes.h>
27 
29 #include "libavutil/intreadwrite.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 
34 #define SMACKER_PAL 0x01
35 #define SMACKER_FLAG_RING_FRAME 0x01
36 
37 enum SAudFlags {
43 };
44 
45 typedef struct SmackerContext {
46  uint32_t frames;
47  /* frame info */
48  uint32_t *frm_size;
50  /* internal variables */
51  int64_t next_frame_pos;
52  int cur_frame;
54  int indexes[7];
55  /* current frame for demuxing */
56  uint32_t frame_size;
57  int flags;
60  uint8_t pal[768];
61  int64_t aud_pts[7];
63 
64 /* palette used in Smacker */
65 static const uint8_t smk_pal[64] = {
66  0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
67  0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
68  0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
69  0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
70  0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
71  0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
72  0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
73  0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
74 };
75 
76 
77 static int smacker_probe(const AVProbeData *p)
78 {
79  if ( AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
80  && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
81  return 0;
82 
83  if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
84  return AVPROBE_SCORE_MAX/4;
85 
86  return AVPROBE_SCORE_MAX;
87 }
88 
90 {
91  AVIOContext *pb = s->pb;
92  SmackerContext *smk = s->priv_data;
93  AVStream *st;
94  AVCodecParameters *par;
95  uint32_t magic, width, height, flags, treesize;
96  int i, ret, pts_inc;
97  int tbase;
98 
99  /* read and check header */
100  magic = avio_rl32(pb);
101  if (magic != MKTAG('S', 'M', 'K', '2') && magic != MKTAG('S', 'M', 'K', '4'))
102  return AVERROR_INVALIDDATA;
103  width = avio_rl32(pb);
104  height = avio_rl32(pb);
105  smk->frames = avio_rl32(pb);
106  pts_inc = avio_rl32(pb);
107  if (pts_inc > INT_MAX / 100) {
108  av_log(s, AV_LOG_ERROR, "pts_inc %d is too large\n", pts_inc);
109  return AVERROR_INVALIDDATA;
110  }
111 
112  flags = avio_rl32(pb);
113  if (flags & SMACKER_FLAG_RING_FRAME)
114  smk->frames++;
115  if (smk->frames > 0xFFFFFF) {
116  av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
117  return AVERROR_INVALIDDATA;
118  }
119 
120  avio_skip(pb, 28); /* Unused audio related data */
121 
122  treesize = avio_rl32(pb);
123  if (treesize >= UINT_MAX/4) {
124  // treesize + 16 must not overflow (this check is probably redundant)
125  av_log(s, AV_LOG_ERROR, "treesize too large\n");
126  return AVERROR_INVALIDDATA;
127  }
128 
129  st = avformat_new_stream(s, NULL);
130  if (!st)
131  return AVERROR(ENOMEM);
132 
133  smk->videoindex = st->index;
134  /* Smacker uses 100000 as internal timebase */
135  if (pts_inc < 0)
136  pts_inc = -pts_inc;
137  else
138  pts_inc *= 100;
139  tbase = 100000;
140  av_reduce(&tbase, &pts_inc, tbase, pts_inc, (1UL << 31) - 1);
141  avpriv_set_pts_info(st, 33, pts_inc, tbase);
142  st->duration = smk->frames;
143 
144  /* init video codec */
145  par = st->codecpar;
146  par->width = width;
147  par->height = height;
148  par->format = AV_PIX_FMT_PAL8;
151  par->codec_tag = magic;
152 
153  if ((ret = ff_alloc_extradata(par, treesize + 16)) < 0) {
154  av_log(s, AV_LOG_ERROR,
155  "Cannot allocate %"PRIu32" bytes of extradata\n",
156  treesize + 16);
157  return ret;
158  }
159  if ((ret = ffio_read_size(pb, par->extradata, 16)) < 0)
160  return ret;
161 
162  /* handle possible audio streams */
163  for (i = 0; i < 7; i++) {
164  uint32_t rate = avio_rl24(pb);
165  uint8_t aflag = avio_r8(pb);
166 
167  smk->indexes[i] = -1;
168 
169  if (rate) {
170  AVStream *ast = avformat_new_stream(s, NULL);
171  AVCodecParameters *par;
172  if (!ast)
173  return AVERROR(ENOMEM);
174 
175  smk->indexes[i] = ast->index;
176  par = ast->codecpar;
178  if (aflag & SMK_AUD_BINKAUD) {
180  } else if (aflag & SMK_AUD_USEDCT) {
182  } else if (aflag & SMK_AUD_PACKED) {
184  par->codec_tag = MKTAG('S', 'M', 'K', 'A');
185  } else {
187  }
188  if (aflag & SMK_AUD_STEREO) {
189  par->channels = 2;
191  } else {
192  par->channels = 1;
194  }
195  par->sample_rate = rate;
196  par->bits_per_coded_sample = (aflag & SMK_AUD_16BITS) ? 16 : 8;
197  if (par->bits_per_coded_sample == 16 &&
200  avpriv_set_pts_info(ast, 64, 1, par->sample_rate * par->channels
201  * par->bits_per_coded_sample / 8);
202  }
203  }
204 
205  avio_rl32(pb); /* padding */
206 
207  /* setup data */
208  st->priv_data = av_malloc_array(smk->frames, sizeof(*smk->frm_size) +
209  sizeof(*smk->frm_flags));
210  if (!st->priv_data)
211  return AVERROR(ENOMEM);
212  smk->frm_size = st->priv_data;
213  smk->frm_flags = (void*)(smk->frm_size + smk->frames);
214 
215  /* read frame info */
216  for (i = 0; i < smk->frames; i++) {
217  smk->frm_size[i] = avio_rl32(pb);
218  }
219  if ((ret = ffio_read_size(pb, smk->frm_flags, smk->frames)) < 0 ||
220  /* load trees to extradata, they will be unpacked by decoder */
221  (ret = ffio_read_size(pb, par->extradata + 16,
222  par->extradata_size - 16)) < 0) {
223  return ret;
224  }
225 
226  return 0;
227 }
228 
230 {
231  SmackerContext *smk = s->priv_data;
232  int flags;
233  int ret;
234 
235  if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
236  return AVERROR_EOF;
237 
238  /* if we demuxed all streams, pass another frame */
239  if (!smk->next_audio_index) {
240  smk->frame_size = smk->frm_size[smk->cur_frame] & (~3);
241  smk->next_frame_pos = avio_tell(s->pb) + smk->frame_size;
242  flags = smk->frm_flags[smk->cur_frame];
243  smk->flags = flags >> 1;
244  /* handle palette change event */
245  if (flags & SMACKER_PAL) {
246  int size, sz, t, off, j, pos;
247  uint8_t *pal = smk->pal;
248  uint8_t oldpal[768];
249 
250  memcpy(oldpal, pal, 768);
251  size = avio_r8(s->pb);
252  size = size * 4;
253  if (size > smk->frame_size) {
254  ret = AVERROR_INVALIDDATA;
255  goto next_frame;
256  }
257  smk->frame_size -= size--;
258  sz = 0;
259  pos = avio_tell(s->pb) + size;
260  while (sz < 256) {
261  t = avio_r8(s->pb);
262  if (t & 0x80) { /* skip palette entries */
263  sz += (t & 0x7F) + 1;
264  pal += ((t & 0x7F) + 1) * 3;
265  } else if (t & 0x40) { /* copy with offset */
266  off = avio_r8(s->pb);
267  j = (t & 0x3F) + 1;
268  if (off + j > 0x100) {
269  av_log(s, AV_LOG_ERROR,
270  "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
271  off, j);
272  ret = AVERROR_INVALIDDATA;
273  goto next_frame;
274  }
275  off *= 3;
276  while (j-- && sz < 256) {
277  *pal++ = oldpal[off + 0];
278  *pal++ = oldpal[off + 1];
279  *pal++ = oldpal[off + 2];
280  sz++;
281  off += 3;
282  }
283  } else { /* new entries */
284  *pal++ = smk_pal[t];
285  *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
286  *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
287  sz++;
288  }
289  }
290  avio_seek(s->pb, pos, 0);
291  smk->new_palette = 1;
292  }
293  }
294 
295  for (int i = smk->next_audio_index; i < 7; i++) {
296  if (smk->flags & (1 << i)) {
297  uint32_t size;
298 
299  size = avio_rl32(s->pb);
300  if ((int)size < 8 || size > smk->frame_size) {
301  av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
302  ret = AVERROR_INVALIDDATA;
303  goto next_frame;
304  }
305  smk->frame_size -= size;
306  size -= 4;
307 
308  if (smk->indexes[i] < 0) {
309  avio_skip(s->pb, size);
310  continue;
311  }
312  if ((ret = av_get_packet(s->pb, pkt, size)) != size) {
313  ret = ret < 0 ? ret : AVERROR_INVALIDDATA;
314  goto next_frame;
315  }
316  pkt->stream_index = smk->indexes[i];
317  pkt->pts = smk->aud_pts[i];
318  smk->aud_pts[i] += AV_RL32(pkt->data);
319  smk->next_audio_index = i + 1;
320  return 0;
321  }
322  }
323 
324  if (smk->frame_size >= INT_MAX/2) {
325  ret = AVERROR_INVALIDDATA;
326  goto next_frame;
327  }
328  if ((ret = av_new_packet(pkt, smk->frame_size + 769)) < 0)
329  goto next_frame;
330  flags = smk->new_palette;
331  if (smk->frm_size[smk->cur_frame] & 1)
332  flags |= 2;
333  pkt->data[0] = flags;
334  memcpy(pkt->data + 1, smk->pal, 768);
335  ret = ffio_read_size(s->pb, pkt->data + 769, smk->frame_size);
336  if (ret < 0)
337  goto next_frame;
338  pkt->stream_index = smk->videoindex;
339  pkt->pts = smk->cur_frame;
340  smk->next_audio_index = 0;
341  smk->new_palette = 0;
342  smk->cur_frame++;
343 
344  return 0;
345 next_frame:
346  avio_seek(s->pb, smk->next_frame_pos, SEEK_SET);
347  smk->next_audio_index = 0;
348  smk->cur_frame++;
349  return ret;
350 }
351 
353  .name = "smk",
354  .long_name = NULL_IF_CONFIG_SMALL("Smacker"),
355  .priv_data_size = sizeof(SmackerContext),
359 };
#define NULL
Definition: coverity.c:32
SAudFlags
Definition: smacker.c:37
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t next_frame_pos
Definition: smacker.c:51
int new_palette
Definition: smacker.c:59
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:4938
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:686
uint32_t * frm_size
Definition: smacker.c:48
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int index
stream index in AVFormatContext
Definition: avformat.h:877
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
void * priv_data
Definition: avformat.h:891
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:341
static AVPacket pkt
int64_t aud_pts[7]
Definition: smacker.c:61
#define AV_CH_LAYOUT_STEREO
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
static int smacker_probe(const AVProbeData *p)
Definition: smacker.c:77
Format I/O context.
Definition: avformat.h:1351
uint8_t
int width
Video only.
Definition: codec_par.h:126
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
uint8_t pal[768]
Definition: smacker.c:60
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4519
int next_audio_index
Definition: smacker.c:58
#define height
uint8_t * data
Definition: packet.h:363
#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:304
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
#define av_log(a,...)
int videoindex
Definition: smacker.c:53
#define U(x)
Definition: vp56_arith.h:37
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:88
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
unsigned int pos
Definition: spdifenc.c:410
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
uint8_t * frm_flags
Definition: smacker.c:49
#define SMACKER_FLAG_RING_FRAME
Definition: smacker.c:35
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
static const uint8_t smk_pal[64]
Definition: smacker.c:65
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
audio channel layout utility functions
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:3321
static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smacker.c:229
#define width
#define s(width, name)
Definition: cbs_vp9.c:257
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
Stream structure.
Definition: avformat.h:876
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define SMACKER_PAL
Definition: smacker.c:34
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
static int smacker_read_header(AVFormatContext *s)
Definition: smacker.c:89
int cur_frame
Definition: smacker.c:52
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
uint32_t frame_size
Definition: smacker.c:56
AVInputFormat ff_smacker_demuxer
Definition: smacker.c:352
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:925
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
int indexes[7]
Definition: smacker.c:54
Main libavformat public API header.
uint32_t frames
Definition: smacker.c:46
void * priv_data
Format private data.
Definition: avformat.h:1379
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
int channels
Audio only.
Definition: codec_par.h:166
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:650
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
#define av_malloc_array(a, b)
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:368
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:751
int stream_index
Definition: packet.h:365
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 AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:406
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: packet.h:340
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
int i
Definition: input.c:406