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