-
Notifications
You must be signed in to change notification settings - Fork 3
/
GPMFStream.cs
153 lines (127 loc) · 3.67 KB
/
GPMFStream.cs
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
using System;
using System.Linq;
namespace Cromatix.MP4Reader
{
public class GPMFStream : IDisposable
{
public byte[] Content;
public int Position;
public int NestLevel;
public int[] NestSize;
public int DeviceCount;
public int DeviceId;
public string DeviceName;
public readonly int NESTLIMIT = 16;
public int Length
{
get
{
if (Content != null)
return Content.Length;
else
return 0;
}
}
public string FourCC
{
get
{
if (Content != null)
{
return ByteUtil.FourCCFomBytes(Content, Position);
}
return string.Empty;
}
}
public SampleType Type
{
get
{
if (Content != null)
{
SampleType type = (SampleType)(Content[Position + 4] & 0xff);
if (type == SampleType.COMPRESSED && Content[Position + 8] < Length)
{
return (SampleType)(Content[Position + 8] & 0xff);
}
return type;
}
return SampleType.ERROR;
}
}
public int Repeat
{
get
{
if (Content != null)
{
uint bytes32 = ByteUtil.BytesToInt(Content, Position + 4);
int repeat = Samples(bytes32);
SampleType type = (SampleType)(Content[Position + 4] & 0xff);
if (type == SampleType.COMPRESSED && Content[Position + 4] < Length)
{
repeat = Samples(Content[Position + 8]);
}
return repeat;
}
return 0;
}
}
public int StructSize
{
get
{
if (Content != null)
{
uint bytes32 = ByteUtil.BytesToInt(Content, Position + 4);
int ssize = SampleSize(bytes32);
SampleType type = (SampleType)(Content[Position + 4] & 0xff);
if (type == SampleType.COMPRESSED && Position + 8 < Length)
{
ssize = SampleSize(Content[Position + 8]);
}
return ssize;
}
return 0;
}
}
public int DataSize(uint num)
{
return (SampleSize(num) * Samples(num) + 3) & ~0x3;
}
public byte[] GetRawData(int size)
{
if (Content != null)
{
var byteSpan = new ReadOnlySpan<byte>(Content, Position + 8, size);
return byteSpan.ToArray();
}
return null;
}
public GPMFStream(byte[] _buffer)
{
Content = _buffer;
NestSize = new int[NESTLIMIT];
}
public int SampleSize(uint num)
{
return ((int)(num >> 8)) & 0xff;
}
private int Samples(uint num)
{
return (((int)(num >> 24)) & 0xff) | (((int)(num >> 16) & 0xff) << 8);
}
private void Reset()
{
Position = 0;
NestLevel = 0;
Content = null;
NestSize.ToList().ForEach(x => x = 0);
GC.Collect();
}
public void Dispose()
{
Reset();
}
}
}