• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 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 
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->tracks = av_realloc_f(fourxm->tracks,
00176                                               sizeof(AudioTrack),
00177                                               current_track + 1);
00178                 if (!fourxm->tracks) {
00179                     ret = AVERROR(ENOMEM);
00180                     goto fail;
00181                 }
00182                 memset(&fourxm->tracks[fourxm->track_count], 0,
00183                        sizeof(AudioTrack) * (current_track + 1 - fourxm->track_count));
00184                 fourxm->track_count = current_track + 1;
00185             }
00186             fourxm->tracks[current_track].adpcm       = AV_RL32(&header[i + 12]);
00187             fourxm->tracks[current_track].channels    = AV_RL32(&header[i + 36]);
00188             fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
00189             fourxm->tracks[current_track].bits        = AV_RL32(&header[i + 44]);
00190             fourxm->tracks[current_track].audio_pts   = 0;
00191             if(   fourxm->tracks[current_track].channels    <= 0
00192                || fourxm->tracks[current_track].sample_rate <= 0
00193                || fourxm->tracks[current_track].bits        <  0){
00194                 av_log(s, AV_LOG_ERROR, "audio header invalid\n");
00195                 ret= -1;
00196                 goto fail;
00197             }
00198             i += 8 + size;
00199 
00200             /* allocate a new AVStream */
00201             st = av_new_stream(s, current_track);
00202             if (!st){
00203                 ret= AVERROR(ENOMEM);
00204                 goto fail;
00205             }
00206 
00207             av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
00208 
00209             fourxm->tracks[current_track].stream_index = st->index;
00210 
00211             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00212             st->codec->codec_tag = 0;
00213             st->codec->channels              = fourxm->tracks[current_track].channels;
00214             st->codec->sample_rate           = fourxm->tracks[current_track].sample_rate;
00215             st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
00216             st->codec->bit_rate              = st->codec->channels * st->codec->sample_rate *
00217                 st->codec->bits_per_coded_sample;
00218             st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00219             if (fourxm->tracks[current_track].adpcm){
00220                 st->codec->codec_id = CODEC_ID_ADPCM_4XM;
00221             }else if (st->codec->bits_per_coded_sample == 8){
00222                 st->codec->codec_id = CODEC_ID_PCM_U8;
00223             }else
00224                 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00225         }
00226     }
00227 
00228     /* skip over the LIST-MOVI chunk (which is where the stream should be */
00229     GET_LIST_HEADER();
00230     if (fourcc_tag != MOVI_TAG){
00231         ret= AVERROR_INVALIDDATA;
00232         goto fail;
00233     }
00234 
00235     av_free(header);
00236     /* initialize context members */
00237     fourxm->video_pts = -1;  /* first frame will push to 0 */
00238 
00239     return 0;
00240 fail:
00241     av_freep(&fourxm->tracks);
00242     av_free(header);
00243     return ret;
00244 }
00245 
00246 static int fourxm_read_packet(AVFormatContext *s,
00247                               AVPacket *pkt)
00248 {
00249     FourxmDemuxContext *fourxm = s->priv_data;
00250     AVIOContext *pb = s->pb;
00251     unsigned int fourcc_tag;
00252     unsigned int size;
00253     int ret = 0;
00254     unsigned int track_number;
00255     int packet_read = 0;
00256     unsigned char header[8];
00257     int audio_frame_count;
00258 
00259     while (!packet_read) {
00260 
00261         if ((ret = avio_read(s->pb, header, 8)) < 0)
00262             return ret;
00263         fourcc_tag = AV_RL32(&header[0]);
00264         size = AV_RL32(&header[4]);
00265         if (url_feof(pb))
00266             return AVERROR(EIO);
00267         switch (fourcc_tag) {
00268 
00269         case LIST_TAG:
00270             /* this is a good time to bump the video pts */
00271             fourxm->video_pts ++;
00272 
00273             /* skip the LIST-* tag and move on to the next fourcc */
00274             avio_rl32(pb);
00275             break;
00276 
00277         case ifrm_TAG:
00278         case pfrm_TAG:
00279         case cfrm_TAG:
00280         case ifr2_TAG:
00281         case pfr2_TAG:
00282         case cfr2_TAG:
00283             /* allocate 8 more bytes than 'size' to account for fourcc
00284              * and size */
00285             if (size + 8 < size || av_new_packet(pkt, size + 8))
00286                 return AVERROR(EIO);
00287             pkt->stream_index = fourxm->video_stream_index;
00288             pkt->pts = fourxm->video_pts;
00289             pkt->pos = avio_tell(s->pb);
00290             memcpy(pkt->data, header, 8);
00291             ret = avio_read(s->pb, &pkt->data[8], size);
00292 
00293             if (ret < 0){
00294                 av_free_packet(pkt);
00295             }else
00296                 packet_read = 1;
00297             break;
00298 
00299         case snd__TAG:
00300             track_number = avio_rl32(pb);
00301             avio_skip(pb, 4);
00302             size-=8;
00303 
00304             if (track_number < fourxm->track_count && fourxm->tracks[track_number].channels>0) {
00305                 ret= av_get_packet(s->pb, pkt, size);
00306                 if(ret<0)
00307                     return AVERROR(EIO);
00308                 pkt->stream_index =
00309                     fourxm->tracks[track_number].stream_index;
00310                 pkt->pts = fourxm->tracks[track_number].audio_pts;
00311                 packet_read = 1;
00312 
00313                 /* pts accounting */
00314                 audio_frame_count = size;
00315                 if (fourxm->tracks[track_number].adpcm)
00316                     audio_frame_count -=
00317                         2 * (fourxm->tracks[track_number].channels);
00318                 audio_frame_count /=
00319                       fourxm->tracks[track_number].channels;
00320                 if (fourxm->tracks[track_number].adpcm){
00321                     audio_frame_count *= 2;
00322                 }else
00323                     audio_frame_count /=
00324                     (fourxm->tracks[track_number].bits / 8);
00325                 fourxm->tracks[track_number].audio_pts += audio_frame_count;
00326 
00327             } else {
00328                 avio_skip(pb, size);
00329             }
00330             break;
00331 
00332         default:
00333             avio_skip(pb, size);
00334             break;
00335         }
00336     }
00337     return ret;
00338 }
00339 
00340 static int fourxm_read_close(AVFormatContext *s)
00341 {
00342     FourxmDemuxContext *fourxm = s->priv_data;
00343 
00344     av_freep(&fourxm->tracks);
00345 
00346     return 0;
00347 }
00348 
00349 AVInputFormat ff_fourxm_demuxer = {
00350     "4xm",
00351     NULL_IF_CONFIG_SMALL("4X Technologies format"),
00352     sizeof(FourxmDemuxContext),
00353     fourxm_probe,
00354     fourxm_read_header,
00355     fourxm_read_packet,
00356     fourxm_read_close,
00357 };

Generated on Wed Apr 11 2012 07:31:32 for FFmpeg by  doxygen 1.7.1