2013-09-11 17:32:40 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for DummyReader 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
|
|
|
*/
|
|
|
|
|
|
2012-10-16 16:45:31 -05:00
|
|
|
#include "../include/DummyReader.h"
|
|
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2014-01-05 22:37:11 -06:00
|
|
|
// Blank constructor for DummyReader, with default settings.
|
2014-01-06 00:04:40 -06:00
|
|
|
DummyReader::DummyReader() {
|
2014-01-05 22:37:11 -06:00
|
|
|
|
2014-01-06 00:04:40 -06:00
|
|
|
// Call actual constructor with default values
|
|
|
|
|
DummyReader(Fraction(24,1), 1280, 768, 44100, 2, 30.0);
|
2014-01-05 22:37:11 -06:00
|
|
|
}
|
|
|
|
|
|
2012-10-16 16:45:31 -05:00
|
|
|
// Constructor for DummyReader. Pass a framerate and samplerate.
|
2014-01-06 00:04:40 -06:00
|
|
|
DummyReader::DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration) {
|
|
|
|
|
|
|
|
|
|
// Set key info settings
|
|
|
|
|
info.has_audio = false;
|
|
|
|
|
info.has_video = true;
|
|
|
|
|
info.file_size = width * height * sizeof(int);
|
|
|
|
|
info.vcodec = "raw";
|
|
|
|
|
info.fps = fps;
|
|
|
|
|
info.width = width;
|
|
|
|
|
info.height = height;
|
|
|
|
|
info.sample_rate = sample_rate;
|
|
|
|
|
info.channels = channels;
|
|
|
|
|
info.duration = duration;
|
|
|
|
|
info.video_length = duration * fps.ToFloat();
|
|
|
|
|
info.pixel_ratio.num = 1;
|
|
|
|
|
info.pixel_ratio.den = 1;
|
|
|
|
|
info.video_timebase = fps.Reciprocal();
|
|
|
|
|
info.acodec = "raw";
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
2019-03-14 09:26:56 -07:00
|
|
|
// Open and Close the reader, to populate its attributes (such as height, width, etc...)
|
2012-10-16 16:45:31 -05:00
|
|
|
Open();
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 10:51:40 -07:00
|
|
|
DummyReader::~DummyReader() {
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-16 16:45:31 -05:00
|
|
|
// Open image file
|
2017-10-26 18:44:35 -05:00
|
|
|
void DummyReader::Open()
|
2012-10-16 16:45:31 -05:00
|
|
|
{
|
|
|
|
|
// Open reader if not already open
|
|
|
|
|
if (!is_open)
|
|
|
|
|
{
|
|
|
|
|
// Create or get frame object
|
2017-08-20 17:37:39 -05:00
|
|
|
image_frame = std::make_shared<Frame>(1, info.width, info.height, "#000000", info.sample_rate, info.channels);
|
2012-10-16 16:45:31 -05:00
|
|
|
|
|
|
|
|
// Mark as "open"
|
|
|
|
|
is_open = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close image file
|
|
|
|
|
void DummyReader::Close()
|
|
|
|
|
{
|
|
|
|
|
// Close all objects, if reader is 'open'
|
|
|
|
|
if (is_open)
|
|
|
|
|
{
|
|
|
|
|
// Mark as "closed"
|
|
|
|
|
is_open = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get an openshot::Frame object for a specific frame number of this reader.
|
2017-10-26 18:44:35 -05:00
|
|
|
std::shared_ptr<Frame> DummyReader::GetFrame(int64_t requested_frame)
|
2012-10-16 16:45:31 -05:00
|
|
|
{
|
|
|
|
|
// Check for open reader (or throw exception)
|
|
|
|
|
if (!is_open)
|
|
|
|
|
throw ReaderClosed("The ImageReader is closed. Call Open() before calling this method.", "dummy");
|
|
|
|
|
|
|
|
|
|
if (image_frame)
|
|
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, allowing only a single thread to run the following code at one time
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(getFrameCriticalSection);
|
|
|
|
|
|
2012-10-16 16:45:31 -05:00
|
|
|
// Always return same frame (regardless of which frame number was requested)
|
|
|
|
|
image_frame->number = requested_frame;
|
|
|
|
|
return image_frame;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// no frame loaded
|
|
|
|
|
throw InvalidFile("No frame could be created from this type of file.", "dummy");
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-07 21:09:55 -06:00
|
|
|
// Generate JSON string of this object
|
|
|
|
|
string DummyReader::Json() {
|
2012-10-16 16:45:31 -05:00
|
|
|
|
2013-12-07 21:09:55 -06:00
|
|
|
// Return formatted string
|
|
|
|
|
return JsonValue().toStyledString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate Json::JsonValue for this object
|
|
|
|
|
Json::Value DummyReader::JsonValue() {
|
|
|
|
|
|
|
|
|
|
// Create root json object
|
|
|
|
|
Json::Value root = ReaderBase::JsonValue(); // get parent properties
|
|
|
|
|
root["type"] = "DummyReader";
|
|
|
|
|
|
|
|
|
|
// return JsonValue
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load JSON string into this object
|
2017-10-26 18:44:35 -05:00
|
|
|
void DummyReader::SetJson(string value) {
|
2013-12-07 21:09:55 -06:00
|
|
|
|
|
|
|
|
// Parse JSON string into JSON objects
|
|
|
|
|
Json::Value root;
|
2019-06-19 21:20:04 -04:00
|
|
|
Json::CharReaderBuilder rbuilder;
|
|
|
|
|
Json::CharReader* reader(rbuilder.newCharReader());
|
|
|
|
|
|
|
|
|
|
string errors;
|
2019-07-11 05:00:47 -04:00
|
|
|
bool success = reader->parse( value.c_str(),
|
2019-06-19 21:20:04 -04:00
|
|
|
value.c_str() + value.size(), &root, &errors );
|
2019-07-11 05:00:47 -04:00
|
|
|
delete reader;
|
|
|
|
|
|
2013-12-07 21:09:55 -06:00
|
|
|
if (!success)
|
|
|
|
|
// Raise exception
|
2019-08-27 15:47:39 -04:00
|
|
|
throw InvalidJSON("JSON could not be parsed (or is invalid)");
|
2013-12-07 21:09:55 -06:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Set all values that match
|
|
|
|
|
SetJsonValue(root);
|
|
|
|
|
}
|
2019-07-03 12:58:02 -04:00
|
|
|
catch (const std::exception& e)
|
2013-12-07 21:09:55 -06:00
|
|
|
{
|
|
|
|
|
// Error parsing JSON (or missing keys)
|
2019-08-27 15:47:39 -04:00
|
|
|
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
|
2013-12-07 21:09:55 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load Json::JsonValue into this object
|
2017-10-26 18:44:35 -05:00
|
|
|
void DummyReader::SetJsonValue(Json::Value root) {
|
2013-12-07 21:09:55 -06:00
|
|
|
|
|
|
|
|
// Set parent data
|
|
|
|
|
ReaderBase::SetJsonValue(root);
|
|
|
|
|
|
|
|
|
|
}
|