Libav 0.7.1
libavformat/4xm.c
Go to the documentation of this file.
00001 /*
00002  * 4X Technologies .4xm File Demuxer (no muxer)
00003  * Copyright (c) 2003  The ffmpeg Project
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00030 #include "libavutil/intreadwrite.h"
00031 #include "avformat.h"
00032 
00033 #define     RIFF_TAG MKTAG('R', 'I', 'F', 'F')
00034 #define  FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
00035 #define     LIST_TAG MKTAG('L', 'I', 'S', 'T')
00036 #define     HEAD_TAG MKTAG('H', 'E', 'A', 'D')
00037 #define     TRK__TAG MKTAG('T', 'R', 'K', '_')
00038 #define     MOVI_TAG MKTAG('M', 'O', 'V', 'I')
00039 #define     VTRK_TAG MKTAG('V', 'T', 'R', 'K')
00040 #define     STRK_TAG MKTAG('S', 'T', 'R', 'K')
00041 #define     std__TAG MKTAG('s', 't', 'd', '_')
00042 #define     name_TAG MKTAG('n', 'a', 'm', 'e')
00043 #define     vtrk_TAG MKTAG('v', 't', 'r', 'k')
00044 #define     strk_TAG MKTAG('s', 't', 'r', 'k')
00045 #define     ifrm_TAG MKTAG('i', 'f', 'r', 'm')
00046 #define     pfrm_TAG MKTAG('p', 'f', 'r', 'm')
00047 #define     cfrm_TAG MKTAG('c', 'f', 'r', 'm')
00048 #define     ifr2_TAG MKTAG('i', 'f', 'r', '2')
00049 #define     pfr2_TAG MKTAG('p', 'f', 'r', '2')
00050 #define     cfr2_TAG MKTAG('c', 'f', 'r', '2')
00051 #define     snd__TAG MKTAG('s', 'n', 'd', '_')
00052 
00053 #define vtrk_SIZE 0x44
00054 #define strk_SIZE 0x28
00055 
00056 #define GET_LIST_HEADER() \
00057     fourcc_tag = avio_rl32(pb); \
00058     size = avio_rl32(pb); \
00059     if (fourcc_tag != LIST_TAG) \
00060         return AVERROR_INVALIDDATA; \
00061     fourcc_tag = avio_rl32(pb);
00062 
00063 typedef struct AudioTrack {
00064     int sample_rate;
00065     int bits;
00066     int channels;
00067     int stream_index;
00068     int adpcm;
00069     int64_t audio_pts;
00070 } AudioTrack;
00071 
00072 typedef struct FourxmDemuxContext {
00073     int width;
00074     int height;
00075     int video_stream_index;
00076     int track_count;
00077     AudioTrack *tracks;
00078 
00079     int64_t video_pts;
00080     float fps;
00081 } FourxmDemuxContext;
00082 
00083 static int fourxm_probe(AVProbeData *p)
00084 {
00085     if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
00086         (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
00087         return 0;
00088 
00089     return AVPROBE_SCORE_MAX;
00090 }
00091 
00092 static int fourxm_read_header(AVFormatContext *s,
00093                               AVFormatParameters *ap)
00094 {
00095     AVIOContext *pb = s->pb;
00096     unsigned int fourcc_tag;
00097     unsigned int size;
00098     int header_size;
00099     FourxmDemuxContext *fourxm = s->priv_data;
00100     unsigned char *header;
00101     int i, ret;
00102     AVStream *st;
00103 
00104     fourxm->track_count = 0;
00105     fourxm->tracks = NULL;
00106     fourxm->fps = 1.0;
00107 
00108     /* skip the first 3 32-bit numbers */
00109     avio_skip(pb, 12);
00110 
00111     /* check for LIST-HEAD */
00112     GET_LIST_HEADER();
00113     header_size = size - 4;
00114     if (fourcc_tag != HEAD_TAG || header_size < 0)
00115         return AVERROR_INVALIDDATA;
00116 
00117     /* allocate space for the header and load the whole thing */
00118     header = av_malloc(header_size);
00119     if (!header)
00120         return AVERROR(ENOMEM);
00121     if (avio_read(pb, header, header_size) != header_size){
00122         av_free(header);
00123         return AVERROR(EIO);
00124     }
00125 
00126     /* take the lazy approach and search for any and all vtrk and strk chunks */
00127     for (i = 0; i < header_size - 8; i++) {
00128         fourcc_tag = AV_RL32(&header[i]);
00129         size = AV_RL32(&header[i + 4]);
00130 
00131         if (fourcc_tag == std__TAG) {
00132             fourxm->fps = av_int2flt(AV_RL32(&header[i + 12]));
00133         } else if (fourcc_tag == vtrk_TAG) {
00134             /* check that there is enough data */
00135             if (size != vtrk_SIZE) {
00136                 ret= AVERROR_INVALIDDATA;
00137                 goto fail;
00138             }
00139             fourxm->width  = AV_RL32(&header[i + 36]);
00140             fourxm->height = AV_RL32(&header[i + 40]);
00141 
00142             /* allocate a new AVStream */
00143             st = av_new_stream(s, 0);
00144             if (!st){
00145                 ret= AVERROR(ENOMEM);
00146                 goto fail;
00147             }
00148             av_set_pts_info(st, 60, 1, fourxm->fps);
00149 
00150             fourxm->video_stream_index = st->index;
00151 
00152             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00153             st->codec->codec_id = CODEC_ID_4XM;
00154             st->codec->extradata_size = 4;
00155             st->codec->extradata = av_malloc(4);
00156             AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
00157             st->codec->width  = fourxm->width;
00158             st->codec->height = fourxm->height;
00159 
00160             i += 8 + size;
00161         } else if (fourcc_tag == strk_TAG) {
00162             int current_track;
00163             /* check that there is enough data */
00164             if (size != strk_SIZE) {
00165                 ret= AVERROR_INVALIDDATA;
00166                 goto fail;
00167             }
00168             current_track = AV_RL32(&header[i + 8]);
00169             if((unsigned)current_track >= UINT_MAX / sizeof(AudioTrack) - 1){
00170                 av_log(s, AV_LOG_ERROR, "current_track too large\n");
00171                 ret= -1;
00172                 goto fail;
00173             }
00174             if (current_track + 1 > fourxm->track_count) {
00175                 fourxm->track_count = current_track + 1;
00176                 fourxm->tracks = av_realloc(fourxm->tracks,
00177                     fourxm->track_count * sizeof(AudioTrack));
00178                 if (!fourxm->tracks) {
00179                     ret=  AVERROR(ENOMEM);
00180                     goto fail;
00181                 }
00182             }
00183             fourxm->tracks[current_track].adpcm       = AV_RL32(&header[i + 12]);
00184             fourxm->tracks[current_track].channels    = AV_RL32(&header[i + 36]);
00185             fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
00186             fourxm->tracks[current_track].bits        = AV_RL32(&header[i + 44]);
00187             fourxm->tracks[current_track].audio_pts   = 0;
00188             if(   fourxm->tracks[current_track].channels    <= 0
00189                || fourxm->tracks[current_track].sample_rate <= 0
00190                || fourxm->tracks[current_track].bits        <  0){
00191                 av_log(s, AV_LOG_ERROR, "audio header invalid\n");
00192                 ret= -1;
00193                 goto fail;
00194             }
00195             i += 8 + size;
00196 
00197             /* allocate a new AVStream */
00198             st = av_new_stream(s, current_track);
00199             if (!st){
00200                 ret= AVERROR(ENOMEM);
00201                 goto fail;
00202             }
00203 
00204             av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
00205 
00206             fourxm->tracks[current_track].stream_index = st->index;
00207 
00208             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00209             st->codec->codec_tag = 0;
00210             st->codec->channels              = fourxm->tracks[current_track].channels;
00211             st->codec->sample_rate           = fourxm->tracks[current_track].sample_rate;
00212             st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
00213             st->codec->bit_rate              = st->codec->channels * st->codec->sample_rate *
00214                 st->codec->bits_per_coded_sample;
00215             st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00216             if (fourxm->tracks[current_track].adpcm){
00217                 st->codec->codec_id = CODEC_ID_ADPCM_4XM;
00218             }else if (st->codec->bits_per_coded_sample == 8){
00219                 st->codec->codec_id = CODEC_ID_PCM_U8;
00220             }else
00221                 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00222         }
00223     }
00224 
00225     /* skip over the LIST-MOVI chunk (which is where the stream should be */
00226     GET_LIST_HEADER();
00227     if (fourcc_tag != MOVI_TAG){
00228         ret= AVERROR_INVALIDDATA;
00229         goto fail;
00230     }
00231 
00232     av_free(header);
00233     /* initialize context members */
00234     fourxm->video_pts = -1;  /* first frame will push to 0 */
00235 
00236     return 0;
00237 fail:
00238     av_freep(&fourxm->tracks);
00239     av_free(header);
00240     return ret;
00241 }
00242 
00243 static int fourxm_read_packet(AVFormatContext *s,
00244                               AVPacket *pkt)
00245 {
00246     FourxmDemuxContext *fourxm = s->priv_data;
00247     AVIOContext *pb = s->pb;
00248     unsigned int fourcc_tag;
00249     unsigned int size;
00250     int ret = 0;
00251     unsigned int track_number;
00252     int packet_read = 0;
00253     unsigned char header[8];
00254     int audio_frame_count;
00255 
00256     while (!packet_read) {
00257 
00258         if ((ret = avio_read(s->pb, header, 8)) < 0)
00259             return ret;
00260         fourcc_tag = AV_RL32(&header[0]);
00261         size = AV_RL32(&header[4]);
00262         if (pb->eof_reached)
00263             return AVERROR(EIO);
00264         switch (fourcc_tag) {
00265 
00266         case LIST_TAG:
00267             /* this is a good time to bump the video pts */
00268             fourxm->video_pts ++;
00269 
00270             /* skip the LIST-* tag and move on to the next fourcc */
00271             avio_rl32(pb);
00272             break;
00273 
00274         case ifrm_TAG:
00275         case pfrm_TAG:
00276         case cfrm_TAG:
00277         case ifr2_TAG:
00278         case pfr2_TAG:
00279         case cfr2_TAG:
00280             /* allocate 8 more bytes than 'size' to account for fourcc
00281              * and size */
00282             if (size + 8 < size || av_new_packet(pkt, size + 8))
00283                 return AVERROR(EIO);
00284             pkt->stream_index = fourxm->video_stream_index;
00285             pkt->pts = fourxm->video_pts;
00286             pkt->pos = avio_tell(s->pb);
00287             memcpy(pkt->data, header, 8);
00288             ret = avio_read(s->pb, &pkt->data[8], size);
00289 
00290             if (ret < 0){
00291                 av_free_packet(pkt);
00292             }else
00293                 packet_read = 1;
00294             break;
00295 
00296         case snd__TAG:
00297             track_number = avio_rl32(pb);
00298             avio_skip(pb, 4);
00299             size-=8;
00300 
00301             if (track_number < fourxm->track_count && fourxm->tracks[track_number].channels>0) {
00302                 ret= av_get_packet(s->pb, pkt, size);
00303                 if(ret<0)
00304                     return AVERROR(EIO);
00305                 pkt->stream_index =
00306                     fourxm->tracks[track_number].stream_index;
00307                 pkt->pts = fourxm->tracks[track_number].audio_pts;
00308                 packet_read = 1;
00309 
00310                 /* pts accounting */
00311                 audio_frame_count = size;
00312                 if (fourxm->tracks[track_number].adpcm)
00313                     audio_frame_count -=
00314                         2 * (fourxm->tracks[track_number].channels);
00315                 audio_frame_count /=
00316                       fourxm->tracks[track_number].channels;
00317                 if (fourxm->tracks[track_number].adpcm){
00318                     audio_frame_count *= 2;
00319                 }else
00320                     audio_frame_count /=
00321                     (fourxm->tracks[track_number].bits / 8);
00322                 fourxm->tracks[track_number].audio_pts += audio_frame_count;
00323 
00324             } else {
00325                 avio_skip(pb, size);
00326             }
00327             break;
00328 
00329         default:
00330             avio_skip(pb, size);
00331             break;
00332         }
00333     }
00334     return ret;
00335 }
00336 
00337 static int fourxm_read_close(AVFormatContext *s)
00338 {
00339     FourxmDemuxContext *fourxm = s->priv_data;
00340 
00341     av_freep(&fourxm->tracks);
00342 
00343     return 0;
00344 }
00345 
00346 AVInputFormat ff_fourxm_demuxer = {
00347     "4xm",
00348     NULL_IF_CONFIG_SMALL("4X Technologies format"),
00349     sizeof(FourxmDemuxContext),
00350     fourxm_probe,
00351     fourxm_read_header,
00352     fourxm_read_packet,
00353     fourxm_read_close,
00354 };