2 * consumer_decklink.c -- output through Blackmagic Design DeckLink
3 * Copyright (C) 2010 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>
27 #include "DeckLinkAPI.h"
29 static const unsigned PREROLL_MINIMUM = 3;
36 pthread_mutex_t mutex;
39 static sample_fifo sample_fifo_init()
41 sample_fifo fifo = (sample_fifo) calloc( 1, sizeof( *fifo ) );
42 pthread_mutex_init( &fifo->mutex, NULL );
46 static void sample_fifo_append( sample_fifo fifo, int16_t *samples, int count )
48 pthread_mutex_lock( &fifo->mutex );
49 if ( ( fifo->size - fifo->used ) < count )
51 fifo->size += count * 5;
52 fifo->buffer = (int16_t*) realloc( fifo->buffer, fifo->size * sizeof( int16_t ) );
54 memcpy( fifo->buffer + fifo->used, samples, count * sizeof( int16_t ) );
56 pthread_mutex_unlock( &fifo->mutex );
59 static void sample_fifo_remove( sample_fifo fifo, int count )
61 pthread_mutex_lock( &fifo->mutex );
62 if ( count > fifo->used )
65 memmove( fifo->buffer, fifo->buffer + count, fifo->used * sizeof( int16_t ) );
66 pthread_mutex_unlock( &fifo->mutex );
69 static void sample_fifo_close( sample_fifo fifo )
72 pthread_mutex_destroy( &fifo->mutex );
77 class DeckLinkConsumer
78 : public IDeckLinkVideoOutputCallback
79 , public IDeckLinkAudioOutputCallback
82 mlt_consumer_s m_consumer;
83 IDeckLink* m_deckLink;
84 IDeckLinkOutput* m_deckLinkOutput;
85 IDeckLinkDisplayMode* m_displayMode;
86 pthread_mutex_t m_mutex;
87 pthread_cond_t m_condition;
90 BMDTimeValue m_duration;
91 BMDTimeScale m_timescale;
97 unsigned m_prerollCounter;
99 uint32_t m_maxAudioBuffer;
100 mlt_deque m_videoFrameQ;
105 IDeckLinkKeyer* m_deckLinkKeyer;
107 IDeckLinkDisplayMode* getDisplayMode()
109 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( getConsumer() ) );
110 IDeckLinkDisplayModeIterator* iter;
111 IDeckLinkDisplayMode* mode;
112 IDeckLinkDisplayMode* result = 0;
114 if ( m_deckLinkOutput->GetDisplayModeIterator( &iter ) == S_OK )
116 while ( !result && iter->Next( &mode ) == S_OK )
118 m_width = mode->GetWidth();
119 m_height = mode->GetHeight();
120 mode->GetFrameRate( &m_duration, &m_timescale );
121 m_fps = (double) m_timescale / m_duration;
122 int p = mode->GetFieldDominance() == bmdProgressiveFrame;
123 mlt_log_verbose( getConsumer(), "BMD mode %dx%d %.3f fps prog %d\n", m_width, m_height, m_fps, p );
125 if ( m_width == profile->width && m_height == profile->height && p == profile->progressive
126 && m_fps == mlt_profile_fps( profile ) )
135 mlt_consumer getConsumer()
136 { return &m_consumer; }
137 uint64_t isBuffering() const
138 { return m_prerollCounter < m_preroll; }
142 if ( m_deckLinkKeyer )
143 m_deckLinkKeyer->Release();
144 if ( m_deckLinkOutput )
145 m_deckLinkOutput->Release();
147 m_deckLink->Release();
150 mlt_deque_close( m_videoFrameQ );
151 pthread_mutex_destroy( &m_mutex );
152 pthread_cond_destroy( &m_condition );
156 bool open( unsigned card = 0 )
158 IDeckLinkIterator* deckLinkIterator = CreateDeckLinkIteratorInstance();
161 if ( !deckLinkIterator )
163 mlt_log_error( getConsumer(), "The DeckLink drivers not installed.\n" );
167 // Connect to the Nth DeckLink instance
169 if ( deckLinkIterator->Next( &m_deckLink ) != S_OK )
171 mlt_log_error( getConsumer(), "DeckLink card not found\n" );
172 deckLinkIterator->Release();
175 } while ( ++i <= card );
176 deckLinkIterator->Release();
178 // Obtain the audio/video output interface (IDeckLinkOutput)
179 if ( m_deckLink->QueryInterface( IID_IDeckLinkOutput, (void**)&m_deckLinkOutput ) != S_OK )
181 mlt_log_error( getConsumer(), "No DeckLink cards support output\n" );
182 m_deckLink->Release();
187 // Get the keyer interface
188 IDeckLinkAttributes *deckLinkAttributes = 0;
190 if ( m_deckLink->QueryInterface( IID_IDeckLinkAttributes, (void**) &deckLinkAttributes ) == S_OK )
193 if ( deckLinkAttributes->GetFlag( BMDDeckLinkSupportsInternalKeying, &flag ) == S_OK && flag )
195 if ( m_deckLink->QueryInterface( IID_IDeckLinkKeyer, (void**) &m_deckLinkKeyer ) != S_OK )
197 mlt_log_error( getConsumer(), "Failed to get keyer\n" );
198 m_deckLinkOutput->Release();
199 m_deckLinkOutput = 0;
200 m_deckLink->Release();
205 deckLinkAttributes->Release();
208 // Provide this class as a delegate to the audio and video output interfaces
209 m_deckLinkOutput->SetScheduledFrameCompletionCallback( this );
210 m_deckLinkOutput->SetAudioCallback( this );
212 pthread_mutex_init( &m_mutex, NULL );
213 pthread_cond_init( &m_condition, NULL );
214 m_maxAudioBuffer = bmdAudioSampleRate48kHz;
215 m_videoFrameQ = mlt_deque_init();
220 bool start( unsigned preroll )
222 mlt_properties properties = MLT_CONSUMER_PROPERTIES( getConsumer() );
224 // Initialize members
228 m_isPrerolling = true;
229 m_prerollCounter = 0;
230 m_preroll = preroll < PREROLL_MINIMUM ? PREROLL_MINIMUM : preroll;
231 m_channels = mlt_properties_get_int( properties, "channels" );
232 m_isAudio = !mlt_properties_get_int( properties, "audio_off" );
234 m_displayMode = getDisplayMode();
235 if ( !m_displayMode )
237 mlt_log_error( getConsumer(), "Profile is not compatible with decklink.\n" );
242 if ( m_deckLinkKeyer && ( m_isKeyer = mlt_properties_get_int( properties, "keyer" ) ) )
244 bool external = false;
245 double level = mlt_properties_get_double( properties, "keyer_level" );
247 if ( m_deckLinkKeyer->Enable( external ) != S_OK )
248 mlt_log_error( getConsumer(), "Failed to enable keyer\n" );
249 m_deckLinkKeyer->SetLevel( level <= 1 ? ( level > 0 ? 255 * level : 255 ) : 255 );
253 else if ( m_deckLinkKeyer )
255 m_deckLinkKeyer->Disable();
258 // Set the video output mode
259 if ( S_OK != m_deckLinkOutput->EnableVideoOutput( m_displayMode->GetDisplayMode(), bmdVideoOutputFlagDefault) )
261 mlt_log_error( getConsumer(), "Failed to enable video output\n" );
265 // Set the audio output mode
268 m_deckLinkOutput->DisableAudioOutput();
271 if ( S_OK != m_deckLinkOutput->EnableAudioOutput( bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
272 m_channels, bmdAudioOutputStreamContinuous ) )
274 mlt_log_error( getConsumer(), "Failed to enable audio output\n" );
278 m_fifo = sample_fifo_init();
279 m_deckLinkOutput->BeginAudioPreroll();
286 pthread_mutex_lock( &m_mutex );
287 pthread_cond_broadcast( &m_condition );
288 pthread_mutex_unlock( &m_mutex );
296 gettimeofday( &tv, NULL );
297 ts.tv_sec = tv.tv_sec + 1;
298 ts.tv_nsec = tv.tv_usec * 1000;
299 pthread_mutex_lock( &m_mutex );
300 pthread_cond_timedwait( &m_condition, &m_mutex, &ts );
301 pthread_mutex_unlock( &m_mutex );
306 // Stop the audio and video output streams immediately
307 if ( m_deckLinkOutput )
309 m_deckLinkOutput->StopScheduledPlayback( 0, 0, 0 );
310 m_deckLinkOutput->DisableAudioOutput();
311 m_deckLinkOutput->DisableVideoOutput();
313 while ( mlt_deque_count( m_videoFrameQ ) )
315 IDeckLinkMutableVideoFrame* frame = (IDeckLinkMutableVideoFrame*) mlt_deque_pop_back( m_videoFrameQ );
318 if ( m_fifo ) sample_fifo_close( m_fifo );
319 mlt_frame_close( m_frame );
322 void renderAudio( mlt_frame frame )
324 mlt_audio_format format = mlt_audio_s16;
325 int frequency = bmdAudioSampleRate48kHz;
326 int samples = mlt_sample_calculator( m_fps, frequency, m_count );
329 if ( !mlt_frame_get_audio( frame, (void**) &pcm, &format, &frequency, &m_channels, &samples ) )
333 if ( !m_isPrerolling )
335 uint32_t audioCount = 0;
336 uint32_t videoCount = 0;
339 m_deckLinkOutput->GetBufferedAudioSampleFrameCount( &audioCount );
340 m_deckLinkOutput->GetBufferedVideoFrameCount( &videoCount );
342 // Underflow typically occurs during non-normal speed playback.
343 if ( audioCount < 1 || videoCount < 1 )
345 // Upon switching to normal playback, buffer some frames faster than realtime.
346 mlt_log_info( getConsumer(), "buffer underrun: audio buf %u video buf %u frames\n", audioCount, videoCount );
347 m_prerollCounter = 0;
353 // Only append audio to reach the ideal level and not overbuffer.
354 int ideal = ( m_preroll - 1 ) * bmdAudioSampleRate48kHz / m_fps;
355 int actual = m_fifo->used / m_channels + audioCount;
356 int diff = ideal / 2 - actual;
357 count = diff < 0 ? 0 : diff < count ? diff : count;
361 sample_fifo_append( m_fifo, pcm, count * m_channels );
367 BMDPixelFormat format = m_isKeyer? bmdFormat8BitARGB : bmdFormat8BitYUV;
368 IDeckLinkMutableVideoFrame* frame = 0;
370 int stride = m_width * ( m_isKeyer? 4 : 2 );
372 // Generate a DeckLink video frame
373 if ( S_OK != m_deckLinkOutput->CreateVideoFrame( m_width, m_height,
374 stride, format, bmdFrameFlagDefault, &frame ) )
376 mlt_log_verbose( getConsumer(), "Failed to create video frame\n" );
381 // Make the first line black for field order correction.
382 if ( S_OK == frame->GetBytes( (void**) &buffer ) && buffer )
386 memset( buffer, 0, stride );
388 else for ( int i = 0; i < m_width; i++ )
394 mlt_log_debug( getConsumer(), "created video frame\n" );
395 mlt_deque_push_back( m_videoFrameQ, frame );
402 mlt_image_format format = m_isKeyer? mlt_image_rgb24a : mlt_image_yuv422;
405 if ( !mlt_frame_get_image( m_frame, &image, &format, &m_width, &m_height, 0 ) )
407 IDeckLinkMutableVideoFrame* decklinkFrame = (IDeckLinkMutableVideoFrame*) mlt_deque_pop_back( m_videoFrameQ );
409 int stride = m_width * ( m_isKeyer? 4 : 2 );
411 decklinkFrame->GetBytes( (void**) &buffer );
414 int progressive = mlt_properties_get_int( MLT_FRAME_PROPERTIES( m_frame ), "progressive" );
418 // Normal non-keyer playout - needs byte swapping
419 if ( !progressive && m_displayMode->GetFieldDominance() == bmdUpperFieldFirst )
420 // convert lower field first to top field first
421 swab( image, buffer + stride, stride * ( m_height - 1 ) );
423 swab( image, buffer, stride * m_height );
425 else if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES( m_frame ), "test_image" ) )
427 // Normal keyer output
428 int y = m_height + 1;
429 uint32_t* s = (uint32_t*) image;
430 uint32_t* d = (uint32_t*) buffer;
432 if ( !progressive && m_displayMode->GetFieldDominance() == bmdUpperFieldFirst )
434 // Correct field order
439 // Need to relocate alpha channel RGBA => ARGB
445 *d++ = ( *s << 8 ) | ( *s >> 24 );
452 // Keying blank frames - nullify alpha
453 memset( buffer, 0, stride * m_height );
455 m_deckLinkOutput->ScheduleVideoFrame( decklinkFrame, m_count * m_duration, m_duration, m_timescale );
457 mlt_deque_push_front( m_videoFrameQ, decklinkFrame );
461 HRESULT render( mlt_frame frame )
463 HRESULT result = S_OK;
466 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES(frame), "_speed" );
467 if ( m_isAudio && speed == 1.0 )
468 renderAudio( frame );
470 // Create video frames while pre-rolling
471 if ( m_isPrerolling )
473 if ( !createFrame() )
475 mlt_log_error( getConsumer(), "failed to create video frame\n" );
480 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered") )
482 // Close the previous frame and use the new one
483 mlt_frame_close( m_frame );
490 // Reuse the last frame
491 mlt_log_verbose( getConsumer(), "dropped video frame %u\n", ++m_dropped );
498 // Check for end of pre-roll
499 if ( ++m_prerollCounter > m_preroll && m_isPrerolling )
501 // Start audio and video output
503 m_deckLinkOutput->EndAudioPreroll();
504 m_deckLinkOutput->StartScheduledPlayback( 0, m_timescale, 1.0 );
505 m_isPrerolling = false;
511 // *** DeckLink API implementation of IDeckLinkVideoOutputCallback IDeckLinkAudioOutputCallback *** //
513 // IUnknown needs only a dummy implementation
514 virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID iid, LPVOID *ppv )
515 { return E_NOINTERFACE; }
516 virtual ULONG STDMETHODCALLTYPE AddRef()
518 virtual ULONG STDMETHODCALLTYPE Release()
521 /************************* DeckLink API Delegate Methods *****************************/
523 virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted( IDeckLinkVideoFrame* completedFrame, BMDOutputFrameCompletionResult completed )
525 // When a video frame has been released by the API, schedule another video frame to be output
531 virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped()
533 return mlt_consumer_is_stopped( getConsumer() ) ? S_FALSE : S_OK;
536 virtual HRESULT STDMETHODCALLTYPE RenderAudioSamples( bool preroll )
538 // Provide more audio samples to the DeckLink API
539 HRESULT result = S_OK;
541 uint32_t count = m_fifo->used / m_channels;
542 uint32_t buffered = count;
545 // Stay under preferred buffer level
546 && ( S_OK == m_deckLinkOutput->GetBufferedAudioSampleFrameCount( &buffered ) )
547 && buffered < m_maxAudioBuffer )
549 uint32_t written = 0;
551 buffered = m_maxAudioBuffer - buffered;
552 count = buffered > count ? count : buffered;
553 result = m_deckLinkOutput->ScheduleAudioSamples( m_fifo->buffer, count, 0, 0, &written );
555 sample_fifo_remove( m_fifo, written * m_channels );
565 static void *run( void *arg )
567 // Map the argument to the object
568 DeckLinkConsumer* decklink = (DeckLinkConsumer*) arg;
569 mlt_consumer consumer = decklink->getConsumer();
571 // Get the properties
572 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
574 // Convenience functionality
575 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
579 mlt_frame frame = NULL;
581 // Loop while running
582 while ( !terminated && mlt_properties_get_int( properties, "running" ) )
585 if ( ( frame = mlt_consumer_rt_frame( consumer ) ) )
587 // Check for termination
588 if ( terminate_on_pause )
589 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
591 decklink->render( frame );
592 if ( !decklink->isBuffering() )
594 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
598 // Indicate that the consumer is stopped
600 mlt_properties_set_int( properties, "running", 0 );
601 mlt_consumer_stopped( consumer );
606 /** Start the consumer.
609 static int start( mlt_consumer consumer )
611 // Get the properties
612 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
613 DeckLinkConsumer* decklink = (DeckLinkConsumer*) consumer->child;
614 int result = decklink->start( mlt_properties_get_int( properties, "preroll" ) ) ? 0 : 1;
616 // Check that we're not already running
617 if ( !result && !mlt_properties_get_int( properties, "running" ) )
620 pthread_t *pthread = (pthread_t*) calloc( 1, sizeof( pthread_t ) );
622 // Assign the thread to properties
623 mlt_properties_set_data( properties, "pthread", pthread, sizeof( pthread_t ), free, NULL );
625 // Set the running state
626 mlt_properties_set_int( properties, "running", 1 );
627 mlt_properties_set_int( properties, "joined", 0 );
630 pthread_create( pthread, NULL, run, consumer->child );
635 /** Stop the consumer.
638 static int stop( mlt_consumer consumer )
640 // Get the properties
641 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
643 // Check that we're running
644 if ( !mlt_properties_get_int( properties, "joined" ) )
647 pthread_t *pthread = (pthread_t*) mlt_properties_get_data( properties, "pthread", NULL );
652 mlt_properties_set_int( properties, "running", 0 );
653 mlt_properties_set_int( properties, "joined", 1 );
655 // Wait for termination
656 pthread_join( *pthread, NULL );
663 /** Determine if the consumer is stopped.
666 static int is_stopped( mlt_consumer consumer )
668 // Get the properties
669 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
670 return !mlt_properties_get_int( properties, "running" );
673 /** Close the consumer.
676 static void close( mlt_consumer consumer )
679 mlt_consumer_stop( consumer );
682 consumer->close = NULL;
683 mlt_consumer_close( consumer );
686 delete (DeckLinkConsumer*) consumer->child;
691 /** Initialise the consumer.
694 mlt_consumer consumer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
696 // Allocate the consumer
697 DeckLinkConsumer* decklink = new DeckLinkConsumer();
698 mlt_consumer consumer = NULL;
701 if ( decklink && !mlt_consumer_init( decklink->getConsumer(), decklink, profile ) )
703 // If initialises without error
704 if ( decklink->open( arg? atoi(arg) : 0 ) )
706 consumer = decklink->getConsumer();
709 consumer->close = close;
710 consumer->start = start;
711 consumer->stop = stop;
712 consumer->is_stopped = is_stopped;
720 extern mlt_producer producer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
722 static mlt_properties metadata( mlt_service_type type, const char *id, void *data )
724 char file[ PATH_MAX ];
725 const char *service_type = NULL;
729 service_type = "consumer";
732 service_type = "producer";
737 snprintf( file, PATH_MAX, "%s/decklink/%s_%s.yml", mlt_environment( "MLT_DATA" ), service_type, id );
738 return mlt_properties_parse_yaml( file );
743 MLT_REGISTER( consumer_type, "decklink", consumer_decklink_init );
744 MLT_REGISTER( producer_type, "decklink", producer_decklink_init );
745 MLT_REGISTER_METADATA( consumer_type, "decklink", metadata, NULL );
746 MLT_REGISTER_METADATA( producer_type, "decklink", metadata, NULL );