Ocean
FFMMovie.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MEDIA_FFM_MOVIE_H
9 #define META_OCEAN_MEDIA_FFM_MOVIE_H
10 
13 
14 #include "ocean/base/Thread.h"
15 
16 #include "ocean/media/Movie.h"
17 
18 // Forward declaration.
19 struct AVCodecContext;
20 struct AVFrame;
21 struct AVStream;
22 
23 namespace Ocean
24 {
25 
26 namespace Media
27 {
28 
29 namespace FFmpeg
30 {
31 
32 /**
33  * This class implements an FFmpeg movie object.
34  * @ingroup mediaffm
35  */
36 class FFMMovie :
37  virtual public FFMMedium,
38  virtual public Movie,
39  protected Thread
40 {
41  friend class FFMLibrary;
42 
43  protected:
44 
45  /**
46  * Definition of an unordered map mapping frame indices to presentation timestamps.
47  */
48  typedef std::unordered_map<int64_t, int64_t> PacketTimestampMap;
49 
50  public:
51 
52  /**
53  * Clones this movie medium and returns a new independent instance of this medium.
54  * @see Medium::clone()
55  */
56  MediumRef clone() const override;
57 
58  /**
59  * Returns the duration of the movie medium.
60  * @see FiniteMedium::duration()
61  */
62  double duration() const override;
63 
64  /**
65  * Returns the duration of the movie medium without speed consideration.
66  * @see FiniteMedium::normalDuration()
67  */
68  double normalDuration() const override;
69 
70  /**
71  * Returns the recent position of the movie medium.
72  * @see FiniteMedium::position()
73  */
74  double position() const override;
75 
76  /**
77  * Sets the recent position of the movie medium.
78  * @see FiniteMedium::setPosition()
79  */
80  bool setPosition(const double position) override;
81 
82  /**
83  * Returns the speed of the movie medium.
84  * @see FiniteMedium::speed()
85  */
86  float speed() const override;
87 
88  /**
89  * Sets the speed of the movie medium.
90  * @see FiniteMedium::setSpeed()
91  */
92  bool setSpeed(const float speed) override;
93 
94  /**
95  * Returns the volume of the sound in db.
96  * @see SoundMedium::soundVolume()
97  */
98  float soundVolume() const override;
99 
100  /**
101  * Returns whether the movie medium is in a mute state.
102  * @see SoundMedium::soundMute()
103  */
104  bool soundMute() const override;
105 
106  /**
107  * Sets the volume of the sound in db.
108  * @see SoundMedium::setSoundVolume()
109  */
110  bool setSoundVolume(const float volume) override;
111 
112  /**
113  * Sets or unsets the movie medium to a mute state.
114  * @see SoundMedium::setSoundMute()
115  */
116  bool setSoundMute(const bool mute) override;
117 
118  /**
119  * Enables or disables the audio in this movie (has no effect if the movie does not have audio).
120  * @see Movie::setUseSound()
121  */
122  bool setUseSound(const bool state) override;
123 
124  protected:
125 
126  /**
127  * Creates a new movie by a given url.
128  * @param url Url of the movie
129  */
130  explicit FFMMovie(const std::string& url);
131 
132  /**
133  * Destructs a FFMMovie object.
134  */
135  ~FFMMovie() override;
136 
137  /**
138  * Internally starts the medium.
139  * @see AVFMedium::internalStart()
140  */
141  bool internalStart() override;
142 
143  /**
144  * Internally pauses the medium.
145  * @see AVFMedium::internalPause()
146  */
147  bool internalPause() override;
148 
149  /**
150  * Internally stops the medium.
151  * @see AVFMedium::internalStop()
152  */
153  bool internalStop() override;
154 
155  /**
156  * Creates and opens the video codec.
157  * @return True, if succeeded
158  */
160 
161  /**
162  * Releases the video codec.
163  */
165 
166  /**
167  * Extracts the Ocean frame from a FFmpeg frame.
168  * @param avFrame The FFmpeg frame from which the data will be extracted, must be valid
169  * @param avPixelFormat The pixel format of the FFmpeg frame, provided as 'AVPixelFormat', must be valid
170  * @param avColorRange The FFmpeg color range associated with the pixel format, provided as 'AVColorRange'
171  * @return The resulting Ocean frame owning the memory, invalid otherwise
172  */
173  static Frame extractFrame(AVFrame* avFrame, const int avPixelFormat, const int avColorRange);
174 
175  /**
176  * Translates a FFmpeg pixel format to an Ocean pixel format.
177  * @param avPixelFormat The FFmpeg pixel format to translate, provided as 'AVPixelFormat'
178  * @param avColorRange The FFmpeg color range associated with the pixel format, provided as 'AVColorRange'
179  * @return The corresponding Ocean pixel format, FORMAT_UNDEFINED if no corresponding pixel format exists
180  */
181  static FrameType::PixelFormat translatePixelFormat(const int avPixelFormat, const int avColorRange);
182 
183  private:
184 
185  /**
186  * Thread run function.
187  */
188  void threadRun() override;
189 
190  protected:
191 
192  /// The FFmpeg video codec context.
193  AVCodecContext* avVideoCodecContext_ = nullptr;
194 
195  /// The FFmpeg video stream.
196  AVStream* avVideoStream_ = nullptr;
197 
198  /// The index of the FFmpeg video stream.
200 
201  /// The current playback position in seconds.
202  std::atomic<double> position_ = 0.0;
203 
204  /// The seek playback position in seconds, -1 if not set.
205  std::atomic<double> seekPosition_ = -1.0;
206 
207  /// The duration of the movie in seconds, for a default speed with 1.0, -1 if unknown
208  double normalDuration_ = -1.0;
209 
210  /// The playback speed of the movie.
211  std::atomic<float> speed_ = 1.0f;
212 
213  /// True, if the movie is paused.
214  std::atomic<bool> isPaused_ = false;
215 };
216 
217 }
218 
219 }
220 
221 }
222 
223 #endif // META_OCEAN_MEDIA_FFM_MOVIE_H
This class implements Ocean's image class.
Definition: Frame.h:1792
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
This class implements the FFmpeg library.
Definition: FFMLibrary.h:30
This is the base class for all FFmpeg mediums.
Definition: FFMMedium.h:32
This class implements an FFmpeg movie object.
Definition: FFMMovie.h:40
bool setSoundVolume(const float volume) override
Sets the volume of the sound in db.
void threadRun() override
Thread run function.
bool internalStart() override
Internally starts the medium.
double duration() const override
Returns the duration of the movie medium.
float soundVolume() const override
Returns the volume of the sound in db.
MediumRef clone() const override
Clones this movie medium and returns a new independent instance of this medium.
void releaseVideoCodec()
Releases the video codec.
bool internalPause() override
Internally pauses the medium.
std::atomic< double > seekPosition_
The seek playback position in seconds, -1 if not set.
Definition: FFMMovie.h:205
std::atomic< float > speed_
The playback speed of the movie.
Definition: FFMMovie.h:211
bool soundMute() const override
Returns whether the movie medium is in a mute state.
double position() const override
Returns the recent position of the movie medium.
bool createAndOpenVideoCodec()
Creates and opens the video codec.
FFMMovie(const std::string &url)
Creates a new movie by a given url.
bool setSoundMute(const bool mute) override
Sets or unsets the movie medium to a mute state.
std::atomic< double > position_
The current playback position in seconds.
Definition: FFMMovie.h:202
static Frame extractFrame(AVFrame *avFrame, const int avPixelFormat, const int avColorRange)
Extracts the Ocean frame from a FFmpeg frame.
std::unordered_map< int64_t, int64_t > PacketTimestampMap
Definition of an unordered map mapping frame indices to presentation timestamps.
Definition: FFMMovie.h:48
AVStream * avVideoStream_
The FFmpeg video stream.
Definition: FFMMovie.h:196
bool setSpeed(const float speed) override
Sets the speed of the movie medium.
bool internalStop() override
Internally stops the medium.
bool setUseSound(const bool state) override
Enables or disables the audio in this movie (has no effect if the movie does not have audio).
double normalDuration_
The duration of the movie in seconds, for a default speed with 1.0, -1 if unknown.
Definition: FFMMovie.h:208
float speed() const override
Returns the speed of the movie medium.
static FrameType::PixelFormat translatePixelFormat(const int avPixelFormat, const int avColorRange)
Translates a FFmpeg pixel format to an Ocean pixel format.
AVCodecContext * avVideoCodecContext_
The FFmpeg video codec context.
Definition: FFMMovie.h:193
~FFMMovie() override
Destructs a FFMMovie object.
std::atomic< bool > isPaused_
True, if the movie is paused.
Definition: FFMMovie.h:214
bool setPosition(const double position) override
Sets the recent position of the movie medium.
int avVideoStreamIndex_
The index of the FFmpeg video stream.
Definition: FFMMovie.h:199
double normalDuration() const override
Returns the duration of the movie medium without speed consideration.
const std::string & url() const
Returns the url of the medium.
Definition: Medium.h:236
This class is the base class for all movies.
Definition: Movie.h:40
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
This class implements a thread.
Definition: Thread.h:115
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15