If you have an issue with any of our projects. feel free to register.

Commit bb38853e authored by Christopher Snowhill's avatar Christopher Snowhill

Updated VGMStream to r1050-3272-g842171b8

parent 6a534ad9
......@@ -5,17 +5,17 @@
/* libacm 1.2 (despite what libacm.h says) from: https://github.com/markokr/libacm */
typedef struct {
STREAMFILE *streamfile;
STREAMFILE* streamfile; /* reference */
int offset;
} acm_io_config;
static int acm_read_streamfile(void *ptr, int size, int n, void *arg);
static int acm_seek_streamfile(void *arg, int offset, int whence);
static int acm_get_length_streamfile(void *arg);
acm_codec_data *init_acm(STREAMFILE *streamFile, int force_channel_number) {
static int acm_read_streamfile(void* ptr, int size, int n, void* arg);
static int acm_seek_streamfile(void* arg, int offset, int whence);
static int acm_get_length_streamfile(void* arg);
acm_codec_data* init_acm(STREAMFILE* sf, int force_channel_number) {
acm_codec_data* data = NULL;
char filename[PATH_LIMIT];
data = calloc(1,sizeof(acm_codec_data));
......@@ -24,15 +24,14 @@ acm_codec_data *init_acm(STREAMFILE *streamFile, int force_channel_number) {
data->io_config = calloc(1,sizeof(acm_io_config));
if (!data->io_config) goto fail;
streamFile->get_name(streamFile,filename,sizeof(filename));
data->streamfile = open_streamfile(streamFile,filename);
data->streamfile = reopen_streamfile(sf, 0);
if (!data->streamfile) goto fail;
/* Setup libacm decoder, needs read callbacks and a parameter for said callbacks */
{
ACMStream *handle = NULL;
ACMStream* handle = NULL;
int res;
acm_io_config *io_config = data->io_config;
acm_io_config* io_config = data->io_config;
acm_io_callbacks io_callbacks = {0};
io_config->offset = 0;
......@@ -60,8 +59,8 @@ fail:
return NULL;
}
void decode_acm(acm_codec_data *data, sample * outbuf, int32_t samples_to_do, int channelspacing) {
ACMStream * acm = data->handle;
void decode_acm(acm_codec_data* data, sample_t* outbuf, int32_t samples_to_do, int channelspacing) {
ACMStream* acm = data->handle;
int32_t samples_read = 0;
while (samples_read < samples_to_do) {
......@@ -79,14 +78,14 @@ void decode_acm(acm_codec_data *data, sample * outbuf, int32_t samples_to_do, in
}
}
void reset_acm(acm_codec_data *data) {
void reset_acm(acm_codec_data* data) {
if (!data || !data->handle)
return;
acm_seek_pcm(data->handle, 0);
}
void free_acm(acm_codec_data *data) {
void free_acm(acm_codec_data* data) {
if (!data)
return;
......@@ -96,6 +95,11 @@ void free_acm(acm_codec_data *data) {
free(data);
}
STREAMFILE* acm_get_streamfile(acm_codec_data* data) {
if (!data) return NULL;
return data->streamfile;
}
/* ******************************* */
static int acm_read_streamfile(void *ptr, int size, int n, void *arg) {
......
#include "coding.h"
#include "../util.h"
void decode_adx(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int32_t frame_size, coding_t coding_type) {
void decode_adx(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do, int32_t frame_size, coding_t coding_type) {
uint8_t frame[0x12] = {0};
off_t frame_offset;
int i, frames_in, sample_count = 0;
......@@ -21,12 +21,22 @@ void decode_adx(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing
frame_offset = stream->offset + bytes_per_frame * frames_in;
read_streamfile(frame, frame_offset, bytes_per_frame, stream->streamfile); /* ignore EOF errors */
scale = get_16bitBE(frame+0x00);
scale = get_s16be(frame+0x00);
switch(coding_type) {
case coding_CRI_ADX:
scale = scale + 1;
coef1 = stream->adpcm_coef[0];
coef2 = stream->adpcm_coef[1];
/* Detect EOF scale (0x8001) found in some ADX of any type, signals "stop decoding" (without this frame?).
* Normally num_samples stops right before it, but ADXPLAY will honor it even in the middle on a file
* (may repeat last sample buffer). Some Baroque (SAT) videos set it on file end, but num_samples goes beyond.
* Just the upper bit triggers it even in encrypted ADX (max is 0x7FFF), but the check only here just in case. */
if (frame[0] == 0x80 && frame[1] == 0x01) {
scale = 0; /* fix scaled click, maybe should just exit */
VGM_LOG("ADX: reached EOF scale\n");
}
break;
case coding_CRI_ADX_exp:
scale = 1 << (12 - scale);
......@@ -79,6 +89,6 @@ void decode_adx(VGMSTREAMCHANNEL * stream, sample_t * outbuf, int channelspacing
}
}
void adx_next_key(VGMSTREAMCHANNEL * stream) {
void adx_next_key(VGMSTREAMCHANNEL* stream) {
stream->adx_xor = (stream->adx_xor * stream->adx_mult + stream->adx_add) & 0x7fff;
}
......@@ -4,6 +4,14 @@
#ifdef VGM_USE_MAIATRAC3PLUS
#include "maiatrac3plus.h"
struct maiatrac3plus_codec_data {
sample_t* buffer;
int channels;
int samples_discard;
void* handle;
};
maiatrac3plus_codec_data *init_at3plus() {
maiatrac3plus_codec_data *data = malloc(sizeof(maiatrac3plus_codec_data));
......
#include "coding.h"
#ifdef VGM_USE_ATRAC9
#ifdef __MACOSX__
#include <libatrac9/libatrac9.h>
#else
#include "libatrac9.h"
#endif
#include "libatrac9/libatrac9.h"
/* opaque struct */
struct atrac9_codec_data {
uint8_t *data_buffer;
uint8_t* data_buffer;
size_t data_buffer_size;
sample_t *sample_buffer;
sample_t* sample_buffer;
size_t samples_filled; /* number of samples in the buffer */
size_t samples_used; /* number of samples extracted from the buffer */
......@@ -21,15 +17,15 @@ struct atrac9_codec_data {
atrac9_config config;
void *handle; /* decoder handle */
void* handle; /* decoder handle */
Atrac9CodecInfo info; /* decoder info */
};
atrac9_codec_data *init_atrac9(atrac9_config *cfg) {
atrac9_codec_data* init_atrac9(atrac9_config* cfg) {
int status;
uint8_t config_data[4];
atrac9_codec_data *data = NULL;
atrac9_codec_data* data = NULL;
data = calloc(1, sizeof(atrac9_codec_data));
if (!data) goto fail;
......@@ -69,9 +65,9 @@ fail:
return NULL;
}
void decode_atrac9(VGMSTREAM *vgmstream, sample_t * outbuf, int32_t samples_to_do, int channels) {
VGMSTREAMCHANNEL *stream = &vgmstream->ch[0];
atrac9_codec_data * data = vgmstream->codec_data;
void decode_atrac9(VGMSTREAM* vgmstream, sample_t* outbuf, int32_t samples_to_do, int channels) {
VGMSTREAMCHANNEL* stream = &vgmstream->ch[0];
atrac9_codec_data* data = vgmstream->codec_data;
int samples_done = 0;
......@@ -138,8 +134,7 @@ decode_fail:
memset(outbuf + samples_done * channels, 0, (samples_to_do - samples_done) * sizeof(sample) * channels);
}
void reset_atrac9(VGMSTREAM *vgmstream) {
atrac9_codec_data *data = vgmstream->codec_data;
void reset_atrac9(atrac9_codec_data* data) {
if (!data) return;
if (!data->handle)
......@@ -171,11 +166,11 @@ fail:
return; /* decode calls should fail... */
}
void seek_atrac9(VGMSTREAM *vgmstream, int32_t num_sample) {
atrac9_codec_data *data = vgmstream->codec_data;
void seek_atrac9(VGMSTREAM* vgmstream, int32_t num_sample) {
atrac9_codec_data* data = vgmstream->codec_data;
if (!data) return;
reset_atrac9(vgmstream);
reset_atrac9(data);
/* find closest offset to desired sample, and samples to discard after that offset to reach loop */
{
......@@ -216,7 +211,7 @@ void seek_atrac9(VGMSTREAM *vgmstream, int32_t num_sample) {
}
void free_atrac9(atrac9_codec_data *data) {
void free_atrac9(atrac9_codec_data* data) {
if (!data) return;
if (data->handle) Atrac9ReleaseHandle(data->handle);
......@@ -269,7 +264,7 @@ fail:
return 0;
}
size_t atrac9_bytes_to_samples(size_t bytes, atrac9_codec_data *data) {
size_t atrac9_bytes_to_samples(size_t bytes, atrac9_codec_data* data) {
return bytes / data->info.superframeSize * (data->info.frameSamples * data->info.framesInSuperframe);
}
......
......@@ -1012,152 +1012,6 @@ size_t aac_get_samples(STREAMFILE *streamFile, off_t start_offset, size_t bytes)
return frames * samples_per_frame;
}
/* ******************************************** */
/* BITSTREAM */
/* ******************************************** */
/* Read bits (max 32) from buf and update the bit offset. Vorbis packs values in LSB order and byte by byte.
* (ex. from 2 bytes 00100111 00000001 we can could read 4b=0111 and 6b=010010, 6b=remainder (second value is split into the 2nd byte) */
static int r_bits_vorbis(vgm_bitstream * ib, int num_bits, uint32_t * value) {
off_t off, pos;
int i, bit_buf, bit_val;
if (num_bits == 0) return 1;
if (num_bits > 32 || num_bits < 0 || ib->b_off + num_bits > ib->bufsize*8) goto fail;
*value = 0; /* set all bits to 0 */
off = ib->b_off / 8; /* byte offset */
pos = ib->b_off % 8; /* bit sub-offset */
for (i = 0; i < num_bits; i++) {
bit_buf = (1U << pos) & 0xFF; /* bit check for buf */
bit_val = (1U << i); /* bit to set in value */
if (ib->buf[off] & bit_buf) /* is bit in buf set? */
*value |= bit_val; /* set bit */
pos++; /* new byte starts */
if (pos%8 == 0) {
pos = 0;
off++;
}
}
ib->b_off += num_bits;
return 1;
fail:
return 0;
}
/* Write bits (max 32) to buf and update the bit offset. Vorbis packs values in LSB order and byte by byte.
* (ex. writing 1101011010 from b_off 2 we get 01101011 00001101 (value split, and 11 in the first byte skipped)*/
static int w_bits_vorbis(vgm_bitstream * ob, int num_bits, uint32_t value) {
off_t off, pos;
int i, bit_val, bit_buf;
if (num_bits == 0) return 1;
if (num_bits > 32 || num_bits < 0 || ob->b_off + num_bits > ob->bufsize*8) goto fail;
off = ob->b_off / 8; /* byte offset */
pos = ob->b_off % 8; /* bit sub-offset */
for (i = 0; i < num_bits; i++) {
bit_val = (1U << i); /* bit check for value */
bit_buf = (1U << pos) & 0xFF; /* bit to set in buf */
if (value & bit_val) /* is bit in val set? */
ob->buf[off] |= bit_buf; /* set bit */
else
ob->buf[off] &= ~bit_buf; /* unset bit */
pos++; /* new byte starts */
if (pos%8 == 0) {
pos = 0;
off++;
}
}
ob->b_off += num_bits;
return 1;
fail:
return 0;
}
/* Read bits (max 32) from buf and update the bit offset. Order is BE (MSF). */
static int r_bits_msf(vgm_bitstream * ib, int num_bits, uint32_t * value) {
off_t off, pos;
int i, bit_buf, bit_val;
if (num_bits == 0) return 1;
if (num_bits > 32 || num_bits < 0 || ib->b_off + num_bits > ib->bufsize*8) goto fail;
*value = 0; /* set all bits to 0 */
off = ib->b_off / 8; /* byte offset */
pos = ib->b_off % 8; /* bit sub-offset */
for (i = 0; i < num_bits; i++) {
bit_buf = (1U << (8-1-pos)) & 0xFF; /* bit check for buf */
bit_val = (1U << (num_bits-1-i)); /* bit to set in value */
if (ib->buf[off] & bit_buf) /* is bit in buf set? */
*value |= bit_val; /* set bit */
pos++;
if (pos%8 == 0) { /* new byte starts */
pos = 0;
off++;
}
}
ib->b_off += num_bits;
return 1;
fail:
return 0;
}
/* Write bits (max 32) to buf and update the bit offset. Order is BE (MSF). */
static int w_bits_msf(vgm_bitstream * ob, int num_bits, uint32_t value) {
off_t off, pos;
int i, bit_val, bit_buf;
if (num_bits == 0) return 1;
if (num_bits > 32 || num_bits < 0 || ob->b_off + num_bits > ob->bufsize*8) goto fail;
off = ob->b_off / 8; /* byte offset */
pos = ob->b_off % 8; /* bit sub-offset */
for (i = 0; i < num_bits; i++) {
bit_val = (1U << (num_bits-1-i)); /* bit check for value */
bit_buf = (1U << (8-1-pos)) & 0xFF; /* bit to set in buf */
if (value & bit_val) /* is bit in val set? */
ob->buf[off] |= bit_buf; /* set bit */
else
ob->buf[off] &= ~bit_buf; /* unset bit */
pos++;
if (pos%8 == 0) { /* new byte starts */
pos = 0;
off++;
}
}
ob->b_off += num_bits;
return 1;
fail:
return 0;
}
int r_bits(vgm_bitstream * ib, int num_bits, uint32_t * value) {
if (ib->mode == BITSTREAM_VORBIS)
return r_bits_vorbis(ib,num_bits,value);
else
return r_bits_msf(ib,num_bits,value);
}
int w_bits(vgm_bitstream * ob, int num_bits, uint32_t value) {
if (ob->mode == BITSTREAM_VORBIS)
return w_bits_vorbis(ob,num_bits,value);
else
return w_bits_msf(ob,num_bits,value);
}
/* ******************************************** */
/* CUSTOM STREAMFILES */
/* ******************************************** */
......
#include "coding.h"
#ifdef VGM_USE_G719
#ifdef __MACOSX__
#include <g719/g719.h>
#else
#include <g719.h>
#endif
#ifdef VGM_USE_G719
#define G719_MAX_CODES ((1280/8)) /* in int16, so max frame size is (value/8)*2 (0xF0=common, 0x140=decoder max 2560b, rare) */
struct g719_codec_data {
sample_t buffer[960];
void *handle;
};
g719_codec_data *init_g719(int channel_count, int frame_size) {
g719_codec_data* init_g719(int channel_count, int frame_size) {
int i;
g719_codec_data *data = NULL;
g719_codec_data* data = NULL;
if (frame_size / sizeof(int16_t) > G719_MAX_CODES)
goto fail;
......@@ -39,10 +38,10 @@ fail:
}
void decode_g719(VGMSTREAM * vgmstream, sample * outbuf, int channelspacing, int32_t samples_to_do, int channel) {
VGMSTREAMCHANNEL *ch = &vgmstream->ch[channel];
g719_codec_data *data = vgmstream->codec_data;
g719_codec_data *ch_data = &data[channel];
void decode_g719(VGMSTREAM* vgmstream, sample_t* outbuf, int channelspacing, int32_t samples_to_do, int channel) {
VGMSTREAMCHANNEL* ch = &vgmstream->ch[channel];
g719_codec_data* data = vgmstream->codec_data;
g719_codec_data* ch_data = &data[channel];
int i;
if (0 == vgmstream->samples_into_block) {
......@@ -58,7 +57,7 @@ void decode_g719(VGMSTREAM * vgmstream, sample * outbuf, int channelspacing, int
}
void reset_g719(g719_codec_data * data, int channels) {
void reset_g719(g719_codec_data* data, int channels) {
int i;
if (!data) return;
......@@ -67,7 +66,7 @@ void reset_g719(g719_codec_data * data, int channels) {
}
}
void free_g719(g719_codec_data * data, int channels) {
void free_g719(g719_codec_data* data, int channels) {
int i;
if (!data) return;
......
#include "coding.h"
struct hca_codec_data {
STREAMFILE* streamfile;
clHCA_stInfo info;
signed short* sample_buffer;
size_t samples_filled;
size_t samples_consumed;
size_t samples_to_discard;
void* data_buffer;
unsigned int current_block;
void* handle;
};
/* init a HCA stream; STREAMFILE will be duplicated for internal use. */
hca_codec_data * init_hca(STREAMFILE *streamFile) {
char filename[PATH_LIMIT];
hca_codec_data* init_hca(STREAMFILE* sf) {
uint8_t header_buffer[0x2000]; /* hca header buffer data (probable max ~0x400) */
hca_codec_data * data = NULL; /* vgmstream HCA context */
hca_codec_data* data = NULL; /* vgmstream HCA context */
int header_size;
int status;
/* test header */
if (read_streamfile(header_buffer, 0x00, 0x08, streamFile) != 0x08)
if (read_streamfile(header_buffer, 0x00, 0x08, sf) != 0x08)
goto fail;
header_size = clHCA_isOurFile(header_buffer, 0x08);
if (header_size < 0 || header_size > 0x1000)
goto fail;
if (read_streamfile(header_buffer, 0x00, header_size, streamFile) != header_size)
if (read_streamfile(header_buffer, 0x00, header_size, sf) != header_size)
goto fail;
/* init vgmstream context */
......@@ -39,8 +54,7 @@ hca_codec_data * init_hca(STREAMFILE *streamFile) {
if (!data->sample_buffer) goto fail;
/* load streamfile for reads */
get_streamfile_name(streamFile,filename, sizeof(filename));
data->streamfile = open_streamfile(streamFile,filename);
data->streamfile = reopen_streamfile(sf, 0);
if (!data->streamfile) goto fail;
/* set initial values */
......@@ -53,7 +67,7 @@ fail:
return NULL;
}
void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do) {
void decode_hca(hca_codec_data* data, sample_t* outbuf, int32_t samples_to_do) {
int samples_done = 0;
const unsigned int channels = data->info.channelCount;
const unsigned int blockSize = data->info.blockSize;
......@@ -120,7 +134,7 @@ void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do) {
}
}
void reset_hca(hca_codec_data * data) {
void reset_hca(hca_codec_data* data) {
if (!data) return;
clHCA_DecodeReset(data->handle);
......@@ -130,7 +144,7 @@ void reset_hca(hca_codec_data * data) {
data->samples_to_discard = data->info.encoderDelay;
}
void loop_hca(hca_codec_data * data, int32_t num_sample) {
void loop_hca(hca_codec_data* data, int32_t num_sample) {
if (!data) return;
/* manually calc loop values if not set (should only happen with installed/forced looping,
......@@ -149,7 +163,7 @@ void loop_hca(hca_codec_data * data, int32_t num_sample) {
data->samples_to_discard = data->info.loopStartDelay;
}
void free_hca(hca_codec_data * data) {
void free_hca(hca_codec_data* data) {
if (!data) return;
close_streamfile(data->streamfile);
......@@ -175,7 +189,7 @@ void free_hca(hca_codec_data * data) {
/* Test a number of frames if key decrypts correctly.
* Returns score: <0: error/wrong, 0: unknown/silent file, >0: good (the closest to 1 the better). */
int test_hca_key(hca_codec_data * data, unsigned long long keycode) {
int test_hca_key(hca_codec_data* data, unsigned long long keycode) {
size_t test_frames = 0, current_frame = 0, blank_frames = 0;
int total_score = 0, found_regular_frame = 0;
const unsigned int blockSize = data->info.blockSize;
......@@ -242,3 +256,16 @@ int test_hca_key(hca_codec_data * data, unsigned long long keycode) {
clHCA_DecodeReset(data->handle);
return total_score;
}
void hca_set_encryption_key(hca_codec_data* data, uint64_t keycode) {
clHCA_SetKey(data->handle, (unsigned long long)keycode);
}
clHCA_stInfo* hca_get_info(hca_codec_data* data) {
return &data->info;
}
STREAMFILE* hca_get_streamfile(hca_codec_data* data) {
if (!data) return NULL;
return data->streamfile;
}
......@@ -285,6 +285,60 @@ static void mtf_ima_expand_nibble(VGMSTREAMCHANNEL * stream, off_t byte_offset,
if (*step_index > 88) *step_index=88;
}
/* IMA table pre-modified like this:
for i=0..89
adpcm = clamp(adpcm[i], 0x1fff) * 4;
*/