]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/4xm.c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
[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 file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file 4xm.c
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/
28  */
29
30 #include "avformat.h"
31
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  snd__TAG MKTAG('s', 'n', 'd', '_')
48
49 #define vtrk_SIZE 0x44
50 #define strk_SIZE 0x28
51
52 #define GET_LIST_HEADER() \
53     fourcc_tag = get_le32(pb); \
54     size = get_le32(pb); \
55     if (fourcc_tag != LIST_TAG) \
56         return AVERROR_INVALIDDATA; \
57     fourcc_tag = get_le32(pb);
58
59 typedef struct AudioTrack {
60     int sample_rate;
61     int bits;
62     int channels;
63     int stream_index;
64     int adpcm;
65 } AudioTrack;
66
67 typedef struct FourxmDemuxContext {
68     int width;
69     int height;
70     int video_stream_index;
71     int track_count;
72     AudioTrack *tracks;
73     int selected_track;
74
75     int64_t audio_pts;
76     int64_t video_pts;
77     float fps;
78 } FourxmDemuxContext;
79
80 static int fourxm_probe(AVProbeData *p)
81 {
82     if (p->buf_size < 12)
83         return 0;
84
85     if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
86         (LE_32(&p->buf[8]) != _4XMV_TAG))
87         return 0;
88
89     return AVPROBE_SCORE_MAX;
90 }
91
92 static int fourxm_read_header(AVFormatContext *s,
93                               AVFormatParameters *ap)
94 {
95     ByteIOContext *pb = &s->pb;
96     unsigned int fourcc_tag;
97     unsigned int size;
98     int header_size;
99     FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
100     unsigned char *header;
101     int i;
102     int current_track = -1;
103     AVStream *st;
104
105     fourxm->track_count = 0;
106     fourxm->tracks = NULL;
107     fourxm->selected_track = 0;
108     fourxm->fps = 1.0;
109
110     /* skip the first 3 32-bit numbers */
111     url_fseek(pb, 12, SEEK_CUR);
112
113     /* check for LIST-HEAD */
114     GET_LIST_HEADER();
115     header_size = size - 4;
116     if (fourcc_tag != HEAD_TAG)
117         return AVERROR_INVALIDDATA;
118
119     /* allocate space for the header and load the whole thing */
120     header = av_malloc(header_size);
121     if (!header)
122         return AVERROR_NOMEM;
123     if (get_buffer(pb, header, header_size) != header_size)
124         return AVERROR_IO;
125
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 = LE_32(&header[i]);
129         size = LE_32(&header[i + 4]);
130
131         if (fourcc_tag == std__TAG) {
132             fourxm->fps = av_int2flt(LE_32(&header[i + 12]));
133         } else if (fourcc_tag == vtrk_TAG) {
134             /* check that there is enough data */
135             if (size != vtrk_SIZE) {
136                 av_free(header);
137                 return AVERROR_INVALIDDATA;
138             }
139             fourxm->width = LE_32(&header[i + 36]);
140             fourxm->height = LE_32(&header[i + 40]);
141             i += 8 + size;
142
143             /* allocate a new AVStream */
144             st = av_new_stream(s, 0);
145             if (!st)
146                 return AVERROR_NOMEM;
147             av_set_pts_info(st, 60, 1, fourxm->fps);
148
149             fourxm->video_stream_index = st->index;
150
151             st->codec->codec_type = CODEC_TYPE_VIDEO;
152             st->codec->codec_id = CODEC_ID_4XM;
153             st->codec->codec_tag = 0;  /* no fourcc */
154             st->codec->width = fourxm->width;
155             st->codec->height = fourxm->height;
156
157         } else if (fourcc_tag == strk_TAG) {
158             /* check that there is enough data */
159             if (size != strk_SIZE) {
160                 av_free(header);
161                 return AVERROR_INVALIDDATA;
162             }
163             current_track = LE_32(&header[i + 8]);
164             if (current_track + 1 > fourxm->track_count) {
165                 fourxm->track_count = current_track + 1;
166                 if((unsigned)fourxm->track_count >= UINT_MAX / sizeof(AudioTrack))
167                     return -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].adpcm = LE_32(&header[i + 12]);
176             fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
177             fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
178             fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
179             i += 8 + size;
180
181             /* allocate a new AVStream */
182             st = av_new_stream(s, current_track);
183             if (!st)
184                 return AVERROR_NOMEM;
185
186             av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
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;
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)
201                 st->codec->codec_id = CODEC_ID_PCM_U8;
202             else
203                 st->codec->codec_id = CODEC_ID_PCM_S16LE;
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
214     /* initialize context members */
215     fourxm->video_pts = -1;  /* first frame will push to 0 */
216     fourxm->audio_pts = 0;
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, out_size;
228     int ret = 0;
229     int track_number;
230     int packet_read = 0;
231     unsigned char header[8];
232     int audio_frame_count;
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 (url_feof(pb))
241             return AVERROR_IO;
242         switch (fourcc_tag) {
243
244         case LIST_TAG:
245             /* this is a good time to bump the video pts */
246             fourxm->video_pts ++;
247
248             /* skip the LIST-* tag and move on to the next fourcc */
249             get_le32(pb);
250             break;
251
252         case ifrm_TAG:
253         case pfrm_TAG:
254         case cfrm_TAG:{
255
256             /* allocate 8 more bytes than 'size' to account for fourcc
257              * and size */
258             if (size + 8 < size || av_new_packet(pkt, size + 8))
259                 return AVERROR_IO;
260             pkt->stream_index = fourxm->video_stream_index;
261             pkt->pts = fourxm->video_pts;
262             pkt->pos = url_ftell(&s->pb);
263             memcpy(pkt->data, header, 8);
264             ret = get_buffer(&s->pb, &pkt->data[8], size);
265
266             if (ret < 0)
267                 av_free_packet(pkt);
268             else
269                 packet_read = 1;
270             break;
271         }
272
273         case snd__TAG:
274             track_number = get_le32(pb);
275             out_size= get_le32(pb);
276             size-=8;
277
278             if (track_number == fourxm->selected_track) {
279                 ret= av_get_packet(&s->pb, pkt, size);
280                 if(ret<0)
281                     return AVERROR_IO;
282                 pkt->stream_index =
283                     fourxm->tracks[fourxm->selected_track].stream_index;
284                 pkt->pts = fourxm->audio_pts;
285                 packet_read = 1;
286
287                 /* pts accounting */
288                 audio_frame_count = size;
289                 if (fourxm->tracks[fourxm->selected_track].adpcm)
290                     audio_frame_count -=
291                         2 * (fourxm->tracks[fourxm->selected_track].channels);
292                 audio_frame_count /=
293                       fourxm->tracks[fourxm->selected_track].channels;
294                 if (fourxm->tracks[fourxm->selected_track].adpcm)
295                     audio_frame_count *= 2;
296                 else
297                     audio_frame_count /=
298                     (fourxm->tracks[fourxm->selected_track].bits / 8);
299                 fourxm->audio_pts += audio_frame_count;
300
301             } else {
302                 url_fseek(pb, size, SEEK_CUR);
303             }
304             break;
305
306         default:
307             url_fseek(pb, size, SEEK_CUR);
308             break;
309         }
310     }
311     return ret;
312 }
313
314 static int fourxm_read_close(AVFormatContext *s)
315 {
316     FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
317
318     av_free(fourxm->tracks);
319
320     return 0;
321 }
322
323 AVInputFormat fourxm_demuxer = {
324     "4xm",
325     "4X Technologies format",
326     sizeof(FourxmDemuxContext),
327     fourxm_probe,
328     fourxm_read_header,
329     fourxm_read_packet,
330     fourxm_read_close,
331 };