Statistics
| Branch: | Revision:

ffmpeg / libavformat / 4xm.c @ 5aa0a644

History | View | Annotate | Download (10.7 KB)

1 cef4ba9e Mike Melanson
/*
2
 * 4X Technologies .4xm File Demuxer (no muxer)
3
 * Copyright (c) 2003  The ffmpeg Project
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
20
/**
21
 * @file 4xm.c
22
 * 4X Technologies file demuxer
23
 * by Mike Melanson (melanson@pcisys.net)
24
 * for more information on the .4xm file format, visit:
25
 *   http://www.pcisys.net/~melanson/codecs/
26
 */
27
28
#include "avformat.h"
29
30
#define LE_16(x)  ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
31
#define LE_32(x)  ((((uint8_t*)(x))[3] << 24) | \
32
                   (((uint8_t*)(x))[2] << 16) | \
33
                   (((uint8_t*)(x))[1] << 8) | \
34
                    ((uint8_t*)(x))[0])
35
36
#define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
37 8e7284ba Mike Melanson
        ( (long)(unsigned char)(ch0) | \
38
        ( (long)(unsigned char)(ch1) << 8 ) | \
39
        ( (long)(unsigned char)(ch2) << 16 ) | \
40
        ( (long)(unsigned char)(ch3) << 24 ) )
41 cef4ba9e Mike Melanson
42
#define  RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
43
#define _4XMV_TAG FOURCC_TAG('4', 'X', 'M', 'V')
44
#define  LIST_TAG FOURCC_TAG('L', 'I', 'S', 'T')
45
#define  HEAD_TAG FOURCC_TAG('H', 'E', 'A', 'D')
46
#define  TRK__TAG FOURCC_TAG('T', 'R', 'K', '_')
47
#define  MOVI_TAG FOURCC_TAG('M', 'O', 'V', 'I')
48
#define  VTRK_TAG FOURCC_TAG('V', 'T', 'R', 'K')
49
#define  STRK_TAG FOURCC_TAG('S', 'T', 'R', 'K')
50
#define  name_TAG FOURCC_TAG('n', 'a', 'm', 'e')
51
#define  vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k')
52
#define  strk_TAG FOURCC_TAG('s', 't', 'r', 'k')
53
#define  ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm')
54
#define  pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm')
55
#define  cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm')
56
#define  snd__TAG FOURCC_TAG('s', 'n', 'd', '_')
57
#define  _TAG FOURCC_TAG('', '', '', '')
58
59
#define vtrk_SIZE 0x44
60
#define strk_SIZE 0x28
61
62
#define GET_LIST_HEADER() \
63 8e7284ba Mike Melanson
    fourcc_tag = get_le32(pb); \
64 cef4ba9e Mike Melanson
    size = get_le32(pb); \
65
    if (fourcc_tag != LIST_TAG) \
66
        return AVERROR_INVALIDDATA; \
67 8e7284ba Mike Melanson
    fourcc_tag = get_le32(pb);
68 cef4ba9e Mike Melanson
69
typedef struct AudioTrack {
70
    int sample_rate;
71
    int bits;
72
    int channels;
73 8e7284ba Mike Melanson
    int stream_index;
74 4b465299 Michael Niedermayer
    int adpcm;
75 cef4ba9e Mike Melanson
} AudioTrack;
76
77
typedef struct FourxmDemuxContext {
78
    int width;
79
    int height;
80 8e7284ba Mike Melanson
    int video_stream_index;
81 cef4ba9e Mike Melanson
    int track_count;
82
    AudioTrack *tracks;
83
    int selected_track;
84 8e7284ba Mike Melanson
85
    int64_t pts;
86
    int last_chunk_was_audio;
87
    int last_audio_frame_count;
88 cef4ba9e Mike Melanson
} FourxmDemuxContext;
89
90
static int fourxm_probe(AVProbeData *p)
91
{
92 e7106d5b Mike Melanson
    if (p->buf_size < 12)
93
        return 0;
94
95 8e7284ba Mike Melanson
    if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
96
        (LE_32(&p->buf[8]) != _4XMV_TAG))
97 cef4ba9e Mike Melanson
        return 0;
98
99
    return AVPROBE_SCORE_MAX;
100
}
101
102
static int fourxm_read_header(AVFormatContext *s,
103 8e7284ba Mike Melanson
                              AVFormatParameters *ap)
104 cef4ba9e Mike Melanson
{
105
    ByteIOContext *pb = &s->pb;
106
    unsigned int fourcc_tag;
107
    unsigned int size;
108
    int header_size;
109
    FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
110
    unsigned char *header;
111
    int i;
112
    int current_track = -1;
113
    AVStream *st;
114
115
    fourxm->track_count = 0;
116
    fourxm->tracks = NULL;
117
    fourxm->selected_track = 0;
118
119
    /* skip the first 3 32-bit numbers */
120
    url_fseek(pb, 12, SEEK_CUR);
121
122
    /* check for LIST-HEAD */
123
    GET_LIST_HEADER();
124
    header_size = size - 4;
125
    if (fourcc_tag != HEAD_TAG)
126
        return AVERROR_INVALIDDATA;
127
128
    /* allocate space for the header and load the whole thing */
129
    header = av_malloc(header_size);
130
    if (!header)
131
        return AVERROR_NOMEM;
132
    if (get_buffer(pb, header, header_size) != header_size)
133
        return AVERROR_IO;
134
135
    /* take the lazy approach and search for any and all vtrk and strk chunks */
136
    for (i = 0; i < header_size - 8; i++) {
137 8e7284ba Mike Melanson
        fourcc_tag = LE_32(&header[i]);
138 cef4ba9e Mike Melanson
        size = LE_32(&header[i + 4]);
139
140
        if (fourcc_tag == vtrk_TAG) {
141
            /* check that there is enough data */
142
            if (size != vtrk_SIZE) {
143
                av_free(header);
144
                return AVERROR_INVALIDDATA;
145
            }
146
            fourxm->width = LE_32(&header[i + 36]);
147
            fourxm->height = LE_32(&header[i + 40]);
148
            i += 8 + size;
149 8e7284ba Mike Melanson
150
            /* allocate a new AVStream */
151
            st = av_new_stream(s, 0);
152
            if (!st)
153
                return AVERROR_NOMEM;
154
155
            fourxm->video_stream_index = st->index;
156
157
            st->codec.codec_type = CODEC_TYPE_VIDEO;
158
            st->codec.codec_id = CODEC_ID_4XM;
159
            st->codec.codec_tag = 0;  /* no fourcc */
160
            st->codec.width = fourxm->width;
161
            st->codec.height = fourxm->height;
162
163 cef4ba9e Mike Melanson
        } else if (fourcc_tag == strk_TAG) {
164
            /* check that there is enough data */
165
            if (size != strk_SIZE) {
166
                av_free(header);
167
                return AVERROR_INVALIDDATA;
168
            }
169
            current_track = LE_32(&header[i + 8]);
170
            if (current_track + 1 > fourxm->track_count) {
171 5a634a69 Mike Melanson
                fourxm->track_count = current_track + 1;
172
                fourxm->tracks = av_realloc(fourxm->tracks, 
173
                    fourxm->track_count * sizeof(AudioTrack));
174 cef4ba9e Mike Melanson
                if (!fourxm->tracks) {
175
                    av_free(header);
176
                    return AVERROR_NOMEM;
177
                }
178
            }
179 4b465299 Michael Niedermayer
            fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
180 cef4ba9e Mike Melanson
            fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
181
            fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
182
            fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
183 4b465299 Michael Niedermayer
            printf("bps:%d\n", fourxm->tracks[current_track].bits);
184 cef4ba9e Mike Melanson
            i += 8 + size;
185 8e7284ba Mike Melanson
186
            /* allocate a new AVStream */
187
            st = av_new_stream(s, current_track);
188
            if (!st)
189
                return AVERROR_NOMEM;
190
191
            fourxm->tracks[current_track].stream_index = st->index;
192
193
            st->codec.codec_type = CODEC_TYPE_AUDIO;
194
            st->codec.codec_tag = 1;
195
            st->codec.channels = fourxm->tracks[current_track].channels;
196
            st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
197
            st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
198
            st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
199
                st->codec.bits_per_sample;
200
            st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
201 4b465299 Michael Niedermayer
            if (fourxm->tracks[current_track].adpcm)
202
                st->codec.codec_id = CODEC_ID_ADPCM_4XM;
203
            else if (st->codec.bits_per_sample == 8)
204 8e7284ba Mike Melanson
                st->codec.codec_id = CODEC_ID_PCM_U8;
205
            else
206
                st->codec.codec_id = CODEC_ID_PCM_S16LE;
207 cef4ba9e Mike Melanson
        }
208
    }
209
210
    av_free(header);
211
212
    /* skip over the LIST-MOVI chunk (which is where the stream should be */
213
    GET_LIST_HEADER();
214
    if (fourcc_tag != MOVI_TAG)
215
        return AVERROR_INVALIDDATA;
216
217 8e7284ba Mike Melanson
    /* initialize context members */
218
    fourxm->pts = 0;
219
    fourxm->last_chunk_was_audio = 0;
220
    fourxm->last_audio_frame_count = 0;
221
222
    /* set the pts reference (1 pts = 1/90000) */
223
    s->pts_num = 1;
224
    s->pts_den = 90000;
225 cef4ba9e Mike Melanson
226
    return 0;
227
}
228
229
static int fourxm_read_packet(AVFormatContext *s,
230 8e7284ba Mike Melanson
                              AVPacket *pkt)
231 cef4ba9e Mike Melanson
{
232
    FourxmDemuxContext *fourxm = s->priv_data;
233
    ByteIOContext *pb = &s->pb;
234
    unsigned int fourcc_tag;
235 4b465299 Michael Niedermayer
    unsigned int size, out_size;
236 cef4ba9e Mike Melanson
    int ret = 0;
237
    int track_number;
238
    int packet_read = 0;
239 8e7284ba Mike Melanson
    unsigned char header[8];
240
    int64_t pts_inc;
241 cef4ba9e Mike Melanson
242
    while (!packet_read) {
243
244 8e7284ba Mike Melanson
        if ((ret = get_buffer(&s->pb, header, 8)) < 0)
245
            return ret;
246
        fourcc_tag = LE_32(&header[0]);
247
        size = LE_32(&header[4]);
248 cef4ba9e Mike Melanson
        if (url_feof(pb))
249
            return -EIO;
250
        switch (fourcc_tag) {
251
252 73a19b2d Mike Melanson
        case LIST_TAG:
253
            /* skip the LIST-* tag and move on to the next fourcc */
254
            get_le32(pb);
255
            break;
256
257 cef4ba9e Mike Melanson
        case ifrm_TAG:
258
        case pfrm_TAG:
259 4a106616 Michael Niedermayer
        case cfrm_TAG:{
260 8e7284ba Mike Melanson
261 e7106d5b Mike Melanson
            /* bump the pts if the last data sent out was audio */
262 8e7284ba Mike Melanson
            if (fourxm->last_chunk_was_audio) {
263
                fourxm->last_chunk_was_audio = 0;
264
                pts_inc = fourxm->last_audio_frame_count;
265
                pts_inc *= 90000;
266
                pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
267
                fourxm->pts += pts_inc;
268
            }
269
270
            /* allocate 8 more bytes than 'size' to account for fourcc
271
             * and size */
272
            if (av_new_packet(pkt, size + 8))
273
                return -EIO;
274
            pkt->stream_index = fourxm->video_stream_index;
275
            pkt->pts = fourxm->pts;
276
            memcpy(pkt->data, header, 8);
277
            ret = get_buffer(&s->pb, &pkt->data[8], size);
278
279
            if (ret < 0)
280
                av_free_packet(pkt);
281
            else
282
                packet_read = 1;
283 4a106616 Michael Niedermayer
            break;
284
        }
285 8e7284ba Mike Melanson
286 cef4ba9e Mike Melanson
        case snd__TAG:
287
            track_number = get_le32(pb);
288 4b465299 Michael Niedermayer
            out_size= get_le32(pb);
289
            size-=8;
290
291 cef4ba9e Mike Melanson
            if (track_number == fourxm->selected_track) {
292
                if (av_new_packet(pkt, size))
293
                    return -EIO;
294 8e7284ba Mike Melanson
                pkt->stream_index = 
295
                    fourxm->tracks[fourxm->selected_track].stream_index;
296
                pkt->pts = fourxm->pts;
297 cef4ba9e Mike Melanson
                ret = get_buffer(&s->pb, pkt->data, size);
298
                if (ret < 0)
299
                    av_free_packet(pkt);
300 8e7284ba Mike Melanson
                else
301
                    packet_read = 1;
302
303
                /* maintain pts info for the benefit of the video track */
304
                fourxm->last_chunk_was_audio = 1;
305
                fourxm->last_audio_frame_count = size /
306
                    ((fourxm->tracks[fourxm->selected_track].bits / 8) *
307
                      fourxm->tracks[fourxm->selected_track].channels);
308
309 cef4ba9e Mike Melanson
            } else {
310
                url_fseek(pb, size, SEEK_CUR);
311
            }
312
            break;
313
314
        default:
315
            url_fseek(pb, size, SEEK_CUR);
316
            break;
317
        }
318
    }
319
    return ret;
320
}
321
322
static int fourxm_read_close(AVFormatContext *s)
323
{
324
    FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
325
326
    av_free(fourxm->tracks);
327
328
    return 0;
329
}
330
331
static AVInputFormat fourxm_iformat = {
332
    "4xm",
333
    "4X Technologies format",
334
    sizeof(FourxmDemuxContext),
335
    fourxm_probe,
336
    fourxm_read_header,
337
    fourxm_read_packet,
338
    fourxm_read_close,
339
};
340
341
int fourxm_init(void)
342
{
343
    av_register_input_format(&fourxm_iformat);
344
    return 0;
345
}