CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HW/SimpleAudioDec.h
Views: 1401
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/sceAudio.h"
22
23
// Decodes packet by packet - does NOT demux.
24
25
// audioType
26
enum PSPAudioType {
27
PSP_CODEC_AT3PLUS = 0x00001000,
28
PSP_CODEC_AT3 = 0x00001001,
29
PSP_CODEC_MP3 = 0x00001002,
30
PSP_CODEC_AAC = 0x00001003,
31
};
32
33
class AudioDecoder {
34
public:
35
virtual ~AudioDecoder() {}
36
37
virtual PSPAudioType GetAudioType() const = 0;
38
39
// inbytesConsumed can include skipping metadata.
40
// outSamples is in stereo samples. So you have to multiply by 4 for 16-bit stereo audio to get bytes.
41
// For Atrac3, if *outSamples != 0, it'll cap the number of samples to output. In this case, its value can only shrink.
42
// TODO: Implement that in the other decoders too, if needed.
43
virtual bool Decode(const uint8_t *inbuf, int inbytes, int *inbytesConsumed, int outputChannels, int16_t *outbuf, int *outSamples) = 0;
44
virtual bool IsOK() const = 0;
45
46
virtual void SetChannels(int channels) = 0;
47
virtual void FlushBuffers() {}
48
49
// Just metadata.
50
void SetCtxPtr(uint32_t ptr) { ctxPtr = ptr; }
51
uint32_t GetCtxPtr() const { return ctxPtr; }
52
53
private:
54
uint32_t ctxPtr = 0xFFFFFFFF;
55
};
56
57
void AudioClose(AudioDecoder **ctx);
58
const char *GetCodecName(int codec); // audioType
59
bool IsValidCodec(PSPAudioType codec);
60
AudioDecoder *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz = 44100, int channels = 2, size_t blockAlign = 0, const uint8_t *extraData = nullptr, size_t extraDataSize = 0);
61
62
class AuCtx {
63
public:
64
AuCtx();
65
~AuCtx();
66
67
u32 AuDecode(u32 pcmAddr);
68
69
u32 AuNotifyAddStreamData(int size);
70
int AuCheckStreamDataNeeded();
71
int AuStreamBytesNeeded();
72
int AuStreamWorkareaSize();
73
u32 AuResetPlayPosition();
74
u32 AuResetPlayPositionByFrame(int position);
75
u32 AuGetInfoToAddStreamData(u32 bufPtr, u32 sizePtr, u32 srcPosPtr);
76
77
void SetReadPos(int pos) { readPos = pos; }
78
int ReadPos() { return readPos; }
79
80
void DoState(PointerWrap &p);
81
82
void EatSourceBuff(int amount) {
83
if (amount > (int)sourcebuff.size()) {
84
amount = (int)sourcebuff.size();
85
}
86
if (amount > 0)
87
sourcebuff.erase(sourcebuff.begin(), sourcebuff.begin() + amount);
88
AuBufAvailable -= amount;
89
}
90
// Au source information. Written to from for example sceAacInit so public for now.
91
u64 startPos = 0;
92
u64 endPos = 0;
93
u32 AuBuf = 0;
94
u32 AuBufSize = 0;
95
u32 PCMBuf = 0;
96
u32 PCMBufSize = 0;
97
98
int freq = -1; // used by AAC only?
99
int BitRate = 0;
100
int SamplingRate = -1;
101
int Channels = 0;
102
int Version = -1;
103
104
// State variables. These should be relatively easy to move into private.
105
u32 SumDecodedSamples = 0;
106
int LoopNum = -1;
107
u32 MaxOutputSample = 0;
108
int FrameNum = 0;
109
110
// Au decoder
111
AudioDecoder *decoder = nullptr;
112
113
private:
114
size_t FindNextMp3Sync();
115
116
std::vector<u8> sourcebuff; // source buffer
117
118
// buffers informations
119
int AuBufAvailable = 0; // the available buffer of AuBuf to be able to recharge data
120
int readPos = 0; // read position in audio source file
121
int askedReadSize = 0; // the size of data requied to be read from file by the game
122
int nextOutputHalf = 0;
123
};
124
125
126
127
128
129