summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/indi/webcam/v4l1_pwc.cpp
blob: 9f6448856f6b35534c55404e9474497daf710848 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
    Phlips webcam driver for V4L 1
    Copyright (C) 2005 by Jasem Mutlaq

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#include <iostream>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <string.h>

#include "ccvt.h"
#include "pwc-ioctl.h"
#include "v4l1_pwc.h"
#include "../eventloop.h"

#define ERRMSG_SIZ	1024

extern int errno;
using namespace std;

V4L1_PWC::V4L1_PWC()
{
   frameRate=15;
   fd=-1;
   streamActive = true;

   YBuf       = NULL;
   UBuf       = NULL;
   VBuf       = NULL;
   colorBuffer= NULL;
   buffer_start=NULL;
}

V4L1_PWC::~V4L1_PWC()
{

} 

int V4L1_PWC::connectCam(const char * devpath, char *errmsg)
{
   options= (ioNoBlock|ioUseSelect|haveBrightness|haveContrast|haveColor);
   struct pwc_probe probe;
   bool IsPhilips = false;
   frameRate=15;
   fd=-1;
   streamActive = true;
   buffer_start=NULL;
   
   if (-1 == (fd=open(devpath,
                           O_RDONLY | ((options & ioNoBlock) ? O_NONBLOCK : 0)))) {
      
      strncpy(errmsg, strerror(errno), 1024);
      cerr << strerror(errno);
      return -1;
   } 
  
   cerr << "Device opened" << endl;
   
   if (fd != -1) {
      if (-1 == ioctl(fd,VIDIOCGCAP,&capability)) {
         cerr << "Error: ioctl (VIDIOCGCAP)" << endl;
	 strncpy(errmsg, "ioctl (VIDIOCGCAP)", 1024);
	 return -1;
      }
      if (-1 == ioctl (fd, VIDIOCGWIN, &window)) {
         cerr << "Error: ioctl (VIDIOCGWIN)" << endl;
	 strncpy(errmsg, "ioctl (VIDIOCGWIN)", 1024);
	 return -1;
      }
      if (-1 == ioctl (fd, VIDIOCGPICT, &picture_format)) {
         cerr << "Error: ioctl (VIDIOCGPICT)" << endl;
	 strncpy(errmsg, "ioctl (VIDIOCGPICT)", 1024);
	 return -1;
      }
      init(0);
   }
   
  // Check to see if it's really a philips webcam       
  if (ioctl(fd, VIDIOCPWCPROBE, &probe) == 0) 
  {
  	    if (!strcmp(capability.name,probe.name))
	    {
	       IsPhilips = true;
	       type_=probe.type;
            }
 }
	 
  if (IsPhilips)
      cerr << "Philips webcam type " << type_ << " detected" << endl;
  else
  {
   strncpy(errmsg, "No Philips webcam detected.", 1024);
   return -1;
  }
    
   cerr << "initial size w:" << window.width << " -- h: " << window.height << endl;
   
   
   mmapInit();
   
   setWhiteBalanceMode(PWC_WB_AUTO, errmsg);
   multiplicateur_=1;
   skippedFrame_=0;
   lastGain_=getGain();

   cerr << "All successful, returning\n";
   return fd;
}

void V4L1_PWC::checkSize(int & x, int & y) 
{
 if (x>=capability.maxwidth && y >= capability.maxheight)
   {
      x=capability.maxwidth;
      y=capability.maxheight;
   } else if (x>=352 && y >=288 && type_<700) {
      x=352;y=288;
   } else if (x>=320 && y >= 240) {
      x=320;y=240;
   } else if (x>=176 && y >=144 && type_<700 ) {
      x=176;y=144;
   } else if (x>=160 && y >=120 ) {
      x=160;y=120;
   } else {
      x=capability.minwidth;
      y=capability.minheight;
   }
}

bool V4L1_PWC::setSize(int x, int y) 
{

   int oldX, oldY;
   char msg[ERRMSG_SIZ];
   checkSize(x,y);
   
   oldX = window.width;
   oldY = window.height;
   
   window.width=x;
   window.height=y;
   
   if (ioctl (fd, VIDIOCSWIN, &window))
   {
       snprintf(msg, ERRMSG_SIZ, "ioctl(VIDIOCSWIN) %s", strerror(errno));
       cerr << msg << endl;
       window.width=oldX;
       window.height=oldY;
       return false;
   }
   ioctl (fd, VIDIOCGWIN, &window);

   cerr << "New size is x=" << window.width << " " << "y=" << window.height <<endl;
   
   allocBuffers();
   
   return true;
}

int V4L1_PWC::saveSettings(char *errmsg)
{
   if (ioctl(fd, VIDIOCPWCSUSER)==-1)
    {
      snprintf(errmsg, ERRMSG_SIZ, "VIDIOCPWCSUSER %s", strerror(errno));
      return -1;
   }
   
   return 0;
}

void V4L1_PWC::restoreSettings()
 {
   ioctl(fd, VIDIOCPWCRUSER);
   getPictureSettings();
}

void V4L1_PWC::restoreFactorySettings()
{
   ioctl(fd, VIDIOCPWCFACTORY);
   getPictureSettings();
}

int V4L1_PWC::setGain(int val, char *errmsg)
 {
   if(-1==ioctl(fd, VIDIOCPWCSAGC, &val))
   {
      snprintf(errmsg, ERRMSG_SIZ, "VIDIOCPWCSAGC %s", strerror(errno));
      return -1;
   }
   else lastGain_=val;
   
   cerr << "setGain "<<val<<endl;
   
   return lastGain_;
}

int V4L1_PWC::getGain()
{
   int gain;
   char msg[ERRMSG_SIZ];
   static int cpt=0;
   if ((cpt%4)==0)
   {
      if (-1==ioctl(fd, VIDIOCPWCGAGC, &gain))
      {
         //perror("VIDIOCPWCGAGC");
	 snprintf(msg, ERRMSG_SIZ, "VIDIOCPWCGAGC %s", strerror(errno));
	 cerr << msg << endl;
         gain=lastGain_;
      } else
      {
         ++cpt;
         lastGain_=gain;
      }
   } else
   {
      ++cpt;
      gain=lastGain_; 
   }
   //cerr << "get gain "<<gain<<endl;
   if (gain < 0) gain*=-1;
   return gain;
}

int V4L1_PWC::setExposure(int val, char *errmsg)
 {
   //cout << "set exposure "<<val<<"\n";
   
   if (-1==ioctl(fd, VIDIOCPWCSSHUTTER, &val))
   {
     snprintf(errmsg, ERRMSG_SIZ, "VIDIOCPWCSSHUTTER %s", strerror(errno));
     return -1;
  }
  
  return 0;
}

void V4L1_PWC::setCompression(int val)
{
   ioctl(fd, VIDIOCPWCSCQUAL, &val); 
}

int V4L1_PWC::getCompression()
{
   int gain;
   ioctl(fd, VIDIOCPWCGCQUAL , &gain);
   if (gain < 0) gain*=-1;
   return gain;
}

int V4L1_PWC::setNoiseRemoval(int val, char *errmsg)
{
   if (-1 == ioctl(fd, VIDIOCPWCSDYNNOISE, &val))
   {
       snprintf(errmsg, ERRMSG_SIZ, "VIDIOCPWCGDYNNOISE %s", strerror(errno));
       return -1;
   }
   
   return 0;
}

int V4L1_PWC::getNoiseRemoval()
{
   int gain;
   char msg[ERRMSG_SIZ];
   
   if (-1 == ioctl(fd, VIDIOCPWCGDYNNOISE , &gain)) 
   {
   	 snprintf(msg, ERRMSG_SIZ, "VIDIOCPWCGDYNNOISE %s", strerror(errno));
	 cerr << msg << endl;
   }
      
   cout <<"get noise = "<<gain<<endl;
   return gain;
}

int V4L1_PWC::setSharpness(int val, char *errmsg)
 {
   if (-1 == ioctl(fd, VIDIOCPWCSCONTOUR, &val))
   {
       snprintf(errmsg, ERRMSG_SIZ, "VIDIOCPWCSCONTOUR %s", strerror(errno));
       return -1;
  }
  
  return 0;
}

int V4L1_PWC::getSharpness()
{
   int gain;
   char msg[ERRMSG_SIZ];
   
   if (-1 == ioctl(fd, VIDIOCPWCGCONTOUR, &gain)) 
   {
      snprintf(msg, ERRMSG_SIZ, "VIDIOCPWCGCONTOUR %s", strerror(errno));
      cerr << msg << endl;
  }
   
   cout <<"get sharpness = "<<gain<<endl;
   return gain;
}

int V4L1_PWC::setBackLight(bool val, char *errmsg)
{
   static int on=1;
   static int off=0;
   if (-1 == ioctl(fd,VIDIOCPWCSBACKLIGHT, &  val?&on:&off))
   {
        snprintf(errmsg, ERRMSG_SIZ, "VIDIOCPWCSBACKLIGHT %s", strerror(errno));
	return -1;
   }
   
   return 0;
}

bool V4L1_PWC::getBackLight()
{
   int val;
   char msg[ERRMSG_SIZ];
   
   if (-1 == ioctl(fd,VIDIOCPWCGBACKLIGHT, & val)) 
   {
      snprintf(msg, ERRMSG_SIZ, "VIDIOCPWCSBACKLIGHT %s", strerror(errno));
      cerr << msg << endl;
   }

   return val !=0;
}

int V4L1_PWC::setFlicker(bool val, char *errmsg)
{
   static int on=1;
   static int off=0;
   if (-1 == ioctl(fd,VIDIOCPWCSFLICKER, val?&on:&off))
   {
      snprintf(errmsg, ERRMSG_SIZ, "VIDIOCPWCSFLICKER %s", strerror(errno));
      return -1;
   }
   
   return 0;
   
}

bool V4L1_PWC::getFlicker() 
{
   int val;
   char msg[ERRMSG_SIZ];
   
   if (-1 == ioctl(fd,VIDIOCPWCGFLICKER, & val))
   {
         snprintf(msg, ERRMSG_SIZ, "VIDIOCPWCGFLICKER %s", strerror(errno));
	 cerr << msg << endl;
   }
   
   return val !=0;
}

void V4L1_PWC::setGama(int val)
{
   picture_format.whiteness=val;
   setPictureSettings();
}

int V4L1_PWC::getGama() 
{
   return picture_format.whiteness;
}

int V4L1_PWC::setFrameRate(int value, char *errmsg)
 {
   window.flags = (window.flags & ~PWC_FPS_MASK) | ((value << PWC_FPS_SHIFT) & PWC_FPS_MASK);
   if (ioctl(fd, VIDIOCSWIN, &window))
   {
             snprintf(errmsg, ERRMSG_SIZ, "setFrameRate %s", strerror(errno));
	     return -1;
   }
   
   ioctl(fd, VIDIOCGWIN, &window);
   frameRate = value;
   
   return 0;
   //emit exposureTime(multiplicateur_/(double)getFrameRate());
}

int V4L1_PWC::getFrameRate() 
{
   return ((window.flags&PWC_FPS_FRMASK)>>PWC_FPS_SHIFT); 
}

int V4L1_PWC::getWhiteBalance()
{
   char msg[ERRMSG_SIZ];
   struct pwc_whitebalance tmp_whitebalance;
   tmp_whitebalance.mode = tmp_whitebalance.manual_red = tmp_whitebalance.manual_blue = tmp_whitebalance.read_red = tmp_whitebalance.read_blue = PWC_WB_AUTO;
   
   if (ioctl(fd, VIDIOCPWCGAWB, &tmp_whitebalance)) 
   {
           snprintf(msg, ERRMSG_SIZ, "getWhiteBalance %s", strerror(errno));
	   cerr << msg << endl;
   }
   else
   {
#if 0
      cout << "mode="<<tmp_whitebalance.mode
           <<" mr="<<tmp_whitebalance.manual_red
           <<" mb="<<tmp_whitebalance.manual_blue
           <<" ar="<<tmp_whitebalance.read_red
           <<" ab="<<tmp_whitebalance.read_blue
           <<endl;
#endif
      /* manual_red and manual_blue are garbage :-( */
      whiteBalanceMode_=tmp_whitebalance.mode;
   }
            
      return whiteBalanceMode_;
     
      /*switch(whiteBalanceMode_) {
      case PWC_WB_INDOOR:
         setProperty("WhiteBalanceMode","Indor");
         break;
      case PWC_WB_OUTDOOR:
         setProperty("WhiteBalanceMode","Outdoor");
         break;
      case PWC_WB_FL:
         setProperty("WhiteBalanceMode","Neon");
         break;
      case PWC_WB_MANUAL:
          setProperty("WhiteBalanceMode","Manual");
          whiteBalanceRed_=tmp_whitebalance.manual_red;
          whiteBalanceBlue_=tmp_whitebalance.manual_blue;

         break;
      case PWC_WB_AUTO:
         setProperty("WhiteBalanceMode","Auto");
         whiteBalanceRed_=tmp_whitebalance.read_red;
         whiteBalanceBlue_=tmp_whitebalance.read_blue;
         break;
      default:
         setProperty("WhiteBalanceMode","???");
      }
         
      emit whiteBalanceModeChange(whiteBalanceMode_);

      if (((whiteBalanceMode_ == PWC_WB_AUTO) && liveWhiteBalance_)
          || whiteBalanceMode_ != PWC_WB_AUTO) {
         setProperty("WhiteBalanceRed",whiteBalanceRed_);
         emit whiteBalanceRedChange(whiteBalanceRed_);
         setProperty("WhiteBalanceBlue",whiteBalanceBlue_);   
         emit whiteBalanceBlueChange(whiteBalanceBlue_);
         if (guiBuild()) {         
            remoteCTRLWBred_->show();
            remoteCTRLWBblue_->show();
         }
      } else {
         if (guiBuild()) {         
            remoteCTRLWBred_->hide();
            remoteCTRLWBblue_->hide();
         }
      }
   }*/
}

int V4L1_PWC::setWhiteBalance(char *errmsg) 
{
   struct pwc_whitebalance wb;
   wb.mode=whiteBalanceMode_;
   if (wb.mode == PWC_WB_MANUAL)
   {
      wb.manual_red=whiteBalanceRed_;
      wb.manual_blue=whiteBalanceBlue_;
   }
   
   if (ioctl(fd, VIDIOCPWCSAWB, &wb))
   {
       snprintf(errmsg, ERRMSG_SIZ, "setWhiteBalance %s", strerror(errno)); 
       return -1;
   }
   
   return 0;
}

int V4L1_PWC::setWhiteBalanceMode(int val, char *errmsg)
 {
   if (val == whiteBalanceMode_) 
      return whiteBalanceMode_;
      
   if (val != PWC_WB_AUTO)
   {
      if ( val != PWC_WB_MANUAL)
      {
         whiteBalanceMode_=val;
         if (setWhiteBalance(errmsg) < 0)
	   return -1;
      }
      
      //whiteBalanceMode_=PWC_WB_AUTO;
      whiteBalanceMode_= val;
      if (setWhiteBalance(errmsg) < 0)
       return -1;
      getWhiteBalance();
   }

   /*if (guiBuild()) {
      if (val != PWC_WB_AUTO
          || ( liveWhiteBalance_ && (val ==PWC_WB_AUTO))) {
         remoteCTRLWBred_->show();
         remoteCTRLWBblue_->show();
      } else {
         remoteCTRLWBred_->hide();
         remoteCTRLWBblue_->hide();
      }
   }*/
   
   whiteBalanceMode_=val;
   if (setWhiteBalance(errmsg) < 0)
    return -1;
   getWhiteBalance();
   
   return 0;
}

int V4L1_PWC::setWhiteBalanceRed(int val, char *errmsg) 
{
   whiteBalanceMode_ = PWC_WB_MANUAL;
   whiteBalanceRed_=val;
   if (setWhiteBalance(errmsg) < 0)
    return -1;
    
   return 0;
}

int V4L1_PWC::setWhiteBalanceBlue(int val, char *errmsg)
{
   whiteBalanceMode_ = PWC_WB_MANUAL;
   whiteBalanceBlue_=val;
   if (setWhiteBalance(errmsg) < 0)
     return -1;
     
   return 0;
}