video2.c
Go to the documentation of this file.
00001 /*******************************************************
00002  *
00003  * Author: Hirokazu Kato, Atsishi Nakazawa
00004  *
00005  *         kato@sys.im.hiroshima-cu.ac.jp
00006  *         nakazawa@inolab.sys.es.osaka-u.ac.jp
00007  *
00008  * Revision: 4.1
00009  * Date: 99/07/16
00010  *
00011 *******************************************************/
00012 
00013 #include <string.h>
00014 #include <vl/vl.h>
00015 #include <AR/sys/videoSGI.h>
00016 
00017 typedef struct {
00018     ARVideoFormat   format;
00019     ARVideoPacking  packing;
00020     ARVideoZoom     zoom;
00021     int             xsize;
00022     int             ysize;
00023 
00024     VLPath          path;
00025     VLNode          src;
00026     VLNode          drn;
00027     VLBuffer        buffer;
00028     unsigned char   *t_buf;
00029     int             t_size;
00030     int             f1_is_first;
00031 
00032     int             setup_flag;
00033     int             active_flag;
00034     int             start_flag;
00035     int             buf_flag;
00036     int             buffer_size;
00037 } ARVideoAtt;
00038 
00039 static int                   open_flag = 0;
00040 static ARVideoDeviceTypeList dev_list;
00041 static VLServer              svr;
00042 static VLDev                 device_id[ARVideoDeviceMax];
00043 static ARVideoAtt            video_att[ARVideoDeviceMax];
00044 
00045 
00046 static void            activate_path( int dev_id );
00047 static void            deactivate_path( int dev_id );
00048 static void            error_exit(void);
00049 
00050 
00051 
00052 
00053 int  arVideoOpen2(void)
00054 {
00055     int     i;
00056 
00057     if( open_flag == 1 ) return(-1);
00058 
00059     /* Connect to the daemon */
00060     if (!(svr = vlOpenVideo(""))) error_exit();
00061     open_flag = 1;
00062 
00063     for( i = 0; i < ARVideoDeviceMax; i++ ) {
00064         video_att[i].setup_flag  = 0;
00065         video_att[i].buffer_size = 30;
00066     }
00067     if( arVideoInqDevice2( &dev_list ) < 0 ) error_exit();
00068 
00069     return(0);
00070 }
00071 
00072 int arVideoClose2(void)
00073 {
00074     int     i;
00075 
00076     if( open_flag == 0 ) return(-1);
00077 
00078     for( i = 0; i < ARVideoDeviceMax; i++ ) {
00079         if( video_att[i].setup_flag == 0 ) continue;
00080 
00081         if( arVideoCleanupDevice2( i ) < 0 ) exit(0);
00082     }
00083 
00084     vlCloseVideo(svr);
00085     open_flag = 0;
00086 
00087     return(0);
00088 }
00089 
00090 
00091 int  arVideoInqDevice2( ARVideoDeviceTypeList *dev_list2 )
00092 {
00093     static int  device_check_flag = 0;
00094     VLDevList   vl_devlist;
00095     int         i;
00096 
00097     if( open_flag == 0 ) return(-1);
00098 
00099 
00100     if( device_check_flag == 0 ) {
00101         if( vlGetDeviceList(svr, &vl_devlist) == -1 ) error_exit();
00102 
00103         dev_list.num = vl_devlist.numDevices;
00104 
00105         for( i = 0; i < dev_list.num; i++ ) {
00106             device_id[i] = vl_devlist.devices[i].dev;
00107             if( strcmp( vl_devlist.devices[i].name, "vino" ) == 0 ) {
00108                 dev_list.type[i] = AR_VIDEO_INDY;
00109             }
00110             else if( strcmp( vl_devlist.devices[i].name, "mvp" ) == 0 ) {
00111                 dev_list.type[i] = AR_VIDEO_O2;
00112             }
00113             else if( strcmp( vl_devlist.devices[i].name, "ev1" ) == 0 ) {
00114                 dev_list.type[i] = AR_VIDEO_GALILEO;
00115             }
00116             else if( strcmp( vl_devlist.devices[i].name, "evo" ) == 0 ) {
00117                 dev_list.type[i] = AR_VIDEO_OCTANE;
00118             }
00119             else if( strcmp( vl_devlist.devices[i].name, "impact" ) == 0 ) {
00120                 dev_list.type[i] = AR_VIDEO_IMPACT;
00121             }
00122             else {
00123                 printf("Unknown devide type!! --> %s\n", vl_devlist.devices[i].name);
00124                 vlCloseVideo(svr);
00125                 exit(0);
00126             }
00127         }
00128 
00129         device_check_flag = 1;
00130     }
00131 
00132     dev_list2->num = dev_list.num;
00133     for( i = 0; i < dev_list.num; i++ ) {
00134         dev_list2->type[i] = dev_list.type[i];
00135     }
00136 
00137     return( 0 );
00138 }
00139 
00140 int arVideoInqSize2( int dev_id, int *x, int *y )
00141 {
00142     if( open_flag == 0 ) return(-1);
00143     if( video_att[dev_id].setup_flag == 0 ) return(-1);
00144 
00145     if( video_att[dev_id].active_flag == 0 ) {
00146         activate_path( dev_id );
00147         video_att[dev_id].active_flag = 1;
00148     }
00149 
00150     *x = video_att[dev_id].xsize;
00151     *y = video_att[dev_id].ysize;
00152 
00153     return(0);
00154 }
00155 
00156 
00157 
00158 int  arVideoSetupDevice2( int            dev_id,
00159                          ARVideoFormat  format,
00160                          ARVideoPacking packing,
00161                          ARVideoZoom    zoom )
00162 {
00163     int     number;
00164 
00165     if( open_flag == 0 ) return(-1);
00166     if( video_att[dev_id].setup_flag == 1 ) return(-1);
00167 
00168 
00169     /* Set up a source node */
00170     number = VL_ANY;
00171     if( (video_att[dev_id].src = vlGetNode(svr, VL_SRC, VL_VIDEO, number)) < 0 ) error_exit();
00172 
00173 
00174     /* Set up a drain node in memory */
00175     if( (video_att[dev_id].drn = vlGetNode(svr, VL_DRN, VL_MEM, VL_ANY)) < 0 ) error_exit();
00176 
00177 
00178     /* Create a path using the first device that will support it */
00179     if( (video_att[dev_id].path = vlCreatePath(svr, device_id[dev_id],
00180                                                video_att[dev_id].src,
00181                                                video_att[dev_id].drn)) < 0 ) error_exit();
00182 
00183 
00184     video_att[dev_id].format  = format;
00185     video_att[dev_id].packing = packing;
00186     video_att[dev_id].zoom    = zoom;
00187 
00188     if( dev_list.type[dev_id] == AR_VIDEO_GALILEO ) {
00189         if( packing == AR_VIDEO_RGB_332 ) {
00190             printf("This subroutine support only VIDEO_RGB_332\n");
00191             printf("on Galileo Video device.\n");
00192             exit(1);
00193         }
00194         if( format == AR_VIDEO_NONINTERLEAVED ) {
00195             printf("This subroutine does not support AR_VIDEO_NONINTERLEAVED\n");
00196             printf("on Galileo Video device.\n");
00197             exit(1);
00198         }
00199         if( zoom == AR_VIDEO_1_P_8 ) {
00200             printf("This subroutine does not support AR_VIDEO_1_P_8\n");
00201             printf("on Galileo Video device.\n");
00202             exit(1);
00203         }
00204     }
00205     if( dev_list.type[dev_id] == AR_VIDEO_O2 ){
00206         if( packing == AR_VIDEO_MONO ) {
00207             printf("This subroutine cannot support AR_VIDEO_MONO\n");
00208             printf("on O2 MVP Video Device\n");
00209             exit(1);
00210         }     
00211     }
00212 
00213 
00214     video_att[dev_id].setup_flag = 1;
00215 
00216     return(0);
00217 }
00218 
00219 int  arVideoCleanupDevice2( int dev_id )
00220 {
00221     if( open_flag == 0 ) return(-1);
00222     if( video_att[dev_id].setup_flag == 0 ) return(-1);
00223 
00224     if( video_att[dev_id].start_flag == 1 ) {
00225         if( arVideoStop2(dev_id) < 0 ) exit(0);
00226     }
00227 
00228     if( video_att[dev_id].active_flag == 1 ) {
00229         deactivate_path( dev_id );
00230         video_att[dev_id].active_flag = 0;
00231     }
00232 
00233     if( vlDestroyPath(svr, video_att[dev_id].path) < 0 ) error_exit();
00234     video_att[dev_id].setup_flag = 0;
00235 
00236     return(0);
00237 }
00238 
00239 
00240 
00241 int arVideoStart2( int dev_id )
00242 {
00243     VLTransferDescriptor xferDesc;
00244 
00245     if( open_flag == 0 ) return(-1);
00246     if( video_att[dev_id].setup_flag == 0 ) return(-1);
00247     if( video_att[dev_id].start_flag == 1 ) return(-1);
00248 
00249     if( video_att[dev_id].active_flag == 0 ) {
00250         activate_path( dev_id );
00251         video_att[dev_id].active_flag = 1;
00252     }
00253 
00254     /* Begin the data transfer */
00255     xferDesc.trigger = VLTriggerImmediate;
00256     xferDesc.mode    = VL_TRANSFER_MODE_CONTINUOUS;
00257     xferDesc.count   = 0;
00258     xferDesc.delay   = 0;
00259     if(vlBeginTransfer(svr, video_att[dev_id].path, 1, &xferDesc)) error_exit();
00260 
00261     video_att[dev_id].start_flag = 1;
00262 
00263     return(0);
00264 }
00265 
00266 int arVideoStop2( int dev_id )
00267 {
00268     if( open_flag == 0 ) return(-1);
00269     if( video_att[dev_id].setup_flag  == 0 ) return(-1);
00270     if( video_att[dev_id].active_flag == 0 ) return(-1);
00271     if( video_att[dev_id].start_flag  == 0 ) return(-1);
00272 
00273     if( video_att[dev_id].buf_flag == 1 ) {
00274         if( vlPutFree(svr, video_att[dev_id].buffer) < 0 ) error_exit();
00275         video_att[dev_id].buf_flag = 0;
00276     }
00277 
00278     /* End the data transfer */
00279     if( vlEndTransfer(svr, video_att[dev_id].path) < 0 ) error_exit();
00280     if( vlBufferReset(svr, video_att[dev_id].buffer) < 0 ) error_exit();
00281 
00282     video_att[dev_id].start_flag = 0;
00283 
00284     return(0);
00285 }
00286 
00287 int arVideoSetBufferSize2( int dev_id, int size )
00288 {
00289     if( open_flag == 0 ) return(-1);
00290     if( video_att[dev_id].active_flag == 1 ) return(-1);
00291 
00292     video_att[dev_id].buffer_size = size;
00293 
00294     return(0);
00295 }
00296 
00297 unsigned char *arVideoGetImage2( int dev_id )
00298 {
00299     VLInfoPtr       info;
00300     unsigned char   *dataPtr;
00301     unsigned char   *p1, *p2, *p3;
00302     int             i, j, k, l;
00303     int             xsize, ysize;
00304 
00305     if( open_flag == 0 )                     return(NULL);
00306     if( video_att[dev_id].setup_flag  == 0 ) return(NULL);
00307     if( video_att[dev_id].active_flag == 0 ) return(NULL);
00308     if( video_att[dev_id].start_flag  == 0 ) return(NULL);
00309 
00310     if( video_att[dev_id].buf_flag == 1 ) {
00311         vlPutFree(svr, video_att[dev_id].buffer);
00312         video_att[dev_id].buf_flag = 0;
00313     }
00314 
00315     info = vlGetLatestValid(svr, video_att[dev_id].buffer);
00316     if( info == NULL ) return(NULL);
00317     
00318     /* Get a pointer to the frame */
00319     dataPtr = vlGetActiveRegion(svr, video_att[dev_id].buffer, info);
00320 
00321     video_att[dev_id].buf_flag = 1;
00322     return( dataPtr );
00323 }
00324 
00325 
00326 
00327 static void error_exit(void)
00328 {
00329     vlPerror("Video Library");
00330     exit(0);
00331 }
00332 
00333 static void activate_path( int dev_id )
00334 {
00335     VLControlValue  val;
00336     VLControlValue  timing;
00337     VLControlValue  dominance;
00338     VLPath          paths[ARVideoDeviceMax];
00339     int             count;
00340     int             timing_525;
00341     int             i;
00342 
00343     if( open_flag == 0 ) exit(0);
00344     if( video_att[dev_id].setup_flag  == 0 ) exit(0);
00345     if( video_att[dev_id].active_flag == 1 ) exit(0);
00346     if( video_att[dev_id].start_flag  == 1 ) exit(0);
00347 
00348     count = 0;
00349     for( i = 0; i < ARVideoDeviceMax; i++ ) {
00350         if( i == dev_id ) {
00351             paths[count++] = video_att[i].path;
00352         }
00353         else if( video_att[dev_id].active_flag == 1 ) {
00354             paths[count++] = video_att[i].path;
00355         }
00356     }
00357 
00358     if((vlSetupPaths(svr, (VLPathList)paths, count, VL_LOCK, VL_LOCK)) < 0) error_exit();
00359 
00360     /* Set the CAP type */
00361     switch( video_att[dev_id].format ) {
00362       case AR_VIDEO_INTERLEAVED:
00363         val.intVal = VL_CAPTURE_INTERLEAVED;
00364         break;
00365       case AR_VIDEO_NONINTERLEAVED:
00366         val.intVal = VL_CAPTURE_NONINTERLEAVED;
00367         break;
00368       case AR_VIDEO_ODD:
00369         val.intVal = VL_CAPTURE_ODD_FIELDS;
00370         break;
00371       case AR_VIDEO_EVEN:
00372         val.intVal = VL_CAPTURE_EVEN_FIELDS;
00373         break;
00374       default: error_exit();
00375     }
00376     if(vlSetControl(svr, video_att[dev_id].path,
00377                     video_att[dev_id].drn, VL_CAP_TYPE, &val) < 0) error_exit();
00378 
00379     /* Set the zoom  */
00380     val.fractVal.numerator = 1;
00381     switch( video_att[dev_id].zoom ) {
00382       case AR_VIDEO_1_P_1:
00383         val.fractVal.denominator = 1;
00384         break;
00385       case AR_VIDEO_1_P_2:
00386         val.fractVal.denominator = 2;
00387         break;
00388       case AR_VIDEO_1_P_4:
00389         val.fractVal.denominator = 4;
00390         break;
00391       case AR_VIDEO_1_P_8:
00392         val.fractVal.denominator = 8;
00393         break;
00394       default: error_exit();
00395     }
00396     if(vlSetControl(svr, video_att[dev_id].path,
00397                     video_att[dev_id].drn, VL_ZOOM, &val) < 0) error_exit();
00398 
00399     /* Set the packing to RGB */
00400     switch( video_att[dev_id].packing ) {
00401       case AR_VIDEO_RGB_8:
00402         val.intVal = VL_PACKING_RGB_8;
00403         break;
00404       case AR_VIDEO_RGB_332:
00405         val.intVal = VL_PACKING_RGB_332_P;
00406         break;
00407       case AR_VIDEO_MONO:
00408         val.intVal = VL_PACKING_Y_8_P;
00409         break;
00410       case AR_VIDEO_YVYU:
00411         val.intVal = VL_PACKING_YVYU_422_8;
00412         break;
00413       default: error_exit();
00414     }
00415     if(vlSetControl(svr, video_att[dev_id].path,
00416                     video_att[dev_id].drn, VL_PACKING, &val) < 0) error_exit();
00417     
00418     
00419     /* Get the video size */
00420     if( vlGetControl(svr, video_att[dev_id].path,
00421                      video_att[dev_id].drn, VL_SIZE, &val) < 0 ) error_exit();
00422     video_att[dev_id].xsize = val.xyVal.x;
00423     video_att[dev_id].ysize = val.xyVal.y;
00424 
00425     /* Create and register a buffer */
00426     video_att[dev_id].buffer = vlCreateBuffer(svr, video_att[dev_id].path,
00427                                                    video_att[dev_id].drn,
00428                                                    video_att[dev_id].buffer_size);
00429     if(video_att[dev_id].buffer == NULL) error_exit();  
00430     vlRegisterBuffer(svr, video_att[dev_id].path,
00431                           video_att[dev_id].drn,
00432                           video_att[dev_id].buffer);
00433 
00434     video_att[dev_id].t_size = vlGetTransferSize(svr, video_att[dev_id].path);
00435     video_att[dev_id].t_buf = (unsigned char *)malloc( video_att[dev_id].t_size * 2 );
00436     if( video_att[dev_id].t_buf == NULL ) {
00437         printf("malloc error !!\n");
00438         exit(0);
00439     }
00440 }
00441 
00442 static void deactivate_path( int dev_id )
00443 {
00444     if( open_flag == 0 ) exit(0);
00445     if( video_att[dev_id].setup_flag  == 0 ) exit(0);
00446     if( video_att[dev_id].active_flag == 0 ) exit(0);
00447     if( video_att[dev_id].start_flag  == 1 ) exit(0);
00448     if( video_att[dev_id].buf_flag    == 1 ) exit(0);
00449 
00450     vlDeregisterBuffer(svr, video_att[dev_id].path,
00451                             video_att[dev_id].drn,
00452                             video_att[dev_id].buffer);
00453     vlDestroyBuffer(svr, video_att[dev_id].buffer);
00454     if((vlSetupPaths(svr, (VLPathList)&video_att[dev_id].path, 1,
00455                      VL_DONE_USING, VL_DONE_USING)) < 0) error_exit();
00456 
00457     free( video_att[dev_id].t_buf );
00458 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


ar_recog
Author(s): Graylin Trevor Jay and Christopher Crick
autogenerated on Fri Jan 25 2013 12:15:00