]> git.sesse.net Git - mlt/blob - src/modules/decklink/producer_decklink.cpp
Merge pull request #1 from eddrog/master
[mlt] / src / modules / decklink / producer_decklink.cpp
1 /*
2  * producer_decklink.c -- input from Blackmagic Design DeckLink
3  * Copyright (C) 2011 Dan Dennedy <dan@dennedy.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with consumer library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <framework/mlt.h>
21 #include <stdlib.h>
22 #include <pthread.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <sys/time.h>
27 #include "common.h"
28
29 class DeckLinkProducer
30         : public IDeckLinkInputCallback
31 {
32 private:
33         mlt_producer     m_producer;
34         IDeckLink*       m_decklink;
35         IDeckLinkInput*  m_decklinkInput;
36         mlt_deque        m_queue;
37         pthread_mutex_t  m_mutex;
38         pthread_cond_t   m_condition;
39         bool             m_started;
40         int              m_dropped;
41         bool             m_isBuffering;
42         int              m_topFieldFirst;
43         int              m_colorspace;
44         int              m_vancLines;
45         mlt_cache        m_cache;
46
47         BMDDisplayMode getDisplayMode( mlt_profile profile, int vancLines )
48         {
49                 IDeckLinkDisplayModeIterator* iter = NULL;
50                 IDeckLinkDisplayMode* mode = NULL;
51                 BMDDisplayMode result = (BMDDisplayMode) bmdDisplayModeNotSupported;
52
53                 if ( m_decklinkInput->GetDisplayModeIterator( &iter ) == S_OK )
54                 {
55                         while ( !result && iter->Next( &mode ) == S_OK )
56                         {
57                                 int width = mode->GetWidth();
58                                 int height = mode->GetHeight();
59                                 BMDTimeValue duration;
60                                 BMDTimeScale timescale;
61                                 mode->GetFrameRate( &duration, &timescale );
62                                 double fps = (double) timescale / duration;
63                                 int p = mode->GetFieldDominance() == bmdProgressiveFrame;
64                                 m_topFieldFirst = mode->GetFieldDominance() == bmdUpperFieldFirst;
65                                 m_colorspace = ( mode->GetFlags() & bmdDisplayModeColorspaceRec709 ) ? 709 : 601;
66                                 mlt_log_verbose( getProducer(), "BMD mode %dx%d %.3f fps prog %d tff %d\n", width, height, fps, p, m_topFieldFirst );
67
68                                 if ( width == profile->width && p == profile->progressive
69                                          && ( height + vancLines == profile->height || ( height == 486 && profile->height == 480 + vancLines ) )
70                                          && fps == mlt_profile_fps( profile ) )
71                                         result = mode->GetDisplayMode();
72                                 SAFE_RELEASE( mode );
73                         }
74                         SAFE_RELEASE( iter );
75                 }
76
77                 return result;
78         }
79
80 public:
81
82         void setProducer( mlt_producer producer )
83                 { m_producer = producer; }
84
85         mlt_producer getProducer() const
86                 { return m_producer; }
87
88         DeckLinkProducer()
89         {
90                 m_decklink = NULL;
91                 m_decklinkInput = NULL;
92         }
93
94         ~DeckLinkProducer()
95         {
96                 if ( m_queue )
97                 {
98                         stop();
99                         mlt_deque_close( m_queue );
100                         pthread_mutex_destroy( &m_mutex );
101                         pthread_cond_destroy( &m_condition );
102                         mlt_cache_close( m_cache );
103                 }
104                 SAFE_RELEASE( m_decklinkInput );
105                 SAFE_RELEASE( m_decklink );
106         }
107
108         bool open( unsigned card =  0 )
109         {
110                 IDeckLinkIterator* decklinkIterator = NULL;
111                 try
112                 {
113 #ifdef WIN32
114                         HRESULT result =  CoInitialize( NULL );
115                         if ( FAILED( result ) )
116                                 throw "COM initialization failed";
117                         result = CoCreateInstance( CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**) &decklinkIterator );
118                         if ( FAILED( result ) )
119                                 throw "The DeckLink drivers are not installed.";
120 #else
121                         decklinkIterator = CreateDeckLinkIteratorInstance();
122                         if ( !decklinkIterator )
123                                 throw "The DeckLink drivers are not installed.";
124 #endif
125                         // Connect to the Nth DeckLink instance
126                         for ( unsigned i = 0; decklinkIterator->Next( &m_decklink ) == S_OK ; i++)
127                         {
128                                 if ( i == card )
129                                         break;
130                                 else
131                                         SAFE_RELEASE( m_decklink );
132                         }
133                         SAFE_RELEASE( decklinkIterator );
134                         if ( !m_decklink )
135                                 throw "DeckLink card not found.";
136
137                         // Get the input interface
138                         if ( m_decklink->QueryInterface( IID_IDeckLinkInput, (void**) &m_decklinkInput ) != S_OK )
139                                 throw "No DeckLink cards support input.";
140
141                         // Provide this class as a delegate to the input callback
142                         m_decklinkInput->SetCallback( this );
143
144                         // Initialize other members
145                         pthread_mutex_init( &m_mutex, NULL );
146                         pthread_cond_init( &m_condition, NULL );
147                         m_queue = mlt_deque_init();
148                         m_started = false;
149                         m_dropped = 0;
150                         m_isBuffering = true;
151                         m_cache = mlt_cache_init();
152
153                         // 3 covers YADIF and increasing framerate use cases
154                         mlt_cache_set_size( m_cache, 3 );
155                 }
156                 catch ( const char *error )
157                 {
158                         SAFE_RELEASE( m_decklinkInput );
159                         SAFE_RELEASE( m_decklink );
160                         mlt_log_error( getProducer(), "%s\n", error );
161                         return false;
162                 }
163                 return true;
164         }
165
166         bool start( mlt_profile profile = 0 )
167         {
168                 if ( m_started )
169                         return false;
170                 try
171                 {
172                         // Initialize some members
173                         m_vancLines = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "vanc" );
174                         if ( m_vancLines == -1 )
175                                 m_vancLines = profile->height <= 512 ? 26 : 32;
176
177                         if ( !profile )
178                                 profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
179
180                         // Get the display mode
181                         BMDDisplayMode displayMode = getDisplayMode( profile, m_vancLines );
182                         if ( displayMode == (BMDDisplayMode) bmdDisplayModeNotSupported )
183                         {
184                                 mlt_log_info( getProducer(), "profile = %dx%d %f fps %s\n", profile->width, profile->height,
185                                                           mlt_profile_fps( profile ), profile->progressive? "progressive" : "interlace" );
186                                 throw "Profile is not compatible with decklink.";
187                         }
188
189                         // Determine if supports input format detection
190 #ifdef WIN32
191                         BOOL doesDetectFormat = FALSE;
192 #else
193                         bool doesDetectFormat = false;
194 #endif
195                         IDeckLinkAttributes *decklinkAttributes = 0;
196                         if ( m_decklink->QueryInterface( IID_IDeckLinkAttributes, (void**) &decklinkAttributes ) == S_OK )
197                         {
198                                 if ( decklinkAttributes->GetFlag( BMDDeckLinkSupportsInputFormatDetection, &doesDetectFormat ) != S_OK )
199                                         doesDetectFormat = false;
200                                 SAFE_RELEASE( decklinkAttributes );
201                         }
202                         mlt_log_verbose( getProducer(), "%s format detection\n", doesDetectFormat ? "supports" : "does not support" );
203
204                         // Enable video capture
205                         BMDPixelFormat pixelFormat = bmdFormat8BitYUV;
206                         BMDVideoInputFlags flags = doesDetectFormat ? bmdVideoInputEnableFormatDetection : bmdVideoInputFlagDefault;
207                         if ( S_OK != m_decklinkInput->EnableVideoInput( displayMode, pixelFormat, flags ) )
208                                 throw "Failed to enable video capture.";
209
210                         // Enable audio capture
211                         BMDAudioSampleRate sampleRate = bmdAudioSampleRate48kHz;
212                         BMDAudioSampleType sampleType = bmdAudioSampleType16bitInteger;
213                         int channels = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" );
214                         if ( S_OK != m_decklinkInput->EnableAudioInput( sampleRate, sampleType, channels ) )
215                                 throw "Failed to enable audio capture.";
216
217                         // Start capture
218                         m_dropped = 0;
219                         mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "dropped", m_dropped );
220                         m_started = m_decklinkInput->StartStreams() == S_OK;
221                         if ( !m_started )
222                                 throw "Failed to start capture.";
223                 }
224                 catch ( const char *error )
225                 {
226                         m_decklinkInput->DisableVideoInput();
227                         mlt_log_error( getProducer(), "%s\n", error );
228                         return false;
229                 }
230                 return true;
231         }
232
233         void stop()
234         {
235                 if ( !m_started )
236                         return;
237                 m_started = false;
238
239                 // Release the wait in getFrame
240                 pthread_mutex_lock( &m_mutex );
241                 pthread_cond_broadcast( &m_condition );
242                 pthread_mutex_unlock( &m_mutex );
243
244                 m_decklinkInput->StopStreams();
245                 m_decklinkInput->DisableVideoInput();
246                 m_decklinkInput->DisableAudioInput();
247
248                 // Cleanup queue
249                 pthread_mutex_lock( &m_mutex );
250                 while ( mlt_frame frame = (mlt_frame) mlt_deque_pop_back( m_queue ) )
251                         mlt_frame_close( frame );
252                 pthread_mutex_unlock( &m_mutex );
253         }
254
255         mlt_frame getFrame()
256         {
257                 struct timeval now;
258                 struct timespec tm;
259                 double fps = mlt_producer_get_fps( getProducer() );
260                 mlt_position position = mlt_producer_position( getProducer() );
261                 mlt_frame frame = mlt_cache_get_frame( m_cache, position );
262
263                 // Allow the buffer to fill to the requested initial buffer level.
264                 if ( m_isBuffering )
265                 {
266                         int prefill = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "prefill" );
267                         int buffer = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "buffer" );
268
269                         m_isBuffering = false;
270                         prefill = prefill > buffer ? buffer : prefill;
271                         pthread_mutex_lock( &m_mutex );
272                         while ( mlt_deque_count( m_queue ) < prefill )
273                         {
274                                 // Wait up to buffer/fps seconds
275                                 gettimeofday( &now, NULL );
276                                 long usec = now.tv_sec * 1000000 + now.tv_usec;
277                                 usec += 1000000 * buffer / fps;
278                                 tm.tv_sec = usec / 1000000;
279                                 tm.tv_nsec = (usec % 1000000) * 1000;
280                                 if ( pthread_cond_timedwait( &m_condition, &m_mutex, &tm ) )
281                                         break;
282                         }
283                         pthread_mutex_unlock( &m_mutex );
284                 }
285
286                 if ( !frame )
287                 {
288                         // Wait if queue is empty
289                         pthread_mutex_lock( &m_mutex );
290                         while ( mlt_deque_count( m_queue ) < 1 )
291                         {
292                                 // Wait up to twice frame duration
293                                 gettimeofday( &now, NULL );
294                                 long usec = now.tv_sec * 1000000 + now.tv_usec;
295                                 usec += 2000000 / fps;
296                                 tm.tv_sec = usec / 1000000;
297                                 tm.tv_nsec = (usec % 1000000) * 1000;
298                                 if ( pthread_cond_timedwait( &m_condition, &m_mutex, &tm ) )
299                                         // Stop waiting if error (timed out)
300                                         break;
301                         }
302                         frame = ( mlt_frame ) mlt_deque_pop_front( m_queue );
303                         pthread_mutex_unlock( &m_mutex );
304
305                         // add to cache
306                         if ( frame )
307                         {
308                                 mlt_frame_set_position( frame, position );
309                                 mlt_cache_put_frame( m_cache, frame );
310                         }
311                 }
312
313                 // Set frame timestamp and properties
314                 if ( frame )
315                 {
316                         mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
317                         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
318                         mlt_properties_set_int( properties, "progressive", profile->progressive );
319                         mlt_properties_set_int( properties, "meta.media.progressive", profile->progressive );
320                         mlt_properties_set_int( properties, "top_field_first", m_topFieldFirst );
321                         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
322                         mlt_properties_set_int( properties, "meta.media.sample_aspect_num", profile->sample_aspect_num );
323                         mlt_properties_set_int( properties, "meta.media.sample_aspect_den", profile->sample_aspect_den );
324                         mlt_properties_set_int( properties, "meta.media.frame_rate_num", profile->frame_rate_num );
325                         mlt_properties_set_int( properties, "meta.media.frame_rate_den", profile->frame_rate_den );
326                         mlt_properties_set_int( properties, "width", profile->width );
327                         mlt_properties_set_int( properties, "meta.media.width", profile->width );
328                         mlt_properties_set_int( properties, "height", profile->height );
329                         mlt_properties_set_int( properties, "meta.media.height", profile->height );
330                         mlt_properties_set_int( properties, "format", mlt_image_yuv422 );
331                         mlt_properties_set_int( properties, "colorspace", m_colorspace );
332                         mlt_properties_set_int( properties, "meta.media.colorspace", m_colorspace );
333                         mlt_properties_set_int( properties, "audio_frequency", 48000 );
334                         mlt_properties_set_int( properties, "audio_channels",
335                                 mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" ) );
336                 }
337                 else
338                         mlt_log_warning( getProducer(), "buffer underrun\n" );
339
340                 return frame;
341         }
342
343         // *** DeckLink API implementation of IDeckLinkInputCallback *** //
344
345         // IUnknown needs only a dummy implementation
346         virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID iid, LPVOID *ppv )
347                 { return E_NOINTERFACE; }
348         virtual ULONG STDMETHODCALLTYPE AddRef()
349                 { return 1; }
350         virtual ULONG STDMETHODCALLTYPE Release()
351                 { return 1; }
352
353         /************************* DeckLink API Delegate Methods *****************************/
354
355         virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(
356                         IDeckLinkVideoInputFrame* video,
357                         IDeckLinkAudioInputPacket* audio )
358         {
359                 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "preview" ) &&
360                         mlt_producer_get_speed( getProducer() ) == 0.0 && !mlt_deque_count( m_queue ))
361                 {
362                         pthread_cond_broadcast( &m_condition );
363                         return S_OK;
364                 }
365
366                 // Create mlt_frame
367                 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( getProducer() ) );
368
369                 // Copy video
370                 if ( video )
371                 {
372                         if ( !( video->GetFlags() & bmdFrameHasNoInputSource ) )
373                         {
374                                 int size = video->GetRowBytes() * ( video->GetHeight() + m_vancLines );
375                                 void* image = mlt_pool_alloc( size );
376                                 void* buffer = 0;
377                                 unsigned char* p = (unsigned char*) image;
378                                 int n = size / 2;
379 \
380                                 // Initialize VANC lines to nominal black
381                                 while ( --n )
382                                 {
383                                         *p ++ = 16;
384                                         *p ++ = 128;
385                                 }
386
387                                 // Capture VANC
388                                 if ( m_vancLines > 0 )
389                                 {
390                                         IDeckLinkVideoFrameAncillary* vanc = 0;
391                                         if ( video->GetAncillaryData( &vanc ) == S_OK && vanc )
392                                         {
393                                                 for ( int i = 1; i < m_vancLines + 1; i++ )
394                                                 {
395                                                         if ( vanc->GetBufferForVerticalBlankingLine( i, &buffer ) == S_OK )
396                                                                 swab( (char*) buffer, (char*) image + ( i - 1 ) * video->GetRowBytes(), video->GetRowBytes() );
397                                                         else
398                                                                 mlt_log_debug( getProducer(), "failed capture vanc line %d\n", i );
399                                                 }
400                                                 SAFE_RELEASE(vanc);
401                                         }
402                                 }
403
404                                 // Capture image
405                                 video->GetBytes( &buffer );
406                                 if ( image && buffer )
407                                 {
408                                         size =  video->GetRowBytes() * video->GetHeight();
409                                         swab( (char*) buffer, (char*) image + m_vancLines * video->GetRowBytes(), size );
410                                         mlt_frame_set_image( frame, (uint8_t*) image, size, mlt_pool_release );
411                                 }
412                                 else if ( image )
413                                 {
414                                         mlt_log_verbose( getProducer(), "no video\n" );
415                                         mlt_pool_release( image );
416                                 }
417                         }
418                         else
419                         {
420                                 mlt_log_verbose( getProducer(), "no signal\n" );
421                                 mlt_frame_close( frame );
422                                 frame = 0;
423                         }
424
425                         // Get timecode
426                         IDeckLinkTimecode* timecode = 0;
427                         if ( video->GetTimecode( bmdTimecodeVITC, &timecode ) == S_OK && timecode )
428                         {
429                                 DLString timecodeString = 0;
430
431                                 if ( timecode->GetString( &timecodeString ) == S_OK )
432                                 {
433                                         char* s = getCString( timecodeString );
434                                         mlt_properties_set( MLT_FRAME_PROPERTIES( frame ), "meta.attr.vitc.markup", s );
435                                         mlt_log_debug( getProducer(), "timecode %s\n", s );
436                                         freeCString( s );
437                                 }
438                                 freeDLString( timecodeString );
439                                 SAFE_RELEASE( timecode );
440                         }
441                 }
442                 else
443                 {
444                         mlt_log_verbose( getProducer(), "no video\n" );
445                         mlt_frame_close( frame );
446                         frame = 0;
447                 }
448
449                 // Copy audio
450                 if ( frame && audio )
451                 {
452                         int channels = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" );
453                         int size = audio->GetSampleFrameCount() * channels * sizeof(int16_t);
454                         mlt_audio_format format = mlt_audio_s16;
455                         void* pcm = mlt_pool_alloc( size );
456                         void* buffer = 0;
457
458                         audio->GetBytes( &buffer );
459                         if ( buffer )
460                         {
461                                 memcpy( pcm, buffer, size );
462                                 mlt_frame_set_audio( frame, pcm, format, size, mlt_pool_release );
463                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(frame), "audio_samples", audio->GetSampleFrameCount() );
464                         }
465                         else
466                         {
467                                 mlt_log_verbose( getProducer(), "no audio\n" );
468                                 mlt_pool_release( pcm );
469                         }
470                 }
471                 else
472                 {
473                         mlt_log_verbose( getProducer(), "no audio\n" );
474                 }
475
476                 // Put frame in queue
477                 if ( frame )
478                 {
479                         int queueMax = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "buffer" );
480                         pthread_mutex_lock( &m_mutex );
481                         if ( mlt_deque_count( m_queue ) < queueMax )
482                         {
483                                 mlt_deque_push_back( m_queue, frame );
484                                 pthread_cond_broadcast( &m_condition );
485                         }
486                         else
487                         {
488                                 mlt_frame_close( frame );
489                                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "dropped", ++m_dropped );
490                                 mlt_log_warning( getProducer(), "frame dropped %d\n", m_dropped );
491                         }
492                         pthread_mutex_unlock( &m_mutex );
493                 }
494
495                 return S_OK;
496         }
497
498         virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(
499                         BMDVideoInputFormatChangedEvents events,
500                         IDeckLinkDisplayMode* mode,
501                         BMDDetectedVideoInputFormatFlags flags )
502         {
503                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
504                 if ( events & bmdVideoInputDisplayModeChanged )
505                 {
506                         BMDTimeValue duration;
507                         BMDTimeScale timescale;
508                         mode->GetFrameRate( &duration, &timescale );
509                         profile->width = mode->GetWidth();
510                         profile->height = mode->GetHeight() + m_vancLines;
511                         profile->frame_rate_num = timescale;
512                         profile->frame_rate_den = duration;
513                         if ( profile->width == 720 )
514                         {
515                                 if ( profile->height == 576 )
516                                 {
517                                         profile->sample_aspect_num = 16;
518                                         profile->sample_aspect_den = 15;
519                                 }
520                                 else
521                                 {
522                                         profile->sample_aspect_num = 8;
523                                         profile->sample_aspect_den = 9;
524                                 }
525                                 profile->display_aspect_num = 4;
526                                 profile->display_aspect_den = 3;
527                         }
528                         else
529                         {
530                                 profile->sample_aspect_num = 1;
531                                 profile->sample_aspect_den = 1;
532                                 profile->display_aspect_num = 16;
533                                 profile->display_aspect_den = 9;
534                         }
535                         free( profile->description );
536                         profile->description = strdup( "decklink" );
537                         mlt_log_verbose( getProducer(), "format changed %dx%d %.3f fps\n",
538                                 profile->width, profile->height, (double) profile->frame_rate_num / profile->frame_rate_den );
539                 }
540                 if ( events & bmdVideoInputFieldDominanceChanged )
541                 {
542                         profile->progressive = mode->GetFieldDominance() == bmdProgressiveFrame;
543                         m_topFieldFirst = mode->GetFieldDominance() == bmdUpperFieldFirst;
544                         mlt_log_verbose( getProducer(), "field dominance changed prog %d tff %d\n",
545                                 profile->progressive, m_topFieldFirst );
546                 }
547                 if ( events & bmdVideoInputColorspaceChanged )
548                 {
549                         profile->colorspace = m_colorspace =
550                                 ( mode->GetFlags() & bmdDisplayModeColorspaceRec709 ) ? 709 : 601;
551                         mlt_log_verbose( getProducer(), "colorspace changed %d\n", profile->colorspace );
552                 }
553                 return S_OK;
554         }
555 };
556
557 static int get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
558 {
559         return mlt_frame_get_audio( frame, (void**) buffer, format, frequency, channels, samples );
560 }
561
562 static int get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
563 {
564         return mlt_frame_get_image( frame, buffer, format, width, height, writable );
565 }
566
567 static int get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
568 {
569         DeckLinkProducer* decklink = (DeckLinkProducer*) producer->child;
570         mlt_position pos = mlt_producer_position( producer );
571         mlt_position end = mlt_producer_get_playtime( producer );
572         end = ( mlt_producer_get_length( producer ) < end ? mlt_producer_get_length( producer ) : end ) - 1;
573
574         // Re-open if needed
575         if ( !decklink && pos < end )
576         {
577                 producer->child = decklink = new DeckLinkProducer();
578                 decklink->setProducer( producer );
579                 decklink->open( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES(producer), "resource" ) );
580         }
581
582         // Start if needed
583         if ( decklink )
584         {
585                 decklink->start( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) );
586
587                 // Get the next frame from the decklink object
588                 if ( ( *frame = decklink->getFrame() ))
589                 {
590                         // Add audio and video getters
591                         mlt_frame_push_audio( *frame, (void*) get_audio );
592                         mlt_frame_push_get_image( *frame, get_image );
593                 }
594         }
595         if ( !*frame )
596                 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE(producer) );
597
598         // Calculate the next timecode
599         mlt_producer_prepare_next( producer );
600
601         // Close DeckLink if at end
602         if ( pos >= end && decklink )
603         {
604                 decklink->stop();
605                 delete decklink;
606                 producer->child = NULL;
607         }
608
609         return 0;
610 }
611
612 static void producer_close( mlt_producer producer )
613 {
614         delete (DeckLinkProducer*) producer->child;
615         producer->close = NULL;
616         mlt_producer_close( producer );
617 }
618
619 extern "C" {
620
621 // Listen for the list_devices property to be set
622 static void on_property_changed( void*, mlt_properties properties, const char *name )
623 {
624         IDeckLinkIterator* decklinkIterator = NULL;
625         IDeckLink* decklink = NULL;
626         IDeckLinkInput* decklinkInput = NULL;
627         int i = 0;
628
629         if ( name && !strcmp( name, "list_devices" ) )
630                 mlt_event_block( (mlt_event) mlt_properties_get_data( properties, "list-devices-event", NULL ) );
631         else
632                 return;
633
634 #ifdef WIN32
635         if ( FAILED( CoInitialize( NULL ) ) )
636                 return;
637         if ( FAILED( CoCreateInstance( CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**) &decklinkIterator ) ) )
638                 return;
639 #else
640         if ( !( decklinkIterator = CreateDeckLinkIteratorInstance() ) )
641                 return;
642 #endif
643         for ( ; decklinkIterator->Next( &decklink ) == S_OK; i++ )
644         {
645                 if ( decklink->QueryInterface( IID_IDeckLinkInput, (void**) &decklinkInput ) == S_OK )
646                 {
647                         DLString name = NULL;
648                         if ( decklink->GetModelName( &name ) == S_OK )
649                         {
650                                 char *name_cstr = getCString( name );
651                                 const char *format = "device.%d";
652                                 char *key = (char*) calloc( 1, strlen( format ) + 1 );
653
654                                 sprintf( key, format, i );
655                                 mlt_properties_set( properties, key, name_cstr );
656                                 free( key );
657                                 freeDLString( name );
658                                 freeCString( name_cstr );
659                         }
660                         SAFE_RELEASE( decklinkInput );
661                 }
662                 SAFE_RELEASE( decklink );
663         }
664         SAFE_RELEASE( decklinkIterator );
665         mlt_properties_set_int( properties, "devices", i );
666 }
667
668 /** Initialise the producer.
669  */
670
671 mlt_producer producer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
672 {
673         // Allocate the producer
674         DeckLinkProducer* decklink = new DeckLinkProducer();
675         mlt_producer producer = (mlt_producer) calloc( 1, sizeof( *producer ) );
676
677         // If allocated and initializes
678         if ( decklink && !mlt_producer_init( producer, decklink ) )
679         {
680                 if ( decklink->open( arg? atoi( arg ) : 0 ) )
681                 {
682                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
683
684                         // Close DeckLink and defer re-open to get_frame
685                         delete decklink;
686                         producer->child = NULL;
687
688                         // Set callbacks
689                         producer->close = (mlt_destructor) producer_close;
690                         producer->get_frame = get_frame;
691
692                         // Set properties
693                         mlt_properties_set( properties, "resource", (arg && strcmp( arg, ""))? arg : "0" );
694                         mlt_properties_set_int( properties, "channels", 2 );
695                         mlt_properties_set_int( properties, "buffer", 25 );
696                         mlt_properties_set_int( properties, "prefill", 25 );
697
698                         // These properties effectively make it infinite.
699                         mlt_properties_set_int( properties, "length", INT_MAX );
700                         mlt_properties_set_int( properties, "out", INT_MAX - 1 );
701                         mlt_properties_set( properties, "eof", "loop" );
702
703                         mlt_event event = mlt_events_listen( properties, properties, "property-changed", (mlt_listener) on_property_changed );
704                         mlt_properties_set_data( properties, "list-devices-event", event, 0, NULL, NULL );
705                 }
706         }
707
708         return producer;
709 }
710
711 }