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