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
26 #include "libavutil/bswap.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/intreadwrite.h"
32 #define SMACKER_PAL 0x01
33 #define SMACKER_FLAG_RING_FRAME 0x01
36 SMK_AUD_PACKED
= 0x80,
37 SMK_AUD_16BITS
= 0x20,
38 SMK_AUD_STEREO
= 0x10,
39 SMK_AUD_BINKAUD
= 0x08,
43 typedef struct SmackerContext
{
44 /* Smacker file header */
46 uint32_t width
, height
;
52 uint32_t mmap_size
, mclr_size
, full_size
, type_size
;
59 /* internal variables */
63 /* current frame for demuxing */
75 typedef struct SmackerFrame
{
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
93 static int smacker_probe(AVProbeData
*p
)
95 if(p
->buf
[0] == 'S' && p
->buf
[1] == 'M' && p
->buf
[2] == 'K'
96 && (p
->buf
[3] == '2' || p
->buf
[3] == '4'))
97 return AVPROBE_SCORE_MAX
;
102 static int smacker_read_header(AVFormatContext
*s
)
104 AVIOContext
*pb
= s
->pb
;
105 SmackerContext
*smk
= s
->priv_data
;
106 AVStream
*st
, *ast
[7];
110 /* read and check header */
111 smk
->magic
= avio_rl32(pb
);
112 if (smk
->magic
!= MKTAG('S', 'M', 'K', '2') && smk
->magic
!= MKTAG('S', 'M', 'K', '4'))
114 smk
->width
= avio_rl32(pb
);
115 smk
->height
= avio_rl32(pb
);
116 smk
->frames
= avio_rl32(pb
);
117 smk
->pts_inc
= (int32_t)avio_rl32(pb
);
118 smk
->flags
= avio_rl32(pb
);
119 if(smk
->flags
& SMACKER_FLAG_RING_FRAME
)
121 for(i
= 0; i
< 7; i
++)
122 smk
->audio
[i
] = avio_rl32(pb
);
123 smk
->treesize
= avio_rl32(pb
);
125 if(smk
->treesize
>= UINT_MAX
/4){ // smk->treesize + 16 must not overflow (this check is probably redundant)
126 av_log(s
, AV_LOG_ERROR
, "treesize too large\n");
130 //FIXME remove extradata "rebuilding"
131 smk
->mmap_size
= avio_rl32(pb
);
132 smk
->mclr_size
= avio_rl32(pb
);
133 smk
->full_size
= avio_rl32(pb
);
134 smk
->type_size
= avio_rl32(pb
);
135 for(i
= 0; i
< 7; i
++) {
136 smk
->rates
[i
] = avio_rl24(pb
);
137 smk
->aflags
[i
] = avio_r8(pb
);
139 smk
->pad
= avio_rl32(pb
);
141 if(smk
->frames
> 0xFFFFFF) {
142 av_log(s
, AV_LOG_ERROR
, "Too many frames: %i\n", smk
->frames
);
145 smk
->frm_size
= av_malloc(smk
->frames
* 4);
146 smk
->frm_flags
= av_malloc(smk
->frames
);
148 smk
->is_ver4
= (smk
->magic
!= MKTAG('S', 'M', 'K', '2'));
150 /* read frame info */
151 for(i
= 0; i
< smk
->frames
; i
++) {
152 smk
->frm_size
[i
] = avio_rl32(pb
);
154 for(i
= 0; i
< smk
->frames
; i
++) {
155 smk
->frm_flags
[i
] = avio_r8(pb
);
158 /* init video codec */
159 st
= avformat_new_stream(s
, NULL
);
162 smk
->videoindex
= st
->index
;
163 st
->codec
->width
= smk
->width
;
164 st
->codec
->height
= smk
->height
;
165 st
->codec
->pix_fmt
= AV_PIX_FMT_PAL8
;
166 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
167 st
->codec
->codec_id
= AV_CODEC_ID_SMACKVIDEO
;
168 st
->codec
->codec_tag
= smk
->magic
;
169 /* Smacker uses 100000 as internal timebase */
171 smk
->pts_inc
= -smk
->pts_inc
;
175 av_reduce(&tbase
, &smk
->pts_inc
, tbase
, smk
->pts_inc
, (1UL<<31)-1);
176 avpriv_set_pts_info(st
, 33, smk
->pts_inc
, tbase
);
177 st
->duration
= smk
->frames
;
178 /* handle possible audio streams */
179 for(i
= 0; i
< 7; i
++) {
180 smk
->indexes
[i
] = -1;
182 ast
[i
] = avformat_new_stream(s
, NULL
);
183 smk
->indexes
[i
] = ast
[i
]->index
;
184 ast
[i
]->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
185 if (smk
->aflags
[i
] & SMK_AUD_BINKAUD
) {
186 ast
[i
]->codec
->codec_id
= AV_CODEC_ID_BINKAUDIO_RDFT
;
187 } else if (smk
->aflags
[i
] & SMK_AUD_USEDCT
) {
188 ast
[i
]->codec
->codec_id
= AV_CODEC_ID_BINKAUDIO_DCT
;
189 } else if (smk
->aflags
[i
] & SMK_AUD_PACKED
){
190 ast
[i
]->codec
->codec_id
= AV_CODEC_ID_SMACKAUDIO
;
191 ast
[i
]->codec
->codec_tag
= MKTAG('S', 'M', 'K', 'A');
193 ast
[i
]->codec
->codec_id
= AV_CODEC_ID_PCM_U8
;
195 if (smk
->aflags
[i
] & SMK_AUD_STEREO
) {
196 ast
[i
]->codec
->channels
= 2;
197 ast
[i
]->codec
->channel_layout
= AV_CH_LAYOUT_STEREO
;
199 ast
[i
]->codec
->channels
= 1;
200 ast
[i
]->codec
->channel_layout
= AV_CH_LAYOUT_MONO
;
202 ast
[i
]->codec
->sample_rate
= smk
->rates
[i
];
203 ast
[i
]->codec
->bits_per_coded_sample
= (smk
->aflags
[i
] & SMK_AUD_16BITS
) ? 16 : 8;
204 if(ast
[i
]->codec
->bits_per_coded_sample
== 16 && ast
[i
]->codec
->codec_id
== AV_CODEC_ID_PCM_U8
)
205 ast
[i
]->codec
->codec_id
= AV_CODEC_ID_PCM_S16LE
;
206 avpriv_set_pts_info(ast
[i
], 64, 1, ast
[i
]->codec
->sample_rate
207 * ast
[i
]->codec
->channels
* ast
[i
]->codec
->bits_per_coded_sample
/ 8);
212 /* load trees to extradata, they will be unpacked by decoder */
213 st
->codec
->extradata
= av_malloc(smk
->treesize
+ 16);
214 st
->codec
->extradata_size
= smk
->treesize
+ 16;
215 if(!st
->codec
->extradata
){
216 av_log(s
, AV_LOG_ERROR
, "Cannot allocate %i bytes of extradata\n", smk
->treesize
+ 16);
217 av_free(smk
->frm_size
);
218 av_free(smk
->frm_flags
);
221 ret
= avio_read(pb
, st
->codec
->extradata
+ 16, st
->codec
->extradata_size
- 16);
222 if(ret
!= st
->codec
->extradata_size
- 16){
223 av_free(smk
->frm_size
);
224 av_free(smk
->frm_flags
);
227 ((int32_t*)st
->codec
->extradata
)[0] = av_le2ne32(smk
->mmap_size
);
228 ((int32_t*)st
->codec
->extradata
)[1] = av_le2ne32(smk
->mclr_size
);
229 ((int32_t*)st
->codec
->extradata
)[2] = av_le2ne32(smk
->full_size
);
230 ((int32_t*)st
->codec
->extradata
)[3] = av_le2ne32(smk
->type_size
);
233 smk
->nextpos
= avio_tell(pb
);
239 static int smacker_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
241 SmackerContext
*smk
= s
->priv_data
;
248 if (s
->pb
->eof_reached
|| smk
->cur_frame
>= smk
->frames
)
251 /* if we demuxed all streams, pass another frame */
252 if(smk
->curstream
< 0) {
253 avio_seek(s
->pb
, smk
->nextpos
, 0);
254 frame_size
= smk
->frm_size
[smk
->cur_frame
] & (~3);
255 flags
= smk
->frm_flags
[smk
->cur_frame
];
256 /* handle palette change event */
257 if(flags
& SMACKER_PAL
){
258 int size
, sz
, t
, off
, j
, pos
;
259 uint8_t *pal
= smk
->pal
;
262 memcpy(oldpal
, pal
, 768);
263 size
= avio_r8(s
->pb
);
268 pos
= avio_tell(s
->pb
) + size
;
271 if(t
& 0x80){ /* skip palette entries */
272 sz
+= (t
& 0x7F) + 1;
273 pal
+= ((t
& 0x7F) + 1) * 3;
274 } else if(t
& 0x40){ /* copy with offset */
275 off
= avio_r8(s
->pb
);
277 if (off
+ j
> 0xff) {
278 av_log(s
, AV_LOG_ERROR
,
279 "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
281 return AVERROR_INVALIDDATA
;
284 while(j
-- && sz
< 256) {
285 *pal
++ = oldpal
[off
+ 0];
286 *pal
++ = oldpal
[off
+ 1];
287 *pal
++ = oldpal
[off
+ 2];
291 } else { /* new entries */
293 *pal
++ = smk_pal
[avio_r8(s
->pb
) & 0x3F];
294 *pal
++ = smk_pal
[avio_r8(s
->pb
) & 0x3F];
298 avio_seek(s
->pb
, pos
, 0);
303 /* if audio chunks are present, put them to stack and retrieve later */
304 for(i
= 0; i
< 7; i
++) {
309 size
= avio_rl32(s
->pb
) - 4;
313 tmpbuf
= av_realloc(smk
->bufs
[smk
->curstream
], size
);
315 return AVERROR(ENOMEM
);
316 smk
->bufs
[smk
->curstream
] = tmpbuf
;
317 smk
->buf_sizes
[smk
->curstream
] = size
;
318 ret
= avio_read(s
->pb
, smk
->bufs
[smk
->curstream
], size
);
321 smk
->stream_id
[smk
->curstream
] = smk
->indexes
[i
];
326 return AVERROR_INVALIDDATA
;
327 if (av_new_packet(pkt
, frame_size
+ 769))
328 return AVERROR(ENOMEM
);
329 if(smk
->frm_size
[smk
->cur_frame
] & 1)
331 pkt
->data
[0] = palchange
;
332 memcpy(pkt
->data
+ 1, smk
->pal
, 768);
333 ret
= avio_read(s
->pb
, pkt
->data
+ 769, frame_size
);
334 if(ret
!= frame_size
)
336 pkt
->stream_index
= smk
->videoindex
;
337 pkt
->size
= ret
+ 769;
339 smk
->nextpos
= avio_tell(s
->pb
);
341 if (av_new_packet(pkt
, smk
->buf_sizes
[smk
->curstream
]))
342 return AVERROR(ENOMEM
);
343 memcpy(pkt
->data
, smk
->bufs
[smk
->curstream
], smk
->buf_sizes
[smk
->curstream
]);
344 pkt
->size
= smk
->buf_sizes
[smk
->curstream
];
345 pkt
->stream_index
= smk
->stream_id
[smk
->curstream
];
346 pkt
->pts
= smk
->aud_pts
[smk
->curstream
];
347 smk
->aud_pts
[smk
->curstream
] += AV_RL32(pkt
->data
);
354 static int smacker_read_close(AVFormatContext
*s
)
356 SmackerContext
*smk
= s
->priv_data
;
359 for(i
= 0; i
< 7; i
++)
360 av_free(smk
->bufs
[i
]);
361 av_free(smk
->frm_size
);
362 av_free(smk
->frm_flags
);
367 AVInputFormat ff_smacker_demuxer
= {
369 .long_name
= NULL_IF_CONFIG_SMALL("Smacker video"),
370 .priv_data_size
= sizeof(SmackerContext
),
371 .read_probe
= smacker_probe
,
372 .read_header
= smacker_read_header
,
373 .read_packet
= smacker_read_packet
,
374 .read_close
= smacker_read_close
,