-
Notifications
You must be signed in to change notification settings - Fork 0
/
DVDVideoCodecAndroidMediaCodec.h
206 lines (170 loc) · 5.64 KB
/
DVDVideoCodecAndroidMediaCodec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright (C) 2013-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
#include "DVDStreamInfo.h"
#include "DVDVideoCodec.h"
#include "cores/VideoPlayer/Process/VideoBuffer.h"
#include "threads/SingleLock.h"
#include "threads/Thread.h"
#include "utils/Geometry.h"
#include "platform/android/activity/JNIXBMCVideoView.h"
#include <atomic>
#include <deque>
#include <memory>
#include <vector>
#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <androidjni/Surface.h>
#include <media/NdkMediaCodec.h>
class CJNISurface;
class CJNISurfaceTexture;
class CJNIMediaCodec;
class CJNIMediaFormat;
class CDVDMediaCodecOnFrameAvailable;
class CJNIByteBuffer;
class CBitstreamConverter;
struct AMediaCrypto;
struct DemuxCryptoInfo;
struct mpeg2_sequence;
typedef struct amc_demux
{
uint8_t* pData;
int iSize;
double dts;
double pts;
} amc_demux;
struct CMediaCodec
{
CMediaCodec(const char* name);
virtual ~CMediaCodec();
AMediaCodec* codec() const { return m_codec; };
private:
AMediaCodec* m_codec;
};
class CMediaCodecVideoBufferPool;
class CMediaCodecVideoBuffer : public CVideoBuffer
{
public:
CMediaCodecVideoBuffer(int id)
: CVideoBuffer(id){};
virtual ~CMediaCodecVideoBuffer() = default;
void Set(int internalId,
int textureId,
std::shared_ptr<CJNISurfaceTexture> surfaceTexture,
std::shared_ptr<CDVDMediaCodecOnFrameAvailable> frameAvailable,
std::shared_ptr<CJNIXBMCVideoView> videoView);
// meat and potatoes
bool WaitForFrame(int millis);
// MediaCodec related
void ReleaseOutputBuffer(bool render,
int64_t displayTime,
CMediaCodecVideoBufferPool* pool = nullptr);
// SurfaceTexture released
int GetBufferId() const;
int GetTextureId() const;
void GetTransformMatrix(float* textureMatrix);
void UpdateTexImage();
void RenderUpdate(const CRect& DestRect, int64_t displayTime);
bool HasSurfaceTexture() const { return m_surfacetexture.operator bool(); };
private:
int m_bufferId = -1;
unsigned int m_textureId = 0;
// shared_ptr bits, shared between
// CDVDVideoCodecAndroidMediaCodec and LinuxRenderGLES.
std::shared_ptr<CJNISurfaceTexture> m_surfacetexture;
std::shared_ptr<CDVDMediaCodecOnFrameAvailable> m_frameready;
std::shared_ptr<CJNIXBMCVideoView> m_videoview;
};
class CMediaCodecVideoBufferPool : public IVideoBufferPool
{
public:
CMediaCodecVideoBufferPool(std::shared_ptr<CMediaCodec> mediaCodec)
: m_codec(mediaCodec){};
virtual ~CMediaCodecVideoBufferPool();
virtual CVideoBuffer* Get() override;
virtual void Return(int id) override;
std::shared_ptr<CMediaCodec> GetMediaCodec();
void ResetMediaCodec();
void ReleaseMediaCodecBuffers();
private:
CCriticalSection m_criticalSection;
;
std::shared_ptr<CMediaCodec> m_codec;
std::vector<CMediaCodecVideoBuffer*> m_videoBuffers;
std::vector<int> m_freeBuffers;
};
class CDVDVideoCodecAndroidMediaCodec : public CDVDVideoCodec, public CJNISurfaceHolderCallback
{
public:
CDVDVideoCodecAndroidMediaCodec(CProcessInfo& processInfo, bool surface_render = false);
virtual ~CDVDVideoCodecAndroidMediaCodec();
// registration
static CDVDVideoCodec* Create(CProcessInfo& processInfo);
static bool Register();
// required overrides
virtual bool Open(CDVDStreamInfo& hints, CDVDCodecOptions& options) override;
virtual bool AddData(const DemuxPacket& packet) override;
virtual void Reset() override;
virtual bool Reconfigure(CDVDStreamInfo& hints) override;
virtual VCReturn GetPicture(VideoPicture* pVideoPicture) override;
virtual const char* GetName() override { return m_formatname.c_str(); };
virtual void SetCodecControl(int flags) override;
virtual unsigned GetAllowedReferences() override;
protected:
void Dispose();
void FlushInternal(void);
void SignalEndOfStream();
void InjectExtraData(AMediaFormat* mediaformat);
std::vector<uint8_t> GetHDRStaticMetadata();
bool ConfigureMediaCodec(void);
int GetOutputPicture(void);
void ConfigureOutputFormat(AMediaFormat* mediaformat);
void UpdateFpsDuration();
// surface handling functions
static void CallbackInitSurfaceTexture(void*);
void InitSurfaceTexture(void);
void ReleaseSurfaceTexture(void);
CDVDStreamInfo m_hints;
std::string m_mime;
std::string m_codecname;
int m_colorFormat;
std::string m_formatname;
bool m_opened;
int m_codecControlFlags;
int m_state;
int m_noPictureLoop;
std::shared_ptr<CJNIXBMCVideoView> m_jnivideoview;
CJNISurface* m_jnisurface;
CJNISurface m_jnivideosurface;
AMediaCrypto* m_crypto;
unsigned int m_textureId;
std::shared_ptr<CMediaCodec> m_codec;
ANativeWindow* m_surface;
std::shared_ptr<CJNISurfaceTexture> m_surfaceTexture;
std::shared_ptr<CDVDMediaCodecOnFrameAvailable> m_frameAvailable;
amc_demux m_demux_pkt;
std::shared_ptr<CMediaCodecVideoBufferPool> m_videoBufferPool;
uint32_t m_OutputDuration, m_fpsDuration;
int64_t m_lastPTS;
int64_t m_invalidPTSValue = 0;
double m_dtsShift;
static std::atomic<bool> m_InstanceGuard;
CBitstreamConverter* m_bitstream;
VideoPicture m_videobuffer;
int m_indexInputBuffer;
bool m_render_surface;
mpeg2_sequence* m_mpeg2_sequence;
int m_src_offset[4];
int m_src_stride[4];
bool m_useDTSforPTS;
// CJNISurfaceHolderCallback interface
public:
virtual void surfaceChanged(CJNISurfaceHolder holder, int format, int width, int height) override;
virtual void surfaceCreated(CJNISurfaceHolder holder) override;
virtual void surfaceDestroyed(CJNISurfaceHolder holder) override;
};