1019
|
1 /*
|
|
2 * Smacker decoder
|
|
3 * Copyright (c) 2006 Konstantin Shishkov.
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 */
|
|
19
|
|
20 /*
|
|
21 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
|
|
22 */
|
|
23
|
|
24 #include "avformat.h"
|
|
25 #include "avi.h"
|
|
26 #include "bswap.h"
|
|
27
|
|
28 #define SMACKER_PAL 0x01
|
|
29
|
|
30 enum SAudFlags {
|
|
31 SMK_AUD_PACKED = 0x80000000,
|
|
32 SMK_AUD_16BITS = 0x20000000,
|
|
33 SMK_AUD_STEREO = 0x10000000,
|
|
34 SMK_AUD_BINKAUD = 0x08000000,
|
|
35 SMK_AUD_USEDCT = 0x04000000
|
|
36 };
|
|
37
|
|
38 typedef struct SmackerContext {
|
|
39 /* Smacker file header */
|
|
40 uint32_t magic;
|
|
41 uint32_t width, height;
|
|
42 uint32_t frames;
|
|
43 int pts_inc;
|
|
44 uint32_t flags;
|
|
45 uint32_t audio[7];
|
|
46 uint32_t treesize;
|
|
47 uint32_t mmap_size, mclr_size, full_size, type_size;
|
|
48 uint32_t rates[7];
|
|
49 uint32_t pad;
|
|
50 /* frame info */
|
|
51 uint32_t *frm_size;
|
|
52 uint8_t *frm_flags;
|
|
53 /* internal variables */
|
|
54 int cur_frame;
|
|
55 int is_ver4;
|
|
56 int64_t cur_pts;
|
|
57 /* current frame for demuxing */
|
|
58 uint8_t pal[768];
|
|
59 int indexes[7];
|
|
60 int videoindex;
|
|
61 uint8_t *bufs[7];
|
|
62 int buf_sizes[7];
|
|
63 int stream_id[7];
|
|
64 int curstream;
|
|
65 offset_t nextpos;
|
|
66 } SmackerContext;
|
|
67
|
|
68 typedef struct SmackerFrame {
|
|
69 int64_t pts;
|
|
70 int stream;
|
|
71 } SmackerFrame;
|
|
72
|
|
73 /* palette used in Smacker */
|
|
74 static const uint8_t smk_pal[64] = {
|
|
75 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
|
|
76 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
|
|
77 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
|
|
78 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
|
|
79 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
|
|
80 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
|
|
81 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
|
|
82 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
|
|
83 };
|
|
84
|
|
85
|
|
86 static int smacker_probe(AVProbeData *p)
|
|
87 {
|
|
88 if (p->buf_size < 4)
|
|
89 return 0;
|
|
90 if(p->buf[0] == 'S' && p->buf[1] == 'M' && p->buf[2] == 'K'
|
|
91 && (p->buf[3] == '2' || p->buf[3] == '4'))
|
|
92 return AVPROBE_SCORE_MAX;
|
|
93 else
|
|
94 return 0;
|
|
95 }
|
|
96
|
|
97 static int smacker_read_header(AVFormatContext *s, AVFormatParameters *ap)
|
|
98 {
|
|
99 ByteIOContext *pb = &s->pb;
|
|
100 SmackerContext *smk = (SmackerContext *)s->priv_data;
|
|
101 AVStream *st, *ast[7];
|
|
102 int i, ret;
|
|
103 int tbase;
|
|
104
|
|
105 /* read and check header */
|
|
106 smk->magic = get_le32(pb);
|
|
107 if (smk->magic != MKTAG('S', 'M', 'K', '2') && smk->magic != MKTAG('S', 'M', 'K', '4'))
|
|
108 return -1;
|
|
109 smk->width = get_le32(pb);
|
|
110 smk->height = get_le32(pb);
|
|
111 smk->frames = get_le32(pb);
|
|
112 smk->pts_inc = (int32_t)get_le32(pb);
|
|
113 smk->flags = get_le32(pb);
|
|
114 for(i = 0; i < 7; i++)
|
|
115 smk->audio[i] = get_le32(pb);
|
|
116 smk->treesize = get_le32(pb);
|
1079
|
117
|
|
118 if(smk->treesize >= UINT_MAX/4){ // smk->treesize + 16 must not overflow (this check is probably redundant)
|
|
119 av_log(s, AV_LOG_ERROR, "treesize too large\n");
|
|
120 return -1;
|
|
121 }
|
|
122
|
|
123 //FIXME remove extradata "rebuilding"
|
1019
|
124 smk->mmap_size = get_le32(pb);
|
|
125 smk->mclr_size = get_le32(pb);
|
|
126 smk->full_size = get_le32(pb);
|
|
127 smk->type_size = get_le32(pb);
|
|
128 for(i = 0; i < 7; i++)
|
|
129 smk->rates[i] = get_le32(pb);
|
|
130 smk->pad = get_le32(pb);
|
|
131 /* setup data */
|
|
132 if(smk->frames > 0xFFFFFF) {
|
|
133 av_log(s, AV_LOG_ERROR, "Too many frames: %i\n", smk->frames);
|
|
134 return -1;
|
|
135 }
|
|
136 smk->frm_size = av_malloc(smk->frames * 4);
|
|
137 smk->frm_flags = av_malloc(smk->frames);
|
|
138
|
|
139 smk->is_ver4 = (smk->magic != MKTAG('S', 'M', 'K', '2'));
|
|
140
|
|
141 /* read frame info */
|
|
142 for(i = 0; i < smk->frames; i++) {
|
|
143 smk->frm_size[i] = get_le32(pb);
|
|
144 }
|
|
145 for(i = 0; i < smk->frames; i++) {
|
|
146 smk->frm_flags[i] = get_byte(pb);
|
|
147 }
|
|
148
|
|
149 /* init video codec */
|
|
150 st = av_new_stream(s, 0);
|
|
151 if (!st)
|
|
152 return -1;
|
|
153 smk->videoindex = st->index;
|
|
154 st->codec->width = smk->width;
|
|
155 st->codec->height = smk->height;
|
|
156 st->codec->pix_fmt = PIX_FMT_PAL8;
|
|
157 st->codec->codec_type = CODEC_TYPE_VIDEO;
|
|
158 st->codec->codec_id = CODEC_ID_SMACKVIDEO;
|
|
159 st->codec->codec_tag = smk->is_ver4;
|
|
160 /* Smacker uses 100000 as internal timebase */
|
|
161 if(smk->pts_inc < 0)
|
|
162 smk->pts_inc = -smk->pts_inc;
|
|
163 else
|
|
164 smk->pts_inc *= 100;
|
|
165 tbase = 100000;
|
|
166 av_reduce(&tbase, &smk->pts_inc, tbase, smk->pts_inc, (1UL<<31)-1);
|
|
167 av_set_pts_info(st, 33, smk->pts_inc, tbase);
|
|
168 /* handle possible audio streams */
|
|
169 for(i = 0; i < 7; i++) {
|
|
170 smk->indexes[i] = -1;
|
|
171 if((smk->rates[i] & 0xFFFFFF) && !(smk->rates[i] & SMK_AUD_BINKAUD)){
|
|
172 ast[i] = av_new_stream(s, 0);
|
|
173 smk->indexes[i] = ast[i]->index;
|
|
174 av_set_pts_info(ast[i], 33, smk->pts_inc, tbase);
|
|
175 ast[i]->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
176 ast[i]->codec->codec_id = (smk->rates[i] & SMK_AUD_PACKED) ? CODEC_ID_SMACKAUDIO : CODEC_ID_PCM_U8;
|
|
177 ast[i]->codec->codec_tag = 0;
|
|
178 ast[i]->codec->channels = (smk->rates[i] & SMK_AUD_STEREO) ? 2 : 1;
|
|
179 ast[i]->codec->sample_rate = smk->rates[i] & 0xFFFFFF;
|
|
180 ast[i]->codec->bits_per_sample = (smk->rates[i] & SMK_AUD_16BITS) ? 16 : 8;
|
|
181 if(ast[i]->codec->bits_per_sample == 16 && ast[i]->codec->codec_id == CODEC_ID_PCM_U8)
|
|
182 ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE;
|
|
183 }
|
|
184 }
|
|
185
|
|
186
|
|
187 /* load trees to extradata, they will be unpacked by decoder */
|
|
188 st->codec->extradata = av_malloc(smk->treesize + 16);
|
|
189 st->codec->extradata_size = smk->treesize + 16;
|
|
190 if(!st->codec->extradata){
|
|
191 av_log(s, AV_LOG_ERROR, "Cannot allocate %i bytes of extradata\n", smk->treesize + 16);
|
|
192 av_free(smk->frm_size);
|
|
193 av_free(smk->frm_flags);
|
|
194 return -1;
|
|
195 }
|
|
196 ret = get_buffer(pb, st->codec->extradata + 16, st->codec->extradata_size - 16);
|
|
197 if(ret != st->codec->extradata_size - 16){
|
|
198 av_free(smk->frm_size);
|
|
199 av_free(smk->frm_flags);
|
|
200 return AVERROR_IO;
|
|
201 }
|
|
202 ((int32_t*)st->codec->extradata)[0] = le2me_32(smk->mmap_size);
|
|
203 ((int32_t*)st->codec->extradata)[1] = le2me_32(smk->mclr_size);
|
|
204 ((int32_t*)st->codec->extradata)[2] = le2me_32(smk->full_size);
|
|
205 ((int32_t*)st->codec->extradata)[3] = le2me_32(smk->type_size);
|
|
206
|
|
207 smk->curstream = -1;
|
|
208 smk->nextpos = url_ftell(pb);
|
|
209
|
|
210 return 0;
|
|
211 }
|
|
212
|
|
213
|
|
214 static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
215 {
|
|
216 SmackerContext *smk = (SmackerContext *)s->priv_data;
|
|
217 int flags;
|
|
218 int ret;
|
|
219 int i;
|
|
220 int frame_size = 0;
|
|
221 int palchange = 0;
|
|
222 int pos;
|
|
223
|
|
224 if (url_feof(&s->pb) || smk->cur_frame >= smk->frames)
|
|
225 return -EIO;
|
|
226
|
|
227 /* if we demuxed all streams, pass another frame */
|
|
228 if(smk->curstream < 0) {
|
|
229 url_fseek(&s->pb, smk->nextpos, 0);
|
|
230 frame_size = smk->frm_size[smk->cur_frame] & (~3);
|
|
231 flags = smk->frm_flags[smk->cur_frame];
|
|
232 /* handle palette change event */
|
|
233 pos = url_ftell(&s->pb);
|
|
234 if(flags & SMACKER_PAL){
|
|
235 int size, sz, t, off, j, pos;
|
|
236 uint8_t *pal = smk->pal;
|
|
237 uint8_t oldpal[768];
|
|
238
|
|
239 memcpy(oldpal, pal, 768);
|
|
240 size = get_byte(&s->pb);
|
|
241 size = size * 4 - 1;
|
|
242 frame_size -= size;
|
|
243 frame_size--;
|
|
244 sz = 0;
|
|
245 pos = url_ftell(&s->pb) + size;
|
|
246 while(sz < 256){
|
|
247 t = get_byte(&s->pb);
|
|
248 if(t & 0x80){ /* skip palette entries */
|
|
249 sz += (t & 0x7F) + 1;
|
|
250 pal += ((t & 0x7F) + 1) * 3;
|
|
251 } else if(t & 0x40){ /* copy with offset */
|
|
252 off = get_byte(&s->pb) * 3;
|
|
253 j = (t & 0x3F) + 1;
|
|
254 while(j-- && sz < 256) {
|
|
255 *pal++ = oldpal[off + 0];
|
|
256 *pal++ = oldpal[off + 1];
|
|
257 *pal++ = oldpal[off + 2];
|
|
258 sz++;
|
|
259 off += 3;
|
|
260 }
|
|
261 } else { /* new entries */
|
|
262 *pal++ = smk_pal[t];
|
|
263 *pal++ = smk_pal[get_byte(&s->pb) & 0x3F];
|
|
264 *pal++ = smk_pal[get_byte(&s->pb) & 0x3F];
|
|
265 sz++;
|
|
266 }
|
|
267 }
|
|
268 url_fseek(&s->pb, pos, 0);
|
|
269 palchange |= 1;
|
|
270 }
|
|
271 flags >>= 1;
|
|
272 smk->curstream = -1;
|
|
273 /* if audio chunks are present, put them to stack and retrieve later */
|
|
274 for(i = 0; i < 7; i++) {
|
|
275 if(flags & 1) {
|
|
276 int size;
|
|
277 size = get_le32(&s->pb) - 4;
|
|
278 frame_size -= size;
|
|
279 frame_size -= 4;
|
|
280 smk->curstream++;
|
|
281 smk->bufs[smk->curstream] = av_realloc(smk->bufs[smk->curstream], size);
|
|
282 smk->buf_sizes[smk->curstream] = size;
|
|
283 ret = get_buffer(&s->pb, smk->bufs[smk->curstream], size);
|
|
284 if(ret != size)
|
|
285 return AVERROR_IO;
|
|
286 smk->stream_id[smk->curstream] = smk->indexes[i];
|
|
287 }
|
|
288 flags >>= 1;
|
|
289 }
|
|
290 if (av_new_packet(pkt, frame_size + 768))
|
|
291 return AVERROR_NOMEM;
|
|
292 if(smk->frm_size[smk->cur_frame] & 1)
|
|
293 palchange |= 2;
|
|
294 pkt->data[0] = palchange;
|
|
295 memcpy(pkt->data + 1, smk->pal, 768);
|
|
296 ret = get_buffer(&s->pb, pkt->data + 769, frame_size);
|
|
297 if(ret != frame_size)
|
|
298 return AVERROR_IO;
|
|
299 pkt->stream_index = smk->videoindex;
|
|
300 pkt->size = ret + 769;
|
|
301 smk->cur_frame++;
|
|
302 smk->nextpos = url_ftell(&s->pb);
|
|
303 } else {
|
|
304 if (av_new_packet(pkt, smk->buf_sizes[smk->curstream]))
|
|
305 return AVERROR_NOMEM;
|
|
306 memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
|
|
307 pkt->size = smk->buf_sizes[smk->curstream];
|
|
308 pkt->stream_index = smk->stream_id[smk->curstream];
|
|
309 smk->curstream--;
|
|
310 }
|
|
311
|
|
312 return 0;
|
|
313 }
|
|
314
|
|
315 static int smacker_read_close(AVFormatContext *s)
|
|
316 {
|
|
317 SmackerContext *smk = (SmackerContext *)s->priv_data;
|
|
318 int i;
|
|
319
|
|
320 for(i = 0; i < 7; i++)
|
|
321 if(smk->bufs[i])
|
|
322 av_free(smk->bufs[i]);
|
|
323 if(smk->frm_size)
|
|
324 av_free(smk->frm_size);
|
|
325 if(smk->frm_flags)
|
|
326 av_free(smk->frm_flags);
|
|
327
|
|
328 for(i=0;i<s->nb_streams;i++) {
|
|
329 AVStream *st = s->streams[i];
|
|
330 if(st->codec->extradata)
|
|
331 av_free(st->codec->extradata);
|
|
332 }
|
|
333 return 0;
|
|
334 }
|
|
335
|
|
336 static AVInputFormat smacker_iformat = {
|
|
337 "smk",
|
|
338 "Smacker Video",
|
|
339 sizeof(SmackerContext),
|
|
340 smacker_probe,
|
|
341 smacker_read_header,
|
|
342 smacker_read_packet,
|
|
343 smacker_read_close,
|
|
344 };
|
|
345
|
|
346 int smacker_init(void)
|
|
347 {
|
|
348 av_register_input_format(&smacker_iformat);
|
|
349 return 0;
|
|
350 }
|