OpenShot Library | libopenshot  0.1.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 <tr1/memory>
44 #include "CacheMemory.h"
45 #include "Exceptions.h"
46 #include "OpenMPUtilities.h"
47 
48 
49 using namespace std;
50 
51 namespace openshot
52 {
53  /**
54  * @brief This struct holds the associated video frame and starting sample # for an audio packet.
55  *
56  * Because audio packets do not match up with video frames, this helps determine exactly
57  * where the audio packet's samples belong.
58  */
60  {
61  int frame;
63  int is_near(AudioLocation location, int samples_per_frame, int amount);
64  };
65 
66  /**
67  * @brief This class uses the FFmpeg libraries, to open video files and audio files, and return
68  * openshot::Frame objects for any frame in the file.
69  *
70  * All seeking and caching is handled internally, and the primary public interface is the GetFrame()
71  * method. To use this reader, simply create an instance of this class, and call the GetFrame method
72  * to start retrieving frames. Use the <b>info</b> struct to obtain information on the file, such as the length
73  * (# of frames), height, width, bit rate, frames per second (fps), etc...
74  *
75  * @code
76  * // Create a reader for a video
77  * FFmpegReader r("MyAwesomeVideo.webm");
78  * r.Open(); // Open the reader
79  *
80  * // Get frame number 1 from the video
81  * tr1::shared_ptr<Frame> f = r.GetFrame(1);
82  *
83  * // Now that we have an openshot::Frame object, lets have some fun!
84  * f->Display(); // Display the frame on the screen
85  * f->DisplayWaveform(); // Display the audio waveform as an image
86  * f->Play(); // Play the audio through your speaker
87  *
88  * // Close the reader
89  * r.Close();
90  * @endcode
91  */
92  class FFmpegReader : public ReaderBase
93  {
94  private:
95  string path;
96 
97  AVFormatContext *pFormatCtx;
98  int i, videoStream, audioStream;
99  AVCodecContext *pCodecCtx, *aCodecCtx;
100  AVStream *pStream, *aStream;
101  AVPacket *packet;
102  AVPicture *pFrame;
103  bool is_open;
104  bool is_duration_known;
105  bool check_interlace;
106  bool check_fps;
107  bool has_missing_frames;
108 
109  CacheMemory working_cache;
110  CacheMemory missing_frames;
111  map<AVPicture*, AVPicture*> frames;
112  map<long int, long int> processing_video_frames;
113  multimap<long int, long int> processing_audio_frames;
114  map<long int, long int> processed_video_frames;
115  map<long int, long int> processed_audio_frames;
116  multimap<long int, long int> missing_video_frames;
117  multimap<long int, long int> missing_video_frames_source;
118  multimap<long int, long int> missing_audio_frames;
119  multimap<long int, long int> missing_audio_frames_source;
120  multimap<long int, long int> duplicate_video_frames;
121  map<long int, int> checked_frames;
122  AudioLocation previous_packet_location;
123 
124  // DEBUG VARIABLES (FOR AUDIO ISSUES)
125  int prev_samples;
126  long int prev_pts;
127  long int pts_total;
128  long int pts_counter;
129  long int num_packets_since_video_frame;
130  long int num_checks_since_final;
131  tr1::shared_ptr<Frame> last_video_frame;
132 
133  bool is_seeking;
134  long int seeking_pts;
135  long int seeking_frame;
136  bool is_video_seek;
137  int seek_count;
138  long int seek_audio_frame_found;
139  long int seek_video_frame_found;
140 
141  long int audio_pts_offset;
142  long int video_pts_offset;
143  long int last_frame;
144  long int largest_frame_processed;
145  long int 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(long int requested_frame);
155 
156  /// Check the working queue, and move finished frames to the finished queue
157  void CheckWorkingFrames(bool end_of_stream, long int requested_frame);
158 
159  /// Convert image to RGB format
160  void convert_image(long int current_frame, AVPicture *copyFrame, int width, int height, PixelFormat pix_fmt);
161 
162  /// Convert Frame Number into Audio PTS
163  long int ConvertFrameToAudioPTS(long int frame_number);
164 
165  /// Convert Frame Number into Video PTS
166  long int ConvertFrameToVideoPTS(long int frame_number);
167 
168  /// Convert Video PTS into Frame Number
169  long int ConvertVideoPTStoFrame(long int pts);
170 
171  /// Create a new Frame (or return an existing one) and add it to the working queue.
172  tr1::shared_ptr<Frame> CreateFrame(long int requested_frame);
173 
174  /// Calculate Starting video frame and sample # for an audio PTS
175  AudioLocation GetAudioPTSLocation(long int pts);
176 
177  /// Get an AVFrame (if any)
178  bool GetAVFrame();
179 
180  /// Get the next packet (if any)
181  int GetNextPacket();
182 
183  /// Get the smallest video frame that is still being processed
184  long int GetSmallestVideoFrame();
185 
186  /// Get the smallest audio frame that is still being processed
187  long int GetSmallestAudioFrame();
188 
189  /// Get the PTS for the current video packet
190  long int GetVideoPTS();
191 
192  /// Remove partial frames due to seek
193  bool IsPartialFrame(long int requested_frame);
194 
195  /// Process a video packet
196  void ProcessVideoPacket(long int requested_frame);
197 
198  /// Process an audio packet
199  void ProcessAudioPacket(long int requested_frame, long int target_frame, int starting_sample);
200 
201  /// Read the stream until we find the requested Frame
202  tr1::shared_ptr<Frame> ReadStream(long int requested_frame);
203 
204  /// Remove AVFrame from cache (and deallocate it's memory)
205  void RemoveAVFrame(AVPicture*);
206 
207  /// Remove AVPacket from cache (and deallocate it's memory)
208  void RemoveAVPacket(AVPacket*);
209 
210  /// Seek to a specific Frame. This is not always frame accurate, it's more of an estimation on many codecs.
211  void Seek(long int requested_frame) throw(TooManySeeks);
212 
213  /// Update PTS Offset (if any)
214  void UpdatePTSOffset(bool is_video);
215 
216  /// Update File Info for audio streams
217  void UpdateAudioInfo();
218 
219  /// Update File Info for video streams
220  void UpdateVideoInfo();
221 
222  public:
223  /// Final cache object used to hold final frames
225 
226  /// Enable or disable seeking. Seeking can more quickly locate the requested frame, but some
227  /// codecs have trouble seeking, and can introduce artifacts or blank images into the video.
229 
230  /// Constructor for FFmpegReader. This automatically opens the media file and loads
231  /// frame 1, or it throws one of the following exceptions.
232  FFmpegReader(string path) throw(InvalidFile, NoStreamsFound, InvalidCodec);
233 
234  /// Constructor for FFmpegReader. This only opens the media file to inspect it's properties
235  /// if inspect_reader=true. When not inspecting the media file, it's much faster, and useful
236  /// when you are inflating the object using JSON after instantiating it.
237  FFmpegReader(string path, bool inspect_reader) throw(InvalidFile, NoStreamsFound, InvalidCodec);
238 
239  /// Destructor
240  ~FFmpegReader();
241 
242  /// Close File
243  void Close();
244 
245  /// Get the cache object used by this reader
246  CacheMemory* GetCache() { return &final_cache; };
247 
248  /// Get a shared pointer to a openshot::Frame object for a specific frame number of this reader.
249  ///
250  /// @returns The requested frame of video
251  /// @param requested_frame The frame number that is requested.
252  tr1::shared_ptr<Frame> GetFrame(long int requested_frame) throw(OutOfBoundsFrame, ReaderClosed, TooManySeeks);
253 
254  /// Determine if reader is open or closed
255  bool IsOpen() { return is_open; };
256 
257  /// Return the type name of the class
258  string Name() { return "FFmpegReader"; };
259 
260  /// Get and Set JSON methods
261  string Json(); ///< Generate JSON string of this object
262  void SetJson(string value) throw(InvalidJSON); ///< Load JSON string into this object
263  Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
264  void SetJsonValue(Json::Value root) throw(InvalidFile); ///< Load Json::JsonValue into this object
265 
266  /// Open File - which is called by the constructor automatically
267  void Open() throw(InvalidFile, NoStreamsFound, InvalidCodec);
268  };
269 
270 }
271 
272 #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:246
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:234
Header file for CacheMemory class.
bool IsOpen()
Determine if reader is open or closed.
Definition: FFmpegReader.h:255
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
Exception when no valid codec is found for a file.
Definition: Exceptions.h:122
Exception when no streams are found in the file.
Definition: Exceptions.h:192
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
#define PixelFormat
Exception for frames that are out of bounds.
Definition: Exceptions.h:202
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:152
This struct holds the associated video frame and starting sample # for an audio packet.
Definition: FFmpegReader.h:59
CacheMemory final_cache
Final cache object used to hold final frames.
Definition: FFmpegReader.h:224
string Name()
Return the type name of the class.
Definition: FFmpegReader.h:258
Header file for FFmpegUtilities.
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:48
Exception when too many seek attempts happen.
Definition: Exceptions.h:254