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 SimpleAudio;
35 
36 #ifdef USE_FFMPEG
37 struct SwsContext;
38 struct AVFrame;
39 struct AVIOContext;
40 struct AVFormatContext;
41 struct AVCodecContext;
42 #endif
43 
getMpegTimeStamp(const u8 * buf)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 {
55 public:
56 	MediaEngine();
57 	~MediaEngine();
58 
59 	void closeMedia();
60 	bool loadStream(const u8 *buffer, int readSize, int RingbufferSize);
61 	bool reloadStream();
62 	bool addVideoStream(int streamNum, int streamId = -1);
63 	// open the mpeg context
64 	bool openContext(bool keepReadPos = false);
65 	void closeContext();
66 
67 	// Returns number of packets actually added. I guess the buffer might be full.
68 	int addStreamData(const u8 *buffer, int addSize);
69 	bool seekTo(s64 timestamp, int videoPixelMode);
70 
71 	bool setVideoStream(int streamNum, bool force = false);
72 	// TODO: Return false if the stream doesn't exist.
setAudioStream(int streamNum)73 	bool setAudioStream(int streamNum) { m_audioStream = streamNum; return true; }
74 
75 	u8 *getFrameImage();
76 	int getRemainSize();
77 	int getAudioRemainSize();
78 
79 	bool stepVideo(int videoPixelMode, bool skipFrame = false);
80 	int writeVideoImage(u32 bufferPtr, int frameWidth = 512, int videoPixelMode = 3);
81 	int writeVideoImageWithRange(u32 bufferPtr, int frameWidth, int videoPixelMode,
82 	                             int xpos, int ypos, int width, int height);
83 	int getAudioSamples(u32 bufferPtr);
84 
85 	s64 getVideoTimeStamp();
86 	s64 getAudioTimeStamp();
87 	s64 getLastTimeStamp();
88 
IsVideoEnd()89 	bool IsVideoEnd() { return m_isVideoEnd; }
90 	bool IsNoAudioData();
91 	bool IsActuallyPlayingAudio();
VideoWidth()92 	int VideoWidth() { return m_desWidth; }
VideoHeight()93 	int VideoHeight() { return m_desHeight; }
94 
95 	void DoState(PointerWrap &p);
96 
97 private:
98 	bool SetupStreams();
99 	bool setVideoDim(int width = 0, int height = 0);
100 	void updateSwsFormat(int videoPixelMode);
101 	int getNextAudioFrame(u8 **buf, int *headerCode1, int *headerCode2);
102 
103 public:  // TODO: Very little of this below should be public.
104 
105 	// Video ffmpeg context - not used for audio
106 #ifdef USE_FFMPEG
107 	AVFormatContext *m_pFormatCtx;
108 	std::map<int, AVCodecContext *> m_pCodecCtxs;
109 	AVFrame *m_pFrame;
110 	AVFrame *m_pFrameRGB;
111 	AVIOContext *m_pIOContext;
112 	SwsContext *m_sws_ctx;
113 #endif
114 
115 	int m_sws_fmt;
116 	u8 *m_buffer;
117 	int m_videoStream;
118 	int m_expectedVideoStreams;
119 
120 	// Used by the demuxer.
121 	int m_audioStream;
122 
123 	int m_desWidth;
124 	int m_desHeight;
125 	int m_decodingsize;
126 	int m_bufSize;
127 	s64 m_videopts = 0;
128 	s64 m_lastPts = -1;
129 	BufferQueue *m_pdata;
130 
131 	MpegDemux *m_demux;
132 	SimpleAudio *m_audioContext;
133 	s64 m_audiopts;
134 
135 	s64 m_firstTimeStamp;
136 	s64 m_lastTimeStamp;
137 
138 	bool m_isVideoEnd;
139 
140 	int m_ringbuffersize;
141 	u8 m_mpegheader[0x10000];  // TODO: Allocate separately
142 	int m_mpegheaderReadPos;
143 	int m_mpegheaderSize;
144 
145 	// used for audio type
146 	int m_audioType;
147 };
148