Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HW/SimpleAudioDec.h
3186 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include "Core/HW/MediaEngine.h"
21
#include "Core/HLE/sceAudiocodec.h"
22
23
// Decodes packet by packet - does NOT demux.
24
25
// This basically corresponds to the core of sceAudiocodec.
26
class AudioDecoder {
27
public:
28
virtual ~AudioDecoder() {}
29
30
virtual PSPAudioType GetAudioType() const = 0;
31
32
// inbytesConsumed can include skipping metadata.
33
// outSamples is in stereo samples. So you have to multiply by 4 for 16-bit stereo audio to get bytes.
34
// For Atrac3, if *outSamples != 0, it'll cap the number of samples to output. In this case, its value can only shrink.
35
// TODO: Implement that in the other decoders too, if needed.
36
virtual bool Decode(const uint8_t *inbuf, int inbytes, int *inbytesConsumed, int outputChannels, int16_t *outbuf, int *outSamples) = 0;
37
virtual bool IsOK() const = 0;
38
39
// NOTE: This can come late (MediaEngine::getAudioSample)! But it will come before the first Decode.
40
virtual void SetChannels(int channels) = 0;
41
virtual void FlushBuffers() {}
42
43
// Just metadata.
44
void SetCtxPtr(uint32_t ptr) { ctxPtr = ptr; }
45
uint32_t GetCtxPtr() const { return ctxPtr; }
46
47
private:
48
uint32_t ctxPtr = 0xFFFFFFFF;
49
};
50
51
void AudioClose(AudioDecoder **ctx);
52
const char *GetCodecName(int codec); // audioType
53
bool IsValidCodec(PSPAudioType codec);
54
AudioDecoder *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz = 44100, int channels = 2, size_t blockAlign = 0, const uint8_t *extraData = nullptr, size_t extraDataSize = 0);
55
56
class AuCtx {
57
public:
58
AuCtx();
59
~AuCtx();
60
61
u32 AuDecode(u32 pcmAddr);
62
63
u32 AuNotifyAddStreamData(int size);
64
int AuCheckStreamDataNeeded();
65
int AuStreamBytesNeeded();
66
int AuStreamWorkareaSize();
67
u32 AuResetPlayPosition();
68
u32 AuResetPlayPositionByFrame(int position);
69
u32 AuGetInfoToAddStreamData(u32 bufPtr, u32 sizePtr, u32 srcPosPtr);
70
71
void SetReadPos(int pos) { readPos = pos; }
72
int ReadPos() { return readPos; }
73
74
void DoState(PointerWrap &p);
75
76
void EatSourceBuff(int amount) {
77
if (amount > (int)sourcebuff.size()) {
78
amount = (int)sourcebuff.size();
79
}
80
if (amount > 0)
81
sourcebuff.erase(sourcebuff.begin(), sourcebuff.begin() + amount);
82
AuBufAvailable -= amount;
83
}
84
// Au source information. Written to from for example sceAacInit so public for now.
85
u64 startPos = 0;
86
u64 endPos = 0;
87
u32 AuBuf = 0;
88
u32 AuBufSize = 0;
89
u32 PCMBuf = 0;
90
u32 PCMBufSize = 0;
91
92
int freq = -1; // used by AAC only?
93
int BitRate = 0;
94
int SamplingRate = -1;
95
int Channels = 0;
96
int Version = -1;
97
98
// State variables. These should be relatively easy to move into private.
99
u32 SumDecodedSamples = 0;
100
int LoopNum = -1;
101
u32 MaxOutputSample = 0;
102
int FrameNum = 0;
103
104
// Au decoder
105
AudioDecoder *decoder = nullptr;
106
107
private:
108
size_t FindNextMp3Sync();
109
110
std::vector<u8> sourcebuff; // source buffer
111
112
// buffers informations
113
int AuBufAvailable = 0; // the available buffer of AuBuf to be able to recharge data
114
int readPos = 0; // read position in audio source file
115
int askedReadSize = 0; // the size of data requied to be read from file by the game
116
int nextOutputHalf = 0;
117
};
118
119
120
121
122
123