Add RAGE aud MPEG [GTA IV (PS3)]

This commit is contained in:
bnnm 2024-05-03 13:45:59 +02:00
parent 5005788fb5
commit 88c5abba00
5 changed files with 124 additions and 66 deletions

View File

@ -519,7 +519,8 @@ int mpeg_get_sample_rate(mpeg_codec_data* data);
long mpeg_bytes_to_samples(long bytes, const mpeg_codec_data* data);
uint32_t mpeg_get_tag_size(STREAMFILE* sf, uint32_t offset, uint32_t header);
int mpeg_get_frame_info(STREAMFILE* sf, off_t offset, mpeg_frame_info* info);
bool mpeg_get_frame_info(STREAMFILE* sf, off_t offset, mpeg_frame_info* info);
bool mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info* info);
int test_ahx_key(STREAMFILE* sf, off_t offset, crikey_t* crikey);
#endif

View File

@ -8,7 +8,7 @@ int mpeg_custom_setup_init_default(STREAMFILE* sf, off_t start_offset, mpeg_code
/* get frame info at offset */
if ( !mpeg_get_frame_info(sf, start_offset, &info))
if (!mpeg_get_frame_info(sf, start_offset, &info))
goto fail;
switch(info.layer) {
case 1: *coding_type = coding_MPEG_layer1; break;
@ -264,7 +264,7 @@ fail:
* Gets info from a MPEG frame header at offset. Normally you would use mpg123_info but somehow
* it's wrong at times (maybe because we use an ancient version) so here we do our thing.
*/
static int mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info* info) {
bool mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info* info) {
/* index tables */
static const int versions[4] = { /* MPEG 2.5 */ 3, /* reserved */ -1, /* MPEG 2 */ 2, /* MPEG 1 */ 1 };
static const int layers[4] = { -1,3,2,1 };
@ -330,12 +330,12 @@ static int mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info* info) {
default: goto fail;
}
return 1;
return true;
fail:
return 0;
return false;
}
int mpeg_get_frame_info(STREAMFILE* sf, off_t offset, mpeg_frame_info* info) {
bool mpeg_get_frame_info(STREAMFILE* sf, off_t offset, mpeg_frame_info* info) {
uint32_t header = read_u32be(offset, sf);
return mpeg_get_frame_info_h(header, info);
}

View File

@ -113,9 +113,9 @@ fail:
/* Init custom MPEG, with given type and config */
mpeg_codec_data* init_mpeg_custom(STREAMFILE* sf, off_t start_offset, coding_t* coding_type, int channels, mpeg_custom_t type, mpeg_custom_config* config) {
mpeg_codec_data* init_mpeg_custom(STREAMFILE* sf, off_t start_offset, coding_t* coding_type, int channels, mpeg_custom_t type, mpeg_custom_config* cfg) {
mpeg_codec_data* data = NULL;
int i, ok;
int ok;
/* init codec */
data = calloc(1, sizeof(mpeg_codec_data));
@ -124,7 +124,8 @@ mpeg_codec_data* init_mpeg_custom(STREAMFILE* sf, off_t start_offset, coding_t*
/* keep around to decode */
data->custom = 1;
data->type = type;
memcpy(&data->config, config, sizeof(mpeg_custom_config));
if (cfg)
memcpy(&data->config, cfg, sizeof(mpeg_custom_config));
data->config.channels = channels;
data->default_buffer_size = MPEG_DATA_BUFFER_SIZE;
@ -161,7 +162,7 @@ mpeg_codec_data* init_mpeg_custom(STREAMFILE* sf, off_t start_offset, coding_t*
data->streams_size += 1;
data->streams = calloc(data->streams_size, sizeof(mpeg_custom_stream*));
for (i = 0; i < data->streams_size; i++) {
for (int i = 0; i < data->streams_size; i++) {
data->streams[i] = calloc(1, sizeof(mpeg_custom_stream));
data->streams[i]->m = init_mpg123_handle(); /* decoder not shared as may need several frames to decode)*/
if (!data->streams[i]->m) goto fail;

View File

@ -1,6 +1,7 @@
#include "meta.h"
#include "../coding/coding.h"
#include "../layout/layout.h"
#include "../util.h"
#include "../util/endianness.h"
#include "rage_aud_streamfile.h"
@ -22,7 +23,7 @@ typedef struct {
uint32_t stream_size;
} aud_header;
static int parse_aud_header(STREAMFILE* sf, aud_header* aud);
static bool parse_aud_header(STREAMFILE* sf, aud_header* aud);
static layered_layout_data* build_layered_rage_aud(STREAMFILE* sf, aud_header* aud);
@ -88,15 +89,14 @@ VGMSTREAM* init_vgmstream_rage_aud(STREAMFILE* sf) {
#ifdef VGM_USE_MPEG
case 0x0100: { /* MPEG (PS3) */
mpeg_custom_config cfg = {0};
if (aud.is_streamed) {
goto fail;
vgmstream->layout_data = build_layered_rage_aud(sf, &aud);
if (!vgmstream->layout_data) goto fail;
vgmstream->layout_type = layout_layered;
vgmstream->coding_type = coding_MPEG_custom;
}
else {
cfg.chunk_size = aud.block_chunk;
cfg.big_endian = aud.big_endian;
vgmstream->codec_data = init_mpeg_custom(sf, aud.stream_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_STANDARD, &cfg);
vgmstream->codec_data = init_mpeg_custom(sf, aud.stream_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_STANDARD, NULL);
if (!vgmstream->codec_data) goto fail;
vgmstream->layout_type = layout_none;
}
@ -126,7 +126,7 @@ fail:
}
/* Parse Rockstar's AUD header (much info from SparkIV). */
static int parse_aud_header(STREAMFILE* sf, aud_header* aud) {
static bool parse_aud_header(STREAMFILE* sf, aud_header* aud) {
int target_subsong = sf->stream_index;
read_u64_t read_u64;
read_u32_t read_u32;
@ -139,8 +139,8 @@ static int parse_aud_header(STREAMFILE* sf, aud_header* aud) {
read_u16 = aud->big_endian ? read_u16be : read_u16le;
uint64_t table_offset = read_u64(0x00, sf);
if (table_offset > 0x18000) /* typically 0x1c~0x1000, seen 0x17BE8 */
return 0;
if (table_offset > 0x20000 || table_offset < 0x1c) /* typically 0x1c~0x1000, seen ~0x19000 */
return false;
/* use bank's stream count to detect */
aud->is_streamed = (read_u32(0x10, sf) == 0);
@ -160,14 +160,14 @@ static int parse_aud_header(STREAMFILE* sf, aud_header* aud) {
aud->stream_offset = read_u32(0x2c, sf);
channel_info_offset = channel_table_offset + aud->channels * 0x10;
/* block count is off in rare XMA streams:
/* block count is off in rare XMA streams, though we only need it to check the header:
* GTA4 - Header says 2 blocks, actually has 3 - EP1_SFX/RP03_ML
* MCLA - Header says 3 blocks, actually has 4 - AUDIO/X360/SFX/AMBIENCE_STREAM/AMB_QUADSHOT_MALL_ADVERT_09
*/
uint32_t expected_size = aud->block_count * aud->block_chunk + aud->stream_offset;
if (expected_size != get_streamfile_size(sf) && expected_size + aud->block_chunk != get_streamfile_size(sf)) {
VGM_LOG("RAGE AUD: bad file size\n");
goto fail;
//;VGM_LOG("RAGE AUD: bad file size\n");
return false;
}
/* channel table (one entry per channel, points to channel info) */
@ -186,7 +186,8 @@ static int parse_aud_header(STREAMFILE* sf, aud_header* aud) {
aud->codec = read_u32(channel_info_offset + 0x1c, sf);
/* (when codec is IMA) */
/* 0x20(8): adpcm states offset, 0x38: num states? (reference for seeks?) */
/* rest: unknown data */
/* rest: unknown data (varies per codec?) */
/* in MC:LA there is samples-per-frame and frame size table for MPEG */
/* block table (one entry per block) */
/* 0x00: data size processed up to this block (doesn't count block padding) */
@ -207,11 +208,11 @@ static int parse_aud_header(STREAMFILE* sf, aud_header* aud) {
/* 0x14(4): unknown */
aud->stream_offset = read_u32(0x18, sf); /* base start_offset */
if (target_subsong == 0) target_subsong = 1;
if (target_subsong < 0 || target_subsong > aud->total_subsongs || aud->total_subsongs < 1) goto fail;
if (stream_table_offset != 0x1c)
goto fail;
return false;
if (!check_subsongs(&target_subsong, aud->total_subsongs))
return false;
stream_info_offset = stream_table_offset + 0x10 * aud->total_subsongs;
/* stream table (one entry per stream, points to stream info) */
@ -234,13 +235,13 @@ static int parse_aud_header(STREAMFILE* sf, aud_header* aud) {
/* 0x20(8): adpcm states offset, 0x38: num states? (reference for seeks?) */
/* rest: unknown data */
/* GTA IV PS3's 0x7D27B1E8' #165 and #166 seem to point to the same wrong offset pointing to the middle of data
* (original bug/unused? maybe should allow MPEG resync?) */
aud->channels = 1;
}
return 1;
fail:
return 0;
return true;
}
/* ************************************************************************* */
@ -279,11 +280,20 @@ static VGMSTREAM* build_blocks_vgmstream(STREAMFILE* sf, aud_header* aud, int ch
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
//TODO
//xma_fix_raw_samples(vgmstream, temp_sf, substream_offset, substream_size, 0, 0,0); /* samples are ok? */
break;
}
#endif
#ifdef VGM_USE_MPEG
case 0x0100: { /* MPEG (PS3) */
vgmstream->codec_data = init_mpeg_custom(temp_sf, substream_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_STANDARD, NULL);
if (!vgmstream->codec_data) goto fail;
vgmstream->layout_type = layout_none;
break;
}
#endif
default:
goto fail;
}
@ -293,7 +303,7 @@ static VGMSTREAM* build_blocks_vgmstream(STREAMFILE* sf, aud_header* aud, int ch
close_streamfile(temp_sf);
return vgmstream;
fail:
;VGM_LOG("AUD: can't open decoder\n");
;VGM_LOG("RAGE AUD: can't open decoder\n");
close_vgmstream(vgmstream);
close_streamfile(temp_sf);
return NULL;

View File

@ -3,27 +3,30 @@
#include "deblock_streamfile.h"
#include "../util/endianness.h"
#include "../util/log.h"
#include "../coding/coding.h"
#define RAGE_AUD_MAX_MUSIC_CHANNELS 7 /* max known */
#define RAGE_AUD_MAX_MUSIC_CHANNELS 7 /* known max */
#define RAGE_AUD_FRAME_SIZE 0x800
/* ************************************************************************* */
typedef struct {
int start_entry; /* innacurate! */
int start_entry;
int entries;
int32_t channel_skip;
int32_t channel_samples;
uint32_t channel_size; /* size of this channel's data (not including padding) */
uint32_t frame_size;
/* derived */
uint32_t chunk_start; /* relative to block offset */
uint32_t chunk_size; /* size of this channel's data (not including padding) */
uint32_t chunk_start; /* relative to block offset */
uint32_t chunk_size; /* size of this channel's data (may include padding) */
} rage_aud_block_t;
typedef struct {
int big_endian;
uint8_t codec;
int codec;
int channels;
uint32_t block_offset;
uint32_t header_size;
@ -36,26 +39,30 @@ typedef struct {
* - ...
* - frames from channel N
* - usually there is padding between channels or blocks
*
*
* Header format:
* - base header (for all channels)
* 0x00: seek info offset (within block)
* 0x08: seek table offset
* 0x10: seek table offset
* - channel info (per channel)
* 0x00: start entry for that channel?
* may be off by +1/+2?
* 0x04: frames in this channel (may be different between channels)
* 0x08: samples to discard in the beginning of this block (MPEG/XMA2)
* this seems to repeat XMA frames, which decoders don't like, so maybe they just skip whole frames
* 0x00: start entry/frame for that channel
* Sometimes a channel has N frames while next channel start_entry N+1/2, meaning last frames will be blank/padding
* 0x04: entries/frames in this channel (may be different between channels)
* This refers to 1 logical chunk of N sub-frames (XMA1=single XMA super-frame, MPEG=N VBR frames).
* May be partially/fully blank in MPEG and sizes/paddings aren't very consistent even in similar files (MA:LA's HANGOUT_CROWD_*).
* 0x08: samples to discard in the beginning of this block? (MPEG/XMA2)
* When this is set, channel repeats XMA/MPEG super-frames from prev block. However discard doesn't seem to match
* repeated samples, and skip value may be very small too (ex. just 4 skip samples but repeats make 8416 samples).
* So maybe they just skip data like we do below and don't actually use this value.
* 0x0c: samples in channel without discard? (for MPEG/XMA2 can vary between channels)
* (next fields only exists for MPEG)
* 0x10: close to number of frames but varies a bit?
* 0x14: channel chunk size (not counting padding)
* - seek table (entries for all channels)
* 0x10: close to number of VBR frames but varies a bit?
* 0x14: channel data size (not including padding between channels)
* - seek table (entries for all channels, 1 per frame)
* 0x00: start?
* 0x04: end?
* - padding until this block's end
* - padding up to data start
*/
static bool read_rage_aud_block(STREAMFILE* sf, rage_aud_block_info_t* bi) {
read_s32_t read_s32 = bi->big_endian ? read_s32be : read_s32le;
@ -63,6 +70,7 @@ static bool read_rage_aud_block(STREAMFILE* sf, rage_aud_block_info_t* bi) {
uint32_t channel_entry_size, seek_entry_size;
uint32_t offset = bi->block_offset;
int channels = bi->channels;
/* read stupid block crap + derived info at once so hopefully it's a bit easier to understand */
switch(bi->codec) {
@ -70,8 +78,13 @@ static bool read_rage_aud_block(STREAMFILE* sf, rage_aud_block_info_t* bi) {
channel_entry_size = 0x10;
seek_entry_size = 0x08;
break;
case 0x0100: /* MPEG */
channel_entry_size = 0x18;
seek_entry_size = 0x08;
break;
default:
goto fail;
VGM_LOG("RAGE AUD: unknown codec %x\n", bi->codec);
return false;
}
/* base header */
@ -85,7 +98,9 @@ static bool read_rage_aud_block(STREAMFILE* sf, rage_aud_block_info_t* bi) {
bi->blk[ch].entries = read_s32(offset + 0x04, sf);
bi->blk[ch].channel_skip = read_s32(offset + 0x08, sf);
bi->blk[ch].channel_samples = read_s32(offset + 0x0c, sf);
/* others: optional */
if (bi->codec == 0x0100) { /* MPEG */
bi->blk[ch].channel_size = read_s32(offset + 0x14, sf);
}
offset += channel_entry_size;
}
@ -97,15 +112,15 @@ static bool read_rage_aud_block(STREAMFILE* sf, rage_aud_block_info_t* bi) {
/* derived info */
for (int ch = 0; ch < channels; ch++) {
bi->blk[ch].frame_size = RAGE_AUD_FRAME_SIZE; /* XMA1 super-frame or MPEG chunk of N VBR frames */
bi->blk[ch].chunk_size = bi->blk[ch].entries * bi->blk[ch].frame_size; /* full size between channels, may be padded */
switch(bi->codec) {
case 0x0000: /* XMA1 */
bi->blk[ch].frame_size = 0x800;
bi->blk[ch].chunk_size = bi->blk[ch].entries * bi->blk[ch].frame_size;
bi->blk[ch].channel_size = bi->blk[ch].chunk_size; /* no padding */
break;
/* in MPEG frames seem to be VBR, so would need channel chunk size */
default:
goto fail;
break;
}
}
@ -115,20 +130,22 @@ static bool read_rage_aud_block(STREAMFILE* sf, rage_aud_block_info_t* bi) {
* a table as big as prev blocks, repeating old values for unused entries, so final header size is consistent */
if (!bi->header_size)
bi->header_size = offset - bi->block_offset;
offset = bi->block_offset + align_size_to_block(bi->header_size, 0x800);
offset = bi->block_offset + align_size_to_block(bi->header_size, RAGE_AUD_FRAME_SIZE);
}
/* set frame starts per channel */
uint32_t header_chunk = offset - bi->block_offset;
for (int ch = 0; ch < channels; ch++) {
bi->blk[ch].chunk_start = offset - bi->block_offset;
offset += bi->blk[ch].chunk_size;
bi->blk[ch].chunk_start = header_chunk + bi->blk[ch].start_entry * bi->blk[ch].frame_size;
/* unlike AWC there may be unknown padding between channels, so needs start_entry to calc offset */
//bi->blk[ch].chunk_start = offset - bi->block_offset;
//offset += bi->blk[ch].chunk_size;
}
/* beyond this is padding until chunk_start */
return true;
fail:
return false;
}
/* Find data that repeats in the beginning of a new block at the end of last block.
@ -138,15 +155,41 @@ static uint32_t get_block_repeated_size(STREAMFILE* sf, rage_aud_block_info_t* b
if (bi->blk[channel].channel_skip == 0)
return 0;
if (bi->block_offset >= get_streamfile_size(sf))
return 0;
switch(bi->codec) {
case 0x0000: { /* XMA1 */
/* when data repeats seems to clone the last (super-)frame */
/* when data repeats seems to clone the last super-frame */
return bi->blk[channel].frame_size;
}
case 0x0100: { /* MPEG */
/* first super-frame will repeat N VBR old sub-frames, without crossing frame_size.
* ex. repeated frames' size could be set to 0x774 (7 sub-frames) if adding 1 more would take >0x800.
* After last sub-frame there may be padding up to frame_size (GTA4 only?). */
uint8_t frame[RAGE_AUD_FRAME_SIZE];
uint32_t offset = bi->block_offset + bi->blk[channel].chunk_start;
read_streamfile(frame, offset, sizeof(frame), sf);
/* read sub-frames until padding or end */
int skip_size = 0x00;
while (skip_size < sizeof(frame) - 0x04) {
if (frame[skip_size] == 0x00) /* padding found */
return RAGE_AUD_FRAME_SIZE;
mpeg_frame_info info = {0};
uint32_t header = get_u32be(frame + skip_size);
if (!mpeg_get_frame_info_h(header, &info)) /* ? */
return RAGE_AUD_FRAME_SIZE;
if (skip_size + info.frame_size > sizeof(frame)) /* not a repeated frame */
return skip_size;
skip_size += info.frame_size;
}
return skip_size; /* skip_size fills frame size */
}
default:
;VGM_LOG("RAGE_AUD: found channel skip in codec %x\n", bi->codec); /* not seen */
return 0;
@ -165,6 +208,9 @@ static void block_callback(STREAMFILE *sf, deblock_io_data* data) {
bi.codec = data->cfg.track_type;
bi.header_size = data->cfg.config;
if (bi.block_offset >= get_streamfile_size(sf))
return;
if (!read_rage_aud_block(sf, &bi))
return; //???
data->cfg.config = bi.header_size; /* fixed for all blocks but calc'd on first one */
@ -173,11 +219,11 @@ static void block_callback(STREAMFILE *sf, deblock_io_data* data) {
data->block_size = data->cfg.chunk_size;
data->skip_size = bi.blk[channel].chunk_start + repeat_size;
data->data_size = bi.blk[channel].chunk_size - repeat_size;
data->data_size = bi.blk[channel].channel_size - repeat_size;
}
/* deblocks RAGE_AUD blocks */
static STREAMFILE* setup_rage_aud_streamfile(STREAMFILE* sf, uint32_t stream_offset, uint32_t stream_size, uint32_t block_size, int channels, int channel, uint8_t codec, int big_endian) {
static STREAMFILE* setup_rage_aud_streamfile(STREAMFILE* sf, uint32_t stream_offset, uint32_t stream_size, uint32_t block_size, int channels, int channel, int codec, bool big_endian) {
STREAMFILE* new_sf = NULL;
deblock_config_t cfg = {0};