Statistics
| Branch: | Revision:

ffmpeg / libavformat / electronicarts.c @ f0a9978c

History | View | Annotate | Download (7.93 KB)

1
/* Electronic Arts Multimedia File Demuxer
2
 * Copyright (c) 2004  The ffmpeg Project
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

    
21
/**
22
 * @file electronicarts.c
23
 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
24
 * by Robin Kay (komadori at gekkou.co.uk)
25
 */
26

    
27
#include "avformat.h"
28

    
29
#define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
30
#define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
31
#define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
32
#define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')
33
#define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
34
#define _TAG MKTAG('', '', '', '')
35

    
36
#define EA_SAMPLE_RATE 22050
37
#define EA_BITS_PER_SAMPLE 16
38
#define EA_PREAMBLE_SIZE 8
39

    
40
typedef struct EaDemuxContext {
41
    int width;
42
    int height;
43
    int video_stream_index;
44
    int track_count;
45

    
46
    int audio_stream_index;
47
    int audio_frame_counter;
48

    
49
    int64_t audio_pts;
50
    int64_t video_pts;
51
    int video_pts_inc;
52
    float fps;
53

    
54
    int num_channels;
55
    int num_samples;
56
    int compression_type;
57
} EaDemuxContext;
58

    
59
static uint32_t read_arbitary(ByteIOContext *pb) {
60
    uint8_t size, byte;
61
    int i;
62
    uint32_t word;
63

    
64
    size = get_byte(pb);
65

    
66
    word = 0;
67
    for (i = 0; i < size; i++) {
68
        byte = get_byte(pb);
69
        word <<= 8;
70
        word |= byte;
71
    }
72

    
73
    return word;
74
}
75

    
76
/*
77
 * Process WVE file header
78
 * Returns 1 if the WVE file is valid and successfully opened, 0 otherwise
79
 */
80
static int process_ea_header(AVFormatContext *s) {
81
    int inHeader;
82
    uint32_t blockid, size;
83
    EaDemuxContext *ea = s->priv_data;
84
    ByteIOContext *pb = &s->pb;
85

    
86
    if (get_buffer(pb, (void*)&blockid, 4) != 4) {
87
        return 0;
88
    }
89
    if (le2me_32(blockid) != SCHl_TAG) {
90
        return 0;
91
    }
92

    
93
    if (get_buffer(pb, (void*)&size, 4) != 4) {
94
        return 0;
95
    }
96
    size = le2me_32(size);
97

    
98
    if (get_buffer(pb, (void*)&blockid, 4) != 4) {
99
        return 0;
100
    }
101
    if (le2me_32(blockid) != PT00_TAG) {
102
        av_log (s, AV_LOG_ERROR, "PT header missing\n");
103
        return 0;
104
    }
105

    
106
    inHeader = 1;
107
    while (inHeader) {
108
        int inSubheader;
109
        uint8_t byte;
110
        byte = get_byte(pb) & 0xFF;
111

    
112
        switch (byte) {
113
        case 0xFD:
114
            av_log (s, AV_LOG_INFO, "entered audio subheader\n");
115
            inSubheader = 1;
116
            while (inSubheader) {
117
                uint8_t subbyte;
118
                subbyte = get_byte(pb) & 0xFF;
119

    
120
                switch (subbyte) {
121
                case 0x82:
122
                    ea->num_channels = read_arbitary(pb);
123
                    av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
124
                    break;
125
                case 0x83:
126
                    ea->compression_type = read_arbitary(pb);
127
                    av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", ea->compression_type);
128
                    break;
129
                case 0x85:
130
                    ea->num_samples = read_arbitary(pb);
131
                    av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
132
                    break;
133
                case 0x8A:
134
                    av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
135
                    av_log (s, AV_LOG_INFO, "exited audio subheader\n");
136
                    inSubheader = 0;
137
                    break;
138
                default:
139
                    av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
140
                    break;
141
                }
142
            }
143
            break;
144
        case 0xFF:
145
            av_log (s, AV_LOG_INFO, "end of header block reached\n");
146
            inHeader = 0;
147
            break;
148
        default:
149
            av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
150
            break;
151
        }
152
    }
153

    
154
    if ((ea->num_channels != 2) || (ea->compression_type != 7)) {
155
        av_log (s, AV_LOG_ERROR, "unsupported stream type\n");
156
        return 0;
157
    }
158

    
159
    /* skip to the start of the data */
160
    url_fseek(pb, size, SEEK_SET);
161

    
162
    return 1;
163
}
164

    
165

    
166
static int ea_probe(AVProbeData *p)
167
{
168
    if (AV_RL32(&p->buf[0]) != SCHl_TAG)
169
        return 0;
170

    
171
    return AVPROBE_SCORE_MAX;
172
}
173

    
174
static int ea_read_header(AVFormatContext *s,
175
                          AVFormatParameters *ap)
176
{
177
    EaDemuxContext *ea = s->priv_data;
178
    AVStream *st;
179

    
180
    if (!process_ea_header(s))
181
        return AVERROR(EIO);
182

    
183
#if 0
184
    /* initialize the video decoder stream */
185
    st = av_new_stream(s, 0);
186
    if (!st)
187
        return AVERROR(ENOMEM);
188
    av_set_pts_info(st, 33, 1, 90000);
189
    ea->video_stream_index = st->index;
190
    st->codec->codec_type = CODEC_TYPE_VIDEO;
191
    st->codec->codec_id = CODEC_ID_EA_MJPEG;
192
    st->codec->codec_tag = 0;  /* no fourcc */
193
#endif
194

    
195
    /* initialize the audio decoder stream */
196
    st = av_new_stream(s, 0);
197
    if (!st)
198
        return AVERROR(ENOMEM);
199
    av_set_pts_info(st, 33, 1, EA_SAMPLE_RATE);
200
    st->codec->codec_type = CODEC_TYPE_AUDIO;
201
    st->codec->codec_id = CODEC_ID_ADPCM_EA;
202
    st->codec->codec_tag = 0;  /* no tag */
203
    st->codec->channels = ea->num_channels;
204
    st->codec->sample_rate = EA_SAMPLE_RATE;
205
    st->codec->bits_per_sample = EA_BITS_PER_SAMPLE;
206
    st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
207
        st->codec->bits_per_sample / 4;
208
    st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
209

    
210
    ea->audio_stream_index = st->index;
211
    ea->audio_frame_counter = 0;
212

    
213
    return 1;
214
}
215

    
216
static int ea_read_packet(AVFormatContext *s,
217
                          AVPacket *pkt)
218
{
219
    EaDemuxContext *ea = s->priv_data;
220
    ByteIOContext *pb = &s->pb;
221
    int ret = 0;
222
    int packet_read = 0;
223
    unsigned char preamble[EA_PREAMBLE_SIZE];
224
    unsigned int chunk_type, chunk_size;
225

    
226
    while (!packet_read) {
227

    
228
        if (get_buffer(pb, preamble, EA_PREAMBLE_SIZE) != EA_PREAMBLE_SIZE)
229
            return AVERROR(EIO);
230
        chunk_type = AV_RL32(&preamble[0]);
231
        chunk_size = AV_RL32(&preamble[4]) - EA_PREAMBLE_SIZE;
232

    
233
        switch (chunk_type) {
234
        /* audio data */
235
        case SCDl_TAG:
236
            ret = av_get_packet(pb, pkt, chunk_size);
237
            if (ret != chunk_size)
238
                ret = AVERROR(EIO);
239
            else {
240
                    pkt->stream_index = ea->audio_stream_index;
241
                    pkt->pts = 90000;
242
                    pkt->pts *= ea->audio_frame_counter;
243
                    pkt->pts /= EA_SAMPLE_RATE;
244

    
245
                    /* 2 samples/byte, 1 or 2 samples per frame depending
246
                     * on stereo; chunk also has 12-byte header */
247
                    ea->audio_frame_counter += ((chunk_size - 12) * 2) /
248
                        ea->num_channels;
249
            }
250

    
251
            packet_read = 1;
252
            break;
253

    
254
        /* ending tag */
255
        case SCEl_TAG:
256
            ret = AVERROR(EIO);
257
            packet_read = 1;
258
            break;
259

    
260
        default:
261
            url_fseek(pb, chunk_size, SEEK_CUR);
262
            break;
263
        }
264

    
265
        /* ending packet */
266
        if (chunk_type == SCEl_TAG) {
267
        }
268
    }
269

    
270
    return ret;
271
}
272

    
273
static int ea_read_close(AVFormatContext *s)
274
{
275
//    EaDemuxContext *ea = s->priv_data;
276

    
277
    return 0;
278
}
279

    
280
AVInputFormat ea_demuxer = {
281
    "ea",
282
    "Electronic Arts Multimedia Format",
283
    sizeof(EaDemuxContext),
284
    ea_probe,
285
    ea_read_header,
286
    ea_read_packet,
287
    ea_read_close,
288
};