~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/qcap/v4l.c

Version: ~ [ wine-1.5.31 ] ~ [ wine-1.5.30 ] ~ [ wine-1.5.29 ] ~ [ wine-1.5.28 ] ~ [ wine-1.5.27 ] ~ [ wine-1.5.26 ] ~ [ wine-1.5.25 ] ~ [ wine-1.5.24 ] ~ [ wine-1.5.23 ] ~ [ wine-1.5.22 ] ~ [ wine-1.5.21 ] ~ [ wine-1.5.20 ] ~ [ wine-1.5.19 ] ~ [ wine-1.5.18 ] ~ [ wine-1.5.17 ] ~ [ wine-1.5.16 ] ~ [ wine-1.5.15 ] ~ [ wine-1.5.14 ] ~ [ wine-1.5.13 ] ~ [ wine-1.5.12 ] ~ [ wine-1.5.11 ] ~ [ wine-1.5.10 ] ~ [ wine-1.5.9 ] ~ [ wine-1.5.8 ] ~ [ wine-1.5.7 ] ~ [ wine-1.4.1 ] ~ [ wine-1.5.6 ] ~ [ wine-1.5.5 ] ~ [ wine-1.5.4 ] ~ [ wine-1.5.3 ] ~ [ wine-1.5.2 ] ~ [ wine-1.5.1 ] ~ [ wine-1.5.0 ] ~ [ wine-1.4 ] ~ [ wine-1.4-rc6 ] ~ [ wine-1.4-rc5 ] ~ [ wine-1.4-rc4 ] ~ [ wine-1.4-rc3 ] ~ [ wine-1.4-rc2 ] ~ [ wine-1.4-rc1 ] ~ [ wine-1.3.37 ] ~ [ wine-1.3.36 ] ~ [ wine-1.3.35 ] ~ [ wine-1.3.34 ] ~ [ wine-1.3.33 ] ~ [ wine-1.3.32 ] ~ [ wine-1.3.31 ] ~ [ wine-1.3.30 ] ~ [ wine-1.3.29 ] ~ [ wine-1.3.28 ] ~ [ wine-1.3.27 ] ~ [ wine-1.3.26 ] ~ [ wine-1.3.25 ] ~ [ wine-1.3.24 ] ~ [ wine-1.3.23 ] ~ [ wine-1.3.22 ] ~ [ wine-1.3.21 ] ~ [ wine-1.3.20 ] ~ [ wine-1.3.19 ] ~ [ wine-1.3.18 ] ~ [ wine-1.2.3 ] ~ [ wine-1.3.17 ] ~ [ wine-1.3.16 ] ~ [ wine-1.3.15 ] ~ [ wine-1.3.14 ] ~ [ wine-1.3.13 ] ~ [ wine-1.3.12 ] ~ [ wine-1.3.11 ] ~ [ wine-1.3.10 ] ~ [ wine-1.3.9 ] ~ [ wine-1.2.2 ] ~ [ wine-1.3.8 ] ~ [ wine-1.3.7 ] ~ [ wine-1.3.6 ] ~ [ wine-1.3.5 ] ~ [ wine-1.2.1 ] ~ [ wine-1.3.4 ] ~ [ wine-1.3.3 ] ~ [ wine-1.3.2 ] ~ [ wine-1.3.1 ] ~ [ wine-1.3.0 ] ~ [ wine-1.2 ] ~ [ wine-1.2-rc7 ] ~ [ wine-1.2-rc6 ] ~ [ wine-1.2-rc5 ] ~ [ wine-1.2-rc4 ] ~ [ wine-1.2-rc3 ] ~ [ wine-1.2-rc2 ] ~ [ wine-1.2-rc1 ] ~ [ wine-1.1.44 ] ~ [ wine-1.1.43 ] ~ [ wine-1.1.42 ] ~ [ wine-1.1.41 ] ~ [ wine-1.1.40 ] ~ [ wine-1.1.39 ] ~ [ wine-1.1.38 ] ~ [ wine-1.1.37 ] ~ [ wine-1.1.36 ] ~ [ wine-1.1.35 ] ~ [ wine-1.1.34 ] ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * DirectShow capture services (QCAP.DLL)
  3  *
  4  * Copyright 2005 Maarten Lankhorst
  5  *
  6  * This file contains the part of the vfw capture interface that
  7  * does the actual Video4Linux(1/2) stuff required for capturing
  8  * and setting/getting media format..
  9  *
 10  * This library is free software; you can redistribute it and/or
 11  * modify it under the terms of the GNU Lesser General Public
 12  * License as published by the Free Software Foundation; either
 13  * version 2.1 of the License, or (at your option) any later version.
 14  *
 15  * This library is distributed in the hope that it will be useful,
 16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18  * Lesser General Public License for more details.
 19  *
 20  * You should have received a copy of the GNU Lesser General Public
 21  * License along with this library; if not, write to the Free Software
 22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 23  */
 24 
 25 #include "config.h"
 26 #include "wine/port.h"
 27 #include "wine/library.h"
 28 
 29 #define NONAMELESSSTRUCT
 30 #define NONAMELESSUNION
 31 #define COBJMACROS
 32 
 33 #include <stdarg.h>
 34 #include "windef.h"
 35 #include "winbase.h"
 36 #include "wtypes.h"
 37 #include "wingdi.h"
 38 #include "winuser.h"
 39 #include "dshow.h"
 40 #include "vfwmsgs.h"
 41 #include "amvideo.h"
 42 #include "wine/debug.h"
 43 
 44 #include "capture.h"
 45 #include "qcap_main.h"
 46 
 47 #include <stdio.h>
 48 #include <fcntl.h>
 49 
 50 #ifdef HAVE_SYS_IOCTL_H
 51 #include <sys/ioctl.h>
 52 #endif
 53 #ifdef HAVE_SYS_MMAN_H
 54 #include <sys/mman.h>
 55 #endif
 56 #ifdef HAVE_SYS_ERRNO_H
 57 #include <sys/errno.h>
 58 #endif
 59 #ifdef HAVE_SYS_TIME_H
 60 #include <sys/time.h>
 61 #endif
 62 #ifdef HAVE_ASM_TYPES_H
 63 #include <asm/types.h>
 64 #endif
 65 #ifdef HAVE_LINUX_VIDEODEV_H
 66 #include <linux/videodev.h>
 67 #endif
 68 #ifdef HAVE_UNISTD_H
 69 #include <unistd.h>
 70 #endif
 71 
 72 WINE_DEFAULT_DEBUG_CHANNEL(qcap_v4l);
 73 
 74 #ifdef HAVE_LINUX_VIDEODEV_H
 75 
 76 static typeof(open) *video_open = open;
 77 static typeof(close) *video_close = close;
 78 static typeof(ioctl) *video_ioctl = ioctl;
 79 static typeof(read) *video_read = read;
 80 static typeof(mmap) *video_mmap = mmap;
 81 static typeof(munmap) *video_munmap = munmap;
 82 
 83 static void video_init(void)
 84 {
 85 #ifdef SONAME_LIBV4L1
 86     static void *video_lib;
 87 
 88     if (video_lib)
 89         return;
 90     video_lib = wine_dlopen(SONAME_LIBV4L1, RTLD_NOW, NULL, 0);
 91     if (!video_lib)
 92         return;
 93     video_open = wine_dlsym(video_lib, "v4l1_open", NULL, 0);
 94     video_close = wine_dlsym(video_lib, "v4l1_close", NULL, 0);
 95     video_ioctl = wine_dlsym(video_lib, "v4l1_ioctl", NULL, 0);
 96     video_read = wine_dlsym(video_lib, "v4l1_read", NULL, 0);
 97     video_mmap = wine_dlsym(video_lib, "v4l1_mmap", NULL, 0);
 98     video_munmap = wine_dlsym(video_lib, "v4l1_munmap", NULL, 0);
 99 #endif
100 }
101 
102 typedef void (* Renderer)(const Capture *, LPBYTE bufferin, const BYTE *stream);
103 
104 struct _Capture
105 {
106     UINT width, height, bitDepth, fps, outputwidth, outputheight;
107     BOOL swresize;
108 
109     CRITICAL_SECTION CritSect;
110 
111     IPin *pOut;
112     int fd, mmap;
113     int iscommitted, stopped;
114     struct video_picture pict;
115     int dbrightness, dhue, dcolour, dcontrast;
116 
117     /* mmap (V4l1) */
118     struct video_mmap *grab_buf;
119     struct video_mbuf gb_buffers;
120     unsigned char *pmap;
121     int buffers;
122 
123     /* read (V4l1) */
124     int imagesize;
125     char * grab_data;
126 
127     int curframe;
128 
129     HANDLE thread;
130     Renderer renderer;
131 };
132 
133 struct renderlist
134 {
135     int depth;
136     const char* name;
137     Renderer renderer;
138 };
139 
140 static void renderer_RGB(const Capture *capBox, LPBYTE bufferin, const BYTE *stream);
141 static void renderer_YUV(const Capture *capBox, LPBYTE bufferin, const BYTE *stream);
142 
143 static const struct renderlist renderlist_V4l[] = {
144     {  0, "NULL renderer",               NULL },
145     {  8, "Gray scales",                 NULL }, /* 1,  Don't support  */
146     {  0, "High 240 cube (BT848)",       NULL }, /* 2,  Don't support  */
147     { 16, "16 bit RGB (565)",            NULL }, /* 3,  Don't support  */
148     { 24, "24 bit RGB values",   renderer_RGB }, /* 4,  Supported,     */
149     { 32, "32 bit RGB values",   renderer_RGB }, /* 5,  Supported      */
150     { 16, "15 bit RGB (555)",            NULL }, /* 6,  Don't support  */
151     { 16, "YUV 422 (Not P)",     renderer_YUV }, /* 7,  Supported */
152     { 16, "YUYV (Not P)",        renderer_YUV }, /* 8,  Supported */
153     { 16, "UYVY (Not P)",        renderer_YUV }, /* 9,  Supported */
154     { 16, "YUV 420 (Not P)", NULL }, /* 10, Not supported, if I had to guess it's YYUYYV */
155     { 12, "YUV 411 (Not P)",     renderer_YUV }, /* 11, Supported */
156     {  0, "Raw capturing (BT848)",       NULL }, /* 12, Don't support  */
157     { 16, "YUV 422 (Planar)",    renderer_YUV }, /* 13, Supported */
158     { 12, "YUV 411 (Planar)",    renderer_YUV }, /* 14, Supported */
159     { 12, "YUV 420 (Planar)",    renderer_YUV }, /* 15, Supported */
160     { 10, "YUV 410 (Planar)",    renderer_YUV }, /* 16, Supported */
161     /* FIXME: add YUV420 support */
162     {  0, NULL,                          NULL },
163 };
164 
165 static const int fallback_V4l[] = { 4, 5, 7, 8, 9, 13, 15, 14, 16, 11, -1 };
166 /* Fallback: First try raw formats (Should try yuv first perhaps?), then yuv */
167 
168 /* static const Capture defbox; */
169 
170 static int xioctl(int fd, int request, void * arg)
171 {
172     int r;
173 
174     do {
175         r = video_ioctl (fd, request, arg);
176     } while (-1 == r && EINTR == errno);
177 
178     return r;
179 }
180 
181 /* Prepare the capture buffers */
182 static HRESULT V4l_Prepare(Capture *capBox)
183 {
184     TRACE("%p: Preparing for %dx%d resolution\n", capBox, capBox->width, capBox->height);
185 
186     /* Try mmap */
187     capBox->mmap = 0;
188     if (xioctl(capBox->fd, VIDIOCGMBUF, &capBox->gb_buffers) != -1 &&
189         capBox->gb_buffers.frames)
190     {
191         capBox->buffers = capBox->gb_buffers.frames;
192         if (capBox->gb_buffers.frames > 1)
193             capBox->buffers = 1;
194         TRACE("%p: Using %d/%d buffers\n", capBox,
195               capBox->buffers, capBox->gb_buffers.frames);
196 
197         capBox->pmap = video_mmap( 0, capBox->gb_buffers.size, PROT_READ|PROT_WRITE,
198                                    MAP_SHARED, capBox->fd, 0 );
199         if (capBox->pmap != MAP_FAILED)
200         {
201             int i;
202 
203             capBox->grab_buf = CoTaskMemAlloc(sizeof(struct video_mmap) * capBox->buffers);
204             if (!capBox->grab_buf)
205             {
206                 video_munmap(capBox->pmap, capBox->gb_buffers.size);
207                 return E_OUTOFMEMORY;
208             }
209 
210             /* Setup mmap capture buffers. */
211             for (i = 0; i < capBox->buffers; i++)
212             {
213                 capBox->grab_buf[i].format = capBox->pict.palette;
214                 capBox->grab_buf[i].frame = i;
215                 capBox->grab_buf[i].width = capBox->width;
216                 capBox->grab_buf[i].height = capBox->height;
217             }
218             capBox->mmap = 1;
219         }
220     }
221     if (!capBox->mmap)
222     {
223         capBox->buffers = 1;
224         capBox->imagesize = renderlist_V4l[capBox->pict.palette].depth *
225                                 capBox->height * capBox->width / 8;
226         capBox->grab_data = CoTaskMemAlloc(capBox->imagesize);
227         if (!capBox->grab_data)
228             return E_OUTOFMEMORY;
229     }
230     TRACE("Using mmap: %d\n", capBox->mmap);
231     return S_OK;
232 }
233 
234 static void V4l_Unprepare(Capture *capBox)
235 {
236     if (capBox->mmap)
237     {
238         for (capBox->curframe = 0; capBox->curframe < capBox->buffers; capBox->curframe++) 
239             xioctl(capBox->fd, VIDIOCSYNC, &capBox->grab_buf[capBox->curframe]);
240         video_munmap(capBox->pmap, capBox->gb_buffers.size);
241         CoTaskMemFree(capBox->grab_buf);
242     }
243     else
244         CoTaskMemFree(capBox->grab_data);
245 }
246 
247 HRESULT qcap_driver_destroy(Capture *capBox)
248 {
249     TRACE("%p\n", capBox);
250 
251     if( capBox->fd != -1 )
252         video_close(capBox->fd);
253     capBox->CritSect.DebugInfo->Spare[0] = 0;
254     DeleteCriticalSection(&capBox->CritSect);
255     CoTaskMemFree(capBox);
256     return S_OK;
257 }
258 
259 HRESULT qcap_driver_set_format(Capture *capBox, AM_MEDIA_TYPE * mT)
260 {
261     int newheight, newwidth;
262     struct video_window window;
263     VIDEOINFOHEADER *format;
264 
265     TRACE("%p\n", capBox);
266 
267     format = (VIDEOINFOHEADER *) mT->pbFormat;
268     if (format->bmiHeader.biBitCount != 24 ||
269         format->bmiHeader.biCompression != BI_RGB)
270     {
271         FIXME("unsupported media type %d %d\n", format->bmiHeader.biBitCount,
272               format->bmiHeader.biCompression );
273         return VFW_E_INVALIDMEDIATYPE;
274     }
275 
276     newwidth = format->bmiHeader.biWidth;
277     newheight = format->bmiHeader.biHeight;
278 
279     TRACE("%p -> (%p) - %d %d\n", capBox, mT, newwidth, newheight);
280 
281     if (capBox->height == newheight && capBox->width == newwidth)
282         return S_OK;
283 
284     if(-1 == xioctl(capBox->fd, VIDIOCGWIN, &window))
285     {
286         ERR("ioctl(VIDIOCGWIN) failed (%d)\n", errno);
287         return E_FAIL;
288     }
289     window.width = newwidth;
290     window.height = newheight;
291     if (xioctl(capBox->fd, VIDIOCSWIN, &window) == -1)
292     {
293         TRACE("using software resize: %dx%d -> %dx%d\n",
294                window.width, window.height, capBox->width, capBox->height);
295         capBox->swresize = TRUE;
296     }
297     else
298     {
299         capBox->height = window.height;
300         capBox->width = window.width;
301         capBox->swresize = FALSE;
302     }
303     capBox->outputwidth = window.width;
304     capBox->outputheight = window.height;
305     return S_OK;
306 }
307 
308 HRESULT qcap_driver_get_format(const Capture *capBox, AM_MEDIA_TYPE ** mT)
309 {
310     VIDEOINFOHEADER *vi;
311 
312     mT[0] = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
313     if (!mT[0])
314         return E_OUTOFMEMORY;
315     vi = CoTaskMemAlloc(sizeof(VIDEOINFOHEADER));
316     mT[0]->cbFormat = sizeof(VIDEOINFOHEADER);
317     if (!vi)
318     {
319         CoTaskMemFree(mT[0]);
320         return E_OUTOFMEMORY;
321     }
322     mT[0]->majortype = MEDIATYPE_Video;
323     mT[0]->subtype = MEDIASUBTYPE_RGB24;
324     mT[0]->formattype = FORMAT_VideoInfo;
325     mT[0]->bFixedSizeSamples = TRUE;
326     mT[0]->bTemporalCompression = FALSE;
327     mT[0]->pUnk = NULL;
328     mT[0]->lSampleSize = capBox->outputwidth * capBox->outputheight * capBox->bitDepth / 8;
329     TRACE("Output format: %dx%d - %d bits = %u KB\n", capBox->outputwidth,
330           capBox->outputheight, capBox->bitDepth, mT[0]->lSampleSize/1024);
331     vi->rcSource.left = 0; vi->rcSource.top = 0;
332     vi->rcTarget.left = 0; vi->rcTarget.top = 0;
333     vi->rcSource.right = capBox->width; vi->rcSource.bottom = capBox->height;
334     vi->rcTarget.right = capBox->outputwidth; vi->rcTarget.bottom = capBox->outputheight;
335     vi->dwBitRate = capBox->fps * mT[0]->lSampleSize;
336     vi->dwBitErrorRate = 0;
337     vi->AvgTimePerFrame = (LONGLONG)10000000.0 / (LONGLONG)capBox->fps;
338     vi->bmiHeader.biSize = 40;
339     vi->bmiHeader.biWidth = capBox->outputwidth;
340     vi->bmiHeader.biHeight = capBox->outputheight;
341     vi->bmiHeader.biPlanes = 1;
342     vi->bmiHeader.biBitCount = 24;
343     vi->bmiHeader.biCompression = BI_RGB;
344     vi->bmiHeader.biSizeImage = mT[0]->lSampleSize;
345     vi->bmiHeader.biClrUsed = vi->bmiHeader.biClrImportant = 0;
346     vi->bmiHeader.biXPelsPerMeter = 100;
347     vi->bmiHeader.biYPelsPerMeter = 100;
348     mT[0]->pbFormat = (void *)vi;
349     dump_AM_MEDIA_TYPE(mT[0]);
350     return S_OK;
351 }
352 
353 HRESULT qcap_driver_get_prop_range( Capture *capBox,
354             VideoProcAmpProperty Property, LONG *pMin, LONG *pMax,
355             LONG *pSteppingDelta, LONG *pDefault, LONG *pCapsFlags )
356 {
357     TRACE("%p -> %d %p %p %p %p %p\n", capBox, Property,
358           pMin, pMax, pSteppingDelta, pDefault, pCapsFlags);
359 
360     switch (Property)
361     {
362     case VideoProcAmp_Brightness:
363         *pDefault = capBox->dbrightness;
364         break;
365     case VideoProcAmp_Contrast:
366         *pDefault = capBox->dcontrast;
367         break;
368     case VideoProcAmp_Hue:
369         *pDefault = capBox->dhue;
370         break;
371     case VideoProcAmp_Saturation:
372         *pDefault = capBox->dcolour;
373         break;
374     default:
375         FIXME("Not implemented %d\n", Property);
376         return E_NOTIMPL;
377     }
378     *pMin = 0;
379     *pMax = 65535;
380     *pSteppingDelta = 65536/256;
381     *pCapsFlags = VideoProcAmp_Flags_Manual;
382     return S_OK;
383 }
384 
385 HRESULT qcap_driver_get_prop( Capture *capBox,
386             VideoProcAmpProperty Property, LONG *lValue, LONG *Flags )
387 {
388     TRACE("%p -> %d %p %p\n", capBox, Property, lValue, Flags);
389 
390     switch (Property)
391     {
392     case VideoProcAmp_Brightness:
393         *lValue = capBox->pict.brightness;
394         break;
395     case VideoProcAmp_Contrast:
396         *lValue = capBox->pict.contrast;
397         break;
398     case VideoProcAmp_Hue:
399         *lValue = capBox->pict.hue;
400         break;
401     case VideoProcAmp_Saturation:
402         *lValue = capBox->pict.colour;
403         break;
404     default:
405         FIXME("Not implemented %d\n", Property);
406         return E_NOTIMPL;
407     }
408     *Flags = VideoProcAmp_Flags_Manual;
409     return S_OK;
410 }
411 
412 HRESULT qcap_driver_set_prop(Capture *capBox, VideoProcAmpProperty Property,
413             LONG lValue, LONG Flags)
414 {
415     TRACE("%p -> %d %d %d\n", capBox, Property, lValue, Flags);
416 
417     switch (Property)
418     {
419     case VideoProcAmp_Brightness:
420         capBox->pict.brightness = lValue;
421         break;
422     case VideoProcAmp_Contrast:
423         capBox->pict.contrast = lValue;
424         break;
425     case VideoProcAmp_Hue:
426         capBox->pict.hue = lValue;
427         break;
428     case VideoProcAmp_Saturation:
429         capBox->pict.colour = lValue;
430         break;
431     default:
432         FIXME("Not implemented %d\n", Property);
433         return E_NOTIMPL;
434     }
435 
436     if (xioctl(capBox->fd, VIDIOCSPICT, &capBox->pict) == -1)
437     {
438         ERR("ioctl(VIDIOCSPICT) failed (%d)\n",errno);
439         return E_FAIL;
440     }
441     return S_OK;
442 }
443 
444 static void renderer_RGB(const Capture *capBox, LPBYTE bufferin, const BYTE *stream)
445 {
446     int depth = renderlist_V4l[capBox->pict.palette].depth;
447     int size = capBox->height * capBox->width * depth / 8;
448     int pointer, offset;
449 
450     switch (depth)
451     {
452     case 24:
453         memcpy(bufferin, stream, size);
454         break;
455     case 32:
456         pointer = 0;
457         offset = 1;
458         while (pointer + offset <= size)
459         {
460             bufferin[pointer] = stream[pointer + offset];
461             pointer++;
462             bufferin[pointer] = stream[pointer + offset];
463             pointer++;
464             bufferin[pointer] = stream[pointer + offset];
465             pointer++;
466             offset++;
467         }
468         break;
469     default:
470         ERR("Unknown bit depth %d\n", depth);
471         return;
472     }
473 }
474 
475 static void renderer_YUV(const Capture *capBox, LPBYTE bufferin, const BYTE *stream)
476 {
477     enum YUV_Format format;
478 
479     switch (capBox->pict.palette)
480     {
481     case  7: /* YUV422  -  same as YUYV */
482     case  8: /* YUYV    */
483         format = YUYV;
484         break;
485     case  9: /* UYVY    */
486         format = UYVY;
487         break;
488     case 11: /* YUV411  */
489         format = UYYVYY;
490         break;
491     case 13: /* YUV422P */
492         format = YUVP_421;
493         break;
494     case 14: /* YUV411P */
495         format = YUVP_441;
496         break;
497     case 15: /* YUV420P */
498         format = YUVP_422;
499         break;
500     case 16: /* YUV410P */
501         format = YUVP_444;
502         break;
503     default:
504         ERR("Unknown palette %d\n", capBox->pict.palette);
505         return;
506     }
507     YUV_To_RGB24(format, bufferin, stream, capBox->width, capBox->height);
508 }
509 
510 static void Resize(const Capture * capBox, LPBYTE output, const BYTE *input)
511 {
512     /* the whole image needs to be reversed,
513        because the dibs are messed up in windows */
514     if (!capBox->swresize)
515     {
516         int depth = capBox->bitDepth / 8;
517         int inoffset = 0, outoffset = capBox->height * capBox->width * depth;
518         int ow = capBox->width * depth;
519         while (outoffset > 0)
520         {
521             int x;
522             outoffset -= ow;
523             for (x = 0; x < ow; x++)
524                 output[outoffset + x] = input[inoffset + x];
525             inoffset += ow;
526         }
527     }
528     else
529     {
530         HDC dc_s, dc_d;
531         HBITMAP bmp_s, bmp_d;
532         int depth = capBox->bitDepth / 8;
533         int inoffset = 0, outoffset = (capBox->outputheight) * capBox->outputwidth * depth;
534         int ow = capBox->outputwidth * depth;
535         LPBYTE myarray;
536 
537         /* FIXME: Improve software resizing: add error checks and optimize */
538 
539         myarray = CoTaskMemAlloc(capBox->outputwidth * capBox->outputheight * depth);
540         dc_s = CreateCompatibleDC(NULL);
541         dc_d = CreateCompatibleDC(NULL);
542         bmp_s = CreateBitmap(capBox->width, capBox->height, 1, capBox->bitDepth, input);
543         bmp_d = CreateBitmap(capBox->outputwidth, capBox->outputheight, 1, capBox->bitDepth, NULL);
544         SelectObject(dc_s, bmp_s);
545         SelectObject(dc_d, bmp_d);
546         StretchBlt(dc_d, 0, 0, capBox->outputwidth, capBox->outputheight,
547                    dc_s, 0, 0, capBox->width, capBox->height, SRCCOPY);
548         GetBitmapBits(bmp_d, capBox->outputwidth * capBox->outputheight * depth, myarray);
549         while (outoffset > 0)
550         {
551             int i;
552 
553             outoffset -= ow;
554             for (i = 0; i < ow; i++)
555                 output[outoffset + i] = myarray[inoffset + i];
556             inoffset += ow;
557         }
558         CoTaskMemFree(myarray);
559         DeleteObject(dc_s);
560         DeleteObject(dc_d);
561         DeleteObject(bmp_s);
562         DeleteObject(bmp_d);
563     }
564 }
565 
566 static void V4l_GetFrame(Capture * capBox, unsigned char ** pInput)
567 {
568     if (capBox->mmap)
569     {
570         if (xioctl(capBox->fd, VIDIOCSYNC, &capBox->grab_buf[capBox->curframe]) == -1)
571             WARN("Syncing ioctl failed: %d\n", errno);
572 
573         *pInput = capBox->pmap + capBox->gb_buffers.offsets[capBox->curframe];
574     }
575     else
576     {
577         int retval;
578         while ((retval = video_read(capBox->fd, capBox->grab_data, capBox->imagesize)) == -1)
579             if (errno != EAGAIN) break;
580         if (retval == -1)
581             WARN("Error occurred while reading from device: %s\n", strerror(errno));
582         *pInput = (unsigned char*) capBox->grab_data;
583     }
584 }
585 
586 static void V4l_FreeFrame(Capture * capBox)
587 {
588     TRACE("\n");
589     if (capBox->mmap)
590     {
591         if (xioctl(capBox->fd, VIDIOCMCAPTURE, &capBox->grab_buf[capBox->curframe]) == -1)
592            ERR("Freeing frame for capture failed: %s\n", strerror(errno));
593     }
594     if (++capBox->curframe == capBox->buffers)
595         capBox->curframe = 0;
596 }
597 
598 static DWORD WINAPI ReadThread(LPVOID lParam)
599 {
600     Capture * capBox = lParam;
601     HRESULT hr;
602     IMediaSample *pSample = NULL;
603     ULONG framecount = 0;
604     unsigned char *pTarget, *pInput, *pOutput;
605 
606     hr = V4l_Prepare(capBox);
607     if (FAILED(hr))
608         goto fail;
609 
610     pOutput = CoTaskMemAlloc(capBox->width * capBox->height * capBox->bitDepth / 8);
611     capBox->curframe = 0;
612     do {
613         V4l_FreeFrame(capBox);
614     } while (capBox->curframe != 0);
615 
616     while (1)
617     {
618         EnterCriticalSection(&capBox->CritSect);
619         if (capBox->stopped)
620             break;
621         hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin *)capBox->pOut, &pSample, NULL, NULL, 0);
622         if (SUCCEEDED(hr))
623         {
624             int len;
625             
626             if (!capBox->swresize)
627                 len = capBox->height * capBox->width * capBox->bitDepth / 8;
628             else
629                 len = capBox->outputheight * capBox->outputwidth * capBox->bitDepth / 8;
630             IMediaSample_SetActualDataLength(pSample, len);
631 
632             len = IMediaSample_GetActualDataLength(pSample);
633             TRACE("Data length: %d KB\n", len / 1024);
634 
635             IMediaSample_GetPointer(pSample, &pTarget);
636             /* FIXME: Check return values.. */
637             V4l_GetFrame(capBox, &pInput);
638             capBox->renderer(capBox, pOutput, pInput);
639             Resize(capBox, pTarget, pOutput);
640             hr = BaseOutputPinImpl_Deliver((BaseOutputPin *)capBox->pOut, pSample);
641             TRACE("%p -> Frame %u: %x\n", capBox, ++framecount, hr);
642             IMediaSample_Release(pSample);
643             V4l_FreeFrame(capBox);
644         }
645         LeaveCriticalSection(&capBox->CritSect);
646         if (FAILED(hr) && hr != VFW_E_NOT_CONNECTED)
647         {
648             ERR("Received error: %x\n", hr);
649             goto cfail;
650         }
651     }
652     LeaveCriticalSection(&capBox->CritSect);
653     CoTaskMemFree(pOutput);
654 
655     return 0;
656 
657 cfail:
658     CoTaskMemFree(pOutput);
659     V4l_Unprepare(capBox);
660     LeaveCriticalSection(&capBox->CritSect);
661 
662 fail:
663     capBox->thread = 0; capBox->stopped = 1;
664     FIXME("Stop IFilterGraph\n");
665     return 0;
666 }
667 
668 HRESULT qcap_driver_run(Capture *capBox, FILTER_STATE *state)
669 {
670     HANDLE thread;
671     HRESULT hr;
672 
673     TRACE("%p -> (%p)\n", capBox, state); 
674 
675     if (*state == State_Running) return S_OK;
676 
677     EnterCriticalSection(&capBox->CritSect);
678 
679     capBox->stopped = 0;
680 
681     if (*state == State_Stopped)
682     {
683         *state = State_Running;
684         if (!capBox->iscommitted++)
685         {
686             IMemAllocator * pAlloc = NULL;
687             ALLOCATOR_PROPERTIES ap, actual;
688             BaseOutputPin *out;
689 
690             ap.cBuffers = 3;
691             if (!capBox->swresize)
692                 ap.cbBuffer = capBox->width * capBox->height;
693             else
694                 ap.cbBuffer = capBox->outputwidth * capBox->outputheight;
695             ap.cbBuffer = (ap.cbBuffer * capBox->bitDepth) / 8;
696             ap.cbAlign = 1;
697             ap.cbPrefix = 0;
698 
699             out = (BaseOutputPin *)capBox->pOut;
700             hr = IMemInputPin_GetAllocator(out->pMemInputPin, &pAlloc);
701 
702             if (SUCCEEDED(hr))
703                 hr = IMemAllocator_SetProperties(pAlloc, &ap, &actual);
704 
705             if (SUCCEEDED(hr))
706                 hr = IMemAllocator_Commit(pAlloc);
707 
708             if (pAlloc)
709                 IMemAllocator_Release(pAlloc);
710 
711             TRACE("Committing allocator: %x\n", hr);
712         }
713 
714         thread = CreateThread(NULL, 0, ReadThread, capBox, 0, NULL);
715         if (thread)
716         {
717             capBox->thread = thread;
718             SetThreadPriority(thread, THREAD_PRIORITY_LOWEST);
719             LeaveCriticalSection(&capBox->CritSect);
720             return S_OK;
721         }
722         ERR("Creating thread failed.. %u\n", GetLastError());
723         LeaveCriticalSection(&capBox->CritSect);
724         return E_FAIL;
725     }
726 
727     ResumeThread(capBox->thread);
728     *state = State_Running;
729     LeaveCriticalSection(&capBox->CritSect);
730     return S_OK;
731 }
732 
733 HRESULT qcap_driver_pause(Capture *capBox, FILTER_STATE *state)
734 {
735     TRACE("%p -> (%p)\n", capBox, state);     
736 
737     if (*state == State_Paused)
738         return S_OK;
739     if (*state == State_Stopped)
740         qcap_driver_run(capBox, state);
741 
742     EnterCriticalSection(&capBox->CritSect);
743     *state = State_Paused;
744     SuspendThread(capBox->thread);
745     LeaveCriticalSection(&capBox->CritSect);
746 
747     return S_OK;
748 }
749 
750 HRESULT qcap_driver_stop(Capture *capBox, FILTER_STATE *state)
751 {
752     TRACE("%p -> (%p)\n", capBox, state);
753 
754     if (*state == State_Stopped)
755         return S_OK;
756 
757     EnterCriticalSection(&capBox->CritSect);
758 
759     if (capBox->thread)
760     {
761         if (*state == State_Paused)
762             ResumeThread(capBox->thread);
763         capBox->stopped = 1;
764         capBox->thread = 0;
765         if (capBox->iscommitted)
766         {
767             IMemInputPin *pMem = NULL;
768             IMemAllocator * pAlloc = NULL;
769             IPin *pConnect = NULL;
770             HRESULT hr;
771 
772             capBox->iscommitted = 0;
773 
774             hr = IPin_ConnectedTo(capBox->pOut, &pConnect);
775 
776             if (SUCCEEDED(hr))
777                 hr = IPin_QueryInterface(pConnect, &IID_IMemInputPin, (void **) &pMem);
778 
779             if (SUCCEEDED(hr))
780                 hr = IMemInputPin_GetAllocator(pMem, &pAlloc);
781 
782             if (SUCCEEDED(hr))
783                 hr = IMemAllocator_Decommit(pAlloc);
784 
785             if (pAlloc)
786                 IMemAllocator_Release(pAlloc);
787 
788             if (pMem)
789                 IMemInputPin_Release(pMem);
790 
791             if (pConnect)
792                 IPin_Release(pConnect);
793 
794             if (hr != S_OK && hr != VFW_E_NOT_COMMITTED)
795                 WARN("Decommitting allocator: %x\n", hr);
796         }
797         V4l_Unprepare(capBox);
798     }
799 
800     *state = State_Stopped;
801     LeaveCriticalSection(&capBox->CritSect);
802     return S_OK;
803 }
804 
805 Capture * qcap_driver_init( IPin *pOut, USHORT card )
806 {
807     Capture * capBox = NULL;
808     char device[20];
809     struct video_capability capa;
810     struct video_picture pict;
811     struct video_window window;
812 
813     YUV_Init();
814     video_init();
815 
816     capBox = CoTaskMemAlloc(sizeof(Capture));
817     if (!capBox)
818         goto error;
819 
820     /* capBox->vtbl = &defboxVtbl; */
821 
822     InitializeCriticalSection( &capBox->CritSect );
823     capBox->CritSect.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Capture.CritSect");
824 
825     sprintf(device, "/dev/video%i", card);
826     TRACE("opening %s\n", device);
827     capBox->fd = video_open(device, O_RDWR | O_NONBLOCK);
828     if (capBox->fd == -1)
829     {
830         WARN("open failed (%d)\n", errno);
831         goto error;
832     }
833 
834     memset(&capa, 0, sizeof(capa));
835 
836     if (xioctl(capBox->fd, VIDIOCGCAP, &capa) == -1)
837     {
838         WARN("ioctl(VIDIOCGCAP) failed (%d)\n", errno);
839         goto error;
840     }
841 
842     if (!(capa.type & VID_TYPE_CAPTURE))
843     {
844         WARN("not a video capture device\n");
845         goto error;
846     }
847 
848     TRACE("%d inputs on %s\n", capa.channels, capa.name );
849 
850     if (xioctl(capBox->fd, VIDIOCGPICT, &pict) == -1)
851     {
852         ERR("ioctl(VIDIOCGPICT) failed (%d)\n", errno );
853         goto error;
854     }
855 
856     TRACE("depth %d palette %d (%s) hue %d color %d contrast %d\n",
857           pict.depth, pict.palette, renderlist_V4l[pict.palette].name,
858           pict.hue, pict.colour, pict.contrast );
859 
860     capBox->dbrightness = pict.brightness;
861     capBox->dcolour = pict.colour;
862     capBox->dhue = pict.hue;
863     capBox->dcontrast = pict.contrast;
864 
865     if (!renderlist_V4l[pict.palette].renderer)
866     {
867         int palet = pict.palette, i;
868 
869         TRACE("No renderer available for %s, falling back to defaults\n",
870              renderlist_V4l[pict.palette].name);
871         capBox->renderer = NULL;
872         for (i = 0; fallback_V4l[i] >=0 ; i++)
873         {
874             int n = fallback_V4l[i];
875 
876             if (renderlist_V4l[n].renderer == NULL)
877                 continue;
878 
879             pict.depth = renderlist_V4l[n].depth;
880             pict.palette = n;
881             if (xioctl(capBox->fd, VIDIOCSPICT, &pict) == -1)
882             {
883                 TRACE("Could not render with %s (%d)\n",
884                       renderlist_V4l[n].name, n);
885                 continue;
886             }
887             TRACE("using renderer %s (%d)\n", 
888                   renderlist_V4l[n].name, n);
889             capBox->renderer = renderlist_V4l[n].renderer;
890             break;
891         }
892 
893         if (!capBox->renderer)
894         {
895             ERR("video format %s isn't available\n",
896                  renderlist_V4l[palet].name);
897             goto error;
898         }
899     }
900     else
901     {
902         TRACE("Using the suggested format\n");
903         capBox->renderer = renderlist_V4l[pict.palette].renderer;
904     }
905     memcpy(&capBox->pict, &pict, sizeof(struct video_picture));
906 
907     memset(&window, 0, sizeof(window));
908     if (xioctl(capBox->fd, VIDIOCGWIN, &window) == -1)
909     {
910         WARN("VIDIOCGWIN failed (%d)\n", errno);
911         goto error;
912     }
913 
914     capBox->height = capBox->outputheight = window.height;
915     capBox->width = capBox->outputwidth = window.width;
916     capBox->swresize = FALSE;
917     capBox->bitDepth = 24;
918     capBox->pOut = pOut;
919     capBox->fps = 3;
920     capBox->stopped = 0;
921     capBox->curframe = 0;
922     capBox->iscommitted = 0;
923 
924     TRACE("format: %d bits - %d x %d\n", capBox->bitDepth, capBox->width, capBox->height);
925 
926     return capBox;
927 
928 error:
929     if (capBox)
930         qcap_driver_destroy( capBox );
931 
932     return NULL;
933 }
934 
935 #else
936 
937 Capture * qcap_driver_init( IPin *pOut, USHORT card )
938 {
939     const char msg[] = 
940         "The v4l headers were not available at compile time,\n"
941         "so video capture support is not available.\n";
942     MESSAGE(msg);
943     return NULL;
944 }
945 
946 #define FAIL_WITH_ERR \
947     ERR("v4l absent: shouldn't be called\n"); \
948     return E_NOTIMPL
949 
950 HRESULT qcap_driver_destroy(Capture *capBox)
951 {
952     FAIL_WITH_ERR;
953 }
954 
955 HRESULT qcap_driver_set_format(Capture *capBox, AM_MEDIA_TYPE * mT)
956 {
957     FAIL_WITH_ERR;
958 }
959 
960 HRESULT qcap_driver_get_format(const Capture *capBox, AM_MEDIA_TYPE ** mT)
961 {
962     FAIL_WITH_ERR;
963 }
964 
965 HRESULT qcap_driver_get_prop_range( Capture *capBox,
966         VideoProcAmpProperty  Property, LONG *pMin, LONG *pMax,
967         LONG *pSteppingDelta, LONG *pDefault, LONG *pCapsFlags )
968 {
969     FAIL_WITH_ERR;
970 }
971 
972 HRESULT qcap_driver_get_prop(Capture *capBox,
973         VideoProcAmpProperty Property, LONG *lValue, LONG *Flags)
974 {
975     FAIL_WITH_ERR;
976 }
977 
978 HRESULT qcap_driver_set_prop(Capture *capBox, VideoProcAmpProperty Property,
979         LONG lValue, LONG Flags)
980 {
981     FAIL_WITH_ERR;
982 }
983 
984 HRESULT qcap_driver_run(Capture *capBox, FILTER_STATE *state)
985 {
986     FAIL_WITH_ERR;
987 }
988 
989 HRESULT qcap_driver_pause(Capture *capBox, FILTER_STATE *state)
990 {
991     FAIL_WITH_ERR;
992 }
993 
994 HRESULT qcap_driver_stop(Capture *capBox, FILTER_STATE *state)
995 {
996     FAIL_WITH_ERR;
997 }
998 
999 #endif /* HAVE_LINUX_VIDEODEV_H */
1000 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.