00001
00017 #include "vetVideo4Linux.h"
00018 #include "../vetUtility.h"
00019
00020 #include <unistd.h>
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <fcntl.h>
00024 #include <stdio.h>
00025 #include <sys/ioctl.h>
00026 #include <stdlib.h>
00027 #include <linux/types.h>
00028
00035 vetVideo4Linux::vetVideo4Linux(char* device) : vetInput(0)
00036 {
00037 fd = -1;
00038 videoBuffer = NULL;
00039 reset();
00040 connect(device);
00041 }
00042
00046 vetVideo4Linux::~vetVideo4Linux()
00047 {
00048 disconnect();
00049 delete [] videoBuffer;
00050 }
00051
00059 VETRESULT vetVideo4Linux::connect(char* device)
00060 {
00061 if ( device == NULL )
00062 return VETRET_PARAM_ERR;
00063
00064 if ( fd != -1 )
00065 return VETRET_ILLEGAL_USE;
00066
00067
00068 fd = open(device,O_RDONLY);
00069
00070 if (fd < 0)
00071 return VETRET_INTERNAL_ERR;
00072
00073
00074 if (ioctl(fd,VIDIOCGCAP, &cap) < 0) {
00075 close(fd);
00076 return VETRET_INTERNAL_ERR;
00077 }
00078
00079
00080 if (ioctl(fd,VIDIOCGWIN, &win) < 0) {
00081 close(fd);
00082 return VETRET_INTERNAL_ERR;
00083 }
00084
00085
00086 if (ioctl (fd,VIDIOCGPICT, &vpic) < 0) {
00087 close(fd);
00088 return VETRET_INTERNAL_ERR;
00089 }
00090
00091
00092 if (cap.type & VID_TYPE_MONOCHROME)
00093 {
00094 vpic.depth=8;
00095 vpic.palette=VIDEO_PALETTE_GREY;
00096 if(ioctl(fd,VIDIOCSPICT,&vpic) < 0)
00097 {
00098 vpic.depth=6;
00099 if(ioctl(fd,VIDIOCSPICT, &vpic) < 0)
00100 {
00101 vpic.depth=4;
00102 if (ioctl(fd,VIDIOCSPICT, &vpic) <0) {
00103 close(fd);
00104 return VETRET_INTERNAL_ERR;
00105 }
00106 }
00107 }
00108 }
00109 else
00110 {
00111 vpic.depth = 24;
00112 vpic.palette = VIDEO_PALETTE_RGB24;
00113
00114 if (ioctl(fd,VIDIOCSPICT,&vpic) <0)
00115 {
00116 vpic.palette=VIDEO_PALETTE_RGB565;
00117 vpic.depth=16;
00118
00119 if(ioctl(fd,VIDIOCSPICT,&vpic) == -1)
00120 {
00121 vpic.palette = VIDEO_PALETTE_RGB555;
00122 vpic.depth=15;
00123
00124 if(ioctl(fd, VIDIOCSPICT, &vpic) == -1) {
00125 close(fd);
00126 throw "Unable to find a supported Capture format";
00127 }
00128 }
00129 }
00130 }
00131 bpp = vpic.depth;
00132
00133 if (win.width && win.height)
00134 {
00135 if (videoBuffer != NULL)
00136 delete [] videoBuffer;
00137
00138 videoBuffer = (unsigned char*) malloc(win.width * win.height * 3);
00139
00140 if (videoBuffer == NULL)
00141 return VETRET_INTERNAL_ERR;
00142 }
00143
00144 return VETRET_OK;
00145 }
00146
00152 VETRESULT vetVideo4Linux::disconnect()
00153 {
00154 if ( fd==-1 )
00155 return VETRET_ILLEGAL_USE;
00156
00157 close(fd);
00158 fd=-1;
00159 win.height = 0;
00160 win.width = 0;
00161 win.height = 0;
00162 vpic.palette = 0;
00163
00164 return VETRET_OK;
00165 }
00166
00172 VETRESULT vetVideo4Linux::reset()
00173 {
00174 disconnect();
00175
00176 if (videoBuffer != NULL)
00177 delete [] videoBuffer;
00178
00179 videoBuffer = NULL;
00180 fd = -1;
00181 win.height = 0;
00182 win.width = 0;
00183 win.height = 0;
00184 vpic.palette = 0;
00185
00186 return VETRET_OK;
00187 }
00188
00189
00195 bool vetVideo4Linux::EoF()
00196 {
00197 if ( fd == -1 )
00198 return true;
00199
00200 return false;
00201 }
00202
00203
00216 VETRESULT vetVideo4Linux::extractTo(vetFrameYUV420& img)
00217 {
00218 if ( fd == -1 )
00219 return VETRET_ILLEGAL_USE;
00220
00221 if (img.width != win.width || img.height != win.height)
00222 {
00223 img.reAllocCanvas(win.width, win.height);
00224 }
00225
00226 if ( vpic.palette == VIDEO_PALETTE_RGB24 )
00227 {
00228 return VETRET_NOT_IMPLEMENTED;
00229 }
00230 else if ( vpic.palette == VIDEO_PALETTE_RGB565 )
00231 {
00232 return VETRET_NOT_IMPLEMENTED;
00233 }
00234 else if ( vpic.palette == VIDEO_PALETTE_RGB555 )
00235 {
00236 return VETRET_NOT_IMPLEMENTED;
00237 }
00238
00239 return VETRET_OK;
00240 }
00241
00254 VETRESULT vetVideo4Linux::extractTo(vetFrameRGB24& img)
00255 {
00256 if ( fd == -1 )
00257 return VETRET_ILLEGAL_USE;
00258
00259 if (img.width != win.width || img.height != win.height)
00260 {
00261 img.reAllocCanvas(win.width, win.height);
00262 }
00263
00264 if ( vpic.palette == VIDEO_PALETTE_RGB24 )
00265 {
00266 if (videoBuffer == NULL)
00267 return VETRET_INTERNAL_ERR;
00268
00269 read(fd,videoBuffer, win.width * win.height * 3 * sizeof(unsigned char) );
00270 vetUtility::conv_bgr_rgb((unsigned char*)img.data[0], videoBuffer, win.width, win.height);
00271 }
00272 else if ( vpic.palette == VIDEO_PALETTE_RGB565 )
00273 {
00274 return VETRET_NOT_IMPLEMENTED;
00275 }
00276 else if ( vpic.palette == VIDEO_PALETTE_RGB555 )
00277 {
00278 return VETRET_NOT_IMPLEMENTED;
00279 }
00280
00281 return VETRET_OK;
00282 }
00283
00296 VETRESULT vetVideo4Linux::extractTo(vetFrameT<unsigned char>& img)
00297 {
00298 if ( fd == -1 )
00299 return VETRET_ILLEGAL_USE;
00300
00301 if (win.width != img.width || win.height != img.height)
00302 img.reAllocCanvas(win.width, win.height);
00303
00304 if ( vpic.palette == VIDEO_PALETTE_RGB24 && img.profile == vetFrame::VETFRAME_BGR24 )
00305 {
00306 read(fd, img.data, win.width * win.height * 3 * sizeof(unsigned char) );
00307 return VETRET_OK;
00308 }
00309 if ( vpic.palette == VIDEO_PALETTE_RGB24 && img.profile == vetFrame::VETFRAME_RGB24 )
00310 {
00311 if (videoBuffer == NULL)
00312 return VETRET_INTERNAL_ERR;
00313
00314 read(fd,videoBuffer, win.width * win.height * 3 * sizeof(unsigned char) );
00315 vetUtility::conv_bgr_rgb(img.data, videoBuffer, win.width, win.height);
00316
00317 }
00318
00319 return VETRET_NOT_IMPLEMENTED;
00320 }
00321
00322