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
26 #include "wine/port.h"
27 #include "wine/library.h"
29 #define NONAMELESSSTRUCT
30 #define NONAMELESSUNION
42 #include "wine/debug.h"
45 #include "qcap_main.h"
51 #ifdef HAVE_SYS_IOCTL_H
52 #include <sys/ioctl.h>
54 #ifdef HAVE_SYS_MMAN_H
57 #ifdef HAVE_SYS_ERRNO_H
58 #include <sys/errno.h>
60 #ifdef HAVE_SYS_TIME_H
63 #ifdef HAVE_ASM_TYPES_H
64 #include <asm/types.h>
66 #ifdef HAVE_LINUX_VIDEODEV_H
67 #include <linux/videodev.h>
73 WINE_DEFAULT_DEBUG_CHANNEL(qcap_v4l
);
75 #ifdef HAVE_LINUX_VIDEODEV_H
77 static typeof(open
) *video_open
= open
;
78 static typeof(close
) *video_close
= close
;
79 static typeof(ioctl
) *video_ioctl
= ioctl
;
80 static typeof(read
) *video_read
= read
;
81 static typeof(mmap
) *video_mmap
= mmap
;
82 static typeof(munmap
) *video_munmap
= munmap
;
84 static void video_init(void)
87 static void *video_lib
;
91 video_lib
= wine_dlopen(SONAME_LIBV4L1
, RTLD_NOW
, NULL
, 0);
94 video_open
= wine_dlsym(video_lib
, "v4l1_open", NULL
, 0);
95 video_close
= wine_dlsym(video_lib
, "v4l1_close", NULL
, 0);
96 video_ioctl
= wine_dlsym(video_lib
, "v4l1_ioctl", NULL
, 0);
97 video_read
= wine_dlsym(video_lib
, "v4l1_read", NULL
, 0);
98 video_mmap
= wine_dlsym(video_lib
, "v4l1_mmap", NULL
, 0);
99 video_munmap
= wine_dlsym(video_lib
, "v4l1_munmap", NULL
, 0);
103 typedef void (* Renderer
)(const Capture
*, LPBYTE bufferin
, const BYTE
*stream
);
107 UINT width
, height
, bitDepth
, fps
, outputwidth
, outputheight
;
110 CRITICAL_SECTION CritSect
;
114 int iscommitted
, stopped
;
115 struct video_picture pict
;
116 int dbrightness
, dhue
, dcolour
, dcontrast
;
119 struct video_mmap
*grab_buf
;
120 struct video_mbuf gb_buffers
;
141 static void renderer_RGB(const Capture
*capBox
, LPBYTE bufferin
, const BYTE
*stream
);
142 static void renderer_YUV(const Capture
*capBox
, LPBYTE bufferin
, const BYTE
*stream
);
144 static const struct renderlist renderlist_V4l
[] = {
145 { 0, "NULL renderer", NULL
},
146 { 8, "Gray scales", NULL
}, /* 1, Don't support */
147 { 0, "High 240 cube (BT848)", NULL
}, /* 2, Don't support */
148 { 16, "16 bit RGB (565)", NULL
}, /* 3, Don't support */
149 { 24, "24 bit RGB values", renderer_RGB
}, /* 4, Supported, */
150 { 32, "32 bit RGB values", renderer_RGB
}, /* 5, Supported */
151 { 16, "15 bit RGB (555)", NULL
}, /* 6, Don't support */
152 { 16, "YUV 422 (Not P)", renderer_YUV
}, /* 7, Supported */
153 { 16, "YUYV (Not P)", renderer_YUV
}, /* 8, Supported */
154 { 16, "UYVY (Not P)", renderer_YUV
}, /* 9, Supported */
155 { 16, "YUV 420 (Not P)", NULL
}, /* 10, Not supported, if I had to guess it's YYUYYV */
156 { 12, "YUV 411 (Not P)", renderer_YUV
}, /* 11, Supported */
157 { 0, "Raw capturing (BT848)", NULL
}, /* 12, Don't support */
158 { 16, "YUV 422 (Planar)", renderer_YUV
}, /* 13, Supported */
159 { 12, "YUV 411 (Planar)", renderer_YUV
}, /* 14, Supported */
160 { 12, "YUV 420 (Planar)", renderer_YUV
}, /* 15, Supported */
161 { 10, "YUV 410 (Planar)", renderer_YUV
}, /* 16, Supported */
162 /* FIXME: add YUV420 support */
166 static const int fallback_V4l
[] = { 4, 5, 7, 8, 9, 13, 15, 14, 16, 11, -1 };
167 /* Fallback: First try raw formats (Should try yuv first perhaps?), then yuv */
169 /* static const Capture defbox; */
171 static int xioctl(int fd
, int request
, void * arg
)
176 r
= video_ioctl (fd
, request
, arg
);
177 } while (-1 == r
&& EINTR
== errno
);
182 /* Prepare the capture buffers */
183 static HRESULT
V4l_Prepare(Capture
*capBox
)
185 TRACE("%p: Preparing for %dx%d resolution\n", capBox
, capBox
->width
, capBox
->height
);
189 if (xioctl(capBox
->fd
, VIDIOCGMBUF
, &capBox
->gb_buffers
) != -1 &&
190 capBox
->gb_buffers
.frames
)
192 capBox
->buffers
= capBox
->gb_buffers
.frames
;
193 if (capBox
->gb_buffers
.frames
> 1)
195 TRACE("%p: Using %d/%d buffers\n", capBox
,
196 capBox
->buffers
, capBox
->gb_buffers
.frames
);
198 capBox
->pmap
= video_mmap( 0, capBox
->gb_buffers
.size
, PROT_READ
|PROT_WRITE
,
199 MAP_SHARED
, capBox
->fd
, 0 );
200 if (capBox
->pmap
!= MAP_FAILED
)
204 capBox
->grab_buf
= CoTaskMemAlloc(sizeof(struct video_mmap
) * capBox
->buffers
);
205 if (!capBox
->grab_buf
)
207 video_munmap(capBox
->pmap
, capBox
->gb_buffers
.size
);
208 return E_OUTOFMEMORY
;
211 /* Setup mmap capture buffers. */
212 for (i
= 0; i
< capBox
->buffers
; i
++)
214 capBox
->grab_buf
[i
].format
= capBox
->pict
.palette
;
215 capBox
->grab_buf
[i
].frame
= i
;
216 capBox
->grab_buf
[i
].width
= capBox
->width
;
217 capBox
->grab_buf
[i
].height
= capBox
->height
;
225 capBox
->imagesize
= renderlist_V4l
[capBox
->pict
.palette
].depth
*
226 capBox
->height
* capBox
->width
/ 8;
227 capBox
->grab_data
= CoTaskMemAlloc(capBox
->imagesize
);
228 if (!capBox
->grab_data
)
229 return E_OUTOFMEMORY
;
231 TRACE("Using mmap: %d\n", capBox
->mmap
);
235 static void V4l_Unprepare(Capture
*capBox
)
239 for (capBox
->curframe
= 0; capBox
->curframe
< capBox
->buffers
; capBox
->curframe
++)
240 xioctl(capBox
->fd
, VIDIOCSYNC
, &capBox
->grab_buf
[capBox
->curframe
]);
241 video_munmap(capBox
->pmap
, capBox
->gb_buffers
.size
);
242 CoTaskMemFree(capBox
->grab_buf
);
245 CoTaskMemFree(capBox
->grab_data
);
248 HRESULT
qcap_driver_destroy(Capture
*capBox
)
250 TRACE("%p\n", capBox
);
252 if( capBox
->fd
!= -1 )
253 video_close(capBox
->fd
);
254 capBox
->CritSect
.DebugInfo
->Spare
[0] = 0;
255 DeleteCriticalSection(&capBox
->CritSect
);
256 CoTaskMemFree(capBox
);
260 HRESULT
qcap_driver_set_format(Capture
*capBox
, AM_MEDIA_TYPE
* mT
)
262 int newheight
, newwidth
;
263 struct video_window window
;
264 VIDEOINFOHEADER
*format
;
266 TRACE("%p\n", capBox
);
268 format
= (VIDEOINFOHEADER
*) mT
->pbFormat
;
269 if (format
->bmiHeader
.biBitCount
!= 24 ||
270 format
->bmiHeader
.biCompression
!= BI_RGB
)
272 FIXME("unsupported media type %d %d\n", format
->bmiHeader
.biBitCount
,
273 format
->bmiHeader
.biCompression
);
274 return VFW_E_INVALIDMEDIATYPE
;
277 newwidth
= format
->bmiHeader
.biWidth
;
278 newheight
= format
->bmiHeader
.biHeight
;
280 TRACE("%p -> (%p) - %d %d\n", capBox
, mT
, newwidth
, newheight
);
282 if (capBox
->height
== newheight
&& capBox
->width
== newwidth
)
285 if(-1 == xioctl(capBox
->fd
, VIDIOCGWIN
, &window
))
287 ERR("ioctl(VIDIOCGWIN) failed (%d)\n", errno
);
290 window
.width
= newwidth
;
291 window
.height
= newheight
;
292 if (xioctl(capBox
->fd
, VIDIOCSWIN
, &window
) == -1)
294 TRACE("using software resize: %dx%d -> %dx%d\n",
295 window
.width
, window
.height
, capBox
->width
, capBox
->height
);
296 capBox
->swresize
= TRUE
;
300 capBox
->height
= window
.height
;
301 capBox
->width
= window
.width
;
302 capBox
->swresize
= FALSE
;
304 capBox
->outputwidth
= window
.width
;
305 capBox
->outputheight
= window
.height
;
309 HRESULT
qcap_driver_get_format(const Capture
*capBox
, AM_MEDIA_TYPE
** mT
)
313 mT
[0] = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
315 return E_OUTOFMEMORY
;
316 vi
= CoTaskMemAlloc(sizeof(VIDEOINFOHEADER
));
317 mT
[0]->cbFormat
= sizeof(VIDEOINFOHEADER
);
320 CoTaskMemFree(mT
[0]);
321 return E_OUTOFMEMORY
;
323 mT
[0]->majortype
= MEDIATYPE_Video
;
324 mT
[0]->subtype
= MEDIASUBTYPE_RGB24
;
325 mT
[0]->formattype
= FORMAT_VideoInfo
;
326 mT
[0]->bFixedSizeSamples
= TRUE
;
327 mT
[0]->bTemporalCompression
= FALSE
;
329 mT
[0]->lSampleSize
= capBox
->outputwidth
* capBox
->outputheight
* capBox
->bitDepth
/ 8;
330 TRACE("Output format: %dx%d - %d bits = %u KB\n", capBox
->outputwidth
,
331 capBox
->outputheight
, capBox
->bitDepth
, mT
[0]->lSampleSize
/1024);
332 vi
->rcSource
.left
= 0; vi
->rcSource
.top
= 0;
333 vi
->rcTarget
.left
= 0; vi
->rcTarget
.top
= 0;
334 vi
->rcSource
.right
= capBox
->width
; vi
->rcSource
.bottom
= capBox
->height
;
335 vi
->rcTarget
.right
= capBox
->outputwidth
; vi
->rcTarget
.bottom
= capBox
->outputheight
;
336 vi
->dwBitRate
= capBox
->fps
* mT
[0]->lSampleSize
;
337 vi
->dwBitErrorRate
= 0;
338 vi
->AvgTimePerFrame
= (LONGLONG
)10000000.0 / (LONGLONG
)capBox
->fps
;
339 vi
->bmiHeader
.biSize
= 40;
340 vi
->bmiHeader
.biWidth
= capBox
->outputwidth
;
341 vi
->bmiHeader
.biHeight
= capBox
->outputheight
;
342 vi
->bmiHeader
.biPlanes
= 1;
343 vi
->bmiHeader
.biBitCount
= 24;
344 vi
->bmiHeader
.biCompression
= BI_RGB
;
345 vi
->bmiHeader
.biSizeImage
= mT
[0]->lSampleSize
;
346 vi
->bmiHeader
.biClrUsed
= vi
->bmiHeader
.biClrImportant
= 0;
347 vi
->bmiHeader
.biXPelsPerMeter
= 100;
348 vi
->bmiHeader
.biYPelsPerMeter
= 100;
349 mT
[0]->pbFormat
= (void *)vi
;
350 dump_AM_MEDIA_TYPE(mT
[0]);
354 HRESULT
qcap_driver_get_prop_range( Capture
*capBox
, LONG Property
, LONG
*pMin
,
355 LONG
*pMax
, LONG
*pSteppingDelta
, LONG
*pDefault
, LONG
*pCapsFlags
)
357 TRACE("%p -> %d %p %p %p %p %p\n", capBox
, Property
,
358 pMin
, pMax
, pSteppingDelta
, pDefault
, pCapsFlags
);
362 case VideoProcAmp_Brightness
:
363 *pDefault
= capBox
->dbrightness
;
365 case VideoProcAmp_Contrast
:
366 *pDefault
= capBox
->dcontrast
;
368 case VideoProcAmp_Hue
:
369 *pDefault
= capBox
->dhue
;
371 case VideoProcAmp_Saturation
:
372 *pDefault
= capBox
->dcolour
;
375 FIXME("Not implemented %d\n", Property
);
380 *pSteppingDelta
= 65536/256;
381 *pCapsFlags
= VideoProcAmp_Flags_Manual
;
385 HRESULT
qcap_driver_get_prop( Capture
*capBox
, LONG Property
, LONG
*lValue
, LONG
*Flags
)
387 TRACE("%p -> %d %p %p\n", capBox
, Property
, lValue
, Flags
);
391 case VideoProcAmp_Brightness
:
392 *lValue
= capBox
->pict
.brightness
;
394 case VideoProcAmp_Contrast
:
395 *lValue
= capBox
->pict
.contrast
;
397 case VideoProcAmp_Hue
:
398 *lValue
= capBox
->pict
.hue
;
400 case VideoProcAmp_Saturation
:
401 *lValue
= capBox
->pict
.colour
;
404 FIXME("Not implemented %d\n", Property
);
407 *Flags
= VideoProcAmp_Flags_Manual
;
411 HRESULT
qcap_driver_set_prop(Capture
*capBox
, long Property
, long lValue
, long Flags
)
413 TRACE("%p -> %ld %ld %ld\n", capBox
, Property
, lValue
, Flags
);
417 case VideoProcAmp_Brightness
:
418 capBox
->pict
.brightness
= lValue
;
420 case VideoProcAmp_Contrast
:
421 capBox
->pict
.contrast
= lValue
;
423 case VideoProcAmp_Hue
:
424 capBox
->pict
.hue
= lValue
;
426 case VideoProcAmp_Saturation
:
427 capBox
->pict
.colour
= lValue
;
430 FIXME("Not implemented %ld\n", Property
);
434 if (xioctl(capBox
->fd
, VIDIOCSPICT
, &capBox
->pict
) == -1)
436 ERR("ioctl(VIDIOCSPICT) failed (%d)\n",errno
);
442 static void renderer_RGB(const Capture
*capBox
, LPBYTE bufferin
, const BYTE
*stream
)
444 int depth
= renderlist_V4l
[capBox
->pict
.palette
].depth
;
445 int size
= capBox
->height
* capBox
->width
* depth
/ 8;
451 memcpy(bufferin
, stream
, size
);
456 while (pointer
+ offset
<= size
)
458 bufferin
[pointer
] = stream
[pointer
+ offset
];
460 bufferin
[pointer
] = stream
[pointer
+ offset
];
462 bufferin
[pointer
] = stream
[pointer
+ offset
];
468 ERR("Unknown bit depth %d\n", depth
);
473 static void renderer_YUV(const Capture
*capBox
, LPBYTE bufferin
, const BYTE
*stream
)
475 enum YUV_Format format
;
477 switch (capBox
->pict
.palette
)
479 case 7: /* YUV422 - same as YUYV */
486 case 11: /* YUV411 */
489 case 13: /* YUV422P */
492 case 14: /* YUV411P */
495 case 15: /* YUV420P */
498 case 16: /* YUV410P */
502 ERR("Unknown palette %d\n", capBox
->pict
.palette
);
505 YUV_To_RGB24(format
, bufferin
, stream
, capBox
->width
, capBox
->height
);
508 static void Resize(const Capture
* capBox
, LPBYTE output
, const BYTE
*input
)
510 /* the whole image needs to be reversed,
511 because the dibs are messed up in windows */
512 if (!capBox
->swresize
)
514 int depth
= capBox
->bitDepth
/ 8;
515 int inoffset
= 0, outoffset
= capBox
->height
* capBox
->width
* depth
;
516 int ow
= capBox
->width
* depth
;
517 while (outoffset
> 0)
521 for (x
= 0; x
< ow
; x
++)
522 output
[outoffset
+ x
] = input
[inoffset
+ x
];
529 HBITMAP bmp_s
, bmp_d
;
530 int depth
= capBox
->bitDepth
/ 8;
531 int inoffset
= 0, outoffset
= (capBox
->outputheight
) * capBox
->outputwidth
* depth
;
532 int ow
= capBox
->outputwidth
* depth
;
535 /* FIXME: Improve software resizing: add error checks and optimize */
537 myarray
= CoTaskMemAlloc(capBox
->outputwidth
* capBox
->outputheight
* depth
);
538 dc_s
= CreateCompatibleDC(NULL
);
539 dc_d
= CreateCompatibleDC(NULL
);
540 bmp_s
= CreateBitmap(capBox
->width
, capBox
->height
, 1, capBox
->bitDepth
, input
);
541 bmp_d
= CreateBitmap(capBox
->outputwidth
, capBox
->outputheight
, 1, capBox
->bitDepth
, NULL
);
542 SelectObject(dc_s
, bmp_s
);
543 SelectObject(dc_d
, bmp_d
);
544 StretchBlt(dc_d
, 0, 0, capBox
->outputwidth
, capBox
->outputheight
,
545 dc_s
, 0, 0, capBox
->width
, capBox
->height
, SRCCOPY
);
546 GetBitmapBits(bmp_d
, capBox
->outputwidth
* capBox
->outputheight
* depth
, myarray
);
547 while (outoffset
> 0)
552 for (i
= 0; i
< ow
; i
++)
553 output
[outoffset
+ i
] = myarray
[inoffset
+ i
];
556 CoTaskMemFree(myarray
);
564 static void V4l_GetFrame(Capture
* capBox
, unsigned char ** pInput
)
568 if (xioctl(capBox
->fd
, VIDIOCSYNC
, &capBox
->grab_buf
[capBox
->curframe
]) == -1)
569 WARN("Syncing ioctl failed: %d\n", errno
);
571 *pInput
= capBox
->pmap
+ capBox
->gb_buffers
.offsets
[capBox
->curframe
];
576 while ((retval
= video_read(capBox
->fd
, capBox
->grab_data
, capBox
->imagesize
)) == -1)
577 if (errno
!= EAGAIN
) break;
579 WARN("Error occurred while reading from device: %s\n", strerror(errno
));
580 *pInput
= (unsigned char*) capBox
->grab_data
;
584 static void V4l_FreeFrame(Capture
* capBox
)
589 if (xioctl(capBox
->fd
, VIDIOCMCAPTURE
, &capBox
->grab_buf
[capBox
->curframe
]) == -1)
590 ERR("Freeing frame for capture failed: %s\n", strerror(errno
));
592 if (++capBox
->curframe
== capBox
->buffers
)
593 capBox
->curframe
= 0;
596 static DWORD WINAPI
ReadThread(LPVOID lParam
)
598 Capture
* capBox
= lParam
;
600 IMediaSample
*pSample
= NULL
;
601 unsigned long framecount
= 0;
602 unsigned char *pTarget
, *pInput
, *pOutput
;
604 hr
= V4l_Prepare(capBox
);
608 pOutput
= CoTaskMemAlloc(capBox
->width
* capBox
->height
* capBox
->bitDepth
/ 8);
609 capBox
->curframe
= 0;
611 V4l_FreeFrame(capBox
);
612 } while (capBox
->curframe
!= 0);
616 EnterCriticalSection(&capBox
->CritSect
);
619 hr
= OutputPin_GetDeliveryBuffer((OutputPin
*)capBox
->pOut
, &pSample
, NULL
, NULL
, 0);
624 if (!capBox
->swresize
)
625 len
= capBox
->height
* capBox
->width
* capBox
->bitDepth
/ 8;
627 len
= capBox
->outputheight
* capBox
->outputwidth
* capBox
->bitDepth
/ 8;
628 IMediaSample_SetActualDataLength(pSample
, len
);
630 len
= IMediaSample_GetActualDataLength(pSample
);
631 TRACE("Data length: %d KB\n", len
/ 1024);
633 IMediaSample_GetPointer(pSample
, &pTarget
);
634 /* FIXME: Check return values.. */
635 V4l_GetFrame(capBox
, &pInput
);
636 capBox
->renderer(capBox
, pOutput
, pInput
);
637 Resize(capBox
, pTarget
, pOutput
);
638 hr
= OutputPin_SendSample((OutputPin
*)capBox
->pOut
, pSample
);
639 TRACE("%p -> Frame %lu: %x\n", capBox
, ++framecount
, hr
);
640 IMediaSample_Release(pSample
);
641 V4l_FreeFrame(capBox
);
643 LeaveCriticalSection(&capBox
->CritSect
);
644 if (FAILED(hr
) && hr
!= VFW_E_NOT_CONNECTED
)
646 ERR("Received error: %x\n", hr
);
650 LeaveCriticalSection(&capBox
->CritSect
);
651 CoTaskMemFree(pOutput
);
656 CoTaskMemFree(pOutput
);
657 V4l_Unprepare(capBox
);
658 LeaveCriticalSection(&capBox
->CritSect
);
661 capBox
->thread
= 0; capBox
->stopped
= 1;
662 FIXME("Stop IFilterGraph\n");
666 HRESULT
qcap_driver_run(Capture
*capBox
, FILTER_STATE
*state
)
671 TRACE("%p -> (%p)\n", capBox
, state
);
673 if (*state
== State_Running
) return S_OK
;
675 EnterCriticalSection(&capBox
->CritSect
);
679 if (*state
== State_Stopped
)
681 *state
= State_Running
;
682 if (!capBox
->iscommitted
++)
684 IMemAllocator
* pAlloc
= NULL
;
685 ALLOCATOR_PROPERTIES ap
, actual
;
689 if (!capBox
->swresize
)
690 ap
.cbBuffer
= capBox
->width
* capBox
->height
;
692 ap
.cbBuffer
= capBox
->outputwidth
* capBox
->outputheight
;
693 ap
.cbBuffer
= (ap
.cbBuffer
* capBox
->bitDepth
) / 8;
697 out
= (OutputPin
*)capBox
->pOut
;
698 hr
= IMemInputPin_GetAllocator(out
->pMemInputPin
, &pAlloc
);
701 hr
= IMemAllocator_SetProperties(pAlloc
, &ap
, &actual
);
704 hr
= IMemAllocator_Commit(pAlloc
);
707 IMemAllocator_Release(pAlloc
);
709 TRACE("Committing allocator: %x\n", hr
);
712 thread
= CreateThread(NULL
, 0, ReadThread
, capBox
, 0, NULL
);
715 capBox
->thread
= thread
;
716 SetThreadPriority(thread
, THREAD_PRIORITY_LOWEST
);
717 LeaveCriticalSection(&capBox
->CritSect
);
720 ERR("Creating thread failed.. %u\n", GetLastError());
721 LeaveCriticalSection(&capBox
->CritSect
);
725 ResumeThread(capBox
->thread
);
726 *state
= State_Running
;
727 LeaveCriticalSection(&capBox
->CritSect
);
731 HRESULT
qcap_driver_pause(Capture
*capBox
, FILTER_STATE
*state
)
733 TRACE("%p -> (%p)\n", capBox
, state
);
735 if (*state
== State_Paused
)
737 if (*state
== State_Stopped
)
738 qcap_driver_run(capBox
, state
);
740 EnterCriticalSection(&capBox
->CritSect
);
741 *state
= State_Paused
;
742 SuspendThread(capBox
->thread
);
743 LeaveCriticalSection(&capBox
->CritSect
);
748 HRESULT
qcap_driver_stop(Capture
*capBox
, FILTER_STATE
*state
)
750 TRACE("%p -> (%p)\n", capBox
, state
);
752 if (*state
== State_Stopped
)
755 EnterCriticalSection(&capBox
->CritSect
);
759 if (*state
== State_Paused
)
760 ResumeThread(capBox
->thread
);
763 if (capBox
->iscommitted
)
765 IMemInputPin
*pMem
= NULL
;
766 IMemAllocator
* pAlloc
= NULL
;
767 IPin
*pConnect
= NULL
;
770 capBox
->iscommitted
= 0;
772 hr
= IPin_ConnectedTo(capBox
->pOut
, &pConnect
);
775 hr
= IPin_QueryInterface(pConnect
, &IID_IMemInputPin
, (void **) &pMem
);
778 hr
= IMemInputPin_GetAllocator(pMem
, &pAlloc
);
781 hr
= IMemAllocator_Decommit(pAlloc
);
784 IMemAllocator_Release(pAlloc
);
787 IMemInputPin_Release(pMem
);
790 IPin_Release(pConnect
);
792 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_COMMITTED
)
793 WARN("Decommitting allocator: %x\n", hr
);
795 V4l_Unprepare(capBox
);
798 *state
= State_Stopped
;
799 LeaveCriticalSection(&capBox
->CritSect
);
803 Capture
* qcap_driver_init( IPin
*pOut
, USHORT card
)
805 Capture
* capBox
= NULL
;
807 struct video_capability capa
;
808 struct video_picture pict
;
809 struct video_window window
;
814 capBox
= CoTaskMemAlloc(sizeof(Capture
));
818 /* capBox->vtbl = &defboxVtbl; */
820 InitializeCriticalSection( &capBox
->CritSect
);
821 capBox
->CritSect
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": Capture.CritSect");
823 sprintf(device
, "/dev/video%i", card
);
824 TRACE("opening %s\n", device
);
825 capBox
->fd
= video_open(device
, O_RDWR
| O_NONBLOCK
);
826 if (capBox
->fd
== -1)
828 WARN("open failed (%d)\n", errno
);
832 memset(&capa
, 0, sizeof(capa
));
834 if (xioctl(capBox
->fd
, VIDIOCGCAP
, &capa
) == -1)
836 WARN("ioctl(VIDIOCGCAP) failed (%d)\n", errno
);
840 if (!(capa
.type
& VID_TYPE_CAPTURE
))
842 WARN("not a video capture device\n");
846 TRACE("%d inputs on %s\n", capa
.channels
, capa
.name
);
848 if (xioctl(capBox
->fd
, VIDIOCGPICT
, &pict
) == -1)
850 ERR("ioctl(VIDIOCGPICT) failed (%d)\n", errno
);
854 TRACE("depth %d palette %d (%s) hue %d color %d contrast %d\n",
855 pict
.depth
, pict
.palette
, renderlist_V4l
[pict
.palette
].name
,
856 pict
.hue
, pict
.colour
, pict
.contrast
);
858 capBox
->dbrightness
= pict
.brightness
;
859 capBox
->dcolour
= pict
.colour
;
860 capBox
->dhue
= pict
.hue
;
861 capBox
->dcontrast
= pict
.contrast
;
863 if (!renderlist_V4l
[pict
.palette
].renderer
)
865 int palet
= pict
.palette
, i
;
867 TRACE("No renderer available for %s, falling back to defaults\n",
868 renderlist_V4l
[pict
.palette
].name
);
869 capBox
->renderer
= NULL
;
870 for (i
= 0; fallback_V4l
[i
] >=0 ; i
++)
872 int n
= fallback_V4l
[i
];
874 if (renderlist_V4l
[n
].renderer
== NULL
)
877 pict
.depth
= renderlist_V4l
[n
].depth
;
879 if (xioctl(capBox
->fd
, VIDIOCSPICT
, &pict
) == -1)
881 TRACE("Could not render with %s (%d)\n",
882 renderlist_V4l
[n
].name
, n
);
885 TRACE("using renderer %s (%d)\n",
886 renderlist_V4l
[n
].name
, n
);
887 capBox
->renderer
= renderlist_V4l
[n
].renderer
;
891 if (!capBox
->renderer
)
893 ERR("video format %s isn't available\n",
894 renderlist_V4l
[palet
].name
);
900 TRACE("Using the suggested format\n");
901 capBox
->renderer
= renderlist_V4l
[pict
.palette
].renderer
;
903 memcpy(&capBox
->pict
, &pict
, sizeof(struct video_picture
));
905 memset(&window
, 0, sizeof(window
));
906 if (xioctl(capBox
->fd
, VIDIOCGWIN
, &window
) == -1)
908 WARN("VIDIOCGWIN failed (%d)\n", errno
);
912 capBox
->height
= capBox
->outputheight
= window
.height
;
913 capBox
->width
= capBox
->outputwidth
= window
.width
;
914 capBox
->swresize
= FALSE
;
915 capBox
->bitDepth
= 24;
919 capBox
->curframe
= 0;
920 capBox
->iscommitted
= 0;
922 TRACE("format: %d bits - %d x %d\n", capBox
->bitDepth
, capBox
->width
, capBox
->height
);
928 qcap_driver_destroy( capBox
);
935 Capture
* qcap_driver_init( IPin
*pOut
, USHORT card
)
938 "The v4l headers were not available at compile time,\n"
939 "so video capture support is not available.\n";
944 #define FAIL_WITH_ERR \
945 ERR("v4l absent: shouldn't be called\n"); \
948 HRESULT
qcap_driver_destroy(Capture
*capBox
)
953 HRESULT
qcap_driver_set_format(Capture
*capBox
, AM_MEDIA_TYPE
* mT
)
958 HRESULT
qcap_driver_get_format(const Capture
*capBox
, AM_MEDIA_TYPE
** mT
)
963 HRESULT
qcap_driver_get_prop_range( Capture
*capBox
, LONG Property
, LONG
*pMin
,
964 LONG
*pMax
, LONG
*pSteppingDelta
, LONG
*pDefault
, LONG
*pCapsFlags
)
969 HRESULT
qcap_driver_get_prop(Capture
*capBox
, LONG Property
, LONG
*lValue
, LONG
*Flags
)
974 HRESULT
qcap_driver_set_prop(Capture
*capBox
, long Property
, long lValue
, long Flags
)
979 HRESULT
qcap_driver_run(Capture
*capBox
, FILTER_STATE
*state
)
984 HRESULT
qcap_driver_pause(Capture
*capBox
, FILTER_STATE
*state
)
989 HRESULT
qcap_driver_stop(Capture
*capBox
, FILTER_STATE
*state
)
994 #endif /* HAVE_LINUX_VIDEODEV_H */