home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / video / security / video.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.0 KB  |  263 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "video.h"
  18. #include <stdio.h>
  19. #include <bstring.h>
  20. #include <sys/param.h>
  21. #include "convertImageFormat.h"
  22. #define ERROR fprintf(stderr,"Errno: %d",vlErrno); vlPerror(vlStrError(vlErrno))
  23.  
  24. #ifdef DEBUG
  25. #define dprintf printf
  26. #else
  27. #define dprintf 0&& 
  28. #endif
  29.  
  30. Video::Video()
  31. {
  32.   _hasVideo = FALSE;
  33.   _server = NULL;
  34.   _info = NULL;
  35.   _width = _height = 0;
  36.   _state = -1;
  37.   _mem = 0;
  38. }
  39.  
  40. Video::~Video()
  41. {}
  42.  
  43. void Video::freeBuffer()
  44. {
  45.   dprintf("freeBuffer\n");
  46.   if(!_hasVideo)return;
  47.   if(vlEndTransfer(_server, _path) != VLSuccess){
  48.     fprintf(stderr,"Error in end transfer.\n");
  49.     ERROR;
  50.   }
  51.   if(vlDeregisterBuffer(_server, _path, _mem, _buf) != VLSuccess){
  52.     fprintf(stderr,"Error in deregister.\n");
  53.     ERROR;
  54.   }
  55.   if(vlDestroyBuffer(_server, _buf) != VLSuccess){
  56.     fprintf(stderr,"Error in destroy buffer.\n");
  57.     ERROR;
  58.   }
  59. }
  60.  
  61. void Video::getBuffer()
  62. {
  63.   dprintf("getBuffer\n");
  64.   if(!_hasVideo) return;
  65.   _buf = vlCreateBuffer(_server, _path, _mem, 1);
  66.   if(vlRegisterBuffer(_server, _path, _mem, _buf) != VLSuccess){
  67.     fprintf(stderr,"Cannot register buffer.\n");
  68.     ERROR;
  69.     return;
  70.   }
  71.   if(vlBeginTransfer(_server, _path, 0, NULL) != VLSuccess){
  72.     fprintf(stderr,"Error in begin transfer\n");
  73.     ERROR;
  74.   }
  75. }
  76.  
  77. VideoOut::VideoOut()
  78. {
  79.   dprintf("VideoOut\n");
  80.   VLControlValue val;
  81.   if(!(_server = vlOpenVideo(""))){
  82.     return;
  83.   }
  84.   _src = vlGetNode(_server, VL_SRC, VL_MEM, VL_ANY);
  85.   _mem = _src;
  86.   _drn = vlGetNode(_server, VL_DRN, VL_VIDEO, VL_ANY);
  87.   _path = vlCreatePath(_server, VL_ANY, _src, _drn);
  88.   if(vlSetupPaths(_server, (VLPathList)&_path,1,VL_SHARE, VL_SHARE) != VLSuccess){
  89.     return;
  90.   }
  91.   val.intVal = VL_PACKING_RGBA_8;
  92.   if(vlSetControl(_server, _path, _mem, VL_PACKING, &val) != VLSuccess){
  93.     fprintf(stderr,"Cannot set video out to RGBA_8\n");
  94.     ERROR;
  95.     return;
  96.   }
  97.   if(vlGetControl(_server, _path, _mem, VL_SIZE, &val) == VLSuccess){
  98.     _width = val.xyVal.x;
  99.     _height = val.xyVal.y;
  100.   }
  101.   _frameSize = vlGetTransferSize(_server,_path);
  102.   _hasVideo = TRUE;
  103. }
  104.  
  105. VideoOut::~VideoOut()
  106. {
  107.   dprintf("End VideoOut\n");
  108.   if(!_hasVideo) return;
  109.   vlDestroyPath(_server,_path);
  110.   vlCloseVideo(_server);
  111. }
  112.  
  113. void VideoOut::loadFrame(int width, int height, void *rgbBuffer)
  114. {
  115.   unsigned char *pImage, *pFrame, *frameBuffer;
  116.   unsigned char *yuvBuffer, *imageBuffer;
  117.   long i,y;
  118.   int xSize, ySize, xOffset, yOffset, chunkSize;
  119.   dprintf("out loadFrame\n");
  120.   if(!_hasVideo) return;
  121.   if(!_info){
  122.     do
  123.     {
  124.       _info = vlGetNextFree(_server, _buf, _frameSize);
  125.     } while (!_info && !vlBufferDone(_buf));
  126.   }
  127.   frameBuffer = (unsigned char *)vlGetActiveRegion(_server, _buf, _info);
  128.   yuvBuffer = NULL;
  129.   xSize = MIN(_width, width);
  130.   ySize = MIN(_height,height);
  131.   xOffset = (_width-xSize)/2;
  132.   yOffset = (_height-ySize)/2;
  133.   chunkSize = _frameSize / _width / _height;
  134.   yuvBuffer = NULL;
  135.   if(chunkSize == 2){
  136.     yuvBuffer = new unsigned char[width*height*chunkSize];
  137.     csFlipVertically(TRUE);
  138.     RGBXToYUV422(width, height, 0, rgbBuffer, 0, (void *)yuvBuffer);
  139.     imageBuffer = yuvBuffer;
  140.     for(i=0;i<_frameSize; i+=4){
  141.       frameBuffer[i] = 128;
  142.       frameBuffer[i+1] = 16;
  143.       frameBuffer[i+2] = 128;
  144.       frameBuffer[i+3] = 16;
  145.     }
  146.   }else{
  147.     bzero(frameBuffer,_frameSize);
  148.     imageBuffer = (unsigned char *)rgbBuffer;
  149.   }
  150.   for(y=0; y<ySize; y++){
  151.     pImage = imageBuffer + width *( ySize - 1 - y) * chunkSize;
  152.     pFrame = frameBuffer + (_width*(y+yOffset) + xOffset) * chunkSize;
  153.     bcopy(pImage, pFrame, xSize * chunkSize);
  154.   }
  155.   delete [] yuvBuffer;
  156.  
  157.   if(vlPutValid(_server, _buf) != VLSuccess){
  158.     fprintf(stderr,"Error in vlPutValid\n");
  159.     ERROR;
  160.   }
  161. }
  162.  
  163. VideoIn::VideoIn(int divisor, int deviceNum, int isRGBmode)
  164. {
  165.   VLControlValue val;
  166.   dprintf("VideoIn\n");
  167.   if(!(_server = vlOpenVideo(""))){
  168.     return;
  169.   }
  170.   _src = vlGetNode(_server, VL_SRC, VL_VIDEO, VL_ANY);
  171.   _drn = vlGetNode(_server, VL_DRN, VL_MEM, VL_ANY);
  172.   _mem = _drn;
  173.   if(deviceNum == -1){
  174.     _path = vlCreatePath(_server, VL_ANY, _src, _drn);
  175.   }else{
  176.     _path = vlCreatePath(_server, deviceNum, _src, _drn);
  177.   }
  178.   if(vlSetupPaths(_server, (VLPathList)&_path,1,VL_SHARE, VL_SHARE) != VLSuccess){
  179.     return;
  180.   }
  181.   if(isRGBmode){
  182.     val.intVal = VL_PACKING_RGBA_8;
  183.     if(vlSetControl(_server, _path, _mem, VL_PACKING, &val) != VLSuccess){
  184.       fprintf(stderr,"Cannot set video out to RGBA_8\n");
  185.       ERROR;
  186.       return;
  187.     }
  188.   }else{
  189.     val.intVal = VL_PACKING_YVYU_422_8;
  190.     if(vlSetControl(_server, _path, _mem, VL_PACKING, &val) != VLSuccess){
  191.       fprintf(stderr,"Cannot set video out to RGBA_8\n");
  192.       ERROR;
  193.       return;
  194.     }
  195.   }    
  196.   if(divisor != 1){
  197.     val.fractVal.numerator = 1;
  198.     val.fractVal.denominator = divisor;
  199.     if(vlSetControl(_server, _path, _mem, VL_ZOOM, &val) != VLSuccess){
  200.       fprintf(stderr,"Cannot set zoom to %d\n",divisor);
  201.       divisor = 1;
  202.     }
  203.   }
  204.   if(vlGetControl(_server, _path, _mem, VL_SIZE, &val) == VLSuccess){
  205.     _width = val.xyVal.x;
  206.     _height = val.xyVal.y;
  207.   }
  208.   _frameSize = vlGetTransferSize(_server,_path);
  209.   _hasVideo = TRUE;
  210. }
  211.  
  212. VideoIn::~VideoIn()
  213. {
  214.   dprintf("end VideoIn\n");
  215.   if(!_hasVideo) return;
  216.   vlDestroyPath(_server,_path);
  217.   vlCloseVideo(_server);
  218. }
  219.  
  220. void VideoIn::loadFrame(int width, int height, void *imageBuffer)
  221. {
  222.   unsigned char *pImage, *pFrame, *frameBuffer;
  223.   unsigned char *image;
  224.   long i,y;
  225.   int xSize, ySize, xOffset, yOffset, chunkSize;
  226.   dprintf("in loadFrame\n");
  227.   if(!_hasVideo) return;
  228.   do
  229.   {
  230.     _info = vlGetNextValid(_server, _buf);
  231.   } while (!_info);
  232.   image = (unsigned char *)imageBuffer;
  233.   frameBuffer = (unsigned char *)vlGetActiveRegion(_server, _buf, _info);
  234.   xSize = MIN(_width, width);
  235.   ySize = MIN(_height,height);
  236.   xOffset = (_width-xSize)/2;
  237.   yOffset = (_height-ySize)/2;
  238.   chunkSize = _frameSize / _width / _height;
  239.   if(chunkSize == 2){
  240.     if(width != _width || height != _height){
  241.       for(i=0;i<width*height*chunkSize;i+=4){
  242.     image[i] = 128;
  243.     image[i+1] = 16;
  244.     image[i+2] = 128;
  245.     image[i+3] = 16;
  246.       }
  247.     }
  248.   }else{
  249.     if(width != _width || height != _height){
  250.       bzero(imageBuffer, width*height*chunkSize);
  251.     }
  252.   }
  253.   for(y=0; y<ySize; y++){
  254.     pImage = image + width * (ySize-1 - y) * chunkSize;
  255.     pFrame = frameBuffer + (_width*(y+yOffset) + xOffset) * chunkSize;
  256.     bcopy(pFrame, pImage, xSize * chunkSize);
  257.   }
  258.   if(vlPutFree(_server, _buf) != VLSuccess){
  259.     fprintf(stderr,"Error in vlPutValid\n");
  260.     ERROR;
  261.   }
  262. }
  263.