forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathVideoCapabilities.cs
245 lines (211 loc) · 8.65 KB
/
VideoCapabilities.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
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
// AForge Direct Show Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2009-2013
// contacts@aforgenet.com
//
namespace AForge.Video.DirectShow
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using AForge.Video;
using AForge.Video.DirectShow.Internals;
/// <summary>
/// Capabilities of video device such as frame size and frame rate.
/// </summary>
public class VideoCapabilities
{
/// <summary>
/// Frame size supported by video device.
/// </summary>
public readonly Size FrameSize;
/// <summary>
/// Frame rate supported by video device for corresponding <see cref="FrameSize">frame size</see>.
/// </summary>
///
/// <remarks><para><note>This field is depricated - should not be used.
/// Its value equals to <see cref="AverageFrameRate"/>.</note></para>
/// </remarks>
///
[Obsolete( "No longer supported. Use AverageFrameRate instead." )]
public int FrameRate
{
get { return AverageFrameRate; }
}
/// <summary>
/// Average frame rate of video device for corresponding <see cref="FrameSize">frame size</see>.
/// </summary>
public readonly int AverageFrameRate;
/// <summary>
/// Maximum frame rate of video device for corresponding <see cref="FrameSize">frame size</see>.
/// </summary>
public readonly int MaximumFrameRate;
/// <summary>
/// Number of bits per pixel provided by the camera.
/// </summary>
public readonly int BitCount;
internal VideoCapabilities( ) { }
// Retrieve capabilities of a video device
static internal VideoCapabilities[] FromStreamConfig( IAMStreamConfig videoStreamConfig )
{
if ( videoStreamConfig == null )
throw new ArgumentNullException( "videoStreamConfig" );
// ensure this device reports capabilities
int count, size;
int hr = videoStreamConfig.GetNumberOfCapabilities( out count, out size );
if ( hr != 0 )
Marshal.ThrowExceptionForHR( hr );
if ( count <= 0 )
throw new NotSupportedException( "This video device does not report capabilities." );
if ( size > Marshal.SizeOf( typeof( VideoStreamConfigCaps ) ) )
throw new NotSupportedException( "Unable to retrieve video device capabilities. This video device requires a larger VideoStreamConfigCaps structure." );
// group capabilities with similar parameters
Dictionary<uint, VideoCapabilities> videocapsList = new Dictionary<uint, VideoCapabilities>( );
for ( int i = 0; i < count; i++ )
{
try
{
VideoCapabilities vc = new VideoCapabilities( videoStreamConfig, i );
uint key = ( ( (uint) vc.FrameSize.Height ) << 32 ) |
( ( (uint) vc.FrameSize.Width ) << 16 );
if ( !videocapsList.ContainsKey( key ) )
{
videocapsList.Add( key, vc );
}
else
{
if ( vc.BitCount > videocapsList[key].BitCount )
{
videocapsList[key] = vc;
}
}
}
catch
{
}
}
VideoCapabilities[] videocaps = new VideoCapabilities[videocapsList.Count];
videocapsList.Values.CopyTo( videocaps, 0 );
return videocaps;
}
// Retrieve capabilities of a video device
internal VideoCapabilities( IAMStreamConfig videoStreamConfig, int index )
{
AMMediaType mediaType = null;
VideoStreamConfigCaps caps = new VideoStreamConfigCaps( );
try
{
// retrieve capabilities struct at the specified index
int hr = videoStreamConfig.GetStreamCaps( index, out mediaType, caps );
if ( hr != 0 )
Marshal.ThrowExceptionForHR( hr );
if ( mediaType.FormatType == FormatType.VideoInfo )
{
VideoInfoHeader videoInfo = (VideoInfoHeader) Marshal.PtrToStructure( mediaType.FormatPtr, typeof( VideoInfoHeader ) );
FrameSize = new Size( videoInfo.BmiHeader.Width, videoInfo.BmiHeader.Height );
BitCount = videoInfo.BmiHeader.BitCount;
AverageFrameRate = (int) ( 10000000 / videoInfo.AverageTimePerFrame );
MaximumFrameRate = (int) ( 10000000 / caps.MinFrameInterval );
}
else if ( mediaType.FormatType == FormatType.VideoInfo2 )
{
VideoInfoHeader2 videoInfo = (VideoInfoHeader2) Marshal.PtrToStructure( mediaType.FormatPtr, typeof( VideoInfoHeader2 ) );
FrameSize = new Size( videoInfo.BmiHeader.Width, videoInfo.BmiHeader.Height );
BitCount = videoInfo.BmiHeader.BitCount;
AverageFrameRate = (int) ( 10000000 / videoInfo.AverageTimePerFrame );
MaximumFrameRate = (int) ( 10000000 / caps.MinFrameInterval );
}
else
{
throw new ApplicationException( "Unsupported format found." );
}
// ignore 12 bpp formats for now, since it was noticed they cause issues on Windows 8
// TODO: proper fix needs to be done so ICaptureGraphBuilder2::RenderStream() does not fail
// on such formats
if ( BitCount <= 12 )
{
throw new ApplicationException( "Unsupported format found." );
}
}
finally
{
if ( mediaType != null )
mediaType.Dispose( );
}
}
/// <summary>
/// Check if the video capability equals to the specified object.
/// </summary>
///
/// <param name="obj">Object to compare with.</param>
///
/// <returns>Returns true if both are equal are equal or false otherwise.</returns>
///
public override bool Equals( object obj )
{
return Equals( obj as VideoCapabilities );
}
/// <summary>
/// Check if two video capabilities are equal.
/// </summary>
///
/// <param name="vc2">Second video capability to compare with.</param>
///
/// <returns>Returns true if both video capabilities are equal or false otherwise.</returns>
///
public bool Equals( VideoCapabilities vc2 )
{
if ( (object) vc2 == null )
{
return false;
}
return ( ( FrameSize == vc2.FrameSize ) && ( BitCount == vc2.BitCount ) );
}
/// <summary>
/// Get hash code of the object.
/// </summary>
///
/// <returns>Returns hash code ot the object </returns>
public override int GetHashCode( )
{
return FrameSize.GetHashCode( ) ^ BitCount;
}
/// <summary>
/// Equality operator.
/// </summary>
///
/// <param name="a">First object to check.</param>
/// <param name="b">Seconds object to check.</param>
///
/// <returns>Return true if both objects are equal or false otherwise.</returns>
public static bool operator ==( VideoCapabilities a, VideoCapabilities b )
{
// if both are null, or both are same instance, return true.
if ( object.ReferenceEquals( a, b ) )
{
return true;
}
// if one is null, but not both, return false.
if ( ( (object) a == null ) || ( (object) b == null ) )
{
return false;
}
return a.Equals( b );
}
/// <summary>
/// Inequality operator.
/// </summary>
///
/// <param name="a">First object to check.</param>
/// <param name="b">Seconds object to check.</param>
///
/// <returns>Return true if both objects are not equal or false otherwise.</returns>
public static bool operator !=( VideoCapabilities a, VideoCapabilities b )
{
return !( a == b );
}
}
}