OpenShot Library | libopenshot  0.2.5
DecklinkReader.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for DecklinkReader class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "../include/DecklinkReader.h"
32 
33 using namespace openshot;
34 
35 DecklinkReader::DecklinkReader(int device, int video_mode, int pixel_format, int channels, int sample_depth)
36  : device(device), is_open(false), g_videoModeIndex(video_mode), g_audioChannels(channels), g_audioSampleDepth(sample_depth)
37 {
38  // Init decklink variables
39  inputFlags = 0;
40  selectedDisplayMode = bmdModeNTSC;
41  pixelFormat = bmdFormat8BitYUV;
42  displayModeCount = 0;
43  exitStatus = 1;
44  foundDisplayMode = false;
45  pthread_mutex_init(&sleepMutex, NULL);
46  pthread_cond_init(&sleepCond, NULL);
47 
48  switch(pixel_format)
49  {
50  case 0: pixelFormat = bmdFormat8BitYUV; break;
51  case 1: pixelFormat = bmdFormat10BitYUV; break;
52  case 2: pixelFormat = bmdFormat10BitRGB; break;
53  default:
54  throw DecklinkError("Pixel format is not valid (must be 0,1,2).");
55  }
56 
57 
58  // Attempt to open blackmagic card
59  deckLinkIterator = CreateDeckLinkIteratorInstance();
60 
61  if (!deckLinkIterator)
62  throw DecklinkError("This application requires the DeckLink drivers installed.");
63 
64  /* Connect to a DeckLink instance */
65  for (int device_count = 0; device_count <= device; device_count++)
66  {
67  // Check for requested device
68  result = deckLinkIterator->Next(&deckLink);
69  if (result != S_OK)
70  throw DecklinkError("No DeckLink PCI cards found.");
71 
72  if (device_count == device)
73  break;
74  }
75 
76  if (deckLink->QueryInterface(IID_IDeckLinkInput, (void**)&deckLinkInput) != S_OK)
77  throw DecklinkError("DeckLink QueryInterface Failed.");
78 
79  // Obtain an IDeckLinkDisplayModeIterator to enumerate the display modes supported on output
80  result = deckLinkInput->GetDisplayModeIterator(&displayModeIterator);
81  if (result != S_OK)
82  throw DecklinkError("Could not obtain the video output display mode iterator.");
83 
84  // Init deckLinkOutput (needed for color conversion)
85  if (deckLink->QueryInterface(IID_IDeckLinkOutput, (void**)&m_deckLinkOutput) != S_OK)
86  throw DecklinkError("Failed to create a deckLinkOutput(), used to convert YUV to RGB.");
87 
88  // Init the YUV to RGB conversion
89  if(!(m_deckLinkConverter = CreateVideoConversionInstance()))
90  throw DecklinkError("Failed to create a VideoConversionInstance(), used to convert YUV to RGB.");
91 
92  // Create Delegate & Pass in pointers to the output and converters
93  delegate = new DeckLinkInputDelegate(&sleepCond, m_deckLinkOutput, m_deckLinkConverter);
94  deckLinkInput->SetCallback(delegate);
95 
96 
97 
98  if (g_videoModeIndex < 0)
99  throw DecklinkError("No video mode specified.");
100 
101  // Loop through all available display modes, until a match is found (if any)
102  while (displayModeIterator->Next(&displayMode) == S_OK)
103  {
104  if (g_videoModeIndex == displayModeCount)
105  {
106  BMDDisplayModeSupport result;
107 
108  foundDisplayMode = true;
109  displayMode->GetName(&displayModeName);
110  selectedDisplayMode = displayMode->GetDisplayMode();
111  deckLinkInput->DoesSupportVideoMode(selectedDisplayMode, pixelFormat, bmdVideoInputFlagDefault, &result, NULL);
112 
113  // Get framerate
114  displayMode->GetFrameRate(&frameRateDuration, &frameRateScale);
115 
116  if (result == bmdDisplayModeNotSupported)
117  throw DecklinkError("The display mode does not support the selected pixel format.");
118 
119  if (inputFlags & bmdVideoInputDualStream3D)
120  {
121  if (!(displayMode->GetFlags() & bmdDisplayModeSupports3D))
122  throw DecklinkError("The display mode does not support 3D.");
123  }
124 
125  break;
126  }
127  displayModeCount++;
128  displayMode->Release();
129  }
130 
131  if (!foundDisplayMode)
132  throw DecklinkError("Invalid video mode. No matching ones found.");
133 
134  // Check for video input
135  result = deckLinkInput->EnableVideoInput(selectedDisplayMode, pixelFormat, inputFlags);
136  if(result != S_OK)
137  throw DecklinkError("Failed to enable video input. Is another application using the card?");
138 
139  // Check for audio input
140  result = deckLinkInput->EnableAudioInput(bmdAudioSampleRate48kHz, g_audioSampleDepth, g_audioChannels);
141  if(result != S_OK)
142  throw DecklinkError("Failed to enable audio input. Is another application using the card?");
143 
144 }
145 
146 // destructor
148 {
149  if (displayModeIterator != NULL)
150  {
151  displayModeIterator->Release();
152  displayModeIterator = NULL;
153  }
154 
155  if (deckLinkInput != NULL)
156  {
157  deckLinkInput->Release();
158  deckLinkInput = NULL;
159  }
160 
161  if (deckLink != NULL)
162  {
163  deckLink->Release();
164  deckLink = NULL;
165  }
166 
167  if (deckLinkIterator != NULL)
168  deckLinkIterator->Release();
169 }
170 
171 // Open image file
173 {
174  // Open reader if not already open
175  if (!is_open)
176  {
177  // Start the streams
178  result = deckLinkInput->StartStreams();
179  if(result != S_OK)
180  throw DecklinkError("Failed to start the video and audio streams.");
181 
182 
183  // Update image properties
184  info.has_audio = false;
185  info.has_video = true;
186  info.vcodec = displayModeName;
187  info.width = displayMode->GetWidth();
188  info.height = displayMode->GetHeight();
189  info.file_size = info.width * info.height * sizeof(char) * 4;
190  info.pixel_ratio.num = 1;
191  info.pixel_ratio.den = 1;
192  info.duration = 60 * 60 * 24; // 24 hour duration... since we're capturing a live stream
193  info.fps.num = frameRateScale;
194  info.fps.den = frameRateDuration;
195  info.video_timebase.num = frameRateDuration;
196  info.video_timebase.den = frameRateScale;
198 
199  // Calculate the DAR (display aspect ratio)
201 
202  // Reduce size fraction
203  size.Reduce();
204 
205  // Set the ratio based on the reduced fraction
206  info.display_ratio.num = size.num;
207  info.display_ratio.den = size.den;
208 
209  // Mark as "open"
210  is_open = true;
211  }
212 }
213 
214 // Close device and video stream
216 {
217  // Close all objects, if reader is 'open'
218  if (is_open)
219  {
220  // Stop streams
221  result = deckLinkInput->StopStreams();
222 
223  if(result != S_OK)
224  throw DecklinkError("Failed to stop the video and audio streams.");
225 
226  // Mark as "closed"
227  is_open = false;
228  }
229 }
230 
232 {
233  return delegate->GetCurrentFrameNumber();
234 }
235 
236 // Get an openshot::Frame object for the next available LIVE frame
237 std::shared_ptr<Frame> DecklinkReader::GetFrame(int64_t requested_frame)
238 {
239  // Get a frame from the delegate decklink class (which is collecting them on another thread)
240  std::shared_ptr<Frame> f = delegate->GetFrame(requested_frame);
241 
242 // cout << "Change the frame number to " << requested_frame << endl;
243 // f->SetFrameNumber(requested_frame);
244  return f; // frame # does not matter, since it always gets the oldest frame
245 }
246 
247 
248 // Generate JSON string of this object
249 std::string DecklinkReader::Json() const {
250 
251  // Return formatted string
252  return JsonValue().toStyledString();
253 }
254 
255 // Generate Json::Value for this object
256 Json::Value DecklinkReader::JsonValue() const {
257 
258  // Create root json object
259  Json::Value root = ReaderBase::JsonValue(); // get parent properties
260  root["type"] = "DecklinkReader";
261 
262  // return JsonValue
263  return root;
264 }
265 
266 // Load JSON string into this object
267 void DecklinkReader::SetJson(const std::string value) {
268 
269  // Parse JSON string into JSON objects
270  try
271  {
272  const Json::Value root = openshot::stringToJson(value);
273  // Set all values that match
274  SetJsonValue(root);
275  }
276  catch (const std::exception& e)
277  {
278  // Error parsing JSON (or missing keys)
279  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
280  }
281 }
282 
283 // Load Json::Value into this object
284 void DecklinkReader::SetJsonValue(const Json::Value root) {
285 
286  // Set parent data
288 
289  // Re-Open path, and re-init everything (if needed)
290  if (is_open)
291  {
292  Close();
293  Open();
294  }
295 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:116
openshot::DecklinkReader::Open
void Open()
Open device and video stream - which is called by the constructor automatically.
Definition: DecklinkReader.cpp:172
openshot::DecklinkReader::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: DecklinkReader.cpp:267
openshot::DecklinkReader::DecklinkReader
DecklinkReader(int device, int video_mode, int pixel_format, int channels, int sample_depth)
Definition: DecklinkReader.cpp:35
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:171
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:39
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:45
openshot::DecklinkReader::Close
void Close()
Destructor.
Definition: DecklinkReader.cpp:215
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition: ReaderBase.h:65
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:62
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
openshot::DecklinkError
Exception when accessing a blackmagic decklink card.
Definition: Exceptions.h:80
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:75
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:47
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:48
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:74
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:206
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:66
DeckLinkInputDelegate::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame)
Definition: DecklinkInput.cpp:110
DeckLinkInputDelegate
Implementation of the Blackmagic Decklink API (used by the DecklinkReader)
Definition: DecklinkInput.h:74
openshot::ReaderInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:77
openshot::ReaderInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:74
openshot::DecklinkReader::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: DecklinkReader.cpp:284
DeckLinkInputDelegate::GetCurrentFrameNumber
unsigned long GetCurrentFrameNumber()
Definition: DecklinkInput.cpp:102
openshot::DecklinkReader::GetFrame
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
Definition: DecklinkReader.cpp:237
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
openshot::DecklinkReader::GetCurrentFrameNumber
unsigned long GetCurrentFrameNumber()
Definition: DecklinkReader.cpp:231
openshot::Fraction::ToDouble
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:49
openshot::DecklinkReader::~DecklinkReader
~DecklinkReader()
Definition: DecklinkReader.cpp:147
openshot::ReaderInfo::pixel_ratio
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition: ReaderBase.h:72
openshot::DecklinkReader::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: DecklinkReader.cpp:256
openshot::DecklinkReader::Json
std::string Json() const override
Get and Set JSON methods.
Definition: DecklinkReader.cpp:249
openshot::ReaderInfo::display_ratio
openshot::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:73