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