2010-03-21 05:23:18 +01:00
|
|
|
#include "coding.h"
|
2020-01-12 11:57:20 +01:00
|
|
|
#include "g7221_decoder_lib.h"
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2017-04-29 22:37:15 +02:00
|
|
|
#ifdef VGM_USE_G7221
|
2020-02-08 12:54:13 +01:00
|
|
|
#define G7221_MAX_FRAME_SIZE 0x78 /* 960/8 */
|
|
|
|
#define G7221_MAX_FRAME_SAMPLES 640 /* 32000/50 */
|
2018-08-14 22:20:36 +02:00
|
|
|
|
2020-01-12 11:57:20 +01:00
|
|
|
struct g7221_codec_data {
|
2020-02-08 12:54:13 +01:00
|
|
|
int channels;
|
|
|
|
int frame_size;
|
|
|
|
struct g7221_channel_data {
|
|
|
|
sample_t buffer[G7221_MAX_FRAME_SAMPLES];
|
|
|
|
g7221_handle* handle;
|
|
|
|
} *ch;
|
2020-01-12 11:57:20 +01:00
|
|
|
};
|
2017-04-29 22:37:15 +02:00
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
g7221_codec_data* init_g7221(int channels, int frame_size) {
|
2017-10-14 12:41:59 +02:00
|
|
|
int i;
|
2020-01-12 11:57:20 +01:00
|
|
|
g7221_codec_data* data = NULL;
|
2017-10-14 12:41:59 +02:00
|
|
|
|
2020-01-12 11:57:20 +01:00
|
|
|
if (frame_size > G7221_MAX_FRAME_SIZE)
|
2018-08-14 22:20:36 +02:00
|
|
|
goto fail;
|
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
data = calloc(1, sizeof(g7221_codec_data));
|
2017-10-14 12:41:59 +02:00
|
|
|
if (!data) goto fail;
|
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
data->channels = channels;
|
|
|
|
data->frame_size = frame_size;
|
|
|
|
|
|
|
|
data->ch = calloc(channels, sizeof(struct g7221_channel_data));
|
|
|
|
if (!data->ch) goto fail;
|
|
|
|
|
|
|
|
for (i = 0; i < data->channels; i++) {
|
|
|
|
data->ch[i].handle = g7221_init(frame_size);
|
|
|
|
if (!data->ch[i].handle) goto fail;
|
2017-10-14 12:41:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
fail:
|
2020-02-08 12:54:13 +01:00
|
|
|
free_g7221(data);
|
2017-10-14 12:41:59 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
void decode_g7221(VGMSTREAM* vgmstream, sample_t* outbuf, int channelspacing, int32_t samples_to_do, int channel) {
|
|
|
|
VGMSTREAMCHANNEL* ch = &vgmstream->ch[channel];
|
|
|
|
g7221_codec_data* data = vgmstream->codec_data;
|
|
|
|
struct g7221_channel_data* ch_data = &data->ch[channel];
|
2010-03-23 21:57:12 +01:00
|
|
|
int i;
|
2010-03-21 05:23:18 +01:00
|
|
|
|
2018-08-14 22:20:36 +02:00
|
|
|
if (0 == vgmstream->samples_into_block) {
|
2020-01-12 11:57:20 +01:00
|
|
|
uint8_t buf[G7221_MAX_FRAME_SIZE];
|
|
|
|
size_t bytes;
|
2020-02-08 12:54:13 +01:00
|
|
|
size_t read = data->frame_size;
|
2020-01-12 11:57:20 +01:00
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
bytes = read_streamfile(buf, ch->offset, read, ch->streamfile);
|
2020-01-12 11:57:20 +01:00
|
|
|
if (bytes != read) {
|
|
|
|
//g7221_decode_empty(ch_data->handle, ch_data->buffer);
|
|
|
|
memset(ch_data->buffer, 0, sizeof(ch_data->buffer));
|
|
|
|
VGM_LOG("S14: EOF read\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g7221_decode_frame(ch_data->handle, buf, ch_data->buffer);
|
|
|
|
}
|
2010-03-23 21:57:12 +01:00
|
|
|
}
|
|
|
|
|
2018-08-14 22:20:36 +02:00
|
|
|
for (i = 0; i < samples_to_do; i++) {
|
2010-03-23 21:57:12 +01:00
|
|
|
outbuf[i*channelspacing] = ch_data->buffer[vgmstream->samples_into_block+i];
|
2010-03-21 05:23:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 22:37:15 +02:00
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
void reset_g7221(g7221_codec_data* data) {
|
2017-04-29 22:37:15 +02:00
|
|
|
int i;
|
2018-03-10 16:59:00 +01:00
|
|
|
if (!data) return;
|
2017-04-29 22:37:15 +02:00
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
for (i = 0; i < data->channels; i++) {
|
|
|
|
g7221_reset(data->ch[i].handle);
|
2017-04-29 22:37:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
void free_g7221(g7221_codec_data* data) {
|
2018-08-14 22:20:36 +02:00
|
|
|
int i;
|
|
|
|
if (!data) return;
|
2017-04-29 22:37:15 +02:00
|
|
|
|
2020-02-08 12:54:13 +01:00
|
|
|
for (i = 0; i < data->channels; i++) {
|
|
|
|
g7221_free(data->ch[i].handle);
|
2017-04-29 22:37:15 +02:00
|
|
|
}
|
2020-02-08 12:54:13 +01:00
|
|
|
free(data->ch);
|
2018-08-14 22:20:36 +02:00
|
|
|
free(data);
|
2017-04-29 22:37:15 +02:00
|
|
|
}
|
|
|
|
|
2010-03-21 05:23:18 +01:00
|
|
|
#endif
|