FFmpeg
Loading...
Searching...
No Matches
scd.c
Go to the documentation of this file.
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
28#include "libavutil/internal.h"
29#include "libavutil/macros.h"
30#include "libavutil/mem.h"
32#include "avformat.h"
33#include "avio_internal.h"
34#include "demux.h"
35
36#define SCD_MAGIC ((uint64_t)MKBETAG('S', 'E', 'D', 'B') << 32 | \
37 MKBETAG('S', 'S', 'C', 'F'))
38#define SCD_MIN_HEADER_SIZE 20
39#define SCD_OFFSET_HEADER_SIZE 28
40#define SCD_TRACK_HEADER_SIZE 32
41
42#define SCD_TRACK_ID_PCM 0
43#define SCD_TRACK_ID_OGG 6
44#define SCD_TRACK_ID_MP3 7
45#define SCD_TRACK_ID_MS_ADPCM 12
46
47typedef struct SCDOffsetTable {
48 uint16_t count;
49 uint32_t offset;
50 uint32_t *entries;
52
53typedef 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
68typedef 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
79 uint32_t bytes_read;
81
87
88static int scd_probe(const AVProbeData *p)
89{
90 if (AV_RB64(p->buf) != SCD_MAGIC)
91 return 0;
92
93 return AVPROBE_SCORE_MAX;
94}
95
97{
98 int64_t ret;
99
100 if ((ret = avio_seek(s->pb, table->offset, SEEK_SET)) < 0)
101 return ret;
102
103 if ((table->entries = av_calloc(table->count, sizeof(uint32_t))) == NULL)
104 return ret;
105
106 if ((ret = avio_read(s->pb, (unsigned char*)table->entries, table->count * sizeof(uint32_t))) < 0)
107 return ret;
108
109 for (size_t i = 0; i < table->count; i++)
110 table->entries[i] = AV_RB32(table->entries + i);
111
112 av_log(s, AV_LOG_TRACE, "Table, size = %u, offset = %u\n", table->count, table->offset);
113 for (size_t i = 0; i < table->count; i++)
114 av_log(s, AV_LOG_TRACE, " [%02zu]: %u\n", i, table->entries[i]);
115
116 return 0;
117}
118
120{
121 int64_t ret;
122 SCDDemuxContext *ctx = s->priv_data;
123 uint8_t buf[SCD_OFFSET_HEADER_SIZE];
124
125 if ((ret = ffio_read_size(s->pb, buf, SCD_OFFSET_HEADER_SIZE)) < 0)
126 return ret;
127
128 ctx->hdr.table0.count = AV_RB16(buf + 0);
129 ctx->hdr.table1.count = AV_RB16(buf + 2);
130 ctx->hdr.table2.count = AV_RB16(buf + 4);
131 ctx->hdr.unk2 = AV_RB16(buf + 6);
132 ctx->hdr.table0.offset = AV_RB32(buf + 8);
133 ctx->hdr.table1.offset = AV_RB32(buf + 12);
134 ctx->hdr.table2.offset = AV_RB32(buf + 16);
135 ctx->hdr.unk3 = AV_RB32(buf + 20);
136 ctx->hdr.unk4 = AV_RB32(buf + 24);
137
138 if ((ret = scd_read_table(s, &ctx->hdr.table0)) < 0)
139 return ret;
140
141 if ((ret = scd_read_table(s, &ctx->hdr.table1)) < 0)
142 return ret;
143
144 if ((ret = scd_read_table(s, &ctx->hdr.table2)) < 0)
145 return ret;
146
147 return 0;
148}
149
151{
152 int64_t ret;
153 uint32_t hoffset;
154 AVStream *st;
156 SCDDemuxContext *ctx = s->priv_data;
157 uint8_t buf[SCD_TRACK_HEADER_SIZE];
158
159 /* Mark as experimental until I find more files from more than just one game. */
160 if (s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
161 av_log(s, AV_LOG_ERROR, "SCD demuxing is experimental, "
162 "add '-strict %d' if you want to use it.\n",
165 }
166
167 hoffset = ctx->hdr.table1.entries[index];
168
169 if ((ret = avio_seek(s->pb, hoffset, SEEK_SET)) < 0)
170 return ret;
171
172 if ((ret = avio_read(s->pb, buf, SCD_TRACK_HEADER_SIZE)) < 0)
173 return ret;
174
175 track->length = AV_RB32(buf + 0);
176 track->num_channels = AV_RB32(buf + 4);
177 track->sample_rate = AV_RB32(buf + 8);
178 track->data_type = AV_RB32(buf + 12);
179 track->loop_start = AV_RB32(buf + 16);
180 track->loop_end = AV_RB32(buf + 20);
181 track->data_offset = AV_RB32(buf + 24);
182 track->aux_count = AV_RB32(buf + 28);
183
184 /* Sanity checks */
185 if (!track->num_channels || track->num_channels > 8 ||
186 track->sample_rate >= 192000 ||
187 track->loop_start > track->loop_end)
188 return AVERROR_INVALIDDATA;
189
190 track->absolute_offset = hoffset + SCD_TRACK_HEADER_SIZE + track->data_offset;
191 track->bytes_read = 0;
192
193 /* Not sure what to do with these, it seems to be fine to ignore them. */
194 if (track->aux_count != 0)
195 av_log(s, AV_LOG_DEBUG, "[%d] Track has %u auxiliary chunk(s).\n", index, track->aux_count);
196
197 if ((st = avformat_new_stream(s, NULL)) == NULL)
198 return AVERROR(ENOMEM);
199
200 par = st->codecpar;
202 par->ch_layout.nb_channels = (int)track->num_channels;
203 par->sample_rate = (int)track->sample_rate;
204 st->index = index;
205 st->start_time = 0;
206
207 /* TODO: Check this with other types. Drakengard 3 MP3s have 47999 instead of 48000. */
208 if (track->data_type == SCD_TRACK_ID_MP3)
209 par->sample_rate += 1;
210
211 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
212
213 if (av_dict_set_int(&st->metadata, "start", track->absolute_offset, 0) < 0)
214 return AVERROR(ENOMEM);
215
216 if (av_dict_set_int(&st->metadata, "loop_start", track->loop_start, 0) < 0)
217 return AVERROR(ENOMEM);
218
219 if (av_dict_set_int(&st->metadata, "loop_end", track->loop_end, 0) < 0)
220 return AVERROR(ENOMEM);
221
222 switch(track->data_type) {
223 case SCD_TRACK_ID_PCM:
225 par->bits_per_coded_sample = 16;
227 break;
228 case SCD_TRACK_ID_MP3:
231 break;
232 case SCD_TRACK_ID_OGG:
234 default:
236 avpriv_request_sample(s, "data type %u", track->data_type);
237 }
238
239 return 0;
240}
241
243{
244 int64_t ret;
245 SCDDemuxContext *ctx = s->priv_data;
246 uint8_t buf[SCD_MIN_HEADER_SIZE];
247
248 if ((ret = ffio_read_size(s->pb, buf, SCD_MIN_HEADER_SIZE)) < 0)
249 return ret;
250
251 ctx->hdr.magic = AV_RB64(buf + 0);
252 ctx->hdr.version = AV_RB32(buf + 8);
253 ctx->hdr.unk1 = AV_RB16(buf + 12);
254 ctx->hdr.header_size = AV_RB16(buf + 14);
255 ctx->hdr.file_size = AV_RB32(buf + 16);
256
257 if (ctx->hdr.magic != SCD_MAGIC)
258 return AVERROR_INVALIDDATA;
259
260 if (ctx->hdr.version != 3) {
261 avpriv_request_sample(s, "SCD version %u", ctx->hdr.version);
263 }
264
265 if (ctx->hdr.header_size < SCD_MIN_HEADER_SIZE)
266 return AVERROR_INVALIDDATA;
267
268 if ((ret = avio_skip(s->pb, ctx->hdr.header_size - SCD_MIN_HEADER_SIZE)) < 0)
269 return ret;
270
271 if ((ret = scd_read_offsets(s)) < 0)
272 return ret;
273
274 ctx->tracks = av_calloc(ctx->hdr.table1.count, sizeof(SCDTrackHeader));
275 if (ctx->tracks == NULL)
276 return AVERROR(ENOMEM);
277
278 for (int i = 0; i < ctx->hdr.table1.count; i++) {
279 if ((ret = scd_read_track(s, ctx->tracks + i, i)) < 0)
280 return ret;
281 }
282
283 if (ctx->hdr.table1.count == 0)
284 return 0;
285
286 if ((ret = avio_seek(s->pb, ctx->tracks[0].absolute_offset, SEEK_SET)) < 0)
287 return ret;
288
289 return 0;
290}
291
293{
294 SCDDemuxContext *ctx = s->priv_data;
296
297 /* Streams aren't interleaved, round-robin them. */
298 for (int i = 0; i < ctx->hdr.table1.count; i++) {
299 int64_t ret;
300 int size;
301 SCDTrackHeader *trk;
302
303 ctx->current_track %= ctx->hdr.table1.count;
304
305 trk = ctx->tracks + ctx->current_track;
306 par = s->streams[ctx->current_track]->codecpar;
307
308 if (trk->bytes_read >= trk->length)
309 continue;
310
311 if ((ret = avio_seek(s->pb, trk->absolute_offset + trk->bytes_read, SEEK_SET)) < 0)
312 return ret;
313
314 switch(trk->data_type) {
315 case SCD_TRACK_ID_PCM:
316 size = par->block_align;
317 break;
318 case SCD_TRACK_ID_MP3:
319 default:
320 size = FFMIN(trk->length - trk->bytes_read, 4096);
321 break;
322 }
323
324 ret = av_get_packet(s->pb, pkt, size);
325 if (ret == AVERROR_EOF) {
326 trk->length = trk->bytes_read;
327 continue;
328 } else if (ret < 0) {
329 return ret;
330 }
331
332 if (trk->data_type == SCD_TRACK_ID_PCM) {
333 pkt->pts = trk->bytes_read / (par->ch_layout.nb_channels * sizeof(uint16_t));
334 pkt->duration = size / (par->ch_layout.nb_channels * sizeof(int16_t));
335 }
336
337 trk->bytes_read += ret;
338 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
339 pkt->stream_index = ctx->current_track;
340
341 ctx->current_track++;
342 return 0;
343 }
344
345 return AVERROR_EOF;
346}
347
348static int scd_seek(AVFormatContext *s, int stream_index,
349 int64_t pts, int flags)
350{
351 SCDDemuxContext *ctx = s->priv_data;
352
353 if (pts != 0)
354 return AVERROR(EINVAL);
355
356 for(int i = 0; i < ctx->hdr.table1.count; ++i)
357 ctx->tracks[i].bytes_read = 0;
358
359 return 0;
360}
361
363{
364 SCDDemuxContext *ctx = s->priv_data;
365
366 av_freep(&ctx->hdr.table0.entries);
367 av_freep(&ctx->hdr.table1.entries);
368 av_freep(&ctx->hdr.table2.entries);
369 av_freep(&ctx->tracks);
370 return 0;
371}
372
374 .p.name = "scd",
375 .p.long_name = NULL_IF_CONFIG_SMALL("Square Enix SCD"),
376 .priv_data_size = sizeof(SCDDemuxContext),
377 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
383};
const FFInputFormat ff_scd_demuxer
Definition scd.c:373
static AVFormatContext * ctx
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition avformat.h:615
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition defs.h:62
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:454
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:177
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it.
Definition error.h:74
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
int index
Definition gxfenc.c:90
#define AV_RB32(p)
#define AV_RB64(p)
#define AV_RB16(p)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
Utility Preprocessor macros.
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
static const uint16_t table[]
Definition prosumer.c:203
#define SCD_MAGIC
Definition scd.c:36
#define SCD_TRACK_HEADER_SIZE
Definition scd.c:40
#define SCD_TRACK_ID_MP3
Definition scd.c:44
#define SCD_TRACK_ID_PCM
Definition scd.c:42
#define SCD_TRACK_ID_MS_ADPCM
Definition scd.c:45
static int scd_read_header(AVFormatContext *s)
Definition scd.c:242
static int scd_read_table(AVFormatContext *s, SCDOffsetTable *table)
Definition scd.c:96
#define SCD_TRACK_ID_OGG
Definition scd.c:43
static int scd_read_close(AVFormatContext *s)
Definition scd.c:362
#define SCD_OFFSET_HEADER_SIZE
Definition scd.c:39
static int scd_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition scd.c:348
static int scd_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition scd.c:292
#define SCD_MIN_HEADER_SIZE
Definition scd.c:38
static int scd_read_offsets(AVFormatContext *s)
Definition scd.c:119
static int scd_read_track(AVFormatContext *s, SCDTrackHeader *track, int index)
Definition scd.c:150
static int scd_probe(const AVProbeData *p)
Definition scd.c:88
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVDictionary * metadata
Definition avformat.h:846
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
enum AVStreamParseType need_parsing
Definition internal.h:321
SCDHeader hdr
Definition scd.c:83
int current_track
Definition scd.c:85
SCDTrackHeader * tracks
Definition scd.c:84
uint32_t unk4
Definition scd.c:65
uint32_t unk3
Definition scd.c:64
SCDOffsetTable table0
Definition scd.c:60
uint64_t magic
Definition scd.c:54
uint16_t header_size
Definition scd.c:57
uint32_t file_size
Definition scd.c:58
SCDOffsetTable table1
Definition scd.c:61
uint32_t version
Definition scd.c:55
SCDOffsetTable table2
Definition scd.c:62
uint16_t unk2
Definition scd.c:63
uint16_t unk1
Definition scd.c:56
uint16_t count
Definition scd.c:48
uint32_t offset
Definition scd.c:49
uint32_t * entries
Definition scd.c:50
uint32_t bytes_read
Definition scd.c:79
uint32_t data_offset
Definition scd.c:75
uint32_t aux_count
Definition scd.c:76
uint32_t num_channels
Definition scd.c:70
uint32_t absolute_offset
Definition scd.c:78
uint32_t loop_end
Definition scd.c:74
uint32_t sample_rate
Definition scd.c:71
uint32_t data_type
Definition scd.c:72
uint32_t length
Definition scd.c:69
uint32_t loop_start
Definition scd.c:73
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static int64_t pts
int size