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 
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 
26 enum SIFFTags{
27  TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
28  TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
29  TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
30  TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
31  TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
32  TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
33 };
34 
35 enum VBFlags{
36  VB_HAS_GMC = 0x01,
37  VB_HAS_AUDIO = 0x04,
38  VB_HAS_VIDEO = 0x08,
41 };
42 
43 typedef struct SIFFContext{
44  int frames;
45  int cur_frame;
46  int rate;
47  int bits;
49 
50  int has_video;
51  int has_audio;
52 
53  int curstrm;
54  int pktsize;
55  int gmcsize;
56  int sndsize;
57 
58  int flags;
59  uint8_t gmc[4];
61 
62 static int siff_probe(AVProbeData *p)
63 {
64  uint32_t tag = AV_RL32(p->buf + 8);
65  /* check file header */
66  if (AV_RL32(p->buf) != TAG_SIFF ||
67  (tag != TAG_VBV1 && tag != TAG_SOUN))
68  return 0;
69  return AVPROBE_SCORE_MAX;
70 }
71 
73 {
74  AVStream *ast;
75  ast = avformat_new_stream(s, NULL);
76  if (!ast)
77  return -1;
80  ast->codec->channels = 1;
81  ast->codec->bits_per_coded_sample = c->bits;
82  ast->codec->sample_rate = c->rate;
83  ast->codec->frame_size = c->block_align;
84  avpriv_set_pts_info(ast, 16, 1, c->rate);
85  return 0;
86 }
87 
89 {
90  AVStream *st;
91  int width, height;
92 
93  if (avio_rl32(pb) != TAG_VBHD){
94  av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
95  return -1;
96  }
97  if(avio_rb32(pb) != 32){
98  av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
99  return -1;
100  }
101  if(avio_rl16(pb) != 1){
102  av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
103  return -1;
104  }
105  width = avio_rl16(pb);
106  height = avio_rl16(pb);
107  avio_skip(pb, 4);
108  c->frames = avio_rl16(pb);
109  if(!c->frames){
110  av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
111  return -1;
112  }
113  c->bits = avio_rl16(pb);
114  c->rate = avio_rl16(pb);
115  c->block_align = c->rate * (c->bits >> 3);
116 
117  avio_skip(pb, 16); //zeroes
118 
119  st = avformat_new_stream(s, NULL);
120  if (!st)
121  return -1;
123  st->codec->codec_id = CODEC_ID_VB;
124  st->codec->codec_tag = MKTAG('V', 'B', 'V', '1');
125  st->codec->width = width;
126  st->codec->height = height;
127  st->codec->pix_fmt = PIX_FMT_PAL8;
128  avpriv_set_pts_info(st, 16, 1, 12);
129 
130  c->cur_frame = 0;
131  c->has_video = 1;
132  c->has_audio = !!c->rate;
133  c->curstrm = -1;
134  if (c->has_audio && create_audio_stream(s, c) < 0)
135  return -1;
136  return 0;
137 }
138 
140 {
141  if (avio_rl32(pb) != TAG_SHDR){
142  av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
143  return -1;
144  }
145  if(avio_rb32(pb) != 8){
146  av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
147  return -1;
148  }
149  avio_skip(pb, 4); //unknown value
150  c->rate = avio_rl16(pb);
151  c->bits = avio_rl16(pb);
152  c->block_align = c->rate * (c->bits >> 3);
153  return create_audio_stream(s, c);
154 }
155 
157 {
158  AVIOContext *pb = s->pb;
159  SIFFContext *c = s->priv_data;
160  uint32_t tag;
161 
162  if (avio_rl32(pb) != TAG_SIFF)
163  return -1;
164  avio_skip(pb, 4); //ignore size
165  tag = avio_rl32(pb);
166 
167  if (tag != TAG_VBV1 && tag != TAG_SOUN){
168  av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
169  return -1;
170  }
171 
172  if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
173  return -1;
174  if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
175  return -1;
176  if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
177  av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
178  return -1;
179  }
180  avio_skip(pb, 4); //ignore size
181 
182  return 0;
183 }
184 
186 {
187  SIFFContext *c = s->priv_data;
188  int size;
189 
190  if (c->has_video){
191  if (c->cur_frame >= c->frames)
192  return AVERROR(EIO);
193  if (c->curstrm == -1){
194  c->pktsize = avio_rl32(s->pb) - 4;
195  c->flags = avio_rl16(s->pb);
196  c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
197  if (c->gmcsize)
198  avio_read(s->pb, c->gmc, c->gmcsize);
199  c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
200  c->curstrm = !!(c->flags & VB_HAS_AUDIO);
201  }
202 
203  if (!c->curstrm){
204  size = c->pktsize - c->sndsize;
205  if (av_new_packet(pkt, size) < 0)
206  return AVERROR(ENOMEM);
207  AV_WL16(pkt->data, c->flags);
208  if (c->gmcsize)
209  memcpy(pkt->data + 2, c->gmc, c->gmcsize);
210  avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
211  pkt->stream_index = 0;
212  c->curstrm = -1;
213  }else{
214  if (av_get_packet(s->pb, pkt, c->sndsize - 4) < 0)
215  return AVERROR(EIO);
216  pkt->stream_index = 1;
217  c->curstrm = 0;
218  }
219  if(!c->cur_frame || c->curstrm)
220  pkt->flags |= AV_PKT_FLAG_KEY;
221  if (c->curstrm == -1)
222  c->cur_frame++;
223  }else{
224  size = av_get_packet(s->pb, pkt, c->block_align);
225  if(size <= 0)
226  return AVERROR(EIO);
227  }
228  return pkt->size;
229 }
230 
232  .name = "siff",
233  .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
234  .priv_data_size = sizeof(SIFFContext),
238  .extensions = "vb,son"
239 };
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
Bytestream IO Context.
Definition: avio.h:68
int size
SIFFTags
Definition: siff.c:26
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
int frames
Definition: siff.c:44
int rate
Definition: siff.c:46
int size
Definition: avcodec.h:909
Definition: siff.c:28
int width
Definition: rotozoom.c:164
static int siff_probe(AVProbeData *p)
Definition: siff.c:62
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
Format I/O context.
Definition: avformat.h:863
int curstrm
Definition: siff.c:53
static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
Definition: siff.c:72
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:769
AVInputFormat ff_siff_demuxer
Definition: siff.c:231
int has_audio
Definition: siff.c:51
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
uint8_t * data
Definition: avcodec.h:908
uint32_t tag
Definition: movenc.c:670
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:269
struct SIFFContext SIFFContext
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#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:191
AV_RL32
Definition: bytestream.h:89
Definition: siff.c:27
int has_video
Definition: siff.c:50
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
uint8_t gmc[4]
Definition: siff.c:59
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
AVCodecContext * codec
codec context
Definition: avformat.h:623
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
Definition: siff.c:139
int cur_frame
Definition: siff.c:45
int width
picture width / height.
Definition: avcodec.h:1408
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
static int read_probe(AVProbeData *p)
Definition: img2.c:185
int sndsize
Definition: siff.c:56
Stream structure.
Definition: avformat.h:620
int frame_size
Samples per packet, initialized when calling 'init'.
Definition: avcodec.h:1470
NULL
Definition: eval.c:50
int pktsize
Definition: siff.c:54
enum AVMediaType codec_type
Definition: avcodec.h:1574
int flags
Definition: siff.c:58
static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: siff.c:185
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
int block_align
Definition: siff.c:48
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
Definition: siff.c:32
VBFlags
Definition: vb.c:33
Definition: siff.c:30
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
Definition: siff.c:29
int height
Definition: gxfenc.c:73
static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
Definition: siff.c:88
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
Main libavformat public API header.
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:75
static int siff_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: siff.c:156
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
int bits
Definition: siff.c:47
int gmcsize
Definition: siff.c:55
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
#define AV_WL16(p, d)
Definition: intreadwrite.h:225
int stream_index
Definition: avcodec.h:910
#define MKTAG(a, b, c, d)
Definition: common.h:231
Definition: siff.c:31
enum CodecID codec_id
Definition: avcodec.h:1575