Statistics
| Branch: | Revision:

ffmpeg / libavformat / 4xm.c @ c04c3282

History | View | Annotate | Download (10.4 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 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 cef4ba9e Mike Melanson
 */
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 11498da3 Mike Melanson
    float fps;
76 cef4ba9e Mike Melanson
} FourxmDemuxContext;
77
78
static int fourxm_probe(AVProbeData *p)
79
{
80 e7106d5b Mike Melanson
    if (p->buf_size < 12)
81
        return 0;
82
83 8e7284ba Mike Melanson
    if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
84
        (LE_32(&p->buf[8]) != _4XMV_TAG))
85 cef4ba9e Mike Melanson
        return 0;
86
87
    return AVPROBE_SCORE_MAX;
88
}
89
90
static int fourxm_read_header(AVFormatContext *s,
91 8e7284ba Mike Melanson
                              AVFormatParameters *ap)
92 cef4ba9e Mike Melanson
{
93
    ByteIOContext *pb = &s->pb;
94
    unsigned int fourcc_tag;
95
    unsigned int size;
96
    int header_size;
97
    FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
98
    unsigned char *header;
99
    int i;
100
    int current_track = -1;
101
    AVStream *st;
102
103
    fourxm->track_count = 0;
104
    fourxm->tracks = NULL;
105
    fourxm->selected_track = 0;
106 11498da3 Mike Melanson
    fourxm->fps = 1.0;
107 cef4ba9e Mike Melanson
108
    /* skip the first 3 32-bit numbers */
109
    url_fseek(pb, 12, SEEK_CUR);
110
111
    /* check for LIST-HEAD */
112
    GET_LIST_HEADER();
113
    header_size = size - 4;
114
    if (fourcc_tag != HEAD_TAG)
115
        return AVERROR_INVALIDDATA;
116
117
    /* allocate space for the header and load the whole thing */
118
    header = av_malloc(header_size);
119
    if (!header)
120
        return AVERROR_NOMEM;
121
    if (get_buffer(pb, header, header_size) != header_size)
122
        return AVERROR_IO;
123
124
    /* take the lazy approach and search for any and all vtrk and strk chunks */
125
    for (i = 0; i < header_size - 8; i++) {
126 8e7284ba Mike Melanson
        fourcc_tag = LE_32(&header[i]);
127 cef4ba9e Mike Melanson
        size = LE_32(&header[i + 4]);
128
129 d66dae57 Mike Melanson
        if (fourcc_tag == std__TAG) {
130 8baa6614 Michael Niedermayer
            fourxm->fps = av_int2flt(LE_32(&header[i + 12]));
131 d66dae57 Mike Melanson
        } else if (fourcc_tag == vtrk_TAG) {
132 cef4ba9e Mike Melanson
            /* check that there is enough data */
133
            if (size != vtrk_SIZE) {
134
                av_free(header);
135
                return AVERROR_INVALIDDATA;
136
            }
137
            fourxm->width = LE_32(&header[i + 36]);
138
            fourxm->height = LE_32(&header[i + 40]);
139
            i += 8 + size;
140 8e7284ba Mike Melanson
141
            /* allocate a new AVStream */
142
            st = av_new_stream(s, 0);
143
            if (!st)
144
                return AVERROR_NOMEM;
145 caf5fb95 Michael Niedermayer
            av_set_pts_info(st, 60, 1, fourxm->fps);
146 8e7284ba Mike Melanson
147
            fourxm->video_stream_index = st->index;
148
149 01f4895c Michael Niedermayer
            st->codec->codec_type = CODEC_TYPE_VIDEO;
150
            st->codec->codec_id = CODEC_ID_4XM;
151
            st->codec->codec_tag = 0;  /* no fourcc */
152
            st->codec->width = fourxm->width;
153
            st->codec->height = fourxm->height;
154 8e7284ba Mike Melanson
155 cef4ba9e Mike Melanson
        } else if (fourcc_tag == strk_TAG) {
156
            /* check that there is enough data */
157
            if (size != strk_SIZE) {
158
                av_free(header);
159
                return AVERROR_INVALIDDATA;
160
            }
161
            current_track = LE_32(&header[i + 8]);
162
            if (current_track + 1 > fourxm->track_count) {
163 5a634a69 Mike Melanson
                fourxm->track_count = current_track + 1;
164 568e18b1 Michael Niedermayer
                if((unsigned)fourxm->track_count >= UINT_MAX / sizeof(AudioTrack))
165
                    return -1;
166 115329f1 Diego Biurrun
                fourxm->tracks = av_realloc(fourxm->tracks,
167 5a634a69 Mike Melanson
                    fourxm->track_count * sizeof(AudioTrack));
168 cef4ba9e Mike Melanson
                if (!fourxm->tracks) {
169
                    av_free(header);
170
                    return AVERROR_NOMEM;
171
                }
172
            }
173 4b465299 Michael Niedermayer
            fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
174 cef4ba9e Mike Melanson
            fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
175
            fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
176
            fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
177
            i += 8 + size;
178 8e7284ba Mike Melanson
179
            /* allocate a new AVStream */
180
            st = av_new_stream(s, current_track);
181
            if (!st)
182
                return AVERROR_NOMEM;
183
184 caf5fb95 Michael Niedermayer
            av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
185 9ee91c2f Michael Niedermayer
186 8e7284ba Mike Melanson
            fourxm->tracks[current_track].stream_index = st->index;
187
188 01f4895c Michael Niedermayer
            st->codec->codec_type = CODEC_TYPE_AUDIO;
189
            st->codec->codec_tag = 1;
190
            st->codec->channels = fourxm->tracks[current_track].channels;
191
            st->codec->sample_rate = fourxm->tracks[current_track].sample_rate;
192
            st->codec->bits_per_sample = fourxm->tracks[current_track].bits;
193
            st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
194
                st->codec->bits_per_sample;
195
            st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
196 4b465299 Michael Niedermayer
            if (fourxm->tracks[current_track].adpcm)
197 01f4895c Michael Niedermayer
                st->codec->codec_id = CODEC_ID_ADPCM_4XM;
198
            else if (st->codec->bits_per_sample == 8)
199
                st->codec->codec_id = CODEC_ID_PCM_U8;
200 8e7284ba Mike Melanson
            else
201 01f4895c Michael Niedermayer
                st->codec->codec_id = CODEC_ID_PCM_S16LE;
202 cef4ba9e Mike Melanson
        }
203
    }
204
205
    av_free(header);
206
207
    /* skip over the LIST-MOVI chunk (which is where the stream should be */
208
    GET_LIST_HEADER();
209
    if (fourcc_tag != MOVI_TAG)
210
        return AVERROR_INVALIDDATA;
211
212 8e7284ba Mike Melanson
    /* initialize context members */
213 caf5fb95 Michael Niedermayer
    fourxm->video_pts = -1;  /* first frame will push to 0 */
214 d66dae57 Mike Melanson
    fourxm->audio_pts = 0;
215 8e7284ba Mike Melanson
216 cef4ba9e Mike Melanson
    return 0;
217
}
218
219
static int fourxm_read_packet(AVFormatContext *s,
220 8e7284ba Mike Melanson
                              AVPacket *pkt)
221 cef4ba9e Mike Melanson
{
222
    FourxmDemuxContext *fourxm = s->priv_data;
223
    ByteIOContext *pb = &s->pb;
224
    unsigned int fourcc_tag;
225 4b465299 Michael Niedermayer
    unsigned int size, out_size;
226 cef4ba9e Mike Melanson
    int ret = 0;
227
    int track_number;
228
    int packet_read = 0;
229 8e7284ba Mike Melanson
    unsigned char header[8];
230 d66dae57 Mike Melanson
    int audio_frame_count;
231 cef4ba9e Mike Melanson
232
    while (!packet_read) {
233
234 8e7284ba Mike Melanson
        if ((ret = get_buffer(&s->pb, header, 8)) < 0)
235
            return ret;
236
        fourcc_tag = LE_32(&header[0]);
237
        size = LE_32(&header[4]);
238 cef4ba9e Mike Melanson
        if (url_feof(pb))
239 0bd586c5 Mike Melanson
            return AVERROR_IO;
240 cef4ba9e Mike Melanson
        switch (fourcc_tag) {
241
242 73a19b2d Mike Melanson
        case LIST_TAG:
243 d66dae57 Mike Melanson
            /* this is a good time to bump the video pts */
244 caf5fb95 Michael Niedermayer
            fourxm->video_pts ++;
245 d66dae57 Mike Melanson
246 73a19b2d Mike Melanson
            /* skip the LIST-* tag and move on to the next fourcc */
247
            get_le32(pb);
248
            break;
249
250 cef4ba9e Mike Melanson
        case ifrm_TAG:
251
        case pfrm_TAG:
252 4a106616 Michael Niedermayer
        case cfrm_TAG:{
253 8e7284ba Mike Melanson
254
            /* allocate 8 more bytes than 'size' to account for fourcc
255
             * and size */
256 0ecca7a4 Michael Niedermayer
            if (size + 8 < size || av_new_packet(pkt, size + 8))
257 0bd586c5 Mike Melanson
                return AVERROR_IO;
258 8e7284ba Mike Melanson
            pkt->stream_index = fourxm->video_stream_index;
259 d66dae57 Mike Melanson
            pkt->pts = fourxm->video_pts;
260 2692067a Michael Niedermayer
            pkt->pos = url_ftell(&s->pb);
261 8e7284ba Mike Melanson
            memcpy(pkt->data, header, 8);
262
            ret = get_buffer(&s->pb, &pkt->data[8], size);
263
264
            if (ret < 0)
265
                av_free_packet(pkt);
266
            else
267
                packet_read = 1;
268 4a106616 Michael Niedermayer
            break;
269
        }
270 8e7284ba Mike Melanson
271 cef4ba9e Mike Melanson
        case snd__TAG:
272
            track_number = get_le32(pb);
273 4b465299 Michael Niedermayer
            out_size= get_le32(pb);
274
            size-=8;
275
276 cef4ba9e Mike Melanson
            if (track_number == fourxm->selected_track) {
277 2692067a Michael Niedermayer
                ret= av_get_packet(&s->pb, pkt, size);
278
                if(ret<0)
279 0bd586c5 Mike Melanson
                    return AVERROR_IO;
280 115329f1 Diego Biurrun
                pkt->stream_index =
281 8e7284ba Mike Melanson
                    fourxm->tracks[fourxm->selected_track].stream_index;
282 d66dae57 Mike Melanson
                pkt->pts = fourxm->audio_pts;
283 2692067a Michael Niedermayer
                packet_read = 1;
284 8e7284ba Mike Melanson
285 d66dae57 Mike Melanson
                /* pts accounting */
286
                audio_frame_count = size;
287
                if (fourxm->tracks[fourxm->selected_track].adpcm)
288 115329f1 Diego Biurrun
                    audio_frame_count -=
289 d66dae57 Mike Melanson
                        2 * (fourxm->tracks[fourxm->selected_track].channels);
290
                audio_frame_count /=
291
                      fourxm->tracks[fourxm->selected_track].channels;
292
                if (fourxm->tracks[fourxm->selected_track].adpcm)
293
                    audio_frame_count *= 2;
294 115329f1 Diego Biurrun
                else
295 d66dae57 Mike Melanson
                    audio_frame_count /=
296
                    (fourxm->tracks[fourxm->selected_track].bits / 8);
297 caf5fb95 Michael Niedermayer
                fourxm->audio_pts += audio_frame_count;
298 8e7284ba Mike Melanson
299 cef4ba9e Mike Melanson
            } else {
300
                url_fseek(pb, size, SEEK_CUR);
301
            }
302
            break;
303
304
        default:
305
            url_fseek(pb, size, SEEK_CUR);
306
            break;
307
        }
308
    }
309
    return ret;
310
}
311
312
static int fourxm_read_close(AVFormatContext *s)
313
{
314
    FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
315
316
    av_free(fourxm->tracks);
317
318
    return 0;
319
}
320
321
static AVInputFormat fourxm_iformat = {
322
    "4xm",
323
    "4X Technologies format",
324
    sizeof(FourxmDemuxContext),
325
    fourxm_probe,
326
    fourxm_read_header,
327
    fourxm_read_packet,
328
    fourxm_read_close,
329
};
330
331
int fourxm_init(void)
332
{
333
    av_register_input_format(&fourxm_iformat);
334
    return 0;
335
}