[artoolkit-commits] artoolkit/lib/SRC/VideoMacOSX video.c, 1.26, 1.27
Optical marker tracking and overlay for augmented reality.
Brought to you by:
philip_lamb
From: Philip L. <phi...@us...> - 2007-08-06 02:40:55
|
Update of /cvsroot/artoolkit/artoolkit/lib/SRC/VideoMacOSX In directory sc8-pr-cvs11.sourceforge.net:/tmp/cvs-serv13603 Modified Files: video.c Log Message: Remove extraneous buffer copy in idle thread. Add some parameter checking. Remove some outdated code. Add early modifications for compiling against QuickTime 6.4 for Windows. Index: video.c =================================================================== RCS file: /cvsroot/artoolkit/artoolkit/lib/SRC/VideoMacOSX/video.c,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** video.c 23 Jan 2007 00:39:28 -0000 1.26 --- video.c 6 Aug 2007 02:40:56 -0000 1.27 *************** *** 79,101 **** // ============================================================================ ! #include <Carbon/Carbon.h> ! #include <QuickTime/QuickTime.h> ! #include <CoreServices/CoreServices.h> // Gestalt() ! #include <pthread.h> ! #include <sys/time.h> ! #include <unistd.h> // usleep() ! #include <sys/types.h> // sysctlbyname() ! #include <sys/sysctl.h> // sysctlbyname() #include <AR/config.h> #include <AR/ar.h> #include <AR/video.h> - #include "videoInternal.h" // ============================================================================ // Private definitions // ============================================================================ //#define AR_VIDEO_SUPPORT_OLD_QUICKTIME // Uncomment to allow use of non-thread safe QuickTime (pre-6.4). - #define AR_VIDEO_DEBUG_FIX_DUAL_PROCESSOR_RACE #define AR_VIDEO_IDLE_INTERVAL_MILLISECONDS_MIN 20L --- 79,118 ---- // ============================================================================ ! #ifdef __APPLE__ ! # include <Carbon/Carbon.h> ! # include <QuickTime/QuickTime.h> ! # include <CoreServices/CoreServices.h> // Gestalt() ! # include <unistd.h> // usleep(), valloc() ! # include <sys/types.h> // sysctlbyname() ! # include <sys/sysctl.h> // sysctlbyname() ! # include "videoInternal.h" ! #elif defined(_WIN32) ! # ifndef __MOVIES__ ! # include <Movies.h> ! # endif ! # ifndef __QTML__ ! # include <QTML.h> ! # endif ! # ifndef __GXMATH__ ! # include <GXMath.h> ! # endif ! #else ! # error ! #endif ! #include <pthread.h> // Use pthreads-win32 on Windows. ! #include <string.h> // memcpy() #include <AR/config.h> #include <AR/ar.h> #include <AR/video.h> // ============================================================================ // Private definitions // ============================================================================ + #ifdef _WIN32 + # define valloc malloc + # define usleep(t) Sleep((DWORD)(t/1000u)); + #endif //#define AR_VIDEO_SUPPORT_OLD_QUICKTIME // Uncomment to allow use of non-thread safe QuickTime (pre-6.4). #define AR_VIDEO_IDLE_INTERVAL_MILLISECONDS_MIN 20L *************** *** 112,115 **** --- 129,147 ---- #endif + // pthreads-win32 cleanup functions must be declared using the cdecl calling convention. + #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__) + # ifndef ARVIDEO_APIENTRY + # define ARVIDEO_APIENTRY __cdecl + # endif + #elif defined(__CYGWIN__) || defined(__MINGW32__) + # ifndef ARVIDEO_APIENTRY + # define ARVIDEO_APIENTRY __attribute__ ((__cdecl__)) + # endif + #else // non-Win32 case. + # ifndef ARVIDEO_APIENTRY + # define ARVIDEO_APIENTRY + # endif + #endif + // ============================================================================ // Private types *************** *** 353,361 **** // Use the SG Dialog to allow the user to select device and compression settings ! if (err = RequestSGSettings(inputIndex, pVdg->seqGrab, pVdg->sgchanVideo, showDialog, standardDialog)) { fprintf(stderr, "RequestSGSettings err=%ld\n", err); goto endFunc; ! } ! if (err = vdgGetSettings(pVdg)) { fprintf(stderr, "vdgGetSettings err=%ld\n", err); --- 385,402 ---- // Use the SG Dialog to allow the user to select device and compression settings ! #ifdef __APPLE__ ! if ((err = RequestSGSettings(inputIndex, pVdg->seqGrab, pVdg->sgchanVideo, showDialog, standardDialog) != noErr)) { fprintf(stderr, "RequestSGSettings err=%ld\n", err); goto endFunc; ! } ! #else ! if (showDialog) { ! if ((err = SGSettingsDialog(pVdg->seqGrab, pVdg->sgchanVideo, 0, 0, seqGrabSettingsPreviewOnly, NULL, 0L)) != noErr)) { ! fprintf(stderr, "SGSettingsDialog err=%ld\n", err); ! goto endFunc; ! } ! } ! #endif ! if (err = vdgGetSettings(pVdg)) { fprintf(stderr, "vdgGetSettings err=%ld\n", err); *************** *** 967,971 **** } ! static void ar2VideoInternalThreadCleanup(void *arg) { AR2VideoParamT *vid; --- 1008,1012 ---- } ! static void ARVIDEO_APIENTRY ar2VideoInternalThreadCleanup(void *arg) { AR2VideoParamT *vid; *************** *** 995,1003 **** int keepAlive = 1; - #ifndef AR_VIDEO_DEBUG_FIX_DUAL_PROCESSOR_RACE - struct timeval tv; // Seconds and microseconds since Jan 1, 1970. - struct timespec ts; // Seconds and nanoseconds since Jan 1, 1970. - int err_i; - #endif // !AR_VIDEO_DEBUG_FIX_DUAL_PROCESSOR_RACE ComponentResult err; int isUpdated = 0; --- 1036,1039 ---- *************** *** 1027,1041 **** while (keepAlive && vdgIsGrabbing(vid->pVdg)) { ! ! #ifndef AR_VIDEO_DEBUG_FIX_DUAL_PROCESSOR_RACE ! gettimeofday(&tv, NULL); ! ts.tv_sec = tv.tv_sec; ! ts.tv_nsec = tv.tv_usec * 1000 + vid->milliSecPerTimer * 1000000; ! if (ts.tv_nsec >= 1000000000) { ! ts.tv_nsec -= 1000000000; ! ts.tv_sec += 1; ! } ! #endif // AR_VIDEO_DEBUG_FIX_DUAL_PROCESSOR_RACE ! #ifdef AR_VIDEO_SUPPORT_OLD_QUICKTIME // Get a lock to access QuickTime (for SGIdle()), but only if more than one thread is running. --- 1063,1067 ---- while (keepAlive && vdgIsGrabbing(vid->pVdg)) { ! #ifdef AR_VIDEO_SUPPORT_OLD_QUICKTIME // Get a lock to access QuickTime (for SGIdle()), but only if more than one thread is running. *************** *** 1127,1151 **** //vid->lastTime = time; } - // Now copy the frame (if double-buffering). - if (vid->bufCopyFlag) { - if (vid->status & AR_VIDEO_STATUS_BIT_BUFFER) { - memcpy((void *)(vid->bufPixelsCopy2), (void *)(vid->bufPixels), vid->bufSize); - } else { - memcpy((void *)(vid->bufPixelsCopy1), (void *)(vid->bufPixels), vid->bufSize); - } - } // Mark status to indicate we have a frame available. vid->status |= AR_VIDEO_STATUS_BIT_READY; } ! // All done. Wewease Wodger! ! #ifndef AR_VIDEO_DEBUG_FIX_DUAL_PROCESSOR_RACE ! err_i = pthread_cond_timedwait(&(vid->condition), &(vid->bufMutex), &ts); ! if (err_i != 0 && err_i != ETIMEDOUT) { ! fprintf(stderr, "ar2VideoInternalThread(): Error %d waiting for condition.\n", err_i); ! keepAlive = 0; ! break; ! } ! #else ar2VideoInternalUnlock(&(vid->bufMutex)); usleep(vid->milliSecPerTimer * 1000); --- 1153,1161 ---- //vid->lastTime = time; } // Mark status to indicate we have a frame available. vid->status |= AR_VIDEO_STATUS_BIT_READY; } ! // All done. Welease Wodger! ar2VideoInternalUnlock(&(vid->bufMutex)); usleep(vid->milliSecPerTimer * 1000); *************** *** 1155,1159 **** break; } - #endif // AR_VIDEO_DEBUG_FIX_DUAL_PROCESSOR_RACE pthread_testcancel(); --- 1165,1168 ---- *************** *** 1164,1167 **** --- 1173,1177 ---- } + #ifdef __APPLE__ static int sysctlbyname_with_pid (const char *name, pid_t pid, void *oldp, size_t *oldlenp, *************** *** 1194,1198 **** // Pass 0 to use current PID. ! int is_pid_native (pid_t pid) { int ret = 0; --- 1204,1208 ---- // Pass 0 to use current PID. ! static int is_pid_native (pid_t pid) { int ret = 0; *************** *** 1211,1214 **** --- 1221,1225 ---- return ret; } + #endif // __APPLE__ #pragma mark - *************** *** 1394,1399 **** if (gVidCount == 0) { if ((err_s = Gestalt(gestaltQuickTimeVersion, &qtVersion)) != noErr) { ! fprintf(stderr,"ar2VideoOpen(): QuickTime not installed (%d).\n", err_s); return (NULL); } --- 1405,1417 ---- if (gVidCount == 0) { + #ifdef _WIN32 + if ((err_s = InitializeQTML(0)) != noErr) { + fprintf(stderr, "ar2VideoOpen(): OS error: QuickTime not installed.\n"); + return (NULL); + } + #endif // _WIN32 + if ((err_s = Gestalt(gestaltQuickTimeVersion, &qtVersion)) != noErr) { ! fprintf(stderr, "ar2VideoOpen(): OS error: QuickTime not installed (%d).\n", err_s); return (NULL); } *************** *** 1451,1454 **** --- 1469,1473 ---- vid->bufCopyFlag = !singleBuffer; + #ifdef __APPLE__ // Find out if we are running on an Intel Mac. if ((err_s = Gestalt(gestaltNativeCPUtype, &cpuType) != noErr)) { *************** *** 1471,1474 **** --- 1490,1494 ---- } } + #endif // __APPLE__ if(!(vid->pVdg = vdgAllocAndInit(grabber))) { *************** *** 1534,1538 **** vid->width = (width ? width : (int)((*vid->vdImageDesc)->width)); vid->height = (height ? height : (int)((*vid->vdImageDesc)->height)); ! SetRect(&(vid->theRect), 0, 0, (short)vid->width, (short)vid->height); // Make a scaling matrix for the sequence if size of incoming video differs from GWorld dimensions. --- 1554,1558 ---- vid->width = (width ? width : (int)((*vid->vdImageDesc)->width)); vid->height = (height ? height : (int)((*vid->vdImageDesc)->height)); ! MacSetRect(&(vid->theRect), 0, 0, (short)vid->width, (short)vid->height); // Make a scaling matrix for the sequence if size of incoming video differs from GWorld dimensions. *************** *** 1602,1606 **** // Erase to black. ! #if 1 CGContextRef ctx; QDBeginCGContext(vid->pGWorld, &ctx); --- 1622,1626 ---- // Erase to black. ! #ifdef __APPLE__ CGContextRef ctx; QDBeginCGContext(vid->pGWorld, &ctx); *************** *** 1668,1672 **** if (!ar2VideoInternalUnlock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoOpen(): Unable to unlock mutex (for QuickTime).\n"); - return (NULL); } } --- 1688,1691 ---- *************** *** 1683,1686 **** --- 1702,1707 ---- #endif // AR_VIDEO_SUPPORT_OLD_QUICKTIME + if( vid == NULL ) return -1; + #ifdef AR_VIDEO_SUPPORT_OLD_QUICKTIME // Get a hold on the QuickTime toolbox. *************** *** 1759,1762 **** --- 1780,1786 ---- // Probably a good idea to close down QuickTime. ExitMovies(); + #ifdef _WIN32 + TerminateQTML(); + #endif // _WIN32 } *************** *** 1772,1775 **** --- 1796,1801 ---- #endif // AR_VIDEO_SUPPORT_OLD_QUICKTIME + if( vid == NULL ) return -1; + #ifdef AR_VIDEO_SUPPORT_OLD_QUICKTIME // Get a hold on the QuickTime toolbox. *************** *** 1777,1781 **** if (!ar2VideoInternalLock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoCapStart(): Unable to lock mutex (for QuickTime).\n"); ! return (1); } weLocked = 1; --- 1803,1807 ---- if (!ar2VideoInternalLock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoCapStart(): Unable to lock mutex (for QuickTime).\n"); ! return (-1); } weLocked = 1; *************** *** 1803,1807 **** if (!ar2VideoInternalUnlock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoCapStart(): Unable to unlock mutex (for QuickTime).\n"); ! return (1); } } --- 1829,1833 ---- if (!ar2VideoInternalUnlock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoCapStart(): Unable to unlock mutex (for QuickTime).\n"); ! return (-1); } } *************** *** 1834,1837 **** --- 1860,1865 ---- ComponentResult err = noErr; + if( vid == NULL ) return -1; + if (vid->threadRunning) { // Cancel thread. *************** *** 1859,1863 **** if (!ar2VideoInternalLock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoCapStop(): Unable to lock mutex (for QuickTime).\n"); ! return (1); } weLocked = 1; --- 1887,1891 ---- if (!ar2VideoInternalLock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoCapStop(): Unable to lock mutex (for QuickTime).\n"); ! return (-1); } weLocked = 1; *************** *** 1877,1881 **** if (!ar2VideoInternalUnlock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoCapStop(): Unable to unlock mutex (for QuickTime).\n"); ! return (1); } } --- 1905,1909 ---- if (!ar2VideoInternalUnlock(&gVidQuickTimeMutex)) { fprintf(stderr, "ar2VideoCapStop(): Unable to unlock mutex (for QuickTime).\n"); ! return (-1); } } *************** *** 1889,1896 **** int ar2VideoInqSize(AR2VideoParamT *vid, int *x,int *y) { // Need lock to guarantee exclusive access to vid. if (!ar2VideoInternalLock(&(vid->bufMutex))) { fprintf(stderr, "ar2VideoInqSize(): Unable to lock mutex.\n"); ! return (1); } --- 1917,1926 ---- int ar2VideoInqSize(AR2VideoParamT *vid, int *x,int *y) { + if( vid == NULL ) return -1; + // Need lock to guarantee exclusive access to vid. if (!ar2VideoInternalLock(&(vid->bufMutex))) { fprintf(stderr, "ar2VideoInqSize(): Unable to lock mutex.\n"); ! return (-1); } *************** *** 1900,1904 **** if (!ar2VideoInternalUnlock(&(vid->bufMutex))) { fprintf(stderr, "ar2VideoInqSize(): Unable to unlock mutex.\n"); ! return (1); } return (0); --- 1930,1934 ---- if (!ar2VideoInternalUnlock(&(vid->bufMutex))) { fprintf(stderr, "ar2VideoInqSize(): Unable to unlock mutex.\n"); ! return (-1); } return (0); *************** *** 1909,1912 **** --- 1939,1944 ---- ARUint8 *pix = NULL; + if (vid == NULL) return (NULL); + // ar2VideoGetImage() used to block waiting for a frame. // This locked the OpenGL frame rate to the camera frame rate. *************** *** 1916,1928 **** //fprintf(stderr, "For vid @ %p got frame %ld.\n", vid, vid->frameCount); ! ! // Prior Mac versions of ar2VideoInternal added 1 to the pixmap base address ! // returned to the caller to cope with the fact that neither ! // arDetectMarker() or argDispImage() knew how to cope with ! // pixel data with ARGB (Apple) or ABGR (SGI) byte ordering. ! // Adding 1 had the effect of passing a pointer to the first byte ! // of non-alpha data. This was an awful hack which caused all sorts ! // of problems and which can now be avoided after rewriting the ! // various bits of the toolkit to cope. if (vid->bufCopyFlag) { // Need lock to guarantee this thread exclusive access to vid. --- 1948,1952 ---- //fprintf(stderr, "For vid @ %p got frame %ld.\n", vid, vid->frameCount); ! // If triple-buffering, time to copy buffer. if (vid->bufCopyFlag) { // Need lock to guarantee this thread exclusive access to vid. |