2017-02-04 13:00:13 +01:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../coding/coding.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
static int bink_get_info(STREAMFILE *streamFile, int * out_total_streams, int * out_channel_count, int * out_sample_rate, int * out_num_samples);
|
2017-02-04 13:00:13 +01:00
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
/* BINK 1/2 - RAD Game Tools movies (audio/video format) */
|
2017-02-04 13:00:13 +01:00
|
|
|
VGMSTREAM * init_vgmstream_bik(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2017-08-18 19:24:19 +02:00
|
|
|
int channel_count = 0, loop_flag = 0, sample_rate = 0, num_samples = 0, total_streams = 0;
|
2017-08-19 10:28:28 +02:00
|
|
|
int stream_index = streamFile->stream_index;
|
2017-08-18 19:24:19 +02:00
|
|
|
|
2017-02-04 13:00:13 +01:00
|
|
|
|
|
|
|
/* check extension, case insensitive (bika = manually demuxed audio) */
|
|
|
|
if (!check_extensions(streamFile,"bik,bika,bik2,bik2a,bk2,bk2a")) goto fail;
|
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
/* check header "BIK" (bink 1) or "KB2" (bink 2), followed by version-char (audio is the same for both) */
|
2017-02-04 13:00:13 +01:00
|
|
|
if ((read_32bitBE(0x00,streamFile) & 0xffffff00) != 0x42494B00 &&
|
|
|
|
(read_32bitBE(0x00,streamFile) & 0xffffff00) != 0x4B423200 ) goto fail;
|
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
/* find target stream info and samples */
|
|
|
|
if (!bink_get_info(streamFile, &total_streams, &channel_count, &sample_rate, &num_samples))
|
|
|
|
goto fail;
|
2017-02-04 13:00:13 +01:00
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count, loop_flag);
|
2017-02-04 13:00:13 +01:00
|
|
|
if (!vgmstream) goto fail;
|
2017-08-18 19:24:19 +02:00
|
|
|
|
2017-02-04 13:00:13 +01:00
|
|
|
vgmstream->layout_type = layout_none;
|
2017-08-18 19:24:19 +02:00
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
vgmstream->num_samples = num_samples;
|
|
|
|
vgmstream->num_streams = total_streams;
|
|
|
|
vgmstream->meta_type = meta_BINK;
|
2017-02-04 13:00:13 +01:00
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
{
|
|
|
|
/* init_FFmpeg uses streamFile->stream_index internally, if specified */
|
2017-08-19 10:28:28 +02:00
|
|
|
vgmstream->codec_data = init_ffmpeg_offset_index(streamFile, 0x0, get_streamfile_size(streamFile), stream_index);
|
2017-08-18 19:24:19 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
goto fail;
|
|
|
|
#endif
|
2017-02-04 13:00:13 +01:00
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2017-08-18 19:24:19 +02:00
|
|
|
close_vgmstream(vgmstream);
|
2017-02-04 13:00:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-18 19:24:19 +02:00
|
|
|
* 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.
|
2017-02-04 13:00:13 +01:00
|
|
|
* (a ~3 min movie needs ~6000-7000 frames = fseeks, should be fast enough)
|
|
|
|
*/
|
2017-08-18 19:24:19 +02:00
|
|
|
static int bink_get_info(STREAMFILE *streamFile, int * out_total_streams, int * out_channel_count, int * out_sample_rate, int * out_num_samples) {
|
2017-02-04 13:00:13 +01:00
|
|
|
uint32_t *offsets = NULL;
|
2017-08-18 19:24:19 +02:00
|
|
|
uint32_t num_frames, num_samples_b = 0;
|
2017-02-04 13:00:13 +01:00
|
|
|
off_t cur_offset;
|
2017-08-18 19:24:19 +02:00
|
|
|
int i, j, sample_rate, channel_count;
|
|
|
|
int total_streams, target_stream = streamFile->stream_index;
|
|
|
|
|
|
|
|
size_t filesize = get_streamfile_size(streamFile);
|
|
|
|
uint32_t signature = (read_32bitBE(0x00,streamFile) & 0xffffff00);
|
|
|
|
uint8_t revision = (read_32bitBE(0x00,streamFile) & 0xFF);
|
|
|
|
|
|
|
|
if (read_32bitLE(0x04,streamFile)+ 0x08 != filesize)
|
|
|
|
goto fail;
|
2017-02-04 13:00:13 +01:00
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
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) */
|
2017-02-04 13:00:13 +01:00
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
/* multichannel/multilanguage audio is usually N streams of stereo/mono, no way to know channel layout */
|
|
|
|
total_streams = read_32bitLE(0x28,streamFile);
|
|
|
|
if (target_stream == 0) target_stream = 1;
|
|
|
|
if (target_stream < 0 || target_stream > total_streams || total_streams < 1 || total_streams > 255) goto fail;
|
2017-02-04 13:00:13 +01:00
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
/* find stream info and position in offset table */
|
|
|
|
cur_offset = 0x2c;
|
|
|
|
if ((signature == 0x42494B00 && (revision == 0x6b)) || /* k */
|
|
|
|
(signature == 0x4B423200 && (revision == 0x69 || revision == 0x6a || revision == 0x6b))) /* i,j,k */
|
|
|
|
cur_offset += 0x04; /* unknown v2 header field */
|
|
|
|
cur_offset += 0x04*total_streams; /* skip streams max packet bytes */
|
|
|
|
sample_rate = (uint16_t)read_16bitLE(cur_offset+0x04*(target_stream-1)+0x00,streamFile);
|
|
|
|
channel_count = (uint16_t)read_16bitLE(cur_offset+0x04*(target_stream-1)+0x02,streamFile) & 0x2000 ? 2 : 1; /* stereo flag */
|
|
|
|
cur_offset += 0x04*total_streams; /* skip streams info */
|
|
|
|
cur_offset += 0x04*total_streams; /* skip streams ids */
|
2017-02-04 13:00:13 +01:00
|
|
|
|
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
/* read frame offsets in a buffer, to avoid fseeking to the table back and forth */
|
2017-02-04 13:00:13 +01:00
|
|
|
offsets = malloc(sizeof(uint32_t) * num_frames);
|
2017-02-17 16:39:38 +01:00
|
|
|
if (!offsets) goto fail;
|
|
|
|
|
2017-02-04 13:00:13 +01:00
|
|
|
for (i=0; i < num_frames; i++) {
|
|
|
|
offsets[i] = read_32bitLE(cur_offset,streamFile) & 0xFFFFFFFE; /* mask first bit (= keyframe) */
|
|
|
|
cur_offset += 0x4;
|
|
|
|
|
|
|
|
if (offsets[i] > filesize) goto fail;
|
|
|
|
}
|
|
|
|
/* after the last index is the file size, validate just in case */
|
2017-08-18 19:24:19 +02:00
|
|
|
if (read_32bitLE(cur_offset,streamFile) != filesize) goto fail;
|
2017-02-04 13:00:13 +01:00
|
|
|
|
|
|
|
/* read each frame header and sum all samples
|
2017-08-18 19:24:19 +02:00
|
|
|
* a frame has N audio packets with a header (one per stream) + video packet */
|
2017-02-04 13:00:13 +01:00
|
|
|
for (i=0; i < num_frames; i++) {
|
|
|
|
cur_offset = offsets[i];
|
|
|
|
|
2017-08-18 19:24:19 +02:00
|
|
|
/* read audio packet headers per stream */
|
|
|
|
for (j=0; j < total_streams; j++) {
|
|
|
|
uint32_t ap_size = read_32bitLE(cur_offset+0x00,streamFile); /* not counting this int */
|
|
|
|
|
|
|
|
if (j == target_stream-1) {
|
|
|
|
if (ap_size > 0)
|
|
|
|
num_samples_b += read_32bitLE(cur_offset+0x04,streamFile); /* decoded samples in bytes */
|
|
|
|
break; /* next frame */
|
|
|
|
}
|
|
|
|
else { /* next stream packet or frame */
|
|
|
|
cur_offset += 4 + ap_size; //todo sometimes ap_size doesn't include itself (+4), others it does?
|
2017-02-04 13:00:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(offsets);
|
2017-08-18 19:24:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (out_total_streams) *out_total_streams = total_streams;
|
|
|
|
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?
|
|
|
|
if (out_num_samples) *out_num_samples = num_samples_b / (2 * channel_count);
|
|
|
|
|
|
|
|
return 1;
|
2017-02-04 13:00:13 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(offsets);
|
|
|
|
return 0;
|
|
|
|
}
|