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