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>
20 #include <arpa/inet.h>
22 #include "DeckLinkAPI.h"
23 #include "DeckLinkAPIDispatch.cpp"
25 static int Open ( vlc_object_t * );
26 static void Close( vlc_object_t * );
28 #define CACHING_TEXT N_("Caching value in ms")
29 #define CACHING_LONGTEXT N_( \
30 "Caching value for SDI captures. This " \
31 "value should be set in milliseconds." )
34 set_shortname( N_("SDI") )
35 set_description( N_("BlackMagic SDI input") )
36 set_category( CAT_INPUT )
37 set_subcategory( SUBCAT_INPUT_ACCESS )
39 add_integer( "sdi-caching", DEFAULT_PTS_DELAY / 1000, NULL,
40 CACHING_TEXT, CACHING_LONGTEXT, true )
43 set_capability( "access_demux", 10 )
44 set_callbacks( Open, Close )
47 static int Demux ( demux_t * );
48 static int Control( demux_t *, int, va_list );
50 class DeckLinkCaptureDelegate;
55 IDeckLinkInput *p_input;
56 DeckLinkCaptureDelegate *p_delegate;
57 es_out_id_t *p_video_es;
58 es_out_id_t *p_audio_es;
61 vlc_mutex_t frame_lock;
62 block_t *p_video_frame; // protected by <frame_lock>
63 block_t *p_audio_frame; // protected by <frame_lock>
64 vlc_cond_t has_frame; // related to <frame_lock>
67 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
70 DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
73 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
74 virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
75 virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
77 virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
78 virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
84 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
86 msg_Dbg( p_demux_, "Video input format changed" );
90 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
92 demux_sys_t *p_sys = p_demux_->p_sys;
93 block_t *p_video_frame = NULL;
94 block_t *p_audio_frame = NULL;
98 if( videoFrame->GetFlags() & bmdFrameHasNoInputSource )
100 msg_Warn( p_demux_, "No input signal detected" );
104 const int i_width = videoFrame->GetWidth();
105 const int i_height = videoFrame->GetHeight();
106 const int i_stride = videoFrame->GetRowBytes();
109 p_video_frame = block_New( p_demux_, i_width * i_height * i_bpp );
112 msg_Err( p_demux_, "Could not allocate memory for video frame" );
117 videoFrame->GetBytes( &frame_bytes );
118 for( int y = 0; y < i_height; ++y )
120 const uint8_t *src = (const uint8_t *)frame_bytes + i_stride * y;
121 uint8_t *dst = (uint8_t *)p_video_frame->p_buffer + i_width * i_bpp * y;
122 memcpy( dst, src, i_width * i_bpp );
125 BMDTimeValue stream_time, frame_duration;
126 videoFrame->GetStreamTime( &stream_time, &frame_duration, CLOCK_FREQ );
127 p_video_frame->i_flags = BLOCK_FLAG_TYPE_I;
128 if( p_sys->b_first_frame )
130 p_video_frame->i_flags |= BLOCK_FLAG_DISCONTINUITY;
131 p_sys->b_first_frame = false;
133 p_video_frame->i_pts = VLC_TS_0 + stream_time;
138 const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * 2;
140 p_audio_frame = block_New( p_demux_, i_bytes );
143 msg_Err( p_demux_, "Could not allocate memory for audio frame" );
148 audioFrame->GetBytes( &frame_bytes );
149 memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
151 BMDTimeValue packet_time;
152 audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
153 p_audio_frame->i_pts = VLC_TS_0 + packet_time;
156 if( p_video_frame || p_audio_frame )
158 vlc_mutex_lock( &p_sys->frame_lock );
160 p_sys->p_video_frame = p_video_frame; // FIXME: leak
162 p_sys->p_audio_frame = p_audio_frame; // FIXME: leak
163 vlc_cond_signal( &p_sys->has_frame );
164 vlc_mutex_unlock( &p_sys->frame_lock );
170 static int Open( vlc_object_t *p_this )
172 demux_t *p_demux = (demux_t*)p_this;
175 /* Only when selected */
176 if( *p_demux->psz_access == '\0' )
180 p_demux->pf_demux = Demux;
181 p_demux->pf_control = Control;
182 p_demux->info.i_update = 0;
183 p_demux->info.i_title = 0;
184 p_demux->info.i_seekpoint = 0;
185 p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
189 vlc_mutex_init( &p_sys->frame_lock );
190 vlc_cond_init( &p_sys->has_frame );
191 p_sys->p_video_frame = NULL;
193 IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
194 if( !decklink_iterator )
196 msg_Err( p_demux, "DeckLink drivers not found." );
197 // FIXME: Leak here and several other error paths.
202 result = decklink_iterator->Next( &p_sys->p_card );
206 msg_Err( p_demux, "No DeckLink PCI cards found" );
210 if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
212 msg_Err( p_demux, "Card has no inputs" );
216 result = p_sys->p_input->EnableVideoInput( bmdModePAL, bmdFormat8BitYUV, 0 );
219 msg_Err( p_demux, "Failed to enable video input" );
223 IDeckLinkDisplayModeIterator *p_display_iterator;
224 result = p_sys->p_input->GetDisplayModeIterator( &p_display_iterator );
227 msg_Err( p_demux, "Failed to enumerate display modes" );
233 IDeckLinkDisplayMode *p_display_mode;
234 result = p_display_iterator->Next( &p_display_mode );
235 if( result != S_OK || !p_display_mode )
240 char mode_id_text[5] = {0};
241 BMDDisplayMode mode_id = ntohl( p_display_mode->GetDisplayMode() );
242 memcpy( mode_id_text, &mode_id, sizeof(mode_id) );
244 const char *mode_name;
245 result = p_display_mode->GetName( &mode_name );
248 msg_Err( p_demux, "Failed to get display mode name" );
252 BMDTimeValue frame_duration, time_scale;
253 result = p_display_mode->GetFrameRate( &frame_duration, &time_scale );
256 msg_Err( p_demux, "Failed to get frame rate" );
260 const char *field_dominance;
261 switch( p_display_mode->GetFieldDominance() )
263 case bmdProgressiveFrame:
264 field_dominance = "";
266 case bmdProgressiveSegmentedFrame:
267 field_dominance = ", segmented";
269 case bmdLowerFieldFirst:
270 field_dominance = ", interlaced [BFF]";
272 case bmdUpperFieldFirst:
273 field_dominance = ", interlaced [TFF]";
275 case bmdUnknownFieldDominance:
277 field_dominance = ", unknown field dominance";
282 sprintf( buf, "Found mode '%s': %s (%dx%d, %.3f fps%s)",
283 mode_id_text, mode_name,
284 p_display_mode->GetWidth(), p_display_mode->GetHeight(),
285 double(time_scale) / frame_duration, field_dominance );
286 msg_Dbg( p_demux, buf );
289 result = p_sys->p_input->EnableAudioInput( 48000, bmdAudioSampleType16bitInteger, 2 );
292 msg_Err( p_demux, "Failed to enable audio input" );
296 p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
297 p_sys->p_input->SetCallback( p_sys->p_delegate );
299 result = p_sys->p_input->StartStreams();
302 msg_Err( p_demux, "Failed to start streams" );
306 /* Declare elementary streams */
307 es_format_t video_fmt;
308 es_format_Init( &video_fmt, VIDEO_ES, VLC_CODEC_UYVY );
309 video_fmt.video.i_width = 720;
310 video_fmt.video.i_height = 576;
311 video_fmt.video.i_sar_num = 16 * video_fmt.video.i_height;
312 video_fmt.video.i_sar_den = 9 * video_fmt.video.i_width;
313 video_fmt.video.i_frame_rate = 25;
314 video_fmt.video.i_frame_rate_base = 1;
315 video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2;
317 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
318 (char*)&video_fmt.i_codec, video_fmt.video.i_width, video_fmt.video.i_height );
319 p_sys->p_video_es = es_out_Add( p_demux->out, &video_fmt );
321 es_format_t audio_fmt;
322 es_format_Init( &audio_fmt, AUDIO_ES, VLC_CODEC_S16N );
323 audio_fmt.audio.i_channels = 2;
324 audio_fmt.audio.i_rate = 48000;
325 audio_fmt.audio.i_bitspersample = 16;
326 audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
327 audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
329 msg_Dbg( p_demux, "added new audio es %4.4s %dHz %dbpp %dch",
330 (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
331 p_sys->p_audio_es = es_out_Add( p_demux->out, &audio_fmt );
333 p_sys->b_first_frame = true;
335 /* Update default_pts to a suitable value for access */
336 var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
341 static void Close( vlc_object_t *p_this )
343 demux_t *p_demux = (demux_t *)p_this;
344 demux_sys_t *p_sys = p_demux->p_sys;
349 static int Control( demux_t *p_demux, int i_query, va_list args )
356 /* Special for access_demux */
357 case DEMUX_CAN_PAUSE:
359 case DEMUX_CAN_CONTROL_PACE:
360 pb = (bool*)va_arg( args, bool * );
364 case DEMUX_GET_PTS_DELAY:
365 pi64 = (int64_t*)va_arg( args, int64_t * );
366 *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
370 pi64 = (int64_t*)va_arg( args, int64_t * );
371 *pi64 = mdate(); // FIXME
374 /* TODO implement others */
382 static int Demux( demux_t *p_demux )
384 demux_sys_t *p_sys = p_demux->p_sys;
385 block_t *p_video_block = NULL;
386 block_t *p_audio_block = NULL;
388 vlc_mutex_lock( &p_sys->frame_lock );
390 while( !p_sys->p_video_frame && !p_sys->p_audio_frame )
391 vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
393 p_video_block = p_sys->p_video_frame;
394 p_sys->p_video_frame = NULL;
396 p_audio_block = p_sys->p_audio_frame;
397 p_sys->p_audio_frame = NULL;
399 vlc_mutex_unlock( &p_sys->frame_lock );
403 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_video_block->i_pts );
404 es_out_Send( p_demux->out, p_sys->p_video_es, p_video_block );
409 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_audio_block->i_pts );
410 es_out_Send( p_demux->out, p_sys->p_audio_es, p_audio_block );