Statistics
| Branch: | Revision:

ffmpeg / libavformat / 4xm.c @ 470bce2b

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 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 470bce2b Diego Biurrun
#define     RIFF_TAG MKTAG('R', 'I', 'F', 'F')
33 c54286ab Diego Biurrun
#define  FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
34 470bce2b Diego Biurrun
#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     ifr2_TAG MKTAG('i', 'f', 'r', '2')
48
#define     pfr2_TAG MKTAG('p', 'f', 'r', '2')
49
#define     cfr2_TAG MKTAG('c', 'f', 'r', '2')
50
#define     snd__TAG MKTAG('s', 'n', 'd', '_')
51 cef4ba9e Mike Melanson
52
#define vtrk_SIZE 0x44
53
#define strk_SIZE 0x28
54
55
#define GET_LIST_HEADER() \
56 8e7284ba Mike Melanson
    fourcc_tag = get_le32(pb); \
57 cef4ba9e Mike Melanson
    size = get_le32(pb); \
58
    if (fourcc_tag != LIST_TAG) \
59
        return AVERROR_INVALIDDATA; \
60 8e7284ba Mike Melanson
    fourcc_tag = get_le32(pb);
61 cef4ba9e Mike Melanson
62
typedef struct AudioTrack {
63
    int sample_rate;
64
    int bits;
65
    int channels;
66 8e7284ba Mike Melanson
    int stream_index;
67 4b465299 Michael Niedermayer
    int adpcm;
68 cef4ba9e Mike Melanson
} AudioTrack;
69
70
typedef struct FourxmDemuxContext {
71
    int width;
72
    int height;
73 8e7284ba Mike Melanson
    int video_stream_index;
74 cef4ba9e Mike Melanson
    int track_count;
75
    AudioTrack *tracks;
76
    int selected_track;
77 8e7284ba Mike Melanson
78 d66dae57 Mike Melanson
    int64_t audio_pts;
79
    int64_t video_pts;
80 11498da3 Mike Melanson
    float fps;
81 cef4ba9e Mike Melanson
} FourxmDemuxContext;
82
83
static int fourxm_probe(AVProbeData *p)
84
{
85 fead30d4 Alex Beregszaszi
    if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
86 c54286ab Diego Biurrun
        (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
87 cef4ba9e Mike Melanson
        return 0;
88
89
    return AVPROBE_SCORE_MAX;
90
}
91
92
static int fourxm_read_header(AVFormatContext *s,
93 8e7284ba Mike Melanson
                              AVFormatParameters *ap)
94 cef4ba9e Mike Melanson
{
95 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
96 cef4ba9e Mike Melanson
    unsigned int fourcc_tag;
97
    unsigned int size;
98
    int header_size;
99 e4141433 Nicholas Tung
    FourxmDemuxContext *fourxm = s->priv_data;
100 cef4ba9e Mike Melanson
    unsigned char *header;
101
    int i;
102
    int current_track = -1;
103
    AVStream *st;
104
105
    fourxm->track_count = 0;
106
    fourxm->tracks = NULL;
107
    fourxm->selected_track = 0;
108 11498da3 Mike Melanson
    fourxm->fps = 1.0;
109 cef4ba9e Mike Melanson
110
    /* skip the first 3 32-bit numbers */
111
    url_fseek(pb, 12, SEEK_CUR);
112
113
    /* check for LIST-HEAD */
114
    GET_LIST_HEADER();
115
    header_size = size - 4;
116
    if (fourcc_tag != HEAD_TAG)
117
        return AVERROR_INVALIDDATA;
118
119
    /* allocate space for the header and load the whole thing */
120
    header = av_malloc(header_size);
121
    if (!header)
122 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
123 cef4ba9e Mike Melanson
    if (get_buffer(pb, header, header_size) != header_size)
124 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
125 cef4ba9e Mike Melanson
126
    /* take the lazy approach and search for any and all vtrk and strk chunks */
127
    for (i = 0; i < header_size - 8; i++) {
128 fead30d4 Alex Beregszaszi
        fourcc_tag = AV_RL32(&header[i]);
129
        size = AV_RL32(&header[i + 4]);
130 cef4ba9e Mike Melanson
131 d66dae57 Mike Melanson
        if (fourcc_tag == std__TAG) {
132 fead30d4 Alex Beregszaszi
            fourxm->fps = av_int2flt(AV_RL32(&header[i + 12]));
133 d66dae57 Mike Melanson
        } else if (fourcc_tag == vtrk_TAG) {
134 cef4ba9e Mike Melanson
            /* check that there is enough data */
135
            if (size != vtrk_SIZE) {
136
                av_free(header);
137
                return AVERROR_INVALIDDATA;
138
            }
139 fead30d4 Alex Beregszaszi
            fourxm->width = AV_RL32(&header[i + 36]);
140
            fourxm->height = AV_RL32(&header[i + 40]);
141 8e7284ba Mike Melanson
142
            /* allocate a new AVStream */
143
            st = av_new_stream(s, 0);
144
            if (!st)
145 769e10f0 Panagiotis Issaris
                return AVERROR(ENOMEM);
146 caf5fb95 Michael Niedermayer
            av_set_pts_info(st, 60, 1, fourxm->fps);
147 8e7284ba Mike Melanson
148
            fourxm->video_stream_index = st->index;
149
150 01f4895c Michael Niedermayer
            st->codec->codec_type = CODEC_TYPE_VIDEO;
151
            st->codec->codec_id = CODEC_ID_4XM;
152 b6f508bb Roberto Togni
            st->codec->extradata_size = 4;
153
            st->codec->extradata = av_malloc(4);
154
            AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
155 01f4895c Michael Niedermayer
            st->codec->width = fourxm->width;
156
            st->codec->height = fourxm->height;
157 8e7284ba Mike Melanson
158 a985a940 Michael Niedermayer
            i += 8 + size;
159 cef4ba9e Mike Melanson
        } else if (fourcc_tag == strk_TAG) {
160
            /* check that there is enough data */
161
            if (size != strk_SIZE) {
162
                av_free(header);
163
                return AVERROR_INVALIDDATA;
164
            }
165 fead30d4 Alex Beregszaszi
            current_track = AV_RL32(&header[i + 8]);
166 cef4ba9e Mike Melanson
            if (current_track + 1 > fourxm->track_count) {
167 5a634a69 Mike Melanson
                fourxm->track_count = current_track + 1;
168 568e18b1 Michael Niedermayer
                if((unsigned)fourxm->track_count >= UINT_MAX / sizeof(AudioTrack))
169
                    return -1;
170 115329f1 Diego Biurrun
                fourxm->tracks = av_realloc(fourxm->tracks,
171 5a634a69 Mike Melanson
                    fourxm->track_count * sizeof(AudioTrack));
172 cef4ba9e Mike Melanson
                if (!fourxm->tracks) {
173
                    av_free(header);
174 769e10f0 Panagiotis Issaris
                    return AVERROR(ENOMEM);
175 cef4ba9e Mike Melanson
                }
176
            }
177 fead30d4 Alex Beregszaszi
            fourxm->tracks[current_track].adpcm = AV_RL32(&header[i + 12]);
178
            fourxm->tracks[current_track].channels = AV_RL32(&header[i + 36]);
179
            fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
180
            fourxm->tracks[current_track].bits = AV_RL32(&header[i + 44]);
181 cef4ba9e Mike Melanson
            i += 8 + size;
182 8e7284ba Mike Melanson
183
            /* allocate a new AVStream */
184
            st = av_new_stream(s, current_track);
185
            if (!st)
186 769e10f0 Panagiotis Issaris
                return AVERROR(ENOMEM);
187 8e7284ba Mike Melanson
188 caf5fb95 Michael Niedermayer
            av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
189 9ee91c2f Michael Niedermayer
190 8e7284ba Mike Melanson
            fourxm->tracks[current_track].stream_index = st->index;
191
192 01f4895c Michael Niedermayer
            st->codec->codec_type = CODEC_TYPE_AUDIO;
193 899af1a2 Diego Biurrun
            st->codec->codec_tag = 0;
194 01f4895c Michael Niedermayer
            st->codec->channels = fourxm->tracks[current_track].channels;
195
            st->codec->sample_rate = fourxm->tracks[current_track].sample_rate;
196 dd1c8f3e Luca Abeni
            st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
197 01f4895c Michael Niedermayer
            st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
198 dd1c8f3e Luca Abeni
                st->codec->bits_per_coded_sample;
199
            st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
200 4b465299 Michael Niedermayer
            if (fourxm->tracks[current_track].adpcm)
201 01f4895c Michael Niedermayer
                st->codec->codec_id = CODEC_ID_ADPCM_4XM;
202 dd1c8f3e Luca Abeni
            else if (st->codec->bits_per_coded_sample == 8)
203 01f4895c Michael Niedermayer
                st->codec->codec_id = CODEC_ID_PCM_U8;
204 8e7284ba Mike Melanson
            else
205 01f4895c Michael Niedermayer
                st->codec->codec_id = CODEC_ID_PCM_S16LE;
206 cef4ba9e Mike Melanson
        }
207
    }
208
209
    av_free(header);
210
211
    /* skip over the LIST-MOVI chunk (which is where the stream should be */
212
    GET_LIST_HEADER();
213
    if (fourcc_tag != MOVI_TAG)
214
        return AVERROR_INVALIDDATA;
215
216 8e7284ba Mike Melanson
    /* initialize context members */
217 caf5fb95 Michael Niedermayer
    fourxm->video_pts = -1;  /* first frame will push to 0 */
218 d66dae57 Mike Melanson
    fourxm->audio_pts = 0;
219 8e7284ba Mike Melanson
220 cef4ba9e Mike Melanson
    return 0;
221
}
222
223
static int fourxm_read_packet(AVFormatContext *s,
224 8e7284ba Mike Melanson
                              AVPacket *pkt)
225 cef4ba9e Mike Melanson
{
226
    FourxmDemuxContext *fourxm = s->priv_data;
227 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
228 cef4ba9e Mike Melanson
    unsigned int fourcc_tag;
229 4b465299 Michael Niedermayer
    unsigned int size, out_size;
230 cef4ba9e Mike Melanson
    int ret = 0;
231
    int track_number;
232
    int packet_read = 0;
233 8e7284ba Mike Melanson
    unsigned char header[8];
234 d66dae57 Mike Melanson
    int audio_frame_count;
235 cef4ba9e Mike Melanson
236
    while (!packet_read) {
237
238 899681cd Björn Axelsson
        if ((ret = get_buffer(s->pb, header, 8)) < 0)
239 8e7284ba Mike Melanson
            return ret;
240 fead30d4 Alex Beregszaszi
        fourcc_tag = AV_RL32(&header[0]);
241
        size = AV_RL32(&header[4]);
242 cef4ba9e Mike Melanson
        if (url_feof(pb))
243 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
244 cef4ba9e Mike Melanson
        switch (fourcc_tag) {
245
246 73a19b2d Mike Melanson
        case LIST_TAG:
247 d66dae57 Mike Melanson
            /* this is a good time to bump the video pts */
248 caf5fb95 Michael Niedermayer
            fourxm->video_pts ++;
249 d66dae57 Mike Melanson
250 73a19b2d Mike Melanson
            /* skip the LIST-* tag and move on to the next fourcc */
251
            get_le32(pb);
252
            break;
253
254 cef4ba9e Mike Melanson
        case ifrm_TAG:
255
        case pfrm_TAG:
256 07870f85 Michael Niedermayer
        case cfrm_TAG:
257
        case ifr2_TAG:
258
        case pfr2_TAG:
259
        case cfr2_TAG:
260
        {
261 8e7284ba Mike Melanson
262
            /* allocate 8 more bytes than 'size' to account for fourcc
263
             * and size */
264 0ecca7a4 Michael Niedermayer
            if (size + 8 < size || av_new_packet(pkt, size + 8))
265 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
266 8e7284ba Mike Melanson
            pkt->stream_index = fourxm->video_stream_index;
267 d66dae57 Mike Melanson
            pkt->pts = fourxm->video_pts;
268 899681cd Björn Axelsson
            pkt->pos = url_ftell(s->pb);
269 8e7284ba Mike Melanson
            memcpy(pkt->data, header, 8);
270 899681cd Björn Axelsson
            ret = get_buffer(s->pb, &pkt->data[8], size);
271 8e7284ba Mike Melanson
272
            if (ret < 0)
273
                av_free_packet(pkt);
274
            else
275
                packet_read = 1;
276 4a106616 Michael Niedermayer
            break;
277
        }
278 8e7284ba Mike Melanson
279 cef4ba9e Mike Melanson
        case snd__TAG:
280
            track_number = get_le32(pb);
281 4b465299 Michael Niedermayer
            out_size= get_le32(pb);
282
            size-=8;
283
284 cef4ba9e Mike Melanson
            if (track_number == fourxm->selected_track) {
285 899681cd Björn Axelsson
                ret= av_get_packet(s->pb, pkt, size);
286 2692067a Michael Niedermayer
                if(ret<0)
287 6f3e0b21 Panagiotis Issaris
                    return AVERROR(EIO);
288 115329f1 Diego Biurrun
                pkt->stream_index =
289 8e7284ba Mike Melanson
                    fourxm->tracks[fourxm->selected_track].stream_index;
290 d66dae57 Mike Melanson
                pkt->pts = fourxm->audio_pts;
291 2692067a Michael Niedermayer
                packet_read = 1;
292 8e7284ba Mike Melanson
293 d66dae57 Mike Melanson
                /* pts accounting */
294
                audio_frame_count = size;
295
                if (fourxm->tracks[fourxm->selected_track].adpcm)
296 115329f1 Diego Biurrun
                    audio_frame_count -=
297 d66dae57 Mike Melanson
                        2 * (fourxm->tracks[fourxm->selected_track].channels);
298
                audio_frame_count /=
299
                      fourxm->tracks[fourxm->selected_track].channels;
300
                if (fourxm->tracks[fourxm->selected_track].adpcm)
301
                    audio_frame_count *= 2;
302 115329f1 Diego Biurrun
                else
303 d66dae57 Mike Melanson
                    audio_frame_count /=
304
                    (fourxm->tracks[fourxm->selected_track].bits / 8);
305 caf5fb95 Michael Niedermayer
                fourxm->audio_pts += audio_frame_count;
306 8e7284ba Mike Melanson
307 cef4ba9e Mike Melanson
            } else {
308
                url_fseek(pb, size, SEEK_CUR);
309
            }
310
            break;
311
312
        default:
313
            url_fseek(pb, size, SEEK_CUR);
314
            break;
315
        }
316
    }
317
    return ret;
318
}
319
320
static int fourxm_read_close(AVFormatContext *s)
321
{
322 e4141433 Nicholas Tung
    FourxmDemuxContext *fourxm = s->priv_data;
323 cef4ba9e Mike Melanson
324
    av_free(fourxm->tracks);
325
326
    return 0;
327
}
328
329 ff70e601 Måns Rullgård
AVInputFormat fourxm_demuxer = {
330 cef4ba9e Mike Melanson
    "4xm",
331 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("4X Technologies format"),
332 cef4ba9e Mike Melanson
    sizeof(FourxmDemuxContext),
333
    fourxm_probe,
334
    fourxm_read_header,
335
    fourxm_read_packet,
336
    fourxm_read_close,
337
};