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 #include "DeckLinkAPI_h.h"
31 #include "DeckLinkAPI.h"
34 #define SAFE_RELEASE(V) if (V) { V->Release(); V = NULL; }
36 class DeckLinkProducer
37 : public IDeckLinkInputCallback
40 mlt_producer m_producer;
41 IDeckLink* m_decklink;
42 IDeckLinkInput* m_decklinkInput;
44 pthread_mutex_t m_mutex;
45 pthread_cond_t m_condition;
54 BMDDisplayMode getDisplayMode( mlt_profile profile, int vancLines )
56 IDeckLinkDisplayModeIterator* iter = NULL;
57 IDeckLinkDisplayMode* mode = NULL;
58 BMDDisplayMode result = (BMDDisplayMode) bmdDisplayModeNotSupported;
60 if ( m_decklinkInput->GetDisplayModeIterator( &iter ) == S_OK )
62 while ( !result && iter->Next( &mode ) == S_OK )
64 int width = mode->GetWidth();
65 int height = mode->GetHeight();
66 BMDTimeValue duration;
67 BMDTimeScale timescale;
68 mode->GetFrameRate( &duration, ×cale );
69 double fps = (double) timescale / duration;
70 int p = mode->GetFieldDominance() == bmdProgressiveFrame;
71 m_topFieldFirst = mode->GetFieldDominance() == bmdUpperFieldFirst;
72 m_colorspace = ( mode->GetFlags() & bmdDisplayModeColorspaceRec709 ) ? 709 : 601;
73 mlt_log_verbose( getProducer(), "BMD mode %dx%d %.3f fps prog %d tff %d\n", width, height, fps, p, m_topFieldFirst );
75 if ( width == profile->width && p == profile->progressive
76 && ( height + vancLines == profile->height || ( height == 486 && profile->height == 480 + vancLines ) )
77 && fps == mlt_profile_fps( profile ) )
78 result = mode->GetDisplayMode();
89 void setProducer( mlt_producer producer )
90 { m_producer = producer; }
92 mlt_producer getProducer() const
93 { return m_producer; }
98 m_decklinkInput = NULL;
106 mlt_deque_close( m_queue );
107 pthread_mutex_destroy( &m_mutex );
108 pthread_cond_destroy( &m_condition );
109 mlt_cache_close( m_cache );
111 SAFE_RELEASE( m_decklinkInput );
112 SAFE_RELEASE( m_decklink );
115 bool open( unsigned card = 0 )
117 IDeckLinkIterator* decklinkIterator = NULL;
121 HRESULT result = CoInitialize( NULL );
122 if ( FAILED( result ) )
123 throw "COM initialization failed";
124 result = CoCreateInstance( CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**) &decklinkIterator );
125 if ( FAILED( result ) )
126 throw "The DeckLink drivers are not installed.";
128 decklinkIterator = CreateDeckLinkIteratorInstance();
129 if ( !decklinkIterator )
130 throw "The DeckLink drivers are not installed.";
132 // Connect to the Nth DeckLink instance
133 for ( unsigned i = 0; decklinkIterator->Next( &m_decklink ) == S_OK ; i++)
138 SAFE_RELEASE( m_decklink );
140 SAFE_RELEASE( decklinkIterator );
142 throw "DeckLink card not found.";
144 // Get the input interface
145 if ( m_decklink->QueryInterface( IID_IDeckLinkInput, (void**) &m_decklinkInput ) != S_OK )
146 throw "No DeckLink cards support input.";
148 // Provide this class as a delegate to the input callback
149 m_decklinkInput->SetCallback( this );
151 // Initialize other members
152 pthread_mutex_init( &m_mutex, NULL );
153 pthread_cond_init( &m_condition, NULL );
154 m_queue = mlt_deque_init();
157 m_isBuffering = true;
158 m_cache = mlt_cache_init();
160 // 3 covers YADIF and increasing framerate use cases
161 mlt_cache_set_size( m_cache, 3 );
163 catch ( const char *error )
165 SAFE_RELEASE( m_decklinkInput );
166 SAFE_RELEASE( m_decklink );
167 mlt_log_error( getProducer(), "%s\n", error );
173 bool start( mlt_profile profile = 0 )
179 // Initialize some members
180 m_vancLines = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "vanc" );
181 if ( m_vancLines == -1 )
182 m_vancLines = profile->height <= 512 ? 26 : 32;
185 profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
187 // Get the display mode
188 BMDDisplayMode displayMode = getDisplayMode( profile, m_vancLines );
189 if ( displayMode == (BMDDisplayMode) bmdDisplayModeNotSupported )
191 mlt_log_info( getProducer(), "profile = %dx%d %f fps %s\n", profile->width, profile->height,
192 mlt_profile_fps( profile ), profile->progressive? "progressive" : "interlace" );
193 throw "Profile is not compatible with decklink.";
196 // Determine if supports input format detection
198 BOOL doesDetectFormat = FALSE;
200 bool doesDetectFormat = false;
202 IDeckLinkAttributes *decklinkAttributes = 0;
203 if ( m_decklink->QueryInterface( IID_IDeckLinkAttributes, (void**) &decklinkAttributes ) == S_OK )
205 if ( decklinkAttributes->GetFlag( BMDDeckLinkSupportsInputFormatDetection, &doesDetectFormat ) != S_OK )
206 doesDetectFormat = false;
207 SAFE_RELEASE( decklinkAttributes );
209 mlt_log_verbose( getProducer(), "%s format detection\n", doesDetectFormat ? "supports" : "does not support" );
211 // Enable video capture
212 BMDPixelFormat pixelFormat = bmdFormat8BitYUV;
213 BMDVideoInputFlags flags = doesDetectFormat ? bmdVideoInputEnableFormatDetection : bmdVideoInputFlagDefault;
214 if ( S_OK != m_decklinkInput->EnableVideoInput( displayMode, pixelFormat, flags ) )
215 throw "Failed to enable video capture.";
217 // Enable audio capture
218 BMDAudioSampleRate sampleRate = bmdAudioSampleRate48kHz;
219 BMDAudioSampleType sampleType = bmdAudioSampleType16bitInteger;
220 int channels = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" );
221 if ( S_OK != m_decklinkInput->EnableAudioInput( sampleRate, sampleType, channels ) )
222 throw "Failed to enable audio capture.";
226 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "dropped", m_dropped );
227 m_started = m_decklinkInput->StartStreams() == S_OK;
229 throw "Failed to start capture.";
231 catch ( const char *error )
233 m_decklinkInput->DisableVideoInput();
234 mlt_log_error( getProducer(), "%s\n", error );
246 // Release the wait in getFrame
247 pthread_mutex_lock( &m_mutex );
248 pthread_cond_broadcast( &m_condition );
249 pthread_mutex_unlock( &m_mutex );
251 m_decklinkInput->StopStreams();
254 pthread_mutex_lock( &m_mutex );
255 while ( mlt_frame frame = (mlt_frame) mlt_deque_pop_back( m_queue ) )
256 mlt_frame_close( frame );
257 pthread_mutex_unlock( &m_mutex );
262 mlt_frame frame = NULL;
265 double fps = mlt_producer_get_fps( getProducer() );
266 mlt_position position = mlt_producer_position( getProducer() );
267 mlt_cache_item cached = mlt_cache_get( m_cache, (void*) position );
269 // Allow the buffer to fill to the requested initial buffer level.
272 int prefill = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "prefill" );
273 int buffer = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "buffer" );
275 m_isBuffering = false;
276 prefill = prefill > buffer ? buffer : prefill;
277 pthread_mutex_lock( &m_mutex );
278 while ( mlt_deque_count( m_queue ) < prefill )
280 // Wait up to buffer/fps seconds
281 gettimeofday( &now, NULL );
282 long usec = now.tv_sec * 1000000 + now.tv_usec;
283 usec += 1000000 * buffer / fps;
284 tm.tv_sec = usec / 1000000;
285 tm.tv_nsec = (usec % 1000000) * 1000;
286 if ( pthread_cond_timedwait( &m_condition, &m_mutex, &tm ) )
289 pthread_mutex_unlock( &m_mutex );
294 // Copy cached frame instead of pulling from queue
295 frame = mlt_frame_clone( (mlt_frame) mlt_cache_item_data( cached, NULL ), 0 );
296 mlt_cache_item_close( cached );
300 // Wait if queue is empty
301 pthread_mutex_lock( &m_mutex );
302 while ( mlt_deque_count( m_queue ) < 1 )
304 // Wait up to twice frame duration
305 gettimeofday( &now, NULL );
306 long usec = now.tv_sec * 1000000 + now.tv_usec;
307 usec += 2000000 / fps;
308 tm.tv_sec = usec / 1000000;
309 tm.tv_nsec = (usec % 1000000) * 1000;
310 if ( pthread_cond_timedwait( &m_condition, &m_mutex, &tm ) )
311 // Stop waiting if error (timed out)
314 frame = ( mlt_frame ) mlt_deque_pop_front( m_queue );
315 pthread_mutex_unlock( &m_mutex );
319 mlt_cache_put( m_cache, (void*) position, mlt_frame_clone( frame, 1 ), 0,
320 (mlt_destructor) mlt_frame_close );
323 // Set frame timestamp and properties
326 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
327 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
328 mlt_properties_set_int( properties, "progressive", profile->progressive );
329 mlt_properties_set_int( properties, "meta.media.progressive", profile->progressive );
330 mlt_properties_set_int( properties, "top_field_first", m_topFieldFirst );
331 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
332 mlt_properties_set_int( properties, "meta.media.sample_aspect_num", profile->sample_aspect_num );
333 mlt_properties_set_int( properties, "meta.media.sample_aspect_den", profile->sample_aspect_den );
334 mlt_properties_set_int( properties, "meta.media.frame_rate_num", profile->frame_rate_num );
335 mlt_properties_set_int( properties, "meta.media.frame_rate_den", profile->frame_rate_den );
336 mlt_properties_set_int( properties, "width", profile->width );
337 mlt_properties_set_int( properties, "meta.media.width", profile->width );
338 mlt_properties_set_int( properties, "height", profile->height );
339 mlt_properties_set_int( properties, "meta.media.height", profile->height );
340 mlt_properties_set_int( properties, "format", mlt_image_yuv422 );
341 mlt_properties_set_int( properties, "colorspace", m_colorspace );
342 mlt_properties_set_int( properties, "meta.media.colorspace", m_colorspace );
343 mlt_properties_set_int( properties, "audio_frequency", 48000 );
344 mlt_properties_set_int( properties, "audio_channels",
345 mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" ) );
348 mlt_log_warning( getProducer(), "buffer underrun\n" );
353 // *** DeckLink API implementation of IDeckLinkInputCallback *** //
355 // IUnknown needs only a dummy implementation
356 virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID iid, LPVOID *ppv )
357 { return E_NOINTERFACE; }
358 virtual ULONG STDMETHODCALLTYPE AddRef()
360 virtual ULONG STDMETHODCALLTYPE Release()
363 /************************* DeckLink API Delegate Methods *****************************/
365 virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(
366 IDeckLinkVideoInputFrame* video,
367 IDeckLinkAudioInputPacket* audio )
369 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "preview" ) &&
370 mlt_producer_get_speed( getProducer() ) == 0.0 && !mlt_deque_count( m_queue ))
372 pthread_cond_broadcast( &m_condition );
377 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( getProducer() ) );
382 if ( !( video->GetFlags() & bmdFrameHasNoInputSource ) )
384 int size = video->GetRowBytes() * ( video->GetHeight() + m_vancLines );
385 void* image = mlt_pool_alloc( size );
387 unsigned char* p = (unsigned char*) image;
390 // Initialize VANC lines to nominal black
398 if ( m_vancLines > 0 )
400 IDeckLinkVideoFrameAncillary* vanc = 0;
401 if ( video->GetAncillaryData( &vanc ) == S_OK && vanc )
403 for ( int i = 1; i < m_vancLines + 1; i++ )
405 if ( vanc->GetBufferForVerticalBlankingLine( i, &buffer ) == S_OK )
406 swab( (char*) buffer, (char*) image + ( i - 1 ) * video->GetRowBytes(), video->GetRowBytes() );
408 mlt_log_debug( getProducer(), "failed capture vanc line %d\n", i );
415 video->GetBytes( &buffer );
416 if ( image && buffer )
418 size = video->GetRowBytes() * video->GetHeight();
419 swab( (char*) buffer, (char*) image + m_vancLines * video->GetRowBytes(), size );
420 mlt_frame_set_image( frame, (uint8_t*) image, size, mlt_pool_release );
424 mlt_log_verbose( getProducer(), "no video\n" );
425 mlt_pool_release( image );
430 mlt_log_verbose( getProducer(), "no signal\n" );
431 mlt_frame_close( frame );
436 IDeckLinkTimecode* timecode = 0;
437 if ( video->GetTimecode( bmdTimecodeVITC, &timecode ) == S_OK && timecode )
439 const char* timecodeString = 0;
442 if ( timecode->GetString( (BSTR*) &timecodeString ) == S_OK )
444 if ( timecode->GetString( &timecodeString ) == S_OK )
447 mlt_properties_set( MLT_FRAME_PROPERTIES( frame ), "meta.attr.vitc.markup", timecodeString );
448 mlt_log_debug( getProducer(), "timecode %s\n", timecodeString );
450 if ( timecodeString )
451 free( (void*) timecodeString );
452 SAFE_RELEASE( timecode );
457 mlt_log_verbose( getProducer(), "no video\n" );
458 mlt_frame_close( frame );
463 if ( frame && audio )
465 int channels = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" );
466 int size = audio->GetSampleFrameCount() * channels * sizeof(int16_t);
467 mlt_audio_format format = mlt_audio_s16;
468 void* pcm = mlt_pool_alloc( size );
471 audio->GetBytes( &buffer );
474 memcpy( pcm, buffer, size );
475 mlt_frame_set_audio( frame, pcm, format, size, mlt_pool_release );
476 mlt_properties_set_int( MLT_FRAME_PROPERTIES(frame), "audio_samples", audio->GetSampleFrameCount() );
480 mlt_log_verbose( getProducer(), "no audio\n" );
481 mlt_pool_release( pcm );
486 mlt_log_verbose( getProducer(), "no audio\n" );
489 // Put frame in queue
492 int queueMax = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "buffer" );
493 pthread_mutex_lock( &m_mutex );
494 if ( mlt_deque_count( m_queue ) < queueMax )
496 mlt_deque_push_back( m_queue, frame );
497 pthread_cond_broadcast( &m_condition );
501 mlt_frame_close( frame );
502 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "dropped", ++m_dropped );
503 mlt_log_warning( getProducer(), "frame dropped %d\n", m_dropped );
505 pthread_mutex_unlock( &m_mutex );
511 virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(
512 BMDVideoInputFormatChangedEvents events,
513 IDeckLinkDisplayMode* mode,
514 BMDDetectedVideoInputFormatFlags flags )
516 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
517 if ( events & bmdVideoInputDisplayModeChanged )
519 BMDTimeValue duration;
520 BMDTimeScale timescale;
521 mode->GetFrameRate( &duration, ×cale );
522 profile->width = mode->GetWidth();
523 profile->height = mode->GetHeight() + m_vancLines;
524 profile->frame_rate_num = timescale;
525 profile->frame_rate_den = duration;
526 if ( profile->width == 720 )
528 if ( profile->height == 576 )
530 profile->sample_aspect_num = 16;
531 profile->sample_aspect_den = 15;
535 profile->sample_aspect_num = 8;
536 profile->sample_aspect_den = 9;
538 profile->display_aspect_num = 4;
539 profile->display_aspect_den = 3;
543 profile->sample_aspect_num = 1;
544 profile->sample_aspect_den = 1;
545 profile->display_aspect_num = 16;
546 profile->display_aspect_den = 9;
548 free( profile->description );
549 profile->description = strdup( "decklink" );
550 mlt_log_verbose( getProducer(), "format changed %dx%d %.3f fps\n",
551 profile->width, profile->height, (double) profile->frame_rate_num / profile->frame_rate_den );
553 if ( events & bmdVideoInputFieldDominanceChanged )
555 profile->progressive = mode->GetFieldDominance() == bmdProgressiveFrame;
556 m_topFieldFirst = mode->GetFieldDominance() == bmdUpperFieldFirst;
557 mlt_log_verbose( getProducer(), "field dominance changed prog %d tff %d\n",
558 profile->progressive, m_topFieldFirst );
560 if ( events & bmdVideoInputColorspaceChanged )
562 profile->colorspace = m_colorspace =
563 ( mode->GetFlags() & bmdDisplayModeColorspaceRec709 ) ? 709 : 601;
564 mlt_log_verbose( getProducer(), "colorspace changed %d\n", profile->colorspace );
570 static int get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
572 return mlt_frame_get_audio( frame, (void**) buffer, format, frequency, channels, samples );
575 static int get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
577 return mlt_frame_get_image( frame, buffer, format, width, height, writable );
580 static int get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
582 DeckLinkProducer* decklink = (DeckLinkProducer*) producer->child;
583 mlt_position pos = mlt_producer_position( producer );
584 mlt_position end = mlt_producer_get_playtime( producer );
585 end = ( mlt_producer_get_length( producer ) < end ? mlt_producer_get_length( producer ) : end ) - 1;
588 if ( !decklink && pos < end )
590 producer->child = decklink = new DeckLinkProducer();
591 decklink->setProducer( producer );
592 decklink->open( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES(producer), "resource" ) );
598 decklink->start( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) );
600 // Get the next frame from the decklink object
601 if ( ( *frame = decklink->getFrame() ))
603 // Add audio and video getters
604 mlt_frame_push_audio( *frame, (void*) get_audio );
605 mlt_frame_push_get_image( *frame, get_image );
609 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE(producer) );
611 // Calculate the next timecode
612 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
613 mlt_producer_prepare_next( producer );
615 // Close DeckLink if at end
616 if ( pos >= end && decklink )
620 producer->child = NULL;
626 static void producer_close( mlt_producer producer )
628 delete (DeckLinkProducer*) producer->child;
629 producer->close = NULL;
630 mlt_producer_close( producer );
635 // Listen for the list_devices property to be set
636 static void on_property_changed( void*, mlt_properties properties, const char *name )
638 IDeckLinkIterator* decklinkIterator = NULL;
639 IDeckLink* decklink = NULL;
640 IDeckLinkInput* decklinkInput = NULL;
643 if ( name && !strcmp( name, "list_devices" ) )
644 mlt_event_block( (mlt_event) mlt_properties_get_data( properties, "list-devices-event", NULL ) );
649 if ( FAILED( CoInitialize( NULL ) ) )
651 if ( FAILED( CoCreateInstance( CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**) &decklinkIterator ) ) )
654 if ( !( decklinkIterator = CreateDeckLinkIteratorInstance() ) )
657 for ( ; decklinkIterator->Next( &decklink ) == S_OK; i++ )
659 if ( decklink->QueryInterface( IID_IDeckLinkInput, (void**) &decklinkInput ) == S_OK )
662 if ( decklink->GetModelName( (const char**) &name ) == S_OK )
664 const char *format = "device.%d";
665 char *key = (char*) calloc( 1, strlen( format ) + 1 );
667 sprintf( key, format, i );
668 mlt_properties_set( properties, key, name );
672 SAFE_RELEASE( decklinkInput );
674 SAFE_RELEASE( decklink );
676 SAFE_RELEASE( decklinkIterator );
677 mlt_properties_set_int( properties, "devices", i );
680 /** Initialise the producer.
683 mlt_producer producer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
685 // Allocate the producer
686 DeckLinkProducer* decklink = new DeckLinkProducer();
687 mlt_producer producer = (mlt_producer) calloc( 1, sizeof( *producer ) );
689 // If allocated and initializes
690 if ( decklink && !mlt_producer_init( producer, decklink ) )
692 if ( decklink->open( arg? atoi( arg ) : 0 ) )
694 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
696 // Close DeckLink and defer re-open to get_frame
698 producer->child = NULL;
701 producer->close = (mlt_destructor) producer_close;
702 producer->get_frame = get_frame;
705 mlt_properties_set( properties, "resource", (arg && strcmp( arg, ""))? arg : "0" );
706 mlt_properties_set_int( properties, "channels", 2 );
707 mlt_properties_set_int( properties, "buffer", 25 );
708 mlt_properties_set_int( properties, "prefill", 25 );
710 // These properties effectively make it infinite.
711 mlt_properties_set_int( properties, "length", INT_MAX );
712 mlt_properties_set_int( properties, "out", INT_MAX - 1 );
713 mlt_properties_set( properties, "eof", "loop" );
715 mlt_event event = mlt_events_listen( properties, properties, "property-changed", (mlt_listener) on_property_changed );
716 mlt_properties_set_data( properties, "list-devices-event", event, 0, NULL, NULL );