cleanup: move stuff

This commit is contained in:
bnnm 2024-07-27 15:22:21 +02:00
parent 14419cce34
commit 1d0e57583c
9 changed files with 223 additions and 179 deletions

View File

@ -188,7 +188,7 @@ void decode_seek(VGMSTREAM* vgmstream) {
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
if (vgmstream->coding_type == coding_MP4_AAC) {
seek_mp4_aac(vgmstream, vgmstream->loop_sample);
seek_mp4_aac(vgmstream, vgmstream->loop_current_sample);
}
#endif
@ -560,7 +560,7 @@ int decode_get_samples_per_frame(VGMSTREAM* vgmstream) {
return 0; /* ~100 (range), ~16 (DCT) */
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
case coding_MP4_AAC:
return ((mp4_aac_codec_data*)vgmstream->codec_data)->samples_per_frame;
return mp4_get_samples_per_frame(vgmstream->codec_data);
#endif
#ifdef VGM_USE_ATRAC9
case coding_ATRAC9:

View File

@ -330,8 +330,7 @@ static STREAMFILE* get_vgmstream_average_bitrate_channel_streamfile(VGMSTREAM* v
#endif
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
if (vgmstream->coding_type == coding_MP4_AAC) {
mp4_aac_codec_data *data = vgmstream->codec_data;
return data ? data->if_file.streamfile : NULL;
return mp4_aac_get_streamfile(vgmstream->codec_data);
}
#endif

View File

@ -561,10 +561,19 @@ void free_g719(g719_codec_data* data, int channels);
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
/* mp4_aac_decoder */
typedef struct mp4_aac_codec_data mp4_aac_codec_data;
mp4_aac_codec_data* init_mp4_aac(STREAMFILE* sf);
void decode_mp4_aac(mp4_aac_codec_data* data, sample_t* outbuf, int32_t samples_to_do, int channels);
void reset_mp4_aac(VGMSTREAM* vgmstream);
void seek_mp4_aac(VGMSTREAM* vgmstream, int32_t num_sample);
void free_mp4_aac(mp4_aac_codec_data* data);
STREAMFILE* mp4_aac_get_streamfile(mp4_aac_codec_data* data);
int32_t mp4_aac_get_samples(mp4_aac_codec_data* data);
int32_t mp4_aac_get_samples_per_frame(mp4_aac_codec_data* data);
int mp4_aac_get_sample_rate(mp4_aac_codec_data* data);
int mp4_aac_get_channels(mp4_aac_codec_data* data);
#endif

View File

@ -1,6 +1,159 @@
#include "../vgmstream.h"
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
#ifdef VGM_USE_MP4V2
#define MP4V2_NO_STDINT_DEFS
#include <mp4v2/mp4v2.h>
#endif
#ifdef VGM_USE_FDKAAC
#include <aacdecoder_lib.h>
#endif
typedef struct {
STREAMFILE* streamfile;
uint64_t start;
uint64_t offset;
uint64_t size;
} mp4_streamfile;
struct mp4_aac_codec_data {
mp4_streamfile if_file;
MP4FileHandle h_mp4file;
MP4TrackId track_id;
unsigned long sampleId;
unsigned long numSamples;
UINT codec_init_data_size;
HANDLE_AACDECODER h_aacdecoder;
unsigned int sample_ptr;
unsigned int samples_per_frame
unsigned int samples_discard;
INT_PCM sample_buffer[( (6) * (2048)*4 )];
};
// VGM_USE_MP4V2
static void* mp4_file_open( const char* name, MP4FileMode mode ) {
char * endptr;
#ifdef _MSC_VER
unsigned __int64 ptr = _strtoui64( name, &endptr, 16 );
#else
unsigned long ptr = strtoul( name, &endptr, 16 );
#endif
return (void*) ptr;
}
static int mp4_file_seek( void* handle, int64_t pos ) {
mp4_streamfile * file = ( mp4_streamfile * ) handle;
if ( pos > file->size ) pos = file->size;
pos += file->start;
file->offset = pos;
return 0;
}
static int mp4_file_get_size( void* handle, int64_t* size ) {
mp4_streamfile * file = ( mp4_streamfile * ) handle;
*size = file->size;
return 0;
}
static int mp4_file_read( void* handle, void* buffer, int64_t size, int64_t* nin, int64_t maxChunkSize ) {
mp4_streamfile * file = ( mp4_streamfile * ) handle;
int64_t max_size = file->size - file->offset - file->start;
if ( size > max_size ) size = max_size;
if ( size > 0 )
{
*nin = read_streamfile( (uint8_t *) buffer, file->offset, size, file->streamfile );
file->offset += *nin;
}
else
{
*nin = 0;
return 1;
}
return 0;
}
static int mp4_file_write( void* handle, const void* buffer, int64_t size, int64_t* nout, int64_t maxChunkSize ) {
return 1;
}
static int mp4_file_close( void* handle ) {
return 0;
}
MP4FileProvider mp4_file_provider = { mp4_file_open, mp4_file_seek, mp4_file_read, mp4_file_write, mp4_file_close, mp4_file_get_size };
mp4_aac_codec_data* init_mp4_aac(STREAMFILE* sf) {
char filename[PATH_LIMIT];
uint32_t start = 0;
uint32_t size = get_streamfile_size(sf);
CStreamInfo* stream_info = NULL;
uint8_t* buffer = NULL;
uint32_t buffer_size;
UINT ubuffer_size, bytes_valid;
mp4_aac_codec_data* data = calloc(1, sizeof(mp4_aac_codec_data));
if (!data) goto fail;
data->if_file.streamfile = sf;
data->if_file.start = start;
data->if_file.offset = start;
data->if_file.size = size;
/* Big ol' kludge! */
sprintf( filename, "%p", &data->if_file );
data->h_mp4file = MP4ReadProvider( filename, &mp4_file_provider );
if ( !data->h_mp4file ) goto fail;
if ( MP4GetNumberOfTracks(data->h_mp4file, MP4_AUDIO_TRACK_TYPE, '\000') != 1 ) goto fail;
data->track_id = MP4FindTrackId( data->h_mp4file, 0, MP4_AUDIO_TRACK_TYPE, '\000' );
data->h_aacdecoder = aacDecoder_Open( TT_MP4_RAW, 1 );
if ( !data->h_aacdecoder ) goto fail;
MP4GetTrackESConfiguration( data->h_mp4file, data->track_id, (uint8_t**)(&buffer), (uint32_t*)(&buffer_size));
ubuffer_size = buffer_size;
if ( aacDecoder_ConfigRaw( data->h_aacdecoder, &buffer, &ubuffer_size ) ) goto fail;
free( buffer ); buffer = NULL;
data->sampleId = 1;
data->numSamples = MP4GetTrackNumberOfSamples( data->h_mp4file, data->track_id );
if (!MP4ReadSample(data->h_mp4file, data->track_id, data->sampleId, (uint8_t**)(&buffer), (uint32_t*)(&buffer_size), 0, 0, 0, 0)) goto fail;
ubuffer_size = buffer_size;
bytes_valid = buffer_size;
if ( aacDecoder_Fill( data->h_aacdecoder, &buffer, &ubuffer_size, &bytes_valid ) || bytes_valid ) goto fail;
if ( aacDecoder_DecodeFrame( data->h_aacdecoder, data->sample_buffer, ( (6) * (2048)*4 ), 0 ) ) goto fail;
free( buffer ); buffer = NULL;
data->sample_ptr = 0;
stream_info = aacDecoder_GetStreamInfo( data->h_aacdecoder );
data->samples_per_frame = stream_info->frameSize;
data->samples_discard = 0;
sf->get_name( sf, filename, sizeof(filename) );
data->if_file.streamfile = sf->open(sf, filename, 0);
if (!data->if_file.streamfile) goto fail;
return data;
fail:
free( buffer ); buffer = NULL;
free_mp4_aac(data);
}
static void convert_samples(INT_PCM * src, sample * dest, int32_t count) {
int32_t i;
for ( i = 0; i < count; i++ ) {
@ -111,4 +264,38 @@ void free_mp4_aac(mp4_aac_codec_data * data) {
}
}
void mp4_aac_get_streamfile(mp4_aac_codec_data* data) {
if (!data)
return NULL;
return data->if_file.streamfile;
}
int32_t mp4_aac_get_samples(mp4_aac_codec_data* data) {
if (!data)
return 0;
return (int32_t)(data->samples_per_frame * data->numSamples);
}
int32_t mp4_aac_get_samples_per_frame(mp4_aac_codec_data* data) {
if (!data)
return 0;
return (int32_t)(data->samples_per_frame);
}
int mp4_aac_get_sample_rate(mp4_aac_codec_data* data) {
if (!data)
return 0;
CStreamInfo* stream_info = aacDecoder_GetStreamInfo( data->h_aacdecoder );
return stream_info->sample_rate;
}
int mp4_aac_get_channels(mp4_aac_codec_data* data) {
if (!data)
return 0;
CStreamInfo* stream_info = aacDecoder_GetStreamInfo( data->h_aacdecoder );
return stream_info->numChannels;
}
#endif

View File

@ -147,7 +147,6 @@ VGMSTREAM* init_vgmstream_akb(STREAMFILE* sf) {
#ifdef VGM_USE_FFMPEG
case 0x06: { /* M4A with AAC [The World Ends with You (iPad)] */
/* init_vgmstream_akb_mp4 above has priority, but this works fine too */
vgmstream->codec_data = init_ffmpeg_offset(sf, start_offset,stream_size-start_offset);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg;

View File

@ -163,8 +163,7 @@ VGMSTREAM* init_vgmstream_mp4_aac_ffmpeg(STREAMFILE* sf);
#endif
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
VGMSTREAM * init_vgmstream_mp4_aac(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_mp4_aac_offset(STREAMFILE *streamFile, uint64_t start, uint64_t size);
VGMSTREAM* init_vgmstream_mp4_aac(STREAMFILE* sf);
#endif
VGMSTREAM* init_vgmstream_sli_loops(STREAMFILE* sf);

View File

@ -2,159 +2,42 @@
#include "../coding/coding.h"
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
// VGM_USE_MP4V2
void* mp4_file_open( const char* name, MP4FileMode mode )
{
char * endptr;
#ifdef _MSC_VER
unsigned __int64 ptr = _strtoui64( name, &endptr, 16 );
#else
unsigned long ptr = strtoul( name, &endptr, 16 );
#endif
return (void*) ptr;
}
int mp4_file_seek( void* handle, int64_t pos )
{
mp4_streamfile * file = ( mp4_streamfile * ) handle;
if ( pos > file->size ) pos = file->size;
pos += file->start;
file->offset = pos;
return 0;
}
int mp4_file_get_size( void* handle, int64_t* size )
{
mp4_streamfile * file = ( mp4_streamfile * ) handle;
*size = file->size;
return 0;
}
int mp4_file_read( void* handle, void* buffer, int64_t size, int64_t* nin, int64_t maxChunkSize )
{
mp4_streamfile * file = ( mp4_streamfile * ) handle;
int64_t max_size = file->size - file->offset - file->start;
if ( size > max_size ) size = max_size;
if ( size > 0 )
{
*nin = read_streamfile( (uint8_t *) buffer, file->offset, size, file->streamfile );
file->offset += *nin;
}
else
{
*nin = 0;
return 1;
}
return 0;
}
int mp4_file_write( void* handle, const void* buffer, int64_t size, int64_t* nout, int64_t maxChunkSize )
{
return 1;
}
int mp4_file_close( void* handle )
{
return 0;
}
MP4FileProvider mp4_file_provider = { mp4_file_open, mp4_file_seek, mp4_file_read, mp4_file_write, mp4_file_close, mp4_file_get_size };
// VGM_USE_FDKAAC
VGMSTREAM * init_vgmstream_mp4_aac_offset(STREAMFILE *sf, uint64_t start, uint64_t size);
static VGMSTREAM* init_vgmstream_mp4_aac_offset(STREAMFILE* sf, uint64_t start, uint64_t size);
VGMSTREAM * init_vgmstream_mp4_aac(STREAMFILE *sf) {
return init_vgmstream_mp4_aac_offset( sf, 0, sf->get_size(sf) );
VGMSTREAM* init_vgmstream_mp4_aac(STREAMFILE* sf) {
return init_vgmstream_mp4_aac_offset( sf, 0x00, get_streamfile_size(sf));
}
VGMSTREAM * init_vgmstream_mp4_aac_offset(STREAMFILE *sf, uint64_t start, uint64_t size) {
VGMSTREAM * vgmstream = NULL;
static VGMSTREAM* init_vgmstream_mp4_aac_offset(STREAMFILE* sf, uint64_t start, uint64_t size) {
VGMSTREAM* vgmstream = NULL;
mp4_aac_codec_data* data = NULL;
char filename[PATH_LIMIT];
data = init_mp4_aac(sf);
if (!data) goto fail;
mp4_aac_codec_data * aac_file = ( mp4_aac_codec_data * ) calloc(1, sizeof(mp4_aac_codec_data));
int channels = mp4_aac_get_channels(data);
CStreamInfo * stream_info;
uint8_t * buffer = NULL;
uint32_t buffer_size;
UINT ubuffer_size, bytes_valid;
if ( !aac_file ) goto fail;
aac_file->if_file.streamfile = sf;
aac_file->if_file.start = start;
aac_file->if_file.offset = start;
aac_file->if_file.size = size;
/* Big ol' kludge! */
sprintf( filename, "%p", &aac_file->if_file );
aac_file->h_mp4file = MP4ReadProvider( filename, &mp4_file_provider );
if ( !aac_file->h_mp4file ) goto fail;
if ( MP4GetNumberOfTracks(aac_file->h_mp4file, MP4_AUDIO_TRACK_TYPE, '\000') != 1 ) goto fail;
aac_file->track_id = MP4FindTrackId( aac_file->h_mp4file, 0, MP4_AUDIO_TRACK_TYPE, '\000' );
aac_file->h_aacdecoder = aacDecoder_Open( TT_MP4_RAW, 1 );
if ( !aac_file->h_aacdecoder ) goto fail;
MP4GetTrackESConfiguration( aac_file->h_mp4file, aac_file->track_id, (uint8_t**)(&buffer), (uint32_t*)(&buffer_size));
ubuffer_size = buffer_size;
if ( aacDecoder_ConfigRaw( aac_file->h_aacdecoder, &buffer, &ubuffer_size ) ) goto fail;
free( buffer ); buffer = NULL;
aac_file->sampleId = 1;
aac_file->numSamples = MP4GetTrackNumberOfSamples( aac_file->h_mp4file, aac_file->track_id );
if (!MP4ReadSample(aac_file->h_mp4file, aac_file->track_id, aac_file->sampleId, (uint8_t**)(&buffer), (uint32_t*)(&buffer_size), 0, 0, 0, 0)) goto fail;
ubuffer_size = buffer_size;
bytes_valid = buffer_size;
if ( aacDecoder_Fill( aac_file->h_aacdecoder, &buffer, &ubuffer_size, &bytes_valid ) || bytes_valid ) goto fail;
if ( aacDecoder_DecodeFrame( aac_file->h_aacdecoder, aac_file->sample_buffer, ( (6) * (2048)*4 ), 0 ) ) goto fail;
free( buffer ); buffer = NULL;
aac_file->sample_ptr = 0;
stream_info = aacDecoder_GetStreamInfo( aac_file->h_aacdecoder );
aac_file->samples_per_frame = stream_info->frameSize;
aac_file->samples_discard = 0;
sf->get_name( sf, filename, sizeof(filename) );
aac_file->if_file.streamfile = sf->open(sf, filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
if (!aac_file->if_file.streamfile) goto fail;
vgmstream = allocate_vgmstream( stream_info->numChannels, 1 );
vgmstream = allocate_vgmstream(channels, 0);
if (!vgmstream) goto fail;
vgmstream->loop_flag = 0;
vgmstream->codec_data = aac_file;
vgmstream->channels = stream_info->numChannels;
vgmstream->sample_rate = stream_info->sampleRate;
vgmstream->num_samples = stream_info->frameSize * aac_file->numSamples;
vgmstream->sample_rate = mp4_aac_get_sample_rate(data);
vgmstream->num_samples = mp4_aac_get_samples(data);
vgmstream->codec_data = data;
vgmstream->coding_type = coding_MP4_AAC;
vgmstream->layout_type = layout_none;
vgmstream->meta_type = meta_MP4;
return vgmstream;
fail:
if ( buffer ) free( buffer );
if ( aac_file ) {
if ( aac_file->h_aacdecoder ) aacDecoder_Close( aac_file->h_aacdecoder );
if ( aac_file->h_mp4file ) MP4Close( aac_file->h_mp4file, 0 );
free( aac_file );
}
free_mp4_aac(data);
if (vgmstream) {
vgmstream->codec_data = NULL;
close_vgmstream(vgmstream);
}
return NULL;
}
#endif

View File

@ -1,8 +1,8 @@
/*
* vgmstream.h - definitions for VGMSTREAM, encapsulating a multi-channel, looped audio stream
* vgmstream.h - internal definitions for VGMSTREAM, encapsulating a multi-channel, looped audio stream
*/
#ifndef _VGMSTREAM_H
#define _VGMSTREAM_H
#ifndef _VGMSTREAM_H_
#define _VGMSTREAM_H_
/* Due mostly to licensing issues, Vorbis, MPEG, G.722.1, etc decoding is done by external libraries.
* Libs are disabled by default, defined on compile-time for builds that support it */
@ -24,15 +24,6 @@
#include "streamfile.h"
#include "vgmstream_types.h"
#ifdef VGM_USE_MP4V2
#define MP4V2_NO_STDINT_DEFS
#include <mp4v2/mp4v2.h>
#endif
#ifdef VGM_USE_FDKAAC
#include <aacdecoder_lib.h>
#endif
#include "coding/g72x_state.h"
@ -266,26 +257,6 @@ typedef struct {
} layered_layout_data;
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
typedef struct {
STREAMFILE* streamfile;
uint64_t start;
uint64_t offset;
uint64_t size;
} mp4_streamfile;
typedef struct {
mp4_streamfile if_file;
MP4FileHandle h_mp4file;
MP4TrackId track_id;
unsigned long sampleId, numSamples;
UINT codec_init_data_size;
HANDLE_AACDECODER h_aacdecoder;
unsigned int sample_ptr, samples_per_frame, samples_discard;
INT_PCM sample_buffer[( (6) * (2048)*4 )];
} mp4_aac_codec_data;
#endif
// VGMStream description in structure format
typedef struct {
int sample_rate;

View File

@ -237,11 +237,8 @@ init_vgmstream_t init_vgmstream_functions[] = {
init_vgmstream_kt_wiibgm,
init_vgmstream_bfstm,
init_vgmstream_mca,
#if 0
init_vgmstream_mp4_aac,
#endif
#if defined(VGM_USE_MP4V2) && defined(VGM_USE_FDKAAC)
init_vgmstream_akb_mp4,
init_vgmstream_mp4_aac,
#endif
init_vgmstream_ktss,
init_vgmstream_hca,