-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathVP8Codec.cs
More file actions
231 lines (197 loc) · 8.34 KB
/
VP8Codec.cs
File metadata and controls
231 lines (197 loc) · 8.34 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
//-----------------------------------------------------------------------------
// Filename: VP8Codec.cs
//
// Description: Implements a VP8 video encoder and decoder.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 05 Nov 2020 Aaron Clauson Created, Dublin, Ireland.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using SIPSorceryMedia.Abstractions;
namespace Vpx.Net
{
public class VP8Codec : IVideoEncoder, IDisposable
{
private ILogger logger = SIPSorcery.LogFactory.CreateLogger<VP8Codec>();
private static readonly List<VideoFormat> _supportedFormats = new List<VideoFormat>
{
new VideoFormat(VideoCodecsEnum.VP8, 100)
};
public List<VideoFormat> SupportedFormats
{
get { return _supportedFormats; }
}
//private Vp8Codec _vp8Encoder;
private vpx_codec_ctx_t _vp8Decoder;
private bool _forceKeyFrame = false;
private Object _decoderLock = new object();
private Object _encoderLock = new object();
/// <summary>
/// Creates a new video encoder can encode and decode samples.
/// </summary>
public VP8Codec()
{ }
public void ForceKeyFrame() => _forceKeyFrame = true;
public bool IsSupported(VideoCodecsEnum codec) => codec == VideoCodecsEnum.VP8;
public byte[] EncodeVideo(int width, int height, byte[] sample, VideoPixelFormatsEnum pixelFormat, VideoCodecsEnum codec)
{
//lock (_encoderLock)
//{
// if (_vp8Encoder == null)
// {
// _vp8Encoder = new Vp8Codec();
// _vp8Encoder.InitialiseEncoder((uint)width, (uint)height);
// }
// var i420Buffer = PixelConverter.ToI420(width, height, sample, pixelFormat);
// var encodedBuffer = _vp8Encoder.Encode(i420Buffer, _forceKeyFrame);
// if (_forceKeyFrame)
// {
// _forceKeyFrame = false;
// }
// return encodedBuffer;
//}
throw new NotImplementedException("TODO: The encoder has not yet been ported.");
}
public unsafe IEnumerable<VideoSample> DecodeVideo(byte[] frame, VideoPixelFormatsEnum pixelFormat, VideoCodecsEnum codec)
{
lock (_decoderLock)
{
if (_vp8Decoder == null)
{
_vp8Decoder = new vpx_codec_ctx_t();
vpx_codec_iface_t algo = vp8_dx.vpx_codec_vp8_dx();
vpx_codec_dec_cfg_t cfg = new vpx_codec_dec_cfg_t { threads = 1 };
vpx_codec_err_t res = vpx_decoder.vpx_codec_dec_init(_vp8Decoder, algo, cfg, 0);
}
//logger.LogDebug($"Attempting to decode {frame.Length} bytes.");
//Console.WriteLine(frame.HexStr());
fixed (byte* pFrame = frame)
{
var result = vpx_decoder.vpx_codec_decode(_vp8Decoder, pFrame, (uint)frame.Length, IntPtr.Zero, 0);
//logger.LogDebug($"VP8 decode result {result}.");
if (result != vpx_codec_err_t.VPX_CODEC_OK)
{
logger.LogWarning($"VP8 decode of video sample failed with {result}.");
}
}
IntPtr iter = IntPtr.Zero;
var img = vpx_decoder.vpx_codec_get_frame(_vp8Decoder, iter);
if (img == null)
{
logger.LogWarning("Image could not be acquired from VP8 decoder stage.");
}
else
{
int dwidth = (int)img.d_w;
int dheight = (int)img.d_h;
int sz = dwidth * dheight;
var yPlane = img.planes[0];
var uPlane = img.planes[1];
var vPlane = img.planes[2];
byte[] decodedBuffer = new byte[dwidth * dheight * 3 / 2];
for (uint row = 0; row < dheight; row++)
{
Marshal.Copy((IntPtr)(yPlane + row * img.stride[0]), decodedBuffer, (int)(row * dwidth), (int)dwidth);
if (row < dheight / 2)
{
Marshal.Copy((IntPtr)(uPlane + row * img.stride[1]), decodedBuffer, (int)(sz + row * (dwidth / 2)), (int)dwidth / 2);
Marshal.Copy((IntPtr)(vPlane + row * img.stride[2]), decodedBuffer, (int)(sz + sz / 4 + row * (dwidth / 2)), (int)dwidth / 2);
}
}
byte[] rgb = PixelConverter.I420toBGR(decodedBuffer, dwidth, dheight, out _);
return new List<VideoSample> { new VideoSample { Width = img.d_w, Height = img.d_h, Sample = rgb } };
}
return new List<VideoSample>();
}
}
public void Dispose()
{
//_vp8Encoder?.Dispose();
//_vp8Decoder?.Dispose();
}
public byte[] EncodeVideoFaster(RawImage rawImage, VideoCodecsEnum codec)
{
throw new NotImplementedException();
}
public IEnumerable<RawImage> DecodeVideoFaster(byte[] encodedSample, VideoPixelFormatsEnum pixelFormat, VideoCodecsEnum codec)
{
throw new NotImplementedException();
}
}
public static class StrHelper
{
private static readonly sbyte[] _hexDigits =
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
private static readonly char[] hexmap = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public static string HexStr(this byte[] buffer, char? separator = null)
{
return buffer.HexStr(buffer.Length, separator);
}
public static string HexStr(this byte[] buffer, int length, char? separator = null)
{
string rv = string.Empty;
for (int i = 0; i < length; i++)
{
var val = buffer[i];
rv += hexmap[val >> 4];
rv += hexmap[val & 15];
if (separator != null && i != length - 1)
{
rv += separator;
}
}
return rv.ToUpper();
}
public static byte[] ParseHexStr(string hexStr)
{
List<byte> buffer = new List<byte>();
var chars = hexStr.ToCharArray();
int posn = 0;
while (posn < hexStr.Length)
{
while (char.IsWhiteSpace(chars[posn]))
{
posn++;
}
sbyte c = _hexDigits[chars[posn++]];
if (c == -1)
{
break;
}
sbyte n = (sbyte)(c << 4);
c = _hexDigits[chars[posn++]];
if (c == -1)
{
break;
}
n |= c;
buffer.Add((byte)n);
}
return buffer.ToArray();
}
}
}