Statistics
| Branch: | Revision:

ffmpeg / libavformat / iff.c @ 451b353d

History | View | Annotate | Download (8.43 KB)

1 3a027f5e Jai Menon
/*
2 4477dedc Diego Biurrun
 * IFF (.iff) file demuxer
3 3a027f5e Jai Menon
 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4 02c4f626 Peter Ross
 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5 3a027f5e Jai Menon
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24 bad5537e Diego Biurrun
 * @file libavformat/iff.c
25 4477dedc Diego Biurrun
 * IFF file demuxer
26 3a027f5e Jai Menon
 * by Jaikrishnan Menon
27
 * for more information on the .iff file format, visit:
28
 * http://wiki.multimedia.cx/index.php?title=IFF
29
 */
30
31 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
32 3a027f5e Jai Menon
#include "avformat.h"
33
34
#define ID_8SVX       MKTAG('8','S','V','X')
35
#define ID_VHDR       MKTAG('V','H','D','R')
36
#define ID_ATAK       MKTAG('A','T','A','K')
37
#define ID_RLSE       MKTAG('R','L','S','E')
38
#define ID_CHAN       MKTAG('C','H','A','N')
39 02c4f626 Peter Ross
#define ID_PBM        MKTAG('P','B','M',' ')
40
#define ID_ILBM       MKTAG('I','L','B','M')
41
#define ID_BMHD       MKTAG('B','M','H','D')
42
#define ID_CMAP       MKTAG('C','M','A','P')
43 3a027f5e Jai Menon
44
#define ID_FORM       MKTAG('F','O','R','M')
45
#define ID_ANNO       MKTAG('A','N','N','O')
46
#define ID_AUTH       MKTAG('A','U','T','H')
47
#define ID_CHRS       MKTAG('C','H','R','S')
48
#define ID_COPYRIGHT  MKTAG('(','c',')',' ')
49
#define ID_CSET       MKTAG('C','S','E','T')
50
#define ID_FVER       MKTAG('F','V','E','R')
51
#define ID_NAME       MKTAG('N','A','M','E')
52
#define ID_TEXT       MKTAG('T','E','X','T')
53
#define ID_BODY       MKTAG('B','O','D','Y')
54
55
#define LEFT    2
56
#define RIGHT   4
57
#define STEREO  6
58
59
#define PACKET_SIZE 1024
60
61 f98dcded Jai Menon
typedef enum {COMP_NONE, COMP_FIB, COMP_EXP} svx8_compression_type;
62 02c4f626 Peter Ross
typedef enum {BITMAP_RAW, BITMAP_BYTERUN1} bitmap_compression_type;
63 3a027f5e Jai Menon
64
typedef struct {
65
    uint32_t  body_size;
66
    uint32_t  sent_bytes;
67
    uint32_t  audio_frame_count;
68
} IffDemuxContext;
69
70 86b2d47f Jai Menon
71
static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
72
{
73
    uint8_t *end = dest + size;
74
    size = size>>1;
75
76
    while(dest < end) {
77
        *dest++ = *src;
78
        *dest++ = *(src+size);
79
        src++;
80
    }
81
}
82
83 3a027f5e Jai Menon
static int iff_probe(AVProbeData *p)
84
{
85
    const uint8_t *d = p->buf;
86
87
    if ( AV_RL32(d)   == ID_FORM &&
88 02c4f626 Peter Ross
         (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
89 3a027f5e Jai Menon
        return AVPROBE_SCORE_MAX;
90
    return 0;
91
}
92
93
static int iff_read_header(AVFormatContext *s,
94
                           AVFormatParameters *ap)
95
{
96
    IffDemuxContext *iff = s->priv_data;
97
    ByteIOContext *pb = s->pb;
98
    AVStream *st;
99
    uint32_t chunk_id, data_size;
100 b2fec0fc Diego Biurrun
    int padding, done = 0;
101 02c4f626 Peter Ross
    int compression = -1;
102 3a027f5e Jai Menon
103
    st = av_new_stream(s, 0);
104
    if (!st)
105
      return AVERROR(ENOMEM);
106
107
    st->codec->channels = 1;
108 02c4f626 Peter Ross
    url_fskip(pb, 8);
109
    // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
110
    st->codec->codec_tag = get_le32(pb);
111 3a027f5e Jai Menon
112
    while(!done && !url_feof(pb)) {
113
        chunk_id = get_le32(pb);
114
        data_size = get_be32(pb);
115
        padding = data_size & 1;
116
117
        switch(chunk_id) {
118
        case ID_VHDR:
119 02c4f626 Peter Ross
            st->codec->codec_type = CODEC_TYPE_AUDIO;
120 3a027f5e Jai Menon
            url_fskip(pb, 12);
121
            st->codec->sample_rate = get_be16(pb);
122
            url_fskip(pb, 1);
123 02c4f626 Peter Ross
            compression            = get_byte(pb);
124 3a027f5e Jai Menon
            url_fskip(pb, 4);
125
            break;
126
127
        case ID_BODY:
128
            iff->body_size = data_size;
129
            done = 1;
130
            break;
131
132
        case ID_CHAN:
133
            st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
134
            break;
135
136 02c4f626 Peter Ross
        case ID_CMAP:
137
            st->codec->extradata_size = data_size;
138
            st->codec->extradata      = av_malloc(data_size);
139
            if (!st->codec->extradata)
140
                return AVERROR(ENOMEM);
141
            if (get_buffer(pb, st->codec->extradata, data_size) < 0)
142
                return AVERROR(EIO);
143
            break;
144
145
        case ID_BMHD:
146
            st->codec->codec_type            = CODEC_TYPE_VIDEO;
147
            st->codec->width                 = get_be16(pb);
148
            st->codec->height                = get_be16(pb);
149
            url_fskip(pb, 4); // x, y offset
150
            st->codec->bits_per_coded_sample = get_byte(pb);
151
            url_fskip(pb, 1); // masking
152
            compression                      = get_byte(pb);
153
            url_fskip(pb, 3); // paddding, transparent
154
            st->sample_aspect_ratio.num      = get_byte(pb);
155
            st->sample_aspect_ratio.den      = get_byte(pb);
156
            url_fskip(pb, 4); // source page width, height
157
            break;
158
159 3a027f5e Jai Menon
        default:
160
            url_fseek(pb, data_size + padding, SEEK_CUR);
161
            break;
162
        }
163
    }
164
165 02c4f626 Peter Ross
    switch(st->codec->codec_type) {
166
    case CODEC_TYPE_AUDIO:
167 3a027f5e Jai Menon
    av_set_pts_info(st, 32, 1, st->codec->sample_rate);
168
169 02c4f626 Peter Ross
    switch(compression) {
170 3a027f5e Jai Menon
    case COMP_NONE:
171
        st->codec->codec_id = CODEC_ID_PCM_S8;
172
        break;
173
    case COMP_FIB:
174
        st->codec->codec_id = CODEC_ID_8SVX_FIB;
175
        break;
176
    case COMP_EXP:
177
        st->codec->codec_id = CODEC_ID_8SVX_EXP;
178
        break;
179
    default:
180
        av_log(s, AV_LOG_ERROR, "iff: unknown compression method\n");
181
        return -1;
182
    }
183
184 dd1c8f3e Luca Abeni
    st->codec->bits_per_coded_sample = 8;
185
    st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
186
    st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
187 02c4f626 Peter Ross
    break;
188
189
    case CODEC_TYPE_VIDEO:
190
        switch (compression) {
191
        case BITMAP_RAW:
192
            if (st->codec->codec_tag == ID_ILBM) {
193
                st->codec->codec_id = CODEC_ID_IFF_ILBM;
194
            } else {
195
                st->codec->codec_id = CODEC_ID_RAWVIDEO;
196
                st->codec->pix_fmt  = PIX_FMT_PAL8;
197
                st->codec->codec_tag = 0;
198
            }
199
            break;
200
        case BITMAP_BYTERUN1:
201
            st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
202
            break;
203
        default:
204
            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
205
            return AVERROR_INVALIDDATA;
206
        }
207
        break;
208
    default:
209
        return -1;
210
    }
211 3a027f5e Jai Menon
212
    return 0;
213
}
214
215 02c4f626 Peter Ross
int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal);
216
217 3a027f5e Jai Menon
static int iff_read_packet(AVFormatContext *s,
218
                           AVPacket *pkt)
219
{
220
    IffDemuxContext *iff = s->priv_data;
221
    ByteIOContext *pb = s->pb;
222 02c4f626 Peter Ross
    AVStream *st = s->streams[0];
223 3a027f5e Jai Menon
    int ret;
224
225 02c4f626 Peter Ross
    if(iff->sent_bytes >= iff->body_size)
226 3a027f5e Jai Menon
        return AVERROR(EIO);
227 86b2d47f Jai Menon
228
    if(s->streams[0]->codec->channels == 2) {
229
        uint8_t sample_buffer[PACKET_SIZE];
230
231
        ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
232
        if(av_new_packet(pkt, PACKET_SIZE) < 0) {
233
            av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n");
234
            return AVERROR(ENOMEM);
235
        }
236
        interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
237 02c4f626 Peter Ross
    } else if (s->streams[0]->codec->codec_id == CODEC_ID_RAWVIDEO) {
238
        if(av_new_packet(pkt, iff->body_size + AVPALETTE_SIZE) < 0) {
239
            return AVERROR(ENOMEM);
240
        }
241
242
        ret = ff_cmap_read_palette(st->codec, (uint32_t*)(pkt->data + iff->body_size));
243
        if (ret < 0)
244
            return ret;
245
        av_freep(&st->codec->extradata);
246
        st->codec->extradata_size = 0;
247
248
        ret = get_buffer(pb, pkt->data, iff->body_size);
249
    } else if (s->streams[0]->codec->codec_type == CODEC_TYPE_VIDEO) {
250
        ret = av_get_packet(pb, pkt, iff->body_size);
251
    } else {
252 86b2d47f Jai Menon
        ret = av_get_packet(pb, pkt, PACKET_SIZE);
253
    }
254 3a027f5e Jai Menon
255
    if(iff->sent_bytes == 0)
256
        pkt->flags |= PKT_FLAG_KEY;
257
258 02c4f626 Peter Ross
    if(s->streams[0]->codec->codec_type == CODEC_TYPE_AUDIO) {
259 451b353d Peter Ross
        iff->sent_bytes += PACKET_SIZE;
260 02c4f626 Peter Ross
    } else {
261
        iff->sent_bytes = iff->body_size;
262
    }
263 3a027f5e Jai Menon
    pkt->stream_index = 0;
264 02c4f626 Peter Ross
    if(s->streams[0]->codec->codec_type == CODEC_TYPE_AUDIO) {
265 451b353d Peter Ross
        pkt->pts = iff->audio_frame_count;
266
        iff->audio_frame_count += ret / s->streams[0]->codec->channels;
267 02c4f626 Peter Ross
    }
268 3a027f5e Jai Menon
    return ret;
269
}
270
271
AVInputFormat iff_demuxer = {
272
    "IFF",
273 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("IFF format"),
274 3a027f5e Jai Menon
    sizeof(IffDemuxContext),
275
    iff_probe,
276
    iff_read_header,
277
    iff_read_packet,
278
};