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;
58 block_t *p_frame; // protected by <lock>
61 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
64 DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
67 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
68 virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
69 virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
71 virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
72 virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
78 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
80 msg_Dbg( p_demux_, "Video input format changed" );
84 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
86 demux_sys_t *p_sys = p_demux_->p_sys;
90 if(videoFrame->GetFlags() & bmdFrameHasNoInputSource)
92 msg_Warn( p_demux_, "No input signal detected" );
96 msg_Dbg( p_demux_, "Received a frame" );
99 p_frame = block_New( p_demux_, 720 * 576 * 3 );
102 msg_Err( p_demux_, "Could not allocate memory for frame" );
107 videoFrame->GetBytes( &frame_bytes );
108 memcpy( p_frame->p_buffer, frame_bytes, 720 * 576 * 3 );
110 BMDTimeValue stream_time, frame_duration;
111 videoFrame->GetStreamTime( &stream_time, &frame_duration, 1000000 );
112 p_frame->i_pts = stream_time;
114 vlc_mutex_lock( &p_sys->lock );
115 p_sys->p_frame = p_frame; // FIXME: leak
116 vlc_mutex_unlock( &p_sys->lock );
122 static int Open( vlc_object_t *p_this )
124 demux_t *p_demux = (demux_t*)p_this;
127 /* Only when selected */
128 if( *p_demux->psz_access == '\0' )
132 p_demux->pf_demux = Demux;
133 p_demux->pf_control = Control;
134 p_demux->info.i_update = 0;
135 p_demux->info.i_title = 0;
136 p_demux->info.i_seekpoint = 0;
137 p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
141 vlc_mutex_init( &p_sys->lock );
142 p_sys->p_frame = NULL;
144 IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
145 if( !decklink_iterator )
147 msg_Err( p_demux, "DeckLink drivers not found." );
148 // FIXME: Leak here and several other error paths.
153 result = decklink_iterator->Next( &p_sys->p_card );
157 msg_Err( p_demux, "No DeckLink PCI cards found" );
161 if( p_sys->p_card->QueryInterface(IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
163 msg_Err( p_demux, "Card has no inputs" );
167 p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
168 p_sys->p_input->SetCallback( p_sys->p_delegate );
170 result = p_sys->p_input->EnableVideoInput(bmdModePAL, bmdFormat8BitYUV, 0);
173 msg_Err( p_demux, "Failed to enable video input" );
178 result = p_sys->p_input->StartStreams();
181 msg_Err( p_demux, "Failed to start streams" );
185 /*eDeclare elementary streams */
187 es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_UYVY );
188 fmt.video.i_width = 720;
189 fmt.video.i_height = 576;
190 fmt.video.i_sar_num = 1;
191 fmt.video.i_sar_den = 1;
193 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
194 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
195 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
197 /* Update default_pts to a suitable value for access */
198 var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
203 static void Close( vlc_object_t *p_this )
205 demux_t *p_demux = (demux_t *)p_this;
206 demux_sys_t *p_sys = p_demux->p_sys;
211 static int Control( demux_t *p_demux, int i_query, va_list args )
218 /* Special for access_demux */
219 case DEMUX_CAN_PAUSE:
221 case DEMUX_CAN_CONTROL_PACE:
222 pb = (bool*)va_arg( args, bool * );
226 case DEMUX_GET_PTS_DELAY:
227 pi64 = (int64_t*)va_arg( args, int64_t * );
228 *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
232 pi64 = (int64_t*)va_arg( args, int64_t * );
233 *pi64 = mdate(); // FIXME
236 /* TODO implement others */
244 static int Demux( demux_t *p_demux )
246 demux_sys_t *p_sys = p_demux->p_sys;
248 vlc_mutex_lock( &p_sys->lock );
249 block_t *p_block = p_sys->p_frame;
250 p_sys->p_frame = NULL;
251 vlc_mutex_unlock( &p_sys->lock );
255 msg_Dbg( p_demux, "Sending frame" );
256 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
257 es_out_Send( p_demux->out, p_sys->p_es, p_block );
261 usleep(10000); // FIXME