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