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;
91 static float get_le_float(unsigned char *buffer)
94 unsigned char *float_buffer = (unsigned char *)&f;
96 #ifdef WORDS_BIGENDIAN
97 float_buffer[0] = buffer[3];
98 float_buffer[1] = buffer[2];
99 float_buffer[2] = buffer[1];
100 float_buffer[3] = buffer[0];
102 float_buffer[0] = buffer[0];
103 float_buffer[1] = buffer[1];
104 float_buffer[2] = buffer[2];
105 float_buffer[3] = buffer[3];
111 static int fourxm_probe(AVProbeData *p)
113 if (p->buf_size < 12)
116 if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
117 (LE_32(&p->buf[8]) != _4XMV_TAG))
120 return AVPROBE_SCORE_MAX;
123 static int fourxm_read_header(AVFormatContext *s,
124 AVFormatParameters *ap)
126 ByteIOContext *pb = &s->pb;
127 unsigned int fourcc_tag;
130 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
131 unsigned char *header;
133 int current_track = -1;
137 fourxm->track_count = 0;
138 fourxm->tracks = NULL;
139 fourxm->selected_track = 0;
141 /* skip the first 3 32-bit numbers */
142 url_fseek(pb, 12, SEEK_CUR);
144 /* check for LIST-HEAD */
146 header_size = size - 4;
147 if (fourcc_tag != HEAD_TAG)
148 return AVERROR_INVALIDDATA;
150 /* allocate space for the header and load the whole thing */
151 header = av_malloc(header_size);
153 return AVERROR_NOMEM;
154 if (get_buffer(pb, header, header_size) != header_size)
157 /* take the lazy approach and search for any and all vtrk and strk chunks */
158 for (i = 0; i < header_size - 8; i++) {
159 fourcc_tag = LE_32(&header[i]);
160 size = LE_32(&header[i + 4]);
162 if (fourcc_tag == std__TAG) {
163 fps = get_le_float(&header[i + 12]);
164 fourxm->video_pts_inc = (int)(90000.0 / fps);
165 } else if (fourcc_tag == vtrk_TAG) {
166 /* check that there is enough data */
167 if (size != vtrk_SIZE) {
169 return AVERROR_INVALIDDATA;
171 fourxm->width = LE_32(&header[i + 36]);
172 fourxm->height = LE_32(&header[i + 40]);
175 /* allocate a new AVStream */
176 st = av_new_stream(s, 0);
178 return AVERROR_NOMEM;
180 fourxm->video_stream_index = st->index;
182 st->codec.codec_type = CODEC_TYPE_VIDEO;
183 st->codec.codec_id = CODEC_ID_4XM;
184 st->codec.codec_tag = 0; /* no fourcc */
185 st->codec.width = fourxm->width;
186 st->codec.height = fourxm->height;
188 } else if (fourcc_tag == strk_TAG) {
189 /* check that there is enough data */
190 if (size != strk_SIZE) {
192 return AVERROR_INVALIDDATA;
194 current_track = LE_32(&header[i + 8]);
195 if (current_track + 1 > fourxm->track_count) {
196 fourxm->track_count = current_track + 1;
197 fourxm->tracks = av_realloc(fourxm->tracks,
198 fourxm->track_count * sizeof(AudioTrack));
199 if (!fourxm->tracks) {
201 return AVERROR_NOMEM;
204 fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
205 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
206 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
207 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
210 /* allocate a new AVStream */
211 st = av_new_stream(s, current_track);
213 return AVERROR_NOMEM;
215 fourxm->tracks[current_track].stream_index = st->index;
217 st->codec.codec_type = CODEC_TYPE_AUDIO;
218 st->codec.codec_tag = 1;
219 st->codec.channels = fourxm->tracks[current_track].channels;
220 st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
221 st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
222 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
223 st->codec.bits_per_sample;
224 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
225 if (fourxm->tracks[current_track].adpcm)
226 st->codec.codec_id = CODEC_ID_ADPCM_4XM;
227 else if (st->codec.bits_per_sample == 8)
228 st->codec.codec_id = CODEC_ID_PCM_U8;
230 st->codec.codec_id = CODEC_ID_PCM_S16LE;
236 /* skip over the LIST-MOVI chunk (which is where the stream should be */
238 if (fourcc_tag != MOVI_TAG)
239 return AVERROR_INVALIDDATA;
241 /* initialize context members */
242 fourxm->video_pts = -fourxm->video_pts_inc; /* first frame will push to 0 */
243 fourxm->audio_pts = 0;
245 /* set the pts reference (1 pts = 1/90000) */
252 static int fourxm_read_packet(AVFormatContext *s,
255 FourxmDemuxContext *fourxm = s->priv_data;
256 ByteIOContext *pb = &s->pb;
257 unsigned int fourcc_tag;
258 unsigned int size, out_size;
262 unsigned char header[8];
264 int audio_frame_count;
266 while (!packet_read) {
268 if ((ret = get_buffer(&s->pb, header, 8)) < 0)
270 fourcc_tag = LE_32(&header[0]);
271 size = LE_32(&header[4]);
274 switch (fourcc_tag) {
277 /* this is a good time to bump the video pts */
278 fourxm->video_pts += fourxm->video_pts_inc;
280 /* skip the LIST-* tag and move on to the next fourcc */
288 /* allocate 8 more bytes than 'size' to account for fourcc
290 if (av_new_packet(pkt, size + 8))
292 pkt->stream_index = fourxm->video_stream_index;
293 pkt->pts = fourxm->video_pts;
294 memcpy(pkt->data, header, 8);
295 ret = get_buffer(&s->pb, &pkt->data[8], size);
305 track_number = get_le32(pb);
306 out_size= get_le32(pb);
309 if (track_number == fourxm->selected_track) {
310 if (av_new_packet(pkt, size))
313 fourxm->tracks[fourxm->selected_track].stream_index;
314 pkt->pts = fourxm->audio_pts;
315 ret = get_buffer(&s->pb, pkt->data, size);
322 audio_frame_count = size;
323 if (fourxm->tracks[fourxm->selected_track].adpcm)
325 2 * (fourxm->tracks[fourxm->selected_track].channels);
327 fourxm->tracks[fourxm->selected_track].channels;
328 if (fourxm->tracks[fourxm->selected_track].adpcm)
329 audio_frame_count *= 2;
332 (fourxm->tracks[fourxm->selected_track].bits / 8);
333 pts_inc = audio_frame_count;
335 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
336 fourxm->audio_pts += pts_inc;
339 url_fseek(pb, size, SEEK_CUR);
344 url_fseek(pb, size, SEEK_CUR);
351 static int fourxm_read_close(AVFormatContext *s)
353 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
355 av_free(fourxm->tracks);
360 static AVInputFormat fourxm_iformat = {
362 "4X Technologies format",
363 sizeof(FourxmDemuxContext),
370 int fourxm_init(void)
372 av_register_input_format(&fourxm_iformat);