Cleanup and logs

This commit is contained in:
bnnm 2021-09-04 20:31:11 +02:00
parent 0b82366d3d
commit a4fe6a9352
5 changed files with 382 additions and 344 deletions

View File

@ -322,7 +322,7 @@ ffmpeg_codec_data* init_ffmpeg_header_offset_subsong(STREAMFILE* sf, uint8_t* he
goto fail; goto fail;
if (size == 0 || start + size > get_streamfile_size(sf)) { if (size == 0 || start + size > get_streamfile_size(sf)) {
VGM_LOG("FFMPEG: wrong start+size found: %x + %x > %x \n", (uint32_t)start, (uint32_t)size, get_streamfile_size(sf)); vgm_logi(size != 0, "FFMPEG: wrong start+size found: %x + %x > %x \n", (uint32_t)start, (uint32_t)size, get_streamfile_size(sf));
size = get_streamfile_size(sf) - start; size = get_streamfile_size(sf) - start;
} }

View File

@ -1,147 +1,154 @@
#include "meta.h" #include "meta.h"
#include "../coding/coding.h" #include "../coding/coding.h"
#include "../util.h" #include "../util.h"
static int bink_get_info(STREAMFILE *streamFile, int target_subsong, int * out_total_streams, size_t *out_stream_size, int * out_channel_count, int * out_sample_rate, int * out_num_samples); static int bink_get_info(STREAMFILE* sf, int target_subsong, int* p_total_subsongs, size_t* p_stream_size, int* p_channels, int* p_sample_rate, int* p_num_samples);
/* BINK 1/2 - RAD Game Tools movies (audio/video format) */ /* BINK 1/2 - RAD Game Tools movies (audio/video format) */
VGMSTREAM * init_vgmstream_bik(STREAMFILE *streamFile) { VGMSTREAM* init_vgmstream_bik(STREAMFILE* sf) {
VGMSTREAM * vgmstream = NULL; VGMSTREAM * vgmstream = NULL;
int channel_count = 0, loop_flag = 0, sample_rate = 0, num_samples = 0; int channels = 0, loop_flag = 0, sample_rate = 0, num_samples = 0;
int total_subsongs = 0, target_subsong = streamFile->stream_index; int total_subsongs = 0, target_subsong = sf->stream_index;
size_t stream_size; size_t stream_size;
/* checks */ /* checks */
/* .bik/bik2/bk2: standard /* .bik/bik2/bk2: standard
* .bika = fake extension for demuxed audio */ * .bika: fake extension for demuxed audio */
if (!check_extensions(streamFile,"bik,bika,bik2,bk2")) if (!check_extensions(sf,"bik,bik2,bk2,bika"))
goto fail; goto fail;
/* check header "BIK" (bink 1) or "KB2" (bink 2), followed by version-char (audio is the same for both) */ /* check bink1/2 header, followed by version-char (audio is the same) */
if ((read_32bitBE(0x00,streamFile) & 0xffffff00) != 0x42494B00 && if ((read_32bitBE(0x00,sf) & 0xffffff00) != get_id32be("BIK\0") &&
(read_32bitBE(0x00,streamFile) & 0xffffff00) != 0x4B423200 ) goto fail; (read_32bitBE(0x00,sf) & 0xffffff00) != get_id32be("KB2\0"))
goto fail;
/* find target stream info and samples */
if (!bink_get_info(streamFile, target_subsong, &total_subsongs, &stream_size, &channel_count, &sample_rate, &num_samples)) /* find target stream info and samples */
goto fail; if (!bink_get_info(sf, target_subsong, &total_subsongs, &stream_size, &channels, &sample_rate, &num_samples))
goto fail;
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count, loop_flag); /* build the VGMSTREAM */
if (!vgmstream) goto fail; vgmstream = allocate_vgmstream(channels, loop_flag);
if (!vgmstream) goto fail;
vgmstream->layout_type = layout_none;
vgmstream->sample_rate = sample_rate; vgmstream->layout_type = layout_none;
vgmstream->num_samples = num_samples; vgmstream->sample_rate = sample_rate;
vgmstream->num_streams = total_subsongs; vgmstream->num_samples = num_samples;
vgmstream->stream_size = stream_size; vgmstream->num_streams = total_subsongs;
vgmstream->meta_type = meta_BINK; vgmstream->stream_size = stream_size;
vgmstream->meta_type = meta_BINK;
#ifdef VGM_USE_FFMPEG
{ #ifdef VGM_USE_FFMPEG
/* target_subsong should be passed manually */ {
/* target_subsong should be passed manually */
vgmstream->codec_data = init_ffmpeg_header_offset_subsong(streamFile, NULL,0, 0x0,get_streamfile_size(streamFile), target_subsong); vgmstream->codec_data = init_ffmpeg_header_offset_subsong(sf, NULL,0, 0x0,0, target_subsong);
if (!vgmstream->codec_data) goto fail; if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg; vgmstream->coding_type = coding_FFmpeg;
} }
#else #else
goto fail; goto fail;
#endif #endif
return vgmstream; return vgmstream;
fail: fail:
close_vgmstream(vgmstream); close_vgmstream(vgmstream);
return NULL; return NULL;
} }
/** /**
* Gets stream info, and number of samples in a BINK file by reading all frames' headers (as it's VBR), * Gets stream info, and number of samples in a BINK file by reading all frames' headers (as it's VBR),
* as they are not in the main header. The header for BINK1 and 2 is the same. * as they are not in the main header. The header for BINK1 and 2 is the same.
* (a ~3 min movie needs ~6000-7000 frames = fseeks, should be fast enough) * (a ~3 min movie needs ~6000-7000 frames = fseeks, should be fast enough)
*/ */
static int bink_get_info(STREAMFILE *streamFile, int target_subsong, int * out_total_subsongs, size_t * out_stream_size, int * out_channel_count, int * out_sample_rate, int * out_num_samples) { static int bink_get_info(STREAMFILE* sf, int target_subsong, int* p_total_subsongs, size_t* p_stream_size, int* p_channels, int* p_sample_rate, int* p_num_samples) {
uint32_t *offsets = NULL; uint32_t* offsets = NULL;
uint32_t num_frames, num_samples_b = 0; uint32_t num_frames, num_samples_b = 0;
off_t cur_offset; off_t cur_offset;
int i, j, sample_rate, channel_count; int i, j, sample_rate, channels;
int total_subsongs; int total_subsongs;
size_t stream_size = 0; size_t stream_size = 0;
size_t filesize = get_streamfile_size(streamFile); size_t filesize = get_streamfile_size(sf);
uint32_t signature = (read_32bitBE(0x00,streamFile) & 0xffffff00); uint32_t signature = (read_32bitBE(0x00,sf) & 0xffffff00);
uint8_t revision = (read_32bitBE(0x00,streamFile) & 0xFF); uint8_t revision = (read_32bitBE(0x00,sf) & 0xFF);
if (read_32bitLE(0x04,streamFile)+ 0x08 != filesize)
goto fail; if (read_32bitLE(0x04,sf) + 0x08 != filesize)
goto fail;
num_frames = (uint32_t)read_32bitLE(0x08,streamFile);
if (num_frames == 0 || num_frames > 0x100000) goto fail; /* something must be off (avoids big allocs below) */ num_frames = (uint32_t)read_32bitLE(0x08,sf);
if (num_frames == 0 || num_frames > 0x100000) goto fail; /* something must be off (avoids big allocs below) */
/* multichannel/multilanguage audio is usually N streams of stereo/mono, no way to know channel layout */
total_subsongs = read_32bitLE(0x28,streamFile); /* multichannel/multilanguage audio is usually N streams of stereo/mono, no way to know channel layout */
if (target_subsong == 0) target_subsong = 1; total_subsongs = read_32bitLE(0x28,sf);
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1 || total_subsongs > 255) goto fail; if (total_subsongs < 1) {
vgm_logi("BIK: no audio in movie (ignore)\n");
/* find stream info and position in offset table */ goto fail;
cur_offset = 0x2c; }
if ((signature == 0x42494B00 && (revision == 0x6b)) || /* k */
(signature == 0x4B423200 && (revision == 0x69 || revision == 0x6a || revision == 0x6b))) /* i,j,k */ if (target_subsong == 0) target_subsong = 1;
cur_offset += 0x04; /* unknown v2 header field */ if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs > 255) goto fail;
cur_offset += 0x04*total_subsongs; /* skip streams max packet bytes */
sample_rate = (uint16_t)read_16bitLE(cur_offset+0x04*(target_subsong-1)+0x00,streamFile);
channel_count = (uint16_t)read_16bitLE(cur_offset+0x04*(target_subsong-1)+0x02,streamFile) & 0x2000 ? 2 : 1; /* stereo flag */ /* find stream info and position in offset table */
cur_offset += 0x04*total_subsongs; /* skip streams info */ cur_offset = 0x2c;
cur_offset += 0x04*total_subsongs; /* skip streams ids */ if ((signature == 0x42494B00 && (revision == 0x6b)) || /* k */
(signature == 0x4B423200 && (revision == 0x69 || revision == 0x6a || revision == 0x6b))) /* i,j,k */
cur_offset += 0x04; /* unknown v2 header field */
/* read frame offsets in a buffer, to avoid fseeking to the table back and forth */ cur_offset += 0x04*total_subsongs; /* skip streams max packet bytes */
offsets = malloc(sizeof(uint32_t) * num_frames); sample_rate = (uint16_t)read_16bitLE(cur_offset+0x04*(target_subsong-1)+0x00,sf);
if (!offsets) goto fail; channels = (uint16_t)read_16bitLE(cur_offset+0x04*(target_subsong-1)+0x02,sf) & 0x2000 ? 2 : 1; /* stereo flag */
cur_offset += 0x04*total_subsongs; /* skip streams info */
for (i=0; i < num_frames; i++) { cur_offset += 0x04*total_subsongs; /* skip streams ids */
offsets[i] = read_32bitLE(cur_offset,streamFile) & 0xFFFFFFFE; /* mask first bit (= keyframe) */
cur_offset += 0x4;
/* read frame offsets in a buffer, to avoid fseeking to the table back and forth */
if (offsets[i] > filesize) goto fail; offsets = malloc(sizeof(uint32_t) * num_frames);
} if (!offsets) goto fail;
/* after the last index is the file size, validate just in case */
if (read_32bitLE(cur_offset,streamFile) != filesize) goto fail; for (i=0; i < num_frames; i++) {
offsets[i] = read_32bitLE(cur_offset,sf) & 0xFFFFFFFE; /* mask first bit (= keyframe) */
/* read each frame header and sum all samples cur_offset += 0x4;
* a frame has N audio packets with a header (one per stream) + video packet */
for (i=0; i < num_frames; i++) { if (offsets[i] > filesize) goto fail;
cur_offset = offsets[i]; }
/* after the last index is the file size, validate just in case */
/* read audio packet headers per stream */ if (read_32bitLE(cur_offset,sf) != filesize) goto fail;
for (j=0; j < total_subsongs; j++) {
uint32_t ap_size = read_32bitLE(cur_offset+0x00,streamFile); /* not counting this int */ /* read each frame header and sum all samples
* a frame has N audio packets with a header (one per stream) + video packet */
if (j == target_subsong-1) { for (i=0; i < num_frames; i++) {
stream_size += 0x04 + ap_size; cur_offset = offsets[i];
if (ap_size > 0)
num_samples_b += read_32bitLE(cur_offset+0x04,streamFile); /* decoded samples in bytes */ /* read audio packet headers per stream */
break; /* next frame */ for (j=0; j < total_subsongs; j++) {
} uint32_t ap_size = read_32bitLE(cur_offset+0x00,sf); /* not counting this int */
else { /* next stream packet or frame */
cur_offset += 4 + ap_size; //todo sometimes ap_size doesn't include itself (+4), others it does? if (j == target_subsong-1) {
} stream_size += 0x04 + ap_size;
} if (ap_size > 0)
} num_samples_b += read_32bitLE(cur_offset+0x04,sf); /* decoded samples in bytes */
break; /* next frame */
free(offsets); }
else { /* next stream packet or frame */
cur_offset += 4 + ap_size; //todo sometimes ap_size doesn't include itself (+4), others it does?
if (out_total_subsongs) *out_total_subsongs = total_subsongs; }
if (out_stream_size) *out_stream_size = stream_size; }
if (out_sample_rate) *out_sample_rate = sample_rate; }
if (out_channel_count) *out_channel_count = channel_count;
//todo returns a few more samples (~48) than binkconv.exe? free(offsets);
if (out_num_samples) *out_num_samples = num_samples_b / (2 * channel_count);
return 1; if (p_total_subsongs) *p_total_subsongs = total_subsongs;
if (p_stream_size) *p_stream_size = stream_size;
fail: if (p_sample_rate) *p_sample_rate = sample_rate;
free(offsets); if (p_channels) *p_channels = channels;
return 0; //todo returns a few more samples (~48) than binkconv.exe?
} if (p_num_samples) *p_num_samples = num_samples_b / (2 * channels);
return 1;
fail:
free(offsets);
return 0;
}

View File

@ -1,5 +1,6 @@
#include "meta.h" #include "meta.h"
#include "../coding/coding.h" #include "../coding/coding.h"
#include "../util/chunks.h"
/* BKHD - Wwise soundbank container */ /* BKHD - Wwise soundbank container */
@ -19,9 +20,9 @@ VGMSTREAM* init_vgmstream_bkhd(STREAMFILE* sf) {
if (!check_extensions(sf,"bnk")) if (!check_extensions(sf,"bnk"))
goto fail; goto fail;
if (read_u32be(0x00, sf) == 0x414B424B) /* "AKBK" [Shadowrun (X360)] */ if (is_id32be(0x00, sf, "AKBK")) /* [Shadowrun (X360)] */
base_offset = 0x0c; base_offset = 0x0c;
if (read_u32be(base_offset + 0x00, sf) != 0x424B4844) /* "BKHD" */ if (!is_id32be(base_offset + 0x00, sf, "BKHD"))
goto fail; goto fail;
big_endian = guess_endianness32bit(base_offset + 0x04, sf); big_endian = guess_endianness32bit(base_offset + 0x04, sf);
read_u32 = big_endian ? read_u32be : read_u32le; read_u32 = big_endian ? read_u32be : read_u32le;
@ -77,16 +78,41 @@ VGMSTREAM* init_vgmstream_bkhd(STREAMFILE* sf) {
subfile_size = read_u32(offset + 0x14, sf); subfile_size = read_u32(offset + 0x14, sf);
} }
else { else {
off_t didx_offset, data_offset, offset; enum {
size_t didx_size; CHUNK_DIDX = 0x44494458, /* "DIDX" */
if (!find_chunk(sf, 0x44494458, 0x00,0, &didx_offset, &didx_size, big_endian, 0)) /* "DIDX" */ CHUNK_DATA = 0x44415441, /* "DATA" */
goto fail; };
if (!find_chunk(sf, 0x44415441, 0x00,0, &data_offset, NULL, big_endian, 0)) /* "DATA" */ off_t didx_offset = 0, data_offset = 0, didx_size = 0, offset;
chunk_t rc = {0};
rc.be_size = big_endian;
rc.current = 0x00;
while (next_chunk(&rc, sf)) {
switch(rc.type) {
case CHUNK_DIDX:
didx_offset = rc.offset;
didx_size = rc.size;
break;
case CHUNK_DATA:
data_offset = rc.offset;
break;
default:
break;
}
}
if (!didx_offset || !data_offset)
goto fail; goto fail;
total_subsongs = didx_size / 0x0c; total_subsongs = didx_size / 0x0c;
if (total_subsongs < 1) {
vgm_logi("BKHD: bank has no subsongs (ignore)\n");
goto fail;
}
if (target_subsong == 0) target_subsong = 1; if (target_subsong == 0) target_subsong = 1;
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail; if (target_subsong > total_subsongs) goto fail;
offset = didx_offset + (target_subsong - 1) * 0x0c; offset = didx_offset + (target_subsong - 1) * 0x0c;
subfile_id = read_u32(offset + 0x00, sf); subfile_id = read_u32(offset + 0x00, sf);

View File

@ -1,187 +1,192 @@
#include "meta.h" #include "meta.h"
#include "../coding/coding.h" #include "../coding/coding.h"
#include "../util.h" #include "../util.h"
static int smacker_get_info(STREAMFILE *streamFile, int target_subsong, int * out_total_streams, size_t *out_stream_size, int * out_channel_count, int * out_sample_rate, int * out_num_samples); static int smacker_get_info(STREAMFILE* sf, int target_subsong, int* p_total_subsongs, size_t* p_stream_size, int* p_channels, int* p_sample_rate, int* p_num_samples);
/* SMK - RAD Game Tools Smacker movies (audio/video format) */ /* SMK - RAD Game Tools older Smacker movies (audio/video format) */
VGMSTREAM * init_vgmstream_smk(STREAMFILE *streamFile) { VGMSTREAM* init_vgmstream_smk(STREAMFILE *sf) {
VGMSTREAM * vgmstream = NULL; VGMSTREAM* vgmstream = NULL;
int channel_count = 0, loop_flag = 0, sample_rate = 0, num_samples = 0; int channels = 0, loop_flag = 0, sample_rate = 0, num_samples = 0;
int total_subsongs = 0, target_subsong = streamFile->stream_index; int total_subsongs = 0, target_subsong = sf->stream_index;
size_t stream_size; size_t stream_size;
/* checks */ /* checks */
if (!check_extensions(streamFile,"smk")) if (!check_extensions(sf,"smk"))
goto fail; goto fail;
if (read_32bitBE(0x00,streamFile) != 0x534D4B32 && /* "SMK2" */ if (!is_id32be(0x00,sf, "SMK2") &&
read_32bitBE(0x00,streamFile) != 0x534D4B34) /* "SMK4" */ !is_id32be(0x00,sf, "SMK4"))
goto fail; goto fail;
/* find target stream info */ /* find target stream info */
if (!smacker_get_info(streamFile, target_subsong, &total_subsongs, &stream_size, &channel_count, &sample_rate, &num_samples)) if (!smacker_get_info(sf, target_subsong, &total_subsongs, &stream_size, &channels, &sample_rate, &num_samples))
goto fail; goto fail;
/* build the VGMSTREAM */ /* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count, loop_flag); vgmstream = allocate_vgmstream(channels, loop_flag);
if (!vgmstream) goto fail; if (!vgmstream) goto fail;
vgmstream->layout_type = layout_none; vgmstream->meta_type = meta_SMACKER;
vgmstream->sample_rate = sample_rate; vgmstream->sample_rate = sample_rate;
vgmstream->num_samples = num_samples; vgmstream->num_samples = num_samples;
vgmstream->num_streams = total_subsongs;
vgmstream->stream_size = stream_size; vgmstream->num_streams = total_subsongs;
vgmstream->meta_type = meta_SMACKER; vgmstream->stream_size = stream_size;
{ {
#ifdef VGM_USE_FFMPEG #ifdef VGM_USE_FFMPEG
/* target_subsong should be passed manually */ /* target_subsong should be passed manually */
vgmstream->codec_data = init_ffmpeg_header_offset_subsong(streamFile, NULL,0, 0x0,get_streamfile_size(streamFile), target_subsong); vgmstream->codec_data = init_ffmpeg_header_offset_subsong(sf, NULL,0, 0x00, 0, target_subsong);
if (!vgmstream->codec_data) goto fail; if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg; vgmstream->coding_type = coding_FFmpeg;
#else vgmstream->layout_type = layout_none;
goto fail; #else
#endif goto fail;
} #endif
}
return vgmstream;
return vgmstream;
fail:
close_vgmstream(vgmstream); fail:
return NULL; close_vgmstream(vgmstream);
} return NULL;
}
typedef enum {
SMK_AUDIO_PACKED = (1<<7), typedef enum {
SMK_AUDIO_PRESENT = (1<<6), SMK_AUDIO_PACKED = (1<<7),
SMK_AUDIO_16BITS = (1<<5), SMK_AUDIO_PRESENT = (1<<6),
SMK_AUDIO_STEREO = (1<<4), SMK_AUDIO_16BITS = (1<<5),
SMK_AUDIO_BINK_RDFT = (1<<3), SMK_AUDIO_STEREO = (1<<4),
SMK_AUDIO_BINK_DCT = (1<<2), SMK_AUDIO_BINK_RDFT = (1<<3),
//SMK_AUD_UNUSED1 = (1<<1), SMK_AUDIO_BINK_DCT = (1<<2),
//SMK_AUD_UNUSED0 = (1<<0), //SMK_AUD_UNUSED1 = (1<<1),
} smk_audio_flag; //SMK_AUD_UNUSED0 = (1<<0),
} smk_audio_flag;
//todo test multilang streams and codecs other than SMACKAUD
/* Gets stream info, and number of samples in a file by reading frames //todo test multilang streams and codecs other than SMACKAUD
* info: https://wiki.multimedia.cx/index.php/Smacker */ /* Gets stream info, and number of samples in a file by reading frames
static int smacker_get_info(STREAMFILE *sf, int target_subsong, int * out_total_subsongs, size_t * out_stream_size, int * out_channel_count, int * out_sample_rate, int * out_num_samples) { * info: https://wiki.multimedia.cx/index.php/Smacker */
STREAMFILE *sf_index = NULL; static int smacker_get_info(STREAMFILE* sf, int target_subsong, int* out_total_subsongs, size_t* p_stream_size, int* p_channels, int* p_sample_rate, int* p_num_samples) {
uint32_t flags, total_frames, trees_sizes; STREAMFILE* sf_index = NULL;
off_t size_offset, type_offset, data_offset; uint32_t flags, total_frames, trees_sizes;
int i, j, sample_rate = 0, channel_count = 0, num_samples = 0; off_t size_offset, type_offset, data_offset;
int total_subsongs, target_stream = 0; int i, j, sample_rate = 0, channels = 0, num_samples = 0;
size_t stream_size = 0; int total_subsongs, target_stream = 0;
uint8_t stream_flags = 0; size_t stream_size = 0;
uint8_t stream_flags = 0;
/* rough format:
* - header (id, frames, video info/config, audio info) /* rough format:
* - frame sizes table * - header (id, frames, video info/config, audio info)
* - frame info table * - frame sizes table
* - huffman trees * - frame info table
* - frame data * - huffman trees
*/ * - frame data
*/
/* read header */
total_frames = read_u32le(0x0c,sf); /* read header */
if (total_frames <= 0 || total_frames > 0x100000) goto fail; /* something must be off */ total_frames = read_u32le(0x0c,sf);
if (total_frames <= 0 || total_frames > 0x100000) goto fail; /* something must be off */
flags = read_u32le(0x14,sf);
if (flags & 1) /* extra "ring frame" */ flags = read_u32le(0x14,sf);
total_frames += 1; if (flags & 1) /* extra "ring frame" */
total_frames += 1;
trees_sizes = read_u32le(0x34,sf);
trees_sizes = read_u32le(0x34,sf);
if (target_subsong == 0) target_subsong = 1;
total_subsongs = 0; if (target_subsong == 0) target_subsong = 1;
for (i = 0; i < 7; i++) { /* up to 7 audio (multilang?) streams */ total_subsongs = 0;
uint32_t audio_info = read_u32le(0x48 + 0x04*i,sf); for (i = 0; i < 7; i++) { /* up to 7 audio (multilang?) streams */
uint8_t audio_flags = (audio_info >> 24) & 0xFF; uint32_t audio_info = read_u32le(0x48 + 0x04*i,sf);
int audio_rate = audio_info & 0x00FFFFFF; uint8_t audio_flags = (audio_info >> 24) & 0xFF;
int audio_rate = audio_info & 0x00FFFFFF;
if (audio_flags & SMK_AUDIO_PRESENT) {
total_subsongs++; if (audio_flags & SMK_AUDIO_PRESENT) {
total_subsongs++;
if (target_subsong == total_subsongs) {
target_stream = i; if (target_subsong == total_subsongs) {
sample_rate = audio_rate & 0x00FFFFFF; target_stream = i;
channel_count = (audio_flags & SMK_AUDIO_STEREO) ? 2 : 1; sample_rate = audio_rate & 0x00FFFFFF;
stream_flags = audio_flags; channels = (audio_flags & SMK_AUDIO_STEREO) ? 2 : 1;
} stream_flags = audio_flags;
} }
} }
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail; }
if (sample_rate == 0 || channel_count == 0) goto fail; if (total_subsongs < 1) {
vgm_logi("SMK: no audio in movie (ignore)\n");
/* read size and type tables into buffer */ goto fail;
sf_index = reopen_streamfile(sf, total_frames*0x04 + total_frames*0x01); }
if (!sf_index) goto fail; if (target_subsong < 0 || target_subsong > total_subsongs) goto fail;
if (sample_rate == 0 || channels == 0) goto fail;
/* read frames and sum all samples, since some codecs are VBR */
size_offset = 0x68; /* read size and type tables into buffer */
type_offset = size_offset + total_frames*0x04; sf_index = reopen_streamfile(sf, total_frames*0x04 + total_frames*0x01);
data_offset = type_offset + total_frames*0x01 + trees_sizes; if (!sf_index) goto fail;
for (i=0; i < total_frames; i++) {
uint32_t frame_size = read_u32le(size_offset,sf_index) & 0xFFFFFFFC; /* last 2 bits are keyframe flags */ /* read frames and sum all samples, since some codecs are VBR */
uint8_t frame_type = read_u8 (type_offset,sf_index); /* 0: has palette, 1..7: has stream N) */ size_offset = 0x68;
off_t offset = data_offset; type_offset = size_offset + total_frames*0x04;
data_offset = type_offset + total_frames*0x01 + trees_sizes;
/* skip palette */ for (i = 0; i < total_frames; i++) {
if (frame_type & (1<<0)) { uint32_t frame_size = read_u32le(size_offset,sf_index) & 0xFFFFFFFC; /* last 2 bits are keyframe flags */
uint8_t palette_size = read_u8(offset,sf); uint8_t frame_type = read_u8 (type_offset,sf_index); /* 0: has palette, 1..7: has stream N) */
offset += palette_size * 4; off_t offset = data_offset;
}
/* skip palette */
/* read target audio packet and ignore rest (though probably all streams are the same) */ if (frame_type & (1<<0)) {
for (j = 0; j < 7; j++) { uint8_t palette_size = read_u8(offset,sf);
uint32_t audio_size; offset += palette_size * 4;
}
/* check if stream N exists in this frame (supposedly streams can be go in separate frames) */
if ( !(frame_type & (1<<(j+1))) ) /* read target audio packet and ignore rest (though probably all streams are the same) */
continue; for (j = 0; j < 7; j++) {
uint32_t audio_size;
audio_size = read_u32le(offset,sf);
/* check if stream N exists in this frame (supposedly streams can be go in separate frames) */
if (j == target_stream) { if ( !(frame_type & (1<<(j+1))) )
continue;
/* resulting PCM bytes to samples */
if (stream_flags & SMK_AUDIO_PACKED) { /* Smacker and maybe Bink codecs */ audio_size = read_u32le(offset,sf);
uint32_t unpacked_size = read_u32le(offset+0x04,sf);
num_samples += unpacked_size / (0x02 * channel_count); if (j == target_stream) {
}
else if (stream_flags & SMK_AUDIO_16BITS) { /* PCM16 */ /* resulting PCM bytes to samples */
num_samples += (audio_size - 0x04) / (0x02 * channel_count); if (stream_flags & SMK_AUDIO_PACKED) { /* Smacker and maybe Bink codecs */
} uint32_t unpacked_size = read_u32le(offset+0x04,sf);
else { /* PCM8 */ num_samples += unpacked_size / (0x02 * channels);
num_samples += (audio_size - 0x04) / (0x01 * channel_count); }
} else if (stream_flags & SMK_AUDIO_16BITS) { /* PCM16 */
} num_samples += (audio_size - 0x04) / (0x02 * channels);
}
stream_size += audio_size; else { /* PCM8 */
offset += audio_size; num_samples += (audio_size - 0x04) / (0x01 * channels);
} }
}
/* rest is video packet (size = offset - data_offset) */
stream_size += audio_size;
size_offset += 0x04; offset += audio_size;
type_offset += 0x01; }
data_offset += frame_size;
} /* rest is video packet (size = offset - data_offset) */
if (out_total_subsongs) *out_total_subsongs = total_subsongs; size_offset += 0x04;
if (out_stream_size) *out_stream_size = stream_size; type_offset += 0x01;
if (out_sample_rate) *out_sample_rate = sample_rate; data_offset += frame_size;
if (out_channel_count) *out_channel_count = channel_count; }
if (out_num_samples) *out_num_samples = num_samples;
if (out_total_subsongs) *out_total_subsongs = total_subsongs;
close_streamfile(sf_index); if (p_stream_size) *p_stream_size = stream_size;
return 1; if (p_sample_rate) *p_sample_rate = sample_rate;
if (p_channels) *p_channels = channels;
fail: if (p_num_samples) *p_num_samples = num_samples;
close_streamfile(sf_index);
return 0; close_streamfile(sf_index);
} return 1;
fail:
close_streamfile(sf_index);
return 0;
}

View File

@ -518,7 +518,7 @@ static void seek_force_loop(VGMSTREAM* vgmstream, int loop_count) {
return; return;
/* pretend decoder reached loop end so state is set to loop start */ /* pretend decoder reached loop end so state is set to loop start */
vgmstream->loop_count = loop_count - 1; /* seeking to first loop musy become ++ > 0 */ vgmstream->loop_count = loop_count - 1; /* seeking to first loop must become ++ > 0 */
vgmstream->current_sample = vgmstream->loop_end_sample; vgmstream->current_sample = vgmstream->loop_end_sample;
vgmstream_do_loop(vgmstream); vgmstream_do_loop(vgmstream);
} }