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