OpenShot Library | libopenshot  0.1.9
ChunkReader.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header 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 #ifndef OPENSHOT_CHUNK_READER_H
29 #define OPENSHOT_CHUNK_READER_H
30 
31 #include "ReaderBase.h"
32 #include "FFmpegReader.h"
33 
34 #include <cmath>
35 #include <ctime>
36 #include <iostream>
37 #include <fstream>
38 #include <omp.h>
39 #include <QtCore/qdir.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <memory>
43 #include "Json.h"
44 #include "CacheMemory.h"
45 #include "Exceptions.h"
46 
47 using namespace std;
48 
49 namespace openshot
50 {
51 
52  /**
53  * @brief This struct holds the location of a frame within a chunk.
54  *
55  * Chunks are small video files, which typically contain a few seconds of video each.
56  * Locating a frame among these small video files is accomplished by using
57  * this struct.
58  */
60  {
61  int64_t number; ///< The chunk number
62  int64_t frame; ///< The frame number
63  };
64 
65  /**
66  * @brief This enumeration allows the user to choose which version
67  * of the chunk they would like (low, medium, or high quality).
68  *
69  * Since chunks contain multiple video streams, this version enumeration
70  * allows the user to choose which version of the chunk they would like.
71  * For example, if you want a small version with reduced quality, you can
72  * choose the THUMBNAIL version. This is used on the ChunkReader
73  * constructor.
74  */
76  {
77  THUMBNAIL, ///< The lowest quality stream contained in this chunk file
78  PREVIEW, ///< The medium quality stream contained in this chunk file
79  FINAL ///< The highest quality stream contained in this chunk file
80  };
81 
82  /**
83  * @brief This class reads a special chunk-formatted file, which can be easily
84  * shared in a distributed environment.
85  *
86  * It stores the video in small "chunks", which are really just short video clips,
87  * a few seconds each. A ChunkReader only needs the part of the chunk that contains
88  * the frames it is looking for. For example, if you only need the end of a video,
89  * only the last few chunks might be needed to successfully access those openshot::Frame objects.
90  *
91  * \code
92  * // This example demonstrates how to read a chunk folder and access frame objects inside it.
93  * ChunkReader r("/home/jonathan/apps/chunks/chunk1/", FINAL); // Load highest quality version of this chunk file
94  * r.DisplayInfo(); // Display all known details about this chunk file
95  * r.Open(); // Open the reader
96  *
97  * // Access frame 1
98  * r.GetFrame(1)->Display();
99  *
100  * // Close the reader
101  * r.Close();
102  * \endcode
103  */
104  class ChunkReader : public ReaderBase
105  {
106  private:
107  string path;
108  bool is_open;
109  int64_t chunk_size;
110  FFmpegReader *local_reader;
111  ChunkLocation previous_location;
112  ChunkVersion version;
113  std::shared_ptr<Frame> last_frame;
114 
115  /// Check if folder path existing
116  bool does_folder_exist(string path);
117 
118  /// Find the location of a frame in a chunk
119  ChunkLocation find_chunk_frame(int64_t requested_frame);
120 
121  /// get a formatted path of a specific chunk
122  string get_chunk_path(int64_t chunk_number, string folder, string extension);
123 
124  /// Load JSON meta data about this chunk folder
125  void load_json();
126 
127  public:
128 
129  /// @brief Constructor for ChunkReader. This automatically opens the chunk file or folder and loads
130  /// frame 1, or it throws one of the following exceptions.
131  /// @param path The folder path / location of a chunk (chunks are stored as folders)
132  /// @param chunk_version Choose the video version / quality (THUMBNAIL, PREVIEW, or FINAL)
133  ChunkReader(string path, ChunkVersion chunk_version);
134 
135  /// Close the reader
136  void Close();
137 
138  /// @brief Get the chunk size (number of frames to write in each chunk)
139  /// @returns The number of frames in this chunk
140  int64_t GetChunkSize() { return chunk_size; };
141 
142  /// @brief Set the chunk size (number of frames to write in each chunk)
143  /// @param new_size The number of frames per chunk
144  void SetChunkSize(int64_t new_size) { chunk_size = new_size; };
145 
146  /// Get the cache object used by this reader (always return NULL for this reader)
147  CacheMemory* GetCache() { return NULL; };
148 
149  /// @brief Get an openshot::Frame object for a specific frame number of this reader.
150  /// @returns The requested frame (containing the image and audio)
151  /// @param requested_frame The frame number you want to retrieve
152  std::shared_ptr<Frame> GetFrame(int64_t requested_frame);
153 
154  /// Determine if reader is open or closed
155  bool IsOpen() { return is_open; };
156 
157  /// Return the type name of the class
158  string Name() { return "ChunkReader"; };
159 
160  /// Get and Set JSON methods
161  string Json(); ///< Generate JSON string of this object
162  void SetJson(string value); ///< Load JSON string into this object
163  Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
164  void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object
165 
166  /// Open the reader. This is required before you can access frames or data from the reader.
167  void Open();
168  };
169 
170 }
171 
172 #endif
This class reads a special chunk-formatted file, which can be easily shared in a distributed environm...
Definition: ChunkReader.h:104
Header file for ReaderBase class.
The lowest quality stream contained in this chunk file.
Definition: ChunkReader.h:77
Header file for FFmpegReader class.
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
Header file for CacheMemory class.
bool IsOpen()
Determine if reader is open or closed.
Definition: ChunkReader.h:155
Header file for all Exception classes.
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:92
The highest quality stream contained in this chunk file.
Definition: ChunkReader.h:79
The medium quality stream contained in this chunk file.
Definition: ChunkReader.h:78
Header file for JSON class.
void SetChunkSize(int64_t new_size)
Set the chunk size (number of frames to write in each chunk)
Definition: ChunkReader.h:144
CacheMemory * GetCache()
Get the cache object used by this reader (always return NULL for this reader)
Definition: ChunkReader.h:147
int64_t number
The chunk number.
Definition: ChunkReader.h:61
ChunkVersion
This enumeration allows the user to choose which version of the chunk they would like (low...
Definition: ChunkReader.h:75
int64_t GetChunkSize()
Get the chunk size (number of frames to write in each chunk)
Definition: ChunkReader.h:140
This namespace is the default namespace for all code in the openshot library.
string Name()
Return the type name of the class.
Definition: ChunkReader.h:158
int64_t frame
The frame number.
Definition: ChunkReader.h:62
This struct holds the location of a frame within a chunk.
Definition: ChunkReader.h:59
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:48