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 std__TAG FOURCC_TAG('s', 't', 'd', '_')
51 #define name_TAG FOURCC_TAG('n', 'a', 'm', 'e')
52 #define vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k')
53 #define strk_TAG FOURCC_TAG('s', 't', 'r', 'k')
54 #define ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm')
55 #define pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm')
56 #define cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm')
57 #define snd__TAG FOURCC_TAG('s', 'n', 'd', '_')
58 #define _TAG FOURCC_TAG('', '', '', '')
60 #define vtrk_SIZE 0x44
61 #define strk_SIZE 0x28
63 #define GET_LIST_HEADER() \
64 fourcc_tag = get_le32(pb); \
65 size = get_le32(pb); \
66 if (fourcc_tag != LIST_TAG) \
67 return AVERROR_INVALIDDATA; \
68 fourcc_tag = get_le32(pb);
70 typedef struct AudioTrack {
78 typedef struct FourxmDemuxContext {
81 int video_stream_index;
92 static float get_le_float(unsigned char *buffer)
95 unsigned char *float_buffer = (unsigned char *)&f;
97 #ifdef WORDS_BIGENDIAN
98 float_buffer[0] = buffer[3];
99 float_buffer[1] = buffer[2];
100 float_buffer[2] = buffer[1];
101 float_buffer[3] = buffer[0];
103 float_buffer[0] = buffer[0];
104 float_buffer[1] = buffer[1];
105 float_buffer[2] = buffer[2];
106 float_buffer[3] = buffer[3];
112 static int fourxm_probe(AVProbeData *p)
114 if (p->buf_size < 12)
117 if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
118 (LE_32(&p->buf[8]) != _4XMV_TAG))
121 return AVPROBE_SCORE_MAX;
124 static int fourxm_read_header(AVFormatContext *s,
125 AVFormatParameters *ap)
127 ByteIOContext *pb = &s->pb;
128 unsigned int fourcc_tag;
131 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
132 unsigned char *header;
134 int current_track = -1;
137 fourxm->track_count = 0;
138 fourxm->tracks = NULL;
139 fourxm->selected_track = 0;
142 /* skip the first 3 32-bit numbers */
143 url_fseek(pb, 12, SEEK_CUR);
145 /* check for LIST-HEAD */
147 header_size = size - 4;
148 if (fourcc_tag != HEAD_TAG)
149 return AVERROR_INVALIDDATA;
151 /* allocate space for the header and load the whole thing */
152 header = av_malloc(header_size);
154 return AVERROR_NOMEM;
155 if (get_buffer(pb, header, header_size) != header_size)
158 /* take the lazy approach and search for any and all vtrk and strk chunks */
159 for (i = 0; i < header_size - 8; i++) {
160 fourcc_tag = LE_32(&header[i]);
161 size = LE_32(&header[i + 4]);
163 if (fourcc_tag == std__TAG) {
164 fourxm->fps = get_le_float(&header[i + 12]);
165 fourxm->video_pts_inc = (int)(90000.0 / fourxm->fps);
166 } else if (fourcc_tag == vtrk_TAG) {
167 /* check that there is enough data */
168 if (size != vtrk_SIZE) {
170 return AVERROR_INVALIDDATA;
172 fourxm->width = LE_32(&header[i + 36]);
173 fourxm->height = LE_32(&header[i + 40]);
176 /* allocate a new AVStream */
177 st = av_new_stream(s, 0);
179 return AVERROR_NOMEM;
181 fourxm->video_stream_index = st->index;
183 st->codec.frame_rate = fourxm->fps;
184 st->codec.frame_rate_base = 1.0;
185 st->codec.codec_type = CODEC_TYPE_VIDEO;
186 st->codec.codec_id = CODEC_ID_4XM;
187 st->codec.codec_tag = 0; /* no fourcc */
188 st->codec.width = fourxm->width;
189 st->codec.height = fourxm->height;
191 } else if (fourcc_tag == strk_TAG) {
192 /* check that there is enough data */
193 if (size != strk_SIZE) {
195 return AVERROR_INVALIDDATA;
197 current_track = LE_32(&header[i + 8]);
198 if (current_track + 1 > fourxm->track_count) {
199 fourxm->track_count = current_track + 1;
200 fourxm->tracks = av_realloc(fourxm->tracks,
201 fourxm->track_count * sizeof(AudioTrack));
202 if (!fourxm->tracks) {
204 return AVERROR_NOMEM;
207 fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
208 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
209 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
210 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
213 /* allocate a new AVStream */
214 st = av_new_stream(s, current_track);
216 return AVERROR_NOMEM;
218 fourxm->tracks[current_track].stream_index = st->index;
220 st->codec.codec_type = CODEC_TYPE_AUDIO;
221 st->codec.codec_tag = 1;
222 st->codec.channels = fourxm->tracks[current_track].channels;
223 st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
224 st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
225 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
226 st->codec.bits_per_sample;
227 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
228 if (fourxm->tracks[current_track].adpcm)
229 st->codec.codec_id = CODEC_ID_ADPCM_4XM;
230 else if (st->codec.bits_per_sample == 8)
231 st->codec.codec_id = CODEC_ID_PCM_U8;
233 st->codec.codec_id = CODEC_ID_PCM_S16LE;
239 /* skip over the LIST-MOVI chunk (which is where the stream should be */
241 if (fourcc_tag != MOVI_TAG)
242 return AVERROR_INVALIDDATA;
244 /* initialize context members */
245 fourxm->video_pts = -fourxm->video_pts_inc; /* first frame will push to 0 */
246 fourxm->audio_pts = 0;
248 /* set the pts reference (1 pts = 1/90000) */
255 static int fourxm_read_packet(AVFormatContext *s,
258 FourxmDemuxContext *fourxm = s->priv_data;
259 ByteIOContext *pb = &s->pb;
260 unsigned int fourcc_tag;
261 unsigned int size, out_size;
265 unsigned char header[8];
267 int audio_frame_count;
269 while (!packet_read) {
271 if ((ret = get_buffer(&s->pb, header, 8)) < 0)
273 fourcc_tag = LE_32(&header[0]);
274 size = LE_32(&header[4]);
277 switch (fourcc_tag) {
280 /* this is a good time to bump the video pts */
281 fourxm->video_pts += fourxm->video_pts_inc;
283 /* skip the LIST-* tag and move on to the next fourcc */
291 /* allocate 8 more bytes than 'size' to account for fourcc
293 if (av_new_packet(pkt, size + 8))
295 pkt->stream_index = fourxm->video_stream_index;
296 pkt->pts = fourxm->video_pts;
297 memcpy(pkt->data, header, 8);
298 ret = get_buffer(&s->pb, &pkt->data[8], size);
308 track_number = get_le32(pb);
309 out_size= get_le32(pb);
312 if (track_number == fourxm->selected_track) {
313 if (av_new_packet(pkt, size))
316 fourxm->tracks[fourxm->selected_track].stream_index;
317 pkt->pts = fourxm->audio_pts;
318 ret = get_buffer(&s->pb, pkt->data, size);
325 audio_frame_count = size;
326 if (fourxm->tracks[fourxm->selected_track].adpcm)
328 2 * (fourxm->tracks[fourxm->selected_track].channels);
330 fourxm->tracks[fourxm->selected_track].channels;
331 if (fourxm->tracks[fourxm->selected_track].adpcm)
332 audio_frame_count *= 2;
335 (fourxm->tracks[fourxm->selected_track].bits / 8);
336 pts_inc = audio_frame_count;
338 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
339 fourxm->audio_pts += pts_inc;
342 url_fseek(pb, size, SEEK_CUR);
347 url_fseek(pb, size, SEEK_CUR);
354 static int fourxm_read_close(AVFormatContext *s)
356 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
358 av_free(fourxm->tracks);
363 static AVInputFormat fourxm_iformat = {
365 "4X Technologies format",
366 sizeof(FourxmDemuxContext),
373 int fourxm_init(void)
375 av_register_input_format(&fourxm_iformat);