Libav
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 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 /*
23  * Based on http://wiki.multimedia.cx/index.php?title=Smacker
24  */
25 
26 #include "libavutil/bswap.h"
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 #include "internal.h"
31 
32 #define SMACKER_PAL 0x01
33 #define SMACKER_FLAG_RING_FRAME 0x01
34 
35 enum SAudFlags {
41 };
42 
43 typedef struct SmackerContext {
44  /* Smacker file header */
45  uint32_t magic;
46  uint32_t width, height;
47  uint32_t frames;
48  int pts_inc;
49  uint32_t flags;
50  uint32_t audio[7];
51  uint32_t treesize;
54  uint32_t rates[7];
55  uint32_t pad;
56  /* frame info */
57  uint32_t *frm_size;
59  /* internal variables */
60  int cur_frame;
61  int is_ver4;
62  int64_t cur_pts;
63  /* current frame for demuxing */
64  uint8_t pal[768];
65  int indexes[7];
68  int buf_sizes[7];
69  int stream_id[7];
70  int curstream;
71  int64_t nextpos;
72  int64_t aud_pts[7];
74 
75 typedef struct SmackerFrame {
76  int64_t pts;
77  int stream;
78 } SmackerFrame;
79 
80 /* palette used in Smacker */
81 static const uint8_t smk_pal[64] = {
82  0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
83  0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
84  0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
85  0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
86  0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
87  0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
88  0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
89  0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
90 };
91 
92 
93 static int smacker_probe(AVProbeData *p)
94 {
95  if(p->buf[0] == 'S' && p->buf[1] == 'M' && p->buf[2] == 'K'
96  && (p->buf[3] == '2' || p->buf[3] == '4'))
97  return AVPROBE_SCORE_MAX;
98  else
99  return 0;
100 }
101 
103 {
104  AVIOContext *pb = s->pb;
105  SmackerContext *smk = s->priv_data;
106  AVStream *st, *ast[7];
107  int i, ret;
108  int tbase;
109 
110  /* read and check header */
111  smk->magic = avio_rl32(pb);
112  if (smk->magic != MKTAG('S', 'M', 'K', '2') && smk->magic != MKTAG('S', 'M', 'K', '4'))
113  return -1;
114  smk->width = avio_rl32(pb);
115  smk->height = avio_rl32(pb);
116  smk->frames = avio_rl32(pb);
117  smk->pts_inc = (int32_t)avio_rl32(pb);
118  smk->flags = avio_rl32(pb);
119  if(smk->flags & SMACKER_FLAG_RING_FRAME)
120  smk->frames++;
121  for(i = 0; i < 7; i++)
122  smk->audio[i] = avio_rl32(pb);
123  smk->treesize = avio_rl32(pb);
124 
125  if(smk->treesize >= UINT_MAX/4){ // smk->treesize + 16 must not overflow (this check is probably redundant)
126  av_log(s, AV_LOG_ERROR, "treesize too large\n");
127  return -1;
128  }
129 
130 //FIXME remove extradata "rebuilding"
131  smk->mmap_size = avio_rl32(pb);
132  smk->mclr_size = avio_rl32(pb);
133  smk->full_size = avio_rl32(pb);
134  smk->type_size = avio_rl32(pb);
135  for(i = 0; i < 7; i++) {
136  smk->rates[i] = avio_rl24(pb);
137  smk->aflags[i] = avio_r8(pb);
138  }
139  smk->pad = avio_rl32(pb);
140  /* setup data */
141  if(smk->frames > 0xFFFFFF) {
142  av_log(s, AV_LOG_ERROR, "Too many frames: %i\n", smk->frames);
143  return -1;
144  }
145  smk->frm_size = av_malloc(smk->frames * 4);
146  smk->frm_flags = av_malloc(smk->frames);
147 
148  smk->is_ver4 = (smk->magic != MKTAG('S', 'M', 'K', '2'));
149 
150  /* read frame info */
151  for(i = 0; i < smk->frames; i++) {
152  smk->frm_size[i] = avio_rl32(pb);
153  }
154  for(i = 0; i < smk->frames; i++) {
155  smk->frm_flags[i] = avio_r8(pb);
156  }
157 
158  /* init video codec */
159  st = avformat_new_stream(s, NULL);
160  if (!st)
161  return -1;
162  smk->videoindex = st->index;
163  st->codec->width = smk->width;
164  st->codec->height = smk->height;
168  st->codec->codec_tag = smk->magic;
169  /* Smacker uses 100000 as internal timebase */
170  if(smk->pts_inc < 0)
171  smk->pts_inc = -smk->pts_inc;
172  else
173  smk->pts_inc *= 100;
174  tbase = 100000;
175  av_reduce(&tbase, &smk->pts_inc, tbase, smk->pts_inc, (1UL<<31)-1);
176  avpriv_set_pts_info(st, 33, smk->pts_inc, tbase);
177  st->duration = smk->frames;
178  /* handle possible audio streams */
179  for(i = 0; i < 7; i++) {
180  smk->indexes[i] = -1;
181  if (smk->rates[i]) {
182  ast[i] = avformat_new_stream(s, NULL);
183  smk->indexes[i] = ast[i]->index;
185  if (smk->aflags[i] & SMK_AUD_BINKAUD) {
187  } else if (smk->aflags[i] & SMK_AUD_USEDCT) {
189  } else if (smk->aflags[i] & SMK_AUD_PACKED){
191  ast[i]->codec->codec_tag = MKTAG('S', 'M', 'K', 'A');
192  } else {
193  ast[i]->codec->codec_id = AV_CODEC_ID_PCM_U8;
194  }
195  if (smk->aflags[i] & SMK_AUD_STEREO) {
196  ast[i]->codec->channels = 2;
198  } else {
199  ast[i]->codec->channels = 1;
201  }
202  ast[i]->codec->sample_rate = smk->rates[i];
203  ast[i]->codec->bits_per_coded_sample = (smk->aflags[i] & SMK_AUD_16BITS) ? 16 : 8;
204  if(ast[i]->codec->bits_per_coded_sample == 16 && ast[i]->codec->codec_id == AV_CODEC_ID_PCM_U8)
206  avpriv_set_pts_info(ast[i], 64, 1, ast[i]->codec->sample_rate
207  * ast[i]->codec->channels * ast[i]->codec->bits_per_coded_sample / 8);
208  }
209  }
210 
211 
212  /* load trees to extradata, they will be unpacked by decoder */
213  st->codec->extradata = av_mallocz(smk->treesize + 16 +
215  st->codec->extradata_size = smk->treesize + 16;
216  if(!st->codec->extradata){
217  av_log(s, AV_LOG_ERROR, "Cannot allocate %i bytes of extradata\n", smk->treesize + 16);
218  av_free(smk->frm_size);
219  av_free(smk->frm_flags);
220  return -1;
221  }
222  ret = avio_read(pb, st->codec->extradata + 16, st->codec->extradata_size - 16);
223  if(ret != st->codec->extradata_size - 16){
224  av_free(smk->frm_size);
225  av_free(smk->frm_flags);
226  return AVERROR(EIO);
227  }
228  ((int32_t*)st->codec->extradata)[0] = av_le2ne32(smk->mmap_size);
229  ((int32_t*)st->codec->extradata)[1] = av_le2ne32(smk->mclr_size);
230  ((int32_t*)st->codec->extradata)[2] = av_le2ne32(smk->full_size);
231  ((int32_t*)st->codec->extradata)[3] = av_le2ne32(smk->type_size);
232 
233  smk->curstream = -1;
234  smk->nextpos = avio_tell(pb);
235 
236  return 0;
237 }
238 
239 
241 {
242  SmackerContext *smk = s->priv_data;
243  int flags;
244  int ret;
245  int i;
246  int frame_size = 0;
247  int palchange = 0;
248 
249  if (s->pb->eof_reached || smk->cur_frame >= smk->frames)
250  return AVERROR_EOF;
251 
252  /* if we demuxed all streams, pass another frame */
253  if(smk->curstream < 0) {
254  avio_seek(s->pb, smk->nextpos, 0);
255  frame_size = smk->frm_size[smk->cur_frame] & (~3);
256  flags = smk->frm_flags[smk->cur_frame];
257  /* handle palette change event */
258  if(flags & SMACKER_PAL){
259  int size, sz, t, off, j, pos;
260  uint8_t *pal = smk->pal;
261  uint8_t oldpal[768];
262 
263  memcpy(oldpal, pal, 768);
264  size = avio_r8(s->pb);
265  size = size * 4 - 1;
266  frame_size -= size;
267  frame_size--;
268  sz = 0;
269  pos = avio_tell(s->pb) + size;
270  while(sz < 256){
271  t = avio_r8(s->pb);
272  if(t & 0x80){ /* skip palette entries */
273  sz += (t & 0x7F) + 1;
274  pal += ((t & 0x7F) + 1) * 3;
275  } else if(t & 0x40){ /* copy with offset */
276  off = avio_r8(s->pb);
277  j = (t & 0x3F) + 1;
278  if (off + j > 0x100) {
279  av_log(s, AV_LOG_ERROR,
280  "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
281  off, j);
282  return AVERROR_INVALIDDATA;
283  }
284  off *= 3;
285  while(j-- && sz < 256) {
286  *pal++ = oldpal[off + 0];
287  *pal++ = oldpal[off + 1];
288  *pal++ = oldpal[off + 2];
289  sz++;
290  off += 3;
291  }
292  } else { /* new entries */
293  *pal++ = smk_pal[t];
294  *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
295  *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
296  sz++;
297  }
298  }
299  avio_seek(s->pb, pos, 0);
300  palchange |= 1;
301  }
302  flags >>= 1;
303  smk->curstream = -1;
304  /* if audio chunks are present, put them to stack and retrieve later */
305  for(i = 0; i < 7; i++) {
306  if(flags & 1) {
307  uint32_t size;
308  int err;
309 
310  size = avio_rl32(s->pb) - 4;
311  if (!size || size > frame_size) {
312  av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
313  return AVERROR_INVALIDDATA;
314  }
315  frame_size -= size;
316  frame_size -= 4;
317  smk->curstream++;
318  if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) {
319  smk->buf_sizes[smk->curstream] = 0;
320  return err;
321  }
322  smk->buf_sizes[smk->curstream] = size;
323  ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
324  if(ret != size)
325  return AVERROR(EIO);
326  smk->stream_id[smk->curstream] = smk->indexes[i];
327  }
328  flags >>= 1;
329  }
330  if (frame_size < 0 || frame_size >= INT_MAX/2)
331  return AVERROR_INVALIDDATA;
332  if (av_new_packet(pkt, frame_size + 769))
333  return AVERROR(ENOMEM);
334  if(smk->frm_size[smk->cur_frame] & 1)
335  palchange |= 2;
336  pkt->data[0] = palchange;
337  memcpy(pkt->data + 1, smk->pal, 768);
338  ret = avio_read(s->pb, pkt->data + 769, frame_size);
339  if(ret != frame_size)
340  return AVERROR(EIO);
341  pkt->stream_index = smk->videoindex;
342  pkt->pts = smk->cur_frame;
343  pkt->size = ret + 769;
344  smk->cur_frame++;
345  smk->nextpos = avio_tell(s->pb);
346  } else {
347  if (smk->stream_id[smk->curstream] < 0)
348  return AVERROR_INVALIDDATA;
349  if (av_new_packet(pkt, smk->buf_sizes[smk->curstream]))
350  return AVERROR(ENOMEM);
351  memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
352  pkt->size = smk->buf_sizes[smk->curstream];
353  pkt->stream_index = smk->stream_id[smk->curstream];
354  pkt->pts = smk->aud_pts[smk->curstream];
355  smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);
356  smk->curstream--;
357  }
358 
359  return 0;
360 }
361 
363 {
364  SmackerContext *smk = s->priv_data;
365  int i;
366 
367  for(i = 0; i < 7; i++)
368  av_free(smk->bufs[i]);
369  av_free(smk->frm_size);
370  av_free(smk->frm_flags);
371 
372  return 0;
373 }
374 
376  .name = "smk",
377  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
378  .priv_data_size = sizeof(SmackerContext),
383 };
SAudFlags
Definition: smacker.c:35
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
int64_t pts
Definition: smacker.c:76
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
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:3208
uint32_t * frm_size
Definition: smacker.c:57
uint32_t type_size
Definition: smacker.c:52
int index
stream index in AVFormatContext
Definition: avformat.h:684
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
int64_t aud_pts[7]
Definition: smacker.c:72
#define AV_CH_LAYOUT_STEREO
#define av_le2ne32(x)
Definition: bswap.h:98
uint32_t audio[7]
Definition: smacker.c:50
Format I/O context.
Definition: avformat.h:871
static int smacker_probe(AVProbeData *p)
Definition: smacker.c:93
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
int stream
Definition: smacker.c:77
uint8_t pal[768]
Definition: smacker.c:64
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
int64_t nextpos
Definition: smacker.c:71
uint8_t * data
Definition: avcodec.h:973
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
uint32_t pad
Definition: smacker.c:55
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2481
static float t
Definition: output.c:52
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:446
uint32_t mclr_size
Definition: smacker.c:52
uint32_t full_size
Definition: smacker.c:52
int videoindex
Definition: smacker.c:66
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:80
#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
int buf_sizes[7]
Definition: smacker.c:68
uint8_t aflags[7]
Definition: smacker.c:53
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:558
#define AVERROR(e)
Definition: error.h:43
uint32_t flags
Definition: smacker.c:49
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
uint8_t * frm_flags
Definition: smacker.c:58
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2662
#define SMACKER_FLAG_RING_FRAME
Definition: smacker.c:33
int off
Definition: dsputil_bfin.c:29
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1840
static const uint8_t smk_pal[64]
Definition: smacker.c:81
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:437
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
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
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:390
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:509
audio channel layout utility functions
static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: smacker.c:240
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1217
int32_t
#define AV_RL32
Definition: intreadwrite.h:146
int curstream
Definition: smacker.c:70
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:536
int64_t cur_pts
Definition: smacker.c:62
static int smacker_read_close(AVFormatContext *s)
Definition: smacker.c:362
Stream structure.
Definition: avformat.h:683
NULL
Definition: eval.c:55
#define SMACKER_PAL
Definition: smacker.c:32
uint32_t rates[7]
Definition: smacker.c:54
enum AVMediaType codec_type
Definition: avcodec.h:1062
uint32_t width
Definition: smacker.c:46
enum AVCodecID codec_id
Definition: avcodec.h:1065
int sample_rate
samples per second
Definition: avcodec.h:1779
AVIOContext * pb
I/O context.
Definition: avformat.h:913
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1080
int extradata_size
Definition: avcodec.h:1163
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
static int smacker_read_header(AVFormatContext *s)
Definition: smacker.c:102
int cur_frame
Definition: smacker.c:60
byte swapping routines
uint32_t treesize
Definition: smacker.c:51
This structure contains the data a format has to probe a file.
Definition: avformat.h:388
AVInputFormat ff_smacker_demuxer
Definition: smacker.c:375
int stream_id[7]
Definition: smacker.c:69
uint8_t * bufs[7]
Definition: smacker.c:67
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:734
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:395
int indexes[7]
Definition: smacker.c:65
Main libavformat public API header.
uint32_t frames
Definition: smacker.c:47
uint32_t mmap_size
Definition: smacker.c:52
int eof_reached
true if eof reached
Definition: avio.h:96
uint32_t height
Definition: smacker.c:46
int channels
number of audio channels
Definition: avcodec.h:1780
void * priv_data
Format private data.
Definition: avformat.h:899
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:516
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:550
int stream_index
Definition: avcodec.h:975
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:950
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
uint32_t magic
Definition: smacker.c:45