1 /*****************************************************************************
2 * dshow.cpp : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: dshow.cpp,v 1.16 2003/11/24 20:45:23 gbazin Exp $
7 * Author: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
37 /*****************************************************************************
38 * Access: local prototypes
39 *****************************************************************************/
40 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
41 static ssize_t ReadDV ( input_thread_t *, byte_t *, size_t );
43 static int OpenDevice( input_thread_t *, string, vlc_bool_t );
44 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
45 list<string> *, vlc_bool_t );
46 static AM_MEDIA_TYPE EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
47 int, int, int, int, int, int );
48 static bool ConnectFilters( IFilterGraph *, IBaseFilter *, IPin * );
50 static int FindDevicesCallback( vlc_object_t *, char const *,
51 vlc_value_t, vlc_value_t, void * );
53 /* Debug only, use this to find out GUIDs */
55 UuidToString( (IID *)&IID_IAMBufferNegotiation, &p_st );
56 msg_Err( p_input, "BufferNegotiation: %s" , p_st );
80 static void SetDWBE( uint8_t *p, uint32_t dw )
82 p[0] = (dw >> 24)&0xff;
83 p[1] = (dw >> 16)&0xff;
84 p[2] = (dw >> 8)&0xff;
88 static void SetQWBE( uint8_t *p, uint64_t qw )
90 SetDWBE( p, (qw >> 32)&0xffffffff );
91 SetDWBE( &p[4], qw&0xffffffff );
94 /*****************************************************************************
96 *****************************************************************************/
97 static char *ppsz_vdev[] = { "", "none" };
98 static char *ppsz_vdev_text[] = { N_("Default"), N_("None") };
99 static char *ppsz_adev[] = { "", "none" };
100 static char *ppsz_adev_text[] = { N_("Default"), N_("None") };
102 #define CACHING_TEXT N_("Caching value in ms")
103 #define CACHING_LONGTEXT N_( \
104 "Allows you to modify the default caching value for directshow streams. " \
105 "This value should be set in miliseconds units." )
106 #define VDEV_TEXT N_("Video device name")
107 #define VDEV_LONGTEXT N_( \
108 "You can specify the name of the video device that will be used by the " \
109 "DirectShow plugin. If you don't specify anything, the default device " \
111 #define ADEV_TEXT N_("Audio device name")
112 #define ADEV_LONGTEXT N_( \
113 "You can specify the name of the audio device that will be used by the " \
114 "DirectShow plugin. If you don't specify anything, the default device " \
116 #define SIZE_TEXT N_("Video size")
117 #define SIZE_LONGTEXT N_( \
118 "You can specify the size of the video that will be displayed by the " \
119 "DirectShow plugin. If you don't specify anything the default size for " \
120 "your device will be used.")
121 #define CHROMA_TEXT N_("Video input chroma format")
122 #define CHROMA_LONGTEXT N_( \
123 "Force the DirectShow video input to use a specific chroma format " \
124 "(eg. I420 (default), RV24, etc...)")
126 static int AccessOpen ( vlc_object_t * );
127 static void AccessClose( vlc_object_t * );
129 static int DemuxOpen ( vlc_object_t * );
130 static void DemuxClose ( vlc_object_t * );
133 set_description( _("DirectShow input") );
134 add_category_hint( N_("dshow"), NULL, VLC_TRUE );
135 add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
136 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
138 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
139 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
141 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
142 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
144 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
146 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
148 add_shortcut( "dshow" );
149 set_capability( "access", 0 );
150 set_callbacks( AccessOpen, AccessClose );
153 set_description( _("DirectShow demuxer") );
154 add_shortcut( "dshow" );
155 set_capability( "demux", 200 );
156 set_callbacks( DemuxOpen, DemuxClose );
160 /****************************************************************************
161 * DirectShow elementary stream descriptor
162 ****************************************************************************/
163 typedef struct dshow_stream_t
166 IBaseFilter *p_device_filter;
167 CaptureFilter *p_capture_filter;
174 VIDEOINFOHEADER video;
183 /****************************************************************************
184 * Access descriptor declaration
185 ****************************************************************************/
191 IFilterGraph *p_graph;
192 IMediaControl *p_control;
199 /* list of elementary streams */
200 dshow_stream_t **pp_streams;
202 int i_current_stream;
204 /* misc properties */
210 /*****************************************************************************
211 * Open: open direct show device
212 *****************************************************************************/
213 static int AccessOpen( vlc_object_t *p_this )
215 input_thread_t *p_input = (input_thread_t *)p_this;
219 /* Get/parse options and open device(s) */
220 string vdevname, adevname;
221 int i_width = 0, i_height = 0, i_chroma = VLC_FOURCC('I','4','2','0');
223 var_Create( p_input, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
224 var_Get( p_input, "dshow-vdev", &val );
225 if( val.psz_string ) vdevname = string( val.psz_string );
226 if( val.psz_string ) free( val.psz_string );
228 var_Create( p_input, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
229 var_Get( p_input, "dshow-adev", &val );
230 if( val.psz_string ) adevname = string( val.psz_string );
231 if( val.psz_string ) free( val.psz_string );
233 var_Create( p_input, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
234 var_Get( p_input, "dshow-size", &val );
235 if( val.psz_string && *val.psz_string )
237 if( !strcmp( val.psz_string, "subqcif" ) )
239 i_width = 128; i_height = 96;
241 else if( !strcmp( val.psz_string, "qsif" ) )
243 i_width = 160; i_height = 120;
245 else if( !strcmp( val.psz_string, "qcif" ) )
247 i_width = 176; i_height = 144;
249 else if( !strcmp( val.psz_string, "sif" ) )
251 i_width = 320; i_height = 240;
253 else if( !strcmp( val.psz_string, "cif" ) )
255 i_width = 352; i_height = 288;
257 else if( !strcmp( val.psz_string, "vga" ) )
259 i_width = 640; i_height = 480;
265 i_width = strtol( val.psz_string, &psz_parser, 0 );
266 if( *psz_parser == 'x' || *psz_parser == 'X')
268 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
270 msg_Dbg( p_input, "Width x Height %dx%d", i_width, i_height );
273 if( val.psz_string ) free( val.psz_string );
275 var_Create( p_input, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
276 var_Get( p_input, "dshow-chroma", &val );
277 if( val.psz_string && strlen( val.psz_string ) >= 4 )
279 i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
280 val.psz_string[2], val.psz_string[3] );
282 if( val.psz_string ) free( val.psz_string );
284 p_input->pf_read = Read;
285 p_input->pf_seek = NULL;
286 p_input->pf_set_area = NULL;
287 p_input->pf_set_program = NULL;
289 vlc_mutex_lock( &p_input->stream.stream_lock );
290 p_input->stream.b_pace_control = 0;
291 p_input->stream.b_seekable = 0;
292 p_input->stream.p_selected_area->i_size = 0;
293 p_input->stream.p_selected_area->i_tell = 0;
294 p_input->stream.i_method = INPUT_METHOD_FILE;
295 vlc_mutex_unlock( &p_input->stream.stream_lock );
297 var_Create( p_input, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
298 var_Get( p_input, "dshow-caching", &val );
299 p_input->i_pts_delay = val.i_int * 1000;
301 /* Initialize OLE/COM */
302 CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
304 /* create access private data */
305 p_input->p_access_data = p_sys =
306 (access_sys_t *)malloc( sizeof( access_sys_t ) );
308 /* Initialize some data */
309 p_sys->i_streams = 0;
310 p_sys->pp_streams = (dshow_stream_t **)malloc( 1 );
311 p_sys->i_width = i_width;
312 p_sys->i_height = i_height;
313 p_sys->i_chroma = i_chroma;
316 p_sys->i_header_size = 8;
317 p_sys->p_header = (uint8_t *)malloc( p_sys->i_header_size );
318 memcpy( &p_sys->p_header[0], ".dsh", 4 );
319 SetDWBE( &p_sys->p_header[4], 1 );
320 p_sys->i_header_pos = p_sys->i_header_size;
322 /* Build directshow graph */
323 CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
324 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph );
326 p_sys->p_graph->QueryInterface( IID_IMediaControl,
327 (void **)&p_sys->p_control );
329 if( OpenDevice( p_input, vdevname, 0 ) != VLC_SUCCESS )
331 msg_Err( p_input, "can't open video");
334 if( OpenDevice( p_input, adevname, 1 ) != VLC_SUCCESS )
336 msg_Err( p_input, "can't open audio");
339 if( !p_sys->i_streams )
341 /* Release directshow objects */
342 if( p_sys->p_control ) p_sys->p_control->Release();
343 p_sys->p_graph->Release();
345 /* Uninitialize OLE/COM */
348 free( p_sys->p_header );
349 free( p_sys->pp_streams );
354 /* Initialize some data */
355 p_sys->i_current_stream = 0;
356 p_input->i_mtu += p_sys->i_header_size + 16 /* data header size */;
358 vlc_mutex_init( p_input, &p_sys->lock );
359 vlc_cond_init( p_input, &p_sys->wait );
361 /* Everything is ready. Let's rock baby */
362 p_sys->p_control->Run();
367 /*****************************************************************************
368 * AccessClose: close device
369 *****************************************************************************/
370 static void AccessClose( vlc_object_t *p_this )
372 input_thread_t *p_input = (input_thread_t *)p_this;
373 access_sys_t *p_sys = p_input->p_access_data;
375 /* Stop capturing stuff */
376 p_sys->p_control->Stop();
377 p_sys->p_control->Release();
379 /* Remove filters from graph */
380 for( int i = 0; i < p_sys->i_streams; i++ )
382 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
383 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
384 p_sys->pp_streams[i]->p_capture_filter->Release();
385 p_sys->pp_streams[i]->p_device_filter->Release();
387 p_sys->p_graph->Release();
389 /* Uninitialize OLE/COM */
392 free( p_sys->p_header );
393 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
394 free( p_sys->pp_streams );
398 /****************************************************************************
400 ****************************************************************************/
401 static bool ConnectFilters( IFilterGraph *p_graph, IBaseFilter *p_filter,
404 IEnumPins *p_enumpins;
408 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
410 while( S_OK == p_enumpins->Next( 1, &p_output_pin, &i_fetched ) )
412 if( S_OK == p_graph->ConnectDirect( p_output_pin, p_input_pin, 0 ) )
414 p_enumpins->Release();
419 p_enumpins->Release();
423 static int OpenDevice( input_thread_t *p_input, string devicename,
426 access_sys_t *p_sys = p_input->p_access_data;
427 list<string> list_devices;
429 /* Enumerate devices and display their names */
430 FindCaptureDevice( (vlc_object_t *)p_input, NULL, &list_devices, b_audio );
432 if( !list_devices.size() )
435 list<string>::iterator iter;
436 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
437 msg_Dbg( p_input, "found device: %s", iter->c_str() );
439 /* If no device name was specified, pick the 1st one */
440 if( devicename.size() == 0 )
442 devicename = *list_devices.begin();
445 // Use the system device enumerator and class enumerator to find
446 // a capture/preview device, such as a desktop USB video camera.
447 IBaseFilter *p_device_filter =
448 FindCaptureDevice( (vlc_object_t *)p_input, &devicename,
450 if( p_device_filter )
451 msg_Dbg( p_input, "using device: %s", devicename.c_str() );
454 msg_Err( p_input, "can't use device: %s", devicename.c_str() );
458 AM_MEDIA_TYPE media_type =
459 EnumDeviceCaps( (vlc_object_t *)p_input, p_device_filter,
460 p_sys->i_chroma, p_sys->i_width, p_sys->i_height,
463 /* Create and add our capture filter */
464 CaptureFilter *p_capture_filter = new CaptureFilter( p_input, media_type );
465 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
467 /* Add the device filter to the graph (seems necessary with VfW before
468 * accessing pin attributes). */
469 p_sys->p_graph->AddFilter( p_device_filter, 0 );
471 /* Attempt to connect one of this device's capture output pins */
472 msg_Dbg( p_input, "connecting filters" );
473 if( ConnectFilters( p_sys->p_graph, p_device_filter,
474 p_capture_filter->CustomGetPin() ) )
477 dshow_stream_t dshow_stream;
478 dshow_stream.b_invert = VLC_FALSE;
479 dshow_stream.b_pts = VLC_FALSE;
481 p_capture_filter->CustomGetPin()->CustomGetMediaType();
483 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
485 msg_Dbg( p_input, "MEDIATYPE_Video");
487 /* Packed RGB formats */
488 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB1 )
489 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
490 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB4 )
491 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
492 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB8 )
493 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
494 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB555 )
495 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
496 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB565 )
497 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
498 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB24 )
499 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
500 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB32 )
501 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
502 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_ARGB32 )
503 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
505 /* Packed YUV formats */
506 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YVYU )
507 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', 'Y', 'U' );
508 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YUYV )
509 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
510 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y411 )
511 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '1', 'N' );
512 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y211 )
513 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', '2', '1', '1' );
514 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YUY2 ||
515 dshow_stream.mt.subtype == MEDIASUBTYPE_UYVY )
516 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
518 /* Planar YUV formats */
519 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_I420 )
520 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '2', '0' );
521 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y41P )
522 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '1', '1' );
523 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YV12 ||
524 dshow_stream.mt.subtype == MEDIASUBTYPE_IYUV )
525 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', '1', '2' );
526 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YVU9 )
527 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', 'U', '9' );
530 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvsl )
531 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'l' );
532 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvsd )
533 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'd' );
534 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvhd )
535 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 'h', 'd' );
539 dshow_stream.header.video =
540 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
542 int i_height = dshow_stream.header.video.bmiHeader.biHeight;
544 /* Check if the image is inverted (bottom to top) */
545 if( dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '1' ) ||
546 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '4' ) ||
547 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '8' ) ||
548 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
549 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
550 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
551 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
552 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', 'A' ) )
554 if( i_height > 0 ) dshow_stream.b_invert = VLC_TRUE;
555 else i_height = - i_height;
558 /* Check if we are dealing with a DV stream */
559 if( dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 's', 'l' ) ||
560 dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 's', 'd' ) ||
561 dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 'h', 'd' ) )
563 p_input->pf_read = ReadDV;
564 if( !p_input->psz_demux || !*p_input->psz_demux )
566 p_input->psz_demux = "rawdv";
571 /* Add video stream to header */
572 p_sys->i_header_size += 20;
573 p_sys->p_header = (uint8_t *)realloc( p_sys->p_header,
574 p_sys->i_header_size );
575 memcpy( &p_sys->p_header[p_sys->i_header_pos], "vids", 4 );
576 memcpy( &p_sys->p_header[p_sys->i_header_pos + 4],
577 &dshow_stream.i_fourcc, 4 );
578 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 8],
579 dshow_stream.header.video.bmiHeader.biWidth );
580 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 12], i_height );
581 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 16], 0 );
582 p_sys->i_header_pos = p_sys->i_header_size;
584 /* Greatly simplifies the reading routine */
585 int i_mtu = dshow_stream.header.video.bmiHeader.biWidth *
587 p_input->i_mtu = __MAX( p_input->i_mtu, (unsigned int)i_mtu );
590 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio &&
591 dshow_stream.mt.formattype == FORMAT_WaveFormatEx )
593 msg_Dbg( p_input, "MEDIATYPE_Audio");
595 if( dshow_stream.mt.subtype == MEDIASUBTYPE_PCM )
596 dshow_stream.i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
597 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_IEEE_FLOAT )
598 dshow_stream.i_fourcc = VLC_FOURCC( 'f', 'l', '3', '2' );
601 dshow_stream.header.audio =
602 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
604 /* Add audio stream to header */
605 p_sys->i_header_size += 20;
606 p_sys->p_header = (uint8_t *)realloc( p_sys->p_header,
607 p_sys->i_header_size );
608 memcpy( &p_sys->p_header[p_sys->i_header_pos], "auds", 4 );
609 memcpy( &p_sys->p_header[p_sys->i_header_pos + 4],
610 &dshow_stream.i_fourcc, 4 );
611 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 8],
612 dshow_stream.header.audio.nChannels );
613 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 12],
614 dshow_stream.header.audio.nSamplesPerSec );
615 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 16],
616 dshow_stream.header.audio.wBitsPerSample );
617 p_sys->i_header_pos = p_sys->i_header_size;
619 /* Greatly simplifies the reading routine */
620 IAMBufferNegotiation *p_ambuf;
624 p_capture_filter->CustomGetPin()->ConnectedTo( &p_pin );
625 if( SUCCEEDED( p_pin->QueryInterface(
626 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
628 ALLOCATOR_PROPERTIES AllocProp;
629 memset( &AllocProp, 0, sizeof( ALLOCATOR_PROPERTIES ) );
630 p_ambuf->GetAllocatorProperties( &AllocProp );
632 i_mtu = AllocProp.cbBuffer;
637 i_mtu = dshow_stream.header.audio.nSamplesPerSec *
638 dshow_stream.header.audio.nChannels *
639 dshow_stream.header.audio.wBitsPerSample / 8;
642 p_input->i_mtu = __MAX( p_input->i_mtu, (unsigned int)i_mtu );
646 /* Add directshow elementary stream to our list */
647 dshow_stream.p_device_filter = p_device_filter;
648 dshow_stream.p_capture_filter = p_capture_filter;
651 (dshow_stream_t **)realloc( p_sys->pp_streams,
652 sizeof(dshow_stream_t *)
653 * (p_sys->i_streams + 1) );
654 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
655 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
656 SetDWBE( &p_sys->p_header[4], (uint32_t)p_sys->i_streams );
662 /* Remove filters from graph */
663 p_sys->p_graph->RemoveFilter( p_device_filter );
664 p_sys->p_graph->RemoveFilter( p_capture_filter );
666 /* Release objects */
667 p_device_filter->Release();
668 p_capture_filter->Release();
674 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
675 list<string> *p_listdevices, vlc_bool_t b_audio )
677 IBaseFilter *p_base_filter = NULL;
678 IMoniker *p_moniker = NULL;
682 /* Create the system device enumerator */
683 ICreateDevEnum *p_dev_enum = NULL;
685 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
686 IID_ICreateDevEnum, (void **)&p_dev_enum );
689 msg_Err( p_this, "failed to create the device enumerator (0x%x)", hr);
693 /* Create an enumerator for the video capture devices */
694 IEnumMoniker *p_class_enum = NULL;
696 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
699 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
701 p_dev_enum->Release();
704 msg_Err( p_this, "failed to create the class enumerator (0x%x)", hr );
708 /* If there are no enumerators for the requested type, then
709 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
710 if( p_class_enum == NULL )
712 msg_Err( p_this, "no capture device was detected." );
716 /* Enumerate the devices */
718 /* Note that if the Next() call succeeds but there are no monikers,
719 * it will return S_FALSE (which is not a failure). Therefore, we check
720 * that the return code is S_OK instead of using SUCCEEDED() macro. */
722 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
724 /* Getting the property page to get the device name */
726 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
732 hr = p_bag->Read( L"FriendlyName", &var, NULL );
736 int i_convert = ( lstrlenW( var.bstrVal ) + 1 ) * 2;
737 char *p_buf = (char *)alloca( i_convert ); p_buf[0] = 0;
738 WideCharToMultiByte( CP_ACP, 0, var.bstrVal, -1, p_buf,
739 i_convert, NULL, NULL );
740 SysFreeString(var.bstrVal);
742 if( p_listdevices ) p_listdevices->push_back( p_buf );
744 if( p_devicename && *p_devicename == string(p_buf) )
746 /* Bind Moniker to a filter object */
747 hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
748 (void **)&p_base_filter );
751 msg_Err( p_this, "couldn't bind moniker to filter "
752 "object (0x%x)", hr );
753 p_moniker->Release();
754 p_class_enum->Release();
757 p_moniker->Release();
758 p_class_enum->Release();
759 return p_base_filter;
764 p_moniker->Release();
767 p_class_enum->Release();
771 static AM_MEDIA_TYPE EnumDeviceCaps( vlc_object_t *p_this,
772 IBaseFilter *p_filter,
773 int i_chroma, int i_width, int i_height,
774 int i_channels, int i_samplespersec,
775 int i_bitspersample )
777 IEnumPins *p_enumpins;
779 IEnumMediaTypes *p_enummt;
781 AM_MEDIA_TYPE media_type;
782 media_type.majortype = GUID_NULL;
783 media_type.subtype = GUID_NULL;
784 media_type.formattype = GUID_NULL;
785 media_type.pUnk = NULL;
786 media_type.cbFormat = 0;
787 media_type.pbFormat = NULL;
789 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return media_type;
791 /*while*/if( p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
794 if( SUCCEEDED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
797 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
800 if( p_mt->majortype == MEDIATYPE_Video )
802 int i_fourcc = VLC_FOURCC(' ', ' ', ' ', ' ');
804 /* Packed RGB formats */
805 if( p_mt->subtype == MEDIASUBTYPE_RGB1 )
806 i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
807 if( p_mt->subtype == MEDIASUBTYPE_RGB4 )
808 i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
809 if( p_mt->subtype == MEDIASUBTYPE_RGB8 )
810 i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
811 else if( p_mt->subtype == MEDIASUBTYPE_RGB555 )
812 i_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
813 else if( p_mt->subtype == MEDIASUBTYPE_RGB565 )
814 i_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
815 else if( p_mt->subtype == MEDIASUBTYPE_RGB24 )
816 i_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
817 else if( p_mt->subtype == MEDIASUBTYPE_RGB32 )
818 i_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
819 else if( p_mt->subtype == MEDIASUBTYPE_ARGB32 )
820 i_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
821 else i_fourcc = *((int *)&p_mt->subtype);
823 int i_current_width = p_mt->pbFormat ?
824 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth : 0;
825 int i_current_height = p_mt->pbFormat ?
826 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight : 0;
827 if( i_current_height < 0 )
828 i_current_height = -i_current_height;
830 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
831 "accepts chroma: %4.4s, width:%i, height:%i",
832 (char *)&i_fourcc, i_current_width,
835 if( (!i_chroma || i_fourcc == i_chroma) &&
836 (!i_width || i_width == i_current_width) &&
837 (!i_height || i_height == i_current_height) )
839 /* Pick the 1st match */
842 i_width = i_current_width;
843 i_height = i_current_height;
847 FreeMediaType( *p_mt );
850 else if( p_mt->majortype == MEDIATYPE_Audio )
853 int i_current_channels =
854 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
855 int i_current_samplespersec =
856 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
857 int i_current_bitspersample =
858 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
860 if( p_mt->subtype == MEDIASUBTYPE_PCM )
861 i_fourcc = VLC_FOURCC( 'p', 'c', 'm', ' ' );
862 else i_fourcc = *((int *)&p_mt->subtype);
864 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
865 "accepts format: %4.4s, channels:%i, "
866 "samples/sec:%i bits/sample:%i",
867 (char *)&i_fourcc, i_current_channels,
868 i_current_samplespersec, i_current_bitspersample);
870 if( (!i_channels || i_channels == i_current_channels) &&
872 i_samplespersec == i_current_samplespersec) &&
874 i_bitspersample == i_current_bitspersample) )
876 /* Pick the 1st match */
878 i_channels = i_current_channels;
879 i_samplespersec = i_current_samplespersec;
880 i_bitspersample = i_current_bitspersample;
882 /* Setup a few properties like the audio latency */
883 IAMBufferNegotiation *p_ambuf;
885 if( SUCCEEDED( p_output_pin->QueryInterface(
886 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
888 ALLOCATOR_PROPERTIES AllocProp;
889 AllocProp.cbAlign = -1;
890 AllocProp.cbBuffer = i_channels * i_samplespersec *
891 i_bitspersample / 8 / 10 ; /*100 ms of latency*/
892 AllocProp.cbPrefix = -1;
893 AllocProp.cBuffers = -1;
894 p_ambuf->SuggestAllocatorProperties( &AllocProp );
900 FreeMediaType( *p_mt );
904 CoTaskMemFree( (PVOID)p_mt );
909 p_output_pin->Release();
912 p_enumpins->Release();
916 /*****************************************************************************
917 * Read: reads from the device into PES packets.
918 *****************************************************************************
919 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
921 *****************************************************************************/
922 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
925 access_sys_t *p_sys = p_input->p_access_data;
926 dshow_stream_t *p_stream = NULL;
927 byte_t *p_buf_orig = p_buffer;
928 VLCMediaSample sample;
932 if( p_sys->i_header_pos )
934 /* First header of the stream */
935 memcpy( p_buffer, p_sys->p_header, p_sys->i_header_size );
936 p_buffer += p_sys->i_header_size;
937 p_sys->i_header_pos = 0;
942 /* Get new sample/frame from next elementary stream.
943 * We first loop through all the elementary streams and if all our
944 * fifos are empty we block until we are signaled some new data has
946 vlc_mutex_lock( &p_sys->lock );
949 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
951 p_stream = p_sys->pp_streams[i_stream];
952 if( p_stream->mt.majortype == MEDIATYPE_Audio &&
953 p_stream->p_capture_filter &&
954 p_stream->p_capture_filter->CustomGetPin()
955 ->CustomGetSample( &sample ) == S_OK )
960 if( i_stream == p_sys->i_streams )
961 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
963 p_stream = p_sys->pp_streams[i_stream];
964 if( p_stream->p_capture_filter &&
965 p_stream->p_capture_filter->CustomGetPin()
966 ->CustomGetSample( &sample ) == S_OK )
971 if( i_stream == p_sys->i_streams )
973 /* No data available. Wait until some data has arrived */
974 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
975 vlc_mutex_unlock( &p_sys->lock );
979 vlc_mutex_unlock( &p_sys->lock );
984 i_data_size = sample.p_sample->GetActualDataLength();
985 sample.p_sample->GetPointer( &p_data );
987 REFERENCE_TIME i_pts, i_end_date;
988 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
989 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
993 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
995 /* Use our data timestamp */
996 i_pts = sample.i_timestamp;
997 p_stream->b_pts = VLC_TRUE;
1002 msg_Dbg( p_input, "Read() stream: %i PTS: "I64Fd, i_stream, i_pts );
1005 /* Create pseudo header */
1006 SetDWBE( &p_sys->p_header[0], i_stream );
1007 SetDWBE( &p_sys->p_header[4], i_data_size );
1008 SetQWBE( &p_sys->p_header[8], i_pts * 9 / 1000 );
1011 msg_Info( p_input, "access read %i data_size %i", i_len, i_data_size );
1014 /* First copy header */
1015 memcpy( p_buffer, p_sys->p_header, 16 /* header size */ );
1016 p_buffer += 16 /* header size */;
1018 /* Then copy stream data if any */
1019 if( !p_stream->b_invert )
1021 p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
1022 p_buffer += i_data_size;
1026 int i_width = p_stream->header.video.bmiHeader.biWidth;
1027 int i_height = p_stream->header.video.bmiHeader.biHeight;
1028 if( i_height < 0 ) i_height = - i_height;
1030 switch( p_stream->i_fourcc )
1032 case VLC_FOURCC( 'R', 'V', '1', '5' ):
1033 case VLC_FOURCC( 'R', 'V', '1', '6' ):
1036 case VLC_FOURCC( 'R', 'V', '2', '4' ):
1039 case VLC_FOURCC( 'R', 'V', '3', '2' ):
1040 case VLC_FOURCC( 'R', 'G', 'B', 'A' ):
1045 for( int i = i_height - 1; i >= 0; i-- )
1047 p_input->p_vlc->pf_memcpy( p_buffer,
1048 &p_data[i * i_width], i_width );
1050 p_buffer += i_width;
1054 sample.p_sample->Release();
1056 /* The caller got what he wanted */
1057 return p_buffer - p_buf_orig;
1060 return 0; /* never reached */
1063 /*****************************************************************************
1064 * ReadDV: reads from the DV device into PES packets.
1065 *****************************************************************************
1066 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1068 *****************************************************************************/
1069 static ssize_t ReadDV( input_thread_t * p_input, byte_t * p_buffer,
1072 access_sys_t *p_sys = p_input->p_access_data;
1073 dshow_stream_t *p_stream = NULL;
1074 VLCMediaSample sample;
1078 /* Read 1 DV frame (they contain the video and audio data) */
1080 /* There must be only 1 elementary stream to produce a valid
1082 p_stream = p_sys->pp_streams[0];
1086 /* Get new sample/frame from the elementary stream (blocking). */
1087 vlc_mutex_lock( &p_sys->lock );
1089 if( p_stream->p_capture_filter->CustomGetPin()
1090 ->CustomGetSample( &sample ) != S_OK )
1092 /* No data available. Wait until some data has arrived */
1093 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1094 vlc_mutex_unlock( &p_sys->lock );
1098 vlc_mutex_unlock( &p_sys->lock );
1103 i_data_size = sample.p_sample->GetActualDataLength();
1104 sample.p_sample->GetPointer( &p_data );
1106 REFERENCE_TIME i_pts, i_end_date;
1107 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1108 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1112 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1114 /* Use our data timestamp */
1115 i_pts = sample.i_timestamp;
1116 p_stream->b_pts = VLC_TRUE;
1121 msg_Info( p_input, "access read %i data_size %i PTS: "I64Fd,
1122 i_len, i_data_size, i_pts );
1125 p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
1127 sample.p_sample->Release();
1129 /* The caller got what he wanted */
1133 return 0; /* never reached */
1136 /*****************************************************************************
1137 * Demux: local prototypes
1138 *****************************************************************************/
1145 static int Demux ( input_thread_t * );
1147 /****************************************************************************
1149 ****************************************************************************/
1150 static int DemuxOpen( vlc_object_t *p_this )
1152 input_thread_t *p_input = (input_thread_t *)p_this;
1159 /* a little test to see if it's a dshow stream */
1160 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
1162 msg_Warn( p_input, "dshow plugin discarded (cannot peek)" );
1163 return VLC_EGENERIC;
1166 if( memcmp( p_peek, ".dsh", 4 ) ||
1167 ( i_es = GetDWBE( &p_peek[4] ) ) <= 0 )
1169 msg_Warn( p_input, "dshow plugin discarded (not a valid stream)" );
1170 return VLC_EGENERIC;
1173 vlc_mutex_lock( &p_input->stream.stream_lock );
1174 if( input_InitStream( p_input, 0 ) == -1)
1176 vlc_mutex_unlock( &p_input->stream.stream_lock );
1177 msg_Err( p_input, "cannot init stream" );
1178 return VLC_EGENERIC;
1180 p_input->stream.i_mux_rate = 0 / 50;
1181 vlc_mutex_unlock( &p_input->stream.stream_lock );
1183 p_input->pf_demux = Demux;
1184 p_input->pf_demux_control = demux_vaControlDefault;
1185 p_input->p_demux_data = p_sys =
1186 (demux_sys_t *)malloc( sizeof( demux_sys_t ) );
1190 if( stream_Peek( p_input->s, &p_peek, 8 + 20 * i_es ) < 8 + 20 * i_es )
1192 msg_Err( p_input, "dshow plugin discarded (cannot peek)" );
1193 return VLC_EGENERIC;
1197 for( i = 0; i < i_es; i++ )
1201 if( !memcmp( p_peek, "auds", 4 ) )
1203 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( p_peek[4], p_peek[5],
1204 p_peek[6], p_peek[7] ) );
1206 fmt.audio.i_channels = GetDWBE( &p_peek[8] );
1207 fmt.audio.i_rate = GetDWBE( &p_peek[12] );
1208 fmt.audio.i_bitspersample = GetDWBE( &p_peek[16] );
1209 fmt.audio.i_blockalign = fmt.audio.i_channels *
1210 fmt.audio.i_bitspersample / 8;
1211 fmt.i_bitrate = fmt.audio.i_channels *
1213 fmt.audio.i_bitspersample;
1215 msg_Dbg( p_input, "new audio es %d channels %dHz",
1216 fmt.audio.i_channels, fmt.audio.i_rate );
1218 TAB_APPEND( p_sys->i_es, p_sys->es,
1219 es_out_Add( p_input->p_es_out, &fmt ) );
1221 else if( !memcmp( p_peek, "vids", 4 ) )
1223 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[4], p_peek[5],
1224 p_peek[6], p_peek[7] ) );
1225 fmt.video.i_width = GetDWBE( &p_peek[8] );
1226 fmt.video.i_height = GetDWBE( &p_peek[12] );
1228 msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1229 (char*)&fmt.i_codec,
1230 fmt.video.i_width, fmt.video.i_height );
1231 TAB_APPEND( p_sys->i_es, p_sys->es,
1232 es_out_Add( p_input->p_es_out, &fmt ) );
1239 stream_Read( p_input->s, NULL, 8 + 20 * i_es );
1244 /****************************************************************************
1246 ****************************************************************************/
1247 static void DemuxClose( vlc_object_t *p_this )
1249 input_thread_t *p_input = (input_thread_t *)p_this;
1250 demux_sys_t *p_sys = p_input->p_demux_data;
1252 if( p_sys->i_es > 0 )
1259 /****************************************************************************
1261 ****************************************************************************/
1262 static int Demux( input_thread_t *p_input )
1264 demux_sys_t *p_sys = p_input->p_demux_data;
1273 if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1275 msg_Warn( p_input, "cannot peek (EOF ?)" );
1279 i_es = GetDWBE( &p_peek[0] );
1280 if( i_es < 0 || i_es >= p_sys->i_es )
1282 msg_Err( p_input, "cannot find ES" );
1286 i_size = GetDWBE( &p_peek[4] );
1287 i_pcr = GetQWBE( &p_peek[8] );
1289 if( ( p_block = stream_Block( p_input->s, 16 + i_size ) ) == NULL )
1291 msg_Warn( p_input, "cannot read data" );
1295 p_block->p_buffer += 16;
1296 p_block->i_buffer -= 16;
1298 /* Call the pace control. */
1299 input_ClockManageRef( p_input, p_input->stream.p_selected_program, i_pcr );
1302 p_block->i_pts = i_pcr <= 0 ? 0 :
1303 input_ClockGetTS( p_input, p_input->stream.p_selected_program, i_pcr );
1305 es_out_Send( p_input->p_es_out, p_sys->es[i_es], p_block );
1311 /*****************************************************************************
1312 * config variable callback
1313 *****************************************************************************/
1314 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1315 vlc_value_t newval, vlc_value_t oldval, void * )
1317 module_config_t *p_item;
1318 vlc_bool_t b_audio = VLC_FALSE;
1321 p_item = config_FindConfig( p_this, psz_name );
1322 if( !p_item ) return VLC_SUCCESS;
1324 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1326 /* Clear-up the current list */
1327 if( p_item->i_list )
1329 /* Keep the 2 first entries */
1330 for( i = 2; i < p_item->i_list; i++ )
1332 free( p_item->ppsz_list[i] );
1333 free( p_item->ppsz_list_text[i] );
1335 /* TODO: Remove when no more needed */
1336 p_item->ppsz_list[i] = NULL;
1337 p_item->ppsz_list_text[i] = NULL;
1341 /* Find list of devices */
1342 list<string> list_devices;
1344 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1346 if( !list_devices.size() ) return VLC_SUCCESS;
1349 (char **)realloc( p_item->ppsz_list,
1350 (list_devices.size()+3) * sizeof(char *) );
1351 p_item->ppsz_list_text =
1352 (char **)realloc( p_item->ppsz_list_text,
1353 (list_devices.size()+3) * sizeof(char *) );
1355 list<string>::iterator iter;
1356 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
1359 p_item->ppsz_list[i] = strdup( iter->c_str() );
1360 p_item->ppsz_list_text[i] = strdup( iter->c_str() );
1363 p_item->ppsz_list[i] = NULL;
1364 p_item->ppsz_list_text[i] = NULL;