Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 #include "avformat.h"
25 #include "internal.h"
26 
27 enum SIFFTags{
28  TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
29  TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
30  TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
31  TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
32  TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
33  TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
34 };
35 
36 enum VBFlags{
37  VB_HAS_GMC = 0x01,
38  VB_HAS_AUDIO = 0x04,
39  VB_HAS_VIDEO = 0x08,
42 };
43 
44 typedef struct SIFFContext{
45  int frames;
46  int cur_frame;
47  int rate;
48  int bits;
50 
51  int has_video;
52  int has_audio;
53 
54  int curstrm;
55  int pktsize;
56  int gmcsize;
57  int sndsize;
58 
59  int flags;
62 
63 static int siff_probe(AVProbeData *p)
64 {
65  uint32_t tag = AV_RL32(p->buf + 8);
66  /* check file header */
67  if (AV_RL32(p->buf) != TAG_SIFF ||
68  (tag != TAG_VBV1 && tag != TAG_SOUN))
69  return 0;
70  return AVPROBE_SCORE_MAX;
71 }
72 
74 {
75  AVStream *ast;
76  ast = avformat_new_stream(s, NULL);
77  if (!ast)
78  return -1;
81  ast->codec->channels = 1;
83  ast->codec->bits_per_coded_sample = 8;
84  ast->codec->sample_rate = c->rate;
85  avpriv_set_pts_info(ast, 16, 1, c->rate);
86  ast->start_time = 0;
87  return 0;
88 }
89 
91 {
92  AVStream *st;
93  int width, height;
94 
95  if (avio_rl32(pb) != TAG_VBHD){
96  av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
97  return -1;
98  }
99  if(avio_rb32(pb) != 32){
100  av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
101  return -1;
102  }
103  if(avio_rl16(pb) != 1){
104  av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
105  return -1;
106  }
107  width = avio_rl16(pb);
108  height = avio_rl16(pb);
109  avio_skip(pb, 4);
110  c->frames = avio_rl16(pb);
111  if(!c->frames){
112  av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
113  return -1;
114  }
115  c->bits = avio_rl16(pb);
116  c->rate = avio_rl16(pb);
117  c->block_align = c->rate * (c->bits >> 3);
118 
119  avio_skip(pb, 16); //zeroes
120 
121  st = avformat_new_stream(s, NULL);
122  if (!st)
123  return -1;
126  st->codec->codec_tag = MKTAG('V', 'B', 'V', '1');
127  st->codec->width = width;
128  st->codec->height = height;
130  avpriv_set_pts_info(st, 16, 1, 12);
131 
132  c->cur_frame = 0;
133  c->has_video = 1;
134  c->has_audio = !!c->rate;
135  c->curstrm = -1;
136  if (c->has_audio && create_audio_stream(s, c) < 0)
137  return -1;
138  return 0;
139 }
140 
142 {
143  if (avio_rl32(pb) != TAG_SHDR){
144  av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
145  return -1;
146  }
147  if(avio_rb32(pb) != 8){
148  av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
149  return -1;
150  }
151  avio_skip(pb, 4); //unknown value
152  c->rate = avio_rl16(pb);
153  c->bits = avio_rl16(pb);
154  c->block_align = c->rate * (c->bits >> 3);
155  return create_audio_stream(s, c);
156 }
157 
159 {
160  AVIOContext *pb = s->pb;
161  SIFFContext *c = s->priv_data;
162  uint32_t tag;
163 
164  if (avio_rl32(pb) != TAG_SIFF)
165  return -1;
166  avio_skip(pb, 4); //ignore size
167  tag = avio_rl32(pb);
168 
169  if (tag != TAG_VBV1 && tag != TAG_SOUN){
170  av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
171  return -1;
172  }
173 
174  if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
175  return -1;
176  if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
177  return -1;
178  if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
179  av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
180  return -1;
181  }
182  avio_skip(pb, 4); //ignore size
183 
184  return 0;
185 }
186 
188 {
189  SIFFContext *c = s->priv_data;
190  int size;
191 
192  if (c->has_video){
193  if (c->cur_frame >= c->frames)
194  return AVERROR(EIO);
195  if (c->curstrm == -1){
196  c->pktsize = avio_rl32(s->pb) - 4;
197  c->flags = avio_rl16(s->pb);
198  c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
199  if (c->gmcsize)
200  avio_read(s->pb, c->gmc, c->gmcsize);
201  c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
202  c->curstrm = !!(c->flags & VB_HAS_AUDIO);
203  }
204 
205  if (!c->curstrm){
206  size = c->pktsize - c->sndsize;
207  if (av_new_packet(pkt, size) < 0)
208  return AVERROR(ENOMEM);
209  AV_WL16(pkt->data, c->flags);
210  if (c->gmcsize)
211  memcpy(pkt->data + 2, c->gmc, c->gmcsize);
212  avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
213  pkt->stream_index = 0;
214  c->curstrm = -1;
215  }else{
216  if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
217  return AVERROR(EIO);
218  pkt->stream_index = 1;
219  pkt->duration = size;
220  c->curstrm = 0;
221  }
222  if(!c->cur_frame || c->curstrm)
223  pkt->flags |= AV_PKT_FLAG_KEY;
224  if (c->curstrm == -1)
225  c->cur_frame++;
226  }else{
227  size = av_get_packet(s->pb, pkt, c->block_align);
228  if(size <= 0)
229  return AVERROR(EIO);
230  pkt->duration = size;
231  }
232  return pkt->size;
233 }
234 
236  .name = "siff",
237  .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
238  .priv_data_size = sizeof(SIFFContext),
242  .extensions = "vb,son",
243 };
Bytestream IO Context.
Definition: avio.h:68
int size
SIFFTags
Definition: siff.c:27
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:2829
int frames
Definition: siff.c:45
int rate
Definition: siff.c:47
int size
Definition: avcodec.h:974
Definition: siff.c:29
static int siff_probe(AVProbeData *p)
Definition: siff.c:63
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
Format I/O context.
Definition: avformat.h:922
int curstrm
Definition: siff.c:54
uint8_t
static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
Definition: siff.c:73
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
AVInputFormat ff_siff_demuxer
Definition: siff.c:235
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
int has_audio
Definition: siff.c:52
uint8_t * data
Definition: avcodec.h:973
uint32_t tag
Definition: movenc.c:844
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:117
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2523
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
Definition: siff.c:28
int has_video
Definition: siff.c:51
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
uint8_t gmc[4]
Definition: siff.c:60
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
Definition: siff.c:141
int cur_frame
Definition: siff.c:46
audio channel layout utility functions
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1229
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define AV_RL32
Definition: intreadwrite.h:146
int sndsize
Definition: siff.c:57
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
int pktsize
Definition: siff.c:55
static int width
Definition: utils.c:156
enum AVMediaType codec_type
Definition: avcodec.h:1058
int flags
Definition: siff.c:59
static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: siff.c:187
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
AVIOContext * pb
I/O context.
Definition: avformat.h:964
int block_align
Definition: siff.c:49
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Definition: siff.c:33
VBFlags
Definition: vb.c:34
Definition: siff.c:31
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
Definition: siff.c:30
int height
Definition: gxfenc.c:72
static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
Definition: siff.c:90
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:548
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:749
int channels
number of audio channels
Definition: avcodec.h:1808
void * priv_data
Format private data.
Definition: avformat.h:950
int bits
Definition: siff.c:48
int gmcsize
Definition: siff.c:56
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
#define AV_WL16(p, d)
Definition: intreadwrite.h:225
int stream_index
Definition: avcodec.h:975
static int siff_read_header(AVFormatContext *s)
Definition: siff.c:158
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:950
Definition: siff.c:32