Adding psx_decoder.c + support for PS2 (ADS/SS2) Files

git-svn-id: https://vgmstream.svn.sourceforge.net/svnroot/vgmstream@78 51a99a44-fe44-0410-b1ba-c3e57ba2b86b
This commit is contained in:
fastelbja 2008-05-04 20:36:40 +00:00
parent 0b003b45b4
commit 9cfb2629db
7 changed files with 179 additions and 2 deletions

40
src/coding/psx_decoder.c Normal file
View File

@ -0,0 +1,40 @@
#include "psx_decoder.h"
#include "../util.h"
double VAG_f[5][2] = { { 0.0 , 0.0 },
{ 60.0 / 64.0 , 0.0 },
{ 115.0 / 64.0 , -52.0 / 64.0 },
{ 98.0 / 64.0 , -55.0 / 64.0 } ,
{ 122.0 / 64.0 , -60.0 / 64.0 } } ;
void decode_psx(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do) {
int predict_nr, shift_factor, sample;
int16_t hist1=stream->adpcm_history1_16;
int16_t hist2=stream->adpcm_history2_16;
int scale;
int i;
int32_t sample_count;
predict_nr = read_8bit(stream->offset,stream->streamfile) >> 4;
shift_factor = read_8bit(stream->offset,stream->streamfile) & 0xf;
for (i=first_sample,sample_count=0; i<first_sample+samples_to_do; i++,sample_count+=channelspacing) {
int sample_byte = (short)read_8bit(stream->offset+2+i/2,stream->streamfile);
scale = (short)((i&1 ?
sample_byte >> 4 :
sample_byte & 0x0f)<<12);
sample=(int)((scale >> shift_factor)+hist1*VAG_f[predict_nr][0]+hist2*VAG_f[predict_nr][1]);
outbuf[sample_count] = clamp16(sample);
hist2=hist1;
hist1=sample;
}
stream->adpcm_history1_16=hist1;
stream->adpcm_history2_16=hist2;
}

9
src/coding/psx_decoder.h Normal file
View File

@ -0,0 +1,9 @@
#include "../vgmstream.h"
#ifndef _PSX_DECODER_H
#define _PSX_DECODER_H
void decode_psx(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do);
#endif

94
src/meta/ps2_ads.c Normal file
View File

@ -0,0 +1,94 @@
#include "ps2_ads.h"
#include "../util.h"
/* Sony .ADS with SShd & SSbd Headers */
VGMSTREAM * init_vgmstream_ps2_ads(const char * const filename) {
VGMSTREAM * vgmstream = NULL;
STREAMFILE * infile = NULL;
int loop_flag=0;
int channel_count;
off_t start_offset;
int i;
/* check extension, case insensitive */
if (strcasecmp("ads",filename_extension(filename)) &&
strcasecmp("ss2",filename_extension(filename))) goto fail;
/* try to open the file for header reading */
infile = open_streamfile(filename);
if (!infile) goto fail;
/* check SShd Header */
if (read_32bitBE(0x00,infile) != 0x53536864)
goto fail;
/* check SSbd Header */
if (read_32bitBE(0x20,infile) != 0x53536264)
goto fail;
/* check if file is not corrupt */
if (get_streamfile_size(infile)<(size_t)(read_32bitLE(0x24,infile) + 0x28))
goto fail;
/* check loop */
loop_flag = (read_32bitLE(0x18,infile)!=0xFFFFFFFF);
channel_count=read_32bitLE(0x10,infile);
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
/* fill in the vital statistics */
vgmstream->channels = read_32bitLE(0x10,infile);
vgmstream->sample_rate = read_32bitLE(0x0C,infile);
/* Check for Compression Scheme */
vgmstream->coding_type = coding_PSX;
vgmstream->num_samples = read_32bitLE(0x24,infile)/16*28/vgmstream->channels;
/* SS2 container with RAW Interleaved PCM */
if (read_32bitLE(0x08,infile)==0x01) {
vgmstream->coding_type=coding_PCM16LE;
vgmstream->num_samples = read_32bitLE(0x24,infile)/2/vgmstream->channels;
}
/* Get loop point values */
if(vgmstream->loop_flag) {
vgmstream->loop_start_sample = read_32bitLE(0x18,infile);
vgmstream->loop_end_sample = read_32bitLE(0x1C,infile);
}
/* don't know why, but it does happen, in ps2 too :( */
if (vgmstream->loop_end_sample > vgmstream->num_samples)
vgmstream->loop_end_sample = vgmstream->num_samples;
vgmstream->interleave_block_size = read_32bitLE(0x14,infile);
vgmstream->layout_type = layout_interleave;
vgmstream->meta_type = meta_PS2_SShd;
close_streamfile(infile); infile=NULL;
/* open the file for reading by each channel */
{
for (i=0;i<channel_count;i++) {
vgmstream->ch[i].streamfile = open_streamfile_buffer(filename,0x8000);
if (!vgmstream->ch[i].streamfile) goto fail;
vgmstream->ch[i].channel_start_offset=
vgmstream->ch[i].offset=
0x28+vgmstream->interleave_block_size*i;
}
}
return vgmstream;
/* clean up anything we may have opened */
fail:
if (infile) close_streamfile(infile);
if (vgmstream) close_vgmstream(vgmstream);
return NULL;
}

8
src/meta/ps2_ads.h Normal file
View File

@ -0,0 +1,8 @@
#include "../vgmstream.h"
#ifndef _PS2_ADS_H
#define _PS2_ADS_H
VGMSTREAM * init_vgmstream_ps2_ads(const char * const filename);
#endif

View File

@ -19,6 +19,7 @@
#include "meta/ngc_dsp_std.h"
#include "meta/Cstr.h"
#include "meta/gcsw.h"
#include "meta/ps2_ads.h"
#include "layout/interleave.h"
#include "layout/nolayout.h"
#include "layout/blocked.h"
@ -29,12 +30,14 @@
#include "coding/ngc_dtk_decoder.h"
#include "coding/g721_decoder.h"
#include "coding/ngc_afc_decoder.h"
#include "coding/psx_decoder.h"
/*
* List of functions that will recognize files. These should correspond pretty
* directly to the metadata types
*/
#define INIT_VGMSTREAM_FCNS 13
#define INIT_VGMSTREAM_FCNS 14
VGMSTREAM * (*init_vgmstream_fcns[INIT_VGMSTREAM_FCNS])(const char * const) = {
init_vgmstream_adx,
init_vgmstream_brstm,
@ -49,6 +52,7 @@ VGMSTREAM * (*init_vgmstream_fcns[INIT_VGMSTREAM_FCNS])(const char * const) = {
init_vgmstream_ngc_dsp_std,
init_vgmstream_Cstr,
init_vgmstream_gcsw,
init_vgmstream_ps2_ads,
};
@ -192,6 +196,8 @@ int get_vgmstream_samples_per_frame(VGMSTREAM * vgmstream) {
return 1;
case coding_NGC_AFC:
return 16;
case coding_PSX:
return 28;
default:
return 0;
}
@ -225,6 +231,8 @@ int get_vgmstream_frame_size(VGMSTREAM * vgmstream) {
return 0;
case coding_NGC_AFC:
return 9;
case coding_PSX:
return 16;
default:
return 0;
}
@ -307,6 +315,13 @@ void decode_vgmstream(VGMSTREAM * vgmstream, int samples_written, int samples_to
samples_to_do);
}
break;
case coding_PSX:
for (chan=0;chan<vgmstream->channels;chan++) {
decode_psx(&vgmstream->ch[chan],buffer+samples_written*vgmstream->channels+chan,
vgmstream->channels,vgmstream->samples_into_block,
samples_to_do);
}
break;
}
}
@ -459,6 +474,9 @@ void describe_vgmstream(VGMSTREAM * vgmstream, char * desc, int length) {
case coding_NGC_AFC:
snprintf(temp,TEMPSIZE,"Gamecube \"AFC\" 4-bit ADPCM");
break;
case coding_PSX:
snprintf(temp,TEMPSIZE,"Playstation 4-bit ADPCM");
break;
default:
snprintf(temp,TEMPSIZE,"CANNOT DECODE");
}
@ -555,6 +573,9 @@ void describe_vgmstream(VGMSTREAM * vgmstream, char * desc, int length) {
case meta_GCSW:
snprintf(temp,TEMPSIZE,"GCSW header");
break;
case meta_PS2_SShd:
snprintf(temp,TEMPSIZE,"ADS File (with SShd header)");
break;
default:
snprintf(temp,TEMPSIZE,"THEY SHOULD HAVE SENT A POET");
}

View File

@ -22,6 +22,7 @@ typedef enum {
coding_NGC_DTK, /* NGC hardware disc ADPCM, called DTK, TRK or ADP */
coding_G721, /* CCITT G.721 ADPCM */
coding_NGC_AFC, /* NGC ADPCM, called AFC */
coding_PSX, /* PSX & PS2 ADPCM */
} coding_t;
/* The layout type specifies how the sound data is laid out in the file */
@ -69,6 +70,8 @@ typedef enum {
meta_RSF, /* Retro Studios RSF, no header (.rsf) */
meta_HALPST, /* HAL Labs HALPST */
meta_GCSW, /* GCSW (PCM) */
meta_PS2_SShd, /* .ADS with SShd header */
} meta_t;
typedef struct {

View File

@ -45,7 +45,7 @@ int fade_samples = 0;
#define EXTENSION_LIST_SIZE 1024
char working_extension_list[EXTENSION_LIST_SIZE] = {0};
#define EXTENSION_COUNT 11
#define EXTENSION_COUNT 13
char * extension_list[EXTENSION_COUNT] = {
"adx\0ADX Audio File (*.ADX)\0",
"afc\0AFC Audio File (*.AFC)\0",
@ -58,6 +58,8 @@ char * extension_list[EXTENSION_COUNT] = {
"rsf\0RSF Audio File (*.RSF)\0",
"dsp\0DSP Audio File (*.DSP)\0",
"gcw\0GCW Audio File (*.GCW)\0",
"ads\0PS2 ADS Audio File (*.ADS)\0",
"ss2\0PS2 SS2 Audio File (*.SS2)\0",
};
/* stubs, we don't do anything fancy yet */