+ vlc_mutex_init( &p_sys->frame_lock );
+ vlc_cond_init( &p_sys->has_frame );
+ p_sys->b_first_frame = true;
+
+ IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
+ if( !decklink_iterator )
+ {
+ msg_Err( p_demux, "DeckLink drivers not found." );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ HRESULT result;
+
+ i_card_index = var_InheritInteger( p_demux, "sdi-card-index" );
+ for( int i = 0; i <= i_card_index; ++i )
+ {
+ if( p_sys->p_card )
+ p_sys->p_card->Release();
+ result = decklink_iterator->Next( &p_sys->p_card );
+ if( result != S_OK )
+ break;
+ }
+
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "DeckLink PCI card %d not found", i_card_index );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ const char *psz_model_name;
+ result = p_sys->p_card->GetModelName( &psz_model_name );
+
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Could not get model name" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ msg_Dbg( p_demux, "Opened DeckLink PCI card %d (%s)", i_card_index, psz_model_name );
+
+ if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
+ {
+ msg_Err( p_demux, "Card has no inputs" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ /* Set up the video and audio sources. */
+ IDeckLinkConfiguration *p_config;
+ if( p_sys->p_card->QueryInterface( IID_IDeckLinkConfiguration, (void**)&p_config) != S_OK )
+ {
+ msg_Err( p_demux, "Failed to get configuration interface" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ psz_video_connection = var_CreateGetNonEmptyString( p_demux, "sdi-video-connection" );
+ if( psz_video_connection )
+ {
+ BMDVideoConnection conn;
+ if ( !strcmp( psz_video_connection, "sdi" ) )
+ conn = bmdVideoConnectionSDI;
+ else if ( !strcmp( psz_video_connection, "hdmi" ) )
+ conn = bmdVideoConnectionHDMI;
+ else if ( !strcmp( psz_video_connection, "opticalsdi" ) )
+ conn = bmdVideoConnectionOpticalSDI;
+ else if ( !strcmp( psz_video_connection, "component" ) )
+ conn = bmdVideoConnectionComponent;
+ else if ( !strcmp( psz_video_connection, "composite" ) )
+ conn = bmdVideoConnectionComposite;
+ else if ( !strcmp( psz_video_connection, "svideo" ) )
+ conn = bmdVideoConnectionSVideo;
+ else
+ {
+ msg_Err( p_demux, "Invalid --sdi-video-connection specified; choose one of " \
+ "sdi, hdmi, opticalsdi, component, composite, or svideo." );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ msg_Dbg( p_demux, "Setting video input format to 0x%x", conn);
+ result = p_config->SetVideoInputFormat( conn );
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Failed to set video input connection" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+ }
+
+ psz_audio_connection = var_CreateGetNonEmptyString( p_demux, "sdi-audio-connection" );
+ if( psz_audio_connection )
+ {
+ BMDAudioConnection conn;
+ if ( !strcmp( psz_audio_connection, "embedded" ) )
+ conn = bmdAudioConnectionEmbedded;
+ else if ( !strcmp( psz_audio_connection, "aesebu" ) )
+ conn = bmdAudioConnectionAESEBU;
+ else if ( !strcmp( psz_audio_connection, "analog" ) )
+ conn = bmdAudioConnectionAnalog;
+ else
+ {
+ msg_Err( p_demux, "Invalid --sdi-audio-connection specified; choose one of " \
+ "embedded, aesebu, or analog." );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ msg_Dbg( p_demux, "Setting audio input format to 0x%x", conn);
+ result = p_config->SetAudioInputFormat( conn );
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Failed to set audio input connection" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+ }
+
+ /* Get the list of display modes. */
+ IDeckLinkDisplayModeIterator *p_display_iterator;
+ result = p_sys->p_input->GetDisplayModeIterator( &p_display_iterator );
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Failed to enumerate display modes" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ psz_display_mode = var_InheritString( p_demux, "sdi-mode" );
+ if( !psz_display_mode || strlen( psz_display_mode ) == 0 || strlen( psz_display_mode ) > 4 ) {
+ msg_Err( p_demux, "Missing or invalid --sdi-mode string" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ /*
+ * Pad the --sdi-mode string to four characters, so the user can specify e.g. "pal"
+ * without having to add the trailing space.
+ */
+ char sz_display_mode_padded[5];
+ strcpy(sz_display_mode_padded, " ");
+ for( int i = 0; i < strlen( psz_display_mode ); ++i )
+ sz_display_mode_padded[i] = psz_display_mode[i];
+
+ BMDDisplayMode wanted_mode_id;
+ memcpy( &wanted_mode_id, &sz_display_mode_padded, sizeof(wanted_mode_id) );
+
+ b_found_mode = false;
+
+ for (;;)
+ {
+ IDeckLinkDisplayMode *p_display_mode;
+ result = p_display_iterator->Next( &p_display_mode );
+ if( result != S_OK || !p_display_mode )
+ break;
+
+ char sz_mode_id_text[5] = {0};
+ BMDDisplayMode mode_id = ntohl( p_display_mode->GetDisplayMode() );
+ memcpy( sz_mode_id_text, &mode_id, sizeof(mode_id) );
+
+ const char *psz_mode_name;
+ result = p_display_mode->GetName( &psz_mode_name );
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Failed to get display mode name" );
+ p_display_mode->Release();
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ BMDTimeValue frame_duration, time_scale;
+ result = p_display_mode->GetFrameRate( &frame_duration, &time_scale );
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Failed to get frame rate" );
+ p_display_mode->Release();
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ const char *psz_field_dominance;
+ uint32_t i_dominance_flags = 0;
+ switch( p_display_mode->GetFieldDominance() )
+ {
+ case bmdProgressiveFrame:
+ psz_field_dominance = "";
+ break;
+ case bmdProgressiveSegmentedFrame:
+ psz_field_dominance = ", segmented";
+ break;
+ case bmdLowerFieldFirst:
+ psz_field_dominance = ", interlaced [BFF]";
+ i_dominance_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
+ break;
+ case bmdUpperFieldFirst:
+ psz_field_dominance = ", interlaced [TFF]";
+ i_dominance_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
+ break;
+ case bmdUnknownFieldDominance:
+ default:
+ psz_field_dominance = ", unknown field dominance";
+ break;
+ }
+
+ msg_Dbg( p_demux, "Found mode '%s': %s (%dx%d, %.3f fps%s)",
+ sz_mode_id_text, psz_mode_name,
+ p_display_mode->GetWidth(), p_display_mode->GetHeight(),
+ double(time_scale) / frame_duration, psz_field_dominance );
+
+ if( wanted_mode_id == mode_id )
+ {
+ b_found_mode = true;
+ p_sys->i_width = p_display_mode->GetWidth();
+ p_sys->i_height = p_display_mode->GetHeight();
+ p_sys->i_fps_num = time_scale;
+ p_sys->i_fps_den = frame_duration;
+ p_sys->i_dominance_flags = i_dominance_flags;
+ }
+
+ p_display_mode->Release();
+ }
+
+ if( !b_found_mode )
+ {
+ msg_Err( p_demux, "Unknown SDI mode specified. " \
+ "Run VLC with -v --verbose-objects=-all,+sdi " \
+ "to get a list of supported modes." );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ result = p_sys->p_input->EnableVideoInput( htonl( wanted_mode_id ), bmdFormat8BitYUV, 0 );
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Failed to enable video input" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+
+ /* Set up audio. */
+ p_sys->i_rate = var_InheritInteger( p_demux, "sdi-audio-rate" );
+ p_sys->i_channels = var_InheritInteger( p_demux, "sdi-audio-channels" );
+ if( p_sys->i_rate > 0 && p_sys->i_channels > 0 )
+ {
+ result = p_sys->p_input->EnableAudioInput( p_sys->i_rate, bmdAudioSampleType16bitInteger, p_sys->i_channels );
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Failed to enable audio input" );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }
+ }
+
+ p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
+ p_sys->p_input->SetCallback( p_sys->p_delegate );
+
+ result = p_sys->p_input->StartStreams();
+ if( result != S_OK )
+ {
+ msg_Err( p_demux, "Could not start streaming from SDI card. This could be caused "
+ "by invalid video mode or flags, access denied, or card already in use." );
+ ret = VLC_EGENERIC;
+ goto finish;
+ }