]> git.sesse.net Git - ffmpeg/blob - libavformat/smacker.c
avformat/smacker: Only store what is needed later
[ffmpeg] / libavformat / smacker.c
1 /*
2  * Smacker demuxer
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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.
11  *
12  * FFmpeg 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * 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 #include <inttypes.h>
27
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33
34 #define SMACKER_PAL 0x01
35 #define SMACKER_FLAG_RING_FRAME 0x01
36
37 enum SAudFlags {
38     SMK_AUD_PACKED  = 0x80,
39     SMK_AUD_16BITS  = 0x20,
40     SMK_AUD_STEREO  = 0x10,
41     SMK_AUD_BINKAUD = 0x08,
42     SMK_AUD_USEDCT  = 0x04
43 };
44
45 typedef struct SmackerContext {
46     uint32_t frames;
47     /* frame info */
48     uint32_t *frm_size;
49     uint8_t  *frm_flags;
50     /* internal variables */
51     int cur_frame;
52     /* current frame for demuxing */
53     uint8_t pal[768];
54     int indexes[7];
55     int videoindex;
56     uint8_t *bufs[7];
57     int buf_sizes[7];
58     int stream_id[7];
59     int curstream;
60     int64_t nextpos;
61     int64_t aud_pts[7];
62 } SmackerContext;
63
64 typedef struct SmackerFrame {
65     int64_t pts;
66     int stream;
67 } SmackerFrame;
68
69 /* palette used in Smacker */
70 static const uint8_t smk_pal[64] = {
71     0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
72     0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
73     0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
74     0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
75     0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
76     0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
77     0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
78     0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
79 };
80
81
82 static int smacker_probe(const AVProbeData *p)
83 {
84     if (   AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
85         && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
86         return 0;
87
88     if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
89         return AVPROBE_SCORE_MAX/4;
90
91     return AVPROBE_SCORE_MAX;
92 }
93
94 static int smacker_read_header(AVFormatContext *s)
95 {
96     AVIOContext *pb = s->pb;
97     SmackerContext *smk = s->priv_data;
98     AVStream *st;
99     uint32_t magic, width, height, flags, treesize;
100     int i, ret, pts_inc;
101     int tbase;
102
103     /* read and check header */
104     magic  = avio_rl32(pb);
105     if (magic != MKTAG('S', 'M', 'K', '2') && magic != MKTAG('S', 'M', 'K', '4'))
106         return AVERROR_INVALIDDATA;
107     width  = avio_rl32(pb);
108     height = avio_rl32(pb);
109     smk->frames = avio_rl32(pb);
110     pts_inc = avio_rl32(pb);
111     if (pts_inc > INT_MAX / 100) {
112         av_log(s, AV_LOG_ERROR, "pts_inc %d is too large\n", pts_inc);
113         return AVERROR_INVALIDDATA;
114     }
115
116     flags = avio_rl32(pb);
117     if (flags & SMACKER_FLAG_RING_FRAME)
118         smk->frames++;
119     if (smk->frames > 0xFFFFFF) {
120         av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
121         return AVERROR_INVALIDDATA;
122     }
123
124     avio_skip(pb, 28); /* Unused audio related data */
125
126     treesize = avio_rl32(pb);
127     if (treesize >= UINT_MAX/4) {
128         // treesize + 16 must not overflow (this check is probably redundant)
129         av_log(s, AV_LOG_ERROR, "treesize too large\n");
130         return AVERROR_INVALIDDATA;
131     }
132
133     st = avformat_new_stream(s, NULL);
134     if (!st)
135         return AVERROR(ENOMEM);
136
137     /* Smacker uses 100000 as internal timebase */
138     if (pts_inc < 0)
139         pts_inc = -pts_inc;
140     else
141         pts_inc *= 100;
142     tbase = 100000;
143     av_reduce(&tbase, &pts_inc, tbase, pts_inc, (1UL << 31) - 1);
144     avpriv_set_pts_info(st, 33, pts_inc, tbase);
145
146     /* init video codec */
147     st->codecpar->width     = width;
148     st->codecpar->height    = height;
149     st->codecpar->codec_tag = magic;
150
151     if ((ret = ff_alloc_extradata(st->codecpar, treesize + 16)) < 0) {
152         av_log(s, AV_LOG_ERROR,
153                "Cannot allocate %"PRIu32" bytes of extradata\n",
154                treesize + 16);
155         return ret;
156     }
157     if ((ret = ffio_read_size(pb, st->codecpar->extradata, 16)) < 0)
158         return ret;
159
160     /* handle possible audio streams */
161     for(i = 0; i < 7; i++) {
162         uint32_t rate = avio_rl24(pb);
163         uint8_t aflag = avio_r8(pb);
164
165         smk->indexes[i] = -1;
166
167         if (rate) {
168             AVStream *ast = avformat_new_stream(s, NULL);
169             if (!ast)
170                 return AVERROR(ENOMEM);
171
172             smk->indexes[i] = ast->index;
173             ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
174             if (aflag & SMK_AUD_BINKAUD) {
175                 ast->codecpar->codec_id  = AV_CODEC_ID_BINKAUDIO_RDFT;
176             } else if (aflag & SMK_AUD_USEDCT) {
177                 ast->codecpar->codec_id  = AV_CODEC_ID_BINKAUDIO_DCT;
178             } else if (aflag & SMK_AUD_PACKED) {
179                 ast->codecpar->codec_id  = AV_CODEC_ID_SMACKAUDIO;
180                 ast->codecpar->codec_tag = MKTAG('S', 'M', 'K', 'A');
181             } else {
182                 ast->codecpar->codec_id  = AV_CODEC_ID_PCM_U8;
183             }
184             if (aflag & SMK_AUD_STEREO) {
185                 ast->codecpar->channels       = 2;
186                 ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
187             } else {
188                 ast->codecpar->channels       = 1;
189                 ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
190             }
191             ast->codecpar->sample_rate = rate;
192             ast->codecpar->bits_per_coded_sample = (aflag & SMK_AUD_16BITS) ? 16 : 8;
193             if (ast->codecpar->bits_per_coded_sample == 16 &&
194                 ast->codecpar->codec_id == AV_CODEC_ID_PCM_U8)
195                 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
196             avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate
197                     * ast->codecpar->channels * ast->codecpar->bits_per_coded_sample / 8);
198         }
199     }
200     avio_rl32(pb); /* padding */
201     /* setup data */
202     smk->frm_size = av_malloc_array(smk->frames, sizeof(*smk->frm_size));
203     smk->frm_flags = av_malloc(smk->frames);
204     if (!smk->frm_size || !smk->frm_flags) {
205         av_freep(&smk->frm_size);
206         av_freep(&smk->frm_flags);
207         return AVERROR(ENOMEM);
208     }
209
210     /* read frame info */
211     for(i = 0; i < smk->frames; i++) {
212         smk->frm_size[i] = avio_rl32(pb);
213     }
214     for(i = 0; i < smk->frames; i++) {
215         smk->frm_flags[i] = avio_r8(pb);
216     }
217
218     smk->videoindex = st->index;
219     st->codecpar->format = AV_PIX_FMT_PAL8;
220     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
221     st->codecpar->codec_id = AV_CODEC_ID_SMACKVIDEO;
222     st->duration = smk->frames;
223
224     /* load trees to extradata, they will be unpacked by decoder */
225     ret = avio_read(pb, st->codecpar->extradata + 16, st->codecpar->extradata_size - 16);
226     if(ret != st->codecpar->extradata_size - 16){
227         av_freep(&smk->frm_size);
228         av_freep(&smk->frm_flags);
229         return AVERROR(EIO);
230     }
231
232     smk->curstream = -1;
233     smk->nextpos = avio_tell(pb);
234
235     return 0;
236 }
237
238
239 static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
240 {
241     SmackerContext *smk = s->priv_data;
242     int flags;
243     int ret;
244     int i;
245     int frame_size = 0;
246     int palchange = 0;
247
248     if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
249         return AVERROR_EOF;
250
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;
260             uint8_t oldpal[768];
261
262             memcpy(oldpal, pal, 768);
263             size = avio_r8(s->pb);
264             size = size * 4 - 1;
265             if(size + 1 > frame_size)
266                 return AVERROR_INVALIDDATA;
267             frame_size -= size;
268             frame_size--;
269             sz = 0;
270             pos = avio_tell(s->pb) + size;
271             while(sz < 256){
272                 t = avio_r8(s->pb);
273                 if(t & 0x80){ /* skip palette entries */
274                     sz += (t & 0x7F) + 1;
275                     pal += ((t & 0x7F) + 1) * 3;
276                 } else if(t & 0x40){ /* copy with offset */
277                     off = avio_r8(s->pb);
278                     j = (t & 0x3F) + 1;
279                     if (off + j > 0x100) {
280                         av_log(s, AV_LOG_ERROR,
281                                "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
282                                off, j);
283                         return AVERROR_INVALIDDATA;
284                     }
285                     off *= 3;
286                     while(j-- && sz < 256) {
287                         *pal++ = oldpal[off + 0];
288                         *pal++ = oldpal[off + 1];
289                         *pal++ = oldpal[off + 2];
290                         sz++;
291                         off += 3;
292                     }
293                 } else { /* new entries */
294                     *pal++ = smk_pal[t];
295                     *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
296                     *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
297                     sz++;
298                 }
299             }
300             avio_seek(s->pb, pos, 0);
301             palchange |= 1;
302         }
303         flags >>= 1;
304         smk->curstream = -1;
305         /* if audio chunks are present, put them to stack and retrieve later */
306         for(i = 0; i < 7; i++) {
307             if(flags & 1) {
308                 uint32_t size;
309                 int err;
310
311                 size = avio_rl32(s->pb) - 4;
312                 if (!size || size + 4LL > frame_size) {
313                     av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
314                     return AVERROR_INVALIDDATA;
315                 }
316                 frame_size -= size;
317                 frame_size -= 4;
318                 smk->curstream++;
319                 if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) {
320                     smk->buf_sizes[smk->curstream] = 0;
321                     return err;
322                 }
323                 smk->buf_sizes[smk->curstream] = size;
324                 ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
325                 if(ret != size)
326                     return AVERROR(EIO);
327                 smk->stream_id[smk->curstream] = smk->indexes[i];
328             }
329             flags >>= 1;
330         }
331         if (frame_size < 0 || frame_size >= INT_MAX/2)
332             return AVERROR_INVALIDDATA;
333         if ((ret = av_new_packet(pkt, frame_size + 769)) < 0)
334             return ret;
335         if(smk->frm_size[smk->cur_frame] & 1)
336             palchange |= 2;
337         pkt->data[0] = palchange;
338         memcpy(pkt->data + 1, smk->pal, 768);
339         ret = avio_read(s->pb, pkt->data + 769, frame_size);
340         if(ret != frame_size)
341             return AVERROR(EIO);
342         pkt->stream_index = smk->videoindex;
343         pkt->pts          = smk->cur_frame;
344         pkt->size = ret + 769;
345         smk->cur_frame++;
346         smk->nextpos = avio_tell(s->pb);
347     } else {
348         if (smk->stream_id[smk->curstream] < 0 || !smk->bufs[smk->curstream])
349             return AVERROR_INVALIDDATA;
350         if ((ret = av_new_packet(pkt, smk->buf_sizes[smk->curstream])) < 0)
351             return ret;
352         memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
353         pkt->size = smk->buf_sizes[smk->curstream];
354         pkt->stream_index = smk->stream_id[smk->curstream];
355         pkt->pts = smk->aud_pts[smk->curstream];
356         smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);
357         smk->curstream--;
358     }
359
360     return 0;
361 }
362
363 static int smacker_read_close(AVFormatContext *s)
364 {
365     SmackerContext *smk = s->priv_data;
366     int i;
367
368     for(i = 0; i < 7; i++)
369         av_freep(&smk->bufs[i]);
370     av_freep(&smk->frm_size);
371     av_freep(&smk->frm_flags);
372
373     return 0;
374 }
375
376 AVInputFormat ff_smacker_demuxer = {
377     .name           = "smk",
378     .long_name      = NULL_IF_CONFIG_SMALL("Smacker"),
379     .priv_data_size = sizeof(SmackerContext),
380     .read_probe     = smacker_probe,
381     .read_header    = smacker_read_header,
382     .read_packet    = smacker_read_packet,
383     .read_close     = smacker_read_close,
384 };