Statistics
| Branch: | Revision:

ffmpeg / libavformat / iff.c @ 1af99b02

History | View | Annotate | Download (12.2 KB)

1
/*
2
 * IFF (.iff) file demuxer
3
 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4
 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5
 * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23

    
24
/**
25
 * @file
26
 * IFF file demuxer
27
 * by Jaikrishnan Menon
28
 * for more information on the .iff file format, visit:
29
 * http://wiki.multimedia.cx/index.php?title=IFF
30
 */
31

    
32
#include "libavcodec/bytestream.h"
33
#include "libavutil/intreadwrite.h"
34
#include "avformat.h"
35

    
36
#define ID_8SVX       MKTAG('8','S','V','X')
37
#define ID_VHDR       MKTAG('V','H','D','R')
38
#define ID_ATAK       MKTAG('A','T','A','K')
39
#define ID_RLSE       MKTAG('R','L','S','E')
40
#define ID_CHAN       MKTAG('C','H','A','N')
41
#define ID_PBM        MKTAG('P','B','M',' ')
42
#define ID_ILBM       MKTAG('I','L','B','M')
43
#define ID_BMHD       MKTAG('B','M','H','D')
44
#define ID_CAMG       MKTAG('C','A','M','G')
45
#define ID_CMAP       MKTAG('C','M','A','P')
46

    
47
#define ID_FORM       MKTAG('F','O','R','M')
48
#define ID_ANNO       MKTAG('A','N','N','O')
49
#define ID_AUTH       MKTAG('A','U','T','H')
50
#define ID_CHRS       MKTAG('C','H','R','S')
51
#define ID_COPYRIGHT  MKTAG('(','c',')',' ')
52
#define ID_CSET       MKTAG('C','S','E','T')
53
#define ID_FVER       MKTAG('F','V','E','R')
54
#define ID_NAME       MKTAG('N','A','M','E')
55
#define ID_TEXT       MKTAG('T','E','X','T')
56
#define ID_BODY       MKTAG('B','O','D','Y')
57
#define ID_ANNO       MKTAG('A','N','N','O')
58

    
59
#define LEFT    2
60
#define RIGHT   4
61
#define STEREO  6
62

    
63
#define PACKET_SIZE 1024
64

    
65
/**
66
 * This number of bytes if added at the beginning of each AVPacket
67
 * which contain additional information about video properties
68
 * which has to be shared between demuxer and decoder.
69
 * This number may change between frames, e.g. the demuxer might
70
 * set it to smallest possible size of 2 to indicate that there's
71
 * no extradata changing in this frame.
72
 */
73
#define IFF_EXTRA_VIDEO_SIZE 9
74

    
75
typedef enum {
76
    COMP_NONE,
77
    COMP_FIB,
78
    COMP_EXP
79
} svx8_compression_type;
80

    
81
typedef enum {
82
    BITMAP_RAW,
83
    BITMAP_BYTERUN1
84
} bitmap_compression_type;
85

    
86
typedef struct {
87
    uint64_t  body_pos;
88
    uint32_t  body_size;
89
    uint32_t  sent_bytes;
90
    uint32_t  audio_frame_count;
91
    svx8_compression_type   svx8_compression;
92
    bitmap_compression_type bitmap_compression;  ///< delta compression method used
93
    unsigned  bpp;          ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
94
    unsigned  ham;          ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
95
    unsigned  flags;        ///< 1 for EHB, 0 is no extra half darkening
96
    unsigned  transparency; ///< transparency color index in palette
97
    unsigned  masking;      ///< masking method used
98
} IffDemuxContext;
99

    
100

    
101
static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
102
{
103
    uint8_t *end = dest + size;
104
    size = size>>1;
105

    
106
    while(dest < end) {
107
        *dest++ = *src;
108
        *dest++ = *(src+size);
109
        src++;
110
    }
111
}
112

    
113
/* Metadata string read */
114
static int get_metadata(AVFormatContext *s,
115
                        const char *const tag,
116
                        const unsigned data_size)
117
{
118
    uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
119

    
120
    if (!buf)
121
        return AVERROR(ENOMEM);
122

    
123
    if (avio_read(s->pb, buf, data_size) < 0) {
124
        av_free(buf);
125
        return AVERROR(EIO);
126
    }
127
    buf[data_size] = 0;
128
    av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
129
    return 0;
130
}
131

    
132
static int iff_probe(AVProbeData *p)
133
{
134
    const uint8_t *d = p->buf;
135

    
136
    if ( AV_RL32(d)   == ID_FORM &&
137
         (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
138
        return AVPROBE_SCORE_MAX;
139
    return 0;
140
}
141

    
142
static int iff_read_header(AVFormatContext *s,
143
                           AVFormatParameters *ap)
144
{
145
    IffDemuxContext *iff = s->priv_data;
146
    AVIOContext *pb = s->pb;
147
    AVStream *st;
148
    uint8_t *buf;
149
    uint32_t chunk_id, data_size;
150
    uint32_t screenmode = 0;
151
    unsigned transparency = 0;
152
    unsigned masking = 0; // no mask
153

    
154
    st = av_new_stream(s, 0);
155
    if (!st)
156
        return AVERROR(ENOMEM);
157

    
158
    st->codec->channels = 1;
159
    avio_skip(pb, 8);
160
    // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
161
    st->codec->codec_tag = avio_rl32(pb);
162

    
163
    while(!url_feof(pb)) {
164
        uint64_t orig_pos;
165
        int res;
166
        const char *metadata_tag = NULL;
167
        chunk_id = avio_rl32(pb);
168
        data_size = avio_rb32(pb);
169
        orig_pos = avio_tell(pb);
170

    
171
        switch(chunk_id) {
172
        case ID_VHDR:
173
            st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
174

    
175
            if (data_size < 14)
176
                return AVERROR_INVALIDDATA;
177
            avio_skip(pb, 12);
178
            st->codec->sample_rate = avio_rb16(pb);
179
            if (data_size >= 16) {
180
                avio_skip(pb, 1);
181
                iff->svx8_compression = avio_r8(pb);
182
            }
183
            break;
184

    
185
        case ID_BODY:
186
            iff->body_pos = avio_tell(pb);
187
            iff->body_size = data_size;
188
            break;
189

    
190
        case ID_CHAN:
191
            if (data_size < 4)
192
                return AVERROR_INVALIDDATA;
193
            st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
194
            break;
195

    
196
        case ID_CAMG:
197
            if (data_size < 4)
198
                return AVERROR_INVALIDDATA;
199
            screenmode                = avio_rb32(pb);
200
            break;
201

    
202
        case ID_CMAP:
203
            st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
204
            st->codec->extradata      = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
205
            if (!st->codec->extradata)
206
                return AVERROR(ENOMEM);
207
            if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
208
                return AVERROR(EIO);
209
            break;
210

    
211
        case ID_BMHD:
212
            iff->bitmap_compression = -1;
213
            st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
214
            if (data_size <= 8)
215
                return AVERROR_INVALIDDATA;
216
            st->codec->width                 = avio_rb16(pb);
217
            st->codec->height                = avio_rb16(pb);
218
            avio_skip(pb, 4); // x, y offset
219
            st->codec->bits_per_coded_sample = avio_r8(pb);
220
            if (data_size >= 10)
221
                masking                      = avio_r8(pb);
222
            if (data_size >= 11)
223
                iff->bitmap_compression      = avio_r8(pb);
224
            if (data_size >= 14) {
225
                avio_skip(pb, 1); // padding
226
                transparency                 = avio_rb16(pb);
227
            }
228
            if (data_size >= 16) {
229
                st->sample_aspect_ratio.num  = avio_r8(pb);
230
                st->sample_aspect_ratio.den  = avio_r8(pb);
231
            }
232
            break;
233

    
234
        case ID_ANNO:
235
        case ID_TEXT:
236
            metadata_tag = "comment";
237
            break;
238

    
239
        case ID_AUTH:
240
            metadata_tag = "artist";
241
            break;
242

    
243
        case ID_COPYRIGHT:
244
            metadata_tag = "copyright";
245
            break;
246

    
247
        case ID_NAME:
248
            metadata_tag = "title";
249
            break;
250
        }
251

    
252
        if (metadata_tag) {
253
            if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
254
                av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
255
                return res;
256
            }
257
        }
258
        avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
259
    }
260

    
261
    avio_seek(pb, iff->body_pos, SEEK_SET);
262

    
263
    switch(st->codec->codec_type) {
264
    case AVMEDIA_TYPE_AUDIO:
265
        av_set_pts_info(st, 32, 1, st->codec->sample_rate);
266

    
267
        switch (iff->svx8_compression) {
268
        case COMP_NONE:
269
            st->codec->codec_id = CODEC_ID_PCM_S8;
270
            break;
271
        case COMP_FIB:
272
            st->codec->codec_id = CODEC_ID_8SVX_FIB;
273
            break;
274
        case COMP_EXP:
275
            st->codec->codec_id = CODEC_ID_8SVX_EXP;
276
            break;
277
        default:
278
            av_log(s, AV_LOG_ERROR,
279
                   "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
280
            return -1;
281
        }
282

    
283
        st->codec->bits_per_coded_sample = 8;
284
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
285
        st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
286
        break;
287

    
288
    case AVMEDIA_TYPE_VIDEO:
289
        iff->bpp          = st->codec->bits_per_coded_sample;
290
        if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
291
            iff->ham      = iff->bpp > 6 ? 6 : 4;
292
            st->codec->bits_per_coded_sample = 24;
293
        }
294
        iff->flags        = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
295
        iff->masking      = masking;
296
        iff->transparency = transparency;
297

    
298
        if (!st->codec->extradata) {
299
            st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
300
            st->codec->extradata      = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
301
            if (!st->codec->extradata)
302
                return AVERROR(ENOMEM);
303
        }
304
        buf = st->codec->extradata;
305
        bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
306
        bytestream_put_byte(&buf, iff->bitmap_compression);
307
        bytestream_put_byte(&buf, iff->bpp);
308
        bytestream_put_byte(&buf, iff->ham);
309
        bytestream_put_byte(&buf, iff->flags);
310
        bytestream_put_be16(&buf, iff->transparency);
311
        bytestream_put_byte(&buf, iff->masking);
312

    
313
        switch (iff->bitmap_compression) {
314
        case BITMAP_RAW:
315
            st->codec->codec_id = CODEC_ID_IFF_ILBM;
316
            break;
317
        case BITMAP_BYTERUN1:
318
            st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
319
            break;
320
        default:
321
            av_log(s, AV_LOG_ERROR,
322
                   "Unknown bitmap compression method '%d'\n", iff->bitmap_compression);
323
            return AVERROR_INVALIDDATA;
324
        }
325
        break;
326
    default:
327
        return -1;
328
    }
329

    
330
    return 0;
331
}
332

    
333
static int iff_read_packet(AVFormatContext *s,
334
                           AVPacket *pkt)
335
{
336
    IffDemuxContext *iff = s->priv_data;
337
    AVIOContext *pb = s->pb;
338
    AVStream *st = s->streams[0];
339
    int ret;
340

    
341
    if(iff->sent_bytes >= iff->body_size)
342
        return AVERROR(EIO);
343

    
344
    if(st->codec->channels == 2) {
345
        uint8_t sample_buffer[PACKET_SIZE];
346

    
347
        ret = avio_read(pb, sample_buffer, PACKET_SIZE);
348
        if(av_new_packet(pkt, PACKET_SIZE) < 0) {
349
            av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
350
            return AVERROR(ENOMEM);
351
        }
352
        interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
353
    } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
354
        uint8_t *buf;
355

    
356
        if (av_new_packet(pkt, iff->body_size + 2) < 0) {
357
            return AVERROR(ENOMEM);
358
        }
359

    
360
        buf = pkt->data;
361
        bytestream_put_be16(&buf, 2);
362
        ret = avio_read(pb, buf, iff->body_size);
363
    } else {
364
        ret = av_get_packet(pb, pkt, PACKET_SIZE);
365
    }
366

    
367
    if(iff->sent_bytes == 0)
368
        pkt->flags |= AV_PKT_FLAG_KEY;
369

    
370
    if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
371
        iff->sent_bytes += PACKET_SIZE;
372
    } else {
373
        iff->sent_bytes = iff->body_size;
374
    }
375
    pkt->stream_index = 0;
376
    if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
377
        pkt->pts = iff->audio_frame_count;
378
        iff->audio_frame_count += ret / st->codec->channels;
379
    }
380
    return ret;
381
}
382

    
383
AVInputFormat ff_iff_demuxer = {
384
    "IFF",
385
    NULL_IF_CONFIG_SMALL("IFF format"),
386
    sizeof(IffDemuxContext),
387
    iff_probe,
388
    iff_read_header,
389
    iff_read_packet,
390
};