2013-09-11 17:32:40 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for DecklinkInput class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>, Blackmagic Design
|
|
|
|
|
*
|
|
|
|
|
* @section LICENSE
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2009 Blackmagic Design
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person or organization
|
|
|
|
|
* obtaining a copy of the software and accompanying documentation covered by
|
|
|
|
|
* this license (the "Software") to use, reproduce, display, distribute,
|
|
|
|
|
* execute, and transmit the Software, and to prepare derivative works of the
|
|
|
|
|
* Software, and to permit third-parties to whom the Software is furnished to
|
|
|
|
|
* do so, all subject to the following:
|
|
|
|
|
*
|
|
|
|
|
* The copyright notices in the Software and this entire statement, including
|
|
|
|
|
* the above license grant, this restriction and the following disclaimer,
|
|
|
|
|
* must be included in all copies of the Software, in whole or in part, and
|
|
|
|
|
* all derivative works of the Software, unless such copies or derivative
|
|
|
|
|
* works are solely in the form of machine-executable object code generated by
|
|
|
|
|
* a source language processor.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
|
|
|
|
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
|
|
|
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* 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/>.
|
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-06 02:09:21 -06:00
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
#include "../include/DecklinkInput.h"
|
2013-02-06 02:09:21 -06:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
DeckLinkInputDelegate::DeckLinkInputDelegate(pthread_cond_t* m_sleepCond, IDeckLinkOutput* m_deckLinkOutput, IDeckLinkVideoConversion* m_deckLinkConverter)
|
2013-02-10 21:16:46 -06:00
|
|
|
: m_refCount(0), g_timecodeFormat(0), frameCount(0), final_frameCount(0)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
|
|
|
|
sleepCond = m_sleepCond;
|
|
|
|
|
deckLinkOutput = m_deckLinkOutput;
|
|
|
|
|
deckLinkConverter = m_deckLinkConverter;
|
|
|
|
|
|
2013-02-12 01:28:48 -06:00
|
|
|
// Set cache size (20 1080p frames)
|
2013-02-15 00:23:55 -06:00
|
|
|
final_frames.SetMaxBytes(60 * 1920 * 1080 * 4 + (44100 * 2 * 4));
|
2013-02-12 01:28:48 -06:00
|
|
|
|
2013-02-06 02:09:21 -06:00
|
|
|
pthread_mutex_init(&m_mutex, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
DeckLinkInputDelegate::~DeckLinkInputDelegate()
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
|
|
|
|
pthread_mutex_destroy(&m_mutex);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
ULONG DeckLinkInputDelegate::AddRef(void)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
|
|
|
|
pthread_mutex_lock(&m_mutex);
|
|
|
|
|
m_refCount++;
|
|
|
|
|
pthread_mutex_unlock(&m_mutex);
|
|
|
|
|
|
|
|
|
|
return (ULONG)m_refCount;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
ULONG DeckLinkInputDelegate::Release(void)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
|
|
|
|
pthread_mutex_lock(&m_mutex);
|
|
|
|
|
m_refCount--;
|
|
|
|
|
pthread_mutex_unlock(&m_mutex);
|
|
|
|
|
|
|
|
|
|
if (m_refCount == 0)
|
|
|
|
|
{
|
|
|
|
|
delete this;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (ULONG)m_refCount;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-12 02:42:18 -06:00
|
|
|
unsigned long DeckLinkInputDelegate::GetCurrentFrameNumber()
|
|
|
|
|
{
|
2013-02-15 00:23:55 -06:00
|
|
|
if (final_frameCount > 0)
|
|
|
|
|
return final_frameCount - 1;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
2013-02-12 02:42:18 -06:00
|
|
|
}
|
|
|
|
|
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<openshot::Frame> DeckLinkInputDelegate::GetFrame(long int requested_frame)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<openshot::Frame> f;
|
2013-02-06 02:09:21 -06:00
|
|
|
|
2013-02-12 02:42:18 -06:00
|
|
|
// Is this frame for the future?
|
2013-02-15 00:23:55 -06:00
|
|
|
while (requested_frame > GetCurrentFrameNumber())
|
2013-02-12 02:42:18 -06:00
|
|
|
{
|
2013-02-15 00:23:55 -06:00
|
|
|
usleep(500 * 1);
|
2013-02-12 02:42:18 -06:00
|
|
|
}
|
|
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
#pragma omp critical (blackmagic_input_queue)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
2013-02-12 01:28:48 -06:00
|
|
|
if (final_frames.Exists(requested_frame))
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
2013-02-12 01:28:48 -06:00
|
|
|
// Get the frame and remove it from the cache
|
|
|
|
|
f = final_frames.GetFrame(requested_frame);
|
|
|
|
|
final_frames.Remove(requested_frame);
|
2013-02-06 02:09:21 -06:00
|
|
|
}
|
2013-02-12 01:28:48 -06:00
|
|
|
else
|
2013-02-15 00:23:55 -06:00
|
|
|
{
|
|
|
|
|
cout << "Can't find " << requested_frame << ", GetCurrentFrameNumber(): " << GetCurrentFrameNumber() << endl;
|
|
|
|
|
final_frames.Display();
|
|
|
|
|
}
|
2013-02-06 02:09:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
HRESULT DeckLinkInputDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
|
|
|
|
// Handle Video Frame
|
|
|
|
|
if(videoFrame)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (videoFrame->GetFlags() & bmdFrameHasNoInputSource)
|
|
|
|
|
{
|
2013-02-10 23:09:27 -06:00
|
|
|
fprintf(stderr, "Frame received (#%lu) - No input signal detected\n", frameCount);
|
2013-02-06 02:09:21 -06:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const char *timecodeString = NULL;
|
|
|
|
|
if (g_timecodeFormat != 0)
|
|
|
|
|
{
|
|
|
|
|
IDeckLinkTimecode *timecode;
|
|
|
|
|
if (videoFrame->GetTimecode(g_timecodeFormat, &timecode) == S_OK)
|
|
|
|
|
{
|
|
|
|
|
timecode->GetString(&timecodeString);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-12 02:42:18 -06:00
|
|
|
// fprintf(stderr, "Frame received (#%lu) [%s] - Size: %li bytes\n",
|
|
|
|
|
// frameCount,
|
|
|
|
|
// timecodeString != NULL ? timecodeString : "No timecode",
|
|
|
|
|
// videoFrame->GetRowBytes() * videoFrame->GetHeight());
|
2013-02-06 02:09:21 -06:00
|
|
|
|
|
|
|
|
if (timecodeString)
|
|
|
|
|
free((void*)timecodeString);
|
|
|
|
|
|
|
|
|
|
// Create a new copy of the YUV frame object
|
|
|
|
|
IDeckLinkMutableVideoFrame *m_yuvFrame = NULL;
|
|
|
|
|
|
|
|
|
|
int width = videoFrame->GetWidth();
|
|
|
|
|
int height = videoFrame->GetHeight();
|
|
|
|
|
|
|
|
|
|
HRESULT res = deckLinkOutput->CreateVideoFrame(
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
videoFrame->GetRowBytes(),
|
|
|
|
|
bmdFormat8BitYUV,
|
|
|
|
|
bmdFrameFlagDefault,
|
|
|
|
|
&m_yuvFrame);
|
|
|
|
|
|
|
|
|
|
// Copy pixel and audio to copied frame
|
|
|
|
|
void *frameBytesSource;
|
|
|
|
|
void *frameBytesDest;
|
|
|
|
|
videoFrame->GetBytes(&frameBytesSource);
|
|
|
|
|
m_yuvFrame->GetBytes(&frameBytesDest);
|
|
|
|
|
memcpy(frameBytesDest, frameBytesSource, videoFrame->GetRowBytes() * height);
|
|
|
|
|
|
|
|
|
|
// Add raw YUV frame to queue
|
|
|
|
|
raw_video_frames.push_back(m_yuvFrame);
|
|
|
|
|
|
|
|
|
|
// Process frames once we have a few (to take advantage of multiple threads)
|
2013-02-15 00:23:55 -06:00
|
|
|
int number_to_process = raw_video_frames.size();
|
2014-04-02 16:48:27 -05:00
|
|
|
if (number_to_process >= OPEN_MP_NUM_PROCESSORS)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//omp_set_num_threads(1);
|
|
|
|
|
omp_set_nested(true);
|
2013-02-10 15:46:41 -06:00
|
|
|
#pragma omp parallel
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
2013-02-10 15:46:41 -06:00
|
|
|
#pragma omp single
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
2013-02-10 15:46:41 -06:00
|
|
|
// Temp frame counters (to keep the frames in order)
|
2013-02-12 01:28:48 -06:00
|
|
|
//frameCount = 0;
|
2013-02-10 15:46:41 -06:00
|
|
|
|
2013-02-06 02:09:21 -06:00
|
|
|
// Loop through each queued image frame
|
|
|
|
|
while (!raw_video_frames.empty())
|
|
|
|
|
{
|
|
|
|
|
// Get front frame (from the queue)
|
|
|
|
|
IDeckLinkMutableVideoFrame* frame = raw_video_frames.front();
|
2013-02-06 02:18:43 -06:00
|
|
|
raw_video_frames.pop_front();
|
2013-02-06 02:09:21 -06:00
|
|
|
|
|
|
|
|
// declare local variables (for OpenMP)
|
|
|
|
|
IDeckLinkOutput *copy_deckLinkOutput(deckLinkOutput);
|
|
|
|
|
IDeckLinkVideoConversion *copy_deckLinkConverter(deckLinkConverter);
|
|
|
|
|
unsigned long copy_frameCount(frameCount);
|
|
|
|
|
|
2013-02-10 15:46:41 -06:00
|
|
|
#pragma omp task firstprivate(copy_deckLinkOutput, copy_deckLinkConverter, frame, copy_frameCount)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
|
|
|
|
// *********** CONVERT YUV source frame to RGB ************
|
|
|
|
|
void *frameBytes;
|
|
|
|
|
void *audioFrameBytes;
|
|
|
|
|
|
|
|
|
|
// Create a new RGB frame object
|
|
|
|
|
IDeckLinkMutableVideoFrame *m_rgbFrame = NULL;
|
|
|
|
|
|
|
|
|
|
int width = videoFrame->GetWidth();
|
|
|
|
|
int height = videoFrame->GetHeight();
|
|
|
|
|
|
|
|
|
|
HRESULT res = copy_deckLinkOutput->CreateVideoFrame(
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
width * 4,
|
|
|
|
|
bmdFormat8BitARGB,
|
|
|
|
|
bmdFrameFlagDefault,
|
|
|
|
|
&m_rgbFrame);
|
|
|
|
|
|
|
|
|
|
if(res != S_OK)
|
|
|
|
|
cout << "BMDOutputDelegate::StartRunning: Error creating RGB frame, res:" << res << endl;
|
|
|
|
|
|
|
|
|
|
// Create a RGB version of this YUV video frame
|
|
|
|
|
copy_deckLinkConverter->ConvertFrame(frame, m_rgbFrame);
|
|
|
|
|
|
|
|
|
|
// Get RGB Byte array
|
|
|
|
|
m_rgbFrame->GetBytes(&frameBytes);
|
|
|
|
|
|
|
|
|
|
// *********** CREATE OPENSHOT FRAME **********
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<openshot::Frame> f(new openshot::Frame(copy_frameCount, width, height, "#000000", 2048, 2));
|
2013-02-06 02:09:21 -06:00
|
|
|
|
|
|
|
|
// Add Image data to openshot frame
|
2015-06-01 00:20:14 -07:00
|
|
|
// TODO: Fix Decklink support with QImage Upgrade
|
|
|
|
|
//f->AddImage(width, height, "ARGB", Magick::CharPixel, (uint8_t*)frameBytes);
|
2013-02-06 02:09:21 -06:00
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
#pragma omp critical (blackmagic_input_queue)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
2013-02-10 15:46:41 -06:00
|
|
|
// Add processed frame to cache (to be recalled in order after the thread pool is done)
|
2016-08-31 02:02:54 -05:00
|
|
|
final_frames.Add(f);
|
2013-02-06 02:09:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Release RGB data
|
|
|
|
|
if (m_rgbFrame)
|
|
|
|
|
m_rgbFrame->Release();
|
|
|
|
|
// Release RGB data
|
|
|
|
|
if (frame)
|
|
|
|
|
frame->Release();
|
|
|
|
|
|
|
|
|
|
} // end task
|
2013-02-10 15:46:41 -06:00
|
|
|
|
|
|
|
|
// Increment frame count
|
|
|
|
|
frameCount++;
|
|
|
|
|
|
2013-02-06 02:09:21 -06:00
|
|
|
} // end while
|
2013-02-10 15:46:41 -06:00
|
|
|
|
2013-02-06 02:09:21 -06:00
|
|
|
} // omp single
|
|
|
|
|
} // omp parallel
|
|
|
|
|
|
2013-02-15 00:23:55 -06:00
|
|
|
// Update final frameCount (since they are done processing now)
|
|
|
|
|
final_frameCount += number_to_process;
|
|
|
|
|
|
2013-02-10 21:16:46 -06:00
|
|
|
|
2013-02-10 15:46:41 -06:00
|
|
|
} // if size > num processors
|
|
|
|
|
} // has video source
|
|
|
|
|
} // if videoFrame
|
2013-02-06 02:09:21 -06:00
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-10 02:19:40 -06:00
|
|
|
HRESULT DeckLinkInputDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
|
2013-02-06 02:09:21 -06:00
|
|
|
{
|
|
|
|
|
return S_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|