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