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