libavformat/siff.c
66f7679c
 /*
  * Beam Software SIFF demuxer
406792e7
  * Copyright (c) 2007 Konstantin Shishkov
66f7679c
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
6a5d31ac
 #include "libavutil/intreadwrite.h"
66f7679c
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
a1a079dc
 #include "avio_internal.h"
66f7679c
 
 enum SIFFTags{
     TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
     TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
     TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
     TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
     TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
     TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
 };
 
 enum VBFlags{
     VB_HAS_GMC     = 0x01,
     VB_HAS_AUDIO   = 0x04,
     VB_HAS_VIDEO   = 0x08,
     VB_HAS_PALETTE = 0x10,
     VB_HAS_LENGTH  = 0x20
 };
 
 typedef struct SIFFContext{
     int frames;
     int cur_frame;
     int rate;
     int bits;
     int block_align;
 
     int has_video;
     int has_audio;
 
     int curstrm;
     int pktsize;
     int gmcsize;
     int sndsize;
 
     int flags;
     uint8_t gmc[4];
 }SIFFContext;
 
 static int siff_probe(AVProbeData *p)
 {
5abfae79
     uint32_t tag = AV_RL32(p->buf + 8);
66f7679c
     /* check file header */
5abfae79
     if (AV_RL32(p->buf) != TAG_SIFF ||
         (tag != TAG_VBV1 && tag != TAG_SOUN))
66f7679c
         return 0;
5abfae79
     return AVPROBE_SCORE_MAX;
66f7679c
 }
 
 static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
 {
     AVStream *ast;
3b3bbdd3
     ast = avformat_new_stream(s, NULL);
66f7679c
     if (!ast)
         return -1;
72415b2a
     ast->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
66f7679c
     ast->codec->codec_id        = CODEC_ID_PCM_U8;
     ast->codec->channels        = 1;
dd1c8f3e
     ast->codec->bits_per_coded_sample = c->bits;
66f7679c
     ast->codec->sample_rate     = c->rate;
     ast->codec->frame_size      = c->block_align;
c3f9ebf7
     avpriv_set_pts_info(ast, 16, 1, c->rate);
66f7679c
     return 0;
 }
 
ae628ec1
 static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
66f7679c
 {
     AVStream *st;
     int width, height;
 
b7effd4e
     if (avio_rl32(pb) != TAG_VBHD){
66f7679c
         av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
         return -1;
     }
b7effd4e
     if(avio_rb32(pb) != 32){
66f7679c
         av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
         return -1;
     }
b7effd4e
     if(avio_rl16(pb) != 1){
66f7679c
         av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
         return -1;
     }
b7effd4e
     width = avio_rl16(pb);
     height = avio_rl16(pb);
45a8a02a
     avio_skip(pb, 4);
b7effd4e
     c->frames = avio_rl16(pb);
66f7679c
     if(!c->frames){
         av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
         return -1;
     }
b7effd4e
     c->bits = avio_rl16(pb);
     c->rate = avio_rl16(pb);
66f7679c
     c->block_align = c->rate * (c->bits >> 3);
 
45a8a02a
     avio_skip(pb, 16); //zeroes
66f7679c
 
3b3bbdd3
     st = avformat_new_stream(s, NULL);
66f7679c
     if (!st)
         return -1;
72415b2a
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
66f7679c
     st->codec->codec_id   = CODEC_ID_VB;
     st->codec->codec_tag  = MKTAG('V', 'B', 'V', '1');
     st->codec->width      = width;
     st->codec->height     = height;
     st->codec->pix_fmt    = PIX_FMT_PAL8;
c3f9ebf7
     avpriv_set_pts_info(st, 16, 1, 12);
66f7679c
 
     c->cur_frame = 0;
     c->has_video = 1;
     c->has_audio = !!c->rate;
     c->curstrm = -1;
     if (c->has_audio && create_audio_stream(s, c) < 0)
         return -1;
     return 0;
 }
 
ae628ec1
 static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
66f7679c
 {
b7effd4e
     if (avio_rl32(pb) != TAG_SHDR){
66f7679c
         av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
         return -1;
     }
b7effd4e
     if(avio_rb32(pb) != 8){
66f7679c
         av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
         return -1;
     }
45a8a02a
     avio_skip(pb, 4); //unknown value
b7effd4e
     c->rate = avio_rl16(pb);
     c->bits = avio_rl16(pb);
66f7679c
     c->block_align = c->rate * (c->bits >> 3);
     return create_audio_stream(s, c);
 }
 
 static int siff_read_header(AVFormatContext *s, AVFormatParameters *ap)
 {
ae628ec1
     AVIOContext *pb = s->pb;
66f7679c
     SIFFContext *c = s->priv_data;
     uint32_t tag;
 
b7effd4e
     if (avio_rl32(pb) != TAG_SIFF)
66f7679c
         return -1;
45a8a02a
     avio_skip(pb, 4); //ignore size
b7effd4e
     tag = avio_rl32(pb);
66f7679c
 
     if (tag != TAG_VBV1 && tag != TAG_SOUN){
         av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
         return -1;
     }
 
     if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
         return -1;
     if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
         return -1;
b7effd4e
     if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
66f7679c
         av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
         return -1;
     }
45a8a02a
     avio_skip(pb, 4); //ignore size
66f7679c
 
     return 0;
 }
 
 static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     SIFFContext *c = s->priv_data;
1d5fab9b
     int size;
66f7679c
 
     if (c->has_video){
         if (c->cur_frame >= c->frames)
             return AVERROR(EIO);
         if (c->curstrm == -1){
b7effd4e
             c->pktsize = avio_rl32(s->pb) - 4;
             c->flags = avio_rl16(s->pb);
66f7679c
             c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
             if (c->gmcsize)
b7effd4e
                 avio_read(s->pb, c->gmc, c->gmcsize);
             c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
66f7679c
             c->curstrm = !!(c->flags & VB_HAS_AUDIO);
         }
 
         if (!c->curstrm){
a1a079dc
             size = c->pktsize - c->sndsize - c->gmcsize - 2;
             size = ffio_limit(s->pb, size);
             if(size < 0 || c->pktsize < c->sndsize)
21ca9604
                 return AVERROR_INVALIDDATA;
a1a079dc
             if (av_new_packet(pkt, size + c->gmcsize + 2) < 0)
66f7679c
                 return AVERROR(ENOMEM);
             AV_WL16(pkt->data, c->flags);
             if (c->gmcsize)
                 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
a1a079dc
             avio_read(s->pb, pkt->data + 2 + c->gmcsize, size);
66f7679c
             pkt->stream_index = 0;
             c->curstrm = -1;
         }else{
899681cd
             if (av_get_packet(s->pb, pkt, c->sndsize - 4) < 0)
66f7679c
                 return AVERROR(EIO);
             pkt->stream_index = 1;
             c->curstrm = 0;
         }
         if(!c->cur_frame || c->curstrm)
cc947f04
             pkt->flags |= AV_PKT_FLAG_KEY;
66f7679c
         if (c->curstrm == -1)
             c->cur_frame++;
     }else{
899681cd
         size = av_get_packet(s->pb, pkt, c->block_align);
66f7679c
         if(size <= 0)
             return AVERROR(EIO);
     }
     return pkt->size;
 }
 
c6610a21
 AVInputFormat ff_siff_demuxer = {
dfc2c4d9
     .name           = "siff",
     .long_name      = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
     .priv_data_size = sizeof(SIFFContext),
     .read_probe     = siff_probe,
     .read_header    = siff_read_header,
     .read_packet    = siff_read_packet,
66f7679c
     .extensions = "vb,son"
 };