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