]> git.sesse.net Git - vlc/blob - modules/access/decklink.cpp
Remove obsolete variable b_first_frame.
[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 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
90 #define ASPECT_RATIO_LONGTEXT N_( \
91     "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
92
93 vlc_module_begin ()
94     set_shortname( N_("DeckLink") )
95     set_description( N_("Blackmagic DeckLink SDI input") )
96     set_category( CAT_INPUT )
97     set_subcategory( SUBCAT_INPUT_ACCESS )
98
99     add_integer( "decklink-card-index", 0, NULL,
100                  CARD_INDEX_TEXT, CARD_INDEX_LONGTEXT, true )
101     add_string( "decklink-mode", "pal ", NULL,
102                  MODE_TEXT, MODE_LONGTEXT, true )
103     add_integer( "decklink-caching", DEFAULT_PTS_DELAY / 1000, NULL,
104                  CACHING_TEXT, CACHING_LONGTEXT, true )
105     add_string( "decklink-audio-connection", 0, NULL,
106                  AUDIO_CONNECTION_TEXT, AUDIO_CONNECTION_LONGTEXT, true )
107     add_integer( "decklink-audio-rate", 48000, NULL,
108                  RATE_TEXT, RATE_LONGTEXT, true )
109     add_integer( "decklink-audio-channels", 2, NULL,
110                  CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
111     add_string( "decklink-video-connection", 0, NULL,
112                  VIDEO_CONNECTION_TEXT, VIDEO_CONNECTION_LONGTEXT, true )
113         change_string_list( ppsz_videoconns, ppsz_videoconns_text, 0 )
114     add_string( "decklink-aspect-ratio", NULL, 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 Demux  ( demux_t * );
123 static int Control( demux_t *, int, va_list );
124
125 class DeckLinkCaptureDelegate;
126
127 struct demux_sys_t
128 {
129     IDeckLink *p_card;
130     IDeckLinkInput *p_input;
131     DeckLinkCaptureDelegate *p_delegate;
132
133     es_out_id_t *p_video_es;
134     es_out_id_t *p_audio_es;
135     int i_last_pts;
136
137     uint32_t i_dominance_flags;
138     int i_channels;
139
140     vlc_mutex_t frame_lock;
141     block_t *p_video_frame;  /* protected by <frame_lock> */
142     block_t *p_audio_frame;  /* protected by <frame_lock> */
143     vlc_cond_t has_frame;  /* related to <frame_lock> */
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
223     if( audioFrame )
224     {
225         const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * p_sys->i_channels;
226
227         p_audio_frame = block_New( p_demux_, i_bytes );
228         if( !p_audio_frame )
229         {
230             msg_Err( p_demux_, "Could not allocate memory for audio frame" );
231             if( p_video_frame )
232                 block_Release( p_video_frame );
233             return S_OK;
234         }
235
236         void *frame_bytes;
237         audioFrame->GetBytes( &frame_bytes );
238         memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
239
240         BMDTimeValue packet_time;
241         audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
242         p_audio_frame->i_pts = p_audio_frame->i_dts = VLC_TS_0 + packet_time;
243     }
244
245     if( p_video_frame || p_audio_frame )
246     {
247         vlc_mutex_lock( &p_sys->frame_lock );
248         if( p_video_frame )
249         {
250             if( p_sys->p_video_frame )
251                 block_Release( p_sys->p_video_frame );
252             p_sys->p_video_frame = p_video_frame;
253         }
254         if( p_audio_frame )
255         {
256             if( p_sys->p_audio_frame )
257                 block_Release( p_sys->p_audio_frame );
258             p_sys->p_audio_frame = p_audio_frame;
259         }
260         vlc_cond_signal( &p_sys->has_frame );
261         vlc_mutex_unlock( &p_sys->frame_lock );
262     }
263
264     return S_OK;
265 }
266
267 static int Open( vlc_object_t *p_this )
268 {
269     demux_t     *p_demux = (demux_t*)p_this;
270     demux_sys_t *p_sys;
271     int         ret = VLC_EGENERIC;
272     char        *psz_aspect;
273     char        *psz_display_mode = NULL;
274     char        *psz_video_connection = NULL;
275     char        *psz_audio_connection = NULL;
276     bool        b_found_mode;
277     int         i_card_index;
278     int         i_width, i_height, i_fps_num, i_fps_den;
279     int         i_rate;
280     unsigned    u_aspect_num, u_aspect_den;
281
282     /* Only when selected */
283     if( *p_demux->psz_access == '\0' )
284         return VLC_EGENERIC;
285
286     /* Set up p_demux */
287     p_demux->pf_demux = Demux;
288     p_demux->pf_control = Control;
289     p_demux->info.i_update = 0;
290     p_demux->info.i_title = 0;
291     p_demux->info.i_seekpoint = 0;
292     p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
293     if( !p_sys )
294         return VLC_ENOMEM;
295
296     vlc_mutex_init( &p_sys->frame_lock );
297     vlc_cond_init( &p_sys->has_frame );
298
299     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
300     if( !decklink_iterator )
301     {
302         msg_Err( p_demux, "DeckLink drivers not found." );
303         goto finish;
304     }
305
306     HRESULT result;
307
308     i_card_index = var_InheritInteger( p_demux, "decklink-card-index" );
309     if( i_card_index < 0 )
310     {
311         msg_Err( p_demux, "Invalid card index %d", i_card_index );
312         goto finish;
313     }
314
315     for( int i = 0; i <= i_card_index; ++i )
316     {
317         if( p_sys->p_card )
318             p_sys->p_card->Release();
319         result = decklink_iterator->Next( &p_sys->p_card );
320         if( result != S_OK )
321             break;
322     }
323
324     if( result != S_OK )
325     {
326         msg_Err( p_demux, "DeckLink PCI card %d not found", i_card_index );
327         goto finish;
328     }
329
330     const char *psz_model_name;
331     result = p_sys->p_card->GetModelName( &psz_model_name );
332
333     if( result != S_OK )
334     {
335         msg_Err( p_demux, "Could not get model name" );
336         goto finish;
337     }
338
339     msg_Dbg( p_demux, "Opened DeckLink PCI card %d (%s)", i_card_index, psz_model_name );
340
341     if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
342     {
343         msg_Err( p_demux, "Card has no inputs" );
344         goto finish;
345     }
346
347     /* Set up the video and audio sources. */
348     IDeckLinkConfiguration *p_config;
349     if( p_sys->p_card->QueryInterface( IID_IDeckLinkConfiguration, (void**)&p_config) != S_OK )
350     {
351         msg_Err( p_demux, "Failed to get configuration interface" );
352         goto finish;
353     }
354
355     psz_video_connection = var_InheritString( p_demux, "decklink-video-connection" );
356     if( psz_video_connection )
357     {
358         BMDVideoConnection conn;
359         if ( !strcmp( psz_video_connection, "sdi" ) )
360             conn = bmdVideoConnectionSDI;
361         else if ( !strcmp( psz_video_connection, "hdmi" ) )
362             conn = bmdVideoConnectionHDMI;
363         else if ( !strcmp( psz_video_connection, "opticalsdi" ) )
364             conn = bmdVideoConnectionOpticalSDI;
365         else if ( !strcmp( psz_video_connection, "component" ) )
366             conn = bmdVideoConnectionComponent;
367         else if ( !strcmp( psz_video_connection, "composite" ) )
368             conn = bmdVideoConnectionComposite;
369         else if ( !strcmp( psz_video_connection, "svideo" ) )
370             conn = bmdVideoConnectionSVideo;
371         else
372         {
373             msg_Err( p_demux, "Invalid --decklink-video-connection specified; choose one of " \
374                               "sdi, hdmi, opticalsdi, component, composite, or svideo." );
375             goto finish;
376         }
377
378         msg_Dbg( p_demux, "Setting video input format to 0x%x", conn);
379         result = p_config->SetVideoInputFormat( conn );
380         if( result != S_OK )
381         {
382             msg_Err( p_demux, "Failed to set video input connection" );
383             goto finish;
384         }
385     }
386
387     psz_audio_connection = var_CreateGetNonEmptyString( p_demux, "decklink-audio-connection" );
388     if( psz_audio_connection )
389     {
390         BMDAudioConnection conn;
391         if ( !strcmp( psz_audio_connection, "embedded" ) )
392             conn = bmdAudioConnectionEmbedded;
393         else if ( !strcmp( psz_audio_connection, "aesebu" ) )
394             conn = bmdAudioConnectionAESEBU;
395         else if ( !strcmp( psz_audio_connection, "analog" ) )
396             conn = bmdAudioConnectionAnalog;
397         else
398         {
399             msg_Err( p_demux, "Invalid --decklink-audio-connection specified; choose one of " \
400                               "embedded, aesebu, or analog." );
401             goto finish;
402         }
403
404         msg_Dbg( p_demux, "Setting audio input format to 0x%x", conn);
405         result = p_config->SetAudioInputFormat( conn );
406         if( result != S_OK )
407         {
408             msg_Err( p_demux, "Failed to set audio input connection" );
409             goto finish;
410         }
411     }
412
413     /* Get the list of display modes. */
414     IDeckLinkDisplayModeIterator *p_display_iterator;
415     result = p_sys->p_input->GetDisplayModeIterator( &p_display_iterator );
416     if( result != S_OK )
417     {
418         msg_Err( p_demux, "Failed to enumerate display modes" );
419         goto finish;
420     }
421
422     psz_display_mode = var_CreateGetNonEmptyString( p_demux, "decklink-mode" );
423     if( !psz_display_mode || strlen( psz_display_mode ) > 4 ) {
424         msg_Err( p_demux, "Missing or invalid --decklink-mode string" );
425         goto finish;
426     }
427
428     /*
429      * Pad the --decklink-mode string to four characters, so the user can specify e.g. "pal"
430      * without having to add the trailing space.
431      */
432     char sz_display_mode_padded[5];
433     strcpy(sz_display_mode_padded, "    ");
434     for( int i = 0; i < strlen( psz_display_mode ); ++i )
435         sz_display_mode_padded[i] = psz_display_mode[i];
436
437     BMDDisplayMode wanted_mode_id;
438     memcpy( &wanted_mode_id, &sz_display_mode_padded, sizeof(wanted_mode_id) );
439
440     b_found_mode = false;
441
442     for (;;)
443     {
444         IDeckLinkDisplayMode *p_display_mode;
445         result = p_display_iterator->Next( &p_display_mode );
446         if( result != S_OK || !p_display_mode )
447             break;
448
449         char sz_mode_id_text[5] = {0};
450         BMDDisplayMode mode_id = ntohl( p_display_mode->GetDisplayMode() );
451         memcpy( sz_mode_id_text, &mode_id, sizeof(mode_id) );
452
453         const char *psz_mode_name;
454         result = p_display_mode->GetName( &psz_mode_name );
455         if( result != S_OK )
456         {
457             msg_Err( p_demux, "Failed to get display mode name" );
458             p_display_mode->Release();
459             goto finish;
460         }
461
462         BMDTimeValue frame_duration, time_scale;
463         result = p_display_mode->GetFrameRate( &frame_duration, &time_scale );
464         if( result != S_OK )
465         {
466             msg_Err( p_demux, "Failed to get frame rate" );
467             p_display_mode->Release();
468             goto finish;
469         }
470
471         const char *psz_field_dominance;
472         uint32_t i_dominance_flags = 0;
473         switch( p_display_mode->GetFieldDominance() )
474         {
475         case bmdProgressiveFrame:
476             psz_field_dominance = "";
477             break;
478         case bmdProgressiveSegmentedFrame:
479             psz_field_dominance = ", segmented";
480             break;
481         case bmdLowerFieldFirst:
482             psz_field_dominance = ", interlaced [BFF]";
483             i_dominance_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
484             break;
485         case bmdUpperFieldFirst:
486             psz_field_dominance = ", interlaced [TFF]";
487             i_dominance_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
488             break;
489         case bmdUnknownFieldDominance:
490         default:
491             psz_field_dominance = ", unknown field dominance";
492             break;
493         }
494
495         msg_Dbg( p_demux, "Found mode '%s': %s (%dx%d, %.3f fps%s)",
496                  sz_mode_id_text, psz_mode_name,
497                  p_display_mode->GetWidth(), p_display_mode->GetHeight(),
498                  double(time_scale) / frame_duration, psz_field_dominance );
499
500         if( wanted_mode_id == mode_id )
501         {
502             b_found_mode = true;
503             i_width = p_display_mode->GetWidth();
504             i_height = p_display_mode->GetHeight();
505             i_fps_num = time_scale;
506             i_fps_den = frame_duration;
507             p_sys->i_dominance_flags = i_dominance_flags;
508         }
509
510         p_display_mode->Release();
511     }
512
513     if( !b_found_mode )
514     {
515         msg_Err( p_demux, "Unknown video mode specified. " \
516                           "Run VLC with -v --verbose-objects=-all,+decklink " \
517                           "to get a list of supported modes." );
518         goto finish;
519     }
520
521     result = p_sys->p_input->EnableVideoInput( htonl( wanted_mode_id ), bmdFormat8BitYUV, 0 );
522     if( result != S_OK )
523     {
524         msg_Err( p_demux, "Failed to enable video input" );
525         goto finish;
526     }
527
528     /* Set up audio. */
529     p_sys->i_channels = var_InheritInteger( p_demux, "decklink-audio-channels" );
530     i_rate = var_InheritInteger( p_demux, "decklink-audio-rate" );
531     if( i_rate > 0 && p_sys->i_channels > 0 )
532     {
533         result = p_sys->p_input->EnableAudioInput( i_rate, bmdAudioSampleType16bitInteger, p_sys->i_channels );
534         if( result != S_OK )
535         {
536             msg_Err( p_demux, "Failed to enable audio input" );
537             goto finish;
538         }
539     }
540
541     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
542     p_sys->p_input->SetCallback( p_sys->p_delegate );
543
544     result = p_sys->p_input->StartStreams();
545     if( result != S_OK )
546     {
547         msg_Err( p_demux, "Could not start streaming from SDI card. This could be caused "
548                           "by invalid video mode or flags, access denied, or card already in use." );
549         goto finish;
550     }
551
552     /* Declare elementary streams */
553     es_format_t video_fmt;
554     es_format_Init( &video_fmt, VIDEO_ES, VLC_CODEC_UYVY );
555     video_fmt.video.i_width = i_width;
556     video_fmt.video.i_height = i_height;
557     video_fmt.video.i_sar_num = 1;
558     video_fmt.video.i_sar_den = 1;
559     video_fmt.video.i_frame_rate = i_fps_num;
560     video_fmt.video.i_frame_rate_base = i_fps_den;
561     video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2 * 8;
562
563     if ( !var_InheritURational( p_demux, &u_aspect_num, &u_aspect_den, "decklink-aspect-ratio" ) &&
564          u_aspect_num > 0 && u_aspect_den > 0 ) {
565         video_fmt.video.i_sar_num = u_aspect_num * video_fmt.video.i_height;
566         video_fmt.video.i_sar_den = u_aspect_den * video_fmt.video.i_width;
567     }
568
569     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
570              (char*)&video_fmt.i_codec, video_fmt.video.i_width, video_fmt.video.i_height );
571     p_sys->p_video_es = es_out_Add( p_demux->out, &video_fmt );
572
573     es_format_t audio_fmt;
574     es_format_Init( &audio_fmt, AUDIO_ES, VLC_CODEC_S16N );
575     audio_fmt.audio.i_channels = p_sys->i_channels;
576     audio_fmt.audio.i_rate = i_rate;
577     audio_fmt.audio.i_bitspersample = 16;
578     audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
579     audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
580
581     msg_Dbg( p_demux, "added new audio es %4.4s %dHz %dbpp %dch",
582              (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
583     p_sys->p_audio_es = es_out_Add( p_demux->out, &audio_fmt );
584
585     ret = VLC_SUCCESS;
586
587 finish:
588     if( decklink_iterator )
589         decklink_iterator->Release();
590
591     if( p_config )
592         p_config->Release();
593
594     free( psz_video_connection );
595     free( psz_audio_connection );
596     free( psz_display_mode );
597
598     if( p_display_iterator )
599         p_display_iterator->Release();
600
601     if( ret != VLC_SUCCESS )
602         Close( p_this );
603
604     return ret;
605 }
606
607 static void Close( vlc_object_t *p_this )
608 {
609     demux_t     *p_demux = (demux_t *)p_this;
610     demux_sys_t *p_sys   = p_demux->p_sys;
611
612     if( p_sys->p_input )
613     {
614         p_sys->p_input->StopStreams();
615         p_sys->p_input->Release();
616     }
617
618     if( p_sys->p_card )
619         p_sys->p_card->Release();
620
621     if( p_sys->p_delegate )
622         p_sys->p_delegate->Release();
623
624     if( p_sys->p_video_frame )
625         block_Release( p_sys->p_video_frame );
626
627     if( p_sys->p_audio_frame )
628         block_Release( p_sys->p_audio_frame );
629
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             *pi64 = p_sys->i_last_pts;
657             return VLC_SUCCESS;
658
659         /* TODO implement others */
660         default:
661             return VLC_EGENERIC;
662     }
663
664     return VLC_EGENERIC;
665 }
666
667 static int Demux( demux_t *p_demux )
668 {
669     demux_sys_t *p_sys = p_demux->p_sys;
670     block_t *p_video_block = NULL;
671     block_t *p_audio_block = NULL;
672
673     vlc_mutex_lock( &p_sys->frame_lock );
674
675     while( !p_sys->p_video_frame && !p_sys->p_audio_frame )
676         vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
677
678     p_video_block = p_sys->p_video_frame;
679     p_sys->p_video_frame = NULL;
680
681     p_audio_block = p_sys->p_audio_frame;
682     p_sys->p_audio_frame = NULL;
683
684     vlc_mutex_unlock( &p_sys->frame_lock );
685
686     if( p_video_block )
687     {
688         if( p_video_block->i_pts > p_sys->i_last_pts )
689             p_sys->i_last_pts = p_video_block->i_pts;
690         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_video_block->i_pts );
691         es_out_Send( p_demux->out, p_sys->p_video_es, p_video_block );
692     }
693
694     if( p_audio_block )
695     {
696         if( p_audio_block->i_pts > p_sys->i_last_pts )
697             p_sys->i_last_pts = p_audio_block->i_pts;
698         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_audio_block->i_pts );
699         es_out_Send( p_demux->out, p_sys->p_audio_es, p_audio_block );
700     }
701
702     return 1;
703 }
704