FFmpeg  2.1.1
siff.c
Go to the documentation of this file.
1 /*
2  * Beam Software SIFF demuxer
3  * Copyright (c) 2007 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "internal.h"
26 #include "avio_internal.h"
27 
28 enum SIFFTags{
29  TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
30  TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
31  TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
32  TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
33  TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
34  TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
35 };
36 
37 enum VBFlags{
38  VB_HAS_GMC = 0x01,
39  VB_HAS_AUDIO = 0x04,
40  VB_HAS_VIDEO = 0x08,
43 };
44 
45 typedef struct SIFFContext{
46  int frames;
47  int cur_frame;
48  int rate;
49  int bits;
51 
52  int has_video;
53  int has_audio;
54 
55  int curstrm;
56  int pktsize;
57  int gmcsize;
58  int sndsize;
59 
60  int flags;
63 
64 static int siff_probe(AVProbeData *p)
65 {
66  uint32_t tag = AV_RL32(p->buf + 8);
67  /* check file header */
68  if (AV_RL32(p->buf) != TAG_SIFF ||
69  (tag != TAG_VBV1 && tag != TAG_SOUN))
70  return 0;
71  return AVPROBE_SCORE_MAX;
72 }
73 
75 {
76  AVStream *ast;
77  ast = avformat_new_stream(s, NULL);
78  if (!ast)
79  return AVERROR(ENOMEM);
82  ast->codec->channels = 1;
84  ast->codec->bits_per_coded_sample = 8;
85  ast->codec->sample_rate = c->rate;
86  avpriv_set_pts_info(ast, 16, 1, c->rate);
87  ast->start_time = 0;
88  return 0;
89 }
90 
92 {
93  AVStream *st;
94  int width, height;
95 
96  if (avio_rl32(pb) != TAG_VBHD){
97  av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
98  return AVERROR_INVALIDDATA;
99  }
100  if(avio_rb32(pb) != 32){
101  av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
102  return AVERROR_INVALIDDATA;
103  }
104  if(avio_rl16(pb) != 1){
105  av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
106  return AVERROR_INVALIDDATA;
107  }
108  width = avio_rl16(pb);
109  height = avio_rl16(pb);
110  avio_skip(pb, 4);
111  c->frames = avio_rl16(pb);
112  if(!c->frames){
113  av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
114  return AVERROR_INVALIDDATA;
115  }
116  c->bits = avio_rl16(pb);
117  c->rate = avio_rl16(pb);
118  c->block_align = c->rate * (c->bits >> 3);
119 
120  avio_skip(pb, 16); //zeroes
121 
122  st = avformat_new_stream(s, NULL);
123  if (!st)
124  return AVERROR(ENOMEM);
127  st->codec->codec_tag = MKTAG('V', 'B', 'V', '1');
128  st->codec->width = width;
129  st->codec->height = height;
131  st->nb_frames =
132  st->duration = c->frames;
133  avpriv_set_pts_info(st, 16, 1, 12);
134 
135  c->cur_frame = 0;
136  c->has_video = 1;
137  c->has_audio = !!c->rate;
138  c->curstrm = -1;
139  if (c->has_audio && create_audio_stream(s, c) < 0)
140  return AVERROR(ENOMEM);
141  return 0;
142 }
143 
145 {
146  if (avio_rl32(pb) != TAG_SHDR){
147  av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
148  return AVERROR_INVALIDDATA;
149  }
150  if(avio_rb32(pb) != 8){
151  av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
152  return AVERROR_INVALIDDATA;
153  }
154  avio_skip(pb, 4); //unknown value
155  c->rate = avio_rl16(pb);
156  c->bits = avio_rl16(pb);
157  c->block_align = c->rate * (c->bits >> 3);
158  return create_audio_stream(s, c);
159 }
160 
162 {
163  AVIOContext *pb = s->pb;
164  SIFFContext *c = s->priv_data;
165  uint32_t tag;
166  int ret;
167 
168  if (avio_rl32(pb) != TAG_SIFF)
169  return AVERROR_INVALIDDATA;
170  avio_skip(pb, 4); //ignore size
171  tag = avio_rl32(pb);
172 
173  if (tag != TAG_VBV1 && tag != TAG_SOUN){
174  av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
175  return AVERROR_INVALIDDATA;
176  }
177 
178  if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0)
179  return ret;
180  if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0)
181  return ret;
182  if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
183  av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
184  return AVERROR_INVALIDDATA;
185  }
186  avio_skip(pb, 4); //ignore size
187 
188  return 0;
189 }
190 
192 {
193  SIFFContext *c = s->priv_data;
194  int size;
195 
196  if (c->has_video){
197  if (c->cur_frame >= c->frames)
198  return AVERROR_EOF;
199  if (c->curstrm == -1){
200  c->pktsize = avio_rl32(s->pb) - 4;
201  c->flags = avio_rl16(s->pb);
202  c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
203  if (c->gmcsize)
204  avio_read(s->pb, c->gmc, c->gmcsize);
205  c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
206  c->curstrm = !!(c->flags & VB_HAS_AUDIO);
207  }
208 
209  if (!c->curstrm){
210  size = c->pktsize - c->sndsize - c->gmcsize - 2;
211  size = ffio_limit(s->pb, size);
212  if(size < 0 || c->pktsize < c->sndsize)
213  return AVERROR_INVALIDDATA;
214  if (av_new_packet(pkt, size + c->gmcsize + 2) < 0)
215  return AVERROR(ENOMEM);
216  AV_WL16(pkt->data, c->flags);
217  if (c->gmcsize)
218  memcpy(pkt->data + 2, c->gmc, c->gmcsize);
219  avio_read(s->pb, pkt->data + 2 + c->gmcsize, size);
220  pkt->stream_index = 0;
221  c->curstrm = -1;
222  }else{
223  if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
224  return AVERROR(EIO);
225  pkt->stream_index = 1;
226  pkt->duration = size;
227  c->curstrm = 0;
228  }
229  if(!c->cur_frame || c->curstrm)
230  pkt->flags |= AV_PKT_FLAG_KEY;
231  if (c->curstrm == -1)
232  c->cur_frame++;
233  }else{
234  size = av_get_packet(s->pb, pkt, c->block_align);
235  if(!size)
236  return AVERROR_EOF;
237  if(size < 0)
238  return AVERROR(EIO);
239  pkt->duration = size;
240  }
241  return pkt->size;
242 }
243 
245  .name = "siff",
246  .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
247  .priv_data_size = sizeof(SIFFContext),
251  .extensions = "vb,son",
252 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
int size
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
SIFFTags
Definition: siff.c:28
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3922
int frames
Definition: siff.c:46
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int rate
Definition: siff.c:48
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:593
int size
Definition: avcodec.h:1064
Definition: siff.c:30
static int siff_probe(AVProbeData *p)
Definition: siff.c:64
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:624
Format I/O context.
Definition: avformat.h:968
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:133
int curstrm
Definition: siff.c:55
uint8_t
static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
Definition: siff.c:74
AVInputFormat ff_siff_demuxer
Definition: siff.c:244
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
int has_audio
Definition: siff.c:53
uint32_t tag
Definition: movenc.c:961
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2563
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
int duration
Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.
Definition: avcodec.h:1085
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:200
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void * priv_data
Format private data.
Definition: avformat.h:988
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
Definition: siff.c:29
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:577
int has_video
Definition: siff.c:52
uint8_t gmc[4]
Definition: siff.c:61
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1934
static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
Definition: siff.c:144
int cur_frame
Definition: siff.c:47
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
int sndsize
Definition: siff.c:58
Definition: vb.c:35
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
#define AV_WL16(p, darg)
Definition: intreadwrite.h:250
int pktsize
Definition: siff.c:56
static int width
Definition: utils.c:158
enum AVMediaType codec_type
Definition: avcodec.h:1154
int flags
Definition: siff.c:60
static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: siff.c:191
enum AVCodecID codec_id
Definition: avcodec.h:1157
int sample_rate
samples per second
Definition: avcodec.h:1873
int block_align
Definition: siff.c:50
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
#define MKTAG(a, b, c, d)
uint8_t * data
Definition: avcodec.h:1063
Definition: siff.c:34
VBFlags
Definition: vb.c:34
Definition: siff.c:32
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
#define AVERROR_EOF
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:342
Definition: siff.c:31
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:720
static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
Definition: siff.c:91
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:480
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:713
static double c[64]
Main libavformat public API header.
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:722
#define AVERROR_INVALIDDATA
#define AV_RL32(x)
Definition: intreadwrite.h:275
int channels
number of audio channels
Definition: avcodec.h:1874
int bits
Definition: siff.c:49
int gmcsize
Definition: siff.c:57
#define AVERROR(e)
static AVPacket pkt
Definition: demuxing.c:52
8 bit with PIX_FMT_RGB32 palette
Definition: avcodec.h:4545
int stream_index
Definition: avcodec.h:1065
static int siff_read_header(AVFormatContext *s)
Definition: siff.c:161
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1040
Definition: siff.c:33