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