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