2013-10-17 14:00:43 +00: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/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-03-23 18:45:08 +00:00
|
|
|
#include "Core/HW/MediaEngine.h"
|
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 20:56:59 +00:00
|
|
|
#include "Core/HLE/sceAudio.h"
|
2014-03-23 18:45:08 +00:00
|
|
|
|
2014-06-22 12:01:23 +00:00
|
|
|
// Wraps FFMPEG for audio decoding in a nice interface.
|
|
|
|
// Decodes packet by packet - does NOT demux.
|
2013-10-18 12:12:10 +00:00
|
|
|
|
2013-10-17 14:00:43 +00:00
|
|
|
// Based on http://ffmpeg.org/doxygen/trunk/doc_2examples_2decoding_encoding_8c-example.html#_a13
|
|
|
|
|
2014-06-22 12:01:23 +00:00
|
|
|
// audioType
|
2024-04-10 10:14:58 +00:00
|
|
|
enum PSPAudioType {
|
2014-06-22 12:01:23 +00:00
|
|
|
PSP_CODEC_AT3PLUS = 0x00001000,
|
|
|
|
PSP_CODEC_AT3 = 0x00001001,
|
|
|
|
PSP_CODEC_MP3 = 0x00001002,
|
|
|
|
PSP_CODEC_AAC = 0x00001003,
|
|
|
|
};
|
|
|
|
|
2024-04-09 22:45:02 +00:00
|
|
|
class AudioDecoder {
|
|
|
|
public:
|
|
|
|
virtual ~AudioDecoder() {}
|
|
|
|
|
|
|
|
virtual bool Decode(const uint8_t* inbuf, int inbytes, uint8_t *outbuf, int *outbytes) = 0;
|
|
|
|
virtual bool IsOK() const = 0;
|
|
|
|
|
|
|
|
virtual int GetOutSamples() const = 0;
|
|
|
|
virtual int GetSourcePos() const = 0;
|
2024-04-10 10:14:58 +00:00
|
|
|
virtual PSPAudioType GetAudioType() const = 0;
|
2024-04-10 10:19:48 +00:00
|
|
|
|
|
|
|
virtual void SetChannels(int channels) = 0;
|
|
|
|
virtual void SetExtraData(const uint8_t *data, int size, int wav_bytes_per_packet) = 0;
|
|
|
|
|
|
|
|
// Just metadata.
|
|
|
|
void SetCtxPtr(uint32_t ptr) { ctxPtr = ptr; }
|
|
|
|
uint32_t GetCtxPtr() const { return ctxPtr; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t ctxPtr = 0xFFFFFFFF;
|
2024-04-09 22:45:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void AudioClose(AudioDecoder **ctx);
|
2014-06-22 12:01:23 +00:00
|
|
|
const char *GetCodecName(int codec); // audioType
|
2024-04-10 10:14:58 +00:00
|
|
|
bool IsValidCodec(PSPAudioType codec);
|
2024-04-10 10:19:48 +00:00
|
|
|
AudioDecoder *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz = 44100, int channels = 2);
|
2014-03-23 18:45:08 +00:00
|
|
|
|
2014-06-22 12:01:23 +00:00
|
|
|
class AuCtx {
|
|
|
|
public:
|
|
|
|
AuCtx();
|
|
|
|
~AuCtx();
|
2014-03-23 18:45:08 +00:00
|
|
|
|
2014-06-22 12:01:23 +00:00
|
|
|
u32 AuDecode(u32 pcmAddr);
|
|
|
|
|
|
|
|
u32 AuNotifyAddStreamData(int size);
|
|
|
|
int AuCheckStreamDataNeeded();
|
2019-04-24 03:42:13 +00:00
|
|
|
int AuStreamBytesNeeded();
|
2019-04-24 03:49:45 +00:00
|
|
|
int AuStreamWorkareaSize();
|
2014-06-22 12:01:23 +00:00
|
|
|
u32 AuResetPlayPosition();
|
|
|
|
u32 AuResetPlayPositionByFrame(int position);
|
2019-04-24 03:18:16 +00:00
|
|
|
u32 AuGetInfoToAddStreamData(u32 bufPtr, u32 sizePtr, u32 srcPosPtr);
|
2014-06-22 12:01:23 +00:00
|
|
|
|
2019-05-04 13:40:43 +00:00
|
|
|
void SetReadPos(int pos) { readPos = pos; }
|
|
|
|
int ReadPos() { return readPos; }
|
|
|
|
|
2014-06-22 12:01:23 +00:00
|
|
|
void DoState(PointerWrap &p);
|
|
|
|
|
|
|
|
void EatSourceBuff(int amount) {
|
2019-05-10 21:26:34 +00:00
|
|
|
if (amount > (int)sourcebuff.size()) {
|
2019-04-28 18:56:39 +00:00
|
|
|
amount = (int)sourcebuff.size();
|
|
|
|
}
|
|
|
|
if (amount > 0)
|
|
|
|
sourcebuff.erase(sourcebuff.begin(), sourcebuff.begin() + amount);
|
2014-06-22 12:01:23 +00:00
|
|
|
AuBufAvailable -= amount;
|
|
|
|
}
|
|
|
|
// Au source information. Written to from for example sceAacInit so public for now.
|
2019-05-04 13:40:43 +00:00
|
|
|
u64 startPos = 0;
|
|
|
|
u64 endPos = 0;
|
|
|
|
u32 AuBuf = 0;
|
|
|
|
u32 AuBufSize = 0;
|
|
|
|
u32 PCMBuf = 0;
|
|
|
|
u32 PCMBufSize = 0;
|
2024-04-09 22:40:16 +00:00
|
|
|
|
|
|
|
int freq = -1; // used by AAC only?
|
2019-04-24 21:13:46 +00:00
|
|
|
int BitRate = 0;
|
|
|
|
int SamplingRate = -1;
|
|
|
|
int Channels = 0;
|
|
|
|
int Version = -1;
|
2014-04-11 13:09:31 +00:00
|
|
|
|
2014-06-22 12:01:23 +00:00
|
|
|
// State variables. These should be relatively easy to move into private.
|
2019-05-04 13:40:43 +00:00
|
|
|
u32 SumDecodedSamples = 0;
|
|
|
|
int LoopNum = -1;
|
2019-04-24 21:31:12 +00:00
|
|
|
u32 MaxOutputSample = 0;
|
2019-05-04 13:40:43 +00:00
|
|
|
int FrameNum = 0;
|
2014-06-22 12:01:23 +00:00
|
|
|
|
2014-04-11 13:09:31 +00:00
|
|
|
// Au decoder
|
2024-04-09 22:45:02 +00:00
|
|
|
AudioDecoder *decoder = nullptr;
|
2014-04-11 13:09:31 +00:00
|
|
|
|
2014-06-22 12:01:23 +00:00
|
|
|
private:
|
2019-04-28 15:35:35 +00:00
|
|
|
size_t FindNextMp3Sync();
|
|
|
|
|
2019-04-28 18:56:39 +00:00
|
|
|
std::vector<u8> sourcebuff; // source buffer
|
2019-05-04 13:40:43 +00:00
|
|
|
|
|
|
|
// buffers informations
|
|
|
|
int AuBufAvailable = 0; // the available buffer of AuBuf to be able to recharge data
|
2024-04-10 10:07:03 +00:00
|
|
|
int readPos = 0; // read position in audio source file
|
2019-05-04 13:40:43 +00:00
|
|
|
int askedReadSize = 0; // the size of data requied to be read from file by the game
|
2019-05-04 14:25:41 +00:00
|
|
|
int nextOutputHalf = 0;
|
2014-04-11 13:09:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|