FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "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 ( AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
96  && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
97  return 0;
98 
99  if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
100  return AVPROBE_SCORE_MAX/4;
101 
102  return AVPROBE_SCORE_MAX;
103 }
104 
106 {
107  AVIOContext *pb = s->pb;
108  SmackerContext *smk = s->priv_data;
109  AVStream *st, *ast[7];
110  int i, ret;
111  int tbase;
112 
113  /* read and check header */
114  smk->magic = avio_rl32(pb);
115  if (smk->magic != MKTAG('S', 'M', 'K', '2') && smk->magic != MKTAG('S', 'M', 'K', '4'))
116  return AVERROR_INVALIDDATA;
117  smk->width = avio_rl32(pb);
118  smk->height = avio_rl32(pb);
119  smk->frames = avio_rl32(pb);
120  smk->pts_inc = (int32_t)avio_rl32(pb);
121  smk->flags = avio_rl32(pb);
122  if(smk->flags & SMACKER_FLAG_RING_FRAME)
123  smk->frames++;
124  for(i = 0; i < 7; i++)
125  smk->audio[i] = avio_rl32(pb);
126  smk->treesize = avio_rl32(pb);
127 
128  if(smk->treesize >= UINT_MAX/4){ // smk->treesize + 16 must not overflow (this check is probably redundant)
129  av_log(s, AV_LOG_ERROR, "treesize too large\n");
130  return AVERROR_INVALIDDATA;
131  }
132 
133 //FIXME remove extradata "rebuilding"
134  smk->mmap_size = avio_rl32(pb);
135  smk->mclr_size = avio_rl32(pb);
136  smk->full_size = avio_rl32(pb);
137  smk->type_size = avio_rl32(pb);
138  for(i = 0; i < 7; i++) {
139  smk->rates[i] = avio_rl24(pb);
140  smk->aflags[i] = avio_r8(pb);
141  }
142  smk->pad = avio_rl32(pb);
143  /* setup data */
144  if(smk->frames > 0xFFFFFF) {
145  av_log(s, AV_LOG_ERROR, "Too many frames: %i\n", smk->frames);
146  return AVERROR_INVALIDDATA;
147  }
148  smk->frm_size = av_malloc_array(smk->frames, sizeof(*smk->frm_size));
149  smk->frm_flags = av_malloc(smk->frames);
150  if (!smk->frm_size || !smk->frm_flags) {
151  av_freep(&smk->frm_size);
152  av_freep(&smk->frm_flags);
153  return AVERROR(ENOMEM);
154  }
155 
156  smk->is_ver4 = (smk->magic != MKTAG('S', 'M', 'K', '2'));
157 
158  /* read frame info */
159  for(i = 0; i < smk->frames; i++) {
160  smk->frm_size[i] = avio_rl32(pb);
161  }
162  for(i = 0; i < smk->frames; i++) {
163  smk->frm_flags[i] = avio_r8(pb);
164  }
165 
166  /* init video codec */
167  st = avformat_new_stream(s, NULL);
168  if (!st)
169  return AVERROR(ENOMEM);
170  smk->videoindex = st->index;
171  st->codec->width = smk->width;
172  st->codec->height = smk->height;
176  st->codec->codec_tag = smk->magic;
177  /* Smacker uses 100000 as internal timebase */
178  if(smk->pts_inc < 0)
179  smk->pts_inc = -smk->pts_inc;
180  else
181  smk->pts_inc *= 100;
182  tbase = 100000;
183  av_reduce(&tbase, &smk->pts_inc, tbase, smk->pts_inc, (1UL<<31)-1);
184  avpriv_set_pts_info(st, 33, smk->pts_inc, tbase);
185  st->duration = smk->frames;
186  /* handle possible audio streams */
187  for(i = 0; i < 7; i++) {
188  smk->indexes[i] = -1;
189  if (smk->rates[i]) {
190  ast[i] = avformat_new_stream(s, NULL);
191  if (!ast[i])
192  return AVERROR(ENOMEM);
193  smk->indexes[i] = ast[i]->index;
195  if (smk->aflags[i] & SMK_AUD_BINKAUD) {
197  } else if (smk->aflags[i] & SMK_AUD_USEDCT) {
199  } else if (smk->aflags[i] & SMK_AUD_PACKED){
201  ast[i]->codec->codec_tag = MKTAG('S', 'M', 'K', 'A');
202  } else {
203  ast[i]->codec->codec_id = AV_CODEC_ID_PCM_U8;
204  }
205  if (smk->aflags[i] & SMK_AUD_STEREO) {
206  ast[i]->codec->channels = 2;
208  } else {
209  ast[i]->codec->channels = 1;
211  }
212  ast[i]->codec->sample_rate = smk->rates[i];
213  ast[i]->codec->bits_per_coded_sample = (smk->aflags[i] & SMK_AUD_16BITS) ? 16 : 8;
214  if(ast[i]->codec->bits_per_coded_sample == 16 && ast[i]->codec->codec_id == AV_CODEC_ID_PCM_U8)
216  avpriv_set_pts_info(ast[i], 64, 1, ast[i]->codec->sample_rate
217  * ast[i]->codec->channels * ast[i]->codec->bits_per_coded_sample / 8);
218  }
219  }
220 
221 
222  /* load trees to extradata, they will be unpacked by decoder */
223  if(ff_alloc_extradata(st->codec, smk->treesize + 16)){
224  av_log(s, AV_LOG_ERROR, "Cannot allocate %i bytes of extradata\n", smk->treesize + 16);
225  av_freep(&smk->frm_size);
226  av_freep(&smk->frm_flags);
227  return AVERROR(ENOMEM);
228  }
229  ret = avio_read(pb, st->codec->extradata + 16, st->codec->extradata_size - 16);
230  if(ret != st->codec->extradata_size - 16){
231  av_freep(&smk->frm_size);
232  av_freep(&smk->frm_flags);
233  return AVERROR(EIO);
234  }
235  ((int32_t*)st->codec->extradata)[0] = av_le2ne32(smk->mmap_size);
236  ((int32_t*)st->codec->extradata)[1] = av_le2ne32(smk->mclr_size);
237  ((int32_t*)st->codec->extradata)[2] = av_le2ne32(smk->full_size);
238  ((int32_t*)st->codec->extradata)[3] = av_le2ne32(smk->type_size);
239 
240  smk->curstream = -1;
241  smk->nextpos = avio_tell(pb);
242 
243  return 0;
244 }
245 
246 
248 {
249  SmackerContext *smk = s->priv_data;
250  int flags;
251  int ret;
252  int i;
253  int frame_size = 0;
254  int palchange = 0;
255 
256  if (url_feof(s->pb) || smk->cur_frame >= smk->frames)
257  return AVERROR_EOF;
258 
259  /* if we demuxed all streams, pass another frame */
260  if(smk->curstream < 0) {
261  avio_seek(s->pb, smk->nextpos, 0);
262  frame_size = smk->frm_size[smk->cur_frame] & (~3);
263  flags = smk->frm_flags[smk->cur_frame];
264  /* handle palette change event */
265  if(flags & SMACKER_PAL){
266  int size, sz, t, off, j, pos;
267  uint8_t *pal = smk->pal;
268  uint8_t oldpal[768];
269 
270  memcpy(oldpal, pal, 768);
271  size = avio_r8(s->pb);
272  size = size * 4 - 1;
273  if(size + 1 > frame_size)
274  return AVERROR_INVALIDDATA;
275  frame_size -= size;
276  frame_size--;
277  sz = 0;
278  pos = avio_tell(s->pb) + size;
279  while(sz < 256){
280  t = avio_r8(s->pb);
281  if(t & 0x80){ /* skip palette entries */
282  sz += (t & 0x7F) + 1;
283  pal += ((t & 0x7F) + 1) * 3;
284  } else if(t & 0x40){ /* copy with offset */
285  off = avio_r8(s->pb);
286  j = (t & 0x3F) + 1;
287  if (off + j > 0x100) {
288  av_log(s, AV_LOG_ERROR,
289  "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
290  off, j);
291  return AVERROR_INVALIDDATA;
292  }
293  off *= 3;
294  while(j-- && sz < 256) {
295  *pal++ = oldpal[off + 0];
296  *pal++ = oldpal[off + 1];
297  *pal++ = oldpal[off + 2];
298  sz++;
299  off += 3;
300  }
301  } else { /* new entries */
302  *pal++ = smk_pal[t];
303  *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
304  *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
305  sz++;
306  }
307  }
308  avio_seek(s->pb, pos, 0);
309  palchange |= 1;
310  }
311  flags >>= 1;
312  smk->curstream = -1;
313  /* if audio chunks are present, put them to stack and retrieve later */
314  for(i = 0; i < 7; i++) {
315  if(flags & 1) {
316  uint32_t size;
317  int err;
318 
319  size = avio_rl32(s->pb) - 4;
320  if (!size || size + 4L > frame_size) {
321  av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
322  return AVERROR_INVALIDDATA;
323  }
324  frame_size -= size;
325  frame_size -= 4;
326  smk->curstream++;
327  if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) {
328  smk->buf_sizes[smk->curstream] = 0;
329  return err;
330  }
331  smk->buf_sizes[smk->curstream] = size;
332  ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
333  if(ret != size)
334  return AVERROR(EIO);
335  smk->stream_id[smk->curstream] = smk->indexes[i];
336  }
337  flags >>= 1;
338  }
339  if (frame_size < 0 || frame_size >= INT_MAX/2)
340  return AVERROR_INVALIDDATA;
341  if (av_new_packet(pkt, frame_size + 769))
342  return AVERROR(ENOMEM);
343  if(smk->frm_size[smk->cur_frame] & 1)
344  palchange |= 2;
345  pkt->data[0] = palchange;
346  memcpy(pkt->data + 1, smk->pal, 768);
347  ret = avio_read(s->pb, pkt->data + 769, frame_size);
348  if(ret != frame_size)
349  return AVERROR(EIO);
350  pkt->stream_index = smk->videoindex;
351  pkt->pts = smk->cur_frame;
352  pkt->size = ret + 769;
353  smk->cur_frame++;
354  smk->nextpos = avio_tell(s->pb);
355  } else {
356  if (smk->stream_id[smk->curstream] < 0 || !smk->bufs[smk->curstream])
357  return AVERROR_INVALIDDATA;
358  if (av_new_packet(pkt, smk->buf_sizes[smk->curstream]))
359  return AVERROR(ENOMEM);
360  memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
361  pkt->size = smk->buf_sizes[smk->curstream];
362  pkt->stream_index = smk->stream_id[smk->curstream];
363  pkt->pts = smk->aud_pts[smk->curstream];
364  smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);
365  smk->curstream--;
366  }
367 
368  return 0;
369 }
370 
372 {
373  SmackerContext *smk = s->priv_data;
374  int i;
375 
376  for(i = 0; i < 7; i++)
377  av_freep(&smk->bufs[i]);
378  av_freep(&smk->frm_size);
379  av_freep(&smk->frm_flags);
380 
381  return 0;
382 }
383 
385  .name = "smk",
386  .long_name = NULL_IF_CONFIG_SMALL("Smacker"),
387  .priv_data_size = sizeof(SmackerContext),
392 };