]> git.sesse.net Git - vlc/blob - modules/access/sdi.cpp
098a121982c1eb619e27340dc665d45f6d3ecc4f
[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 #include <vlc_atomic.h>
20
21 #include <arpa/inet.h>
22
23 #include <DeckLinkAPI.h>
24 #include <DeckLinkAPIDispatch.cpp>
25
26 static int  Open ( vlc_object_t * );
27 static void Close( vlc_object_t * );
28
29 #define CARD_INDEX_TEXT N_("Input card to use")
30 #define CARD_INDEX_LONGTEXT N_( \
31     "SDI capture card to use, if multiple exist. " \
32     "The cards are numbered from 0." )
33
34 #define MODE_TEXT N_("Desired input video mode")
35 #define MODE_LONGTEXT N_( \
36     "Desired input video mode for SDI captures. " \
37     "This value should be a FOURCC code in textual " \
38     "form, e.g. \"ntsc\"." )
39
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42     "Caching value for SDI captures. This " \
43     "value should be set in milliseconds." )
44
45 #define AUDIO_CONNECTION_TEXT N_("Audio connection")
46 #define AUDIO_CONNECTION_LONGTEXT N_( \
47     "Audio connection to use for SDI captures. " \
48     "Valid choices: embedded, aesebu, analog. " \
49     "Leave blank for card default." )
50
51 #define RATE_TEXT N_("Audio sampling rate in Hz")
52 #define RATE_LONGTEXT N_( \
53     "Audio sampling rate (in hertz) for SDI captures. " \
54     "0 disables audio input." )
55
56 #define CHANNELS_TEXT N_("Number of audio channels")
57 #define CHANNELS_LONGTEXT N_( \
58     "Number of input audio channels for SDI captures. " \
59     "Must be 2, 8 or 16. 0 disables audio input." )
60
61 #define VIDEO_CONNECTION_TEXT N_("Video connection")
62 #define VIDEO_CONNECTION_LONGTEXT N_( \
63     "Video connection to use for SDI captures. " \
64     "Valid choices: sdi, hdmi, opticalsdi, component, " \
65     "composite, svideo. " \
66     "Leave blank for card default." )
67
68 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
69 #define ASPECT_RATIO_LONGTEXT N_( \
70     "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
71
72 vlc_module_begin ()
73     set_shortname( N_("SDI") )
74     set_description( N_("Blackmagic SDI input") )
75     set_category( CAT_INPUT )
76     set_subcategory( SUBCAT_INPUT_ACCESS )
77
78     add_integer( "sdi-card-index", 0, NULL,
79                  CARD_INDEX_TEXT, CARD_INDEX_LONGTEXT, true )
80     add_string( "sdi-mode", "pal ", NULL,
81                  MODE_TEXT, MODE_LONGTEXT, true )
82     add_integer( "sdi-caching", DEFAULT_PTS_DELAY / 1000, NULL,
83                  CACHING_TEXT, CACHING_LONGTEXT, true )
84     add_string( "sdi-audio-connection", 0, NULL,
85                  AUDIO_CONNECTION_TEXT, AUDIO_CONNECTION_LONGTEXT, true )
86     add_integer( "sdi-audio-rate", 48000, NULL,
87                  RATE_TEXT, RATE_LONGTEXT, true )
88     add_integer( "sdi-audio-channels", 2, NULL,
89                  CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
90     add_string( "sdi-video-connection", 0, NULL,
91                  VIDEO_CONNECTION_TEXT, VIDEO_CONNECTION_LONGTEXT, true )
92     add_string( "sdi-aspect-ratio", NULL, NULL,
93                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
94
95     add_shortcut( "sdi" )
96     set_capability( "access_demux", 10 )
97     set_callbacks( Open, Close )
98 vlc_module_end ()
99
100 static int Demux  ( demux_t * );
101 static int Control( demux_t *, int, va_list );
102
103 class DeckLinkCaptureDelegate;
104
105 struct demux_sys_t
106 {
107     IDeckLink *p_card;
108     IDeckLinkInput *p_input;
109     DeckLinkCaptureDelegate *p_delegate;
110
111     es_out_id_t *p_video_es;
112     es_out_id_t *p_audio_es;
113     bool b_first_frame;
114     int i_last_pts;
115
116     int i_width, i_height, i_fps_num, i_fps_den;
117     uint32_t i_dominance_flags;
118
119     int i_rate, i_channels;
120
121     vlc_mutex_t frame_lock;
122     block_t *p_video_frame;  /* protected by <frame_lock> */
123     block_t *p_audio_frame;  /* protected by <frame_lock> */
124     vlc_cond_t has_frame;  /* related to <frame_lock> */
125 };
126
127 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
128 {
129 public:
130     DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux)
131     {
132         vlc_atomic_set( &m_ref_, 1 );
133     }
134
135     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
136
137     virtual ULONG STDMETHODCALLTYPE AddRef(void)
138     {
139         return vlc_atomic_inc( &m_ref_ );
140     }
141
142     virtual ULONG STDMETHODCALLTYPE Release(void)
143     {
144         uintptr_t new_ref = vlc_atomic_dec( &m_ref_ );
145         if ( new_ref == 0 )
146             delete this;
147         return new_ref;
148     }
149
150     virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
151     virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
152
153 private:
154     vlc_atomic_t m_ref_;
155     demux_t *p_demux_;
156 };
157
158 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
159 {
160     msg_Dbg( p_demux_, "Video input format changed" );
161     return S_OK;
162 }
163
164 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
165 {
166     demux_sys_t *p_sys = p_demux_->p_sys;
167     block_t *p_video_frame = NULL;
168     block_t *p_audio_frame = NULL;
169
170     if( videoFrame )
171     {
172         if( videoFrame->GetFlags() & bmdFrameHasNoInputSource )
173         {
174             msg_Warn( p_demux_, "No input signal detected" );
175             return S_OK;
176         }
177
178         const int i_width = videoFrame->GetWidth();
179         const int i_height = videoFrame->GetHeight();
180         const int i_stride = videoFrame->GetRowBytes();
181         const int i_bpp = 2;
182
183         p_video_frame = block_New( p_demux_, i_width * i_height * i_bpp );
184         if( !p_video_frame )
185         {
186             msg_Err( p_demux_, "Could not allocate memory for video frame" );
187             return S_OK;
188         }
189
190         void *frame_bytes;
191         videoFrame->GetBytes( &frame_bytes );
192         for( int y = 0; y < i_height; ++y )
193         {
194             const uint8_t *src = (const uint8_t *)frame_bytes + i_stride * y;
195             uint8_t *dst = (uint8_t *)p_video_frame->p_buffer + i_width * i_bpp * y;
196             memcpy( dst, src, i_width * i_bpp );
197         }
198
199         BMDTimeValue stream_time, frame_duration;
200         videoFrame->GetStreamTime( &stream_time, &frame_duration, CLOCK_FREQ );
201         p_video_frame->i_flags = BLOCK_FLAG_TYPE_I | p_sys->i_dominance_flags;
202         if( p_sys->b_first_frame )
203         {
204             p_video_frame->i_flags |= BLOCK_FLAG_DISCONTINUITY;
205             p_sys->b_first_frame = false;
206         }
207         p_video_frame->i_pts = p_video_frame->i_dts = VLC_TS_0 + stream_time;
208     }
209
210     if( audioFrame )
211     {
212         const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * p_sys->i_channels;
213
214         p_audio_frame = block_New( p_demux_, i_bytes );
215         if( !p_audio_frame )
216         {
217             msg_Err( p_demux_, "Could not allocate memory for audio frame" );
218             if( p_video_frame )
219                 block_Release( p_video_frame );
220             return S_OK;
221         }
222
223         void *frame_bytes;
224         audioFrame->GetBytes( &frame_bytes );
225         memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
226
227         BMDTimeValue packet_time;
228         audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
229         p_audio_frame->i_pts = p_audio_frame->i_dts = VLC_TS_0 + packet_time;
230     }
231
232     if( p_video_frame || p_audio_frame )
233     {
234         vlc_mutex_lock( &p_sys->frame_lock );
235         if( p_video_frame )
236         {
237             if( p_sys->p_video_frame )
238                 block_Release( p_sys->p_video_frame );
239             p_sys->p_video_frame = p_video_frame;
240         }
241         if( p_audio_frame )
242         {
243             if( p_sys->p_audio_frame )
244                 block_Release( p_sys->p_audio_frame );
245             p_sys->p_audio_frame = p_audio_frame;
246         }
247         vlc_cond_signal( &p_sys->has_frame );
248         vlc_mutex_unlock( &p_sys->frame_lock );
249     }
250
251     return S_OK;
252 }
253
254 static int Open( vlc_object_t *p_this )
255 {
256     demux_t     *p_demux = (demux_t*)p_this;
257     demux_sys_t *p_sys;
258     int         ret = VLC_SUCCESS;
259     char        *psz_aspect;
260     char        *psz_display_mode = NULL;
261     char        *psz_video_connection = NULL;
262     char        *psz_audio_connection = NULL;
263     bool        b_found_mode;
264     int         i_card_index;
265
266     /* Only when selected */
267     if( *p_demux->psz_access == '\0' )
268         return VLC_EGENERIC;
269
270     /* Set up p_demux */
271     p_demux->pf_demux = Demux;
272     p_demux->pf_control = Control;
273     p_demux->info.i_update = 0;
274     p_demux->info.i_title = 0;
275     p_demux->info.i_seekpoint = 0;
276     p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
277     if( !p_sys )
278         return VLC_ENOMEM;
279
280     vlc_mutex_init( &p_sys->frame_lock );
281     vlc_cond_init( &p_sys->has_frame );
282     p_sys->b_first_frame = true;
283
284     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
285     if( !decklink_iterator )
286     {
287         msg_Err( p_demux, "DeckLink drivers not found." );
288         ret = VLC_EGENERIC;
289         goto finish;
290     }
291
292     HRESULT result;
293
294     i_card_index = var_CreateGetInteger( p_demux, "sdi-card-index" );
295     for( int i = 0; i <= i_card_index; ++i )
296     {
297         if( p_sys->p_card )
298             p_sys->p_card->Release();
299         result = decklink_iterator->Next( &p_sys->p_card );
300         if( result != S_OK )
301             break;
302     }
303
304     if( result != S_OK )
305     {
306         msg_Err( p_demux, "DeckLink PCI card %d not found", i_card_index );
307         ret = VLC_EGENERIC;
308         goto finish;
309     }
310
311     const char *psz_model_name;
312     result = p_sys->p_card->GetModelName( &psz_model_name );
313
314     if( result != S_OK )
315     {
316         msg_Err( p_demux, "Could not get model name" );
317         ret = VLC_EGENERIC;
318         goto finish;
319     }
320
321     msg_Dbg( p_demux, "Opened DeckLink PCI card %d (%s)", i_card_index, psz_model_name );
322
323     if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
324     {
325         msg_Err( p_demux, "Card has no inputs" );
326         ret = VLC_EGENERIC;
327         goto finish;
328     }
329
330     /* Set up the video and audio sources. */
331     IDeckLinkConfiguration *p_config;
332     if( p_sys->p_card->QueryInterface( IID_IDeckLinkConfiguration, (void**)&p_config) != S_OK )
333     {
334         msg_Err( p_demux, "Failed to get configuration interface" );
335         ret = VLC_EGENERIC;
336         goto finish;
337     }
338
339     psz_video_connection = var_CreateGetNonEmptyString( p_demux, "sdi-video-connection" );
340     if( psz_video_connection )
341     {
342         BMDVideoConnection conn;
343         if ( !strcmp( psz_video_connection, "sdi" ) )
344             conn = bmdVideoConnectionSDI;
345         else if ( !strcmp( psz_video_connection, "hdmi" ) )
346             conn = bmdVideoConnectionHDMI;
347         else if ( !strcmp( psz_video_connection, "opticalsdi" ) )
348             conn = bmdVideoConnectionOpticalSDI;
349         else if ( !strcmp( psz_video_connection, "component" ) )
350             conn = bmdVideoConnectionComponent;
351         else if ( !strcmp( psz_video_connection, "composite" ) )
352             conn = bmdVideoConnectionComposite;
353         else if ( !strcmp( psz_video_connection, "svideo" ) )
354             conn = bmdVideoConnectionSVideo;
355         else
356         {
357             msg_Err( p_demux, "Invalid --sdi-video-connection specified; choose one of " \
358                               "sdi, hdmi, opticalsdi, component, composite, or svideo." );
359             ret = VLC_EGENERIC;
360             goto finish;
361         }
362
363         msg_Dbg( p_demux, "Setting video input format to 0x%x", conn);
364         result = p_config->SetVideoInputFormat( conn );
365         if( result != S_OK )
366         {
367             msg_Err( p_demux, "Failed to set video input connection" );
368             ret = VLC_EGENERIC;
369             goto finish;
370         }
371     }
372
373     psz_audio_connection = var_CreateGetNonEmptyString( p_demux, "sdi-audio-connection" );
374     if( psz_audio_connection )
375     {
376         BMDAudioConnection conn;
377         if ( !strcmp( psz_audio_connection, "embedded" ) )
378             conn = bmdAudioConnectionEmbedded;
379         else if ( !strcmp( psz_audio_connection, "aesebu" ) )
380             conn = bmdAudioConnectionAESEBU;
381         else if ( !strcmp( psz_audio_connection, "analog" ) )
382             conn = bmdAudioConnectionAnalog;
383         else
384         {
385             msg_Err( p_demux, "Invalid --sdi-audio-connection specified; choose one of " \
386                               "embedded, aesebu, or analog." );
387             ret = VLC_EGENERIC;
388             goto finish;
389         }
390
391         msg_Dbg( p_demux, "Setting audio input format to 0x%x", conn);
392         result = p_config->SetAudioInputFormat( conn );
393         if( result != S_OK )
394         {
395             msg_Err( p_demux, "Failed to set audio input connection" );
396             ret = VLC_EGENERIC;
397             goto finish;
398         }
399     }
400
401     /* Get the list of display modes. */
402     IDeckLinkDisplayModeIterator *p_display_iterator;
403     result = p_sys->p_input->GetDisplayModeIterator( &p_display_iterator );
404     if( result != S_OK )
405     {
406         msg_Err( p_demux, "Failed to enumerate display modes" );
407         ret = VLC_EGENERIC;
408         goto finish;
409     }
410
411     psz_display_mode = var_CreateGetString( p_demux, "sdi-mode" );
412     if( !psz_display_mode || strlen( psz_display_mode ) == 0 || strlen( psz_display_mode ) > 4 ) {
413         msg_Err( p_demux, "Missing or invalid --sdi-mode string" );
414         ret = VLC_EGENERIC;
415         goto finish;
416     }
417
418     /*
419      * Pad the --sdi-mode string to four characters, so the user can specify e.g. "pal"
420      * without having to add the trailing space.
421      */
422     char sz_display_mode_padded[5];
423     strcpy(sz_display_mode_padded, "    ");
424     for( int i = 0; i < strlen( psz_display_mode ); ++i )
425         sz_display_mode_padded[i] = psz_display_mode[i];
426
427     BMDDisplayMode wanted_mode_id;
428     memcpy( &wanted_mode_id, &sz_display_mode_padded, sizeof(wanted_mode_id) );
429
430     b_found_mode = false;
431
432     for (;;)
433     {
434         IDeckLinkDisplayMode *p_display_mode;
435         result = p_display_iterator->Next( &p_display_mode );
436         if( result != S_OK || !p_display_mode )
437             break;
438
439         char sz_mode_id_text[5] = {0};
440         BMDDisplayMode mode_id = ntohl( p_display_mode->GetDisplayMode() );
441         memcpy( sz_mode_id_text, &mode_id, sizeof(mode_id) );
442
443         const char *psz_mode_name;
444         result = p_display_mode->GetName( &psz_mode_name );
445         if( result != S_OK )
446         {
447             msg_Err( p_demux, "Failed to get display mode name" );
448             p_display_mode->Release();
449             ret = VLC_EGENERIC;
450             goto finish;
451         }
452
453         BMDTimeValue frame_duration, time_scale;
454         result = p_display_mode->GetFrameRate( &frame_duration, &time_scale );
455         if( result != S_OK )
456         {
457             msg_Err( p_demux, "Failed to get frame rate" );
458             p_display_mode->Release();
459             ret = VLC_EGENERIC;
460             goto finish;
461         }
462
463         const char *psz_field_dominance;
464         uint32_t i_dominance_flags = 0;
465         switch( p_display_mode->GetFieldDominance() )
466         {
467         case bmdProgressiveFrame:
468             psz_field_dominance = "";
469             break;
470         case bmdProgressiveSegmentedFrame:
471             psz_field_dominance = ", segmented";
472             break;
473         case bmdLowerFieldFirst:
474             psz_field_dominance = ", interlaced [BFF]";
475             i_dominance_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
476             break;
477         case bmdUpperFieldFirst:
478             psz_field_dominance = ", interlaced [TFF]";
479             i_dominance_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
480             break;
481         case bmdUnknownFieldDominance:
482         default:
483             psz_field_dominance = ", unknown field dominance";
484             break;
485         }
486
487         msg_Dbg( p_demux, "Found mode '%s': %s (%dx%d, %.3f fps%s)",
488                  sz_mode_id_text, psz_mode_name,
489                  p_display_mode->GetWidth(), p_display_mode->GetHeight(),
490                  double(time_scale) / frame_duration, psz_field_dominance );
491
492         if( wanted_mode_id == mode_id )
493         {
494             b_found_mode = true;
495             p_sys->i_width = p_display_mode->GetWidth();
496             p_sys->i_height = p_display_mode->GetHeight();
497             p_sys->i_fps_num = time_scale;
498             p_sys->i_fps_den = frame_duration;
499             p_sys->i_dominance_flags = i_dominance_flags;
500         }
501
502         p_display_mode->Release();
503     }
504
505     if( !b_found_mode )
506     {
507         msg_Err( p_demux, "Unknown SDI mode specified. " \
508                           "Run VLC with -v --verbose-objects=-all,+sdi " \
509                           "to get a list of supported modes." );
510         ret = VLC_EGENERIC;
511         goto finish;
512     }
513
514     result = p_sys->p_input->EnableVideoInput( htonl( wanted_mode_id ), bmdFormat8BitYUV, 0 );
515     if( result != S_OK )
516     {
517         msg_Err( p_demux, "Failed to enable video input" );
518         ret = VLC_EGENERIC;
519         goto finish;
520     }
521
522     /* Set up audio. */
523     p_sys->i_rate = var_CreateGetInteger( p_demux, "sdi-audio-rate" );
524     p_sys->i_channels = var_CreateGetInteger( p_demux, "sdi-audio-channels" );
525     if( p_sys->i_rate > 0 && p_sys->i_channels > 0 )
526     {
527         result = p_sys->p_input->EnableAudioInput( p_sys->i_rate, bmdAudioSampleType16bitInteger, p_sys->i_channels );
528         if( result != S_OK )
529         {
530             msg_Err( p_demux, "Failed to enable audio input" );
531             ret = VLC_EGENERIC;
532             goto finish;
533         }
534     }
535
536     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
537     p_sys->p_input->SetCallback( p_sys->p_delegate );
538
539     result = p_sys->p_input->StartStreams();
540     if( result != S_OK )
541     {
542         msg_Err( p_demux, "Failed to start streams" );
543         ret = VLC_EGENERIC;
544         goto finish;
545     }
546
547     /* Declare elementary streams */
548     es_format_t video_fmt;
549     es_format_Init( &video_fmt, VIDEO_ES, VLC_CODEC_UYVY );
550     video_fmt.video.i_width = p_sys->i_width;
551     video_fmt.video.i_height = p_sys->i_height;
552     video_fmt.video.i_sar_num = 1;
553     video_fmt.video.i_sar_den = 1;
554     video_fmt.video.i_frame_rate = p_sys->i_fps_num;
555     video_fmt.video.i_frame_rate_base = p_sys->i_fps_den;
556     video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2 * 8;
557
558     psz_aspect = var_CreateGetNonEmptyString( p_demux, "sdi-aspect-ratio" );
559     if( psz_aspect )
560     {
561         char *psz_denominator = strchr( psz_aspect, ':' );
562         if( psz_denominator )
563         {
564             *psz_denominator++ = '\0';
565             video_fmt.video.i_sar_num = atoi( psz_aspect )      * video_fmt.video.i_height;
566             video_fmt.video.i_sar_den = atoi( psz_denominator ) * video_fmt.video.i_width;
567         }
568         free( psz_aspect );
569     }
570
571     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
572              (char*)&video_fmt.i_codec, video_fmt.video.i_width, video_fmt.video.i_height );
573     p_sys->p_video_es = es_out_Add( p_demux->out, &video_fmt );
574
575     es_format_t audio_fmt;
576     es_format_Init( &audio_fmt, AUDIO_ES, VLC_CODEC_S16N );
577     audio_fmt.audio.i_channels = p_sys->i_channels;
578     audio_fmt.audio.i_rate = p_sys->i_rate;
579     audio_fmt.audio.i_bitspersample = 16;
580     audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
581     audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
582
583     msg_Dbg( p_demux, "added new audio es %4.4s %dHz %dbpp %dch",
584              (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
585     p_sys->p_audio_es = es_out_Add( p_demux->out, &audio_fmt );
586
587     /* Update default_pts to a suitable value for access */
588     var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
589
590 finish:
591     if( decklink_iterator )
592         decklink_iterator->Release();
593
594     if( p_config )
595         p_config->Release();
596
597     free( psz_video_connection );
598     free( psz_audio_connection );
599     free( psz_display_mode );
600
601     if( p_display_iterator )
602         p_display_iterator->Release();
603
604     if( ret != VLC_SUCCESS )
605         Close( p_this );
606
607     return ret;
608 }
609
610 static void Close( vlc_object_t *p_this )
611 {
612     demux_t     *p_demux = (demux_t *)p_this;
613     demux_sys_t *p_sys   = p_demux->p_sys;
614
615     if( p_sys->p_input )
616     {
617         p_sys->p_input->StopStreams();
618         p_sys->p_input->Release();
619     }
620
621     if( p_sys->p_card )
622         p_sys->p_card->Release();
623
624     if( p_sys->p_delegate )
625         p_sys->p_delegate->Release();
626
627     if( p_sys->p_video_frame )
628         block_Release( p_sys->p_video_frame );
629
630     if( p_sys->p_audio_frame )
631         block_Release( p_sys->p_audio_frame );
632
633     free( p_sys );
634 }
635
636 static int Control( demux_t *p_demux, int i_query, va_list args )
637 {
638     demux_sys_t *p_sys = p_demux->p_sys;
639     bool *pb;
640     int64_t    *pi64;
641
642     switch( i_query )
643     {
644         /* Special for access_demux */
645         case DEMUX_CAN_PAUSE:
646         case DEMUX_CAN_SEEK:
647         case DEMUX_CAN_CONTROL_PACE:
648             pb = (bool*)va_arg( args, bool * );
649             *pb = false;
650             return VLC_SUCCESS;
651
652         case DEMUX_GET_PTS_DELAY:
653             pi64 = (int64_t*)va_arg( args, int64_t * );
654             *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
655             return VLC_SUCCESS;
656
657         case DEMUX_GET_TIME:
658             pi64 = (int64_t*)va_arg( args, int64_t * );
659             *pi64 = p_sys->i_last_pts;
660             return VLC_SUCCESS;
661
662         /* TODO implement others */
663         default:
664             return VLC_EGENERIC;
665     }
666
667     return VLC_EGENERIC;
668 }
669
670 static int Demux( demux_t *p_demux )
671 {
672     demux_sys_t *p_sys = p_demux->p_sys;
673     block_t *p_video_block = NULL;
674     block_t *p_audio_block = NULL;
675
676     vlc_mutex_lock( &p_sys->frame_lock );
677
678     while( !p_sys->p_video_frame && !p_sys->p_audio_frame )
679         vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
680
681     p_video_block = p_sys->p_video_frame;
682     p_sys->p_video_frame = NULL;
683
684     p_audio_block = p_sys->p_audio_frame;
685     p_sys->p_audio_frame = NULL;
686
687     vlc_mutex_unlock( &p_sys->frame_lock );
688
689     if( p_video_block )
690     {
691         if( p_video_block->i_pts > p_sys->i_last_pts )
692             p_sys->i_last_pts = p_video_block->i_pts;
693         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_video_block->i_pts );
694         es_out_Send( p_demux->out, p_sys->p_video_es, p_video_block );
695     }
696
697     if( p_audio_block )
698     {
699         if( p_audio_block->i_pts > p_sys->i_last_pts )
700             p_sys->i_last_pts = p_audio_block->i_pts;
701         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_audio_block->i_pts );
702         es_out_Send( p_demux->out, p_sys->p_audio_es, p_audio_block );
703     }
704
705     return 1;
706 }
707