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 #define __STDC_FORMAT_MACROS /* see inttypes.h */
21 #include <framework/mlt.h>
31 #include "DeckLinkAPI_h.h"
33 #include "DeckLinkAPI.h"
36 static const unsigned PREROLL_MINIMUM = 3;
38 class DeckLinkConsumer
39 : public IDeckLinkVideoOutputCallback
42 mlt_consumer_s m_consumer;
43 IDeckLink* m_deckLink;
44 IDeckLinkOutput* m_deckLinkOutput;
45 IDeckLinkDisplayMode* m_displayMode;
48 BMDTimeValue m_duration;
49 BMDTimeScale m_timescale;
54 IDeckLinkMutableVideoFrame* m_decklinkFrame;
57 IDeckLinkKeyer* m_deckLinkKeyer;
58 bool m_terminate_on_pause;
62 pthread_t m_prerollThread;
64 IDeckLinkDisplayMode* getDisplayMode()
66 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( getConsumer() ) );
67 IDeckLinkDisplayModeIterator* iter;
68 IDeckLinkDisplayMode* mode;
69 IDeckLinkDisplayMode* result = 0;
71 if ( m_deckLinkOutput->GetDisplayModeIterator( &iter ) == S_OK )
73 while ( !result && iter->Next( &mode ) == S_OK )
75 m_width = mode->GetWidth();
76 m_height = mode->GetHeight();
77 mode->GetFrameRate( &m_duration, &m_timescale );
78 m_fps = (double) m_timescale / m_duration;
79 int p = mode->GetFieldDominance() == bmdProgressiveFrame;
80 mlt_log_verbose( getConsumer(), "BMD mode %dx%d %.3f fps prog %d\n", m_width, m_height, m_fps, p );
82 if ( m_width == profile->width && p == profile->progressive
83 && m_fps == mlt_profile_fps( profile )
84 && ( m_height == profile->height || ( m_height == 486 && profile->height == 480 ) ) )
93 mlt_consumer getConsumer()
94 { return &m_consumer; }
98 if ( m_deckLinkKeyer )
99 m_deckLinkKeyer->Release();
100 if ( m_deckLinkOutput )
101 m_deckLinkOutput->Release();
103 m_deckLink->Release();
106 bool open( unsigned card = 0 )
110 IDeckLinkIterator* deckLinkIterator = NULL;
111 HRESULT result = CoInitialize( NULL );
112 if ( FAILED( result ) )
114 mlt_log_error( getConsumer(), "COM initialization failed\n" );
117 result = CoCreateInstance( CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**) &deckLinkIterator );
118 if ( FAILED( result ) )
120 mlt_log_error( getConsumer(), "The DeckLink drivers not installed.\n" );
124 IDeckLinkIterator* deckLinkIterator = CreateDeckLinkIteratorInstance();
126 if ( !deckLinkIterator )
128 mlt_log_error( getConsumer(), "The DeckLink drivers not installed.\n" );
133 // Connect to the Nth DeckLink instance
135 if ( deckLinkIterator->Next( &m_deckLink ) != S_OK )
137 mlt_log_error( getConsumer(), "DeckLink card not found\n" );
138 deckLinkIterator->Release();
141 } while ( ++i <= card );
142 deckLinkIterator->Release();
144 // Obtain the audio/video output interface (IDeckLinkOutput)
145 if ( m_deckLink->QueryInterface( IID_IDeckLinkOutput, (void**)&m_deckLinkOutput ) != S_OK )
147 mlt_log_error( getConsumer(), "No DeckLink cards support output\n" );
148 m_deckLink->Release();
153 // Get the keyer interface
154 IDeckLinkAttributes *deckLinkAttributes = 0;
156 if ( m_deckLink->QueryInterface( IID_IDeckLinkAttributes, (void**) &deckLinkAttributes ) == S_OK )
163 if ( deckLinkAttributes->GetFlag( BMDDeckLinkSupportsInternalKeying, &flag ) == S_OK && flag )
165 if ( m_deckLink->QueryInterface( IID_IDeckLinkKeyer, (void**) &m_deckLinkKeyer ) != S_OK )
167 mlt_log_error( getConsumer(), "Failed to get keyer\n" );
168 m_deckLinkOutput->Release();
169 m_deckLinkOutput = 0;
170 m_deckLink->Release();
175 deckLinkAttributes->Release();
178 // Provide this class as a delegate to the audio and video output interfaces
179 m_deckLinkOutput->SetScheduledFrameCompletionCallback( this );
185 void* preroll_thread()
188 for ( unsigned i = 0; i < m_preroll; i++ )
189 ScheduleNextFrame( true );
191 // start scheduled playback
192 m_deckLinkOutput->StartScheduledPlayback( 0, m_timescale, 1.0 );
197 static void* preroll_thread_proxy( void* arg )
199 DeckLinkConsumer* self = static_cast< DeckLinkConsumer* >( arg );
200 return self->preroll_thread();
203 bool start( unsigned preroll )
205 mlt_properties properties = MLT_CONSUMER_PROPERTIES( getConsumer() );
207 // Initialize members
210 m_decklinkFrame = NULL;
211 preroll = preroll < PREROLL_MINIMUM ? PREROLL_MINIMUM : preroll;
212 m_channels = mlt_properties_get_int( properties, "channels" );
213 m_isAudio = !mlt_properties_get_int( properties, "audio_off" );
214 m_terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
217 m_displayMode = getDisplayMode();
218 if ( !m_displayMode )
220 mlt_log_error( getConsumer(), "Profile is not compatible with decklink.\n" );
225 if ( m_deckLinkKeyer && ( m_isKeyer = mlt_properties_get_int( properties, "keyer" ) ) )
227 bool external = ( m_isKeyer == 2 );
228 double level = mlt_properties_get_double( properties, "keyer_level" );
230 if ( m_deckLinkKeyer->Enable( external ) != S_OK )
231 mlt_log_error( getConsumer(), "Failed to enable %s keyer\n",
232 external ? "external" : "internal" );
233 m_deckLinkKeyer->SetLevel( level <= 1 ? ( level > 0 ? 255 * level : 255 ) : 255 );
235 else if ( m_deckLinkKeyer )
237 m_deckLinkKeyer->Disable();
240 // Set the video output mode
241 if ( S_OK != m_deckLinkOutput->EnableVideoOutput( m_displayMode->GetDisplayMode(), bmdVideoOutputFlagDefault ) )
243 mlt_log_error( getConsumer(), "Failed to enable video output\n" );
247 // Set the audio output mode
250 m_deckLinkOutput->DisableAudioOutput();
253 if ( S_OK != m_deckLinkOutput->EnableAudioOutput( bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
254 m_channels, bmdAudioOutputStreamTimestamped ) )
256 mlt_log_error( getConsumer(), "Failed to enable audio output\n" );
264 // Do preroll in thread to ensure asynchronicity of mlt_consumer_start().
265 pthread_create( &m_prerollThread, NULL, preroll_thread_proxy, this );
267 // Set the running state
268 mlt_properties_set_int( properties, "running", 1 );
275 mlt_properties properties = MLT_CONSUMER_PROPERTIES( getConsumer() );
276 bool wasRunning = !!mlt_properties_get_int( properties, "running" );
278 // set running state is 0
279 mlt_properties_set_int( properties, "running", 0 );
280 mlt_consumer_stopped( getConsumer() );
282 // Stop the audio and video output streams immediately
283 if ( m_deckLinkOutput )
285 m_deckLinkOutput->StopScheduledPlayback( 0, 0, 0 );
286 m_deckLinkOutput->DisableAudioOutput();
287 m_deckLinkOutput->DisableVideoOutput();
290 // release decklink frame
291 if ( m_decklinkFrame )
292 m_decklinkFrame->Release();
293 m_decklinkFrame = NULL;
296 pthread_join( m_prerollThread, NULL );
301 void renderAudio( mlt_frame frame )
303 mlt_audio_format format = mlt_audio_s16;
304 int frequency = bmdAudioSampleRate48kHz;
305 int samples = mlt_sample_calculator( m_fps, frequency, m_count );
308 if ( !mlt_frame_get_audio( frame, (void**) &pcm, &format, &frequency, &m_channels, &samples ) )
311 unsigned long written = 0;
313 uint32_t written = 0;
315 BMDTimeValue streamTime = m_count * frequency * m_duration / m_timescale;
316 m_deckLinkOutput->GetBufferedAudioSampleFrameCount( &written );
317 if ( written > (m_preroll + 1) * samples )
319 mlt_log_verbose( getConsumer(), "renderAudio: will flush %lu audiosamples\n", written );
320 m_deckLinkOutput->FlushBufferedAudioSamples();
323 m_deckLinkOutput->ScheduleAudioSamples( pcm, samples, streamTime, frequency, (unsigned long*) &written );
325 m_deckLinkOutput->ScheduleAudioSamples( pcm, samples, streamTime, frequency, &written );
328 if ( written != (uint32_t) samples )
329 mlt_log_verbose( getConsumer(), "renderAudio: samples=%d, written=%lu\n", samples, written );
333 bool createFrame( IDeckLinkMutableVideoFrame** decklinkFrame )
335 BMDPixelFormat format = m_isKeyer? bmdFormat8BitARGB : bmdFormat8BitYUV;
336 IDeckLinkMutableVideoFrame* frame = 0;
338 int stride = m_width * ( m_isKeyer? 4 : 2 );
340 *decklinkFrame = NULL;
342 // Generate a DeckLink video frame
343 if ( S_OK != m_deckLinkOutput->CreateVideoFrame( m_width, m_height,
344 stride, format, bmdFrameFlagDefault, &frame ) )
346 mlt_log_verbose( getConsumer(), "Failed to create video frame\n" );
351 // Make the first line black for field order correction.
352 if ( S_OK == frame->GetBytes( (void**) &buffer ) && buffer )
356 memset( buffer, 0, stride );
358 else for ( int i = 0; i < m_width; i++ )
365 *decklinkFrame = frame;
370 void renderVideo( mlt_frame frame )
372 mlt_image_format format = m_isKeyer? mlt_image_rgb24a : mlt_image_yuv422;
374 int rendered = mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered");
375 int height = m_height;
377 if ( rendered && !mlt_frame_get_image( frame, &image, &format, &m_width, &height, 0 ) )
380 int stride = m_width * ( m_isKeyer? 4 : 2 );
382 if ( m_decklinkFrame )
383 m_decklinkFrame->Release();
384 if ( createFrame( &m_decklinkFrame ) )
385 m_decklinkFrame->GetBytes( (void**) &buffer );
389 int progressive = mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "progressive" );
391 // NTSC SDI is always 486 lines
392 if ( m_height == 486 && height == 480 )
394 // blank first 6 lines
397 memset( buffer, 0, stride * 6 );
398 buffer += stride * 6;
400 else for ( int i = 0; i < m_width * 6; i++ )
408 // Normal non-keyer playout - needs byte swapping
409 if ( !progressive && m_displayMode->GetFieldDominance() == bmdUpperFieldFirst )
410 // convert lower field first to top field first
411 swab( (char*) image, (char*) buffer + stride, stride * ( height - 1 ) );
413 swab( (char*) image, (char*) buffer, stride * height );
415 else if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "test_image" ) )
417 // Normal keyer output
419 uint32_t* s = (uint32_t*) image;
420 uint32_t* d = (uint32_t*) buffer;
422 if ( !progressive && m_displayMode->GetFieldDominance() == bmdUpperFieldFirst )
424 // Correct field order
430 // Need to relocate alpha channel RGBA => ARGB
436 *d++ = ( *s << 8 ) | ( *s >> 24 );
443 // Keying blank frames - nullify alpha
444 memset( buffer, 0, stride * height );
448 if ( m_decklinkFrame )
449 m_deckLinkOutput->ScheduleVideoFrame( m_decklinkFrame, m_count * m_duration, m_duration, m_timescale );
452 mlt_log_verbose( getConsumer(), "dropped video frame %u\n", ++m_dropped );
455 HRESULT render( mlt_frame frame )
457 HRESULT result = S_OK;
460 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES(frame), "_speed" );
461 if ( m_isAudio && speed == 1.0 )
462 renderAudio( frame );
465 renderVideo( frame );
471 // *** DeckLink API implementation of IDeckLinkVideoOutputCallback IDeckLinkAudioOutputCallback *** //
473 // IUnknown needs only a dummy implementation
474 virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID iid, LPVOID *ppv )
475 { return E_NOINTERFACE; }
476 virtual ULONG STDMETHODCALLTYPE AddRef()
478 virtual ULONG STDMETHODCALLTYPE Release()
481 /************************* DeckLink API Delegate Methods *****************************/
483 virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted( IDeckLinkVideoFrame* completedFrame, BMDOutputFrameCompletionResult completed )
487 mlt_properties properties = MLT_CONSUMER_PROPERTIES( getConsumer() );
489 if ( mlt_properties_get( properties, "priority" ) )
493 pthread_attr_t tattr;
494 struct sched_param param;
496 pthread_attr_init(&tattr);
497 pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
499 if ( !strcmp( "max", mlt_properties_get( properties, "priority" ) ) )
500 param.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
501 else if ( !strcmp( "min", mlt_properties_get( properties, "priority" ) ) )
502 param.sched_priority = sched_get_priority_min(SCHED_FIFO) + 1;
504 param.sched_priority = mlt_properties_get_int( properties, "priority" );
506 pthread_attr_setschedparam(&tattr, ¶m);
508 thread = pthread_self();
510 r = pthread_setschedparam(thread, SCHED_FIFO, ¶m);
512 mlt_log_verbose( getConsumer(),
513 "ScheduledFrameCompleted: pthread_setschedparam returned %d\n", r);
515 mlt_log_verbose( getConsumer(),
516 "ScheduledFrameCompleted: param.sched_priority=%d\n", param.sched_priority);
527 m_deckLinkOutput->GetBufferedAudioSampleFrameCount( &cnt );
530 mlt_log_debug( getConsumer(),
531 "ScheduledFrameCompleted: GetBufferedAudioSampleFrameCount %u -> %lu, m_count=%"PRIu64"\n",
532 m_acnt, cnt, m_count );
536 // When a video frame has been released by the API, schedule another video frame to be output
538 // ignore handler if frame was flushed
539 if ( bmdOutputFrameFlushed == completed )
542 // schedule next frame
543 ScheduleNextFrame( false );
545 // step forward frames counter if underrun
546 if ( bmdOutputFrameDisplayedLate == completed )
548 mlt_log_verbose( getConsumer(), "ScheduledFrameCompleted: bmdOutputFrameDisplayedLate == completed\n" );
551 if ( bmdOutputFrameDropped == completed )
553 mlt_log_verbose( getConsumer(), "ScheduledFrameCompleted: bmdOutputFrameDropped == completed\n" );
560 virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped()
562 return mlt_consumer_is_stopped( getConsumer() ) ? S_FALSE : S_OK;
566 void ScheduleNextFrame( bool preroll )
569 mlt_consumer consumer = getConsumer();
571 // Get the properties
572 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
575 mlt_frame frame = NULL;
577 if( mlt_properties_get_int( properties, "running" ) || preroll )
579 frame = mlt_consumer_rt_frame( consumer );
584 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
586 // terminate on pause
587 if ( m_terminate_on_pause &&
588 mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0 )
591 mlt_frame_close( frame );
597 /** Start the consumer.
600 static int start( mlt_consumer consumer )
602 // Get the properties
603 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
604 DeckLinkConsumer* decklink = (DeckLinkConsumer*) consumer->child;
605 return decklink->start( mlt_properties_get_int( properties, "preroll" ) ) ? 0 : 1;
608 /** Stop the consumer.
611 static int stop( mlt_consumer consumer )
613 // Get the properties
614 DeckLinkConsumer* decklink = (DeckLinkConsumer*) consumer->child;
615 return decklink->stop();
618 /** Determine if the consumer is stopped.
621 static int is_stopped( mlt_consumer consumer )
623 // Get the properties
624 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
625 return !mlt_properties_get_int( properties, "running" );
628 /** Close the consumer.
631 static void close( mlt_consumer consumer )
634 mlt_consumer_stop( consumer );
637 consumer->close = NULL;
638 mlt_consumer_close( consumer );
641 delete (DeckLinkConsumer*) consumer->child;
646 /** Initialise the consumer.
649 mlt_consumer consumer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
651 // Allocate the consumer
652 DeckLinkConsumer* decklink = new DeckLinkConsumer();
653 mlt_consumer consumer = NULL;
656 if ( decklink && !mlt_consumer_init( decklink->getConsumer(), decklink, profile ) )
658 // If initialises without error
659 if ( decklink->open( arg? atoi(arg) : 0 ) )
661 consumer = decklink->getConsumer();
664 consumer->close = close;
665 consumer->start = start;
666 consumer->stop = stop;
667 consumer->is_stopped = is_stopped;
668 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "deinterlace_method", "onefield" );
676 extern mlt_producer producer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
678 static mlt_properties metadata( mlt_service_type type, const char *id, void *data )
680 char file[ PATH_MAX ];
681 const char *service_type = NULL;
685 service_type = "consumer";
688 service_type = "producer";
693 snprintf( file, PATH_MAX, "%s/decklink/%s_%s.yml", mlt_environment( "MLT_DATA" ), service_type, id );
694 return mlt_properties_parse_yaml( file );
699 MLT_REGISTER( consumer_type, "decklink", consumer_decklink_init );
700 MLT_REGISTER( producer_type, "decklink", producer_decklink_init );
701 MLT_REGISTER_METADATA( consumer_type, "decklink", metadata, NULL );
702 MLT_REGISTER_METADATA( producer_type, "decklink", metadata, NULL );