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