FFmpeg coverage


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