OpenShot Library | libopenshot  0.2.3
ChunkReader.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for ChunkReader 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/ChunkReader.h"
29 #include "../include/FFmpegReader.h"
30 
31 using namespace openshot;
32 
33 ChunkReader::ChunkReader(string path, ChunkVersion chunk_version)
34  : path(path), chunk_size(24 * 3), is_open(false), version(chunk_version), local_reader(NULL)
35 {
36  // Check if folder exists?
37  if (!does_folder_exist(path))
38  // Raise exception
39  throw InvalidFile("Chunk folder could not be opened.", path);
40 
41  // Init previous location
42  previous_location.number = 0;
43  previous_location.frame = 0;
44 
45  // Open and Close the reader, to populate it's attributes (such as height, width, etc...)
46  Open();
47  Close();
48 }
49 
50 // Check if folder path existing
51 bool ChunkReader::does_folder_exist(string path)
52 {
53  QDir dir(path.c_str());
54  return dir.exists();
55 }
56 
57 // Load JSON meta data about this chunk folder
58 void ChunkReader::load_json()
59 {
60  // Load path of chunk folder
61  string json_path = QDir::cleanPath(QString(path.c_str()) + QDir::separator() + "info.json").toStdString();
62  stringstream json_string;
63 
64  // Read the JSON file
65  ifstream myfile (json_path.c_str());
66  string line = "";
67  if (myfile.is_open())
68  {
69  while (myfile.good())
70  {
71  getline (myfile, line);
72  json_string << line;
73  }
74  myfile.close();
75  }
76 
77  // Parse JSON string into JSON objects
78  Json::Value root;
79  Json::Reader reader;
80  bool success = reader.parse( json_string.str(), root );
81  if (!success)
82  // Raise exception
83  throw InvalidJSON("Chunk folder could not be opened.", path);
84 
85 
86  // Set info from the JSON objects
87  try
88  {
89  info.has_video = root["has_video"].asBool();
90  info.has_audio = root["has_audio"].asBool();
91  info.duration = root["duration"].asDouble();
92  info.file_size = atoll(root["file_size"].asString().c_str());
93  info.height = root["height"].asInt();
94  info.width = root["width"].asInt();
95  info.pixel_format = root["pixel_format"].asInt();
96  info.fps.num = root["fps"]["num"].asInt();
97  info.fps.den = root["fps"]["den"].asInt();
98  info.video_bit_rate = root["video_bit_rate"].asUInt();
99  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
100  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
101  info.display_ratio.num = root["display_ratio"]["num"].asInt();
102  info.display_ratio.den = root["display_ratio"]["den"].asInt();
103  info.vcodec = root["vcodec"].asString();
104  info.video_length = atoll(root["video_length"].asString().c_str());
105  info.video_stream_index = root["video_stream_index"].asInt();
106  info.video_timebase.num = root["video_timebase"]["num"].asInt();
107  info.video_timebase.den = root["video_timebase"]["den"].asInt();
108  info.interlaced_frame = root["interlaced_frame"].asBool();
109  info.top_field_first = root["top_field_first"].asBool();
110  info.acodec = root["acodec"].asString();
111  info.audio_bit_rate = root["audio_bit_rate"].asUInt();
112  info.sample_rate = root["sample_rate"].asUInt();
113  info.channels = root["channels"].asInt();
114  info.audio_stream_index = root["audio_stream_index"].asInt();
115  info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
116  info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
117 
118  }
119  catch (exception e)
120  {
121  // Error parsing JSON (or missing keys)
122  throw InvalidJSON("JSON could not be parsed (or is invalid).", path);
123  }
124 }
125 
126 // Find the location of a frame in a chunk
127 ChunkLocation ChunkReader::find_chunk_frame(int64_t requested_frame)
128 {
129  // Determine which chunk contains this frame.
130  int64_t chunk_number = (requested_frame / chunk_size) + 1;
131 
132  // Determine which frame in this chunk
133  int64_t start_frame_of_chunk = (chunk_number - 1) * chunk_size;
134  int64_t chunk_frame_number = (requested_frame - start_frame_of_chunk) + 1; // Add 1 to adjust for the 1st frame of every chunk is just there to "stoke" the audio samples from the previous chunk.
135 
136  // Prepare chunk location struct
137  ChunkLocation location = {chunk_number, chunk_frame_number};
138 
139  return location;
140 }
141 
142 // Open chunk folder or file
144 {
145  // Open reader if not already open
146  if (!is_open)
147  {
148  // parse JSON and load info.json file
149  load_json();
150 
151  // Mark as "open"
152  is_open = true;
153  }
154 }
155 
156 // Close image file
158 {
159  // Close all objects, if reader is 'open'
160  if (is_open)
161  {
162  // Mark as "closed"
163  is_open = false;
164  }
165 }
166 
167 // get a formatted path of a specific chunk
168 string ChunkReader::get_chunk_path(int64_t chunk_number, string folder, string extension)
169 {
170  // Create path of new chunk video
171  stringstream chunk_count_string;
172  chunk_count_string << chunk_number;
173  QString padded_count = "%1"; //chunk_count_string.str().c_str();
174  padded_count = padded_count.arg(chunk_count_string.str().c_str(), 6, '0');
175  if (folder.length() != 0 && extension.length() != 0)
176  // Return path with FOLDER and EXTENSION name
177  return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str() + QDir::separator() + padded_count + extension.c_str()).toStdString();
178 
179  else if (folder.length() == 0 && extension.length() != 0)
180  // Return path with NO FOLDER and EXTENSION name
181  return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + padded_count + extension.c_str()).toStdString();
182 
183  else if (folder.length() != 0 && extension.length() == 0)
184  // Return path with FOLDER and NO EXTENSION
185  return QDir::cleanPath(QString(path.c_str()) + QDir::separator() + folder.c_str()).toStdString();
186  else
187  return "";
188 }
189 
190 // Get an openshot::Frame object for a specific frame number of this reader.
191 std::shared_ptr<Frame> ChunkReader::GetFrame(int64_t requested_frame)
192 {
193  // Determine what chunk contains this frame
194  ChunkLocation location = find_chunk_frame(requested_frame);
195 
196  // New Chunk (Close the old reader, and open the new one)
197  if (previous_location.number != location.number)
198  {
199  // Determine version of chunk
200  string folder_name = "";
201  switch (version)
202  {
203  case THUMBNAIL:
204  folder_name = "thumb";
205  break;
206  case PREVIEW:
207  folder_name = "preview";
208  break;
209  case FINAL:
210  folder_name = "final";
211  break;
212  }
213 
214  // Load path of chunk video
215  string chunk_video_path = get_chunk_path(location.number, folder_name, ".webm");
216 
217  // Close existing reader (if needed)
218  if (local_reader)
219  {
220  cout << "Close READER" << endl;
221  // Close and delete old reader
222  local_reader->Close();
223  delete local_reader;
224  }
225 
226  try
227  {
228  cout << "Load READER: " << chunk_video_path << endl;
229  // Load new FFmpegReader
230  local_reader = new FFmpegReader(chunk_video_path);
231  local_reader->Open(); // open reader
232 
233  } catch (InvalidFile)
234  {
235  // Invalid Chunk (possibly it is not found)
236  throw ChunkNotFound(path, requested_frame, location.number, location.frame);
237  }
238 
239  // Set the new location
240  previous_location = location;
241  }
242 
243  // Get the frame (from the current reader)
244  last_frame = local_reader->GetFrame(location.frame);
245 
246  // Update the frame number property
247  last_frame->number = requested_frame;
248 
249  // Return the frame
250  return last_frame;
251 }
252 
253 // Generate JSON string of this object
255 
256  // Return formatted string
257  return JsonValue().toStyledString();
258 }
259 
260 // Generate Json::JsonValue for this object
261 Json::Value ChunkReader::JsonValue() {
262 
263  // Create root json object
264  Json::Value root = ReaderBase::JsonValue(); // get parent properties
265  root["type"] = "ChunkReader";
266  root["path"] = path;
267  stringstream chunk_size_stream;
268  chunk_size_stream << chunk_size;
269  root["chunk_size"] = chunk_size_stream.str();
270  root["chunk_version"] = version;
271 
272  // return JsonValue
273  return root;
274 }
275 
276 // Load JSON string into this object
277 void ChunkReader::SetJson(string value) {
278 
279  // Parse JSON string into JSON objects
280  Json::Value root;
281  Json::Reader reader;
282  bool success = reader.parse( value, root );
283  if (!success)
284  // Raise exception
285  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
286 
287  try
288  {
289  // Set all values that match
290  SetJsonValue(root);
291  }
292  catch (exception e)
293  {
294  // Error parsing JSON (or missing keys)
295  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
296  }
297 }
298 
299 // Load Json::JsonValue into this object
300 void ChunkReader::SetJsonValue(Json::Value root) {
301 
302  // Set parent data
304 
305  // Set data from Json (if key is found)
306  if (!root["path"].isNull())
307  path = root["path"].asString();
308  if (!root["chunk_size"].isNull())
309  chunk_size = atoll(root["chunk_size"].asString().c_str());
310  if (!root["chunk_version"].isNull())
311  version = (ChunkVersion) root["chunk_version"].asInt();
312 
313  // Re-Open path, and re-init everything (if needed)
314  if (is_open)
315  {
316  Close();
317  Open();
318  }
319 }
string Json()
Get and Set JSON methods.
int num
Numerator for the fraction.
Definition: Fraction.h:44
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
void Open()
Open the reader. This is required before you can access frames or data from the reader.
Exception when a required chunk is missing.
Definition: Exceptions.h:56
void SetJson(string value)
Load JSON string into this object.
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:68
void Close()
Close the reader.
float duration
Length of time (in seconds)
Definition: ReaderBase.h:65
The lowest quality stream contained in this chunk file.
Definition: ChunkReader.h:75
string acodec
The name of the audio codec used to encode / decode the video stream.
Definition: ReaderBase.h:80
virtual void Close()=0
Close the reader (and any resources it was consuming)
bool has_video
Determines if this file has a video stream.
Definition: ReaderBase.h:62
Json::Value JsonValue()
Generate Json::JsonValue for this object.
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:73
int64_t file_size
Size of file (in bytes)
Definition: ReaderBase.h:66
int audio_bit_rate
The bit rate of the audio stream (in bytes)
Definition: ReaderBase.h:81
virtual std::shared_ptr< Frame > GetFrame(int64_t number)=0
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:63
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:94
The highest quality stream contained in this chunk file.
Definition: ChunkReader.h:77
int audio_stream_index
The index of the audio stream.
Definition: ReaderBase.h:85
The medium quality stream contained in this chunk file.
Definition: ChunkReader.h:76
int64_t video_length
The number of frames in the video stream.
Definition: ReaderBase.h:75
int height
The height of the video (in pixels)
Definition: ReaderBase.h:67
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
std::shared_ptr< Frame > GetFrame(int64_t requested_frame)
Get an openshot::Frame object for a specific frame number of this reader.
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
Definition: ReaderBase.cpp:114
int64_t number
The chunk number.
Definition: ChunkReader.h:59
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low...
Definition: ChunkReader.h:73
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
Definition: ReaderBase.cpp:169
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:111
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:70
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: ReaderBase.h:77
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: ReaderBase.h:72
Exception for invalid JSON.
Definition: Exceptions.h:152
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: ReaderBase.h:69
int video_bit_rate
The bit rate of the video stream (in bytes)
Definition: ReaderBase.h:71
int64_t frame
The frame number.
Definition: ChunkReader.h:60
Fraction audio_timebase
The audio timebase determines how long each audio packet should be played.
Definition: ReaderBase.h:86
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: ReaderBase.h:74
int den
Denominator for the fraction.
Definition: Fraction.h:45
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:83
This struct holds the location of a frame within a chunk.
Definition: ChunkReader.h:57
int video_stream_index
The index of the video stream.
Definition: ReaderBase.h:76
ChunkReader(string path, ChunkVersion chunk_version)
Constructor for ChunkReader. This automatically opens the chunk file or folder and loads frame 1...
Definition: ChunkReader.cpp:33
virtual void Open()=0
Open the reader (and start consuming resources, such as images or video files)
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:82