]> git.sesse.net Git - ffmpeg/blob - libavformat/4xm.c
dispatch video as well as audio
[ffmpeg] / libavformat / 4xm.c
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 ) \
37         ( (long)(unsigned char)(ch0) | \
38         ( (long)(unsigned char)(ch1) << 8 ) | \
39         ( (long)(unsigned char)(ch2) << 16 ) | \
40         ( (long)(unsigned char)(ch3) << 24 ) )
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() \
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);
68
69 typedef struct AudioTrack {
70     int sample_rate;
71     int bits;
72     int channels;
73     int stream_index;
74 } AudioTrack;
75
76 typedef struct FourxmDemuxContext {
77     int width;
78     int height;
79     int video_stream_index;
80     int track_count;
81     AudioTrack *tracks;
82     int selected_track;
83
84     int64_t pts;
85     int last_chunk_was_audio;
86     int last_audio_frame_count;
87 } FourxmDemuxContext;
88
89 static int fourxm_probe(AVProbeData *p)
90 {
91     if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
92         (LE_32(&p->buf[8]) != _4XMV_TAG))
93         return 0;
94
95     return AVPROBE_SCORE_MAX;
96 }
97
98 static int fourxm_read_header(AVFormatContext *s,
99                               AVFormatParameters *ap)
100 {
101     ByteIOContext *pb = &s->pb;
102     unsigned int fourcc_tag;
103     unsigned int size;
104     int header_size;
105     FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
106     unsigned char *header;
107     int i;
108     int current_track = -1;
109     AVStream *st;
110
111     fourxm->track_count = 0;
112     fourxm->tracks = NULL;
113     fourxm->selected_track = 0;
114
115     /* skip the first 3 32-bit numbers */
116     url_fseek(pb, 12, SEEK_CUR);
117
118     /* check for LIST-HEAD */
119     GET_LIST_HEADER();
120     header_size = size - 4;
121     if (fourcc_tag != HEAD_TAG)
122         return AVERROR_INVALIDDATA;
123
124     /* allocate space for the header and load the whole thing */
125     header = av_malloc(header_size);
126     if (!header)
127         return AVERROR_NOMEM;
128     if (get_buffer(pb, header, header_size) != header_size)
129         return AVERROR_IO;
130
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]);
135
136         if (fourcc_tag == vtrk_TAG) {
137             /* check that there is enough data */
138             if (size != vtrk_SIZE) {
139                 av_free(header);
140                 return AVERROR_INVALIDDATA;
141             }
142             fourxm->width = LE_32(&header[i + 36]);
143             fourxm->height = LE_32(&header[i + 40]);
144             i += 8 + size;
145
146             /* allocate a new AVStream */
147             st = av_new_stream(s, 0);
148             if (!st)
149                 return AVERROR_NOMEM;
150
151             fourxm->video_stream_index = st->index;
152
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;
158
159         } else if (fourcc_tag == strk_TAG) {
160             /* check that there is enough data */
161             if (size != strk_SIZE) {
162                 av_free(header);
163                 return AVERROR_INVALIDDATA;
164             }
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) {
171                     av_free(header);
172                     return AVERROR_NOMEM;
173                 }
174             }
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]);
178             i += 8 + size;
179
180             /* allocate a new AVStream */
181             st = av_new_stream(s, current_track);
182             if (!st)
183                 return AVERROR_NOMEM;
184
185             fourxm->tracks[current_track].stream_index = st->index;
186
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;
197             else
198                 st->codec.codec_id = CODEC_ID_PCM_S16LE;
199         }
200     }
201
202     av_free(header);
203
204     /* skip over the LIST-MOVI chunk (which is where the stream should be */
205     GET_LIST_HEADER();
206     if (fourcc_tag != MOVI_TAG)
207         return AVERROR_INVALIDDATA;
208
209     /* initialize context members */
210     fourxm->pts = 0;
211     fourxm->last_chunk_was_audio = 0;
212     fourxm->last_audio_frame_count = 0;
213
214     /* set the pts reference (1 pts = 1/90000) */
215     s->pts_num = 1;
216     s->pts_den = 90000;
217
218     return 0;
219 }
220
221 static int fourxm_read_packet(AVFormatContext *s,
222                               AVPacket *pkt)
223 {
224     FourxmDemuxContext *fourxm = s->priv_data;
225     ByteIOContext *pb = &s->pb;
226     unsigned int fourcc_tag;
227     unsigned int size;
228     int ret = 0;
229     int track_number;
230     int packet_read = 0;
231     unsigned char header[8];
232     int64_t pts_inc;
233
234     while (!packet_read) {
235
236         if ((ret = get_buffer(&s->pb, header, 8)) < 0)
237             return ret;
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 */
242             get_le32(pb);
243             fourcc_tag = get_le32(pb);
244             size = get_le32(pb);
245         }
246
247         if (url_feof(pb))
248             return -EIO;
249
250         switch (fourcc_tag) {
251
252         case ifrm_TAG:
253         case pfrm_TAG:
254         case cfrm_TAG:{
255
256 int id, whole;
257 static int stats[1000];
258
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;
263                 pts_inc *= 90000;
264                 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
265                 fourxm->pts += pts_inc;
266             }
267
268             /* allocate 8 more bytes than 'size' to account for fourcc
269              * and size */
270             if (av_new_packet(pkt, size + 8))
271                 return -EIO;
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);
276
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]);
282 }
283
284             if (ret < 0)
285                 av_free_packet(pkt);
286             else
287                 packet_read = 1;
288             break;
289         }
290
291         case snd__TAG:
292 printf (" snd_ chunk, ");
293             track_number = get_le32(pb);
294             size = get_le32(pb);
295             if (track_number == fourxm->selected_track) {
296 printf ("correct track, dispatching...\n");
297                 if (av_new_packet(pkt, size))
298                     return -EIO;
299                 pkt->stream_index = 
300                     fourxm->tracks[fourxm->selected_track].stream_index;
301                 pkt->pts = fourxm->pts;
302                 ret = get_buffer(&s->pb, pkt->data, size);
303                 if (ret < 0)
304                     av_free_packet(pkt);
305                 else
306                     packet_read = 1;
307
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);
313
314             } else {
315 printf ("wrong track, skipping...\n");
316                 url_fseek(pb, size, SEEK_CUR);
317             }
318             break;
319
320         default:
321             url_fseek(pb, size, SEEK_CUR);
322             break;
323         }
324     }
325
326     return ret;
327 }
328
329 static int fourxm_read_close(AVFormatContext *s)
330 {
331     FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
332
333     av_free(fourxm->tracks);
334
335     return 0;
336 }
337
338 static AVInputFormat fourxm_iformat = {
339     "4xm",
340     "4X Technologies format",
341     sizeof(FourxmDemuxContext),
342     fourxm_probe,
343     fourxm_read_header,
344     fourxm_read_packet,
345     fourxm_read_close,
346 };
347
348 int fourxm_init(void)
349 {
350     av_register_input_format(&fourxm_iformat);
351     return 0;
352 }