]> git.sesse.net Git - vlc/blob - modules/access/sdi.cpp
Enumerate all modes from the card, and print them out.
[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 <arpa/inet.h>
21
22 #include "DeckLinkAPI.h"
23 #include "DeckLinkAPIDispatch.cpp"
24
25 static int  Open ( vlc_object_t * );
26 static void Close( vlc_object_t * );
27
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." )
32
33 vlc_module_begin ()
34     set_shortname( N_("SDI") )
35     set_description( N_("BlackMagic SDI input") )
36     set_category( CAT_INPUT )
37     set_subcategory( SUBCAT_INPUT_ACCESS )
38
39     add_integer( "sdi-caching", DEFAULT_PTS_DELAY / 1000, NULL,
40                  CACHING_TEXT, CACHING_LONGTEXT, true )
41
42     add_shortcut( "sdi" )
43     set_capability( "access_demux", 10 )
44     set_callbacks( Open, Close )
45 vlc_module_end ()
46
47 static int Demux  ( demux_t * );
48 static int Control( demux_t *, int, va_list );
49
50 class DeckLinkCaptureDelegate;
51
52 struct demux_sys_t
53 {
54     IDeckLink *p_card;
55     IDeckLinkInput *p_input;
56     DeckLinkCaptureDelegate *p_delegate;
57     es_out_id_t *p_video_es;
58     es_out_id_t *p_audio_es;
59     bool b_first_frame;
60
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>
65 };
66
67 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
68 {
69 public:
70     DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
71
72     // FIXME: These leak.
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; }
76
77     virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
78     virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
79
80 private:
81     demux_t *p_demux_;
82 };
83
84 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
85 {
86     msg_Dbg( p_demux_, "Video input format changed" );    
87     return S_OK;
88 }
89
90 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
91 {
92     demux_sys_t *p_sys = p_demux_->p_sys;
93     block_t *p_video_frame = NULL;
94     block_t *p_audio_frame = NULL;
95
96     if( videoFrame )
97     {
98         if( videoFrame->GetFlags() & bmdFrameHasNoInputSource )
99         {
100             msg_Warn( p_demux_, "No input signal detected" );
101             return S_OK;
102         }
103
104         const int i_width = videoFrame->GetWidth();
105         const int i_height = videoFrame->GetHeight();
106         const int i_stride = videoFrame->GetRowBytes();
107         const int i_bpp = 2;
108
109         p_video_frame = block_New( p_demux_, i_width * i_height * i_bpp );
110         if( !p_video_frame )
111         {
112             msg_Err( p_demux_, "Could not allocate memory for video frame" );
113             return S_OK;
114         }
115
116         void *frame_bytes;
117         videoFrame->GetBytes( &frame_bytes );
118         for( int y = 0; y < i_height; ++y )
119         {
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 );
123         }
124
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 )
129         {
130             p_video_frame->i_flags |= BLOCK_FLAG_DISCONTINUITY;
131             p_sys->b_first_frame = false;
132         }
133         p_video_frame->i_pts = VLC_TS_0 + stream_time;
134     }
135     
136     if( audioFrame )
137     {
138         const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * 2;
139
140         p_audio_frame = block_New( p_demux_, i_bytes );
141         if( !p_audio_frame )
142         {
143             msg_Err( p_demux_, "Could not allocate memory for audio frame" );
144             return S_OK;
145         }
146
147         void *frame_bytes;
148         audioFrame->GetBytes( &frame_bytes );
149         memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
150
151         BMDTimeValue packet_time;
152         audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
153         p_audio_frame->i_pts = VLC_TS_0 + packet_time;
154     }
155
156     if( p_video_frame || p_audio_frame )
157     {
158         vlc_mutex_lock( &p_sys->frame_lock );
159         if( p_video_frame )
160             p_sys->p_video_frame = p_video_frame;  // FIXME: leak
161         if( p_audio_frame )
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 );
165     }
166
167     return S_OK;
168 }
169
170 static int Open( vlc_object_t *p_this )
171 {
172     demux_t     *p_demux = (demux_t*)p_this;
173     demux_sys_t *p_sys;
174
175     /* Only when selected */
176     if( *p_demux->psz_access == '\0' )
177         return VLC_EGENERIC;
178
179     /* Set up p_demux */
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 ) );
186     if( !p_sys )
187         return VLC_ENOMEM;
188
189     vlc_mutex_init( &p_sys->frame_lock );
190     vlc_cond_init( &p_sys->has_frame );
191     p_sys->p_video_frame = NULL;
192
193     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
194     if( !decklink_iterator )
195     {
196         msg_Err( p_demux, "DeckLink drivers not found." );
197         // FIXME: Leak here and several other error paths.
198         return VLC_EGENERIC;
199     }
200
201     HRESULT result;
202     result = decklink_iterator->Next( &p_sys->p_card );
203
204     if( result != S_OK )
205     {
206         msg_Err( p_demux, "No DeckLink PCI cards found" );
207         return VLC_EGENERIC;
208     }
209
210     if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
211     {
212         msg_Err( p_demux, "Card has no inputs" );
213         return VLC_EGENERIC;
214     }
215
216     result = p_sys->p_input->EnableVideoInput( bmdModePAL, bmdFormat8BitYUV, 0 );
217     if( result != S_OK )
218     {
219         msg_Err( p_demux, "Failed to enable video input" );
220         return VLC_EGENERIC;
221     }
222         
223     IDeckLinkDisplayModeIterator *p_display_iterator;
224     result = p_sys->p_input->GetDisplayModeIterator( &p_display_iterator );
225     if( result != S_OK )
226     {
227         msg_Err( p_demux, "Failed to enumerate display modes" );
228         return VLC_EGENERIC;
229     }
230
231     for (;;)
232     {
233         IDeckLinkDisplayMode *p_display_mode;
234         result = p_display_iterator->Next( &p_display_mode );
235         if( result != S_OK || !p_display_mode )
236         {
237             break; 
238         }
239
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) );
243
244         const char *mode_name;
245         result = p_display_mode->GetName( &mode_name );
246         if( result != S_OK )
247         {
248             msg_Err( p_demux, "Failed to get display mode name" );
249             return VLC_EGENERIC;
250         }
251
252         BMDTimeValue frame_duration, time_scale;
253         result = p_display_mode->GetFrameRate( &frame_duration, &time_scale );
254         if( result != S_OK )
255         {
256             msg_Err( p_demux, "Failed to get frame rate" );
257             return VLC_EGENERIC;
258         }
259
260         const char *field_dominance;
261         switch( p_display_mode->GetFieldDominance() )
262         {
263         case bmdProgressiveFrame:
264             field_dominance = "";
265             break;
266         case bmdProgressiveSegmentedFrame:
267             field_dominance = ", segmented";
268             break;
269         case bmdLowerFieldFirst:
270             field_dominance = ", interlaced [BFF]";
271             break;
272         case bmdUpperFieldFirst:
273             field_dominance = ", interlaced [TFF]";
274             break;
275         case bmdUnknownFieldDominance:
276         default:
277             field_dominance = ", unknown field dominance";
278             break;
279         }
280
281         char buf[256];
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 );
287     }
288    
289     result = p_sys->p_input->EnableAudioInput( 48000, bmdAudioSampleType16bitInteger, 2 );
290     if( result != S_OK )
291     {
292         msg_Err( p_demux, "Failed to enable audio input" );
293         return VLC_EGENERIC;
294     }
295     
296     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
297     p_sys->p_input->SetCallback( p_sys->p_delegate );
298
299     result = p_sys->p_input->StartStreams();
300     if( result != S_OK )
301     {
302         msg_Err( p_demux, "Failed to start streams" );
303         return VLC_EGENERIC;
304     }
305
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;
316
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 );
320     
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;
328
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 );
332
333     p_sys->b_first_frame = true;
334
335     /* Update default_pts to a suitable value for access */
336     var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
337
338     return VLC_SUCCESS;
339 }
340
341 static void Close( vlc_object_t *p_this )
342 {
343     demux_t     *p_demux = (demux_t *)p_this;
344     demux_sys_t *p_sys   = p_demux->p_sys;
345
346     free( p_sys );
347 }
348
349 static int Control( demux_t *p_demux, int i_query, va_list args )
350 {
351     bool *pb;
352     int64_t    *pi64;
353
354     switch( i_query )
355     {
356         /* Special for access_demux */
357         case DEMUX_CAN_PAUSE:
358         case DEMUX_CAN_SEEK:
359         case DEMUX_CAN_CONTROL_PACE:
360             pb = (bool*)va_arg( args, bool * );
361             *pb = false;
362             return VLC_SUCCESS;
363
364         case DEMUX_GET_PTS_DELAY:
365             pi64 = (int64_t*)va_arg( args, int64_t * );
366             *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
367             return VLC_SUCCESS;
368
369         case DEMUX_GET_TIME:
370             pi64 = (int64_t*)va_arg( args, int64_t * );
371             *pi64 = mdate();  // FIXME
372             return VLC_SUCCESS;
373
374         /* TODO implement others */
375         default:
376             return VLC_EGENERIC;
377     }
378
379     return VLC_EGENERIC;
380 }
381
382 static int Demux( demux_t *p_demux )
383 {
384     demux_sys_t *p_sys = p_demux->p_sys;
385     block_t *p_video_block = NULL;
386     block_t *p_audio_block = NULL;
387
388     vlc_mutex_lock( &p_sys->frame_lock );
389
390     while( !p_sys->p_video_frame && !p_sys->p_audio_frame )
391         vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
392
393     p_video_block = p_sys->p_video_frame;
394     p_sys->p_video_frame = NULL;
395
396     p_audio_block = p_sys->p_audio_frame;
397     p_sys->p_audio_frame = NULL;
398
399     vlc_mutex_unlock( &p_sys->frame_lock );
400
401     if( p_video_block )
402     {
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 );
405     }
406     
407     if( p_audio_block )
408     {
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 );
411     }
412
413     return 1;
414 }
415