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 {
46 /* Smacker file header */
48 uint32_t width, height;
60 /* internal variables */
64 /* current frame for demuxing */
76 typedef struct SmackerFrame {
81 /* palette used in Smacker */
82 static const uint8_t smk_pal[64] = {
83 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
84 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
85 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
86 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
87 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
88 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
89 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
90 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
94 static int smacker_probe(const AVProbeData *p)
96 if ( AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
97 && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
100 if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
101 return AVPROBE_SCORE_MAX/4;
103 return AVPROBE_SCORE_MAX;
106 static int smacker_read_header(AVFormatContext *s)
108 AVIOContext *pb = s->pb;
109 SmackerContext *smk = s->priv_data;
110 AVStream *st, *ast[7];
114 /* read and check header */
115 smk->magic = avio_rl32(pb);
116 if (smk->magic != MKTAG('S', 'M', 'K', '2') && smk->magic != MKTAG('S', 'M', 'K', '4'))
117 return AVERROR_INVALIDDATA;
118 smk->width = avio_rl32(pb);
119 smk->height = avio_rl32(pb);
120 smk->frames = avio_rl32(pb);
121 smk->pts_inc = (int32_t)avio_rl32(pb);
122 if (smk->pts_inc > INT_MAX / 100) {
123 av_log(s, AV_LOG_ERROR, "pts_inc %d is too large\n", smk->pts_inc);
124 return AVERROR_INVALIDDATA;
127 smk->flags = avio_rl32(pb);
128 if(smk->flags & SMACKER_FLAG_RING_FRAME)
130 if (smk->frames > 0xFFFFFF) {
131 av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
132 return AVERROR_INVALIDDATA;
134 for(i = 0; i < 7; i++)
135 smk->audio[i] = avio_rl32(pb);
136 smk->treesize = avio_rl32(pb);
138 if(smk->treesize >= UINT_MAX/4){ // smk->treesize + 16 must not overflow (this check is probably redundant)
139 av_log(s, AV_LOG_ERROR, "treesize too large\n");
140 return AVERROR_INVALIDDATA;
143 st = avformat_new_stream(s, NULL);
145 return AVERROR(ENOMEM);
147 if ((ret = ff_alloc_extradata(st->codecpar, smk->treesize + 16)) < 0) {
148 av_log(s, AV_LOG_ERROR,
149 "Cannot allocate %"PRIu32" bytes of extradata\n",
153 if ((ret = ffio_read_size(pb, st->codecpar->extradata, 16)) < 0)
156 for(i = 0; i < 7; i++) {
157 smk->rates[i] = avio_rl24(pb);
158 smk->aflags[i] = avio_r8(pb);
160 smk->pad = avio_rl32(pb);
162 smk->frm_size = av_malloc_array(smk->frames, sizeof(*smk->frm_size));
163 smk->frm_flags = av_malloc(smk->frames);
164 if (!smk->frm_size || !smk->frm_flags) {
165 av_freep(&smk->frm_size);
166 av_freep(&smk->frm_flags);
167 return AVERROR(ENOMEM);
170 smk->is_ver4 = (smk->magic != MKTAG('S', 'M', 'K', '2'));
172 /* read frame info */
173 for(i = 0; i < smk->frames; i++) {
174 smk->frm_size[i] = avio_rl32(pb);
176 for(i = 0; i < smk->frames; i++) {
177 smk->frm_flags[i] = avio_r8(pb);
180 /* init video codec */
181 smk->videoindex = st->index;
182 st->codecpar->width = smk->width;
183 st->codecpar->height = smk->height;
184 st->codecpar->format = AV_PIX_FMT_PAL8;
185 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
186 st->codecpar->codec_id = AV_CODEC_ID_SMACKVIDEO;
187 st->codecpar->codec_tag = smk->magic;
188 /* Smacker uses 100000 as internal timebase */
190 smk->pts_inc = -smk->pts_inc;
194 av_reduce(&tbase, &smk->pts_inc, tbase, smk->pts_inc, (1UL<<31)-1);
195 avpriv_set_pts_info(st, 33, smk->pts_inc, tbase);
196 st->duration = smk->frames;
197 /* handle possible audio streams */
198 for(i = 0; i < 7; i++) {
199 smk->indexes[i] = -1;
201 ast[i] = avformat_new_stream(s, NULL);
203 av_freep(&smk->frm_size);
204 av_freep(&smk->frm_flags);
205 return AVERROR(ENOMEM);
207 smk->indexes[i] = ast[i]->index;
208 ast[i]->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
209 if (smk->aflags[i] & SMK_AUD_BINKAUD) {
210 ast[i]->codecpar->codec_id = AV_CODEC_ID_BINKAUDIO_RDFT;
211 } else if (smk->aflags[i] & SMK_AUD_USEDCT) {
212 ast[i]->codecpar->codec_id = AV_CODEC_ID_BINKAUDIO_DCT;
213 } else if (smk->aflags[i] & SMK_AUD_PACKED){
214 ast[i]->codecpar->codec_id = AV_CODEC_ID_SMACKAUDIO;
215 ast[i]->codecpar->codec_tag = MKTAG('S', 'M', 'K', 'A');
217 ast[i]->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
219 if (smk->aflags[i] & SMK_AUD_STEREO) {
220 ast[i]->codecpar->channels = 2;
221 ast[i]->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
223 ast[i]->codecpar->channels = 1;
224 ast[i]->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
226 ast[i]->codecpar->sample_rate = smk->rates[i];
227 ast[i]->codecpar->bits_per_coded_sample = (smk->aflags[i] & SMK_AUD_16BITS) ? 16 : 8;
228 if(ast[i]->codecpar->bits_per_coded_sample == 16 && ast[i]->codecpar->codec_id == AV_CODEC_ID_PCM_U8)
229 ast[i]->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
230 avpriv_set_pts_info(ast[i], 64, 1, ast[i]->codecpar->sample_rate
231 * ast[i]->codecpar->channels * ast[i]->codecpar->bits_per_coded_sample / 8);
236 /* load trees to extradata, they will be unpacked by decoder */
237 ret = avio_read(pb, st->codecpar->extradata + 16, st->codecpar->extradata_size - 16);
238 if(ret != st->codecpar->extradata_size - 16){
239 av_freep(&smk->frm_size);
240 av_freep(&smk->frm_flags);
245 smk->nextpos = avio_tell(pb);
251 static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
253 SmackerContext *smk = s->priv_data;
260 if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
263 /* if we demuxed all streams, pass another frame */
264 if(smk->curstream < 0) {
265 avio_seek(s->pb, smk->nextpos, 0);
266 frame_size = smk->frm_size[smk->cur_frame] & (~3);
267 flags = smk->frm_flags[smk->cur_frame];
268 /* handle palette change event */
269 if(flags & SMACKER_PAL){
270 int size, sz, t, off, j, pos;
271 uint8_t *pal = smk->pal;
274 memcpy(oldpal, pal, 768);
275 size = avio_r8(s->pb);
277 if(size + 1 > frame_size)
278 return AVERROR_INVALIDDATA;
282 pos = avio_tell(s->pb) + size;
285 if(t & 0x80){ /* skip palette entries */
286 sz += (t & 0x7F) + 1;
287 pal += ((t & 0x7F) + 1) * 3;
288 } else if(t & 0x40){ /* copy with offset */
289 off = avio_r8(s->pb);
291 if (off + j > 0x100) {
292 av_log(s, AV_LOG_ERROR,
293 "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
295 return AVERROR_INVALIDDATA;
298 while(j-- && sz < 256) {
299 *pal++ = oldpal[off + 0];
300 *pal++ = oldpal[off + 1];
301 *pal++ = oldpal[off + 2];
305 } else { /* new entries */
307 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
308 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
312 avio_seek(s->pb, pos, 0);
317 /* if audio chunks are present, put them to stack and retrieve later */
318 for(i = 0; i < 7; i++) {
323 size = avio_rl32(s->pb) - 4;
324 if (!size || size + 4LL > frame_size) {
325 av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
326 return AVERROR_INVALIDDATA;
331 if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) {
332 smk->buf_sizes[smk->curstream] = 0;
335 smk->buf_sizes[smk->curstream] = size;
336 ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
339 smk->stream_id[smk->curstream] = smk->indexes[i];
343 if (frame_size < 0 || frame_size >= INT_MAX/2)
344 return AVERROR_INVALIDDATA;
345 if ((ret = av_new_packet(pkt, frame_size + 769)) < 0)
347 if(smk->frm_size[smk->cur_frame] & 1)
349 pkt->data[0] = palchange;
350 memcpy(pkt->data + 1, smk->pal, 768);
351 ret = avio_read(s->pb, pkt->data + 769, frame_size);
352 if(ret != frame_size)
354 pkt->stream_index = smk->videoindex;
355 pkt->pts = smk->cur_frame;
356 pkt->size = ret + 769;
358 smk->nextpos = avio_tell(s->pb);
360 if (smk->stream_id[smk->curstream] < 0 || !smk->bufs[smk->curstream])
361 return AVERROR_INVALIDDATA;
362 if ((ret = av_new_packet(pkt, smk->buf_sizes[smk->curstream])) < 0)
364 memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
365 pkt->size = smk->buf_sizes[smk->curstream];
366 pkt->stream_index = smk->stream_id[smk->curstream];
367 pkt->pts = smk->aud_pts[smk->curstream];
368 smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);
375 static int smacker_read_close(AVFormatContext *s)
377 SmackerContext *smk = s->priv_data;
380 for(i = 0; i < 7; i++)
381 av_freep(&smk->bufs[i]);
382 av_freep(&smk->frm_size);
383 av_freep(&smk->frm_flags);
388 AVInputFormat ff_smacker_demuxer = {
390 .long_name = NULL_IF_CONFIG_SMALL("Smacker"),
391 .priv_data_size = sizeof(SmackerContext),
392 .read_probe = smacker_probe,
393 .read_header = smacker_read_header,
394 .read_packet = smacker_read_packet,
395 .read_close = smacker_read_close,