2 * 4X Technologies .4xm File Demuxer (no muxer)
3 * Copyright (c) 2003 The ffmpeg Project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * 4X Technologies file demuxer
25 * by Mike Melanson (melanson@pcisys.net)
26 * for more information on the .4xm file format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
32 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
33 #define _4XMV_TAG MKTAG('4', 'X', 'M', 'V')
34 #define LIST_TAG MKTAG('L', 'I', 'S', 'T')
35 #define HEAD_TAG MKTAG('H', 'E', 'A', 'D')
36 #define TRK__TAG MKTAG('T', 'R', 'K', '_')
37 #define MOVI_TAG MKTAG('M', 'O', 'V', 'I')
38 #define VTRK_TAG MKTAG('V', 'T', 'R', 'K')
39 #define STRK_TAG MKTAG('S', 'T', 'R', 'K')
40 #define std__TAG MKTAG('s', 't', 'd', '_')
41 #define name_TAG MKTAG('n', 'a', 'm', 'e')
42 #define vtrk_TAG MKTAG('v', 't', 'r', 'k')
43 #define strk_TAG MKTAG('s', 't', 'r', 'k')
44 #define ifrm_TAG MKTAG('i', 'f', 'r', 'm')
45 #define pfrm_TAG MKTAG('p', 'f', 'r', 'm')
46 #define cfrm_TAG MKTAG('c', 'f', 'r', 'm')
47 #define ifr2_TAG MKTAG('i', 'f', 'r', '2')
48 #define pfr2_TAG MKTAG('p', 'f', 'r', '2')
49 #define cfr2_TAG MKTAG('c', 'f', 'r', '2')
50 #define snd__TAG MKTAG('s', 'n', 'd', '_')
52 #define vtrk_SIZE 0x44
53 #define strk_SIZE 0x28
55 #define GET_LIST_HEADER() \
56 fourcc_tag = get_le32(pb); \
57 size = get_le32(pb); \
58 if (fourcc_tag != LIST_TAG) \
59 return AVERROR_INVALIDDATA; \
60 fourcc_tag = get_le32(pb);
62 typedef struct AudioTrack
{
70 typedef struct FourxmDemuxContext
{
73 int video_stream_index
;
83 static int fourxm_probe(AVProbeData
*p
)
85 if ((AV_RL32(&p
->buf
[0]) != RIFF_TAG
) ||
86 (AV_RL32(&p
->buf
[8]) != _4XMV_TAG
))
89 return AVPROBE_SCORE_MAX
;
92 static int fourxm_read_header(AVFormatContext
*s
,
93 AVFormatParameters
*ap
)
95 ByteIOContext
*pb
= s
->pb
;
96 unsigned int fourcc_tag
;
99 FourxmDemuxContext
*fourxm
= s
->priv_data
;
100 unsigned char *header
;
102 int current_track
= -1;
105 fourxm
->track_count
= 0;
106 fourxm
->tracks
= NULL
;
107 fourxm
->selected_track
= 0;
110 /* skip the first 3 32-bit numbers */
111 url_fseek(pb
, 12, SEEK_CUR
);
113 /* check for LIST-HEAD */
115 header_size
= size
- 4;
116 if (fourcc_tag
!= HEAD_TAG
)
117 return AVERROR_INVALIDDATA
;
119 /* allocate space for the header and load the whole thing */
120 header
= av_malloc(header_size
);
122 return AVERROR(ENOMEM
);
123 if (get_buffer(pb
, header
, header_size
) != header_size
)
126 /* take the lazy approach and search for any and all vtrk and strk chunks */
127 for (i
= 0; i
< header_size
- 8; i
++) {
128 fourcc_tag
= AV_RL32(&header
[i
]);
129 size
= AV_RL32(&header
[i
+ 4]);
131 if (fourcc_tag
== std__TAG
) {
132 fourxm
->fps
= av_int2flt(AV_RL32(&header
[i
+ 12]));
133 } else if (fourcc_tag
== vtrk_TAG
) {
134 /* check that there is enough data */
135 if (size
!= vtrk_SIZE
) {
137 return AVERROR_INVALIDDATA
;
139 fourxm
->width
= AV_RL32(&header
[i
+ 36]);
140 fourxm
->height
= AV_RL32(&header
[i
+ 40]);
142 /* allocate a new AVStream */
143 st
= av_new_stream(s
, 0);
145 return AVERROR(ENOMEM
);
146 av_set_pts_info(st
, 60, 1, fourxm
->fps
);
148 fourxm
->video_stream_index
= st
->index
;
150 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
151 st
->codec
->codec_id
= CODEC_ID_4XM
;
152 st
->codec
->extradata_size
= 4;
153 st
->codec
->extradata
= av_malloc(4);
154 AV_WL32(st
->codec
->extradata
, AV_RL32(&header
[i
+ 16]));
155 st
->codec
->width
= fourxm
->width
;
156 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
= AV_RL32(&header
[i
+ 8]);
166 if (current_track
+ 1 > fourxm
->track_count
) {
167 fourxm
->track_count
= current_track
+ 1;
168 if((unsigned)fourxm
->track_count
>= UINT_MAX
/ sizeof(AudioTrack
))
170 fourxm
->tracks
= av_realloc(fourxm
->tracks
,
171 fourxm
->track_count
* sizeof(AudioTrack
));
172 if (!fourxm
->tracks
) {
174 return AVERROR(ENOMEM
);
177 fourxm
->tracks
[current_track
].adpcm
= AV_RL32(&header
[i
+ 12]);
178 fourxm
->tracks
[current_track
].channels
= AV_RL32(&header
[i
+ 36]);
179 fourxm
->tracks
[current_track
].sample_rate
= AV_RL32(&header
[i
+ 40]);
180 fourxm
->tracks
[current_track
].bits
= AV_RL32(&header
[i
+ 44]);
183 /* allocate a new AVStream */
184 st
= av_new_stream(s
, current_track
);
186 return AVERROR(ENOMEM
);
188 av_set_pts_info(st
, 60, 1, fourxm
->tracks
[current_track
].sample_rate
);
190 fourxm
->tracks
[current_track
].stream_index
= st
->index
;
192 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
193 st
->codec
->codec_tag
= 0;
194 st
->codec
->channels
= fourxm
->tracks
[current_track
].channels
;
195 st
->codec
->sample_rate
= fourxm
->tracks
[current_track
].sample_rate
;
196 st
->codec
->bits_per_sample
= fourxm
->tracks
[current_track
].bits
;
197 st
->codec
->bit_rate
= st
->codec
->channels
* st
->codec
->sample_rate
*
198 st
->codec
->bits_per_sample
;
199 st
->codec
->block_align
= st
->codec
->channels
* st
->codec
->bits_per_sample
;
200 if (fourxm
->tracks
[current_track
].adpcm
)
201 st
->codec
->codec_id
= CODEC_ID_ADPCM_4XM
;
202 else if (st
->codec
->bits_per_sample
== 8)
203 st
->codec
->codec_id
= CODEC_ID_PCM_U8
;
205 st
->codec
->codec_id
= CODEC_ID_PCM_S16LE
;
211 /* skip over the LIST-MOVI chunk (which is where the stream should be */
213 if (fourcc_tag
!= MOVI_TAG
)
214 return AVERROR_INVALIDDATA
;
216 /* initialize context members */
217 fourxm
->video_pts
= -1; /* first frame will push to 0 */
218 fourxm
->audio_pts
= 0;
223 static int fourxm_read_packet(AVFormatContext
*s
,
226 FourxmDemuxContext
*fourxm
= s
->priv_data
;
227 ByteIOContext
*pb
= s
->pb
;
228 unsigned int fourcc_tag
;
229 unsigned int size
, out_size
;
233 unsigned char header
[8];
234 int audio_frame_count
;
236 while (!packet_read
) {
238 if ((ret
= get_buffer(s
->pb
, header
, 8)) < 0)
240 fourcc_tag
= AV_RL32(&header
[0]);
241 size
= AV_RL32(&header
[4]);
244 switch (fourcc_tag
) {
247 /* this is a good time to bump the video pts */
248 fourxm
->video_pts
++;
250 /* skip the LIST-* tag and move on to the next fourcc */
262 /* allocate 8 more bytes than 'size' to account for fourcc
264 if (size
+ 8 < size
|| av_new_packet(pkt
, size
+ 8))
266 pkt
->stream_index
= fourxm
->video_stream_index
;
267 pkt
->pts
= fourxm
->video_pts
;
268 pkt
->pos
= url_ftell(s
->pb
);
269 memcpy(pkt
->data
, header
, 8);
270 ret
= get_buffer(s
->pb
, &pkt
->data
[8], size
);
280 track_number
= get_le32(pb
);
281 out_size
= get_le32(pb
);
284 if (track_number
== fourxm
->selected_track
) {
285 ret
= av_get_packet(s
->pb
, pkt
, size
);
289 fourxm
->tracks
[fourxm
->selected_track
].stream_index
;
290 pkt
->pts
= fourxm
->audio_pts
;
294 audio_frame_count
= size
;
295 if (fourxm
->tracks
[fourxm
->selected_track
].adpcm
)
297 2 * (fourxm
->tracks
[fourxm
->selected_track
].channels
);
299 fourxm
->tracks
[fourxm
->selected_track
].channels
;
300 if (fourxm
->tracks
[fourxm
->selected_track
].adpcm
)
301 audio_frame_count
*= 2;
304 (fourxm
->tracks
[fourxm
->selected_track
].bits
/ 8);
305 fourxm
->audio_pts
+= audio_frame_count
;
308 url_fseek(pb
, size
, SEEK_CUR
);
313 url_fseek(pb
, size
, SEEK_CUR
);
320 static int fourxm_read_close(AVFormatContext
*s
)
322 FourxmDemuxContext
*fourxm
= s
->priv_data
;
324 av_free(fourxm
->tracks
);
329 AVInputFormat fourxm_demuxer
= {
331 "4X Technologies format",
332 sizeof(FourxmDemuxContext
),