OpenShot Library | libopenshot  0.3.2
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
Header file for AudioLocation class.
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:101
CacheMemory * GetCache() override
Get the cache object used by this reader.
Definition: FFmpegReader.h:249
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:228
std::string Name() override
Return the type name of the class.
Definition: FFmpegReader.h:261
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:258
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: AudioLocation.h:25
This struct holds the packet counts and end-of-file detection for an openshot::FFmpegReader.
Definition: FFmpegReader.h:45
void reset(bool eof)
Definition: FFmpegReader.h:68