]> git.sesse.net Git - vlc/blob - modules/access/dshow/dshow.cpp
* modules/access/*: strings review + coding style fixes.
[vlc] / modules / access / dshow / dshow.cpp
1 /*****************************************************************************
2  * dshow.cpp : DirectShow access module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 VideoLAN
5  * $Id: dshow.cpp,v 1.25 2004/01/25 17:31:22 gbazin Exp $
6  *
7  * Author: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/vout.h>
34
35 #include "filter.h"
36
37 /*****************************************************************************
38  * Access: local prototypes
39  *****************************************************************************/
40 static ssize_t Read          ( input_thread_t *, byte_t *, size_t );
41 static ssize_t ReadCompressed( input_thread_t *, byte_t *, size_t );
42
43 static int OpenDevice( input_thread_t *, string, vlc_bool_t );
44 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
45                                        list<string> *, vlc_bool_t );
46 static AM_MEDIA_TYPE EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
47                                      int, int, int, int, int, int );
48 static bool ConnectFilters( IFilterGraph *, IBaseFilter *, IPin * );
49
50 static int FindDevicesCallback( vlc_object_t *, char const *,
51                                 vlc_value_t, vlc_value_t, void * );
52
53 static void PropertiesPage( input_thread_t *, IBaseFilter * );
54
55 #if 0
56     /* Debug only, use this to find out GUIDs */
57     unsigned char p_st[];
58     UuidToString( (IID *)&IID_IAMBufferNegotiation, &p_st );
59     msg_Err( p_input, "BufferNegotiation: %s" , p_st );
60 #endif
61
62 /*
63  * header:
64  *  fcc  ".dsh"
65  *  u32    stream count
66  *      fcc "auds"|"vids"       0
67  *      fcc codec               4
68  *      if vids
69  *          u32 width           8
70  *          u32 height          12
71  *          u32 padding         16
72  *      if auds
73  *          u32 channels        12
74  *          u32 samplerate      8
75  *          u32 samplesize      16
76  *
77  * data:
78  *  u32     stream number
79  *  u32     data size
80  *  u8      data
81  */
82
83 static void SetDWBE( uint8_t *p, uint32_t dw )
84 {
85     p[0] = (dw >> 24)&0xff;
86     p[1] = (dw >> 16)&0xff;
87     p[2] = (dw >>  8)&0xff;
88     p[3] = (dw      )&0xff;
89 }
90
91 static void SetQWBE( uint8_t *p, uint64_t qw )
92 {
93     SetDWBE( p, (qw >> 32)&0xffffffff );
94     SetDWBE( &p[4], qw&0xffffffff );
95 }
96
97 /*****************************************************************************
98  * Module descriptor
99  *****************************************************************************/
100 static char *ppsz_vdev[] = { "", "none" };
101 static char *ppsz_vdev_text[] = { N_("Default"), N_("None") };
102 static char *ppsz_adev[] = { "", "none" };
103 static char *ppsz_adev_text[] = { N_("Default"), N_("None") };
104
105 #define CACHING_TEXT N_("Caching value in ms")
106 #define CACHING_LONGTEXT N_( \
107     "Allows you to modify the default caching value for directshow streams. " \
108     "This value should be set in milliseconds units." )
109 #define VDEV_TEXT N_("Video device name")
110 #define VDEV_LONGTEXT N_( \
111     "You can specify the name of the video device that will be used by the " \
112     "DirectShow plugin. If you don't specify anything, the default device " \
113     "will be used.")
114 #define ADEV_TEXT N_("Audio device name")
115 #define ADEV_LONGTEXT N_( \
116     "You can specify the name of the audio device that will be used by the " \
117     "DirectShow plugin. If you don't specify anything, the default device " \
118     "will be used.")
119 #define SIZE_TEXT N_("Video size")
120 #define SIZE_LONGTEXT N_( \
121     "You can specify the size of the video that will be displayed by the " \
122     "DirectShow plugin. If you don't specify anything the default size for " \
123     "your device will be used.")
124 #define CHROMA_TEXT N_("Video input chroma format")
125 #define CHROMA_LONGTEXT N_( \
126     "Force the DirectShow video input to use a specific chroma format " \
127     "(eg. I420 (default), RV24, etc.)")
128 #define CONFIG_TEXT N_("Device properties")
129 #define CONFIG_LONGTEXT N_( \
130     "Show the properties dialog of the selected device.")
131
132 static int  AccessOpen ( vlc_object_t * );
133 static void AccessClose( vlc_object_t * );
134
135 static int  DemuxOpen  ( vlc_object_t * );
136 static void DemuxClose ( vlc_object_t * );
137
138 vlc_module_begin();
139     set_description( _("DirectShow input") );
140     add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
141                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
142
143     add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
144         change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
145
146     add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
147         change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
148
149     add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
150
151     add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
152                 VLC_TRUE );
153
154     add_bool( "dshow-config", VLC_FALSE, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
155               VLC_TRUE );
156
157     add_shortcut( "dshow" );
158     set_capability( "access", 0 );
159     set_callbacks( AccessOpen, AccessClose );
160
161     add_submodule();
162     set_description( _("DirectShow demuxer") );
163     add_shortcut( "dshow" );
164     set_capability( "demux", 200 );
165     set_callbacks( DemuxOpen, DemuxClose );
166
167 vlc_module_end();
168
169 /****************************************************************************
170  * DirectShow elementary stream descriptor
171  ****************************************************************************/
172 typedef struct dshow_stream_t
173 {
174     string          devicename;
175     IBaseFilter     *p_device_filter;
176     CaptureFilter   *p_capture_filter;
177     AM_MEDIA_TYPE   mt;
178     int             i_fourcc;
179     vlc_bool_t      b_invert;
180
181     union
182     {
183       VIDEOINFOHEADER video;
184       WAVEFORMATEX    audio;
185
186     } header;
187
188     vlc_bool_t      b_pts;
189
190 } dshow_stream_t;
191
192 /****************************************************************************
193  * Access descriptor declaration
194  ****************************************************************************/
195 struct access_sys_t
196 {
197     vlc_mutex_t lock;
198     vlc_cond_t  wait;
199
200     IFilterGraph  *p_graph;
201     IMediaControl *p_control;
202
203     /* header */
204     int     i_header_size;
205     int     i_header_pos;
206     uint8_t *p_header;
207
208     /* list of elementary streams */
209     dshow_stream_t **pp_streams;
210     int            i_streams;
211     int            i_current_stream;
212
213     /* misc properties */
214     int            i_width;
215     int            i_height;
216     int            i_chroma;
217     int            b_audio;
218 };
219
220 /*****************************************************************************
221  * Open: open direct show device
222  *****************************************************************************/
223 static int AccessOpen( vlc_object_t *p_this )
224 {
225     input_thread_t *p_input = (input_thread_t *)p_this;
226     access_sys_t   *p_sys;
227     vlc_value_t    val;
228
229     /* Get/parse options and open device(s) */
230     string vdevname, adevname;
231     int i_width = 0, i_height = 0, i_chroma = 0;
232
233     var_Create( p_input, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
234     var_Get( p_input, "dshow-vdev", &val );
235     if( val.psz_string ) vdevname = string( val.psz_string );
236     if( val.psz_string ) free( val.psz_string );
237
238     var_Create( p_input, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
239     var_Get( p_input, "dshow-adev", &val );
240     if( val.psz_string ) adevname = string( val.psz_string );
241     if( val.psz_string ) free( val.psz_string );
242
243     var_Create( p_input, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
244     var_Get( p_input, "dshow-size", &val );
245     if( val.psz_string && *val.psz_string )
246     {
247         if( !strcmp( val.psz_string, "subqcif" ) )
248         {
249             i_width  = 128; i_height = 96;
250         }
251         else if( !strcmp( val.psz_string, "qsif" ) )
252         {
253             i_width  = 160; i_height = 120;
254         }
255         else if( !strcmp( val.psz_string, "qcif" ) )
256         {
257             i_width  = 176; i_height = 144;
258         }
259         else if( !strcmp( val.psz_string, "sif" ) )
260         {
261             i_width  = 320; i_height = 240;
262         }
263         else if( !strcmp( val.psz_string, "cif" ) )
264         {
265             i_width  = 352; i_height = 288;
266         }
267         else if( !strcmp( val.psz_string, "vga" ) )
268         {
269             i_width  = 640; i_height = 480;
270         }
271         else
272         {
273             /* Width x Height */
274             char *psz_parser;
275             i_width = strtol( val.psz_string, &psz_parser, 0 );
276             if( *psz_parser == 'x' || *psz_parser == 'X')
277             {
278                 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
279             }
280             msg_Dbg( p_input, "Width x Height %dx%d", i_width, i_height );
281         }
282     }
283     if( val.psz_string ) free( val.psz_string );
284
285     var_Create( p_input, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
286     var_Get( p_input, "dshow-chroma", &val );
287     if( val.psz_string && strlen( val.psz_string ) >= 4 )
288     {
289         i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
290                                val.psz_string[2], val.psz_string[3] );
291     }
292     if( val.psz_string ) free( val.psz_string );
293
294     p_input->pf_read        = Read;
295     p_input->pf_seek        = NULL;
296     p_input->pf_set_area    = NULL;
297     p_input->pf_set_program = NULL;
298
299     vlc_mutex_lock( &p_input->stream.stream_lock );
300     p_input->stream.b_pace_control = 0;
301     p_input->stream.b_seekable = 0;
302     p_input->stream.p_selected_area->i_size = 0;
303     p_input->stream.p_selected_area->i_tell = 0;
304     p_input->stream.i_method = INPUT_METHOD_FILE;
305     vlc_mutex_unlock( &p_input->stream.stream_lock );
306
307     var_Create( p_input, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
308     var_Get( p_input, "dshow-caching", &val );
309     p_input->i_pts_delay = val.i_int * 1000;
310
311     /* Initialize OLE/COM */
312     CoInitialize( 0 );
313
314     /* create access private data */
315     p_input->p_access_data = p_sys =
316         (access_sys_t *)malloc( sizeof( access_sys_t ) );
317
318     /* Initialize some data */
319     p_sys->i_streams = 0;
320     p_sys->pp_streams = (dshow_stream_t **)malloc( 1 );
321     p_sys->i_width = i_width;
322     p_sys->i_height = i_height;
323     p_sys->i_chroma = i_chroma;
324     p_sys->b_audio = VLC_TRUE;
325
326     /* Create header */
327     p_sys->i_header_size = 8;
328     p_sys->p_header      = (uint8_t *)malloc( p_sys->i_header_size );
329     memcpy(  &p_sys->p_header[0], ".dsh", 4 );
330     SetDWBE( &p_sys->p_header[4], 1 );
331     p_sys->i_header_pos = p_sys->i_header_size;
332
333     /* Build directshow graph */
334     CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
335                       (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph );
336
337     p_sys->p_graph->QueryInterface( IID_IMediaControl,
338                                     (void **)&p_sys->p_control );
339
340     if( OpenDevice( p_input, vdevname, 0 ) != VLC_SUCCESS )
341     {
342         msg_Err( p_input, "can't open video");
343     }
344
345     if( p_sys->b_audio && OpenDevice( p_input, adevname, 1 ) != VLC_SUCCESS )
346     {
347         msg_Err( p_input, "can't open audio");
348     }
349
350     if( !p_sys->i_streams )
351     {
352         /* Release directshow objects */
353         if( p_sys->p_control ) p_sys->p_control->Release();
354         p_sys->p_graph->Release();
355
356         /* Uninitialize OLE/COM */
357         CoUninitialize();
358
359         free( p_sys->p_header );
360         free( p_sys->pp_streams );
361         free( p_sys );
362         return VLC_EGENERIC;
363     }
364
365     /* Initialize some data */
366     p_sys->i_current_stream = 0;
367     p_input->i_mtu += p_sys->i_header_size + 16 /* data header size */;
368
369     vlc_mutex_init( p_input, &p_sys->lock );
370     vlc_cond_init( p_input, &p_sys->wait );
371
372     /* Everything is ready. Let's rock baby */
373     p_sys->p_control->Run();
374
375     return VLC_SUCCESS;
376 }
377
378 /*****************************************************************************
379  * AccessClose: close device
380  *****************************************************************************/
381 static void AccessClose( vlc_object_t *p_this )
382 {
383     input_thread_t *p_input = (input_thread_t *)p_this;
384     access_sys_t    *p_sys  = p_input->p_access_data;
385
386     /* Stop capturing stuff */
387     //p_sys->p_control->Stop(); /* FIXME?: we get stuck here sometimes */
388     p_sys->p_control->Release();
389
390     /* Remove filters from graph */
391     for( int i = 0; i < p_sys->i_streams; i++ )
392     {
393         p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
394         p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
395         p_sys->pp_streams[i]->p_capture_filter->Release();
396         p_sys->pp_streams[i]->p_device_filter->Release();
397     }
398     p_sys->p_graph->Release();
399
400     /* Uninitialize OLE/COM */
401     CoUninitialize();
402
403     free( p_sys->p_header );
404     for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
405     free( p_sys->pp_streams );
406     free( p_sys );
407 }
408
409 /****************************************************************************
410  * ConnectFilters
411  ****************************************************************************/
412 static bool ConnectFilters( IFilterGraph *p_graph, IBaseFilter *p_filter,
413                             IPin *p_input_pin )
414 {
415     IEnumPins *p_enumpins;
416     IPin *p_output_pin;
417     ULONG i_fetched;
418
419     if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
420
421     while( S_OK == p_enumpins->Next( 1, &p_output_pin, &i_fetched ) )
422     {
423         if( S_OK == p_graph->ConnectDirect( p_output_pin, p_input_pin, 0 ) )
424         {
425             p_enumpins->Release();
426             return true;
427         }
428     }
429
430     p_enumpins->Release();
431     return false;
432 }
433
434 static int OpenDevice( input_thread_t *p_input, string devicename,
435                        vlc_bool_t b_audio )
436 {
437     access_sys_t *p_sys = p_input->p_access_data;
438     list<string> list_devices;
439
440     /* Enumerate devices and display their names */
441     FindCaptureDevice( (vlc_object_t *)p_input, NULL, &list_devices, b_audio );
442
443     if( !list_devices.size() )
444         return VLC_EGENERIC;
445
446     list<string>::iterator iter;
447     for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
448         msg_Dbg( p_input, "found device: %s", iter->c_str() );
449
450     /* If no device name was specified, pick the 1st one */
451     if( devicename.size() == 0 )
452     {
453         devicename = *list_devices.begin();
454     }
455
456     // Use the system device enumerator and class enumerator to find
457     // a capture/preview device, such as a desktop USB video camera.
458     IBaseFilter *p_device_filter =
459         FindCaptureDevice( (vlc_object_t *)p_input, &devicename,
460                            NULL, b_audio );
461     if( p_device_filter )
462         msg_Dbg( p_input, "using device: %s", devicename.c_str() );
463     else
464     {
465         msg_Err( p_input, "can't use device: %s", devicename.c_str() );
466         return VLC_EGENERIC;
467     }
468
469     AM_MEDIA_TYPE media_type =
470         EnumDeviceCaps( (vlc_object_t *)p_input, p_device_filter,
471                         p_sys->i_chroma, p_sys->i_width, p_sys->i_height,
472                         0, 0, 0 );
473
474     /* Create and add our capture filter */
475     CaptureFilter *p_capture_filter = new CaptureFilter( p_input, media_type );
476     p_sys->p_graph->AddFilter( p_capture_filter, 0 );
477
478     /* Add the device filter to the graph (seems necessary with VfW before
479      * accessing pin attributes). */
480     p_sys->p_graph->AddFilter( p_device_filter, 0 );
481
482     /* Attempt to connect one of this device's capture output pins */
483     msg_Dbg( p_input, "connecting filters" );
484     if( ConnectFilters( p_sys->p_graph, p_device_filter,
485                         p_capture_filter->CustomGetPin() ) )
486     {
487         /* Success */
488         dshow_stream_t dshow_stream;
489         dshow_stream.b_invert = VLC_FALSE;
490         dshow_stream.b_pts = VLC_FALSE;
491         dshow_stream.mt =
492             p_capture_filter->CustomGetPin()->CustomGetMediaType();
493
494         if( dshow_stream.mt.majortype == MEDIATYPE_Video )
495         {
496             msg_Dbg( p_input, "MEDIATYPE_Video");
497
498             /* Packed RGB formats */
499             if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB1 )
500                 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
501             if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB4 )
502                 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
503             if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB8 )
504                 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
505             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB555 )
506                 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
507             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB565 )
508                 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
509             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB24 )
510                 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
511             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB32 )
512                 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
513             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_ARGB32 )
514                 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
515
516             /* Packed YUV formats */
517             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YVYU )
518                 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', 'Y', 'U' );
519             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YUYV )
520                 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
521             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y411 )
522                 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '1', 'N' );
523             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y211 )
524                 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', '2', '1', '1' );
525             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YUY2 ||
526                      dshow_stream.mt.subtype == MEDIASUBTYPE_UYVY )
527                 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
528
529             /* Planar YUV formats */
530             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_I420 )
531                 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '2', '0' );
532             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y41P )
533                 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '1', '1' );
534             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YV12 ||
535                      dshow_stream.mt.subtype == MEDIASUBTYPE_IYUV )
536                 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', '1', '2' );
537             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YVU9 )
538                 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', 'U', '9' );
539
540             /* DV formats */
541             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvsl )
542                 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'l' );
543             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvsd )
544                 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'd' );
545             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvhd )
546                 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 'h', 'd' );
547
548             /* MPEG video formats */
549             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_MPEG2_VIDEO )
550                 dshow_stream.i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'v' );
551
552             else goto fail;
553
554             dshow_stream.header.video =
555                 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
556
557             int i_height = dshow_stream.header.video.bmiHeader.biHeight;
558
559             /* Check if the image is inverted (bottom to top) */
560             if( dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '1' ) ||
561                 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '4' ) ||
562                 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '8' ) ||
563                 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
564                 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
565                 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
566                 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
567                 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', 'A' ) )
568             {
569                 if( i_height > 0 ) dshow_stream.b_invert = VLC_TRUE;
570                 else i_height = - i_height;
571             }
572
573             /* Check if we are dealing with a DV stream */
574             if( dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 's', 'l' ) ||
575                 dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 's', 'd' ) ||
576                 dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 'h', 'd' ) )
577             {
578                 p_input->pf_read = ReadCompressed;
579                 if( !p_input->psz_demux || !*p_input->psz_demux )
580                 {
581                     p_input->psz_demux = "rawdv";
582                 }
583                 p_sys->b_audio = VLC_FALSE;
584             }
585
586             /* Check if we are dealing with an MPEG video stream */
587             if( dshow_stream.i_fourcc == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
588             {
589                 p_input->pf_read = ReadCompressed;
590                 if( !p_input->psz_demux || !*p_input->psz_demux )
591                 {
592                     p_input->psz_demux = "mpgv";
593                 }
594                 p_sys->b_audio = VLC_FALSE;
595             }
596
597             /* Add video stream to header */
598             p_sys->i_header_size += 20;
599             p_sys->p_header = (uint8_t *)realloc( p_sys->p_header,
600                                                   p_sys->i_header_size );
601             memcpy(  &p_sys->p_header[p_sys->i_header_pos], "vids", 4 );
602             memcpy(  &p_sys->p_header[p_sys->i_header_pos + 4],
603                      &dshow_stream.i_fourcc, 4 );
604             SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 8],
605                      dshow_stream.header.video.bmiHeader.biWidth );
606             SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 12], i_height );
607             SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 16], 0 );
608             p_sys->i_header_pos = p_sys->i_header_size;
609
610             /* Greatly simplifies the reading routine */
611             int i_mtu = dshow_stream.header.video.bmiHeader.biWidth *
612                 i_height * 4;
613             p_input->i_mtu = __MAX( p_input->i_mtu, (unsigned int)i_mtu );
614         }
615
616         else if( dshow_stream.mt.majortype == MEDIATYPE_Audio &&
617                  dshow_stream.mt.formattype == FORMAT_WaveFormatEx )
618         {
619             msg_Dbg( p_input, "MEDIATYPE_Audio");
620
621             if( dshow_stream.mt.subtype == MEDIASUBTYPE_PCM )
622                 dshow_stream.i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
623             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_IEEE_FLOAT )
624                 dshow_stream.i_fourcc = VLC_FOURCC( 'f', 'l', '3', '2' );
625             else goto fail;
626
627             dshow_stream.header.audio =
628                 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
629
630             /* Add audio stream to header */
631             p_sys->i_header_size += 20;
632             p_sys->p_header = (uint8_t *)realloc( p_sys->p_header,
633                                                   p_sys->i_header_size );
634             memcpy(  &p_sys->p_header[p_sys->i_header_pos], "auds", 4 );
635             memcpy(  &p_sys->p_header[p_sys->i_header_pos + 4],
636                      &dshow_stream.i_fourcc, 4 );
637             SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 8],
638                      dshow_stream.header.audio.nChannels );
639             SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 12],
640                      dshow_stream.header.audio.nSamplesPerSec );
641             SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 16],
642                      dshow_stream.header.audio.wBitsPerSample );
643             p_sys->i_header_pos = p_sys->i_header_size;
644
645             /* Greatly simplifies the reading routine */
646             IAMBufferNegotiation *p_ambuf;
647             IPin *p_pin;
648             int i_mtu;
649
650             p_capture_filter->CustomGetPin()->ConnectedTo( &p_pin );
651             if( SUCCEEDED( p_pin->QueryInterface(
652                   IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
653             {
654                 ALLOCATOR_PROPERTIES AllocProp;
655                 memset( &AllocProp, 0, sizeof( ALLOCATOR_PROPERTIES ) );
656                 p_ambuf->GetAllocatorProperties( &AllocProp );
657                 p_ambuf->Release();
658                 i_mtu = AllocProp.cbBuffer;
659             }
660             else
661             {
662                 /* Worst case */
663                 i_mtu = dshow_stream.header.audio.nSamplesPerSec *
664                         dshow_stream.header.audio.nChannels *
665                         dshow_stream.header.audio.wBitsPerSample / 8;
666             }
667             p_pin->Release();
668             p_input->i_mtu = __MAX( p_input->i_mtu, (unsigned int)i_mtu );
669         }
670
671         else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
672         {
673             msg_Dbg( p_input, "MEDIATYPE_Stream" );
674
675             if( dshow_stream.mt.subtype == MEDIASUBTYPE_MPEG2_PROGRAM )
676                 dshow_stream.i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'p' );
677             else if( dshow_stream.mt.subtype == MEDIASUBTYPE_MPEG2_TRANSPORT )
678                 dshow_stream.i_fourcc = VLC_FOURCC( 'm', 'p', '2', 't' );
679
680             msg_Dbg( p_input, "selected stream pin accepts format: %4.4s",
681                      (char *)&dshow_stream.i_fourcc);
682
683             p_sys->b_audio = VLC_FALSE;
684             p_sys->i_header_size = 0;
685             p_sys->i_header_pos = 0;
686             p_input->i_mtu = INPUT_DEFAULT_BUFSIZE;
687
688             p_input->pf_read = ReadCompressed;
689             p_input->pf_set_program = input_SetProgram;
690         }
691
692         else
693         {
694             msg_Dbg( p_input, "unknown stream majortype" );
695             goto fail;
696         }
697
698         /* Show properties */
699         vlc_value_t val;
700         var_Create( p_input, "dshow-config",
701                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
702         var_Get( p_input, "dshow-config", &val );
703
704         if(val.i_int) PropertiesPage( p_input, p_device_filter );
705
706         /* Add directshow elementary stream to our list */
707         dshow_stream.p_device_filter = p_device_filter;
708         dshow_stream.p_capture_filter = p_capture_filter;
709
710         p_sys->pp_streams =
711             (dshow_stream_t **)realloc( p_sys->pp_streams,
712                                         sizeof(dshow_stream_t *)
713                                         * (p_sys->i_streams + 1) );
714         p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
715         *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
716         SetDWBE( &p_sys->p_header[4], (uint32_t)p_sys->i_streams );
717
718         return VLC_SUCCESS;
719     }
720
721  fail:
722     /* Remove filters from graph */
723     p_sys->p_graph->RemoveFilter( p_device_filter );
724     p_sys->p_graph->RemoveFilter( p_capture_filter );
725
726     /* Release objects */
727     p_device_filter->Release();
728     p_capture_filter->Release();
729
730     return VLC_EGENERIC;
731 }
732
733 static IBaseFilter *
734 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
735                    list<string> *p_listdevices, vlc_bool_t b_audio )
736 {
737     IBaseFilter *p_base_filter = NULL;
738     IMoniker *p_moniker = NULL;
739     ULONG i_fetched;
740     HRESULT hr;
741
742     /* Create the system device enumerator */
743     ICreateDevEnum *p_dev_enum = NULL;
744
745     hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
746                            IID_ICreateDevEnum, (void **)&p_dev_enum );
747     if( FAILED(hr) )
748     {
749         msg_Err( p_this, "failed to create the device enumerator (0x%x)", hr);
750         return NULL;
751     }
752
753     /* Create an enumerator for the video capture devices */
754     IEnumMoniker *p_class_enum = NULL;
755     if( !b_audio )
756         hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
757                                                 &p_class_enum, 0 );
758     else
759         hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
760                                                 &p_class_enum, 0 );
761     p_dev_enum->Release();
762     if( FAILED(hr) )
763     {
764         msg_Err( p_this, "failed to create the class enumerator (0x%x)", hr );
765         return NULL;
766     }
767
768     /* If there are no enumerators for the requested type, then
769      * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
770     if( p_class_enum == NULL )
771     {
772         msg_Err( p_this, "no capture device was detected." );
773         return NULL;
774     }
775
776     /* Enumerate the devices */
777
778     /* Note that if the Next() call succeeds but there are no monikers,
779      * it will return S_FALSE (which is not a failure). Therefore, we check
780      * that the return code is S_OK instead of using SUCCEEDED() macro. */
781
782     while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
783     {
784         /* Getting the property page to get the device name */
785         IPropertyBag *p_bag;
786         hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
787                                        (void **)&p_bag );
788         if( SUCCEEDED(hr) )
789         {
790             VARIANT var;
791             var.vt = VT_BSTR;
792             hr = p_bag->Read( L"FriendlyName", &var, NULL );
793             p_bag->Release();
794             if( SUCCEEDED(hr) )
795             {
796                 int i_convert = ( lstrlenW( var.bstrVal ) + 1 ) * 2;
797                 char *p_buf = (char *)alloca( i_convert ); p_buf[0] = 0;
798                 WideCharToMultiByte( CP_ACP, 0, var.bstrVal, -1, p_buf,
799                                      i_convert, NULL, NULL );
800                 SysFreeString(var.bstrVal);
801
802                 if( p_listdevices ) p_listdevices->push_back( p_buf );
803
804                 if( p_devicename && *p_devicename == string(p_buf) )
805                 {
806                     /* Bind Moniker to a filter object */
807                     hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
808                                                   (void **)&p_base_filter );
809                     if( FAILED(hr) )
810                     {
811                         msg_Err( p_this, "couldn't bind moniker to filter "
812                                  "object (0x%x)", hr );
813                         p_moniker->Release();
814                         p_class_enum->Release();
815                         return NULL;
816                     }
817                     p_moniker->Release();
818                     p_class_enum->Release();
819                     return p_base_filter;
820                 }
821             }
822         }
823
824         p_moniker->Release();
825     }
826
827     p_class_enum->Release();
828     return NULL;
829 }
830
831 static AM_MEDIA_TYPE EnumDeviceCaps( vlc_object_t *p_this,
832                                      IBaseFilter *p_filter,
833                                      int i_fourcc, int i_width, int i_height,
834                                      int i_channels, int i_samplespersec,
835                                      int i_bitspersample )
836 {
837     IEnumPins *p_enumpins;
838     IPin *p_output_pin;
839     IEnumMediaTypes *p_enummt;
840     int i_orig_fourcc = i_fourcc;
841     vlc_bool_t b_found = VLC_FALSE;
842
843     AM_MEDIA_TYPE media_type;
844     media_type.majortype = GUID_NULL;
845     media_type.subtype = GUID_NULL;
846     media_type.formattype = GUID_NULL;
847     media_type.pUnk = NULL;
848     media_type.cbFormat = 0;
849     media_type.pbFormat = NULL;
850
851     if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return media_type;
852
853     while( !b_found && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
854     {
855         PIN_INFO info;
856
857         if( S_OK == p_output_pin->QueryPinInfo( &info ) )
858         {
859             msg_Dbg( p_this, "EnumDeviceCaps: pin %S", info.achName );
860             if( info.pFilter ) info.pFilter->Release();
861         }
862
863         /* Probe pin */
864         if( SUCCEEDED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
865         {
866             AM_MEDIA_TYPE *p_mt;
867             while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
868             {
869
870                 if( p_mt->majortype == MEDIATYPE_Video )
871                 {
872                     int i_current_fourcc = VLC_FOURCC(' ', ' ', ' ', ' ');
873
874                     /* Packed RGB formats */
875                     if( p_mt->subtype == MEDIASUBTYPE_RGB1 )
876                         i_current_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
877                     if( p_mt->subtype == MEDIASUBTYPE_RGB4 )
878                         i_current_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
879                     if( p_mt->subtype == MEDIASUBTYPE_RGB8 )
880                         i_current_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
881                     else if( p_mt->subtype == MEDIASUBTYPE_RGB555 )
882                         i_current_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
883                     else if( p_mt->subtype == MEDIASUBTYPE_RGB565 )
884                         i_current_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
885                     else if( p_mt->subtype == MEDIASUBTYPE_RGB24 )
886                         i_current_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
887                     else if( p_mt->subtype == MEDIASUBTYPE_RGB32 )
888                         i_current_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
889                     else if( p_mt->subtype == MEDIASUBTYPE_ARGB32 )
890                         i_current_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
891
892                     /* MPEG2 video elementary stream */
893                     else if( p_mt->subtype == MEDIASUBTYPE_MPEG2_VIDEO )
894                         i_current_fourcc = VLC_FOURCC( 'm', 'p', '2', 'v' );
895
896                     /* hauppauge pvr video preview */
897                     else if( p_mt->subtype == MEDIASUBTYPE_PREVIEW_VIDEO )
898                         i_current_fourcc = VLC_FOURCC( 'P', 'V', 'R', 'V' );
899
900                     else i_current_fourcc = *((int *)&p_mt->subtype);
901
902                     int i_current_width = p_mt->pbFormat ?
903                         ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth : 0;
904                     int i_current_height = p_mt->pbFormat ?
905                         ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight : 0;
906                     if( i_current_height < 0 )
907                         i_current_height = -i_current_height; 
908
909                     msg_Dbg( p_this, "EnumDeviceCaps: input pin "
910                              "accepts chroma: %4.4s, width:%i, height:%i",
911                              (char *)&i_current_fourcc, i_current_width,
912                              i_current_height );
913
914                     if( ( !i_fourcc || i_fourcc == i_current_fourcc ||
915                           ( !i_orig_fourcc && i_current_fourcc ==
916                             VLC_FOURCC('I','4','2','0') ) ) &&
917                         ( !i_width || i_width == i_current_width ) &&
918                         ( !i_height || i_height == i_current_height ) )
919                     {
920                         /* Pick the 1st match */
921                         media_type = *p_mt;
922                         i_fourcc = i_current_fourcc;
923                         i_width = i_current_width;
924                         i_height = i_current_height;
925                         b_found = VLC_TRUE;
926                     }
927                     else
928                     {
929                         FreeMediaType( *p_mt );
930                     }
931                 }
932                 else if( p_mt->majortype == MEDIATYPE_Audio )
933                 {
934                     int i_current_fourcc;
935                     int i_current_channels =
936                         ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
937                     int i_current_samplespersec =
938                         ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
939                     int i_current_bitspersample =
940                         ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
941
942                     if( p_mt->subtype == MEDIASUBTYPE_PCM )
943                         i_current_fourcc = VLC_FOURCC( 'p', 'c', 'm', ' ' );
944                     else i_current_fourcc = *((int *)&p_mt->subtype);
945
946                     msg_Dbg( p_this, "EnumDeviceCaps: input pin "
947                              "accepts format: %4.4s, channels:%i, "
948                              "samples/sec:%i bits/sample:%i",
949                              (char *)&i_current_fourcc, i_current_channels,
950                              i_current_samplespersec, i_current_bitspersample);
951
952                     if( (!i_channels || i_channels == i_current_channels) &&
953                         (!i_samplespersec ||
954                          i_samplespersec == i_current_samplespersec) &&
955                         (!i_bitspersample ||
956                          i_bitspersample == i_current_bitspersample) )
957                     {
958                         /* Pick the 1st match */
959                         media_type = *p_mt;
960                         i_channels = i_current_channels;
961                         i_samplespersec = i_current_samplespersec;
962                         i_bitspersample = i_current_bitspersample;
963                         b_found = VLC_TRUE;
964
965                         /* Setup a few properties like the audio latency */
966                         IAMBufferNegotiation *p_ambuf;
967
968                         if( SUCCEEDED( p_output_pin->QueryInterface(
969                               IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
970                         {
971                             ALLOCATOR_PROPERTIES AllocProp;
972                             AllocProp.cbAlign = -1;
973                             AllocProp.cbBuffer = i_channels * i_samplespersec *
974                               i_bitspersample / 8 / 10 ; /*100 ms of latency*/
975                             AllocProp.cbPrefix = -1;
976                             AllocProp.cBuffers = -1;
977                             p_ambuf->SuggestAllocatorProperties( &AllocProp );
978                             p_ambuf->Release();
979                         }
980                     }
981                     else
982                     {
983                         FreeMediaType( *p_mt );
984                     }
985                 }
986                 else if( p_mt->majortype == MEDIATYPE_Stream )
987                 {
988                     msg_Dbg( p_this, "EnumDeviceCaps: MEDIATYPE_Stream" );
989
990                     int i_current_fourcc = VLC_FOURCC(' ', ' ', ' ', ' ');
991
992                     if( p_mt->subtype == MEDIASUBTYPE_MPEG2_PROGRAM )
993                         i_current_fourcc = VLC_FOURCC( 'm', 'p', '2', 'p' );
994                     else if( p_mt->subtype == MEDIASUBTYPE_MPEG2_TRANSPORT )
995                         i_current_fourcc = VLC_FOURCC( 'm', 'p', '2', 't' );
996
997                     if( ( !i_fourcc || i_fourcc == i_current_fourcc ) )
998                     {
999                         /* Pick the 1st match */
1000                         media_type = *p_mt;
1001                         i_fourcc = i_current_fourcc;
1002                         b_found = VLC_TRUE;
1003                     }
1004                     else
1005                     {
1006                         FreeMediaType( *p_mt );
1007                     }
1008                 }
1009                 else
1010                 {
1011                     msg_Dbg( p_this,
1012                              "EnumDeviceCaps: input pin: unknown format" );
1013                     FreeMediaType( *p_mt );
1014                 }
1015
1016                 CoTaskMemFree( (PVOID)p_mt );
1017             }
1018             p_enummt->Release();
1019         }
1020
1021         p_output_pin->Release();
1022     }
1023
1024     p_enumpins->Release();
1025     return media_type;
1026 }
1027
1028 /*****************************************************************************
1029  * Read: reads from the device into PES packets.
1030  *****************************************************************************
1031  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1032  * bytes.
1033  *****************************************************************************/
1034 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
1035                      size_t i_len )
1036 {
1037     access_sys_t   *p_sys = p_input->p_access_data;
1038     dshow_stream_t *p_stream = NULL;
1039     byte_t         *p_buf_orig = p_buffer;
1040     VLCMediaSample  sample;
1041     int             i_data_size;
1042     uint8_t         *p_data;
1043
1044     if( p_sys->i_header_pos )
1045     {
1046         /* First header of the stream */
1047         memcpy( p_buffer, p_sys->p_header, p_sys->i_header_size );
1048         p_buffer += p_sys->i_header_size;
1049         p_sys->i_header_pos = 0;
1050     }
1051
1052     while( 1 )
1053     {
1054         /* Get new sample/frame from next elementary stream.
1055          * We first loop through all the elementary streams and if all our
1056          * fifos are empty we block until we are signaled some new data has
1057          * arrived. */
1058         vlc_mutex_lock( &p_sys->lock );
1059
1060         int i_stream;
1061         for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1062         {
1063             p_stream = p_sys->pp_streams[i_stream];
1064             if( p_stream->mt.majortype == MEDIATYPE_Audio &&
1065                 p_stream->p_capture_filter &&
1066                 p_stream->p_capture_filter->CustomGetPin()
1067                   ->CustomGetSample( &sample ) == S_OK )
1068             {
1069                 break;
1070             }
1071         }
1072         if( i_stream == p_sys->i_streams )
1073         for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1074         {
1075             p_stream = p_sys->pp_streams[i_stream];
1076             if( p_stream->p_capture_filter &&
1077                 p_stream->p_capture_filter->CustomGetPin()
1078                   ->CustomGetSample( &sample ) == S_OK )
1079             {
1080                 break;
1081             }
1082         }
1083         if( i_stream == p_sys->i_streams )
1084         {
1085             /* No data available. Wait until some data has arrived */
1086             vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1087             vlc_mutex_unlock( &p_sys->lock );
1088             continue;
1089         }
1090
1091         vlc_mutex_unlock( &p_sys->lock );
1092
1093         /*
1094          * We got our sample
1095          */
1096         i_data_size = sample.p_sample->GetActualDataLength();
1097         sample.p_sample->GetPointer( &p_data );
1098
1099         REFERENCE_TIME i_pts, i_end_date;
1100         HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1101         if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1102
1103         if( !i_pts )
1104         {
1105             if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1106             {
1107                 /* Use our data timestamp */
1108                 i_pts = sample.i_timestamp;
1109                 p_stream->b_pts = VLC_TRUE;
1110             }
1111         }
1112
1113 #if 0
1114         msg_Dbg( p_input, "Read() stream: %i PTS: "I64Fd, i_stream, i_pts );
1115 #endif
1116
1117         /* Create pseudo header */
1118         SetDWBE( &p_sys->p_header[0], i_stream );
1119         SetDWBE( &p_sys->p_header[4], i_data_size );
1120         SetQWBE( &p_sys->p_header[8], i_pts  * 9 / 1000 );
1121
1122 #if 0
1123         msg_Info( p_input, "access read %i data_size %i", i_len, i_data_size );
1124 #endif
1125
1126         /* First copy header */
1127         memcpy( p_buffer, p_sys->p_header, 16 /* header size */ );
1128         p_buffer += 16 /* header size */;
1129
1130         /* Then copy stream data if any */
1131         if( !p_stream->b_invert )
1132         {
1133             p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
1134             p_buffer += i_data_size;
1135         }
1136         else
1137         {
1138             int i_width = p_stream->header.video.bmiHeader.biWidth;
1139             int i_height = p_stream->header.video.bmiHeader.biHeight;
1140             if( i_height < 0 ) i_height = - i_height;
1141
1142             switch( p_stream->i_fourcc )
1143             {
1144             case VLC_FOURCC( 'R', 'V', '1', '5' ):
1145             case VLC_FOURCC( 'R', 'V', '1', '6' ):
1146                 i_width *= 2;
1147                 break;
1148             case VLC_FOURCC( 'R', 'V', '2', '4' ):
1149                 i_width *= 3;
1150                 break;
1151             case VLC_FOURCC( 'R', 'V', '3', '2' ):
1152             case VLC_FOURCC( 'R', 'G', 'B', 'A' ):
1153                 i_width *= 4;
1154                 break;
1155             }
1156
1157             for( int i = i_height - 1; i >= 0; i-- )
1158             {
1159                 p_input->p_vlc->pf_memcpy( p_buffer,
1160                      &p_data[i * i_width], i_width );
1161
1162                 p_buffer += i_width;
1163             }
1164         }
1165
1166         sample.p_sample->Release();
1167
1168         /* The caller got what he wanted */
1169         return p_buffer - p_buf_orig;
1170     }
1171
1172     return 0; /* never reached */
1173 }
1174
1175 /*****************************************************************************
1176  * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1177  *****************************************************************************
1178  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1179  * bytes.
1180  *****************************************************************************/
1181 static ssize_t ReadCompressed( input_thread_t * p_input, byte_t * p_buffer,
1182                                size_t i_len )
1183 {
1184     access_sys_t   *p_sys = p_input->p_access_data;
1185     dshow_stream_t *p_stream = NULL;
1186     VLCMediaSample  sample;
1187     int             i_data_size;
1188     uint8_t         *p_data;
1189
1190     /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1191
1192     /* There must be only 1 elementary stream to produce a valid stream
1193      * of MPEG or DV data */
1194     p_stream = p_sys->pp_streams[0];
1195
1196     while( 1 )
1197     {
1198         if( p_input->b_die || p_input->b_error ) return 0;
1199
1200         /* Get new sample/frame from the elementary stream (blocking). */
1201         vlc_mutex_lock( &p_sys->lock );
1202
1203         if( p_stream->p_capture_filter->CustomGetPin()
1204               ->CustomGetSample( &sample ) != S_OK )
1205         {
1206             /* No data available. Wait until some data has arrived */
1207             vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1208             vlc_mutex_unlock( &p_sys->lock );
1209             continue;
1210         }
1211
1212         vlc_mutex_unlock( &p_sys->lock );
1213
1214         /*
1215          * We got our sample
1216          */
1217         i_data_size = sample.p_sample->GetActualDataLength();
1218         sample.p_sample->GetPointer( &p_data );
1219
1220 #if 0
1221         msg_Info( p_input, "access read %i data_size %i", i_len, i_data_size );
1222 #endif
1223         i_data_size = __MIN( i_data_size, (int)i_len );
1224
1225         p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
1226
1227         sample.p_sample->Release();
1228
1229         /* The caller got what he wanted */
1230         return i_data_size;
1231     }
1232
1233     return 0; /* never reached */
1234 }
1235
1236 /*****************************************************************************
1237  * Demux: local prototypes
1238  *****************************************************************************/
1239 struct demux_sys_t
1240 {
1241     int         i_es;
1242     es_out_id_t **es;
1243 };
1244
1245 static int  Demux      ( input_thread_t * );
1246
1247 /****************************************************************************
1248  * DemuxOpen:
1249  ****************************************************************************/
1250 static int DemuxOpen( vlc_object_t *p_this )
1251 {
1252     input_thread_t *p_input = (input_thread_t *)p_this;
1253     demux_sys_t    *p_sys;
1254
1255     uint8_t        *p_peek;
1256     int            i_es;
1257     int            i;
1258
1259     /* a little test to see if it's a dshow stream */
1260     if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
1261     {
1262         msg_Warn( p_input, "dshow plugin discarded (cannot peek)" );
1263         return VLC_EGENERIC;
1264     }
1265
1266     if( memcmp( p_peek, ".dsh", 4 ) ||
1267         ( i_es = GetDWBE( &p_peek[4] ) ) <= 0 )
1268     {
1269         msg_Warn( p_input, "dshow plugin discarded (not a valid stream)" );
1270         return VLC_EGENERIC;
1271     }
1272
1273     vlc_mutex_lock( &p_input->stream.stream_lock );
1274     if( input_InitStream( p_input, 0 ) == -1)
1275     {
1276         vlc_mutex_unlock( &p_input->stream.stream_lock );
1277         msg_Err( p_input, "cannot init stream" );
1278         return VLC_EGENERIC;
1279     }
1280     p_input->stream.i_mux_rate =  0 / 50;
1281     vlc_mutex_unlock( &p_input->stream.stream_lock );
1282
1283     p_input->pf_demux = Demux;
1284     p_input->pf_demux_control = demux_vaControlDefault;
1285     p_input->p_demux_data = p_sys =
1286         (demux_sys_t *)malloc( sizeof( demux_sys_t ) );
1287     p_sys->i_es = 0;
1288     p_sys->es   = NULL;
1289
1290     if( stream_Peek( p_input->s, &p_peek, 8 + 20 * i_es ) < 8 + 20 * i_es )
1291     {
1292         msg_Err( p_input, "dshow plugin discarded (cannot peek)" );
1293         return VLC_EGENERIC;
1294     }
1295     p_peek += 8;
1296
1297     for( i = 0; i < i_es; i++ )
1298     {
1299         es_format_t fmt;
1300
1301         if( !memcmp( p_peek, "auds", 4 ) )
1302         {
1303             es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( p_peek[4], p_peek[5],
1304                                                         p_peek[6], p_peek[7] ) );
1305
1306             fmt.audio.i_channels = GetDWBE( &p_peek[8] );
1307             fmt.audio.i_rate = GetDWBE( &p_peek[12] );
1308             fmt.audio.i_bitspersample = GetDWBE( &p_peek[16] );
1309             fmt.audio.i_blockalign = fmt.audio.i_channels *
1310                                      fmt.audio.i_bitspersample / 8;
1311             fmt.i_bitrate = fmt.audio.i_channels *
1312                             fmt.audio.i_rate *
1313                             fmt.audio.i_bitspersample;
1314
1315             msg_Dbg( p_input, "new audio es %d channels %dHz",
1316                      fmt.audio.i_channels, fmt.audio.i_rate );
1317
1318             TAB_APPEND( p_sys->i_es, p_sys->es,
1319                         es_out_Add( p_input->p_es_out, &fmt ) );
1320         }
1321         else if( !memcmp( p_peek, "vids", 4 ) )
1322         {
1323             es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[4], p_peek[5],
1324                                                         p_peek[6], p_peek[7] ) );
1325             fmt.video.i_width  = GetDWBE( &p_peek[8] );
1326             fmt.video.i_height = GetDWBE( &p_peek[12] );
1327
1328             msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1329                      (char*)&fmt.i_codec,
1330                      fmt.video.i_width, fmt.video.i_height );
1331             TAB_APPEND( p_sys->i_es, p_sys->es,
1332                         es_out_Add( p_input->p_es_out, &fmt ) );
1333         }
1334
1335         p_peek += 20;
1336     }
1337
1338     /* Skip header */
1339     stream_Read( p_input->s, NULL, 8 + 20 * i_es );
1340
1341     return VLC_SUCCESS;
1342 }
1343
1344 /****************************************************************************
1345  * DemuxClose:
1346  ****************************************************************************/
1347 static void DemuxClose( vlc_object_t *p_this )
1348 {
1349     input_thread_t *p_input = (input_thread_t *)p_this;
1350     demux_sys_t    *p_sys = p_input->p_demux_data;
1351
1352     if( p_sys->i_es > 0 )
1353     {
1354         free( p_sys->es );
1355     }
1356     free( p_sys );
1357 }
1358
1359 /****************************************************************************
1360  * Demux:
1361  ****************************************************************************/
1362 static int Demux( input_thread_t *p_input )
1363 {
1364     demux_sys_t *p_sys = p_input->p_demux_data;
1365     block_t     *p_block;
1366
1367     int i_es;
1368     int i_size;
1369
1370     uint8_t *p_peek;
1371     mtime_t i_pcr;
1372
1373     if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1374     {
1375         msg_Warn( p_input, "cannot peek (EOF ?)" );
1376         return 0;
1377     }
1378
1379     i_es   = GetDWBE( &p_peek[0] );
1380     if( i_es < 0 || i_es >= p_sys->i_es )
1381     {
1382         msg_Err( p_input, "cannot find ES" );
1383         return -1;
1384     }
1385
1386     i_size = GetDWBE( &p_peek[4] );
1387     i_pcr  = GetQWBE( &p_peek[8] );
1388
1389     if( ( p_block = stream_Block( p_input->s, 16 + i_size ) ) == NULL )
1390     {
1391         msg_Warn( p_input, "cannot read data" );
1392         return 0;
1393     }
1394
1395     p_block->p_buffer += 16;
1396     p_block->i_buffer -= 16;
1397
1398     /* Call the pace control. */
1399     input_ClockManageRef( p_input, p_input->stream.p_selected_program, i_pcr );
1400
1401     p_block->i_dts =
1402     p_block->i_pts = i_pcr <= 0 ? 0 :
1403         input_ClockGetTS( p_input, p_input->stream.p_selected_program, i_pcr );
1404
1405     es_out_Send( p_input->p_es_out, p_sys->es[i_es], p_block );
1406
1407     return 1;
1408 }
1409
1410
1411 /*****************************************************************************
1412  * config variable callback
1413  *****************************************************************************/
1414 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1415                                vlc_value_t newval, vlc_value_t oldval, void * )
1416 {
1417     module_config_t *p_item;
1418     vlc_bool_t b_audio = VLC_FALSE;
1419     int i;
1420
1421     p_item = config_FindConfig( p_this, psz_name );
1422     if( !p_item ) return VLC_SUCCESS;
1423
1424     if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1425
1426     /* Clear-up the current list */
1427     if( p_item->i_list )
1428     {
1429         /* Keep the 2 first entries */
1430         for( i = 2; i < p_item->i_list; i++ )
1431         {
1432             free( p_item->ppsz_list[i] );
1433             free( p_item->ppsz_list_text[i] );
1434         }
1435         /* TODO: Remove when no more needed */
1436         p_item->ppsz_list[i] = NULL;
1437         p_item->ppsz_list_text[i] = NULL;
1438     }
1439     p_item->i_list = 2;
1440
1441     /* Find list of devices */
1442     list<string> list_devices;
1443
1444     FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1445
1446     if( !list_devices.size() ) return VLC_SUCCESS;
1447
1448     p_item->ppsz_list =
1449         (char **)realloc( p_item->ppsz_list,
1450                           (list_devices.size()+3) * sizeof(char *) );
1451     p_item->ppsz_list_text =
1452         (char **)realloc( p_item->ppsz_list_text,
1453                           (list_devices.size()+3) * sizeof(char *) );
1454
1455     list<string>::iterator iter;
1456     for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
1457          iter++, i++ )
1458     {
1459         p_item->ppsz_list[i] = strdup( iter->c_str() );
1460         p_item->ppsz_list_text[i] = NULL;
1461         p_item->i_list++;
1462     }
1463     p_item->ppsz_list[i] = NULL;
1464     p_item->ppsz_list_text[i] = NULL;
1465
1466     return VLC_SUCCESS;
1467 }
1468
1469 static void PropertiesPage( input_thread_t *p_input,
1470                             IBaseFilter *p_device_filter )
1471 {
1472     ISpecifyPropertyPages *p_spec;
1473     CAUUID cauuid;
1474  
1475     HRESULT hr = p_device_filter->QueryInterface( IID_ISpecifyPropertyPages,
1476                                                   (void **)&p_spec );
1477     if( hr == S_OK )
1478     {
1479         hr = p_spec->GetPages( &cauuid );
1480         if( hr == S_OK )
1481         {
1482             HWND hwnd_desktop = ::GetDesktopWindow();
1483
1484             hr = OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1,
1485                                          (IUnknown **)&p_device_filter,
1486                                          cauuid.cElems,
1487                                          (GUID *)cauuid.pElems, 0, 0, NULL );
1488             if( hr == S_OK )
1489             {
1490                 msg_Dbg( p_input, "made OleCreatePropertyFrame");
1491             }
1492
1493             CoTaskMemFree( cauuid.pElems );
1494         }
1495
1496         p_spec->Release();
1497     }
1498 }