PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/src-qt4/StandardTypes/Modules/Video/Common/v4ldemos/v4l.c

https://github.com/jeez/iqr
C | 465 lines | 280 code | 137 blank | 48 comment | 27 complexity | 79417dc55dabebebfcfa2fd2e1ef6a55 MD5 | raw file
  1. //---------------------------------------------------------------------------------
  2. // v4l.c - V4L library implementation
  3. //
  4. // Author: Ben Bridgwater
  5. // History:
  6. // 03/07/99 Initial version
  7. // 03/14/99 Added V4LBuildRGB8Palette().
  8. // 03/18/99 Added complete set of V4LRGBMasks.
  9. // 03/18/99 Added RGB to/from YCrCb conversions.
  10. // 03/20/99 Changed V4lMSet/GetFormat to implement capture size restrictions.
  11. // 09/10/00 Fixed V4LYCbCrToRGB(), V4LRGBToYCbCr().
  12. // 09/16/00 Moved RGB<->YCbCr conversion to rgb2yuv module.
  13. // 09/16/00 Moved V4LBuildRGB8Palette to hi240 module.
  14. // 02/03/01 Fixed missing "case" keywords in V4LMSetFormat switch.
  15. //---------------------------------------------------------------------------------
  16. #include <stdio.h> // debug
  17. #include <errno.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/types.h>
  20. #include "v4l.h"
  21. //---------------------------------------------------------------------------------
  22. static V4LFormat _v4lFormat; // Set by V4LMSetFormat, used by V4LMCapture
  23. //---------------------------------------------------------------------------------
  24. const V4LRGBMasks v4lRGB15Masks = { 0x7c00, 0x03e0, 0x001f};
  25. const V4LRGBMasks v4lRGB16Masks = { 0xf800, 0x07e0, 0x001f};
  26. const V4LRGBMasks v4lRGB24Masks = { 0xff0000, 0x00ff00, 0x0000ff}; // B, G, R
  27. const V4LRGBMasks v4lRGB32Masks = {0x00ff0000, 0x0000ff00, 0x000000ff}; // B, G, R, -
  28. //---------------------------------------------------------------------------------
  29. V4LMaskName V4LVideoTypeMaskList[] = {
  30. {VID_TYPE_CAPTURE, "VID_TYPE_CAPTURE"},
  31. {VID_TYPE_TUNER, "VID_TYPE_TUNER"},
  32. {VID_TYPE_TELETEXT, "VID_TYPE_TELETEXT"},
  33. {VID_TYPE_OVERLAY, "VID_TYPE_OVERLAY"},
  34. {VID_TYPE_CHROMAKEY, "VID_TYPE_CHROMAKEY"},
  35. {VID_TYPE_CLIPPING, "VID_TYPE_CLIPPING"},
  36. {VID_TYPE_FRAMERAM, "VID_TYPE_FRAMERAM"},
  37. {VID_TYPE_SCALES, "VID_TYPE_SCALES"},
  38. {VID_TYPE_MONOCHROME, "VID_TYPE_MONOCHROME"},
  39. {VID_TYPE_SUBCAPTURE, "VID_TYPE_SUBCAPTURE"},
  40. {0}
  41. };
  42. V4LMaskName V4LTunerCapabilityMaskList[] = {
  43. {VIDEO_TUNER_PAL, "VIDEO_TUNER_PAL"},
  44. {VIDEO_TUNER_NTSC, "VIDEO_TUNER_NTSC"},
  45. {VIDEO_TUNER_SECAM, "VIDEO_TUNER_SECAM"},
  46. {VIDEO_TUNER_LOW, "VIDEO_TUNER_LOW"},
  47. {VIDEO_TUNER_NORM, "VIDEO_TUNER_NORM"},
  48. {0}
  49. };
  50. //---------------------------------------------------------------------------------
  51. char *V4LChannelTypeName(int channelType)
  52. {
  53. switch (channelType) {
  54. case VIDEO_TYPE_TV: return ("TV");
  55. case VIDEO_TYPE_CAMERA: return ("Camera");
  56. default: return ("?");
  57. } // end switch
  58. } // end V4LChannelTypeName
  59. //---------------------------------------------------------------------------------
  60. char *V4LTunerModeName(int tunerMode)
  61. {
  62. switch (tunerMode) {
  63. case VIDEO_MODE_PAL: return ("PAL");
  64. case VIDEO_MODE_NTSC: return ("NTSC");
  65. case VIDEO_MODE_SECAM: return ("SECAM");
  66. case VIDEO_MODE_AUTO: return ("AUTO");
  67. default: return ("?");
  68. } // end switch
  69. } // end V4LTunerModeName
  70. //---------------------------------------------------------------------------------
  71. char *V4LFormatName(int format)
  72. {
  73. switch (format) {
  74. case VIDEO_PALETTE_GREY: return ("VIDEO_PALETTE_GREY");
  75. case VIDEO_PALETTE_HI240: return ("VIDEO_PALETTE_HI240");
  76. case VIDEO_PALETTE_RGB565: return ("VIDEO_PALETTE_RGB565");
  77. case VIDEO_PALETTE_RGB24: return ("VIDEO_PALETTE_RGB24");
  78. case VIDEO_PALETTE_RGB32: return ("VIDEO_PALETTE_RGB32");
  79. case VIDEO_PALETTE_RGB555: return ("VIDEO_PALETTE_RGB555");
  80. case VIDEO_PALETTE_YUV422: return ("VIDEO_PALETTE_YUV422");
  81. case VIDEO_PALETTE_YUYV: return ("VIDEO_PALETTE_YUYV");
  82. case VIDEO_PALETTE_UYVY: return ("VIDEO_PALETTE_UYVY");
  83. case VIDEO_PALETTE_YUV420: return ("VIDEO_PALETTE_YUV420");
  84. case VIDEO_PALETTE_YUV411: return ("VIDEO_PALETTE_YUV411");
  85. case VIDEO_PALETTE_RAW: return ("VIDEO_PALETTE_RAW");
  86. case VIDEO_PALETTE_YUV422P: return ("VIDEO_PALETTE_YUV422P");
  87. case VIDEO_PALETTE_YUV411P: return ("VIDEO_PALETTE_YUV411P");
  88. case VIDEO_PALETTE_YUV420P: return ("VIDEO_PALETTE_YUV420P");
  89. case VIDEO_PALETTE_YUV410P: return ("VIDEO_PALETTE_YUV410P");
  90. default: return ("?");
  91. } // end switch
  92. } // end V4LFormatName
  93. //---------------------------------------------------------------------------------
  94. int V4LGetCaps(int fd, V4LCaps *v4lCaps)
  95. {
  96. if (ioctl(fd, VIDIOCGCAP, v4lCaps) < 0) {
  97. return (errno);
  98. } // end if
  99. return (0);
  100. } // end V4LGetCaps
  101. //---------------------------------------------------------------------------------
  102. int V4LSetSource(int fd, int channel, int norm)
  103. {
  104. V4LChannel v4lchannel;
  105. v4lchannel.channel = channel;
  106. v4lchannel.norm = norm; // VIDEO_MODE_NTSC, etc
  107. if (ioctl(fd, VIDIOCSCHAN, &v4lchannel) < 0) {
  108. return (errno);
  109. } // end if
  110. return (0);
  111. } // end V4LSetSource
  112. //---------------------------------------------------------------------------------
  113. int V4LGetSource(int fd, int source, V4LChannel *v4lchannel)
  114. {
  115. v4lchannel->channel = source;
  116. if (ioctl(fd, VIDIOCGCHAN, v4lchannel) < 0) {
  117. return (errno);
  118. } // end if
  119. return (0);
  120. } // end V4LGetSource
  121. //---------------------------------------------------------------------------------
  122. int _V4LSetPicture(int fd, V4LPicture *v4lPicture)
  123. {
  124. // Warning: Changes overlay format if bpp matches framebuffer depth bpp
  125. if (ioctl(fd, VIDIOCSPICT, v4lPicture) < 0) {
  126. return (errno);
  127. } // end if
  128. return (0);
  129. } // end _V4LSetPicture
  130. //---------------------------------------------------------------------------------
  131. int _V4LGetPicture(int fd, V4LPicture *v4lPicture)
  132. {
  133. if (ioctl(fd, VIDIOCGPICT, v4lPicture) < 0) {
  134. return (errno);
  135. } // end if
  136. return (0);
  137. } // end _V4LGetPicture
  138. //---------------------------------------------------------------------------------
  139. int V4LSetBrightness(int fd, int brightness)
  140. {
  141. V4LPicture v4lPicture;
  142. if (_V4LGetPicture(fd, &v4lPicture) < 0) {
  143. return (errno);
  144. } // end if
  145. v4lPicture.brightness = brightness;
  146. return (_V4LSetPicture(fd, &v4lPicture));
  147. } // end V4LSetBrightness
  148. //---------------------------------------------------------------------------------
  149. int V4LSetContrast(int fd, int contrast)
  150. {
  151. V4LPicture v4lPicture;
  152. if (_V4LGetPicture(fd, &v4lPicture) < 0) {
  153. return (errno);
  154. } // end if
  155. v4lPicture.contrast = contrast;
  156. return (_V4LSetPicture(fd, &v4lPicture));
  157. } // end V4LSetContrast
  158. //---------------------------------------------------------------------------------
  159. int V4LSetSaturation(int fd, int saturation)
  160. {
  161. V4LPicture v4lPicture;
  162. if (_V4LGetPicture(fd, &v4lPicture) < 0) {
  163. return (errno);
  164. } // end if
  165. v4lPicture.colour = saturation;
  166. return (_V4LSetPicture(fd, &v4lPicture));
  167. } // end V4LSetSaturation
  168. //---------------------------------------------------------------------------------
  169. int V4LSetHue(int fd, int hue)
  170. {
  171. V4LPicture v4lPicture;
  172. if (_V4LGetPicture(fd, &v4lPicture) < 0) {
  173. return (errno);
  174. } // end if
  175. v4lPicture.hue = hue;
  176. return (_V4LSetPicture(fd, &v4lPicture));
  177. } // end V4LSetHue
  178. //---------------------------------------------------------------------------------
  179. int V4LOSetWindow(int fd, V4LWindow *v4lWindow)
  180. {
  181. if (ioctl(fd, VIDIOCSWIN, v4lWindow) < 0) {
  182. return (errno);
  183. } // end if
  184. return (0);
  185. } // end V4LOSetWindow
  186. //---------------------------------------------------------------------------------
  187. int V4LOGetWindow(int fd, V4LWindow *v4lWindow)
  188. {
  189. if (ioctl(fd, VIDIOCGWIN, v4lWindow) < 0) {
  190. return (errno);
  191. } // end if
  192. return (0);
  193. } // end V4LOGetWindow
  194. //---------------------------------------------------------------------------------
  195. int V4LOSetFramebuffer(int fd, V4LFramebuffer *v4lFramebuffer)
  196. {
  197. if (ioctl(fd, VIDIOCSFBUF, v4lFramebuffer) < 0) {
  198. return (errno);
  199. } // end if
  200. return (0);
  201. } // end V4LOSetFramebuffer
  202. //---------------------------------------------------------------------------------
  203. int V4LOGetFramebuffer(int fd, V4LFramebuffer *v4lFramebuffer)
  204. {
  205. if (ioctl(fd, VIDIOCGFBUF, v4lFramebuffer) < 0) {
  206. return (errno);
  207. } // end if
  208. return (0);
  209. } // end V4LOGetFramebuffer
  210. //---------------------------------------------------------------------------------
  211. int V4LOCapture(int fd, int on)
  212. {
  213. int captureOn = 1;
  214. int captureOff = 0;
  215. if (ioctl(fd, VIDIOCCAPTURE, (int *) (on ? &captureOn : &captureOff)) < 0) {
  216. return (errno);
  217. } // end if
  218. return (0);
  219. } // end V4LOCapture
  220. //---------------------------------------------------------------------------------
  221. int V4LMGetMMInfo(int fd, V4LMMInfo *v4lMMInfo)
  222. {
  223. if (ioctl(fd, VIDIOCGMBUF, v4lMMInfo) < 0) {
  224. return (errno);
  225. } // end if
  226. return (0);
  227. } // end V4LMGetMMInfo
  228. //---------------------------------------------------------------------------------
  229. void V4LMSetFormat(V4LFormat *v4lFormat)
  230. {
  231. #if (0)
  232. printf("V4LMSetFormat(%s, %i, %i)\n", V4LFormatName(v4lFormat->format), v4lFormat->width, v4lFormat->height);
  233. #endif
  234. _v4lFormat = *v4lFormat;
  235. switch (_v4lFormat.format) {
  236. case V4L_FORMAT_YUV422P:
  237. case V4L_FORMAT_YUV411P:
  238. case V4L_FORMAT_YUV420P:
  239. case V4L_FORMAT_YUV410P:
  240. _v4lFormat.width = (_v4lFormat.width / 16) * 16;
  241. break;
  242. case V4L_FORMAT_YUY2:
  243. _v4lFormat.width = (_v4lFormat.width / 2) * 2;
  244. break;
  245. case V4L_FORMAT_Y41P:
  246. _v4lFormat.width = (_v4lFormat.width / 8) * 8;
  247. break;
  248. } // end switch
  249. _v4lFormat.width = (_v4lFormat.width / 4) * 4;
  250. _v4lFormat.height = (_v4lFormat.height / 4) * 4;
  251. } // end V4LMSetFormat
  252. //---------------------------------------------------------------------------------
  253. void V4LMGetFormat(V4LFormat *v4lFormat)
  254. {
  255. *v4lFormat = _v4lFormat;
  256. } // end V4LMGetFormat
  257. //---------------------------------------------------------------------------------
  258. int V4LMCapture(int fd, int frame)
  259. {
  260. struct video_mmap videoMMap;
  261. videoMMap.width = _v4lFormat.width;
  262. videoMMap.height = _v4lFormat.height;
  263. videoMMap.format = _v4lFormat.format;
  264. videoMMap.frame = frame;
  265. if (ioctl(fd, VIDIOCMCAPTURE, &videoMMap) < 0) {
  266. return (errno);
  267. } // end if
  268. return (0);
  269. } // end V4LMCapture
  270. //---------------------------------------------------------------------------------
  271. int V4LMSync(int fd, int frame)
  272. {
  273. if (ioctl(fd, VIDIOCSYNC, &frame) < 0) {
  274. return (errno);
  275. } // end if
  276. return (0);
  277. } // end V4LMSync
  278. //---------------------------------------------------------------------------------
  279. int V4LSetTuner(int fd, V4LTuner *v4lTuner)
  280. {
  281. v4lTuner->tuner = 0; // Assume there's only one!
  282. if (ioctl(fd, VIDIOCSTUNER, v4lTuner) < 0) {
  283. return (errno);
  284. } // end if
  285. return (0);
  286. } // end V4LSetTuner
  287. //---------------------------------------------------------------------------------
  288. int V4LGetTuner(int fd, V4LTuner *v4lTuner)
  289. {
  290. v4lTuner->tuner = 0; // Assume there's only one!
  291. if (ioctl(fd, VIDIOCGTUNER, v4lTuner) < 0) {
  292. return (errno);
  293. } // end if
  294. return (0);
  295. } // end V4LGetTuner
  296. //---------------------------------------------------------------------------------
  297. int V4LSetTunerFreq(int fd, ulong tunerFreq)
  298. {
  299. if (ioctl(fd, VIDIOCSFREQ, &tunerFreq) < 0) {
  300. return (errno);
  301. } // end if
  302. return (0);
  303. } // end V4LSetTunerFreq
  304. //---------------------------------------------------------------------------------
  305. int V4LGetTunerFreq(int fd, ulong *tunerFreq)
  306. {
  307. if (ioctl(fd, VIDIOCGFREQ, tunerFreq) < 0) {
  308. return (errno);
  309. } // end if
  310. return (0);
  311. } // end V4LGetTunerFreq
  312. //---------------------------------------------------------------------------------
  313. int V4LSetAudio(int fd, V4LAudio *v4lAudio)
  314. {
  315. if (ioctl(fd, VIDIOCSAUDIO, v4lAudio) < 0) {
  316. return (errno);
  317. } // end if
  318. return (0);
  319. } // end V4LSetAudio
  320. //---------------------------------------------------------------------------------
  321. int V4LGetAudio(int fd, V4LAudio *v4lAudio)
  322. {
  323. if (ioctl(fd, VIDIOCGAUDIO, v4lAudio) < 0) {
  324. return (errno);
  325. } // end if
  326. return (0);
  327. } // end V4LGetAudio
  328. //---------------------------------------------------------------------------------