Ejemplo n.º 1
0
/* BGW - from Final Fantasy XI (PC) music files */
VGMSTREAM * init_vgmstream_bgw(STREAMFILE *streamFile) {
    VGMSTREAM * vgmstream = NULL;
    STREAMFILE *temp_streamFile = NULL;
    uint32_t codec, file_size, block_size, sample_rate, block_align;
    int32_t loop_start;
    off_t start_offset;

    int channel_count, loop_flag = 0;

    /* check extensions */
    if ( !check_extensions(streamFile, "bgw") )
        goto fail;

    /* check header */
    if (read_32bitBE(0x00,streamFile) != 0x42474d53 || /* "BGMS" */
        read_32bitBE(0x04,streamFile) != 0x74726561 || /* "trea" */
        read_32bitBE(0x08,streamFile) != 0x6d000000 )  /* "m\0\0\0" */
        goto fail;

    codec = read_32bitLE(0x0c,streamFile);
    file_size = read_32bitLE(0x10,streamFile);
    /*file_id = read_32bitLE(0x14,streamFile);*/
    block_size = read_32bitLE(0x18,streamFile);
    loop_start = read_32bitLE(0x1c,streamFile);
    sample_rate = (read_32bitLE(0x20,streamFile) + read_32bitLE(0x24,streamFile)) & 0x7FFFFFFF; /* bizarrely obfuscated sample rate */
    start_offset = read_32bitLE(0x28,streamFile);
    /*0x2c: unk (vol?) */
    /*0x2d: unk (0x10?) */
    channel_count = read_8bit(0x2e,streamFile);
    block_align = (uint8_t)read_8bit(0x2f,streamFile);

    if (file_size != get_streamfile_size(streamFile))
        goto fail;

    loop_flag = (loop_start > 0);

    /* build the VGMSTREAM */
    vgmstream = allocate_vgmstream(channel_count,loop_flag);
    if (!vgmstream) goto fail;

    vgmstream->meta_type = meta_FFXI_BGW;
    vgmstream->sample_rate = sample_rate;

    switch (codec) {
        case 0: /* PS ADPCM */
            vgmstream->coding_type = coding_PSX_cfg;
            vgmstream->layout_type = layout_interleave;
            vgmstream->interleave_block_size = (block_align / 2) + 1; /* half, even if channels = 1 */

            vgmstream->num_samples = block_size * block_align;
            if (loop_flag) {
                vgmstream->loop_start_sample = (loop_start-1) * block_align;
                vgmstream->loop_end_sample = vgmstream->num_samples;
            }
            
            break;

#ifdef VGM_USE_FFMPEG
        case 3: { /* ATRAC3 (encrypted) */
            uint8_t buf[0x100];
            int bytes, joint_stereo, skip_samples;
            size_t data_size = file_size - start_offset;

            vgmstream->num_samples = block_size; /* atrac3_bytes_to_samples gives the same value */
            if (loop_flag) {
                vgmstream->loop_start_sample = loop_start;
                vgmstream->loop_end_sample = vgmstream->num_samples;
            }

            block_align  = 0xC0 * vgmstream->channels; /* 0x00 in header */
            joint_stereo = 0;
            skip_samples = 0;

            bytes = ffmpeg_make_riff_atrac3(buf, 0x100, vgmstream->num_samples, data_size, vgmstream->channels, vgmstream->sample_rate, block_align, joint_stereo, skip_samples);
            if (bytes <= 0) goto fail;

            temp_streamFile = setup_bgw_atrac3_streamfile(streamFile, start_offset,data_size, 0xC0,channel_count);
            if (!temp_streamFile) goto fail;

            vgmstream->codec_data = init_ffmpeg_header_offset(temp_streamFile, buf,bytes, 0,data_size);
            if (!vgmstream->codec_data) goto fail;
            vgmstream->coding_type = coding_FFmpeg;
            vgmstream->layout_type = layout_none;

            close_streamfile(temp_streamFile);
            break;
        }
#endif

        default:
            goto fail;
    }


    if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
        goto fail;
    return vgmstream;

fail:
    close_streamfile(temp_streamFile);
    close_vgmstream(vgmstream);
    return NULL;
}
Ejemplo n.º 2
0
/* 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;
}