/src/ffmpeg/libavformat/siff.c
Line | Count | Source |
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 | | |
22 | | #include "libavutil/channel_layout.h" |
23 | | #include "libavutil/intreadwrite.h" |
24 | | |
25 | | #include "avformat.h" |
26 | | #include "demux.h" |
27 | | #include "internal.h" |
28 | | #include "avio_internal.h" |
29 | | |
30 | | enum SIFFTags { |
31 | | TAG_SIFF = MKTAG('S', 'I', 'F', 'F'), |
32 | | TAG_BODY = MKTAG('B', 'O', 'D', 'Y'), |
33 | | TAG_VBHD = MKTAG('V', 'B', 'H', 'D'), |
34 | | TAG_SHDR = MKTAG('S', 'H', 'D', 'R'), |
35 | | TAG_VBV1 = MKTAG('V', 'B', 'V', '1'), |
36 | | TAG_SOUN = MKTAG('S', 'O', 'U', 'N'), |
37 | | }; |
38 | | |
39 | | enum VBFlags { |
40 | | VB_HAS_GMC = 0x01, |
41 | | VB_HAS_AUDIO = 0x04, |
42 | | VB_HAS_VIDEO = 0x08, |
43 | | VB_HAS_PALETTE = 0x10, |
44 | | VB_HAS_LENGTH = 0x20 |
45 | | }; |
46 | | |
47 | | typedef struct SIFFContext { |
48 | | int frames; |
49 | | int cur_frame; |
50 | | int rate; |
51 | | int bits; |
52 | | int block_align; |
53 | | |
54 | | int has_video; |
55 | | int has_audio; |
56 | | |
57 | | int curstrm; |
58 | | unsigned int pktsize; |
59 | | int gmcsize; |
60 | | unsigned int sndsize; |
61 | | |
62 | | unsigned int flags; |
63 | | uint8_t gmc[4]; |
64 | | } SIFFContext; |
65 | | |
66 | | static int siff_probe(const AVProbeData *p) |
67 | 964k | { |
68 | 964k | uint32_t tag = AV_RL32(p->buf + 8); |
69 | | /* check file header */ |
70 | 964k | if (AV_RL32(p->buf) != TAG_SIFF || |
71 | 394 | (tag != TAG_VBV1 && tag != TAG_SOUN)) |
72 | 964k | return 0; |
73 | 121 | return AVPROBE_SCORE_MAX; |
74 | 964k | } |
75 | | |
76 | | static int create_audio_stream(AVFormatContext *s, SIFFContext *c) |
77 | 1.55k | { |
78 | 1.55k | AVStream *ast; |
79 | 1.55k | ast = avformat_new_stream(s, NULL); |
80 | 1.55k | if (!ast) |
81 | 0 | return AVERROR(ENOMEM); |
82 | 1.55k | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
83 | 1.55k | ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8; |
84 | 1.55k | ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; |
85 | 1.55k | ast->codecpar->bits_per_coded_sample = 8; |
86 | 1.55k | ast->codecpar->sample_rate = c->rate; |
87 | 1.55k | avpriv_set_pts_info(ast, 16, 1, c->rate); |
88 | 1.55k | ast->start_time = 0; |
89 | 1.55k | return 0; |
90 | 1.55k | } |
91 | | |
92 | | static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb) |
93 | 1.09k | { |
94 | 1.09k | AVStream *st; |
95 | 1.09k | int width, height; |
96 | | |
97 | 1.09k | if (avio_rl32(pb) != TAG_VBHD) { |
98 | 63 | av_log(s, AV_LOG_ERROR, "Header chunk is missing\n"); |
99 | 63 | return AVERROR_INVALIDDATA; |
100 | 63 | } |
101 | 1.03k | if (avio_rb32(pb) != 32) { |
102 | 46 | av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n"); |
103 | 46 | return AVERROR_INVALIDDATA; |
104 | 46 | } |
105 | 990 | if (avio_rl16(pb) != 1) { |
106 | 19 | av_log(s, AV_LOG_ERROR, "Incorrect header version\n"); |
107 | 19 | return AVERROR_INVALIDDATA; |
108 | 19 | } |
109 | 971 | width = avio_rl16(pb); |
110 | 971 | height = avio_rl16(pb); |
111 | 971 | avio_skip(pb, 4); |
112 | 971 | c->frames = avio_rl16(pb); |
113 | 971 | if (!c->frames) { |
114 | 11 | av_log(s, AV_LOG_ERROR, "File contains no frames ???\n"); |
115 | 11 | return AVERROR_INVALIDDATA; |
116 | 11 | } |
117 | 960 | c->bits = avio_rl16(pb); |
118 | 960 | c->rate = avio_rl16(pb); |
119 | 960 | c->block_align = c->rate * (c->bits >> 3); |
120 | | |
121 | 960 | avio_skip(pb, 16); // zeroes |
122 | | |
123 | 960 | st = avformat_new_stream(s, NULL); |
124 | 960 | if (!st) |
125 | 0 | return AVERROR(ENOMEM); |
126 | 960 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
127 | 960 | st->codecpar->codec_id = AV_CODEC_ID_VB; |
128 | 960 | st->codecpar->codec_tag = MKTAG('V', 'B', 'V', '1'); |
129 | 960 | st->codecpar->width = width; |
130 | 960 | st->codecpar->height = height; |
131 | 960 | st->codecpar->format = AV_PIX_FMT_PAL8; |
132 | 960 | st->nb_frames = |
133 | 960 | st->duration = c->frames; |
134 | 960 | avpriv_set_pts_info(st, 16, 1, 12); |
135 | | |
136 | 960 | c->cur_frame = 0; |
137 | 960 | c->has_video = 1; |
138 | 960 | c->has_audio = !!c->rate; |
139 | 960 | c->curstrm = -1; |
140 | 960 | if (c->has_audio) |
141 | 904 | return create_audio_stream(s, c); |
142 | 56 | return 0; |
143 | 960 | } |
144 | | |
145 | | static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb) |
146 | 748 | { |
147 | 748 | if (avio_rl32(pb) != TAG_SHDR) { |
148 | 61 | av_log(s, AV_LOG_ERROR, "Header chunk is missing\n"); |
149 | 61 | return AVERROR_INVALIDDATA; |
150 | 61 | } |
151 | 687 | if (avio_rb32(pb) != 8) { |
152 | 41 | av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n"); |
153 | 41 | return AVERROR_INVALIDDATA; |
154 | 41 | } |
155 | 646 | avio_skip(pb, 4); // unknown value |
156 | 646 | c->rate = avio_rl16(pb); |
157 | 646 | c->bits = avio_rl16(pb); |
158 | 646 | c->block_align = c->rate * (c->bits >> 3); |
159 | 646 | return create_audio_stream(s, c); |
160 | 687 | } |
161 | | |
162 | | static int siff_read_header(AVFormatContext *s) |
163 | 2.14k | { |
164 | 2.14k | AVIOContext *pb = s->pb; |
165 | 2.14k | SIFFContext *c = s->priv_data; |
166 | 2.14k | uint32_t tag; |
167 | 2.14k | int ret; |
168 | | |
169 | 2.14k | if (avio_rl32(pb) != TAG_SIFF) |
170 | 191 | return AVERROR_INVALIDDATA; |
171 | 1.95k | avio_skip(pb, 4); // ignore size |
172 | 1.95k | tag = avio_rl32(pb); |
173 | | |
174 | 1.95k | if (tag != TAG_VBV1 && tag != TAG_SOUN) { |
175 | 109 | av_log(s, AV_LOG_ERROR, "Not a VBV file\n"); |
176 | 109 | return AVERROR_INVALIDDATA; |
177 | 109 | } |
178 | | |
179 | 1.84k | if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0) |
180 | 139 | return ret; |
181 | 1.70k | if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0) |
182 | 102 | return ret; |
183 | 1.60k | if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')) { |
184 | 122 | av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n"); |
185 | 122 | return AVERROR_INVALIDDATA; |
186 | 122 | } |
187 | 1.48k | avio_skip(pb, 4); // ignore size |
188 | | |
189 | 1.48k | return 0; |
190 | 1.60k | } |
191 | | |
192 | | static int siff_read_packet(AVFormatContext *s, AVPacket *pkt) |
193 | 53.3k | { |
194 | 53.3k | SIFFContext *c = s->priv_data; |
195 | 53.3k | int ret; |
196 | | |
197 | 53.3k | if (c->has_video) { |
198 | 18.3k | unsigned int size; |
199 | 18.3k | if (c->cur_frame >= c->frames) |
200 | 8 | return AVERROR_EOF; |
201 | 18.3k | if (c->curstrm == -1) { |
202 | 12.0k | unsigned pktsize = avio_rl32(s->pb); |
203 | 12.0k | if (pktsize < 4) |
204 | 353 | return AVERROR_INVALIDDATA; |
205 | 11.6k | c->pktsize = pktsize - 4; |
206 | 11.6k | c->flags = avio_rl16(s->pb); |
207 | 11.6k | if (c->flags & VB_HAS_AUDIO && !c->has_audio) |
208 | 18 | return AVERROR_INVALIDDATA; |
209 | 11.6k | c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0; |
210 | 11.6k | if (c->gmcsize) |
211 | 1.92k | avio_read(s->pb, c->gmc, c->gmcsize); |
212 | 11.6k | c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb) : 0; |
213 | 11.6k | c->curstrm = !!(c->flags & VB_HAS_AUDIO); |
214 | 11.6k | } |
215 | | |
216 | 17.9k | if (!c->curstrm) { |
217 | 12.0k | if (c->pktsize < 2LL + c->sndsize + c->gmcsize) |
218 | 285 | return AVERROR_INVALIDDATA; |
219 | | |
220 | 11.8k | size = c->pktsize - c->sndsize - c->gmcsize - 2; |
221 | 11.8k | size = ffio_limit(s->pb, size); |
222 | 11.8k | if ((ret = av_new_packet(pkt, size + c->gmcsize + 2)) < 0) |
223 | 246 | return ret; |
224 | 11.5k | AV_WL16(pkt->data, c->flags); |
225 | 11.5k | if (c->gmcsize) |
226 | 1.85k | memcpy(pkt->data + 2, c->gmc, c->gmcsize); |
227 | 11.5k | if (avio_read(s->pb, pkt->data + 2 + c->gmcsize, size) != size) { |
228 | 625 | return AVERROR_INVALIDDATA; |
229 | 625 | } |
230 | 10.9k | pkt->stream_index = 0; |
231 | 10.9k | c->curstrm = -1; |
232 | 10.9k | } else { |
233 | 5.87k | int pktsize = av_get_packet(s->pb, pkt, c->sndsize - 4); |
234 | 5.87k | if (pktsize < 0) |
235 | 197 | return AVERROR_INVALIDDATA; |
236 | 5.68k | pkt->stream_index = 1; |
237 | 5.68k | pkt->duration = pktsize; |
238 | 5.68k | c->curstrm = 0; |
239 | 5.68k | } |
240 | 16.6k | if (!c->cur_frame || c->curstrm) |
241 | 11.3k | pkt->flags |= AV_PKT_FLAG_KEY; |
242 | 16.6k | if (c->curstrm == -1) |
243 | 10.9k | c->cur_frame++; |
244 | 35.0k | } else { |
245 | 35.0k | int pktsize = av_get_packet(s->pb, pkt, c->block_align); |
246 | 35.0k | if (!pktsize) |
247 | 49 | return AVERROR_EOF; |
248 | 35.0k | if (pktsize <= 0) |
249 | 882 | return AVERROR_INVALIDDATA; |
250 | 34.1k | pkt->duration = pktsize; |
251 | 34.1k | } |
252 | 50.7k | return pkt->size; |
253 | 53.3k | } |
254 | | |
255 | | const FFInputFormat ff_siff_demuxer = { |
256 | | .p.name = "siff", |
257 | | .p.long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"), |
258 | | .p.extensions = "vb,son", |
259 | | .priv_data_size = sizeof(SIFFContext), |
260 | | .read_probe = siff_probe, |
261 | | .read_header = siff_read_header, |
262 | | .read_packet = siff_read_packet, |
263 | | }; |