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 "DeckLinkAPI.h"
21 #include "DeckLinkAPIDispatch.cpp"
23 static int Open ( vlc_object_t * );
24 static void Close( vlc_object_t * );
26 #define CACHING_TEXT N_("Caching value in ms")
27 #define CACHING_LONGTEXT N_( \
28 "Caching value for SDI captures. This " \
29 "value should be set in milliseconds." )
32 set_shortname( N_("SDI") )
33 set_description( N_("BlackMagic SDI input") )
34 set_category( CAT_INPUT )
35 set_subcategory( SUBCAT_INPUT_ACCESS )
37 add_integer( "sdi-caching", DEFAULT_PTS_DELAY / 1000, NULL,
38 CACHING_TEXT, CACHING_LONGTEXT, true )
41 set_capability( "access_demux", 10 )
42 set_callbacks( Open, Close )
45 static int Demux ( demux_t * );
46 static int Control( demux_t *, int, va_list );
48 class DeckLinkCaptureDelegate;
53 IDeckLinkInput *p_input;
54 DeckLinkCaptureDelegate *p_delegate;
55 es_out_id_t *p_video_es;
56 es_out_id_t *p_audio_es;
58 vlc_mutex_t frame_lock;
59 block_t *p_video_frame; // protected by <frame_lock>
60 block_t *p_audio_frame; // protected by <frame_lock>
61 vlc_cond_t has_frame; // related to <frame_lock>
64 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
67 DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
70 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
71 virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
72 virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
74 virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
75 virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
81 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
83 msg_Dbg( p_demux_, "Video input format changed" );
87 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
89 demux_sys_t *p_sys = p_demux_->p_sys;
90 block_t *p_video_frame = NULL;
91 block_t *p_audio_frame = NULL;
95 if( videoFrame->GetFlags() & bmdFrameHasNoInputSource )
97 msg_Warn( p_demux_, "No input signal detected" );
101 const int i_width = videoFrame->GetWidth();
102 const int i_height = videoFrame->GetHeight();
103 const int i_stride = videoFrame->GetRowBytes();
106 p_video_frame = block_New( p_demux_, i_width * i_height * i_bpp );
109 msg_Err( p_demux_, "Could not allocate memory for video frame" );
114 videoFrame->GetBytes( &frame_bytes );
115 for( int y = 0; y < i_height; ++y )
117 const uint8_t *src = (const uint8_t *)frame_bytes + i_stride * y;
118 uint8_t *dst = (uint8_t *)p_video_frame->p_buffer + i_width * i_bpp * y;
119 memcpy( dst, src, i_width * i_bpp );
122 BMDTimeValue stream_time, frame_duration;
123 videoFrame->GetStreamTime( &stream_time, &frame_duration, CLOCK_FREQ );
124 p_video_frame->i_pts = VLC_TS_0 + stream_time;
129 const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * 2;
131 p_audio_frame = block_New( p_demux_, i_bytes );
134 msg_Err( p_demux_, "Could not allocate memory for audio frame" );
139 audioFrame->GetBytes( &frame_bytes );
140 memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
142 BMDTimeValue packet_time;
143 audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
144 p_audio_frame->i_pts = VLC_TS_0 + packet_time;
147 if( p_video_frame || p_audio_frame )
149 vlc_mutex_lock( &p_sys->frame_lock );
151 p_sys->p_video_frame = p_video_frame; // FIXME: leak
153 p_sys->p_audio_frame = p_audio_frame; // FIXME: leak
154 vlc_cond_signal( &p_sys->has_frame );
155 vlc_mutex_unlock( &p_sys->frame_lock );
161 static int Open( vlc_object_t *p_this )
163 demux_t *p_demux = (demux_t*)p_this;
166 /* Only when selected */
167 if( *p_demux->psz_access == '\0' )
171 p_demux->pf_demux = Demux;
172 p_demux->pf_control = Control;
173 p_demux->info.i_update = 0;
174 p_demux->info.i_title = 0;
175 p_demux->info.i_seekpoint = 0;
176 p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
180 vlc_mutex_init( &p_sys->frame_lock );
181 vlc_cond_init( &p_sys->has_frame );
182 p_sys->p_video_frame = NULL;
184 IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
185 if( !decklink_iterator )
187 msg_Err( p_demux, "DeckLink drivers not found." );
188 // FIXME: Leak here and several other error paths.
193 result = decklink_iterator->Next( &p_sys->p_card );
197 msg_Err( p_demux, "No DeckLink PCI cards found" );
201 if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
203 msg_Err( p_demux, "Card has no inputs" );
207 result = p_sys->p_input->EnableVideoInput( bmdModePAL, bmdFormat8BitYUV, 0 );
210 msg_Err( p_demux, "Failed to enable video input" );
214 result = p_sys->p_input->EnableAudioInput( 48000, bmdAudioSampleType16bitInteger, 2 );
217 msg_Err( p_demux, "Failed to enable audio input" );
221 p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
222 p_sys->p_input->SetCallback( p_sys->p_delegate );
224 result = p_sys->p_input->StartStreams();
227 msg_Err( p_demux, "Failed to start streams" );
231 /* Declare elementary streams */
232 es_format_t video_fmt;
233 es_format_Init( &video_fmt, VIDEO_ES, VLC_CODEC_UYVY );
234 video_fmt.video.i_width = 720;
235 video_fmt.video.i_height = 576;
236 video_fmt.video.i_sar_num = 16 * video_fmt.video.i_height;
237 video_fmt.video.i_sar_den = 9 * video_fmt.video.i_width;
238 video_fmt.video.i_frame_rate = 25;
239 video_fmt.video.i_frame_rate_base = 1;
240 video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2;
242 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
243 (char*)&video_fmt.i_codec, video_fmt.video.i_width, video_fmt.video.i_height );
244 p_sys->p_video_es = es_out_Add( p_demux->out, &video_fmt );
246 es_format_t audio_fmt;
247 es_format_Init( &audio_fmt, AUDIO_ES, VLC_CODEC_S16N );
248 audio_fmt.audio.i_channels = 2;
249 audio_fmt.audio.i_rate = 48000;
250 audio_fmt.audio.i_bitspersample = 16;
251 audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
252 audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
254 msg_Dbg( p_demux, "added new audio es %4.4s %dHz %dbpp %dch",
255 (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
256 p_sys->p_audio_es = es_out_Add( p_demux->out, &audio_fmt );
258 /* Update default_pts to a suitable value for access */
259 var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
264 static void Close( vlc_object_t *p_this )
266 demux_t *p_demux = (demux_t *)p_this;
267 demux_sys_t *p_sys = p_demux->p_sys;
272 static int Control( demux_t *p_demux, int i_query, va_list args )
279 /* Special for access_demux */
280 case DEMUX_CAN_PAUSE:
282 case DEMUX_CAN_CONTROL_PACE:
283 pb = (bool*)va_arg( args, bool * );
287 case DEMUX_GET_PTS_DELAY:
288 pi64 = (int64_t*)va_arg( args, int64_t * );
289 *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
293 pi64 = (int64_t*)va_arg( args, int64_t * );
294 *pi64 = mdate(); // FIXME
297 /* TODO implement others */
305 static int Demux( demux_t *p_demux )
307 demux_sys_t *p_sys = p_demux->p_sys;
308 block_t *p_video_block = NULL;
309 block_t *p_audio_block = NULL;
311 vlc_mutex_lock( &p_sys->frame_lock );
313 while( !p_sys->p_video_frame && !p_sys->p_audio_frame )
314 vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
316 p_video_block = p_sys->p_video_frame;
317 p_sys->p_video_frame = NULL;
319 p_audio_block = p_sys->p_audio_frame;
320 p_sys->p_audio_frame = NULL;
322 vlc_mutex_unlock( &p_sys->frame_lock );
326 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_video_block->i_pts );
327 es_out_Send( p_demux->out, p_sys->p_video_es, p_video_block );
332 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_audio_block->i_pts );
333 es_out_Send( p_demux->out, p_sys->p_audio_es, p_audio_block );