CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/HLE/sceAtrac.cpp
Views: 1401
// Copyright (c) 2012- 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#include <algorithm>1819#include "Common/Serialize/Serializer.h"20#include "Common/Serialize/SerializeFuncs.h"21#include "Core/HLE/HLE.h"22#include "Core/HLE/FunctionWrappers.h"23#include "Core/MIPS/MIPS.h"24#include "Core/CoreTiming.h"25#include "Core/MemMapHelpers.h"26#include "Core/Reporting.h"27#include "Core/Config.h"28#include "Core/Debugger/MemBlockInfo.h"29#include "Core/HW/MediaEngine.h"30#include "Core/HW/BufferQueue.h"3132#include "Core/HLE/sceKernel.h"33#include "Core/HLE/sceUtility.h"34#include "Core/HLE/sceKernelMemory.h"35#include "Core/HLE/sceAtrac.h"36#include "Core/HLE/AtracCtx.h"37#include "Core/HLE/AtracCtx2.h"38#include "Core/System.h"3940// Notes about sceAtrac buffer management41//42// sceAtrac decodes from a buffer the game fills, where this buffer is one of:43// * Not yet initialized (state NO DATA = 1)44// * The entire size of the audio data, and filled with audio data (state ALL DATA LOADED = 2)45// * The entire size, but only partially filled so far (state HALFWAY BUFFER = 3)46// * Smaller than the audio, sliding without any loop (state STREAMED WITHOUT LOOP = 4)47// * Smaller than the audio, sliding with a loop at the end (state STREAMED WITH LOOP AT END = 5)48// * Smaller with a second buffer to help with a loop in the middle (state STREAMED WITH SECOND BUF = 6)49// * Not managed, decoding using "low level" manual looping etc. (LOW LEVEL = 8)50// * Not managed, reserved externally - possibly by sceSas - through low level (RESERVED = 16)51//52// This buffer is generally filled by sceAtracAddStreamData, and where to fill it is given by53// either sceAtracGetStreamDataInfo when continuing to move forwards in the stream of audio data,54// or sceAtracGetBufferInfoForResetting when seeking to a specific location in the audio stream.55//56// State 6 indicates a second buffer is needed. This buffer is used to manage looping correctly.57// To determine how to fill it, the game will call sceAtracGetSecondBufferInfo, then after filling58// the buffer it will call sceAtracSetSecondBuffer.59// The second buffer will just contain the data for the end of loop. The "first" buffer may manage60// only the looped portion, or some of the part after the loop (depending on second buf size.)61//62// Most files will be in RIFF format. It's also possible to load in an OMA/AA3 format file, but63// ultimately this will share the same buffer - it's just offset a bit more.64//65// Low level decoding doesn't use the buffer, and decodes only a single packet at a time.66//67// Lastly, sceSas has some integration with sceAtrac, which allows setting an Atrac id as68// a voice for an SAS core. In this mode, the game will directly modify some of the context,69// but will largely only interact using sceSas.70//71// Note that this buffer is THE view of the audio stream. On a PSP, the firmware does not manage72// any cache or separate version of the buffer - at most it manages decode state from earlier in73// the buffer.7475static const int atracDecodeDelay = 2300;7677const int PSP_NUM_ATRAC_IDS = 6;78static bool atracInited = true;79static AtracBase *atracContexts[PSP_NUM_ATRAC_IDS];80static u32 atracContextTypes[PSP_NUM_ATRAC_IDS];81static int atracLibVersion = 0;82static u32 atracLibCrc = 0;8384void __AtracInit() {85_assert_(sizeof(SceAtracContext) == 256);8687atracInited = true;88memset(atracContexts, 0, sizeof(atracContexts));8990// Start with 2 of each in this order.91atracContextTypes[0] = PSP_MODE_AT_3_PLUS;92atracContextTypes[1] = PSP_MODE_AT_3_PLUS;93atracContextTypes[2] = PSP_MODE_AT_3;94atracContextTypes[3] = PSP_MODE_AT_3;95atracContextTypes[4] = 0;96atracContextTypes[5] = 0;97}9899void __AtracShutdown() {100for (size_t i = 0; i < ARRAY_SIZE(atracContexts); ++i) {101delete atracContexts[i];102atracContexts[i] = nullptr;103}104}105106void __AtracLoadModule(int version, u32 crc) {107atracLibVersion = version;108atracLibCrc = crc;109INFO_LOG(Log::ME, "AtracInit, atracLibVersion 0x%0x, atracLibcrc %x", atracLibVersion, atracLibCrc);110}111112void __AtracDoState(PointerWrap &p) {113auto s = p.Section("sceAtrac", 1, 2);114if (!s)115return;116117Do(p, atracInited);118for (int i = 0; i < PSP_NUM_ATRAC_IDS; ++i) {119bool valid = atracContexts[i] != nullptr;120Do(p, valid);121if (valid) {122DoSubClass<AtracBase, Atrac>(p, atracContexts[i]);123} else {124delete atracContexts[i];125atracContexts[i] = nullptr;126}127}128DoArray(p, atracContextTypes, PSP_NUM_ATRAC_IDS);129if (s < 2) {130atracLibVersion = 0;131atracLibCrc = 0;132}133else {134Do(p, atracLibVersion);135Do(p, atracLibCrc);136}137}138139static AtracBase *allocAtrac(bool forceOld = false) {140if (g_Config.bUseNewAtrac && !forceOld) {141return new Atrac2();142} else {143return new Atrac();144}145}146147static AtracBase *getAtrac(int atracID) {148if (atracID < 0 || atracID >= PSP_NUM_ATRAC_IDS) {149return nullptr;150}151AtracBase *atrac = atracContexts[atracID];152if (atrac) {153atrac->UpdateContextFromPSPMem();154}155return atrac;156}157158static int createAtrac(AtracBase *atrac) {159for (int i = 0; i < (int)ARRAY_SIZE(atracContexts); ++i) {160if (atracContextTypes[i] == atrac->CodecType() && atracContexts[i] == 0) {161atracContexts[i] = atrac;162atrac->atracID_ = i;163return i;164}165}166return ATRAC_ERROR_NO_ATRACID;167}168169static int deleteAtrac(int atracID) {170if (atracID >= 0 && atracID < PSP_NUM_ATRAC_IDS) {171if (atracContexts[atracID] != nullptr) {172delete atracContexts[atracID];173atracContexts[atracID] = nullptr;174return 0;175}176}177return ATRAC_ERROR_BAD_ATRACID;178}179180// Really, allocate an Atrac context of a specific codec type.181// Useful to initialize a context for low level decode.182static u32 sceAtracGetAtracID(int codecType) {183if (codecType != PSP_MODE_AT_3 && codecType != PSP_MODE_AT_3_PLUS) {184return hleReportError(Log::ME, ATRAC_ERROR_INVALID_CODECTYPE, "invalid codecType");185}186187AtracBase *atrac = allocAtrac();188atrac->GetTrackMut().codecType = codecType;189int atracID = createAtrac(atrac);190if (atracID < 0) {191delete atrac;192return hleLogError(Log::ME, atracID, "no free ID");193}194195return hleLogSuccessInfoI(Log::ME, atracID);196}197198static u32 AtracValidateData(const AtracBase *atrac) {199if (!atrac) {200return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "bad atrac ID");201} else if (atrac->BufferState() == ATRAC_STATUS_NO_DATA) {202return hleLogError(Log::ME, ATRAC_ERROR_NO_DATA, "no data");203} else {204return 0;205}206}207208static u32 AtracValidateManaged(const AtracBase *atrac) {209if (!atrac) {210return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "bad atrac ID");211} else if (atrac->BufferState() == ATRAC_STATUS_NO_DATA) {212return hleLogError(Log::ME, ATRAC_ERROR_NO_DATA, "no data");213} else if (atrac->BufferState() == ATRAC_STATUS_LOW_LEVEL) {214return hleLogError(Log::ME, ATRAC_ERROR_IS_LOW_LEVEL, "low level stream, can't use");215} else if (atrac->BufferState() == ATRAC_STATUS_FOR_SCESAS) {216return hleLogError(Log::ME, ATRAC_ERROR_IS_FOR_SCESAS, "SAS stream, can't use");217} else {218return 0;219}220}221222// Notifies that more data is (OR will be very soon) available in the buffer.223// This implies it has been added to whatever position sceAtracGetStreamDataInfo would indicate.224//225// The total size of the buffer is atrac->bufferMaxSize_.226static u32 sceAtracAddStreamData(int atracID, u32 bytesToAdd) {227AtracBase *atrac = getAtrac(atracID);228u32 err = AtracValidateManaged(atrac);229if (err != 0) {230// Already logged.231return err;232}233234if (atrac->BufferState() == ATRAC_STATUS_ALL_DATA_LOADED) {235// Let's avoid spurious warnings. Some games call this with 0 which is pretty harmless.236if (bytesToAdd == 0)237return hleLogDebug(Log::ME, ATRAC_ERROR_ALL_DATA_LOADED, "stream entirely loaded");238return hleLogWarning(Log::ME, ATRAC_ERROR_ALL_DATA_LOADED, "stream entirely loaded");239}240241int ret = atrac->AddStreamData(bytesToAdd);242if (ret < 0) {243return ret;244}245return hleLogSuccessI(Log::ME, 0);246}247248// Note that outAddr being null is completely valid here, used to skip data.249static u32 sceAtracDecodeData(int atracID, u32 outAddr, u32 numSamplesAddr, u32 finishFlagAddr, u32 remainAddr) {250AtracBase *atrac = getAtrac(atracID);251u32 err = AtracValidateData(atrac);252if (err != 0) {253// Already logged.254return err;255}256257u32 numSamples = 0;258u32 finish = 0;259int remains = 0;260int ret = atrac->DecodeData(Memory::GetPointerWrite(outAddr), outAddr, &numSamples, &finish, &remains);261if (ret != (int)ATRAC_ERROR_BAD_ATRACID && ret != (int)ATRAC_ERROR_NO_DATA) {262if (Memory::IsValidAddress(numSamplesAddr))263Memory::WriteUnchecked_U32(numSamples, numSamplesAddr);264if (Memory::IsValidAddress(finishFlagAddr))265Memory::WriteUnchecked_U32(finish, finishFlagAddr);266// On error, no remaining frame value is written.267if (ret == 0 && Memory::IsValidAddress(remainAddr))268Memory::WriteUnchecked_U32(remains, remainAddr);269}270DEBUG_LOG(Log::ME, "%08x=sceAtracDecodeData(%i, %08x, %08x[%08x], %08x[%08x], %08x[%d])", ret, atracID, outAddr,271numSamplesAddr, numSamples,272finishFlagAddr, finish,273remainAddr, remains);274if (!ret) {275// decode data successfully, delay thread276return hleDelayResult(ret, "atrac decode data", atracDecodeDelay);277}278return ret;279}280281static u32 sceAtracEndEntry() {282ERROR_LOG_REPORT(Log::ME, "UNIMPL sceAtracEndEntry()");283return 0;284}285286// Obtains information about what needs to be in the buffer to seek (or "reset")287// Generally called by games right before calling sceAtracResetPlayPosition().288static u32 sceAtracGetBufferInfoForResetting(int atracID, int sample, u32 bufferInfoAddr) {289auto bufferInfo = PSPPointer<AtracResetBufferInfo>::Create(bufferInfoAddr);290291AtracBase *atrac = getAtrac(atracID);292u32 err = AtracValidateManaged(atrac);293if (err != 0) {294// Already logged.295return err;296}297298if (!bufferInfo.IsValid()) {299return hleReportError(Log::ME, SCE_KERNEL_ERROR_ILLEGAL_ADDR, "invalid buffer, should crash");300} else if (atrac->BufferState() == ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER && atrac->SecondBufferSize() == 0) {301return hleReportError(Log::ME, ATRAC_ERROR_SECOND_BUFFER_NEEDED, "no second buffer");302} else if ((u32)sample + atrac->GetTrack().firstSampleOffset > (u32)atrac->GetTrack().endSample + atrac->GetTrack().firstSampleOffset) {303// NOTE: Above we have to add firstSampleOffset to both sides - we seem to rely on wraparound.304return hleLogWarning(Log::ME, ATRAC_ERROR_BAD_SAMPLE, "invalid sample position");305} else {306atrac->GetResetBufferInfo(bufferInfo, sample);307return hleLogSuccessInfoI(Log::ME, 0);308}309}310311static u32 sceAtracGetBitrate(int atracID, u32 outBitrateAddr) {312AtracBase *atrac = getAtrac(atracID);313u32 err = AtracValidateData(atrac);314if (err != 0) {315// Already logged.316return err;317}318319atrac->GetTrackMut().UpdateBitrate();320321if (Memory::IsValidAddress(outBitrateAddr)) {322Memory::WriteUnchecked_U32(atrac->GetTrack().bitrate, outBitrateAddr);323return hleLogSuccessI(Log::ME, 0);324} else {325return hleLogError(Log::ME, 0, "invalid address");326}327}328329static u32 sceAtracGetChannel(int atracID, u32 channelAddr) {330AtracBase *atrac = getAtrac(atracID);331u32 err = AtracValidateData(atrac);332if (err != 0) {333// Already logged.334return err;335}336337if (Memory::IsValidAddress(channelAddr)){338Memory::WriteUnchecked_U32(atrac->GetTrack().channels, channelAddr);339return hleLogSuccessI(Log::ME, 0);340} else {341return hleLogError(Log::ME, 0, "invalid address");342}343}344345static u32 sceAtracGetLoopStatus(int atracID, u32 loopNumAddr, u32 statusAddr) {346AtracBase *atrac = getAtrac(atracID);347u32 err = AtracValidateData(atrac);348if (err != 0) {349// Already logged.350return err;351}352353if (Memory::IsValidAddress(loopNumAddr))354Memory::WriteUnchecked_U32(atrac->LoopNum(), loopNumAddr);355// return audio's loopinfo in at3 file356if (Memory::IsValidAddress(statusAddr)) {357if (atrac->GetTrack().loopinfo.size() > 0)358Memory::WriteUnchecked_U32(1, statusAddr);359else360Memory::WriteUnchecked_U32(0, statusAddr);361return hleLogSuccessI(Log::ME, 0);362} else {363return hleLogError(Log::ME, 0, "invalid address");364}365}366367static u32 sceAtracGetInternalErrorInfo(int atracID, u32 errorAddr) {368AtracBase *atrac = getAtrac(atracID);369u32 err = AtracValidateData(atrac);370if (err != 0) {371// Already logged.372return err;373}374ERROR_LOG(Log::ME, "UNIMPL sceAtracGetInternalErrorInfo(%i, %08x)", atracID, errorAddr);375if (Memory::IsValidAddress(errorAddr))376Memory::WriteUnchecked_U32(0, errorAddr);377return 0;378}379380static u32 sceAtracGetMaxSample(int atracID, u32 maxSamplesAddr) {381AtracBase *atrac = getAtrac(atracID);382u32 err = AtracValidateData(atrac);383if (err != 0) {384// Already logged.385return err;386}387388if (Memory::IsValidAddress(maxSamplesAddr)) {389Memory::WriteUnchecked_U32(atrac->GetTrack().SamplesPerFrame(), maxSamplesAddr);390return hleLogSuccessI(Log::ME, 0);391} else {392return hleLogError(Log::ME, 0, "invalid address");393}394}395396static u32 sceAtracGetNextDecodePosition(int atracID, u32 outposAddr) {397AtracBase *atrac = getAtrac(atracID);398u32 err = AtracValidateData(atrac);399if (err != 0) {400// Already logged.401return err;402}403404if (Memory::IsValidAddress(outposAddr)) {405if (atrac->CurrentSample() >= atrac->GetTrack().endSample) {406Memory::WriteUnchecked_U32(0, outposAddr);407return hleLogSuccessI(Log::ME, ATRAC_ERROR_ALL_DATA_DECODED, "all data decoded - beyond endSample");408} else {409Memory::WriteUnchecked_U32(atrac->CurrentSample(), outposAddr);410return hleLogSuccessI(Log::ME, 0);411}412} else {413return hleLogError(Log::ME, 0, "invalid address");414}415}416417static u32 sceAtracGetNextSample(int atracID, u32 outNAddr) {418AtracBase *atrac = getAtrac(atracID);419u32 err = AtracValidateData(atrac);420if (err != 0) {421// Already logged.422return err;423}424if (atrac->CurrentSample() >= atrac->GetTrack().endSample) {425if (Memory::IsValidAddress(outNAddr))426Memory::WriteUnchecked_U32(0, outNAddr);427return hleLogSuccessI(Log::ME, 0, "0 samples left");428}429430u32 numSamples = atrac->GetNextSamples();431432if (Memory::IsValidAddress(outNAddr))433Memory::WriteUnchecked_U32(numSamples, outNAddr);434return hleLogSuccessI(Log::ME, 0, "%d samples left", numSamples);435}436437// Obtains the number of frames remaining in the buffer which can be decoded.438// When no more data would be needed, this returns a negative number.439static u32 sceAtracGetRemainFrame(int atracID, u32 remainAddr) {440auto remainingFrames = PSPPointer<u32_le>::Create(remainAddr);441442AtracBase *atrac = getAtrac(atracID);443u32 err = AtracValidateManaged(atrac);444if (err != 0) {445// Already logged.446return err;447}448449if (!remainingFrames.IsValid()) {450// Would crash.451return hleReportError(Log::ME, SCE_KERNEL_ERROR_ILLEGAL_ADDR, "invalid remainingFrames pointer");452}453454*remainingFrames = atrac->RemainingFrames();455return hleLogSuccessI(Log::ME, 0);456}457458static u32 sceAtracGetSecondBufferInfo(int atracID, u32 fileOffsetAddr, u32 desiredSizeAddr) {459auto fileOffset = PSPPointer<u32_le>::Create(fileOffsetAddr);460auto desiredSize = PSPPointer<u32_le>::Create(desiredSizeAddr);461462AtracBase *atrac = getAtrac(atracID);463u32 err = AtracValidateManaged(atrac);464if (err != 0) {465// Already logged.466return err;467}468469if (!fileOffset.IsValid() || !desiredSize.IsValid()) {470// Would crash.471return hleReportError(Log::ME, SCE_KERNEL_ERROR_ILLEGAL_ADDR, "invalid addresses");472}473474return atrac->GetSecondBufferInfo(fileOffset, desiredSize);475}476477static u32 sceAtracGetSoundSample(int atracID, u32 outEndSampleAddr, u32 outLoopStartSampleAddr, u32 outLoopEndSampleAddr) {478AtracBase *atrac = getAtrac(atracID);479u32 err = AtracValidateManaged(atrac);480if (err != 0) {481// Already logged.482return err;483}484485auto outEndSample = PSPPointer<u32_le>::Create(outEndSampleAddr);486if (outEndSample.IsValid())487*outEndSample = atrac->GetTrack().endSample;488auto outLoopStart = PSPPointer<u32_le>::Create(outLoopStartSampleAddr);489if (outLoopStart.IsValid())490*outLoopStart = atrac->GetTrack().loopStartSample == -1 ? -1 : atrac->GetTrack().loopStartSample - atrac->GetTrack().FirstSampleOffsetFull();491auto outLoopEnd = PSPPointer<u32_le>::Create(outLoopEndSampleAddr);492if (outLoopEnd.IsValid())493*outLoopEnd = atrac->GetTrack().loopEndSample == -1 ? -1 : atrac->GetTrack().loopEndSample - atrac->GetTrack().FirstSampleOffsetFull();494495if (!outEndSample.IsValid() || !outLoopStart.IsValid() || !outLoopEnd.IsValid()) {496return hleReportError(Log::ME, 0, "invalid address");497}498return hleLogSuccessI(Log::ME, 0);499}500501// Games call this function to get some info for add more stream data,502// such as where the data read from, where the data add to,503// and how many bytes are allowed to add.504static u32 sceAtracGetStreamDataInfo(int atracID, u32 writePtrAddr, u32 writableBytesAddr, u32 readOffsetAddr) {505AtracBase *atrac = getAtrac(atracID);506u32 err = AtracValidateManaged(atrac);507if (err != 0) {508// Already logged.509return err;510}511512u32 writePtr;513u32 writableBytes;514u32 readOffset;515atrac->GetStreamDataInfo(&writePtr, &writableBytes, &readOffset);516517if (Memory::IsValidAddress(writePtrAddr))518Memory::WriteUnchecked_U32(writePtr, writePtrAddr);519if (Memory::IsValidAddress(writableBytesAddr))520Memory::WriteUnchecked_U32(writableBytes, writableBytesAddr);521if (Memory::IsValidAddress(readOffsetAddr))522Memory::WriteUnchecked_U32(readOffset, readOffsetAddr);523524return hleLogSuccessI(Log::ME, 0);525}526527static u32 sceAtracReleaseAtracID(int atracID) {528int result = deleteAtrac(atracID);529if (result < 0) {530if (atracID >= 0) {531return hleLogError(Log::ME, result, "did not exist");532} else {533return hleLogWarning(Log::ME, result, "did not exist");534}535}536return hleLogSuccessInfoI(Log::ME, result);537}538539// This is called when a game wants to seek (or "reset") to a specific position in the audio data.540// Normally, sceAtracGetBufferInfoForResetting() is called to determine how to buffer.541// The game must add sufficient packets to the buffer in order to complete the seek.542static u32 sceAtracResetPlayPosition(int atracID, int sample, int bytesWrittenFirstBuf, int bytesWrittenSecondBuf) {543AtracBase *atrac = getAtrac(atracID);544u32 err = AtracValidateManaged(atrac);545if (err != 0) {546// Already logged.547return err;548}549550if (atrac->BufferState() == ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER && atrac->SecondBufferSize() == 0) {551return hleReportError(Log::ME, ATRAC_ERROR_SECOND_BUFFER_NEEDED, "no second buffer");552} else if ((u32)sample + atrac->GetTrack().firstSampleOffset > (u32)atrac->GetTrack().endSample + atrac->GetTrack().firstSampleOffset) {553// NOTE: Above we have to add firstSampleOffset to both sides - we seem to rely on wraparound.554return hleLogWarning(Log::ME, ATRAC_ERROR_BAD_SAMPLE, "invalid sample position");555}556557u32 res = atrac->ResetPlayPosition(sample, bytesWrittenFirstBuf, bytesWrittenSecondBuf);558if (res != 0) {559// Already logged.560return res;561}562563return hleDelayResult(hleLogSuccessInfoI(Log::ME, 0), "reset play pos", 3000);564}565566static int _AtracSetData(int atracID, u32 buffer, u32 readSize, u32 bufferSize, int outputChannels, bool needReturnAtracID) {567AtracBase *atrac = getAtrac(atracID);568// Don't use AtracValidateManaged here.569if (!atrac)570return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "invalid atrac ID");571int ret = atrac->SetData(buffer, readSize, bufferSize, outputChannels, needReturnAtracID ? atracID : 0);572// not sure the real delay time573return hleDelayResult(ret, "atrac set data", 100);574}575576static u32 sceAtracSetHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u32 bufferSize) {577AtracBase *atrac = getAtrac(atracID);578// Don't use AtracValidateManaged here.579if (!atrac) {580return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "invalid atrac ID");581}582583if (readSize > bufferSize) {584return hleLogError(Log::ME, ATRAC_ERROR_INCORRECT_READ_SIZE, "read size too large");585}586587int ret = atrac->Analyze(buffer, readSize);588if (ret < 0) {589// Already logged.590return ret;591}592593return _AtracSetData(atracID, buffer, readSize, bufferSize, 2, false);594}595596static u32 sceAtracSetSecondBuffer(int atracID, u32 secondBuffer, u32 secondBufferSize) {597AtracBase *atrac = getAtrac(atracID);598u32 err = AtracValidateManaged(atrac);599if (err != 0) {600// Already logged.601return err;602}603604return atrac->SetSecondBuffer(secondBuffer, secondBufferSize);605}606607static u32 sceAtracSetData(int atracID, u32 buffer, u32 bufferSize) {608AtracBase *atrac = getAtrac(atracID);609if (!atrac) {610return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "bad atrac ID");611}612613int ret = atrac->Analyze(buffer, bufferSize);614if (ret < 0) {615// Already logged.616return ret;617}618619if (atrac->GetTrack().codecType != atracContextTypes[atracID]) {620// TODO: Should this not change the buffer size?621return hleReportError(Log::ME, ATRAC_ERROR_WRONG_CODECTYPE, "atracID uses different codec type than data");622}623624return _AtracSetData(atracID, buffer, bufferSize, bufferSize, 2, false);625}626627static int sceAtracSetDataAndGetID(u32 buffer, int bufferSize) {628// A large value happens in Tales of VS, and isn't handled somewhere properly as a u32.629// It's impossible for it to be that big anyway, so cap it.630if (bufferSize < 0) {631WARN_LOG(Log::ME, "sceAtracSetDataAndGetID(%08x, %08x): negative bufferSize", buffer, bufferSize);632bufferSize = 0x10000000;633}634635AtracBase *atrac = allocAtrac();636int ret = atrac->Analyze(buffer, bufferSize);637if (ret < 0) {638// Already logged.639delete atrac;640return ret;641}642int atracID = createAtrac(atrac);643if (atracID < 0) {644delete atrac;645return hleLogError(Log::ME, atracID, "no free ID");646}647648return _AtracSetData(atracID, buffer, bufferSize, bufferSize, 2, true);649}650651static int sceAtracSetHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bufferSize) {652if (readSize > bufferSize) {653return hleLogError(Log::ME, ATRAC_ERROR_INCORRECT_READ_SIZE, "read size too large");654}655AtracBase *atrac = allocAtrac();656int ret = atrac->Analyze(buffer, readSize);657if (ret < 0) {658// Already logged.659delete atrac;660return ret;661}662int atracID = createAtrac(atrac);663if (atracID < 0) {664delete atrac;665return hleLogError(Log::ME, atracID, "no free ID");666}667return _AtracSetData(atracID, buffer, readSize, bufferSize, 2, true);668}669670static u32 sceAtracStartEntry() {671ERROR_LOG_REPORT(Log::ME, "UNIMPL sceAtracStartEntry()");672return 0;673}674675static u32 sceAtracSetLoopNum(int atracID, int loopNum) {676AtracBase *atrac = getAtrac(atracID);677u32 err = AtracValidateData(atrac);678if (err != 0) {679// Already logged.680return err;681}682if (atrac->GetTrack().loopinfo.size() == 0) {683if (loopNum == -1) {684// This is very common and not really a problem.685return hleLogDebug(Log::ME, ATRAC_ERROR_NO_LOOP_INFORMATION, "no loop information to write to!");686} else {687return hleLogError(Log::ME, ATRAC_ERROR_NO_LOOP_INFORMATION, "no loop information to write to!");688}689}690691atrac->SetLoopNum(loopNum);692return hleLogSuccessI(Log::ME, 0);693}694695static int sceAtracReinit(int at3Count, int at3plusCount) {696for (int i = 0; i < PSP_NUM_ATRAC_IDS; ++i) {697if (atracContexts[i] != nullptr) {698ERROR_LOG_REPORT(Log::ME, "sceAtracReinit(%d, %d): cannot reinit while IDs in use", at3Count, at3plusCount);699return SCE_KERNEL_ERROR_BUSY;700}701}702703memset(atracContextTypes, 0, sizeof(atracContextTypes));704int next = 0;705int space = PSP_NUM_ATRAC_IDS;706707// This seems to deinit things. Mostly, it cause a reschedule on next deinit (but -1, -1 does not.)708if (at3Count == 0 && at3plusCount == 0) {709INFO_LOG(Log::ME, "sceAtracReinit(%d, %d): deinit", at3Count, at3plusCount);710atracInited = false;711return hleDelayResult(0, "atrac reinit", 200);712}713714// First, ATRAC3+. These IDs seem to cost double (probably memory.)715// Intentionally signed. 9999 tries to allocate, -1 does not.716for (int i = 0; i < at3plusCount; ++i) {717space -= 2;718if (space >= 0) {719atracContextTypes[next++] = PSP_MODE_AT_3_PLUS;720}721}722for (int i = 0; i < at3Count; ++i) {723space -= 1;724if (space >= 0) {725atracContextTypes[next++] = PSP_MODE_AT_3;726}727}728729// If we ran out of space, we still initialize some, but return an error.730int result = space >= 0 ? 0 : (int)SCE_KERNEL_ERROR_OUT_OF_MEMORY;731if (atracInited || next == 0) {732atracInited = true;733return hleLogSuccessInfoI(Log::ME, result);734} else {735atracInited = true;736return hleDelayResult(hleLogSuccessInfoI(Log::ME, result), "atrac reinit", 400);737}738}739740static int sceAtracGetOutputChannel(int atracID, u32 outputChanPtr) {741AtracBase *atrac = getAtrac(atracID);742u32 err = AtracValidateData(atrac);743if (err != 0) {744// Already logged.745return err;746}747if (Memory::IsValidAddress(outputChanPtr)) {748Memory::WriteUnchecked_U32(atrac->GetOutputChannels(), outputChanPtr);749return hleLogSuccessI(Log::ME, 0);750} else {751return hleLogError(Log::ME, 0, "invalid address");752}753}754755static int sceAtracIsSecondBufferNeeded(int atracID) {756AtracBase *atrac = getAtrac(atracID);757u32 err = AtracValidateManaged(atrac);758if (err != 0) {759// Already logged.760return err;761}762763// Note that this returns true whether the buffer is already set or not.764int needed = atrac->BufferState() == ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER ? 1 : 0;765return hleLogSuccessI(Log::ME, needed);766}767768static int sceAtracSetMOutHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u32 bufferSize) {769AtracBase *atrac = getAtrac(atracID);770// Don't use AtracValidate* here.771if (!atrac) {772return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "bad atrac ID");773}774if (readSize > bufferSize) {775return hleLogError(Log::ME, ATRAC_ERROR_INCORRECT_READ_SIZE, "read size too large");776}777778int ret = atrac->Analyze(buffer, readSize);779if (ret < 0) {780// Already logged.781return ret;782}783if (atrac->GetTrack().channels != 1) {784// It seems it still sets the data.785atrac->SetData(buffer, readSize, bufferSize, 2, 0);786return hleReportError(Log::ME, ATRAC_ERROR_NOT_MONO, "not mono data");787} else {788return _AtracSetData(atracID, buffer, readSize, bufferSize, 1, false);789}790}791792// Note: This doesn't seem to be part of any available libatrac3plus library.793static u32 sceAtracSetMOutData(int atracID, u32 buffer, u32 bufferSize) {794AtracBase *atrac = getAtrac(atracID);795// Don't use AtracValidate* here.796if (!atrac) {797return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "bad atrac ID");798}799800int ret = atrac->Analyze(buffer, bufferSize);801if (ret < 0) {802// Already logged.803return ret;804}805if (atrac->GetTrack().channels != 1) {806// It seems it still sets the data.807atrac->SetData(buffer, bufferSize, bufferSize, 2, 0);808return hleReportError(Log::ME, ATRAC_ERROR_NOT_MONO, "not mono data");809} else {810return _AtracSetData(atracID, buffer, bufferSize, bufferSize, 1, false);811}812}813814// Note: This doesn't seem to be part of any available libatrac3plus library.815static int sceAtracSetMOutDataAndGetID(u32 buffer, u32 bufferSize) {816AtracBase *atrac = allocAtrac();817int ret = atrac->Analyze(buffer, bufferSize);818if (ret < 0) {819// Already logged.820delete atrac;821return ret;822}823if (atrac->GetTrack().channels != 1) {824delete atrac;825return hleReportError(Log::ME, ATRAC_ERROR_NOT_MONO, "not mono data");826}827int atracID = createAtrac(atrac);828if (atracID < 0) {829delete atrac;830return hleLogError(Log::ME, atracID, "no free ID");831}832833return _AtracSetData(atracID, buffer, bufferSize, bufferSize, 1, true);834}835836static int sceAtracSetMOutHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bufferSize) {837if (readSize > bufferSize) {838return hleLogError(Log::ME, ATRAC_ERROR_INCORRECT_READ_SIZE, "read size too large");839}840AtracBase *atrac = allocAtrac();841int ret = atrac->Analyze(buffer, readSize);842if (ret < 0) {843// Already logged.844delete atrac;845return ret;846}847if (atrac->GetTrack().channels != 1) {848delete atrac;849return hleReportError(Log::ME, ATRAC_ERROR_NOT_MONO, "not mono data");850}851int atracID = createAtrac(atrac);852if (atracID < 0) {853delete atrac;854return hleLogError(Log::ME, atracID, "no free ID");855}856857return _AtracSetData(atracID, buffer, readSize, bufferSize, 1, true);858}859860static int sceAtracSetAA3DataAndGetID(u32 buffer, u32 bufferSize, u32 fileSize, u32 metadataSizeAddr) {861AtracBase *atrac = allocAtrac();862int ret = atrac->AnalyzeAA3(buffer, bufferSize, fileSize);863if (ret < 0) {864// Already logged.865delete atrac;866return ret;867}868int atracID = createAtrac(atrac);869if (atracID < 0) {870delete atrac;871return hleLogError(Log::ME, atracID, "no free ID");872}873874return _AtracSetData(atracID, buffer, bufferSize, bufferSize, 2, true);875}876877static u32 _sceAtracGetContextAddress(int atracID) {878AtracBase *atrac = getAtrac(atracID);879if (!atrac) {880ERROR_LOG(Log::ME, "_sceAtracGetContextAddress(%i): bad atrac id", atracID);881return 0;882}883if (!atrac->context_.IsValid()) {884// allocate a new context_885u32 contextSize = sizeof(SceAtracContext);886// Note that Alloc can increase contextSize to the "grain" size.887atrac->context_ = kernelMemory.Alloc(contextSize, false, StringFromFormat("AtracCtx/%d", atracID).c_str());888if (atrac->context_.IsValid())889Memory::Memset(atrac->context_.ptr, 0, contextSize, "AtracContextClear");890WARN_LOG(Log::ME, "%08x=_sceAtracGetContextAddress(%i): allocated new context", atrac->context_.ptr, atracID);891}892else893WARN_LOG(Log::ME, "%08x=_sceAtracGetContextAddress(%i)", atrac->context_.ptr, atracID);894atrac->WriteContextToPSPMem();895return atrac->context_.ptr;896}897898struct At3HeaderMap {899u16 bytes;900u16 channels;901u8 jointStereo;902903bool Matches(const AtracBase *at) const {904return bytes == at->GetTrack().BytesPerFrame() && channels == at->GetTrack().channels;905}906};907908// These should represent all possible supported bitrates (66, 104, and 132 for stereo.)909static const At3HeaderMap at3HeaderMap[] = {910{ 0x00C0, 1, 0 }, // 132/2 (66) kbps mono911{ 0x0098, 1, 0 }, // 105/2 (52.5) kbps mono912{ 0x0180, 2, 0 }, // 132 kbps stereo913{ 0x0130, 2, 0 }, // 105 kbps stereo914// At this size, stereo can only use joint stereo.915{ 0x00C0, 2, 1 }, // 66 kbps stereo916};917918static int sceAtracLowLevelInitDecoder(int atracID, u32 paramsAddr) {919AtracBase *atrac = getAtrac(atracID);920if (!atrac) {921return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "bad atrac ID");922}923924if (!Memory::IsValidAddress(paramsAddr)) {925// TODO: Returning zero as code was before. Needs testing.926return hleReportError(Log::ME, 0, "invalid pointers");927}928929bool jointStereo = false;930if (atrac->GetTrack().codecType == PSP_MODE_AT_3) {931// See if we can match the actual jointStereo value.932bool found = false;933for (size_t i = 0; i < ARRAY_SIZE(at3HeaderMap); ++i) {934if (at3HeaderMap[i].Matches(atrac)) {935jointStereo = at3HeaderMap[i].jointStereo;936found = true;937}938}939if (!found) {940ERROR_LOG_REPORT(Log::ME, "AT3 header map lacks entry for bpf: %i channels: %i", atrac->GetTrack().BytesPerFrame(), atrac->GetTrack().channels);941// TODO: Should we return an error code for these values?942}943}944945atrac->InitLowLevel(paramsAddr, jointStereo);946947const char *codecName = atrac->GetTrack().codecType == PSP_MODE_AT_3 ? "atrac3" : "atrac3+";948const char *channelName = atrac->GetTrack().channels == 1 ? "mono" : "stereo";949return hleLogSuccessInfoI(Log::ME, 0, "%s %s audio", codecName, channelName);950}951952static int sceAtracLowLevelDecode(int atracID, u32 sourceAddr, u32 sourceBytesConsumedAddr, u32 samplesAddr, u32 sampleBytesAddr) {953auto srcp = PSPPointer<u8>::Create(sourceAddr);954auto srcConsumed = PSPPointer<u32_le>::Create(sourceBytesConsumedAddr);955auto outp = PSPPointer<s16>::Create(samplesAddr);956auto outWritten = PSPPointer<u32_le>::Create(sampleBytesAddr);957958AtracBase *atrac = getAtrac(atracID);959if (!atrac) {960return hleLogError(Log::ME, ATRAC_ERROR_BAD_ATRACID, "bad atrac ID");961}962963if (!srcp.IsValid() || !srcConsumed.IsValid() || !outp.IsValid() || !outWritten.IsValid()) {964// TODO: Returning zero as code was before. Needs testing.965return hleReportError(Log::ME, 0, "invalid pointers");966}967968int bytesConsumed = 0;969int outSamples = 0;970int channels = atrac->GetOutputChannels();971atrac->Decoder()->Decode(srcp, atrac->GetTrack().BytesPerFrame(), &bytesConsumed, channels, outp, &outSamples);972int bytesWritten = outSamples * channels * sizeof(int16_t);973*srcConsumed = bytesConsumed;974*outWritten = bytesWritten;975976NotifyMemInfo(MemBlockFlags::WRITE, samplesAddr, bytesWritten, "AtracLowLevelDecode");977return hleLogDebug(Log::ME, hleDelayResult(0, "low level atrac decode data", atracDecodeDelay));978}979980static int sceAtracSetAA3HalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bufferSize, u32 fileSize) {981if (readSize > bufferSize) {982return hleLogError(Log::ME, ATRAC_ERROR_INCORRECT_READ_SIZE, "read size too large");983}984985AtracBase *atrac = allocAtrac();986int ret = atrac->AnalyzeAA3(buffer, readSize, fileSize);987if (ret < 0) {988// Already logged.989delete atrac;990return ret;991}992int atracID = createAtrac(atrac);993if (atracID < 0) {994delete atrac;995return hleLogError(Log::ME, atracID, "no free ID");996}997998return _AtracSetData(atracID, buffer, readSize, bufferSize, 2, true);999}10001001// External interface used by sceSas' AT3 integration.10021003u32 AtracSasAddStreamData(int atracID, u32 bufPtr, u32 bytesToAdd) {1004AtracBase *atrac = getAtrac(atracID);1005if (!atrac)1006return 0;1007return atrac->AddStreamDataSas(bufPtr, bytesToAdd);1008}10091010u32 AtracSasDecodeData(int atracID, u8* outbuf, u32 outbufPtr, u32 *SamplesNum, u32* finish, int *remains) {1011AtracBase *atrac = getAtrac(atracID);1012if (!atrac)1013return 0;1014return atrac->DecodeData(outbuf, outbufPtr, SamplesNum, finish, remains);1015}10161017int AtracSasGetIDByContext(u32 contextAddr) {1018int atracID = (int)Memory::Read_U32(contextAddr + 0xfc);1019// Restored old hack here that forces outputChannels_ to 1, since sceSas expects mono output, unlike normal usage.1020// This is for savestate compatibility.1021// I think it would be better to simply pass in a 1 as a parameter to atrac->DecodeData in AtracSasDecodeData above.1022AtracBase *atrac = getAtrac(atracID);1023atrac->SetOutputChannels(1);1024return atracID;1025}10261027const HLEFunction sceAtrac3plus[] = {1028{0X7DB31251, &WrapU_IU<sceAtracAddStreamData>, "sceAtracAddStreamData", 'x', "ix" },1029{0X6A8C3CD5, &WrapU_IUUUU<sceAtracDecodeData>, "sceAtracDecodeData", 'x', "ixppp"},1030{0XD5C28CC0, &WrapU_V<sceAtracEndEntry>, "sceAtracEndEntry", 'x', "" },1031{0X780F88D1, &WrapU_I<sceAtracGetAtracID>, "sceAtracGetAtracID", 'i', "x" },1032{0XCA3CA3D2, &WrapU_IIU<sceAtracGetBufferInfoForResetting>, "sceAtracGetBufferInfoForReseting", 'x', "iix" },1033{0XA554A158, &WrapU_IU<sceAtracGetBitrate>, "sceAtracGetBitrate", 'x', "ip" },1034{0X31668BAA, &WrapU_IU<sceAtracGetChannel>, "sceAtracGetChannel", 'x', "ip" },1035{0XFAA4F89B, &WrapU_IUU<sceAtracGetLoopStatus>, "sceAtracGetLoopStatus", 'x', "ipp" },1036{0XE88F759B, &WrapU_IU<sceAtracGetInternalErrorInfo>, "sceAtracGetInternalErrorInfo", 'x', "ip" },1037{0XD6A5F2F7, &WrapU_IU<sceAtracGetMaxSample>, "sceAtracGetMaxSample", 'x', "ip" },1038{0XE23E3A35, &WrapU_IU<sceAtracGetNextDecodePosition>, "sceAtracGetNextDecodePosition", 'x', "ip" },1039{0X36FAABFB, &WrapU_IU<sceAtracGetNextSample>, "sceAtracGetNextSample", 'x', "ip" },1040{0X9AE849A7, &WrapU_IU<sceAtracGetRemainFrame>, "sceAtracGetRemainFrame", 'x', "ip" },1041{0X83E85EA0, &WrapU_IUU<sceAtracGetSecondBufferInfo>, "sceAtracGetSecondBufferInfo", 'x', "ipp" },1042{0XA2BBA8BE, &WrapU_IUUU<sceAtracGetSoundSample>, "sceAtracGetSoundSample", 'x', "ippp" },1043{0X5D268707, &WrapU_IUUU<sceAtracGetStreamDataInfo>, "sceAtracGetStreamDataInfo", 'x', "ippp" },1044{0X61EB33F5, &WrapU_I<sceAtracReleaseAtracID>, "sceAtracReleaseAtracID", 'x', "i" },1045{0X644E5607, &WrapU_IIII<sceAtracResetPlayPosition>, "sceAtracResetPlayPosition", 'x', "iiii" },1046{0X3F6E26B5, &WrapU_IUUU<sceAtracSetHalfwayBuffer>, "sceAtracSetHalfwayBuffer", 'x', "ixxx" },1047{0X83BF7AFD, &WrapU_IUU<sceAtracSetSecondBuffer>, "sceAtracSetSecondBuffer", 'x', "ixx" },1048{0X0E2A73AB, &WrapU_IUU<sceAtracSetData>, "sceAtracSetData", 'x', "ixx" },1049{0X7A20E7AF, &WrapI_UI<sceAtracSetDataAndGetID>, "sceAtracSetDataAndGetID", 'i', "xx" },1050{0XD1F59FDB, &WrapU_V<sceAtracStartEntry>, "sceAtracStartEntry", 'x', "" },1051{0X868120B5, &WrapU_II<sceAtracSetLoopNum>, "sceAtracSetLoopNum", 'x', "ii" },1052{0X132F1ECA, &WrapI_II<sceAtracReinit>, "sceAtracReinit", 'x', "ii" },1053{0XECA32A99, &WrapI_I<sceAtracIsSecondBufferNeeded>, "sceAtracIsSecondBufferNeeded", 'i', "i" },1054{0X0FAE370E, &WrapI_UUU<sceAtracSetHalfwayBufferAndGetID>, "sceAtracSetHalfwayBufferAndGetID", 'i', "xxx" },1055{0X2DD3E298, &WrapU_IIU<sceAtracGetBufferInfoForResetting>, "sceAtracGetBufferInfoForResetting", 'x', "iix" },1056{0X5CF9D852, &WrapI_IUUU<sceAtracSetMOutHalfwayBuffer>, "sceAtracSetMOutHalfwayBuffer", 'x', "ixxx" },1057{0XF6837A1A, &WrapU_IUU<sceAtracSetMOutData>, "sceAtracSetMOutData", 'x', "ixx" },1058{0X472E3825, &WrapI_UU<sceAtracSetMOutDataAndGetID>, "sceAtracSetMOutDataAndGetID", 'i', "xx" },1059{0X9CD7DE03, &WrapI_UUU<sceAtracSetMOutHalfwayBufferAndGetID>, "sceAtracSetMOutHalfwayBufferAndGetID", 'i', "xxx" },1060{0XB3B5D042, &WrapI_IU<sceAtracGetOutputChannel>, "sceAtracGetOutputChannel", 'x', "ip" },1061{0X5622B7C1, &WrapI_UUUU<sceAtracSetAA3DataAndGetID>, "sceAtracSetAA3DataAndGetID", 'i', "xxxp" },1062{0X5DD66588, &WrapI_UUUU<sceAtracSetAA3HalfwayBufferAndGetID>, "sceAtracSetAA3HalfwayBufferAndGetID", 'i', "xxxx" },1063{0X231FC6B7, &WrapU_I<_sceAtracGetContextAddress>, "_sceAtracGetContextAddress", 'x', "i" },1064{0X1575D64B, &WrapI_IU<sceAtracLowLevelInitDecoder>, "sceAtracLowLevelInitDecoder", 'x', "ix" },1065{0X0C116E1B, &WrapI_IUUUU<sceAtracLowLevelDecode>, "sceAtracLowLevelDecode", 'x', "ixpxp"},1066};10671068void Register_sceAtrac3plus() {1069// Two names1070RegisterModule("sceATRAC3plus_Library", ARRAY_SIZE(sceAtrac3plus), sceAtrac3plus);1071RegisterModule("sceAtrac3plus", ARRAY_SIZE(sceAtrac3plus), sceAtrac3plus);1072}107310741075