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 $
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 /*****************************************************************************
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 );
45 static int DemuxOpen ( vlc_object_t * );
46 static void DemuxClose ( vlc_object_t * );
47 static int Demux ( input_thread_t * );
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 * );
56 static int FindDevicesCallback( vlc_object_t *, char const *,
57 vlc_value_t, vlc_value_t, void * );
59 /* Debug only, use this to find out GUIDs */
61 UuidToString( (IID *)&IID_IAMBufferNegotiation, &p_st );
62 msg_Err( p_input, "BufferNegotiation: %s" , p_st );
65 /*****************************************************************************
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") };
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 " \
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 " \
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...)")
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 );
103 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
104 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
106 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
107 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
109 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
111 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
113 add_shortcut( "dshow" );
114 set_capability( "access", 0 );
115 set_callbacks( AccessOpen, AccessClose );
118 set_description( _("DirectShow demuxer") );
119 add_shortcut( "dshow" );
120 set_capability( "demux", 200 );
121 set_callbacks( DemuxOpen, DemuxClose );
125 /****************************************************************************
127 ****************************************************************************/
133 * fcc "auds"|"vids" 0
150 static void SetDWBE( uint8_t *p, uint32_t dw )
152 p[0] = (dw >> 24)&0xff;
153 p[1] = (dw >> 16)&0xff;
154 p[2] = (dw >> 8)&0xff;
158 static void SetQWBE( uint8_t *p, uint64_t qw )
160 SetDWBE( p, (qw >> 32)&0xffffffff );
161 SetDWBE( &p[4], qw&0xffffffff );
164 /****************************************************************************
165 * DirectShow elementary stream descriptor
166 ****************************************************************************/
167 typedef struct dshow_stream_t
170 IBaseFilter *p_device_filter;
171 CaptureFilter *p_capture_filter;
178 VIDEOINFOHEADER video;
187 /****************************************************************************
188 * Access descriptor declaration
189 ****************************************************************************/
195 IFilterGraph *p_graph;
196 IMediaControl *p_control;
203 /* list of elementary streams */
204 dshow_stream_t **pp_streams;
206 int i_current_stream;
208 /* misc properties */
214 /*****************************************************************************
215 * Open: open direct show device
216 *****************************************************************************/
217 static int AccessOpen( vlc_object_t *p_this )
219 input_thread_t *p_input = (input_thread_t *)p_this;
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');
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 );
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 );
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 )
241 if( !strcmp( val.psz_string, "subqcif" ) )
243 i_width = 128; i_height = 96;
245 else if( !strcmp( val.psz_string, "qsif" ) )
247 i_width = 160; i_height = 120;
249 else if( !strcmp( val.psz_string, "qcif" ) )
251 i_width = 176; i_height = 144;
253 else if( !strcmp( val.psz_string, "sif" ) )
255 i_width = 320; i_height = 240;
257 else if( !strcmp( val.psz_string, "cif" ) )
259 i_width = 352; i_height = 288;
261 else if( !strcmp( val.psz_string, "vga" ) )
263 i_width = 640; i_height = 480;
269 i_width = strtol( val.psz_string, &psz_parser, 0 );
270 if( *psz_parser == 'x' || *psz_parser == 'X')
272 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
274 msg_Dbg( p_input, "Width x Height %dx%d", i_width, i_height );
277 if( val.psz_string ) free( val.psz_string );
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 )
283 i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
284 val.psz_string[2], val.psz_string[3] );
286 if( val.psz_string ) free( val.psz_string );
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;
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 );
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;
305 /* Initialize OLE/COM */
306 CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
308 /* create access private data */
309 p_input->p_access_data = p_sys =
310 (access_sys_t *)malloc( sizeof( access_sys_t ) );
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;
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;
326 /* Build directshow graph */
327 CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
328 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph );
330 p_sys->p_graph->QueryInterface( IID_IMediaControl,
331 (void **)&p_sys->p_control );
333 if( OpenDevice( p_input, vdevname, 0 ) != VLC_SUCCESS )
335 msg_Err( p_input, "can't open video");
338 if( OpenDevice( p_input, adevname, 1 ) != VLC_SUCCESS )
340 msg_Err( p_input, "can't open audio");
343 if( !p_sys->i_streams )
345 /* Release directshow objects */
346 if( p_sys->p_control ) p_sys->p_control->Release();
347 p_sys->p_graph->Release();
349 /* Uninitialize OLE/COM */
352 free( p_sys->p_header );
353 free( p_sys->pp_streams );
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 */;
362 vlc_mutex_init( p_input, &p_sys->lock );
363 vlc_cond_init( p_input, &p_sys->wait );
365 /* Everything is ready. Let's rock baby */
366 p_sys->p_control->Run();
371 /*****************************************************************************
372 * AccessClose: close device
373 *****************************************************************************/
374 static void AccessClose( vlc_object_t *p_this )
376 input_thread_t *p_input = (input_thread_t *)p_this;
377 access_sys_t *p_sys = p_input->p_access_data;
379 /* Stop capturing stuff */
380 p_sys->p_control->Stop();
381 p_sys->p_control->Release();
383 /* Remove filters from graph */
384 for( int i = 0; i < p_sys->i_streams; i++ )
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();
391 p_sys->p_graph->Release();
393 /* Uninitialize OLE/COM */
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 );
402 /****************************************************************************
404 ****************************************************************************/
405 static bool ConnectFilters( IFilterGraph *p_graph, IBaseFilter *p_filter,
408 IEnumPins *p_enumpins;
412 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
414 while( S_OK == p_enumpins->Next( 1, &p_output_pin, &i_fetched ) )
416 if( S_OK == p_graph->ConnectDirect( p_output_pin, p_input_pin, 0 ) )
418 p_enumpins->Release();
423 p_enumpins->Release();
427 static int OpenDevice( input_thread_t *p_input, string devicename,
430 access_sys_t *p_sys = p_input->p_access_data;
431 list<string> list_devices;
433 /* Enumerate devices and display their names */
434 FindCaptureDevice( (vlc_object_t *)p_input, NULL, &list_devices, b_audio );
436 if( !list_devices.size() )
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() );
443 /* If no device name was specified, pick the 1st one */
444 if( devicename.size() == 0 )
446 devicename = *list_devices.begin();
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,
454 if( p_device_filter )
455 msg_Dbg( p_input, "using device: %s", devicename.c_str() );
458 msg_Err( p_input, "can't use device: %s", devicename.c_str() );
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,
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 );
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 );
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() ) )
481 dshow_stream_t dshow_stream;
482 dshow_stream.b_invert = VLC_FALSE;
483 dshow_stream.b_pts = VLC_FALSE;
485 p_capture_filter->CustomGetPin()->CustomGetMediaType();
487 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
489 msg_Dbg( p_input, "MEDIATYPE_Video");
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' );
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' );
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' );
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' );
543 dshow_stream.header.video =
544 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
546 int i_height = dshow_stream.header.video.bmiHeader.biHeight;
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' ) )
558 if( i_height > 0 ) dshow_stream.b_invert = VLC_TRUE;
559 else i_height = - i_height;
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' ) )
567 p_input->pf_read = ReadDV;
568 if( !p_input->psz_demux || !*p_input->psz_demux )
570 p_input->psz_demux = "rawdv";
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;
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);
594 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio &&
595 dshow_stream.mt.formattype == FORMAT_WaveFormatEx )
597 msg_Dbg( p_input, "MEDIATYPE_Audio");
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' );
605 dshow_stream.header.audio =
606 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
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;
623 /* Greatly simplifies the reading routine */
624 IAMBufferNegotiation *p_ambuf;
628 p_capture_filter->CustomGetPin()->ConnectedTo( &p_pin );
629 if( SUCCEEDED( p_pin->QueryInterface(
630 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
632 ALLOCATOR_PROPERTIES AllocProp;
633 memset( &AllocProp, 0, sizeof( ALLOCATOR_PROPERTIES ) );
634 p_ambuf->GetAllocatorProperties( &AllocProp );
636 i_mtu = AllocProp.cbBuffer;
641 i_mtu = dshow_stream.header.audio.nSamplesPerSec *
642 dshow_stream.header.audio.nChannels *
643 dshow_stream.header.audio.wBitsPerSample / 8;
646 p_input->i_mtu = __MAX( p_input->i_mtu, i_mtu );
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;
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 );
666 /* Remove filters from graph */
667 p_sys->p_graph->RemoveFilter( p_device_filter );
668 p_sys->p_graph->RemoveFilter( p_capture_filter );
670 /* Release objects */
671 p_device_filter->Release();
672 p_capture_filter->Release();
678 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
679 list<string> *p_listdevices, vlc_bool_t b_audio )
681 IBaseFilter *p_base_filter = NULL;
682 IMoniker *p_moniker = NULL;
686 /* Create the system device enumerator */
687 ICreateDevEnum *p_dev_enum = NULL;
689 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
690 IID_ICreateDevEnum, (void **)&p_dev_enum );
693 msg_Err( p_this, "failed to create the device enumerator (0x%x)", hr);
697 /* Create an enumerator for the video capture devices */
698 IEnumMoniker *p_class_enum = NULL;
700 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
703 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
705 p_dev_enum->Release();
708 msg_Err( p_this, "failed to create the class enumerator (0x%x)", hr );
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 )
716 msg_Err( p_this, "no capture device was detected." );
720 /* Enumerate the devices */
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. */
726 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
728 /* Getting the property page to get the device name */
730 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
736 hr = p_bag->Read( L"FriendlyName", &var, NULL );
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);
746 if( p_listdevices ) p_listdevices->push_back( p_buf );
748 if( p_devicename && *p_devicename == string(p_buf) )
750 /* Bind Moniker to a filter object */
751 hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
752 (void **)&p_base_filter );
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();
761 p_moniker->Release();
762 p_class_enum->Release();
763 return p_base_filter;
768 p_moniker->Release();
771 p_class_enum->Release();
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 )
781 IEnumPins *p_enumpins;
783 IEnumMediaTypes *p_enummt;
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;
793 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return media_type;
795 /*while*/if( p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
798 if( SUCCEEDED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
801 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
804 if( p_mt->majortype == MEDIATYPE_Video )
806 int i_fourcc = VLC_FOURCC(' ', ' ', ' ', ' ');
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);
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;
832 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
833 "accepts chroma: %4.4s, width:%i, height:%i",
834 (char *)&i_fourcc, i_current_width,
837 if( (!i_chroma || i_fourcc == i_chroma) &&
838 (!i_width || i_width == i_current_width) &&
839 (!i_height || i_height == i_current_height) )
841 /* Pick the 1st match */
844 i_width = i_current_width;
845 i_height = i_current_height;
849 FreeMediaType( *p_mt );
852 else if( p_mt->majortype == MEDIATYPE_Audio )
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;
862 if( p_mt->subtype == MEDIASUBTYPE_PCM )
863 i_fourcc = VLC_FOURCC( 'p', 'c', 'm', ' ' );
864 else i_fourcc = *((int *)&p_mt->subtype);
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);
872 if( (!i_channels || i_channels == i_current_channels) &&
874 i_samplespersec == i_current_samplespersec) &&
876 i_bitspersample == i_current_bitspersample) )
878 /* Pick the 1st match */
880 i_channels = i_current_channels;
881 i_samplespersec = i_current_samplespersec;
882 i_bitspersample = i_current_bitspersample;
884 /* Setup a few properties like the audio latency */
885 IAMBufferNegotiation *p_ambuf;
887 if( SUCCEEDED( p_output_pin->QueryInterface(
888 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
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 );
902 FreeMediaType( *p_mt );
906 CoTaskMemFree( (PVOID)p_mt );
911 p_output_pin->Release();
914 p_enumpins->Release();
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
923 *****************************************************************************/
924 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
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;
934 if( p_sys->i_header_pos )
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;
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
948 vlc_mutex_lock( &p_sys->lock );
951 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
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 )
962 if( i_stream == p_sys->i_streams )
963 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
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 )
973 if( i_stream == p_sys->i_streams )
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 );
981 vlc_mutex_unlock( &p_sys->lock );
986 i_data_size = sample.p_sample->GetActualDataLength();
987 sample.p_sample->GetPointer( &p_data );
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;
995 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
997 /* Use our data timestamp */
998 i_pts = sample.i_timestamp;
999 p_stream->b_pts = VLC_TRUE;
1004 msg_Dbg( p_input, "Read() stream: %i PTS: "I64Fd, i_stream, i_pts );
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 );
1013 msg_Info( p_input, "access read %i data_size %i", i_len, i_data_size );
1016 /* First copy header */
1017 memcpy( p_buffer, p_sys->p_header, 16 /* header size */ );
1018 p_buffer += 16 /* header size */;
1020 /* Then copy stream data if any */
1021 if( !p_stream->b_invert )
1023 p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
1024 p_buffer += i_data_size;
1028 int i_width = p_stream->header.video.bmiHeader.biWidth;
1029 int i_height = p_stream->header.video.bmiHeader.biHeight;
1031 switch( p_stream->i_fourcc )
1033 case VLC_FOURCC( 'R', 'V', '1', '5' ):
1034 case VLC_FOURCC( 'R', 'V', '1', '6' ):
1037 case VLC_FOURCC( 'R', 'V', '2', '4' ):
1040 case VLC_FOURCC( 'R', 'V', '3', '2' ):
1041 case VLC_FOURCC( 'R', 'G', 'B', 'A' ):
1046 for( int i = i_height - 1; i >= 0; i-- )
1048 p_input->p_vlc->pf_memcpy( p_buffer,
1049 &p_data[i * i_width], i_width );
1051 p_buffer += i_width;
1055 sample.p_sample->Release();
1057 /* The caller got what he wanted */
1058 return p_buffer - p_buf_orig;
1061 return 0; /* never reached */
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
1069 *****************************************************************************/
1070 static ssize_t ReadDV( input_thread_t * p_input, byte_t * p_buffer,
1073 access_sys_t *p_sys = p_input->p_access_data;
1074 dshow_stream_t *p_stream = NULL;
1075 VLCMediaSample sample;
1079 /* Read 1 DV frame (they contain the video and audio data) */
1081 /* There must be only 1 elementary stream to produce a valid
1083 p_stream = p_sys->pp_streams[0];
1087 /* Get new sample/frame from the elementary stream (blocking). */
1088 vlc_mutex_lock( &p_sys->lock );
1090 if( p_stream->p_capture_filter->CustomGetPin()
1091 ->CustomGetSample( &sample ) != S_OK )
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 );
1099 vlc_mutex_unlock( &p_sys->lock );
1104 i_data_size = sample.p_sample->GetActualDataLength();
1105 sample.p_sample->GetPointer( &p_data );
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;
1113 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1115 /* Use our data timestamp */
1116 i_pts = sample.i_timestamp;
1117 p_stream->b_pts = VLC_TRUE;
1122 msg_Info( p_input, "access read %i data_size %i PTS: "I64Fd,
1123 i_len, i_data_size, i_pts );
1126 p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
1128 sample.p_sample->Release();
1130 /* The caller got what he wanted */
1134 return 0; /* never reached */
1137 /****************************************************************************
1139 ****************************************************************************/
1140 static int DemuxOpen( vlc_object_t *p_this )
1142 input_thread_t *p_input = (input_thread_t *)p_this;
1148 data_packet_t *p_pk;
1150 /* a little test to see if it's a dshow stream */
1151 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
1153 msg_Warn( p_input, "dshow plugin discarded (cannot peek)" );
1154 return VLC_EGENERIC;
1157 if( strcmp( (const char *)p_peek, ".dsh" ) ||
1158 GetDWBE( &p_peek[4] ) <= 0 )
1160 msg_Warn( p_input, "dshow plugin discarded (not a valid stream)" );
1161 return VLC_EGENERIC;
1164 /* create one program */
1165 vlc_mutex_lock( &p_input->stream.stream_lock );
1166 if( input_InitStream( p_input, 0 ) == -1)
1168 vlc_mutex_unlock( &p_input->stream.stream_lock );
1169 msg_Err( p_input, "cannot init stream" );
1170 return VLC_EGENERIC;
1172 if( input_AddProgram( p_input, 0, 0) == NULL )
1174 vlc_mutex_unlock( &p_input->stream.stream_lock );
1175 msg_Err( p_input, "cannot add program" );
1176 return VLC_EGENERIC;
1179 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1180 p_input->stream.i_mux_rate = 0;
1182 i_streams = GetDWBE( &p_peek[4] );
1183 if( input_Peek( p_input, &p_peek, 8 + 20 * i_streams )
1184 < 8 + 20 * i_streams )
1186 msg_Err( p_input, "dshow plugin discarded (cannot peek)" );
1187 return VLC_EGENERIC;
1191 for( i = 0; i < i_streams; i++ )
1193 es_descriptor_t *p_es;
1195 if( !strncmp( (const char *)p_peek, "auds", 4 ) )
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;
1202 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1204 p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
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;
1214 msg_Dbg( p_input, "added new audio es %d channels %dHz",
1215 wf->nChannels, wf->nSamplesPerSec );
1217 input_SelectES( p_input, p_es );
1220 else if( !strncmp( (const char *)p_peek, "vids", 4 ) )
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;
1227 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1229 p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) );
1231 bih->biSize = sizeof( BITMAPINFOHEADER );
1232 bih->biWidth = GetDWBE( &p_peek[8] );
1233 bih->biHeight = GetDWBE( &p_peek[12] );
1235 bih->biBitCount = 0;
1236 bih->biCompression = 0;
1237 bih->biSizeImage= 0;
1238 bih->biXPelsPerMeter = 0;
1239 bih->biYPelsPerMeter = 0;
1241 bih->biClrImportant = 0;
1243 msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1244 (char*)&p_es->i_fourcc, bih->biWidth, bih->biHeight );
1246 input_SelectES( p_input, p_es );
1253 p_input->stream.p_selected_program->b_is_ok = 1;
1254 vlc_mutex_unlock( &p_input->stream.stream_lock );
1256 if( input_SplitBuffer( p_input, &p_pk, 8 + i_streams * 20 ) > 0 )
1258 input_DeletePacket( p_input->p_method_data, p_pk );
1261 p_input->pf_demux = Demux;
1262 p_input->pf_demux_control = demux_vaControlDefault;
1266 static void DemuxClose( vlc_object_t *p_this )
1271 static int Demux( input_thread_t *p_input )
1273 es_descriptor_t *p_es;
1274 pes_packet_t *p_pes;
1281 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
1283 msg_Warn( p_input, "cannot peek (EOF ?)" );
1287 i_stream = GetDWBE( &p_peek[0] );
1288 i_size = GetDWBE( &p_peek[4] );
1289 i_pcr = GetQWBE( &p_peek[8] );
1291 p_es = p_input->stream.p_selected_program->pp_es[i_stream];
1294 msg_Err( p_input, "cannot find ES" );
1297 p_pes = input_NewPES( p_input->p_method_data );
1300 msg_Warn( p_input, "cannot allocate PES" );
1307 data_packet_t *p_data;
1310 if( (i_read = input_SplitBuffer( p_input, &p_data, i_size ) ) <= 0 )
1312 input_DeletePES( p_input->p_method_data, p_pes );
1315 if( !p_pes->p_first )
1317 p_pes->p_first = p_data;
1318 p_pes->i_nb_data = 1;
1319 p_pes->i_pes_size = i_read;
1323 p_pes->p_last->p_next = p_data;
1325 p_pes->i_pes_size += i_read;
1327 p_pes->p_last = p_data;
1331 p_pes->p_first->p_payload_start += 16;
1332 p_pes->i_pes_size -= 16;
1334 if( p_es && p_es->p_dec )
1336 /* Call the pace control. */
1337 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1340 p_pes->i_pts = p_pes->i_dts = i_pcr <= 0 ? 0 :
1341 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
1344 input_DecodePES( p_es->p_dec, p_pes );
1348 input_DeletePES( p_input->p_method_data, p_pes );
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 * )
1361 module_config_t *p_item;
1362 vlc_bool_t b_audio = VLC_FALSE;
1365 p_item = config_FindConfig( p_this, psz_name );
1366 if( !p_item ) return VLC_SUCCESS;
1368 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1370 /* Clear-up the current list */
1371 if( p_item->i_list )
1373 /* Keep the 2 first entries */
1374 for( i = 2; i < p_item->i_list; i++ )
1376 free( p_item->ppsz_list[i] );
1377 free( p_item->ppsz_list_text[i] );
1379 /* TODO: Remove when no more needed */
1380 p_item->ppsz_list[i] = NULL;
1381 p_item->ppsz_list_text[i] = NULL;
1385 /* Find list of devices */
1386 list<string> list_devices;
1388 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1390 if( !list_devices.size() ) return VLC_SUCCESS;
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 *) );
1399 list<string>::iterator iter;
1400 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
1403 p_item->ppsz_list[i] = strdup( iter->c_str() );
1404 p_item->ppsz_list_text[i] = strdup( iter->c_str() );
1407 p_item->ppsz_list[i] = NULL;
1408 p_item->ppsz_list_text[i] = NULL;