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

libavformat/thp.c

Go to the documentation of this file.
00001 /*
00002  * THP Demuxer
00003  * Copyright (c) 2007 Marco Gerards
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 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 
00025 typedef struct ThpDemuxContext {
00026     int              version;
00027     int              first_frame;
00028     int              first_framesz;
00029     int              last_frame;
00030     int              compoff;
00031     int              framecnt;
00032     AVRational       fps;
00033     int              frame;
00034     int              next_frame;
00035     int              next_framesz;
00036     int              video_stream_index;
00037     int              audio_stream_index;
00038     int              compcount;
00039     unsigned char    components[16];
00040     AVStream*        vst;
00041     int              has_audio;
00042     int              audiosize;
00043 } ThpDemuxContext;
00044 
00045 
00046 static int thp_probe(AVProbeData *p)
00047 {
00048     /* check file header */
00049     if (AV_RL32(p->buf) == MKTAG('T', 'H', 'P', '\0'))
00050         return AVPROBE_SCORE_MAX;
00051     else
00052         return 0;
00053 }
00054 
00055 static int thp_read_header(AVFormatContext *s,
00056                            AVFormatParameters *ap)
00057 {
00058     ThpDemuxContext *thp = s->priv_data;
00059     AVStream *st;
00060     ByteIOContext *pb = s->pb;
00061     int i;
00062 
00063     /* Read the file header.  */
00064                            get_be32(pb); /* Skip Magic.  */
00065     thp->version         = get_be32(pb);
00066 
00067                            get_be32(pb); /* Max buf size.  */
00068                            get_be32(pb); /* Max samples.  */
00069 
00070     thp->fps             = av_d2q(av_int2flt(get_be32(pb)), INT_MAX);
00071     thp->framecnt        = get_be32(pb);
00072     thp->first_framesz   = get_be32(pb);
00073                            get_be32(pb); /* Data size.  */
00074 
00075     thp->compoff         = get_be32(pb);
00076                            get_be32(pb); /* offsetDataOffset.  */
00077     thp->first_frame     = get_be32(pb);
00078     thp->last_frame      = get_be32(pb);
00079 
00080     thp->next_framesz    = thp->first_framesz;
00081     thp->next_frame      = thp->first_frame;
00082 
00083     /* Read the component structure.  */
00084     url_fseek (pb, thp->compoff, SEEK_SET);
00085     thp->compcount       = get_be32(pb);
00086 
00087     /* Read the list of component types.  */
00088     get_buffer(pb, thp->components, 16);
00089 
00090     for (i = 0; i < thp->compcount; i++) {
00091         if (thp->components[i] == 0) {
00092             if (thp->vst != 0)
00093                 break;
00094 
00095             /* Video component.  */
00096             st = av_new_stream(s, 0);
00097             if (!st)
00098                 return AVERROR(ENOMEM);
00099 
00100             /* The denominator and numerator are switched because 1/fps
00101                is required.  */
00102             av_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
00103             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00104             st->codec->codec_id = CODEC_ID_THP;
00105             st->codec->codec_tag = 0;  /* no fourcc */
00106             st->codec->width = get_be32(pb);
00107             st->codec->height = get_be32(pb);
00108             st->codec->sample_rate = av_q2d(thp->fps);
00109             thp->vst = st;
00110             thp->video_stream_index = st->index;
00111 
00112             if (thp->version == 0x11000)
00113                 get_be32(pb); /* Unknown.  */
00114         } else if (thp->components[i] == 1) {
00115             if (thp->has_audio != 0)
00116                 break;
00117 
00118             /* Audio component.  */
00119             st = av_new_stream(s, 0);
00120             if (!st)
00121                 return AVERROR(ENOMEM);
00122 
00123             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00124             st->codec->codec_id = CODEC_ID_ADPCM_THP;
00125             st->codec->codec_tag = 0;  /* no fourcc */
00126             st->codec->channels    = get_be32(pb); /* numChannels.  */
00127             st->codec->sample_rate = get_be32(pb); /* Frequency.  */
00128 
00129             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00130 
00131             thp->audio_stream_index = st->index;
00132             thp->has_audio = 1;
00133         }
00134     }
00135 
00136     return 0;
00137 }
00138 
00139 static int thp_read_packet(AVFormatContext *s,
00140                             AVPacket *pkt)
00141 {
00142     ThpDemuxContext *thp = s->priv_data;
00143     ByteIOContext *pb = s->pb;
00144     int size;
00145     int ret;
00146 
00147     if (thp->audiosize == 0) {
00148         /* Terminate when last frame is reached.  */
00149         if (thp->frame >= thp->framecnt)
00150             return AVERROR(EIO);
00151 
00152         url_fseek(pb, thp->next_frame, SEEK_SET);
00153 
00154         /* Locate the next frame and read out its size.  */
00155         thp->next_frame += thp->next_framesz;
00156         thp->next_framesz = get_be32(pb);
00157 
00158                         get_be32(pb); /* Previous total size.  */
00159         size          = get_be32(pb); /* Total size of this frame.  */
00160 
00161         /* Store the audiosize so the next time this function is called,
00162            the audio can be read.  */
00163         if (thp->has_audio)
00164             thp->audiosize = get_be32(pb); /* Audio size.  */
00165         else
00166             thp->frame++;
00167 
00168         ret = av_get_packet(pb, pkt, size);
00169         if (ret != size) {
00170             av_free_packet(pkt);
00171             return AVERROR(EIO);
00172         }
00173 
00174         pkt->stream_index = thp->video_stream_index;
00175     } else {
00176         ret = av_get_packet(pb, pkt, thp->audiosize);
00177         if (ret != thp->audiosize) {
00178             av_free_packet(pkt);
00179             return AVERROR(EIO);
00180         }
00181 
00182         pkt->stream_index = thp->audio_stream_index;
00183         thp->audiosize = 0;
00184         thp->frame++;
00185     }
00186 
00187     return 0;
00188 }
00189 
00190 AVInputFormat thp_demuxer = {
00191     "thp",
00192     NULL_IF_CONFIG_SMALL("THP"),
00193     sizeof(ThpDemuxContext),
00194     thp_probe,
00195     thp_read_header,
00196     thp_read_packet
00197 };

Generated on Fri Sep 16 2011 17:17:51 for FFmpeg by  doxygen 1.7.1