home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "video.h"
- #include <stdio.h>
- #include <bstring.h>
- #include <sys/param.h>
- #include "convertImageFormat.h"
- #define ERROR fprintf(stderr,"Errno: %d",vlErrno); vlPerror(vlStrError(vlErrno))
-
- #ifdef DEBUG
- #define dprintf printf
- #else
- #define dprintf 0&&
- #endif
-
- Video::Video()
- {
- _hasVideo = FALSE;
- _server = NULL;
- _info = NULL;
- _width = _height = 0;
- _state = -1;
- _mem = 0;
- }
-
- Video::~Video()
- {}
-
- void Video::freeBuffer()
- {
- dprintf("freeBuffer\n");
- if(!_hasVideo)return;
- if(vlEndTransfer(_server, _path) != VLSuccess){
- fprintf(stderr,"Error in end transfer.\n");
- ERROR;
- }
- if(vlDeregisterBuffer(_server, _path, _mem, _buf) != VLSuccess){
- fprintf(stderr,"Error in deregister.\n");
- ERROR;
- }
- if(vlDestroyBuffer(_server, _buf) != VLSuccess){
- fprintf(stderr,"Error in destroy buffer.\n");
- ERROR;
- }
- }
-
- void Video::getBuffer()
- {
- dprintf("getBuffer\n");
- if(!_hasVideo) return;
- _buf = vlCreateBuffer(_server, _path, _mem, 1);
- if(vlRegisterBuffer(_server, _path, _mem, _buf) != VLSuccess){
- fprintf(stderr,"Cannot register buffer.\n");
- ERROR;
- return;
- }
- if(vlBeginTransfer(_server, _path, 0, NULL) != VLSuccess){
- fprintf(stderr,"Error in begin transfer\n");
- ERROR;
- }
- }
-
- VideoOut::VideoOut()
- {
- dprintf("VideoOut\n");
- VLControlValue val;
- if(!(_server = vlOpenVideo(""))){
- return;
- }
- _src = vlGetNode(_server, VL_SRC, VL_MEM, VL_ANY);
- _mem = _src;
- _drn = vlGetNode(_server, VL_DRN, VL_VIDEO, VL_ANY);
- _path = vlCreatePath(_server, VL_ANY, _src, _drn);
- if(vlSetupPaths(_server, (VLPathList)&_path,1,VL_SHARE, VL_SHARE) != VLSuccess){
- return;
- }
- val.intVal = VL_PACKING_RGBA_8;
- if(vlSetControl(_server, _path, _mem, VL_PACKING, &val) != VLSuccess){
- fprintf(stderr,"Cannot set video out to RGBA_8\n");
- ERROR;
- return;
- }
- if(vlGetControl(_server, _path, _mem, VL_SIZE, &val) == VLSuccess){
- _width = val.xyVal.x;
- _height = val.xyVal.y;
- }
- _frameSize = vlGetTransferSize(_server,_path);
- _hasVideo = TRUE;
- }
-
- VideoOut::~VideoOut()
- {
- dprintf("End VideoOut\n");
- if(!_hasVideo) return;
- vlDestroyPath(_server,_path);
- vlCloseVideo(_server);
- }
-
- void VideoOut::loadFrame(int width, int height, void *rgbBuffer)
- {
- unsigned char *pImage, *pFrame, *frameBuffer;
- unsigned char *yuvBuffer, *imageBuffer;
- long i,y;
- int xSize, ySize, xOffset, yOffset, chunkSize;
- dprintf("out loadFrame\n");
- if(!_hasVideo) return;
- if(!_info){
- do
- {
- _info = vlGetNextFree(_server, _buf, _frameSize);
- } while (!_info && !vlBufferDone(_buf));
- }
- frameBuffer = (unsigned char *)vlGetActiveRegion(_server, _buf, _info);
- yuvBuffer = NULL;
- xSize = MIN(_width, width);
- ySize = MIN(_height,height);
- xOffset = (_width-xSize)/2;
- yOffset = (_height-ySize)/2;
- chunkSize = _frameSize / _width / _height;
- yuvBuffer = NULL;
- if(chunkSize == 2){
- yuvBuffer = new unsigned char[width*height*chunkSize];
- csFlipVertically(TRUE);
- RGBXToYUV422(width, height, 0, rgbBuffer, 0, (void *)yuvBuffer);
- imageBuffer = yuvBuffer;
- for(i=0;i<_frameSize; i+=4){
- frameBuffer[i] = 128;
- frameBuffer[i+1] = 16;
- frameBuffer[i+2] = 128;
- frameBuffer[i+3] = 16;
- }
- }else{
- bzero(frameBuffer,_frameSize);
- imageBuffer = (unsigned char *)rgbBuffer;
- }
- for(y=0; y<ySize; y++){
- pImage = imageBuffer + width *( ySize - 1 - y) * chunkSize;
- pFrame = frameBuffer + (_width*(y+yOffset) + xOffset) * chunkSize;
- bcopy(pImage, pFrame, xSize * chunkSize);
- }
- delete [] yuvBuffer;
-
- if(vlPutValid(_server, _buf) != VLSuccess){
- fprintf(stderr,"Error in vlPutValid\n");
- ERROR;
- }
- }
-
- VideoIn::VideoIn(int divisor, int deviceNum, int isRGBmode)
- {
- VLControlValue val;
- dprintf("VideoIn\n");
- if(!(_server = vlOpenVideo(""))){
- return;
- }
- _src = vlGetNode(_server, VL_SRC, VL_VIDEO, VL_ANY);
- _drn = vlGetNode(_server, VL_DRN, VL_MEM, VL_ANY);
- _mem = _drn;
- if(deviceNum == -1){
- _path = vlCreatePath(_server, VL_ANY, _src, _drn);
- }else{
- _path = vlCreatePath(_server, deviceNum, _src, _drn);
- }
- if(vlSetupPaths(_server, (VLPathList)&_path,1,VL_SHARE, VL_SHARE) != VLSuccess){
- return;
- }
- if(isRGBmode){
- val.intVal = VL_PACKING_RGBA_8;
- if(vlSetControl(_server, _path, _mem, VL_PACKING, &val) != VLSuccess){
- fprintf(stderr,"Cannot set video out to RGBA_8\n");
- ERROR;
- return;
- }
- }else{
- val.intVal = VL_PACKING_YVYU_422_8;
- if(vlSetControl(_server, _path, _mem, VL_PACKING, &val) != VLSuccess){
- fprintf(stderr,"Cannot set video out to RGBA_8\n");
- ERROR;
- return;
- }
- }
- if(divisor != 1){
- val.fractVal.numerator = 1;
- val.fractVal.denominator = divisor;
- if(vlSetControl(_server, _path, _mem, VL_ZOOM, &val) != VLSuccess){
- fprintf(stderr,"Cannot set zoom to %d\n",divisor);
- divisor = 1;
- }
- }
- if(vlGetControl(_server, _path, _mem, VL_SIZE, &val) == VLSuccess){
- _width = val.xyVal.x;
- _height = val.xyVal.y;
- }
- _frameSize = vlGetTransferSize(_server,_path);
- _hasVideo = TRUE;
- }
-
- VideoIn::~VideoIn()
- {
- dprintf("end VideoIn\n");
- if(!_hasVideo) return;
- vlDestroyPath(_server,_path);
- vlCloseVideo(_server);
- }
-
- void VideoIn::loadFrame(int width, int height, void *imageBuffer)
- {
- unsigned char *pImage, *pFrame, *frameBuffer;
- unsigned char *image;
- long i,y;
- int xSize, ySize, xOffset, yOffset, chunkSize;
- dprintf("in loadFrame\n");
- if(!_hasVideo) return;
- do
- {
- _info = vlGetNextValid(_server, _buf);
- } while (!_info);
- image = (unsigned char *)imageBuffer;
- frameBuffer = (unsigned char *)vlGetActiveRegion(_server, _buf, _info);
- xSize = MIN(_width, width);
- ySize = MIN(_height,height);
- xOffset = (_width-xSize)/2;
- yOffset = (_height-ySize)/2;
- chunkSize = _frameSize / _width / _height;
- if(chunkSize == 2){
- if(width != _width || height != _height){
- for(i=0;i<width*height*chunkSize;i+=4){
- image[i] = 128;
- image[i+1] = 16;
- image[i+2] = 128;
- image[i+3] = 16;
- }
- }
- }else{
- if(width != _width || height != _height){
- bzero(imageBuffer, width*height*chunkSize);
- }
- }
- for(y=0; y<ySize; y++){
- pImage = image + width * (ySize-1 - y) * chunkSize;
- pFrame = frameBuffer + (_width*(y+yOffset) + xOffset) * chunkSize;
- bcopy(pFrame, pImage, xSize * chunkSize);
- }
- if(vlPutFree(_server, _buf) != VLSuccess){
- fprintf(stderr,"Error in vlPutValid\n");
- ERROR;
- }
- }
-