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

libavformat/siff.c

Go to the documentation of this file.
00001 /*
00002  * Beam Software SIFF demuxer
00003  * Copyright (c) 2007 Konstantin Shishkov
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 
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 
00026 enum SIFFTags{
00027     TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
00028     TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
00029     TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
00030     TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
00031     TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
00032     TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
00033 };
00034 
00035 enum VBFlags{
00036     VB_HAS_GMC     = 0x01,
00037     VB_HAS_AUDIO   = 0x04,
00038     VB_HAS_VIDEO   = 0x08,
00039     VB_HAS_PALETTE = 0x10,
00040     VB_HAS_LENGTH  = 0x20
00041 };
00042 
00043 typedef struct SIFFContext{
00044     int frames;
00045     int cur_frame;
00046     int rate;
00047     int bits;
00048     int block_align;
00049 
00050     int has_video;
00051     int has_audio;
00052 
00053     int curstrm;
00054     int pktsize;
00055     int gmcsize;
00056     int sndsize;
00057 
00058     int flags;
00059     uint8_t gmc[4];
00060 }SIFFContext;
00061 
00062 static int siff_probe(AVProbeData *p)
00063 {
00064     uint32_t tag = AV_RL32(p->buf + 8);
00065     /* check file header */
00066     if (AV_RL32(p->buf) != TAG_SIFF ||
00067         (tag != TAG_VBV1 && tag != TAG_SOUN))
00068         return 0;
00069     return AVPROBE_SCORE_MAX;
00070 }
00071 
00072 static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
00073 {
00074     AVStream *ast;
00075     ast = avformat_new_stream(s, NULL);
00076     if (!ast)
00077         return -1;
00078     ast->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
00079     ast->codec->codec_id        = CODEC_ID_PCM_U8;
00080     ast->codec->channels        = 1;
00081     ast->codec->bits_per_coded_sample = c->bits;
00082     ast->codec->sample_rate     = c->rate;
00083     ast->codec->frame_size      = c->block_align;
00084     avpriv_set_pts_info(ast, 16, 1, c->rate);
00085     return 0;
00086 }
00087 
00088 static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
00089 {
00090     AVStream *st;
00091     int width, height;
00092 
00093     if (avio_rl32(pb) != TAG_VBHD){
00094         av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
00095         return -1;
00096     }
00097     if(avio_rb32(pb) != 32){
00098         av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
00099         return -1;
00100     }
00101     if(avio_rl16(pb) != 1){
00102         av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
00103         return -1;
00104     }
00105     width = avio_rl16(pb);
00106     height = avio_rl16(pb);
00107     avio_skip(pb, 4);
00108     c->frames = avio_rl16(pb);
00109     if(!c->frames){
00110         av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
00111         return -1;
00112     }
00113     c->bits = avio_rl16(pb);
00114     c->rate = avio_rl16(pb);
00115     c->block_align = c->rate * (c->bits >> 3);
00116 
00117     avio_skip(pb, 16); //zeroes
00118 
00119     st = avformat_new_stream(s, NULL);
00120     if (!st)
00121         return -1;
00122     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00123     st->codec->codec_id   = CODEC_ID_VB;
00124     st->codec->codec_tag  = MKTAG('V', 'B', 'V', '1');
00125     st->codec->width      = width;
00126     st->codec->height     = height;
00127     st->codec->pix_fmt    = PIX_FMT_PAL8;
00128     avpriv_set_pts_info(st, 16, 1, 12);
00129 
00130     c->cur_frame = 0;
00131     c->has_video = 1;
00132     c->has_audio = !!c->rate;
00133     c->curstrm = -1;
00134     if (c->has_audio && create_audio_stream(s, c) < 0)
00135         return -1;
00136     return 0;
00137 }
00138 
00139 static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
00140 {
00141     if (avio_rl32(pb) != TAG_SHDR){
00142         av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
00143         return -1;
00144     }
00145     if(avio_rb32(pb) != 8){
00146         av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
00147         return -1;
00148     }
00149     avio_skip(pb, 4); //unknown value
00150     c->rate = avio_rl16(pb);
00151     c->bits = avio_rl16(pb);
00152     c->block_align = c->rate * (c->bits >> 3);
00153     return create_audio_stream(s, c);
00154 }
00155 
00156 static int siff_read_header(AVFormatContext *s, AVFormatParameters *ap)
00157 {
00158     AVIOContext *pb = s->pb;
00159     SIFFContext *c = s->priv_data;
00160     uint32_t tag;
00161 
00162     if (avio_rl32(pb) != TAG_SIFF)
00163         return -1;
00164     avio_skip(pb, 4); //ignore size
00165     tag = avio_rl32(pb);
00166 
00167     if (tag != TAG_VBV1 && tag != TAG_SOUN){
00168         av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
00169         return -1;
00170     }
00171 
00172     if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
00173         return -1;
00174     if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
00175         return -1;
00176     if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
00177         av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
00178         return -1;
00179     }
00180     avio_skip(pb, 4); //ignore size
00181 
00182     return 0;
00183 }
00184 
00185 static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
00186 {
00187     SIFFContext *c = s->priv_data;
00188     int size;
00189 
00190     if (c->has_video){
00191         if (c->cur_frame >= c->frames)
00192             return AVERROR(EIO);
00193         if (c->curstrm == -1){
00194             c->pktsize = avio_rl32(s->pb) - 4;
00195             c->flags = avio_rl16(s->pb);
00196             c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
00197             if (c->gmcsize)
00198                 avio_read(s->pb, c->gmc, c->gmcsize);
00199             c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
00200             c->curstrm = !!(c->flags & VB_HAS_AUDIO);
00201         }
00202 
00203         if (!c->curstrm){
00204             size = c->pktsize - c->sndsize;
00205             if (av_new_packet(pkt, size) < 0)
00206                 return AVERROR(ENOMEM);
00207             AV_WL16(pkt->data, c->flags);
00208             if (c->gmcsize)
00209                 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
00210             avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
00211             pkt->stream_index = 0;
00212             c->curstrm = -1;
00213         }else{
00214             if (av_get_packet(s->pb, pkt, c->sndsize - 4) < 0)
00215                 return AVERROR(EIO);
00216             pkt->stream_index = 1;
00217             c->curstrm = 0;
00218         }
00219         if(!c->cur_frame || c->curstrm)
00220             pkt->flags |= AV_PKT_FLAG_KEY;
00221         if (c->curstrm == -1)
00222             c->cur_frame++;
00223     }else{
00224         size = av_get_packet(s->pb, pkt, c->block_align);
00225         if(size <= 0)
00226             return AVERROR(EIO);
00227     }
00228     return pkt->size;
00229 }
00230 
00231 AVInputFormat ff_siff_demuxer = {
00232     .name           = "siff",
00233     .long_name      = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
00234     .priv_data_size = sizeof(SIFFContext),
00235     .read_probe     = siff_probe,
00236     .read_header    = siff_read_header,
00237     .read_packet    = siff_read_packet,
00238     .extensions = "vb,son"
00239 };
Generated on Sun Apr 22 2012 21:54:08 for Libav by doxygen 1.7.1