OpenShot Library | libopenshot  0.3.0
FFmpegReader.h
Go to the documentation of this file.
1 
12 // Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
13 //
14 // SPDX-License-Identifier: LGPL-3.0-or-later
15 
16 #ifndef OPENSHOT_FFMPEG_READER_H
17 #define OPENSHOT_FFMPEG_READER_H
18 
19 #include "ReaderBase.h"
20 
21 // Include FFmpeg headers and macros
22 #include "FFmpegUtilities.h"
23 
24 #include <cmath>
25 #include <ctime>
26 #include <iostream>
27 #include <stdio.h>
28 #include <memory>
29 #include "CacheMemory.h"
30 #include "Clip.h"
31 #include "OpenMPUtilities.h"
32 #include "Settings.h"
33 
34 
35 namespace openshot {
42  struct AudioLocation {
43  int64_t frame;
45 
46  bool is_near(AudioLocation location, int samples_per_frame, int64_t amount);
47  };
48 
57  struct PacketStatus {
58  // Track counts of video and audio packets read & decoded
59  int64_t video_read = 0;
60  int64_t video_decoded = 0;
61  int64_t audio_read = 0;
62  int64_t audio_decoded = 0;
63 
64  // Track end-of-file detection on video/audio and overall
65  bool video_eof = true;
66  bool audio_eof = true;
67  bool packets_eof = true;
68  bool end_of_file = true;
69 
70  int64_t packets_read() {
71  // Return total packets read
72  return video_read + audio_read;
73  }
74 
75  int64_t packets_decoded() {
76  // Return total packets decoded
78  }
79 
80  void reset(bool eof) {
81  // Reset counts and EOF detection for packets
83  video_eof = eof; audio_eof = eof; packets_eof = eof; end_of_file = eof;
84  }
85  };
86 
113  class FFmpegReader : public ReaderBase {
114  private:
115  std::string path;
116 
117  AVFormatContext *pFormatCtx;
118  int videoStream, audioStream;
119  AVCodecContext *pCodecCtx, *aCodecCtx;
120 #if USE_HW_ACCEL
121  AVBufferRef *hw_device_ctx = NULL; //PM
122 #endif
123  AVStream *pStream, *aStream;
124  AVPacket *packet;
125  AVFrame *pFrame;
126  bool is_open;
127  bool is_duration_known;
128  bool check_interlace;
129  bool check_fps;
130  int max_concurrent_frames;
131 
132  CacheMemory working_cache;
133  AudioLocation previous_packet_location;
134 
135  // DEBUG VARIABLES (FOR AUDIO ISSUES)
136  int prev_samples;
137  int64_t prev_pts;
138  int64_t pts_total;
139  int64_t pts_counter;
140  std::shared_ptr<openshot::Frame> last_video_frame;
141 
142  bool is_seeking;
143  int64_t seeking_pts;
144  int64_t seeking_frame;
145  bool is_video_seek;
146  int seek_count;
147  int64_t seek_audio_frame_found;
148  int64_t seek_video_frame_found;
149 
150  int64_t last_frame;
151  int64_t largest_frame_processed;
152  int64_t current_video_frame;
153 
154  int64_t audio_pts;
155  int64_t video_pts;
156  bool hold_packet;
157  double pts_offset_seconds;
158  double audio_pts_seconds;
159  double video_pts_seconds;
160  int64_t NO_PTS_OFFSET;
161  PacketStatus packet_status;
162 
163  int hw_de_supported = 0; // Is set by FFmpegReader
164 #if USE_HW_ACCEL
165  AVPixelFormat hw_de_av_pix_fmt = AV_PIX_FMT_NONE;
166  AVHWDeviceType hw_de_av_device_type = AV_HWDEVICE_TYPE_NONE;
167  int IsHardwareDecodeSupported(int codecid);
168 #endif
169 
171  void CheckFPS();
172 
174  bool CheckSeek(bool is_video);
175 
177  void CheckWorkingFrames(int64_t requested_frame);
178 
180  int64_t ConvertFrameToAudioPTS(int64_t frame_number);
181 
183  int64_t ConvertFrameToVideoPTS(int64_t frame_number);
184 
186  int64_t ConvertVideoPTStoFrame(int64_t pts);
187 
189  std::shared_ptr<openshot::Frame> CreateFrame(int64_t requested_frame);
190 
192  AudioLocation GetAudioPTSLocation(int64_t pts);
193 
195  bool GetAVFrame();
196 
198  int GetNextPacket();
199 
201  int64_t GetPacketPTS();
202 
204  bool HasAlbumArt();
205 
207  bool IsPartialFrame(int64_t requested_frame);
208 
210  void ProcessVideoPacket(int64_t requested_frame);
211 
213  void ProcessAudioPacket(int64_t requested_frame);
214 
216  std::shared_ptr<openshot::Frame> ReadStream(int64_t requested_frame);
217 
219  void RemoveAVFrame(AVFrame *);
220 
222  void RemoveAVPacket(AVPacket *);
223 
225  void Seek(int64_t requested_frame);
226 
230  void UpdatePTSOffset();
231 
233  void UpdateAudioInfo();
234 
236  void UpdateVideoInfo();
237 
238  public:
241 
245 
252  FFmpegReader(const std::string& path, bool inspect_reader=true);
253 
255  virtual ~FFmpegReader();
256 
258  void Close() override;
259 
261  CacheMemory *GetCache() override { return &final_cache; };
262 
267  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
268 
270  bool IsOpen() override { return is_open; };
271 
273  std::string Name() override { return "FFmpegReader"; };
274 
275  // Get and Set JSON methods
276  std::string Json() const override;
277  void SetJson(const std::string value) override;
278  Json::Value JsonValue() const override;
279  void SetJsonValue(const Json::Value root) override;
280 
282  void Open() override;
283 
285  bool GetIsDurationKnown();
286  };
287 
288 }
289 
290 #endif
Header file for CacheMemory class.
Header file for Clip class.
Header file for FFmpegUtilities.
Header file for OpenMPUtilities (set some common macros)
Header file for ReaderBase class.
Header file for global Settings class.
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:29
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:113
CacheMemory * GetCache() override
Get the cache object used by this reader.
Definition: FFmpegReader.h:261
void Open() override
Open File - which is called by the constructor automatically.
FFmpegReader(const std::string &path, bool inspect_reader=true)
Constructor for FFmpegReader.
Json::Value JsonValue() const override
Generate Json::Value for this object.
bool GetIsDurationKnown()
Return true if frame can be read with GetFrame()
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
CacheMemory final_cache
Final cache object used to hold final frames.
Definition: FFmpegReader.h:240
std::string Name() override
Return the type name of the class.
Definition: FFmpegReader.h:273
virtual ~FFmpegReader()
Destructor.
std::string Json() const override
Generate JSON string of this object.
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
void Close() override
Close File.
void SetJson(const std::string value) override
Load JSON string into this object.
bool IsOpen() override
Determine if reader is open or closed.
Definition: FFmpegReader.h:270
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:76
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
This struct holds the associated video frame and starting sample # for an audio packet.
Definition: FFmpegReader.h:42
bool is_near(AudioLocation location, int samples_per_frame, int64_t amount)
This struct holds the packet counts and end-of-file detection for an openshot::FFmpegReader.
Definition: FFmpegReader.h:57
void reset(bool eof)
Definition: FFmpegReader.h:80