/* .SMV - from Cho Aniki Zero (PSP) */ VGMSTREAM * init_vgmstream_smv(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; off_t start_offset; int loop_flag, channel_count; size_t channel_size, loop_start; /* check extension */ if (!check_extensions(streamFile, "smv")) goto fail; channel_size = read_32bitLE(0x00,streamFile); /* 0x08: number of full interleave blocks */ channel_count = read_16bitLE(0x0a,streamFile); loop_start = read_32bitLE(0x18,streamFile); loop_flag = (loop_start != -1); start_offset = 0x800; if (channel_size * channel_count + start_offset != get_streamfile_size(streamFile)) goto fail; channel_size -= 0x10; /* last value has SPU end frame without flag 0x7 as it should */ /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count, loop_flag); if (!vgmstream) goto fail; vgmstream->sample_rate = read_32bitLE(0x10, streamFile); vgmstream->num_samples = ps_bytes_to_samples(channel_size*channel_count, channel_count); vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start*channel_count, channel_count); vgmstream->loop_end_sample = vgmstream->num_samples; vgmstream->meta_type = meta_SMV; vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = read_32bitLE(0x04, streamFile); vgmstream->interleave_last_block_size = read_32bitLE(0x0c, streamFile); if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) goto fail; return vgmstream; fail: close_vgmstream(vgmstream); return NULL; }
/* MIB+MIH - SCEE MultiStream interleaved bank (header+data) [namCollection: Ace Combat 2 (PS2), Rampage: Total Destruction (PS2)] */ VGMSTREAM * init_vgmstream_mib_mih(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; STREAMFILE * streamHeader = NULL; off_t start_offset; size_t data_size, frame_size, frame_last, frame_count; int channel_count, loop_flag; /* check extension */ if (!check_extensions(streamFile, "mib")) goto fail; streamHeader = open_streamfile_by_ext(streamFile,"mih"); if (!streamHeader) goto fail; if (read_32bitBE(0x00,streamHeader) != 0x40000000) /* header size */ goto fail; loop_flag = 0; /* MIB+MIH don't PS-ADPCM loop flags */ channel_count = read_32bitLE(0x08,streamHeader); start_offset = 0x00; /* 0x04: padding (0x20, MIH header must be multiple of 0x40) */ frame_last = (uint16_t)read_16bitLE(0x05,streamHeader); frame_size = read_32bitLE(0x10,streamHeader); frame_count = read_32bitLE(0x14,streamHeader); if (frame_count == 0) { /* rarely [Gladius (PS2)] */ frame_count = get_streamfile_size(streamFile) / (frame_size * channel_count); } data_size = frame_count * frame_size; data_size -= frame_last ? (frame_size-frame_last) : 0; /* last frame has less usable data */ data_size *= channel_count; /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->sample_rate = read_32bitLE(0x0C,streamHeader); vgmstream->num_samples = ps_bytes_to_samples(data_size, channel_count); vgmstream->meta_type = meta_PS2_MIB_MIH; vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = frame_size; if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) goto fail; close_streamfile(streamHeader); return vgmstream; fail: close_streamfile(streamHeader); close_vgmstream(vgmstream); return NULL; }
/* .ASS - from Dai Senryaku VII: Exceed (PS2) */ VGMSTREAM * init_vgmstream_ps2_ass(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; off_t start_offset; size_t channel_size, interleave; int loop_flag, channel_count, sample_rate; int32_t num_samples, loop_start = 0, loop_end = 0; /* checks */ if (!check_extensions(streamFile, "ass")) goto fail; start_offset = 0x800; channel_count = read_32bitLE(0x00,streamFile); /* assumed */ if (channel_count != 2) goto fail; sample_rate = read_32bitLE(0x04,streamFile); channel_size = read_32bitLE(0x08,streamFile); interleave = read_32bitLE(0x0c,streamFile); num_samples = ps_bytes_to_samples(channel_size,1); loop_flag = ps_find_loop_offsets(streamFile, start_offset, channel_size*channel_count, channel_count, interleave, &loop_start, &loop_end); loop_flag = loop_flag && (num_samples > 10*sample_rate); /* disable looping for smaller files (in seconds) */ /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->meta_type = meta_PS2_ASS; vgmstream->sample_rate = sample_rate; vgmstream->num_samples = num_samples; vgmstream->loop_start_sample = loop_start; vgmstream->loop_end_sample = loop_end; vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = interleave; if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) goto fail; return vgmstream; fail: close_vgmstream(vgmstream); return NULL; }
/* MUSC - from Krome's PS2 games (The Legend of Spyro, Ty the Tasmanian Tiger) */ VGMSTREAM * init_vgmstream_musc(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; int loop_flag, channel_count; off_t start_offset; size_t data_size; /* .mus is the real extension, .musc is the header ID */ if (!check_extensions(streamFile,"mus,musc")) goto fail; if (read_32bitBE(0x00,streamFile) != 0x4D555343) /* "MUSC" */ goto fail; start_offset = read_32bitLE(0x10,streamFile); data_size = read_32bitLE(0x14,streamFile); if (start_offset + data_size != get_streamfile_size(streamFile)) goto fail; /* always does full loops unless it ends in silence */ loop_flag = read_32bitBE(get_streamfile_size(streamFile) - 0x10,streamFile) != 0x0C000000; channel_count = 2; /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->sample_rate = (uint16_t)read_16bitLE(0x06,streamFile); vgmstream->num_samples = ps_bytes_to_samples(data_size, channel_count); vgmstream->loop_start_sample = 0; vgmstream->loop_end_sample = vgmstream->num_samples; vgmstream->meta_type = meta_MUSC; vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = read_32bitLE(0x18,streamFile) / 2; if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) goto fail; return vgmstream; fail: close_vgmstream(vgmstream); return NULL; }
/* A2M - from Artificial Mind & Movement games [Scooby-Doo! Unmasked (PS2)] */ VGMSTREAM * init_vgmstream_a2m(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; off_t start_offset; size_t data_size; int loop_flag, channel_count; /* checks */ if ( !check_extensions(streamFile,"int") ) goto fail; if (read_32bitBE(0x00,streamFile) != 0x41324D00) /* "A2M\0" */ goto fail; if (read_32bitBE(0x04,streamFile) != 0x50533200) /* "PS2\0" */ goto fail; start_offset = 0x30; data_size = get_streamfile_size(streamFile) - start_offset; channel_count = 2; loop_flag = 0; /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->meta_type = meta_A2M; vgmstream->sample_rate = read_32bitBE(0x10,streamFile); vgmstream->num_samples = ps_bytes_to_samples(data_size,channel_count); vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = 0x6000; if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) ) goto fail; return vgmstream; fail: close_vgmstream(vgmstream); return NULL; }
/* SAB - from Worms 4: Mayhem (PC/Xbox/PS2) */ VGMSTREAM * init_vgmstream_sab(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; off_t start_offset; int loop_flag, channel_count = 0, is_stream, align, codec, sample_rate, stream_size, loop_start, loop_end; int total_subsongs, target_subsong = streamFile->stream_index; /* .sab: main, .sob: config/names */ if (!check_extensions(streamFile,"sab")) goto fail; if (read_32bitBE(0x00,streamFile) != 0x43535732 && /* "CSW2" (Windows) */ read_32bitBE(0x00,streamFile) != 0x43535032 && /* "CSP2" (PS2) */ read_32bitBE(0x00,streamFile) != 0x43535832) /* "CSX2" (Xbox) */ goto fail; is_stream = read_32bitLE(0x04,streamFile) & 0x04; /* other flags don't seem to matter */ total_subsongs = is_stream ? 1 : read_32bitLE(0x08,streamFile); if (target_subsong == 0) target_subsong = 1; if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail; align = read_32bitLE(0x0c,streamFile); /* doubles as interleave */ /* stream config */ codec = read_32bitLE(0x18 + 0x1c*(target_subsong-1) + 0x00,streamFile); channel_count = read_32bitLE(0x18 + 0x1c*(target_subsong-1) + 0x04,streamFile); sample_rate = read_32bitLE(0x18 + 0x1c*(target_subsong-1) + 0x08,streamFile); stream_size = read_32bitLE(0x18 + 0x1c*(target_subsong-1) + 0x0c,streamFile); loop_start = read_32bitLE(0x18 + 0x1c*(target_subsong-1) + 0x10,streamFile); loop_end = read_32bitLE(0x18 + 0x1c*(target_subsong-1) + 0x14,streamFile); loop_flag = (loop_end > 0); start_offset = 0x18 + 0x1c*total_subsongs; if (start_offset % align) start_offset += align - (start_offset % align); start_offset += read_32bitLE(0x18 + 0x1c*(target_subsong-1) + 0x18,streamFile); if (is_stream) { channel_count = read_32bitLE(0x08,streamFile); /* uncommon, but non-stream stereo exists */ stream_size *= channel_count; } /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->sample_rate = sample_rate; vgmstream->num_streams = total_subsongs; vgmstream->stream_size = stream_size; vgmstream->meta_type = meta_SAB; switch(codec) { case 0x01: /* PC */ vgmstream->coding_type = coding_PCM16LE; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = is_stream ? align : 0x02; vgmstream->num_samples = pcm_bytes_to_samples(stream_size, vgmstream->channels, 16); vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, vgmstream->channels, 16); vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end, vgmstream->channels, 16); break; case 0x04: /* PS2 */ vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = is_stream ? align : 0x10; vgmstream->num_samples = ps_bytes_to_samples(stream_size, vgmstream->channels); vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start, vgmstream->channels); vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end, vgmstream->channels); break; case 0x08: /* Xbox */ vgmstream->coding_type = is_stream ? coding_XBOX_IMA_int : coding_XBOX_IMA; vgmstream->layout_type = is_stream ? layout_interleave : layout_none; vgmstream->interleave_block_size = is_stream ? align : 0x00; vgmstream->num_samples = xbox_ima_bytes_to_samples(stream_size, vgmstream->channels); vgmstream->loop_start_sample = xbox_ima_bytes_to_samples(loop_start, vgmstream->channels); vgmstream->loop_end_sample = xbox_ima_bytes_to_samples(loop_end, vgmstream->channels); break; default: VGM_LOG("SAB: unknown codec\n"); goto fail; } get_stream_name(vgmstream->stream_name, streamFile, target_subsong); if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) goto fail; return vgmstream; fail: close_vgmstream(vgmstream); return NULL; }
/* .VSV - from Square Enix games [Dawn of Mana: Seiken Densetsu 4 (PS2), Kingdom Hearts Re:Chain of Memories (PS2)] */ VGMSTREAM * init_vgmstream_vsv(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; STREAMFILE *temp_streamFile = NULL; off_t start_offset; int loop_flag, channel_count, flags, sample_rate, is_rs; size_t loop_start, adjust, data_size, interleave; /* checks */ /* .vsv: extension from internal filenames [KH Re:CoM (PS2), DoM (PS2), KH HD I.5 + II.5 ReMIX (PS4)] * .psh: fake */ if (!check_extensions(streamFile, "vsv,psh")) goto fail; /* 0x00(1x4): flags/config? */ if ((uint8_t)read_8bit(0x03,streamFile) > 0x64) /* possibly volume */ goto fail; if ((uint8_t)read_8bit(0x0a,streamFile) != 0) /* not seen */ goto fail; /* Romancing SaGa (PS2) uses an earlier? version, this seems to work */ is_rs = ((uint16_t)read_16bitLE(0x00,streamFile) == 0); start_offset = 0x00; /* correct, but needs some tricks to fix sound (see below) */ interleave = 0x800; adjust = (uint16_t)read_16bitLE(0x04,streamFile); loop_start = ((uint16_t)read_16bitLE(0x06,streamFile) & 0x7FFF) * interleave; loop_flag = (uint16_t)read_16bitLE(0x06,streamFile) & 0x8000; /* loop_start != 0 works too, no files loop from beginning to end */ sample_rate = (uint16_t)read_16bitLE(0x08,streamFile); flags = (uint8_t)read_8bit (0x0b,streamFile); /* values: 0x01=stereo, 0x10=mono */ data_size = (uint16_t)read_16bitLE(0x0c,streamFile) * interleave; /* 0x0e: ? (may be a low-ish value) */ channel_count = (flags & 1) ? 2 : 1; /* must discard to avoid wrong loops and unwanted data (easier to see in voices) */ if (!is_rs) { /* RS doesn't do this */ /* adjust & 0xF800 is unknown (values=0x0000|0x0800|0xF800, can be mono/stereo, loop/no, adjust/no) */ size_t discard = adjust & 0x07FF; /* at (file_end - 0x800 + discard) is a 0x03 PS flag to check this (adjust 0 does discard full block) */ data_size -= (0x800 - discard) * channel_count; } /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count, loop_flag); if (!vgmstream) goto fail; vgmstream->meta_type = meta_VSV; vgmstream->sample_rate = sample_rate; vgmstream->num_samples = ps_bytes_to_samples(data_size, channel_count); vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start, channel_count); vgmstream->loop_end_sample = vgmstream->num_samples; /* these loops are odd, but comparing the audio wave with the OSTs values seem correct */ if (is_rs) { vgmstream->loop_start_sample -= ps_bytes_to_samples(channel_count*interleave,channel_count); /* maybe *before* loop block? */ vgmstream->loop_start_sample -= ps_bytes_to_samples(0x200*channel_count,channel_count); /* maybe default adjust? */ } vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = interleave; temp_streamFile = setup_vsv_streamfile(streamFile, start_offset, data_size); if (!temp_streamFile) goto fail; if (!vgmstream_open_stream(vgmstream, temp_streamFile, start_offset)) goto fail; close_streamfile(temp_streamFile); return vgmstream; fail: close_streamfile(temp_streamFile); close_vgmstream(vgmstream); return NULL; }
/* .ADS - Sony's "Audio Stream" format [Edit Racing (PS2), Evergrace II (PS2), Pri-Saga! Portable (PSP)] */ VGMSTREAM * init_vgmstream_ps2_ads(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; off_t start_offset; int loop_flag, channel_count, sample_rate, interleave, is_loop_samples = 0; size_t body_size, stream_size, file_size; uint32_t codec, loop_start_sample = 0, loop_end_sample = 0, loop_start_offset = 0, loop_end_offset = 0; coding_t coding_type; int ignore_silent_frame_cavia = 0, ignore_silent_frame_capcom = 0; /* checks */ /* .ads: actual extension * .ss2: demuxed videos (fake?) * .pcm: Taisho Mononoke Ibunroku (PS2) * .adx: Armored Core 3 (PS2) * [no actual extension]: MotoGP (PS2) * .800: Mobile Suit Gundam: The One Year War (PS2) */ if (!check_extensions(streamFile, "ads,ss2,pcm,adx,,800")) goto fail; if (read_32bitBE(0x00,streamFile) != 0x53536864 && /* "SShd" */ read_32bitBE(0x20,streamFile) != 0x53536264) /* "SSbd" */ goto fail; if (read_32bitLE(0x04,streamFile) != 0x18 && /* standard header size */ read_32bitLE(0x04,streamFile) != 0x20) /* True Fortune (PS2) */ goto fail; /* base values (a bit unorderly since devs hack ADS too much and detection is messy) */ { codec = read_32bitLE(0x08,streamFile); sample_rate = read_32bitLE(0x0C,streamFile); channel_count = read_32bitLE(0x10,streamFile); /* up to 4 [Eve of Extinction (PS2)]*/ interleave = read_32bitLE(0x14,streamFile); /* set even when mono */ switch(codec) { case 0x01: /* official definition */ case 0x80000001: /* [Evergrace II (PS2), but not other From Soft games] */ coding_type = coding_PCM16LE; /* Angel Studios/Rockstar San Diego videos codec hijack [Red Dead Revolver (PS2), Spy Hunter 2 (PS2)] */ if (sample_rate == 12000 && interleave == 0x200) { sample_rate = 48000; interleave = 0x40; coding_type = coding_DVI_IMA_int; /* should try to detect IMA data but it's not so easy, this works ok since * no known games use these settings, videos normally are 48000/24000hz */ } break; case 0x10: /* official definition */ case 0x02: /* Capcom games extension, stereo only [Megaman X7 (PS2), Breath of Fire V (PS2), Clock Tower 3 (PS2)] */ coding_type = coding_PSX; break; case 0x00: /* PCM16BE from official docs, probably never used */ default: VGM_LOG("ADS: unknown codec\n"); goto fail; } } /* sizes */ { file_size = get_streamfile_size(streamFile); body_size = read_32bitLE(0x24,streamFile); /* bigger than file_size in rare cases, even if containing all data (ex. Megaman X7's SY04.ADS) */ if (body_size + 0x28 > file_size) { body_size = file_size - 0x28; } /* True Fortune: weird stream size */ if (body_size * 2 == file_size - 0x18) { body_size = (body_size * 2) - 0x10; } stream_size = body_size; } /* offset */ { start_offset = 0x28; /* start padding (body size is ok, may have end padding) [Evergrace II (PS2), Armored Core 3 (PS2)] */ /* detection depends on files being properly ripped, so broken/cut files won't play ok */ if (file_size - body_size >= 0x800) { start_offset = 0x800; /* aligned to sector */ /* too much end padding, happens in Super Galdelic Hour's SEL.ADS, maybe in bad rips too */ VGM_ASSERT(file_size - body_size > 0x8000, "ADS: big end padding %x\n", file_size - body_size); } /* "ADSC" container */ if (coding_type == coding_PSX && read_32bitLE(0x28,streamFile) == 0x1000 /* real start */ && read_32bitLE(0x2c,streamFile) == 0 && read_32bitLE(0x1008,streamFile) != 0) { int i; int is_adsc = 1; /* should be empty up to data start */ for (i = 0; i < 0xFDC/4; i++) { if (read_32bitLE(0x2c+(i*4),streamFile) != 0) { is_adsc = 0; break; } } if (is_adsc) { start_offset = 0x1000 - 0x08; /* remove "ADSC" alignment */ /* stream_size doesn't count start offset padding */ } } } /* loops */ { uint32_t loop_start, loop_end; loop_start = read_32bitLE(0x18,streamFile); loop_end = read_32bitLE(0x1C,streamFile); loop_flag = 0; /* detect loops the best we can; docs say those are loop block addresses, * but each maker does whatever (no games seem to use PS-ADPCM loop flags though) */ if (loop_start != 0xFFFFFFFF && loop_end == 0xFFFFFFFF) { if (codec == 0x02) { /* Capcom codec */ /* Capcom games: loop_start is address * 0x10 [Mega Man X7, Breath of Fire V, Clock Tower 3] */ loop_flag = ((loop_start * 0x10) + 0x200 < body_size); /* near the end (+0x20~80) means no loop */ loop_start_offset = loop_start * 0x10; ignore_silent_frame_capcom = 1; } else if (read_32bitBE(0x28,streamFile) == 0x50414421) { /* "PAD!" padding until 0x800 */ /* Super Galdelic Hour: loop_start is PCM bytes */ loop_flag = 1; loop_start_sample = loop_start / 2 / channel_count; is_loop_samples = 1; } else if ((loop_start % 0x800 == 0) && loop_start > 0) {/* sector-aligned, min/0 is 0x800 */ /* cavia games: loop_start is offset [Drakengard 1/2, GITS: Stand Alone Complex] */ /* offset is absolute from the "cavia stream format" container that adjusts ADS start */ loop_flag = 1; loop_start_offset = loop_start - 0x800; ignore_silent_frame_cavia = 1; } else if (loop_start % 0x800 != 0 || loop_start == 0) { /* not sector aligned */ /* Katakamuna: loop_start is address * 0x10 */ loop_flag = 1; loop_start_offset = loop_start * 0x10; } } else if (loop_start != 0xFFFFFFFF && loop_end != 0xFFFFFFFF && loop_end > 0) { /* ignore Kamen Rider Blade and others */ #if 0 //todo improve detection to avoid clashing with address*0x20 if (loop_end == body_size / 0x10) { /* always body_size? but not all files should loop */ /* Akane Iro ni Somaru Saka - Parallel: loops is address * 0x10 */ loop_flag = 1; loop_start_offset = loop_start * 0x10; loop_end_offset = loop_end * 0x10; } #endif if (loop_end <= body_size / 0x70 && coding_type == coding_PCM16LE) { /* close to body_size */ /* Armored Core - Nexus: loops is address * 0x70 */ loop_flag = 1; loop_start_offset = loop_start * 0x70; loop_end_offset = loop_end * 0x70; } else if (loop_end <= body_size / 0x20 && coding_type == coding_PCM16LE) { /* close to body_size */ /* Armored Core - Nine Breaker: loops is address * 0x20 */ loop_flag = 1; loop_start_offset = loop_start * 0x20; loop_end_offset = loop_end * 0x20; } else if (loop_end <= body_size / 0x20 && coding_type == coding_PSX) { /* close to body_size */ /* various games: loops is address * 0x20 [Fire Pro Wrestling Returns, A.C.E. - Another Century's Episode] */ loop_flag = 1; loop_start_offset = loop_start * 0x20; loop_end_offset = loop_end * 0x20; } else if ((loop_end > body_size / 0x20 && coding_type == coding_PSX) || (loop_end > body_size / 0x70 && coding_type == coding_PCM16LE)) { /* various games: loops in samples [Eve of Extinction, Culdcept, WWE Smackdown! 3] */ loop_flag = 1; loop_start_sample = loop_start; loop_end_sample = loop_end; is_loop_samples = 1; } } //todo Jet Ion Grand Prix seems to have some loop-like values at 0x28 //todo Yoake mae yori Ruriiro na has loops in unknown format } /* most games have empty PS-ADPCM frames in the last interleave block that should be skipped for smooth looping */ if (coding_type == coding_PSX) { off_t offset, min_offset; offset = start_offset + stream_size; min_offset = offset - interleave; do { offset -= 0x10; if (read_8bit(offset+0x01,streamFile) == 0x07) { stream_size -= 0x10*channel_count;/* ignore don't decode flag/padding frame (most common) [ex. Capcom games] */ } else if (read_32bitBE(offset+0x00,streamFile) == 0x00000000 && read_32bitBE(offset+0x04,streamFile) == 0x00000000 && read_32bitBE(offset+0x08,streamFile) == 0x00000000 && read_32bitBE(offset+0x0c,streamFile) == 0x00000000) { stream_size -= 0x10*channel_count; /* ignore null frame [ex. A.C.E. Another Century Episode 1/2/3] */ } else if (read_32bitBE(offset+0x00,streamFile) == 0x00007777 && read_32bitBE(offset+0x04,streamFile) == 0x77777777 && read_32bitBE(offset+0x08,streamFile) == 0x77777777 && read_32bitBE(offset+0x0c,streamFile) == 0x77777777) { stream_size -= 0x10*channel_count; /* ignore padding frame [ex. Akane Iro ni Somaru Saka - Parallel] */ } else if (read_32bitBE(offset+0x00,streamFile) == 0x0C020000 && read_32bitBE(offset+0x04,streamFile) == 0x00000000 && read_32bitBE(offset+0x08,streamFile) == 0x00000000 && read_32bitBE(offset+0x0c,streamFile) == 0x00000000 && ignore_silent_frame_cavia) { stream_size -= 0x10*channel_count; /* ignore silent frame [ex. cavia games] */ } else if (read_32bitBE(offset+0x00,streamFile) == 0x0C010000 && read_32bitBE(offset+0x04,streamFile) == 0x00000000 && read_32bitBE(offset+0x08,streamFile) == 0x00000000 && read_32bitBE(offset+0x0c,streamFile) == 0x00000000 && ignore_silent_frame_capcom) { stream_size -= 0x10*channel_count; /* ignore silent frame [ex. Capcom games] */ } else { break; /* standard frame */ } } while(offset > min_offset); /* don't bother fixing loop_end_offset since will be adjusted to num_samples later, if needed */ } /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->sample_rate = sample_rate; vgmstream->coding_type = coding_type; vgmstream->interleave_block_size = interleave; vgmstream->layout_type = layout_interleave; vgmstream->meta_type = meta_PS2_SShd; switch(coding_type) { case coding_PCM16LE: vgmstream->num_samples = pcm_bytes_to_samples(stream_size, channel_count, 16); break; case coding_PSX: vgmstream->num_samples = ps_bytes_to_samples(stream_size, channel_count); break; case coding_DVI_IMA_int: vgmstream->num_samples = ima_bytes_to_samples(stream_size, channel_count); break; default: goto fail; } if (vgmstream->loop_flag) { if (is_loop_samples) { vgmstream->loop_start_sample = loop_start_sample; vgmstream->loop_end_sample = loop_end_sample; } else { switch(vgmstream->coding_type) { case coding_PCM16LE: vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start_offset,channel_count,16); vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end_offset,channel_count,16); break; case coding_PSX: vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start_offset,channel_count); vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end_offset,channel_count); break; default: goto fail; } } /* when loop_end = 0xFFFFFFFF */ if (vgmstream->loop_end_sample == 0) vgmstream->loop_end_sample = vgmstream->num_samples; /* happens even when loops are directly samples, loops sound fine (ex. Culdcept) */ if (vgmstream->loop_end_sample > vgmstream->num_samples) vgmstream->loop_end_sample = vgmstream->num_samples; } if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) goto fail; return vgmstream; fail: close_vgmstream(vgmstream); return NULL; }
/* SCD - Square-Enix games (FF XIII, XIV) */ VGMSTREAM * init_vgmstream_sqex_scd(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; off_t start_offset, tables_offset, meta_offset, extradata_offset, name_offset = 0; int32_t stream_size, extradata_size, loop_start, loop_end; int loop_flag = 0, channel_count, codec, sample_rate; int version, target_entry, aux_chunk_count; int total_subsongs, target_subsong = streamFile->stream_index; int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL; int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL; /* check extension, case insensitive */ if ( !check_extensions(streamFile, "scd") ) goto fail; /** main header **/ if (read_32bitBE(0x00,streamFile) != 0x53454442 && /* "SEDB" */ read_32bitBE(0x04,streamFile) != 0x53534346) /* "SSCF" */ goto fail; if (read_8bit(0x0c,streamFile) == 0x01) { /* big endian flag */ //size_offset = 0x14; read_32bit = read_32bitBE; read_16bit = read_16bitBE; } else { //size_offset = 0x10; read_32bit = read_32bitLE; read_16bit = read_16bitLE; } /* SSCF version? (older SSCFs from Crisis Core/FFXI X360 seem to be V3/2) */ if (read_8bit(0x0d,streamFile) != 0x04) goto fail; /* v2: FFXIII demo (PS3), FFT0 test files (PC); v3: common; v4: Kingdom Hearts 2.8 (PS4) */ version = read_32bit(0x08,streamFile); if (version != 2 && version != 3 && version != 4) goto fail; tables_offset = read_16bit(0x0e,streamFile); /* usually 0x30 or 0x20 */ #if 0 /* never mind, FFXIII music_68tak.ps3.scd is 0x80 shorter */ /* check file size with header value */ if (read_32bit(size_offset,streamFile) != get_streamfile_size(streamFile)) goto fail; #endif /** offset tables **/ /* 0x00(2): table1/4 (unknown) entries */ /* 0x02(2): table2 (unknown) entries */ /* 0x04(2): table3 (headers) entries */ /* 0x06(2): unknown, varies even for clone files */ /* (implicit: table1 starts at 0x20) */ /* 0x08: table2 (unknown) start offset */ /* 0x0c: table3 (headers) start offset */ /* 0x10: table4 (unknown) start offset */ /* 0x14: always null? */ /* 0x18: table5? (unknown) start offset? */ /* 0x1c: unknown, often null */ /* each table entry is an uint32_t offset; after entries there is padding */ /* if a table isn't present entries is 0 and offset points to next table */ /* find meta_offset in table3 (headers) and total subsongs */ { int i; int headers_entries = read_16bit(tables_offset+0x04,streamFile); off_t headers_offset = read_32bit(tables_offset+0x0c,streamFile); if (target_subsong == 0) target_subsong = 1; total_subsongs = 0; meta_offset = 0; /* manually find subsongs as entries can be dummy (ex. sfx banks in FF XIV or FF Type-0) */ for (i = 0; i < headers_entries; i++) { off_t entry_offset = read_32bit(headers_offset + i*0x04,streamFile); if (read_32bit(entry_offset+0x0c,streamFile) == -1) continue; /* codec -1 when dummy */ total_subsongs++; if (!meta_offset && total_subsongs == target_subsong) { meta_offset = entry_offset; target_entry = i; } } if (meta_offset == 0) goto fail; /* SCD can contain 0 entries too */ } /** stream header **/ stream_size = read_32bit(meta_offset+0x00,streamFile); channel_count = read_32bit(meta_offset+0x04,streamFile); sample_rate = read_32bit(meta_offset+0x08,streamFile); codec = read_32bit(meta_offset+0x0c,streamFile); loop_start = read_32bit(meta_offset+0x10,streamFile); loop_end = read_32bit(meta_offset+0x14,streamFile); extradata_size = read_32bit(meta_offset+0x18,streamFile); aux_chunk_count = read_32bit(meta_offset+0x1c,streamFile); /* 0x01e(2): unknown, seen in some FF XIV sfx (MSADPCM) */ loop_flag = (loop_end > 0); extradata_offset = meta_offset + 0x20; start_offset = extradata_offset + extradata_size; /* only "MARK" chunk is known (some FF XIV PS3 have "STBL" but it's not counted) */ if (aux_chunk_count > 1 && aux_chunk_count < 0xFFFF) { /* some FF XIV Heavensward IMA sfx have 0x01000000 */ VGM_LOG("SCD: unknown aux chunk count %i\n", aux_chunk_count); goto fail; } /* skips aux chunks, sometimes needed (Lightning Returns X360, FF XIV PC) */ if (aux_chunk_count && read_32bitBE(extradata_offset, streamFile) == 0x4D41524B) { /* "MARK" */ extradata_offset += read_32bit(extradata_offset+0x04, streamFile); } /* find name if possible */ if (version == 4) { int info_entries = read_16bit(tables_offset+0x00,streamFile); int headers_entries = read_16bit(tables_offset+0x04,streamFile); off_t info_offset = tables_offset+0x20; /* not very exact as table1 and table3 entries may differ in V3, not sure about V4 */ if (info_entries == headers_entries) { off_t entry_offset = read_16bit(info_offset + 0x04*target_entry,streamFile); name_offset = entry_offset+0x30; } } #ifdef VGM_USE_VORBIS /* special case using init_vgmstream_ogg_vorbis */ if (codec == 0x06) { VGMSTREAM *ogg_vgmstream; uint8_t ogg_version, ogg_byte; ogg_vorbis_meta_info_t ovmi = {0}; ovmi.meta_type = meta_SQEX_SCD; ovmi.total_subsongs = total_subsongs; /* loop values are in bytes, let init_vgmstream_ogg_vorbis find loop comments instead */ ogg_version = read_8bit(extradata_offset + 0x00, streamFile); /* 0x01(1): 0x20 in v2/3, this ogg miniheader size? */ ogg_byte = read_8bit(extradata_offset + 0x02, streamFile); /* 0x03(1): ? in v3 */ if (ogg_version == 0) { /* 0x10? header, then custom Vorbis header before regular Ogg (FF XIV PC v1) */ ovmi.stream_size = stream_size; } else { /* 0x20 header, then seek table */ size_t seek_table_size = read_32bit(extradata_offset+0x10, streamFile); size_t vorb_header_size = read_32bit(extradata_offset+0x14, streamFile); /* 0x18(4): ? (can be 0) */ if ((extradata_offset-meta_offset) + seek_table_size + vorb_header_size != extradata_size) goto fail; ovmi.stream_size = vorb_header_size + stream_size; start_offset = extradata_offset + 0x20 + seek_table_size; /* extradata_size skips vorb_header */ if (ogg_version == 2) { /* header is XOR'ed using byte (FF XIV PC) */ ovmi.decryption_callback = scd_ogg_v2_decryption_callback; ovmi.scd_xor = ogg_byte; ovmi.scd_xor_length = vorb_header_size; } else if (ogg_version == 3) { /* file is XOR'ed using table (FF XIV Heavensward PC) */ ovmi.decryption_callback = scd_ogg_v3_decryption_callback; ovmi.scd_xor = stream_size & 0xFF; /* ogg_byte not used? */ ovmi.scd_xor_length = vorb_header_size + stream_size; } else { VGM_LOG("SCD: unknown ogg_version 0x%x\n", ogg_version); } } /* actual Ogg init */ ogg_vgmstream = init_vgmstream_ogg_vorbis_callbacks(streamFile, NULL, start_offset, &ovmi); if (ogg_vgmstream && name_offset) read_string(ogg_vgmstream->stream_name, PATH_LIMIT, name_offset, streamFile); return ogg_vgmstream; } #endif /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->sample_rate = sample_rate; vgmstream->num_streams = total_subsongs; vgmstream->stream_size = stream_size; vgmstream->meta_type = meta_SQEX_SCD; if (name_offset) read_string(vgmstream->stream_name, PATH_LIMIT, name_offset, streamFile); switch (codec) { case 0x01: /* PCM */ vgmstream->coding_type = coding_PCM16LE; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = 0x02; vgmstream->num_samples = pcm_bytes_to_samples(stream_size, channel_count, 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); } break; case 0x03: /* PS-ADPCM [Final Fantasy Type-0] */ vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = 0x10; vgmstream->num_samples = ps_bytes_to_samples(stream_size, channel_count); 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); } break; case 0x06: /* OGG [Final Fantasy XIII-2 (PC), Final Fantasy XIV (PC)] */ goto fail; /* handled above */ #ifdef VGM_USE_MPEG case 0x07: { /* MPEG [Final Fantasy XIII (PS3)] */ mpeg_codec_data *mpeg_data = NULL; mpeg_custom_config cfg = {0}; cfg.interleave = 0x800; /* for multistream [Final Fantasy XIII-2 (PS3)], otherwise ignored */ cfg.data_size = stream_size; mpeg_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_SCD, &cfg); if (!mpeg_data) goto fail; vgmstream->codec_data = mpeg_data; vgmstream->layout_type = layout_none; /* some Drakengard 3, Kingdom Hearts HD have adjusted sample rate (47999, 44099), for looping? */ vgmstream->num_samples = mpeg_bytes_to_samples(stream_size, mpeg_data); vgmstream->loop_start_sample = mpeg_bytes_to_samples(loop_start, mpeg_data); vgmstream->loop_end_sample = mpeg_bytes_to_samples(loop_end, mpeg_data); /* somehow loops offsets aren't always frame-aligned, and the code below supposedly helped, * but there isn't much difference since MPEG loops are rough (1152-aligned). Seems it * would help more loop_start - ~1000, loop_end + ~1000 (ex. FFXIII-2 music_SunMizu.ps3.scd) */ //vgmstream->num_samples -= vgmstream->num_samples % 576; //vgmstream->loop_start_sample -= vgmstream->loop_start_sample % 576; //vgmstream->loop_end_sample -= vgmstream->loop_end_sample % 576; break; } #endif case 0x0C: /* MS ADPCM [Final Fantasy XIV (PC) sfx] */ vgmstream->coding_type = coding_MSADPCM; vgmstream->layout_type = layout_none; vgmstream->interleave_block_size = read_16bit(extradata_offset+0x0c,streamFile); /* in extradata_offset is a WAVEFORMATEX (including coefs and all) */ vgmstream->num_samples = msadpcm_bytes_to_samples(stream_size, vgmstream->interleave_block_size, vgmstream->channels); if (loop_flag) { vgmstream->loop_start_sample = msadpcm_bytes_to_samples(loop_start, vgmstream->interleave_block_size, vgmstream->channels); vgmstream->loop_end_sample = msadpcm_bytes_to_samples(loop_end, vgmstream->interleave_block_size, vgmstream->channels); } break; case 0x0A: /* DSP ADPCM [Dragon Quest X (Wii)] */ case 0x15: { /* DSP ADPCM [Dragon Quest X (Wii U)] (no apparent differences except higher sample rate) */ const off_t interleave_size = 0x800; const off_t stride_size = interleave_size * channel_count; int i; size_t total_size; layered_layout_data * data = NULL; /* interleaved DSPs including the header (so the first 0x800 is 0x60 header + 0x740 data) * so interleave layout can't used; we'll setup de-interleaving streamfiles as layers/channels instead */ //todo this could be simplified using a block layout or adding interleave_first_block vgmstream->coding_type = coding_NGC_DSP; vgmstream->layout_type = layout_layered; /* read from the first DSP header and verify other channel headers */ { total_size = (read_32bitBE(start_offset+0x04,streamFile)+1)/2; /* rounded nibbles / 2 */ vgmstream->num_samples = read_32bitBE(start_offset+0x00,streamFile); if (loop_flag) { vgmstream->loop_start_sample = loop_start; vgmstream->loop_end_sample = loop_end+1; } for (i = 1; i < channel_count; i++) { if ((read_32bitBE(start_offset+4,streamFile)+1)/2 != total_size || read_32bitBE(start_offset+interleave_size*i+0x00,streamFile) != vgmstream->num_samples) { goto fail; } } } /* init layout */ data = init_layout_layered(channel_count); if (!data) goto fail; vgmstream->layout_data = data; /* open each layer subfile */ for (i = 0; i < channel_count; i++) { STREAMFILE* temp_streamFile = setup_scd_dsp_streamfile(streamFile, start_offset+interleave_size*i, interleave_size, stride_size, total_size); if (!temp_streamFile) goto fail; data->layers[i] = init_vgmstream_ngc_dsp_std(temp_streamFile); close_streamfile(temp_streamFile); if (!data->layers[i]) goto fail; } /* setup layered VGMSTREAMs */ if (!setup_layout_layered(data)) goto fail; break; } #ifdef VGM_USE_FFMPEG case 0x0B: { /* XMA2 [Final Fantasy (X360), Lightning Returns (X360) sfx, Kingdom Hearts 2.8 (X1)] */ ffmpeg_codec_data *ffmpeg_data = NULL; uint8_t buf[200]; int32_t bytes; /* extradata_offset+0x00: fmt0x166 header (BE), extradata_offset+0x34: seek table */ bytes = ffmpeg_make_riff_xma_from_fmt_chunk(buf,200, extradata_offset,0x34, stream_size, streamFile, 1); ffmpeg_data = init_ffmpeg_header_offset(streamFile, buf,bytes, start_offset,stream_size); if (!ffmpeg_data) goto fail; vgmstream->codec_data = ffmpeg_data; vgmstream->coding_type = coding_FFmpeg; vgmstream->layout_type = layout_none; vgmstream->num_samples = ffmpeg_data->totalSamples; vgmstream->loop_start_sample = loop_start; vgmstream->loop_end_sample = loop_end; xma_fix_raw_samples(vgmstream, streamFile, start_offset,stream_size, 0, 0,0); /* samples are ok, loops? */ break; } case 0x0E: { /* ATRAC3/ATRAC3plus [Lord of Arcana (PSP), Final Fantasy Type-0] */ ffmpeg_codec_data *ffmpeg_data = NULL; /* full RIFF header at start_offset/extradata_offset (same) */ ffmpeg_data = init_ffmpeg_offset(streamFile, start_offset,stream_size); if (!ffmpeg_data) goto fail; vgmstream->codec_data = ffmpeg_data; vgmstream->coding_type = coding_FFmpeg; vgmstream->layout_type = layout_none; vgmstream->num_samples = ffmpeg_data->totalSamples; /* fact samples */ vgmstream->loop_start_sample = loop_start; vgmstream->loop_end_sample = loop_end; if (ffmpeg_data->skipSamples <= 0) /* in case FFmpeg didn't get them */ ffmpeg_set_skip_samples(ffmpeg_data, riff_get_fact_skip_samples(streamFile, start_offset)); /* SCD loop/sample values are relative (without skip samples) vs RIFF (with skip samples), no need to adjust */ break; } #endif #ifdef VGM_USE_ATRAC9 case 0x16: { /* ATRAC9 [Kingdom Hearts 2.8 (PS4)] */ atrac9_config cfg = {0}; /* post header has various typical ATRAC9 values */ cfg.channels = vgmstream->channels; cfg.config_data = read_32bit(extradata_offset+0x0c,streamFile); cfg.encoder_delay = read_32bit(extradata_offset+0x18,streamFile); vgmstream->codec_data = init_atrac9(&cfg); if (!vgmstream->codec_data) goto fail; vgmstream->coding_type = coding_ATRAC9; vgmstream->layout_type = layout_none; vgmstream->num_samples = read_32bit(extradata_offset+0x10,streamFile); /* loop values above are also weird and ignored */ vgmstream->loop_start_sample = read_32bit(extradata_offset+0x20, streamFile) - (loop_flag ? cfg.encoder_delay : 0); //loop_start vgmstream->loop_end_sample = read_32bit(extradata_offset+0x24, streamFile) - (loop_flag ? cfg.encoder_delay : 0); //loop_end break; } #endif case -1: /* used for dummy entries */ default: VGM_LOG("SCD: unknown codec 0x%x\n", codec); goto fail; } if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) ) goto fail; return vgmstream; fail: close_vgmstream(vgmstream); return NULL; }
/* VAGp - Sony SDK format, created by various tools */ VGMSTREAM * init_vgmstream_vag(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; off_t start_offset; size_t file_size, channel_size, interleave; meta_t meta_type; int channel_count = 0, loop_flag, sample_rate; uint32_t vag_id, version; int32_t loop_start_sample = 0, loop_end_sample = 0; int allow_dual_stereo = 0; /* checks */ /* .vag: standard * .swag: Frantix (PSP) * .str: Ben10 Galactic Racing * .vig: MX vs. ATV Untamed (PS2) * .l/r: Crash Nitro Kart (PS2), Gradius V (PS2) */ if ( !check_extensions(streamFile,"vag,swag,str,vig,l,r") ) goto fail; /* check VAG Header */ if (((read_32bitBE(0x00,streamFile) & 0xFFFFFF00) != 0x56414700) && /* "VAG" */ ((read_32bitLE(0x00,streamFile) & 0xFFFFFF00) != 0x56414700)) goto fail; file_size = get_streamfile_size(streamFile); /* version used to create the file: * - 00000000 = v1.8 PC, * - 00000002 = v1.3 Mac (used?) * - 00000003 = v1.6+ Mac * - 00000020 = v2.0 PC (most common) * - 00000004 = ? (later games) * - 00000006 = ? (vagconv) * - 00020001 = v2.1 (vagconv2) * - 00030000 = v3.0 (vagconv2) */ version = (uint32_t)read_32bitBE(0x04,streamFile); /* 0x08-0c: reserved */ channel_size = read_32bitBE(0x0c,streamFile); sample_rate = read_32bitBE(0x10,streamFile); /* 0x14-20 reserved */ /* 0x20-30: name (optional) */ /* 0x30: data start (first 0x10 usually 0s to init SPU) */ /* check variation */ vag_id = read_32bitBE(0x00,streamFile); switch(vag_id) { case 0x56414731: /* "VAG1" (1 channel) [Metal Gear Solid 3 (PS2)] */ meta_type = meta_PS2_VAG1; start_offset = 0x40; /* 0x30 is extra data in VAG1 */ channel_count = 1; interleave = 0; loop_flag = 0; break; case 0x56414732: /* "VAG2" (2 channels) [Metal Gear Solid 3 (PS2)] */ meta_type = meta_PS2_VAG2; start_offset = 0x40; /* 0x30 is extra data in VAG2 */ channel_count = 2; interleave = 0x800; loop_flag = 0; break; case 0x56414769: /* "VAGi" (interleaved) */ meta_type = meta_PS2_VAGi; start_offset = 0x800; channel_count = 2; interleave = read_32bitLE(0x08,streamFile); loop_flag = 0; break; case 0x70474156: /* pGAV (little endian / stereo) [Jak 3 (PS2), Jak X (PS2)] */ meta_type = meta_PS2_pGAV; start_offset = 0x00; //todo 0x30, requires interleave_first if (read_32bitBE(0x20,streamFile) == 0x53746572) { /* "Ster" */ channel_count = 2; if (read_32bitLE(0x2000,streamFile) == 0x56414770) /* "pGAV" */ interleave = 0x2000; /* Jak 3 interleave, includes header */ else if (read_32bitLE(0x1000,streamFile) == 0x56414770) /* "pGAV" */ interleave = 0x1000; /* Jak X interleave, includes header */ else goto fail; //todo interleave_first = interleave - start_offset; /* interleave includes header */ } else { channel_count = 1; interleave = 0; } channel_size = read_32bitLE(0x0C,streamFile) / channel_count; sample_rate = read_32bitLE(0x10,streamFile); //todo adjust channel_size, includes part of header? loop_flag = 0; break; case 0x56414770: /* "VAGp" (standard and variations) */ meta_type = meta_PS2_VAGp; if (check_extensions(streamFile,"vig")) { /* MX vs. ATV Untamed (PS2) */ start_offset = 0x800 - 0x20; channel_count = 2; interleave = 0x10; loop_flag = 0; } else if (check_extensions(streamFile,"swag")) { /* algo "VAGp" at (file_size / channels) */ /* Frantix (PSP) */ start_offset = 0x40; /* channel_size ignores empty frame */ channel_count = 2; interleave = file_size / channel_count; channel_size = read_32bitLE(0x0c,streamFile); sample_rate = read_32bitLE(0x10,streamFile); loop_flag = ps_find_loop_offsets(streamFile, start_offset, channel_size*channel_count, channel_count, interleave, &loop_start_sample, &loop_end_sample); } else if (read_32bitBE(0x6000,streamFile) == 0x56414770) { /* "VAGp" */ /* The Simpsons Wrestling (PS1) */ start_offset = 0x00; //todo 0x30, requires interleave_first channel_count = 2; interleave = 0x6000; //todo interleave_first = interleave - start_offset; /* includes header */ channel_size += 0x30; loop_flag = 0; } else if (read_32bitBE(0x1000,streamFile) == 0x56414770) { /* "VAGp" */ /* Shikigami no Shiro (PS2) */ start_offset = 0x00; //todo 0x30, requires interleave_first channel_count = 2; interleave = 0x1000; //todo interleave_first = interleave - start_offset; /* includes header */ channel_size += 0x30; loop_flag = ps_find_loop_offsets(streamFile, start_offset, channel_size*channel_count, channel_count, interleave, &loop_start_sample, &loop_end_sample); } else if (read_32bitBE(0x30,streamFile) == 0x56414770) { /* "VAGp" */ /* The Red Star (PS2) */ start_offset = 0x60; /* two VAGp headers */ channel_count = 2; if ((file_size - start_offset) % 0x4000 == 0) interleave = 0x4000; else if ((file_size - start_offset) % 0x4180 == 0) interleave = 0x4180; else goto fail; loop_flag = 0; /* loop segments */ } else if (version == 0x40000000) { /* Killzone (PS2) */ start_offset = 0x30; channel_count = 1; interleave = 0; channel_size = read_32bitLE(0x0C,streamFile) / channel_count; sample_rate = read_32bitLE(0x10,streamFile); loop_flag = 0; } else if (version == 0x00020001 || version == 0x00030000) { /* standard Vita/PS4 .vag [Chronovolt (Vita), Grand Kingdom (PS4)] */ start_offset = 0x30; interleave = 0x10; /* channels are at 0x1e, except Ukiyo no Roushi (Vita), which has * loop start/end frame (but also uses PS-ADPCM flags) */ if (read_32bitBE(0x18,streamFile) == 0 && (read_32bitBE(0x1c,streamFile) & 0xFFFF00FF) == 0 && read_8bit(0x1e,streamFile) < 16) { channel_count = read_8bit(0x1e,streamFile); if (channel_count == 0) channel_count = 1; /* ex. early games [Lumines (Vita)] */ } else { channel_count = 1; } channel_size = channel_size / channel_count; loop_flag = ps_find_loop_offsets(streamFile, start_offset, channel_size*channel_count, channel_count, interleave, &loop_start_sample, &loop_end_sample); } else { /* standard PS1/PS2/PS3 .vag [Ecco the Dolphin (PS2), Legasista (PS3)] */ start_offset = 0x30; interleave = 0; channel_count = 1; loop_flag = ps_find_loop_offsets_full(streamFile, start_offset, channel_size*channel_count, channel_count, interleave, &loop_start_sample, &loop_end_sample); allow_dual_stereo = 1; /* often found with external L/R files */ } break; default: goto fail; } /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->meta_type = meta_type; vgmstream->allow_dual_stereo = allow_dual_stereo; vgmstream->sample_rate = sample_rate; vgmstream->num_samples = ps_bytes_to_samples(channel_size,1); vgmstream->loop_start_sample = loop_start_sample; vgmstream->loop_end_sample = loop_end_sample; vgmstream->coding_type = coding_PSX; if (version == 0x00020001 || version == 0x00030000) vgmstream->coding_type = coding_HEVAG; vgmstream->layout_type = (channel_count == 1) ? layout_none : layout_interleave; vgmstream->interleave_block_size = interleave; read_string(vgmstream->stream_name,0x10+1, 0x20,streamFile); /* always, can be null */ if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) ) goto fail; return vgmstream; fail: close_vgmstream(vgmstream); return NULL; }
/* RXWS - from Sony SCEI PS2 games (Okage: Shadow King, Genji, Bokura no Kazoku) */ VGMSTREAM * init_vgmstream_ps2_rxws(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; STREAMFILE * streamHeader = NULL; off_t start_offset, chunk_offset, name_offset = 0; size_t stream_size, chunk_size; int loop_flag = 0, channel_count, is_separate = 0, type, sample_rate; int32_t loop_start, loop_end; int total_subsongs, target_subsong = streamFile->stream_index; /* check extensions */ /* .xws: header and data, .xwh+xwb: header + data (.bin+dat are also found in Wild Arms 4/5) */ if (!check_extensions(streamFile,"xws,xwb")) goto fail; is_separate = check_extensions(streamFile,"xwb"); /* xwh+xwb: use xwh as header; otherwise use the current file */ if (is_separate) { /* extra check to avoid hijacking Microsoft's XWB */ if ((read_32bitBE(0x00,streamFile) == 0x57424E44) || /* "WBND" (LE) */ (read_32bitBE(0x00,streamFile) == 0x444E4257)) /* "DNBW" (BE) */ goto fail; streamHeader = open_stream_ext(streamFile, "xwh"); if (!streamHeader) goto fail; } else { streamHeader = streamFile; } if (read_32bitBE(0x00,streamHeader) != 0x52585753) /* "RXWS" */ goto fail; /* file size (just the .xwh/xws) */ if (read_32bitLE(0x04,streamHeader)+0x10 != get_streamfile_size(streamHeader)) goto fail; /* 0x08(4): version (0x100/0x200), 0x0C: null */ /* typical chunks: FORM, FTXT, MARK, BODY (for .xws) */ if (read_32bitBE(0x10,streamHeader) != 0x464F524D) /* "FORM", main header (always first) */ goto fail; chunk_size = read_32bitLE(0x10+0x04,streamHeader); /* size - 0x10 */ /* 0x08 version (0x100), 0x0c: null */ chunk_offset = 0x20; /* check multi-streams */ total_subsongs = read_32bitLE(chunk_offset+0x00,streamHeader); if (target_subsong == 0) target_subsong = 1; if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail; /* read stream header */ { off_t header_offset = chunk_offset + 0x4 + 0x1c * (target_subsong-1); /* position in FORM */ off_t stream_offset, next_stream_offset, data_offset = 0; type = read_8bit(header_offset+0x00, streamHeader); /* 0x01(1): unknown (always 0x1c), 0x02(2): flags? (usually 8002/0002, & 0x01 if looped) */ /* 0x04(4): vol/pan stuff? (0x00007F7F), 0x08(1): null?, 0x0c(4): null? */ channel_count = read_8bit(header_offset+0x09, streamHeader); sample_rate = (uint16_t)read_16bitLE(header_offset+0x0a,streamHeader); stream_offset = read_32bitLE(header_offset+0x10,streamHeader); loop_end = read_32bitLE(header_offset+0x14,streamHeader); loop_start = read_32bitLE(header_offset+0x18,streamHeader); loop_flag = (loop_start != 0xFFFFFFFF); /* find data start and size */ if (is_separate) { data_offset = 0x00; } else { off_t current_chunk = 0x10; /* note the extra 0x10 in chunk_size/offsets */ while (current_chunk < get_streamfile_size(streamFile)) { if (read_32bitBE(current_chunk,streamFile) == 0x424F4459) { /* "BODY" chunk_type */ data_offset = 0x10 + current_chunk; break; } current_chunk += 0x10 + read_32bitLE(current_chunk+4,streamFile); } if (!data_offset) goto fail; } if (target_subsong == total_subsongs) { next_stream_offset = data_offset + get_streamfile_size(is_separate ? streamFile : streamHeader); } else { off_t next_header_offset = chunk_offset + 0x4 + 0x1c * (target_subsong); next_stream_offset = read_32bitLE(next_header_offset+0x10,streamHeader); } stream_size = next_stream_offset - stream_offset; start_offset = data_offset + stream_offset; } /* get stream name (always follows FORM) */ if (read_32bitBE(0x10+0x10 + chunk_size,streamHeader) == 0x46545854) { /* "FTXT" */ chunk_offset = 0x10+0x10 + chunk_size + 0x10; if (read_32bitLE(chunk_offset+0x00,streamHeader) == total_subsongs) { name_offset = chunk_offset + read_32bitLE(chunk_offset+0x04 + (target_subsong-1)*0x04,streamHeader); } } /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; vgmstream->sample_rate = sample_rate; vgmstream->num_streams = total_subsongs; vgmstream->stream_size = stream_size; vgmstream->meta_type = meta_PS2_RXWS; if (name_offset) read_string(vgmstream->stream_name,STREAM_NAME_SIZE, name_offset,streamHeader); switch (type) { case 0x00: /* PS-ADPCM */ vgmstream->coding_type = coding_PSX; vgmstream->layout_type = layout_interleave; vgmstream->interleave_block_size = 0x10; vgmstream->num_samples = ps_bytes_to_samples(loop_end, channel_count); vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start, channel_count); vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end, channel_count); break; case 0x01: /* PCM */ vgmstream->coding_type = coding_PCM16LE; vgmstream->layout_type = channel_count==1 ? layout_none : layout_interleave; vgmstream->interleave_block_size = 0x2; vgmstream->num_samples = pcm_bytes_to_samples(loop_end, channel_count, 16); 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); break; #ifdef VGM_USE_FFMPEG case 0x02: { /* ATRAC3 */ uint8_t buf[0x100]; int32_t bytes, block_size, encoder_delay, joint_stereo; block_size = 0xc0 * channel_count; joint_stereo = 0; encoder_delay = 0x0; bytes = ffmpeg_make_riff_atrac3(buf, 0x100, vgmstream->num_samples, stream_size, vgmstream->channels, vgmstream->sample_rate, block_size, joint_stereo, encoder_delay); if (bytes <= 0) goto fail; vgmstream->codec_data = init_ffmpeg_header_offset(streamFile, buf,bytes, start_offset,stream_size); if (!vgmstream->codec_data) goto fail; vgmstream->coding_type = coding_FFmpeg; vgmstream->layout_type = layout_none; vgmstream->num_samples = loop_end; vgmstream->loop_start_sample = loop_start; vgmstream->loop_end_sample = loop_end; break; } #endif default: goto fail; } /* open the file for reading */ if (!vgmstream_open_stream(vgmstream,streamFile,start_offset)) goto fail; if (is_separate && streamHeader) close_streamfile(streamHeader); return vgmstream; fail: if (is_separate && streamHeader) close_streamfile(streamHeader); close_vgmstream(vgmstream); return NULL; }