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 
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(const AVProbeData *p)
96 {
97  if ( AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
98  && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
99  return 0;
100 
101  if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
102  return AVPROBE_SCORE_MAX/4;
103 
104  return AVPROBE_SCORE_MAX;
105 }
106 
108 {
109  AVIOContext *pb = s->pb;
110  SmackerContext *smk = s->priv_data;
111  AVStream *st, *ast[7];
112  int i, ret;
113  int tbase;
114 
115  /* read and check header */
116  smk->magic = avio_rl32(pb);
117  if (smk->magic != MKTAG('S', 'M', 'K', '2') && smk->magic != MKTAG('S', 'M', 'K', '4'))
118  return AVERROR_INVALIDDATA;
119  smk->width = avio_rl32(pb);
120  smk->height = avio_rl32(pb);
121  smk->frames = avio_rl32(pb);
122  smk->pts_inc = (int32_t)avio_rl32(pb);
123  if (smk->pts_inc > INT_MAX / 100) {
124  av_log(s, AV_LOG_ERROR, "pts_inc %d is too large\n", smk->pts_inc);
125  return AVERROR_INVALIDDATA;
126  }
127 
128  smk->flags = avio_rl32(pb);
129  if(smk->flags & SMACKER_FLAG_RING_FRAME)
130  smk->frames++;
131  for(i = 0; i < 7; i++)
132  smk->audio[i] = avio_rl32(pb);
133  smk->treesize = avio_rl32(pb);
134 
135  if(smk->treesize >= UINT_MAX/4){ // smk->treesize + 16 must not overflow (this check is probably redundant)
136  av_log(s, AV_LOG_ERROR, "treesize too large\n");
137  return AVERROR_INVALIDDATA;
138  }
139 
140 //FIXME remove extradata "rebuilding"
141  smk->mmap_size = avio_rl32(pb);
142  smk->mclr_size = avio_rl32(pb);
143  smk->full_size = avio_rl32(pb);
144  smk->type_size = avio_rl32(pb);
145  for(i = 0; i < 7; i++) {
146  smk->rates[i] = avio_rl24(pb);
147  smk->aflags[i] = avio_r8(pb);
148  }
149  smk->pad = avio_rl32(pb);
150  /* setup data */
151  if(smk->frames > 0xFFFFFF) {
152  av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
153  return AVERROR_INVALIDDATA;
154  }
155  smk->frm_size = av_malloc_array(smk->frames, sizeof(*smk->frm_size));
156  smk->frm_flags = av_malloc(smk->frames);
157  if (!smk->frm_size || !smk->frm_flags) {
158  av_freep(&smk->frm_size);
159  av_freep(&smk->frm_flags);
160  return AVERROR(ENOMEM);
161  }
162 
163  smk->is_ver4 = (smk->magic != MKTAG('S', 'M', 'K', '2'));
164 
165  /* read frame info */
166  for(i = 0; i < smk->frames; i++) {
167  smk->frm_size[i] = avio_rl32(pb);
168  }
169  for(i = 0; i < smk->frames; i++) {
170  smk->frm_flags[i] = avio_r8(pb);
171  }
172 
173  /* init video codec */
174  st = avformat_new_stream(s, NULL);
175  if (!st) {
176  av_freep(&smk->frm_size);
177  av_freep(&smk->frm_flags);
178  return AVERROR(ENOMEM);
179  }
180  smk->videoindex = st->index;
181  st->codecpar->width = smk->width;
182  st->codecpar->height = smk->height;
186  st->codecpar->codec_tag = smk->magic;
187  /* Smacker uses 100000 as internal timebase */
188  if(smk->pts_inc < 0)
189  smk->pts_inc = -smk->pts_inc;
190  else
191  smk->pts_inc *= 100;
192  tbase = 100000;
193  av_reduce(&tbase, &smk->pts_inc, tbase, smk->pts_inc, (1UL<<31)-1);
194  avpriv_set_pts_info(st, 33, smk->pts_inc, tbase);
195  st->duration = smk->frames;
196  /* handle possible audio streams */
197  for(i = 0; i < 7; i++) {
198  smk->indexes[i] = -1;
199  if (smk->rates[i]) {
200  ast[i] = avformat_new_stream(s, NULL);
201  if (!ast[i]) {
202  av_freep(&smk->frm_size);
203  av_freep(&smk->frm_flags);
204  return AVERROR(ENOMEM);
205  }
206  smk->indexes[i] = ast[i]->index;
208  if (smk->aflags[i] & SMK_AUD_BINKAUD) {
210  } else if (smk->aflags[i] & SMK_AUD_USEDCT) {
212  } else if (smk->aflags[i] & SMK_AUD_PACKED){
214  ast[i]->codecpar->codec_tag = MKTAG('S', 'M', 'K', 'A');
215  } else {
217  }
218  if (smk->aflags[i] & SMK_AUD_STEREO) {
219  ast[i]->codecpar->channels = 2;
221  } else {
222  ast[i]->codecpar->channels = 1;
224  }
225  ast[i]->codecpar->sample_rate = smk->rates[i];
226  ast[i]->codecpar->bits_per_coded_sample = (smk->aflags[i] & SMK_AUD_16BITS) ? 16 : 8;
227  if(ast[i]->codecpar->bits_per_coded_sample == 16 && ast[i]->codecpar->codec_id == AV_CODEC_ID_PCM_U8)
229  avpriv_set_pts_info(ast[i], 64, 1, ast[i]->codecpar->sample_rate
230  * ast[i]->codecpar->channels * ast[i]->codecpar->bits_per_coded_sample / 8);
231  }
232  }
233 
234 
235  /* load trees to extradata, they will be unpacked by decoder */
236  if(ff_alloc_extradata(st->codecpar, smk->treesize + 16)){
237  av_log(s, AV_LOG_ERROR,
238  "Cannot allocate %"PRIu32" bytes of extradata\n",
239  smk->treesize + 16);
240  av_freep(&smk->frm_size);
241  av_freep(&smk->frm_flags);
242  return AVERROR(ENOMEM);
243  }
244  ret = avio_read(pb, st->codecpar->extradata + 16, st->codecpar->extradata_size - 16);
245  if(ret != st->codecpar->extradata_size - 16){
246  av_freep(&smk->frm_size);
247  av_freep(&smk->frm_flags);
248  return AVERROR(EIO);
249  }
250  ((int32_t*)st->codecpar->extradata)[0] = av_le2ne32(smk->mmap_size);
251  ((int32_t*)st->codecpar->extradata)[1] = av_le2ne32(smk->mclr_size);
252  ((int32_t*)st->codecpar->extradata)[2] = av_le2ne32(smk->full_size);
253  ((int32_t*)st->codecpar->extradata)[3] = av_le2ne32(smk->type_size);
254 
255  smk->curstream = -1;
256  smk->nextpos = avio_tell(pb);
257 
258  return 0;
259 }
260 
261 
263 {
264  SmackerContext *smk = s->priv_data;
265  int flags;
266  int ret;
267  int i;
268  int frame_size = 0;
269  int palchange = 0;
270 
271  if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
272  return AVERROR_EOF;
273 
274  /* if we demuxed all streams, pass another frame */
275  if(smk->curstream < 0) {
276  avio_seek(s->pb, smk->nextpos, 0);
277  frame_size = smk->frm_size[smk->cur_frame] & (~3);
278  flags = smk->frm_flags[smk->cur_frame];
279  /* handle palette change event */
280  if(flags & SMACKER_PAL){
281  int size, sz, t, off, j, pos;
282  uint8_t *pal = smk->pal;
283  uint8_t oldpal[768];
284 
285  memcpy(oldpal, pal, 768);
286  size = avio_r8(s->pb);
287  size = size * 4 - 1;
288  if(size + 1 > frame_size)
289  return AVERROR_INVALIDDATA;
290  frame_size -= size;
291  frame_size--;
292  sz = 0;
293  pos = avio_tell(s->pb) + size;
294  while(sz < 256){
295  t = avio_r8(s->pb);
296  if(t & 0x80){ /* skip palette entries */
297  sz += (t & 0x7F) + 1;
298  pal += ((t & 0x7F) + 1) * 3;
299  } else if(t & 0x40){ /* copy with offset */
300  off = avio_r8(s->pb);
301  j = (t & 0x3F) + 1;
302  if (off + j > 0x100) {
303  av_log(s, AV_LOG_ERROR,
304  "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
305  off, j);
306  return AVERROR_INVALIDDATA;
307  }
308  off *= 3;
309  while(j-- && sz < 256) {
310  *pal++ = oldpal[off + 0];
311  *pal++ = oldpal[off + 1];
312  *pal++ = oldpal[off + 2];
313  sz++;
314  off += 3;
315  }
316  } else { /* new entries */
317  *pal++ = smk_pal[t];
318  *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
319  *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
320  sz++;
321  }
322  }
323  avio_seek(s->pb, pos, 0);
324  palchange |= 1;
325  }
326  flags >>= 1;
327  smk->curstream = -1;
328  /* if audio chunks are present, put them to stack and retrieve later */
329  for(i = 0; i < 7; i++) {
330  if(flags & 1) {
331  uint32_t size;
332  int err;
333 
334  size = avio_rl32(s->pb) - 4;
335  if (!size || size + 4LL > frame_size) {
336  av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
337  return AVERROR_INVALIDDATA;
338  }
339  frame_size -= size;
340  frame_size -= 4;
341  smk->curstream++;
342  if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) {
343  smk->buf_sizes[smk->curstream] = 0;
344  return err;
345  }
346  smk->buf_sizes[smk->curstream] = size;
347  ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
348  if(ret != size)
349  return AVERROR(EIO);
350  smk->stream_id[smk->curstream] = smk->indexes[i];
351  }
352  flags >>= 1;
353  }
354  if (frame_size < 0 || frame_size >= INT_MAX/2)
355  return AVERROR_INVALIDDATA;
356  if (av_new_packet(pkt, frame_size + 769))
357  return AVERROR(ENOMEM);
358  if(smk->frm_size[smk->cur_frame] & 1)
359  palchange |= 2;
360  pkt->data[0] = palchange;
361  memcpy(pkt->data + 1, smk->pal, 768);
362  ret = avio_read(s->pb, pkt->data + 769, frame_size);
363  if(ret != frame_size)
364  return AVERROR(EIO);
365  pkt->stream_index = smk->videoindex;
366  pkt->pts = smk->cur_frame;
367  pkt->size = ret + 769;
368  smk->cur_frame++;
369  smk->nextpos = avio_tell(s->pb);
370  } else {
371  if (smk->stream_id[smk->curstream] < 0 || !smk->bufs[smk->curstream])
372  return AVERROR_INVALIDDATA;
373  if (av_new_packet(pkt, smk->buf_sizes[smk->curstream]))
374  return AVERROR(ENOMEM);
375  memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
376  pkt->size = smk->buf_sizes[smk->curstream];
377  pkt->stream_index = smk->stream_id[smk->curstream];
378  pkt->pts = smk->aud_pts[smk->curstream];
379  smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);
380  smk->curstream--;
381  }
382 
383  return 0;
384 }
385 
387 {
388  SmackerContext *smk = s->priv_data;
389  int i;
390 
391  for(i = 0; i < 7; i++)
392  av_freep(&smk->bufs[i]);
393  av_freep(&smk->frm_size);
394  av_freep(&smk->frm_flags);
395 
396  return 0;
397 }
398 
400  .name = "smk",
401  .long_name = NULL_IF_CONFIG_SMALL("Smacker"),
402  .priv_data_size = sizeof(SmackerContext),
407 };
#define NULL
Definition: coverity.c:32
SAudFlags
Definition: smacker.c:37
Bytestream IO Context.
Definition: avio.h:161
int64_t pts
Definition: smacker.c:78
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h: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:4929
uint32_t * frm_size
Definition: smacker.c:59
uint32_t type_size
Definition: smacker.c:54
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3968
int index
stream index in AVFormatContext
Definition: avformat.h:882
int size
Definition: avcodec.h:1481
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
static AVPacket pkt
int64_t aud_pts[7]
Definition: smacker.c:74
#define AV_CH_LAYOUT_STEREO
#define av_le2ne32(x)
Definition: bswap.h:96
static int smacker_probe(const AVProbeData *p)
Definition: smacker.c:95
uint32_t audio[7]
Definition: smacker.c:52
Format I/O context.
Definition: avformat.h:1358
uint8_t
#define av_malloc(s)
int width
Video only.
Definition: avcodec.h:4034
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
int stream
Definition: smacker.c:79
uint8_t pal[768]
Definition: smacker.c:66
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4502
int64_t nextpos
Definition: smacker.c:73
uint8_t * data
Definition: avcodec.h:1480
uint32_t pad
Definition: smacker.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
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: avcodec.h:4070
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:650
uint32_t mclr_size
Definition: smacker.c:54
uint32_t full_size
Definition: smacker.c:54
int videoindex
Definition: smacker.c:68
#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:86
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:772
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:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3964
uint8_t * frm_flags
Definition: smacker.c:60
#define SMACKER_FLAG_RING_FRAME
Definition: smacker.c:35
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3986
static const uint8_t smk_pal[64]
Definition: smacker.c:83
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:641
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
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:262
int32_t
#define s(width, name)
Definition: cbs_vp9.c:257
int curstream
Definition: smacker.c:72
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
int64_t cur_pts
Definition: smacker.c:64
static int smacker_read_close(AVFormatContext *s)
Definition: smacker.c:386
Stream structure.
Definition: avformat.h:881
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define SMACKER_PAL
Definition: smacker.c:34
uint32_t rates[7]
Definition: smacker.c:56
int frame_size
Definition: mxfenc.c:2223
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
uint32_t width
Definition: smacker.c:48
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
static int smacker_read_header(AVFormatContext *s)
Definition: smacker.c:107
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:446
AVInputFormat ff_smacker_demuxer
Definition: smacker.c:399
int stream_id[7]
Definition: smacker.c:71
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
uint8_t * bufs[7]
Definition: smacker.c:69
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:930
int sample_rate
Audio only.
Definition: avcodec.h:4078
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
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
uint32_t height
Definition: smacker.c:48
void * priv_data
Format private data.
Definition: avformat.h:1386
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4010
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3982
int channels
Audio only.
Definition: avcodec.h:4074
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
#define av_malloc_array(a, b)
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:361
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3972
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:764
int stream_index
Definition: avcodec.h:1482
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:366
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1457
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1473
uint32_t magic
Definition: smacker.c:47