Added WMSF variation; AT3/MP3 support via FFMPEG

This commit is contained in:
bnnm 2016-11-25 22:29:07 +01:00
parent 9e6458c4b9
commit eb491e0e1b

View File

@ -6,54 +6,81 @@
VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) { VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL; VGMSTREAM * vgmstream = NULL;
char filename[PATH_LIMIT]; char filename[PATH_LIMIT];
off_t start_offset; off_t start_offset, header_offset = 0;
int32_t loop_start, loop_end; int32_t data_size, loop_start, loop_end;
int loop_flag = 0; int loop_flag = 0;
int channel_count; int channel_count;
int codec_id; int codec_id;
size_t fileLength;
#ifdef VGM_USE_FFMPEG
ffmpeg_codec_data *ffmpeg_data = NULL;
#endif
/* check extension, case insensitive */ /* check extension, case insensitive */
streamFile->get_name(streamFile,filename,sizeof(filename)); streamFile->get_name(streamFile,filename,sizeof(filename));
if (strcasecmp("msf",filename_extension(filename))) goto fail; if (strcasecmp("msf",filename_extension(filename))) goto fail;
if (read_8bit(0x0,streamFile) != 0x4D) goto fail; /* M */ /* "WMSF" variation with a mini header over the MSFC header, same extension */
if (read_8bit(0x1,streamFile) != 0x53) goto fail; /* S */ if (read_32bitBE(0x00,streamFile) == 0x574D5346) {
if (read_8bit(0x2,streamFile) != 0x46) goto fail; /* F */ header_offset = 0x10;
}
start_offset = header_offset+0x40;
fileLength = get_streamfile_size(streamFile); /* usually 0x4D534643 "MSFC" */
if (read_8bit(header_offset+0x0,streamFile) != 0x4D) goto fail; /* M */
if (read_8bit(header_offset+0x1,streamFile) != 0x53) goto fail; /* S */
if (read_8bit(header_offset+0x2,streamFile) != 0x46) goto fail; /* F */
loop_flag = (read_32bitBE(0x18,streamFile) != 0xFFFFFFFF);
if (loop_flag)
{ data_size = read_32bitBE(header_offset+0x0C,streamFile); /* without header*/
loop_start = read_32bitBE(0x18,streamFile); if (data_size==0xFFFFFFFF) {
loop_end = read_32bitBE(0x0C,streamFile); size_t fileLength = get_streamfile_size(streamFile);
data_size = fileLength - start_offset;
} }
channel_count = read_32bitBE(0x8,streamFile); /* block_align/loop_type? = read_32bitBE(header_offset+0x14,streamFile);*/ /* 00/40 when no loop, 11/50/51/71 */
codec_id = read_32bitBE(0x4,streamFile);
loop_start = read_32bitBE(header_offset+0x18,streamFile);
loop_end = read_32bitBE(header_offset+0x1C,streamFile); /* loop duration */
loop_flag = loop_start != 0xFFFFFFFF;
if (loop_flag) {
if (loop_end==0xFFFFFFFF) {/* not seen */
loop_end = data_size;
} else {
loop_end = loop_start + loop_end; /* usually equals data_size but not always */
if ( loop_end > data_size)/* not seen */
loop_end = data_size;
}
}
channel_count = read_32bitBE(header_offset+0x8,streamFile);
codec_id = read_32bitBE(header_offset+0x4,streamFile);
/* build the VGMSTREAM */ /* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag); vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail; if (!vgmstream) goto fail;
/* fill in the vital statistics */ /* fill in the vital statistics */
vgmstream->channels = channel_count; vgmstream->channels = channel_count;
/* Sample rate hack for strange files that don't have a specified frequency */ /* Sample rate hack for strange files that don't have a specified frequency */
if (read_32bitBE(0x10,streamFile)==0x00000000) if (read_32bitBE(header_offset+0x10,streamFile)==0x00000000)
vgmstream->sample_rate = 48000; vgmstream->sample_rate = 48000;
else else
vgmstream->sample_rate = read_32bitBE(0x10,streamFile); vgmstream->sample_rate = read_32bitBE(header_offset+0x10,streamFile);
start_offset = 0x40;
vgmstream->meta_type = meta_PS3_MSF;
switch (codec_id) { switch (codec_id) {
case 0x0: /* PCM (Big Endian) */ case 0x0: /* PCM (Big Endian) */
{ {
vgmstream->coding_type = coding_PCM16BE; vgmstream->coding_type = coding_PCM16BE;
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/2/channel_count; vgmstream->num_samples = data_size/2/channel_count;
if (loop_flag){ if (loop_flag){
vgmstream->loop_start_sample = loop_start/2/channel_count; vgmstream->loop_start_sample = loop_start/2/channel_count;
@ -74,12 +101,7 @@ VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) {
case 0x3: /* PSx ADPCM */ case 0x3: /* PSx ADPCM */
{ {
vgmstream->coding_type = coding_PSX; vgmstream->coding_type = coding_PSX;
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)*28/16/channel_count; vgmstream->num_samples = data_size*28/16/channel_count;
if (vgmstream->num_samples == 0xFFFFFFFF)
{
vgmstream->num_samples = (fileLength - start_offset)*28/16/channel_count;
}
if (loop_flag) if (loop_flag)
{ {
@ -94,11 +116,62 @@ VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) {
else if (channel_count > 1) else if (channel_count > 1)
{ {
vgmstream->layout_type = layout_interleave; vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x10; // read_32bitBE(0x14,streamFile); vgmstream->interleave_block_size = 0x10;
} }
} }
break; break;
#ifdef VGM_USE_MPEG #ifdef VGM_USE_FFMPEG
case 0x4: /* ATRAC3 (frame size 96) */
case 0x5: /* ATRAC3 (frame size 152) */
case 0x6: /* ATRAC3 (frame size 192) */
/* delegate to FFMpeg, it can parse MSF files */
ffmpeg_data = init_ffmpeg_offset(streamFile, header_offset, streamFile->get_size(streamFile) );
if ( !ffmpeg_data ) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
vgmstream->meta_type = meta_FFmpeg;
vgmstream->codec_data = ffmpeg_data;
vgmstream->num_samples = ffmpeg_data->totalFrames;
if (loop_flag) {
int atrac3_frame = 1024;
int block_align = (codec_id == 0x4 ? 96 : codec_id == 0x5 ? 152 : 192) * channel_count;
/* int block_align = ffmpeg_data->codecCtx->block_align; *//* is this always set? */
vgmstream->loop_start_sample = (loop_start / block_align) * atrac3_frame;
vgmstream->loop_end_sample = (loop_end / block_align) * atrac3_frame;
}
break;
#endif
#if defined(VGM_USE_FFMPEG) && !defined(VGM_USE_MPEG)
case 0x7: /* MPEG */
/* delegate to FFMpeg, it can parse MSF files */
ffmpeg_data = init_ffmpeg_offset(streamFile, header_offset, streamFile->get_size(streamFile) );
if ( !ffmpeg_data ) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
vgmstream->meta_type = meta_FFmpeg;
vgmstream->codec_data = ffmpeg_data;
/* todo check CBR better (bitrate=0?) */
/* vgmstream->num_samples = ffmpeg_data->totalFrames; */ /* duration is not set/innacurate for MP3 in FFMpeg */
vgmstream->num_samples = (int64_t)data_size * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
if (loop_flag) {
int frame_size = ffmpeg_data->codecCtx->frame_size;
vgmstream->loop_start_sample = (int64_t)loop_start * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
vgmstream->loop_start_sample -= vgmstream->loop_start_sample==frame_size ? frame_size
: vgmstream->loop_start_sample % frame_size;
vgmstream->loop_end_sample = (int64_t)loop_end * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
vgmstream->loop_end_sample -= vgmstream->loop_end_sample==frame_size ? frame_size
: vgmstream->loop_end_sample % frame_size;
}
break;
#endif
#if VGM_USE_MPEG
case 0x7: /* MPEG */ case 0x7: /* MPEG */
{ {
mpeg_codec_data *mpeg_data = NULL; mpeg_codec_data *mpeg_data = NULL;
@ -114,7 +187,7 @@ VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) {
vgmstream->coding_type = ct; vgmstream->coding_type = ct;
vgmstream->layout_type = layout_mpeg; vgmstream->layout_type = layout_mpeg;
if (mi.vbr != MPG123_CBR) goto fail; if (mi.vbr != MPG123_CBR) goto fail;
vgmstream->num_samples = mpeg_bytes_to_samples(read_32bitBE(0xC,streamFile), &mi); vgmstream->num_samples = mpeg_bytes_to_samples(data_size, &mi);
vgmstream->num_samples -= vgmstream->num_samples%576; vgmstream->num_samples -= vgmstream->num_samples%576;
if (loop_flag) { if (loop_flag) {
vgmstream->loop_start_sample = mpeg_bytes_to_samples(loop_start, &mi); vgmstream->loop_start_sample = mpeg_bytes_to_samples(loop_start, &mi);
@ -130,7 +203,6 @@ VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) {
goto fail; goto fail;
} }
vgmstream->meta_type = meta_PS3_MSF;
/* open the file for reading */ /* open the file for reading */
{ {
@ -151,6 +223,12 @@ VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) {
/* clean up anything we may have opened */ /* clean up anything we may have opened */
fail: fail:
#ifdef VGM_USE_FFMPEG
if (ffmpeg_data) {
free_ffmpeg(ffmpeg_data);
vgmstream->codec_data = NULL;
}
#endif
if (vgmstream) close_vgmstream(vgmstream); if (vgmstream) close_vgmstream(vgmstream);
return NULL; return NULL;
} }