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