]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/4xm.c
use the proper file framerate (specified by a float); account the pts
[frescor/ffmpeg.git] / 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  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('', '', '', '')
59
60 #define vtrk_SIZE 0x44
61 #define strk_SIZE 0x28
62
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);
69
70 typedef struct AudioTrack {
71     int sample_rate;
72     int bits;
73     int channels;
74     int stream_index;
75     int adpcm;
76 } AudioTrack;
77
78 typedef struct FourxmDemuxContext {
79     int width;
80     int height;
81     int video_stream_index;
82     int track_count;
83     AudioTrack *tracks;
84     int selected_track;
85
86     int64_t audio_pts;
87     int64_t video_pts;
88     int video_pts_inc;
89 } FourxmDemuxContext;
90
91 static float get_le_float(unsigned char *buffer)
92 {
93     float f;
94     unsigned char *float_buffer = (unsigned char *)&f;
95
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];
101 #else
102     float_buffer[0] = buffer[0];
103     float_buffer[1] = buffer[1];
104     float_buffer[2] = buffer[2];
105     float_buffer[3] = buffer[3];
106 #endif
107
108     return f;
109 }
110
111 static int fourxm_probe(AVProbeData *p)
112 {
113     if (p->buf_size < 12)
114         return 0;
115
116     if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
117         (LE_32(&p->buf[8]) != _4XMV_TAG))
118         return 0;
119
120     return AVPROBE_SCORE_MAX;
121 }
122
123 static int fourxm_read_header(AVFormatContext *s,
124                               AVFormatParameters *ap)
125 {
126     ByteIOContext *pb = &s->pb;
127     unsigned int fourcc_tag;
128     unsigned int size;
129     int header_size;
130     FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
131     unsigned char *header;
132     int i;
133     int current_track = -1;
134     AVStream *st;
135     float fps;
136
137     fourxm->track_count = 0;
138     fourxm->tracks = NULL;
139     fourxm->selected_track = 0;
140
141     /* skip the first 3 32-bit numbers */
142     url_fseek(pb, 12, SEEK_CUR);
143
144     /* check for LIST-HEAD */
145     GET_LIST_HEADER();
146     header_size = size - 4;
147     if (fourcc_tag != HEAD_TAG)
148         return AVERROR_INVALIDDATA;
149
150     /* allocate space for the header and load the whole thing */
151     header = av_malloc(header_size);
152     if (!header)
153         return AVERROR_NOMEM;
154     if (get_buffer(pb, header, header_size) != header_size)
155         return AVERROR_IO;
156
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]);
161
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) {
168                 av_free(header);
169                 return AVERROR_INVALIDDATA;
170             }
171             fourxm->width = LE_32(&header[i + 36]);
172             fourxm->height = LE_32(&header[i + 40]);
173             i += 8 + size;
174
175             /* allocate a new AVStream */
176             st = av_new_stream(s, 0);
177             if (!st)
178                 return AVERROR_NOMEM;
179
180             fourxm->video_stream_index = st->index;
181
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;
187
188         } else if (fourcc_tag == strk_TAG) {
189             /* check that there is enough data */
190             if (size != strk_SIZE) {
191                 av_free(header);
192                 return AVERROR_INVALIDDATA;
193             }
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) {
200                     av_free(header);
201                     return AVERROR_NOMEM;
202                 }
203             }
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]);
208             i += 8 + size;
209
210             /* allocate a new AVStream */
211             st = av_new_stream(s, current_track);
212             if (!st)
213                 return AVERROR_NOMEM;
214
215             fourxm->tracks[current_track].stream_index = st->index;
216
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;
229             else
230                 st->codec.codec_id = CODEC_ID_PCM_S16LE;
231         }
232     }
233
234     av_free(header);
235
236     /* skip over the LIST-MOVI chunk (which is where the stream should be */
237     GET_LIST_HEADER();
238     if (fourcc_tag != MOVI_TAG)
239         return AVERROR_INVALIDDATA;
240
241     /* initialize context members */
242     fourxm->video_pts = -fourxm->video_pts_inc;  /* first frame will push to 0 */
243     fourxm->audio_pts = 0;
244
245     /* set the pts reference (1 pts = 1/90000) */
246     s->pts_num = 1;
247     s->pts_den = 90000;
248
249     return 0;
250 }
251
252 static int fourxm_read_packet(AVFormatContext *s,
253                               AVPacket *pkt)
254 {
255     FourxmDemuxContext *fourxm = s->priv_data;
256     ByteIOContext *pb = &s->pb;
257     unsigned int fourcc_tag;
258     unsigned int size, out_size;
259     int ret = 0;
260     int track_number;
261     int packet_read = 0;
262     unsigned char header[8];
263     int64_t pts_inc;
264     int audio_frame_count;
265
266     while (!packet_read) {
267
268         if ((ret = get_buffer(&s->pb, header, 8)) < 0)
269             return ret;
270         fourcc_tag = LE_32(&header[0]);
271         size = LE_32(&header[4]);
272         if (url_feof(pb))
273             return -EIO;
274         switch (fourcc_tag) {
275
276         case LIST_TAG:
277             /* this is a good time to bump the video pts */
278             fourxm->video_pts += fourxm->video_pts_inc;
279
280             /* skip the LIST-* tag and move on to the next fourcc */
281             get_le32(pb);
282             break;
283
284         case ifrm_TAG:
285         case pfrm_TAG:
286         case cfrm_TAG:{
287
288             /* allocate 8 more bytes than 'size' to account for fourcc
289              * and size */
290             if (av_new_packet(pkt, size + 8))
291                 return -EIO;
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);
296
297             if (ret < 0)
298                 av_free_packet(pkt);
299             else
300                 packet_read = 1;
301             break;
302         }
303
304         case snd__TAG:
305             track_number = get_le32(pb);
306             out_size= get_le32(pb);
307             size-=8;
308
309             if (track_number == fourxm->selected_track) {
310                 if (av_new_packet(pkt, size))
311                     return -EIO;
312                 pkt->stream_index = 
313                     fourxm->tracks[fourxm->selected_track].stream_index;
314                 pkt->pts = fourxm->audio_pts;
315                 ret = get_buffer(&s->pb, pkt->data, size);
316                 if (ret < 0)
317                     av_free_packet(pkt);
318                 else
319                     packet_read = 1;
320
321                 /* pts accounting */
322                 audio_frame_count = size;
323                 if (fourxm->tracks[fourxm->selected_track].adpcm)
324                     audio_frame_count -= 
325                         2 * (fourxm->tracks[fourxm->selected_track].channels);
326                 audio_frame_count /=
327                       fourxm->tracks[fourxm->selected_track].channels;
328                 if (fourxm->tracks[fourxm->selected_track].adpcm)
329                     audio_frame_count *= 2;
330                 else 
331                     audio_frame_count /=
332                     (fourxm->tracks[fourxm->selected_track].bits / 8);
333                 pts_inc = audio_frame_count;
334                 pts_inc *= 90000;
335                 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
336                 fourxm->audio_pts += pts_inc;
337
338             } else {
339                 url_fseek(pb, size, SEEK_CUR);
340             }
341             break;
342
343         default:
344             url_fseek(pb, size, SEEK_CUR);
345             break;
346         }
347     }
348     return ret;
349 }
350
351 static int fourxm_read_close(AVFormatContext *s)
352 {
353     FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
354
355     av_free(fourxm->tracks);
356
357     return 0;
358 }
359
360 static AVInputFormat fourxm_iformat = {
361     "4xm",
362     "4X Technologies format",
363     sizeof(FourxmDemuxContext),
364     fourxm_probe,
365     fourxm_read_header,
366     fourxm_read_packet,
367     fourxm_read_close,
368 };
369
370 int fourxm_init(void)
371 {
372     av_register_input_format(&fourxm_iformat);
373     return 0;
374 }