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