forked from nscript-site/NScript.AndroidBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.cs
More file actions
247 lines (211 loc) · 7.3 KB
/
Decoder.cs
File metadata and controls
247 lines (211 loc) · 7.3 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NScript.AndroidBot
{
using FFmpeg.AutoGen;
using Geb.Image;
public unsafe class FrameSink
{
private SwsContextHolder m_sws = null;
public bool Open()
{
return true;
}
public int Width { get; internal set; }
public int Height { get; internal set; }
public AVPixelFormat PixelFormat { get; set; }
private Object syncRoot = new object();
public Action<ImageBgr24> OnRender { get; set; }
public void Close() { }
public bool Push(AVFrame* frame)
{
if (Width > 0 && Height > 0 && frame != null)
{
ImageBgr24 img = new ImageBgr24(Width, Height);
WriteToFrame(frame, (Byte*)img.Start, img.Width * 3, AVPixelFormat.AV_PIX_FMT_BGR24, Width, Height);
lock(syncRoot)
{
if (Image != null) Image.Dispose();
Image = img;
}
OnRender?.Invoke(img);
}
return true;
}
public ImageBgr24 Image { get; set; }
/// <summary>
/// 获得当前帧的图像
/// </summary>
/// <returns> 当前帧的图像 </returns>
public ImageBgr24 GetFrameImage()
{
lock (syncRoot)
{
if (Image == null) return null;
else return Image.Clone();
}
}
public unsafe bool WriteToFrame(AVFrame* m_avFrame, byte* frameData, int stride, AVPixelFormat frameFmt, int width, int height)
{
if (m_avFrame == null || m_avFrame->data[0] == null) return false;
SwsContextHolder sws = GetSwsContextHolder(this.Width, this.Height, this.PixelFormat, width, height, frameFmt, ffmpeg.SWS_BILINEAR);
byte*[] dstData = { frameData };
int[] dstLinesize = { stride };
ffmpeg.sws_scale(sws.Context, m_avFrame->data,
m_avFrame->linesize, 0, Height, dstData,
dstLinesize);
return true;
}
private SwsContextHolder GetSwsContextHolder(int srcW, int srcH, AVPixelFormat srcFmt, int dstW, int dstH, AVPixelFormat dstFmt, int flags)
{
if (m_sws == null)
{
m_sws = new SwsContextHolder(srcW, srcH, srcFmt, dstW, dstH, dstFmt, flags);
return m_sws;
}
else if (m_sws.Match(srcW, srcH, srcFmt, dstW, dstH, dstFmt, flags) == true)
{
return m_sws;
}
else
{
m_sws.Dispose();
m_sws = new SwsContextHolder(srcW, srcH, srcFmt, dstW, dstH, dstFmt, flags);
return m_sws;
}
}
}
public unsafe class Decoder : PacketSink
{
public override unsafe bool Open(AVCodec* codec, AVPixelFormat fmt)
{
decoder_open(codec);
foreach (var item in this.FrameSinks)
item.PixelFormat = fmt;
return true;
}
public override unsafe bool Push(AVPacket* packet)
{
decoder_push(packet);
return true;
}
public override void Close()
{
decoder_close();
}
public void AddSink(FrameSink sink)
{
this.FrameSinks.Add(sink);
}
AVCodecContext* codec_ctx;
AVFrame* frame;
private List<FrameSink> FrameSinks { get; set; } = new List<FrameSink>();
public void CloseFrameSinks()
{
this.CloseFrameSinks(FrameSinks.Count);
}
public void CloseFrameSinks(int count)
{
count = Math.Max(count, FrameSinks.Count);
while (count > 0)
{
count--;
var frameSink = FrameSinks[count];
frameSink.Close();
}
}
public void OpenFrameSinks()
{
for (int i = 0; i < FrameSinks.Count; i++)
{
var item = FrameSinks[i];
if (item.Open() == false)
{
CloseFrameSinks(i);
throw new BotException("Could not open frame sink " + i);
}
}
}
public void decoder_open(AVCodec* codec)
{
this.codec_ctx = ffmpeg.avcodec_alloc_context3(codec);
if (codec_ctx == null)
{
throw new BotException("Could not allocate decoder context");
}
if (ffmpeg.avcodec_open2(codec_ctx, codec, null) < 0)
{
FFmpegUtils.avcodec_free_context(ref codec_ctx);
throw new BotException("Could not open codec");
}
//double framerate = (double)(codec_ctx->framerate.num/ (double)codec_ctx->framerate.den);
//Console.WriteLine(framerate);
frame = ffmpeg.av_frame_alloc();
if (frame == null)
{
ffmpeg.avcodec_close(codec_ctx);
FFmpegUtils.avcodec_free_context(ref codec_ctx);
throw new BotException("Could not create decoder frame");
}
try
{
this.OpenFrameSinks();
}
catch
{
FFmpegUtils.av_frame_free(ref frame);
ffmpeg.avcodec_close(codec_ctx);
FFmpegUtils.avcodec_free_context(ref codec_ctx);
throw;
}
}
public void decoder_close()
{
this.CloseFrameSinks();
FFmpegUtils.av_frame_free(ref frame);
ffmpeg.avcodec_close(codec_ctx);
FFmpegUtils.avcodec_free_context(ref codec_ctx);
}
public bool push_frame_to_sinks(AVFrame* frame)
{
for (int i = 0; i < this.FrameSinks.Count; ++i)
{
var sink = FrameSinks[i];
if (sink.Push(frame) == false)
{
//LOGE("Could not send frame to sink %d", i);
return false;
}
}
return true;
}
public void decoder_push(AVPacket* packet)
{
bool is_config = packet->pts == ffmpeg.AV_NOPTS_VALUE;
if (is_config) return;
int ret = ffmpeg.avcodec_send_packet(codec_ctx, packet);
if (ret < 0 && ret != ffmpeg.AVERROR(ffmpeg.EAGAIN))
{
return;
//throw new BotException("Could not send video packet: " + ret);
}
ret = ffmpeg.avcodec_receive_frame(codec_ctx, frame);
if (ret == 0)
{
// a frame was received
bool ok = push_frame_to_sinks(frame);
// A frame lost should not make the whole pipeline fail. The error, if
// any, is already logged.
ffmpeg.av_frame_unref(frame);
}
else if (ret != ffmpeg.AVERROR(ffmpeg.EAGAIN))
{
return;
//throw new BotException("Could not receive video frame: %d" + ret);
}
}
}
}