]> git.sesse.net Git - ffmpeg/blob - libavformat/4xm.c
avprobe: fix formatting.
[ffmpeg] / 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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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
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 "libavutil/intreadwrite.h"
31 #include "libavutil/intfloat.h"
32 #include "avformat.h"
33 #include "internal.h"
34
35 #define     RIFF_TAG MKTAG('R', 'I', 'F', 'F')
36 #define  FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
37 #define     LIST_TAG MKTAG('L', 'I', 'S', 'T')
38 #define     HEAD_TAG MKTAG('H', 'E', 'A', 'D')
39 #define     TRK__TAG MKTAG('T', 'R', 'K', '_')
40 #define     MOVI_TAG MKTAG('M', 'O', 'V', 'I')
41 #define     VTRK_TAG MKTAG('V', 'T', 'R', 'K')
42 #define     STRK_TAG MKTAG('S', 'T', 'R', 'K')
43 #define     std__TAG MKTAG('s', 't', 'd', '_')
44 #define     name_TAG MKTAG('n', 'a', 'm', 'e')
45 #define     vtrk_TAG MKTAG('v', 't', 'r', 'k')
46 #define     strk_TAG MKTAG('s', 't', 'r', 'k')
47 #define     ifrm_TAG MKTAG('i', 'f', 'r', 'm')
48 #define     pfrm_TAG MKTAG('p', 'f', 'r', 'm')
49 #define     cfrm_TAG MKTAG('c', 'f', 'r', 'm')
50 #define     ifr2_TAG MKTAG('i', 'f', 'r', '2')
51 #define     pfr2_TAG MKTAG('p', 'f', 'r', '2')
52 #define     cfr2_TAG MKTAG('c', 'f', 'r', '2')
53 #define     snd__TAG MKTAG('s', 'n', 'd', '_')
54
55 #define vtrk_SIZE 0x44
56 #define strk_SIZE 0x28
57
58 #define GET_LIST_HEADER() \
59     fourcc_tag = avio_rl32(pb); \
60     size = avio_rl32(pb); \
61     if (fourcc_tag != LIST_TAG) \
62         return AVERROR_INVALIDDATA; \
63     fourcc_tag = avio_rl32(pb);
64
65 typedef struct AudioTrack {
66     int sample_rate;
67     int bits;
68     int channels;
69     int stream_index;
70     int adpcm;
71     int64_t audio_pts;
72 } AudioTrack;
73
74 typedef struct FourxmDemuxContext {
75     int width;
76     int height;
77     int video_stream_index;
78     int track_count;
79     AudioTrack *tracks;
80
81     int64_t video_pts;
82     float fps;
83 } FourxmDemuxContext;
84
85 static int fourxm_probe(AVProbeData *p)
86 {
87     if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
88         (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
89         return 0;
90
91     return AVPROBE_SCORE_MAX;
92 }
93
94 static int fourxm_read_header(AVFormatContext *s)
95 {
96     AVIOContext *pb = s->pb;
97     unsigned int fourcc_tag;
98     unsigned int size;
99     int header_size;
100     FourxmDemuxContext *fourxm = s->priv_data;
101     unsigned char *header;
102     int i, ret;
103     AVStream *st;
104
105     fourxm->track_count = 0;
106     fourxm->tracks = NULL;
107     fourxm->fps = 1.0;
108
109     /* skip the first 3 32-bit numbers */
110     avio_skip(pb, 12);
111
112     /* check for LIST-HEAD */
113     GET_LIST_HEADER();
114     header_size = size - 4;
115     if (fourcc_tag != HEAD_TAG || header_size < 0)
116         return AVERROR_INVALIDDATA;
117
118     /* allocate space for the header and load the whole thing */
119     header = av_malloc(header_size);
120     if (!header)
121         return AVERROR(ENOMEM);
122     if (avio_read(pb, header, header_size) != header_size){
123         av_free(header);
124         return AVERROR(EIO);
125     }
126
127     /* take the lazy approach and search for any and all vtrk and strk chunks */
128     for (i = 0; i < header_size - 8; i++) {
129         fourcc_tag = AV_RL32(&header[i]);
130         size = AV_RL32(&header[i + 4]);
131
132         if (fourcc_tag == std__TAG) {
133             fourxm->fps = av_int2float(AV_RL32(&header[i + 12]));
134         } else if (fourcc_tag == vtrk_TAG) {
135             /* check that there is enough data */
136             if (size != vtrk_SIZE) {
137                 ret= AVERROR_INVALIDDATA;
138                 goto fail;
139             }
140             fourxm->width  = AV_RL32(&header[i + 36]);
141             fourxm->height = AV_RL32(&header[i + 40]);
142
143             /* allocate a new AVStream */
144             st = avformat_new_stream(s, NULL);
145             if (!st){
146                 ret= AVERROR(ENOMEM);
147                 goto fail;
148             }
149             avpriv_set_pts_info(st, 60, 1, fourxm->fps);
150
151             fourxm->video_stream_index = st->index;
152
153             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
154             st->codec->codec_id = CODEC_ID_4XM;
155             st->codec->extradata_size = 4;
156             st->codec->extradata = av_malloc(4);
157             AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
158             st->codec->width  = fourxm->width;
159             st->codec->height = fourxm->height;
160
161             i += 8 + size;
162         } else if (fourcc_tag == strk_TAG) {
163             int current_track;
164             /* check that there is enough data */
165             if (size != strk_SIZE) {
166                 ret= AVERROR_INVALIDDATA;
167                 goto fail;
168             }
169             current_track = AV_RL32(&header[i + 8]);
170             if((unsigned)current_track >= UINT_MAX / sizeof(AudioTrack) - 1){
171                 av_log(s, AV_LOG_ERROR, "current_track too large\n");
172                 ret= -1;
173                 goto fail;
174             }
175             if (current_track + 1 > fourxm->track_count) {
176                 fourxm->tracks = av_realloc(fourxm->tracks,
177                     (current_track + 1) * sizeof(AudioTrack));
178                 if (!fourxm->tracks) {
179                     ret = AVERROR(ENOMEM);
180                     goto fail;
181                 }
182                 memset(&fourxm->tracks[fourxm->track_count], 0,
183                        sizeof(AudioTrack) * (current_track + 1 - fourxm->track_count));
184                 fourxm->track_count = current_track + 1;
185             }
186             fourxm->tracks[current_track].adpcm       = AV_RL32(&header[i + 12]);
187             fourxm->tracks[current_track].channels    = AV_RL32(&header[i + 36]);
188             fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
189             fourxm->tracks[current_track].bits        = AV_RL32(&header[i + 44]);
190             fourxm->tracks[current_track].audio_pts   = 0;
191             if(   fourxm->tracks[current_track].channels    <= 0
192                || fourxm->tracks[current_track].sample_rate <= 0
193                || fourxm->tracks[current_track].bits        <  0){
194                 av_log(s, AV_LOG_ERROR, "audio header invalid\n");
195                 ret= -1;
196                 goto fail;
197             }
198             i += 8 + size;
199
200             /* allocate a new AVStream */
201             st = avformat_new_stream(s, NULL);
202             if (!st){
203                 ret= AVERROR(ENOMEM);
204                 goto fail;
205             }
206
207             st->id = current_track;
208             avpriv_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
209
210             fourxm->tracks[current_track].stream_index = st->index;
211
212             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
213             st->codec->codec_tag = 0;
214             st->codec->channels              = fourxm->tracks[current_track].channels;
215             st->codec->sample_rate           = fourxm->tracks[current_track].sample_rate;
216             st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
217             st->codec->bit_rate              = st->codec->channels * st->codec->sample_rate *
218                 st->codec->bits_per_coded_sample;
219             st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
220             if (fourxm->tracks[current_track].adpcm){
221                 st->codec->codec_id = CODEC_ID_ADPCM_4XM;
222             }else if (st->codec->bits_per_coded_sample == 8){
223                 st->codec->codec_id = CODEC_ID_PCM_U8;
224             }else
225                 st->codec->codec_id = CODEC_ID_PCM_S16LE;
226         }
227     }
228
229     /* skip over the LIST-MOVI chunk (which is where the stream should be */
230     GET_LIST_HEADER();
231     if (fourcc_tag != MOVI_TAG){
232         ret= AVERROR_INVALIDDATA;
233         goto fail;
234     }
235
236     av_free(header);
237     /* initialize context members */
238     fourxm->video_pts = -1;  /* first frame will push to 0 */
239
240     return 0;
241 fail:
242     av_freep(&fourxm->tracks);
243     av_free(header);
244     return ret;
245 }
246
247 static int fourxm_read_packet(AVFormatContext *s,
248                               AVPacket *pkt)
249 {
250     FourxmDemuxContext *fourxm = s->priv_data;
251     AVIOContext *pb = s->pb;
252     unsigned int fourcc_tag;
253     unsigned int size;
254     int ret = 0;
255     unsigned int track_number;
256     int packet_read = 0;
257     unsigned char header[8];
258     int audio_frame_count;
259
260     while (!packet_read) {
261
262         if ((ret = avio_read(s->pb, header, 8)) < 0)
263             return ret;
264         fourcc_tag = AV_RL32(&header[0]);
265         size = AV_RL32(&header[4]);
266         if (pb->eof_reached)
267             return AVERROR(EIO);
268         switch (fourcc_tag) {
269
270         case LIST_TAG:
271             /* this is a good time to bump the video pts */
272             fourxm->video_pts ++;
273
274             /* skip the LIST-* tag and move on to the next fourcc */
275             avio_rl32(pb);
276             break;
277
278         case ifrm_TAG:
279         case pfrm_TAG:
280         case cfrm_TAG:
281         case ifr2_TAG:
282         case pfr2_TAG:
283         case cfr2_TAG:
284             /* allocate 8 more bytes than 'size' to account for fourcc
285              * and size */
286             if (size + 8 < size || av_new_packet(pkt, size + 8))
287                 return AVERROR(EIO);
288             pkt->stream_index = fourxm->video_stream_index;
289             pkt->pts = fourxm->video_pts;
290             pkt->pos = avio_tell(s->pb);
291             memcpy(pkt->data, header, 8);
292             ret = avio_read(s->pb, &pkt->data[8], size);
293
294             if (ret < 0){
295                 av_free_packet(pkt);
296             }else
297                 packet_read = 1;
298             break;
299
300         case snd__TAG:
301             track_number = avio_rl32(pb);
302             avio_skip(pb, 4);
303             size-=8;
304
305             if (track_number < fourxm->track_count && fourxm->tracks[track_number].channels>0) {
306                 ret= av_get_packet(s->pb, pkt, size);
307                 if(ret<0)
308                     return AVERROR(EIO);
309                 pkt->stream_index =
310                     fourxm->tracks[track_number].stream_index;
311                 pkt->pts = fourxm->tracks[track_number].audio_pts;
312                 packet_read = 1;
313
314                 /* pts accounting */
315                 audio_frame_count = size;
316                 if (fourxm->tracks[track_number].adpcm)
317                     audio_frame_count -=
318                         2 * (fourxm->tracks[track_number].channels);
319                 audio_frame_count /=
320                       fourxm->tracks[track_number].channels;
321                 if (fourxm->tracks[track_number].adpcm){
322                     audio_frame_count *= 2;
323                 }else
324                     audio_frame_count /=
325                     (fourxm->tracks[track_number].bits / 8);
326                 fourxm->tracks[track_number].audio_pts += audio_frame_count;
327
328             } else {
329                 avio_skip(pb, size);
330             }
331             break;
332
333         default:
334             avio_skip(pb, size);
335             break;
336         }
337     }
338     return ret;
339 }
340
341 static int fourxm_read_close(AVFormatContext *s)
342 {
343     FourxmDemuxContext *fourxm = s->priv_data;
344
345     av_freep(&fourxm->tracks);
346
347     return 0;
348 }
349
350 AVInputFormat ff_fourxm_demuxer = {
351     .name           = "4xm",
352     .long_name      = NULL_IF_CONFIG_SMALL("4X Technologies format"),
353     .priv_data_size = sizeof(FourxmDemuxContext),
354     .read_probe     = fourxm_probe,
355     .read_header    = fourxm_read_header,
356     .read_packet    = fourxm_read_packet,
357     .read_close     = fourxm_read_close,
358 };