3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
23 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
31 #include "avio_internal.h"
34 #define SMACKER_PAL 0x01
35 #define SMACKER_FLAG_RING_FRAME 0x01
38 SMK_AUD_PACKED = 0x80,
39 SMK_AUD_16BITS = 0x20,
40 SMK_AUD_STEREO = 0x10,
41 SMK_AUD_BINKAUD = 0x08,
45 typedef struct SmackerContext {
50 /* internal variables */
51 int64_t next_frame_pos;
56 /* current frame for demuxing */
65 /* palette used in Smacker */
66 static const uint8_t smk_pal[64] = {
67 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
68 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
69 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
70 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
71 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
72 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
73 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
74 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
78 static int smacker_probe(const AVProbeData *p)
80 if ( AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
81 && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
84 if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
85 return AVPROBE_SCORE_MAX/4;
87 return AVPROBE_SCORE_MAX;
90 static int smacker_read_header(AVFormatContext *s)
92 AVIOContext *pb = s->pb;
93 SmackerContext *smk = s->priv_data;
95 AVCodecParameters *par;
96 uint32_t magic, width, height, flags, treesize;
100 /* read and check header */
101 magic = avio_rl32(pb);
102 if (magic != MKTAG('S', 'M', 'K', '2') && magic != MKTAG('S', 'M', 'K', '4'))
103 return AVERROR_INVALIDDATA;
104 width = avio_rl32(pb);
105 height = avio_rl32(pb);
106 smk->frames = avio_rl32(pb);
107 pts_inc = avio_rl32(pb);
108 if (pts_inc > INT_MAX / 100) {
109 av_log(s, AV_LOG_ERROR, "pts_inc %d is too large\n", pts_inc);
110 return AVERROR_INVALIDDATA;
113 flags = avio_rl32(pb);
114 if (flags & SMACKER_FLAG_RING_FRAME)
116 if (smk->frames > 0xFFFFFF) {
117 av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
118 return AVERROR_INVALIDDATA;
121 avio_skip(pb, 28); /* Unused audio related data */
123 treesize = avio_rl32(pb);
124 if (treesize >= UINT_MAX/4) {
125 // treesize + 16 must not overflow (this check is probably redundant)
126 av_log(s, AV_LOG_ERROR, "treesize too large\n");
127 return AVERROR_INVALIDDATA;
130 st = avformat_new_stream(s, NULL);
132 return AVERROR(ENOMEM);
134 smk->videoindex = st->index;
135 /* Smacker uses 100000 as internal timebase */
141 av_reduce(&tbase, &pts_inc, tbase, pts_inc, (1UL << 31) - 1);
142 avpriv_set_pts_info(st, 33, pts_inc, tbase);
143 st->duration = smk->frames;
145 /* init video codec */
148 par->height = height;
149 par->format = AV_PIX_FMT_PAL8;
150 par->codec_type = AVMEDIA_TYPE_VIDEO;
151 par->codec_id = AV_CODEC_ID_SMACKVIDEO;
152 par->codec_tag = magic;
154 if ((ret = ff_alloc_extradata(par, treesize + 16)) < 0) {
155 av_log(s, AV_LOG_ERROR,
156 "Cannot allocate %"PRIu32" bytes of extradata\n",
160 if ((ret = ffio_read_size(pb, par->extradata, 16)) < 0)
163 /* handle possible audio streams */
164 for (i = 0; i < 7; i++) {
165 uint32_t rate = avio_rl24(pb);
166 uint8_t aflag = avio_r8(pb);
168 smk->indexes[i] = -1;
171 AVStream *ast = avformat_new_stream(s, NULL);
172 AVCodecParameters *par;
174 return AVERROR(ENOMEM);
176 smk->indexes[i] = ast->index;
178 par->codec_type = AVMEDIA_TYPE_AUDIO;
179 if (aflag & SMK_AUD_BINKAUD) {
180 par->codec_id = AV_CODEC_ID_BINKAUDIO_RDFT;
181 } else if (aflag & SMK_AUD_USEDCT) {
182 par->codec_id = AV_CODEC_ID_BINKAUDIO_DCT;
183 } else if (aflag & SMK_AUD_PACKED) {
184 par->codec_id = AV_CODEC_ID_SMACKAUDIO;
185 par->codec_tag = MKTAG('S', 'M', 'K', 'A');
187 par->codec_id = AV_CODEC_ID_PCM_U8;
189 if (aflag & SMK_AUD_STEREO) {
191 par->channel_layout = AV_CH_LAYOUT_STEREO;
194 par->channel_layout = AV_CH_LAYOUT_MONO;
196 par->sample_rate = rate;
197 par->bits_per_coded_sample = (aflag & SMK_AUD_16BITS) ? 16 : 8;
198 if (par->bits_per_coded_sample == 16 &&
199 par->codec_id == AV_CODEC_ID_PCM_U8)
200 par->codec_id = AV_CODEC_ID_PCM_S16LE;
202 smk->duration_size[i] = 4;
203 avpriv_set_pts_info(ast, 64, 1, par->sample_rate * par->channels
204 * par->bits_per_coded_sample / 8);
208 avio_rl32(pb); /* padding */
211 st->priv_data = av_malloc_array(smk->frames, sizeof(*smk->frm_size) +
212 sizeof(*smk->frm_flags));
214 return AVERROR(ENOMEM);
215 smk->frm_size = st->priv_data;
216 smk->frm_flags = (void*)(smk->frm_size + smk->frames);
218 /* read frame info */
219 for (i = 0; i < smk->frames; i++) {
220 smk->frm_size[i] = avio_rl32(pb);
222 if ((ret = ffio_read_size(pb, smk->frm_flags, smk->frames)) < 0 ||
223 /* load trees to extradata, they will be unpacked by decoder */
224 (ret = ffio_read_size(pb, par->extradata + 16,
225 par->extradata_size - 16)) < 0) {
232 static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
234 SmackerContext *smk = s->priv_data;
238 if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
241 /* if we demuxed all streams, pass another frame */
242 if (!smk->next_audio_index) {
243 smk->frame_size = smk->frm_size[smk->cur_frame] & (~3);
244 smk->next_frame_pos = avio_tell(s->pb) + smk->frame_size;
245 flags = smk->frm_flags[smk->cur_frame];
246 smk->flags = flags >> 1;
247 /* handle palette change event */
248 if (flags & SMACKER_PAL) {
249 int size, sz, t, off, j, pos;
250 uint8_t *pal = smk->pal;
253 memcpy(oldpal, pal, 768);
254 size = avio_r8(s->pb);
256 if (size > smk->frame_size) {
257 ret = AVERROR_INVALIDDATA;
260 smk->frame_size -= size--;
262 pos = avio_tell(s->pb) + size;
265 if (t & 0x80) { /* skip palette entries */
266 sz += (t & 0x7F) + 1;
267 pal += ((t & 0x7F) + 1) * 3;
268 } else if (t & 0x40) { /* copy with offset */
269 off = avio_r8(s->pb);
271 if (off + j > 0x100) {
272 av_log(s, AV_LOG_ERROR,
273 "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
275 ret = AVERROR_INVALIDDATA;
279 while (j-- && sz < 256) {
280 *pal++ = oldpal[off + 0];
281 *pal++ = oldpal[off + 1];
282 *pal++ = oldpal[off + 2];
286 } else { /* new entries */
288 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
289 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
293 avio_seek(s->pb, pos, 0);
294 smk->new_palette = 1;
298 for (int i = smk->next_audio_index; i < 7; i++) {
299 if (smk->flags & (1 << i)) {
302 size = avio_rl32(s->pb);
303 if ((int)size < 4 + smk->duration_size[i] || size > smk->frame_size) {
304 av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
305 ret = AVERROR_INVALIDDATA;
308 smk->frame_size -= size;
311 if (smk->indexes[i] < 0 ||
312 s->streams[smk->indexes[i]]->discard >= AVDISCARD_ALL) {
313 smk->aud_pts[i] += smk->duration_size[i] ? avio_rl32(s->pb)
315 avio_skip(s->pb, size - smk->duration_size[i]);
318 if ((ret = av_get_packet(s->pb, pkt, size)) != size) {
319 ret = ret < 0 ? ret : AVERROR_INVALIDDATA;
322 pkt->stream_index = smk->indexes[i];
323 pkt->pts = smk->aud_pts[i];
324 pkt->duration = smk->duration_size[i] ? AV_RL32(pkt->data)
326 smk->aud_pts[i] += pkt->duration;
327 smk->next_audio_index = i + 1;
332 if (s->streams[smk->videoindex]->discard >= AVDISCARD_ALL) {
336 if (smk->frame_size >= INT_MAX/2) {
337 ret = AVERROR_INVALIDDATA;
340 if ((ret = av_new_packet(pkt, smk->frame_size + 769)) < 0)
342 flags = smk->new_palette;
343 if (smk->frm_size[smk->cur_frame] & 1)
345 pkt->data[0] = flags;
346 memcpy(pkt->data + 1, smk->pal, 768);
347 ret = ffio_read_size(s->pb, pkt->data + 769, smk->frame_size);
350 pkt->stream_index = smk->videoindex;
351 pkt->pts = smk->cur_frame;
352 smk->next_audio_index = 0;
353 smk->new_palette = 0;
358 avio_seek(s->pb, smk->next_frame_pos, SEEK_SET);
359 smk->next_audio_index = 0;
364 static int smacker_read_seek(AVFormatContext *s, int stream_index,
365 int64_t timestamp, int flags)
367 SmackerContext *smk = s->priv_data;
370 /* only rewinding to start is supported */
371 if (timestamp != 0) {
372 av_log(s, AV_LOG_ERROR,
373 "Random seeks are not supported (can only seek to start).\n");
374 return AVERROR(EINVAL);
377 if ((ret = avio_seek(s->pb, s->internal->data_offset, SEEK_SET)) < 0)
381 smk->next_audio_index = 0;
382 smk->new_palette = 0;
383 memset(smk->pal, 0, sizeof(smk->pal));
384 memset(smk->aud_pts, 0, sizeof(smk->aud_pts));
389 AVInputFormat ff_smacker_demuxer = {
391 .long_name = NULL_IF_CONFIG_SMALL("Smacker"),
392 .priv_data_size = sizeof(SmackerContext),
393 .read_probe = smacker_probe,
394 .read_header = smacker_read_header,
395 .read_packet = smacker_read_packet,
396 .read_seek = smacker_read_seek,