OpenShot Library | libopenshot  0.2.3
FFmpegReader.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for FFmpegReader class
4  * @author Jonathan Thomas <jonathan@openshot.org>, Fabrice Bellard
5  *
6  * @section LICENSE
7  *
8  * Copyright (c) 2008-2013 OpenShot Studios, LLC, Fabrice Bellard
9  * (http://www.openshotstudios.com). This file is part of
10  * OpenShot Library (http://www.openshot.org), an open-source project
11  * dedicated to delivering high quality video editing and animation solutions
12  * to the world.
13  *
14  * This file is originally based on the Libavformat API example, and then modified
15  * by the libopenshot project.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef OPENSHOT_FFMPEG_READER_H
32 #define OPENSHOT_FFMPEG_READER_H
33 
34 #include "ReaderBase.h"
35 
36 // Include FFmpeg headers and macros
37 #include "FFmpegUtilities.h"
38 
39 #include <cmath>
40 #include <ctime>
41 #include <iostream>
42 #include <stdio.h>
43 #include <memory>
44 #include "CacheMemory.h"
45 #include "Clip.h"
46 #include "Exceptions.h"
47 #include "OpenMPUtilities.h"
48 #include "Settings.h"
49 
50 
51 using namespace std;
52 
53 namespace openshot
54 {
55  /**
56  * @brief This struct holds the associated video frame and starting sample # for an audio packet.
57  *
58  * Because audio packets do not match up with video frames, this helps determine exactly
59  * where the audio packet's samples belong.
60  */
62  {
63  int64_t frame;
65  bool is_near(AudioLocation location, int samples_per_frame, int64_t amount);
66  };
67 
68  /**
69  * @brief This class uses the FFmpeg libraries, to open video files and audio files, and return
70  * openshot::Frame objects for any frame in the file.
71  *
72  * All seeking and caching is handled internally, and the primary public interface is the GetFrame()
73  * method. To use this reader, simply create an instance of this class, and call the GetFrame method
74  * to start retrieving frames. Use the <b>info</b> struct to obtain information on the file, such as the length
75  * (# of frames), height, width, bit rate, frames per second (fps), etc...
76  *
77  * @code
78  * // Create a reader for a video
79  * FFmpegReader r("MyAwesomeVideo.webm");
80  * r.Open(); // Open the reader
81  *
82  * // Get frame number 1 from the video
83  * std::shared_ptr<Frame> f = r.GetFrame(1);
84  *
85  * // Now that we have an openshot::Frame object, lets have some fun!
86  * f->Display(); // Display the frame on the screen
87  * f->DisplayWaveform(); // Display the audio waveform as an image
88  * f->Play(); // Play the audio through your speaker
89  *
90  * // Close the reader
91  * r.Close();
92  * @endcode
93  */
94  class FFmpegReader : public ReaderBase
95  {
96  private:
97  string path;
98 
99  AVFormatContext *pFormatCtx;
100  int i, videoStream, audioStream;
101  AVCodecContext *pCodecCtx, *aCodecCtx;
102  AVStream *pStream, *aStream;
103  AVPacket *packet;
104  AVFrame *pFrame;
105  bool is_open;
106  bool is_duration_known;
107  bool check_interlace;
108  bool check_fps;
109  bool has_missing_frames;
110 
111  CacheMemory working_cache;
112  CacheMemory missing_frames;
113  map<int64_t, int64_t> processing_video_frames;
114  multimap<int64_t, int64_t> processing_audio_frames;
115  map<int64_t, int64_t> processed_video_frames;
116  map<int64_t, int64_t> processed_audio_frames;
117  multimap<int64_t, int64_t> missing_video_frames;
118  multimap<int64_t, int64_t> missing_video_frames_source;
119  multimap<int64_t, int64_t> missing_audio_frames;
120  multimap<int64_t, int64_t> missing_audio_frames_source;
121  map<int64_t, int> checked_frames;
122  AudioLocation previous_packet_location;
123 
124  // DEBUG VARIABLES (FOR AUDIO ISSUES)
125  int prev_samples;
126  int64_t prev_pts;
127  int64_t pts_total;
128  int64_t pts_counter;
129  int64_t num_packets_since_video_frame;
130  int64_t num_checks_since_final;
131  std::shared_ptr<Frame> last_video_frame;
132 
133  bool is_seeking;
134  int64_t seeking_pts;
135  int64_t seeking_frame;
136  bool is_video_seek;
137  int seek_count;
138  int64_t seek_audio_frame_found;
139  int64_t seek_video_frame_found;
140 
141  int64_t audio_pts_offset;
142  int64_t video_pts_offset;
143  int64_t last_frame;
144  int64_t largest_frame_processed;
145  int64_t current_video_frame; // can't reliably use PTS of video to determine this
146 
147  /// Check for the correct frames per second value by scanning the 1st few seconds of video packets.
148  void CheckFPS();
149 
150  /// Check the current seek position and determine if we need to seek again
151  bool CheckSeek(bool is_video);
152 
153  /// Check if a frame is missing and attempt to replace it's frame image (and
154  bool CheckMissingFrame(int64_t requested_frame);
155 
156  /// Check the working queue, and move finished frames to the finished queue
157  void CheckWorkingFrames(bool end_of_stream, int64_t requested_frame);
158 
159  /// Convert Frame Number into Audio PTS
160  int64_t ConvertFrameToAudioPTS(int64_t frame_number);
161 
162  /// Convert Frame Number into Video PTS
163  int64_t ConvertFrameToVideoPTS(int64_t frame_number);
164 
165  /// Convert Video PTS into Frame Number
166  int64_t ConvertVideoPTStoFrame(int64_t pts);
167 
168  /// Create a new Frame (or return an existing one) and add it to the working queue.
169  std::shared_ptr<Frame> CreateFrame(int64_t requested_frame);
170 
171  /// Calculate Starting video frame and sample # for an audio PTS
172  AudioLocation GetAudioPTSLocation(int64_t pts);
173 
174  /// Get an AVFrame (if any)
175  bool GetAVFrame();
176 
177  /// Get the next packet (if any)
178  int GetNextPacket();
179 
180  /// Get the smallest video frame that is still being processed
181  int64_t GetSmallestVideoFrame();
182 
183  /// Get the smallest audio frame that is still being processed
184  int64_t GetSmallestAudioFrame();
185 
186  /// Get the PTS for the current video packet
187  int64_t GetVideoPTS();
188 
189  /// Remove partial frames due to seek
190  bool IsPartialFrame(int64_t requested_frame);
191 
192  /// Process a video packet
193  void ProcessVideoPacket(int64_t requested_frame);
194 
195  /// Process an audio packet
196  void ProcessAudioPacket(int64_t requested_frame, int64_t target_frame, int starting_sample);
197 
198  /// Read the stream until we find the requested Frame
199  std::shared_ptr<Frame> ReadStream(int64_t requested_frame);
200 
201  /// Remove AVFrame from cache (and deallocate it's memory)
202  void RemoveAVFrame(AVFrame*);
203 
204  /// Remove AVPacket from cache (and deallocate it's memory)
205  void RemoveAVPacket(AVPacket*);
206 
207  /// Seek to a specific Frame. This is not always frame accurate, it's more of an estimation on many codecs.
208  void Seek(int64_t requested_frame);
209 
210  /// Update PTS Offset (if any)
211  void UpdatePTSOffset(bool is_video);
212 
213  /// Update File Info for audio streams
214  void UpdateAudioInfo();
215 
216  /// Update File Info for video streams
217  void UpdateVideoInfo();
218 
219  public:
220  /// Final cache object used to hold final frames
222 
223  /// Enable or disable seeking. Seeking can more quickly locate the requested frame, but some
224  /// codecs have trouble seeking, and can introduce artifacts or blank images into the video.
226 
227  /// Constructor for FFmpegReader. This automatically opens the media file and loads
228  /// frame 1, or it throws one of the following exceptions.
229  FFmpegReader(string path);
230 
231  /// Constructor for FFmpegReader. This only opens the media file to inspect it's properties
232  /// if inspect_reader=true. When not inspecting the media file, it's much faster, and useful
233  /// when you are inflating the object using JSON after instantiating it.
234  FFmpegReader(string path, bool inspect_reader);
235 
236  /// Destructor
237  ~FFmpegReader();
238 
239  /// Close File
240  void Close();
241 
242  /// Get the cache object used by this reader
243  CacheMemory* GetCache() { return &final_cache; };
244 
245  /// Get a shared pointer to a openshot::Frame object for a specific frame number of this reader.
246  ///
247  /// @returns The requested frame of video
248  /// @param requested_frame The frame number that is requested.
249  std::shared_ptr<Frame> GetFrame(int64_t requested_frame);
250 
251  /// Determine if reader is open or closed
252  bool IsOpen() { return is_open; };
253 
254  /// Return the type name of the class
255  string Name() { return "FFmpegReader"; };
256 
257  /// Get and Set JSON methods
258  string Json(); ///< Generate JSON string of this object
259  void SetJson(string value); ///< Load JSON string into this object
260  Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
261  void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object
262 
263  /// Open File - which is called by the constructor automatically
264  void Open();
265  };
266 
267 }
268 
269 #endif
Header file for ReaderBase class.
Header file for OpenMPUtilities (set some common macros)
CacheMemory * GetCache()
Get the cache object used by this reader.
Definition: FFmpegReader.h:243
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
Header file for CacheMemory class.
bool IsOpen()
Determine if reader is open or closed.
Definition: FFmpegReader.h:252
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:94
Header file for Clip class.
Header file for global Settings class.
This struct holds the associated video frame and starting sample # for an audio packet.
Definition: FFmpegReader.h:61
CacheMemory final_cache
Final cache object used to hold final frames.
Definition: FFmpegReader.h:221
string Name()
Return the type name of the class.
Definition: FFmpegReader.h:255
Header file for FFmpegUtilities.
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:48