]> git.sesse.net Git - ffmpeg/blob - libavformat/smacker.c
avformat/smacker: Remove unused structure
[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 /* 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     uint32_t magic, width, height, flags, treesize;
95     int i, ret, pts_inc;
96     int tbase;
97
98     /* read and check header */
99     magic  = avio_rl32(pb);
100     if (magic != MKTAG('S', 'M', 'K', '2') && magic != MKTAG('S', 'M', 'K', '4'))
101         return AVERROR_INVALIDDATA;
102     width  = avio_rl32(pb);
103     height = avio_rl32(pb);
104     smk->frames = avio_rl32(pb);
105     pts_inc = avio_rl32(pb);
106     if (pts_inc > INT_MAX / 100) {
107         av_log(s, AV_LOG_ERROR, "pts_inc %d is too large\n", pts_inc);
108         return AVERROR_INVALIDDATA;
109     }
110
111     flags = avio_rl32(pb);
112     if (flags & SMACKER_FLAG_RING_FRAME)
113         smk->frames++;
114     if (smk->frames > 0xFFFFFF) {
115         av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
116         return AVERROR_INVALIDDATA;
117     }
118
119     avio_skip(pb, 28); /* Unused audio related data */
120
121     treesize = avio_rl32(pb);
122     if (treesize >= UINT_MAX/4) {
123         // treesize + 16 must not overflow (this check is probably redundant)
124         av_log(s, AV_LOG_ERROR, "treesize too large\n");
125         return AVERROR_INVALIDDATA;
126     }
127
128     st = avformat_new_stream(s, NULL);
129     if (!st)
130         return AVERROR(ENOMEM);
131
132     /* Smacker uses 100000 as internal timebase */
133     if (pts_inc < 0)
134         pts_inc = -pts_inc;
135     else
136         pts_inc *= 100;
137     tbase = 100000;
138     av_reduce(&tbase, &pts_inc, tbase, pts_inc, (1UL << 31) - 1);
139     avpriv_set_pts_info(st, 33, pts_inc, tbase);
140
141     /* init video codec */
142     st->codecpar->width     = width;
143     st->codecpar->height    = height;
144     st->codecpar->codec_tag = magic;
145
146     if ((ret = ff_alloc_extradata(st->codecpar, treesize + 16)) < 0) {
147         av_log(s, AV_LOG_ERROR,
148                "Cannot allocate %"PRIu32" bytes of extradata\n",
149                treesize + 16);
150         return ret;
151     }
152     if ((ret = ffio_read_size(pb, st->codecpar->extradata, 16)) < 0)
153         return ret;
154
155     /* handle possible audio streams */
156     for(i = 0; i < 7; i++) {
157         uint32_t rate = avio_rl24(pb);
158         uint8_t aflag = avio_r8(pb);
159
160         smk->indexes[i] = -1;
161
162         if (rate) {
163             AVStream *ast = avformat_new_stream(s, NULL);
164             if (!ast)
165                 return AVERROR(ENOMEM);
166
167             smk->indexes[i] = ast->index;
168             ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
169             if (aflag & SMK_AUD_BINKAUD) {
170                 ast->codecpar->codec_id  = AV_CODEC_ID_BINKAUDIO_RDFT;
171             } else if (aflag & SMK_AUD_USEDCT) {
172                 ast->codecpar->codec_id  = AV_CODEC_ID_BINKAUDIO_DCT;
173             } else if (aflag & SMK_AUD_PACKED) {
174                 ast->codecpar->codec_id  = AV_CODEC_ID_SMACKAUDIO;
175                 ast->codecpar->codec_tag = MKTAG('S', 'M', 'K', 'A');
176             } else {
177                 ast->codecpar->codec_id  = AV_CODEC_ID_PCM_U8;
178             }
179             if (aflag & SMK_AUD_STEREO) {
180                 ast->codecpar->channels       = 2;
181                 ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
182             } else {
183                 ast->codecpar->channels       = 1;
184                 ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
185             }
186             ast->codecpar->sample_rate = rate;
187             ast->codecpar->bits_per_coded_sample = (aflag & SMK_AUD_16BITS) ? 16 : 8;
188             if (ast->codecpar->bits_per_coded_sample == 16 &&
189                 ast->codecpar->codec_id == AV_CODEC_ID_PCM_U8)
190                 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
191             avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate
192                     * ast->codecpar->channels * ast->codecpar->bits_per_coded_sample / 8);
193         }
194     }
195     avio_rl32(pb); /* padding */
196     /* setup data */
197     smk->frm_size = av_malloc_array(smk->frames, sizeof(*smk->frm_size));
198     smk->frm_flags = av_malloc(smk->frames);
199     if (!smk->frm_size || !smk->frm_flags) {
200         av_freep(&smk->frm_size);
201         av_freep(&smk->frm_flags);
202         return AVERROR(ENOMEM);
203     }
204
205     /* read frame info */
206     for(i = 0; i < smk->frames; i++) {
207         smk->frm_size[i] = avio_rl32(pb);
208     }
209     for(i = 0; i < smk->frames; i++) {
210         smk->frm_flags[i] = avio_r8(pb);
211     }
212
213     smk->videoindex = st->index;
214     st->codecpar->format = AV_PIX_FMT_PAL8;
215     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
216     st->codecpar->codec_id = AV_CODEC_ID_SMACKVIDEO;
217     st->duration = smk->frames;
218
219     /* load trees to extradata, they will be unpacked by decoder */
220     ret = avio_read(pb, st->codecpar->extradata + 16, st->codecpar->extradata_size - 16);
221     if(ret != st->codecpar->extradata_size - 16){
222         av_freep(&smk->frm_size);
223         av_freep(&smk->frm_flags);
224         return AVERROR(EIO);
225     }
226
227     smk->curstream = -1;
228     smk->nextpos = avio_tell(pb);
229
230     return 0;
231 }
232
233
234 static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
235 {
236     SmackerContext *smk = s->priv_data;
237     int flags;
238     int ret;
239     int i;
240     int frame_size = 0;
241     int palchange = 0;
242
243     if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
244         return AVERROR_EOF;
245
246     /* if we demuxed all streams, pass another frame */
247     if(smk->curstream < 0) {
248         avio_seek(s->pb, smk->nextpos, 0);
249         frame_size = smk->frm_size[smk->cur_frame] & (~3);
250         flags = smk->frm_flags[smk->cur_frame];
251         /* handle palette change event */
252         if(flags & SMACKER_PAL){
253             int size, sz, t, off, j, pos;
254             uint8_t *pal = smk->pal;
255             uint8_t oldpal[768];
256
257             memcpy(oldpal, pal, 768);
258             size = avio_r8(s->pb);
259             size = size * 4 - 1;
260             if(size + 1 > frame_size)
261                 return AVERROR_INVALIDDATA;
262             frame_size -= size;
263             frame_size--;
264             sz = 0;
265             pos = avio_tell(s->pb) + size;
266             while(sz < 256){
267                 t = avio_r8(s->pb);
268                 if(t & 0x80){ /* skip palette entries */
269                     sz += (t & 0x7F) + 1;
270                     pal += ((t & 0x7F) + 1) * 3;
271                 } else if(t & 0x40){ /* copy with offset */
272                     off = avio_r8(s->pb);
273                     j = (t & 0x3F) + 1;
274                     if (off + j > 0x100) {
275                         av_log(s, AV_LOG_ERROR,
276                                "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
277                                off, j);
278                         return AVERROR_INVALIDDATA;
279                     }
280                     off *= 3;
281                     while(j-- && sz < 256) {
282                         *pal++ = oldpal[off + 0];
283                         *pal++ = oldpal[off + 1];
284                         *pal++ = oldpal[off + 2];
285                         sz++;
286                         off += 3;
287                     }
288                 } else { /* new entries */
289                     *pal++ = smk_pal[t];
290                     *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
291                     *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
292                     sz++;
293                 }
294             }
295             avio_seek(s->pb, pos, 0);
296             palchange |= 1;
297         }
298         flags >>= 1;
299         smk->curstream = -1;
300         /* if audio chunks are present, put them to stack and retrieve later */
301         for(i = 0; i < 7; i++) {
302             if(flags & 1) {
303                 uint32_t size;
304                 int err;
305
306                 size = avio_rl32(s->pb) - 4;
307                 if (!size || size + 4LL > frame_size) {
308                     av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
309                     return AVERROR_INVALIDDATA;
310                 }
311                 frame_size -= size;
312                 frame_size -= 4;
313                 smk->curstream++;
314                 if ((err = av_reallocp(&smk->bufs[smk->curstream], size)) < 0) {
315                     smk->buf_sizes[smk->curstream] = 0;
316                     return err;
317                 }
318                 smk->buf_sizes[smk->curstream] = size;
319                 ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
320                 if(ret != size)
321                     return AVERROR(EIO);
322                 smk->stream_id[smk->curstream] = smk->indexes[i];
323             }
324             flags >>= 1;
325         }
326         if (frame_size < 0 || frame_size >= INT_MAX/2)
327             return AVERROR_INVALIDDATA;
328         if ((ret = av_new_packet(pkt, frame_size + 769)) < 0)
329             return ret;
330         if(smk->frm_size[smk->cur_frame] & 1)
331             palchange |= 2;
332         pkt->data[0] = palchange;
333         memcpy(pkt->data + 1, smk->pal, 768);
334         ret = avio_read(s->pb, pkt->data + 769, frame_size);
335         if(ret != frame_size)
336             return AVERROR(EIO);
337         pkt->stream_index = smk->videoindex;
338         pkt->pts          = smk->cur_frame;
339         pkt->size = ret + 769;
340         smk->cur_frame++;
341         smk->nextpos = avio_tell(s->pb);
342     } else {
343         if (smk->stream_id[smk->curstream] < 0 || !smk->bufs[smk->curstream])
344             return AVERROR_INVALIDDATA;
345         if ((ret = av_new_packet(pkt, smk->buf_sizes[smk->curstream])) < 0)
346             return ret;
347         memcpy(pkt->data, smk->bufs[smk->curstream], smk->buf_sizes[smk->curstream]);
348         pkt->size = smk->buf_sizes[smk->curstream];
349         pkt->stream_index = smk->stream_id[smk->curstream];
350         pkt->pts = smk->aud_pts[smk->curstream];
351         smk->aud_pts[smk->curstream] += AV_RL32(pkt->data);
352         smk->curstream--;
353     }
354
355     return 0;
356 }
357
358 static int smacker_read_close(AVFormatContext *s)
359 {
360     SmackerContext *smk = s->priv_data;
361     int i;
362
363     for(i = 0; i < 7; i++)
364         av_freep(&smk->bufs[i]);
365     av_freep(&smk->frm_size);
366     av_freep(&smk->frm_flags);
367
368     return 0;
369 }
370
371 AVInputFormat ff_smacker_demuxer = {
372     .name           = "smk",
373     .long_name      = NULL_IF_CONFIG_SMALL("Smacker"),
374     .priv_data_size = sizeof(SmackerContext),
375     .read_probe     = smacker_probe,
376     .read_header    = smacker_read_header,
377     .read_packet    = smacker_read_packet,
378     .read_close     = smacker_read_close,
379 };