OpenVideo Documentation

   Main Page       Modules       Class Hierarchy       Alphabetical List       Compound List       File List       Compound Members       Related Pages   

v4l.c

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------------
00002 // v4l.c - V4L library implementation
00003 //
00004 // Author:  Ben Bridgwater
00005 // History:
00006 // 03/07/99 Initial version
00007 // 03/14/99 Added V4LBuildRGB8Palette().
00008 // 03/18/99 Added complete set of V4LRGBMasks.
00009 // 03/18/99 Added RGB to/from YCrCb conversions.
00010 // 03/20/99 Changed V4lMSet/GetFormat to implement capture size restrictions.
00011 // 09/10/00 Fixed V4LYCbCrToRGB(), V4LRGBToYCbCr().
00012 // 09/16/00 Moved RGB<->YCbCr conversion to rgb2yuv module.
00013 // 09/16/00 Moved V4LBuildRGB8Palette to hi240 module.
00014 // 02/03/01 Fixed missing "case" keywords in V4LMSetFormat switch.
00015 //---------------------------------------------------------------------------------
00016 
00017 #include <stdio.h> // debug
00018 
00019 #include <errno.h>
00020 #include <sys/ioctl.h>
00021 #include <sys/types.h>
00022 
00023 #include <openvideo/nodes/v4l.h>
00024 
00025 //---------------------------------------------------------------------------------
00026 
00027 static V4LFormat _v4lFormat; // Set by V4LMSetFormat, used by V4LMCapture
00028 
00029 //---------------------------------------------------------------------------------
00030 
00031 const V4LRGBMasks v4lRGB15Masks = {    0x7c00,     0x03e0,     0x001f};
00032 const V4LRGBMasks v4lRGB16Masks = {    0xf800,     0x07e0,     0x001f};
00033 const V4LRGBMasks v4lRGB24Masks = {  0xff0000,   0x00ff00,   0x0000ff}; // B, G, R
00034 const V4LRGBMasks v4lRGB32Masks = {0x00ff0000, 0x0000ff00, 0x000000ff}; // B, G, R, -
00035 
00036 //---------------------------------------------------------------------------------
00037 
00038 V4LMaskName V4LVideoTypeMaskList[] = {
00039   {VID_TYPE_CAPTURE,    "VID_TYPE_CAPTURE"},
00040   {VID_TYPE_TUNER,      "VID_TYPE_TUNER"},
00041   {VID_TYPE_TELETEXT,   "VID_TYPE_TELETEXT"},
00042   {VID_TYPE_OVERLAY,    "VID_TYPE_OVERLAY"},
00043   {VID_TYPE_CHROMAKEY,  "VID_TYPE_CHROMAKEY"},
00044   {VID_TYPE_CLIPPING,   "VID_TYPE_CLIPPING"},
00045   {VID_TYPE_FRAMERAM,   "VID_TYPE_FRAMERAM"},
00046   {VID_TYPE_SCALES,     "VID_TYPE_SCALES"},
00047   {VID_TYPE_MONOCHROME, "VID_TYPE_MONOCHROME"},
00048   {VID_TYPE_SUBCAPTURE, "VID_TYPE_SUBCAPTURE"},
00049   {0}
00050 };
00051 
00052 V4LMaskName V4LTunerCapabilityMaskList[] = {
00053   {VIDEO_TUNER_PAL,     "VIDEO_TUNER_PAL"},
00054   {VIDEO_TUNER_NTSC,    "VIDEO_TUNER_NTSC"},
00055   {VIDEO_TUNER_SECAM,   "VIDEO_TUNER_SECAM"},
00056   {VIDEO_TUNER_LOW,     "VIDEO_TUNER_LOW"},
00057   {VIDEO_TUNER_NORM,    "VIDEO_TUNER_NORM"},
00058   {0}
00059 };
00060 
00061 //---------------------------------------------------------------------------------
00062 
00063 char *V4LChannelTypeName(int channelType)
00064 {
00065   switch (channelType) {
00066   case VIDEO_TYPE_TV:     return ("TV");
00067   case VIDEO_TYPE_CAMERA: return ("Camera");
00068   default:                return ("?");
00069   } // end switch
00070 
00071 } // end V4LChannelTypeName
00072 
00073 //---------------------------------------------------------------------------------
00074 
00075 char *V4LTunerModeName(int tunerMode)
00076 {
00077   switch (tunerMode) {
00078   case VIDEO_MODE_PAL:   return ("PAL");
00079   case VIDEO_MODE_NTSC:  return ("NTSC");
00080   case VIDEO_MODE_SECAM: return ("SECAM");
00081   case VIDEO_MODE_AUTO:  return ("AUTO");
00082   default:               return ("?");
00083   } // end switch
00084 
00085 } // end V4LTunerModeName
00086 
00087 //---------------------------------------------------------------------------------
00088 
00089 char *V4LFormatName(int format)
00090 {
00091   switch (format) {
00092   case VIDEO_PALETTE_GREY:    return ("VIDEO_PALETTE_GREY");
00093   case VIDEO_PALETTE_HI240:   return ("VIDEO_PALETTE_HI240");
00094   case VIDEO_PALETTE_RGB565:  return ("VIDEO_PALETTE_RGB565");
00095   case VIDEO_PALETTE_RGB24:   return ("VIDEO_PALETTE_RGB24");
00096   case VIDEO_PALETTE_RGB32:   return ("VIDEO_PALETTE_RGB32");
00097   case VIDEO_PALETTE_RGB555:  return ("VIDEO_PALETTE_RGB555");
00098   case VIDEO_PALETTE_YUV422:  return ("VIDEO_PALETTE_YUV422");
00099   case VIDEO_PALETTE_YUYV:    return ("VIDEO_PALETTE_YUYV");
00100   case VIDEO_PALETTE_UYVY:    return ("VIDEO_PALETTE_UYVY");
00101   case VIDEO_PALETTE_YUV420:  return ("VIDEO_PALETTE_YUV420");
00102   case VIDEO_PALETTE_YUV411:  return ("VIDEO_PALETTE_YUV411");
00103   case VIDEO_PALETTE_RAW:     return ("VIDEO_PALETTE_RAW");
00104   case VIDEO_PALETTE_YUV422P: return ("VIDEO_PALETTE_YUV422P");
00105   case VIDEO_PALETTE_YUV411P: return ("VIDEO_PALETTE_YUV411P");
00106   case VIDEO_PALETTE_YUV420P: return ("VIDEO_PALETTE_YUV420P");
00107   case VIDEO_PALETTE_YUV410P: return ("VIDEO_PALETTE_YUV410P");
00108   default:                    return ("?");
00109   } // end switch
00110 
00111 } // end V4LFormatName
00112 
00113 //---------------------------------------------------------------------------------
00114 
00115 int V4LGetCaps(int fd, V4LCaps *v4lCaps)
00116 {
00117   if (ioctl(fd, VIDIOCGCAP, v4lCaps) < 0) {
00118     return (errno);
00119   } // end if
00120 
00121   return (0);
00122 
00123 } // end V4LGetCaps
00124 
00125 //---------------------------------------------------------------------------------
00126 
00127 int V4LSetSource(int fd, int channel, int norm)
00128 {
00129   V4LChannel v4lchannel;
00130 
00131   v4lchannel.channel = channel;
00132   v4lchannel.norm = norm; // VIDEO_MODE_NTSC, etc
00133 
00134   if (ioctl(fd, VIDIOCSCHAN, &v4lchannel) < 0) {
00135     return (errno);
00136   } // end if
00137 
00138   return (0);
00139 
00140 } // end V4LSetSource
00141 
00142 //---------------------------------------------------------------------------------
00143 
00144 int V4LGetSource(int fd, int source, V4LChannel *v4lchannel)
00145 {
00146   v4lchannel->channel = source;
00147 
00148   if (ioctl(fd, VIDIOCGCHAN, v4lchannel) < 0) {
00149     return (errno);
00150   } // end if
00151 
00152   return (0);
00153 
00154 } // end V4LGetSource
00155 
00156 //---------------------------------------------------------------------------------
00157 
00158 int _V4LSetPicture(int fd, V4LPicture *v4lPicture)
00159 {
00160   // Warning: Changes overlay format if bpp matches framebuffer depth bpp
00161 
00162   if (ioctl(fd, VIDIOCSPICT, v4lPicture) < 0) {
00163     return (errno);
00164   } // end if
00165 
00166   return (0);
00167 
00168 } // end _V4LSetPicture
00169 
00170 //---------------------------------------------------------------------------------
00171 
00172 int _V4LGetPicture(int fd, V4LPicture *v4lPicture)
00173 {
00174   if (ioctl(fd, VIDIOCGPICT, v4lPicture) < 0) {
00175     return (errno);
00176   } // end if
00177 
00178   return (0);
00179 
00180 } // end _V4LGetPicture
00181 
00182 //---------------------------------------------------------------------------------
00183 
00184 int V4LSetBrightness(int fd, int brightness)
00185 {
00186   V4LPicture v4lPicture;
00187 
00188   if (_V4LGetPicture(fd, &v4lPicture) < 0) {
00189     return (errno);
00190   } // end if
00191 
00192   v4lPicture.brightness = brightness;
00193 
00194   return (_V4LSetPicture(fd, &v4lPicture));
00195 
00196 } // end V4LSetBrightness
00197 
00198 //---------------------------------------------------------------------------------
00199 
00200 int V4LSetContrast(int fd, int contrast)
00201 {
00202   V4LPicture v4lPicture;
00203 
00204   if (_V4LGetPicture(fd, &v4lPicture) < 0) {
00205     return (errno);
00206   } // end if
00207 
00208   v4lPicture.contrast = contrast;
00209 
00210   return (_V4LSetPicture(fd, &v4lPicture));
00211 
00212 } // end V4LSetContrast
00213 
00214 //---------------------------------------------------------------------------------
00215 
00216 int V4LSetSaturation(int fd, int saturation)
00217 {
00218   V4LPicture v4lPicture;
00219 
00220   if (_V4LGetPicture(fd, &v4lPicture) < 0) {
00221     return (errno);
00222   } // end if
00223 
00224   v4lPicture.colour = saturation;
00225 
00226   return (_V4LSetPicture(fd, &v4lPicture));
00227 
00228 } // end V4LSetSaturation
00229 
00230 //---------------------------------------------------------------------------------
00231 
00232 int V4LSetHue(int fd, int hue)
00233 {
00234   V4LPicture v4lPicture;
00235 
00236   if (_V4LGetPicture(fd, &v4lPicture) < 0) {
00237     return (errno);
00238   } // end if
00239 
00240   v4lPicture.hue = hue;
00241 
00242   return (_V4LSetPicture(fd, &v4lPicture));
00243 
00244 } // end V4LSetHue
00245 
00246 //---------------------------------------------------------------------------------
00247 
00248 int V4LOSetWindow(int fd, V4LWindow *v4lWindow)
00249 {
00250   if (ioctl(fd, VIDIOCSWIN, v4lWindow) < 0) {
00251     return (errno);
00252   } // end if
00253 
00254   return (0);
00255 
00256 } // end V4LOSetWindow
00257 
00258 //---------------------------------------------------------------------------------
00259 
00260 int V4LOGetWindow(int fd, V4LWindow *v4lWindow)
00261 {
00262   if (ioctl(fd, VIDIOCGWIN, v4lWindow) < 0) {
00263     return (errno);
00264   } // end if
00265 
00266   return (0);
00267 
00268 } // end V4LOGetWindow
00269 
00270 //---------------------------------------------------------------------------------
00271 
00272 int V4LOSetFramebuffer(int fd, V4LFramebuffer *v4lFramebuffer)
00273 {
00274   if (ioctl(fd, VIDIOCSFBUF, v4lFramebuffer) < 0) {
00275     return (errno);
00276   } // end if
00277 
00278   return (0);
00279 
00280 } // end V4LOSetFramebuffer
00281 
00282 //---------------------------------------------------------------------------------
00283 
00284 int V4LOGetFramebuffer(int fd, V4LFramebuffer *v4lFramebuffer)
00285 {
00286   if (ioctl(fd, VIDIOCGFBUF, v4lFramebuffer) < 0) {
00287     return (errno);
00288   } // end if
00289 
00290   return (0);
00291 
00292 } // end V4LOGetFramebuffer
00293 
00294 //---------------------------------------------------------------------------------
00295 
00296 int V4LOCapture(int fd, int on)
00297 {
00298   int captureOn  = 1;
00299   int captureOff = 0;
00300 
00301   if (ioctl(fd, VIDIOCCAPTURE, (int *) (on ? &captureOn : &captureOff)) < 0) {
00302     return (errno);
00303   } // end if
00304 
00305   return (0);
00306 
00307 } // end V4LOCapture
00308 
00309 //---------------------------------------------------------------------------------
00310 
00311 int V4LMGetMMInfo(int fd, V4LMMInfo *v4lMMInfo)
00312 {
00313   if (ioctl(fd, VIDIOCGMBUF, v4lMMInfo) < 0) {
00314     return (errno);
00315   } // end if
00316 
00317   return (0);
00318 
00319 } // end V4LMGetMMInfo
00320 
00321 //---------------------------------------------------------------------------------
00322 
00323 void V4LMSetFormat(V4LFormat *v4lFormat)
00324 {
00325 #if (0)
00326   printf("V4LMSetFormat(%s, %i, %i)\n", V4LFormatName(v4lFormat->format), v4lFormat->width, v4lFormat->height);
00327 #endif
00328   _v4lFormat = *v4lFormat;
00329 
00330   switch (_v4lFormat.format) {
00331   case V4L_FORMAT_YUV422P:
00332   case V4L_FORMAT_YUV411P:
00333   case V4L_FORMAT_YUV420P:
00334   case V4L_FORMAT_YUV410P:
00335     _v4lFormat.width = (_v4lFormat.width / 16) * 16;
00336     break;
00337   case V4L_FORMAT_YUY2:
00338     _v4lFormat.width = (_v4lFormat.width / 2) * 2;
00339     break;
00340   case V4L_FORMAT_Y41P:
00341     _v4lFormat.width = (_v4lFormat.width / 8) * 8;
00342     break;
00343   } // end switch
00344 
00345   _v4lFormat.width  = (_v4lFormat.width  / 4) * 4;
00346   _v4lFormat.height = (_v4lFormat.height / 4) * 4;
00347 
00348 } // end V4LMSetFormat
00349 
00350 //---------------------------------------------------------------------------------
00351 
00352 void V4LMGetFormat(V4LFormat *v4lFormat)
00353 {
00354   *v4lFormat = _v4lFormat;
00355 
00356 } // end V4LMGetFormat
00357 
00358 //---------------------------------------------------------------------------------
00359 
00360 int V4LMCapture(int fd, int frame)
00361 {
00362   struct video_mmap videoMMap;
00363 
00364   videoMMap.width  = _v4lFormat.width;
00365   videoMMap.height = _v4lFormat.height;
00366   videoMMap.format = _v4lFormat.format;
00367   videoMMap.frame  = frame;
00368 
00369   if (ioctl(fd, VIDIOCMCAPTURE, &videoMMap) < 0) {
00370     return (errno);
00371   } // end if
00372 
00373   return (0);
00374 
00375 } // end V4LMCapture
00376 
00377 //---------------------------------------------------------------------------------
00378 
00379 int V4LMSync(int fd, int frame)
00380 {
00381   if (ioctl(fd, VIDIOCSYNC, &frame) < 0) {
00382     return (errno);
00383   } // end if
00384 
00385   return (0);
00386 
00387 } // end V4LMSync
00388 
00389 //---------------------------------------------------------------------------------
00390 
00391 int V4LSetTuner(int fd, V4LTuner *v4lTuner)
00392 {
00393   v4lTuner->tuner = 0; // Assume there's only one!
00394 
00395   if (ioctl(fd, VIDIOCSTUNER, v4lTuner) < 0) {
00396     return (errno);
00397   } // end if
00398 
00399   return (0);
00400 
00401 } // end V4LSetTuner
00402 
00403 //---------------------------------------------------------------------------------
00404 
00405 int V4LGetTuner(int fd, V4LTuner *v4lTuner)
00406 {
00407   v4lTuner->tuner = 0; // Assume there's only one!
00408 
00409   if (ioctl(fd, VIDIOCGTUNER, v4lTuner) < 0) {
00410     return (errno);
00411   } // end if
00412 
00413   return (0);
00414 
00415 } // end V4LGetTuner
00416 
00417 //---------------------------------------------------------------------------------
00418 
00419 int V4LSetTunerFreq(int fd, ulong tunerFreq)
00420 {
00421   if (ioctl(fd, VIDIOCSFREQ, &tunerFreq) < 0) {
00422     return (errno);
00423   } // end if
00424 
00425   return (0);
00426 
00427 } // end V4LSetTunerFreq
00428 
00429 //---------------------------------------------------------------------------------
00430 
00431 int V4LGetTunerFreq(int fd, ulong *tunerFreq)
00432 {
00433   if (ioctl(fd, VIDIOCGFREQ, tunerFreq) < 0) {
00434     return (errno);
00435   } // end if
00436 
00437   return (0);
00438 
00439 } // end V4LGetTunerFreq
00440 
00441 //---------------------------------------------------------------------------------
00442 
00443 int V4LSetAudio(int fd, V4LAudio *v4lAudio)
00444 {
00445   if (ioctl(fd, VIDIOCSAUDIO, v4lAudio) < 0) {
00446     return (errno);
00447   } // end if
00448 
00449   return (0);
00450 
00451 } // end V4LSetAudio
00452 
00453 //---------------------------------------------------------------------------------
00454 
00455 int V4LGetAudio(int fd, V4LAudio *v4lAudio)
00456 {
00457   if (ioctl(fd, VIDIOCGAUDIO, v4lAudio) < 0) {
00458     return (errno);
00459   } // end if
00460 
00461   return (0);
00462 
00463 } // end V4LGetAudio
00464 
00465 //---------------------------------------------------------------------------------
 This page was generated at Wed May 31 13:04:16 2006 for OpenVideo by Doxygen.
 If you have any comments, please send a message to schmalstieg@icg.tu-graz.ac.at.
www.studierstube.org