forked from shanselman/DirectShowLib-FORK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaObj.cs
649 lines (552 loc) · 18 KB
/
MediaObj.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#region license
/*
DirectShowLib - Provide access to DirectShow interfaces via .NET
Copyright (C) 2007
http://sourceforge.net/projects/directshownet/
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#endregion
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;
namespace DirectShowLib.DMO
{
#region Declarations
#if ALLOW_UNTESTED_INTERFACES
/// <summary>
/// From DMO_QUALITY_STATUS_FLAGS
/// </summary>
[Flags]
public enum DMOQualityStatus
{
None = 0x0,
Enabled = 0x1
}
#endif
/// <summary>
/// From _DMO_OUTPUT_DATA_BUFFER_FLAGS
/// </summary>
[Flags]
public enum DMOOutputDataBufferFlags
{
None = 0,
SyncPoint = 0x1,
Time = 0x2,
TimeLength = 0x4,
InComplete = 0x1000000
} ;
/// <summary>
/// From DMO_ENUM_FLAGS
/// </summary>
[Flags]
public enum DMOEnumerator
{
None = 0,
IncludeKeyed = 0x00000001
}
/// <summary>
/// From DMO_REGISTER_FLAGS
/// </summary>
[Flags]
public enum DMORegisterFlags
{
None = 0,
IsKeyed = 0x00000001
};
/// <summary>
/// From DMO_PROCESS_OUTPUT_FLAGS
/// </summary>
[Flags]
public enum DMOProcessOutput
{
None = 0x0,
DiscardWhenNoBuffer = 0x00000001
}
/// <summary>
/// From DMO_INPUT_DATA_BUFFER_FLAGS
/// </summary>
[Flags]
public enum DMOInputDataBuffer
{
None = 0,
SyncPoint = 0x1,
Time = 0x2,
TimeLength = 0x4
}
/// <summary>
/// From DMO_INPLACE_PROCESS_FLAGS
/// </summary>
[Flags]
public enum DMOInplaceProcess
{
Normal = 0,
Zero = 0x1
}
/// <summary>
/// From DMO_INPUT_STREAM_INFO_FLAGS
/// </summary>
[Flags]
public enum DMOInputStreamInfo
{
None = 0x0,
WholeSamples = 0x1,
SingleSamplePerBuffer = 0x2,
FixedSampleSize = 0x4,
HoldsBuffers = 0x8
}
/// <summary>
/// From DMO_OUTPUT_STREAM_INFO_FLAGS
/// </summary>
[Flags]
public enum DMOOutputStreamInfo
{
None = 0x0,
WholeSamples = 0x1,
SingleSamplePerBuffer = 0x2,
FixedSampleSize = 0x4,
Discardable = 0x8,
Optional = 0x10
}
/// <summary>
/// From _DMO_SET_TYPE_FLAGS
/// </summary>
[Flags]
public enum DMOSetType
{
None = 0x0,
TestOnly = 0x1,
Clear = 0x2
}
/// <summary>
/// From DMO_INPUT_STATUS_FLAGS
/// </summary>
[Flags]
public enum DMOInputStatusFlags
{
None = 0x0,
AcceptData = 0x1
}
/// <summary>
/// From DMO_VIDEO_OUTPUT_STREAM_FLAGS
/// </summary>
[Flags]
public enum DMOVideoOutputStream
{
None = 0x0,
NeedsPreviousSample = 0x1
}
/// <summary>
/// From DMO_PARTIAL_MEDIATYPE
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DMOPartialMediatype
{
public Guid type;
public Guid subtype;
}
/// <summary>
/// From DMO_OUTPUT_DATA_BUFFER
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct DMOOutputDataBuffer
{
[MarshalAs(UnmanagedType.Interface)]
public IMediaBuffer pBuffer;
public DMOOutputDataBufferFlags dwStatus;
public long rtTimestamp;
public long rtTimelength;
}
#endregion
#region GUIDS
sealed public class DMOCategory
{
private DMOCategory()
{
// Prevent people from trying to instantiate this class
}
/// <summary> DMOCATEGORY_AUDIO_DECODER </summary>
public static readonly Guid AudioDecoder = new Guid(0x57f2db8b, 0xe6bb, 0x4513, 0x9d, 0x43, 0xdc, 0xd2, 0xa6, 0x59, 0x31, 0x25);
/// <summary> DMOCATEGORY_AUDIO_ENCODER </summary>
public static readonly Guid AudioEncoder = new Guid(0x33D9A761, 0x90C8, 0x11d0, 0xBD, 0x43, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86);
/// <summary> DMOCATEGORY_VIDEO_DECODER </summary>
public static readonly Guid VideoDecoder = new Guid(0x4a69b442, 0x28be, 0x4991, 0x96, 0x9c, 0xb5, 0x00, 0xad, 0xf5, 0xd8, 0xa8);
/// <summary> DMOCATEGORY_VIDEO_ENCODER </summary>
public static readonly Guid VideoEncoder = new Guid(0x33D9A760, 0x90C8, 0x11d0, 0xBD, 0x43, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86);
/// <summary> DMOCATEGORY_AUDIO_EFFECT </summary>
public static readonly Guid AudioEffect = new Guid(0xf3602b3f, 0x0592, 0x48df, 0xa4, 0xcd, 0x67, 0x47, 0x21, 0xe7, 0xeb, 0xeb);
/// <summary> DMOCATEGORY_VIDEO_EFFECT </summary>
public static readonly Guid VideoEffect = new Guid(0xd990ee14, 0x776c, 0x4723, 0xbe, 0x46, 0x3d, 0xa2, 0xf5, 0x6f, 0x10, 0xb9);
/// <summary> DMOCATEGORY_AUDIO_CAPTURE_EFFECT </summary>
public static readonly Guid AudioCaptureEffect = new Guid(0xf665aaba, 0x3e09, 0x4920, 0xaa, 0x5f, 0x21, 0x98, 0x11, 0x14, 0x8f, 0x09);
}
#endregion
#region API Declares
sealed public class DMOUtils
{
[DllImport("msdmo.dll", ExactSpelling = true), SuppressUnmanagedCodeSecurity]
public static extern int DMOEnum(
[MarshalAs(UnmanagedType.LPStruct)] Guid DMOCategory,
DMOEnumerator dwFlags,
int cInTypes,
[In] DMOPartialMediatype[] pInTypes,
int cOutTypes,
[In] DMOPartialMediatype[] pOutTypes,
out IEnumDMO ppEnum
);
[DllImport("msdmo.dll", ExactSpelling = true), SuppressUnmanagedCodeSecurity]
public static extern int MoInitMediaType(
[Out] DirectShowLib.AMMediaType pmt,
int i
);
[DllImport("msdmo.dll", ExactSpelling = true), SuppressUnmanagedCodeSecurity]
public static extern int MoCopyMediaType(
[Out, MarshalAs(UnmanagedType.LPStruct)] AMMediaType dst,
[In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType src
);
[DllImport("MSDmo.dll", CharSet = CharSet.Unicode, ExactSpelling = true), SuppressUnmanagedCodeSecurity]
static extern public int DMORegister(
[MarshalAs(UnmanagedType.LPWStr)] string szName,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid clsidDMO,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid DMOCategory,
DMORegisterFlags dwFlags,
int cInTypes,
[In] DMOPartialMediatype[] pInTypes,
int cOutTypes,
[In] DMOPartialMediatype[] pOutTypes
);
[DllImport("MSDmo.dll", ExactSpelling = true), SuppressUnmanagedCodeSecurity]
static extern public int DMOUnregister(
[In, MarshalAs(UnmanagedType.LPStruct)] Guid clsidDMO,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid guidCategory
);
[DllImport("MSDmo.dll", CharSet = CharSet.Unicode, ExactSpelling = true), SuppressUnmanagedCodeSecurity]
static extern public int DMOGetName(
[In, MarshalAs(UnmanagedType.LPStruct)] Guid clsidDMO,
[Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 80)] StringBuilder szName
);
[DllImport("MSDmo.dll", ExactSpelling = true), SuppressUnmanagedCodeSecurity]
static extern public int DMOGetTypes(
[In, MarshalAs(UnmanagedType.LPStruct)] Guid clsidDMO,
int ulInputTypesRequested,
out int pulInputTypesSupplied,
[Out] DMOPartialMediatype[] pInTypes,
int ulOutputTypesRequested,
out int pulOutputTypesSupplied,
[Out] DMOPartialMediatype[] pOutTypes
);
private DMOUtils()
{
}
}
#endregion
#region Utility Classes
sealed public class DMOResults
{
private DMOResults()
{
// Prevent people from trying to instantiate this class
}
public const int E_InvalidStreamIndex = unchecked((int)0x80040201);
public const int E_InvalidType = unchecked((int)0x80040202);
public const int E_TypeNotSet = unchecked((int)0x80040203);
public const int E_NotAccepting = unchecked((int)0x80040204);
public const int E_TypeNotAccepted = unchecked((int)0x80040205);
public const int E_NoMoreItems = unchecked((int)0x80040206);
}
sealed public class DMOError
{
private DMOError()
{
// Prevent people from trying to instantiate this class
}
public static string GetErrorText(int hr)
{
string sRet = null;
switch (hr)
{
case DMOResults.E_InvalidStreamIndex:
sRet = "Invalid stream index.";
break;
case DMOResults.E_InvalidType:
sRet = "Invalid media type.";
break;
case DMOResults.E_TypeNotSet:
sRet = "Media type was not set. One or more streams require a media type before this operation can be performed.";
break;
case DMOResults.E_NotAccepting:
sRet = "Data cannot be accepted on this stream. You might need to process more output data; see IMediaObject::ProcessInput.";
break;
case DMOResults.E_TypeNotAccepted:
sRet = "Media type was not accepted.";
break;
case DMOResults.E_NoMoreItems:
sRet = "Media-type index is out of range.";
break;
default:
sRet = DsError.GetErrorText(hr);
break;
}
return sRet;
}
/// <summary>
/// If hr has a "failed" status code (E_*), throw an exception. Note that status
/// messages (S_*) are not considered failure codes. If DMO or DShow error text
/// is available, it is used to build the exception, otherwise a generic COM error
/// is thrown.
/// </summary>
/// <param name="hr">The HRESULT to check</param>
public static void ThrowExceptionForHR(int hr)
{
// If an error has occurred
if (hr < 0)
{
// If a string is returned, build a com error from it
string buf = GetErrorText(hr);
if (buf != null)
{
throw new COMException(buf, hr);
}
else
{
// No string, just use standard com error
Marshal.ThrowExceptionForHR(hr);
}
}
}
}
#endregion
#region Interfaces
#if ALLOW_UNTESTED_INTERFACES
[ComImport, System.Security.SuppressUnmanagedCodeSecurity,
Guid("65ABEA96-CF36-453F-AF8A-705E98F16260"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDMOQualityControl
{
[PreserveSig]
int SetNow(
[In] long rtNow
);
[PreserveSig]
int SetStatus(
[In] DMOQualityStatus dwFlags
);
[PreserveSig]
int GetStatus(
out DMOQualityStatus pdwFlags
);
}
#endif
[ComImport, System.Security.SuppressUnmanagedCodeSecurity,
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("2C3CD98A-2BFA-4A53-9C27-5249BA64BA0F")]
public interface IEnumDMO
{
[PreserveSig]
int Next(
int cItemsToFetch,
[Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] Guid[] pCLSID,
[Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, ArraySubType = UnmanagedType.LPWStr)] string[] Names,
[In] IntPtr pcItemsFetched
);
[PreserveSig]
int Skip(
int cItemsToSkip
);
[PreserveSig]
int Reset();
[PreserveSig]
int Clone(
[MarshalAs(UnmanagedType.Interface)] out IEnumDMO ppEnum
);
}
[ComImport, System.Security.SuppressUnmanagedCodeSecurity,
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("651B9AD0-0FC7-4AA9-9538-D89931010741")]
public interface IMediaObjectInPlace
{
[PreserveSig]
int Process(
[In] int ulSize,
[In] IntPtr pData,
[In] long refTimeStart,
[In] DMOInplaceProcess dwFlags
);
[PreserveSig]
int Clone(
[MarshalAs(UnmanagedType.Interface)] out IMediaObjectInPlace ppMediaObject
);
[PreserveSig]
int GetLatency(
out long pLatencyTime
);
}
[ComImport, System.Security.SuppressUnmanagedCodeSecurity,
Guid("59EFF8B9-938C-4A26-82F2-95CB84CDC837"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMediaBuffer
{
[PreserveSig]
int SetLength(
int cbLength
);
[PreserveSig]
int GetMaxLength(
out int pcbMaxLength
);
[PreserveSig]
int GetBufferAndLength(
out IntPtr ppBuffer,
out int pcbLength
);
}
[ComImport, System.Security.SuppressUnmanagedCodeSecurity,
Guid("D8AD0F58-5494-4102-97C5-EC798E59BCF4"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMediaObject
{
[PreserveSig]
int GetStreamCount(
out int pcInputStreams,
out int pcOutputStreams
);
[PreserveSig]
int GetInputStreamInfo(
int dwInputStreamIndex,
out DMOInputStreamInfo pdwFlags
);
[PreserveSig]
int GetOutputStreamInfo(
int dwOutputStreamIndex,
out DMOOutputStreamInfo pdwFlags
);
[PreserveSig]
int GetInputType(
int dwInputStreamIndex,
int dwTypeIndex,
[Out] AMMediaType pmt
);
[PreserveSig]
int GetOutputType(
int dwOutputStreamIndex,
int dwTypeIndex,
[Out] AMMediaType pmt
);
[PreserveSig]
int SetInputType(
int dwInputStreamIndex,
[In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt,
DMOSetType dwFlags
);
[PreserveSig]
int SetOutputType(
int dwOutputStreamIndex,
[In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt,
DMOSetType dwFlags
);
[PreserveSig]
int GetInputCurrentType(
int dwInputStreamIndex,
[Out] AMMediaType pmt
);
[PreserveSig]
int GetOutputCurrentType(
int dwOutputStreamIndex,
[Out] AMMediaType pmt
);
[PreserveSig]
int GetInputSizeInfo(
int dwInputStreamIndex,
out int pcbSize,
out int pcbMaxLookahead,
out int pcbAlignment
);
[PreserveSig]
int GetOutputSizeInfo(
int dwOutputStreamIndex,
out int pcbSize,
out int pcbAlignment
);
[PreserveSig]
int GetInputMaxLatency(
int dwInputStreamIndex,
out long prtMaxLatency
);
[PreserveSig]
int SetInputMaxLatency(
int dwInputStreamIndex,
long rtMaxLatency
);
[PreserveSig]
int Flush();
[PreserveSig]
int Discontinuity(
int dwInputStreamIndex
);
[PreserveSig]
int AllocateStreamingResources();
[PreserveSig]
int FreeStreamingResources();
[PreserveSig]
int GetInputStatus(
int dwInputStreamIndex,
out DMOInputStatusFlags dwFlags
);
[PreserveSig]
int ProcessInput(
int dwInputStreamIndex,
IMediaBuffer pBuffer,
DMOInputDataBuffer dwFlags,
long rtTimestamp,
long rtTimelength
);
[PreserveSig]
int ProcessOutput(
DMOProcessOutput dwFlags,
int cOutputBufferCount,
[In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] DMOOutputDataBuffer[] pOutputBuffers,
out int pdwStatus
);
[PreserveSig]
int Lock(
[MarshalAs(UnmanagedType.Bool)] bool bLock
);
}
[ComImport, System.Security.SuppressUnmanagedCodeSecurity,
Guid("BE8F4F4E-5B16-4D29-B350-7F6B5D9298AC"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDMOVideoOutputOptimizations
{
[PreserveSig]
int QueryOperationModePreferences(
int ulOutputStreamIndex,
out DMOVideoOutputStream pdwRequestedCapabilities
);
[PreserveSig]
int SetOperationMode(
int ulOutputStreamIndex,
DMOVideoOutputStream dwEnabledFeatures
);
[PreserveSig]
int GetCurrentOperationMode(
int ulOutputStreamIndex,
out DMOVideoOutputStream pdwEnabledFeatures
);
[PreserveSig]
int GetCurrentSampleRequirements(
int ulOutputStreamIndex,
out DMOVideoOutputStream pdwRequestedFeatures
);
}
#endregion
}