3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of Libav.
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.
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.
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
23 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
28 #include "libavutil/bswap.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/intreadwrite.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;
54 uint32_t mmap_size, mclr_size, full_size, type_size;
61 /* internal variables */
65 /* current frame for demuxing */
77 typedef struct SmackerFrame {
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
95 static int smacker_probe(AVProbeData *p)
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;
104 static int smacker_read_header(AVFormatContext *s)
106 AVIOContext *pb = s->pb;
107 SmackerContext *smk = s->priv_data;
108 AVStream *st, *ast[7];
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'))
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)
123 for(i = 0; i < 7; i++)
124 smk->audio[i] = avio_rl32(pb);
125 smk->treesize = avio_rl32(pb);
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");
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);
141 smk->pad = avio_rl32(pb);
143 if(smk->frames > 0xFFFFFF) {
144 av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
147 smk->frm_size = av_malloc(smk->frames * 4);
148 smk->frm_flags = av_malloc(smk->frames);
150 smk->is_ver4 = (smk->magic != MKTAG('S', 'M', 'K', '2'));
152 /* read frame info */
153 for(i = 0; i < smk->frames; i++) {
154 smk->frm_size[i] = avio_rl32(pb);
156 for(i = 0; i < smk->frames; i++) {
157 smk->frm_flags[i] = avio_r8(pb);
160 /* init video codec */
161 st = avformat_new_stream(s, NULL);
164 smk->videoindex = st->index;
165 st->codec->width = smk->width;
166 st->codec->height = smk->height;
167 st->codec->pix_fmt = AV_PIX_FMT_PAL8;
168 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
169 st->codec->codec_id = AV_CODEC_ID_SMACKVIDEO;
170 st->codec->codec_tag = smk->magic;
171 /* Smacker uses 100000 as internal timebase */
173 smk->pts_inc = -smk->pts_inc;
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;
184 ast[i] = avformat_new_stream(s, NULL);
185 smk->indexes[i] = ast[i]->index;
186 ast[i]->codec->codec_type = AVMEDIA_TYPE_AUDIO;
187 if (smk->aflags[i] & SMK_AUD_BINKAUD) {
188 ast[i]->codec->codec_id = AV_CODEC_ID_BINKAUDIO_RDFT;
189 } else if (smk->aflags[i] & SMK_AUD_USEDCT) {
190 ast[i]->codec->codec_id = AV_CODEC_ID_BINKAUDIO_DCT;
191 } else if (smk->aflags[i] & SMK_AUD_PACKED){
192 ast[i]->codec->codec_id = AV_CODEC_ID_SMACKAUDIO;
193 ast[i]->codec->codec_tag = MKTAG('S', 'M', 'K', 'A');
195 ast[i]->codec->codec_id = AV_CODEC_ID_PCM_U8;
197 if (smk->aflags[i] & SMK_AUD_STEREO) {
198 ast[i]->codec->channels = 2;
199 ast[i]->codec->channel_layout = AV_CH_LAYOUT_STEREO;
201 ast[i]->codec->channels = 1;
202 ast[i]->codec->channel_layout = AV_CH_LAYOUT_MONO;
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)
207 ast[i]->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
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);
214 /* load trees to extradata, they will be unpacked by decoder */
215 st->codec->extradata = av_mallocz(smk->treesize + 16 +
216 AV_INPUT_BUFFER_PADDING_SIZE);
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",
222 av_free(smk->frm_size);
223 av_free(smk->frm_flags);
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);
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);
238 smk->nextpos = avio_tell(pb);
244 static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
246 SmackerContext *smk = s->priv_data;
253 if (s->pb->eof_reached || smk->cur_frame >= smk->frames)
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;
267 memcpy(oldpal, pal, 768);
268 size = avio_r8(s->pb);
273 pos = avio_tell(s->pb) + size;
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);
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",
286 return AVERROR_INVALIDDATA;
289 while(j-- && sz < 256) {
290 *pal++ = oldpal[off + 0];
291 *pal++ = oldpal[off + 1];
292 *pal++ = oldpal[off + 2];
296 } else { /* new entries */
298 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
299 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
303 avio_seek(s->pb, pos, 0);
308 /* if audio chunks are present, put them to stack and retrieve later */
309 for(i = 0; i < 7; i++) {
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;
322 if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) {
323 smk->buf_sizes[smk->curstream] = 0;
326 smk->buf_sizes[smk->curstream] = size;
327 ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
330 smk->stream_id[smk->curstream] = smk->indexes[i];
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)
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)
345 pkt->stream_index = smk->videoindex;
346 pkt->pts = smk->cur_frame;
347 pkt->size = ret + 769;
349 smk->nextpos = avio_tell(s->pb);
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);
366 static int smacker_read_close(AVFormatContext *s)
368 SmackerContext *smk = s->priv_data;
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);
379 AVInputFormat ff_smacker_demuxer = {
381 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
382 .priv_data_size = sizeof(SmackerContext),
383 .read_probe = smacker_probe,
384 .read_header = smacker_read_header,
385 .read_packet = smacker_read_packet,
386 .read_close = smacker_read_close,