2 * Beam Software SIFF demuxer
3 * Copyright (c) 2007 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
27 #include "avio_internal.h"
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'),
42 VB_HAS_PALETTE
= 0x10,
46 typedef struct SIFFContext
{
65 static int siff_probe(const AVProbeData
*p
)
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
))
72 return AVPROBE_SCORE_MAX
;
75 static int create_audio_stream(AVFormatContext
*s
, SIFFContext
*c
)
78 ast
= avformat_new_stream(s
, NULL
);
80 return AVERROR(ENOMEM
);
81 ast
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
82 ast
->codecpar
->codec_id
= AV_CODEC_ID_PCM_U8
;
83 ast
->codecpar
->channels
= 1;
84 ast
->codecpar
->channel_layout
= AV_CH_LAYOUT_MONO
;
85 ast
->codecpar
->bits_per_coded_sample
= 8;
86 ast
->codecpar
->sample_rate
= c
->rate
;
87 avpriv_set_pts_info(ast
, 16, 1, c
->rate
);
92 static int siff_parse_vbv1(AVFormatContext
*s
, SIFFContext
*c
, AVIOContext
*pb
)
97 if (avio_rl32(pb
) != TAG_VBHD
) {
98 av_log(s
, AV_LOG_ERROR
, "Header chunk is missing\n");
99 return AVERROR_INVALIDDATA
;
101 if (avio_rb32(pb
) != 32) {
102 av_log(s
, AV_LOG_ERROR
, "Header chunk size is incorrect\n");
103 return AVERROR_INVALIDDATA
;
105 if (avio_rl16(pb
) != 1) {
106 av_log(s
, AV_LOG_ERROR
, "Incorrect header version\n");
107 return AVERROR_INVALIDDATA
;
109 width
= avio_rl16(pb
);
110 height
= avio_rl16(pb
);
112 c
->frames
= avio_rl16(pb
);
114 av_log(s
, AV_LOG_ERROR
, "File contains no frames ???\n");
115 return AVERROR_INVALIDDATA
;
117 c
->bits
= avio_rl16(pb
);
118 c
->rate
= avio_rl16(pb
);
119 c
->block_align
= c
->rate
* (c
->bits
>> 3);
121 avio_skip(pb
, 16); // zeroes
123 st
= avformat_new_stream(s
, NULL
);
125 return AVERROR(ENOMEM
);
126 st
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
127 st
->codecpar
->codec_id
= AV_CODEC_ID_VB
;
128 st
->codecpar
->codec_tag
= MKTAG('V', 'B', 'V', '1');
129 st
->codecpar
->width
= width
;
130 st
->codecpar
->height
= height
;
131 st
->codecpar
->format
= AV_PIX_FMT_PAL8
;
133 st
->duration
= c
->frames
;
134 avpriv_set_pts_info(st
, 16, 1, 12);
138 c
->has_audio
= !!c
->rate
;
141 return create_audio_stream(s
, c
);
145 static int siff_parse_soun(AVFormatContext
*s
, SIFFContext
*c
, AVIOContext
*pb
)
147 if (avio_rl32(pb
) != TAG_SHDR
) {
148 av_log(s
, AV_LOG_ERROR
, "Header chunk is missing\n");
149 return AVERROR_INVALIDDATA
;
151 if (avio_rb32(pb
) != 8) {
152 av_log(s
, AV_LOG_ERROR
, "Header chunk size is incorrect\n");
153 return AVERROR_INVALIDDATA
;
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
);
162 static int siff_read_header(AVFormatContext
*s
)
164 AVIOContext
*pb
= s
->pb
;
165 SIFFContext
*c
= s
->priv_data
;
169 if (avio_rl32(pb
) != TAG_SIFF
)
170 return AVERROR_INVALIDDATA
;
171 avio_skip(pb
, 4); // ignore size
174 if (tag
!= TAG_VBV1
&& tag
!= TAG_SOUN
) {
175 av_log(s
, AV_LOG_ERROR
, "Not a VBV file\n");
176 return AVERROR_INVALIDDATA
;
179 if (tag
== TAG_VBV1
&& (ret
= siff_parse_vbv1(s
, c
, pb
)) < 0)
181 if (tag
== TAG_SOUN
&& (ret
= siff_parse_soun(s
, c
, pb
)) < 0)
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
;
187 avio_skip(pb
, 4); // ignore size
192 static int siff_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
194 SIFFContext
*c
= s
->priv_data
;
199 if (c
->cur_frame
>= c
->frames
)
201 if (c
->curstrm
== -1) {
202 unsigned pktsize
= avio_rl32(s
->pb
);
204 return AVERROR_INVALIDDATA
;
205 c
->pktsize
= pktsize
- 4;
206 c
->flags
= avio_rl16(s
->pb
);
207 if (c
->flags
& VB_HAS_AUDIO
&& !c
->has_audio
)
208 return AVERROR_INVALIDDATA
;
209 c
->gmcsize
= (c
->flags
& VB_HAS_GMC
) ? 4 : 0;
211 avio_read(s
->pb
, c
->gmc
, c
->gmcsize
);
212 c
->sndsize
= (c
->flags
& VB_HAS_AUDIO
) ? avio_rl32(s
->pb
) : 0;
213 c
->curstrm
= !!(c
->flags
& VB_HAS_AUDIO
);
217 if (c
->pktsize
< 2LL + c
->sndsize
+ c
->gmcsize
)
218 return AVERROR_INVALIDDATA
;
220 size
= c
->pktsize
- c
->sndsize
- c
->gmcsize
- 2;
221 size
= ffio_limit(s
->pb
, size
);
222 if ((ret
= av_new_packet(pkt
, size
+ c
->gmcsize
+ 2)) < 0)
224 AV_WL16(pkt
->data
, c
->flags
);
226 memcpy(pkt
->data
+ 2, c
->gmc
, c
->gmcsize
);
227 if (avio_read(s
->pb
, pkt
->data
+ 2 + c
->gmcsize
, size
) != size
) {
228 return AVERROR_INVALIDDATA
;
230 pkt
->stream_index
= 0;
233 int pktsize
= av_get_packet(s
->pb
, pkt
, c
->sndsize
- 4);
236 pkt
->stream_index
= 1;
237 pkt
->duration
= pktsize
;
240 if (!c
->cur_frame
|| c
->curstrm
)
241 pkt
->flags
|= AV_PKT_FLAG_KEY
;
242 if (c
->curstrm
== -1)
245 int pktsize
= av_get_packet(s
->pb
, pkt
, c
->block_align
);
250 pkt
->duration
= pktsize
;
255 AVInputFormat ff_siff_demuxer
= {
257 .long_name
= NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
258 .priv_data_size
= sizeof(SIFFContext
),
259 .read_probe
= siff_probe
,
260 .read_header
= siff_read_header
,
261 .read_packet
= siff_read_packet
,
262 .extensions
= "vb,son",