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