cleanup: get/put

This commit is contained in:
bnnm 2024-01-07 16:57:26 +01:00
parent 08e3ae4872
commit 5231825ddb
11 changed files with 110 additions and 118 deletions

View File

@ -1051,7 +1051,7 @@ static size_t make_wav_header(uint8_t* buf, size_t buf_size, int32_t sample_coun
goto fail;
memcpy(buf+0x00, "RIFF", 0x04); /* RIFF header */
put_32bitLE(buf+0x04, (int32_t)(header_size - 0x08 + data_size)); /* size of RIFF */
put_u32le(buf+0x04, (int32_t)(header_size - 0x08 + data_size)); /* size of RIFF */
memcpy(buf+0x08, "WAVE", 4); /* WAVE header */

View File

@ -33,7 +33,7 @@ atrac9_codec_data* init_atrac9(atrac9_config* cfg) {
data->handle = Atrac9GetHandle();
if (!data->handle) goto fail;
put_32bitBE(config_data, cfg->config_data);
put_u32be(config_data, cfg->config_data);
status = Atrac9InitDecoder(data->handle, config_data);
if (status < 0) goto fail;
@ -150,7 +150,7 @@ void reset_atrac9(atrac9_codec_data* data) {
data->handle = Atrac9GetHandle();
if (!data->handle) goto fail;
put_32bitBE(config_data, data->config.config_data);
put_u32be(config_data, data->config.config_data);
status = Atrac9InitDecoder(data->handle, config_data);
if (status < 0) goto fail;
}

View File

@ -33,7 +33,7 @@ static const float xa_coefs[16][2] = {
void decode_ea_xas_v1(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int channel) {
uint8_t frame[0x4c] = {0};
off_t frame_offset;
int group, row, i, samples_done = 0, sample_count = 0;
int samples_done = 0, sample_count = 0;
size_t bytes_per_frame, samples_per_frame;
@ -55,11 +55,11 @@ void decode_ea_xas_v1(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspa
/* parse group headers */
for (group = 0; group < 4; group++) {
for (int group = 0; group < 4; group++) {
float coef1, coef2;
int16_t hist1, hist2;
uint8_t shift;
uint32_t group_header = (uint32_t)get_32bitLE(frame + group*0x4); /* always LE */
uint32_t group_header = get_u32le(frame + group*0x4); /* always LE */
coef1 = xa_coefs[group_header & 0x0F][0];
coef2 = xa_coefs[group_header & 0x0F][1];
@ -80,8 +80,8 @@ void decode_ea_xas_v1(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspa
sample_count++;
/* process nibbles per group */
for (row = 0; row < 15; row++) {
for (i = 0; i < 1*2; i++) {
for (int row = 0; row < 15; row++) {
for (int i = 0; i < 1 * 2; i++) {
uint8_t nibbles = frame[4*4 + row*0x04 + group + i/2];
int sample;
@ -116,7 +116,7 @@ void decode_ea_xas_v1(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspa
void decode_ea_xas_v0(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do) {
uint8_t frame[0x13] = {0};
off_t frame_offset;
int i, frames_in, samples_done = 0, sample_count = 0;
int frames_in, samples_done = 0, sample_count = 0;
size_t bytes_per_frame, samples_per_frame;
@ -129,14 +129,14 @@ void decode_ea_xas_v0(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspa
frame_offset = stream->offset + bytes_per_frame * frames_in;
read_streamfile(frame, frame_offset, bytes_per_frame, stream->streamfile); /* ignore EOF errors */
//todo see above
//TODO make expand function and fuse with above
/* process frame */
{
float coef1, coef2;
int16_t hist1, hist2;
uint8_t shift;
uint32_t frame_header = (uint32_t)get_32bitLE(frame); /* always LE */
uint32_t frame_header = get_u32le(frame); /* always LE */
coef1 = xa_coefs[frame_header & 0x0F][0];
coef2 = xa_coefs[frame_header & 0x0F][1];
@ -157,7 +157,7 @@ void decode_ea_xas_v0(VGMSTREAMCHANNEL * stream, sample * outbuf, int channelspa
sample_count++;
/* process nibbles */
for (i = 0; i < 0x0f*2; i++) {
for (int i = 0; i < 0x0f * 2; i++) {
uint8_t nibbles = frame[0x02 + 0x02 + i/2];
int sample;

View File

@ -668,7 +668,7 @@ static void ealayer3_copy_pcm_block(uint8_t* outbuf, off_t pcm_offset, int pcm_n
int pos = 0;
for (i = 0; i < pcm_number * channels_per_frame; i++) {
int16_t pcm_sample = get_s16be(pcm_block + pos);
put_16bitLE(outbuf + pos, pcm_sample);
put_s16le(outbuf + pos, pcm_sample);
pos += sizeof(sample);
}
@ -680,7 +680,7 @@ static void ealayer3_copy_pcm_block(uint8_t* outbuf, off_t pcm_offset, int pcm_n
int put_pos = sizeof(sample) * ch;
for (i = 0; i < pcm_number; i++) {
int16_t pcm_sample = get_s16be(pcm_block + get_pos);
put_16bitLE(outbuf + put_pos, pcm_sample);
put_s16le(outbuf + put_pos, pcm_sample);
get_pos += sizeof(sample);
put_pos += sizeof(sample) * channels_per_frame;

View File

@ -135,8 +135,8 @@ static int eamp3_write_pcm_block(VGMSTREAMCHANNEL *stream, mpeg_codec_data *data
/* read + write PCM block samples (always LE) */
for (i = 0; i < eaf->pcm_number * data->channels_per_frame; i++) {
off_t pcm_offset = stream->offset + eaf->pre_size + eaf->mpeg_size + sizeof(sample)*i;
int16_t pcm_sample = read_16bitLE(pcm_offset,stream->streamfile);
put_16bitLE(ms->output_buffer + bytes_filled + sizeof(sample)*i, pcm_sample);
int16_t pcm_sample = read_s16le(pcm_offset,stream->streamfile);
put_s16le(ms->output_buffer + bytes_filled + sizeof(sample) * i, pcm_sample);
}
ms->samples_filled += eaf->pcm_number;

View File

@ -175,8 +175,8 @@ static int build_header(uint8_t* buf, size_t bufsize, STREAMFILE* sf, off_t pack
if (0x07+packet_size-0x03 > bufsize) return 0;
put_8bit (buf+0x00, read_8bit(packet_offset,sf)); /* packet_type */
memcpy (buf+0x01, "vorbis", 6); /* id */
put_u8(buf+0x00, read_8bit(packet_offset,sf)); /* packet_type */
memcpy(buf+0x01, "vorbis", 6); /* id */
bytes = read_streamfile(buf+0x07,packet_offset+0x03, packet_size-0x03,sf); /* copy rest (all except id+"SK") */
if (packet_size-0x03 != bytes)
return 0;

View File

@ -740,8 +740,8 @@ static int ww2ogg_generate_vorbis_setup(bitstream_t* ow, bitstream_t* iw, vorbis
/* packet header */
put_8bit(ow->buf+0x00, 0x05); /* packet_type (setup) */
memcpy (ow->buf+0x01, "vorbis", 6); /* id */
put_u8(ow->buf+0x00, 0x05); /* packet_type (setup) */
memcpy(ow->buf+0x01, "vorbis", 6); /* id */
ow->b_off += (1+6) * 8; /* bit offset of output (Vorbis) setup, after fake type + id */
@ -1233,12 +1233,12 @@ static int load_wvc_array(uint8_t* buf, size_t bufsize, uint32_t codebook_id, ww
int codebook_count;
/* at the end of the WVC is an offset table, and we need to find codebook id (number) offset */
table_start = get_32bitLE(wvc + wvc_size - 4); /* last offset */
table_start = get_u32le(wvc + wvc_size - 4); /* last offset */
codebook_count = ((wvc_size - table_start) / 4) - 1;
if (codebook_id >= codebook_count) goto fail;
codebook_offset = get_32bitLE(wvc + table_start + codebook_id*4);
codebook_size = get_32bitLE(wvc + table_start + codebook_id*4 + 4) - codebook_offset;
codebook_offset = get_u32le(wvc + table_start + codebook_id*4);
codebook_size = get_u32le(wvc + table_start + codebook_id*4 + 4) - codebook_offset;
if (codebook_size > bufsize) goto fail;
memcpy(buf, wvc+codebook_offset, codebook_size);

View File

@ -1143,7 +1143,7 @@ static const meta_info meta_info_list[] = {
{meta_ADS_MIDWAY, "Midway ADS header"},
{meta_PS2_MCG, "Gunvari MCG Header"},
{meta_ZSD, "Konami ZSD header"},
{meta_REDSPARK, "RedSpark Header"},
{meta_REDSPARK, "RedSpark header"},
{meta_IVAUD, "Rockstar .ivaud header"},
{meta_DSP_WII_WSD, ".WSD header"},
{meta_WII_NDP, "Icon Games NDP header"},

View File

@ -2,8 +2,8 @@
#include "../coding/coding.h"
/* Apple Core Audio Format File - from iOS games [Vectros (iOS), Ridge Racer Accelerated (iOS)] */
VGMSTREAM * init_vgmstream_apple_caff(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
VGMSTREAM* init_vgmstream_apple_caff(STREAMFILE* sf) {
VGMSTREAM* vgmstream = NULL;
off_t start_offset = 0, chunk_offset;
size_t file_size, data_size = 0;
int loop_flag, channel_count = 0, sample_rate = 0;
@ -15,20 +15,19 @@ VGMSTREAM * init_vgmstream_apple_caff(STREAMFILE *streamFile) {
/* checks */
if (!check_extensions(streamFile, "caf"))
goto fail;
if (!is_id32be(0x00,sf, "caff"))
return NULL;
if (read_32bitBE(0x04,sf) != 0x00010000) /* version/flags */
return NULL;
if (!check_extensions(sf, "caf"))
return NULL;
if (read_32bitBE(0x00,streamFile) != 0x63616666) /* "caff" */
goto fail;
if (read_32bitBE(0x04,streamFile) != 0x00010000) /* version/flags */
goto fail;
file_size = get_streamfile_size(streamFile);
file_size = get_streamfile_size(sf);
chunk_offset = 0x08;
while (chunk_offset < file_size) {
uint32_t chunk_type = read_32bitBE(chunk_offset+0x00,streamFile);
uint32_t chunk_size = (uint32_t)read_64bitBE(chunk_offset+0x04,streamFile);
uint32_t chunk_type = read_u32be(chunk_offset+0x00,sf);
uint32_t chunk_size = (uint32_t)read_u64be(chunk_offset+0x04,sf);
chunk_offset += 0x0c;
switch (chunk_type) {
@ -37,26 +36,26 @@ VGMSTREAM * init_vgmstream_apple_caff(STREAMFILE *streamFile) {
found_desc = 1;
{
uint64_t sample_long = (uint64_t)read_64bitBE(chunk_offset+0x00, streamFile);
uint64_t sample_long = read_u64be(chunk_offset+0x00, sf);
double* sample_double; /* double sample rate, double the fun */
sample_double = (double*)&sample_long;
sample_rate = (int)(*sample_double);
}
codec = read_32bitBE(chunk_offset+0x08, streamFile);
codec = read_32bitBE(chunk_offset+0x08, sf);
//codec_flags = read_32bitBE(chunk_offset+0x0c, streamFile);
bytes_per_packet = read_32bitBE(chunk_offset+0x10, streamFile);
samples_per_packet = read_32bitBE(chunk_offset+0x14, streamFile);
channels_per_packet = read_32bitBE(chunk_offset+0x18, streamFile);
bits_per_sample = read_32bitBE(chunk_offset+0x1C, streamFile);
bytes_per_packet = read_32bitBE(chunk_offset+0x10, sf);
samples_per_packet = read_32bitBE(chunk_offset+0x14, sf);
channels_per_packet = read_32bitBE(chunk_offset+0x18, sf);
bits_per_sample = read_32bitBE(chunk_offset+0x1C, sf);
break;
case 0x70616b74: /* "pakt" */
//found_pakt = 1;
//packets_table_size = (uint32_t)read_64bitBE(chunk_offset+0x00,streamFile); /* 0 for constant bitrate */
valid_samples = (uint32_t)read_64bitBE(chunk_offset+0x08,streamFile);
//packets_table_size = (uint32_t)read_u64be(chunk_offset+0x00,streamFile); /* 0 for constant bitrate */
valid_samples = (uint32_t)read_u64be(chunk_offset+0x08,sf);
//priming_samples = read_32bitBE(chunk_offset+0x10,streamFile); /* encoder delay samples */
//unused_samples = read_32bitBE(chunk_offset+0x14,streamFile); /* footer samples */
break;
@ -91,8 +90,8 @@ VGMSTREAM * init_vgmstream_apple_caff(STREAMFILE *streamFile) {
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = sample_rate;
vgmstream->meta_type = meta_CAFF;
vgmstream->sample_rate = sample_rate;
switch(codec) {
case 0x6C70636D: /* "lpcm" */
@ -150,7 +149,7 @@ VGMSTREAM * init_vgmstream_apple_caff(STREAMFILE *streamFile) {
goto fail;
}
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
if (!vgmstream_open_stream(vgmstream,sf,start_offset))
goto fail;
return vgmstream;

View File

@ -1,66 +1,58 @@
#ifndef _BGW_STREAMFILE_H_
#define _BGW_STREAMFILE_H_
#include "../streamfile.h"
#define BGW_KEY_MAX (0xC0*2)
typedef struct {
uint8_t key[BGW_KEY_MAX];
size_t key_size;
} bgw_decryption_data;
/* Encrypted ATRAC3 info from Moogle Toolbox (https://sourceforge.net/projects/mogbox/) */
static size_t bgw_decryption_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, bgw_decryption_data* data) {
size_t bytes_read;
int i;
bytes_read = streamfile->read(streamfile, dest, offset, length);
/* decrypt data (xor) */
for (i = 0; i < bytes_read; i++) {
dest[i] ^= data->key[(offset + i) % data->key_size];
}
//todo: a few files (music069.bgw, music071.bgw, music900.bgw) have the last frames unencrypted,
// though they are blank and encoder ignores wrongly decrypted frames and outputs blank samples as well
return bytes_read;
}
static STREAMFILE* setup_bgw_atrac3_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, size_t frame_size, int channels) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
bgw_decryption_data io_data = {0};
size_t io_data_size = sizeof(bgw_decryption_data);
int ch;
/* setup decryption with key (first frame + modified channel header) */
if (frame_size*channels == 0 || frame_size*channels > BGW_KEY_MAX) goto fail;
io_data.key_size = read_streamfile(io_data.key, subfile_offset, frame_size*channels, streamFile);
for (ch = 0; ch < channels; ch++) {
uint32_t xor = get_32bitBE(io_data.key + frame_size*ch);
put_32bitBE(io_data.key + frame_size*ch, xor ^ 0xA0024E9F);
}
/* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_clamp_streamfile(temp_streamFile, subfile_offset,subfile_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, bgw_decryption_read,NULL);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _BGW_STREAMFILE_H_ */
#ifndef _BGW_STREAMFILE_H_
#define _BGW_STREAMFILE_H_
#include "../streamfile.h"
#define BGW_KEY_MAX (0xC0 * 2)
typedef struct {
uint8_t key[BGW_KEY_MAX];
size_t key_size;
} bgw_decryption_data;
/* Encrypted ATRAC3 info from Moogle Toolbox (https://sourceforge.net/projects/mogbox/) */
static size_t bgw_decryption_read(STREAMFILE* sf, uint8_t* dest, off_t offset, size_t length, bgw_decryption_data* data) {
size_t bytes_read = sf->read(sf, dest, offset, length);
/* decrypt data (xor) */
for (int i = 0; i < bytes_read; i++) {
dest[i] ^= data->key[(offset + i) % data->key_size];
}
//TODO: a few files (music069.bgw, music071.bgw, music900.bgw) have the last frames unencrypted,
// though they are blank and encoder ignores wrongly decrypted frames and outputs blank samples as well.
// Only in files that don't loop?
return bytes_read;
}
static STREAMFILE* setup_bgw_atrac3_streamfile(STREAMFILE* sf, off_t subfile_offset, size_t subfile_size, size_t frame_size, int channels) {
STREAMFILE* new_sf = NULL;
bgw_decryption_data io_data = {0};
size_t io_data_size = sizeof(bgw_decryption_data);
/* setup decryption with key (first frame + modified channel header) */
size_t key_size = frame_size * channels;
if (key_size <= 0 || key_size > BGW_KEY_MAX)
goto fail;
io_data.key_size = read_streamfile(io_data.key, subfile_offset, key_size, sf);
if (io_data.key_size != key_size)
goto fail;
for (int ch = 0; ch < channels; ch++) {
uint32_t xor = get_u32be(io_data.key + frame_size * ch);
put_u32be(io_data.key + frame_size * ch, xor ^ 0xA0024E9F);
}
/* setup subfile */
new_sf = open_wrap_streamfile_f(sf);
new_sf = open_clamp_streamfile_f(new_sf, subfile_offset,subfile_size);
new_sf = open_io_streamfile_f(new_sf, &io_data,io_data_size, bgw_decryption_read,NULL);
return new_sf;
fail:
close_streamfile(new_sf);
return NULL;
}
#endif

View File

@ -1,6 +1,7 @@
#ifndef _READER_SF_H
#define _READER_SF_H
#include "../streamfile.h"
#include "reader_get.h"
/* Sometimes you just need an int, and we're doing the buffering.
@ -10,37 +11,37 @@ static inline int16_t read_16bitLE(off_t offset, STREAMFILE* sf) {
uint8_t buf[2];
if (read_streamfile(buf,offset,2,sf)!=2) return -1;
return get_16bitLE(buf);
return get_s16le(buf);
}
static inline int16_t read_16bitBE(off_t offset, STREAMFILE* sf) {
uint8_t buf[2];
if (read_streamfile(buf,offset,2,sf)!=2) return -1;
return get_16bitBE(buf);
return get_s16be(buf);
}
static inline int32_t read_32bitLE(off_t offset, STREAMFILE* sf) {
uint8_t buf[4];
if (read_streamfile(buf,offset,4,sf)!=4) return -1;
return get_32bitLE(buf);
return get_s32le(buf);
}
static inline int32_t read_32bitBE(off_t offset, STREAMFILE* sf) {
uint8_t buf[4];
if (read_streamfile(buf,offset,4,sf)!=4) return -1;
return get_32bitBE(buf);
return get_s32be(buf);
}
static inline int64_t read_64bitLE(off_t offset, STREAMFILE* sf) {
uint8_t buf[8];
if (read_streamfile(buf,offset,8,sf)!=8) return -1;
return get_64bitLE(buf);
return get_s64le(buf);
}
static inline int64_t read_64bitBE(off_t offset, STREAMFILE* sf) {
uint8_t buf[8];
if (read_streamfile(buf,offset,8,sf)!=8) return -1;
return get_64bitBE(buf);
return get_s64be(buf);
}
static inline int8_t read_8bit(off_t offset, STREAMFILE* sf) {
uint8_t buf[1];