2 * producer_decklink.c -- input from Blackmagic Design DeckLink
3 * Copyright (C) 2011 Dan Dennedy <dan@dennedy.org>
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.
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.
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
20 #include <framework/mlt.h>
29 class DeckLinkProducer
30 : public IDeckLinkInputCallback
33 mlt_producer m_producer;
34 IDeckLink* m_decklink;
35 IDeckLinkInput* m_decklinkInput;
37 pthread_mutex_t m_mutex;
38 pthread_cond_t m_condition;
47 BMDDisplayMode getDisplayMode( mlt_profile profile, int vancLines )
49 IDeckLinkDisplayModeIterator* iter = NULL;
50 IDeckLinkDisplayMode* mode = NULL;
51 BMDDisplayMode result = (BMDDisplayMode) bmdDisplayModeNotSupported;
53 if ( m_decklinkInput->GetDisplayModeIterator( &iter ) == S_OK )
55 while ( !result && iter->Next( &mode ) == S_OK )
57 int width = mode->GetWidth();
58 int height = mode->GetHeight();
59 BMDTimeValue duration;
60 BMDTimeScale timescale;
61 mode->GetFrameRate( &duration, ×cale );
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 );
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();
82 void setProducer( mlt_producer producer )
83 { m_producer = producer; }
85 mlt_producer getProducer() const
86 { return m_producer; }
92 m_decklinkInput = NULL;
100 mlt_deque_close( m_queue );
101 pthread_mutex_destroy( &m_mutex );
102 pthread_cond_destroy( &m_condition );
103 mlt_cache_close( m_cache );
105 SAFE_RELEASE( m_decklinkInput );
106 SAFE_RELEASE( m_decklink );
109 bool open( unsigned card = 0 )
111 IDeckLinkIterator* decklinkIterator = NULL;
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.";
122 decklinkIterator = CreateDeckLinkIteratorInstance();
123 if ( !decklinkIterator )
124 throw "The DeckLink drivers are not installed.";
126 // Connect to the Nth DeckLink instance
127 for ( unsigned i = 0; decklinkIterator->Next( &m_decklink ) == S_OK ; i++)
132 SAFE_RELEASE( m_decklink );
134 SAFE_RELEASE( decklinkIterator );
136 throw "DeckLink card not found.";
138 // Get the input interface
139 if ( m_decklink->QueryInterface( IID_IDeckLinkInput, (void**) &m_decklinkInput ) != S_OK )
140 throw "No DeckLink cards support input.";
142 // Provide this class as a delegate to the input callback
143 m_decklinkInput->SetCallback( this );
145 // Initialize other members
146 pthread_mutex_init( &m_mutex, NULL );
147 pthread_cond_init( &m_condition, NULL );
148 m_queue = mlt_deque_init();
151 m_isBuffering = true;
152 m_cache = mlt_cache_init();
154 // 3 covers YADIF and increasing framerate use cases
155 mlt_cache_set_size( m_cache, 3 );
157 catch ( const char *error )
159 SAFE_RELEASE( m_decklinkInput );
160 SAFE_RELEASE( m_decklink );
161 mlt_log_error( getProducer(), "%s\n", error );
167 bool start( mlt_profile profile = 0 )
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;
179 profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
181 // Get the display mode
182 BMDDisplayMode displayMode = getDisplayMode( profile, m_vancLines );
183 if ( displayMode == (BMDDisplayMode) bmdDisplayModeNotSupported )
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.";
190 // Determine if supports input format detection
192 BOOL doesDetectFormat = FALSE;
194 bool doesDetectFormat = false;
196 IDeckLinkAttributes *decklinkAttributes = 0;
197 if ( m_decklink->QueryInterface( IID_IDeckLinkAttributes, (void**) &decklinkAttributes ) == S_OK )
199 if ( decklinkAttributes->GetFlag( BMDDeckLinkSupportsInputFormatDetection, &doesDetectFormat ) != S_OK )
200 doesDetectFormat = false;
201 SAFE_RELEASE( decklinkAttributes );
203 mlt_log_verbose( getProducer(), "%s format detection\n", doesDetectFormat ? "supports" : "does not support" );
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.";
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.";
220 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "dropped", m_dropped );
221 m_started = m_decklinkInput->StartStreams() == S_OK;
223 throw "Failed to start capture.";
225 catch ( const char *error )
227 m_decklinkInput->DisableVideoInput();
228 mlt_log_error( getProducer(), "%s\n", error );
240 // Release the wait in getFrame
241 pthread_mutex_lock( &m_mutex );
242 pthread_cond_broadcast( &m_condition );
243 pthread_mutex_unlock( &m_mutex );
245 m_decklinkInput->StopStreams();
246 m_decklinkInput->DisableVideoInput();
247 m_decklinkInput->DisableAudioInput();
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 );
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 );
264 // Allow the buffer to fill to the requested initial buffer level.
267 int prefill = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "prefill" );
268 int buffer = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "buffer" );
270 m_isBuffering = false;
271 prefill = prefill > buffer ? buffer : prefill;
272 pthread_mutex_lock( &m_mutex );
273 while ( mlt_deque_count( m_queue ) < prefill )
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 ) )
284 pthread_mutex_unlock( &m_mutex );
289 // Wait if queue is empty
290 pthread_mutex_lock( &m_mutex );
291 while ( mlt_deque_count( m_queue ) < 1 )
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)
303 frame = ( mlt_frame ) mlt_deque_pop_front( m_queue );
304 pthread_mutex_unlock( &m_mutex );
309 mlt_frame_set_position( frame, position );
310 mlt_cache_put_frame( m_cache, frame );
314 // Set frame timestamp and properties
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" ) );
339 mlt_log_warning( getProducer(), "buffer underrun\n" );
344 // *** DeckLink API implementation of IDeckLinkInputCallback *** //
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()
351 virtual ULONG STDMETHODCALLTYPE Release()
354 /************************* DeckLink API Delegate Methods *****************************/
356 virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(
357 IDeckLinkVideoInputFrame* video,
358 IDeckLinkAudioInputPacket* audio )
360 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "preview" ) &&
361 mlt_producer_get_speed( getProducer() ) == 0.0 && !mlt_deque_count( m_queue ))
363 pthread_cond_broadcast( &m_condition );
368 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( getProducer() ) );
373 if ( !( video->GetFlags() & bmdFrameHasNoInputSource ) )
375 int size = video->GetRowBytes() * ( video->GetHeight() + m_vancLines );
376 void* image = mlt_pool_alloc( size );
378 unsigned char* p = (unsigned char*) image;
381 // Initialize VANC lines to nominal black
389 if ( m_vancLines > 0 )
391 IDeckLinkVideoFrameAncillary* vanc = 0;
392 if ( video->GetAncillaryData( &vanc ) == S_OK && vanc )
394 for ( int i = 1; i < m_vancLines + 1; i++ )
396 if ( vanc->GetBufferForVerticalBlankingLine( i, &buffer ) == S_OK )
397 swab( (char*) buffer, (char*) image + ( i - 1 ) * video->GetRowBytes(), video->GetRowBytes() );
399 mlt_log_debug( getProducer(), "failed capture vanc line %d\n", i );
406 video->GetBytes( &buffer );
407 if ( image && buffer )
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 );
415 mlt_log_verbose( getProducer(), "no video\n" );
416 mlt_pool_release( image );
421 mlt_log_verbose( getProducer(), "no signal\n" );
422 mlt_frame_close( frame );
427 IDeckLinkTimecode* timecode = 0;
428 if ( video->GetTimecode( bmdTimecodeVITC, &timecode ) == S_OK && timecode )
430 DLString timecodeString = 0;
432 if ( timecode->GetString( &timecodeString ) == S_OK )
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 );
439 freeDLString( timecodeString );
440 SAFE_RELEASE( timecode );
445 mlt_log_verbose( getProducer(), "no video\n" );
446 mlt_frame_close( frame );
451 if ( frame && audio )
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 );
459 audio->GetBytes( &buffer );
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() );
468 mlt_log_verbose( getProducer(), "no audio\n" );
469 mlt_pool_release( pcm );
474 mlt_log_verbose( getProducer(), "no audio\n" );
477 // Put frame in queue
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 )
484 mlt_deque_push_back( m_queue, frame );
485 pthread_cond_broadcast( &m_condition );
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 );
493 pthread_mutex_unlock( &m_mutex );
499 virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(
500 BMDVideoInputFormatChangedEvents events,
501 IDeckLinkDisplayMode* mode,
502 BMDDetectedVideoInputFormatFlags flags )
504 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
505 if ( events & bmdVideoInputDisplayModeChanged )
507 BMDTimeValue duration;
508 BMDTimeScale timescale;
509 mode->GetFrameRate( &duration, ×cale );
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 )
516 if ( profile->height == 576 )
518 profile->sample_aspect_num = 16;
519 profile->sample_aspect_den = 15;
523 profile->sample_aspect_num = 8;
524 profile->sample_aspect_den = 9;
526 profile->display_aspect_num = 4;
527 profile->display_aspect_den = 3;
531 profile->sample_aspect_num = 1;
532 profile->sample_aspect_den = 1;
533 profile->display_aspect_num = 16;
534 profile->display_aspect_den = 9;
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 );
541 if ( events & bmdVideoInputFieldDominanceChanged )
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 );
548 if ( events & bmdVideoInputColorspaceChanged )
550 profile->colorspace = m_colorspace =
551 ( mode->GetFlags() & bmdDisplayModeColorspaceRec709 ) ? 709 : 601;
552 mlt_log_verbose( getProducer(), "colorspace changed %d\n", profile->colorspace );
558 static int get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
560 return mlt_frame_get_audio( frame, (void**) buffer, format, frequency, channels, samples );
563 static int get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
565 return mlt_frame_get_image( frame, buffer, format, width, height, writable );
568 static int get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
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;
576 if ( !decklink && pos < end )
578 producer->child = decklink = new DeckLinkProducer();
579 decklink->setProducer( producer );
580 decklink->open( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES(producer), "resource" ) );
586 decklink->start( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) );
588 // Get the next frame from the decklink object
589 if ( ( *frame = decklink->getFrame() ))
591 // Add audio and video getters
592 mlt_frame_push_audio( *frame, (void*) get_audio );
593 mlt_frame_push_get_image( *frame, get_image );
597 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE(producer) );
599 // Calculate the next timecode
600 mlt_producer_prepare_next( producer );
602 // Close DeckLink if at end
603 if ( pos >= end && decklink )
607 producer->child = NULL;
613 static void producer_close( mlt_producer producer )
615 delete (DeckLinkProducer*) producer->child;
616 producer->close = NULL;
617 mlt_producer_close( producer );
622 // Listen for the list_devices property to be set
623 static void on_property_changed( void*, mlt_properties properties, const char *name )
625 IDeckLinkIterator* decklinkIterator = NULL;
626 IDeckLink* decklink = NULL;
627 IDeckLinkInput* decklinkInput = NULL;
630 if ( name && !strcmp( name, "list_devices" ) )
631 mlt_event_block( (mlt_event) mlt_properties_get_data( properties, "list-devices-event", NULL ) );
636 if ( FAILED( CoInitialize( NULL ) ) )
638 if ( FAILED( CoCreateInstance( CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**) &decklinkIterator ) ) )
641 if ( !( decklinkIterator = CreateDeckLinkIteratorInstance() ) )
644 for ( ; decklinkIterator->Next( &decklink ) == S_OK; i++ )
646 if ( decklink->QueryInterface( IID_IDeckLinkInput, (void**) &decklinkInput ) == S_OK )
648 DLString name = NULL;
649 if ( decklink->GetModelName( &name ) == S_OK )
651 char *name_cstr = getCString( name );
652 const char *format = "device.%d";
653 char *key = (char*) calloc( 1, strlen( format ) + 1 );
655 sprintf( key, format, i );
656 mlt_properties_set( properties, key, name_cstr );
658 freeDLString( name );
659 freeCString( name_cstr );
661 SAFE_RELEASE( decklinkInput );
663 SAFE_RELEASE( decklink );
665 SAFE_RELEASE( decklinkIterator );
666 mlt_properties_set_int( properties, "devices", i );
669 /** Initialise the producer.
672 mlt_producer producer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
674 // Allocate the producer
675 DeckLinkProducer* decklink = new DeckLinkProducer();
676 mlt_producer producer = (mlt_producer) calloc( 1, sizeof( *producer ) );
678 // If allocated and initializes
679 if ( decklink && !mlt_producer_init( producer, decklink ) )
681 if ( decklink->open( arg? atoi( arg ) : 0 ) )
683 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
685 // Close DeckLink and defer re-open to get_frame
687 producer->child = NULL;
690 producer->close = (mlt_destructor) producer_close;
691 producer->get_frame = get_frame;
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 );
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" );
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 );