Statistics
| Branch: | Revision:

ffmpeg / libavformat / 4xm.c @ 5a757507

History | View | Annotate | Download (11.1 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 3a278992 Mike Melanson
#define  RIFF_TAG MKTAG('R', 'I', 'F', 'F')
31
#define _4XMV_TAG MKTAG('4', 'X', 'M', 'V')
32
#define  LIST_TAG MKTAG('L', 'I', 'S', 'T')
33
#define  HEAD_TAG MKTAG('H', 'E', 'A', 'D')
34
#define  TRK__TAG MKTAG('T', 'R', 'K', '_')
35
#define  MOVI_TAG MKTAG('M', 'O', 'V', 'I')
36
#define  VTRK_TAG MKTAG('V', 'T', 'R', 'K')
37
#define  STRK_TAG MKTAG('S', 'T', 'R', 'K')
38
#define  std__TAG MKTAG('s', 't', 'd', '_')
39
#define  name_TAG MKTAG('n', 'a', 'm', 'e')
40
#define  vtrk_TAG MKTAG('v', 't', 'r', 'k')
41
#define  strk_TAG MKTAG('s', 't', 'r', 'k')
42
#define  ifrm_TAG MKTAG('i', 'f', 'r', 'm')
43
#define  pfrm_TAG MKTAG('p', 'f', 'r', 'm')
44
#define  cfrm_TAG MKTAG('c', 'f', 'r', 'm')
45
#define  snd__TAG MKTAG('s', 'n', 'd', '_')
46 cef4ba9e Mike Melanson
47
#define vtrk_SIZE 0x44
48
#define strk_SIZE 0x28
49
50
#define GET_LIST_HEADER() \
51 8e7284ba Mike Melanson
    fourcc_tag = get_le32(pb); \
52 cef4ba9e Mike Melanson
    size = get_le32(pb); \
53
    if (fourcc_tag != LIST_TAG) \
54
        return AVERROR_INVALIDDATA; \
55 8e7284ba Mike Melanson
    fourcc_tag = get_le32(pb);
56 cef4ba9e Mike Melanson
57
typedef struct AudioTrack {
58
    int sample_rate;
59
    int bits;
60
    int channels;
61 8e7284ba Mike Melanson
    int stream_index;
62 4b465299 Michael Niedermayer
    int adpcm;
63 cef4ba9e Mike Melanson
} AudioTrack;
64
65
typedef struct FourxmDemuxContext {
66
    int width;
67
    int height;
68 8e7284ba Mike Melanson
    int video_stream_index;
69 cef4ba9e Mike Melanson
    int track_count;
70
    AudioTrack *tracks;
71
    int selected_track;
72 8e7284ba Mike Melanson
73 d66dae57 Mike Melanson
    int64_t audio_pts;
74
    int64_t video_pts;
75
    int video_pts_inc;
76 11498da3 Mike Melanson
    float fps;
77 cef4ba9e Mike Melanson
} FourxmDemuxContext;
78
79 d66dae57 Mike Melanson
static float get_le_float(unsigned char *buffer)
80
{
81
    float f;
82
    unsigned char *float_buffer = (unsigned char *)&f;
83
84
#ifdef WORDS_BIGENDIAN
85
    float_buffer[0] = buffer[3];
86
    float_buffer[1] = buffer[2];
87
    float_buffer[2] = buffer[1];
88
    float_buffer[3] = buffer[0];
89
#else
90
    float_buffer[0] = buffer[0];
91
    float_buffer[1] = buffer[1];
92
    float_buffer[2] = buffer[2];
93
    float_buffer[3] = buffer[3];
94
#endif
95
96
    return f;
97
}
98
99 cef4ba9e Mike Melanson
static int fourxm_probe(AVProbeData *p)
100
{
101 e7106d5b Mike Melanson
    if (p->buf_size < 12)
102
        return 0;
103
104 8e7284ba Mike Melanson
    if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
105
        (LE_32(&p->buf[8]) != _4XMV_TAG))
106 cef4ba9e Mike Melanson
        return 0;
107
108
    return AVPROBE_SCORE_MAX;
109
}
110
111
static int fourxm_read_header(AVFormatContext *s,
112 8e7284ba Mike Melanson
                              AVFormatParameters *ap)
113 cef4ba9e Mike Melanson
{
114
    ByteIOContext *pb = &s->pb;
115
    unsigned int fourcc_tag;
116
    unsigned int size;
117
    int header_size;
118
    FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
119
    unsigned char *header;
120
    int i;
121
    int current_track = -1;
122
    AVStream *st;
123
124
    fourxm->track_count = 0;
125
    fourxm->tracks = NULL;
126
    fourxm->selected_track = 0;
127 11498da3 Mike Melanson
    fourxm->fps = 1.0;
128 cef4ba9e Mike Melanson
129
    /* skip the first 3 32-bit numbers */
130
    url_fseek(pb, 12, SEEK_CUR);
131
132
    /* check for LIST-HEAD */
133
    GET_LIST_HEADER();
134
    header_size = size - 4;
135
    if (fourcc_tag != HEAD_TAG)
136
        return AVERROR_INVALIDDATA;
137
138
    /* allocate space for the header and load the whole thing */
139
    header = av_malloc(header_size);
140
    if (!header)
141
        return AVERROR_NOMEM;
142
    if (get_buffer(pb, header, header_size) != header_size)
143
        return AVERROR_IO;
144
145
    /* take the lazy approach and search for any and all vtrk and strk chunks */
146
    for (i = 0; i < header_size - 8; i++) {
147 8e7284ba Mike Melanson
        fourcc_tag = LE_32(&header[i]);
148 cef4ba9e Mike Melanson
        size = LE_32(&header[i + 4]);
149
150 d66dae57 Mike Melanson
        if (fourcc_tag == std__TAG) {
151 11498da3 Mike Melanson
            fourxm->fps = get_le_float(&header[i + 12]);
152
            fourxm->video_pts_inc = (int)(90000.0 / fourxm->fps);
153 d66dae57 Mike Melanson
        } else if (fourcc_tag == vtrk_TAG) {
154 cef4ba9e Mike Melanson
            /* check that there is enough data */
155
            if (size != vtrk_SIZE) {
156
                av_free(header);
157
                return AVERROR_INVALIDDATA;
158
            }
159
            fourxm->width = LE_32(&header[i + 36]);
160
            fourxm->height = LE_32(&header[i + 40]);
161
            i += 8 + size;
162 8e7284ba Mike Melanson
163
            /* allocate a new AVStream */
164
            st = av_new_stream(s, 0);
165
            if (!st)
166
                return AVERROR_NOMEM;
167
168
            fourxm->video_stream_index = st->index;
169
170 11498da3 Mike Melanson
            st->codec.frame_rate = fourxm->fps;
171
            st->codec.frame_rate_base = 1.0;
172 8e7284ba Mike Melanson
            st->codec.codec_type = CODEC_TYPE_VIDEO;
173
            st->codec.codec_id = CODEC_ID_4XM;
174
            st->codec.codec_tag = 0;  /* no fourcc */
175
            st->codec.width = fourxm->width;
176
            st->codec.height = fourxm->height;
177
178 cef4ba9e Mike Melanson
        } else if (fourcc_tag == strk_TAG) {
179
            /* check that there is enough data */
180
            if (size != strk_SIZE) {
181
                av_free(header);
182
                return AVERROR_INVALIDDATA;
183
            }
184
            current_track = LE_32(&header[i + 8]);
185
            if (current_track + 1 > fourxm->track_count) {
186 5a634a69 Mike Melanson
                fourxm->track_count = current_track + 1;
187
                fourxm->tracks = av_realloc(fourxm->tracks, 
188
                    fourxm->track_count * sizeof(AudioTrack));
189 cef4ba9e Mike Melanson
                if (!fourxm->tracks) {
190
                    av_free(header);
191
                    return AVERROR_NOMEM;
192
                }
193
            }
194 4b465299 Michael Niedermayer
            fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
195 cef4ba9e Mike Melanson
            fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
196
            fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
197
            fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
198
            i += 8 + size;
199 8e7284ba Mike Melanson
200
            /* allocate a new AVStream */
201
            st = av_new_stream(s, current_track);
202
            if (!st)
203
                return AVERROR_NOMEM;
204
205
            fourxm->tracks[current_track].stream_index = st->index;
206
207
            st->codec.codec_type = CODEC_TYPE_AUDIO;
208
            st->codec.codec_tag = 1;
209
            st->codec.channels = fourxm->tracks[current_track].channels;
210
            st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
211
            st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
212
            st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
213
                st->codec.bits_per_sample;
214
            st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
215 4b465299 Michael Niedermayer
            if (fourxm->tracks[current_track].adpcm)
216
                st->codec.codec_id = CODEC_ID_ADPCM_4XM;
217
            else if (st->codec.bits_per_sample == 8)
218 8e7284ba Mike Melanson
                st->codec.codec_id = CODEC_ID_PCM_U8;
219
            else
220
                st->codec.codec_id = CODEC_ID_PCM_S16LE;
221 cef4ba9e Mike Melanson
        }
222
    }
223
224
    av_free(header);
225
226
    /* skip over the LIST-MOVI chunk (which is where the stream should be */
227
    GET_LIST_HEADER();
228
    if (fourcc_tag != MOVI_TAG)
229
        return AVERROR_INVALIDDATA;
230
231 8e7284ba Mike Melanson
    /* initialize context members */
232 d66dae57 Mike Melanson
    fourxm->video_pts = -fourxm->video_pts_inc;  /* first frame will push to 0 */
233
    fourxm->audio_pts = 0;
234 8e7284ba Mike Melanson
235
    /* set the pts reference (1 pts = 1/90000) */
236
    s->pts_num = 1;
237
    s->pts_den = 90000;
238 cef4ba9e Mike Melanson
239
    return 0;
240
}
241
242
static int fourxm_read_packet(AVFormatContext *s,
243 8e7284ba Mike Melanson
                              AVPacket *pkt)
244 cef4ba9e Mike Melanson
{
245
    FourxmDemuxContext *fourxm = s->priv_data;
246
    ByteIOContext *pb = &s->pb;
247
    unsigned int fourcc_tag;
248 4b465299 Michael Niedermayer
    unsigned int size, out_size;
249 cef4ba9e Mike Melanson
    int ret = 0;
250
    int track_number;
251
    int packet_read = 0;
252 8e7284ba Mike Melanson
    unsigned char header[8];
253
    int64_t pts_inc;
254 d66dae57 Mike Melanson
    int audio_frame_count;
255 cef4ba9e Mike Melanson
256
    while (!packet_read) {
257
258 8e7284ba Mike Melanson
        if ((ret = get_buffer(&s->pb, header, 8)) < 0)
259
            return ret;
260
        fourcc_tag = LE_32(&header[0]);
261
        size = LE_32(&header[4]);
262 cef4ba9e Mike Melanson
        if (url_feof(pb))
263
            return -EIO;
264
        switch (fourcc_tag) {
265
266 73a19b2d Mike Melanson
        case LIST_TAG:
267 d66dae57 Mike Melanson
            /* this is a good time to bump the video pts */
268
            fourxm->video_pts += fourxm->video_pts_inc;
269
270 73a19b2d Mike Melanson
            /* skip the LIST-* tag and move on to the next fourcc */
271
            get_le32(pb);
272
            break;
273
274 cef4ba9e Mike Melanson
        case ifrm_TAG:
275
        case pfrm_TAG:
276 4a106616 Michael Niedermayer
        case cfrm_TAG:{
277 8e7284ba Mike Melanson
278
            /* allocate 8 more bytes than 'size' to account for fourcc
279
             * and size */
280
            if (av_new_packet(pkt, size + 8))
281
                return -EIO;
282
            pkt->stream_index = fourxm->video_stream_index;
283 d66dae57 Mike Melanson
            pkt->pts = fourxm->video_pts;
284 8e7284ba Mike Melanson
            memcpy(pkt->data, header, 8);
285
            ret = get_buffer(&s->pb, &pkt->data[8], size);
286
287
            if (ret < 0)
288
                av_free_packet(pkt);
289
            else
290
                packet_read = 1;
291 4a106616 Michael Niedermayer
            break;
292
        }
293 8e7284ba Mike Melanson
294 cef4ba9e Mike Melanson
        case snd__TAG:
295
            track_number = get_le32(pb);
296 4b465299 Michael Niedermayer
            out_size= get_le32(pb);
297
            size-=8;
298
299 cef4ba9e Mike Melanson
            if (track_number == fourxm->selected_track) {
300
                if (av_new_packet(pkt, size))
301
                    return -EIO;
302 8e7284ba Mike Melanson
                pkt->stream_index = 
303
                    fourxm->tracks[fourxm->selected_track].stream_index;
304 d66dae57 Mike Melanson
                pkt->pts = fourxm->audio_pts;
305 cef4ba9e Mike Melanson
                ret = get_buffer(&s->pb, pkt->data, size);
306
                if (ret < 0)
307
                    av_free_packet(pkt);
308 8e7284ba Mike Melanson
                else
309
                    packet_read = 1;
310
311 d66dae57 Mike Melanson
                /* pts accounting */
312
                audio_frame_count = size;
313
                if (fourxm->tracks[fourxm->selected_track].adpcm)
314
                    audio_frame_count -= 
315
                        2 * (fourxm->tracks[fourxm->selected_track].channels);
316
                audio_frame_count /=
317
                      fourxm->tracks[fourxm->selected_track].channels;
318
                if (fourxm->tracks[fourxm->selected_track].adpcm)
319
                    audio_frame_count *= 2;
320
                else 
321
                    audio_frame_count /=
322
                    (fourxm->tracks[fourxm->selected_track].bits / 8);
323
                pts_inc = audio_frame_count;
324
                pts_inc *= 90000;
325
                pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
326
                fourxm->audio_pts += pts_inc;
327 8e7284ba Mike Melanson
328 cef4ba9e Mike Melanson
            } else {
329
                url_fseek(pb, size, SEEK_CUR);
330
            }
331
            break;
332
333
        default:
334
            url_fseek(pb, size, SEEK_CUR);
335
            break;
336
        }
337
    }
338
    return ret;
339
}
340
341
static int fourxm_read_close(AVFormatContext *s)
342
{
343
    FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
344
345
    av_free(fourxm->tracks);
346
347
    return 0;
348
}
349
350
static AVInputFormat fourxm_iformat = {
351
    "4xm",
352
    "4X Technologies format",
353
    sizeof(FourxmDemuxContext),
354
    fourxm_probe,
355
    fourxm_read_header,
356
    fourxm_read_packet,
357
    fourxm_read_close,
358
};
359
360
int fourxm_init(void)
361
{
362
    av_register_input_format(&fourxm_iformat);
363
    return 0;
364
}