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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 /**
00023  * @file 4xm.c
00024  * 4X Technologies file demuxer
00025  * by Mike Melanson (melanson@pcisys.net)
00026  * for more information on the .4xm file format, visit:
00027  *   http://www.pcisys.net/~melanson/codecs/
00028  */
00029 
00030 #include "avformat.h"
00031 
00032 #define  RIFF_TAG MKTAG('R', 'I', 'F', 'F')
00033 #define _4XMV_TAG MKTAG('4', 'X', 'M', 'V')
00034 #define  LIST_TAG MKTAG('L', 'I', 'S', 'T')
00035 #define  HEAD_TAG MKTAG('H', 'E', 'A', 'D')
00036 #define  TRK__TAG MKTAG('T', 'R', 'K', '_')
00037 #define  MOVI_TAG MKTAG('M', 'O', 'V', 'I')
00038 #define  VTRK_TAG MKTAG('V', 'T', 'R', 'K')
00039 #define  STRK_TAG MKTAG('S', 'T', 'R', 'K')
00040 #define  std__TAG MKTAG('s', 't', 'd', '_')
00041 #define  name_TAG MKTAG('n', 'a', 'm', 'e')
00042 #define  vtrk_TAG MKTAG('v', 't', 'r', 'k')
00043 #define  strk_TAG MKTAG('s', 't', 'r', 'k')
00044 #define  ifrm_TAG MKTAG('i', 'f', 'r', 'm')
00045 #define  pfrm_TAG MKTAG('p', 'f', 'r', 'm')
00046 #define  cfrm_TAG MKTAG('c', 'f', 'r', 'm')
00047 #define  ifr2_TAG MKTAG('i', 'f', 'r', '2')
00048 #define  pfr2_TAG MKTAG('p', 'f', 'r', '2')
00049 #define  cfr2_TAG MKTAG('c', 'f', 'r', '2')
00050 #define  snd__TAG MKTAG('s', 'n', 'd', '_')
00051 
00052 #define vtrk_SIZE 0x44
00053 #define strk_SIZE 0x28
00054 
00055 #define GET_LIST_HEADER() \
00056     fourcc_tag = get_le32(pb); \
00057     size = get_le32(pb); \
00058     if (fourcc_tag != LIST_TAG) \
00059         return AVERROR_INVALIDDATA; \
00060     fourcc_tag = get_le32(pb);
00061 
00062 typedef struct AudioTrack {
00063     int sample_rate;
00064     int bits;
00065     int channels;
00066     int stream_index;
00067     int adpcm;
00068 } AudioTrack;
00069 
00070 typedef struct FourxmDemuxContext {
00071     int width;
00072     int height;
00073     int video_stream_index;
00074     int track_count;
00075     AudioTrack *tracks;
00076     int selected_track;
00077 
00078     int64_t audio_pts;
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]) != _4XMV_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     ByteIOContext *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;
00102     int current_track = -1;
00103     AVStream *st;
00104 
00105     fourxm->track_count = 0;
00106     fourxm->tracks = NULL;
00107     fourxm->selected_track = 0;
00108     fourxm->fps = 1.0;
00109 
00110     /* skip the first 3 32-bit numbers */
00111     url_fseek(pb, 12, SEEK_CUR);
00112 
00113     /* check for LIST-HEAD */
00114     GET_LIST_HEADER();
00115     header_size = size - 4;
00116     if (fourcc_tag != HEAD_TAG)
00117         return AVERROR_INVALIDDATA;
00118 
00119     /* allocate space for the header and load the whole thing */
00120     header = av_malloc(header_size);
00121     if (!header)
00122         return AVERROR(ENOMEM);
00123     if (get_buffer(pb, header, header_size) != header_size)
00124         return AVERROR(EIO);
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                 av_free(header);
00137                 return AVERROR_INVALIDDATA;
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                 return AVERROR(ENOMEM);
00146             av_set_pts_info(st, 60, 1, fourxm->fps);
00147 
00148             fourxm->video_stream_index = st->index;
00149 
00150             st->codec->codec_type = CODEC_TYPE_VIDEO;
00151             st->codec->codec_id = CODEC_ID_4XM;
00152             st->codec->extradata_size = 4;
00153             st->codec->extradata = av_malloc(4);
00154             AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
00155             st->codec->width = fourxm->width;
00156             st->codec->height = fourxm->height;
00157 
00158             i += 8 + size;
00159         } else if (fourcc_tag == strk_TAG) {
00160             /* check that there is enough data */
00161             if (size != strk_SIZE) {
00162                 av_free(header);
00163                 return AVERROR_INVALIDDATA;
00164             }
00165             current_track = AV_RL32(&header[i + 8]);
00166             if (current_track + 1 > fourxm->track_count) {
00167                 fourxm->track_count = current_track + 1;
00168                 if((unsigned)fourxm->track_count >= UINT_MAX / sizeof(AudioTrack))
00169                     return -1;
00170                 fourxm->tracks = av_realloc(fourxm->tracks,
00171                     fourxm->track_count * sizeof(AudioTrack));
00172                 if (!fourxm->tracks) {
00173                     av_free(header);
00174                     return AVERROR(ENOMEM);
00175                 }
00176             }
00177             fourxm->tracks[current_track].adpcm = AV_RL32(&header[i + 12]);
00178             fourxm->tracks[current_track].channels = AV_RL32(&header[i + 36]);
00179             fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
00180             fourxm->tracks[current_track].bits = AV_RL32(&header[i + 44]);
00181             i += 8 + size;
00182 
00183             /* allocate a new AVStream */
00184             st = av_new_stream(s, current_track);
00185             if (!st)
00186                 return AVERROR(ENOMEM);
00187 
00188             av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
00189 
00190             fourxm->tracks[current_track].stream_index = st->index;
00191 
00192             st->codec->codec_type = CODEC_TYPE_AUDIO;
00193             st->codec->codec_tag = 0;
00194             st->codec->channels = fourxm->tracks[current_track].channels;
00195             st->codec->sample_rate = fourxm->tracks[current_track].sample_rate;
00196             st->codec->bits_per_sample = fourxm->tracks[current_track].bits;
00197             st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00198                 st->codec->bits_per_sample;
00199             st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
00200             if (fourxm->tracks[current_track].adpcm)
00201                 st->codec->codec_id = CODEC_ID_ADPCM_4XM;
00202             else if (st->codec->bits_per_sample == 8)
00203                 st->codec->codec_id = CODEC_ID_PCM_U8;
00204             else
00205                 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00206         }
00207     }
00208 
00209     av_free(header);
00210 
00211     /* skip over the LIST-MOVI chunk (which is where the stream should be */
00212     GET_LIST_HEADER();
00213     if (fourcc_tag != MOVI_TAG)
00214         return AVERROR_INVALIDDATA;
00215 
00216     /* initialize context members */
00217     fourxm->video_pts = -1;  /* first frame will push to 0 */
00218     fourxm->audio_pts = 0;
00219 
00220     return 0;
00221 }
00222 
00223 static int fourxm_read_packet(AVFormatContext *s,
00224                               AVPacket *pkt)
00225 {
00226     FourxmDemuxContext *fourxm = s->priv_data;
00227     ByteIOContext *pb = s->pb;
00228     unsigned int fourcc_tag;
00229     unsigned int size, out_size;
00230     int ret = 0;
00231     int track_number;
00232     int packet_read = 0;
00233     unsigned char header[8];
00234     int audio_frame_count;
00235 
00236     while (!packet_read) {
00237 
00238         if ((ret = get_buffer(s->pb, header, 8)) < 0)
00239             return ret;
00240         fourcc_tag = AV_RL32(&header[0]);
00241         size = AV_RL32(&header[4]);
00242         if (url_feof(pb))
00243             return AVERROR(EIO);
00244         switch (fourcc_tag) {
00245 
00246         case LIST_TAG:
00247             /* this is a good time to bump the video pts */
00248             fourxm->video_pts ++;
00249 
00250             /* skip the LIST-* tag and move on to the next fourcc */
00251             get_le32(pb);
00252             break;
00253 
00254         case ifrm_TAG:
00255         case pfrm_TAG:
00256         case cfrm_TAG:
00257         case ifr2_TAG:
00258         case pfr2_TAG:
00259         case cfr2_TAG:
00260         {
00261 
00262             /* allocate 8 more bytes than 'size' to account for fourcc
00263              * and size */
00264             if (size + 8 < size || av_new_packet(pkt, size + 8))
00265                 return AVERROR(EIO);
00266             pkt->stream_index = fourxm->video_stream_index;
00267             pkt->pts = fourxm->video_pts;
00268             pkt->pos = url_ftell(s->pb);
00269             memcpy(pkt->data, header, 8);
00270             ret = get_buffer(s->pb, &pkt->data[8], size);
00271 
00272             if (ret < 0)
00273                 av_free_packet(pkt);
00274             else
00275                 packet_read = 1;
00276             break;
00277         }
00278 
00279         case snd__TAG:
00280             track_number = get_le32(pb);
00281             out_size= get_le32(pb);
00282             size-=8;
00283 
00284             if (track_number == fourxm->selected_track) {
00285                 ret= av_get_packet(s->pb, pkt, size);
00286                 if(ret<0)
00287                     return AVERROR(EIO);
00288                 pkt->stream_index =
00289                     fourxm->tracks[fourxm->selected_track].stream_index;
00290                 pkt->pts = fourxm->audio_pts;
00291                 packet_read = 1;
00292 
00293                 /* pts accounting */
00294                 audio_frame_count = size;
00295                 if (fourxm->tracks[fourxm->selected_track].adpcm)
00296                     audio_frame_count -=
00297                         2 * (fourxm->tracks[fourxm->selected_track].channels);
00298                 audio_frame_count /=
00299                       fourxm->tracks[fourxm->selected_track].channels;
00300                 if (fourxm->tracks[fourxm->selected_track].adpcm)
00301                     audio_frame_count *= 2;
00302                 else
00303                     audio_frame_count /=
00304                     (fourxm->tracks[fourxm->selected_track].bits / 8);
00305                 fourxm->audio_pts += audio_frame_count;
00306 
00307             } else {
00308                 url_fseek(pb, size, SEEK_CUR);
00309             }
00310             break;
00311 
00312         default:
00313             url_fseek(pb, size, SEEK_CUR);
00314             break;
00315         }
00316     }
00317     return ret;
00318 }
00319 
00320 static int fourxm_read_close(AVFormatContext *s)
00321 {
00322     FourxmDemuxContext *fourxm = s->priv_data;
00323 
00324     av_free(fourxm->tracks);
00325 
00326     return 0;
00327 }
00328 
00329 AVInputFormat fourxm_demuxer = {
00330     "4xm",
00331     NULL_IF_CONFIG_SMALL("4X Technologies format"),
00332     sizeof(FourxmDemuxContext),
00333     fourxm_probe,
00334     fourxm_read_header,
00335     fourxm_read_packet,
00336     fourxm_read_close,
00337 };

Generated on Thu Aug 28 16:44:14 2008 for libextractor by  doxygen 1.5.1