137
|
1 /*
|
|
2 * 4X Technologies .4xm File Demuxer (no muxer)
|
|
3 * Copyright (c) 2003 The ffmpeg Project
|
|
4 *
|
|
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.
|
|
9 *
|
|
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.
|
|
14 *
|
|
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
|
|
18 */
|
|
19
|
|
20 /**
|
|
21 * @file 4xm.c
|
|
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/
|
|
26 */
|
|
27
|
|
28 #include "avformat.h"
|
|
29
|
|
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) | \
|
|
34 ((uint8_t*)(x))[0])
|
|
35
|
|
36 #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
|
143
|
37 ( (long)(unsigned char)(ch0) | \
|
|
38 ( (long)(unsigned char)(ch1) << 8 ) | \
|
|
39 ( (long)(unsigned char)(ch2) << 16 ) | \
|
|
40 ( (long)(unsigned char)(ch3) << 24 ) )
|
137
|
41
|
|
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('', '', '', '')
|
|
58
|
|
59 #define vtrk_SIZE 0x44
|
|
60 #define strk_SIZE 0x28
|
|
61
|
|
62 #define GET_LIST_HEADER() \
|
143
|
63 fourcc_tag = get_le32(pb); \
|
137
|
64 size = get_le32(pb); \
|
|
65 if (fourcc_tag != LIST_TAG) \
|
|
66 return AVERROR_INVALIDDATA; \
|
143
|
67 fourcc_tag = get_le32(pb);
|
137
|
68
|
|
69 typedef struct AudioTrack {
|
|
70 int sample_rate;
|
|
71 int bits;
|
|
72 int channels;
|
143
|
73 int stream_index;
|
145
|
74 int adpcm;
|
137
|
75 } AudioTrack;
|
|
76
|
|
77 typedef struct FourxmDemuxContext {
|
|
78 int width;
|
|
79 int height;
|
143
|
80 int video_stream_index;
|
137
|
81 int track_count;
|
|
82 AudioTrack *tracks;
|
|
83 int selected_track;
|
143
|
84
|
|
85 int64_t pts;
|
|
86 int last_chunk_was_audio;
|
|
87 int last_audio_frame_count;
|
137
|
88 } FourxmDemuxContext;
|
|
89
|
|
90 static int fourxm_probe(AVProbeData *p)
|
|
91 {
|
143
|
92 if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
|
|
93 (LE_32(&p->buf[8]) != _4XMV_TAG))
|
137
|
94 return 0;
|
|
95
|
|
96 return AVPROBE_SCORE_MAX;
|
|
97 }
|
|
98
|
|
99 static int fourxm_read_header(AVFormatContext *s,
|
143
|
100 AVFormatParameters *ap)
|
137
|
101 {
|
|
102 ByteIOContext *pb = &s->pb;
|
|
103 unsigned int fourcc_tag;
|
|
104 unsigned int size;
|
|
105 int header_size;
|
|
106 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
|
|
107 unsigned char *header;
|
|
108 int i;
|
|
109 int current_track = -1;
|
|
110 AVStream *st;
|
|
111
|
|
112 fourxm->track_count = 0;
|
|
113 fourxm->tracks = NULL;
|
|
114 fourxm->selected_track = 0;
|
|
115
|
|
116 /* skip the first 3 32-bit numbers */
|
|
117 url_fseek(pb, 12, SEEK_CUR);
|
|
118
|
|
119 /* check for LIST-HEAD */
|
|
120 GET_LIST_HEADER();
|
|
121 header_size = size - 4;
|
|
122 if (fourcc_tag != HEAD_TAG)
|
|
123 return AVERROR_INVALIDDATA;
|
|
124
|
|
125 /* allocate space for the header and load the whole thing */
|
|
126 header = av_malloc(header_size);
|
|
127 if (!header)
|
|
128 return AVERROR_NOMEM;
|
|
129 if (get_buffer(pb, header, header_size) != header_size)
|
|
130 return AVERROR_IO;
|
|
131
|
|
132 /* take the lazy approach and search for any and all vtrk and strk chunks */
|
|
133 for (i = 0; i < header_size - 8; i++) {
|
143
|
134 fourcc_tag = LE_32(&header[i]);
|
137
|
135 size = LE_32(&header[i + 4]);
|
|
136
|
|
137 if (fourcc_tag == vtrk_TAG) {
|
|
138 /* check that there is enough data */
|
|
139 if (size != vtrk_SIZE) {
|
|
140 av_free(header);
|
|
141 return AVERROR_INVALIDDATA;
|
|
142 }
|
|
143 fourxm->width = LE_32(&header[i + 36]);
|
|
144 fourxm->height = LE_32(&header[i + 40]);
|
|
145 i += 8 + size;
|
143
|
146
|
|
147 /* allocate a new AVStream */
|
|
148 st = av_new_stream(s, 0);
|
|
149 if (!st)
|
|
150 return AVERROR_NOMEM;
|
|
151
|
|
152 fourxm->video_stream_index = st->index;
|
|
153
|
|
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;
|
|
159
|
137
|
160 } else if (fourcc_tag == strk_TAG) {
|
|
161 /* check that there is enough data */
|
|
162 if (size != strk_SIZE) {
|
|
163 av_free(header);
|
|
164 return AVERROR_INVALIDDATA;
|
|
165 }
|
|
166 current_track = LE_32(&header[i + 8]);
|
|
167 if (current_track + 1 > fourxm->track_count) {
|
141
|
168 fourxm->track_count = current_track + 1;
|
|
169 fourxm->tracks = av_realloc(fourxm->tracks,
|
|
170 fourxm->track_count * sizeof(AudioTrack));
|
137
|
171 if (!fourxm->tracks) {
|
|
172 av_free(header);
|
|
173 return AVERROR_NOMEM;
|
|
174 }
|
|
175 }
|
145
|
176 fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
|
137
|
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]);
|
145
|
180 printf("bps:%d\n", fourxm->tracks[current_track].bits);
|
137
|
181 i += 8 + size;
|
143
|
182
|
|
183 /* allocate a new AVStream */
|
|
184 st = av_new_stream(s, current_track);
|
|
185 if (!st)
|
|
186 return AVERROR_NOMEM;
|
|
187
|
|
188 fourxm->tracks[current_track].stream_index = st->index;
|
|
189
|
|
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;
|
145
|
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)
|
143
|
201 st->codec.codec_id = CODEC_ID_PCM_U8;
|
|
202 else
|
|
203 st->codec.codec_id = CODEC_ID_PCM_S16LE;
|
137
|
204 }
|
|
205 }
|
|
206
|
|
207 av_free(header);
|
|
208
|
|
209 /* skip over the LIST-MOVI chunk (which is where the stream should be */
|
|
210 GET_LIST_HEADER();
|
|
211 if (fourcc_tag != MOVI_TAG)
|
|
212 return AVERROR_INVALIDDATA;
|
|
213
|
143
|
214 /* initialize context members */
|
|
215 fourxm->pts = 0;
|
|
216 fourxm->last_chunk_was_audio = 0;
|
|
217 fourxm->last_audio_frame_count = 0;
|
137
|
218
|
143
|
219 /* set the pts reference (1 pts = 1/90000) */
|
|
220 s->pts_num = 1;
|
|
221 s->pts_den = 90000;
|
137
|
222
|
|
223 return 0;
|
|
224 }
|
|
225
|
|
226 static int fourxm_read_packet(AVFormatContext *s,
|
143
|
227 AVPacket *pkt)
|
137
|
228 {
|
|
229 FourxmDemuxContext *fourxm = s->priv_data;
|
|
230 ByteIOContext *pb = &s->pb;
|
|
231 unsigned int fourcc_tag;
|
145
|
232 unsigned int size, out_size;
|
137
|
233 int ret = 0;
|
|
234 int track_number;
|
|
235 int packet_read = 0;
|
143
|
236 unsigned char header[8];
|
|
237 int64_t pts_inc;
|
137
|
238
|
|
239 while (!packet_read) {
|
|
240
|
143
|
241 if ((ret = get_buffer(&s->pb, header, 8)) < 0)
|
|
242 return ret;
|
|
243 fourcc_tag = LE_32(&header[0]);
|
|
244 size = LE_32(&header[4]);
|
145
|
245 //printf(" %8X %c%c%c%c %d\n", fourcc_tag, fourcc_tag, fourcc_tag>>8, fourcc_tag>>16, fourcc_tag>>24, size);
|
137
|
246 if (url_feof(pb))
|
|
247 return -EIO;
|
|
248 switch (fourcc_tag) {
|
|
249
|
144
|
250 case LIST_TAG:
|
|
251 /* skip the LIST-* tag and move on to the next fourcc */
|
|
252 get_le32(pb);
|
|
253 break;
|
|
254
|
137
|
255 case ifrm_TAG:
|
|
256 case pfrm_TAG:
|
139
|
257 case cfrm_TAG:{
|
143
|
258
|
|
259 int id, whole;
|
139
|
260 static int stats[1000];
|
143
|
261
|
|
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;
|
|
266 pts_inc *= 90000;
|
|
267 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
|
|
268 fourxm->pts += pts_inc;
|
|
269 }
|
|
270
|
|
271 /* allocate 8 more bytes than 'size' to account for fourcc
|
|
272 * and size */
|
|
273 if (av_new_packet(pkt, size + 8))
|
|
274 return -EIO;
|
|
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);
|
|
279
|
|
280 if (fourcc_tag == cfrm_TAG) {
|
|
281 id = LE_32(&pkt->data[12]);
|
|
282 whole = LE_32(&pkt->data[16]);
|
139
|
283 stats[id] += size - 12;
|
145
|
284 //printf(" cfrm chunk id:%d size:%d whole:%d until now:%d\n", id, size, whole, stats[id]);
|
143
|
285 }
|
|
286
|
|
287 if (ret < 0)
|
|
288 av_free_packet(pkt);
|
|
289 else
|
|
290 packet_read = 1;
|
139
|
291 break;
|
|
292 }
|
143
|
293
|
137
|
294 case snd__TAG:
|
|
295 printf (" snd_ chunk, ");
|
|
296 track_number = get_le32(pb);
|
145
|
297 out_size= get_le32(pb);
|
|
298 size-=8;
|
|
299
|
137
|
300 if (track_number == fourxm->selected_track) {
|
|
301 printf ("correct track, dispatching...\n");
|
|
302 if (av_new_packet(pkt, size))
|
|
303 return -EIO;
|
143
|
304 pkt->stream_index =
|
|
305 fourxm->tracks[fourxm->selected_track].stream_index;
|
|
306 pkt->pts = fourxm->pts;
|
137
|
307 ret = get_buffer(&s->pb, pkt->data, size);
|
|
308 if (ret < 0)
|
|
309 av_free_packet(pkt);
|
143
|
310 else
|
|
311 packet_read = 1;
|
|
312
|
|
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);
|
|
318
|
137
|
319 } else {
|
|
320 printf ("wrong track, skipping...\n");
|
|
321 url_fseek(pb, size, SEEK_CUR);
|
|
322 }
|
|
323 break;
|
|
324
|
|
325 default:
|
|
326 url_fseek(pb, size, SEEK_CUR);
|
|
327 break;
|
|
328 }
|
|
329 }
|
|
330 return ret;
|
|
331 }
|
|
332
|
|
333 static int fourxm_read_close(AVFormatContext *s)
|
|
334 {
|
|
335 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
|
|
336
|
|
337 av_free(fourxm->tracks);
|
|
338
|
|
339 return 0;
|
|
340 }
|
|
341
|
|
342 static AVInputFormat fourxm_iformat = {
|
|
343 "4xm",
|
|
344 "4X Technologies format",
|
|
345 sizeof(FourxmDemuxContext),
|
|
346 fourxm_probe,
|
|
347 fourxm_read_header,
|
|
348 fourxm_read_packet,
|
|
349 fourxm_read_close,
|
|
350 };
|
|
351
|
|
352 int fourxm_init(void)
|
|
353 {
|
|
354 av_register_input_format(&fourxm_iformat);
|
|
355 return 0;
|
|
356 }
|