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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
|
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
// the Windows Media Foundation API
#ifdef SDL_CAMERA_DRIVER_MEDIAFOUNDATION
#define COBJMACROS
// this seems to be a bug in mfidl.h, just define this to avoid the problem section.
#define __IMFVideoProcessorControl3_INTERFACE_DEFINED__
#include "../../core/windows/SDL_windows.h"
#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#include "../SDL_syscamera.h"
#include "../SDL_camera_c.h"
static const IID SDL_IID_IMFMediaSource = { 0x279a808d, 0xaec7, 0x40c8, { 0x9c, 0x6b, 0xa6, 0xb4, 0x92, 0xc7, 0x8a, 0x66 } };
static const IID SDL_IID_IMF2DBuffer = { 0x7dc9d5f9, 0x9ed9, 0x44ec, { 0x9b, 0xbf, 0x06, 0x00, 0xbb, 0x58, 0x9f, 0xbb } };
static const IID SDL_IID_IMF2DBuffer2 = { 0x33ae5ea6, 0x4316, 0x436f, { 0x8d, 0xdd, 0xd7, 0x3d, 0x22, 0xf8, 0x29, 0xec } };
static const GUID SDL_MF_MT_DEFAULT_STRIDE = { 0x644b4e48, 0x1e02, 0x4516, { 0xb0, 0xeb, 0xc0, 0x1c, 0xa9, 0xd4, 0x9a, 0xc6 } };
static const GUID SDL_MF_MT_MAJOR_TYPE = { 0x48eba18e, 0xf8c9, 0x4687, { 0xbf, 0x11, 0x0a, 0x74, 0xc9, 0xf9, 0x6a, 0x8f } };
static const GUID SDL_MF_MT_SUBTYPE = { 0xf7e34c9a, 0x42e8, 0x4714, { 0xb7, 0x4b, 0xcb, 0x29, 0xd7, 0x2c, 0x35, 0xe5 } };
static const GUID SDL_MF_MT_VIDEO_NOMINAL_RANGE = { 0xc21b8ee5, 0xb956, 0x4071, { 0x8d, 0xaf, 0x32, 0x5e, 0xdf, 0x5c, 0xab, 0x11 } };
static const GUID SDL_MF_MT_VIDEO_PRIMARIES = { 0xdbfbe4d7, 0x0740, 0x4ee0, { 0x81, 0x92, 0x85, 0x0a, 0xb0, 0xe2, 0x19, 0x35 } };
static const GUID SDL_MF_MT_TRANSFER_FUNCTION = { 0x5fb0fce9, 0xbe5c, 0x4935, { 0xa8, 0x11, 0xec, 0x83, 0x8f, 0x8e, 0xed, 0x93 } };
static const GUID SDL_MF_MT_YUV_MATRIX = { 0x3e23d450, 0x2c75, 0x4d25, { 0xa0, 0x0e, 0xb9, 0x16, 0x70, 0xd1, 0x23, 0x27 } };
static const GUID SDL_MF_MT_VIDEO_CHROMA_SITING = { 0x65df2370, 0xc773, 0x4c33, { 0xaa, 0x64, 0x84, 0x3e, 0x06, 0x8e, 0xfb, 0x0c } };
static const GUID SDL_MF_MT_FRAME_SIZE = { 0x1652c33d, 0xd6b2, 0x4012, { 0xb8, 0x34, 0x72, 0x03, 0x08, 0x49, 0xa3, 0x7d } };
static const GUID SDL_MF_MT_FRAME_RATE = { 0xc459a2e8, 0x3d2c, 0x4e44, { 0xb1, 0x32, 0xfe, 0xe5, 0x15, 0x6c, 0x7b, 0xb0 } };
static const GUID SDL_MFMediaType_Video = { 0x73646976, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 } };
static const IID SDL_MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME = { 0x60d0e559, 0x52f8, 0x4fa2, { 0xbb, 0xce, 0xac, 0xdb, 0x34, 0xa8, 0xec, 0x1 } };
static const IID SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE = { 0xc60ac5fe, 0x252a, 0x478f, { 0xa0, 0xef, 0xbc, 0x8f, 0xa5, 0xf7, 0xca, 0xd3 } };
static const IID SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK = { 0x58f0aad8, 0x22bf, 0x4f8a, { 0xbb, 0x3d, 0xd2, 0xc4, 0x97, 0x8c, 0x6e, 0x2f } };
static const IID SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID = { 0x8ac3587a, 0x4ae7, 0x42d8, { 0x99, 0xe0, 0x0a, 0x60, 0x13, 0xee, 0xf9, 0x0f } };
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmultichar"
#endif
#define SDL_DEFINE_MEDIATYPE_GUID(name, fmt) static const GUID SDL_##name = { fmt, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } }
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_RGB555, 24);
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_RGB565, 23);
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_RGB24, 20);
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_RGB32, 22);
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_ARGB32, 21);
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_A2R10G10B10, 31);
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_YV12, FCC('YV12'));
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_IYUV, FCC('IYUV'));
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_YUY2, FCC('YUY2'));
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_UYVY, FCC('UYVY'));
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_YVYU, FCC('YVYU'));
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_NV12, FCC('NV12'));
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_NV21, FCC('NV21'));
SDL_DEFINE_MEDIATYPE_GUID(MFVideoFormat_MJPG, FCC('MJPG'));
#undef SDL_DEFINE_MEDIATYPE_GUID
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
static const struct
{
const GUID *guid;
SDL_PixelFormat format;
SDL_Colorspace colorspace;
} fmtmappings[] = {
// This is not every possible format, just popular ones that SDL can reasonably handle.
// (and we should probably trim this list more.)
{ &SDL_MFVideoFormat_RGB555, SDL_PIXELFORMAT_XRGB1555, SDL_COLORSPACE_SRGB },
{ &SDL_MFVideoFormat_RGB565, SDL_PIXELFORMAT_RGB565, SDL_COLORSPACE_SRGB },
{ &SDL_MFVideoFormat_RGB24, SDL_PIXELFORMAT_RGB24, SDL_COLORSPACE_SRGB },
{ &SDL_MFVideoFormat_RGB32, SDL_PIXELFORMAT_XRGB8888, SDL_COLORSPACE_SRGB },
{ &SDL_MFVideoFormat_ARGB32, SDL_PIXELFORMAT_ARGB8888, SDL_COLORSPACE_SRGB },
{ &SDL_MFVideoFormat_A2R10G10B10, SDL_PIXELFORMAT_ARGB2101010, SDL_COLORSPACE_SRGB },
{ &SDL_MFVideoFormat_YV12, SDL_PIXELFORMAT_YV12, SDL_COLORSPACE_BT709_LIMITED },
{ &SDL_MFVideoFormat_IYUV, SDL_PIXELFORMAT_IYUV, SDL_COLORSPACE_BT709_LIMITED },
{ &SDL_MFVideoFormat_YUY2, SDL_PIXELFORMAT_YUY2, SDL_COLORSPACE_BT709_LIMITED },
{ &SDL_MFVideoFormat_UYVY, SDL_PIXELFORMAT_UYVY, SDL_COLORSPACE_BT709_LIMITED },
{ &SDL_MFVideoFormat_YVYU, SDL_PIXELFORMAT_YVYU, SDL_COLORSPACE_BT709_LIMITED },
{ &SDL_MFVideoFormat_NV12, SDL_PIXELFORMAT_NV12, SDL_COLORSPACE_BT709_LIMITED },
{ &SDL_MFVideoFormat_NV21, SDL_PIXELFORMAT_NV21, SDL_COLORSPACE_BT709_LIMITED },
{ &SDL_MFVideoFormat_MJPG, SDL_PIXELFORMAT_MJPG, SDL_COLORSPACE_SRGB }
};
static SDL_Colorspace GetMediaTypeColorspace(IMFMediaType *mediatype, SDL_Colorspace default_colorspace)
{
SDL_Colorspace colorspace = default_colorspace;
if (SDL_COLORSPACETYPE(colorspace) == SDL_COLOR_TYPE_YCBCR) {
HRESULT ret;
UINT32 range = 0, primaries = 0, transfer = 0, matrix = 0, chroma = 0;
ret = IMFMediaType_GetUINT32(mediatype, &SDL_MF_MT_VIDEO_NOMINAL_RANGE, &range);
if (SUCCEEDED(ret)) {
switch (range) {
case MFNominalRange_0_255:
range = SDL_COLOR_RANGE_FULL;
break;
case MFNominalRange_16_235:
range = SDL_COLOR_RANGE_LIMITED;
break;
default:
range = (UINT32)SDL_COLORSPACERANGE(default_colorspace);
break;
}
} else {
range = (UINT32)SDL_COLORSPACERANGE(default_colorspace);
}
ret = IMFMediaType_GetUINT32(mediatype, &SDL_MF_MT_VIDEO_PRIMARIES, &primaries);
if (SUCCEEDED(ret)) {
switch (primaries) {
case MFVideoPrimaries_BT709:
primaries = SDL_COLOR_PRIMARIES_BT709;
break;
case MFVideoPrimaries_BT470_2_SysM:
primaries = SDL_COLOR_PRIMARIES_BT470M;
break;
case MFVideoPrimaries_BT470_2_SysBG:
primaries = SDL_COLOR_PRIMARIES_BT470BG;
break;
case MFVideoPrimaries_SMPTE170M:
primaries = SDL_COLOR_PRIMARIES_BT601;
break;
case MFVideoPrimaries_SMPTE240M:
primaries = SDL_COLOR_PRIMARIES_SMPTE240;
break;
case MFVideoPrimaries_EBU3213:
primaries = SDL_COLOR_PRIMARIES_EBU3213;
break;
case MFVideoPrimaries_BT2020:
primaries = SDL_COLOR_PRIMARIES_BT2020;
break;
case MFVideoPrimaries_XYZ:
primaries = SDL_COLOR_PRIMARIES_XYZ;
break;
case MFVideoPrimaries_DCI_P3:
primaries = SDL_COLOR_PRIMARIES_SMPTE432;
break;
default:
primaries = (UINT32)SDL_COLORSPACEPRIMARIES(default_colorspace);
break;
}
} else {
primaries = (UINT32)SDL_COLORSPACEPRIMARIES(default_colorspace);
}
ret = IMFMediaType_GetUINT32(mediatype, &SDL_MF_MT_TRANSFER_FUNCTION, &transfer);
if (SUCCEEDED(ret)) {
switch (transfer) {
case MFVideoTransFunc_10:
transfer = SDL_TRANSFER_CHARACTERISTICS_LINEAR;
break;
case MFVideoTransFunc_22:
transfer = SDL_TRANSFER_CHARACTERISTICS_GAMMA22;
break;
case MFVideoTransFunc_709:
transfer = SDL_TRANSFER_CHARACTERISTICS_BT709;
break;
case MFVideoTransFunc_240M:
transfer = SDL_TRANSFER_CHARACTERISTICS_SMPTE240;
break;
case MFVideoTransFunc_sRGB:
transfer = SDL_TRANSFER_CHARACTERISTICS_SRGB;
break;
case MFVideoTransFunc_28:
transfer = SDL_TRANSFER_CHARACTERISTICS_GAMMA28;
break;
case MFVideoTransFunc_Log_100:
transfer = SDL_TRANSFER_CHARACTERISTICS_LOG100;
break;
case MFVideoTransFunc_2084:
transfer = SDL_TRANSFER_CHARACTERISTICS_PQ;
break;
case MFVideoTransFunc_HLG:
transfer = SDL_TRANSFER_CHARACTERISTICS_HLG;
break;
case 18 /* MFVideoTransFunc_BT1361_ECG */:
transfer = SDL_TRANSFER_CHARACTERISTICS_BT1361;
break;
case 19 /* MFVideoTransFunc_SMPTE428 */:
transfer = SDL_TRANSFER_CHARACTERISTICS_SMPTE428;
break;
default:
transfer = (UINT32)SDL_COLORSPACETRANSFER(default_colorspace);
break;
}
} else {
transfer = (UINT32)SDL_COLORSPACETRANSFER(default_colorspace);
}
ret = IMFMediaType_GetUINT32(mediatype, &SDL_MF_MT_YUV_MATRIX, &matrix);
if (SUCCEEDED(ret)) {
switch (matrix) {
case MFVideoTransferMatrix_BT709:
matrix = SDL_MATRIX_COEFFICIENTS_BT709;
break;
case MFVideoTransferMatrix_BT601:
matrix = SDL_MATRIX_COEFFICIENTS_BT601;
break;
case MFVideoTransferMatrix_SMPTE240M:
matrix = SDL_MATRIX_COEFFICIENTS_SMPTE240;
break;
case MFVideoTransferMatrix_BT2020_10:
matrix = SDL_MATRIX_COEFFICIENTS_BT2020_NCL;
break;
case 6 /* MFVideoTransferMatrix_Identity */:
matrix = SDL_MATRIX_COEFFICIENTS_IDENTITY;
break;
case 7 /* MFVideoTransferMatrix_FCC47 */:
matrix = SDL_MATRIX_COEFFICIENTS_FCC;
break;
case 8 /* MFVideoTransferMatrix_YCgCo */:
matrix = SDL_MATRIX_COEFFICIENTS_YCGCO;
break;
case 9 /* MFVideoTransferMatrix_SMPTE2085 */:
matrix = SDL_MATRIX_COEFFICIENTS_SMPTE2085;
break;
case 10 /* MFVideoTransferMatrix_Chroma */:
matrix = SDL_MATRIX_COEFFICIENTS_CHROMA_DERIVED_NCL;
break;
case 11 /* MFVideoTransferMatrix_Chroma_const */:
matrix = SDL_MATRIX_COEFFICIENTS_CHROMA_DERIVED_CL;
break;
case 12 /* MFVideoTransferMatrix_ICtCp */:
matrix = SDL_MATRIX_COEFFICIENTS_ICTCP;
break;
default:
matrix = (UINT32)SDL_COLORSPACEMATRIX(default_colorspace);
break;
}
} else {
matrix = (UINT32)SDL_COLORSPACEMATRIX(default_colorspace);
}
ret = IMFMediaType_GetUINT32(mediatype, &SDL_MF_MT_VIDEO_CHROMA_SITING, &chroma);
if (SUCCEEDED(ret)) {
switch (chroma) {
case MFVideoChromaSubsampling_MPEG2:
chroma = SDL_CHROMA_LOCATION_LEFT;
break;
case MFVideoChromaSubsampling_MPEG1:
chroma = SDL_CHROMA_LOCATION_CENTER;
break;
case MFVideoChromaSubsampling_DV_PAL:
chroma = SDL_CHROMA_LOCATION_TOPLEFT;
break;
default:
chroma = (UINT32)SDL_COLORSPACECHROMA(default_colorspace);
break;
}
} else {
chroma = (UINT32)SDL_COLORSPACECHROMA(default_colorspace);
}
colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR, range, primaries, transfer, matrix, chroma);
}
return colorspace;
}
static void MediaTypeToSDLFmt(IMFMediaType *mediatype, SDL_PixelFormat *format, SDL_Colorspace *colorspace)
{
HRESULT ret;
GUID type;
ret = IMFMediaType_GetGUID(mediatype, &SDL_MF_MT_SUBTYPE, &type);
if (SUCCEEDED(ret)) {
for (size_t i = 0; i < SDL_arraysize(fmtmappings); i++) {
if (WIN_IsEqualGUID(&type, fmtmappings[i].guid)) {
*format = fmtmappings[i].format;
*colorspace = GetMediaTypeColorspace(mediatype, fmtmappings[i].colorspace);
return;
}
}
}
#if DEBUG_CAMERA
SDL_Log("Unknown media type: 0x%x (%c%c%c%c)", type.Data1,
(char)(Uint8)(type.Data1 >> 0),
(char)(Uint8)(type.Data1 >> 8),
(char)(Uint8)(type.Data1 >> 16),
(char)(Uint8)(type.Data1 >> 24));
#endif
*format = SDL_PIXELFORMAT_UNKNOWN;
*colorspace = SDL_COLORSPACE_UNKNOWN;
}
static const GUID *SDLFmtToMFVidFmtGuid(SDL_PixelFormat format)
{
for (size_t i = 0; i < SDL_arraysize(fmtmappings); i++) {
if (fmtmappings[i].format == format) {
return fmtmappings[i].guid;
}
}
return NULL;
}
// handle to Media Foundation libs--Vista and later!--for access to the Media Foundation API.
// mf.dll ...
static HMODULE libmf = NULL;
typedef HRESULT(WINAPI *pfnMFEnumDeviceSources)(IMFAttributes *,IMFActivate ***,UINT32 *);
typedef HRESULT(WINAPI *pfnMFCreateDeviceSource)(IMFAttributes *, IMFMediaSource **);
static pfnMFEnumDeviceSources pMFEnumDeviceSources = NULL;
static pfnMFCreateDeviceSource pMFCreateDeviceSource = NULL;
// mfplat.dll ...
static HMODULE libmfplat = NULL;
typedef HRESULT(WINAPI *pfnMFStartup)(ULONG, DWORD);
typedef HRESULT(WINAPI *pfnMFShutdown)(void);
typedef HRESULT(WINAPI *pfnMFCreateAttributes)(IMFAttributes **, UINT32);
typedef HRESULT(WINAPI *pfnMFCreateMediaType)(IMFMediaType **);
typedef HRESULT(WINAPI *pfnMFGetStrideForBitmapInfoHeader)(DWORD, DWORD, LONG *);
static pfnMFStartup pMFStartup = NULL;
static pfnMFShutdown pMFShutdown = NULL;
static pfnMFCreateAttributes pMFCreateAttributes = NULL;
static pfnMFCreateMediaType pMFCreateMediaType = NULL;
static pfnMFGetStrideForBitmapInfoHeader pMFGetStrideForBitmapInfoHeader = NULL;
// mfreadwrite.dll ...
static HMODULE libmfreadwrite = NULL;
typedef HRESULT(WINAPI *pfnMFCreateSourceReaderFromMediaSource)(IMFMediaSource *, IMFAttributes *, IMFSourceReader **);
static pfnMFCreateSourceReaderFromMediaSource pMFCreateSourceReaderFromMediaSource = NULL;
typedef struct SDL_PrivateCameraData
{
IMFSourceReader *srcreader;
IMFSample *current_sample;
int pitch;
} SDL_PrivateCameraData;
static bool MEDIAFOUNDATION_WaitDevice(SDL_Camera *device)
{
SDL_assert(device->hidden->current_sample == NULL);
IMFSourceReader *srcreader = device->hidden->srcreader;
IMFSample *sample = NULL;
while (!SDL_GetAtomicInt(&device->shutdown)) {
DWORD stream_flags = 0;
const HRESULT ret = IMFSourceReader_ReadSample(srcreader, (DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, NULL, &stream_flags, NULL, &sample);
if (FAILED(ret)) {
return false; // ruh roh.
}
// we currently ignore stream_flags format changes, but my _hope_ is that IMFSourceReader is handling this and
// will continue to give us the explicitly-specified format we requested when opening the device, though, and
// we don't have to manually deal with it.
if (sample != NULL) {
break;
} else if (stream_flags & (MF_SOURCE_READERF_ERROR | MF_SOURCE_READERF_ENDOFSTREAM)) {
return false; // apparently this camera has gone down. :/
}
// otherwise, there was some minor burp, probably; just try again.
}
device->hidden->current_sample = sample;
return true;
}
#ifdef KEEP_ACQUIRED_BUFFERS_LOCKED
#define PROP_SURFACE_IMFOBJS_POINTER "SDL.camera.mediafoundation.imfobjs"
typedef struct SDL_IMFObjects
{
IMF2DBuffer2 *buffer2d2;
IMF2DBuffer *buffer2d;
IMFMediaBuffer *buffer;
IMFSample *sample;
} SDL_IMFObjects;
static void SDLCALL CleanupIMF2DBuffer2(void *userdata, void *value)
{
SDL_IMFObjects *objs = (SDL_IMFObjects *)value;
IMF2DBuffer2_Unlock2D(objs->buffer2d2);
IMF2DBuffer2_Release(objs->buffer2d2);
IMFMediaBuffer_Release(objs->buffer);
IMFSample_Release(objs->sample);
SDL_free(objs);
}
static void SDLCALL CleanupIMF2DBuffer(void *userdata, void *value)
{
SDL_IMFObjects *objs = (SDL_IMFObjects *)value;
IMF2DBuffer_Unlock2D(objs->buffer2d);
IMF2DBuffer_Release(objs->buffer2d);
IMFMediaBuffer_Release(objs->buffer);
IMFSample_Release(objs->sample);
SDL_free(objs);
}
static void SDLCALL CleanupIMFMediaBuffer(void *userdata, void *value)
{
SDL_IMFObjects *objs = (SDL_IMFObjects *)value;
IMFMediaBuffer_Unlock(objs->buffer);
IMFMediaBuffer_Release(objs->buffer);
IMFSample_Release(objs->sample);
SDL_free(objs);
}
static SDL_CameraFrameResult MEDIAFOUNDATION_AcquireFrame(SDL_Camera *device, SDL_Surface *frame, Uint64 *timestampNS)
{
SDL_assert(device->hidden->current_sample != NULL);
SDL_CameraFrameResult result = SDL_CAMERA_FRAME_READY;
HRESULT ret;
LONGLONG timestamp100NS = 0;
SDL_IMFObjects *objs = (SDL_IMFObjects *) SDL_calloc(1, sizeof (SDL_IMFObjects));
if (objs == NULL) {
return SDL_CAMERA_FRAME_ERROR;
}
objs->sample = device->hidden->current_sample;
device->hidden->current_sample = NULL;
const SDL_PropertiesID surfprops = SDL_GetSurfaceProperties(frame);
if (!surfprops) {
result = SDL_CAMERA_FRAME_ERROR;
} else {
ret = IMFSample_GetSampleTime(objs->sample, ×tamp100NS);
if (FAILED(ret)) {
result = SDL_CAMERA_FRAME_ERROR;
}
*timestampNS = timestamp100NS * 100; // the timestamps are in 100-nanosecond increments; move to full nanoseconds.
}
ret = (result == SDL_CAMERA_FRAME_ERROR) ? E_FAIL : IMFSample_ConvertToContiguousBuffer(objs->sample, &objs->buffer); // IMFSample_GetBufferByIndex(objs->sample, 0, &objs->buffer);
if (FAILED(ret)) {
SDL_free(objs);
result = SDL_CAMERA_FRAME_ERROR;
} else {
BYTE *pixels = NULL;
LONG pitch = 0;
DWORD buflen = 0;
if (SUCCEEDED(IMFMediaBuffer_QueryInterface(objs->buffer, &SDL_IID_IMF2DBuffer2, (void **)&objs->buffer2d2))) {
BYTE *bufstart = NULL;
ret = IMF2DBuffer2_Lock2DSize(objs->buffer2d2, MF2DBuffer_LockFlags_Read, &pixels, &pitch, &bufstart, &buflen);
if (FAILED(ret)) {
result = SDL_CAMERA_FRAME_ERROR;
CleanupIMF2DBuffer2(NULL, objs);
} else {
if (frame->format == SDL_PIXELFORMAT_MJPG) {
pitch = (LONG)buflen;
}
if (pitch < 0) { // image rows are reversed.
pixels += -pitch * (frame->h - 1);
}
frame->pixels = pixels;
frame->pitch = (int)pitch;
if (!SDL_SetPointerPropertyWithCleanup(surfprops, PROP_SURFACE_IMFOBJS_POINTER, objs, CleanupIMF2DBuffer2, NULL)) {
result = SDL_CAMERA_FRAME_ERROR;
}
}
} else if (frame->format != SDL_PIXELFORMAT_MJPG &&
SUCCEEDED(IMFMediaBuffer_QueryInterface(objs->buffer, &SDL_IID_IMF2DBuffer, (void **)&objs->buffer2d))) {
ret = IMF2DBuffer_Lock2D(objs->buffer2d, &pixels, &pitch);
if (FAILED(ret)) {
CleanupIMF2DBuffer(NULL, objs);
result = SDL_CAMERA_FRAME_ERROR;
} else {
if (pitch < 0) { // image rows are reversed.
pixels += -pitch * (frame->h - 1);
}
frame->pixels = pixels;
frame->pitch = (int)pitch;
if (!SDL_SetPointerPropertyWithCleanup(surfprops, PROP_SURFACE_IMFOBJS_POINTER, objs, CleanupIMF2DBuffer, NULL)) {
result = SDL_CAMERA_FRAME_ERROR;
}
}
} else {
DWORD maxlen = 0;
ret = IMFMediaBuffer_Lock(objs->buffer, &pixels, &maxlen, &buflen);
if (FAILED(ret)) {
CleanupIMFMediaBuffer(NULL, objs);
result = SDL_CAMERA_FRAME_ERROR;
} else {
if (frame->format == SDL_PIXELFORMAT_MJPG) {
pitch = (LONG)buflen;
} else {
pitch = (LONG)device->hidden->pitch;
}
if (pitch < 0) { // image rows are reversed.
pixels += -pitch * (frame->h - 1);
}
frame->pixels = pixels;
frame->pitch = (int)pitch;
if (!SDL_SetPointerPropertyWithCleanup(surfprops, PROP_SURFACE_IMFOBJS_POINTER, objs, CleanupIMFMediaBuffer, NULL)) {
result = SDL_CAMERA_FRAME_ERROR;
}
}
}
}
if (result != SDL_CAMERA_FRAME_READY) {
*timestampNS = 0;
}
return result;
}
static void MEDIAFOUNDATION_ReleaseFrame(SDL_Camera *device, SDL_Surface *frame)
{
const SDL_PropertiesID surfprops = SDL_GetSurfaceProperties(frame);
if (surfprops) {
// this will release the IMFBuffer and IMFSample objects for this frame.
SDL_ClearProperty(surfprops, PROP_SURFACE_IMFOBJS_POINTER);
}
}
#else
static SDL_CameraFrameResult MEDIAFOUNDATION_CopyFrame(SDL_Surface *frame, const BYTE *pixels, LONG pitch, DWORD buflen)
{
frame->pixels = SDL_aligned_alloc(SDL_GetSIMDAlignment(), buflen);
if (!frame->pixels) {
return SDL_CAMERA_FRAME_ERROR;
}
const BYTE *start = pixels;
if (pitch < 0) { // image rows are reversed.
start += -pitch * (frame->h - 1);
}
SDL_memcpy(frame->pixels, start, buflen);
frame->pitch = (int)pitch;
return SDL_CAMERA_FRAME_READY;
}
static SDL_CameraFrameResult MEDIAFOUNDATION_AcquireFrame(SDL_Camera *device, SDL_Surface *frame, Uint64 *timestampNS)
{
SDL_assert(device->hidden->current_sample != NULL);
SDL_CameraFrameResult result = SDL_CAMERA_FRAME_READY;
HRESULT ret;
LONGLONG timestamp100NS = 0;
IMFSample *sample = device->hidden->current_sample;
device->hidden->current_sample = NULL;
const SDL_PropertiesID surfprops = SDL_GetSurfaceProperties(frame);
if (!surfprops) {
result = SDL_CAMERA_FRAME_ERROR;
} else {
ret = IMFSample_GetSampleTime(sample, ×tamp100NS);
if (FAILED(ret)) {
result = SDL_CAMERA_FRAME_ERROR;
}
*timestampNS = timestamp100NS * 100; // the timestamps are in 100-nanosecond increments; move to full nanoseconds.
}
IMFMediaBuffer *buffer = NULL;
ret = (result < 0) ? E_FAIL : IMFSample_ConvertToContiguousBuffer(sample, &buffer); // IMFSample_GetBufferByIndex(sample, 0, &buffer);
if (FAILED(ret)) {
result = SDL_CAMERA_FRAME_ERROR;
} else {
IMF2DBuffer *buffer2d = NULL;
IMF2DBuffer2 *buffer2d2 = NULL;
BYTE *pixels = NULL;
LONG pitch = 0;
DWORD buflen = 0;
if (SUCCEEDED(IMFMediaBuffer_QueryInterface(buffer, &SDL_IID_IMF2DBuffer2, (void **)&buffer2d2))) {
BYTE *bufstart = NULL;
ret = IMF2DBuffer2_Lock2DSize(buffer2d2, MF2DBuffer_LockFlags_Read, &pixels, &pitch, &bufstart, &buflen);
if (FAILED(ret)) {
result = SDL_CAMERA_FRAME_ERROR;
} else {
if (frame->format == SDL_PIXELFORMAT_MJPG) {
pitch = (LONG)buflen;
}
result = MEDIAFOUNDATION_CopyFrame(frame, pixels, pitch, buflen);
IMF2DBuffer2_Unlock2D(buffer2d2);
}
IMF2DBuffer2_Release(buffer2d2);
} else if (frame->format != SDL_PIXELFORMAT_MJPG &&
SUCCEEDED(IMFMediaBuffer_QueryInterface(buffer, &SDL_IID_IMF2DBuffer, (void **)&buffer2d))) {
ret = IMF2DBuffer_Lock2D(buffer2d, &pixels, &pitch);
if (FAILED(ret)) {
result = SDL_CAMERA_FRAME_ERROR;
} else {
buflen = SDL_abs((int)pitch) * frame->h;
result = MEDIAFOUNDATION_CopyFrame(frame, pixels, pitch, buflen);
IMF2DBuffer_Unlock2D(buffer2d);
}
IMF2DBuffer_Release(buffer2d);
} else {
DWORD maxlen = 0;
ret = IMFMediaBuffer_Lock(buffer, &pixels, &maxlen, &buflen);
if (FAILED(ret)) {
result = SDL_CAMERA_FRAME_ERROR;
} else {
if (frame->format == SDL_PIXELFORMAT_MJPG) {
pitch = (LONG)buflen;
} else {
pitch = (LONG)device->hidden->pitch;
}
result = MEDIAFOUNDATION_CopyFrame(frame, pixels, pitch, buflen);
IMFMediaBuffer_Unlock(buffer);
}
}
IMFMediaBuffer_Release(buffer);
}
IMFSample_Release(sample);
if (result != SDL_CAMERA_FRAME_READY) {
*timestampNS = 0;
}
return result;
}
static void MEDIAFOUNDATION_ReleaseFrame(SDL_Camera *device, SDL_Surface *frame)
{
SDL_aligned_free(frame->pixels);
}
#endif
static void MEDIAFOUNDATION_CloseDevice(SDL_Camera *device)
{
if (device && device->hidden) {
if (device->hidden->srcreader) {
IMFSourceReader_Release(device->hidden->srcreader);
}
if (device->hidden->current_sample) {
IMFSample_Release(device->hidden->current_sample);
}
SDL_free(device->hidden);
device->hidden = NULL;
}
}
// this function is from https://learn.microsoft.com/en-us/windows/win32/medfound/uncompressed-video-buffers
static HRESULT GetDefaultStride(IMFMediaType *pType, LONG *plStride)
{
LONG lStride = 0;
// Try to get the default stride from the media type.
HRESULT ret = IMFMediaType_GetUINT32(pType, &SDL_MF_MT_DEFAULT_STRIDE, (UINT32*)&lStride);
if (FAILED(ret)) {
// Attribute not set. Try to calculate the default stride.
GUID subtype = GUID_NULL;
UINT32 width = 0;
// UINT32 height = 0;
UINT64 val = 0;
// Get the subtype and the image size.
ret = IMFMediaType_GetGUID(pType, &SDL_MF_MT_SUBTYPE, &subtype);
if (FAILED(ret)) {
goto done;
}
ret = IMFMediaType_GetUINT64(pType, &SDL_MF_MT_FRAME_SIZE, &val);
if (FAILED(ret)) {
goto done;
}
width = (UINT32) (val >> 32);
// height = (UINT32) val;
ret = pMFGetStrideForBitmapInfoHeader(subtype.Data1, width, &lStride);
if (FAILED(ret)) {
goto done;
}
// Set the attribute for later reference.
IMFMediaType_SetUINT32(pType, &SDL_MF_MT_DEFAULT_STRIDE, (UINT32) lStride);
}
if (SUCCEEDED(ret)) {
*plStride = lStride;
}
done:
return ret;
}
static bool MEDIAFOUNDATION_OpenDevice(SDL_Camera *device, const SDL_CameraSpec *spec)
{
const char *utf8symlink = (const char *) device->handle;
IMFAttributes *attrs = NULL;
LPWSTR wstrsymlink = NULL;
IMFMediaSource *source = NULL;
IMFMediaType *mediatype = NULL;
IMFSourceReader *srcreader = NULL;
#if 0
DWORD num_streams = 0;
#endif
LONG lstride = 0;
//PROPVARIANT var;
HRESULT ret;
#if 0
IMFStreamDescriptor *streamdesc = NULL;
IMFPresentationDescriptor *presentdesc = NULL;
IMFMediaTypeHandler *handler = NULL;
#endif
#if DEBUG_CAMERA
SDL_Log("CAMERA: opening device with symlink of '%s'", utf8symlink);
#endif
wstrsymlink = WIN_UTF8ToString(utf8symlink);
if (!wstrsymlink) {
goto failed;
}
#define CHECK_HRESULT(what, r) if (FAILED(r)) { WIN_SetErrorFromHRESULT(what " failed", r); goto failed; }
ret = pMFCreateAttributes(&attrs, 1);
CHECK_HRESULT("MFCreateAttributes", ret);
ret = IMFAttributes_SetGUID(attrs, &SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, &SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
CHECK_HRESULT("IMFAttributes_SetGUID(srctype)", ret);
ret = IMFAttributes_SetString(attrs, &SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, wstrsymlink);
CHECK_HRESULT("IMFAttributes_SetString(symlink)", ret);
ret = pMFCreateDeviceSource(attrs, &source);
CHECK_HRESULT("MFCreateDeviceSource", ret);
IMFAttributes_Release(attrs);
SDL_free(wstrsymlink);
attrs = NULL;
wstrsymlink = NULL;
// !!! FIXME: I think it'd be nice to do this without an IMFSourceReader,
// since it's just utility code that has to handle more complex media streams
// than we're dealing with, but this will do for now. The docs are slightly
// insistent that you should use one, though...Maybe it's extremely hard
// to handle directly at the IMFMediaSource layer...?
ret = pMFCreateSourceReaderFromMediaSource(source, NULL, &srcreader);
CHECK_HRESULT("MFCreateSourceReaderFromMediaSource", ret);
// !!! FIXME: do we actually have to find the media type object in the source reader or can we just roll our own like this?
ret = pMFCreateMediaType(&mediatype);
CHECK_HRESULT("MFCreateMediaType", ret);
ret = IMFMediaType_SetGUID(mediatype, &SDL_MF_MT_MAJOR_TYPE, &SDL_MFMediaType_Video);
CHECK_HRESULT("IMFMediaType_SetGUID(major_type)", ret);
ret = IMFMediaType_SetGUID(mediatype, &SDL_MF_MT_SUBTYPE, SDLFmtToMFVidFmtGuid(spec->format));
CHECK_HRESULT("IMFMediaType_SetGUID(subtype)", ret);
ret = IMFMediaType_SetUINT64(mediatype, &SDL_MF_MT_FRAME_SIZE, (((UINT64)spec->width) << 32) | ((UINT64)spec->height));
CHECK_HRESULT("MFSetAttributeSize(frame_size)", ret);
ret = IMFMediaType_SetUINT64(mediatype, &SDL_MF_MT_FRAME_RATE, (((UINT64)spec->framerate_numerator) << 32) | ((UINT64)spec->framerate_denominator));
CHECK_HRESULT("MFSetAttributeRatio(frame_rate)", ret);
ret = IMFSourceReader_SetCurrentMediaType(srcreader, (DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, mediatype);
CHECK_HRESULT("IMFSourceReader_SetCurrentMediaType", ret);
#if 0 // this (untested thing) is what we would do to get started with a IMFMediaSource that _doesn't_ use IMFSourceReader...
ret = IMFMediaSource_CreatePresentationDescriptor(source, &presentdesc);
CHECK_HRESULT("IMFMediaSource_CreatePresentationDescriptor", ret);
ret = IMFPresentationDescriptor_GetStreamDescriptorCount(presentdesc, &num_streams);
CHECK_HRESULT("IMFPresentationDescriptor_GetStreamDescriptorCount", ret);
for (DWORD i = 0; i < num_streams; i++) {
BOOL selected = FALSE;
ret = IMFPresentationDescriptor_GetStreamDescriptorByIndex(presentdesc, i, &selected, &streamdesc);
CHECK_HRESULT("IMFPresentationDescriptor_GetStreamDescriptorByIndex", ret);
if (selected) {
ret = IMFStreamDescriptor_GetMediaTypeHandler(streamdesc, &handler);
CHECK_HRESULT("IMFStreamDescriptor_GetMediaTypeHandler", ret);
IMFMediaTypeHandler_SetCurrentMediaType(handler, mediatype);
IMFMediaTypeHandler_Release(handler);
handler = NULL;
}
IMFStreamDescriptor_Release(streamdesc);
streamdesc = NULL;
}
PropVariantInit(&var);
var.vt = VT_EMPTY;
ret = IMFMediaSource_Start(source, presentdesc, NULL, &var);
PropVariantClear(&var);
CHECK_HRESULT("IMFMediaSource_Start", ret);
IMFPresentationDescriptor_Release(presentdesc);
presentdesc = NULL;
#endif
ret = GetDefaultStride(mediatype, &lstride);
CHECK_HRESULT("GetDefaultStride", ret);
IMFMediaType_Release(mediatype);
mediatype = NULL;
device->hidden = (SDL_PrivateCameraData *) SDL_calloc(1, sizeof (SDL_PrivateCameraData));
if (!device->hidden) {
goto failed;
}
device->hidden->pitch = (int) lstride;
device->hidden->srcreader = srcreader;
IMFMediaSource_Release(source); // srcreader is holding a reference to this.
// There is no user permission prompt for camera access (I think?)
SDL_CameraPermissionOutcome(device, true);
#undef CHECK_HRESULT
return true;
failed:
if (srcreader) {
IMFSourceReader_Release(srcreader);
}
#if 0
if (handler) {
IMFMediaTypeHandler_Release(handler);
}
if (streamdesc) {
IMFStreamDescriptor_Release(streamdesc);
}
if (presentdesc) {
IMFPresentationDescriptor_Release(presentdesc);
}
#endif
if (source) {
IMFMediaSource_Shutdown(source);
IMFMediaSource_Release(source);
}
if (mediatype) {
IMFMediaType_Release(mediatype);
}
if (attrs) {
IMFAttributes_Release(attrs);
}
SDL_free(wstrsymlink);
return false;
}
static void MEDIAFOUNDATION_FreeDeviceHandle(SDL_Camera *device)
{
if (device) {
SDL_free(device->handle); // the device's symlink string.
}
}
static char *QueryActivationObjectString(IMFActivate *activation, const GUID *pguid)
{
LPWSTR wstr = NULL;
UINT32 wlen = 0;
HRESULT ret = IMFActivate_GetAllocatedString(activation, pguid, &wstr, &wlen);
if (FAILED(ret)) {
return NULL;
}
char *utf8str = WIN_StringToUTF8(wstr);
CoTaskMemFree(wstr);
return utf8str;
}
static void GatherCameraSpecs(IMFMediaSource *source, CameraFormatAddData *add_data)
{
HRESULT ret;
// this has like a thousand steps. :/
SDL_zerop(add_data);
IMFPresentationDescriptor *presentdesc = NULL;
ret = IMFMediaSource_CreatePresentationDescriptor(source, &presentdesc);
if (FAILED(ret) || !presentdesc) {
return;
}
DWORD num_streams = 0;
ret = IMFPresentationDescriptor_GetStreamDescriptorCount(presentdesc, &num_streams);
if (FAILED(ret)) {
num_streams = 0;
}
for (DWORD i = 0; i < num_streams; i++) {
IMFStreamDescriptor *streamdesc = NULL;
BOOL selected = FALSE;
ret = IMFPresentationDescriptor_GetStreamDescriptorByIndex(presentdesc, i, &selected, &streamdesc);
if (FAILED(ret) || !streamdesc) {
continue;
}
if (selected) {
IMFMediaTypeHandler *handler = NULL;
ret = IMFStreamDescriptor_GetMediaTypeHandler(streamdesc, &handler);
if (SUCCEEDED(ret) && handler) {
DWORD num_mediatype = 0;
ret = IMFMediaTypeHandler_GetMediaTypeCount(handler, &num_mediatype);
if (FAILED(ret)) {
num_mediatype = 0;
}
for (DWORD j = 0; j < num_mediatype; j++) {
IMFMediaType *mediatype = NULL;
ret = IMFMediaTypeHandler_GetMediaTypeByIndex(handler, j, &mediatype);
if (SUCCEEDED(ret) && mediatype) {
GUID type;
ret = IMFMediaType_GetGUID(mediatype, &SDL_MF_MT_MAJOR_TYPE, &type);
if (SUCCEEDED(ret) && WIN_IsEqualGUID(&type, &SDL_MFMediaType_Video)) {
SDL_PixelFormat sdlfmt = SDL_PIXELFORMAT_UNKNOWN;
SDL_Colorspace colorspace = SDL_COLORSPACE_UNKNOWN;
MediaTypeToSDLFmt(mediatype, &sdlfmt, &colorspace);
if (sdlfmt != SDL_PIXELFORMAT_UNKNOWN) {
UINT64 val = 0;
UINT32 w = 0, h = 0;
ret = IMFMediaType_GetUINT64(mediatype, &SDL_MF_MT_FRAME_SIZE, &val);
w = (UINT32)(val >> 32);
h = (UINT32)val;
if (SUCCEEDED(ret) && w && h) {
UINT32 framerate_numerator = 0, framerate_denominator = 0;
ret = IMFMediaType_GetUINT64(mediatype, &SDL_MF_MT_FRAME_RATE, &val);
framerate_numerator = (UINT32)(val >> 32);
framerate_denominator = (UINT32)val;
if (SUCCEEDED(ret) && framerate_numerator && framerate_denominator) {
SDL_AddCameraFormat(add_data, sdlfmt, colorspace, (int) w, (int) h, (int)framerate_numerator, (int)framerate_denominator);
}
}
}
}
IMFMediaType_Release(mediatype);
}
}
IMFMediaTypeHandler_Release(handler);
}
}
IMFStreamDescriptor_Release(streamdesc);
}
IMFPresentationDescriptor_Release(presentdesc);
}
static bool FindMediaFoundationCameraBySymlink(SDL_Camera *device, void *userdata)
{
return (SDL_strcmp((const char *) device->handle, (const char *) userdata) == 0);
}
static void MaybeAddDevice(IMFActivate *activation)
{
char *symlink = QueryActivationObjectString(activation, &SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK);
if (SDL_FindPhysicalCameraByCallback(FindMediaFoundationCameraBySymlink, symlink)) {
SDL_free(symlink);
return; // already have this one.
}
char *name = QueryActivationObjectString(activation, &SDL_MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME);
if (name && symlink) {
IMFMediaSource *source = NULL;
// "activating" here only creates an object, it doesn't open the actual camera hardware or start recording.
HRESULT ret = IMFActivate_ActivateObject(activation, &SDL_IID_IMFMediaSource, (void**)&source);
if (SUCCEEDED(ret) && source) {
CameraFormatAddData add_data;
GatherCameraSpecs(source, &add_data);
if (add_data.num_specs > 0) {
SDL_AddCamera(name, SDL_CAMERA_POSITION_UNKNOWN, add_data.num_specs, add_data.specs, symlink);
}
SDL_free(add_data.specs);
IMFActivate_ShutdownObject(activation);
IMFMediaSource_Release(source);
}
}
SDL_free(name);
}
static void MEDIAFOUNDATION_DetectDevices(void)
{
// !!! FIXME: use CM_Register_Notification (Win8+) to get device notifications.
// !!! FIXME: Earlier versions can use RegisterDeviceNotification, but I'm not bothering: no hotplug for you!
HRESULT ret;
IMFAttributes *attrs = NULL;
ret = pMFCreateAttributes(&attrs, 1);
if (FAILED(ret)) {
return; // oh well, no cameras for you.
}
ret = IMFAttributes_SetGUID(attrs, &SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, &SDL_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
if (FAILED(ret)) {
IMFAttributes_Release(attrs);
return; // oh well, no cameras for you.
}
IMFActivate **activations = NULL;
UINT32 total = 0;
ret = pMFEnumDeviceSources(attrs, &activations, &total);
IMFAttributes_Release(attrs);
if (FAILED(ret)) {
return; // oh well, no cameras for you.
}
for (UINT32 i = 0; i < total; i++) {
MaybeAddDevice(activations[i]);
IMFActivate_Release(activations[i]);
}
CoTaskMemFree(activations);
}
static void MEDIAFOUNDATION_Deinitialize(void)
{
pMFShutdown();
FreeLibrary(libmfreadwrite);
libmfreadwrite = NULL;
FreeLibrary(libmfplat);
libmfplat = NULL;
FreeLibrary(libmf);
libmf = NULL;
pMFEnumDeviceSources = NULL;
pMFCreateDeviceSource = NULL;
pMFStartup = NULL;
pMFShutdown = NULL;
pMFCreateAttributes = NULL;
pMFCreateMediaType = NULL;
pMFCreateSourceReaderFromMediaSource = NULL;
pMFGetStrideForBitmapInfoHeader = NULL;
}
static bool MEDIAFOUNDATION_Init(SDL_CameraDriverImpl *impl)
{
// !!! FIXME: slide this off into a subroutine
HMODULE mf = LoadLibrary(TEXT("Mf.dll")); // this library is available in Vista and later, but also can be on XP with service packs and Windows
if (!mf) {
return false;
}
HMODULE mfplat = LoadLibrary(TEXT("Mfplat.dll")); // this library is available in Vista and later. No WinXP, so have to LoadLibrary to use it for now!
if (!mfplat) {
FreeLibrary(mf);
return false;
}
HMODULE mfreadwrite = LoadLibrary(TEXT("Mfreadwrite.dll")); // this library is available in Vista and later. No WinXP, so have to LoadLibrary to use it for now!
if (!mfreadwrite) {
FreeLibrary(mfplat);
FreeLibrary(mf);
return false;
}
bool okay = true;
#define LOADSYM(lib, fn) if (okay) { p##fn = (pfn##fn) GetProcAddress(lib, #fn); if (!p##fn) { okay = false; } }
LOADSYM(mf, MFEnumDeviceSources);
LOADSYM(mf, MFCreateDeviceSource);
LOADSYM(mfplat, MFStartup);
LOADSYM(mfplat, MFShutdown);
LOADSYM(mfplat, MFCreateAttributes);
LOADSYM(mfplat, MFCreateMediaType);
LOADSYM(mfplat, MFGetStrideForBitmapInfoHeader);
LOADSYM(mfreadwrite, MFCreateSourceReaderFromMediaSource);
#undef LOADSYM
if (okay) {
const HRESULT ret = pMFStartup(MF_VERSION, MFSTARTUP_LITE);
if (FAILED(ret)) {
okay = false;
}
}
if (!okay) {
FreeLibrary(mfreadwrite);
FreeLibrary(mfplat);
FreeLibrary(mf);
return false;
}
libmf = mf;
libmfplat = mfplat;
libmfreadwrite = mfreadwrite;
impl->DetectDevices = MEDIAFOUNDATION_DetectDevices;
impl->OpenDevice = MEDIAFOUNDATION_OpenDevice;
impl->CloseDevice = MEDIAFOUNDATION_CloseDevice;
impl->WaitDevice = MEDIAFOUNDATION_WaitDevice;
impl->AcquireFrame = MEDIAFOUNDATION_AcquireFrame;
impl->ReleaseFrame = MEDIAFOUNDATION_ReleaseFrame;
impl->FreeDeviceHandle = MEDIAFOUNDATION_FreeDeviceHandle;
impl->Deinitialize = MEDIAFOUNDATION_Deinitialize;
return true;
}
CameraBootStrap MEDIAFOUNDATION_bootstrap = {
"mediafoundation", "SDL Windows Media Foundation camera driver", MEDIAFOUNDATION_Init, false
};
#endif // SDL_CAMERA_DRIVER_MEDIAFOUNDATION
|