DecklinkReader.cpp
Go to the documentation of this file.
/**
* @file
* @brief Source file for DecklinkReader class
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @section LICENSE
*
* Copyright (c) 2008-2014 OpenShot Studios, LLC
* <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/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/DecklinkReader.h"
using namespace openshot;
DecklinkReader::DecklinkReader(int device, int video_mode, int pixel_format, int channels, int sample_depth)
: 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;
default:
throw DecklinkError("Pixel format is not valid (must be 0,1,2).");
}
// 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_IDeckLinkInput, (void**)&deckLinkInput) != S_OK)
throw DecklinkError("DeckLink QueryInterface Failed.");
// Obtain an IDeckLinkDisplayModeIterator to enumerate the display modes supported on output
result = deckLinkInput->GetDisplayModeIterator(&displayModeIterator);
if (result != S_OK)
throw DecklinkError("Could not obtain the video output display mode iterator.");
// Init deckLinkOutput (needed for color conversion)
if (deckLink->QueryInterface(IID_IDeckLinkOutput, (void**)&m_deckLinkOutput) != S_OK)
throw DecklinkError("Failed to create a deckLinkOutput(), used to convert YUV to RGB.");
// Init the YUV to RGB conversion
if(!(m_deckLinkConverter = CreateVideoConversionInstance()))
throw DecklinkError("Failed to create a VideoConversionInstance(), used to convert YUV to RGB.");
// Create Delegate & Pass in pointers to the output and converters
delegate = new DeckLinkInputDelegate(&sleepCond, m_deckLinkOutput, m_deckLinkConverter);
deckLinkInput->SetCallback(delegate);
if (g_videoModeIndex < 0)
throw DecklinkError("No video mode specified.");
// Loop through all available display modes, until a match is found (if any)
while (displayModeIterator->Next(&displayMode) == S_OK)
{
if (g_videoModeIndex == displayModeCount)
{
BMDDisplayModeSupport result;
foundDisplayMode = true;
displayMode->GetName(&displayModeName);
selectedDisplayMode = displayMode->GetDisplayMode();
deckLinkInput->DoesSupportVideoMode(selectedDisplayMode, pixelFormat, bmdVideoInputFlagDefault, &result, NULL);
// Get framerate
displayMode->GetFrameRate(&frameRateDuration, &frameRateScale);
if (result == bmdDisplayModeNotSupported)
throw DecklinkError("The display mode does not support the selected pixel format.");
if (inputFlags & bmdVideoInputDualStream3D)
{
if (!(displayMode->GetFlags() & bmdDisplayModeSupports3D))
throw DecklinkError("The display mode does not support 3D.");
}
break;
}
displayModeCount++;
displayMode->Release();
}
if (!foundDisplayMode)
throw DecklinkError("Invalid video mode. No matching ones found.");
// Check for video input
result = deckLinkInput->EnableVideoInput(selectedDisplayMode, pixelFormat, inputFlags);
if(result != S_OK)
throw DecklinkError("Failed to enable video input. Is another application using the card?");
// Check for audio input
result = deckLinkInput->EnableAudioInput(bmdAudioSampleRate48kHz, g_audioSampleDepth, g_audioChannels);
if(result != S_OK)
throw DecklinkError("Failed to enable audio input. Is another application using the card?");
}
// destructor
DecklinkReader::~DecklinkReader()
{
if (displayModeIterator != NULL)
{
displayModeIterator->Release();
displayModeIterator = NULL;
}
if (deckLinkInput != NULL)
{
deckLinkInput->Release();
deckLinkInput = NULL;
}
if (deckLink != NULL)
{
deckLink->Release();
deckLink = NULL;
}
if (deckLinkIterator != NULL)
deckLinkIterator->Release();
}
// Open image file
void DecklinkReader::Open()
{
// Open reader if not already open
if (!is_open)
{
// Start the streams
result = deckLinkInput->StartStreams();
if(result != S_OK)
throw DecklinkError("Failed to start the video and audio streams.");
// Update image properties
info.has_audio = false;
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 DecklinkReader::Close()
{
// Close all objects, if reader is 'open'
if (is_open)
{
// Stop streams
result = deckLinkInput->StopStreams();
if(result != S_OK)
throw DecklinkError("Failed to stop the video and audio streams.");
// Mark as "closed"
is_open = false;
}
}
unsigned long DecklinkReader::GetCurrentFrameNumber()
{
return delegate->GetCurrentFrameNumber();
}
// Get an openshot::Frame object for the next available LIVE frame
std::shared_ptr<Frame> DecklinkReader::GetFrame(int64_t requested_frame)
{
// Get a frame from the delegate decklink class (which is collecting them on another thread)
std::shared_ptr<Frame> f = delegate->GetFrame(requested_frame);
// cout << "Change the frame number to " << requested_frame << endl;
// f->SetFrameNumber(requested_frame);
return f; // frame # does not matter, since it always gets the oldest frame
}
// Generate JSON string of this object
string DecklinkReader::Json() {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::JsonValue for this object
Json::Value DecklinkReader::JsonValue() {
// Create root json object
Json::Value root = ReaderBase::JsonValue(); // get parent properties
root["type"] = "DecklinkReader";
// return JsonValue
return root;
}
// Load JSON string into this object
void DecklinkReader::SetJson(string value) {
// Parse JSON string into JSON objects
Json::Value root;
Json::Reader reader;
bool success = reader.parse( value, root );
if (!success)
// Raise exception
throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
try
{
// Set all values that match
SetJsonValue(root);
}
catch (exception e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
}
}
// Load Json::JsonValue into this object
void DecklinkReader::SetJsonValue(Json::Value root) {
// Set parent data
ReaderBase::SetJsonValue(root);
// Re-Open path, and re-init everything (if needed)
if (is_open)
{
Close();
Open();
}
}
openshot::DecklinkReader::Open
void Open()
Open device and video stream - which is called by the constructor automatically.
Definition: DecklinkReader.cpp:169
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:168
openshot::DecklinkReader::DecklinkReader
DecklinkReader(int device, int video_mode, int pixel_format, int channels, int sample_depth)
Definition: DecklinkReader.cpp:32
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:42
openshot::DecklinkReader::Close
void Close()
Destructor.
Definition: DecklinkReader.cpp:212
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition: ReaderBase.h:64
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:61
openshot::ReaderInfo::display_ratio
Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3)
Definition: ReaderBase.h:72
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:113
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:67
openshot::ReaderBase::info
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:112
openshot::DecklinkReader::SetJson
void SetJson(string value)
Load JSON string into this object.
Definition: DecklinkReader.cpp:264
openshot::DecklinkError
Exception when accessing a blackmagic decklink card.
Definition: Exceptions.h:70
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:74
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:44
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:45
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:71
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:152
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:65
DeckLinkInputDelegate::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame)
Definition: DecklinkInput.cpp:107
DeckLinkInputDelegate
Implementation of the Blackmagic Decklink API (used by the DecklinkReader)
Definition: DecklinkInput.h:70
openshot::ReaderInfo::pixel_ratio
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition: ReaderBase.h:71
openshot::ReaderInfo::vcodec
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:73
OpenShot Wipe Tests.e
e
Definition: OpenShot Wipe Tests.py:28
openshot::DecklinkReader::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: DecklinkReader.cpp:253
openshot::ReaderInfo::fps
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:69
DeckLinkInputDelegate::GetCurrentFrameNumber
unsigned long GetCurrentFrameNumber()
Definition: DecklinkInput.cpp:99
openshot::ReaderInfo::video_timebase
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:76
openshot::DecklinkReader::GetFrame
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
Definition: DecklinkReader.cpp:234
openshot::DecklinkReader::GetCurrentFrameNumber
unsigned long GetCurrentFrameNumber()
Definition: DecklinkReader.cpp:228
openshot::Fraction::ToDouble
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:46
openshot::DecklinkReader::~DecklinkReader
~DecklinkReader()
Definition: DecklinkReader.cpp:144
openshot::DecklinkReader::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: DecklinkReader.cpp:287
openshot::DecklinkReader::Json
string Json()
Get and Set JSON methods.
Definition: DecklinkReader.cpp:246