patch-modules_highgui_src_cap_v4l.cpp

Patrick Welche, 2012-07-12 06:38 pm

Download (12.3 kB)

 
1
$NetBSD$
2
3
--- modules/highgui/src/cap_v4l.cpp.orig        2012-05-31 14:06:31.000000000 +0000
4
+++ modules/highgui/src/cap_v4l.cpp
5
@@ -202,7 +202,7 @@ make & enjoy!
6
 
7
 #include "precomp.hpp"
8
 
9
-#if !defined WIN32 && defined HAVE_CAMV4L && defined HAVE_CAMV4L2
10
+#if !defined WIN32 && (defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO)
11
 
12
 #define CLEAR(x) memset (&(x), 0, sizeof (x))
13
 
14
@@ -214,19 +214,26 @@ make & enjoy!
15
 #include <sys/types.h>
16
 #include <sys/mman.h>
17
 
18
+#ifdef HAVE_CAMVAL
19
 #include <linux/videodev.h>
20
+#endif
21
 
22
 #include <string.h>
23
 #include <stdlib.h>
24
-#include <asm/types.h>          /* for videodev2.h */
25
 #include <assert.h>
26
 #include <sys/stat.h>
27
 #include <sys/ioctl.h>
28
 
29
 #ifdef HAVE_CAMV4L2
30
+#include <asm/types.h>          /* for videodev2.h */
31
 #include <linux/videodev2.h>
32
 #endif
33
 
34
+#ifdef HAVE_VIDEOIO
35
+#include <sys/videoio.h>
36
+#define HAVE_CAMV4L2 /* XXXPW unclean! */
37
+#endif
38
+
39
 /* Defaults - If your board can do better, set it here.  Set for the most common type inputs. */
40
 #define DEFAULT_V4L_WIDTH  640
41
 #define DEFAULT_V4L_HEIGHT 480
42
@@ -288,11 +295,13 @@ typedef struct CvCaptureCAM_V4L
43
     int deviceHandle;
44
     int bufferIndex;
45
     int FirstCapture;
46
+#ifdef HAVE_CAMV4L
47
     struct video_capability capability;
48
     struct video_window     captureWindow;
49
     struct video_picture    imageProperties;
50
     struct video_mbuf       memoryBuffer;
51
     struct video_mmap       *mmaps;
52
+#endif /* HAVE_CAMV4L */
53
     char *memoryMap;
54
     IplImage frame;
55
 
56
@@ -345,9 +354,6 @@ static int icvSetVideoSize( CvCaptureCAM
57
 static int numCameras = 0;
58
 static int indexList = 0;
59
 
60
-#ifdef HAVE_CAMV4L2
61
-
62
-// IOCTL handling for V4L2
63
 static int xioctl( int fd, int request, void *arg)
64
 {
65
 
66
@@ -361,8 +367,6 @@ static int xioctl( int fd, int request, 
67
 
68
 }
69
 
70
-#endif /* HAVE_CAMV4L2 */
71
-
72
 /* Simple test program: Find number of Video Sources available.
73
    Start from 0 and go to MAX_CAMERAS while checking for the device with that name.
74
    If it fails on the first attempt of /dev/video0, then check if /dev/video is valid.
75
@@ -393,6 +397,8 @@ static void icvInitCapture_V4L() {
76
 
77
 }; /* End icvInitCapture_V4L */
78
 
79
+#ifdef HAVE_CAMV4L
80
+
81
 static int
82
 try_palette(int fd,
83
             struct video_picture *cam_pic,
84
@@ -410,6 +416,8 @@ try_palette(int fd,
85
   return 0;
86
 }
87
 
88
+#endif /* HAVE_CAMV4L */
89
+
90
 #ifdef HAVE_CAMV4L2
91
 
92
 static int try_palette_v4l2(CvCaptureCAM_V4L* capture, unsigned long colorspace)
93
@@ -434,6 +442,8 @@ static int try_palette_v4l2(CvCaptureCAM
94
 
95
 #endif /* HAVE_CAMV4L2 */
96
 
97
+#ifdef HAVE_CAMV4L
98
+
99
 static int try_init_v4l(CvCaptureCAM_V4L* capture, char *deviceName)
100
 {
101
 
102
@@ -448,7 +458,8 @@ static int try_init_v4l(CvCaptureCAM_V4L
103
   /* Test using an open to see if this new device name really does exists. */
104
   /* No matter what the name - it still must be opened! */
105
   capture->deviceHandle = open(deviceName, O_RDWR);
106
-
107
+  if (capture->deviceHandle == -1)
108
+    perror(deviceName);
109
 
110
   if (capture->deviceHandle == 0)
111
   {
112
@@ -462,8 +473,8 @@ static int try_init_v4l(CvCaptureCAM_V4L
113
     /* Query the newly opened device for its capabilities */
114
     if (ioctl(capture->deviceHandle, VIDIOCGCAP, &capture->capability) < 0)
115
     {
116
+      perror("VIDIOCGCAP");
117
       detect = 0;
118
-
119
       icvCloseCAM_V4L(capture);
120
     }
121
       else
122
@@ -476,54 +487,64 @@ static int try_init_v4l(CvCaptureCAM_V4L
123
 
124
 }
125
 
126
+#endif /* HAVE_CAMV4L */
127
+
128
 #ifdef HAVE_CAMV4L2
129
 
130
 static int try_init_v4l2(CvCaptureCAM_V4L* capture, char *deviceName)
131
 {
132
-
133
-  // if detect = -1 then unable to open device
134
-  // if detect = 0 then detected nothing
135
-  // if detect = 1 then V4L2 device
136
-  int detect = 0;
137
-
138
-
139
   // Test device for V4L2 compability
140
+  // Return value:
141
+  // -1 then unable to open device
142
+  //  0 then detected nothing
143
+  //  1 then V4L2 device
144
+
145
+  int deviceIndex;
146
 
147
   /* Open and test V4L2 device */
148
   capture->deviceHandle = open (deviceName, O_RDWR /* required */ | O_NONBLOCK, 0);
149
-
150
-
151
-
152
-  if (capture->deviceHandle == 0)
153
+  if (-1 == capture->deviceHandle)
154
   {
155
-    detect = -1;
156
-
157
+#ifndef NDEBUG
158
+    fprintf(stderr, "(DEBUG) try_init_v4l2 open \"%s\": %s\n", deviceName, strerror(errno));
159
+#endif
160
     icvCloseCAM_V4L(capture);
161
+    return -1;
162
   }
163
 
164
-  if (detect == 0)
165
+  CLEAR (capture->cap);
166
+  if (-1 == xioctl (capture->deviceHandle, VIDIOC_QUERYCAP, &capture->cap))
167
   {
168
-    CLEAR (capture->cap);
169
-    if (-1 == xioctl (capture->deviceHandle, VIDIOC_QUERYCAP, &capture->cap))
170
-    {
171
-      detect = 0;
172
+#ifndef NDEBUG
173
+    fprintf(stderr, "(DEBUG) try_init_v4l2 VIDIOC_QUERYCAP \"%s\": %s\n", deviceName, strerror(errno));
174
+#endif
175
+    icvCloseCAM_V4L(capture);
176
+    return 0;
177
+  }
178
 
179
-      icvCloseCAM_V4L(capture);
180
-    }
181
-      else
182
-    {
183
-      CLEAR (capture->capability);
184
-      capture->capability.type = capture->cap.capabilities;
185
+  /* Query channels number */
186
+  if (-1 == xioctl (capture->deviceHandle, VIDIOC_G_INPUT, &deviceIndex))
187
+  {
188
+#ifndef NDEBUG
189
+    fprintf(stderr, "(DEBUG) try_init_v4l2 VIDIOC_G_INPUT \"%s\": %s\n", deviceName, strerror(errno));
190
+#endif
191
+    icvCloseCAM_V4L(capture);
192
+    return 0;
193
+  }
194
 
195
-      /* Query channels number */
196
-      if (-1 != xioctl (capture->deviceHandle, VIDIOC_G_INPUT, &capture->capability.channels))
197
-      {
198
-        detect = 1;
199
-      }
200
-    }
201
+  /* Query information about current input */
202
+  CLEAR (capture->inp);
203
+  capture->inp.index = deviceIndex;
204
+  if (-1 == xioctl (capture->deviceHandle, VIDIOC_ENUMINPUT, &capture->inp))
205
+  {
206
+#ifndef NDEBUG
207
+    fprintf(stderr, "(DEBUG) try_init_v4l2 VIDIOC_ENUMINPUT \"%s\": %s\n", deviceName, strerror(errno));
208
+#endif
209
+    icvCloseCAM_V4L(capture);
210
+    return 0;
211
   }
212
 
213
-  return detect;
214
+  return 1;
215
 
216
 }
217
 
218
@@ -593,6 +614,8 @@ static int autosetup_capture_mode_v4l2(C
219
 
220
 #endif /* HAVE_CAMV4L2 */
221
 
222
+#ifdef HAVE_CAMV4L
223
+
224
 static int autosetup_capture_mode_v4l(CvCaptureCAM_V4L* capture)
225
 {
226
 
227
@@ -626,6 +649,8 @@ static int autosetup_capture_mode_v4l(Cv
228
 
229
 }
230
 
231
+#endif /* HAVE_CAMV4L */
232
+
233
 #ifdef HAVE_CAMV4L2
234
 
235
 static void v4l2_scan_controls_enumerate_menu(CvCaptureCAM_V4L* capture)
236
@@ -976,8 +1001,8 @@ static int _capture_V4L2 (CvCaptureCAM_V
237
 
238
    /* Set up Image data */
239
    cvInitImageHeader( &capture->frame,
240
-                      cvSize( capture->captureWindow.width,
241
-                              capture->captureWindow.height ),
242
+                      cvSize( capture->form.fmt.pix.width,
243
+                              capture->form.fmt.pix.height ),
244
                       IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 );
245
    /* Allocate space for RGBA data */
246
    capture->frame.imageData = (char *)cvAlloc(capture->frame.imageSize);
247
@@ -987,6 +1012,8 @@ static int _capture_V4L2 (CvCaptureCAM_V
248
 
249
 #endif /* HAVE_CAMV4L2 */
250
 
251
+#ifdef HAVE_CAMV4L
252
+
253
 static int _capture_V4L (CvCaptureCAM_V4L *capture, char *deviceName)
254
 {
255
    int detect_v4l = 0;
256
@@ -1105,6 +1132,8 @@ static int _capture_V4L (CvCaptureCAM_V4
257
    return 1;
258
 }; /* End _capture_V4L */
259
 
260
+#endif /* HAVE_CAMV4L */
261
+
262
 static CvCaptureCAM_V4L * icvCaptureFromCAM_V4L (int index)
263
 {
264
    static int autoindex;
265
@@ -1154,10 +1183,12 @@ static CvCaptureCAM_V4L * icvCaptureFrom
266
        icvCloseCAM_V4L(capture);
267
        V4L2_SUPPORT = 0;
268
 #endif  /* HAVE_CAMV4L2 */
269
+#ifdef HAVE_CAMV4L
270
        if (_capture_V4L (capture, deviceName) == -1) {
271
            icvCloseCAM_V4L(capture);
272
            return NULL;
273
        }
274
+#endif  /* HAVE_CAMV4L */
275
 #ifdef HAVE_CAMV4L2
276
    } else {
277
        V4L2_SUPPORT = 1;
278
@@ -1296,8 +1327,12 @@ static int icvGrabFrameCAM_V4L(CvCapture
279
             perror ("VIDIOC_STREAMON");
280
             return 0;
281
         }
282
-      } else
283
+      }
284
 #endif /* HAVE_CAMV4L2 */
285
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
286
+      else
287
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
288
+#ifdef HAVE_CAMV4L
289
       {
290
 
291
         for (capture->bufferIndex = 0;
292
@@ -1316,6 +1351,7 @@ static int icvGrabFrameCAM_V4L(CvCapture
293
         }
294
 
295
       }
296
+#endif /* HAVE_CAMV4L */
297
 
298
 #if defined(V4L_ABORT_BADJPEG) && defined(HAVE_CAMV4L2)
299
      if (V4L2_SUPPORT == 1)
300
@@ -1337,8 +1373,12 @@ static int icvGrabFrameCAM_V4L(CvCapture
301
 
302
      mainloop_v4l2(capture);
303
 
304
-   } else
305
+   }
306
 #endif /* HAVE_CAMV4L2 */
307
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
308
+     else
309
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
310
+#ifdef HAVE_CAMV4L
311
    {
312
 
313
      capture->mmaps[capture->bufferIndex].frame  = capture->bufferIndex;
314
@@ -1358,6 +1398,7 @@ static int icvGrabFrameCAM_V4L(CvCapture
315
      }
316
 
317
    }
318
+#endif /* HAVE_CAMV4L */
319
 
320
    return(1);
321
 }
322
@@ -2075,6 +2116,7 @@ static IplImage* icvRetrieveFrameCAM_V4L
323
 #ifdef HAVE_CAMV4L2
324
   if (V4L2_SUPPORT == 0)
325
 #endif /* HAVE_CAMV4L2 */
326
+#ifdef HAVE_CAMV4L
327
   {
328
 
329
     /* [FD] this really belongs here */
330
@@ -2083,6 +2125,7 @@ static IplImage* icvRetrieveFrameCAM_V4L
331
     }
332
 
333
   }
334
+#endif /* HAVE_CAMV4L */
335
 
336
    /* Now get what has already been captured as a IplImage return */
337
 
338
@@ -2103,8 +2146,12 @@ static IplImage* icvRetrieveFrameCAM_V4L
339
        capture->frame.imageData = (char *)cvAlloc(capture->frame.imageSize);
340
     }
341
 
342
-  } else
343
+  }
344
 #endif /* HAVE_CAMV4L2 */
345
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
346
+    else
347
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
348
+#ifdef HAVE_CAMV4L
349
   {
350
 
351
     if((capture->frame.width != capture->mmaps[capture->bufferIndex].width)
352
@@ -2118,6 +2165,7 @@ static IplImage* icvRetrieveFrameCAM_V4L
353
     }
354
 
355
   }
356
+#endif /* HAVE_CAMV4L */
357
 
358
 #ifdef HAVE_CAMV4L2
359
 
360
@@ -2145,7 +2193,6 @@ static IplImage* icvRetrieveFrameCAM_V4L
361
                          (unsigned char*)capture->frame.imageData);
362
         break;
363
 #ifdef HAVE_JPEG
364
-#ifdef __USE_GNU
365
     /* support for MJPEG is only available with libjpeg and gcc,
366
        because it's use libjepg and fmemopen()
367
     */
368
@@ -2159,7 +2206,6 @@ static IplImage* icvRetrieveFrameCAM_V4L
369
           return 0;
370
         break;
371
 #endif
372
-#endif
373
 
374
       case PALETTE_YUYV:
375
         yuyv_to_rgb24(capture->form.fmt.pix.width,
376
@@ -2201,8 +2247,12 @@ static IplImage* icvRetrieveFrameCAM_V4L
377
                     (unsigned char*)capture->frame.imageData);
378
         break;
379
       }
380
-  } else
381
+  }
382
 #endif /* HAVE_CAMV4L2 */
383
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
384
+    else
385
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
386
+#ifdef HAVE_CAMV4L
387
   {
388
     
389
     switch(capture->imageProperties.palette) {
390
@@ -2238,6 +2288,7 @@ static IplImage* icvRetrieveFrameCAM_V4L
391
     }
392
 
393
   }
394
+#endif /* HAVE_CAMV4L */
395
 
396
    return(&capture->frame);
397
 }
398
@@ -2358,8 +2409,12 @@ static double icvGetPropertyCAM_V4L (CvC
399
       /* all was OK, so convert to 0.0 - 1.0 range, and return the value */
400
       return ((float)capture->control.value - v4l2_min + 1) / (v4l2_max - v4l2_min);
401
 
402
-  } else
403
+  }
404
 #endif /* HAVE_CAMV4L2 */
405
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
406
+    else
407
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
408
+#ifdef HAVE_CAMV4L
409
   {
410
 
411
     int retval = -1;
412
@@ -2417,6 +2472,7 @@ static double icvGetPropertyCAM_V4L (CvC
413
     return float (retval) / 0xFFFF;
414
 
415
   }
416
+#endif /* HAVE_CAMV4L */
417
 
418
 };
419
 
420
@@ -2489,8 +2545,12 @@ static int icvSetVideoSize( CvCaptureCAM
421
 
422
     return 0;
423
 
424
-  } else
425
+  }
426
 #endif /* HAVE_CAMV4L2 */
427
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
428
+    else
429
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
430
+#ifdef HAVE_CAMV4L
431
   {
432
 
433
     if (capture==0) return 0;
434
@@ -2517,6 +2577,7 @@ static int icvSetVideoSize( CvCaptureCAM
435
      capture->FirstCapture = 1;
436
 
437
   }
438
+#endif /* HAVE_CAMV4L */
439
 
440
   return 0;
441
 
442
@@ -2648,8 +2709,12 @@ static int icvSetControl (CvCaptureCAM_V
443
         perror ("VIDIOC_S_CTRL");
444
         return -1;
445
     }
446
-  } else
447
+  }
448
 #endif /* HAVE_CAMV4L2 */
449
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
450
+    else
451
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
452
+#ifdef HAVE_CAMV4L
453
   {
454
 
455
     int v4l_value;
456
@@ -2694,6 +2759,7 @@ static int icvSetControl (CvCaptureCAM_V
457
        return -1;
458
     }
459
   }
460
+#endif /* HAVE_CAMV4L */
461
 
462
   /* all was OK */
463
   return 0;
464
@@ -2754,6 +2820,7 @@ static void icvCloseCAM_V4L( CvCaptureCA
465
 #ifdef HAVE_CAMV4L2
466
      if (V4L2_SUPPORT == 0)
467
 #endif /* HAVE_CAMV4L2 */
468
+#ifdef HAVE_CAMV4L
469
      {
470
 
471
        if (capture->mmaps)
472
@@ -2762,10 +2829,14 @@ static void icvCloseCAM_V4L( CvCaptureCA
473
          munmap(capture->memoryMap, capture->memoryBuffer.size);
474
 
475
      }
476
+#endif /* HAVE_CAMV4L */
477
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
478
+     else
479
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
480
 #ifdef HAVE_CAMV4L2
481
-     else {
482
+       {
483
        capture->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
484
-       if (ioctl(capture->deviceHandle, VIDIOC_STREAMOFF, &capture->type) < 0) {
485
+       if (-1 == ioctl(capture->deviceHandle, VIDIOC_STREAMOFF, &capture->type)) {
486
            perror ("Unable to stop the stream.");
487
        }
488