OpenShot Library | libopenshot  0.3.0
FrameMapper.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_FRAMEMAPPER_H
14 #define OPENSHOT_FRAMEMAPPER_H
15 
16 #include <assert.h>
17 #include <iostream>
18 #include <vector>
19 #include <memory>
20 
21 #include "CacheMemory.h"
22 #include "ReaderBase.h"
23 #include "Frame.h"
24 #include "Fraction.h"
25 #include "KeyFrame.h"
26 
27 
28 // Include FFmpeg headers and macros
29 #include "FFmpegUtilities.h"
30 #include "OpenMPUtilities.h"
31 
32 
33 namespace openshot
34 {
42  {
46  };
47 
54  struct Field
55  {
56  int64_t Frame;
57  bool isOdd;
58 
59  Field() : Frame(0), isOdd(true) { };
60 
61  Field(int64_t frame, bool isodd)
62  {
63  Frame = frame;
64  isOdd = isodd;
65  }
66  };
67 
74  struct SampleRange
75  {
76  int64_t frame_start;
78 
79  int64_t frame_end;
81 
82  int total;
83  };
84 
91  struct MappedFrame
92  {
96  };
97 
98 
120  class FrameMapper : public ReaderBase {
121  private:
122  bool field_toggle; // Internal odd / even toggle (used when building the mapping)
123  Fraction original; // The original frame rate
124  Fraction target; // The target frame rate
125  PulldownType pulldown; // The pull-down technique
126  ReaderBase *reader; // The source video reader
127  CacheMemory final_cache; // Cache of actual Frame objects
128  bool is_dirty; // When this is true, the next call to GetFrame will re-init the mapping
129  float parent_position; // Position of parent clip (which is used to generate the audio mapping)
130  float parent_start; // Start of parent clip (which is used to generate the audio mapping)
131  SWRCONTEXT *avr; // Audio resampling context object
132 
133  // Internal methods used by init
134  void AddField(int64_t frame);
135  void AddField(Field field);
136 
137  // Clear both the fields & frames lists
138  void Clear();
139 
140  // Get Frame or Generate Blank Frame
141  std::shared_ptr<Frame> GetOrCreateFrame(int64_t number);
142 
144  int64_t AdjustFrameNumber(int64_t clip_frame_number);
145 
146  // Use the original and target frame rates and a pull-down technique to create
147  // a mapping between the original fields and frames or a video to a new frame rate.
148  // This might repeat or skip fields and frames of the original video, depending on
149  // whether the frame rate is increasing or decreasing.
150  void Init();
151 
152  public:
153  // Init some containers
154  std::vector<Field> fields; // List of all fields
155  std::vector<MappedFrame> frames; // List of all frames
156 
158  FrameMapper(ReaderBase *reader, Fraction target_fps, PulldownType target_pulldown, int target_sample_rate, int target_channels, ChannelLayout target_channel_layout);
159 
161  virtual ~FrameMapper();
162 
164  void ChangeMapping(Fraction target_fps, PulldownType pulldown, int target_sample_rate, int target_channels, ChannelLayout target_channel_layout);
165 
167  void Close() override;
168 
170  MappedFrame GetMappedFrame(int64_t TargetFrameNumber);
171 
173  CacheMemory* GetCache() override { return &final_cache; };
174 
181  std::shared_ptr<Frame> GetFrame(int64_t requested_frame) override;
182 
184  bool IsOpen() override;
185 
187  std::string Name() override { return "FrameMapper"; };
188 
189  // Get and Set JSON methods
190  std::string Json() const override;
191  void SetJson(const std::string value) override;
192  Json::Value JsonValue() const override;
193  void SetJsonValue(const Json::Value root) override;
194 
196  void Open() override;
197 
199  void PrintMapping(std::ostream* out=&std::cout);
200 
202  ReaderBase* Reader();
203 
205  void Reader(ReaderBase *new_reader) { reader = new_reader; }
206 
208  void ResampleMappedAudio(std::shared_ptr<Frame> frame, int64_t original_frame_number);
209  };
210 }
211 
212 #endif
Header file for CacheMemory class.
Header file for FFmpegUtilities.
#define SWRCONTEXT
Header file for Fraction class.
Header file for Frame class.
Header file for the Keyframe class.
Header file for OpenMPUtilities (set some common macros)
Header file for ReaderBase class.
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:29
This class represents a fraction.
Definition: Fraction.h:30
This class creates a mapping between 2 different frame rates, applying a specific pull-down technique...
Definition: FrameMapper.h:120
CacheMemory * GetCache() override
Get the cache object used by this reader.
Definition: FrameMapper.h:173
std::shared_ptr< Frame > GetFrame(int64_t requested_frame) override
This method is required for all derived classes of ReaderBase, and return the openshot::Frame object,...
void ChangeMapping(Fraction target_fps, PulldownType pulldown, int target_sample_rate, int target_channels, ChannelLayout target_channel_layout)
Change frame rate or audio mapping details.
void Reader(ReaderBase *new_reader)
Set the current reader.
Definition: FrameMapper.h:205
MappedFrame GetMappedFrame(int64_t TargetFrameNumber)
Get a frame based on the target frame rate and the new frame number of a frame.
std::string Name() override
Return the type name of the class.
Definition: FrameMapper.h:187
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
bool IsOpen() override
Determine if reader is open or closed.
std::vector< Field > fields
Definition: FrameMapper.h:154
ReaderBase * Reader()
Get the current reader.
Definition: FrameMapper.cpp:61
std::vector< MappedFrame > frames
Definition: FrameMapper.h:155
void ResampleMappedAudio(std::shared_ptr< Frame > frame, int64_t original_frame_number)
Resample audio and map channels (if needed)
void Open() override
Open the internal reader.
void Close() override
Close the openshot::FrameMapper and internal reader.
std::string Json() const override
Generate JSON string of this object.
FrameMapper(ReaderBase *reader, Fraction target_fps, PulldownType target_pulldown, int target_sample_rate, int target_channels, ChannelLayout target_channel_layout)
Default constructor for openshot::FrameMapper class.
Definition: FrameMapper.cpp:25
void PrintMapping(std::ostream *out=&std::cout)
Print all of the original frames and which new frames they map to.
void SetJson(const std::string value) override
Load JSON string into this object.
Json::Value JsonValue() const override
Generate Json::Value for this object.
virtual ~FrameMapper()
Destructor.
Definition: FrameMapper.cpp:52
This class represents a single frame of video (i.e. image & audio data)
Definition: Frame.h:91
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
PulldownType
This enumeration determines how frame rates are increased or decreased.
Definition: FrameMapper.h:42
@ PULLDOWN_CLASSIC
Classic 2:3:2:3 pull-down.
Definition: FrameMapper.h:43
@ PULLDOWN_ADVANCED
Advanced 2:3:3:2 pull-down (minimal dirty frames)
Definition: FrameMapper.h:44
@ PULLDOWN_NONE
Do not apply pull-down techniques, just repeat or skip entire frames.
Definition: FrameMapper.h:45
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
This struct holds a single field (half a frame).
Definition: FrameMapper.h:55
Field(int64_t frame, bool isodd)
Definition: FrameMapper.h:61
This struct holds two fields which together make up a complete video frame.
Definition: FrameMapper.h:92
SampleRange Samples
Definition: FrameMapper.h:95
This struct holds a the range of samples needed by this frame.
Definition: FrameMapper.h:75