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/MediaEngine.h
Views: 1401
1
// Copyright (c) 2012- 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
19
20
// Simulation of the hardware video/audio decoders.
21
// The idea is high level emulation where we simply use FFMPEG.
22
23
#pragma once
24
25
// An approximation of what the interface will look like. Similar to JPCSP's.
26
27
#include <map>
28
#include "Common/CommonTypes.h"
29
#include "Core/HLE/sceMpeg.h"
30
#include "Core/HW/MpegDemux.h"
31
#include "Core/HW/SimpleAudioDec.h"
32
33
class PointerWrap;
34
class AudioDecoder;
35
36
#ifdef USE_FFMPEG
37
struct SwsContext;
38
struct AVFrame;
39
struct AVIOContext;
40
struct AVFormatContext;
41
struct AVCodecContext;
42
#endif
43
44
inline s64 getMpegTimeStamp(const u8 *buf) {
45
return (s64)buf[5] | ((s64)buf[4] << 8) | ((s64)buf[3] << 16) | ((s64)buf[2] << 24)
46
| ((s64)buf[1] << 32) | ((s64)buf[0] << 36);
47
}
48
49
#ifdef USE_FFMPEG
50
bool InitFFmpeg();
51
#endif
52
53
class MediaEngine {
54
public:
55
MediaEngine();
56
~MediaEngine();
57
58
void closeMedia();
59
bool loadStream(const u8 *buffer, int readSize, int RingbufferSize);
60
bool reloadStream();
61
bool addVideoStream(int streamNum, int streamId = -1);
62
// open the mpeg context
63
bool openContext(bool keepReadPos = false);
64
void closeContext();
65
66
// Returns number of packets actually added. I guess the buffer might be full.
67
int addStreamData(const u8 *buffer, int addSize);
68
bool seekTo(s64 timestamp, int videoPixelMode);
69
70
bool setVideoStream(int streamNum, bool force = false);
71
// TODO: Return false if the stream doesn't exist.
72
bool setAudioStream(int streamNum) { m_audioStream = streamNum; return true; }
73
74
u8 *getFrameImage();
75
int getRemainSize();
76
int getAudioRemainSize();
77
78
bool stepVideo(int videoPixelMode, bool skipFrame = false);
79
int writeVideoImage(u32 bufferPtr, int frameWidth = 512, int videoPixelMode = 3);
80
int writeVideoImageWithRange(u32 bufferPtr, int frameWidth, int videoPixelMode,
81
int xpos, int ypos, int width, int height);
82
int getAudioSamples(u32 bufferPtr);
83
84
s64 getVideoTimeStamp();
85
s64 getAudioTimeStamp();
86
s64 getLastTimeStamp();
87
88
bool IsVideoEnd() { return m_isVideoEnd; }
89
bool IsNoAudioData();
90
bool IsActuallyPlayingAudio();
91
int VideoWidth() { return m_desWidth; }
92
int VideoHeight() { return m_desHeight; }
93
94
void DoState(PointerWrap &p);
95
96
private:
97
bool SetupStreams();
98
bool setVideoDim(int width = 0, int height = 0);
99
void updateSwsFormat(int videoPixelMode);
100
int getNextAudioFrame(u8 **buf, int *headerCode1, int *headerCode2);
101
102
static int MpegReadbuffer(void *opaque, uint8_t *buf, int buf_size);
103
104
public: // TODO: Very little of this below should be public.
105
106
#ifdef USE_FFMPEG
107
std::map<int, AVCodecContext *> m_pCodecCtxs;
108
AVFrame *m_pFrame = nullptr;
109
AVFrame *m_pFrameRGB = nullptr;
110
#endif
111
112
u8 *m_buffer = nullptr;
113
114
int m_desWidth = 0;
115
int m_desHeight = 0;
116
int m_bufSize;
117
s64 m_videopts = 0;
118
119
s64 m_firstTimeStamp = 0;
120
s64 m_lastTimeStamp = 0;
121
122
bool m_isVideoEnd = false;
123
124
private:
125
#ifdef USE_FFMPEG
126
// Video ffmpeg context - not used for audio
127
AVFormatContext *m_pFormatCtx = nullptr;
128
std::vector<AVCodecContext *> m_codecsToClose;
129
AVIOContext *m_pIOContext = nullptr;
130
SwsContext *m_sws_ctx = nullptr;
131
#endif
132
133
int m_sws_fmt = 0;
134
int m_videoStream = -1;
135
int m_expectedVideoStreams = 0;
136
137
// Used by the demuxer.
138
int m_audioStream = -1;
139
140
int m_decodingsize = 0;
141
BufferQueue *m_pdata = nullptr;
142
143
s64 m_lastPts = -1;
144
145
MpegDemux *m_demux = nullptr;
146
AudioDecoder *m_audioContext = nullptr;
147
s64 m_audiopts = 0;
148
149
// used for audio type
150
int m_audioType;
151
152
int m_ringbuffersize;
153
154
u8 m_mpegheader[0x10000]; // TODO: Allocate separately
155
int m_mpegheaderReadPos = 0;
156
int m_mpegheaderSize = 0;
157
};
158
159