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)
92 if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
93 (LE_32(&p->buf[8]) != _4XMV_TAG))
96 return AVPROBE_SCORE_MAX;
99 static int fourxm_read_header(AVFormatContext *s,
100 AVFormatParameters *ap)
102 ByteIOContext *pb = &s->pb;
103 unsigned int fourcc_tag;
106 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
107 unsigned char *header;
109 int current_track = -1;
112 fourxm->track_count = 0;
113 fourxm->tracks = NULL;
114 fourxm->selected_track = 0;
116 /* skip the first 3 32-bit numbers */
117 url_fseek(pb, 12, SEEK_CUR);
119 /* check for LIST-HEAD */
121 header_size = size - 4;
122 if (fourcc_tag != HEAD_TAG)
123 return AVERROR_INVALIDDATA;
125 /* allocate space for the header and load the whole thing */
126 header = av_malloc(header_size);
128 return AVERROR_NOMEM;
129 if (get_buffer(pb, header, header_size) != header_size)
132 /* take the lazy approach and search for any and all vtrk and strk chunks */
133 for (i = 0; i < header_size - 8; i++) {
134 fourcc_tag = LE_32(&header[i]);
135 size = LE_32(&header[i + 4]);
137 if (fourcc_tag == vtrk_TAG) {
138 /* check that there is enough data */
139 if (size != vtrk_SIZE) {
141 return AVERROR_INVALIDDATA;
143 fourxm->width = LE_32(&header[i + 36]);
144 fourxm->height = LE_32(&header[i + 40]);
147 /* allocate a new AVStream */
148 st = av_new_stream(s, 0);
150 return AVERROR_NOMEM;
152 fourxm->video_stream_index = st->index;
154 st->codec.codec_type = CODEC_TYPE_VIDEO;
155 st->codec.codec_id = CODEC_ID_4XM;
156 st->codec.codec_tag = 0; /* no fourcc */
157 st->codec.width = fourxm->width;
158 st->codec.height = fourxm->height;
160 } else if (fourcc_tag == strk_TAG) {
161 /* check that there is enough data */
162 if (size != strk_SIZE) {
164 return AVERROR_INVALIDDATA;
166 current_track = LE_32(&header[i + 8]);
167 if (current_track + 1 > fourxm->track_count) {
168 fourxm->track_count = current_track + 1;
169 fourxm->tracks = av_realloc(fourxm->tracks,
170 fourxm->track_count * sizeof(AudioTrack));
171 if (!fourxm->tracks) {
173 return AVERROR_NOMEM;
176 fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
177 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
178 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
179 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
180 printf("bps:%d\n", fourxm->tracks[current_track].bits);
183 /* allocate a new AVStream */
184 st = av_new_stream(s, current_track);
186 return AVERROR_NOMEM;
188 fourxm->tracks[current_track].stream_index = st->index;
190 st->codec.codec_type = CODEC_TYPE_AUDIO;
191 st->codec.codec_tag = 1;
192 st->codec.channels = fourxm->tracks[current_track].channels;
193 st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
194 st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
195 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
196 st->codec.bits_per_sample;
197 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
198 if (fourxm->tracks[current_track].adpcm)
199 st->codec.codec_id = CODEC_ID_ADPCM_4XM;
200 else if (st->codec.bits_per_sample == 8)
201 st->codec.codec_id = CODEC_ID_PCM_U8;
203 st->codec.codec_id = CODEC_ID_PCM_S16LE;
209 /* skip over the LIST-MOVI chunk (which is where the stream should be */
211 if (fourcc_tag != MOVI_TAG)
212 return AVERROR_INVALIDDATA;
214 /* initialize context members */
216 fourxm->last_chunk_was_audio = 0;
217 fourxm->last_audio_frame_count = 0;
219 /* set the pts reference (1 pts = 1/90000) */
226 static int fourxm_read_packet(AVFormatContext *s,
229 FourxmDemuxContext *fourxm = s->priv_data;
230 ByteIOContext *pb = &s->pb;
231 unsigned int fourcc_tag;
232 unsigned int size, out_size;
236 unsigned char header[8];
239 while (!packet_read) {
241 if ((ret = get_buffer(&s->pb, header, 8)) < 0)
243 fourcc_tag = LE_32(&header[0]);
244 size = LE_32(&header[4]);
245 //printf(" %8X %c%c%c%c %d\n", fourcc_tag, fourcc_tag, fourcc_tag>>8, fourcc_tag>>16, fourcc_tag>>24, size);
248 switch (fourcc_tag) {
251 /* skip the LIST-* tag and move on to the next fourcc */
260 static int stats[1000];
262 /* bump the pts if this last data sent out was audio */
263 if (fourxm->last_chunk_was_audio) {
264 fourxm->last_chunk_was_audio = 0;
265 pts_inc = fourxm->last_audio_frame_count;
267 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
268 fourxm->pts += pts_inc;
271 /* allocate 8 more bytes than 'size' to account for fourcc
273 if (av_new_packet(pkt, size + 8))
275 pkt->stream_index = fourxm->video_stream_index;
276 pkt->pts = fourxm->pts;
277 memcpy(pkt->data, header, 8);
278 ret = get_buffer(&s->pb, &pkt->data[8], size);
280 if (fourcc_tag == cfrm_TAG) {
281 id = LE_32(&pkt->data[12]);
282 whole = LE_32(&pkt->data[16]);
283 stats[id] += size - 12;
284 //printf(" cfrm chunk id:%d size:%d whole:%d until now:%d\n", id, size, whole, stats[id]);
295 printf (" snd_ chunk, ");
296 track_number = get_le32(pb);
297 out_size= get_le32(pb);
300 if (track_number == fourxm->selected_track) {
301 printf ("correct track, dispatching...\n");
302 if (av_new_packet(pkt, size))
305 fourxm->tracks[fourxm->selected_track].stream_index;
306 pkt->pts = fourxm->pts;
307 ret = get_buffer(&s->pb, pkt->data, size);
313 /* maintain pts info for the benefit of the video track */
314 fourxm->last_chunk_was_audio = 1;
315 fourxm->last_audio_frame_count = size /
316 ((fourxm->tracks[fourxm->selected_track].bits / 8) *
317 fourxm->tracks[fourxm->selected_track].channels);
320 printf ("wrong track, skipping...\n");
321 url_fseek(pb, size, SEEK_CUR);
326 url_fseek(pb, size, SEEK_CUR);
333 static int fourxm_read_close(AVFormatContext *s)
335 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
337 av_free(fourxm->tracks);
342 static AVInputFormat fourxm_iformat = {
344 "4X Technologies format",
345 sizeof(FourxmDemuxContext),
352 int fourxm_init(void)
354 av_register_input_format(&fourxm_iformat);