OpenShot Library | libopenshot  0.6.0
ReaderBase.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include <iostream>
14 #include <iomanip>
15 #include <algorithm>
16 
17 #include "ReaderBase.h"
18 #include "ClipBase.h"
19 #include "Frame.h"
20 
21 #include "Json.h"
22 
23 
24 using namespace openshot;
25 
28 {
29  // Initialize info struct
30  info.has_video = false;
31  info.has_audio = false;
32  info.has_single_image = false;
33  info.duration = 0.0;
34  info.file_size = 0;
35  info.height = 0;
36  info.width = 0;
37  info.pixel_format = -1;
38  info.fps = Fraction();
39  info.video_bit_rate = 0;
42  info.vcodec = "";
43  info.video_length = 0;
46  info.interlaced_frame = false;
47  info.top_field_first = true;
48  info.acodec = "";
49  info.audio_bit_rate = 0;
50  info.sample_rate = 0;
51  info.channels = 0;
55 
56  // Init parent clip
57  clip = NULL;
58  max_decode_width = 0;
60 }
61 
62 // Display file information
63 void ReaderBase::DisplayInfo(std::ostream* out) {
64  *out << std::fixed << std::setprecision(2) << std::boolalpha;
65  *out << "----------------------------" << std::endl;
66  *out << "----- File Information -----" << std::endl;
67  *out << "----------------------------" << std::endl;
68  *out << "--> Has Video: " << info.has_video << std::endl;
69  *out << "--> Has Audio: " << info.has_audio << std::endl;
70  *out << "--> Has Single Image: " << info.has_single_image << std::endl;
71  *out << "--> Duration: " << info.duration << " Seconds" << std::endl;
72  *out << "--> File Size: " << double(info.file_size) / 1024 / 1024 << " MB" << std::endl;
73  *out << "----------------------------" << std::endl;
74  *out << "----- Video Attributes -----" << std::endl;
75  *out << "----------------------------" << std::endl;
76  *out << "--> Width: " << info.width << std::endl;
77  *out << "--> Height: " << info.height << std::endl;
78  *out << "--> Pixel Format: " << info.pixel_format << std::endl;
79  *out << "--> Frames Per Second: " << info.fps.ToDouble() << " (" << info.fps.num << "/" << info.fps.den << ")" << std::endl;
80  *out << "--> Video Bit Rate: " << info.video_bit_rate/1000 << " kb/s" << std::endl;
81  *out << "--> Pixel Ratio: " << info.pixel_ratio.ToDouble() << " (" << info.pixel_ratio.num << "/" << info.pixel_ratio.den << ")" << std::endl;
82  *out << "--> Display Aspect Ratio: " << info.display_ratio.ToDouble() << " (" << info.display_ratio.num << "/" << info.display_ratio.den << ")" << std::endl;
83  *out << "--> Video Codec: " << info.vcodec << std::endl;
84  *out << "--> Video Length: " << info.video_length << " Frames" << std::endl;
85  *out << "--> Video Stream Index: " << info.video_stream_index << std::endl;
86  *out << "--> Video Timebase: " << info.video_timebase.ToDouble() << " (" << info.video_timebase.num << "/" << info.video_timebase.den << ")" << std::endl;
87  *out << "--> Interlaced: " << info.interlaced_frame << std::endl;
88  *out << "--> Interlaced: Top Field First: " << info.top_field_first << std::endl;
89  *out << "----------------------------" << std::endl;
90  *out << "----- Audio Attributes -----" << std::endl;
91  *out << "----------------------------" << std::endl;
92  *out << "--> Audio Codec: " << info.acodec << std::endl;
93  *out << "--> Audio Bit Rate: " << info.audio_bit_rate/1000 << " kb/s" << std::endl;
94  *out << "--> Sample Rate: " << info.sample_rate << " Hz" << std::endl;
95  *out << "--> # of Channels: " << info.channels << std::endl;
96  *out << "--> Channel Layout: " << info.channel_layout << std::endl;
97  *out << "--> Audio Stream Index: " << info.audio_stream_index << std::endl;
98  *out << "--> Audio Timebase: " << info.audio_timebase.ToDouble() << " (" << info.audio_timebase.num << "/" << info.audio_timebase.den << ")" << std::endl;
99  *out << "----------------------------" << std::endl;
100  *out << "--------- Metadata ---------" << std::endl;
101  *out << "----------------------------" << std::endl;
102 
103  // Iterate through metadata
104  for (auto it : info.metadata)
105  *out << "--> " << it.first << ": " << it.second << std::endl;
106 }
107 
108 // Generate Json::Value for this object
109 Json::Value ReaderBase::JsonValue() const {
110 
111  // Create root json object
112  Json::Value root;
113  root["has_video"] = info.has_video;
114  root["has_audio"] = info.has_audio;
115  root["has_single_image"] = info.has_single_image;
116  root["duration"] = info.duration;
117  root["file_size"] = static_cast<Json::Value::Int64>(info.file_size); // direct 64-bit int
118  root["height"] = info.height;
119  root["width"] = info.width;
120  root["pixel_format"] = info.pixel_format;
121  root["fps"] = Json::Value(Json::objectValue);
122  root["fps"]["num"] = info.fps.num;
123  root["fps"]["den"] = info.fps.den;
124  root["video_bit_rate"] = info.video_bit_rate;
125  root["pixel_ratio"] = Json::Value(Json::objectValue);
126  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
127  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
128  root["display_ratio"] = Json::Value(Json::objectValue);
129  root["display_ratio"]["num"] = info.display_ratio.num;
130  root["display_ratio"]["den"] = info.display_ratio.den;
131  root["vcodec"] = info.vcodec;
132  root["video_length"] = static_cast<Json::Value::Int64>(info.video_length);
133  root["video_stream_index"] = info.video_stream_index;
134  root["video_timebase"] = Json::Value(Json::objectValue);
135  root["video_timebase"]["num"] = info.video_timebase.num;
136  root["video_timebase"]["den"] = info.video_timebase.den;
137  root["interlaced_frame"] = info.interlaced_frame;
138  root["top_field_first"] = info.top_field_first;
139  root["acodec"] = info.acodec;
140  root["audio_bit_rate"] = info.audio_bit_rate;
141  root["sample_rate"] = info.sample_rate;
142  root["channels"] = info.channels;
143  root["channel_layout"] = info.channel_layout;
144  root["audio_stream_index"] = info.audio_stream_index;
145  root["audio_timebase"] = Json::Value(Json::objectValue);
146  root["audio_timebase"]["num"] = info.audio_timebase.num;
147  root["audio_timebase"]["den"] = info.audio_timebase.den;
148 
149  // Append metadata map
150  root["metadata"] = Json::Value(Json::objectValue);
151 
152  for (const auto it : info.metadata)
153  root["metadata"][it.first] = it.second;
154 
155  // return JsonValue
156  return root;
157 }
158 
159 // Load Json::Value into this object
160 void ReaderBase::SetJsonValue(const Json::Value root) {
161 
162  // Set data from Json (if key is found)
163  if (!root["has_video"].isNull())
164  info.has_video = root["has_video"].asBool();
165  if (!root["has_audio"].isNull())
166  info.has_audio = root["has_audio"].asBool();
167  if (!root["has_single_image"].isNull())
168  info.has_single_image = root["has_single_image"].asBool();
169  if (!root["duration"].isNull())
170  info.duration = root["duration"].asDouble();
171  if (!root["file_size"].isNull())
172  info.file_size = std::stoll(root["file_size"].asString());
173  if (!root["height"].isNull())
174  info.height = root["height"].asInt();
175  if (!root["width"].isNull())
176  info.width = root["width"].asInt();
177  if (!root["pixel_format"].isNull())
178  info.pixel_format = root["pixel_format"].asInt();
179  if (!root["fps"].isNull() && root["fps"].isObject()) {
180  if (!root["fps"]["num"].isNull())
181  info.fps.num = root["fps"]["num"].asInt();
182  if (!root["fps"]["den"].isNull())
183  info.fps.den = root["fps"]["den"].asInt();
184  }
185  if (!root["video_bit_rate"].isNull())
186  info.video_bit_rate = root["video_bit_rate"].asInt();
187  if (!root["pixel_ratio"].isNull() && root["pixel_ratio"].isObject()) {
188  if (!root["pixel_ratio"]["num"].isNull())
189  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
190  if (!root["pixel_ratio"]["den"].isNull())
191  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
192  }
193  if (!root["display_ratio"].isNull() && root["display_ratio"].isObject()) {
194  if (!root["display_ratio"]["num"].isNull())
195  info.display_ratio.num = root["display_ratio"]["num"].asInt();
196  if (!root["display_ratio"]["den"].isNull())
197  info.display_ratio.den = root["display_ratio"]["den"].asInt();
198  }
199  if (!root["vcodec"].isNull())
200  info.vcodec = root["vcodec"].asString();
201  if (!root["video_length"].isNull())
202  info.video_length = std::stoll(root["video_length"].asString());
203  if (!root["video_stream_index"].isNull())
204  info.video_stream_index = root["video_stream_index"].asInt();
205  if (!root["video_timebase"].isNull() && root["video_timebase"].isObject()) {
206  if (!root["video_timebase"]["num"].isNull())
207  info.video_timebase.num = root["video_timebase"]["num"].asInt();
208  if (!root["video_timebase"]["den"].isNull())
209  info.video_timebase.den = root["video_timebase"]["den"].asInt();
210  }
211  if (!root["interlaced_frame"].isNull())
212  info.interlaced_frame = root["interlaced_frame"].asBool();
213  if (!root["top_field_first"].isNull())
214  info.top_field_first = root["top_field_first"].asBool();
215  if (!root["acodec"].isNull())
216  info.acodec = root["acodec"].asString();
217 
218  if (!root["audio_bit_rate"].isNull())
219  info.audio_bit_rate = root["audio_bit_rate"].asInt();
220  if (!root["sample_rate"].isNull())
221  info.sample_rate = root["sample_rate"].asInt();
222  if (!root["channels"].isNull())
223  info.channels = root["channels"].asInt();
224  if (!root["channel_layout"].isNull())
225  info.channel_layout = (ChannelLayout) root["channel_layout"].asInt();
226  if (!root["audio_stream_index"].isNull())
227  info.audio_stream_index = root["audio_stream_index"].asInt();
228  if (!root["audio_timebase"].isNull() && root["audio_timebase"].isObject()) {
229  if (!root["audio_timebase"]["num"].isNull())
230  info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
231  if (!root["audio_timebase"]["den"].isNull())
232  info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
233  }
234  if (!root["metadata"].isNull() && root["metadata"].isObject()) {
235  for( Json::Value::const_iterator itr = root["metadata"].begin() ; itr != root["metadata"].end() ; itr++ ) {
236  std::string key = itr.key().asString();
237  info.metadata[key] = root["metadata"][key].asString();
238  }
239  }
240 }
241 
244  return clip;
245 }
246 
249  clip = new_clip;
250 }
251 
252 void ReaderBase::SetMaxDecodeSize(int width, int height) {
253  max_decode_width = std::max(0, width);
254  max_decode_height = std::max(0, height);
255 }
256 
258  return max_decode_width;
259 }
260 
262  return max_decode_height;
263 }
264 
266  return max_decode_width > 0 && max_decode_height > 0;
267 }
openshot::ReaderBase::DisplayInfo
void DisplayInfo(std::ostream *out=&std::cout)
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:63
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:60
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition: ReaderBase.cpp:109
openshot::ReaderBase::MaxDecodeHeight
int MaxDecodeHeight() const
Return the current maximum decoded frame height (0 when unlimited).
Definition: ReaderBase.cpp:261
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition: ReaderBase.cpp:160
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::ReaderBase::max_decode_width
int max_decode_width
Optional maximum decoded frame width (0 disables the limit)
Definition: ReaderBase.h:81
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:90
openshot::ReaderInfo::interlaced_frame
bool interlaced_frame
Definition: ReaderBase.h:56
openshot::ReaderInfo::audio_bit_rate
int audio_bit_rate
The bit rate of the audio stream (in bytes)
Definition: ReaderBase.h:59
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition: ReaderBase.h:43
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:40
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:46
openshot::Fraction::ToDouble
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:40
openshot::ReaderBase::clip
openshot::ClipBase * clip
Pointer to the parent clip instance (if any)
Definition: ReaderBase.h:80
openshot::ReaderBase::MaxDecodeWidth
int MaxDecodeWidth() const
Return the current maximum decoded frame width (0 when unlimited).
Definition: ReaderBase.cpp:257
openshot::LAYOUT_MONO
@ LAYOUT_MONO
Definition: ChannelLayouts.h:30
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:53
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:45
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:32
openshot::ReaderBase::SetMaxDecodeSize
void SetMaxDecodeSize(int width, int height)
Set an optional maximum decoded frame size. Use 0,0 to disable the limit.
Definition: ReaderBase.cpp:252
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:41
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:44
openshot::ReaderInfo::has_single_image
bool has_single_image
Determines if this file only contains a single image.
Definition: ReaderBase.h:42
openshot::ReaderInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:55
openshot::ReaderBase::max_decode_height
int max_decode_height
Optional maximum decoded frame height (0 disables the limit)
Definition: ReaderBase.h:82
openshot::ReaderInfo::metadata
std::map< std::string, std::string > metadata
An optional map/dictionary of metadata for this reader.
Definition: ReaderBase.h:65
Frame.h
Header file for Frame class.
openshot::ReaderInfo::audio_stream_index
int audio_stream_index
The index of the audio stream.
Definition: ReaderBase.h:63
openshot::ReaderInfo::audio_timebase
openshot::Fraction audio_timebase
The audio timebase determines how long each audio packet should be played.
Definition: ReaderBase.h:64
openshot::ReaderInfo::pixel_format
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: ReaderBase.h:47
openshot::ReaderInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:52
ReaderBase.h
Header file for ReaderBase class.
openshot::ReaderInfo::channel_layout
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:62
openshot::ReaderBase::ReaderBase
ReaderBase()
Constructor for the base reader, where many things are initialized.
Definition: ReaderBase.cpp:27
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:48
openshot::ReaderInfo::video_bit_rate
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition: ReaderBase.h:49
openshot::ReaderInfo::top_field_first
bool top_field_first
Definition: ReaderBase.h:57
openshot::ChannelLayout
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
Definition: ChannelLayouts.h:28
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:50
Json.h
Header file for JSON class.
openshot::ReaderInfo::video_stream_index
int video_stream_index
The index of the video stream.
Definition: ReaderBase.h:54
openshot::ReaderBase::HasMaxDecodeSize
bool HasMaxDecodeSize() const
Return true when a maximum decoded frame size is active.
Definition: ReaderBase.cpp:265
openshot::ClipBase
This abstract class is the base class, used by all clips in libopenshot.
Definition: ClipBase.h:32
openshot::ReaderInfo::acodec
std::string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:58
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:51
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:61
ClipBase.h
Header file for ClipBase class.
openshot::ReaderBase::ParentClip
openshot::ClipBase * ParentClip()
Parent clip object of this reader (which can be unparented and NULL)
Definition: ReaderBase.cpp:243