Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/scd.c
Line
Count
Source
1
/*
2
 * Square Enix SCD demuxer
3
 * Copyright (C) 2021 Zane van Iperen (zane@zanevaniperen.com)
4
 *
5
 * Based off documentation:
6
 *   http://ffxivexplorer.fragmenterworks.com/research/scd%20files.txt
7
 *
8
 * This file is part of FFmpeg.
9
 *
10
 * FFmpeg is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24
25
#include <stddef.h>
26
27
#include "libavutil/intreadwrite.h"
28
#include "libavutil/internal.h"
29
#include "libavutil/macros.h"
30
#include "libavutil/mem.h"
31
#include "libavformat/internal.h"
32
#include "avformat.h"
33
#include "avio_internal.h"
34
#include "demux.h"
35
36
959k
#define SCD_MAGIC              ((uint64_t)MKBETAG('S', 'E', 'D', 'B') << 32 | \
37
959k
                                          MKBETAG('S', 'S', 'C', 'F'))
38
2.22k
#define SCD_MIN_HEADER_SIZE    20
39
573
#define SCD_OFFSET_HEADER_SIZE 28
40
0
#define SCD_TRACK_HEADER_SIZE  32
41
42
0
#define SCD_TRACK_ID_PCM        0
43
0
#define SCD_TRACK_ID_OGG        6
44
0
#define SCD_TRACK_ID_MP3        7
45
0
#define SCD_TRACK_ID_MS_ADPCM  12
46
47
typedef struct SCDOffsetTable {
48
    uint16_t  count;
49
    uint32_t  offset;
50
    uint32_t *entries;
51
} SCDOffsetTable;
52
53
typedef struct SCDHeader {
54
    uint64_t magic;         /* SEDBSSCF                                     */
55
    uint32_t version;       /* Version number. We only know about 3.        */
56
    uint16_t unk1;          /* Unknown, 260 in Drakengard 3, 1024 in FFXIV. */
57
    uint16_t header_size;   /* Total size of this header.                   */
58
    uint32_t file_size;     /* Is often 0, just ignore it.                  */
59
60
    SCDOffsetTable table0;  /* Table 0, no idea. 56 uint32's/entry.         */
61
    SCDOffsetTable table1;  /* Table 1, contains the track info.            */
62
    SCDOffsetTable table2;  /* Table 2, no idea. 40 uint32's/entry.         */
63
    uint16_t unk2;          /* Unknown, not a count.                        */
64
    uint32_t unk3;          /* Unknown, not an offset.                      */
65
    uint32_t unk4;          /* Unknown, offset to offset.                   */
66
} SCDHeader;
67
68
typedef struct SCDTrackHeader {
69
    uint32_t length;
70
    uint32_t num_channels;
71
    uint32_t sample_rate;
72
    uint32_t data_type;
73
    uint32_t loop_start;
74
    uint32_t loop_end;
75
    uint32_t data_offset; /* Offset to data + this header. */
76
    uint32_t aux_count;
77
78
    uint32_t absolute_offset;
79
    uint32_t bytes_read;
80
} SCDTrackHeader;
81
82
typedef struct SCDDemuxContext {
83
    SCDHeader        hdr;
84
    SCDTrackHeader  *tracks;
85
    int              current_track;
86
} SCDDemuxContext;
87
88
static int scd_probe(const AVProbeData *p)
89
958k
{
90
958k
    if (AV_RB64(p->buf) != SCD_MAGIC)
91
958k
        return 0;
92
93
227
    return AVPROBE_SCORE_MAX;
94
958k
}
95
96
static int scd_read_table(AVFormatContext *s, SCDOffsetTable *table)
97
1.16k
{
98
1.16k
    int64_t ret;
99
100
1.16k
    if ((ret = avio_seek(s->pb, table->offset, SEEK_SET)) < 0)
101
356
        return ret;
102
103
809
    if ((table->entries = av_calloc(table->count, sizeof(uint32_t))) == NULL)
104
0
        return ret;
105
106
809
    if ((ret = avio_read(s->pb, (unsigned char*)table->entries, table->count * sizeof(uint32_t))) < 0)
107
39
        return ret;
108
109
6.42M
    for (size_t i = 0; i < table->count; i++)
110
6.42M
        table->entries[i] = AV_RB32(table->entries + i);
111
112
770
    av_log(s, AV_LOG_TRACE, "Table, size = %u, offset = %u\n", table->count, table->offset);
113
6.42M
    for (size_t i = 0; i < table->count; i++)
114
6.42M
        av_log(s, AV_LOG_TRACE, "  [%02zu]: %u\n", i, table->entries[i]);
115
116
770
    return 0;
117
809
}
118
119
static int scd_read_offsets(AVFormatContext *s)
120
573
{
121
573
    int64_t ret;
122
573
    SCDDemuxContext  *ctx = s->priv_data;
123
573
    uint8_t buf[SCD_OFFSET_HEADER_SIZE];
124
125
573
    if ((ret = avio_read(s->pb, buf, SCD_OFFSET_HEADER_SIZE)) < 0)
126
66
        return ret;
127
128
507
    ctx->hdr.table0.count  = AV_RB16(buf +  0);
129
507
    ctx->hdr.table1.count  = AV_RB16(buf +  2);
130
507
    ctx->hdr.table2.count  = AV_RB16(buf +  4);
131
507
    ctx->hdr.unk2          = AV_RB16(buf +  6);
132
507
    ctx->hdr.table0.offset = AV_RB32(buf +  8);
133
507
    ctx->hdr.table1.offset = AV_RB32(buf + 12);
134
507
    ctx->hdr.table2.offset = AV_RB32(buf + 16);
135
507
    ctx->hdr.unk3          = AV_RB32(buf + 20);
136
507
    ctx->hdr.unk4          = AV_RB32(buf + 24);
137
138
507
    if ((ret = scd_read_table(s, &ctx->hdr.table0)) < 0)
139
136
        return ret;
140
141
371
    if ((ret = scd_read_table(s, &ctx->hdr.table1)) < 0)
142
84
        return ret;
143
144
287
    if ((ret = scd_read_table(s, &ctx->hdr.table2)) < 0)
145
175
        return ret;
146
147
112
    return 0;
148
287
}
149
150
static int scd_read_track(AVFormatContext *s, SCDTrackHeader *track, int index)
151
24
{
152
24
    int64_t ret;
153
24
    uint32_t hoffset;
154
24
    AVStream *st;
155
24
    AVCodecParameters *par;
156
24
    SCDDemuxContext *ctx = s->priv_data;
157
24
    uint8_t buf[SCD_TRACK_HEADER_SIZE];
158
159
    /* Mark as experimental until I find more files from more than just one game. */
160
24
    if (s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
161
24
        av_log(s, AV_LOG_ERROR, "SCD demuxing is experimental, "
162
24
               "add '-strict %d' if you want to use it.\n",
163
24
               FF_COMPLIANCE_EXPERIMENTAL);
164
24
        return AVERROR_EXPERIMENTAL;
165
24
    }
166
167
0
    hoffset = ctx->hdr.table1.entries[index];
168
169
0
    if ((ret = avio_seek(s->pb, hoffset, SEEK_SET)) < 0)
170
0
        return ret;
171
172
0
    if ((ret = avio_read(s->pb, buf, SCD_TRACK_HEADER_SIZE)) < 0)
173
0
        return ret;
174
175
0
    track->length       = AV_RB32(buf +  0);
176
0
    track->num_channels = AV_RB32(buf +  4);
177
0
    track->sample_rate  = AV_RB32(buf +  8);
178
0
    track->data_type    = AV_RB32(buf + 12);
179
0
    track->loop_start   = AV_RB32(buf + 16);
180
0
    track->loop_end     = AV_RB32(buf + 20);
181
0
    track->data_offset  = AV_RB32(buf + 24);
182
0
    track->aux_count    = AV_RB32(buf + 28);
183
184
    /* Sanity checks */
185
0
    if (track->num_channels > 8 || track->sample_rate >= 192000 ||
186
0
        track->loop_start > track->loop_end)
187
0
        return AVERROR_INVALIDDATA;
188
189
0
    track->absolute_offset = hoffset + SCD_TRACK_HEADER_SIZE + track->data_offset;
190
0
    track->bytes_read      = 0;
191
192
    /* Not sure what to do with these, it seems to be fine to ignore them. */
193
0
    if (track->aux_count != 0)
194
0
        av_log(s, AV_LOG_DEBUG, "[%d] Track has %u auxiliary chunk(s).\n", index, track->aux_count);
195
196
0
    if ((st = avformat_new_stream(s, NULL)) == NULL)
197
0
        return AVERROR(ENOMEM);
198
199
0
    par               = st->codecpar;
200
0
    par->codec_type   = AVMEDIA_TYPE_AUDIO;
201
0
    par->ch_layout.nb_channels = (int)track->num_channels;
202
0
    par->sample_rate  = (int)track->sample_rate;
203
0
    st->index         = index;
204
0
    st->start_time    = 0;
205
206
    /* TODO: Check this with other types. Drakengard 3 MP3s have 47999 instead of 48000. */
207
0
    if (track->data_type == SCD_TRACK_ID_MP3)
208
0
        par->sample_rate += 1;
209
210
0
    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
211
212
0
    if (av_dict_set_int(&st->metadata, "start", track->absolute_offset, 0) < 0)
213
0
        return AVERROR(ENOMEM);
214
215
0
    if (av_dict_set_int(&st->metadata, "loop_start", track->loop_start, 0) < 0)
216
0
        return AVERROR(ENOMEM);
217
218
0
    if (av_dict_set_int(&st->metadata, "loop_end", track->loop_end, 0) < 0)
219
0
        return AVERROR(ENOMEM);
220
221
0
    switch(track->data_type) {
222
0
        case SCD_TRACK_ID_PCM:
223
0
            par->codec_id              = AV_CODEC_ID_PCM_S16BE;
224
0
            par->bits_per_coded_sample = 16;
225
0
            par->block_align           = par->bits_per_coded_sample * par->ch_layout.nb_channels / 8;
226
0
            break;
227
0
        case SCD_TRACK_ID_MP3:
228
0
            par->codec_id              = AV_CODEC_ID_MP3;
229
0
            ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
230
0
            break;
231
0
        case SCD_TRACK_ID_OGG:
232
0
        case SCD_TRACK_ID_MS_ADPCM:
233
0
        default:
234
0
            par->codec_id              = AV_CODEC_ID_NONE;
235
0
            avpriv_request_sample(s, "data type %u", track->data_type);
236
0
    }
237
238
0
    return 0;
239
0
}
240
241
static int scd_read_header(AVFormatContext *s)
242
964
{
243
964
    int64_t ret;
244
964
    SCDDemuxContext *ctx = s->priv_data;
245
964
    uint8_t buf[SCD_MIN_HEADER_SIZE];
246
247
964
    if ((ret = ffio_read_size(s->pb, buf, SCD_MIN_HEADER_SIZE)) < 0)
248
134
        return ret;
249
250
830
    ctx->hdr.magic       = AV_RB64(buf +  0);
251
830
    ctx->hdr.version     = AV_RB32(buf +  8);
252
830
    ctx->hdr.unk1        = AV_RB16(buf + 12);
253
830
    ctx->hdr.header_size = AV_RB16(buf + 14);
254
830
    ctx->hdr.file_size   = AV_RB32(buf + 16);
255
256
830
    if (ctx->hdr.magic != SCD_MAGIC)
257
150
        return AVERROR_INVALIDDATA;
258
259
680
    if (ctx->hdr.version != 3) {
260
45
        avpriv_request_sample(s, "SCD version %u", ctx->hdr.version);
261
45
        return AVERROR_PATCHWELCOME;
262
45
    }
263
264
635
    if (ctx->hdr.header_size < SCD_MIN_HEADER_SIZE)
265
6
        return AVERROR_INVALIDDATA;
266
267
629
    if ((ret = avio_skip(s->pb, ctx->hdr.header_size - SCD_MIN_HEADER_SIZE)) < 0)
268
56
        return ret;
269
270
573
    if ((ret = scd_read_offsets(s)) < 0)
271
461
        return ret;
272
273
112
    ctx->tracks = av_calloc(ctx->hdr.table1.count, sizeof(SCDTrackHeader));
274
112
    if (ctx->tracks == NULL)
275
0
        return AVERROR(ENOMEM);
276
277
112
    for (int i = 0; i < ctx->hdr.table1.count; i++) {
278
24
        if ((ret = scd_read_track(s, ctx->tracks + i, i)) < 0)
279
24
            return ret;
280
24
    }
281
282
88
    if (ctx->hdr.table1.count == 0)
283
88
        return 0;
284
285
0
    if ((ret = avio_seek(s->pb, ctx->tracks[0].absolute_offset, SEEK_SET)) < 0)
286
0
        return ret;
287
288
0
    return 0;
289
0
}
290
291
static int scd_read_packet(AVFormatContext *s, AVPacket *pkt)
292
88
{
293
88
    SCDDemuxContext   *ctx = s->priv_data;
294
88
    AVCodecParameters *par;
295
296
    /* Streams aren't interleaved, round-robin them. */
297
88
    for (int i = 0; i < ctx->hdr.table1.count; i++) {
298
0
        int64_t ret;
299
0
        int size;
300
0
        SCDTrackHeader *trk;
301
302
0
        ctx->current_track %= ctx->hdr.table1.count;
303
304
0
        trk = ctx->tracks + ctx->current_track;
305
0
        par = s->streams[ctx->current_track]->codecpar;
306
307
0
        if (trk->bytes_read >= trk->length)
308
0
            continue;
309
310
0
        if ((ret = avio_seek(s->pb, trk->absolute_offset + trk->bytes_read, SEEK_SET)) < 0)
311
0
            return ret;
312
313
0
        switch(trk->data_type) {
314
0
            case SCD_TRACK_ID_PCM:
315
0
                size = par->block_align;
316
0
                break;
317
0
            case SCD_TRACK_ID_MP3:
318
0
            default:
319
0
                size = FFMIN(trk->length - trk->bytes_read, 4096);
320
0
                break;
321
0
        }
322
323
0
        ret = av_get_packet(s->pb, pkt, size);
324
0
        if (ret == AVERROR_EOF) {
325
0
            trk->length = trk->bytes_read;
326
0
            continue;
327
0
        } else if (ret < 0) {
328
0
            return ret;
329
0
        }
330
331
0
        if (trk->data_type == SCD_TRACK_ID_PCM) {
332
0
            pkt->pts      = trk->bytes_read / (par->ch_layout.nb_channels * sizeof(uint16_t));
333
0
            pkt->duration = size / (par->ch_layout.nb_channels * sizeof(int16_t));
334
0
        }
335
336
0
        trk->bytes_read   += ret;
337
0
        pkt->flags        &= ~AV_PKT_FLAG_CORRUPT;
338
0
        pkt->stream_index  = ctx->current_track;
339
340
0
        ctx->current_track++;
341
0
        return 0;
342
0
    }
343
344
88
    return AVERROR_EOF;
345
88
}
346
347
static int scd_seek(AVFormatContext *s, int stream_index,
348
                    int64_t pts, int flags)
349
0
{
350
0
    SCDDemuxContext *ctx = s->priv_data;
351
352
0
    if (pts != 0)
353
0
        return AVERROR(EINVAL);
354
355
0
    for(int i = 0; i < ctx->hdr.table1.count; ++i)
356
0
        ctx->tracks[i].bytes_read = 0;
357
358
0
    return 0;
359
0
}
360
361
static int scd_read_close(AVFormatContext *s)
362
964
{
363
964
    SCDDemuxContext *ctx = s->priv_data;
364
365
964
    av_freep(&ctx->hdr.table0.entries);
366
964
    av_freep(&ctx->hdr.table1.entries);
367
964
    av_freep(&ctx->hdr.table2.entries);
368
964
    av_freep(&ctx->tracks);
369
964
    return 0;
370
964
}
371
372
const FFInputFormat ff_scd_demuxer = {
373
    .p.name         = "scd",
374
    .p.long_name    = NULL_IF_CONFIG_SMALL("Square Enix SCD"),
375
    .priv_data_size = sizeof(SCDDemuxContext),
376
    .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
377
    .read_probe     = scd_probe,
378
    .read_header    = scd_read_header,
379
    .read_packet    = scd_read_packet,
380
    .read_seek      = scd_seek,
381
    .read_close     = scd_read_close,
382
};