2013-09-11 17:32:40 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for DecklinkWriter class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
2013-09-11 17:32:40 -05:00
|
|
|
*
|
2019-06-11 06:48:32 -04:00
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC
|
2014-03-29 18:49:22 -05:00
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
2013-09-11 17:32:40 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-09-11 17:32:40 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-11 17:32:40 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-11 17:32:40 -05:00
|
|
|
*/
|
|
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
#include "../include/DecklinkWriter.h"
|
|
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2017-10-26 18:44:35 -05:00
|
|
|
DecklinkWriter::DecklinkWriter(int device, int video_mode, int pixel_format, int channels, int sample_depth)
|
2013-02-10 02:19:40 -06:00
|
|
|
: device(device), is_open(false), g_videoModeIndex(video_mode), g_audioChannels(channels), g_audioSampleDepth(sample_depth)
|
|
|
|
|
{
|
|
|
|
|
// Init decklink variables
|
|
|
|
|
inputFlags = 0;
|
|
|
|
|
selectedDisplayMode = bmdModeNTSC;
|
|
|
|
|
pixelFormat = bmdFormat8BitYUV;
|
|
|
|
|
displayModeCount = 0;
|
|
|
|
|
exitStatus = 1;
|
|
|
|
|
foundDisplayMode = false;
|
|
|
|
|
pthread_mutex_init(&sleepMutex, NULL);
|
|
|
|
|
pthread_cond_init(&sleepCond, NULL);
|
|
|
|
|
|
|
|
|
|
switch(pixel_format)
|
|
|
|
|
{
|
|
|
|
|
case 0: pixelFormat = bmdFormat8BitYUV; break;
|
|
|
|
|
case 1: pixelFormat = bmdFormat10BitYUV; break;
|
|
|
|
|
case 2: pixelFormat = bmdFormat10BitRGB; break;
|
|
|
|
|
case 3: pixelFormat = bmdFormat8BitARGB; break;
|
|
|
|
|
default:
|
|
|
|
|
throw DecklinkError("Pixel format is not valid (must be 0,1,2,3).");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 00:08:38 -06:00
|
|
|
// Open decklink writer
|
2017-10-26 18:44:35 -05:00
|
|
|
void DecklinkWriter::Open()
|
2013-02-10 02:19:40 -06:00
|
|
|
{
|
|
|
|
|
// Open reader if not already open
|
|
|
|
|
if (!is_open)
|
|
|
|
|
{
|
|
|
|
|
// Attempt to open blackmagic card
|
|
|
|
|
deckLinkIterator = CreateDeckLinkIteratorInstance();
|
|
|
|
|
|
|
|
|
|
if (!deckLinkIterator)
|
|
|
|
|
throw DecklinkError("This application requires the DeckLink drivers installed.");
|
|
|
|
|
|
|
|
|
|
/* Connect to a DeckLink instance */
|
|
|
|
|
for (int device_count = 0; device_count <= device; device_count++)
|
|
|
|
|
{
|
|
|
|
|
// Check for requested device
|
|
|
|
|
result = deckLinkIterator->Next(&deckLink);
|
|
|
|
|
if (result != S_OK)
|
|
|
|
|
throw DecklinkError("No DeckLink PCI cards found.");
|
|
|
|
|
|
|
|
|
|
if (device_count == device)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (deckLink->QueryInterface(IID_IDeckLinkOutput, (void**)&deckLinkOutput) != S_OK)
|
|
|
|
|
throw DecklinkError("DeckLink QueryInterface Failed.");
|
|
|
|
|
|
|
|
|
|
// Obtain an IDeckLinkDisplayModeIterator to enumerate the display modes supported on output
|
|
|
|
|
result = deckLinkOutput->GetDisplayModeIterator(&displayModeIterator);
|
|
|
|
|
if (result != S_OK)
|
|
|
|
|
throw DecklinkError("Could not obtain the video output display mode iterator.");
|
|
|
|
|
|
|
|
|
|
if (g_videoModeIndex < 0)
|
|
|
|
|
throw DecklinkError("No video mode specified.");
|
|
|
|
|
|
|
|
|
|
// Loop through all available display modes, until a match is found (if any)
|
|
|
|
|
const char *displayModeName;
|
|
|
|
|
BMDTimeValue frameRateDuration, frameRateScale;
|
|
|
|
|
|
|
|
|
|
while (displayModeIterator->Next(&displayMode) == S_OK)
|
|
|
|
|
{
|
|
|
|
|
if (g_videoModeIndex == displayModeCount)
|
|
|
|
|
{
|
|
|
|
|
BMDDisplayModeSupport result;
|
|
|
|
|
|
|
|
|
|
foundDisplayMode = true;
|
|
|
|
|
displayMode->GetName(&displayModeName);
|
|
|
|
|
selectedDisplayMode = displayMode->GetDisplayMode();
|
|
|
|
|
//deckLinkOutput->DoesSupportVideoMode(selectedDisplayMode, pixelFormat, bmdVideoOutputFlagDefault, &result, NULL);
|
|
|
|
|
|
|
|
|
|
// Get framerate
|
|
|
|
|
displayMode->GetFrameRate(&frameRateDuration, &frameRateScale);
|
|
|
|
|
|
|
|
|
|
//if (result == bmdDisplayModeNotSupported)
|
|
|
|
|
//{
|
|
|
|
|
// cout << "The display mode does not support the selected pixel format." << endl;
|
|
|
|
|
// throw DecklinkError("The display mode does not support the selected pixel format.");
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
displayModeCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!foundDisplayMode)
|
|
|
|
|
throw DecklinkError("Invalid video mode. No matching ones found.");
|
|
|
|
|
|
|
|
|
|
// Calculate FPS
|
|
|
|
|
unsigned long m_framesPerSecond = (unsigned long)((frameRateScale + (frameRateDuration-1)) / frameRateDuration);
|
|
|
|
|
|
|
|
|
|
// Create Delegate & Pass in pointers to the output and converters
|
|
|
|
|
delegate = new DeckLinkOutputDelegate(displayMode, deckLinkOutput);
|
|
|
|
|
|
|
|
|
|
// Provide this class as a delegate to the audio and video output interfaces
|
|
|
|
|
deckLinkOutput->SetScheduledFrameCompletionCallback(delegate);
|
|
|
|
|
//deckLinkOutput->SetAudioCallback(delegate);
|
|
|
|
|
|
|
|
|
|
// Check for video input
|
|
|
|
|
if (deckLinkOutput->EnableVideoOutput(displayMode->GetDisplayMode(), bmdVideoOutputFlagDefault) != S_OK)
|
|
|
|
|
throw DecklinkError("Failed to enable video output. Is another application using the card?");
|
|
|
|
|
|
|
|
|
|
// Check for audio input
|
|
|
|
|
//if (deckLinkOutput->EnableAudioOutput(bmdAudioSampleRate48kHz, g_audioSampleDepth, g_audioChannels, bmdAudioOutputStreamContinuous) != S_OK)
|
|
|
|
|
// throw DecklinkError("Failed to enable audio output. Is another application using the card?");
|
|
|
|
|
|
|
|
|
|
// Begin video preroll by scheduling a second of frames in hardware
|
2017-08-20 17:37:39 -05:00
|
|
|
//std::shared_ptr<Frame> f(new Frame(1, displayMode->GetWidth(), displayMode->GetHeight(), "Blue"));
|
2013-02-10 21:16:46 -06:00
|
|
|
//f->AddColor(displayMode->GetWidth(), displayMode->GetHeight(), "Blue");
|
2013-02-10 02:19:40 -06:00
|
|
|
|
|
|
|
|
// Preroll 1 second of video
|
2013-02-10 21:16:46 -06:00
|
|
|
//for (unsigned i = 0; i < 16; i++)
|
|
|
|
|
//{
|
|
|
|
|
// // Write 30 blank frames (for preroll)
|
|
|
|
|
// delegate->WriteFrame(f);
|
|
|
|
|
// delegate->ScheduleNextFrame(true);
|
|
|
|
|
//}
|
2013-02-10 02:19:40 -06:00
|
|
|
|
2013-02-10 21:16:46 -06:00
|
|
|
//deckLinkOutput->StartScheduledPlayback(0, 100, 1.0);
|
2013-02-10 02:19:40 -06:00
|
|
|
//if (deckLinkOutput->BeginAudioPreroll() != S_OK)
|
|
|
|
|
// throw DecklinkError("Failed to begin audio preroll.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update image properties
|
|
|
|
|
info.has_audio = true;
|
|
|
|
|
info.has_video = true;
|
|
|
|
|
info.vcodec = displayModeName;
|
|
|
|
|
info.width = displayMode->GetWidth();
|
|
|
|
|
info.height = displayMode->GetHeight();
|
|
|
|
|
info.file_size = info.width * info.height * sizeof(char) * 4;
|
|
|
|
|
info.pixel_ratio.num = 1;
|
|
|
|
|
info.pixel_ratio.den = 1;
|
|
|
|
|
info.duration = 60 * 60 * 24; // 24 hour duration... since we're capturing a live stream
|
|
|
|
|
info.fps.num = frameRateScale;
|
|
|
|
|
info.fps.den = frameRateDuration;
|
|
|
|
|
info.video_timebase.num = frameRateDuration;
|
|
|
|
|
info.video_timebase.den = frameRateScale;
|
|
|
|
|
info.video_length = round(info.duration * info.fps.ToDouble());
|
|
|
|
|
|
|
|
|
|
// Calculate the DAR (display aspect ratio)
|
|
|
|
|
Fraction size(info.width * info.pixel_ratio.num, info.height * info.pixel_ratio.den);
|
|
|
|
|
|
|
|
|
|
// Reduce size fraction
|
|
|
|
|
size.Reduce();
|
|
|
|
|
|
|
|
|
|
// Set the ratio based on the reduced fraction
|
|
|
|
|
info.display_ratio.num = size.num;
|
|
|
|
|
info.display_ratio.den = size.den;
|
|
|
|
|
|
|
|
|
|
// Mark as "open"
|
|
|
|
|
is_open = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close device and video stream
|
|
|
|
|
void DecklinkWriter::Close()
|
|
|
|
|
{
|
|
|
|
|
// Close all objects, if reader is 'open'
|
|
|
|
|
if (is_open)
|
|
|
|
|
{
|
|
|
|
|
// Stop the audio and video output streams immediately
|
|
|
|
|
deckLinkOutput->StopScheduledPlayback(0, NULL, 0);
|
|
|
|
|
deckLinkOutput->DisableAudioOutput();
|
|
|
|
|
deckLinkOutput->DisableVideoOutput();
|
|
|
|
|
|
|
|
|
|
// Release DisplayMode
|
|
|
|
|
displayMode->Release();
|
|
|
|
|
|
|
|
|
|
if (displayModeIterator != NULL)
|
|
|
|
|
{
|
|
|
|
|
displayModeIterator->Release();
|
|
|
|
|
displayModeIterator = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (deckLinkOutput != NULL)
|
|
|
|
|
{
|
|
|
|
|
deckLinkOutput->Release();
|
|
|
|
|
deckLinkOutput = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (deckLink != NULL)
|
|
|
|
|
{
|
|
|
|
|
deckLink->Release();
|
|
|
|
|
deckLink = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (deckLinkIterator != NULL)
|
|
|
|
|
deckLinkIterator->Release();
|
|
|
|
|
|
|
|
|
|
// Mark as "closed"
|
|
|
|
|
is_open = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-08 16:40:57 -05:00
|
|
|
// This method is required for all derived classes of WriterBase. Write a Frame to the video file.
|
2017-10-26 18:44:35 -05:00
|
|
|
void DecklinkWriter::WriteFrame(std::shared_ptr<Frame> frame)
|
2013-02-10 02:19:40 -06:00
|
|
|
{
|
2015-02-05 00:08:38 -06:00
|
|
|
// Check for open reader (or throw exception)
|
|
|
|
|
if (!is_open)
|
2019-08-27 15:47:39 -04:00
|
|
|
throw WriterClosed("The DecklinkWriter is closed. Call Open() before calling this method.");
|
2015-02-05 00:08:38 -06:00
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
delegate->WriteFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-08 16:40:57 -05:00
|
|
|
// This method is required for all derived classes of WriterBase. Write a block of frames from a reader.
|
2017-10-26 18:44:35 -05:00
|
|
|
void DecklinkWriter::WriteFrame(ReaderBase* reader, int start, int length)
|
2013-02-10 02:19:40 -06:00
|
|
|
{
|
|
|
|
|
// Loop through each frame (and encoded it)
|
|
|
|
|
for (int number = start; number <= length; number++)
|
|
|
|
|
{
|
|
|
|
|
// Get the frame
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = reader->GetFrame(number);
|
2013-02-10 02:19:40 -06:00
|
|
|
|
|
|
|
|
// Encode frame
|
|
|
|
|
WriteFrame(f);
|
|
|
|
|
}
|
|
|
|
|
}
|