// Copyright (c) 2013- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include "Core/HW/MediaEngine.h"20#include "Core/HLE/sceAudiocodec.h"2122// Decodes packet by packet - does NOT demux.2324// This basically corresponds to the core of sceAudiocodec.25class AudioDecoder {26public:27virtual ~AudioDecoder() {}2829virtual PSPAudioType GetAudioType() const = 0;3031// inbytesConsumed can include skipping metadata.32// outSamples is in stereo samples. So you have to multiply by 4 for 16-bit stereo audio to get bytes.33// For Atrac3, if *outSamples != 0, it'll cap the number of samples to output. In this case, its value can only shrink.34// TODO: Implement that in the other decoders too, if needed.35virtual bool Decode(const uint8_t *inbuf, int inbytes, int *inbytesConsumed, int outputChannels, int16_t *outbuf, int *outSamples) = 0;36virtual bool IsOK() const = 0;3738// NOTE: This can come late (MediaEngine::getAudioSample)! But it will come before the first Decode.39virtual void SetChannels(int channels) = 0;40virtual void FlushBuffers() {}4142// Just metadata.43void SetCtxPtr(uint32_t ptr) { ctxPtr = ptr; }44uint32_t GetCtxPtr() const { return ctxPtr; }4546private:47uint32_t ctxPtr = 0xFFFFFFFF;48};4950void AudioClose(AudioDecoder **ctx);51const char *GetCodecName(int codec); // audioType52bool IsValidCodec(PSPAudioType codec);53AudioDecoder *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz = 44100, int channels = 2, size_t blockAlign = 0, const uint8_t *extraData = nullptr, size_t extraDataSize = 0);5455class AuCtx {56public:57AuCtx();58~AuCtx();5960u32 AuDecode(u32 pcmAddr);6162u32 AuNotifyAddStreamData(int size);63int AuCheckStreamDataNeeded();64int AuStreamBytesNeeded();65int AuStreamWorkareaSize();66u32 AuResetPlayPosition();67u32 AuResetPlayPositionByFrame(int position);68u32 AuGetInfoToAddStreamData(u32 bufPtr, u32 sizePtr, u32 srcPosPtr);6970void SetReadPos(int pos) { readPos = pos; }71int ReadPos() { return readPos; }7273void DoState(PointerWrap &p);7475void EatSourceBuff(int amount) {76if (amount > (int)sourcebuff.size()) {77amount = (int)sourcebuff.size();78}79if (amount > 0)80sourcebuff.erase(sourcebuff.begin(), sourcebuff.begin() + amount);81AuBufAvailable -= amount;82}83// Au source information. Written to from for example sceAacInit so public for now.84u64 startPos = 0;85u64 endPos = 0;86u32 AuBuf = 0;87u32 AuBufSize = 0;88u32 PCMBuf = 0;89u32 PCMBufSize = 0;9091int freq = -1; // used by AAC only?92int BitRate = 0;93int SamplingRate = -1;94int Channels = 0;95int Version = -1;9697// State variables. These should be relatively easy to move into private.98u32 SumDecodedSamples = 0;99int LoopNum = -1;100u32 MaxOutputSample = 0;101int FrameNum = 0;102103// Au decoder104AudioDecoder *decoder = nullptr;105106private:107size_t FindNextMp3Sync();108109std::vector<u8> sourcebuff; // source buffer110111// buffers informations112int AuBufAvailable = 0; // the available buffer of AuBuf to be able to recharge data113int readPos = 0; // read position in audio source file114int askedReadSize = 0; // the size of data requied to be read from file by the game115int nextOutputHalf = 0;116};117118119120121122123