Loading...
Searching...
No Matches
FFmpegReader.h
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef OPENSHOT_FFMPEG_READER_H
#define OPENSHOT_FFMPEG_READER_H
#include "ReaderBase.h"
// Include FFmpeg headers and macros
#include "FFmpegUtilities.h"
#include <cmath>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <memory>
#include "AudioLocation.h"
#include "CacheMemory.h"
#include "Clip.h"
#include "OpenMPUtilities.h"
#include "Settings.h"
namespace openshot {
struct PacketStatus {
// Track counts of video and audio packets read & decoded
int64_t video_read = 0;
int64_t video_decoded = 0;
int64_t audio_read = 0;
int64_t audio_decoded = 0;
// Track end-of-file detection on video/audio and overall
bool video_eof = true;
bool audio_eof = true;
bool packets_eof = true;
bool end_of_file = true;
int64_t packets_read() {
// Return total packets read
return video_read + audio_read;
}
int64_t packets_decoded() {
// Return total packets decoded
return video_decoded + audio_decoded;
}
void reset(bool eof) {
// Reset counts and EOF detection for packets
video_read = 0; video_decoded = 0; audio_read = 0; audio_decoded = 0;
video_eof = eof; audio_eof = eof; packets_eof = eof; end_of_file = eof;
}
};
class FFmpegReader : public ReaderBase {
private:
std::string path;
AVFormatContext *pFormatCtx;
int videoStream, audioStream;
AVCodecContext *pCodecCtx, *aCodecCtx;
#if USE_HW_ACCEL
AVBufferRef *hw_device_ctx = NULL; //PM
#endif
AVStream *pStream, *aStream;
AVPacket *packet;
AVFrame *pFrame;
bool is_open;
bool is_duration_known;
bool check_interlace;
bool check_fps;
int max_concurrent_frames;
CacheMemory working_cache;
AudioLocation previous_packet_location;
// DEBUG VARIABLES (FOR AUDIO ISSUES)
int prev_samples;
int64_t prev_pts;
int64_t pts_total;
int64_t pts_counter;
std::shared_ptr<openshot::Frame> last_video_frame;
bool is_seeking;
int64_t seeking_pts;
int64_t seeking_frame;
bool is_video_seek;
int seek_count;
int64_t seek_audio_frame_found;
int64_t seek_video_frame_found;
int64_t last_frame;
int64_t largest_frame_processed;
int64_t current_video_frame;
int64_t audio_pts;
int64_t video_pts;
bool hold_packet;
double pts_offset_seconds;
double audio_pts_seconds;
double video_pts_seconds;
int64_t NO_PTS_OFFSET;
PacketStatus packet_status;
int hw_de_supported = 0; // Is set by FFmpegReader
#if USE_HW_ACCEL
AVPixelFormat hw_de_av_pix_fmt = AV_PIX_FMT_NONE;
AVHWDeviceType hw_de_av_device_type = AV_HWDEVICE_TYPE_NONE;
int IsHardwareDecodeSupported(int codecid);
#endif
void CheckFPS();
bool CheckSeek(bool is_video);
void CheckWorkingFrames(int64_t requested_frame);
int64_t ConvertFrameToAudioPTS(int64_t frame_number);
int64_t ConvertFrameToVideoPTS(int64_t frame_number);
int64_t ConvertVideoPTStoFrame(int64_t pts);
std::shared_ptr<openshot::Frame> CreateFrame(int64_t requested_frame);
AudioLocation GetAudioPTSLocation(int64_t pts);
bool GetAVFrame();
int GetNextPacket();
int64_t GetPacketPTS();
bool HasAlbumArt();
bool IsPartialFrame(int64_t requested_frame);
void ProcessVideoPacket(int64_t requested_frame);
void ProcessAudioPacket(int64_t requested_frame);
std::shared_ptr<openshot::Frame> ReadStream(int64_t requested_frame);
void RemoveAVFrame(AVFrame *);
void RemoveAVPacket(AVPacket *);
void Seek(int64_t requested_frame);
void UpdatePTSOffset();
void UpdateAudioInfo();
void UpdateVideoInfo();
public:
CacheMemory final_cache;
bool enable_seek;
FFmpegReader(const std::string& path, bool inspect_reader=true);
virtual ~FFmpegReader();
void Close() override;
CacheMemory *GetCache() override { return &final_cache; };
std::shared_ptr<openshot::Frame> GetFrame(int64_t requested_frame) override;
bool IsOpen() override { return is_open; };
std::string Name() override { return "FFmpegReader"; };
// Get and Set JSON methods
std::string Json() const override;
void SetJson(const std::string value) override;
Json::Value JsonValue() const override;
void SetJsonValue(const Json::Value root) override;
void Open() override;
bool GetIsDurationKnown();
};
}
#endif
AudioLocation.h
Header file for AudioLocation class.
CacheMemory.h
Header file for CacheMemory class.
Clip.h
Header file for Clip class.
FFmpegUtilities.h
Header file for FFmpegUtilities.
OpenMPUtilities.h
Header file for OpenMPUtilities (set some common macros)
ReaderBase.h
Header file for ReaderBase class.
Settings.h
Header file for global Settings class.
openshot::CacheMemory
This class is a memory-based cache manager for Frame objects.
Definition CacheMemory.h:29
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::Open
void Open() override
Open File - which is called by the constructor automatically.
Definition FFmpegReader.cpp:207
openshot::FFmpegReader::GetCache
CacheMemory * GetCache() override
Get the cache object used by this reader.
Definition FFmpegReader.h:249
openshot::FFmpegReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition FFmpegReader.cpp:2372
openshot::FFmpegReader::GetIsDurationKnown
bool GetIsDurationKnown()
Return true if frame can be read with GetFrame()
Definition FFmpegReader.cpp:901
openshot::FFmpegReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition FFmpegReader.cpp:2399
openshot::FFmpegReader::final_cache
CacheMemory final_cache
Final cache object used to hold final frames.
Definition FFmpegReader.h:228
openshot::FFmpegReader::Name
std::string Name() override
Return the type name of the class.
Definition FFmpegReader.h:261
openshot::FFmpegReader::~FFmpegReader
virtual ~FFmpegReader()
Destructor.
Definition FFmpegReader.cpp:100
openshot::FFmpegReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition FFmpegReader.cpp:2365
openshot::FFmpegReader::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t requested_frame) override
Definition FFmpegReader.cpp:905
openshot::FFmpegReader::Close
void Close() override
Close File.
Definition FFmpegReader.cpp:586
openshot::FFmpegReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition FFmpegReader.cpp:2384
openshot::FFmpegReader::IsOpen
bool IsOpen() override
Determine if reader is open or closed.
Definition FFmpegReader.h:258
openshot::FFmpegReader::enable_seek
bool enable_seek
Definition FFmpegReader.h:232
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::AudioLocation
This struct holds the associated video frame and starting sample # for an audio packet.
Definition AudioLocation.h:25
openshot::PacketStatus
This struct holds the packet counts and end-of-file detection for an openshot::FFmpegReader.
Definition FFmpegReader.h:45
openshot::PacketStatus::video_read
int64_t video_read
Definition FFmpegReader.h:47
openshot::PacketStatus::packets_read
int64_t packets_read()
Definition FFmpegReader.h:58
openshot::PacketStatus::end_of_file
bool end_of_file
Definition FFmpegReader.h:56
openshot::PacketStatus::audio_read
int64_t audio_read
Definition FFmpegReader.h:49
openshot::PacketStatus::audio_decoded
int64_t audio_decoded
Definition FFmpegReader.h:50
openshot::PacketStatus::audio_eof
bool audio_eof
Definition FFmpegReader.h:54
openshot::PacketStatus::reset
void reset(bool eof)
Definition FFmpegReader.h:68
openshot::PacketStatus::packets_decoded
int64_t packets_decoded()
Definition FFmpegReader.h:63
openshot::PacketStatus::video_decoded
int64_t video_decoded
Definition FFmpegReader.h:48
openshot::PacketStatus::video_eof
bool video_eof
Definition FFmpegReader.h:53
openshot::PacketStatus::packets_eof
bool packets_eof
Definition FFmpegReader.h:55