VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/libavformat/smacker.c@ 8998

最後變更 在這個檔案從8998是 5776,由 vboxsync 提交於 17 年 前

ffmpeg: exported to OSE

檔案大小: 11.1 KB
 
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
30enum 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
38typedef 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 int64_t aud_pts[7];
67} SmackerContext;
68
69typedef struct SmackerFrame {
70 int64_t pts;
71 int stream;
72} SmackerFrame;
73
74/* palette used in Smacker */
75static const uint8_t smk_pal[64] = {
76 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
77 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
78 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
79 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
80 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
81 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
82 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
83 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
84};
85
86
87static int smacker_probe(AVProbeData *p)
88{
89 if (p->buf_size < 4)
90 return 0;
91 if(p->buf[0] == 'S' && p->buf[1] == 'M' && p->buf[2] == 'K'
92 && (p->buf[3] == '2' || p->buf[3] == '4'))
93 return AVPROBE_SCORE_MAX;
94 else
95 return 0;
96}
97
98static int smacker_read_header(AVFormatContext *s, AVFormatParameters *ap)
99{
100 ByteIOContext *pb = &s->pb;
101 SmackerContext *smk = (SmackerContext *)s->priv_data;
102 AVStream *st, *ast[7];
103 int i, ret;
104 int tbase;
105
106 /* read and check header */
107 smk->magic = get_le32(pb);
108 if (smk->magic != MKTAG('S', 'M', 'K', '2') && smk->magic != MKTAG('S', 'M', 'K', '4'))
109 return -1;
110 smk->width = get_le32(pb);
111 smk->height = get_le32(pb);
112 smk->frames = get_le32(pb);
113 smk->pts_inc = (int32_t)get_le32(pb);
114 smk->flags = get_le32(pb);
115 for(i = 0; i < 7; i++)
116 smk->audio[i] = get_le32(pb);
117 smk->treesize = get_le32(pb);
118
119 if(smk->treesize >= UINT_MAX/4){ // smk->treesize + 16 must not overflow (this check is probably redundant)
120 av_log(s, AV_LOG_ERROR, "treesize too large\n");
121 return -1;
122 }
123
124//FIXME remove extradata "rebuilding"
125 smk->mmap_size = get_le32(pb);
126 smk->mclr_size = get_le32(pb);
127 smk->full_size = get_le32(pb);
128 smk->type_size = get_le32(pb);
129 for(i = 0; i < 7; i++)
130 smk->rates[i] = get_le32(pb);
131 smk->pad = get_le32(pb);
132 /* setup data */
133 if(smk->frames > 0xFFFFFF) {
134 av_log(s, AV_LOG_ERROR, "Too many frames: %i\n", smk->frames);
135 return -1;
136 }
137 smk->frm_size = av_malloc(smk->frames * 4);
138 smk->frm_flags = av_malloc(smk->frames);
139
140 smk->is_ver4 = (smk->magic != MKTAG('S', 'M', 'K', '2'));
141
142 /* read frame info */
143 for(i = 0; i < smk->frames; i++) {
144 smk->frm_size[i] = get_le32(pb);
145 }
146 for(i = 0; i < smk->frames; i++) {
147 smk->frm_flags[i] = get_byte(pb);
148 }
149
150 /* init video codec */
151 st = av_new_stream(s, 0);
152 if (!st)
153 return -1;
154 smk->videoindex = st->index;
155 st->codec->width = smk->width;
156 st->codec->height = smk->height;
157 st->codec->pix_fmt = PIX_FMT_PAL8;
158 st->codec->codec_type = CODEC_TYPE_VIDEO;
159 st->codec->codec_id = CODEC_ID_SMACKVIDEO;
160 st->codec->codec_tag = smk->magic;
161 /* Smacker uses 100000 as internal timebase */
162 if(smk->pts_inc < 0)
163 smk->pts_inc = -smk->pts_inc;
164 else
165 smk->pts_inc *= 100;
166 tbase = 100000;
167 av_reduce(&tbase, &smk->pts_inc, tbase, smk->pts_inc, (1UL<<31)-1);
168 av_set_pts_info(st, 33, smk->pts_inc, tbase);
169 /* handle possible audio streams */
170 for(i = 0; i < 7; i++) {
171 smk->indexes[i] = -1;
172 if((smk->rates[i] & 0xFFFFFF) && !(smk->rates[i] & SMK_AUD_BINKAUD)){
173 ast[i] = av_new_stream(s, 0);
174 smk->indexes[i] = ast[i]->index;
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 = MKTAG('S', 'M', 'K', 'A');
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 av_set_pts_info(ast[i], 64, 1, ast[i]->codec->sample_rate
184 * ast[i]->codec->channels * ast[i]->codec->bits_per_sample / 8);
185 }
186 }
187
188
189 /* load trees to extradata, they will be unpacked by decoder */
190 st->codec->extradata = av_malloc(smk->treesize + 16);
191 st->codec->extradata_size = smk->treesize + 16;
192 if(!st->codec->extradata){
193 av_log(s, AV_LOG_ERROR, "Cannot allocate %i bytes of extradata\n", smk->treesize + 16);
194 av_free(smk->frm_size);
195 av_free(smk->frm_flags);
196 return -1;
197 }
198 ret = get_buffer(pb, st->codec->extradata + 16, st->codec->extradata_size - 16);
199 if(ret != st->codec->extradata_size - 16){
200 av_free(smk->frm_size);
201 av_free(smk->frm_flags);
202 return AVERROR_IO;
203 }
204 ((int32_t*)st->codec->extradata)[0] = le2me_32(smk->mmap_size);
205 ((int32_t*)st->codec->extradata)[1] = le2me_32(smk->mclr_size);
206 ((int32_t*)st->codec->extradata)[2] = le2me_32(smk->full_size);
207 ((int32_t*)st->codec->extradata)[3] = le2me_32(smk->type_size);
208
209 smk->curstream = -1;
210 smk->nextpos = url_ftell(pb);
211
212 return 0;
213}
214
215
216static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
217{
218 SmackerContext *smk = (SmackerContext *)s->priv_data;
219 int flags;
220 int ret;
221 int i;
222 int frame_size = 0;
223 int palchange = 0;
224 int pos;
225
226 if (url_feof(&s->pb) || smk->cur_frame >= smk->frames)
227 return -EIO;
228
229 /* if we demuxed all streams, pass another frame */
230 if(smk->curstream < 0) {
231 url_fseek(&s->pb, smk->nextpos, 0);
232 frame_size = smk->frm_size[smk->cur_frame] & (~3);
233 flags = smk->frm_flags[smk->cur_frame];
234 /* handle palette change event */
235 pos = url_ftell(&s->pb);
236 if(flags & SMACKER_PAL){
237 int size, sz, t, off, j, pos;
238 uint8_t *pal = smk->pal;
239 uint8_t oldpal[768];
240
241 memcpy(oldpal, pal, 768);
242 size = get_byte(&s->pb);
243 size = size * 4 - 1;
244 frame_size -= size;
245 frame_size--;
246 sz = 0;
247 pos = url_ftell(&s->pb) + size;
248 while(sz < 256){
249 t = get_byte(&s->pb);
250 if(t & 0x80){ /* skip palette entries */
251 sz += (t & 0x7F) + 1;
252 pal += ((t & 0x7F) + 1) * 3;
253 } else if(t & 0x40){ /* copy with offset */
254 off = get_byte(&s->pb) * 3;
255 j = (t & 0x3F) + 1;
256 while(j-- && sz < 256) {
257 *pal++ = oldpal[off + 0];
258 *pal++ = oldpal[off + 1];
259 *pal++ = oldpal[off + 2];
260 sz++;
261 off += 3;
262 }
263 } else { /* new entries */
264 *pal++ = smk_pal[t];
265 *pal++ = smk_pal[get_byte(&s->pb) & 0x3F];
266 *pal++ = smk_pal[get_byte(&s->pb) & 0x3F];
267 sz++;
268 }
269 }
270 url_fseek(&s->pb, pos, 0);
271 palchange |= 1;
272 }
273 flags >>= 1;
274 smk->curstream = -1;
275 /* if audio chunks are present, put them to stack and retrieve later */
276 for(i = 0; i < 7; i++) {
277 if(flags & 1) {
278 int size;
279 size = get_le32(&s->pb) - 4;
280 frame_size -= size;
281 frame_size -= 4;
282 smk->curstream++;
283 smk->bufs[smk->curstream] = av_realloc(smk->bufs[smk->curstream], size);
284 smk->buf_sizes[smk->curstream] = size;
285 ret = get_buffer(&s->pb, smk->bufs[smk->curstream], size);
286 if(ret != size)
287 return AVERROR_IO;
288 smk->stream_id[smk->curstream] = smk->indexes[i];
289 }
290 flags >>= 1;
291 }
292 if (av_new_packet(pkt, frame_size + 768))
293 return AVERROR_NOMEM;
294 if(smk->frm_size[smk->cur_frame] & 1)
295 palchange |= 2;
296 pkt->data[0] = palchange;
297 memcpy(pkt->data + 1, smk->pal, 768);
298 ret = get_buffer(&s->pb, pkt->data + 769, frame_size);
299 if(ret != frame_size)
300 return AVERROR_IO;
301 pkt->stream_index = smk->videoindex;
302 pkt->size = ret + 769;
303 smk->cur_frame++;
304 smk->nextpos = url_ftell(&s->pb);
305 } else {
306 if (av_new_packet(pkt, smk->buf_sizes[smk->curstream]))
307 return AVERROR_NOMEM;
308 memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
309 pkt->size = smk->buf_sizes[smk->curstream];
310 pkt->stream_index = smk->stream_id[smk->curstream];
311 pkt->pts = smk->aud_pts[smk->curstream];
312 smk->aud_pts[smk->curstream] += LE_32(pkt->data);
313 smk->curstream--;
314 }
315
316 return 0;
317}
318
319static int smacker_read_close(AVFormatContext *s)
320{
321 SmackerContext *smk = (SmackerContext *)s->priv_data;
322 int i;
323
324 for(i = 0; i < 7; i++)
325 if(smk->bufs[i])
326 av_free(smk->bufs[i]);
327 if(smk->frm_size)
328 av_free(smk->frm_size);
329 if(smk->frm_flags)
330 av_free(smk->frm_flags);
331
332 return 0;
333}
334
335static AVInputFormat smacker_demuxer = {
336 "smk",
337 "Smacker Video",
338 sizeof(SmackerContext),
339 smacker_probe,
340 smacker_read_header,
341 smacker_read_packet,
342 smacker_read_close,
343};
344
345int smacker_init(void)
346{
347 av_register_input_format(&smacker_demuxer);
348 return 0;
349}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette