2
* 4X Technologies .4xm File Demuxer (no muxer)
3
* Copyright (c) 2003 The ffmpeg Project
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.
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.
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
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/
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) | \
36
#define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
37
( (long)(unsigned char)(ch0) | \
38
( (long)(unsigned char)(ch1) << 8 ) | \
39
( (long)(unsigned char)(ch2) << 16 ) | \
40
( (long)(unsigned char)(ch3) << 24 ) )
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('', '', '', '')
59
#define vtrk_SIZE 0x44
60
#define strk_SIZE 0x28
62
#define GET_LIST_HEADER() \
63
fourcc_tag = get_le32(pb); \
64
size = get_le32(pb); \
65
if (fourcc_tag != LIST_TAG) \
66
return AVERROR_INVALIDDATA; \
67
fourcc_tag = get_le32(pb);
69
typedef struct AudioTrack {
77
typedef struct FourxmDemuxContext {
80
int video_stream_index;
86
int last_chunk_was_audio;
87
int last_audio_frame_count;
90
static int fourxm_probe(AVProbeData *p)
95
if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
96
(LE_32(&p->buf[8]) != _4XMV_TAG))
99
return AVPROBE_SCORE_MAX;
102
static int fourxm_read_header(AVFormatContext *s,
103
AVFormatParameters *ap)
105
ByteIOContext *pb = &s->pb;
106
unsigned int fourcc_tag;
109
FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
110
unsigned char *header;
112
int current_track = -1;
115
fourxm->track_count = 0;
116
fourxm->tracks = NULL;
117
fourxm->selected_track = 0;
119
/* skip the first 3 32-bit numbers */
120
url_fseek(pb, 12, SEEK_CUR);
122
/* check for LIST-HEAD */
124
header_size = size - 4;
125
if (fourcc_tag != HEAD_TAG)
126
return AVERROR_INVALIDDATA;
128
/* allocate space for the header and load the whole thing */
129
header = av_malloc(header_size);
131
return AVERROR_NOMEM;
132
if (get_buffer(pb, header, header_size) != header_size)
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
fourcc_tag = LE_32(&header[i]);
138
size = LE_32(&header[i + 4]);
140
if (fourcc_tag == vtrk_TAG) {
141
/* check that there is enough data */
142
if (size != vtrk_SIZE) {
144
return AVERROR_INVALIDDATA;
146
fourxm->width = LE_32(&header[i + 36]);
147
fourxm->height = LE_32(&header[i + 40]);
150
/* allocate a new AVStream */
151
st = av_new_stream(s, 0);
153
return AVERROR_NOMEM;
155
fourxm->video_stream_index = st->index;
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;
163
} else if (fourcc_tag == strk_TAG) {
164
/* check that there is enough data */
165
if (size != strk_SIZE) {
167
return AVERROR_INVALIDDATA;
169
current_track = LE_32(&header[i + 8]);
170
if (current_track + 1 > fourxm->track_count) {
171
fourxm->track_count = current_track + 1;
172
fourxm->tracks = av_realloc(fourxm->tracks,
173
fourxm->track_count * sizeof(AudioTrack));
174
if (!fourxm->tracks) {
176
return AVERROR_NOMEM;
179
fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
180
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
printf("bps:%d\n", fourxm->tracks[current_track].bits);
186
/* allocate a new AVStream */
187
st = av_new_stream(s, current_track);
189
return AVERROR_NOMEM;
191
fourxm->tracks[current_track].stream_index = st->index;
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
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
st->codec.codec_id = CODEC_ID_PCM_U8;
206
st->codec.codec_id = CODEC_ID_PCM_S16LE;
212
/* skip over the LIST-MOVI chunk (which is where the stream should be */
214
if (fourcc_tag != MOVI_TAG)
215
return AVERROR_INVALIDDATA;
217
/* initialize context members */
219
fourxm->last_chunk_was_audio = 0;
220
fourxm->last_audio_frame_count = 0;
222
/* set the pts reference (1 pts = 1/90000) */
229
static int fourxm_read_packet(AVFormatContext *s,
232
FourxmDemuxContext *fourxm = s->priv_data;
233
ByteIOContext *pb = &s->pb;
234
unsigned int fourcc_tag;
235
unsigned int size, out_size;
239
unsigned char header[8];
242
while (!packet_read) {
244
if ((ret = get_buffer(&s->pb, header, 8)) < 0)
246
fourcc_tag = LE_32(&header[0]);
247
size = LE_32(&header[4]);
250
switch (fourcc_tag) {
253
/* skip the LIST-* tag and move on to the next fourcc */
261
/* bump the pts if the last data sent out was audio */
262
if (fourxm->last_chunk_was_audio) {
263
fourxm->last_chunk_was_audio = 0;
264
pts_inc = fourxm->last_audio_frame_count;
266
pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
267
fourxm->pts += pts_inc;
270
/* allocate 8 more bytes than 'size' to account for fourcc
272
if (av_new_packet(pkt, size + 8))
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);
287
track_number = get_le32(pb);
288
out_size= get_le32(pb);
291
if (track_number == fourxm->selected_track) {
292
if (av_new_packet(pkt, size))
295
fourxm->tracks[fourxm->selected_track].stream_index;
296
pkt->pts = fourxm->pts;
297
ret = get_buffer(&s->pb, pkt->data, size);
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);
310
url_fseek(pb, size, SEEK_CUR);
315
url_fseek(pb, size, SEEK_CUR);
322
static int fourxm_read_close(AVFormatContext *s)
324
FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
326
av_free(fourxm->tracks);
331
static AVInputFormat fourxm_iformat = {
333
"4X Technologies format",
334
sizeof(FourxmDemuxContext),
341
int fourxm_init(void)
343
av_register_input_format(&fourxm_iformat);