2014-03-23 19:45:08 +01:00
|
|
|
// Copyright (c) 2013- PPSSPP Project.
|
|
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
2014-04-12 23:16:38 +02:00
|
|
|
#include <algorithm>
|
2014-06-23 22:51:22 +02:00
|
|
|
|
2014-04-12 17:16:31 +02:00
|
|
|
#include "Core/Config.h"
|
2014-04-11 15:09:31 +02:00
|
|
|
#include "Core/HLE/FunctionWrappers.h"
|
2014-03-23 19:45:08 +01:00
|
|
|
#include "Core/HW/SimpleAudioDec.h"
|
|
|
|
|
#include "Core/HW/MediaEngine.h"
|
|
|
|
|
#include "Core/HW/BufferQueue.h"
|
|
|
|
|
|
|
|
|
|
#ifdef USE_FFMPEG
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
2015-02-03 17:46:51 -02:00
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
|
#include "libswresample/swresample.h"
|
|
|
|
|
#include "libavutil/samplefmt.h"
|
2014-03-23 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // USE_FFMPEG
|
|
|
|
|
|
2015-01-18 12:59:26 -08:00
|
|
|
int SimpleAudio::GetAudioCodecID(int audioType) {
|
2014-03-23 19:45:08 +01:00
|
|
|
#ifdef USE_FFMPEG
|
2014-05-29 08:30:00 -07:00
|
|
|
switch (audioType) {
|
2014-03-23 19:45:08 +01:00
|
|
|
case PSP_CODEC_AAC:
|
2015-01-18 12:59:26 -08:00
|
|
|
return AV_CODEC_ID_AAC;
|
2014-03-23 19:45:08 +01:00
|
|
|
case PSP_CODEC_AT3:
|
2015-01-18 12:59:26 -08:00
|
|
|
return AV_CODEC_ID_ATRAC3;
|
2014-03-23 19:45:08 +01:00
|
|
|
case PSP_CODEC_AT3PLUS:
|
2015-01-18 12:59:26 -08:00
|
|
|
return AV_CODEC_ID_ATRAC3P;
|
2014-03-23 19:45:08 +01:00
|
|
|
case PSP_CODEC_MP3:
|
2015-01-18 12:59:26 -08:00
|
|
|
return AV_CODEC_ID_MP3;
|
2014-03-23 19:45:08 +01:00
|
|
|
default:
|
2015-01-18 12:59:26 -08:00
|
|
|
return AV_CODEC_ID_NONE;
|
2014-03-23 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
#else
|
2015-01-18 12:59:26 -08:00
|
|
|
return 0;
|
2014-03-23 19:45:08 +01:00
|
|
|
#endif // USE_FFMPEG
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-22 18:33:09 +02:00
|
|
|
SimpleAudio::SimpleAudio(int audioType, int sample_rate, int channels)
|
2015-01-18 12:59:26 -08:00
|
|
|
: ctxPtr(0xFFFFFFFF), audioType(audioType), sample_rate_(sample_rate), channels_(channels),
|
|
|
|
|
outSamples(0), srcPos(0), wanted_resample_freq(44100), frame_(0), codec_(0), codecCtx_(0), swrCtx_(0),
|
2015-10-10 18:37:15 -07:00
|
|
|
codecOpen_(false) {
|
2014-05-29 08:30:00 -07:00
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimpleAudio::Init() {
|
2014-03-23 19:45:08 +01:00
|
|
|
#ifdef USE_FFMPEG
|
2014-03-29 19:36:53 +01:00
|
|
|
avcodec_register_all();
|
|
|
|
|
av_register_all();
|
|
|
|
|
InitFFmpeg();
|
|
|
|
|
|
2014-03-23 19:45:08 +01:00
|
|
|
frame_ = av_frame_alloc();
|
|
|
|
|
|
2014-04-11 15:09:31 +02:00
|
|
|
// Get Audio Codec ctx
|
2015-01-18 12:59:26 -08:00
|
|
|
int audioCodecId = GetAudioCodecID(audioType);
|
|
|
|
|
if (!audioCodecId) {
|
2014-03-23 19:45:08 +01:00
|
|
|
ERROR_LOG(ME, "This version of FFMPEG does not support Audio codec type: %08x. Update your submodule.", audioType);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Find decoder
|
2014-06-22 14:23:06 +02:00
|
|
|
codec_ = avcodec_find_decoder((AVCodecID)audioCodecId);
|
2014-03-23 19:45:08 +01:00
|
|
|
if (!codec_) {
|
|
|
|
|
// Eh, we shouldn't even have managed to compile. But meh.
|
2014-04-11 15:09:31 +02:00
|
|
|
ERROR_LOG(ME, "This version of FFMPEG does not support AV_CODEC_ctx for audio (%s). Update your submodule.", GetCodecName(audioType));
|
2014-03-23 19:45:08 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Allocate codec context
|
|
|
|
|
codecCtx_ = avcodec_alloc_context3(codec_);
|
|
|
|
|
if (!codecCtx_) {
|
|
|
|
|
ERROR_LOG(ME, "Failed to allocate a codec context");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-06-22 18:33:09 +02:00
|
|
|
codecCtx_->channels = channels_;
|
|
|
|
|
codecCtx_->channel_layout = channels_ == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
|
|
|
|
|
codecCtx_->sample_rate = sample_rate_;
|
2015-01-18 12:59:26 -08:00
|
|
|
codecOpen_ = false;
|
2014-06-22 09:55:14 -07:00
|
|
|
#endif // USE_FFMPEG
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 12:59:26 -08:00
|
|
|
bool SimpleAudio::OpenCodec(int block_align) {
|
2014-06-22 09:55:14 -07:00
|
|
|
#ifdef USE_FFMPEG
|
2015-01-18 12:59:26 -08:00
|
|
|
// Some versions of FFmpeg require this set. May be set in SetExtraData(), but optional.
|
|
|
|
|
// When decoding, we decode by packet, so we know the size.
|
|
|
|
|
if (codecCtx_->block_align == 0) {
|
|
|
|
|
codecCtx_->block_align = block_align;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 19:45:08 +01:00
|
|
|
AVDictionary *opts = 0;
|
2014-06-23 21:42:31 +02:00
|
|
|
int retval = avcodec_open2(codecCtx_, codec_, &opts);
|
|
|
|
|
if (retval < 0) {
|
2014-06-23 22:51:22 +02:00
|
|
|
ERROR_LOG(ME, "Failed to open codec: retval = %i", retval);
|
2014-03-23 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
av_dict_free(&opts);
|
2015-01-18 12:59:26 -08:00
|
|
|
codecOpen_ = true;
|
2014-06-23 21:42:31 +02:00
|
|
|
return retval >= 0;
|
2015-01-18 12:59:26 -08:00
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif // USE_FFMPEG
|
2014-03-23 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-22 09:55:14 -07:00
|
|
|
void SimpleAudio::SetExtraData(u8 *data, int size, int wav_bytes_per_packet) {
|
|
|
|
|
#ifdef USE_FFMPEG
|
|
|
|
|
if (codecCtx_) {
|
2015-10-10 18:37:15 -07:00
|
|
|
codecCtx_->extradata = (uint8_t *)av_mallocz(size);
|
2014-06-22 09:55:14 -07:00
|
|
|
codecCtx_->extradata_size = size;
|
|
|
|
|
codecCtx_->block_align = wav_bytes_per_packet;
|
2015-01-18 12:59:26 -08:00
|
|
|
codecOpen_ = false;
|
2015-10-10 18:37:15 -07:00
|
|
|
|
|
|
|
|
if (data != nullptr) {
|
|
|
|
|
memcpy(codecCtx_->extradata, data, size);
|
|
|
|
|
}
|
2014-06-22 09:55:14 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 23:19:03 -08:00
|
|
|
void SimpleAudio::SetChannels(int channels) {
|
|
|
|
|
if (channels_ == channels) {
|
|
|
|
|
// Do nothing, already set.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-02-24 18:59:41 +01:00
|
|
|
#ifdef USE_FFMPEG
|
2016-01-21 23:19:03 -08:00
|
|
|
|
|
|
|
|
if (codecOpen_) {
|
|
|
|
|
ERROR_LOG(ME, "Codec already open, cannot change channels");
|
|
|
|
|
} else {
|
|
|
|
|
channels_ = channels;
|
|
|
|
|
codecCtx_->channels = channels_;
|
|
|
|
|
codecCtx_->channel_layout = channels_ == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
|
|
|
|
|
}
|
2017-02-24 18:59:41 +01:00
|
|
|
#endif
|
2016-01-21 23:19:03 -08:00
|
|
|
}
|
|
|
|
|
|
2014-03-23 19:45:08 +01:00
|
|
|
SimpleAudio::~SimpleAudio() {
|
|
|
|
|
#ifdef USE_FFMPEG
|
2015-10-10 18:37:15 -07:00
|
|
|
swr_free(&swrCtx_);
|
|
|
|
|
av_frame_free(&frame_);
|
2015-10-10 19:44:39 -07:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 52, 0)
|
2015-10-10 18:37:15 -07:00
|
|
|
avcodec_free_context(&codecCtx_);
|
2015-10-10 19:44:39 -07:00
|
|
|
#else
|
|
|
|
|
// Future versions may add other things to free, but avcodec_free_context didn't exist yet here.
|
|
|
|
|
avcodec_close(codecCtx_);
|
|
|
|
|
av_freep(&codecCtx_->extradata);
|
|
|
|
|
av_freep(&codecCtx_->subtitle_header);
|
|
|
|
|
av_freep(&codecCtx_);
|
|
|
|
|
#endif
|
2014-03-23 19:45:08 +01:00
|
|
|
codec_ = 0;
|
|
|
|
|
#endif // USE_FFMPEG
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-22 14:01:23 +02:00
|
|
|
bool SimpleAudio::IsOK() const {
|
|
|
|
|
#ifdef USE_FFMPEG
|
|
|
|
|
return codec_ != 0;
|
|
|
|
|
#else
|
|
|
|
|
return 0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 12:59:26 -08:00
|
|
|
bool SimpleAudio::Decode(void *inbuf, int inbytes, uint8_t *outbuf, int *outbytes) {
|
2014-03-23 19:45:08 +01:00
|
|
|
#ifdef USE_FFMPEG
|
2015-01-18 12:59:26 -08:00
|
|
|
if (!codecOpen_) {
|
|
|
|
|
OpenCodec(inbytes);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-08 01:09:28 +02:00
|
|
|
AVPacket packet;
|
2014-03-23 19:45:08 +01:00
|
|
|
av_init_packet(&packet);
|
|
|
|
|
packet.data = static_cast<uint8_t *>(inbuf);
|
|
|
|
|
packet.size = inbytes;
|
|
|
|
|
|
|
|
|
|
int got_frame = 0;
|
|
|
|
|
av_frame_unref(frame_);
|
2014-04-11 15:09:31 +02:00
|
|
|
|
|
|
|
|
*outbytes = 0;
|
2014-04-12 02:12:25 +02:00
|
|
|
srcPos = 0;
|
|
|
|
|
int len = avcodec_decode_audio4(codecCtx_, frame_, &got_frame, &packet);
|
2016-05-28 10:11:53 -07:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
|
|
|
|
|
av_packet_unref(&packet);
|
|
|
|
|
#else
|
|
|
|
|
av_free_packet(&packet);
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-04-12 02:12:25 +02:00
|
|
|
if (len < 0) {
|
2014-06-22 18:33:09 +02:00
|
|
|
ERROR_LOG(ME, "Error decoding Audio frame (%i bytes): %i (%08x)", inbytes, len, len);
|
2014-03-23 19:45:08 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2014-04-12 02:12:25 +02:00
|
|
|
|
|
|
|
|
// get bytes consumed in source
|
|
|
|
|
srcPos = len;
|
2014-04-11 15:09:31 +02:00
|
|
|
|
2014-03-23 19:45:08 +01:00
|
|
|
if (got_frame) {
|
|
|
|
|
// Initializing the sample rate convert. We will use it to convert float output into int.
|
|
|
|
|
int64_t wanted_channel_layout = AV_CH_LAYOUT_STEREO; // we want stereo output layout
|
|
|
|
|
int64_t dec_channel_layout = frame_->channel_layout; // decoded channel layout
|
|
|
|
|
|
2014-06-22 17:50:47 +02:00
|
|
|
if (!swrCtx_) {
|
|
|
|
|
swrCtx_ = swr_alloc_set_opts(
|
|
|
|
|
swrCtx_,
|
|
|
|
|
wanted_channel_layout,
|
|
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
|
wanted_resample_freq,
|
|
|
|
|
dec_channel_layout,
|
|
|
|
|
codecCtx_->sample_fmt,
|
|
|
|
|
codecCtx_->sample_rate,
|
|
|
|
|
0,
|
|
|
|
|
NULL);
|
2014-03-23 19:45:08 +01:00
|
|
|
|
2014-06-22 17:50:47 +02:00
|
|
|
if (!swrCtx_ || swr_init(swrCtx_) < 0) {
|
|
|
|
|
ERROR_LOG(ME, "swr_init: Failed to initialize the resampling context");
|
|
|
|
|
avcodec_close(codecCtx_);
|
|
|
|
|
codec_ = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-23 19:45:08 +01:00
|
|
|
}
|
2014-06-22 17:50:47 +02:00
|
|
|
|
2014-03-23 19:45:08 +01:00
|
|
|
// convert audio to AV_SAMPLE_FMT_S16
|
|
|
|
|
int swrRet = swr_convert(swrCtx_, &outbuf, frame_->nb_samples, (const u8 **)frame_->extended_data, frame_->nb_samples);
|
|
|
|
|
if (swrRet < 0) {
|
2014-06-23 21:42:31 +02:00
|
|
|
ERROR_LOG(ME, "swr_convert: Error while converting: %d", swrRet);
|
2014-03-23 19:45:08 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2014-04-08 01:09:28 +02:00
|
|
|
// output samples per frame, we should *2 since we have two channels
|
2014-04-11 15:09:31 +02:00
|
|
|
outSamples = swrRet * 2;
|
2014-04-08 01:09:28 +02:00
|
|
|
|
|
|
|
|
// each sample occupies 2 bytes
|
|
|
|
|
*outbytes = outSamples * 2;
|
2014-03-23 19:45:08 +01:00
|
|
|
|
2014-04-08 01:09:28 +02:00
|
|
|
// Save outbuf into pcm audio, you can uncomment this line to save and check the decoded audio into pcm file.
|
|
|
|
|
// SaveAudio("dump.pcm", outbuf, *outbytes);
|
|
|
|
|
}
|
2014-03-23 19:45:08 +01:00
|
|
|
return true;
|
|
|
|
|
#else
|
|
|
|
|
// Zero bytes output. No need to memset.
|
|
|
|
|
*outbytes = 0;
|
|
|
|
|
return true;
|
|
|
|
|
#endif // USE_FFMPEG
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 12:59:26 -08:00
|
|
|
int SimpleAudio::GetOutSamples() {
|
2014-04-11 15:09:31 +02:00
|
|
|
return outSamples;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 12:59:26 -08:00
|
|
|
int SimpleAudio::GetSourcePos() {
|
2014-04-11 15:09:31 +02:00
|
|
|
return srcPos;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 19:45:08 +01:00
|
|
|
void AudioClose(SimpleAudio **ctx) {
|
|
|
|
|
#ifdef USE_FFMPEG
|
|
|
|
|
delete *ctx;
|
|
|
|
|
*ctx = 0;
|
|
|
|
|
#endif // USE_FFMPEG
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-22 14:01:23 +02:00
|
|
|
|
|
|
|
|
static const char *const codecNames[4] = {
|
|
|
|
|
"AT3+", "AT3", "MP3", "AAC",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char *GetCodecName(int codec) {
|
|
|
|
|
if (codec >= PSP_CODEC_AT3PLUS && codec <= PSP_CODEC_AAC) {
|
|
|
|
|
return codecNames[codec - PSP_CODEC_AT3PLUS];
|
|
|
|
|
} else {
|
|
|
|
|
return "(unk)";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool IsValidCodec(int codec){
|
2014-03-23 19:45:08 +01:00
|
|
|
if (codec >= PSP_CODEC_AT3PLUS && codec <= PSP_CODEC_AAC) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-04-08 01:09:28 +02:00
|
|
|
|
2014-04-11 15:09:31 +02:00
|
|
|
|
|
|
|
|
// sceAu module starts from here
|
|
|
|
|
|
2014-06-22 14:01:23 +02:00
|
|
|
AuCtx::AuCtx() {
|
|
|
|
|
decoder = NULL;
|
|
|
|
|
startPos = 0;
|
|
|
|
|
endPos = 0;
|
|
|
|
|
LoopNum = -1;
|
|
|
|
|
AuBuf = 0;
|
|
|
|
|
AuBufSize = 2048;
|
|
|
|
|
PCMBuf = 0;
|
|
|
|
|
PCMBufSize = 2048;
|
|
|
|
|
AuBufAvailable = 0;
|
|
|
|
|
SamplingRate = 44100;
|
|
|
|
|
freq = SamplingRate;
|
|
|
|
|
BitRate = 0;
|
|
|
|
|
Channels = 2;
|
|
|
|
|
Version = 0;
|
|
|
|
|
SumDecodedSamples = 0;
|
|
|
|
|
MaxOutputSample = 0;
|
|
|
|
|
askedReadSize = 0;
|
|
|
|
|
realReadSize = 0;
|
|
|
|
|
audioType = 0;
|
|
|
|
|
FrameNum = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AuCtx::~AuCtx(){
|
|
|
|
|
if (decoder){
|
|
|
|
|
AudioClose(&decoder);
|
|
|
|
|
decoder = NULL;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-11 15:09:31 +02:00
|
|
|
// return output pcm size, <0 error
|
2014-04-12 02:12:25 +02:00
|
|
|
u32 AuCtx::AuDecode(u32 pcmAddr)
|
2014-04-11 15:09:31 +02:00
|
|
|
{
|
|
|
|
|
if (!Memory::IsValidAddress(pcmAddr)){
|
|
|
|
|
ERROR_LOG(ME, "%s: output bufferAddress %08x is invalctx", __FUNCTION__, pcmAddr);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto outbuf = Memory::GetPointer(PCMBuf);
|
2014-04-13 14:38:42 +02:00
|
|
|
memset(outbuf, 0, PCMBufSize); // important! empty outbuf to avoid noise
|
2014-04-11 15:09:31 +02:00
|
|
|
u32 outpcmbufsize = 0;
|
|
|
|
|
|
2014-04-12 23:16:38 +02:00
|
|
|
int repeat = 1;
|
|
|
|
|
if (g_Config.bSoundSpeedHack){
|
|
|
|
|
repeat = 2;
|
|
|
|
|
}
|
|
|
|
|
int i = 0;
|
|
|
|
|
// decode frames in sourcebuff and output into PCMBuf (each time, we decode one or two frames)
|
|
|
|
|
// some games as Miku like one frame each time, some games like DOA like two frames each time
|
|
|
|
|
while (sourcebuff.size() > 0 && outpcmbufsize < PCMBufSize && i < repeat){
|
|
|
|
|
i++;
|
2014-04-11 15:09:31 +02:00
|
|
|
int pcmframesize;
|
|
|
|
|
// decode
|
2014-04-12 23:16:38 +02:00
|
|
|
decoder->Decode((void*)sourcebuff.c_str(), (int)sourcebuff.size(), outbuf, &pcmframesize);
|
2014-04-11 15:09:31 +02:00
|
|
|
if (pcmframesize == 0){
|
2014-04-12 23:16:38 +02:00
|
|
|
// no output pcm, we are at the end of the stream
|
2014-04-13 14:38:42 +02:00
|
|
|
AuBufAvailable = 0;
|
|
|
|
|
sourcebuff.clear();
|
|
|
|
|
if (LoopNum != 0){
|
|
|
|
|
// if we loop, reset readPos
|
|
|
|
|
readPos = startPos;
|
|
|
|
|
}
|
2014-04-12 17:16:31 +02:00
|
|
|
break;
|
2014-04-11 15:09:31 +02:00
|
|
|
}
|
|
|
|
|
// count total output pcm size
|
|
|
|
|
outpcmbufsize += pcmframesize;
|
|
|
|
|
// count total output samples
|
2014-06-22 17:02:04 +02:00
|
|
|
SumDecodedSamples += decoder->GetOutSamples();
|
2014-04-12 23:16:38 +02:00
|
|
|
// get consumed source length
|
2014-06-22 17:02:04 +02:00
|
|
|
int srcPos = decoder->GetSourcePos();
|
2014-04-12 23:16:38 +02:00
|
|
|
// remove the consumed source
|
|
|
|
|
sourcebuff.erase(0, srcPos);
|
|
|
|
|
// reduce the available Aubuff size
|
2014-04-13 14:38:42 +02:00
|
|
|
// (the available buff size is now used to know if we can read again from file and how many to read)
|
2014-04-11 15:09:31 +02:00
|
|
|
AuBufAvailable -= srcPos;
|
|
|
|
|
// move outbuff position to the current end of output
|
|
|
|
|
outbuf += pcmframesize;
|
2014-04-14 11:47:28 +02:00
|
|
|
// increase FrameNum count
|
|
|
|
|
FrameNum++;
|
2014-04-11 15:09:31 +02:00
|
|
|
}
|
|
|
|
|
Memory::Write_U32(PCMBuf, pcmAddr);
|
|
|
|
|
return outpcmbufsize;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-12 02:12:25 +02:00
|
|
|
u32 AuCtx::AuGetLoopNum()
|
2014-04-11 15:09:31 +02:00
|
|
|
{
|
|
|
|
|
return LoopNum;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-12 02:12:25 +02:00
|
|
|
u32 AuCtx::AuSetLoopNum(int loop)
|
2014-04-11 15:09:31 +02:00
|
|
|
{
|
|
|
|
|
LoopNum = loop;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return 1 to read more data stream, 0 don't read
|
2014-04-12 02:12:25 +02:00
|
|
|
int AuCtx::AuCheckStreamDataNeeded()
|
2014-04-11 15:09:31 +02:00
|
|
|
{
|
|
|
|
|
// if we have no available Au buffer, and the current read position in source file is not the end of stream, then we can read
|
2014-04-13 14:38:42 +02:00
|
|
|
if (AuBufAvailable < (int)AuBufSize && readPos < (int)endPos){
|
2014-04-11 15:09:31 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check how many bytes we have read from source file
|
2014-04-12 02:12:25 +02:00
|
|
|
u32 AuCtx::AuNotifyAddStreamData(int size)
|
2014-04-11 15:09:31 +02:00
|
|
|
{
|
2014-04-12 14:54:25 +02:00
|
|
|
realReadSize = size;
|
2014-07-17 00:49:48 +10:00
|
|
|
int diffsize = realReadSize - askedReadSize;
|
2014-04-13 14:38:42 +02:00
|
|
|
// Notify the real read size
|
2014-07-17 00:49:48 +10:00
|
|
|
if (diffsize != 0){
|
|
|
|
|
readPos += diffsize;
|
|
|
|
|
AuBufAvailable += diffsize;
|
2014-04-12 14:54:25 +02:00
|
|
|
}
|
2014-04-11 15:09:31 +02:00
|
|
|
|
2014-04-12 23:16:38 +02:00
|
|
|
// append AuBuf into sourcebuff
|
|
|
|
|
sourcebuff.append((const char*)Memory::GetPointer(AuBuf), size);
|
|
|
|
|
|
2014-07-17 00:49:48 +10:00
|
|
|
if (readPos >= (int)endPos && LoopNum != 0){
|
2014-04-11 15:09:31 +02:00
|
|
|
// if we need loop, reset readPos
|
|
|
|
|
readPos = startPos;
|
|
|
|
|
// reset LoopNum
|
|
|
|
|
if (LoopNum > 0){
|
|
|
|
|
LoopNum--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// read from stream position srcPos of size bytes into buff
|
2014-04-12 14:54:25 +02:00
|
|
|
// buff, size and srcPos are all pointers
|
2014-04-12 02:12:25 +02:00
|
|
|
u32 AuCtx::AuGetInfoToAddStreamData(u32 buff, u32 size, u32 srcPos)
|
2014-04-11 15:09:31 +02:00
|
|
|
{
|
2014-06-22 14:01:23 +02:00
|
|
|
// you can not read beyond file size and the buffer size
|
2014-04-12 23:16:38 +02:00
|
|
|
int readsize = std::min((int)AuBufSize - AuBufAvailable, (int)endPos - readPos);
|
|
|
|
|
|
2014-06-22 14:01:23 +02:00
|
|
|
// we can recharge AuBuf from its beginning
|
2014-04-11 15:09:31 +02:00
|
|
|
if (Memory::IsValidAddress(buff))
|
|
|
|
|
Memory::Write_U32(AuBuf, buff);
|
|
|
|
|
if (Memory::IsValidAddress(size))
|
2014-04-12 23:16:38 +02:00
|
|
|
Memory::Write_U32(readsize, size);
|
2014-04-11 15:09:31 +02:00
|
|
|
if (Memory::IsValidAddress(srcPos))
|
|
|
|
|
Memory::Write_U32(readPos, srcPos);
|
|
|
|
|
|
2014-04-13 14:38:42 +02:00
|
|
|
// preset the readPos and available size, they will be notified later in NotifyAddStreamData.
|
2014-04-12 23:16:38 +02:00
|
|
|
askedReadSize = readsize;
|
2014-04-12 14:54:25 +02:00
|
|
|
readPos += askedReadSize;
|
|
|
|
|
AuBufAvailable += askedReadSize;
|
|
|
|
|
|
2014-04-11 15:09:31 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-22 14:01:23 +02:00
|
|
|
u32 AuCtx::AuResetPlayPositionByFrame(int position) {
|
2014-04-11 15:09:31 +02:00
|
|
|
readPos = position;
|
|
|
|
|
return 0;
|
Universal Audio Class
Based on my implementation in sceAac https://github.com/hrydgard/ppsspp/pull/5836
I've created a class AuCtx included in My SimpleAudioDec.cpp/.h which aims at providing a standard easy implementation to support all codecs in ffmpeg.
Here, I also completely re-code sceMp3 file with this class to give an example how to use this class, and it has solved all mp3 issues I've observed in the current master.
Tests on different freq and channels mp3 audios as:
Miku custom BGM (48kHz, stereo), Hanayaka Nari Wa ga Ichizoku(32kHz, mono, a little fast but better than before now), downstreet panic (44.1kHz, stereo), and learn jp09(44.1kHz, stero) are just all right.
Especially, I am very glad to see that Miku's Custom BGMs have no repetition issues in first tone any more and no longer stopped in the first second neither. :)
We will come into a new age to fast support new audio formats from now on I hope :P
2014-04-11 22:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-22 14:01:23 +02:00
|
|
|
u32 AuCtx::AuResetPlayPosition() {
|
|
|
|
|
readPos = startPos;
|
|
|
|
|
return 0;
|
Universal Audio Class
Based on my implementation in sceAac https://github.com/hrydgard/ppsspp/pull/5836
I've created a class AuCtx included in My SimpleAudioDec.cpp/.h which aims at providing a standard easy implementation to support all codecs in ffmpeg.
Here, I also completely re-code sceMp3 file with this class to give an example how to use this class, and it has solved all mp3 issues I've observed in the current master.
Tests on different freq and channels mp3 audios as:
Miku custom BGM (48kHz, stereo), Hanayaka Nari Wa ga Ichizoku(32kHz, mono, a little fast but better than before now), downstreet panic (44.1kHz, stereo), and learn jp09(44.1kHz, stero) are just all right.
Especially, I am very glad to see that Miku's Custom BGMs have no repetition issues in first tone any more and no longer stopped in the first second neither. :)
We will come into a new age to fast support new audio formats from now on I hope :P
2014-04-11 22:56:59 +02:00
|
|
|
}
|
2014-04-14 11:47:28 +02:00
|
|
|
|
2014-06-22 14:01:23 +02:00
|
|
|
void AuCtx::DoState(PointerWrap &p) {
|
|
|
|
|
auto s = p.Section("AuContext", 0, 1);
|
|
|
|
|
if (!s)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
p.Do(startPos);
|
|
|
|
|
p.Do(endPos);
|
|
|
|
|
p.Do(AuBuf);
|
|
|
|
|
p.Do(AuBufSize);
|
|
|
|
|
p.Do(PCMBuf);
|
|
|
|
|
p.Do(PCMBufSize);
|
|
|
|
|
p.Do(freq);
|
|
|
|
|
p.Do(SumDecodedSamples);
|
|
|
|
|
p.Do(LoopNum);
|
|
|
|
|
p.Do(Channels);
|
|
|
|
|
p.Do(MaxOutputSample);
|
|
|
|
|
p.Do(readPos);
|
|
|
|
|
p.Do(audioType);
|
|
|
|
|
p.Do(BitRate);
|
|
|
|
|
p.Do(SamplingRate);
|
|
|
|
|
p.Do(askedReadSize);
|
|
|
|
|
p.Do(realReadSize);
|
|
|
|
|
p.Do(FrameNum);
|
|
|
|
|
|
|
|
|
|
if (p.mode == p.MODE_READ) {
|
|
|
|
|
decoder = new SimpleAudio(audioType);
|
|
|
|
|
AuBufAvailable = 0; // reset to read from file at position readPos
|
|
|
|
|
}
|
2014-04-14 13:23:10 +02:00
|
|
|
}
|