Skip to content

Commit c314178

Browse files
committed
highgui/osx: backport AVFoundation support
1 parent 3648c61 commit c314178

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

CMakeLists.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ endif()
137137
# Optional 3rd party components
138138
# ===================================================
139139
OCV_OPTION(WITH_1394 "Include IEEE1394 support" ON IF (NOT ANDROID AND NOT IOS) )
140-
OCV_OPTION(WITH_AVFOUNDATION "Use AVFoundation for Video I/O" ON IF IOS)
140+
OCV_OPTION(WITH_AVFOUNDATION "Use AVFoundation for Video I/O (iOS/Mac)" ON IF APPLE)
141141
OCV_OPTION(WITH_CARBON "Use Carbon for UI instead of Cocoa" OFF IF APPLE )
142142
OCV_OPTION(WITH_CUDA "Include NVidia Cuda Runtime support" ON IF (CMAKE_VERSION VERSION_GREATER "2.8" AND NOT IOS) )
143143
OCV_OPTION(WITH_VTK "Include VTK library support (and build opencv_viz module eiher)" OFF IF (NOT ANDROID AND NOT IOS AND NOT CMAKE_CROSSCOMPILING) )
@@ -162,7 +162,8 @@ OCV_OPTION(WITH_PVAPI "Include Prosilica GigE support" ON
162162
OCV_OPTION(WITH_GIGEAPI "Include Smartek GigE support" ON IF (NOT ANDROID AND NOT IOS) )
163163
OCV_OPTION(WITH_QT "Build with Qt Backend support" OFF IF (NOT ANDROID AND NOT IOS) )
164164
OCV_OPTION(WITH_WIN32UI "Build with Win32 UI Backend support" ON IF WIN32 )
165-
OCV_OPTION(WITH_QUICKTIME "Use QuickTime for Video I/O insted of QTKit" OFF IF APPLE )
165+
OCV_OPTION(WITH_QUICKTIME "Use QuickTime for Video I/O" OFF IF APPLE )
166+
OCV_OPTION(WITH_QTKIT "Use QTKit Video I/O backend" OFF IF APPLE )
166167
OCV_OPTION(WITH_TBB "Include Intel TBB support" OFF IF (NOT IOS) )
167168
OCV_OPTION(WITH_OPENMP "Include OpenMP support" OFF)
168169
OCV_OPTION(WITH_CSTRIPES "Include C= support" OFF IF WIN32 )
@@ -879,10 +880,6 @@ if(ANDROID)
879880
endif()
880881
endif()
881882

882-
if(DEFINED WITH_AVFOUNDATION)
883-
status(" AVFoundation:" WITH_AVFOUNDATION THEN YES ELSE NO)
884-
endif(DEFINED WITH_AVFOUNDATION)
885-
886883
if(DEFINED WITH_FFMPEG)
887884
if(WIN32)
888885
status(" FFMPEG:" WITH_FFMPEG THEN "YES (prebuilt binaries)" ELSE NO)
@@ -923,10 +920,15 @@ if(DEFINED WITH_GIGEAPI)
923920
status(" GigEVisionSDK:" HAVE_GIGE_API THEN YES ELSE NO)
924921
endif(DEFINED WITH_GIGEAPI)
925922

926-
if(DEFINED WITH_QUICKTIME)
923+
if(DEFINED APPLE)
924+
status(" AVFoundation:" HAVE_AVFOUNDATION THEN YES ELSE NO)
925+
if(WITH_QUICKTIME OR HAVE_QUICKTIME)
927926
status(" QuickTime:" HAVE_QUICKTIME THEN YES ELSE NO)
928-
status(" QTKit:" HAVE_QTKIT THEN YES ELSE NO)
929-
endif(DEFINED WITH_QUICKTIME)
927+
endif()
928+
if(WITH_QTKIT OR HAVE_QTKIT)
929+
status(" QTKit:" HAVE_QTKIT THEN "YES (deprecated)" ELSE NO)
930+
endif()
931+
endif(DEFINED APPLE)
930932

931933
if(DEFINED WITH_UNICAP)
932934
status(" UniCap:" HAVE_UNICAP THEN "YES (ver ${ALIASOF_libunicap_VERSION})" ELSE NO)

cmake/OpenCVFindLibsVideo.cmake

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,18 @@ if(WIN32)
273273
endif()
274274
endif(WIN32)
275275

276-
# --- Apple AV Foundation ---
277-
if(WITH_AVFOUNDATION)
278-
set(HAVE_AVFOUNDATION YES)
279-
endif()
280-
281-
# --- QuickTime ---
282-
if (NOT IOS)
283-
if(WITH_QUICKTIME)
284-
set(HAVE_QUICKTIME YES)
285-
elseif(APPLE)
286-
set(HAVE_QTKIT YES)
276+
if(APPLE)
277+
if(WITH_AVFOUNDATION)
278+
set(HAVE_AVFOUNDATION YES)
287279
endif()
288-
endif()
280+
if(NOT IOS)
281+
if(WITH_QUICKTIME)
282+
set(HAVE_QUICKTIME YES)
283+
elseif(WITH_QTKIT)
284+
set(HAVE_QTKIT YES)
285+
endif()
286+
endif()
287+
endif(APPLE)
289288

290289
# --- Intel Perceptual Computing SDK ---
291290
if(WITH_INTELPERC)

modules/highgui/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,13 @@ if(HAVE_IMAGEIO AND IOS)
215215
endif()
216216

217217
if(HAVE_AVFOUNDATION)
218-
list(APPEND highgui_srcs src/cap_avfoundation.mm)
219-
list(APPEND HIGHGUI_LIBRARIES "-framework AVFoundation" "-framework QuartzCore")
218+
if(IOS)
219+
list(APPEND highgui_srcs ${CMAKE_CURRENT_LIST_DIR}/src/cap_avfoundation.mm)
220+
list(APPEND HIGHGUI_LIBRARIES "-framework AVFoundation" "-framework QuartzCore")
221+
else()
222+
list(APPEND highgui_srcs ${CMAKE_CURRENT_LIST_DIR}/src/cap_avfoundation_mac.mm)
223+
list(APPEND HIGHGUI_LIBRARIES "-framework Cocoa" "-framework Accelerate" "-framework AVFoundation" "-framework CoreGraphics" "-framework CoreImage" "-framework CoreMedia" "-framework CoreVideo" "-framework QuartzCore")
224+
endif()
220225
endif()
221226

222227
if(HAVE_QUICKTIME)

modules/highgui/src/cap_avfoundation_mac.mm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ - (IplImage*)getOutput;
155155
uint8_t *mOutImagedata;
156156
IplImage *mOutImage;
157157
size_t currSize;
158+
/*
158159
int mMode;
160+
*/
159161
int mFormat;
160162

161163
bool setupReadingAt(CMTime position);
@@ -677,7 +679,9 @@ -(int) updateImage {
677679
mOutImage = NULL;
678680
mOutImagedata = NULL;
679681
currSize = 0;
682+
/*
680683
mMode = CV_CAP_MODE_BGR;
684+
*/
681685
mFormat = CV_8UC3;
682686
mCurrentSampleBuffer = NULL;
683687
mGrabbedPixels = NULL;
@@ -741,12 +745,15 @@ -(int) updateImage {
741745

742746
// Capture in a pixel format that can be converted efficiently to the output mode.
743747
OSType pixelFormat;
748+
/*
744749
if (mMode == CV_CAP_MODE_BGR || mMode == CV_CAP_MODE_RGB) {
750+
*/
745751
// For CV_CAP_MODE_BGR, read frames as BGRA (AV Foundation's YUV->RGB conversion is slightly faster than OpenCV's CV_YUV2BGR_YV12)
746752
// kCVPixelFormatType_32ABGR is reportedly faster on OS X, but OpenCV doesn't have a CV_ABGR2BGR conversion.
747753
// kCVPixelFormatType_24RGB is significanly slower than kCVPixelFormatType_32BGRA.
748754
pixelFormat = kCVPixelFormatType_32BGRA;
749755
mFormat = CV_8UC3;
756+
/*
750757
} else if (mMode == CV_CAP_MODE_GRAY) {
751758
// For CV_CAP_MODE_GRAY, read frames as 420v (faster than 420f or 422 -- at least for H.264 files)
752759
pixelFormat = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
@@ -759,6 +766,7 @@ -(int) updateImage {
759766
fprintf(stderr, "VIDEOIO ERROR: AVF Mac: Unsupported mode: %d\n", mMode);
760767
return false;
761768
}
769+
*/
762770

763771
NSDictionary *settings =
764772
@{
@@ -848,8 +856,11 @@ -(int) updateImage {
848856

849857
// Output image paramaters.
850858
int outChannels;
859+
/*
851860
if (mMode == CV_CAP_MODE_BGR || mMode == CV_CAP_MODE_RGB) {
861+
*/
852862
outChannels = 3;
863+
/*
853864
} else if (mMode == CV_CAP_MODE_GRAY) {
854865
outChannels = 1;
855866
} else if (mMode == CV_CAP_MODE_YUYV) {
@@ -861,6 +872,7 @@ -(int) updateImage {
861872
mGrabbedPixels = NULL;
862873
return 0;
863874
}
875+
*/
864876

865877
if ( currSize != width*outChannels*height ) {
866878
currSize = width*outChannels*height;
@@ -888,8 +900,11 @@ -(int) updateImage {
888900
if ( pixelFormat == kCVPixelFormatType_32BGRA ) {
889901
deviceChannels = 4;
890902

903+
/*
891904
if (mMode == CV_CAP_MODE_BGR) {
905+
*/
892906
cvtCode = CV_BGRA2BGR;
907+
/*
893908
} else if (mMode == CV_CAP_MODE_RGB) {
894909
cvtCode = CV_BGRA2RGB;
895910
} else if (mMode == CV_CAP_MODE_GRAY) {
@@ -901,11 +916,15 @@ -(int) updateImage {
901916
fprintf(stderr, "OpenCV: unsupported pixel conversion mode\n");
902917
return 0;
903918
}
919+
*/
904920
} else if ( pixelFormat == kCVPixelFormatType_24RGB ) {
905921
deviceChannels = 3;
906922

923+
/*
907924
if (mMode == CV_CAP_MODE_BGR) {
925+
*/
908926
cvtCode = CV_RGB2BGR;
927+
/*
909928
} else if (mMode == CV_CAP_MODE_RGB) {
910929
cvtCode = 0;
911930
} else if (mMode == CV_CAP_MODE_GRAY) {
@@ -917,11 +936,15 @@ -(int) updateImage {
917936
fprintf(stderr, "OpenCV: unsupported pixel conversion mode\n");
918937
return 0;
919938
}
939+
*/
920940
} else if ( pixelFormat == kCVPixelFormatType_422YpCbCr8 ) { // 422 (2vuy, UYVY)
921941
deviceChannels = 2;
922942

943+
/*
923944
if (mMode == CV_CAP_MODE_BGR) {
945+
*/
924946
cvtCode = CV_YUV2BGR_UYVY;
947+
/*
925948
} else if (mMode == CV_CAP_MODE_RGB) {
926949
cvtCode = CV_YUV2RGB_UYVY;
927950
} else if (mMode == CV_CAP_MODE_GRAY) {
@@ -935,15 +958,19 @@ -(int) updateImage {
935958
fprintf(stderr, "OpenCV: unsupported pixel conversion mode\n");
936959
return 0;
937960
}
961+
*/
938962
} else if ( pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange || // 420v
939963
pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange ) { // 420f
940964
// cvCvtColor(CV_YUV2GRAY_420) is expecting a single buffer with both the Y plane and the CrCb planes.
941965
// So, lie about the height of the buffer. cvCvtColor(CV_YUV2GRAY_420) will only read the first 2/3 of it.
942966
height = height * 3 / 2;
943967
deviceChannels = 1;
944968

969+
/*
945970
if (mMode == CV_CAP_MODE_BGR) {
971+
*/
946972
cvtCode = CV_YUV2BGR_YV12;
973+
/*
947974
} else if (mMode == CV_CAP_MODE_RGB) {
948975
cvtCode = CV_YUV2RGB_YV12;
949976
} else if (mMode == CV_CAP_MODE_GRAY) {
@@ -955,6 +982,7 @@ -(int) updateImage {
955982
fprintf(stderr, "OpenCV: unsupported pixel conversion mode\n");
956983
return 0;
957984
}
985+
*/
958986
} else {
959987
fprintf(stderr, "OpenCV: unsupported pixel format 0x%08X\n", pixelFormat);
960988
CVPixelBufferUnlockBaseAddress(mGrabbedPixels, 0);
@@ -1020,8 +1048,10 @@ -(int) updateImage {
10201048
return round((t.value * mAssetTrack.nominalFrameRate) / double(t.timescale));
10211049
case CV_CAP_PROP_FORMAT:
10221050
return mFormat;
1051+
/*
10231052
case CV_CAP_PROP_MODE:
10241053
return mMode;
1054+
*/
10251055
default:
10261056
break;
10271057
}
@@ -1054,6 +1084,7 @@ -(int) updateImage {
10541084
setupReadingAt(t);
10551085
retval = true;
10561086
break;
1087+
/*
10571088
case CV_CAP_PROP_MODE:
10581089
int mode;
10591090
mode = cvRound(value);
@@ -1076,6 +1107,7 @@ -(int) updateImage {
10761107
}
10771108
}
10781109
break;
1110+
*/
10791111
default:
10801112
break;
10811113
}

0 commit comments

Comments
 (0)