user32: Always use the default alignment when displaying submenu popups.
[wine/multimedia.git] / dlls / qcap / v4l.c
bloba7995e2dffa5c52be9f065b516adaa3447450539
1 /*
2 * DirectShow capture services (QCAP.DLL)
4 * Copyright 2005 Maarten Lankhorst
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..
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30 #define COBJMACROS
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
37 #endif
38 #ifdef HAVE_SYS_MMAN_H
39 #include <sys/mman.h>
40 #endif
41 #ifdef HAVE_SYS_ERRNO_H
42 #include <sys/errno.h>
43 #endif
44 #ifdef HAVE_SYS_TIME_H
45 #include <sys/time.h>
46 #endif
47 #ifdef HAVE_ASM_TYPES_H
48 #include <asm/types.h>
49 #endif
50 #ifdef HAVE_LINUX_VIDEODEV_H
51 #include <linux/videodev.h>
52 #endif
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
57 #include "windef.h"
58 #include "winbase.h"
59 #include "wtypes.h"
60 #include "wingdi.h"
61 #include "winuser.h"
62 #include "dshow.h"
63 #include "vfwmsgs.h"
64 #include "amvideo.h"
65 #include "wine/debug.h"
66 #include "wine/library.h"
68 #include "capture.h"
69 #include "qcap_main.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(qcap_v4l);
73 #ifdef HAVE_LINUX_VIDEODEV_H
75 static typeof(open) *video_open = open;
76 static typeof(close) *video_close = close;
77 static typeof(ioctl) *video_ioctl = ioctl;
78 static typeof(read) *video_read = read;
79 static typeof(mmap) *video_mmap = mmap;
80 static typeof(munmap) *video_munmap = munmap;
82 static void video_init(void)
84 #ifdef SONAME_LIBV4L1
85 static void *video_lib;
87 if (video_lib)
88 return;
89 video_lib = wine_dlopen(SONAME_LIBV4L1, RTLD_NOW, NULL, 0);
90 if (!video_lib)
91 return;
92 video_open = wine_dlsym(video_lib, "v4l1_open", NULL, 0);
93 video_close = wine_dlsym(video_lib, "v4l1_close", NULL, 0);
94 video_ioctl = wine_dlsym(video_lib, "v4l1_ioctl", NULL, 0);
95 video_read = wine_dlsym(video_lib, "v4l1_read", NULL, 0);
96 video_mmap = wine_dlsym(video_lib, "v4l1_mmap", NULL, 0);
97 video_munmap = wine_dlsym(video_lib, "v4l1_munmap", NULL, 0);
98 #endif
101 typedef void (* Renderer)(const Capture *, LPBYTE bufferin, const BYTE *stream);
103 struct _Capture
105 UINT width, height, bitDepth, fps, outputwidth, outputheight;
106 BOOL swresize;
108 CRITICAL_SECTION CritSect;
110 IPin *pOut;
111 int fd, mmap;
112 int iscommitted, stopped;
113 struct video_picture pict;
114 int dbrightness, dhue, dcolour, dcontrast;
116 /* mmap (V4l1) */
117 struct video_mmap *grab_buf;
118 struct video_mbuf gb_buffers;
119 unsigned char *pmap;
120 int buffers;
122 /* read (V4l1) */
123 int imagesize;
124 char * grab_data;
126 int curframe;
128 HANDLE thread;
129 Renderer renderer;
132 struct renderlist
134 int depth;
135 const char* name;
136 Renderer renderer;
139 static void renderer_RGB(const Capture *capBox, LPBYTE bufferin, const BYTE *stream);
140 static void renderer_YUV(const Capture *capBox, LPBYTE bufferin, const BYTE *stream);
142 static const struct renderlist renderlist_V4l[] = {
143 { 0, "NULL renderer", NULL },
144 { 8, "Gray scales", NULL }, /* 1, Don't support */
145 { 0, "High 240 cube (BT848)", NULL }, /* 2, Don't support */
146 { 16, "16 bit RGB (565)", NULL }, /* 3, Don't support */
147 { 24, "24 bit RGB values", renderer_RGB }, /* 4, Supported, */
148 { 32, "32 bit RGB values", renderer_RGB }, /* 5, Supported */
149 { 16, "15 bit RGB (555)", NULL }, /* 6, Don't support */
150 { 16, "YUV 422 (Not P)", renderer_YUV }, /* 7, Supported */
151 { 16, "YUYV (Not P)", renderer_YUV }, /* 8, Supported */
152 { 16, "UYVY (Not P)", renderer_YUV }, /* 9, Supported */
153 { 16, "YUV 420 (Not P)", NULL }, /* 10, Not supported, if I had to guess it's YYUYYV */
154 { 12, "YUV 411 (Not P)", renderer_YUV }, /* 11, Supported */
155 { 0, "Raw capturing (BT848)", NULL }, /* 12, Don't support */
156 { 16, "YUV 422 (Planar)", renderer_YUV }, /* 13, Supported */
157 { 12, "YUV 411 (Planar)", renderer_YUV }, /* 14, Supported */
158 { 12, "YUV 420 (Planar)", renderer_YUV }, /* 15, Supported */
159 { 10, "YUV 410 (Planar)", renderer_YUV }, /* 16, Supported */
160 /* FIXME: add YUV420 support */
161 { 0, NULL, NULL },
164 static const int fallback_V4l[] = { 4, 5, 7, 8, 9, 13, 15, 14, 16, 11, -1 };
165 /* Fallback: First try raw formats (Should try yuv first perhaps?), then yuv */
167 /* static const Capture defbox; */
169 static int xioctl(int fd, int request, void * arg)
171 int r;
173 do {
174 r = video_ioctl (fd, request, arg);
175 } while (-1 == r && EINTR == errno);
177 return r;
180 /* Prepare the capture buffers */
181 static HRESULT V4l_Prepare(Capture *capBox)
183 TRACE("%p: Preparing for %dx%d resolution\n", capBox, capBox->width, capBox->height);
185 /* Try mmap */
186 capBox->mmap = 0;
187 if (xioctl(capBox->fd, VIDIOCGMBUF, &capBox->gb_buffers) != -1 &&
188 capBox->gb_buffers.frames)
190 capBox->buffers = capBox->gb_buffers.frames;
191 if (capBox->gb_buffers.frames > 1)
192 capBox->buffers = 1;
193 TRACE("%p: Using %d/%d buffers\n", capBox,
194 capBox->buffers, capBox->gb_buffers.frames);
196 capBox->pmap = video_mmap( 0, capBox->gb_buffers.size, PROT_READ|PROT_WRITE,
197 MAP_SHARED, capBox->fd, 0 );
198 if (capBox->pmap != MAP_FAILED)
200 int i;
202 capBox->grab_buf = CoTaskMemAlloc(sizeof(struct video_mmap) * capBox->buffers);
203 if (!capBox->grab_buf)
205 video_munmap(capBox->pmap, capBox->gb_buffers.size);
206 return E_OUTOFMEMORY;
209 /* Setup mmap capture buffers. */
210 for (i = 0; i < capBox->buffers; i++)
212 capBox->grab_buf[i].format = capBox->pict.palette;
213 capBox->grab_buf[i].frame = i;
214 capBox->grab_buf[i].width = capBox->width;
215 capBox->grab_buf[i].height = capBox->height;
217 capBox->mmap = 1;
220 if (!capBox->mmap)
222 capBox->buffers = 1;
223 capBox->imagesize = renderlist_V4l[capBox->pict.palette].depth *
224 capBox->height * capBox->width / 8;
225 capBox->grab_data = CoTaskMemAlloc(capBox->imagesize);
226 if (!capBox->grab_data)
227 return E_OUTOFMEMORY;
229 TRACE("Using mmap: %d\n", capBox->mmap);
230 return S_OK;
233 static void V4l_Unprepare(Capture *capBox)
235 if (capBox->mmap)
237 for (capBox->curframe = 0; capBox->curframe < capBox->buffers; capBox->curframe++)
238 xioctl(capBox->fd, VIDIOCSYNC, &capBox->grab_buf[capBox->curframe]);
239 video_munmap(capBox->pmap, capBox->gb_buffers.size);
240 CoTaskMemFree(capBox->grab_buf);
242 else
243 CoTaskMemFree(capBox->grab_data);
246 HRESULT qcap_driver_destroy(Capture *capBox)
248 TRACE("%p\n", capBox);
250 if( capBox->fd != -1 )
251 video_close(capBox->fd);
252 capBox->CritSect.DebugInfo->Spare[0] = 0;
253 DeleteCriticalSection(&capBox->CritSect);
254 CoTaskMemFree(capBox);
255 return S_OK;
258 HRESULT qcap_driver_set_format(Capture *capBox, AM_MEDIA_TYPE * mT)
260 int newheight, newwidth;
261 struct video_window window;
262 VIDEOINFOHEADER *format;
264 TRACE("%p\n", capBox);
266 format = (VIDEOINFOHEADER *) mT->pbFormat;
267 if (format->bmiHeader.biBitCount != 24 ||
268 format->bmiHeader.biCompression != BI_RGB)
270 FIXME("unsupported media type %d %d\n", format->bmiHeader.biBitCount,
271 format->bmiHeader.biCompression );
272 return VFW_E_INVALIDMEDIATYPE;
275 newwidth = format->bmiHeader.biWidth;
276 newheight = format->bmiHeader.biHeight;
278 TRACE("%p -> (%p) - %d %d\n", capBox, mT, newwidth, newheight);
280 if (capBox->height == newheight && capBox->width == newwidth)
281 return S_OK;
283 if(-1 == xioctl(capBox->fd, VIDIOCGWIN, &window))
285 ERR("ioctl(VIDIOCGWIN) failed (%d)\n", errno);
286 return E_FAIL;
288 window.width = newwidth;
289 window.height = newheight;
290 if (xioctl(capBox->fd, VIDIOCSWIN, &window) == -1)
292 TRACE("using software resize: %dx%d -> %dx%d\n",
293 window.width, window.height, capBox->width, capBox->height);
294 capBox->swresize = TRUE;
296 else
298 capBox->height = window.height;
299 capBox->width = window.width;
300 capBox->swresize = FALSE;
302 capBox->outputwidth = window.width;
303 capBox->outputheight = window.height;
304 return S_OK;
307 HRESULT qcap_driver_get_format(const Capture *capBox, AM_MEDIA_TYPE ** mT)
309 VIDEOINFOHEADER *vi;
311 mT[0] = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
312 if (!mT[0])
313 return E_OUTOFMEMORY;
314 vi = CoTaskMemAlloc(sizeof(VIDEOINFOHEADER));
315 mT[0]->cbFormat = sizeof(VIDEOINFOHEADER);
316 if (!vi)
318 CoTaskMemFree(mT[0]);
319 return E_OUTOFMEMORY;
321 mT[0]->majortype = MEDIATYPE_Video;
322 mT[0]->subtype = MEDIASUBTYPE_RGB24;
323 mT[0]->formattype = FORMAT_VideoInfo;
324 mT[0]->bFixedSizeSamples = TRUE;
325 mT[0]->bTemporalCompression = FALSE;
326 mT[0]->pUnk = NULL;
327 mT[0]->lSampleSize = capBox->outputwidth * capBox->outputheight * capBox->bitDepth / 8;
328 TRACE("Output format: %dx%d - %d bits = %u KB\n", capBox->outputwidth,
329 capBox->outputheight, capBox->bitDepth, mT[0]->lSampleSize/1024);
330 vi->rcSource.left = 0; vi->rcSource.top = 0;
331 vi->rcTarget.left = 0; vi->rcTarget.top = 0;
332 vi->rcSource.right = capBox->width; vi->rcSource.bottom = capBox->height;
333 vi->rcTarget.right = capBox->outputwidth; vi->rcTarget.bottom = capBox->outputheight;
334 vi->dwBitRate = capBox->fps * mT[0]->lSampleSize;
335 vi->dwBitErrorRate = 0;
336 vi->AvgTimePerFrame = (LONGLONG)10000000.0 / (LONGLONG)capBox->fps;
337 vi->bmiHeader.biSize = 40;
338 vi->bmiHeader.biWidth = capBox->outputwidth;
339 vi->bmiHeader.biHeight = capBox->outputheight;
340 vi->bmiHeader.biPlanes = 1;
341 vi->bmiHeader.biBitCount = 24;
342 vi->bmiHeader.biCompression = BI_RGB;
343 vi->bmiHeader.biSizeImage = mT[0]->lSampleSize;
344 vi->bmiHeader.biClrUsed = vi->bmiHeader.biClrImportant = 0;
345 vi->bmiHeader.biXPelsPerMeter = 100;
346 vi->bmiHeader.biYPelsPerMeter = 100;
347 mT[0]->pbFormat = (void *)vi;
348 dump_AM_MEDIA_TYPE(mT[0]);
349 return S_OK;
352 HRESULT qcap_driver_get_prop_range( Capture *capBox,
353 VideoProcAmpProperty Property, LONG *pMin, LONG *pMax,
354 LONG *pSteppingDelta, LONG *pDefault, LONG *pCapsFlags )
356 TRACE("%p -> %d %p %p %p %p %p\n", capBox, Property,
357 pMin, pMax, pSteppingDelta, pDefault, pCapsFlags);
359 switch (Property)
361 case VideoProcAmp_Brightness:
362 *pDefault = capBox->dbrightness;
363 break;
364 case VideoProcAmp_Contrast:
365 *pDefault = capBox->dcontrast;
366 break;
367 case VideoProcAmp_Hue:
368 *pDefault = capBox->dhue;
369 break;
370 case VideoProcAmp_Saturation:
371 *pDefault = capBox->dcolour;
372 break;
373 default:
374 FIXME("Not implemented %d\n", Property);
375 return E_NOTIMPL;
377 *pMin = 0;
378 *pMax = 65535;
379 *pSteppingDelta = 65536/256;
380 *pCapsFlags = VideoProcAmp_Flags_Manual;
381 return S_OK;
384 HRESULT qcap_driver_get_prop( Capture *capBox,
385 VideoProcAmpProperty Property, LONG *lValue, LONG *Flags )
387 TRACE("%p -> %d %p %p\n", capBox, Property, lValue, Flags);
389 switch (Property)
391 case VideoProcAmp_Brightness:
392 *lValue = capBox->pict.brightness;
393 break;
394 case VideoProcAmp_Contrast:
395 *lValue = capBox->pict.contrast;
396 break;
397 case VideoProcAmp_Hue:
398 *lValue = capBox->pict.hue;
399 break;
400 case VideoProcAmp_Saturation:
401 *lValue = capBox->pict.colour;
402 break;
403 default:
404 FIXME("Not implemented %d\n", Property);
405 return E_NOTIMPL;
407 *Flags = VideoProcAmp_Flags_Manual;
408 return S_OK;
411 HRESULT qcap_driver_set_prop(Capture *capBox, VideoProcAmpProperty Property,
412 LONG lValue, LONG Flags)
414 TRACE("%p -> %d %d %d\n", capBox, Property, lValue, Flags);
416 switch (Property)
418 case VideoProcAmp_Brightness:
419 capBox->pict.brightness = lValue;
420 break;
421 case VideoProcAmp_Contrast:
422 capBox->pict.contrast = lValue;
423 break;
424 case VideoProcAmp_Hue:
425 capBox->pict.hue = lValue;
426 break;
427 case VideoProcAmp_Saturation:
428 capBox->pict.colour = lValue;
429 break;
430 default:
431 FIXME("Not implemented %d\n", Property);
432 return E_NOTIMPL;
435 if (xioctl(capBox->fd, VIDIOCSPICT, &capBox->pict) == -1)
437 ERR("ioctl(VIDIOCSPICT) failed (%d)\n",errno);
438 return E_FAIL;
440 return S_OK;
443 static void renderer_RGB(const Capture *capBox, LPBYTE bufferin, const BYTE *stream)
445 int depth = renderlist_V4l[capBox->pict.palette].depth;
446 int size = capBox->height * capBox->width * depth / 8;
447 int pointer, offset;
449 switch (depth)
451 case 24:
452 memcpy(bufferin, stream, size);
453 break;
454 case 32:
455 pointer = 0;
456 offset = 1;
457 while (pointer + offset <= size)
459 bufferin[pointer] = stream[pointer + offset];
460 pointer++;
461 bufferin[pointer] = stream[pointer + offset];
462 pointer++;
463 bufferin[pointer] = stream[pointer + offset];
464 pointer++;
465 offset++;
467 break;
468 default:
469 ERR("Unknown bit depth %d\n", depth);
470 return;
474 static void renderer_YUV(const Capture *capBox, LPBYTE bufferin, const BYTE *stream)
476 enum YUV_Format format;
478 switch (capBox->pict.palette)
480 case 7: /* YUV422 - same as YUYV */
481 case 8: /* YUYV */
482 format = YUYV;
483 break;
484 case 9: /* UYVY */
485 format = UYVY;
486 break;
487 case 11: /* YUV411 */
488 format = UYYVYY;
489 break;
490 case 13: /* YUV422P */
491 format = YUVP_421;
492 break;
493 case 14: /* YUV411P */
494 format = YUVP_441;
495 break;
496 case 15: /* YUV420P */
497 format = YUVP_422;
498 break;
499 case 16: /* YUV410P */
500 format = YUVP_444;
501 break;
502 default:
503 ERR("Unknown palette %d\n", capBox->pict.palette);
504 return;
506 YUV_To_RGB24(format, bufferin, stream, capBox->width, capBox->height);
509 static void Resize(const Capture * capBox, LPBYTE output, const BYTE *input)
511 /* the whole image needs to be reversed,
512 because the dibs are messed up in windows */
513 if (!capBox->swresize)
515 int depth = capBox->bitDepth / 8;
516 int inoffset = 0, outoffset = capBox->height * capBox->width * depth;
517 int ow = capBox->width * depth;
518 while (outoffset > 0)
520 int x;
521 outoffset -= ow;
522 for (x = 0; x < ow; x++)
523 output[outoffset + x] = input[inoffset + x];
524 inoffset += ow;
527 else
529 HDC dc_s, dc_d;
530 HBITMAP bmp_s, bmp_d;
531 int depth = capBox->bitDepth / 8;
532 int inoffset = 0, outoffset = (capBox->outputheight) * capBox->outputwidth * depth;
533 int ow = capBox->outputwidth * depth;
534 LPBYTE myarray;
536 /* FIXME: Improve software resizing: add error checks and optimize */
538 myarray = CoTaskMemAlloc(capBox->outputwidth * capBox->outputheight * depth);
539 dc_s = CreateCompatibleDC(NULL);
540 dc_d = CreateCompatibleDC(NULL);
541 bmp_s = CreateBitmap(capBox->width, capBox->height, 1, capBox->bitDepth, input);
542 bmp_d = CreateBitmap(capBox->outputwidth, capBox->outputheight, 1, capBox->bitDepth, NULL);
543 SelectObject(dc_s, bmp_s);
544 SelectObject(dc_d, bmp_d);
545 StretchBlt(dc_d, 0, 0, capBox->outputwidth, capBox->outputheight,
546 dc_s, 0, 0, capBox->width, capBox->height, SRCCOPY);
547 GetBitmapBits(bmp_d, capBox->outputwidth * capBox->outputheight * depth, myarray);
548 while (outoffset > 0)
550 int i;
552 outoffset -= ow;
553 for (i = 0; i < ow; i++)
554 output[outoffset + i] = myarray[inoffset + i];
555 inoffset += ow;
557 CoTaskMemFree(myarray);
558 DeleteObject(dc_s);
559 DeleteObject(dc_d);
560 DeleteObject(bmp_s);
561 DeleteObject(bmp_d);
565 static void V4l_GetFrame(Capture * capBox, unsigned char ** pInput)
567 if (capBox->mmap)
569 if (xioctl(capBox->fd, VIDIOCSYNC, &capBox->grab_buf[capBox->curframe]) == -1)
570 WARN("Syncing ioctl failed: %d\n", errno);
572 *pInput = capBox->pmap + capBox->gb_buffers.offsets[capBox->curframe];
574 else
576 int retval;
577 while ((retval = video_read(capBox->fd, capBox->grab_data, capBox->imagesize)) == -1)
578 if (errno != EAGAIN) break;
579 if (retval == -1)
580 WARN("Error occurred while reading from device: %s\n", strerror(errno));
581 *pInput = (unsigned char*) capBox->grab_data;
585 static void V4l_FreeFrame(Capture * capBox)
587 TRACE("\n");
588 if (capBox->mmap)
590 if (xioctl(capBox->fd, VIDIOCMCAPTURE, &capBox->grab_buf[capBox->curframe]) == -1)
591 ERR("Freeing frame for capture failed: %s\n", strerror(errno));
593 if (++capBox->curframe == capBox->buffers)
594 capBox->curframe = 0;
597 static DWORD WINAPI ReadThread(LPVOID lParam)
599 Capture * capBox = lParam;
600 HRESULT hr;
601 IMediaSample *pSample = NULL;
602 ULONG framecount = 0;
603 unsigned char *pTarget, *pInput, *pOutput;
605 hr = V4l_Prepare(capBox);
606 if (FAILED(hr))
607 goto fail;
609 pOutput = CoTaskMemAlloc(capBox->width * capBox->height * capBox->bitDepth / 8);
610 capBox->curframe = 0;
611 do {
612 V4l_FreeFrame(capBox);
613 } while (capBox->curframe != 0);
615 while (1)
617 EnterCriticalSection(&capBox->CritSect);
618 if (capBox->stopped)
619 break;
620 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin *)capBox->pOut, &pSample, NULL, NULL, 0);
621 if (SUCCEEDED(hr))
623 int len;
625 if (!capBox->swresize)
626 len = capBox->height * capBox->width * capBox->bitDepth / 8;
627 else
628 len = capBox->outputheight * capBox->outputwidth * capBox->bitDepth / 8;
629 IMediaSample_SetActualDataLength(pSample, len);
631 len = IMediaSample_GetActualDataLength(pSample);
632 TRACE("Data length: %d KB\n", len / 1024);
634 IMediaSample_GetPointer(pSample, &pTarget);
635 /* FIXME: Check return values.. */
636 V4l_GetFrame(capBox, &pInput);
637 capBox->renderer(capBox, pOutput, pInput);
638 Resize(capBox, pTarget, pOutput);
639 hr = BaseOutputPinImpl_Deliver((BaseOutputPin *)capBox->pOut, pSample);
640 TRACE("%p -> Frame %u: %x\n", capBox, ++framecount, hr);
641 IMediaSample_Release(pSample);
642 V4l_FreeFrame(capBox);
644 LeaveCriticalSection(&capBox->CritSect);
645 if (FAILED(hr) && hr != VFW_E_NOT_CONNECTED)
647 ERR("Received error: %x\n", hr);
648 goto cfail;
651 LeaveCriticalSection(&capBox->CritSect);
652 CoTaskMemFree(pOutput);
654 return 0;
656 cfail:
657 CoTaskMemFree(pOutput);
658 V4l_Unprepare(capBox);
659 LeaveCriticalSection(&capBox->CritSect);
661 fail:
662 capBox->thread = 0; capBox->stopped = 1;
663 FIXME("Stop IFilterGraph\n");
664 return 0;
667 HRESULT qcap_driver_run(Capture *capBox, FILTER_STATE *state)
669 HANDLE thread;
670 HRESULT hr;
672 TRACE("%p -> (%p)\n", capBox, state);
674 if (*state == State_Running) return S_OK;
676 EnterCriticalSection(&capBox->CritSect);
678 capBox->stopped = 0;
680 if (*state == State_Stopped)
682 *state = State_Running;
683 if (!capBox->iscommitted++)
685 IMemAllocator * pAlloc = NULL;
686 ALLOCATOR_PROPERTIES ap, actual;
687 BaseOutputPin *out;
689 ap.cBuffers = 3;
690 if (!capBox->swresize)
691 ap.cbBuffer = capBox->width * capBox->height;
692 else
693 ap.cbBuffer = capBox->outputwidth * capBox->outputheight;
694 ap.cbBuffer = (ap.cbBuffer * capBox->bitDepth) / 8;
695 ap.cbAlign = 1;
696 ap.cbPrefix = 0;
698 out = (BaseOutputPin *)capBox->pOut;
699 hr = IMemInputPin_GetAllocator(out->pMemInputPin, &pAlloc);
701 if (SUCCEEDED(hr))
702 hr = IMemAllocator_SetProperties(pAlloc, &ap, &actual);
704 if (SUCCEEDED(hr))
705 hr = IMemAllocator_Commit(pAlloc);
707 if (pAlloc)
708 IMemAllocator_Release(pAlloc);
710 TRACE("Committing allocator: %x\n", hr);
713 thread = CreateThread(NULL, 0, ReadThread, capBox, 0, NULL);
714 if (thread)
716 capBox->thread = thread;
717 SetThreadPriority(thread, THREAD_PRIORITY_LOWEST);
718 LeaveCriticalSection(&capBox->CritSect);
719 return S_OK;
721 ERR("Creating thread failed.. %u\n", GetLastError());
722 LeaveCriticalSection(&capBox->CritSect);
723 return E_FAIL;
726 ResumeThread(capBox->thread);
727 *state = State_Running;
728 LeaveCriticalSection(&capBox->CritSect);
729 return S_OK;
732 HRESULT qcap_driver_pause(Capture *capBox, FILTER_STATE *state)
734 TRACE("%p -> (%p)\n", capBox, state);
736 if (*state == State_Paused)
737 return S_OK;
738 if (*state == State_Stopped)
739 qcap_driver_run(capBox, state);
741 EnterCriticalSection(&capBox->CritSect);
742 *state = State_Paused;
743 SuspendThread(capBox->thread);
744 LeaveCriticalSection(&capBox->CritSect);
746 return S_OK;
749 HRESULT qcap_driver_stop(Capture *capBox, FILTER_STATE *state)
751 TRACE("%p -> (%p)\n", capBox, state);
753 if (*state == State_Stopped)
754 return S_OK;
756 EnterCriticalSection(&capBox->CritSect);
758 if (capBox->thread)
760 if (*state == State_Paused)
761 ResumeThread(capBox->thread);
762 capBox->stopped = 1;
763 capBox->thread = 0;
764 if (capBox->iscommitted)
766 IMemInputPin *pMem = NULL;
767 IMemAllocator * pAlloc = NULL;
768 IPin *pConnect = NULL;
769 HRESULT hr;
771 capBox->iscommitted = 0;
773 hr = IPin_ConnectedTo(capBox->pOut, &pConnect);
775 if (SUCCEEDED(hr))
776 hr = IPin_QueryInterface(pConnect, &IID_IMemInputPin, (void **) &pMem);
778 if (SUCCEEDED(hr))
779 hr = IMemInputPin_GetAllocator(pMem, &pAlloc);
781 if (SUCCEEDED(hr))
782 hr = IMemAllocator_Decommit(pAlloc);
784 if (pAlloc)
785 IMemAllocator_Release(pAlloc);
787 if (pMem)
788 IMemInputPin_Release(pMem);
790 if (pConnect)
791 IPin_Release(pConnect);
793 if (hr != S_OK && hr != VFW_E_NOT_COMMITTED)
794 WARN("Decommitting allocator: %x\n", hr);
796 V4l_Unprepare(capBox);
799 *state = State_Stopped;
800 LeaveCriticalSection(&capBox->CritSect);
801 return S_OK;
804 Capture * qcap_driver_init( IPin *pOut, USHORT card )
806 Capture * capBox = NULL;
807 char device[20];
808 struct video_capability capa;
809 struct video_picture pict;
810 struct video_window window;
812 YUV_Init();
813 video_init();
815 capBox = CoTaskMemAlloc(sizeof(Capture));
816 if (!capBox)
817 goto error;
819 /* capBox->vtbl = &defboxVtbl; */
821 InitializeCriticalSection( &capBox->CritSect );
822 capBox->CritSect.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Capture.CritSect");
824 sprintf(device, "/dev/video%i", card);
825 TRACE("opening %s\n", device);
826 capBox->fd = video_open(device, O_RDWR | O_NONBLOCK);
827 if (capBox->fd == -1)
829 WARN("open failed (%d)\n", errno);
830 goto error;
833 memset(&capa, 0, sizeof(capa));
835 if (xioctl(capBox->fd, VIDIOCGCAP, &capa) == -1)
837 WARN("ioctl(VIDIOCGCAP) failed (%d)\n", errno);
838 goto error;
841 if (!(capa.type & VID_TYPE_CAPTURE))
843 WARN("not a video capture device\n");
844 goto error;
847 TRACE("%d inputs on %s\n", capa.channels, capa.name );
849 if (xioctl(capBox->fd, VIDIOCGPICT, &pict) == -1)
851 ERR("ioctl(VIDIOCGPICT) failed (%d)\n", errno );
852 goto error;
855 TRACE("depth %d palette %d (%s) hue %d color %d contrast %d\n",
856 pict.depth, pict.palette, renderlist_V4l[pict.palette].name,
857 pict.hue, pict.colour, pict.contrast );
859 capBox->dbrightness = pict.brightness;
860 capBox->dcolour = pict.colour;
861 capBox->dhue = pict.hue;
862 capBox->dcontrast = pict.contrast;
864 if (!renderlist_V4l[pict.palette].renderer)
866 int palet = pict.palette, i;
868 TRACE("No renderer available for %s, falling back to defaults\n",
869 renderlist_V4l[pict.palette].name);
870 capBox->renderer = NULL;
871 for (i = 0; fallback_V4l[i] >=0 ; i++)
873 int n = fallback_V4l[i];
875 if (renderlist_V4l[n].renderer == NULL)
876 continue;
878 pict.depth = renderlist_V4l[n].depth;
879 pict.palette = n;
880 if (xioctl(capBox->fd, VIDIOCSPICT, &pict) == -1)
882 TRACE("Could not render with %s (%d)\n",
883 renderlist_V4l[n].name, n);
884 continue;
886 TRACE("using renderer %s (%d)\n",
887 renderlist_V4l[n].name, n);
888 capBox->renderer = renderlist_V4l[n].renderer;
889 break;
892 if (!capBox->renderer)
894 ERR("video format %s isn't available\n",
895 renderlist_V4l[palet].name);
896 goto error;
899 else
901 TRACE("Using the suggested format\n");
902 capBox->renderer = renderlist_V4l[pict.palette].renderer;
904 memcpy(&capBox->pict, &pict, sizeof(struct video_picture));
906 memset(&window, 0, sizeof(window));
907 if (xioctl(capBox->fd, VIDIOCGWIN, &window) == -1)
909 WARN("VIDIOCGWIN failed (%d)\n", errno);
910 goto error;
913 capBox->height = capBox->outputheight = window.height;
914 capBox->width = capBox->outputwidth = window.width;
915 capBox->swresize = FALSE;
916 capBox->bitDepth = 24;
917 capBox->pOut = pOut;
918 capBox->fps = 3;
919 capBox->stopped = 0;
920 capBox->curframe = 0;
921 capBox->iscommitted = 0;
923 TRACE("format: %d bits - %d x %d\n", capBox->bitDepth, capBox->width, capBox->height);
925 return capBox;
927 error:
928 if (capBox)
929 qcap_driver_destroy( capBox );
931 return NULL;
934 #else
936 Capture * qcap_driver_init( IPin *pOut, USHORT card )
938 const char msg[] =
939 "The v4l headers were not available at compile time,\n"
940 "so video capture support is not available.\n";
941 MESSAGE(msg);
942 return NULL;
945 #define FAIL_WITH_ERR \
946 ERR("v4l absent: shouldn't be called\n"); \
947 return E_NOTIMPL
949 HRESULT qcap_driver_destroy(Capture *capBox)
951 FAIL_WITH_ERR;
954 HRESULT qcap_driver_set_format(Capture *capBox, AM_MEDIA_TYPE * mT)
956 FAIL_WITH_ERR;
959 HRESULT qcap_driver_get_format(const Capture *capBox, AM_MEDIA_TYPE ** mT)
961 FAIL_WITH_ERR;
964 HRESULT qcap_driver_get_prop_range( Capture *capBox,
965 VideoProcAmpProperty Property, LONG *pMin, LONG *pMax,
966 LONG *pSteppingDelta, LONG *pDefault, LONG *pCapsFlags )
968 FAIL_WITH_ERR;
971 HRESULT qcap_driver_get_prop(Capture *capBox,
972 VideoProcAmpProperty Property, LONG *lValue, LONG *Flags)
974 FAIL_WITH_ERR;
977 HRESULT qcap_driver_set_prop(Capture *capBox, VideoProcAmpProperty Property,
978 LONG lValue, LONG Flags)
980 FAIL_WITH_ERR;
983 HRESULT qcap_driver_run(Capture *capBox, FILTER_STATE *state)
985 FAIL_WITH_ERR;
988 HRESULT qcap_driver_pause(Capture *capBox, FILTER_STATE *state)
990 FAIL_WITH_ERR;
993 HRESULT qcap_driver_stop(Capture *capBox, FILTER_STATE *state)
995 FAIL_WITH_ERR;
998 #endif /* HAVE_LINUX_VIDEODEV_H */