Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/sga.c
Line
Count
Source
1
/*
2
 * Digital Pictures SGA game demuxer
3
 *
4
 * Copyright (C) 2021 Paul B Mahol
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#include "libavutil/intreadwrite.h"
24
#include "libavutil/avassert.h"
25
#include "libavutil/channel_layout.h"
26
#include "libavutil/internal.h"
27
#include "avformat.h"
28
#include "demux.h"
29
#include "internal.h"
30
#include "avio_internal.h"
31
32
2.71k
#define SEGA_CD_PCM_NUM 12500000
33
2.71k
#define SEGA_CD_PCM_DEN 786432
34
35
typedef struct SGADemuxContext {
36
    int video_stream_index;
37
    int audio_stream_index;
38
39
    uint8_t sector[65536 * 2];
40
    int sector_headers;
41
    int sample_rate;
42
    int first_audio_size;
43
    int payload_size;
44
    int packet_type;
45
    int flags;
46
    int idx;
47
    int left;
48
    int64_t pkt_pos;
49
} SGADemuxContext;
50
51
static int sga_probe(const AVProbeData *p)
52
964k
{
53
964k
    const uint8_t *src = p->buf;
54
964k
    int score = 0, sectors = 1;
55
964k
    int last_left = 0;
56
964k
    int sample_rate = -1;
57
58
964k
    if (p->buf_size < 2048)
59
791k
        return 0;
60
61
332k
    for (int i = 0; i + 2 < p->buf_size; i += 2048) {
62
303k
        int header = AV_RB16(src + i);
63
64
303k
        if ((header > 0x07FE && header < 0x8100) ||
65
172k
            (header > 0x8200 && header < 0xA100) ||
66
166k
            (header > 0xA200 && header < 0xC100)) {
67
144k
            sectors = 0;
68
144k
            break;
69
144k
        }
70
303k
    }
71
72
316k
    for (int i = 0; i + 4 < p->buf_size;) {
73
309k
        int header = AV_RB16(src + i);
74
309k
        int left   = AV_RB16(src + i + 2);
75
309k
        int offset, type, size;
76
77
309k
        if (last_left < 0)
78
0
            return 0;
79
309k
        if (sectors && header && last_left == 0) {
80
26.6k
            if (header >> 12) {
81
16.8k
                last_left = left;
82
16.8k
            } else {
83
9.77k
                last_left = left = header;
84
9.77k
            }
85
283k
        } else if (sectors && header) {
86
5.38k
            left = header;
87
5.38k
            last_left -= left;
88
5.38k
            if (header != 0x7FE && left < 7)
89
106
                return 0;
90
277k
        } else if (sectors) {
91
13.7k
            if (left <= 8)
92
4.47k
                return 0;
93
9.26k
            i += sectors ? 2048 : left + 4;
94
9.26k
            last_left = 0;
95
9.26k
            continue;
96
13.7k
        }
97
98
295k
        if (sectors && (i > 0 && left < 0x7fe) &&
99
2.00k
            (i + left + 14 < p->buf_size)) {
100
1.42k
            offset = i + left + 2;
101
294k
        } else if (sectors && i > 0) {
102
12.1k
            i += 2048;
103
12.1k
            last_left -= FFMIN(last_left, 2046);
104
12.1k
            continue;
105
282k
        } else {
106
282k
            offset = 0;
107
282k
            last_left = left;
108
282k
        }
109
110
283k
        header = AV_RB16(src + offset);
111
283k
        size   = AV_RB16(src + offset + 2) + 4;
112
113
1.01M
        while ((header & 0xFF00) == 0) {
114
730k
            offset++;
115
730k
            if (offset + 4 >= p->buf_size)
116
97
                break;
117
729k
            header = AV_RB16(src + offset);
118
729k
            size   = AV_RB16(src + offset + 2) + 4;
119
729k
        }
120
121
283k
        if (offset + 12 >= p->buf_size)
122
118
            break;
123
283k
        if ((header & 0xFF) > 1)
124
137k
            return 0;
125
146k
        type = header >> 8;
126
127
146k
        if (type == 0xAA ||
128
133k
            type == 0xA1 ||
129
104k
            type == 0xA2 ||
130
82.9k
            type == 0xA3) {
131
82.9k
            int new_rate;
132
133
82.9k
            if (size <= 12)
134
250
                return 0;
135
82.6k
            new_rate = AV_RB16(src + offset + 8);
136
82.6k
            if (sample_rate < 0)
137
1.31k
                sample_rate = new_rate;
138
82.6k
            if (sample_rate == 0 || new_rate != sample_rate)
139
228
                return 0;
140
82.4k
            if (src[offset + 10] != 1)
141
189
                return 0;
142
143
82.2k
            score += 10;
144
82.2k
        } else if (type == 0xC1 ||
145
60.5k
                   type == 0xC6 ||
146
57.0k
                   type == 0xC7 ||
147
51.7k
                   type == 0xC8 ||
148
49.5k
                   type == 0xC9 ||
149
40.8k
                   type == 0xCB ||
150
37.4k
                   type == 0xCD ||
151
42.0k
                   type == 0xE7) {
152
42.0k
            int nb_pals = src[offset + 9];
153
42.0k
            int tiles_w = src[offset + 10];
154
42.0k
            int tiles_h = src[offset + 11];
155
156
42.0k
            if (size <= 12)
157
869
                return 0;
158
41.1k
            if (nb_pals == 0 || nb_pals > 4)
159
588
                return 0;
160
40.5k
            if (tiles_w == 0 || tiles_w > 80)
161
721
                return 0;
162
39.8k
            if (tiles_h == 0 || tiles_h > 60)
163
246
                return 0;
164
165
39.5k
            score += 10;
166
39.5k
        } else if (header == 0x7FE) {
167
0
            ;
168
21.2k
        } else {
169
21.2k
            return 0;
170
21.2k
        }
171
172
121k
        i += sectors ? 2048 : size + 4;
173
121k
        last_left -= FFMIN(last_left, 2046);
174
175
121k
        if (score < 0)
176
0
            break;
177
121k
    }
178
179
7.40k
    return av_clip(score, 0, AVPROBE_SCORE_MAX);
180
173k
}
181
182
static int sga_read_header(AVFormatContext *s)
183
2.18k
{
184
2.18k
    SGADemuxContext *sga = s->priv_data;
185
2.18k
    AVIOContext *pb = s->pb;
186
187
2.18k
    sga->sector_headers = 1;
188
2.18k
    sga->first_audio_size = 0;
189
2.18k
    sga->video_stream_index = -1;
190
2.18k
    sga->audio_stream_index = -1;
191
2.18k
    sga->left = 2048;
192
2.18k
    sga->idx = 0;
193
194
2.18k
    s->ctx_flags |= AVFMTCTX_NOHEADER;
195
196
2.18k
    if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
197
8.57k
        while (!avio_feof(pb)) {
198
8.26k
            int header = avio_rb16(pb);
199
8.26k
            int type = header >> 8;
200
8.26k
            int skip = 2046;
201
8.26k
            int clock;
202
203
8.26k
            if (!sga->first_audio_size &&
204
5.63k
                (type == 0xAA ||
205
5.49k
                 type == 0xA1 ||
206
5.03k
                 type == 0xA2 ||
207
4.32k
                 type == 0xA3)) {
208
1.63k
                sga->first_audio_size = avio_rb16(pb);
209
1.63k
                avio_skip(pb, 4);
210
1.63k
                clock = avio_rb16(pb);
211
1.63k
                sga->sample_rate = av_rescale(clock,
212
1.63k
                                              SEGA_CD_PCM_NUM,
213
1.63k
                                              SEGA_CD_PCM_DEN);
214
1.63k
                skip -= 8;
215
1.63k
            }
216
8.26k
            if ((header > 0x07FE && header < 0x8100) ||
217
8.00k
                (header > 0x8200 && header < 0xA100) ||
218
7.96k
                (header > 0xA200 && header < 0xC100)) {
219
845
                sga->sector_headers = 0;
220
845
                break;
221
845
            }
222
223
7.41k
            avio_skip(pb, skip);
224
7.41k
        }
225
226
1.15k
        avio_seek(pb, 0, SEEK_SET);
227
1.15k
    }
228
229
2.18k
    return 0;
230
2.18k
}
231
232
static void print_stats(AVFormatContext *s, const char *where)
233
2.10M
{
234
2.10M
    SGADemuxContext *sga = s->priv_data;
235
236
2.10M
    av_log(s, AV_LOG_DEBUG, "START %s\n", where);
237
2.10M
    av_log(s, AV_LOG_DEBUG, "pos: %"PRIX64"\n", avio_tell(s->pb));
238
2.10M
    av_log(s, AV_LOG_DEBUG, "idx: %X\n", sga->idx);
239
2.10M
    av_log(s, AV_LOG_DEBUG, "packet_type: %X\n", sga->packet_type);
240
2.10M
    av_log(s, AV_LOG_DEBUG, "payload_size: %X\n", sga->payload_size);
241
2.10M
    av_log(s, AV_LOG_DEBUG, "SECTOR: %016"PRIX64"\n", AV_RB64(sga->sector));
242
2.10M
    av_log(s, AV_LOG_DEBUG, "stream: %X\n", sga->sector[1]);
243
2.10M
    av_log(s, AV_LOG_DEBUG, "END %s\n", where);
244
2.10M
}
245
246
static void update_type_size(AVFormatContext *s)
247
2.08M
{
248
2.08M
    SGADemuxContext *sga = s->priv_data;
249
250
2.08M
    if (sga->idx >= 4) {
251
2.07M
        sga->packet_type  = sga->sector[0];
252
2.07M
        sga->payload_size = AV_RB16(sga->sector + 2);
253
2.07M
    } else {
254
10.5k
        sga->packet_type  = 0;
255
10.5k
        sga->payload_size = 0;
256
10.5k
    }
257
2.08M
}
258
259
static int sga_video_packet(AVFormatContext *s, AVPacket *pkt)
260
579k
{
261
579k
    SGADemuxContext *sga = s->priv_data;
262
579k
    int ret;
263
264
579k
    if (sga->payload_size <= 8)
265
140
        return AVERROR_INVALIDDATA;
266
267
579k
    if (sga->video_stream_index == -1) {
268
905
        AVRational frame_rate;
269
270
905
        AVStream *st = avformat_new_stream(s, NULL);
271
905
        if (!st)
272
0
            return AVERROR(ENOMEM);
273
274
905
        st->start_time              = 0;
275
905
        st->codecpar->codec_type    = AVMEDIA_TYPE_VIDEO;
276
905
        st->codecpar->codec_tag     = 0;
277
905
        st->codecpar->codec_id      = AV_CODEC_ID_SGA_VIDEO;
278
905
        sga->video_stream_index     = st->index;
279
280
905
        if (sga->first_audio_size > 0 && sga->sample_rate > 0) {
281
516
            frame_rate.num = sga->sample_rate;
282
516
            frame_rate.den = sga->first_audio_size;
283
516
        } else {
284
389
            frame_rate.num = 15;
285
389
            frame_rate.den = 1;
286
389
        }
287
905
        avpriv_set_pts_info(st, 64, frame_rate.den, frame_rate.num);
288
905
    }
289
290
579k
    ret = av_new_packet(pkt, sga->payload_size + 4);
291
579k
    if (ret < 0)
292
0
        return AVERROR(ENOMEM);
293
579k
    memcpy(pkt->data, sga->sector, sga->payload_size + 4);
294
579k
    av_assert0(sga->idx >= sga->payload_size + 4);
295
579k
    memmove(sga->sector, sga->sector + sga->payload_size + 4, sga->idx - sga->payload_size - 4);
296
297
579k
    pkt->stream_index = sga->video_stream_index;
298
579k
    pkt->duration = 1;
299
579k
    pkt->pos = sga->pkt_pos;
300
579k
    pkt->flags |= sga->flags;
301
579k
    sga->idx -= sga->payload_size + 4;
302
579k
    sga->flags = 0;
303
579k
    update_type_size(s);
304
305
579k
    av_log(s, AV_LOG_DEBUG, "VIDEO PACKET: %d:%016"PRIX64" i:%X\n", pkt->size, AV_RB64(sga->sector), sga->idx);
306
307
579k
    return 0;
308
579k
}
309
310
static int sga_audio_packet(AVFormatContext *s, AVPacket *pkt)
311
77.9k
{
312
77.9k
    SGADemuxContext *sga = s->priv_data;
313
77.9k
    int ret;
314
315
77.9k
    if (sga->payload_size <= 8)
316
95
        return AVERROR_INVALIDDATA;
317
318
77.8k
    if (sga->audio_stream_index == -1) {
319
1.08k
        AVStream *st = avformat_new_stream(s, NULL);
320
1.08k
        if (!st)
321
0
            return AVERROR(ENOMEM);
322
323
1.08k
        st->start_time              = 0;
324
1.08k
        st->codecpar->codec_type    = AVMEDIA_TYPE_AUDIO;
325
1.08k
        st->codecpar->codec_tag     = 0;
326
1.08k
        st->codecpar->codec_id      = AV_CODEC_ID_PCM_SGA;
327
1.08k
        st->codecpar->ch_layout     = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
328
1.08k
        st->codecpar->sample_rate   = av_rescale(AV_RB16(sga->sector + 8),
329
1.08k
                                                 SEGA_CD_PCM_NUM,
330
1.08k
                                                 SEGA_CD_PCM_DEN);
331
1.08k
        sga->audio_stream_index     = st->index;
332
333
1.08k
        avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
334
1.08k
    }
335
336
77.8k
    ret = av_new_packet(pkt, sga->payload_size - 8);
337
77.8k
    if (ret < 0)
338
0
        return AVERROR(ENOMEM);
339
77.8k
    memcpy(pkt->data, sga->sector + 12, sga->payload_size - 8);
340
77.8k
    av_assert0(sga->idx >= sga->payload_size + 4);
341
77.8k
    memmove(sga->sector, sga->sector + sga->payload_size + 4, sga->idx - sga->payload_size - 4);
342
343
77.8k
    pkt->stream_index = sga->audio_stream_index;
344
77.8k
    pkt->duration = pkt->size;
345
77.8k
    pkt->pos = sga->pkt_pos;
346
77.8k
    pkt->flags |= sga->flags;
347
77.8k
    sga->idx -= sga->payload_size + 4;
348
77.8k
    sga->flags = 0;
349
77.8k
    update_type_size(s);
350
351
77.8k
    av_log(s, AV_LOG_DEBUG, "AUDIO PACKET: %d:%016"PRIX64" i:%X\n", pkt->size, AV_RB64(sga->sector), sga->idx);
352
353
77.8k
    return 0;
354
77.8k
}
355
356
static int sga_packet(AVFormatContext *s, AVPacket *pkt)
357
670k
{
358
670k
    SGADemuxContext *sga = s->priv_data;
359
670k
    int ret = 0;
360
361
670k
    if (sga->packet_type == 0xCD ||
362
670k
        sga->packet_type == 0xCB ||
363
423k
        sga->packet_type == 0xC9 ||
364
422k
        sga->packet_type == 0xC8 ||
365
422k
        sga->packet_type == 0xC7 ||
366
422k
        sga->packet_type == 0xC6 ||
367
416k
        sga->packet_type == 0xC1 ||
368
579k
        sga->packet_type == 0xE7) {
369
579k
        ret = sga_video_packet(s, pkt);
370
579k
    } else if (sga->packet_type == 0xA1 ||
371
51.3k
               sga->packet_type == 0xA2 ||
372
30.0k
               sga->packet_type == 0xA3 ||
373
77.9k
               sga->packet_type == 0xAA) {
374
77.9k
        ret = sga_audio_packet(s, pkt);
375
77.9k
    } else {
376
12.4k
        if (sga->idx == 0)
377
0
            return AVERROR_EOF;
378
12.4k
        if (sga->sector[0])
379
308
            return AVERROR_INVALIDDATA;
380
12.1k
        memmove(sga->sector, sga->sector + 1, sga->idx - 1);
381
12.1k
        sga->idx--;
382
12.1k
        return AVERROR(EAGAIN);
383
12.4k
    }
384
385
657k
    return ret;
386
670k
}
387
388
static int try_packet(AVFormatContext *s, AVPacket *pkt)
389
696k
{
390
696k
    SGADemuxContext *sga = s->priv_data;
391
696k
    int ret = AVERROR(EAGAIN);
392
393
696k
    update_type_size(s);
394
696k
    if (sga->idx >= sga->payload_size + 4) {
395
670k
        print_stats(s, "before sga_packet");
396
670k
        ret = sga_packet(s, pkt);
397
670k
        print_stats(s,  "after sga_packet");
398
670k
        if (ret != AVERROR(EAGAIN))
399
658k
            return ret;
400
670k
    }
401
402
37.9k
    return sga->idx < sga->payload_size + 4 ? AVERROR(EAGAIN) : ret;
403
696k
}
404
405
static int sga_read_packet(AVFormatContext *s, AVPacket *pkt)
406
661k
{
407
661k
    SGADemuxContext *sga = s->priv_data;
408
661k
    AVIOContext *pb = s->pb;
409
661k
    int header, ret = 0;
410
411
661k
    sga->pkt_pos = avio_tell(pb);
412
413
699k
retry:
414
699k
    update_type_size(s);
415
416
699k
    print_stats(s, "start");
417
699k
    if (avio_feof(pb) &&
418
41.5k
        (!sga->payload_size || sga->idx < sga->payload_size + 4))
419
3.51k
        return AVERROR_EOF;
420
421
696k
    if (sga->idx < sga->payload_size + 4) {
422
29.9k
        ret = ffio_ensure_seekback(pb, 2);
423
29.9k
        if (ret < 0)
424
0
            return ret;
425
426
29.9k
        print_stats(s, "before read header");
427
29.9k
        header = avio_rb16(pb);
428
29.9k
        if (!header) {
429
6.74k
            avio_skip(pb, 2046);
430
6.74k
            sga->left = 0;
431
23.2k
        } else if (!avio_feof(pb) &&
432
23.1k
                   ((header >> 15) ||
433
21.8k
                    !sga->sector_headers)) {
434
21.8k
            avio_seek(pb, -2, SEEK_CUR);
435
21.8k
            sga->flags = AV_PKT_FLAG_KEY;
436
21.8k
            sga->left = 2048;
437
21.8k
        } else {
438
1.42k
            sga->left = 2046;
439
1.42k
        }
440
441
29.9k
        av_assert0(sga->idx + sga->left < sizeof(sga->sector));
442
29.9k
        ret = avio_read(pb, sga->sector + sga->idx, sga->left);
443
29.9k
        if (ret > 0)
444
23.0k
            sga->idx += ret;
445
6.92k
        else if (ret != AVERROR_EOF && ret)
446
0
            return ret;
447
29.9k
        print_stats(s, "after read header");
448
449
29.9k
        update_type_size(s);
450
29.9k
    }
451
452
696k
    ret = try_packet(s, pkt);
453
696k
    if (ret == AVERROR(EAGAIN))
454
37.9k
        goto retry;
455
456
658k
    return ret;
457
696k
}
458
459
static int sga_seek(AVFormatContext *s, int stream_index,
460
                     int64_t timestamp, int flags)
461
0
{
462
0
    SGADemuxContext *sga = s->priv_data;
463
464
0
    sga->packet_type = sga->payload_size = sga->idx = 0;
465
0
    memset(sga->sector, 0, sizeof(sga->sector));
466
467
0
    return -1;
468
0
}
469
470
const FFInputFormat ff_sga_demuxer = {
471
    .p.name         = "sga",
472
    .p.long_name    = NULL_IF_CONFIG_SMALL("Digital Pictures SGA"),
473
    .p.extensions   = "sga",
474
    .p.flags        = AVFMT_GENERIC_INDEX,
475
    .priv_data_size = sizeof(SGADemuxContext),
476
    .read_probe     = sga_probe,
477
    .read_header    = sga_read_header,
478
    .read_packet    = sga_read_packet,
479
    .read_seek      = sga_seek,
480
};