summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/video4linux/video4linux.cc
blob: f1ad9e286d7b2b8cda1caf839e1822ae2a953e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// video4linux.cc
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Nicolas Schodet
//  Modified by Olivier Gaillard
//
// Robot APB Team/Efrei 2005.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "video4linux.hh"
#include "pwc-ioctl.h"
#include "utils/errno_exception.hh"
#include "utils/fd_set.hh"

#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <stdexcept>
#include <iostream>

static const int framerate = 5;

/// Constructeur.
Video4Linux::Video4Linux (const char *dev, int width, int height,
			  Image::PixelFormat pixelFormat, int brightness /*58000*/)
    : width_ (width), height_ (height), fd_ (-1), pixelFormat_ (pixelFormat),
      rgb_ (pixelFormat == Image::rgb || pixelFormat == Image::bgr),
      brightness_ (brightness), contrast_ (32768)
{
    if (pixelFormat == Image::hsi)
	throw std::invalid_argument ("hsi not supported by Video4Linux");
    open (dev);
}

/// Destructeur.
Video4Linux::~Video4Linux (void)
{
    // Munmap.
    munmap (map_, bufSize_);
    close ();
}

/// Lit une image, lance une exception en cas d'erreur.
void
Video4Linux::read (uint8_t *buf, unsigned size)
{
    static const unsigned nbPixels = 3;
    if (buf && size != width_ * height_ * nbPixels)
	throw std::invalid_argument ("Video4Linux::read");
    static const unsigned widthHeight = width_*height_;
    static const unsigned widthHeightDiv4 = widthHeight/4;
    // Capture.
    ioctl (fd_, VIDIOCMCAPTURE, &mmap_);
    // Sync.
    ioctl (fd_, VIDIOCSYNC, &mmap_.frame);
    // Copy.
    if (buf)
      {
	unsigned char *srcY = reinterpret_cast<unsigned char *> (map_);
	unsigned char *srcY2 = srcY + width_;
	unsigned char *srcU = srcY + widthHeight;
	unsigned char *srcV = srcU + widthHeightDiv4;
	unsigned char *dst = reinterpret_cast<unsigned char *> (buf);
	unsigned char *dst2 = dst + width_*3;
	switch (pixelFormat_)
	  {
	  case Image::rgb:
	    memcpy (buf, map_, size);
	    break;
	  case Image::yuv:
	    for (int i = 0; i < height_/2; ++i)
	      {
		for (int j = 0; j < width_/2; ++j)
		  {
		    *dst++ = *srcY++; *dst2++ = *srcY2++;
		    *dst++ = *srcU; *dst2++ = *srcU;
		    *dst++ = *srcV; *dst2++ = *srcV;
		    *dst++ = *srcY++; *dst2++ = *srcY2++;
		    *dst++ = *srcU; *dst2++ = *srcU;
		    *dst++ = *srcV; *dst2++ = *srcV;
		    srcU++; srcV++;
		  }
		dst += width_*3;
		dst2 += width_*3;
		srcY += width_;
		srcY2 += width_;
	      }
	    break;
	  case Image::bgr:
	  case Image::yuv422:
	  case Image::hsi:
	    memcpy (buf, map_, size);
	    break;
	  }
      }
}

/// Active/d�sactive la calibration automatique de la luminosit�
/*void
Video4Linux::setAdaptive (int a)
{
    if (fd_ != -1)
      {
	if (ioctl(fd_, VIDIOCQCSADAPTIVE, &a) != 0)
	    throw errno_exception ("dev <camera>", errno);
      }
}*/


/// Calibre la cam�ra.
void
Video4Linux::calibrate (void)
{
    for (int i = 0; i < 30; ++i)
	read (0, 0);
}

/// Attend qu'une image soit disponible, retourne true si oui.
bool
Video4Linux::wait (int timeout/*-1*/)
{
    FdSet fds;
    fds.set (fd_);
    return fds.wait (timeout);
}

/// Ouvre le p�riph�rique.
void
Video4Linux::open (const char *dev)
{
    if (fd_ != -1)
	close ();
    fd_ = ::open (dev, O_RDWR);
    if (fd_ == -1)
	throw errno_exception (dev, errno);
    // R�cup�res les infos sur la camera.
    video_window win;
    ioctl (fd_, VIDIOCGWIN, &win);
    win.width = width_;
    win.height = height_;
    // Set new framerate 
    win.flags &= ~PWC_FPS_FRMASK;
    win.flags |= (framerate  << PWC_FPS_SHIFT);
    ioctl (fd_, VIDIOCSWIN, &win);
    ioctl (fd_, VIDIOCGWIN, &win);
    width_ = win.width;
    height_ = win.height;
    std::cout << win.width << "x" << win.height << " " << (win.flags & PWC_FPS_FRMASK  >> PWC_FPS_SHIFT) << "fps" << std::endl;
    //pwc_imagesize is;
    //ioctl (fd_, VIDIOCPWCGREALSIZE, &is);
    //std::cout << is.width << "x" << is.height << std::endl;
    // Initialisation mmap.
    video_mbuf mbuf;
    ioctl (fd_, VIDIOCGMBUF, &mbuf);
    bufSize_ = mbuf.size;
    // On r�cup�re les cap.
    video_capability cap;
    if (ioctl (fd_, VIDIOCGCAP, &cap) == -1)
	throw errno_exception (dev, errno);
    // On veut du RGB24.
    video_picture pic;
    ioctl (fd_, VIDIOCGPICT, &pic);
    pic.palette = VIDEO_PALETTE_YUV420P;
    bpp_ = 1.5;
    pic.brightness = brightness_;
    pic.contrast = contrast_;
    ioctl (fd_, VIDIOCSPICT, &pic);
    // Channel.
    video_channel chn;
    ioctl (fd_, VIDIOCGCHAN, &chn);
    chn.channel = 1;
    chn.norm = VIDEO_MODE_NTSC;
    ioctl (fd_, VIDIOCSCHAN, &chn);
    // Mmap.
    map_ = mmap (0, bufSize_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
    // Init du video_mmap
    mmap_.format = VIDEO_PALETTE_YUV420P; 
    mmap_.frame = 0;
    mmap_.width = width_;
    mmap_.height = height_;
}

/// Ferme le p�riph�rique.
void
Video4Linux::close (void)
{
    ::close (fd_);
}