OpenShot Library | libopenshot  0.4.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 "AudioLocation.h"
30 #include "CacheMemory.h"
31 #include "Clip.h"
32 #include "OpenMPUtilities.h"
33 #include "Settings.h"
34 #include <cstdlib>
35 
36 
37 namespace openshot {
46  struct PacketStatus {
47  // Track counts of video and audio packets read & decoded
48  int64_t video_read = 0;
49  int64_t video_decoded = 0;
50  int64_t audio_read = 0;
51  int64_t audio_decoded = 0;
52 
53  // Track end-of-file detection on video/audio and overall
54  bool video_eof = true;
55  bool audio_eof = true;
56  bool packets_eof = true;
57  bool end_of_file = true;
58 
59  int64_t packets_read() {
60  // Return total packets read
61  return video_read + audio_read;
62  }
63 
64  int64_t packets_decoded() {
65  // Return total packets decoded
67  }
68 
69  void reset(bool eof) {
70  // Reset counts and EOF detection for packets
72  video_eof = eof; audio_eof = eof; packets_eof = eof; end_of_file = eof;
73  }
74  };
75 
102  class FFmpegReader : public ReaderBase {
103  private:
104  std::string path;
105 
106  AVFormatContext *pFormatCtx;
107  int videoStream, audioStream;
108  AVCodecContext *pCodecCtx, *aCodecCtx;
109 #if USE_HW_ACCEL
110  AVBufferRef *hw_device_ctx = NULL; //PM
111 #endif
112  AVStream *pStream, *aStream;
113  AVPacket *packet;
114  AVFrame *pFrame;
115  bool is_open;
116  bool is_duration_known;
117  bool check_interlace;
118  bool check_fps;
119  int max_concurrent_frames;
120 
121  CacheMemory working_cache;
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  std::shared_ptr<openshot::Frame> last_video_frame;
130 
131  bool is_seeking;
132  int64_t seeking_pts;
133  int64_t seeking_frame;
134  bool is_video_seek;
135  int seek_count;
136  int64_t seek_audio_frame_found;
137  int64_t seek_video_frame_found;
138 
139  int64_t last_frame;
140  int64_t largest_frame_processed;
141  int64_t current_video_frame;
142 
143  int64_t audio_pts;
144  int64_t video_pts;
145  bool hold_packet;
146  double pts_offset_seconds;
147  double audio_pts_seconds;
148  double video_pts_seconds;
149  int64_t NO_PTS_OFFSET;
150  PacketStatus packet_status;
151 
152  // Cached conversion contexts and frames for performance
153  SwsContext *img_convert_ctx = nullptr;
154  SWRCONTEXT *avr_ctx = nullptr;
155  AVFrame *pFrameRGB_cached = nullptr;
156 
157  int hw_de_supported = 0; // Is set by FFmpegReader
158 #if USE_HW_ACCEL
159  AVPixelFormat hw_de_av_pix_fmt = AV_PIX_FMT_NONE;
160  AVHWDeviceType hw_de_av_device_type = AV_HWDEVICE_TYPE_NONE;
161  int IsHardwareDecodeSupported(int codecid);
162 #endif
163 
165  void CheckFPS();
166 
168  bool CheckSeek(bool is_video);
169 
171  void CheckWorkingFrames(int64_t requested_frame);
172 
174  int64_t ConvertFrameToAudioPTS(int64_t frame_number);
175 
177  int64_t ConvertFrameToVideoPTS(int64_t frame_number);
178 
180  int64_t ConvertVideoPTStoFrame(int64_t pts);
181 
183  std::shared_ptr<openshot::Frame> CreateFrame(int64_t requested_frame);
184 
186  AudioLocation GetAudioPTSLocation(int64_t pts);
187 
189  bool GetAVFrame();
190 
192  int GetNextPacket();
193 
195  int64_t GetPacketPTS();
196 
198  bool HasAlbumArt();
199 
201  bool IsPartialFrame(int64_t requested_frame);
202 
204  void ProcessVideoPacket(int64_t requested_frame);
205 
207  void ProcessAudioPacket(int64_t requested_frame);
208 
210  std::shared_ptr<openshot::Frame> ReadStream(int64_t requested_frame);
211 
213  void RemoveAVFrame(AVFrame *);
214 
216  void RemoveAVPacket(AVPacket *);
217 
219  void Seek(int64_t requested_frame);
220 
224  void UpdatePTSOffset();
225 
227  void UpdateAudioInfo();
228 
230  void UpdateVideoInfo();
231 
232  public:
235 
239 
246  FFmpegReader(const std::string& path, bool inspect_reader=true);
247 
249  virtual ~FFmpegReader();
250 
252  void Close() override;
253 
255  CacheMemory *GetCache() override { return &final_cache; };
256 
261  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
262 
264  bool IsOpen() override { return is_open; };
265 
267  std::string Name() override { return "FFmpegReader"; };
268 
269  // Get and Set JSON methods
270  std::string Json() const override;
271  void SetJson(const std::string value) override;
272  Json::Value JsonValue() const override;
273  void SetJsonValue(const Json::Value root) override;
274 
276  void Open() override;
277 
279  bool GetIsDurationKnown();
280  };
281 
282 }
283 
284 #endif
Settings.h
Header file for global Settings class.
openshot::FFmpegReader::FFmpegReader
FFmpegReader(const std::string &path, bool inspect_reader=true)
Constructor for FFmpegReader.
Definition: FFmpegReader.cpp:71
FFmpegUtilities.h
Header file for FFmpegUtilities.
openshot::PacketStatus::reset
void reset(bool eof)
Definition: FFmpegReader.h:69
Clip.h
Header file for Clip class.
openshot::FFmpegReader::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition: FFmpegReader.cpp:986
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::AudioLocation
This struct holds the associated video frame and starting sample # for an audio packet.
Definition: AudioLocation.h:25
AudioLocation.h
Header file for AudioLocation class.
openshot::FFmpegReader::~FFmpegReader
virtual ~FFmpegReader()
Destructor.
Definition: FFmpegReader.cpp:100
openshot::FFmpegReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: FFmpegReader.cpp:2460
openshot::PacketStatus::audio_read
int64_t audio_read
Definition: FFmpegReader.h:50
openshot::FFmpegReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: FFmpegReader.cpp:2472
openshot::PacketStatus::packets_eof
bool packets_eof
Definition: FFmpegReader.h:56
openshot::PacketStatus::audio_decoded
int64_t audio_decoded
Definition: FFmpegReader.h:51
openshot::PacketStatus::video_read
int64_t video_read
Definition: FFmpegReader.h:48
openshot::PacketStatus::video_eof
bool video_eof
Definition: FFmpegReader.h:54
openshot::CacheMemory
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:29
openshot::FFmpegReader::enable_seek
bool enable_seek
Definition: FFmpegReader.h:238
openshot::PacketStatus
This struct holds the packet counts and end-of-file detection for an openshot::FFmpegReader.
Definition: FFmpegReader.h:46
openshot::FFmpegReader::Open
void Open() override
Open File - which is called by the constructor automatically.
Definition: FFmpegReader.cpp:207
CacheMemory.h
Header file for CacheMemory class.
openshot::FFmpegReader::IsOpen
bool IsOpen() override
Determine if reader is open or closed.
Definition: FFmpegReader.h:264
SWRCONTEXT
#define SWRCONTEXT
Definition: FFmpegUtilities.h:155
openshot::PacketStatus::audio_eof
bool audio_eof
Definition: FFmpegReader.h:55
openshot::FFmpegReader::final_cache
CacheMemory final_cache
Final cache object used to hold final frames.
Definition: FFmpegReader.h:234
openshot::FFmpegReader
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:102
openshot::FFmpegReader::GetCache
CacheMemory * GetCache() override
Get the cache object used by this reader.
Definition: FFmpegReader.h:255
openshot::FFmpegReader::Close
void Close() override
Close File.
Definition: FFmpegReader.cpp:632
openshot::PacketStatus::packets_read
int64_t packets_read()
Definition: FFmpegReader.h:59
openshot::FFmpegReader::Name
std::string Name() override
Return the type name of the class.
Definition: FFmpegReader.h:267
ReaderBase.h
Header file for ReaderBase class.
openshot::PacketStatus::packets_decoded
int64_t packets_decoded()
Definition: FFmpegReader.h:64
OpenMPUtilities.h
Header file for OpenMPUtilities (set some common macros)
openshot::PacketStatus::end_of_file
bool end_of_file
Definition: FFmpegReader.h:57
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::FFmpegReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: FFmpegReader.cpp:2487
openshot::FFmpegReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition: FFmpegReader.cpp:2453
openshot::FFmpegReader::GetIsDurationKnown
bool GetIsDurationKnown()
Return true if frame can be read with GetFrame()
Definition: FFmpegReader.cpp:982
openshot::PacketStatus::video_decoded
int64_t video_decoded
Definition: FFmpegReader.h:49