FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/smacker.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 156 224 69.6%
Functions: 3 4 75.0%
Branches: 71 132 53.8%

Line Branch Exec Source
1 /*
2 * Smacker demuxer
3 * Copyright (c) 2006 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 /*
23 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
24 */
25
26 #include <inttypes.h>
27
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/mem.h"
31 #include "avformat.h"
32 #include "avio_internal.h"
33 #include "demux.h"
34 #include "internal.h"
35
36 #define SMACKER_PAL 0x01
37 #define SMACKER_FLAG_RING_FRAME 0x01
38 #define SMACKER_FLAG_Y_INTERLACE (1 << 1)
39 #define SMACKER_FLAG_Y_DOUBLE (1 << 2)
40
41 enum SAudFlags {
42 SMK_AUD_PACKED = 0x80,
43 SMK_AUD_16BITS = 0x20,
44 SMK_AUD_STEREO = 0x10,
45 SMK_AUD_BINKAUD = 0x08,
46 SMK_AUD_USEDCT = 0x04
47 };
48
49 typedef struct SmackerContext {
50 uint32_t frames;
51 /* frame info */
52 uint32_t *frm_size;
53 uint8_t *frm_flags;
54 /* internal variables */
55 int64_t next_frame_pos;
56 int cur_frame;
57 int videoindex;
58 int indexes[7];
59 int duration_size[7];
60 /* current frame for demuxing */
61 uint32_t frame_size;
62 int flags;
63 int next_audio_index;
64 int new_palette;
65 uint8_t pal[768];
66 int64_t aud_pts[7];
67 } SmackerContext;
68
69 /* palette used in Smacker */
70 static const uint8_t smk_pal[64] = {
71 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
72 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
73 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
74 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
75 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
76 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
77 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
78 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
79 };
80
81
82 7186 static int smacker_probe(const AVProbeData *p)
83 {
84
2/2
✓ Branch 0 taken 7184 times.
✓ Branch 1 taken 2 times.
7186 if ( AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
85
1/2
✓ Branch 0 taken 7184 times.
✗ Branch 1 not taken.
7184 && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
86 7184 return 0;
87
88
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
89 return AVPROBE_SCORE_MAX/4;
90
91 2 return AVPROBE_SCORE_MAX;
92 }
93
94 2 static int smacker_read_header(AVFormatContext *s)
95 {
96 2 AVIOContext *pb = s->pb;
97 2 SmackerContext *smk = s->priv_data;
98 AVStream *st;
99 AVCodecParameters *par;
100 uint32_t magic, width, height, flags, treesize;
101 int64_t pos;
102 int i, ret, pts_inc;
103 int tbase;
104
105 /* read and check header */
106 2 magic = avio_rl32(pb);
107
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (magic != MKTAG('S', 'M', 'K', '2') && magic != MKTAG('S', 'M', 'K', '4'))
108 return AVERROR_INVALIDDATA;
109 2 width = avio_rl32(pb);
110 2 height = avio_rl32(pb);
111 2 smk->frames = avio_rl32(pb);
112 2 pts_inc = avio_rl32(pb);
113
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (pts_inc > INT_MAX / 100 || pts_inc == INT_MIN) {
114 av_log(s, AV_LOG_ERROR, "pts_inc %d is invalid\n", pts_inc);
115 return AVERROR_INVALIDDATA;
116 }
117
118 2 flags = avio_rl32(pb);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (flags & SMACKER_FLAG_RING_FRAME)
120 smk->frames++;
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (smk->frames > 0xFFFFFF) {
122 av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
123 return AVERROR_INVALIDDATA;
124 }
125
126 2 avio_skip(pb, 28); /* Unused audio related data */
127
128 2 treesize = avio_rl32(pb);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (treesize >= UINT_MAX/4) {
130 // treesize + 16 must not overflow (this check is probably redundant)
131 av_log(s, AV_LOG_ERROR, "treesize too large\n");
132 return AVERROR_INVALIDDATA;
133 }
134
135 2 st = avformat_new_stream(s, NULL);
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st)
137 return AVERROR(ENOMEM);
138
139 2 smk->videoindex = st->index;
140 /* Smacker uses 100000 as internal timebase */
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (pts_inc < 0)
142 pts_inc = -pts_inc;
143 else
144 2 pts_inc *= 100;
145 2 tbase = 100000;
146 2 av_reduce(&tbase, &pts_inc, tbase, pts_inc, (1UL << 31) - 1);
147 2 avpriv_set_pts_info(st, 33, pts_inc, tbase);
148 2 st->duration = smk->frames;
149
150 2 st->sample_aspect_ratio = (AVRational){ 1, 1 +
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 !!(flags & (SMACKER_FLAG_Y_INTERLACE | SMACKER_FLAG_Y_DOUBLE)) };
152
153 /* init video codec */
154 2 par = st->codecpar;
155 2 par->width = width;
156 2 par->height = height;
157 2 par->format = AV_PIX_FMT_PAL8;
158 2 par->codec_type = AVMEDIA_TYPE_VIDEO;
159 2 par->codec_id = AV_CODEC_ID_SMACKVIDEO;
160 2 par->codec_tag = magic;
161
162
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_alloc_extradata(par, treesize + 16)) < 0) {
163 av_log(s, AV_LOG_ERROR,
164 "Cannot allocate %"PRIu32" bytes of extradata\n",
165 treesize + 16);
166 return ret;
167 }
168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ffio_read_size(pb, par->extradata, 16)) < 0)
169 return ret;
170
171 /* handle possible audio streams */
172
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 for (i = 0; i < 7; i++) {
173 14 uint32_t rate = avio_rl24(pb);
174 14 uint8_t aflag = avio_r8(pb);
175
176 14 smk->indexes[i] = -1;
177
178
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if (rate) {
179 2 AVStream *ast = avformat_new_stream(s, NULL);
180 AVCodecParameters *par;
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ast)
182 return AVERROR(ENOMEM);
183
184 2 smk->indexes[i] = ast->index;
185 2 par = ast->codecpar;
186 2 par->codec_type = AVMEDIA_TYPE_AUDIO;
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (aflag & SMK_AUD_BINKAUD) {
188 par->codec_id = AV_CODEC_ID_BINKAUDIO_RDFT;
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (aflag & SMK_AUD_USEDCT) {
190 par->codec_id = AV_CODEC_ID_BINKAUDIO_DCT;
191
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (aflag & SMK_AUD_PACKED) {
192 2 par->codec_id = AV_CODEC_ID_SMACKAUDIO;
193 2 par->codec_tag = MKTAG('S', 'M', 'K', 'A');
194 } else {
195 par->codec_id = AV_CODEC_ID_PCM_U8;
196 }
197 2 av_channel_layout_default(&par->ch_layout,
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 !!(aflag & SMK_AUD_STEREO) + 1);
199 2 par->sample_rate = rate;
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 par->bits_per_coded_sample = (aflag & SMK_AUD_16BITS) ? 16 : 8;
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (par->bits_per_coded_sample == 16 &&
202 par->codec_id == AV_CODEC_ID_PCM_U8)
203 par->codec_id = AV_CODEC_ID_PCM_S16LE;
204 else
205 2 smk->duration_size[i] = 4;
206 2 avpriv_set_pts_info(ast, 64, 1, par->sample_rate * par->ch_layout.nb_channels
207 2 * par->bits_per_coded_sample / 8);
208 }
209 }
210
211 2 avio_rl32(pb); /* padding */
212
213 /* setup data */
214 2 st->priv_data = av_malloc_array(smk->frames, sizeof(*smk->frm_size) +
215 sizeof(*smk->frm_flags));
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st->priv_data)
217 return AVERROR(ENOMEM);
218 2 smk->frm_size = st->priv_data;
219 2 smk->frm_flags = (void*)(smk->frm_size + smk->frames);
220
221 /* read frame info */
222 2 pos = 0;
223
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 2 times.
202 for (i = 0; i < smk->frames; i++) {
224 200 smk->frm_size[i] = avio_rl32(pb);
225
3/4
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 200 times.
398 if ((ret = av_add_index_entry(st, pos, i, smk->frm_size[i], 0,
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
198 (i == 0 || (smk->frm_size[i] & 1)) ? AVINDEX_KEYFRAME : 0)) < 0)
227 return ret;
228 200 pos += smk->frm_size[i];
229 }
230
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
4 if ((ret = ffio_read_size(pb, smk->frm_flags, smk->frames)) < 0 ||
231 /* load trees to extradata, they will be unpacked by decoder */
232 2 (ret = ffio_read_size(pb, par->extradata + 16,
233 2 par->extradata_size - 16)) < 0) {
234 return ret;
235 }
236
237 2 return 0;
238 }
239
240 289 static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
241 {
242 289 SmackerContext *smk = s->priv_data;
243 int flags;
244 int ret;
245
246
3/4
✓ Branch 1 taken 289 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 287 times.
289 if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
247 2 return AVERROR_EOF;
248
249 /* if we demuxed all streams, pass another frame */
250
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 87 times.
287 if (!smk->next_audio_index) {
251 200 smk->frame_size = smk->frm_size[smk->cur_frame] & (~3);
252 200 smk->next_frame_pos = avio_tell(s->pb) + smk->frame_size;
253 200 flags = smk->frm_flags[smk->cur_frame];
254 200 smk->flags = flags >> 1;
255 /* handle palette change event */
256
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 198 times.
200 if (flags & SMACKER_PAL) {
257 int size, sz, t, off, j, pos;
258 2 uint8_t *pal = smk->pal;
259 uint8_t oldpal[768];
260
261 2 memcpy(oldpal, pal, 768);
262 2 size = avio_r8(s->pb);
263 2 size = size * 4;
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size > smk->frame_size) {
265 ret = AVERROR_INVALIDDATA;
266 goto next_frame;
267 }
268 2 smk->frame_size -= size--;
269 2 sz = 0;
270 2 pos = avio_tell(s->pb) + size;
271
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 while (sz < 256) {
272 512 t = avio_r8(s->pb);
273
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 510 times.
512 if (t & 0x80) { /* skip palette entries */
274 2 sz += (t & 0x7F) + 1;
275 2 pal += ((t & 0x7F) + 1) * 3;
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 510 times.
510 } else if (t & 0x40) { /* copy with offset */
277 off = avio_r8(s->pb);
278 j = (t & 0x3F) + 1;
279 if (off + j > 0x100) {
280 av_log(s, AV_LOG_ERROR,
281 "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
282 off, j);
283 ret = AVERROR_INVALIDDATA;
284 goto next_frame;
285 }
286 off *= 3;
287 while (j-- && sz < 256) {
288 *pal++ = oldpal[off + 0];
289 *pal++ = oldpal[off + 1];
290 *pal++ = oldpal[off + 2];
291 sz++;
292 off += 3;
293 }
294 } else { /* new entries */
295 510 *pal++ = smk_pal[t];
296 510 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
297 510 *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
298 510 sz++;
299 }
300 }
301 2 avio_seek(s->pb, pos, 0);
302 2 smk->new_palette = 1;
303 }
304 }
305
306
2/2
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 200 times.
1600 for (int i = smk->next_audio_index; i < 7; i++) {
307
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 1228 times.
1400 if (smk->flags & (1 << i)) {
308 uint32_t size;
309
310 172 size = avio_rl32(s->pb);
311
2/4
✓ Branch 0 taken 172 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 172 times.
172 if ((int)size < 4 + smk->duration_size[i] || size > smk->frame_size) {
312 av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
313 ret = AVERROR_INVALIDDATA;
314 goto next_frame;
315 }
316 172 smk->frame_size -= size;
317 172 size -= 4;
318
319
1/2
✓ Branch 0 taken 172 times.
✗ Branch 1 not taken.
172 if (smk->indexes[i] < 0 ||
320
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 87 times.
172 s->streams[smk->indexes[i]]->discard >= AVDISCARD_ALL) {
321 85 smk->aud_pts[i] += smk->duration_size[i] ? avio_rl32(s->pb)
322
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 : size;
323 85 avio_skip(s->pb, size - smk->duration_size[i]);
324 85 continue;
325 }
326
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
87 if ((ret = av_get_packet(s->pb, pkt, size)) != size) {
327 ret = ret < 0 ? ret : AVERROR_INVALIDDATA;
328 goto next_frame;
329 }
330 87 pkt->stream_index = smk->indexes[i];
331 87 pkt->pts = smk->aud_pts[i];
332 261 pkt->duration = smk->duration_size[i] ? AV_RL32(pkt->data)
333
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 : size;
334 87 smk->aud_pts[i] += pkt->duration;
335 87 smk->next_audio_index = i + 1;
336 87 return 0;
337 }
338 }
339
340
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 101 times.
200 if (s->streams[smk->videoindex]->discard >= AVDISCARD_ALL) {
341 99 ret = FFERROR_REDO;
342 99 goto next_frame;
343 }
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (smk->frame_size >= INT_MAX/2) {
345 ret = AVERROR_INVALIDDATA;
346 goto next_frame;
347 }
348
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 if ((ret = av_new_packet(pkt, smk->frame_size + 769)) < 0)
349 goto next_frame;
350 101 flags = smk->new_palette;
351
3/4
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 99 times.
101 if ((smk->frm_size[smk->cur_frame] & 1) || smk->cur_frame == 0)
352 2 flags |= 2;
353 101 pkt->data[0] = flags;
354 101 memcpy(pkt->data + 1, smk->pal, 768);
355 101 ret = ffio_read_size(s->pb, pkt->data + 769, smk->frame_size);
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ret < 0)
357 goto next_frame;
358 101 pkt->stream_index = smk->videoindex;
359 101 pkt->pts = smk->cur_frame;
360 101 pkt->duration = 1;
361
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 99 times.
101 if (flags & 2)
362 2 pkt->flags |= AV_PKT_FLAG_KEY;
363 101 smk->next_audio_index = 0;
364 101 smk->new_palette = 0;
365 101 smk->cur_frame++;
366
367 101 return 0;
368 99 next_frame:
369 99 avio_seek(s->pb, smk->next_frame_pos, SEEK_SET);
370 99 smk->next_audio_index = 0;
371 99 smk->cur_frame++;
372 99 return ret;
373 }
374
375 static int smacker_read_seek(AVFormatContext *s, int stream_index,
376 int64_t timestamp, int flags)
377 {
378 AVStream *st = s->streams[stream_index];
379 SmackerContext *smk = s->priv_data;
380 int64_t pos;
381 int ret;
382
383 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
384 return -1;
385
386 if (timestamp < 0 || timestamp >= smk->frames)
387 return AVERROR(EINVAL);
388
389 ret = av_index_search_timestamp(st, timestamp, flags);
390 if (ret < 0)
391 return ret;
392
393 pos = ffformatcontext(s)->data_offset;
394 pos += ffstream(st)->index_entries[ret].pos;
395 pos = avio_seek(s->pb, pos, SEEK_SET);
396 if (pos < 0)
397 return pos;
398
399 smk->cur_frame = ret;
400 smk->next_audio_index = 0;
401 smk->new_palette = 0;
402 memset(smk->pal, 0, sizeof(smk->pal));
403 memset(smk->aud_pts, 0, sizeof(smk->aud_pts));
404
405 return 0;
406 }
407
408 const FFInputFormat ff_smacker_demuxer = {
409 .p.name = "smk",
410 .p.long_name = NULL_IF_CONFIG_SMALL("Smacker"),
411 .priv_data_size = sizeof(SmackerContext),
412 .read_probe = smacker_probe,
413 .read_header = smacker_read_header,
414 .read_packet = smacker_read_packet,
415 .read_seek = smacker_read_seek,
416 };
417