FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/siff.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 93 135 68.9%
Functions: 5 6 83.3%
Branches: 42 76 55.3%

Line Branch Exec 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 7127 static int siff_probe(const AVProbeData *p)
67 {
68 7127 uint32_t tag = AV_RL32(p->buf + 8);
69 /* check file header */
70
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7125 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
7127 if (AV_RL32(p->buf) != TAG_SIFF ||
71 (tag != TAG_VBV1 && tag != TAG_SOUN))
72 7125 return 0;
73 2 return AVPROBE_SCORE_MAX;
74 }
75
76 2 static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
77 {
78 AVStream *ast;
79 2 ast = avformat_new_stream(s, NULL);
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ast)
81 return AVERROR(ENOMEM);
82 2 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
83 2 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
84 2 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
85 2 ast->codecpar->bits_per_coded_sample = 8;
86 2 ast->codecpar->sample_rate = c->rate;
87 2 avpriv_set_pts_info(ast, 16, 1, c->rate);
88 2 ast->start_time = 0;
89 2 return 0;
90 }
91
92 2 static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
93 {
94 AVStream *st;
95 int width, height;
96
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_rl32(pb) != TAG_VBHD) {
98 av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
99 return AVERROR_INVALIDDATA;
100 }
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_rb32(pb) != 32) {
102 av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
103 return AVERROR_INVALIDDATA;
104 }
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_rl16(pb) != 1) {
106 av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
107 return AVERROR_INVALIDDATA;
108 }
109 2 width = avio_rl16(pb);
110 2 height = avio_rl16(pb);
111 2 avio_skip(pb, 4);
112 2 c->frames = avio_rl16(pb);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!c->frames) {
114 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
115 return AVERROR_INVALIDDATA;
116 }
117 2 c->bits = avio_rl16(pb);
118 2 c->rate = avio_rl16(pb);
119 2 c->block_align = c->rate * (c->bits >> 3);
120
121 2 avio_skip(pb, 16); // zeroes
122
123 2 st = avformat_new_stream(s, NULL);
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st)
125 return AVERROR(ENOMEM);
126 2 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
127 2 st->codecpar->codec_id = AV_CODEC_ID_VB;
128 2 st->codecpar->codec_tag = MKTAG('V', 'B', 'V', '1');
129 2 st->codecpar->width = width;
130 2 st->codecpar->height = height;
131 2 st->codecpar->format = AV_PIX_FMT_PAL8;
132 2 st->nb_frames =
133 2 st->duration = c->frames;
134 2 avpriv_set_pts_info(st, 16, 1, 12);
135
136 2 c->cur_frame = 0;
137 2 c->has_video = 1;
138 2 c->has_audio = !!c->rate;
139 2 c->curstrm = -1;
140
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (c->has_audio)
141 2 return create_audio_stream(s, c);
142 return 0;
143 }
144
145 static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
146 {
147 if (avio_rl32(pb) != TAG_SHDR) {
148 av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
149 return AVERROR_INVALIDDATA;
150 }
151 if (avio_rb32(pb) != 8) {
152 av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
153 return AVERROR_INVALIDDATA;
154 }
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);
160 }
161
162 2 static int siff_read_header(AVFormatContext *s)
163 {
164 2 AVIOContext *pb = s->pb;
165 2 SIFFContext *c = s->priv_data;
166 uint32_t tag;
167 int ret;
168
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_rl32(pb) != TAG_SIFF)
170 return AVERROR_INVALIDDATA;
171 2 avio_skip(pb, 4); // ignore size
172 2 tag = avio_rl32(pb);
173
174
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (tag != TAG_VBV1 && tag != TAG_SOUN) {
175 av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
176 return AVERROR_INVALIDDATA;
177 }
178
179
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0)
180 return ret;
181
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2 if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0)
182 return ret;
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 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;
186 }
187 2 avio_skip(pb, 4); // ignore size
188
189 2 return 0;
190 }
191
192 181 static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
193 {
194 181 SIFFContext *c = s->priv_data;
195 int ret;
196
197
1/2
✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
181 if (c->has_video) {
198 unsigned int size;
199
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 180 times.
181 if (c->cur_frame >= c->frames)
200 1 return AVERROR_EOF;
201
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 17 times.
180 if (c->curstrm == -1) {
202 163 c->pktsize = avio_rl32(s->pb) - 4;
203 163 c->flags = avio_rl16(s->pb);
204
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 146 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
163 if (c->flags & VB_HAS_AUDIO && !c->has_audio)
205 return AVERROR_INVALIDDATA;
206 163 c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
207
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 143 times.
163 if (c->gmcsize)
208 20 avio_read(s->pb, c->gmc, c->gmcsize);
209
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 146 times.
163 c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb) : 0;
210 163 c->curstrm = !!(c->flags & VB_HAS_AUDIO);
211 }
212
213
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 17 times.
180 if (!c->curstrm) {
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 if (c->pktsize < 2LL + c->sndsize + c->gmcsize)
215 return AVERROR_INVALIDDATA;
216
217 163 size = c->pktsize - c->sndsize - c->gmcsize - 2;
218 163 size = ffio_limit(s->pb, size);
219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 163 times.
163 if ((ret = av_new_packet(pkt, size + c->gmcsize + 2)) < 0)
220 return ret;
221 163 AV_WL16(pkt->data, c->flags);
222
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 143 times.
163 if (c->gmcsize)
223 20 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
224
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 163 times.
163 if (avio_read(s->pb, pkt->data + 2 + c->gmcsize, size) != size) {
225 return AVERROR_INVALIDDATA;
226 }
227 163 pkt->stream_index = 0;
228 163 c->curstrm = -1;
229 } else {
230 17 int pktsize = av_get_packet(s->pb, pkt, c->sndsize - 4);
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (pktsize < 0)
232 return AVERROR(EIO);
233 17 pkt->stream_index = 1;
234 17 pkt->duration = pktsize;
235 17 c->curstrm = 0;
236 }
237
4/4
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 161 times.
✓ Branch 3 taken 15 times.
180 if (!c->cur_frame || c->curstrm)
238 165 pkt->flags |= AV_PKT_FLAG_KEY;
239
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 17 times.
180 if (c->curstrm == -1)
240 163 c->cur_frame++;
241 } else {
242 int pktsize = av_get_packet(s->pb, pkt, c->block_align);
243 if (!pktsize)
244 return AVERROR_EOF;
245 if (pktsize <= 0)
246 return AVERROR(EIO);
247 pkt->duration = pktsize;
248 }
249 180 return pkt->size;
250 }
251
252 const FFInputFormat ff_siff_demuxer = {
253 .p.name = "siff",
254 .p.long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
255 .p.extensions = "vb,son",
256 .priv_data_size = sizeof(SIFFContext),
257 .read_probe = siff_probe,
258 .read_header = siff_read_header,
259 .read_packet = siff_read_packet,
260 };
261