FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/smush.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 100 148 67.6%
Functions: 3 3 100.0%
Branches: 33 73 45.2%

Line Branch Exec Source
1 /*
2 * LucasArts Smush demuxer
3 * Copyright (c) 2006 Cyril Zorin
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/intreadwrite.h"
23
24 #include "avformat.h"
25 #include "avio.h"
26 #include "demux.h"
27 #include "internal.h"
28
29 typedef struct SMUSHContext {
30 int version;
31 int audio_stream_index;
32 int video_stream_index;
33 } SMUSHContext;
34
35 7128 static int smush_read_probe(const AVProbeData *p)
36 {
37
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7126 times.
7128 if (((AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M') &&
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 AV_RL32(p->buf + 8) == MKTAG('S', 'H', 'D', 'R')) ||
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7126 times.
7126 (AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M') &&
40 AV_RL32(p->buf + 8) == MKTAG('A', 'H', 'D', 'R')))) {
41 2 return AVPROBE_SCORE_MAX;
42 }
43
44 7126 return 0;
45 }
46
47 2 static int smush_read_header(AVFormatContext *ctx)
48 {
49 2 SMUSHContext *smush = ctx->priv_data;
50 2 AVIOContext *pb = ctx->pb;
51 AVStream *vst, *ast;
52 uint32_t magic, nframes, size, subversion, i;
53 2 uint32_t width = 0, height = 0, got_audio = 0, read = 0;
54 uint32_t sample_rate, channels, palette[256];
55 int ret;
56
57 2 magic = avio_rb32(pb);
58 2 avio_skip(pb, 4); // skip movie size
59
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (magic == MKBETAG('A', 'N', 'I', 'M')) {
61 if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
62 return AVERROR_INVALIDDATA;
63
64 size = avio_rb32(pb);
65 if (size < 3 * 256 + 6)
66 return AVERROR_INVALIDDATA;
67
68 smush->version = 0;
69 subversion = avio_rl16(pb);
70 nframes = avio_rl16(pb);
71 if (!nframes)
72 return AVERROR_INVALIDDATA;
73
74 avio_skip(pb, 2); // skip pad
75
76 for (i = 0; i < 256; i++)
77 palette[i] = avio_rb24(pb);
78
79 avio_skip(pb, size - (3 * 256 + 6));
80
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
81
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
82 return AVERROR_INVALIDDATA;
83
84 2 size = avio_rb32(pb);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size < 14)
86 return AVERROR_INVALIDDATA;
87
88 2 smush->version = 1;
89 2 subversion = avio_rl16(pb);
90 2 nframes = avio_rl32(pb);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!nframes)
92 return AVERROR_INVALIDDATA;
93
94 2 avio_skip(pb, 2); // skip pad
95 2 width = avio_rl16(pb);
96 2 height = avio_rl16(pb);
97 2 avio_skip(pb, 2); // skip pad
98 2 avio_skip(pb, size - 14);
99
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
101 return AVERROR_INVALIDDATA;
102
103 2 size = avio_rb32(pb);
104
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 while (!got_audio && ((read + 8) < size)) {
105 uint32_t sig, chunk_size;
106
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_feof(pb))
108 return AVERROR_EOF;
109
110 2 sig = avio_rb32(pb);
111 2 chunk_size = avio_rb32(pb);
112 2 read += 8;
113
1/3
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2 switch (sig) {
114 2 case MKBETAG('W', 'a', 'v', 'e'):
115 2 got_audio = 1;
116 2 sample_rate = avio_rl32(pb);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sample_rate)
118 return AVERROR_INVALIDDATA;
119
120 2 channels = avio_rl32(pb);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!channels)
122 return AVERROR_INVALIDDATA;
123
124 2 avio_skip(pb, chunk_size - 8);
125 2 read += chunk_size;
126 2 break;
127 case MKBETAG('B', 'l', '1', '6'):
128 case MKBETAG('A', 'N', 'N', 'O'):
129 avio_skip(pb, chunk_size);
130 read += chunk_size;
131 break;
132 default:
133 return AVERROR_INVALIDDATA;
134 }
135 }
136
137 2 avio_skip(pb, size - read);
138 } else {
139 av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
140 return AVERROR_INVALIDDATA;
141 }
142
143 2 vst = avformat_new_stream(ctx, 0);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!vst)
145 return AVERROR(ENOMEM);
146
147 2 smush->video_stream_index = vst->index;
148
149 2 avpriv_set_pts_info(vst, 64, 1, 15);
150
151 2 vst->start_time = 0;
152 2 vst->duration =
153 2 vst->nb_frames = nframes;
154 2 vst->avg_frame_rate = av_inv_q(vst->time_base);
155 2 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
156 2 vst->codecpar->codec_id = AV_CODEC_ID_SANM;
157 2 vst->codecpar->codec_tag = 0;
158 2 vst->codecpar->width = width;
159 2 vst->codecpar->height = height;
160
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!smush->version) {
162 if ((ret = ff_alloc_extradata(vst->codecpar, 1024 + 2)) < 0)
163 return ret;
164
165 AV_WL16(vst->codecpar->extradata, subversion);
166 for (i = 0; i < 256; i++)
167 AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
168 }
169
170
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (got_audio) {
171 2 ast = avformat_new_stream(ctx, 0);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ast)
173 return AVERROR(ENOMEM);
174
175 2 smush->audio_stream_index = ast->index;
176
177 2 ast->start_time = 0;
178 2 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
179 2 ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_VIMA;
180 2 ast->codecpar->codec_tag = 0;
181 2 ast->codecpar->sample_rate = sample_rate;
182 2 ast->codecpar->ch_layout.nb_channels = channels;
183 2 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
184 }
185
186 2 return 0;
187 }
188
189 68 static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
190 {
191 68 SMUSHContext *smush = ctx->priv_data;
192 68 AVIOContext *pb = ctx->pb;
193 68 int done = 0;
194 int ret;
195
196
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 64 times.
164 while (!done) {
197 uint32_t sig, size;
198
199
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 96 times.
100 if (avio_feof(pb))
200 4 return AVERROR_EOF;
201
202 96 sig = avio_rb32(pb);
203 96 size = avio_rb32(pb);
204
205
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
96 switch (sig) {
206 32 case MKBETAG('F', 'R', 'M', 'E'):
207
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (smush->version)
208 32 break;
209 if ((ret = av_get_packet(pb, pkt, size)) < 0)
210 return ret;
211
212 pkt->stream_index = smush->video_stream_index;
213 done = 1;
214 break;
215 32 case MKBETAG('B', 'l', '1', '6'):
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 if ((ret = av_get_packet(pb, pkt, size)) < 0)
217 return ret;
218
219 32 pkt->stream_index = smush->video_stream_index;
220 32 pkt->duration = 1;
221 32 done = 1;
222 32 break;
223 32 case MKBETAG('W', 'a', 'v', 'e'):
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (size < 13)
225 return AVERROR_INVALIDDATA;
226
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 if (av_get_packet(pb, pkt, size) < 13)
227 return AVERROR(EIO);
228
229 32 pkt->stream_index = smush->audio_stream_index;
230 32 pkt->flags |= AV_PKT_FLAG_KEY;
231 32 pkt->duration = AV_RB32(pkt->data);
232
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (pkt->duration == 0xFFFFFFFFu)
233 32 pkt->duration = AV_RB32(pkt->data + 8);
234 32 done = 1;
235 32 break;
236 default:
237 avio_skip(pb, size);
238 break;
239 }
240 }
241
242 64 return 0;
243 }
244
245 const FFInputFormat ff_smush_demuxer = {
246 .p.name = "smush",
247 .p.long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
248 .priv_data_size = sizeof(SMUSHContext),
249 .read_probe = smush_read_probe,
250 .read_header = smush_read_header,
251 .read_packet = smush_read_packet,
252 };
253