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