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 };