Coverage Report

Created: 2026-02-14 06:59

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.24k
#define SEGA_CD_PCM_NUM 12500000
33
2.24k
#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
936k
{
53
936k
    const uint8_t *src = p->buf;
54
936k
    int score = 0, sectors = 1;
55
936k
    int last_left = 0;
56
936k
    int sample_rate = -1;
57
58
936k
    if (p->buf_size < 2048)
59
758k
        return 0;
60
61
339k
    for (int i = 0; i + 2 < p->buf_size; i += 2048) {
62
309k
        int header = AV_RB16(src + i);
63
64
309k
        if ((header > 0x07FE && header < 0x8100) ||
65
174k
            (header > 0x8200 && header < 0xA100) ||
66
167k
            (header > 0xA200 && header < 0xC100)) {
67
148k
            sectors = 0;
68
148k
            break;
69
148k
        }
70
309k
    }
71
72
321k
    for (int i = 0; i + 4 < p->buf_size;) {
73
314k
        int header = AV_RB16(src + i);
74
314k
        int left   = AV_RB16(src + i + 2);
75
314k
        int offset, type, size;
76
77
314k
        if (last_left < 0)
78
0
            return 0;
79
314k
        if (sectors && header && last_left == 0) {
80
27.1k
            if (header >> 12) {
81
17.5k
                last_left = left;
82
17.5k
            } else {
83
9.53k
                last_left = left = header;
84
9.53k
            }
85
287k
        } else if (sectors && header) {
86
5.71k
            left = header;
87
5.71k
            last_left -= left;
88
5.71k
            if (header != 0x7FE && left < 7)
89
111
                return 0;
90
281k
        } else if (sectors) {
91
13.7k
            if (left <= 8)
92
4.50k
                return 0;
93
9.27k
            i += sectors ? 2048 : left + 4;
94
9.27k
            last_left = 0;
95
9.27k
            continue;
96
13.7k
        }
97
98
300k
        if (sectors && (i > 0 && left < 0x7fe) &&
99
2.04k
            (i + left + 14 < p->buf_size)) {
100
1.44k
            offset = i + left + 2;
101
299k
        } else if (sectors && i > 0) {
102
12.7k
            i += 2048;
103
12.7k
            last_left -= FFMIN(last_left, 2046);
104
12.7k
            continue;
105
286k
        } else {
106
286k
            offset = 0;
107
286k
            last_left = left;
108
286k
        }
109
110
287k
        header = AV_RB16(src + offset);
111
287k
        size   = AV_RB16(src + offset + 2) + 4;
112
113
1.04M
        while ((header & 0xFF00) == 0) {
114
756k
            offset++;
115
756k
            if (offset + 4 >= p->buf_size)
116
99
                break;
117
755k
            header = AV_RB16(src + offset);
118
755k
            size   = AV_RB16(src + offset + 2) + 4;
119
755k
        }
120
121
287k
        if (offset + 12 >= p->buf_size)
122
120
            break;
123
287k
        if ((header & 0xFF) > 1)
124
142k
            return 0;
125
145k
        type = header >> 8;
126
127
145k
        if (type == 0xAA ||
128
132k
            type == 0xA1 ||
129
102k
            type == 0xA2 ||
130
80.4k
            type == 0xA3) {
131
80.4k
            int new_rate;
132
133
80.4k
            if (size <= 12)
134
233
                return 0;
135
80.1k
            new_rate = AV_RB16(src + offset + 8);
136
80.1k
            if (sample_rate < 0)
137
1.37k
                sample_rate = new_rate;
138
80.1k
            if (sample_rate == 0 || new_rate != sample_rate)
139
235
                return 0;
140
79.9k
            if (src[offset + 10] != 1)
141
207
                return 0;
142
143
79.7k
            score += 10;
144
79.7k
        } else if (type == 0xC1 ||
145
61.5k
                   type == 0xC6 ||
146
58.0k
                   type == 0xC7 ||
147
54.3k
                   type == 0xC8 ||
148
52.1k
                   type == 0xC9 ||
149
43.2k
                   type == 0xCB ||
150
39.2k
                   type == 0xCD ||
151
44.1k
                   type == 0xE7) {
152
44.1k
            int nb_pals = src[offset + 9];
153
44.1k
            int tiles_w = src[offset + 10];
154
44.1k
            int tiles_h = src[offset + 11];
155
156
44.1k
            if (size <= 12)
157
883
                return 0;
158
43.3k
            if (nb_pals == 0 || nb_pals > 4)
159
622
                return 0;
160
42.6k
            if (tiles_w == 0 || tiles_w > 80)
161
728
                return 0;
162
41.9k
            if (tiles_h == 0 || tiles_h > 60)
163
235
                return 0;
164
165
41.7k
            score += 10;
166
41.7k
        } else if (header == 0x7FE) {
167
0
            ;
168
20.8k
        } else {
169
20.8k
            return 0;
170
20.8k
        }
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
178k
}
181
182
static int sga_read_header(AVFormatContext *s)
183
2.24k
{
184
2.24k
    SGADemuxContext *sga = s->priv_data;
185
2.24k
    AVIOContext *pb = s->pb;
186
187
2.24k
    sga->sector_headers = 1;
188
2.24k
    sga->first_audio_size = 0;
189
2.24k
    sga->video_stream_index = -1;
190
2.24k
    sga->audio_stream_index = -1;
191
2.24k
    sga->left = 2048;
192
2.24k
    sga->idx = 0;
193
194
2.24k
    s->ctx_flags |= AVFMTCTX_NOHEADER;
195
196
2.24k
    if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
197
7.86k
        while (!avio_feof(pb)) {
198
7.54k
            int header = avio_rb16(pb);
199
7.54k
            int type = header >> 8;
200
7.54k
            int skip = 2046;
201
7.54k
            int clock;
202
203
7.54k
            if (!sga->first_audio_size &&
204
5.19k
                (type == 0xAA ||
205
5.04k
                 type == 0xA1 ||
206
4.66k
                 type == 0xA2 ||
207
4.38k
                 type == 0xA3)) {
208
1.11k
                sga->first_audio_size = avio_rb16(pb);
209
1.11k
                avio_skip(pb, 4);
210
1.11k
                clock = avio_rb16(pb);
211
1.11k
                sga->sample_rate = av_rescale(clock,
212
1.11k
                                              SEGA_CD_PCM_NUM,
213
1.11k
                                              SEGA_CD_PCM_DEN);
214
1.11k
                skip -= 8;
215
1.11k
            }
216
7.54k
            if ((header > 0x07FE && header < 0x8100) ||
217
7.24k
                (header > 0x8200 && header < 0xA100) ||
218
7.21k
                (header > 0xA200 && header < 0xC100)) {
219
873
                sga->sector_headers = 0;
220
873
                break;
221
873
            }
222
223
6.66k
            avio_skip(pb, skip);
224
6.66k
        }
225
226
1.19k
        avio_seek(pb, 0, SEEK_SET);
227
1.19k
    }
228
229
2.24k
    return 0;
230
2.24k
}
231
232
static void print_stats(AVFormatContext *s, const char *where)
233
2.29M
{
234
2.29M
    SGADemuxContext *sga = s->priv_data;
235
236
2.29M
    av_log(s, AV_LOG_DEBUG, "START %s\n", where);
237
2.29M
    av_log(s, AV_LOG_DEBUG, "pos: %"PRIX64"\n", avio_tell(s->pb));
238
2.29M
    av_log(s, AV_LOG_DEBUG, "idx: %X\n", sga->idx);
239
2.29M
    av_log(s, AV_LOG_DEBUG, "packet_type: %X\n", sga->packet_type);
240
2.29M
    av_log(s, AV_LOG_DEBUG, "payload_size: %X\n", sga->payload_size);
241
2.29M
    av_log(s, AV_LOG_DEBUG, "SECTOR: %016"PRIX64"\n", AV_RB64(sga->sector));
242
2.29M
    av_log(s, AV_LOG_DEBUG, "stream: %X\n", sga->sector[1]);
243
2.29M
    av_log(s, AV_LOG_DEBUG, "END %s\n", where);
244
2.29M
}
245
246
static void update_type_size(AVFormatContext *s)
247
2.27M
{
248
2.27M
    SGADemuxContext *sga = s->priv_data;
249
250
2.27M
    if (sga->idx >= 4) {
251
2.26M
        sga->packet_type  = sga->sector[0];
252
2.26M
        sga->payload_size = AV_RB16(sga->sector + 2);
253
2.26M
    } else {
254
13.2k
        sga->packet_type  = 0;
255
13.2k
        sga->payload_size = 0;
256
13.2k
    }
257
2.27M
}
258
259
static int sga_video_packet(AVFormatContext *s, AVPacket *pkt)
260
636k
{
261
636k
    SGADemuxContext *sga = s->priv_data;
262
636k
    int ret;
263
264
636k
    if (sga->payload_size <= 8)
265
131
        return AVERROR_INVALIDDATA;
266
267
636k
    if (sga->video_stream_index == -1) {
268
922
        AVRational frame_rate;
269
270
922
        AVStream *st = avformat_new_stream(s, NULL);
271
922
        if (!st)
272
0
            return AVERROR(ENOMEM);
273
274
922
        st->start_time              = 0;
275
922
        st->codecpar->codec_type    = AVMEDIA_TYPE_VIDEO;
276
922
        st->codecpar->codec_tag     = 0;
277
922
        st->codecpar->codec_id      = AV_CODEC_ID_SGA_VIDEO;
278
922
        sga->video_stream_index     = st->index;
279
280
922
        if (sga->first_audio_size > 0 && sga->sample_rate > 0) {
281
536
            frame_rate.num = sga->sample_rate;
282
536
            frame_rate.den = sga->first_audio_size;
283
536
        } else {
284
386
            frame_rate.num = 15;
285
386
            frame_rate.den = 1;
286
386
        }
287
922
        avpriv_set_pts_info(st, 64, frame_rate.den, frame_rate.num);
288
922
    }
289
290
636k
    ret = av_new_packet(pkt, sga->payload_size + 4);
291
636k
    if (ret < 0)
292
0
        return AVERROR(ENOMEM);
293
636k
    memcpy(pkt->data, sga->sector, sga->payload_size + 4);
294
636k
    av_assert0(sga->idx >= sga->payload_size + 4);
295
636k
    memmove(sga->sector, sga->sector + sga->payload_size + 4, sga->idx - sga->payload_size - 4);
296
297
636k
    pkt->stream_index = sga->video_stream_index;
298
636k
    pkt->duration = 1;
299
636k
    pkt->pos = sga->pkt_pos;
300
636k
    pkt->flags |= sga->flags;
301
636k
    sga->idx -= sga->payload_size + 4;
302
636k
    sga->flags = 0;
303
636k
    update_type_size(s);
304
305
636k
    av_log(s, AV_LOG_DEBUG, "VIDEO PACKET: %d:%016"PRIX64" i:%X\n", pkt->size, AV_RB64(sga->sector), sga->idx);
306
307
636k
    return 0;
308
636k
}
309
310
static int sga_audio_packet(AVFormatContext *s, AVPacket *pkt)
311
89.8k
{
312
89.8k
    SGADemuxContext *sga = s->priv_data;
313
89.8k
    int ret;
314
315
89.8k
    if (sga->payload_size <= 8)
316
99
        return AVERROR_INVALIDDATA;
317
318
89.7k
    if (sga->audio_stream_index == -1) {
319
1.12k
        AVStream *st = avformat_new_stream(s, NULL);
320
1.12k
        if (!st)
321
0
            return AVERROR(ENOMEM);
322
323
1.12k
        st->start_time              = 0;
324
1.12k
        st->codecpar->codec_type    = AVMEDIA_TYPE_AUDIO;
325
1.12k
        st->codecpar->codec_tag     = 0;
326
1.12k
        st->codecpar->codec_id      = AV_CODEC_ID_PCM_SGA;
327
1.12k
        st->codecpar->ch_layout     = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
328
1.12k
        st->codecpar->sample_rate   = av_rescale(AV_RB16(sga->sector + 8),
329
1.12k
                                                 SEGA_CD_PCM_NUM,
330
1.12k
                                                 SEGA_CD_PCM_DEN);
331
1.12k
        sga->audio_stream_index     = st->index;
332
333
1.12k
        avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
334
1.12k
    }
335
336
89.7k
    ret = av_new_packet(pkt, sga->payload_size - 8);
337
89.7k
    if (ret < 0)
338
0
        return AVERROR(ENOMEM);
339
89.7k
    memcpy(pkt->data, sga->sector + 12, sga->payload_size - 8);
340
89.7k
    av_assert0(sga->idx >= sga->payload_size + 4);
341
89.7k
    memmove(sga->sector, sga->sector + sga->payload_size + 4, sga->idx - sga->payload_size - 4);
342
343
89.7k
    pkt->stream_index = sga->audio_stream_index;
344
89.7k
    pkt->duration = pkt->size;
345
89.7k
    pkt->pos = sga->pkt_pos;
346
89.7k
    pkt->flags |= sga->flags;
347
89.7k
    sga->idx -= sga->payload_size + 4;
348
89.7k
    sga->flags = 0;
349
89.7k
    update_type_size(s);
350
351
89.7k
    av_log(s, AV_LOG_DEBUG, "AUDIO PACKET: %d:%016"PRIX64" i:%X\n", pkt->size, AV_RB64(sga->sector), sga->idx);
352
353
89.7k
    return 0;
354
89.7k
}
355
356
static int sga_packet(AVFormatContext *s, AVPacket *pkt)
357
736k
{
358
736k
    SGADemuxContext *sga = s->priv_data;
359
736k
    int ret = 0;
360
361
736k
    if (sga->packet_type == 0xCD ||
362
735k
        sga->packet_type == 0xCB ||
363
462k
        sga->packet_type == 0xC9 ||
364
462k
        sga->packet_type == 0xC8 ||
365
461k
        sga->packet_type == 0xC7 ||
366
461k
        sga->packet_type == 0xC6 ||
367
454k
        sga->packet_type == 0xC1 ||
368
636k
        sga->packet_type == 0xE7) {
369
636k
        ret = sga_video_packet(s, pkt);
370
636k
    } else if (sga->packet_type == 0xA1 ||
371
54.0k
               sga->packet_type == 0xA2 ||
372
30.6k
               sga->packet_type == 0xA3 ||
373
89.8k
               sga->packet_type == 0xAA) {
374
89.8k
        ret = sga_audio_packet(s, pkt);
375
89.8k
    } else {
376
9.75k
        if (sga->idx == 0)
377
0
            return AVERROR_EOF;
378
9.75k
        if (sga->sector[0])
379
324
            return AVERROR_INVALIDDATA;
380
9.42k
        memmove(sga->sector, sga->sector + 1, sga->idx - 1);
381
9.42k
        sga->idx--;
382
9.42k
        return AVERROR(EAGAIN);
383
9.75k
    }
384
385
726k
    return ret;
386
736k
}
387
388
static int try_packet(AVFormatContext *s, AVPacket *pkt)
389
759k
{
390
759k
    SGADemuxContext *sga = s->priv_data;
391
759k
    int ret = AVERROR(EAGAIN);
392
393
759k
    update_type_size(s);
394
759k
    if (sga->idx >= sga->payload_size + 4) {
395
736k
        print_stats(s, "before sga_packet");
396
736k
        ret = sga_packet(s, pkt);
397
736k
        print_stats(s,  "after sga_packet");
398
736k
        if (ret != AVERROR(EAGAIN))
399
726k
            return ret;
400
736k
    }
401
402
32.9k
    return sga->idx < sga->payload_size + 4 ? AVERROR(EAGAIN) : ret;
403
759k
}
404
405
static int sga_read_packet(AVFormatContext *s, AVPacket *pkt)
406
730k
{
407
730k
    SGADemuxContext *sga = s->priv_data;
408
730k
    AVIOContext *pb = s->pb;
409
730k
    int header, ret = 0;
410
411
730k
    sga->pkt_pos = avio_tell(pb);
412
413
763k
retry:
414
763k
    update_type_size(s);
415
416
763k
    print_stats(s, "start");
417
763k
    if (avio_feof(pb) &&
418
43.1k
        (!sga->payload_size || sga->idx < sga->payload_size + 4))
419
3.59k
        return AVERROR_EOF;
420
421
759k
    if (sga->idx < sga->payload_size + 4) {
422
27.8k
        ret = ffio_ensure_seekback(pb, 2);
423
27.8k
        if (ret < 0)
424
0
            return ret;
425
426
27.8k
        print_stats(s, "before read header");
427
27.8k
        header = avio_rb16(pb);
428
27.8k
        if (!header) {
429
6.85k
            avio_skip(pb, 2046);
430
6.85k
            sga->left = 0;
431
20.9k
        } else if (!avio_feof(pb) &&
432
20.8k
                   ((header >> 15) ||
433
19.3k
                    !sga->sector_headers)) {
434
19.3k
            avio_seek(pb, -2, SEEK_CUR);
435
19.3k
            sga->flags = AV_PKT_FLAG_KEY;
436
19.3k
            sga->left = 2048;
437
19.3k
        } else {
438
1.61k
            sga->left = 2046;
439
1.61k
        }
440
441
27.8k
        av_assert0(sga->idx + sga->left < sizeof(sga->sector));
442
27.8k
        ret = avio_read(pb, sga->sector + sga->idx, sga->left);
443
27.8k
        if (ret > 0)
444
20.7k
            sga->idx += ret;
445
7.03k
        else if (ret != AVERROR_EOF && ret)
446
0
            return ret;
447
27.8k
        print_stats(s, "after read header");
448
449
27.8k
        update_type_size(s);
450
27.8k
    }
451
452
759k
    ret = try_packet(s, pkt);
453
759k
    if (ret == AVERROR(EAGAIN))
454
32.9k
        goto retry;
455
456
726k
    return ret;
457
759k
}
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
};