]> git.sesse.net Git - vlc/blob - modules/access/sdi.cpp
Add audio input support to the sdi driver.
[vlc] / modules / access / sdi.cpp
1 /* BlackMagic SDI driver */
2
3 #ifdef HAVE_CONFIG_H
4 # include "config.h"
5 #endif
6
7 #ifndef INT64_C
8 #define INT64_C(c) c ## LL
9 #endif
10
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>
18 #include <vlc_fs.h>
19
20 #include "DeckLinkAPI.h"
21 #include "DeckLinkAPIDispatch.cpp"
22
23 static int  Open ( vlc_object_t * );
24 static void Close( vlc_object_t * );
25
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." )
30
31 vlc_module_begin ()
32     set_shortname( N_("SDI") )
33     set_description( N_("BlackMagic SDI input") )
34     set_category( CAT_INPUT )
35     set_subcategory( SUBCAT_INPUT_ACCESS )
36
37     add_integer( "sdi-caching", DEFAULT_PTS_DELAY / 1000, NULL,
38                  CACHING_TEXT, CACHING_LONGTEXT, true )
39
40     add_shortcut( "sdi" )
41     set_capability( "access_demux", 10 )
42     set_callbacks( Open, Close )
43 vlc_module_end ()
44
45 static int Demux  ( demux_t * );
46 static int Control( demux_t *, int, va_list );
47
48 class DeckLinkCaptureDelegate;
49
50 struct demux_sys_t
51 {
52     IDeckLink *p_card;
53     IDeckLinkInput *p_input;
54     DeckLinkCaptureDelegate *p_delegate;
55     es_out_id_t *p_video_es;
56     es_out_id_t *p_audio_es;
57
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>
62 };
63
64 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
65 {
66 public:
67     DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
68
69     // FIXME: These leak.
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; }
73
74     virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
75     virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
76
77 private:
78     demux_t *p_demux_;
79 };
80
81 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
82 {
83     msg_Dbg( p_demux_, "Video input format changed" );    
84     return S_OK;
85 }
86
87 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
88 {
89     demux_sys_t *p_sys = p_demux_->p_sys;
90     block_t *p_video_frame = NULL;
91     block_t *p_audio_frame = NULL;
92
93     if( videoFrame )
94     {
95         if( videoFrame->GetFlags() & bmdFrameHasNoInputSource )
96         {
97             msg_Warn( p_demux_, "No input signal detected" );
98             return S_OK;
99         }
100
101         const int i_width = videoFrame->GetWidth();
102         const int i_height = videoFrame->GetHeight();
103         const int i_stride = videoFrame->GetRowBytes();
104         const int i_bpp = 2;
105
106         p_video_frame = block_New( p_demux_, i_width * i_height * i_bpp );
107         if( !p_video_frame )
108         {
109             msg_Err( p_demux_, "Could not allocate memory for video frame" );
110             return S_OK;
111         }
112
113         void *frame_bytes;
114         videoFrame->GetBytes( &frame_bytes );
115         for( int y = 0; y < i_height; ++y )
116         {
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 );
120         }
121
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;
125     }
126     
127     if( audioFrame )
128     {
129         const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * 2;
130
131         p_audio_frame = block_New( p_demux_, i_bytes );
132         if( !p_audio_frame )
133         {
134             msg_Err( p_demux_, "Could not allocate memory for audio frame" );
135             return S_OK;
136         }
137
138         void *frame_bytes;
139         audioFrame->GetBytes( &frame_bytes );
140         memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
141
142         BMDTimeValue packet_time;
143         audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
144         p_audio_frame->i_pts = VLC_TS_0 + packet_time;
145     }
146
147     if( p_video_frame || p_audio_frame )
148     {
149         vlc_mutex_lock( &p_sys->frame_lock );
150         if( p_video_frame )
151             p_sys->p_video_frame = p_video_frame;  // FIXME: leak
152         if( p_audio_frame )
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 );
156     }
157
158     return S_OK;
159 }
160
161 static int Open( vlc_object_t *p_this )
162 {
163     demux_t     *p_demux = (demux_t*)p_this;
164     demux_sys_t *p_sys;
165
166     /* Only when selected */
167     if( *p_demux->psz_access == '\0' )
168         return VLC_EGENERIC;
169
170     /* Set up p_demux */
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 ) );
177     if( !p_sys )
178         return VLC_ENOMEM;
179
180     vlc_mutex_init( &p_sys->frame_lock );
181     vlc_cond_init( &p_sys->has_frame );
182     p_sys->p_video_frame = NULL;
183
184     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
185     if( !decklink_iterator )
186     {
187         msg_Err( p_demux, "DeckLink drivers not found." );
188         // FIXME: Leak here and several other error paths.
189         return VLC_EGENERIC;
190     }
191
192     HRESULT result;
193     result = decklink_iterator->Next( &p_sys->p_card );
194
195     if( result != S_OK )
196     {
197         msg_Err( p_demux, "No DeckLink PCI cards found" );
198         return VLC_EGENERIC;
199     }
200
201     if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
202     {
203         msg_Err( p_demux, "Card has no inputs" );
204         return VLC_EGENERIC;
205     }
206
207     result = p_sys->p_input->EnableVideoInput( bmdModePAL, bmdFormat8BitYUV, 0 );
208     if( result != S_OK )
209     {
210         msg_Err( p_demux, "Failed to enable video input" );
211         return VLC_EGENERIC;
212     }
213     
214     result = p_sys->p_input->EnableAudioInput( 48000, bmdAudioSampleType16bitInteger, 2 );
215     if( result != S_OK )
216     {
217         msg_Err( p_demux, "Failed to enable audio input" );
218         return VLC_EGENERIC;
219     }
220     
221     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
222     p_sys->p_input->SetCallback( p_sys->p_delegate );
223
224     result = p_sys->p_input->StartStreams();
225     if( result != S_OK )
226     {
227         msg_Err( p_demux, "Failed to start streams" );
228         return VLC_EGENERIC;
229     }
230
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;
241
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 );
245     
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;
253
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 );
257
258     /* Update default_pts to a suitable value for access */
259     var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
260
261     return VLC_SUCCESS;
262 }
263
264 static void Close( vlc_object_t *p_this )
265 {
266     demux_t     *p_demux = (demux_t *)p_this;
267     demux_sys_t *p_sys   = p_demux->p_sys;
268
269     free( p_sys );
270 }
271
272 static int Control( demux_t *p_demux, int i_query, va_list args )
273 {
274     bool *pb;
275     int64_t    *pi64;
276
277     switch( i_query )
278     {
279         /* Special for access_demux */
280         case DEMUX_CAN_PAUSE:
281         case DEMUX_CAN_SEEK:
282         case DEMUX_CAN_CONTROL_PACE:
283             pb = (bool*)va_arg( args, bool * );
284             *pb = false;
285             return VLC_SUCCESS;
286
287         case DEMUX_GET_PTS_DELAY:
288             pi64 = (int64_t*)va_arg( args, int64_t * );
289             *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
290             return VLC_SUCCESS;
291
292         case DEMUX_GET_TIME:
293             pi64 = (int64_t*)va_arg( args, int64_t * );
294             *pi64 = mdate();  // FIXME
295             return VLC_SUCCESS;
296
297         /* TODO implement others */
298         default:
299             return VLC_EGENERIC;
300     }
301
302     return VLC_EGENERIC;
303 }
304
305 static int Demux( demux_t *p_demux )
306 {
307     demux_sys_t *p_sys = p_demux->p_sys;
308     block_t *p_video_block = NULL;
309     block_t *p_audio_block = NULL;
310
311     vlc_mutex_lock( &p_sys->frame_lock );
312
313     while( !p_sys->p_video_frame && !p_sys->p_audio_frame )
314         vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
315
316     p_video_block = p_sys->p_video_frame;
317     p_sys->p_video_frame = NULL;
318
319     p_audio_block = p_sys->p_audio_frame;
320     p_sys->p_audio_frame = NULL;
321
322     vlc_mutex_unlock( &p_sys->frame_lock );
323
324     if( p_video_block )
325     {
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 );
328     }
329     
330     if( p_audio_block )
331     {
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 );
334     }
335
336     return 1;
337 }
338