2013-09-12 17:52:10 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for ReaderBase class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2013-09-12 17:52:10 -05:00
|
|
|
|
2021-04-19 20:38:03 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "ReaderBase.h"
|
2021-09-18 04:16:30 -04:00
|
|
|
#include "ClipBase.h"
|
|
|
|
|
#include "Frame.h"
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2021-04-19 20:38:03 -04:00
|
|
|
#include "Json.h"
|
|
|
|
|
|
2021-10-27 14:34:05 -04:00
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
using namespace openshot;
|
|
|
|
|
|
2014-08-27 09:44:27 -05:00
|
|
|
/// Constructor for the base reader, where many things are initialized.
|
|
|
|
|
ReaderBase::ReaderBase()
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2014-08-27 09:44:27 -05:00
|
|
|
// Initialize info struct
|
2011-10-11 08:44:27 -05:00
|
|
|
info.has_video = false;
|
|
|
|
|
info.has_audio = false;
|
2015-09-28 22:05:50 -05:00
|
|
|
info.has_single_image = false;
|
2011-10-11 08:44:27 -05:00
|
|
|
info.duration = 0.0;
|
|
|
|
|
info.file_size = 0;
|
|
|
|
|
info.height = 0;
|
|
|
|
|
info.width = 0;
|
|
|
|
|
info.pixel_format = -1;
|
|
|
|
|
info.fps = Fraction();
|
|
|
|
|
info.video_bit_rate = 0;
|
|
|
|
|
info.pixel_ratio = Fraction();
|
|
|
|
|
info.display_ratio = Fraction();
|
|
|
|
|
info.vcodec = "";
|
|
|
|
|
info.video_length = 0;
|
|
|
|
|
info.video_stream_index = -1;
|
|
|
|
|
info.video_timebase = Fraction();
|
2011-12-11 20:42:50 -06:00
|
|
|
info.interlaced_frame = false;
|
|
|
|
|
info.top_field_first = true;
|
2011-10-11 08:44:27 -05:00
|
|
|
info.acodec = "";
|
|
|
|
|
info.audio_bit_rate = 0;
|
|
|
|
|
info.sample_rate = 0;
|
|
|
|
|
info.channels = 0;
|
2015-02-05 00:00:52 -06:00
|
|
|
info.channel_layout = LAYOUT_MONO;
|
2011-10-11 08:44:27 -05:00
|
|
|
info.audio_stream_index = -1;
|
|
|
|
|
info.audio_timebase = Fraction();
|
2019-01-19 02:18:52 -06:00
|
|
|
|
|
|
|
|
// Init parent clip
|
Large refactor of Timeline, TimelineBase, ClipBase, and Clip, to allow a Clip access to the parent timeline instance (if available), and thus, certain properties (preview size, timeline FPS, etc...). This allows for a simpler rendering of Clip keyframes (during the Clip::GetFrame method), and a simpler Timeline class, that can change the preview window size dynamically and no longer requires a Singleton Settings class.
- Also removed "crop" from Clip class, as it was never implmeneted correctly, and we have a fully functional "crop" effect when needed
- Added caching to Clip class, to optimize previewing of cached frames (much faster than previous)
2020-10-04 16:59:21 -05:00
|
|
|
clip = NULL;
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Display file information
|
2021-04-19 20:38:03 -04:00
|
|
|
void ReaderBase::DisplayInfo(std::ostream* out) {
|
|
|
|
|
*out << std::fixed << std::setprecision(2) << std::boolalpha;
|
|
|
|
|
*out << "----------------------------" << std::endl;
|
|
|
|
|
*out << "----- File Information -----" << std::endl;
|
|
|
|
|
*out << "----------------------------" << std::endl;
|
|
|
|
|
*out << "--> Has Video: " << info.has_video << std::endl;
|
|
|
|
|
*out << "--> Has Audio: " << info.has_audio << std::endl;
|
|
|
|
|
*out << "--> Has Single Image: " << info.has_single_image << std::endl;
|
|
|
|
|
*out << "--> Duration: " << info.duration << " Seconds" << std::endl;
|
|
|
|
|
*out << "--> File Size: " << double(info.file_size) / 1024 / 1024 << " MB" << std::endl;
|
|
|
|
|
*out << "----------------------------" << std::endl;
|
|
|
|
|
*out << "----- Video Attributes -----" << std::endl;
|
|
|
|
|
*out << "----------------------------" << std::endl;
|
|
|
|
|
*out << "--> Width: " << info.width << std::endl;
|
|
|
|
|
*out << "--> Height: " << info.height << std::endl;
|
|
|
|
|
*out << "--> Pixel Format: " << info.pixel_format << std::endl;
|
|
|
|
|
*out << "--> Frames Per Second: " << info.fps.ToDouble() << " (" << info.fps.num << "/" << info.fps.den << ")" << std::endl;
|
|
|
|
|
*out << "--> Video Bit Rate: " << info.video_bit_rate/1000 << " kb/s" << std::endl;
|
|
|
|
|
*out << "--> Pixel Ratio: " << info.pixel_ratio.ToDouble() << " (" << info.pixel_ratio.num << "/" << info.pixel_ratio.den << ")" << std::endl;
|
|
|
|
|
*out << "--> Display Aspect Ratio: " << info.display_ratio.ToDouble() << " (" << info.display_ratio.num << "/" << info.display_ratio.den << ")" << std::endl;
|
|
|
|
|
*out << "--> Video Codec: " << info.vcodec << std::endl;
|
|
|
|
|
*out << "--> Video Length: " << info.video_length << " Frames" << std::endl;
|
|
|
|
|
*out << "--> Video Stream Index: " << info.video_stream_index << std::endl;
|
|
|
|
|
*out << "--> Video Timebase: " << info.video_timebase.ToDouble() << " (" << info.video_timebase.num << "/" << info.video_timebase.den << ")" << std::endl;
|
|
|
|
|
*out << "--> Interlaced: " << info.interlaced_frame << std::endl;
|
|
|
|
|
*out << "--> Interlaced: Top Field First: " << info.top_field_first << std::endl;
|
|
|
|
|
*out << "----------------------------" << std::endl;
|
|
|
|
|
*out << "----- Audio Attributes -----" << std::endl;
|
|
|
|
|
*out << "----------------------------" << std::endl;
|
|
|
|
|
*out << "--> Audio Codec: " << info.acodec << std::endl;
|
|
|
|
|
*out << "--> Audio Bit Rate: " << info.audio_bit_rate/1000 << " kb/s" << std::endl;
|
|
|
|
|
*out << "--> Sample Rate: " << info.sample_rate << " Hz" << std::endl;
|
|
|
|
|
*out << "--> # of Channels: " << info.channels << std::endl;
|
|
|
|
|
*out << "--> Channel Layout: " << info.channel_layout << std::endl;
|
|
|
|
|
*out << "--> Audio Stream Index: " << info.audio_stream_index << std::endl;
|
|
|
|
|
*out << "--> Audio Timebase: " << info.audio_timebase.ToDouble() << " (" << info.audio_timebase.num << "/" << info.audio_timebase.den << ")" << std::endl;
|
|
|
|
|
*out << "----------------------------" << std::endl;
|
|
|
|
|
*out << "--------- Metadata ---------" << std::endl;
|
|
|
|
|
*out << "----------------------------" << std::endl;
|
2018-02-03 01:57:18 -06:00
|
|
|
|
|
|
|
|
// Iterate through metadata
|
2019-12-27 01:01:48 -05:00
|
|
|
for (auto it : info.metadata)
|
2021-04-19 20:38:03 -04:00
|
|
|
*out << "--> " << it.first << ": " << it.second << std::endl;
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Generate Json::Value for this object
|
|
|
|
|
Json::Value ReaderBase::JsonValue() const {
|
2013-12-07 16:52:09 -06:00
|
|
|
|
2013-12-03 00:13:25 -06:00
|
|
|
// Create root json object
|
|
|
|
|
Json::Value root;
|
|
|
|
|
root["has_video"] = info.has_video;
|
|
|
|
|
root["has_audio"] = info.has_audio;
|
2015-09-28 22:05:50 -05:00
|
|
|
root["has_single_image"] = info.has_single_image;
|
2013-12-03 00:13:25 -06:00
|
|
|
root["duration"] = info.duration;
|
2019-08-04 22:41:51 -04:00
|
|
|
std::stringstream filesize_stream;
|
2013-12-03 00:13:25 -06:00
|
|
|
filesize_stream << info.file_size;
|
|
|
|
|
root["file_size"] = filesize_stream.str();
|
|
|
|
|
root["height"] = info.height;
|
|
|
|
|
root["width"] = info.width;
|
|
|
|
|
root["pixel_format"] = info.pixel_format;
|
|
|
|
|
root["fps"] = Json::Value(Json::objectValue);
|
|
|
|
|
root["fps"]["num"] = info.fps.num;
|
|
|
|
|
root["fps"]["den"] = info.fps.den;
|
|
|
|
|
root["video_bit_rate"] = info.video_bit_rate;
|
|
|
|
|
root["pixel_ratio"] = Json::Value(Json::objectValue);
|
|
|
|
|
root["pixel_ratio"]["num"] = info.pixel_ratio.num;
|
|
|
|
|
root["pixel_ratio"]["den"] = info.pixel_ratio.den;
|
|
|
|
|
root["display_ratio"] = Json::Value(Json::objectValue);
|
|
|
|
|
root["display_ratio"]["num"] = info.display_ratio.num;
|
|
|
|
|
root["display_ratio"]["den"] = info.display_ratio.den;
|
|
|
|
|
root["vcodec"] = info.vcodec;
|
2019-08-04 22:41:51 -04:00
|
|
|
std::stringstream video_length_stream;
|
2013-12-03 00:13:25 -06:00
|
|
|
video_length_stream << info.video_length;
|
|
|
|
|
root["video_length"] = video_length_stream.str();
|
|
|
|
|
root["video_stream_index"] = info.video_stream_index;
|
|
|
|
|
root["video_timebase"] = Json::Value(Json::objectValue);
|
|
|
|
|
root["video_timebase"]["num"] = info.video_timebase.num;
|
|
|
|
|
root["video_timebase"]["den"] = info.video_timebase.den;
|
|
|
|
|
root["interlaced_frame"] = info.interlaced_frame;
|
|
|
|
|
root["top_field_first"] = info.top_field_first;
|
|
|
|
|
root["acodec"] = info.acodec;
|
|
|
|
|
root["audio_bit_rate"] = info.audio_bit_rate;
|
|
|
|
|
root["sample_rate"] = info.sample_rate;
|
|
|
|
|
root["channels"] = info.channels;
|
2015-02-05 00:00:52 -06:00
|
|
|
root["channel_layout"] = info.channel_layout;
|
2013-12-03 00:13:25 -06:00
|
|
|
root["audio_stream_index"] = info.audio_stream_index;
|
|
|
|
|
root["audio_timebase"] = Json::Value(Json::objectValue);
|
|
|
|
|
root["audio_timebase"]["num"] = info.audio_timebase.num;
|
|
|
|
|
root["audio_timebase"]["den"] = info.audio_timebase.den;
|
|
|
|
|
|
2018-02-03 01:57:18 -06:00
|
|
|
// Append metadata map
|
|
|
|
|
root["metadata"] = Json::Value(Json::objectValue);
|
2020-01-20 15:25:40 -05:00
|
|
|
|
|
|
|
|
for (const auto it : info.metadata)
|
2019-12-27 01:01:48 -05:00
|
|
|
root["metadata"][it.first] = it.second;
|
2018-02-03 01:57:18 -06:00
|
|
|
|
2013-12-07 16:52:09 -06:00
|
|
|
// return JsonValue
|
|
|
|
|
return root;
|
2013-12-03 00:13:25 -06:00
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Load Json::Value into this object
|
|
|
|
|
void ReaderBase::SetJsonValue(const Json::Value root) {
|
2013-12-07 16:52:09 -06:00
|
|
|
|
|
|
|
|
// Set data from Json (if key is found)
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["has_video"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.has_video = root["has_video"].asBool();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["has_audio"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.has_audio = root["has_audio"].asBool();
|
2015-09-28 22:05:50 -05:00
|
|
|
if (!root["has_single_image"].isNull())
|
|
|
|
|
info.has_single_image = root["has_single_image"].asBool();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["duration"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.duration = root["duration"].asDouble();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["file_size"].isNull())
|
2019-08-10 11:31:08 -04:00
|
|
|
info.file_size = std::stoll(root["file_size"].asString());
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["height"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.height = root["height"].asInt();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["width"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.width = root["width"].asInt();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["pixel_format"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.pixel_format = root["pixel_format"].asInt();
|
2014-05-16 14:48:25 -05:00
|
|
|
if (!root["fps"].isNull() && root["fps"].isObject()) {
|
|
|
|
|
if (!root["fps"]["num"].isNull())
|
|
|
|
|
info.fps.num = root["fps"]["num"].asInt();
|
|
|
|
|
if (!root["fps"]["den"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.fps.den = root["fps"]["den"].asInt();
|
|
|
|
|
}
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["video_bit_rate"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.video_bit_rate = root["video_bit_rate"].asInt();
|
2014-05-16 14:48:25 -05:00
|
|
|
if (!root["pixel_ratio"].isNull() && root["pixel_ratio"].isObject()) {
|
|
|
|
|
if (!root["pixel_ratio"]["num"].isNull())
|
|
|
|
|
info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
|
|
|
|
|
if (!root["pixel_ratio"]["den"].isNull())
|
|
|
|
|
info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
|
2013-12-07 16:52:09 -06:00
|
|
|
}
|
2014-05-16 14:48:25 -05:00
|
|
|
if (!root["display_ratio"].isNull() && root["display_ratio"].isObject()) {
|
|
|
|
|
if (!root["display_ratio"]["num"].isNull())
|
|
|
|
|
info.display_ratio.num = root["display_ratio"]["num"].asInt();
|
|
|
|
|
if (!root["display_ratio"]["den"].isNull())
|
|
|
|
|
info.display_ratio.den = root["display_ratio"]["den"].asInt();
|
2013-12-07 16:52:09 -06:00
|
|
|
}
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["vcodec"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.vcodec = root["vcodec"].asString();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["video_length"].isNull())
|
2019-08-10 11:31:08 -04:00
|
|
|
info.video_length = std::stoll(root["video_length"].asString());
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["video_stream_index"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.video_stream_index = root["video_stream_index"].asInt();
|
2014-05-16 14:48:25 -05:00
|
|
|
if (!root["video_timebase"].isNull() && root["video_timebase"].isObject()) {
|
|
|
|
|
if (!root["video_timebase"]["num"].isNull())
|
|
|
|
|
info.video_timebase.num = root["video_timebase"]["num"].asInt();
|
|
|
|
|
if (!root["video_timebase"]["den"].isNull())
|
|
|
|
|
info.video_timebase.den = root["video_timebase"]["den"].asInt();
|
2013-12-07 16:52:09 -06:00
|
|
|
}
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["interlaced_frame"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.interlaced_frame = root["interlaced_frame"].asBool();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["top_field_first"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.top_field_first = root["top_field_first"].asBool();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["acodec"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.acodec = root["acodec"].asString();
|
|
|
|
|
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["audio_bit_rate"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.audio_bit_rate = root["audio_bit_rate"].asInt();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["sample_rate"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.sample_rate = root["sample_rate"].asInt();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["channels"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.channels = root["channels"].asInt();
|
2015-02-05 00:00:52 -06:00
|
|
|
if (!root["channel_layout"].isNull())
|
|
|
|
|
info.channel_layout = (ChannelLayout) root["channel_layout"].asInt();
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["audio_stream_index"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
info.audio_stream_index = root["audio_stream_index"].asInt();
|
2014-05-16 14:48:25 -05:00
|
|
|
if (!root["audio_timebase"].isNull() && root["audio_timebase"].isObject()) {
|
|
|
|
|
if (!root["audio_timebase"]["num"].isNull())
|
|
|
|
|
info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
|
|
|
|
|
if (!root["audio_timebase"]["den"].isNull())
|
|
|
|
|
info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
|
2013-12-07 16:52:09 -06:00
|
|
|
}
|
2018-02-03 01:57:18 -06:00
|
|
|
if (!root["metadata"].isNull() && root["metadata"].isObject()) {
|
2019-12-27 08:51:51 -05:00
|
|
|
for( Json::Value::const_iterator itr = root["metadata"].begin() ; itr != root["metadata"].end() ; itr++ ) {
|
2019-08-04 22:41:51 -04:00
|
|
|
std::string key = itr.key().asString();
|
2018-02-03 01:57:18 -06:00
|
|
|
info.metadata[key] = root["metadata"][key].asString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-07 16:52:09 -06:00
|
|
|
}
|
Large refactor of Timeline, TimelineBase, ClipBase, and Clip, to allow a Clip access to the parent timeline instance (if available), and thus, certain properties (preview size, timeline FPS, etc...). This allows for a simpler rendering of Clip keyframes (during the Clip::GetFrame method), and a simpler Timeline class, that can change the preview window size dynamically and no longer requires a Singleton Settings class.
- Also removed "crop" from Clip class, as it was never implmeneted correctly, and we have a fully functional "crop" effect when needed
- Added caching to Clip class, to optimize previewing of cached frames (much faster than previous)
2020-10-04 16:59:21 -05:00
|
|
|
|
|
|
|
|
/// Parent clip object of this reader (which can be unparented and NULL)
|
|
|
|
|
openshot::ClipBase* ReaderBase::ParentClip() {
|
|
|
|
|
return clip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set parent clip object of this reader
|
|
|
|
|
void ReaderBase::ParentClip(openshot::ClipBase* new_clip) {
|
|
|
|
|
clip = new_clip;
|
|
|
|
|
}
|