2010-09-17 20:32:18 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../coding/coding.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
/* MSF header */
|
|
|
|
VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2013-05-27 05:55:50 +02:00
|
|
|
char filename[PATH_LIMIT];
|
2016-11-25 22:29:07 +01:00
|
|
|
off_t start_offset, header_offset = 0;
|
|
|
|
int32_t data_size, loop_start, loop_end;
|
2010-09-17 20:32:18 +02:00
|
|
|
int loop_flag = 0;
|
|
|
|
int channel_count;
|
|
|
|
int codec_id;
|
2016-11-25 22:29:07 +01:00
|
|
|
|
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
ffmpeg_codec_data *ffmpeg_data = NULL;
|
|
|
|
#endif
|
|
|
|
|
2010-09-17 20:32:18 +02:00
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
|
|
|
if (strcasecmp("msf",filename_extension(filename))) goto fail;
|
|
|
|
|
2010-09-26 10:25:43 +02:00
|
|
|
|
2016-11-25 22:29:07 +01:00
|
|
|
/* "WMSF" variation with a mini header over the MSFC header, same extension */
|
|
|
|
if (read_32bitBE(0x00,streamFile) == 0x574D5346) {
|
|
|
|
header_offset = 0x10;
|
|
|
|
}
|
|
|
|
start_offset = header_offset+0x40;
|
2010-09-17 20:32:18 +02:00
|
|
|
|
2016-11-25 22:29:07 +01:00
|
|
|
/* 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 */
|
2011-01-14 02:13:02 +01:00
|
|
|
|
2016-11-25 22:29:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
data_size = read_32bitBE(header_offset+0x0C,streamFile); /* without header*/
|
|
|
|
if (data_size==0xFFFFFFFF) {
|
|
|
|
size_t fileLength = get_streamfile_size(streamFile);
|
|
|
|
data_size = fileLength - start_offset;
|
2010-09-17 20:32:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 22:29:07 +01:00
|
|
|
/* block_align/loop_type? = read_32bitBE(header_offset+0x14,streamFile);*/ /* 00/40 when no loop, 11/50/51/71 */
|
|
|
|
|
|
|
|
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);
|
2010-09-17 20:32:18 +02:00
|
|
|
|
2016-11-25 22:29:07 +01:00
|
|
|
|
2010-09-17 20:32:18 +02:00
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
/* fill in the vital statistics */
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->channels = channel_count;
|
2010-11-05 03:14:53 +01:00
|
|
|
|
2016-11-25 22:29:07 +01:00
|
|
|
/* Sample rate hack for strange files that don't have a specified frequency */
|
|
|
|
if (read_32bitBE(header_offset+0x10,streamFile)==0x00000000)
|
2010-11-05 03:14:53 +01:00
|
|
|
vgmstream->sample_rate = 48000;
|
|
|
|
else
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->sample_rate = read_32bitBE(header_offset+0x10,streamFile);
|
2010-09-26 10:25:43 +02:00
|
|
|
|
2016-11-25 22:29:07 +01:00
|
|
|
|
|
|
|
vgmstream->meta_type = meta_PS3_MSF;
|
2010-09-17 20:32:18 +02:00
|
|
|
|
|
|
|
switch (codec_id) {
|
2010-09-26 10:25:43 +02:00
|
|
|
case 0x0: /* PCM (Big Endian) */
|
|
|
|
{
|
|
|
|
vgmstream->coding_type = coding_PCM16BE;
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->num_samples = data_size/2/channel_count;
|
2011-01-14 02:13:02 +01:00
|
|
|
|
|
|
|
if (loop_flag){
|
2010-09-26 10:25:43 +02:00
|
|
|
vgmstream->loop_start_sample = loop_start/2/channel_count;
|
|
|
|
vgmstream->loop_end_sample = loop_end/2/channel_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel_count == 1)
|
|
|
|
{
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
}
|
|
|
|
else if (channel_count > 1)
|
|
|
|
{
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x3: /* PSx ADPCM */
|
2010-09-17 21:00:21 +02:00
|
|
|
{
|
|
|
|
vgmstream->coding_type = coding_PSX;
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->num_samples = data_size*28/16/channel_count;
|
2011-01-14 02:13:02 +01:00
|
|
|
|
|
|
|
if (loop_flag)
|
|
|
|
{
|
2010-09-26 10:25:43 +02:00
|
|
|
vgmstream->loop_start_sample = loop_start*28/16/channel_count;
|
|
|
|
vgmstream->loop_end_sample = loop_end*28/16/channel_count;
|
2010-09-17 21:00:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (channel_count == 1)
|
|
|
|
{
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
}
|
|
|
|
else if (channel_count > 1)
|
|
|
|
{
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->interleave_block_size = 0x10;
|
2010-09-17 21:00:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-11-25 22:29:07 +01:00
|
|
|
#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
|
2016-12-04 18:07:13 +01:00
|
|
|
#ifdef VGM_USE_FFMPEG
|
2016-11-25 22:29:07 +01:00
|
|
|
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;
|
|
|
|
|
2016-12-04 18:07:13 +01:00
|
|
|
/* todo check CBR better (frame_size=0?) */
|
2016-11-25 22:29:07 +01:00
|
|
|
|
|
|
|
/* 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
|
2016-12-04 18:07:13 +01:00
|
|
|
#if defined(VGM_USE_MPEG) && !defined(VGM_USE_FFMPEG)
|
2010-09-26 10:25:43 +02:00
|
|
|
case 0x7: /* MPEG */
|
2010-09-17 20:32:18 +02:00
|
|
|
{
|
2016-12-04 18:07:13 +01:00
|
|
|
int frame_size = 576; /* todo incorrect looping calcs, MP3 can have other sizes */
|
|
|
|
|
2010-09-17 20:32:18 +02:00
|
|
|
mpeg_codec_data *mpeg_data = NULL;
|
|
|
|
struct mpg123_frameinfo mi;
|
|
|
|
coding_t ct;
|
|
|
|
|
2011-01-19 14:54:59 +01:00
|
|
|
mpeg_data = init_mpeg_codec_data(streamFile, start_offset, vgmstream->sample_rate, vgmstream->channels, &ct, NULL, NULL);
|
2010-09-17 20:32:18 +02:00
|
|
|
if (!mpeg_data) goto fail;
|
|
|
|
vgmstream->codec_data = mpeg_data;
|
|
|
|
|
|
|
|
if (MPG123_OK != mpg123_info(mpeg_data->m, &mi)) goto fail;
|
|
|
|
|
|
|
|
vgmstream->coding_type = ct;
|
|
|
|
vgmstream->layout_type = layout_mpeg;
|
|
|
|
if (mi.vbr != MPG123_CBR) goto fail;
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->num_samples = mpeg_bytes_to_samples(data_size, &mi);
|
2016-12-04 18:07:13 +01:00
|
|
|
vgmstream->num_samples -= vgmstream->num_samples % frame_size;
|
2010-09-17 20:32:18 +02:00
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = mpeg_bytes_to_samples(loop_start, &mi);
|
2016-12-04 18:07:13 +01:00
|
|
|
vgmstream->loop_start_sample -= vgmstream->loop_start_sample % frame_size;
|
2010-09-17 20:32:18 +02:00
|
|
|
vgmstream->loop_end_sample = mpeg_bytes_to_samples(loop_end, &mi);
|
2016-12-04 18:07:13 +01:00
|
|
|
vgmstream->loop_end_sample -= vgmstream->loop_end_sample % frame_size;
|
2010-09-17 20:32:18 +02:00
|
|
|
}
|
|
|
|
vgmstream->interleave_block_size = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
STREAMFILE * file;
|
|
|
|
file = streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
if (!file) goto fail;
|
|
|
|
for (i=0;i<channel_count;i++) {
|
|
|
|
vgmstream->ch[i].streamfile = file;
|
|
|
|
|
|
|
|
vgmstream->ch[i].channel_start_offset=
|
2013-09-03 20:17:16 +02:00
|
|
|
vgmstream->ch[i].offset=start_offset+vgmstream->interleave_block_size*i;
|
2010-09-17 20:32:18 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
/* clean up anything we may have opened */
|
|
|
|
fail:
|
2016-11-25 22:29:07 +01:00
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
if (ffmpeg_data) {
|
|
|
|
free_ffmpeg(ffmpeg_data);
|
|
|
|
vgmstream->codec_data = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2010-09-17 20:32:18 +02:00
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|