FFmpeg
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 
23 #include "libavutil/intreadwrite.h"
24 
25 #include "avformat.h"
26 #include "internal.h"
27 #include "avio_internal.h"
28 
29 enum SIFFTags {
30  TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
31  TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
32  TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
33  TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
34  TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
35  TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
36 };
37 
38 enum VBFlags {
39  VB_HAS_GMC = 0x01,
40  VB_HAS_AUDIO = 0x04,
41  VB_HAS_VIDEO = 0x08,
44 };
45 
46 typedef struct SIFFContext {
47  int frames;
48  int cur_frame;
49  int rate;
50  int bits;
52 
53  int has_video;
54  int has_audio;
55 
56  int curstrm;
57  unsigned int pktsize;
58  int gmcsize;
59  unsigned int sndsize;
60 
61  unsigned int flags;
62  uint8_t gmc[4];
63 } SIFFContext;
64 
65 static int siff_probe(const AVProbeData *p)
66 {
67  uint32_t tag = AV_RL32(p->buf + 8);
68  /* check file header */
69  if (AV_RL32(p->buf) != TAG_SIFF ||
70  (tag != TAG_VBV1 && tag != TAG_SOUN))
71  return 0;
72  return AVPROBE_SCORE_MAX;
73 }
74 
76 {
77  AVStream *ast;
78  ast = avformat_new_stream(s, NULL);
79  if (!ast)
80  return AVERROR(ENOMEM);
83  ast->codecpar->channels = 1;
86  ast->codecpar->sample_rate = c->rate;
87  avpriv_set_pts_info(ast, 16, 1, c->rate);
88  ast->start_time = 0;
89  return 0;
90 }
91 
93 {
94  AVStream *st;
95  int width, height;
96 
97  if (avio_rl32(pb) != TAG_VBHD) {
98  av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
99  return AVERROR_INVALIDDATA;
100  }
101  if (avio_rb32(pb) != 32) {
102  av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
103  return AVERROR_INVALIDDATA;
104  }
105  if (avio_rl16(pb) != 1) {
106  av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
107  return AVERROR_INVALIDDATA;
108  }
109  width = avio_rl16(pb);
110  height = avio_rl16(pb);
111  avio_skip(pb, 4);
112  c->frames = avio_rl16(pb);
113  if (!c->frames) {
114  av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
115  return AVERROR_INVALIDDATA;
116  }
117  c->bits = avio_rl16(pb);
118  c->rate = avio_rl16(pb);
119  c->block_align = c->rate * (c->bits >> 3);
120 
121  avio_skip(pb, 16); // zeroes
122 
123  st = avformat_new_stream(s, NULL);
124  if (!st)
125  return AVERROR(ENOMEM);
128  st->codecpar->codec_tag = MKTAG('V', 'B', 'V', '1');
129  st->codecpar->width = width;
130  st->codecpar->height = height;
132  st->nb_frames =
133  st->duration = c->frames;
134  avpriv_set_pts_info(st, 16, 1, 12);
135 
136  c->cur_frame = 0;
137  c->has_video = 1;
138  c->has_audio = !!c->rate;
139  c->curstrm = -1;
140  if (c->has_audio)
141  return create_audio_stream(s, c);
142  return 0;
143 }
144 
146 {
147  if (avio_rl32(pb) != TAG_SHDR) {
148  av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
149  return AVERROR_INVALIDDATA;
150  }
151  if (avio_rb32(pb) != 8) {
152  av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
153  return AVERROR_INVALIDDATA;
154  }
155  avio_skip(pb, 4); // unknown value
156  c->rate = avio_rl16(pb);
157  c->bits = avio_rl16(pb);
158  c->block_align = c->rate * (c->bits >> 3);
159  return create_audio_stream(s, c);
160 }
161 
163 {
164  AVIOContext *pb = s->pb;
165  SIFFContext *c = s->priv_data;
166  uint32_t tag;
167  int ret;
168 
169  if (avio_rl32(pb) != TAG_SIFF)
170  return AVERROR_INVALIDDATA;
171  avio_skip(pb, 4); // ignore size
172  tag = avio_rl32(pb);
173 
174  if (tag != TAG_VBV1 && tag != TAG_SOUN) {
175  av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
176  return AVERROR_INVALIDDATA;
177  }
178 
179  if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0)
180  return ret;
181  if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0)
182  return ret;
183  if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')) {
184  av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
185  return AVERROR_INVALIDDATA;
186  }
187  avio_skip(pb, 4); // ignore size
188 
189  return 0;
190 }
191 
193 {
194  SIFFContext *c = s->priv_data;
195  int ret;
196 
197  if (c->has_video) {
198  unsigned int size;
199  if (c->cur_frame >= c->frames)
200  return AVERROR_EOF;
201  if (c->curstrm == -1) {
202  c->pktsize = avio_rl32(s->pb) - 4;
203  c->flags = avio_rl16(s->pb);
204  if (c->flags & VB_HAS_AUDIO && !c->has_audio)
205  return AVERROR_INVALIDDATA;
206  c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
207  if (c->gmcsize)
208  avio_read(s->pb, c->gmc, c->gmcsize);
209  c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb) : 0;
210  c->curstrm = !!(c->flags & VB_HAS_AUDIO);
211  }
212 
213  if (!c->curstrm) {
214  if (c->pktsize < 2LL + c->sndsize + c->gmcsize)
215  return AVERROR_INVALIDDATA;
216 
217  size = c->pktsize - c->sndsize - c->gmcsize - 2;
218  size = ffio_limit(s->pb, size);
219  if ((ret = av_new_packet(pkt, size + c->gmcsize + 2)) < 0)
220  return ret;
221  AV_WL16(pkt->data, c->flags);
222  if (c->gmcsize)
223  memcpy(pkt->data + 2, c->gmc, c->gmcsize);
224  if (avio_read(s->pb, pkt->data + 2 + c->gmcsize, size) != size) {
225  return AVERROR_INVALIDDATA;
226  }
227  pkt->stream_index = 0;
228  c->curstrm = -1;
229  } else {
230  int pktsize = av_get_packet(s->pb, pkt, c->sndsize - 4);
231  if (pktsize < 0)
232  return AVERROR(EIO);
233  pkt->stream_index = 1;
234  pkt->duration = pktsize;
235  c->curstrm = 0;
236  }
237  if (!c->cur_frame || c->curstrm)
239  if (c->curstrm == -1)
240  c->cur_frame++;
241  } else {
242  int pktsize = av_get_packet(s->pb, pkt, c->block_align);
243  if (!pktsize)
244  return AVERROR_EOF;
245  if (pktsize <= 0)
246  return AVERROR(EIO);
247  pkt->duration = pktsize;
248  }
249  return pkt->size;
250 }
251 
253  .name = "siff",
254  .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
255  .priv_data_size = sizeof(SIFFContext),
259  .extensions = "vb,son",
260 };
SIFFContext::pktsize
unsigned int pktsize
Definition: siff.c:57
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
SIFFContext::flags
unsigned int flags
Definition: siff.c:61
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
siff_read_header
static int siff_read_header(AVFormatContext *s)
Definition: siff.c:162
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
TAG_SIFF
@ TAG_SIFF
Definition: siff.c:30
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
AV_CODEC_ID_VB
@ AV_CODEC_ID_VB
Definition: codec_id.h:158
siff_parse_soun
static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
Definition: siff.c:145
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:428
SIFFContext::frames
int frames
Definition: siff.c:47
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
VB_HAS_GMC
@ VB_HAS_GMC
Definition: siff.c:39
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
VBFlags
VBFlags
Definition: vb.c:34
TAG_VBV1
@ TAG_VBV1
Definition: siff.c:34
SIFFContext::gmc
uint8_t gmc[4]
Definition: siff.c:62
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:985
SIFFContext
Definition: siff.c:46
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:650
VB_HAS_AUDIO
@ VB_HAS_AUDIO
Definition: siff.c:40
width
#define width
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
SIFFContext::sndsize
unsigned int sndsize
Definition: siff.c:59
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
SIFFContext::rate
int rate
Definition: siff.c:49
SIFFContext::curstrm
int curstrm
Definition: siff.c:56
SIFFContext::cur_frame
int cur_frame
Definition: siff.c:48
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
create_audio_stream
static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
Definition: siff.c:75
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:987
VB_HAS_VIDEO
@ VB_HAS_VIDEO
Definition: siff.c:41
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
TAG_SOUN
@ TAG_SOUN
Definition: siff.c:35
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
SIFFContext::bits
int bits
Definition: siff.c:50
size
int size
Definition: twinvq_data.h:10344
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
TAG_VBHD
@ TAG_VBHD
Definition: siff.c:32
height
#define height
VB_HAS_PALETTE
@ VB_HAS_PALETTE
Definition: siff.c:42
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
SIFFContext::has_audio
int has_audio
Definition: siff.c:54
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: aviobuf.c:1090
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:127
SIFFContext::gmcsize
int gmcsize
Definition: siff.c:58
av_get_packet
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:197
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
tag
uint32_t tag
Definition: movenc.c:1596
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:935
avformat.h
ff_siff_demuxer
const AVInputFormat ff_siff_demuxer
Definition: siff.c:252
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
siff_probe
static int siff_probe(const AVProbeData *p)
Definition: siff.c:65
SIFFContext::has_video
int has_video
Definition: siff.c:53
channel_layout.h
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:641
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, 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:1196
AVPacket::stream_index
int stream_index
Definition: packet.h:375
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:347
TAG_BODY
@ TAG_BODY
Definition: siff.c:31
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:319
TAG_SHDR
@ TAG_SHDR
Definition: siff.c:33
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
SIFFContext::block_align
int block_align
Definition: siff.c:51
SIFFTags
SIFFTags
Definition: siff.c:29
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
siff_read_packet
static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: siff.c:192
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:975
siff_parse_vbv1
static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
Definition: siff.c:92
VB_HAS_LENGTH
@ VB_HAS_LENGTH
Definition: siff.c:43