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 {
76 typedef struct FourxmDemuxContext {
79 int video_stream_index;
85 int last_chunk_was_audio;
86 int last_audio_frame_count;
89 static int fourxm_probe(AVProbeData *p)
91 if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
92 (LE_32(&p->buf[8]) != _4XMV_TAG))
95 return AVPROBE_SCORE_MAX;
98 static int fourxm_read_header(AVFormatContext *s,
99 AVFormatParameters *ap)
101 ByteIOContext *pb = &s->pb;
102 unsigned int fourcc_tag;
105 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
106 unsigned char *header;
108 int current_track = -1;
111 fourxm->track_count = 0;
112 fourxm->tracks = NULL;
113 fourxm->selected_track = 0;
115 /* skip the first 3 32-bit numbers */
116 url_fseek(pb, 12, SEEK_CUR);
118 /* check for LIST-HEAD */
120 header_size = size - 4;
121 if (fourcc_tag != HEAD_TAG)
122 return AVERROR_INVALIDDATA;
124 /* allocate space for the header and load the whole thing */
125 header = av_malloc(header_size);
127 return AVERROR_NOMEM;
128 if (get_buffer(pb, header, header_size) != header_size)
131 /* take the lazy approach and search for any and all vtrk and strk chunks */
132 for (i = 0; i < header_size - 8; i++) {
133 fourcc_tag = LE_32(&header[i]);
134 size = LE_32(&header[i + 4]);
136 if (fourcc_tag == vtrk_TAG) {
137 /* check that there is enough data */
138 if (size != vtrk_SIZE) {
140 return AVERROR_INVALIDDATA;
142 fourxm->width = LE_32(&header[i + 36]);
143 fourxm->height = LE_32(&header[i + 40]);
146 /* allocate a new AVStream */
147 st = av_new_stream(s, 0);
149 return AVERROR_NOMEM;
151 fourxm->video_stream_index = st->index;
153 st->codec.codec_type = CODEC_TYPE_VIDEO;
154 st->codec.codec_id = CODEC_ID_4XM;
155 st->codec.codec_tag = 0; /* no fourcc */
156 st->codec.width = fourxm->width;
157 st->codec.height = fourxm->height;
159 } else if (fourcc_tag == strk_TAG) {
160 /* check that there is enough data */
161 if (size != strk_SIZE) {
163 return AVERROR_INVALIDDATA;
165 current_track = LE_32(&header[i + 8]);
166 if (current_track + 1 > fourxm->track_count) {
167 fourxm->track_count = current_track + 1;
168 fourxm->tracks = av_realloc(fourxm->tracks,
169 fourxm->track_count * sizeof(AudioTrack));
170 if (!fourxm->tracks) {
172 return AVERROR_NOMEM;
175 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
176 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
177 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
180 /* allocate a new AVStream */
181 st = av_new_stream(s, current_track);
183 return AVERROR_NOMEM;
185 fourxm->tracks[current_track].stream_index = st->index;
187 st->codec.codec_type = CODEC_TYPE_AUDIO;
188 st->codec.codec_tag = 1;
189 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 if (st->codec.bits_per_sample == 8)
196 st->codec.codec_id = CODEC_ID_PCM_U8;
198 st->codec.codec_id = CODEC_ID_PCM_S16LE;
204 /* skip over the LIST-MOVI chunk (which is where the stream should be */
206 if (fourcc_tag != MOVI_TAG)
207 return AVERROR_INVALIDDATA;
209 /* initialize context members */
211 fourxm->last_chunk_was_audio = 0;
212 fourxm->last_audio_frame_count = 0;
214 /* set the pts reference (1 pts = 1/90000) */
221 static int fourxm_read_packet(AVFormatContext *s,
224 FourxmDemuxContext *fourxm = s->priv_data;
225 ByteIOContext *pb = &s->pb;
226 unsigned int fourcc_tag;
231 unsigned char header[8];
234 while (!packet_read) {
236 if ((ret = get_buffer(&s->pb, header, 8)) < 0)
238 fourcc_tag = LE_32(&header[0]);
239 size = LE_32(&header[4]);
240 if (fourcc_tag == LIST_TAG) {
241 /* skip the LIST-FRAM tag and get the next fourcc */
243 fourcc_tag = get_le32(pb);
250 switch (fourcc_tag) {
257 static int stats[1000];
259 /* bump the pts if this last data sent out was audio */
260 if (fourxm->last_chunk_was_audio) {
261 fourxm->last_chunk_was_audio = 0;
262 pts_inc = fourxm->last_audio_frame_count;
264 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
265 fourxm->pts += pts_inc;
268 /* allocate 8 more bytes than 'size' to account for fourcc
270 if (av_new_packet(pkt, size + 8))
272 pkt->stream_index = fourxm->video_stream_index;
273 pkt->pts = fourxm->pts;
274 memcpy(pkt->data, header, 8);
275 ret = get_buffer(&s->pb, &pkt->data[8], size);
277 if (fourcc_tag == cfrm_TAG) {
278 id = LE_32(&pkt->data[12]);
279 whole = LE_32(&pkt->data[16]);
280 stats[id] += size - 12;
281 printf(" cfrm chunk id:%d size:%d whole:%d until now:%d\n", id, size, whole, stats[id]);
292 printf (" snd_ chunk, ");
293 track_number = get_le32(pb);
295 if (track_number == fourxm->selected_track) {
296 printf ("correct track, dispatching...\n");
297 if (av_new_packet(pkt, size))
300 fourxm->tracks[fourxm->selected_track].stream_index;
301 pkt->pts = fourxm->pts;
302 ret = get_buffer(&s->pb, pkt->data, size);
308 /* maintain pts info for the benefit of the video track */
309 fourxm->last_chunk_was_audio = 1;
310 fourxm->last_audio_frame_count = size /
311 ((fourxm->tracks[fourxm->selected_track].bits / 8) *
312 fourxm->tracks[fourxm->selected_track].channels);
315 printf ("wrong track, skipping...\n");
316 url_fseek(pb, size, SEEK_CUR);
321 url_fseek(pb, size, SEEK_CUR);
329 static int fourxm_read_close(AVFormatContext *s)
331 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
333 av_free(fourxm->tracks);
338 static AVInputFormat fourxm_iformat = {
340 "4X Technologies format",
341 sizeof(FourxmDemuxContext),
348 int fourxm_init(void)
350 av_register_input_format(&fourxm_iformat);