-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathcamera_windows.c
896 lines (746 loc) · 25.9 KB
/
camera_windows.c
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/*
pygame-ce - Python Game Library
Copyright (C) 2021 Charlie Hayden
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Windows Camera - webcam support for pygame
* Original Author: Charlie Hayden, 2021
*
* This sub-module adds native support for windows webcams to pygame,
* made possible by Microsoft Media Foundation.
*
* Since Media Foundation is still maturing, support is best in Windows 10 or
* above, but I believe this will work in Windows 8 as well.
*
* The Media Foundation documentation is written for use in C++, but the API
* supports C as well. What would be a call to a class method in C++ is a call
* through a pointer table lpVtbl.
* For example obj->func(arguments) would be obj->lpVtbl->func(obj, arguments)
* Also, GUIDs need to be addressed using '&' to work.
*/
#include "_camera.h"
#include "pgcompat.h"
#ifdef PYGAME_WINDOWS_CAMERA
/*these are already included in camera.h, but having them here
* makes all the types be recognized by VS code */
#include <mfapi.h>
#include <mfobjects.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#include <combaseapi.h>
#include <mftransform.h>
#include <math.h>
#define RELEASE(obj) \
if (obj) { \
obj->lpVtbl->Release(obj); \
obj = NULL; \
}
/* HRESULT failure numbers can be looked up on
* hresult.info to get the actual name */
#define FORMATHR(hr, line) \
PyErr_Format(pgExc_SDLError, \
"Media Foundation " \
"HRESULT failure %i on camera_windows.c " \
"line %i", \
hr, line);
#define CHECKHR(hr) \
if (FAILED(hr)) { \
FORMATHR(hr, __LINE__) \
return 0; \
}
#define HANDLEHR(hr) \
if (FAILED(hr)) { \
FORMATHR(hr, __LINE__) \
goto cleanup; \
}
#define T_HANDLEHR(hr) \
if (FAILED(hr)) { \
self->t_error = hr; \
self->t_error_line = __LINE__; \
break; \
}
#define T_FORMATHR(hr, line) \
PyErr_Format(pgExc_SDLError, \
"Media Foundation " \
"HRESULT failure %i on camera_windows.c " \
"line %i (Capture Thread Quit)", \
hr, line);
int
_check_integrity(pgCameraObject *self)
{
if (FAILED(self->t_error)) {
/* MF_E_HW_MFT_FAILED_START_STREAMING */
if (self->t_error == (HRESULT)-1072875772) {
PyErr_SetString(PyExc_SystemError,
"Camera already in use (Capture Thread Quit)");
}
/* MF_E_VIDEO_RECORDING_DEVICE_INVALIDATED */
else if (self->t_error == (HRESULT)-1072873822) {
PyErr_SetString(PyExc_SystemError,
"Camera disconnected (Capture Thread Quit)");
}
else {
T_FORMATHR(self->t_error, self->t_error_line)
}
return 0;
}
return 1;
}
#define FIRST_VIDEO MF_SOURCE_READER_FIRST_VIDEO_STREAM
#define DEVSOURCE_VIDCAP_GUID MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID
#define DEVSOURCE_NAME MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME
#define HR_UPSTREAM_FAILURE -12345
#define DISTANCE(x, y) (int)pow(x, 2) + (int)pow(y, 2)
/* These are the only supported input types
* (TODO?) broaden in the future by enumerating MFTs to find decoders?
* drawn from:
* https://docs.microsoft.com/en-us/windows/win32/medfound/video-processor-mft
*/
#define NUM_FORM 20
const GUID *inp_types[NUM_FORM] = {
&MFVideoFormat_ARGB32, &MFVideoFormat_AYUV, &MFVideoFormat_I420,
&MFVideoFormat_IYUV, &MFVideoFormat_NV11, &MFVideoFormat_NV12,
&MFVideoFormat_RGB24, &MFVideoFormat_RGB32, &MFVideoFormat_RGB555,
&MFVideoFormat_RGB8, &MFVideoFormat_RGB565, &MFVideoFormat_UYVY,
&MFVideoFormat_v410, &MFVideoFormat_Y216, &MFVideoFormat_Y41P,
&MFVideoFormat_Y41T, &MFVideoFormat_Y42T, &MFVideoFormat_YUY2,
&MFVideoFormat_YV12, &MFVideoFormat_YVYU};
int
_is_supported_input_format(GUID format)
{
for (int i = 0; i < NUM_FORM; i++) {
if (format.Data1 == inp_types[i]->Data1)
return 1;
}
return 0;
}
int
_get_name_from_activate(IMFActivate *pActive, WCHAR **ppAttr)
{
HRESULT hr;
UINT32 cchLength = 0;
WCHAR *res = NULL;
hr =
pActive->lpVtbl->GetStringLength(pActive, &DEVSOURCE_NAME, &cchLength);
CHECKHR(hr);
res = malloc(sizeof(WCHAR) * (cchLength + 1));
if (!res) {
hr = E_OUTOFMEMORY;
CHECKHR(hr);
}
hr = pActive->lpVtbl->GetString(pActive, &DEVSOURCE_NAME, res,
cchLength + 1, &cchLength);
HANDLEHR(hr);
*ppAttr = res;
return 1;
cleanup:
free(res);
return 0;
}
WCHAR **
windows_list_cameras(int *num_devices)
{
WCHAR **devices = NULL;
IMFAttributes *pAttributes = NULL;
IMFActivate **ppDevices = NULL;
UINT32 count = 0;
HRESULT hr;
hr = MFCreateAttributes(&pAttributes, 1);
HANDLEHR(hr);
hr = pAttributes->lpVtbl->SetGUID(pAttributes,
&MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
&DEVSOURCE_VIDCAP_GUID);
HANDLEHR(hr);
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
HANDLEHR(hr);
/* freed by caller of this function */
devices = (WCHAR **)malloc(sizeof(WCHAR *) * count);
if (!devices) {
hr = E_OUTOFMEMORY;
HANDLEHR(hr);
}
for (int i = 0; i < (int)count; i++) {
if (!_get_name_from_activate(ppDevices[i], &devices[i])) {
hr = HR_UPSTREAM_FAILURE;
goto cleanup;
}
}
*num_devices = count;
goto cleanup;
cleanup:
RELEASE(pAttributes);
for (int i = 0; i < (int)count; i++) {
RELEASE(ppDevices[i]);
}
CoTaskMemFree(ppDevices);
if (FAILED(hr)) {
if (devices) {
for (int i = 0; i < (int)count; i++) {
free(devices[i]);
}
free(devices);
return NULL;
}
else {
return NULL;
}
}
else {
return devices;
}
}
IMFActivate *
windows_device_from_name(WCHAR *device_name)
{
IMFAttributes *pAttributes = NULL;
IMFActivate **ppDevices = NULL;
IMFActivate *ret_device;
WCHAR *_device_name = NULL;
UINT32 count = 0;
HRESULT hr;
hr = MFCreateAttributes(&pAttributes, 1);
HANDLEHR(hr);
hr = pAttributes->lpVtbl->SetGUID(pAttributes,
&MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
&DEVSOURCE_VIDCAP_GUID);
HANDLEHR(hr);
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
HANDLEHR(hr);
for (int i = 0; i < (int)count; i++) {
if (!_get_name_from_activate(ppDevices[i], &_device_name)) {
hr = HR_UPSTREAM_FAILURE;
goto cleanup;
}
if (!wcscmp(_device_name, device_name)) {
free(_device_name);
for (int j = 0; j < (int)count; j++) {
if (i != j) {
RELEASE(ppDevices[j]);
}
}
ret_device = ppDevices[i];
CoTaskMemFree(ppDevices);
return ret_device;
}
free(_device_name);
}
goto cleanup;
cleanup:
RELEASE(pAttributes);
for (int i = 0; i < (int)count; i++) {
RELEASE(ppDevices[i]);
}
CoTaskMemFree(ppDevices);
return NULL;
}
/* Enumerates the formats supported "natively" by the webcam, and picks one
* that is A) supported by the video processor MFT used for processing, and
* B) close to the requested size */
int
_select_source_type(pgCameraObject *self, IMFMediaType **mp)
{
HRESULT hr;
IMFMediaType *media_type = NULL;
int type_count = 0;
UINT32 t_width, t_height;
IMFMediaType **native_types = NULL;
int *diagonal_distances = NULL;
UINT64 size;
GUID subtype;
int difference = 100000000;
int current_difference;
int index = -1;
int requested_diagonal = DISTANCE(self->width, self->height);
UINT64 fps_max_ratio;
/* Find out how many native media types there are
* (different resolution modes, mostly) */
while (1) {
hr = self->reader->lpVtbl->GetNativeMediaType(
self->reader, FIRST_VIDEO, type_count, &media_type);
if (FAILED(hr)) {
break;
}
type_count++;
RELEASE(media_type);
}
if (!type_count) {
PyErr_SetString(pgExc_SDLError,
"Could not find any video types on this camera");
return 0;
}
native_types = malloc(sizeof(IMFMediaType *) * type_count);
diagonal_distances = malloc(sizeof(int) * type_count);
if (!native_types || !diagonal_distances) {
hr = E_OUTOFMEMORY;
HANDLEHR(hr);
}
/* This is interesting
* https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/9d6a8704-764f-46df-a41c-8e9d84f7f0f3/mjpg-encoded-media-type-is-not-available-for-usbuvc-webcameras-after-windows-10-version-1607-os
* Compressed video modes listed below are "backwards compatibility modes"
* And will always have an uncompressed counterpart (NV12 / YUY2)
* At least in Windows 10 since 2016 */
for (int i = 0; i < type_count; i++) {
hr = self->reader->lpVtbl->GetNativeMediaType(
self->reader, FIRST_VIDEO, i, &media_type);
HANDLEHR(hr);
hr = media_type->lpVtbl->GetUINT64(media_type, &MF_MT_FRAME_SIZE,
&size);
HANDLEHR(hr);
t_width = size >> 32;
t_height = size << 32 >> 32;
hr = media_type->lpVtbl->GetGUID(media_type, &MF_MT_SUBTYPE, &subtype);
HANDLEHR(hr);
/* Debug printf for seeing what media types are available */
/*
printf("media_type: %li x %li, subtype id = %li\n", t_width, t_height,
subtype.Data1);
*/
if (_is_supported_input_format(subtype)) {
native_types[i] = media_type;
diagonal_distances[i] = DISTANCE(t_width, t_height);
}
else {
/* types that are unsupported are set to NULL, which is checked in
* the selection loop */
native_types[i] = NULL;
diagonal_distances[i] = 0;
}
}
for (int i = 0; i < type_count; i++) {
current_difference = diagonal_distances[i] - requested_diagonal;
current_difference = abs(current_difference);
if (current_difference < difference && native_types[i]) {
index = i;
difference = current_difference;
}
}
if (index == -1) {
PyErr_SetString(pgExc_SDLError,
"Could not find a valid video type in any supported"
" format");
hr = HR_UPSTREAM_FAILURE;
goto cleanup;
}
hr = native_types[index]->lpVtbl->GetUINT64(native_types[index],
&MF_MT_FRAME_SIZE, &size);
HANDLEHR(hr);
self->width = size >> 32;
self->height = size << 32 >> 32;
/* More debug printfs that could be handy in the future */
/*
printf("chosen index # =%i\n", index);
printf("chosen width=%lli, chosen height=%lli\n", size >> 32,
size << 32 >> 32);
*/
/* Can't hurt to tell the webcam to use its highest possible framerate
* Although I haven't seen any upside from this either */
hr = native_types[index]->lpVtbl->GetUINT64(
native_types[index], &MF_MT_FRAME_RATE_RANGE_MAX, &fps_max_ratio);
if (!FAILED(hr)) { /* If it fails to get max FPS, don't quit out, just skip
this step */
hr = native_types[index]->lpVtbl->SetUINT64(
native_types[index], &MF_MT_FRAME_RATE, fps_max_ratio);
HANDLEHR(hr);
}
else {
hr = S_OK; /* clear error */
}
*mp = native_types[index];
goto cleanup;
cleanup:
free(diagonal_distances);
for (int i = 0; i < type_count; i++) {
if (i != index) {
RELEASE(native_types[i]);
}
}
if (FAILED(hr)) {
if (index > 0) {
RELEASE(native_types[index]);
}
free(native_types);
return 0;
}
else {
free(native_types);
return 1;
}
}
/* Based on the requested video format out (RGB, HSV, YUV), attempts to find
* a conversion type that works with the source type. MSMF makes very few
* guarantees, but RGB32 has the best support. If even RGB32 fails for some
* reason, it raises an exception. The conversion type is the output of the
* video processor MFT, the source type is the input. */
int
_select_conversion_type(pgCameraObject *self, IMFMediaType **mp)
{
IMFMediaType *conv_type = NULL;
HRESULT hr;
int depth;
IMFTransform *transform = self->transform;
UINT64 size;
hr = MFCreateMediaType(&conv_type);
HANDLEHR(hr);
hr = conv_type->lpVtbl->SetGUID(conv_type, &MF_MT_MAJOR_TYPE,
&MFMediaType_Video);
HANDLEHR(hr);
/* reconstruct the size num out of width/height set by the source */
size = self->width;
size = size << 32;
size += self->height;
hr = conv_type->lpVtbl->SetUINT64(conv_type, &MF_MT_FRAME_SIZE, size);
HANDLEHR(hr);
if (self->color_out != YUV_OUT) {
hr = conv_type->lpVtbl->SetGUID(conv_type, &MF_MT_SUBTYPE,
&MFVideoFormat_RGB32);
HANDLEHR(hr);
hr = transform->lpVtbl->SetOutputType(transform, 0, conv_type,
MFT_SET_TYPE_TEST_ONLY);
HANDLEHR(hr);
self->pixelformat = MFVideoFormat_RGB32.Data1;
depth = 4;
}
else {
hr = conv_type->lpVtbl->SetGUID(conv_type, &MF_MT_SUBTYPE,
&MFVideoFormat_YUY2);
HANDLEHR(hr);
hr = transform->lpVtbl->SetOutputType(transform, 0, conv_type,
MFT_SET_TYPE_TEST_ONLY);
if (hr == (HRESULT)-1072875852) { // MF_E_INVALIDMEDIATYPE
hr = conv_type->lpVtbl->SetGUID(conv_type, &MF_MT_SUBTYPE,
&MFVideoFormat_RGB32);
HANDLEHR(hr);
hr = transform->lpVtbl->SetOutputType(transform, 0, conv_type,
MFT_SET_TYPE_TEST_ONLY);
HANDLEHR(hr);
self->pixelformat = MFVideoFormat_RGB32.Data1;
depth = 4;
}
else {
HANDLEHR(hr);
self->pixelformat = MFVideoFormat_YUY2.Data1;
depth = 2;
}
}
/* make sure the output is right side up by default
* multiplied by 4 because that is the number of bytes per pixel */
hr = conv_type->lpVtbl->SetUINT32(conv_type, &MF_MT_DEFAULT_STRIDE,
self->width * depth);
HANDLEHR(hr);
*mp = conv_type;
return 1;
cleanup:
RELEASE(conv_type);
return 0;
}
DWORD WINAPI
update_function(LPVOID lpParam)
{
pgCameraObject *self = (pgCameraObject *)lpParam;
IMFSample *sample;
HRESULT hr;
DWORD pdwStreamFlags;
IMFSourceReader *reader = self->reader;
IMFMediaType *output_type;
INT32 stride;
while (1) {
sample = NULL;
/* flip control subsystem -
* I managed to do it all within media foundation, without
* camera_mac.m's flip_image, or v4l2's set_controls API.
* The self->transform (Video Processor MFT) also exposes a control
* interface, which can be used to set vertical or horizontal
* mirroring, but not both. So for vertical mirroring, it changes the
* stride of the image so it comes out of the transform upside down */
if (self->hflip) {
hr = self->control->lpVtbl->SetMirror(self->control,
MIRROR_HORIZONTAL);
T_HANDLEHR(hr);
}
else {
hr = self->control->lpVtbl->SetMirror(self->control, MIRROR_NONE);
T_HANDLEHR(hr);
}
if (self->vflip != self->last_vflip) {
hr = self->transform->lpVtbl->GetOutputCurrentType(
self->transform, 0, &output_type);
T_HANDLEHR(hr);
hr = output_type->lpVtbl->GetUINT32(
output_type, &MF_MT_DEFAULT_STRIDE, &stride);
T_HANDLEHR(hr);
hr = output_type->lpVtbl->SetUINT32(
output_type, &MF_MT_DEFAULT_STRIDE, -stride);
T_HANDLEHR(hr);
hr = self->transform->lpVtbl->SetOutputType(self->transform, 0,
output_type, 0);
T_HANDLEHR(hr);
self->last_vflip = self->vflip;
}
hr = reader->lpVtbl->ReadSample(reader, FIRST_VIDEO, 0, 0,
&pdwStreamFlags, NULL, &sample);
T_HANDLEHR(hr);
if (!self->open) {
RELEASE(sample);
break;
}
if (sample) {
/* Put unprocessed sample into another buffer for get_raw() */
RELEASE(self->raw_buf);
sample->lpVtbl->ConvertToContiguousBuffer(sample, &self->raw_buf);
hr = self->transform->lpVtbl->ProcessInput(self->transform, 0,
sample, 0);
T_HANDLEHR(hr);
MFT_OUTPUT_DATA_BUFFER mft_buffer[1];
MFT_OUTPUT_DATA_BUFFER x;
IMFSample *ns;
hr = MFCreateSample(&ns);
T_HANDLEHR(hr);
hr = ns->lpVtbl->AddBuffer(ns, self->buf);
T_HANDLEHR(hr);
x.pSample = ns;
x.dwStreamID = 0;
x.dwStatus = 0;
x.pEvents = NULL;
mft_buffer[0] = x;
DWORD out;
hr = self->transform->lpVtbl->ProcessOutput(self->transform, 0, 1,
mft_buffer, &out);
T_HANDLEHR(hr);
self->buffer_ready = 1;
}
RELEASE(sample);
}
/* printf("exiting 2nd thread...\n"); */
ExitThread(0);
}
int
windows_open_device(pgCameraObject *self)
{
IMFMediaSource *source = NULL;
IMFSourceReader *reader = NULL;
IMFVideoProcessorControl *control = NULL;
IMFTransform *transform = NULL;
IMFMediaType *source_type = NULL;
IMFMediaType *conv_type = NULL;
MFT_OUTPUT_STREAM_INFO info;
HRESULT hr;
IMFActivate *act = windows_device_from_name(self->device_name);
hr = act->lpVtbl->ActivateObject(act, &IID_IMFMediaSource, &source);
RELEASE(act);
HANDLEHR(hr);
/* The commented out code below sets the source reader to use video
* processing, which guarantees it can output RGB32 from any format.
* Sadly, using the source reader in this way would only let me open
* the webcam in a single size, so I replaced it with a video processor
* MFT. (Which also helps with the flip controls). */
/*
IMFAttributes *rsa;
MFCreateAttributes(&rsa, 1);
rsa->lpVtbl->SetUINT32(rsa, &MF_SOURCE_READER_ENABLE_VIDEO_PROCESSING, 1);
hr = MFCreateSourceReaderFromMediaSource(source, rsa, &reader);
RELEASE(rsa);
CHECKHR(hr);
*/
hr = MFCreateSourceReaderFromMediaSource(source, NULL, &reader);
self->reader = reader;
RELEASE(source);
HANDLEHR(hr);
if (!_select_source_type(self, &source_type)) {
return 0;
}
hr = reader->lpVtbl->SetCurrentMediaType(reader, FIRST_VIDEO, NULL,
source_type);
HANDLEHR(hr);
hr = CoCreateInstance(&CLSID_VideoProcessorMFT, NULL, CLSCTX_INPROC_SERVER,
&IID_IMFTransform, &transform);
self->transform = transform;
HANDLEHR(hr);
hr = transform->lpVtbl->QueryInterface(
transform, &IID_IMFVideoProcessorControl, &control);
self->control = control;
HANDLEHR(hr);
hr = transform->lpVtbl->SetInputType(transform, 0, source_type, 0);
HANDLEHR(hr);
if (!_select_conversion_type(self, &conv_type)) {
return 0;
}
hr = transform->lpVtbl->SetOutputType(transform, 0, conv_type, 0);
HANDLEHR(hr);
hr = self->transform->lpVtbl->GetOutputStreamInfo(self->transform, 0,
&info);
HANDLEHR(hr);
hr = MFCreateMemoryBuffer(info.cbSize, &self->buf);
HANDLEHR(hr);
self->t_handle = CreateThread(NULL, 0, update_function, self, 0, NULL);
self->open = 1; /* set here, since this shouldn't happen on error */
self->t_error = S_OK;
self->t_error_line = 0;
return 1;
cleanup:
windows_close_device(self);
return 0;
}
int
windows_close_device(pgCameraObject *self)
{
self->open = 0;
if (self->t_handle) {
WaitForSingleObject(self->t_handle, 3000);
}
RELEASE(self->reader);
RELEASE(self->transform);
RELEASE(self->control);
RELEASE(self->buf);
RELEASE(self->raw_buf);
return 1;
}
int
windows_init_device(pgCameraObject *self)
{
HRESULT hr;
/* I wanted to use COINIT_MULTITHREADED, but something inside SDL started
* by display.set_mode() uses COINIT_APARTMENTTHREADED, and you can't have
* a thread in two modes at once */
hr = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
CHECKHR(hr);
hr = MFStartup(MF_VERSION, MFSTARTUP_LITE);
CHECKHR(hr);
return 1;
}
void
windows_dealloc_device(pgCameraObject *self)
{
PyMem_Free(self->device_name);
MFShutdown();
CoUninitialize();
}
int
windows_process_image(pgCameraObject *self, BYTE *data, DWORD length,
SDL_Surface *surf)
{
SDL_LockSurface(surf);
int size = self->width * self->height;
/* apparently RGB32 is actually BGR to Microsoft */
if (self->pixelformat == MFVideoFormat_RGB32.Data1) {
switch (self->color_out) {
case RGB_OUT:
/* optimized version for 32 bit output surfaces*/
/* memcpy(surf->pixels, data, length); */
bgr32_to_rgb(data, surf->pixels, size, surf->format);
break;
case YUV_OUT:
rgb_to_yuv(data, surf->pixels, size, V4L2_PIX_FMT_XBGR32,
surf->format);
break;
case HSV_OUT:
rgb_to_hsv(data, surf->pixels, size, V4L2_PIX_FMT_XBGR32,
surf->format);
break;
}
}
if (self->pixelformat == MFVideoFormat_YUY2.Data1) {
switch (self->color_out) {
case YUV_OUT:
yuyv_to_yuv(data, surf->pixels, size, surf->format);
break;
case RGB_OUT:
yuyv_to_rgb(data, surf->pixels, size, surf->format);
break;
case HSV_OUT:
yuyv_to_rgb(data, surf->pixels, size, surf->format);
rgb_to_hsv(surf->pixels, surf->pixels, size, V4L2_PIX_FMT_YUYV,
surf->format);
break;
}
}
SDL_UnlockSurface(surf);
return 1;
}
int
windows_read_frame(pgCameraObject *self, SDL_Surface *surf)
{
HRESULT hr;
if (!self->open) {
PyErr_SetString(pgExc_SDLError,
"Camera needs to be started to read data");
return 0;
}
if (!_check_integrity(self)) {
return 0;
};
if (self->buf) {
BYTE *buf_data;
DWORD buf_max_length;
DWORD buf_length;
hr = self->buf->lpVtbl->Lock(self->buf, &buf_data, &buf_max_length,
&buf_length);
CHECKHR(hr);
if (!windows_process_image(self, buf_data, buf_length, surf)) {
return 0;
}
hr = self->buf->lpVtbl->Unlock(self->buf);
CHECKHR(hr);
self->buffer_ready = 0;
}
return 1;
}
int
windows_frame_ready(pgCameraObject *self, int *result)
{
*result = self->buffer_ready;
if (!self->open) {
PyErr_SetString(pgExc_SDLError,
"Camera needs to be started to read data");
return 0;
}
if (!_check_integrity(self)) {
return 0;
};
return 1;
}
PyObject *
windows_read_raw(pgCameraObject *self)
{
PyObject *data = NULL;
HRESULT hr;
if (!self->open) {
PyErr_SetString(pgExc_SDLError,
"Camera needs to be started to read data");
return 0;
}
if (!_check_integrity(self)) {
return 0;
};
if (self->raw_buf) {
BYTE *buf_data;
DWORD buf_max_length;
DWORD buf_length;
hr = self->raw_buf->lpVtbl->Lock(self->raw_buf, &buf_data,
&buf_max_length, &buf_length);
CHECKHR(hr);
data = PyBytes_FromStringAndSize(buf_data, buf_length);
if (!data) {
PyErr_SetString(pgExc_SDLError,
"Error constructing bytes from data");
return 0;
}
hr = self->raw_buf->lpVtbl->Unlock(self->raw_buf);
CHECKHR(hr);
self->buffer_ready = 0;
return data;
}
/* should this be an error instead? */
Py_RETURN_NONE;
}
#endif /* PYGAME_WINDOWS_CAMERA */