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