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