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