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
|
/**************************************************************************
A/V Stream Camera Sample
Copyright (c) 2013, Microsoft Corporation.
File:
Roi.cpp
Abstract:
This file contains the implemention of CRoiConfig, CRoiProperty and
related classes used in the filter to implement ROI handling.
History:
created 6/27/2013
**************************************************************************/
#include "Common.h"
/**************************************************************************
PAGED CODE
**************************************************************************/
#ifdef ALLOC_PRAGMA
#pragma code_seg("PAGE")
#endif // ALLOC_PRAGMA
// Helper function to diff ptrs.
inline
UINT_PTR
ByteDiffPtrs( PVOID pBase, PVOID pCurrent )
{
PAGED_CODE();
return reinterpret_cast<PBYTE>(pCurrent) - reinterpret_cast<PBYTE>(pBase) ;
}
// Helper function to get the size of an ROI control based just on the ID.
static
ULONG
GetSizeOfRoiInfo( ULONG ControlId )
{
PAGED_CODE();
switch( ControlId )
{
case KSPROPERTY_CAMERACONTROL_EXTENDED_WHITEBALANCEMODE:
return sizeof(KSCAMERA_EXTENDEDPROP_ROI_WHITEBALANCE);
break;
case KSPROPERTY_CAMERACONTROL_EXTENDED_EXPOSUREMODE:
return sizeof(KSCAMERA_EXTENDEDPROP_ROI_EXPOSURE);
break;
case KSPROPERTY_CAMERACONTROL_EXTENDED_FOCUSMODE:
return sizeof(KSCAMERA_EXTENDEDPROP_ROI_FOCUS);
break;
default:
NT_ASSERT(FALSE);
return 0;
}
}
// Helper function to get the size of an ROI control.
static
ULONG
GetSize(
_In_ PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL pCtrl
)
{
PAGED_CODE();
// This actually needs to return the type based on the ROI_INFO size
ULONG RoiInfoSize = GetSizeOfRoiInfo( pCtrl->ControlId );
if( RoiInfoSize )
{
return
sizeof(KSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL) +
(pCtrl->ROICount * RoiInfoSize);
}
else
{
NT_ASSERTMSG("GetSizeOfRoiInfo( pCtrl->ControlId ) returned 0! Should never happen!", FALSE);
return 0;
}
}
// Helper function to walk to the next ROI control.
static
PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL
NextCtrl(
_In_ PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL pCtrl
)
{
PAGED_CODE();
ULONG Size = GetSize( pCtrl );
if( Size )
{
return
reinterpret_cast<PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL>
(((PBYTE) pCtrl) + Size);
}
else
{
NT_ASSERTMSG("GetSize( pCtrl ) returned 0! Should never happen!", FALSE);
return nullptr;
}
}
PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL
CRoiProperty::
Find(
_In_ KSPROPERTY_CAMERACONTROL_EXTENDED_PROPERTY Property
)
/*++
Routine Description:
Search this ROI property for a property ID.
Arguments:
Property -
Which control to find.
Return Value:
A pointer to the ROI control structure.
--*/
{
PAGED_CODE();
// We assume the controls have been validated first.
PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL pIspCtrl =
reinterpret_cast<PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL>(reinterpret_cast<PBYTE>(this) + sizeof(CRoiProperty));
// Loop thru the controls.
for( ULONG i=0; i<m_Hdr.ControlCount; i++ )
{
if( pIspCtrl->ControlId == (ULONG) Property )
{
return pIspCtrl;
}
// Advance to the next control.
pIspCtrl = NextCtrl( pIspCtrl );
if(pIspCtrl == nullptr)
{
NT_ASSERTMSG("NextCtrl( pCtrl ) returned 0! Should never happen!", FALSE);
return nullptr;
}
}
return nullptr;
}
PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL
CRoiProperty::
Add(
_In_ PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL pCtrl
)
/*++
Routine Description:
Add a control to the end of this header.
Note: This function does not reallocate memory. Be careful to ensure
there is space available.
Arguments:
pCtrl -
An ROI control to append.
Return Value:
A pointer to the appended ROI control structure.
--*/
{
PAGED_CODE();
// Only bother to add if there are ROIs.
if( pCtrl->ROICount > 0 )
{
// We assume the controls have been validated first.
PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL pIspCtrl =
reinterpret_cast<PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL>(reinterpret_cast<PBYTE>(this) + sizeof(CRoiProperty));
// Loop thru all the controls.
for( ULONG i=0; i<m_Hdr.ControlCount; i++ )
{
// Advance to the next control.
pIspCtrl = NextCtrl( pIspCtrl );
if (pIspCtrl == nullptr)
{
NT_ASSERTMSG("NextCtrl( pCtrl ) returned 0! Should never happen!", FALSE);
return nullptr;
}
}
ULONG SizeToCopy = ::GetSize(pCtrl);
memcpy( pIspCtrl,
pCtrl,
SizeToCopy ) ;
Size += SizeToCopy ; // Adjust our size.
m_Hdr.Size += SizeToCopy ;
m_Hdr.ControlCount++ ; // Add one to the control count.
}
return nullptr;
}
bool
CRoiProperty::
isValid()
/*++
Routine Description:
Parse the current ROI Property list and make sure all the control
structures are self-consistent.
Note: This is a very non-trivial problem.
Arguments:
None
Return Value:
A pointer to the appended ROI control structure.
--*/
{
PAGED_CODE();
DBG_ENTER( "()" );
ULONGLONG FocusFlags = 0;
ULONGLONG ExposureFlags = 0;
ULONGLONG WhiteBalanceFlags = 0;
if( Version < KSCAMERA_EXTENDEDPROP_VERSION ||
PinId != KSCAMERA_EXTENDEDPROP_FILTERSCOPE ||
(Capability & ~(KSCAMERA_EXTENDEDPROP_CAPS_ASYNCCONTROL | KSCAMERA_EXTENDEDPROP_CAPS_CANCELLABLE)) != 0 ||
(Flags & ~KSCAMERA_EXTENDEDPROP_FLAG_CANCELOPERATION) != 0 || // Allow cancellation of ROI.
Size < sizeof(*this) )
{
//NT_ASSERT(FALSE);
if( Version < KSCAMERA_EXTENDEDPROP_VERSION )
{
DBG_TRACE( "Failed: Verison=%d", Version );
}
if( PinId != KSCAMERA_EXTENDEDPROP_FILTERSCOPE )
{
DBG_TRACE( "Failed: PinId=%d", PinId );
}
if( (Capability & ~(KSCAMERA_EXTENDEDPROP_CAPS_ASYNCCONTROL | KSCAMERA_EXTENDEDPROP_CAPS_CANCELLABLE)) != 0 )
{
DBG_TRACE( "Failed: Capability=0x%016llX", Capability );
}
if( (Flags & ~KSCAMERA_EXTENDEDPROP_FLAG_CANCELOPERATION) != 0 )
{
DBG_TRACE( "Failed: Flags=0x%016llX", Flags );
}
if( Size < sizeof(*this) )
{
DBG_TRACE( "Failed(0): Size=%d, should be at least %Iu", Size, sizeof(*this) );
}
DBG_LEAVE( "()=false" );
return false;
}
PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL pIspCtrl =
reinterpret_cast<PKSCAMERA_EXTENDEDPROP_ROI_ISPCONTROL>(reinterpret_cast<BYTE*>(this) + sizeof(*this));
// Loop thru the controls.
for( ULONG i=0; i<m_Hdr.ControlCount; i++ )
{
// Make sure there is room to inspect this control
if( Size < ByteDiffPtrs( this, reinterpret_cast<PBYTE>(pIspCtrl) + sizeof(*pIspCtrl) ) ||
m_Hdr.Size < ByteDiffPtrs( &m_Hdr, reinterpret_cast<PBYTE>(pIspCtrl) + sizeof(*pIspCtrl) ) )
{
//NT_ASSERT(FALSE);
DBG_TRACE( "Failed(1): Size=%d, should be at least %Iu", Size, ByteDiffPtrs( this, pIspCtrl+1 ) );
DBG_TRACE( " Or: m_Hdr.Size=%d, should be at least %Iu", m_Hdr.Size, ByteDiffPtrs( &m_Hdr, pIspCtrl+1 ) );
DBG_LEAVE( "()=false" );
return false;
}
// Make sure the control is of a known type
switch( pIspCtrl->ControlId )
{
case KSPROPERTY_CAMERACONTROL_EXTENDED_WHITEBALANCEMODE:
case KSPROPERTY_CAMERACONTROL_EXTENDED_EXPOSUREMODE:
case KSPROPERTY_CAMERACONTROL_EXTENDED_FOCUSMODE:
break;
default:
//NT_ASSERT(FALSE);
DBG_LEAVE( "()=false : Invalid ControlId = 0x%08X", pIspCtrl->ControlId );
return false;
}
// Make sure there is room for the # of ROIs reported
if( Size < ByteDiffPtrs( this, NextCtrl(pIspCtrl) ) ||
m_Hdr.Size < ByteDiffPtrs( &m_Hdr, NextCtrl(pIspCtrl) ) )
{
//NT_ASSERT(FALSE);
DBG_TRACE( "Failed(2): Size=%d, should be at least %Iu", Size, ByteDiffPtrs( this, NextCtrl(pIspCtrl) ) );
DBG_TRACE( " Or: m_Hdr.Size=%d, should be at least %Iu", m_Hdr.Size, ByteDiffPtrs( &m_Hdr, NextCtrl(pIspCtrl) ) );
DBG_LEAVE( "()=false" );
return false;
}
// Validate the ROIs
for( ULONG j=0; j<pIspCtrl->ROICount; j++ )
{
// Index into to the control's ROI list. Get the equivilent of "pIspCtrl->RoiInfo[j]"
PKSCAMERA_EXTENDEDPROP_ROI_INFO pRoiInfo =
reinterpret_cast<PKSCAMERA_EXTENDEDPROP_ROI_INFO>
((reinterpret_cast<PBYTE>(pIspCtrl) + sizeof(*pIspCtrl)) + (j * GetSizeOfRoiInfo(pIspCtrl->ControlId) ));
// Validate the cooridinates
if( pRoiInfo->Region.top < (LONG) TO_Q31(0) ||
pRoiInfo->Region.bottom < (LONG) TO_Q31(0) ||
pRoiInfo->Region.left < (LONG) TO_Q31(0) ||
pRoiInfo->Region.right < (LONG) TO_Q31(0) )
{
//NT_ASSERT(FALSE);
DBG_TRACE( "Failed: Region out of range." );
DBG_TRACE( " (top=0x%08X, left=0x%08X, bottom=0x%08X, right=0x%08X)"
, pRoiInfo->Region.top
, pRoiInfo->Region.bottom
, pRoiInfo->Region.left
, pRoiInfo->Region.right );
DBG_LEAVE( "()=false" );
return false;
}
// Validate the RegionOfInterestType
switch( pRoiInfo->RegionOfInterestType )
{
case KSCAMERA_EXTENDEDPROP_ROITYPE_UNKNOWN:
case KSCAMERA_EXTENDEDPROP_ROITYPE_FACE:
break;
default:
//NT_ASSERT(FALSE);
DBG_LEAVE( "()=false Failed: RegionOfInterestType=%d", pRoiInfo->RegionOfInterestType );
return false;
}
// Validate the Weight
if( pRoiInfo->Weight > 100 )
{
//NT_ASSERT(FALSE);
DBG_LEAVE( "()=false Failed: Weight=%d", pRoiInfo->Weight );
return false;
}
// Validate the Flags.
// Note: the driver doesn't allow mis-matched ROI Flags.
ULONGLONG RoiFlags = pRoiInfo->Flags & ~KSCAMERA_EXTENDEDPROP_FLAG_CANCELOPERATION;
switch( pIspCtrl->ControlId )
{
// Exposure & WhiteBalance only support Auto, Manual & Lock.
case KSPROPERTY_CAMERACONTROL_EXTENDED_EXPOSUREMODE:
if( j!=0 )
{
if( RoiFlags != ExposureFlags )
{
DBG_LEAVE( "()=false : Mismatched Exposure Flags" );
return false;
}
}
else
{
switch( RoiFlags &
(KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO|
KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_LOCK) )
{
case KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO:
case KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO + KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_LOCK:
case 0:
break;
default:
DBG_LEAVE( "()=false : Invalid Exposure Flags" );
return false;
}
ExposureFlags = RoiFlags;
}
break;
case KSPROPERTY_CAMERACONTROL_EXTENDED_WHITEBALANCEMODE:
if( j!=0 )
{
if( RoiFlags != WhiteBalanceFlags )
{
DBG_LEAVE( "()=false : Mismatched WhiteBalance Flags" );
return false;
}
}
else
{
switch( RoiFlags &
(KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO|
KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_LOCK) )
{
case KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO:
case KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO + KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_LOCK:
case 0:
break;
default:
DBG_LEAVE( "()=false : Invalid WhiteBalance Flags" );
return false;
}
WhiteBalanceFlags = RoiFlags;
}
break;
// Focus supports additional flags...
case KSPROPERTY_CAMERACONTROL_EXTENDED_FOCUSMODE:
if( j!=0 )
{
if( RoiFlags != FocusFlags )
{
DBG_LEAVE( "()=false : Mismatched Focus Flags" );
return false;
}
}
else
{
switch( RoiFlags &
(KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO |
KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_LOCK |
KSCAMERA_EXTENDEDPROP_FOCUS_CONTINUOUS |
KSCAMERA_EXTENDEDPROP_FOCUS_CONTINUOUSLOCK) )
{
case KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO:
case KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_AUTO + KSCAMERA_EXTENDEDPROP_VIDEOPROCFLAG_LOCK:
case KSCAMERA_EXTENDEDPROP_FOCUS_CONTINUOUS:
case KSCAMERA_EXTENDEDPROP_FOCUS_CONTINUOUS + KSCAMERA_EXTENDEDPROP_FOCUS_CONTINUOUSLOCK:
case 0:
break;
default:
DBG_LEAVE( "()=false : Invalid Focus Flags" );
return false;
}
switch( RoiFlags & KSCAMERA_EXTENDEDPROP_FOCUS_RANGE_MASK )
{
case KSCAMERA_EXTENDEDPROP_FOCUS_RANGE_MACRO:
case KSCAMERA_EXTENDEDPROP_FOCUS_RANGE_NORMAL:
case KSCAMERA_EXTENDEDPROP_FOCUS_RANGE_FULLRANGE:
// Depreciated cases are not supported:
//case KSCAMERA_EXTENDEDPROP_FOCUS_RANGE_INFINITY:
//case KSCAMERA_EXTENDEDPROP_FOCUS_RANGE_HYPERFOCAL:
case 0:
break;
default:
DBG_LEAVE( "()=false : Invalid Focus Range" );
return false;
}
FocusFlags = RoiFlags;
}
break;
}
}
// Advance to the next control.
pIspCtrl = NextCtrl( pIspCtrl ) ;
}
DBG_LEAVE( "()=true" );
return true;
}
// Log to the debug output.
void
CRoiProperty::
Log()
{
PAGED_CODE();
DBG_TRACE("Version=%d, PinId=%d, Capability=0x%016llX, Flags=0x%016llX, Size=%d",
Version, PinId, Capability, Flags, Size );
CRoiIspControl *pIspCtrl = reinterpret_cast<CRoiIspControl *> (this+1);
// Loop thru the controls.
for( ULONG i=0; i<m_Hdr.ControlCount; i++ )
{
pIspCtrl->Log();
// Advance to the next control.
pIspCtrl = reinterpret_cast<CRoiIspControl *>( NextCtrl( pIspCtrl ) ) ;
if (pIspCtrl == nullptr)
{
NT_ASSERTMSG("NextCtrl( pCtrl ) returned 0! Should never happen!", FALSE);
return;
}
}
}
//
// Initialize a block of memory from another ROI control
//
// Note: This function assumes enough ROI_INFO structures follow
// such that this copy will not overrun.
//
BOOL
CRoiIspControl::
Init(
_In_ CRoiProperty *pRoiProperty,
_In_ KSPROPERTY_CAMERACONTROL_EXTENDED_PROPERTY Property
)
/*++
Routine Description:
Initialize a block of memory from another ROI control
Note: This function assumes enough ROI_INFO structures follow
such that this copy will not overrun.
Arguments:
pRoiProperty -
The extended ROI control payload.
Property -
Which ISP control to copy from the payload.
Return Value:
TRUE - The ROI was set.
FALSE - The ROI was cleared.
--*/
{
PAGED_CODE();
CRoiIspControl *
pCtrl = reinterpret_cast<CRoiIspControl *> (pRoiProperty->Find( Property ));
if( pCtrl )
{
memcpy( this,
pCtrl,
pCtrl->GetSize() );
return TRUE;
}
ControlId = Property;
ROICount = 0;
return FALSE;
}
// Log to the debug output.
void
CRoiIspControl::
Log()
{
PAGED_CODE();
switch( ControlId )
{
case KSPROPERTY_CAMERACONTROL_EXTENDED_WHITEBALANCEMODE:
DBG_TRACE("WhiteBalance");
break;
case KSPROPERTY_CAMERACONTROL_EXTENDED_EXPOSUREMODE:
DBG_TRACE("Exposure");
break;
case KSPROPERTY_CAMERACONTROL_EXTENDED_FOCUSMODE:
DBG_TRACE("Focus");
break;
default:
DBG_TRACE("[Unknown]");
NT_ASSERT(FALSE);
break;
}
DBG_TRACE("Result=0x%08X", Result);
for( ULONG i=0; i<ROICount; i++ )
{
// Index into to the control's ROI list. Get the equivilent of "pIspCtrl->RoiInfo[j]"
PKSCAMERA_EXTENDEDPROP_ROI_INFO pRoiInfo =
reinterpret_cast<PKSCAMERA_EXTENDEDPROP_ROI_INFO>
(((PBYTE) (this+1)) + (i * GetSizeOfRoiInfo(ControlId) ));
DBG_TRACE(" | Flags=0x%016llX [Top=0x%08X, Left=0x%08X, Bottom=0x%08X, Right=0x%08X]",
pRoiInfo->Flags,
pRoiInfo->Region.top,
pRoiInfo->Region.left,
pRoiInfo->Region.bottom,
pRoiInfo->Region.right );
}
}
//
// Initialize a block of memory with an empty control list.
//
CRoiIspControl::
CRoiIspControl(
_In_ KSPROPERTY_CAMERACONTROL_EXTENDED_PROPERTY Property
)
{
PAGED_CODE();
ControlId = Property;
ROICount = 0;
Result = 0;
Reserved = 0;
}
ULONG
CRoiIspControl::
GetSize()
{
PAGED_CODE();
return (ROICount>0) ? ::GetSize( this ) : 0;
}
|