1 /* Blackmagic SDI driver */
8 #define INT64_C(c) c ## LL
11 #include <vlc_common.h>
12 #include <vlc_plugin.h>
13 #include <vlc_input.h>
14 #include <vlc_demux.h>
15 #include <vlc_access.h>
16 #include <vlc_picture.h>
17 #include <vlc_charset.h>
19 #include <vlc_atomic.h>
21 #include <arpa/inet.h>
23 #include <DeckLinkAPI.h>
24 #include <DeckLinkAPIDispatch.cpp>
26 static int Open ( vlc_object_t * );
27 static void Close( vlc_object_t * );
29 #define CARD_INDEX_TEXT N_("Input card to use")
30 #define CARD_INDEX_LONGTEXT N_( \
31 "DeckLink capture card to use, if multiple exist. " \
32 "The cards are numbered from 0." )
34 #define MODE_TEXT N_("Desired input video mode")
35 #define MODE_LONGTEXT N_( \
36 "Desired input video mode for DeckLink captures. " \
37 "This value should be a FOURCC code in textual " \
38 "form, e.g. \"ntsc\"." )
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42 "Caching value for DeckLink captures. This " \
43 "value should be set in milliseconds." )
45 #define AUDIO_CONNECTION_TEXT N_("Audio connection")
46 #define AUDIO_CONNECTION_LONGTEXT N_( \
47 "Audio connection to use for DeckLink captures. " \
48 "Valid choices: embedded, aesebu, analog. " \
49 "Leave blank for card default." )
51 #define RATE_TEXT N_("Audio sampling rate in Hz")
52 #define RATE_LONGTEXT N_( \
53 "Audio sampling rate (in hertz) for DeckLink captures. " \
54 "0 disables audio input." )
56 #define CHANNELS_TEXT N_("Number of audio channels")
57 #define CHANNELS_LONGTEXT N_( \
58 "Number of input audio channels for DeckLink captures. " \
59 "Must be 2, 8 or 16. 0 disables audio input." )
61 #define VIDEO_CONNECTION_TEXT N_("Video connection")
62 #define VIDEO_CONNECTION_LONGTEXT N_( \
63 "Video connection to use for DeckLink captures. " \
64 "Valid choices: sdi, hdmi, opticalsdi, component, " \
65 "composite, svideo. " \
66 "Leave blank for card default." )
68 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
69 #define ASPECT_RATIO_LONGTEXT N_( \
70 "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
73 set_shortname( N_("DeckLink") )
74 set_description( N_("Blackmagic DeckLink SDI input") )
75 set_category( CAT_INPUT )
76 set_subcategory( SUBCAT_INPUT_ACCESS )
78 add_integer( "decklink-card-index", 0, NULL,
79 CARD_INDEX_TEXT, CARD_INDEX_LONGTEXT, true )
80 add_string( "decklink-mode", "pal ", NULL,
81 MODE_TEXT, MODE_LONGTEXT, true )
82 add_integer( "decklink-caching", DEFAULT_PTS_DELAY / 1000, NULL,
83 CACHING_TEXT, CACHING_LONGTEXT, true )
84 add_string( "decklink-audio-connection", 0, NULL,
85 AUDIO_CONNECTION_TEXT, AUDIO_CONNECTION_LONGTEXT, true )
86 add_integer( "decklink-audio-rate", 48000, NULL,
87 RATE_TEXT, RATE_LONGTEXT, true )
88 add_integer( "decklink-audio-channels", 2, NULL,
89 CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
90 add_string( "decklink-video-connection", 0, NULL,
91 VIDEO_CONNECTION_TEXT, VIDEO_CONNECTION_LONGTEXT, true )
92 add_string( "decklink-aspect-ratio", NULL, NULL,
93 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
95 add_shortcut( "decklink" )
96 set_capability( "access_demux", 10 )
97 set_callbacks( Open, Close )
100 static int Demux ( demux_t * );
101 static int Control( demux_t *, int, va_list );
103 class DeckLinkCaptureDelegate;
108 IDeckLinkInput *p_input;
109 DeckLinkCaptureDelegate *p_delegate;
111 es_out_id_t *p_video_es;
112 es_out_id_t *p_audio_es;
116 int i_width, i_height, i_fps_num, i_fps_den;
117 uint32_t i_dominance_flags;
119 int i_rate, i_channels;
121 vlc_mutex_t frame_lock;
122 block_t *p_video_frame; /* protected by <frame_lock> */
123 block_t *p_audio_frame; /* protected by <frame_lock> */
124 vlc_cond_t has_frame; /* related to <frame_lock> */
127 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
130 DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux)
132 vlc_atomic_set( &m_ref_, 1 );
135 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
137 virtual ULONG STDMETHODCALLTYPE AddRef(void)
139 return vlc_atomic_inc( &m_ref_ );
142 virtual ULONG STDMETHODCALLTYPE Release(void)
144 uintptr_t new_ref = vlc_atomic_dec( &m_ref_ );
150 virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
151 virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
158 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
160 msg_Dbg( p_demux_, "Video input format changed" );
164 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
166 demux_sys_t *p_sys = p_demux_->p_sys;
167 block_t *p_video_frame = NULL;
168 block_t *p_audio_frame = NULL;
172 if( videoFrame->GetFlags() & bmdFrameHasNoInputSource )
174 msg_Warn( p_demux_, "No input signal detected" );
178 const int i_width = videoFrame->GetWidth();
179 const int i_height = videoFrame->GetHeight();
180 const int i_stride = videoFrame->GetRowBytes();
183 p_video_frame = block_New( p_demux_, i_width * i_height * i_bpp );
186 msg_Err( p_demux_, "Could not allocate memory for video frame" );
191 videoFrame->GetBytes( &frame_bytes );
192 for( int y = 0; y < i_height; ++y )
194 const uint8_t *src = (const uint8_t *)frame_bytes + i_stride * y;
195 uint8_t *dst = (uint8_t *)p_video_frame->p_buffer + i_width * i_bpp * y;
196 memcpy( dst, src, i_width * i_bpp );
199 BMDTimeValue stream_time, frame_duration;
200 videoFrame->GetStreamTime( &stream_time, &frame_duration, CLOCK_FREQ );
201 p_video_frame->i_flags = BLOCK_FLAG_TYPE_I | p_sys->i_dominance_flags;
202 if( p_sys->b_first_frame )
204 p_video_frame->i_flags |= BLOCK_FLAG_DISCONTINUITY;
205 p_sys->b_first_frame = false;
207 p_video_frame->i_pts = p_video_frame->i_dts = VLC_TS_0 + stream_time;
212 const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * p_sys->i_channels;
214 p_audio_frame = block_New( p_demux_, i_bytes );
217 msg_Err( p_demux_, "Could not allocate memory for audio frame" );
219 block_Release( p_video_frame );
224 audioFrame->GetBytes( &frame_bytes );
225 memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
227 BMDTimeValue packet_time;
228 audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
229 p_audio_frame->i_pts = p_audio_frame->i_dts = VLC_TS_0 + packet_time;
232 if( p_video_frame || p_audio_frame )
234 vlc_mutex_lock( &p_sys->frame_lock );
237 if( p_sys->p_video_frame )
238 block_Release( p_sys->p_video_frame );
239 p_sys->p_video_frame = p_video_frame;
243 if( p_sys->p_audio_frame )
244 block_Release( p_sys->p_audio_frame );
245 p_sys->p_audio_frame = p_audio_frame;
247 vlc_cond_signal( &p_sys->has_frame );
248 vlc_mutex_unlock( &p_sys->frame_lock );
254 static int Open( vlc_object_t *p_this )
256 demux_t *p_demux = (demux_t*)p_this;
258 int ret = VLC_SUCCESS;
260 char *psz_display_mode = NULL;
261 char *psz_video_connection = NULL;
262 char *psz_audio_connection = NULL;
266 /* Only when selected */
267 if( *p_demux->psz_access == '\0' )
271 p_demux->pf_demux = Demux;
272 p_demux->pf_control = Control;
273 p_demux->info.i_update = 0;
274 p_demux->info.i_title = 0;
275 p_demux->info.i_seekpoint = 0;
276 p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
280 vlc_mutex_init( &p_sys->frame_lock );
281 vlc_cond_init( &p_sys->has_frame );
282 p_sys->b_first_frame = true;
284 IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
285 if( !decklink_iterator )
287 msg_Err( p_demux, "DeckLink drivers not found." );
294 i_card_index = var_InheritInteger( p_demux, "decklink-card-index" );
295 for( int i = 0; i <= i_card_index; ++i )
298 p_sys->p_card->Release();
299 result = decklink_iterator->Next( &p_sys->p_card );
306 msg_Err( p_demux, "DeckLink PCI card %d not found", i_card_index );
311 const char *psz_model_name;
312 result = p_sys->p_card->GetModelName( &psz_model_name );
316 msg_Err( p_demux, "Could not get model name" );
321 msg_Dbg( p_demux, "Opened DeckLink PCI card %d (%s)", i_card_index, psz_model_name );
323 if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
325 msg_Err( p_demux, "Card has no inputs" );
330 /* Set up the video and audio sources. */
331 IDeckLinkConfiguration *p_config;
332 if( p_sys->p_card->QueryInterface( IID_IDeckLinkConfiguration, (void**)&p_config) != S_OK )
334 msg_Err( p_demux, "Failed to get configuration interface" );
339 psz_video_connection = var_CreateGetNonEmptyString( p_demux, "decklink-video-connection" );
340 if( psz_video_connection )
342 BMDVideoConnection conn;
343 if ( !strcmp( psz_video_connection, "sdi" ) )
344 conn = bmdVideoConnectionSDI;
345 else if ( !strcmp( psz_video_connection, "hdmi" ) )
346 conn = bmdVideoConnectionHDMI;
347 else if ( !strcmp( psz_video_connection, "opticalsdi" ) )
348 conn = bmdVideoConnectionOpticalSDI;
349 else if ( !strcmp( psz_video_connection, "component" ) )
350 conn = bmdVideoConnectionComponent;
351 else if ( !strcmp( psz_video_connection, "composite" ) )
352 conn = bmdVideoConnectionComposite;
353 else if ( !strcmp( psz_video_connection, "svideo" ) )
354 conn = bmdVideoConnectionSVideo;
357 msg_Err( p_demux, "Invalid --decklink-video-connection specified; choose one of " \
358 "sdi, hdmi, opticalsdi, component, composite, or svideo." );
363 msg_Dbg( p_demux, "Setting video input format to 0x%x", conn);
364 result = p_config->SetVideoInputFormat( conn );
367 msg_Err( p_demux, "Failed to set video input connection" );
373 psz_audio_connection = var_CreateGetNonEmptyString( p_demux, "decklink-audio-connection" );
374 if( psz_audio_connection )
376 BMDAudioConnection conn;
377 if ( !strcmp( psz_audio_connection, "embedded" ) )
378 conn = bmdAudioConnectionEmbedded;
379 else if ( !strcmp( psz_audio_connection, "aesebu" ) )
380 conn = bmdAudioConnectionAESEBU;
381 else if ( !strcmp( psz_audio_connection, "analog" ) )
382 conn = bmdAudioConnectionAnalog;
385 msg_Err( p_demux, "Invalid --decklink-audio-connection specified; choose one of " \
386 "embedded, aesebu, or analog." );
391 msg_Dbg( p_demux, "Setting audio input format to 0x%x", conn);
392 result = p_config->SetAudioInputFormat( conn );
395 msg_Err( p_demux, "Failed to set audio input connection" );
401 /* Get the list of display modes. */
402 IDeckLinkDisplayModeIterator *p_display_iterator;
403 result = p_sys->p_input->GetDisplayModeIterator( &p_display_iterator );
406 msg_Err( p_demux, "Failed to enumerate display modes" );
411 psz_display_mode = var_InheritString( p_demux, "decklink-mode" );
412 if( !psz_display_mode || strlen( psz_display_mode ) == 0 || strlen( psz_display_mode ) > 4 ) {
413 msg_Err( p_demux, "Missing or invalid --decklink-mode string" );
419 * Pad the --decklink-mode string to four characters, so the user can specify e.g. "pal"
420 * without having to add the trailing space.
422 char sz_display_mode_padded[5];
423 strcpy(sz_display_mode_padded, " ");
424 for( int i = 0; i < strlen( psz_display_mode ); ++i )
425 sz_display_mode_padded[i] = psz_display_mode[i];
427 BMDDisplayMode wanted_mode_id;
428 memcpy( &wanted_mode_id, &sz_display_mode_padded, sizeof(wanted_mode_id) );
430 b_found_mode = false;
434 IDeckLinkDisplayMode *p_display_mode;
435 result = p_display_iterator->Next( &p_display_mode );
436 if( result != S_OK || !p_display_mode )
439 char sz_mode_id_text[5] = {0};
440 BMDDisplayMode mode_id = ntohl( p_display_mode->GetDisplayMode() );
441 memcpy( sz_mode_id_text, &mode_id, sizeof(mode_id) );
443 const char *psz_mode_name;
444 result = p_display_mode->GetName( &psz_mode_name );
447 msg_Err( p_demux, "Failed to get display mode name" );
448 p_display_mode->Release();
453 BMDTimeValue frame_duration, time_scale;
454 result = p_display_mode->GetFrameRate( &frame_duration, &time_scale );
457 msg_Err( p_demux, "Failed to get frame rate" );
458 p_display_mode->Release();
463 const char *psz_field_dominance;
464 uint32_t i_dominance_flags = 0;
465 switch( p_display_mode->GetFieldDominance() )
467 case bmdProgressiveFrame:
468 psz_field_dominance = "";
470 case bmdProgressiveSegmentedFrame:
471 psz_field_dominance = ", segmented";
473 case bmdLowerFieldFirst:
474 psz_field_dominance = ", interlaced [BFF]";
475 i_dominance_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
477 case bmdUpperFieldFirst:
478 psz_field_dominance = ", interlaced [TFF]";
479 i_dominance_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
481 case bmdUnknownFieldDominance:
483 psz_field_dominance = ", unknown field dominance";
487 msg_Dbg( p_demux, "Found mode '%s': %s (%dx%d, %.3f fps%s)",
488 sz_mode_id_text, psz_mode_name,
489 p_display_mode->GetWidth(), p_display_mode->GetHeight(),
490 double(time_scale) / frame_duration, psz_field_dominance );
492 if( wanted_mode_id == mode_id )
495 p_sys->i_width = p_display_mode->GetWidth();
496 p_sys->i_height = p_display_mode->GetHeight();
497 p_sys->i_fps_num = time_scale;
498 p_sys->i_fps_den = frame_duration;
499 p_sys->i_dominance_flags = i_dominance_flags;
502 p_display_mode->Release();
507 msg_Err( p_demux, "Unknown video mode specified. " \
508 "Run VLC with -v --verbose-objects=-all,+decklink " \
509 "to get a list of supported modes." );
514 result = p_sys->p_input->EnableVideoInput( htonl( wanted_mode_id ), bmdFormat8BitYUV, 0 );
517 msg_Err( p_demux, "Failed to enable video input" );
523 p_sys->i_rate = var_InheritInteger( p_demux, "decklink-audio-rate" );
524 p_sys->i_channels = var_InheritInteger( p_demux, "decklink-audio-channels" );
525 if( p_sys->i_rate > 0 && p_sys->i_channels > 0 )
527 result = p_sys->p_input->EnableAudioInput( p_sys->i_rate, bmdAudioSampleType16bitInteger, p_sys->i_channels );
530 msg_Err( p_demux, "Failed to enable audio input" );
536 p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
537 p_sys->p_input->SetCallback( p_sys->p_delegate );
539 result = p_sys->p_input->StartStreams();
542 msg_Err( p_demux, "Could not start streaming from SDI card. This could be caused "
543 "by invalid video mode or flags, access denied, or card already in use." );
548 /* Declare elementary streams */
549 es_format_t video_fmt;
550 es_format_Init( &video_fmt, VIDEO_ES, VLC_CODEC_UYVY );
551 video_fmt.video.i_width = p_sys->i_width;
552 video_fmt.video.i_height = p_sys->i_height;
553 video_fmt.video.i_sar_num = 1;
554 video_fmt.video.i_sar_den = 1;
555 video_fmt.video.i_frame_rate = p_sys->i_fps_num;
556 video_fmt.video.i_frame_rate_base = p_sys->i_fps_den;
557 video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2 * 8;
559 psz_aspect = var_CreateGetNonEmptyString( p_demux, "decklink-aspect-ratio" );
562 char *psz_denominator = strchr( psz_aspect, ':' );
563 if( psz_denominator )
565 *psz_denominator++ = '\0';
566 video_fmt.video.i_sar_num = atoi( psz_aspect ) * video_fmt.video.i_height;
567 video_fmt.video.i_sar_den = atoi( psz_denominator ) * video_fmt.video.i_width;
572 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
573 (char*)&video_fmt.i_codec, video_fmt.video.i_width, video_fmt.video.i_height );
574 p_sys->p_video_es = es_out_Add( p_demux->out, &video_fmt );
576 es_format_t audio_fmt;
577 es_format_Init( &audio_fmt, AUDIO_ES, VLC_CODEC_S16N );
578 audio_fmt.audio.i_channels = p_sys->i_channels;
579 audio_fmt.audio.i_rate = p_sys->i_rate;
580 audio_fmt.audio.i_bitspersample = 16;
581 audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
582 audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
584 msg_Dbg( p_demux, "added new audio es %4.4s %dHz %dbpp %dch",
585 (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
586 p_sys->p_audio_es = es_out_Add( p_demux->out, &audio_fmt );
588 /* Update default_pts to a suitable value for access */
589 var_Create( p_demux, "decklink-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
592 if( decklink_iterator )
593 decklink_iterator->Release();
598 free( psz_video_connection );
599 free( psz_audio_connection );
600 free( psz_display_mode );
602 if( p_display_iterator )
603 p_display_iterator->Release();
605 if( ret != VLC_SUCCESS )
611 static void Close( vlc_object_t *p_this )
613 demux_t *p_demux = (demux_t *)p_this;
614 demux_sys_t *p_sys = p_demux->p_sys;
618 p_sys->p_input->StopStreams();
619 p_sys->p_input->Release();
623 p_sys->p_card->Release();
625 if( p_sys->p_delegate )
626 p_sys->p_delegate->Release();
628 if( p_sys->p_video_frame )
629 block_Release( p_sys->p_video_frame );
631 if( p_sys->p_audio_frame )
632 block_Release( p_sys->p_audio_frame );
637 static int Control( demux_t *p_demux, int i_query, va_list args )
639 demux_sys_t *p_sys = p_demux->p_sys;
645 /* Special for access_demux */
646 case DEMUX_CAN_PAUSE:
648 case DEMUX_CAN_CONTROL_PACE:
649 pb = (bool*)va_arg( args, bool * );
653 case DEMUX_GET_PTS_DELAY:
654 pi64 = (int64_t*)va_arg( args, int64_t * );
655 *pi64 = var_GetInteger( p_demux, "decklink-caching" ) * 1000;
659 pi64 = (int64_t*)va_arg( args, int64_t * );
660 *pi64 = p_sys->i_last_pts;
663 /* TODO implement others */
671 static int Demux( demux_t *p_demux )
673 demux_sys_t *p_sys = p_demux->p_sys;
674 block_t *p_video_block = NULL;
675 block_t *p_audio_block = NULL;
677 vlc_mutex_lock( &p_sys->frame_lock );
679 while( !p_sys->p_video_frame && !p_sys->p_audio_frame )
680 vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
682 p_video_block = p_sys->p_video_frame;
683 p_sys->p_video_frame = NULL;
685 p_audio_block = p_sys->p_audio_frame;
686 p_sys->p_audio_frame = NULL;
688 vlc_mutex_unlock( &p_sys->frame_lock );
692 if( p_video_block->i_pts > p_sys->i_last_pts )
693 p_sys->i_last_pts = p_video_block->i_pts;
694 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_video_block->i_pts );
695 es_out_Send( p_demux->out, p_sys->p_video_es, p_video_block );
700 if( p_audio_block->i_pts > p_sys->i_last_pts )
701 p_sys->i_last_pts = p_audio_block->i_pts;
702 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_audio_block->i_pts );
703 es_out_Send( p_demux->out, p_sys->p_audio_es, p_audio_block );