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