Merge pull request #749 from bnnm/msf-fsb

msf fsb
This commit is contained in:
bnnm 2020-11-06 17:54:07 +01:00 committed by GitHub
commit caa82fc350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 260 additions and 201 deletions

View File

@ -57,6 +57,7 @@ class Cli(object):
p.add_argument('-fss', dest='min_subsongs', help="Filter min subsongs\n(1 filters formats incapable of subsongs)", type=int)
p.add_argument('-fni', dest='include_regex', help="Filter by REGEX including matches of subsong name")
p.add_argument('-fne', dest='exclude_regex', help="Filter by REGEX excluding matches of subsong name")
p.add_argument('-nsc',dest='no_semicolon', help="Remove semicolon names (for songs with multinames)", action='store_true')
p.add_argument('-v', dest='log_level', help="Verbose log level (off|debug|info, default: info)", default='info')
return p.parse_args()
@ -136,7 +137,7 @@ class Cr32Helper(object):
# Makes .txtp (usually 1 but may do N) from a CLI output + subsong
class TxtpMaker(object):
def __init__(self, cfg, output_b):
def __init__(self, cfg, output_b, rename_map):
self.cfg = cfg
self.output = str(output_b).replace("\\r","").replace("\\n","\n")
@ -145,25 +146,31 @@ class TxtpMaker(object):
self.num_samples = self._get_value("stream total samples: ")
self.stream_count = self._get_value("stream count: ")
self.stream_index = self._get_value("stream index: ")
self.stream_name = self._get_string("stream name: ")
self.stream_name = self._get_text("stream name: ")
if self.channels <= 0 or self.sample_rate <= 0:
raise ValueError('Incorrect command result')
self.stream_seconds = self.num_samples / self.sample_rate
self.ignorable = self._is_ignorable(cfg)
self.rename_map = {}
self.rename_map = rename_map
def __str__(self):
return str(self.__dict__)
def _get_string(self, str):
def _get_string(self, str, full=False):
find_pos = self.output.find(str)
if (find_pos == -1):
return None
cut_pos = find_pos + len(str)
str_cut = self.output[cut_pos:]
return str_cut.split()[0]
if full:
return str_cut.split("\n")[0].strip()
else:
return str_cut.split()[0].strip()
def _get_text(self, str):
return self._get_string(str, full=True)
def _get_value(self, str):
res = self._get_string(str)
@ -232,6 +239,12 @@ class TxtpMaker(object):
pos = txt.rfind(".")
if pos >= 0:
txt = txt[:pos]
if self.cfg.no_semicolon:
pos = txt.find(";")
if pos >= 0:
txt = txt[:pos].strip()
return txt
def _write(self, outname, line):
@ -244,7 +257,7 @@ class TxtpMaker(object):
else:
rename_count = 0
self.rename_map[outname] = rename_count + 1
outname = outname.replace(".txtp", "_%s.txtp" % (rename_count))
outname = outname.replace(".txtp", "_%08i.txtp" % (rename_count))
if not cfg.overwrite and os.path.exists(outname):
raise ValueError('TXTP exists in path: ' + outname)
@ -434,6 +447,8 @@ class App(object):
filenames_in += self._find_files('.', filename)
rename_map = {}
total_created = 0
total_dupes = 0
total_errors = 0
@ -466,7 +481,7 @@ class App(object):
if target_subsong == 1:
log.debug("processing %s...", filename_in_clean)
maker = TxtpMaker(self.cfg, output_b)
maker = TxtpMaker(self.cfg, output_b, rename_map)
if not maker.is_ignorable():
self.crc32.update(filename_out)

View File

@ -612,6 +612,7 @@ Loop anchors have priority over `loop_start_segment`, and are ignored in layered
Manually setting values gets old, so TXTP supports a bunch of simple macros. They automate some of the above commands (analyzing the file), and may be combined, so order still matters.
- `volume N (channels)`: sets volume V to selected channels. N.N = percent or NdB = decibels.
- `1.0` or `0dB` = base volume, `2.0` or `6dB` = double volume, `0.5` or `-6dB` = half volume
- `#v N` also works
- `track (channels)`: makes a file of selected channels
- `layer-v N (channels)`: mixes selected channels to N channels with default volume (for layered vocals). If N is 0 (or ommited), automatically sets highest channel count among all layers.
- `layer-b N (channels)`: same, but adjusts volume depending on layers (for layered bgm)

View File

@ -606,6 +606,7 @@ size_t atrac3plus_bytes_to_samples(size_t bytes, int full_block_align);
size_t ac3_bytes_to_samples(size_t bytes, int full_block_align, int channels);
size_t aac_get_samples(STREAMFILE* sf, off_t start_offset, size_t bytes);
size_t mpeg_get_samples(STREAMFILE* sf, off_t start_offset, size_t bytes);
int32_t mpeg_get_samples_clean(STREAMFILE* sf, off_t start, size_t size, size_t* p_loop_start, size_t* p_loop_end, int is_vbr);
/* helper to pass a wrapped, clamped, fake extension-ed, SF to another meta */

View File

@ -3,12 +3,12 @@
#ifdef VGM_USE_MPEG
/* init config and validate per type */
int mpeg_custom_setup_init_default(STREAMFILE *streamFile, off_t start_offset, mpeg_codec_data *data, coding_t *coding_type) {
int mpeg_custom_setup_init_default(STREAMFILE* sf, off_t start_offset, mpeg_codec_data* data, coding_t* coding_type) {
mpeg_frame_info info;
/* get frame info at offset */
if ( !mpeg_get_frame_info(streamFile, start_offset, &info))
if ( !mpeg_get_frame_info(sf, start_offset, &info))
goto fail;
switch(info.layer) {
case 1: *coding_type = coding_MPEG_layer1; break;
@ -113,7 +113,7 @@ fail:
/* writes data to the buffer and moves offsets */
int mpeg_custom_parse_frame_default(VGMSTREAMCHANNEL *stream, mpeg_codec_data *data, int num_stream) {
int mpeg_custom_parse_frame_default(VGMSTREAMCHANNEL* stream, mpeg_codec_data* data, int num_stream) {
mpeg_custom_stream *ms = data->streams[num_stream];
mpeg_frame_info info;
size_t current_data_size = 0;
@ -242,7 +242,7 @@ fail:
* Gets info from a MPEG frame header at offset. Normally you would use mpg123_info but somehow
* it's wrong at times (maybe because we use an ancient version) so here we do our thing.
*/
static int mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info *info) {
static int mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info* info) {
/* index tables */
static const int versions[4] = { /* MPEG 2.5 */ 3, /* reserved */ -1, /* MPEG 2 */ 2, /* MPEG 1 */ 1 };
static const int layers[4] = { -1,3,2,1 };
@ -313,12 +313,12 @@ static int mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info *info) {
fail:
return 0;
}
int mpeg_get_frame_info(STREAMFILE *sf, off_t offset, mpeg_frame_info *info) {
int mpeg_get_frame_info(STREAMFILE* sf, off_t offset, mpeg_frame_info* info) {
uint32_t header = read_u32be(offset, sf);
return mpeg_get_frame_info_h(header, info);
}
size_t mpeg_get_samples(STREAMFILE *sf, off_t start_offset, size_t bytes) {
size_t mpeg_get_samples(STREAMFILE* sf, off_t start_offset, size_t bytes) {
off_t offset = start_offset;
off_t max_offset = start_offset + bytes;
int frames = 0, samples = 0, encoder_delay = 0, encoder_padding = 0;
@ -428,4 +428,48 @@ size_t mpeg_get_samples(STREAMFILE *sf, off_t start_offset, size_t bytes) {
return samples;
}
/* variation of the above, for clean streams = no ID3/VBR headers
* (maybe should be fused in a single thing with config, API is kinda messy too) */
int32_t mpeg_get_samples_clean(STREAMFILE *sf, off_t start, size_t size, size_t* p_loop_start, size_t* p_loop_end, int is_vbr) {
mpeg_frame_info info;
off_t offset = start;
int32_t num_samples = 0, loop_start = 0, loop_end = 0;
if (!is_vbr) {
/* CBR = quick calcs */
if (!mpeg_get_frame_info(sf, offset, &info))
goto fail;
num_samples = size / info.frame_size * info.frame_samples;
loop_start = *p_loop_start / info.frame_size * info.frame_samples;
loop_end = *p_loop_end / info.frame_size * info.frame_samples;
}
else {
/* VBR (or unknown) = count frames */
while (offset < start + size) {
if (!mpeg_get_frame_info(sf, offset, &info))
goto fail;
if (*p_loop_start + start == offset)
loop_start = num_samples;
num_samples += info.frame_samples;
offset += info.frame_size;
if (*p_loop_end + start == offset)
loop_end = num_samples;
}
}
*p_loop_start = loop_start;
*p_loop_end = loop_end;
return num_samples;
fail:
VGM_LOG("MPEG: sample reader failed at %lx\n", offset);
return 0;
}
#endif

View File

@ -1,13 +1,6 @@
#ifndef _FSB_KEYS_H_
#define _FSB_KEYS_H_
typedef struct {
int is_fsb5; /* FSB5 or FSB4/3*/
int is_alt; /* alt XOR mode (seemingly not tied to FSB version or anything) */
size_t fsbkey_size;
const uint8_t *fsbkey;
} fsbkey_info;
/**
* List of known keys, found in aluigi's site (http://aluigi.altervista.org), forums, guessfsb.exe or manually.
*/
@ -84,12 +77,22 @@ static const uint8_t key_scp[] = { 0x42,0x61,0x73,0x69,0x63,0x45,0x6E,0x63,0x72,
/* Guitar Hero: Metallica (X360) */
static const uint8_t key_ghm[] = { 0x8C,0xFA,0xF3,0x14,0xB1,0x53,0xDA,0xAB,0x2B,0x82,0x6B,0xD5,0x55,0x16,0xCF,0x01,0x90,0x20,0x28,0x14,0xB1,0x53,0xD8 };
/* Worms Rumble Beta (PC) */ //"FXnTffGJ9LS855Gc"
static const uint8_t key_wrb[] = { 0x46,0x58,0x6E,0x54,0x66,0x66,0x47,0x4A,0x39,0x4C,0x53,0x38,0x35,0x35,0x47,0x63 };
// Unknown:
// - Battle: Los Angeles
// - Guitar Hero: Warriors of Rock, DJ hero FSB
// - Longmenkezhan
// - Gas Guzzlers: Combat Carnage (PC?) "C5FA83EA64B34EC2BFE" hex or text? [FSB5]
typedef struct {
int is_fsb5; /* FSB5 or FSB4/3*/
int is_alt; /* alt XOR mode (seemingly not tied to FSB version or anything) */
size_t fsbkey_size;
const uint8_t *fsbkey;
} fsbkey_info;
static const fsbkey_info fsbkey_list[] = {
{ 0,0, sizeof(key_dj2),key_dj2 },
{ 0,0, sizeof(key_dfp),key_dfp },//FSB4
@ -149,7 +152,7 @@ static const fsbkey_info fsbkey_list[] = {
{ 1,0, sizeof(key_sek),key_sek },// FSB5
{ 1,0, sizeof(key_scp),key_scp },// FSB5
{ 0,1, sizeof(key_ghm),key_ghm },// FSB4
{ 1,0, sizeof(key_wrb),key_wrb },// FSB5
};
static const int fsbkey_list_count = sizeof(fsbkey_list) / sizeof(fsbkey_list[0]);

View File

@ -7,7 +7,7 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
off_t start_offset;
uint32_t data_size, loop_start = 0, loop_end = 0;
uint32_t codec, flags;
int loop_flag, channel_count, sample_rate;
int loop_flag, channels, sample_rate;
/* checks */
@ -19,14 +19,14 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
goto fail;
/* check header "MSF" + version-char, usually:
* 0x01, 0x02, 0x30 ("0"), 0x35 ("5"), 0x43 ("C") (last/most common version) */
* 0x01, 0x02, 0x30="0", 0x35="5", 0x43="C" (last/most common version) */
if ((read_u32be(0x00,sf) & 0xffffff00) != 0x4D534600) /* "MSF\0" */
goto fail;
start_offset = 0x40;
codec = read_u32be(0x04,sf);
channel_count = read_s32be(0x08,sf);
channels = read_s32be(0x08,sf);
data_size = read_u32be(0x0C,sf); /* without header */
if (data_size == 0xFFFFFFFF) /* unneeded? */
data_size = get_streamfile_size(sf) - start_offset;
@ -35,26 +35,26 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
/* byte flags, not in MSFv1 or v2
* 0x01/02/04/08: loop marker 0/1/2/3
* 0x10: resample options (force 44/48khz)
* 0x20: VBR MP3 source (changed into simplified 0x1a1 CBR)
* 0x20: VBR MP3 source (encoded with min/max quality options, may end up being CBR)
* 0x40: joint stereo MP3 (apparently interleaved stereo for other formats)
* 0x80+: (none/reserved) */
flags = read_u32be(0x14,sf);
/* sometimes loop_start/end is set with flag 0x10, but from tests it only loops if 0x01/02 is set
* 0x10 often goes with 0x01 but not always (Castlevania HoD); Malicious PS3 uses flag 0x2 instead */
loop_flag = flags != 0xffffffff && ((flags & 0x01) || (flags & 0x02));
loop_flag = (flags != 0xffffffff) && ((flags & 0x01) || (flags & 0x02));
/* loop markers (marker N @ 0x18 + N*(4+4), but in practice only marker 0 is used) */
if (loop_flag) {
loop_start = read_u32be(0x18,sf);
loop_end = read_u32be(0x1C,sf); /* loop duration */
loop_end = loop_start + loop_end; /* usually equals data_size but not always */
if (loop_end > data_size)/* not seen */
if (loop_end > data_size) /* not seen */
loop_end = data_size;
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
vgmstream = allocate_vgmstream(channels, loop_flag);
if (!vgmstream) goto fail;
vgmstream->meta_type = meta_MSF;
@ -63,16 +63,16 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
vgmstream->sample_rate = 48000;
switch (codec) {
case 0x00: /* PCM (Big Endian) */
case 0x00: /* PCM (Big Endian) [MSEnc tests] */
case 0x01: { /* PCM (Little Endian) [Smash Cars (PS3)] */
vgmstream->coding_type = codec==0 ? coding_PCM16BE : coding_PCM16LE;
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x02;
vgmstream->num_samples = pcm_bytes_to_samples(data_size, channel_count,16);
vgmstream->num_samples = pcm_bytes_to_samples(data_size, channels, 16);
if (loop_flag){
vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, channel_count,16);
vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end, channel_count,16);
vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, channels, 16);
vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end, channels, 16);
}
break;
@ -87,10 +87,10 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x10;
vgmstream->num_samples = ps_bytes_to_samples(data_size, channel_count);
vgmstream->num_samples = ps_bytes_to_samples(data_size, channels);
if (loop_flag) {
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start,channel_count);
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end,channel_count);
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start,channels);
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end,channels);
}
break;
@ -127,25 +127,23 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
}
#endif
#if defined(VGM_USE_MPEG)
case 0x07: { /* MPEG (CBR LAME MP3) [Dengeki Bunko Fighting Climax (PS3)] */
case 0x07: { /* MPEG (LAME MP3) [Dengeki Bunko Fighting Climax (PS3), Asura's Wrath (PS3)-vbr] */
int is_vbr = (flags & 0x20); /* must calc samples/loop offsets manually */
vgmstream->codec_data = init_mpeg(sf, start_offset, &vgmstream->coding_type, vgmstream->channels);
if (!vgmstream->codec_data) goto fail;
vgmstream->layout_type = layout_none;
vgmstream->num_samples = mpeg_bytes_to_samples(data_size, vgmstream->codec_data);
if (loop_flag) {
vgmstream->loop_start_sample = mpeg_bytes_to_samples(loop_start, vgmstream->codec_data);
vgmstream->loop_end_sample = mpeg_bytes_to_samples(loop_end, vgmstream->codec_data);
/* loops are always aligned to CBR frame beginnings */
}
vgmstream->num_samples = mpeg_get_samples_clean(sf, start_offset, data_size, &loop_start, &loop_end, is_vbr);
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = loop_end;
/* MPEG here seems stripped from ID3/Xing headers, loops are frame offsets */
/* encoder delay varies between 1152 (1f), 528, 576, etc; probably not actually skipped */
break;
}
#elif defined(VGM_USE_FFMPEG)
case 0x07:
{ /* MPEG (CBR LAME MP3) [Dengeki Bunko Fighting Climax (PS3)] */
case 0x07: { /* MPEG (LAME MP3) [Dengeki Bunko Fighting Climax (PS3), Asura's Wrath (PS3)-vbr] */
ffmpeg_codec_data *ffmpeg_data = NULL;
ffmpeg_data = init_ffmpeg_offset(sf, start_offset, sf->get_size(sf));
@ -154,14 +152,12 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
//todo use same calcs as above
vgmstream->num_samples = (int64_t)data_size * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
if (loop_flag) {
vgmstream->loop_start_sample = (int64_t)loop_start * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
vgmstream->loop_end_sample = (int64_t)loop_end * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
/* loops are always aligned to CBR frame beginnings */
}
/* encoder delay varies between 1152 (1f), 528, 576, etc; probably not actually skipped */
break;
}
#endif
@ -171,7 +167,7 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
}
if (!vgmstream_open_stream(vgmstream,sf,start_offset))
if (!vgmstream_open_stream(vgmstream, sf, start_offset))
goto fail;
return vgmstream;

View File

@ -894,13 +894,12 @@ static int get_position(const char* params, double* value_f, char* value_type) {
static int get_volume(const char* params, double *value, int *is_set) {
int n, m;
double temp_f;
int temp_i;
char temp_c1, temp_c2;
if (is_set) *is_set = 0;
/* test if format is NdB (decibels) */
m = sscanf(params, " %i%c%c%n", &temp_i, &temp_c1, &temp_c2, &n);
m = sscanf(params, " %lf%c%c%n", &temp_f, &temp_c1, &temp_c2, &n);
if (m == 3 && temp_c1 == 'd' && (temp_c2 == 'B' || temp_c2 == 'b')) {
/* dB 101:
* - logaritmic scale
@ -916,7 +915,7 @@ static int get_volume(const char* params, double *value, int *is_set) {
*/
if (is_set) *is_set = 1;
*value = pow(10, temp_i / 20.0); /* dB to % where 1.0 = max */
*value = pow(10, temp_f / 20.0); /* dB to % where 1.0 = max */
return n;
}
@ -1466,7 +1465,7 @@ static void parse_params(txtp_entry* entry, char* params) {
//todo cleanup
/* macros */
else if (strcmp(command,"@volume") == 0) {
else if (is_match(command,"v") || is_match(command,"@volume")) {
txtp_mix_data mix = {0};
nm = get_volume(params, &mix.vol, NULL);