vgmstream/src/meta/opus.c

183 lines
5.9 KiB
C
Raw Normal View History

2017-09-30 01:27:47 +02:00
#include "meta.h"
#include "../util.h"
#include "../coding/coding.h"
/* Nintendo OPUS - from Switch games, including header variations (not the same as Ogg Opus) */
static VGMSTREAM * init_vgmstream_opus(STREAMFILE *streamFile, meta_t meta_type, off_t offset, int32_t num_samples, int32_t loop_start, int32_t loop_end) {
2017-09-30 01:27:47 +02:00
VGMSTREAM * vgmstream = NULL;
off_t start_offset;
int loop_flag = 0, channel_count;
off_t data_offset;
size_t data_size, skip = 0;
2017-09-30 01:27:47 +02:00
2018-01-26 17:55:39 +01:00
if ((uint32_t)read_32bitLE(offset + 0x00,streamFile) != 0x80000001)
2017-09-30 01:27:47 +02:00
goto fail;
2018-01-26 17:55:39 +01:00
channel_count = read_8bit(offset + 0x09, streamFile);
/* 0x0a: packet size if CBR, 0 if VBR */
data_offset = offset + read_32bitLE(offset + 0x10, streamFile);
skip = read_32bitLE(offset + 0x1c, streamFile);
2017-09-30 01:27:47 +02:00
2018-01-26 17:55:39 +01:00
if ((uint32_t)read_32bitLE(data_offset, streamFile) != 0x80000004)
goto fail;
2018-01-26 17:55:39 +01:00
data_size = read_32bitLE(data_offset + 0x04, streamFile);
2018-01-26 17:55:39 +01:00
start_offset = data_offset + 0x08;
loop_flag = (loop_end > 0); /* -1 when not set */
2017-09-30 01:27:47 +02:00
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = read_32bitLE(offset + 0x0c,streamFile);
vgmstream->meta_type = meta_OPUS;
2017-09-30 01:27:47 +02:00
vgmstream->num_samples = num_samples;
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = loop_end;
2017-09-30 01:27:47 +02:00
#ifdef VGM_USE_FFMPEG
{
uint8_t buf[0x100];
size_t bytes;
2017-09-30 01:27:47 +02:00
ffmpeg_custom_config cfg;
ffmpeg_codec_data *ffmpeg_data;
2017-09-30 01:27:47 +02:00
bytes = ffmpeg_make_opus_header(buf,0x100, vgmstream->channels, skip, vgmstream->sample_rate);
if (bytes <= 0) goto fail;
memset(&cfg, 0, sizeof(ffmpeg_custom_config));
cfg.type = FFMPEG_SWITCH_OPUS;
ffmpeg_data = init_ffmpeg_config(streamFile, buf,bytes, start_offset,data_size, &cfg);
if (!ffmpeg_data) goto fail;
2017-09-30 01:27:47 +02:00
vgmstream->codec_data = ffmpeg_data;
2017-09-30 01:27:47 +02:00
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
if (ffmpeg_data->skipSamples <= 0) {
ffmpeg_set_skip_samples(ffmpeg_data, skip);
}
if (vgmstream->num_samples == 0) {
vgmstream->num_samples = switch_opus_get_samples(start_offset, data_size,
vgmstream->sample_rate, streamFile) - skip;
}
2017-09-30 01:27:47 +02:00
}
#else
goto fail;
#endif
/* open the file for reading */
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
goto fail;
return vgmstream;
fail:
close_vgmstream(vgmstream);
return NULL;
}
/* standard Switch Opus, Nintendo header + raw data (generated by opus_test.c?) [Lego City Undercover (Switch)] */
VGMSTREAM * init_vgmstream_opus_std(STREAMFILE *streamFile) {
off_t offset = 0;
int num_samples = 0, loop_start = 0, loop_end = 0;
/* checks */
if (!check_extensions(streamFile,"opus,lopus"))
goto fail;
offset = 0x00;
num_samples = 0;
loop_start = 0;
loop_end = 0;
return init_vgmstream_opus(streamFile, meta_OPUS, offset, num_samples,loop_start,loop_end);
fail:
return NULL;
}
/* Nippon1 variation [Disgaea 5 (Switch)] */
VGMSTREAM * init_vgmstream_opus_n1(STREAMFILE *streamFile) {
off_t offset = 0;
int num_samples = 0, loop_start = 0, loop_end = 0;
/* checks */
if ( !check_extensions(streamFile,"opus,lopus"))
goto fail;
if (!((read_32bitBE(0x04,streamFile) == 0x00000000 && read_32bitBE(0x0c,streamFile) == 0x00000000) ||
(read_32bitBE(0x04,streamFile) == 0xFFFFFFFF && read_32bitBE(0x0c,streamFile) == 0xFFFFFFFF)))
goto fail;
offset = 0x10;
num_samples = 0;
loop_start = read_32bitLE(0x00,streamFile);
loop_end = read_32bitLE(0x08,streamFile);
return init_vgmstream_opus(streamFile, meta_OPUS, offset, num_samples,loop_start,loop_end);
fail:
return NULL;
}
/* Capcom variation [Ultra Street Fighter II (Switch), Resident Evil: Revelations (Switch)] */
VGMSTREAM * init_vgmstream_opus_capcom(STREAMFILE *streamFile) {
off_t offset = 0;
int num_samples = 0, loop_start = 0, loop_end = 0;
int channel_count;
/* checks */
if ( !check_extensions(streamFile,"opus,lopus"))
goto fail;
channel_count = read_32bitLE(0x04,streamFile);
if (channel_count != 0x01 && channel_count != 0x02)
goto fail;
num_samples = read_32bitLE(0x00,streamFile);
/* 0x04: channels, >2 uses interleaved streams (2ch+2ch+2ch) */
loop_start = read_32bitLE(0x08,streamFile);
loop_end = read_32bitLE(0x0c,streamFile);
/* 0x10: frame size (with extra data) */
/* 0x14: extra chunk count (each of 0x08, after 0x30) */
/* 0x18: null */
offset = read_32bitLE(0x1c,streamFile);
/* 0x20-8: config? (0x0077C102 04000000 E107070C) */
/* 0x2c: some size? */
return init_vgmstream_opus(streamFile, meta_OPUS, offset, num_samples,loop_start,loop_end);
fail:
return NULL;
}
/* Procyon Studio variation [Xenoblade Chronicles 2 (Switch)] */
VGMSTREAM * init_vgmstream_opus_nop(STREAMFILE *streamFile) {
off_t offset = 0;
int num_samples = 0, loop_start = 0, loop_end = 0, loop_flag;
/* checks */
if (!check_extensions(streamFile,"nop"))
goto fail;
if (read_32bitBE(0x00, streamFile) != 0x73616466 || /* "sadf" */
read_32bitBE(0x08, streamFile) != 0x6f707573) /* "opus" */
goto fail;
offset = read_32bitLE(0x1c, streamFile);
num_samples = read_32bitLE(0x28, streamFile);
loop_flag = read_8bit(0x19, streamFile);
if (loop_flag) {
loop_start = read_32bitLE(0x2c, streamFile);
loop_end = read_32bitLE(0x30, streamFile);
}
return init_vgmstream_opus(streamFile, meta_OPUS, offset, num_samples,loop_start,loop_end);
fail:
return NULL;
}