OpenShot Library | libopenshot 1.1.1
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#include "Enums.h"
21
22// Include FFmpeg headers and macros
23#include "FFmpegUtilities.h"
24
25#include <cmath>
26#include <ctime>
27#include <iostream>
28#include <stdio.h>
29#include <memory>
30#include "AudioLocation.h"
31#include "CacheMemory.h"
32#include "Clip.h"
33#include "OpenMPUtilities.h"
34#include "Settings.h"
35#include <cstdlib>
36
37
38namespace openshot {
47 struct PacketStatus {
48 // Track counts of video and audio packets read & decoded
49 int64_t video_read = 0;
50 int64_t video_decoded = 0;
51 int64_t audio_read = 0;
52 int64_t audio_decoded = 0;
53
54 // Track end-of-file detection on video/audio and overall
55 bool video_eof = true;
56 bool audio_eof = true;
57 bool packets_eof = true;
58 bool end_of_file = true;
59
60 int64_t packets_read() {
61 // Return total packets read
62 return video_read + audio_read;
63 }
64
65 int64_t packets_decoded() {
66 // Return total packets decoded
68 }
69
70 void reset(bool eof) {
71 // Reset counts and EOF detection for packets
73 video_eof = eof; audio_eof = eof; packets_eof = eof; end_of_file = eof;
74 }
75 };
76
103 class FFmpegReader : public ReaderBase {
104 private:
105 std::string path;
106
107 AVFormatContext *pFormatCtx;
108 int videoStream, audioStream;
109 AVCodecContext *pCodecCtx, *aCodecCtx;
110#if USE_HW_ACCEL
111 AVBufferRef *hw_device_ctx = NULL; //PM
112#endif
113 AVStream *pStream, *aStream;
114 AVPacket *packet;
115 AVFrame *pFrame;
116 bool is_open;
117 bool is_duration_known;
118 bool check_interlace;
119 bool check_fps;
120 DurationStrategy duration_strategy;
121
122 CacheMemory working_cache;
123 AudioLocation previous_packet_location;
124
125 // DEBUG VARIABLES (FOR AUDIO ISSUES)
126 int prev_samples;
127 int64_t prev_pts;
128 int64_t pts_total;
129 int64_t pts_counter;
130 std::shared_ptr<openshot::Frame> last_video_frame;
131 std::shared_ptr<openshot::Frame> last_final_video_frame;
132
133 bool is_seeking;
134 int64_t seeking_pts;
135 int64_t seeking_frame;
136 bool is_video_seek;
137 int seek_count;
138 int64_t seek_audio_frame_found;
139 int64_t seek_video_frame_found;
140 int64_t last_seek_max_frame;
141 int seek_stagnant_count;
142
143 int64_t last_frame;
144 int64_t largest_frame_processed;
145 int64_t current_video_frame;
146 // First decoded picture from the beginning: 0 unknown, -1 after a mid-file seek.
147 int64_t first_video_frame = 0;
148
149 int64_t audio_pts;
150 int64_t video_pts;
151 bool hold_packet;
152 double pts_offset_seconds;
153 double audio_pts_seconds;
154 double video_pts_seconds;
155 int64_t NO_PTS_OFFSET;
156 PacketStatus packet_status;
157
158 // Duration bookkeeping
159 double video_stream_duration_seconds = 0.0;
160 double audio_stream_duration_seconds = 0.0;
161 double format_duration_seconds = 0.0;
162 double inferred_duration_seconds = 0.0;
163 int source_width = 0;
164 int source_height = 0;
165 double source_rotation = 0.0;
166
167 // Cached conversion contexts and frames for performance
168 SwsContext *img_convert_ctx = nullptr;
169 SWRCONTEXT *avr_ctx = nullptr;
170 AVFrame *pFrameRGB_cached = nullptr;
171
172 int hw_de_supported = 0; // Is set by FFmpegReader
173 bool force_sw_decode = false;
174 bool hw_decode_failed = false;
175 int hw_decode_error_count = 0;
176 bool hw_decode_succeeded = false;
177#if USE_HW_ACCEL
178 AVPixelFormat hw_de_av_pix_fmt = AV_PIX_FMT_NONE;
179 AVHWDeviceType hw_de_av_device_type = AV_HWDEVICE_TYPE_NONE;
180 int IsHardwareDecodeSupported(int codecid);
181#endif
182
184 void CheckFPS();
185
187 bool CheckSeek();
188
190 void CheckWorkingFrames(int64_t requested_frame);
191
193 int64_t ConvertFrameToAudioPTS(int64_t frame_number);
194
196 int64_t ConvertFrameToVideoPTS(int64_t frame_number);
197
199 int64_t ConvertVideoPTStoFrame(int64_t pts);
200
202 std::shared_ptr<openshot::Frame> CreateFrame(int64_t requested_frame);
203
205 AudioLocation GetAudioPTSLocation(int64_t pts);
206
208 bool GetAVFrame();
209
211 bool ReopenWithoutHardwareDecode(int64_t requested_frame);
212
214 int GetNextPacket();
215
217 int64_t GetPacketPTS();
218
220 bool HasAlbumArt();
221
223 double PickDurationSeconds() const;
224
226 void ApplyDurationStrategy();
227
229 bool IsPartialFrame(int64_t requested_frame);
230
232 void ProcessVideoPacket(int64_t requested_frame);
233
235 void ProcessAudioPacket(int64_t requested_frame);
236
238 std::shared_ptr<openshot::Frame> ReadStream(int64_t requested_frame);
239
241 void RemoveAVFrame(AVFrame *);
242
244 void RemoveAVPacket(AVPacket *);
245
247 void Seek(int64_t requested_frame);
248
252 void UpdatePTSOffset();
253
255 void UpdateAudioInfo();
256
258 void UpdateVideoInfo();
259
261 void UpdateOrientedVideoInfo();
262
264 void ApplyFrameOrientation(std::shared_ptr<openshot::Frame> frame);
265
266 public:
269
273
280 FFmpegReader(const std::string& path, bool inspect_reader=true);
286 FFmpegReader(const std::string& path, DurationStrategy duration_strategy, bool inspect_reader=true);
287
289 virtual ~FFmpegReader();
290
292 void Close() override;
293
295 CacheMemory *GetCache() override { return &final_cache; };
296
301 std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
302
304 bool IsOpen() override { return is_open; };
305
307 bool HardwareDecodeSuccessful() const override;
308
310 std::string Name() override { return "FFmpegReader"; };
311
312 // Get and Set JSON methods
313 std::string Json() const override;
314 void SetJson(const std::string value) override;
315 Json::Value JsonValue() const override;
316 void SetJsonValue(const Json::Value root) override;
317
319 void Open() override;
320
322 bool GetIsDurationKnown();
323 };
324
325}
326
327#endif
Header file for AudioLocation class.
Header file for CacheMemory class.
Header file for Clip class.
Header file for TextReader class.
Header file for FFmpegUtilities.
#define SWRCONTEXT
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:103
void Open() override
Open File - which is called by the constructor automatically.
CacheMemory * GetCache() override
Get the cache object used by this reader.
Definition: FFmpegReader.h:295
bool HardwareDecodeSuccessful() const override
Return true if hardware decode was requested and successfully produced at least one frame.
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:268
std::string Name() override
Return the type name of the class.
Definition: FFmpegReader.h:310
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:304
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: AnimatedCurve.h:24
DurationStrategy
This enumeration determines which duration source to favor.
Definition: Enums.h:60
This struct holds the associated video frame and starting sample # for an audio packet.
Definition: AudioLocation.h:26
This struct holds the packet counts and end-of-file detection for an openshot::FFmpegReader.
Definition: FFmpegReader.h:47
void reset(bool eof)
Definition: FFmpegReader.h:70