2012-08-30 14:35:12 +02:00
|
|
|
#include "meta.h"
|
2017-05-19 17:21:22 +02:00
|
|
|
#include "../coding/coding.h"
|
2012-08-30 14:35:12 +02:00
|
|
|
|
2016-12-06 17:12:56 +01:00
|
|
|
#ifdef VGM_USE_FFMPEG
|
2021-05-25 23:41:19 +02:00
|
|
|
typedef struct {
|
2021-07-23 18:55:03 +02:00
|
|
|
int channels;
|
|
|
|
int sample_rate;
|
2021-05-25 23:41:19 +02:00
|
|
|
int32_t num_samples;
|
|
|
|
int loop_flag;
|
|
|
|
int32_t loop_start;
|
|
|
|
int32_t loop_end;
|
|
|
|
int32_t encoder_delay;
|
2021-07-23 18:55:03 +02:00
|
|
|
int subsongs;
|
2021-05-25 23:41:19 +02:00
|
|
|
} mp4_header;
|
2017-02-04 11:10:02 +01:00
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
static void parse_mp4(STREAMFILE* sf, mp4_header* mp4);
|
2017-02-04 11:10:02 +01:00
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
|
|
|
|
VGMSTREAM* init_vgmstream_mp4_aac_ffmpeg(STREAMFILE* sf) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
2016-12-06 17:12:56 +01:00
|
|
|
off_t start_offset = 0;
|
2021-05-25 23:41:19 +02:00
|
|
|
mp4_header mp4 = {0};
|
|
|
|
size_t file_size;
|
|
|
|
ffmpeg_codec_data* ffmpeg_data = NULL;
|
2016-12-06 17:12:56 +01:00
|
|
|
|
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
/* checks */
|
|
|
|
if ((read_u32be(0x00,sf) & 0xFFFFFF00) != 0) /* first atom BE size (usually ~0x18) */
|
|
|
|
goto fail;
|
|
|
|
if (!is_id32be(0x04,sf, "ftyp"))
|
2016-12-06 17:12:56 +01:00
|
|
|
goto fail;
|
|
|
|
|
2021-10-02 18:15:45 +02:00
|
|
|
/* .bin: Final Fantasy Dimensions (iOS), Final Fantasy V (iOS)
|
|
|
|
* .msd: UNO (iOS) */
|
|
|
|
if (!check_extensions(sf,"mp4,m4a,m4v,lmp4,bin,lbin,msd"))
|
|
|
|
goto fail;
|
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
file_size = get_streamfile_size(sf);
|
2016-12-06 17:12:56 +01:00
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
ffmpeg_data = init_ffmpeg_offset(sf, start_offset, file_size);
|
|
|
|
if (!ffmpeg_data) goto fail;
|
|
|
|
|
|
|
|
parse_mp4(sf, &mp4);
|
2016-12-06 17:12:56 +01:00
|
|
|
|
2021-07-23 18:55:03 +02:00
|
|
|
/* most values aren't read directly and use FFmpeg b/c MP4 makes things hard */
|
|
|
|
if (!mp4.num_samples)
|
|
|
|
mp4.num_samples = ffmpeg_get_samples(ffmpeg_data); /* does this take into account encoder delay? see FFV */
|
|
|
|
if (!mp4.channels)
|
|
|
|
mp4.channels = ffmpeg_get_channels(ffmpeg_data);
|
|
|
|
if (!mp4.sample_rate)
|
|
|
|
mp4.sample_rate = ffmpeg_get_sample_rate(ffmpeg_data);
|
|
|
|
if (!mp4.subsongs)
|
|
|
|
mp4.subsongs = ffmpeg_get_subsong_count(ffmpeg_data); /* may contain N tracks */
|
|
|
|
|
2017-02-04 11:10:02 +01:00
|
|
|
|
2016-12-06 17:12:56 +01:00
|
|
|
/* build the VGMSTREAM */
|
2021-07-23 18:55:03 +02:00
|
|
|
vgmstream = allocate_vgmstream(mp4.channels, mp4.loop_flag);
|
2016-12-06 17:12:56 +01:00
|
|
|
if (!vgmstream) goto fail;
|
2021-05-25 23:41:19 +02:00
|
|
|
|
|
|
|
vgmstream->meta_type = meta_MP4;
|
2021-07-23 18:55:03 +02:00
|
|
|
vgmstream->sample_rate = mp4.sample_rate;
|
2021-05-25 23:41:19 +02:00
|
|
|
vgmstream->num_samples = mp4.num_samples;
|
|
|
|
vgmstream->loop_start_sample = mp4.loop_start;
|
|
|
|
vgmstream->loop_end_sample = mp4.loop_end;
|
|
|
|
|
2017-02-04 11:10:02 +01:00
|
|
|
vgmstream->codec_data = ffmpeg_data;
|
2016-12-06 17:12:56 +01:00
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
2021-07-23 18:55:03 +02:00
|
|
|
vgmstream->num_streams = mp4.subsongs;
|
2016-12-06 17:12:56 +01:00
|
|
|
|
2021-07-23 18:55:03 +02:00
|
|
|
vgmstream->channel_layout = ffmpeg_get_channel_layout(ffmpeg_data);
|
2021-07-23 15:52:05 +02:00
|
|
|
|
|
|
|
/* needed for CRI MP4, otherwise FFmpeg usually reads standard delay */
|
|
|
|
ffmpeg_set_skip_samples(vgmstream->codec_data, mp4.encoder_delay);
|
2019-03-02 15:57:41 +01:00
|
|
|
|
2016-12-06 17:12:56 +01:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2021-07-23 18:55:03 +02:00
|
|
|
free_ffmpeg(ffmpeg_data);
|
|
|
|
if (vgmstream) {
|
|
|
|
vgmstream->codec_data = NULL;
|
|
|
|
close_vgmstream(vgmstream);
|
2016-12-06 17:12:56 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
/* read useful MP4 chunks */
|
|
|
|
static void parse_mp4(STREAMFILE* sf, mp4_header* mp4) {
|
2021-10-02 18:15:45 +02:00
|
|
|
uint32_t offset, suboffset, max_offset, max_suboffset;
|
2021-05-25 23:41:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* MOV format chunks, called "atoms", size goes first because Apple */
|
|
|
|
offset = 0x00;
|
|
|
|
max_offset = get_streamfile_size(sf);
|
|
|
|
while (offset < max_offset) {
|
|
|
|
uint32_t size = read_u32be(offset + 0x00,sf);
|
|
|
|
uint32_t type = read_u32be(offset + 0x04,sf);
|
|
|
|
//offset += 0x08;
|
|
|
|
|
|
|
|
/* just in case */
|
|
|
|
if (size == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case 0x66726565: /* "free" */
|
|
|
|
/* Tales of Hearts R (iOS) has loop info in the first "free" atom */
|
|
|
|
if (read_u32be(offset + 0x08,sf) == 0x4F700002 && (size == 0x38 || size == 0x40)) {
|
|
|
|
/* 0x00: id / "Op" */
|
|
|
|
/* 0x02: channels */
|
|
|
|
/* 0x04/8: sample rate */
|
|
|
|
/* 0x0c: null? */
|
|
|
|
/* 0x10: num_samples (without padding, same as FFmpeg's) */
|
|
|
|
/* 0x14/18/1c/20: offsets to stream info (stts/stsc/stsz/stco) */
|
|
|
|
mp4->encoder_delay = read_u32be(offset + 0x08 + 0x24,sf); /* Apple's 2112 */
|
|
|
|
mp4->loop_flag = read_u32be(offset + 0x08 + 0x28,sf);
|
|
|
|
if (mp4->loop_flag) { /* atom ends if no loop flag */
|
|
|
|
mp4->loop_start = read_u32be(offset + 0x08 + 0x2c,sf);
|
|
|
|
mp4->loop_end = read_u32be(offset + 0x08 + 0x30,sf);
|
|
|
|
}
|
2021-10-02 18:15:45 +02:00
|
|
|
max_offset = 0;
|
|
|
|
}
|
|
|
|
/* M2 emu's .m4a (codename zoom) */
|
|
|
|
else if (is_id32be(offset + 0x08 + 0x00,sf, "ZOOM")) {
|
|
|
|
/* 0x00: id */
|
|
|
|
mp4->encoder_delay = read_s32be(offset + 0x08 + 0x04,sf); /* Apple's 2112, also in iTunes tag */
|
|
|
|
/* 0x08: end padding */
|
|
|
|
mp4->num_samples = read_s32be(offset + 0x08 + 0x0c,sf);
|
|
|
|
mp4->loop_start = read_s32be(offset + 0x08 + 0x10,sf);
|
|
|
|
mp4->loop_end = read_s32be(offset + 0x08 + 0x14,sf);
|
|
|
|
mp4->loop_flag = (mp4->loop_end != 0);
|
|
|
|
if (mp4->loop_flag)
|
|
|
|
mp4->loop_end++; /* assumed, matches num_samples this way */
|
|
|
|
max_offset = 0;
|
2021-05-25 23:41:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x6D6F6F76: { /* "moov" (header) */
|
2021-10-02 18:15:45 +02:00
|
|
|
suboffset = offset + 0x08;
|
2021-05-25 23:41:19 +02:00
|
|
|
max_suboffset = offset + size;
|
|
|
|
while (suboffset < max_suboffset) {
|
|
|
|
uint32_t subsize = read_u32be(suboffset + 0x00,sf);
|
|
|
|
uint32_t subtype = read_u32be(suboffset + 0x04,sf);
|
|
|
|
|
|
|
|
/* padded in ToRR */
|
|
|
|
if (subsize == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch(subtype) {
|
|
|
|
case 0x75647461: /* "udta" */
|
|
|
|
/* CRI subchunk [Imperial SaGa Eclipse (Browser)]
|
|
|
|
* incidentally "moov" header comes after data ("mdat") in CRI's files */
|
|
|
|
if (subsize >= 0x28 && is_id32be(suboffset + 0x08 + 0x04,sf, "criw")) {
|
|
|
|
off_t criw_offset = suboffset + 0x08 + 0x08;
|
|
|
|
|
|
|
|
mp4->loop_start = read_s32be(criw_offset + 0x00,sf);
|
|
|
|
mp4->loop_end = read_s32be(criw_offset + 0x04,sf);
|
|
|
|
mp4->encoder_delay = read_s32be(criw_offset + 0x08,sf); /* Apple's 2112 */
|
|
|
|
mp4->num_samples = read_s32be(criw_offset + 0x0c,sf);
|
|
|
|
mp4->loop_flag = (mp4->loop_end > 0);
|
|
|
|
/* next 2 fields are null */
|
2021-10-02 18:15:45 +02:00
|
|
|
max_offset = 0;
|
2021-05-25 23:41:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
suboffset += subsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2017-02-04 11:10:02 +01:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
offset += size; /* atoms don't seem to need to padding byte, unlike RIFF */
|
2017-02-04 11:10:02 +01:00
|
|
|
}
|
2021-05-25 23:41:19 +02:00
|
|
|
}
|
2017-02-04 11:10:02 +01:00
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
/* CRI's encryption info (for lack of a better place) [Final Fantasy Digital Card Game (Browser)]
|
|
|
|
*
|
|
|
|
* Like other CRI stuff their MP4 can be encrypted, from file's beginning (including headers).
|
|
|
|
* This is more or less how data is decrypted (supposedly, from decompilations), for reference:
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
void criAacCodec_SetDecryptionKey(uint64_t keycode, uint16_t* key) {
|
|
|
|
if (!keycode)
|
|
|
|
return;
|
|
|
|
uint16_t k0 = 4 * ((keycode >> 0) & 0x0FFF) | 1;
|
|
|
|
uint16_t k1 = 2 * ((keycode >> 12) & 0x1FFF) | 1;
|
|
|
|
uint16_t k2 = 4 * ((keycode >> 25) & 0x1FFF) | 1;
|
|
|
|
uint16_t k3 = 2 * ((keycode >> 38) & 0x3FFF) | 1;
|
|
|
|
|
|
|
|
key[0] = k0 ^ k1;
|
|
|
|
key[1] = k1 ^ k2;
|
|
|
|
key[2] = k2 ^ k3;
|
|
|
|
key[3] = ~k3;
|
|
|
|
|
|
|
|
/* criatomexacb_generate_aac_decryption_key is slightly different, unsure which one is used: */
|
|
|
|
//key[0] = k0 ^ k3;
|
|
|
|
//key[1] = k2 ^ k3;
|
|
|
|
//key[2] = k2 ^ k3;
|
|
|
|
//key[3] = ~k3;
|
2017-02-04 11:10:02 +01:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:41:19 +02:00
|
|
|
void criAacCodec_DecryptData(const uint16_t* key, uint8_t* data, uint32_t size) {
|
|
|
|
if (data_size)
|
|
|
|
return;
|
|
|
|
uint16_t seed0 = ~key[3];
|
|
|
|
uint16_t seed1 = seed0 ^ key[2];
|
|
|
|
uint16_t seed2 = seed1 ^ key[1];
|
|
|
|
uint16_t seed3 = seed2 ^ key[0];
|
|
|
|
|
|
|
|
uint16_t xor = 2 * seed0 | 1;
|
|
|
|
uint16_t add = 2 * seed0 | 1; /* not seed1 */
|
|
|
|
uint16_t mul = 4 * seed2 | 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < data_size; i++) {
|
|
|
|
|
|
|
|
if (!(uint16_t)i) { /* every 0x10000, without modulo */
|
|
|
|
mul = (4 * seed2 + seed3 * (mul & 0xFFFC)) & 0xFFFD | 1;
|
|
|
|
add = (2 * seed0 + seed1 * (add & 0xFFFE)) | 1;
|
|
|
|
}
|
|
|
|
xor = xor * mul + add;
|
|
|
|
|
|
|
|
*data ^= (xor >> 8) & 0xFF;
|
|
|
|
++data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-06 17:12:56 +01:00
|
|
|
#endif
|