OpenShot Library | libopenshot  0.3.3
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 
35 
36 namespace openshot {
45  struct PacketStatus {
46  // Track counts of video and audio packets read & decoded
47  int64_t video_read = 0;
48  int64_t video_decoded = 0;
49  int64_t audio_read = 0;
50  int64_t audio_decoded = 0;
51 
52  // Track end-of-file detection on video/audio and overall
53  bool video_eof = true;
54  bool audio_eof = true;
55  bool packets_eof = true;
56  bool end_of_file = true;
57 
58  int64_t packets_read() {
59  // Return total packets read
60  return video_read + audio_read;
61  }
62 
63  int64_t packets_decoded() {
64  // Return total packets decoded
66  }
67 
68  void reset(bool eof) {
69  // Reset counts and EOF detection for packets
71  video_eof = eof; audio_eof = eof; packets_eof = eof; end_of_file = eof;
72  }
73  };
74 
101  class FFmpegReader : public ReaderBase {
102  private:
103  std::string path;
104 
105  AVFormatContext *pFormatCtx;
106  int videoStream, audioStream;
107  AVCodecContext *pCodecCtx, *aCodecCtx;
108 #if USE_HW_ACCEL
109  AVBufferRef *hw_device_ctx = NULL; //PM
110 #endif
111  AVStream *pStream, *aStream;
112  AVPacket *packet;
113  AVFrame *pFrame;
114  bool is_open;
115  bool is_duration_known;
116  bool check_interlace;
117  bool check_fps;
118  int max_concurrent_frames;
119 
120  CacheMemory working_cache;
121  AudioLocation previous_packet_location;
122 
123  // DEBUG VARIABLES (FOR AUDIO ISSUES)
124  int prev_samples;
125  int64_t prev_pts;
126  int64_t pts_total;
127  int64_t pts_counter;
128  std::shared_ptr<openshot::Frame> last_video_frame;
129 
130  bool is_seeking;
131  int64_t seeking_pts;
132  int64_t seeking_frame;
133  bool is_video_seek;
134  int seek_count;
135  int64_t seek_audio_frame_found;
136  int64_t seek_video_frame_found;
137 
138  int64_t last_frame;
139  int64_t largest_frame_processed;
140  int64_t current_video_frame;
141 
142  int64_t audio_pts;
143  int64_t video_pts;
144  bool hold_packet;
145  double pts_offset_seconds;
146  double audio_pts_seconds;
147  double video_pts_seconds;
148  int64_t NO_PTS_OFFSET;
149  PacketStatus packet_status;
150 
151  int hw_de_supported = 0; // Is set by FFmpegReader
152 #if USE_HW_ACCEL
153  AVPixelFormat hw_de_av_pix_fmt = AV_PIX_FMT_NONE;
154  AVHWDeviceType hw_de_av_device_type = AV_HWDEVICE_TYPE_NONE;
155  int IsHardwareDecodeSupported(int codecid);
156 #endif
157 
159  void CheckFPS();
160 
162  bool CheckSeek(bool is_video);
163 
165  void CheckWorkingFrames(int64_t requested_frame);
166 
168  int64_t ConvertFrameToAudioPTS(int64_t frame_number);
169 
171  int64_t ConvertFrameToVideoPTS(int64_t frame_number);
172 
174  int64_t ConvertVideoPTStoFrame(int64_t pts);
175 
177  std::shared_ptr<openshot::Frame> CreateFrame(int64_t requested_frame);
178 
180  AudioLocation GetAudioPTSLocation(int64_t pts);
181 
183  bool GetAVFrame();
184 
186  int GetNextPacket();
187 
189  int64_t GetPacketPTS();
190 
192  bool HasAlbumArt();
193 
195  bool IsPartialFrame(int64_t requested_frame);
196 
198  void ProcessVideoPacket(int64_t requested_frame);
199 
201  void ProcessAudioPacket(int64_t requested_frame);
202 
204  std::shared_ptr<openshot::Frame> ReadStream(int64_t requested_frame);
205 
207  void RemoveAVFrame(AVFrame *);
208 
210  void RemoveAVPacket(AVPacket *);
211 
213  void Seek(int64_t requested_frame);
214 
218  void UpdatePTSOffset();
219 
221  void UpdateAudioInfo();
222 
224  void UpdateVideoInfo();
225 
226  public:
229 
233 
240  FFmpegReader(const std::string& path, bool inspect_reader=true);
241 
243  virtual ~FFmpegReader();
244 
246  void Close() override;
247 
249  CacheMemory *GetCache() override { return &final_cache; };
250 
255  std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
256 
258  bool IsOpen() override { return is_open; };
259 
261  std::string Name() override { return "FFmpegReader"; };
262 
263  // Get and Set JSON methods
264  std::string Json() const override;
265  void SetJson(const std::string value) override;
266  Json::Value JsonValue() const override;
267  void SetJsonValue(const Json::Value root) override;
268 
270  void Open() override;
271 
273  bool GetIsDurationKnown();
274  };
275 
276 }
277 
278 #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:68
Clip.h
Header file for Clip class.
openshot::FFmpegReader::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition: FFmpegReader.cpp:915
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:2382
openshot::PacketStatus::audio_read
int64_t audio_read
Definition: FFmpegReader.h:49
openshot::FFmpegReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: FFmpegReader.cpp:2394
openshot::PacketStatus::packets_eof
bool packets_eof
Definition: FFmpegReader.h:55
openshot::PacketStatus::audio_decoded
int64_t audio_decoded
Definition: FFmpegReader.h:50
openshot::PacketStatus::video_read
int64_t video_read
Definition: FFmpegReader.h:47
openshot::PacketStatus::video_eof
bool video_eof
Definition: FFmpegReader.h:53
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:232
openshot::PacketStatus
This struct holds the packet counts and end-of-file detection for an openshot::FFmpegReader.
Definition: FFmpegReader.h:45
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:258
openshot::PacketStatus::audio_eof
bool audio_eof
Definition: FFmpegReader.h:54
openshot::FFmpegReader::final_cache
CacheMemory final_cache
Final cache object used to hold final frames.
Definition: FFmpegReader.h:228
openshot::FFmpegReader
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:101
openshot::FFmpegReader::GetCache
CacheMemory * GetCache() override
Get the cache object used by this reader.
Definition: FFmpegReader.h:249
openshot::FFmpegReader::Close
void Close() override
Close File.
Definition: FFmpegReader.cpp:586
openshot::PacketStatus::packets_read
int64_t packets_read()
Definition: FFmpegReader.h:58
openshot::FFmpegReader::Name
std::string Name() override
Return the type name of the class.
Definition: FFmpegReader.h:261
ReaderBase.h
Header file for ReaderBase class.
openshot::PacketStatus::packets_decoded
int64_t packets_decoded()
Definition: FFmpegReader.h:63
OpenMPUtilities.h
Header file for OpenMPUtilities (set some common macros)
openshot::PacketStatus::end_of_file
bool end_of_file
Definition: FFmpegReader.h:56
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:2409
openshot::FFmpegReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition: FFmpegReader.cpp:2375
openshot::FFmpegReader::GetIsDurationKnown
bool GetIsDurationKnown()
Return true if frame can be read with GetFrame()
Definition: FFmpegReader.cpp:911
openshot::PacketStatus::video_decoded
int64_t video_decoded
Definition: FFmpegReader.h:48