Statistics
| Branch: | Revision:

ffmpeg / libavformat / 4xm.c @ 756fb7fe

History | View | Annotate | Download (10.3 KB)

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