OpenShot Library | libopenshot  0.2.7
FFmpegWriter.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for FFmpegWriter class
4  * @author Jonathan Thomas <jonathan@openshot.org>, Fabrice Bellard
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
12  * (http://www.openshotstudios.com). This file is part of
13  * OpenShot Library (http://www.openshot.org), an open-source project
14  * dedicated to delivering high quality video editing and animation solutions
15  * to the world.
16  *
17  * This file is originally based on the Libavformat API example, and then modified
18  * by the libopenshot project.
19  *
20  * OpenShot Library is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  * * OpenShot Library (libopenshot) is free software: you can redistribute it
25  * and/or modify it under the terms of the GNU Lesser General Public License
26  * as published by the Free Software Foundation, either version 3 of the
27  * License, or (at your option) any later version.
28  *
29  * OpenShot Library (libopenshot) is distributed in the hope that it will be
30  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32  * GNU Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public License
35  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
36  */
37 
38 
39 #ifndef OPENSHOT_FFMPEG_WRITER_H
40 #define OPENSHOT_FFMPEG_WRITER_H
41 
42 #include "ReaderBase.h"
43 #include "WriterBase.h"
44 
45 // Include FFmpeg headers and macros
46 #include "FFmpegUtilities.h"
47 
48 #include <cmath>
49 #include <ctime>
50 #include <unistd.h>
51 #include "CacheMemory.h"
52 #include "OpenMPUtilities.h"
53 #include "ZmqLogger.h"
54 #include "Settings.h"
55 
56 
57 namespace openshot {
58 
59  /// This enumeration designates the type of stream when encoding (video or audio)
60  enum StreamType {
61  VIDEO_STREAM, ///< A video stream (used to determine which type of stream)
62  AUDIO_STREAM ///< An audio stream (used to determine which type of stream)
63  };
64 
65  /**
66  * @brief This class uses the FFmpeg libraries, to write and encode video files and audio files.
67  *
68  * All FFmpeg options can be set using the SetOption() method, and any Reader may be used
69  * to generate openshot::Frame objects needed for writing. Be sure to use valid bit rates, frame
70  * rates, and sample rates (each format / codec has a limited # of valid options).
71  *
72  * @code SIMPLE EXAMPLE
73  *
74  * // Create a reader for a video
75  * openshot::FFmpegReader r("MyAwesomeVideo.webm");
76  * r.Open(); // Open the target reader
77  *
78  * // Create a writer (which will create a WebM video)
79  * openshot::FFmpegWriter w("/home/jonathan/NewVideo.webm");
80  *
81  * // Set options
82  *
83  * // Sample Rate: 44100, Channels: 2, Bitrate: 128000
84  * w.SetAudioOptions(true, "libvorbis", 44100, 2, openshot::ChannelLayout::LAYOUT_STEREO, 128000);
85  *
86  * // FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
87  * w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000);
88  *
89  * // Open the writer
90  * w.Open();
91  *
92  * // Write all frames from the reader
93  * w.WriteFrame(&r, 1, r.info.video_length);
94  *
95  * // Close the reader & writer
96  * w.Close();
97  * r.Close();
98  * @endcode
99  *
100  * Here is a more advanced example, which sets some additional (and optional) encoding
101  * options.
102  *
103  * @code ADVANCED WRITER EXAMPLE
104  *
105  * // Create a reader for a video
106  * openshot::FFmpegReader r("MyAwesomeVideo.webm");
107  * r.Open(); // Open the reader
108  *
109  * // Create a writer (which will create a WebM video)
110  * openshot::FFmpegWriter w("/home/jonathan/NewVideo.webm");
111  *
112  * // Set options
113  *
114  * // Sample Rate: 44100, Channels: 2, Bitrate: 128000
115  * w.SetAudioOptions(true, "libvorbis", 44100, 2, openshot::ChannelLayout::LAYOUT_STEREO, 128000);
116  *
117  * // FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
118  * w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000);
119  *
120  * // Prepare Streams (Optional method that must be called before any SetOption calls)
121  * w.PrepareStreams();
122  *
123  * // Set some specific encoding options (Optional methods)
124  * w.SetOption(VIDEO_STREAM, "qmin", "2" );
125  * w.SetOption(VIDEO_STREAM, "qmax", "30" );
126  * w.SetOption(VIDEO_STREAM, "crf", "10" );
127  * w.SetOption(VIDEO_STREAM, "rc_min_rate", "2000000" );
128  * w.SetOption(VIDEO_STREAM, "rc_max_rate", "4000000" );
129  * w.SetOption(VIDEO_STREAM, "max_b_frames", "10" );
130  *
131  * // Write the header of the video file
132  * w.WriteHeader();
133  *
134  * // Open the writer
135  * w.Open();
136  *
137  * // Write all frames from the reader
138  * w.WriteFrame(&r, 1, r.info.video_length);
139  *
140  * // Write the trailer of the video file
141  * w.WriteTrailer();
142  *
143  * // Close the reader & writer
144  * w.Close();
145  * r.Close();
146  * @endcode
147  */
148  class FFmpegWriter : public WriterBase {
149  private:
150  std::string path;
151  int cache_size;
152  bool is_writing;
153  bool is_open;
154  int64_t video_timestamp;
155  int64_t audio_timestamp;
156 
157  bool prepare_streams;
158  bool write_header;
159  bool write_trailer;
160 
161  AVFormatContext* oc;
162  AVStream *audio_st, *video_st;
163  AVCodecContext *video_codec_ctx;
164  AVCodecContext *audio_codec_ctx;
165  SwsContext *img_convert_ctx;
166  int16_t *samples;
167  uint8_t *audio_outbuf;
168  uint8_t *audio_encoder_buffer;
169 
170  int num_of_rescalers;
171  int rescaler_position;
172  std::vector<SwsContext *> image_rescalers;
173 
174  int audio_outbuf_size;
175  int audio_input_frame_size;
176  int initial_audio_input_frame_size;
177  int audio_input_position;
178  int audio_encoder_buffer_size;
179  SWRCONTEXT *avr;
180  SWRCONTEXT *avr_planar;
181 
182  /* Resample options */
183  int original_sample_rate;
184  int original_channels;
185 
186  std::shared_ptr<openshot::Frame> last_frame;
187  std::deque<std::shared_ptr<openshot::Frame> > spooled_audio_frames;
188  std::deque<std::shared_ptr<openshot::Frame> > spooled_video_frames;
189 
190  std::deque<std::shared_ptr<openshot::Frame> > queued_audio_frames;
191  std::deque<std::shared_ptr<openshot::Frame> > queued_video_frames;
192 
193  std::deque<std::shared_ptr<openshot::Frame> > processed_frames;
194  std::deque<std::shared_ptr<openshot::Frame> > deallocate_frames;
195 
196  std::map<std::shared_ptr<openshot::Frame>, AVFrame *> av_frames;
197 
198  /// Add an AVFrame to the cache
199  void add_avframe(std::shared_ptr<openshot::Frame> frame, AVFrame *av_frame);
200 
201  /// Add an audio output stream
202  AVStream *add_audio_stream();
203 
204  /// Add a video output stream
205  AVStream *add_video_stream();
206 
207  /// Allocate an AVFrame object
208  AVFrame *allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer);
209 
210  /// Auto detect format (from path)
211  void auto_detect_format();
212 
213  /// Close the audio codec
214  void close_audio(AVFormatContext *oc, AVStream *st);
215 
216  /// Close the video codec
217  void close_video(AVFormatContext *oc, AVStream *st);
218 
219  /// Flush encoders
220  void flush_encoders();
221 
222  /// initialize streams
223  void initialize_streams();
224 
225  /// @brief Init a collection of software rescalers (thread safe)
226  /// @param source_width The source width of the image scalers (used to cache a bunch of scalers)
227  /// @param source_height The source height of the image scalers (used to cache a bunch of scalers)
228  void InitScalers(int source_width, int source_height);
229 
230  /// open audio codec
231  void open_audio(AVFormatContext *oc, AVStream *st);
232 
233  /// open video codec
234  void open_video(AVFormatContext *oc, AVStream *st);
235 
236  /// process video frame
237  void process_video_packet(std::shared_ptr<openshot::Frame> frame);
238 
239  /// write all queued frames' audio to the video file
240  void write_audio_packets(bool is_final);
241 
242  /// write video frame
243  bool write_video_packet(std::shared_ptr<openshot::Frame> frame, AVFrame *frame_final);
244 
245  /// write all queued frames
246  void write_queued_frames();
247 
248  public:
249 
250  /// @brief Constructor for FFmpegWriter.
251  /// Throws an exception on failure to open path.
252  ///
253  /// @param path The file path of the video file you want to open and read
254  FFmpegWriter(const std::string& path);
255 
256  /// Close the writer
257  void Close();
258 
259  /// Get the cache size (number of frames to queue before writing)
260  int GetCacheSize() { return cache_size; };
261 
262  /// Determine if writer is open or closed
263  bool IsOpen() { return is_open; };
264 
265  /// Determine if codec name is valid
266  static bool IsValidCodec(std::string codec_name);
267 
268  /// Open writer
269  void Open();
270 
271  /// Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
272  void OutputStreamInfo();
273 
274  /// @brief Prepare & initialize streams and open codecs. This method is called automatically
275  /// by the Open() method if this method has not yet been called.
276  void PrepareStreams();
277 
278  /// Remove & deallocate all software scalers
279  void RemoveScalers();
280 
281  /// @brief Set audio resample options
282  /// @param sample_rate The number of samples per second of the audio
283  /// @param channels The number of audio channels
284  void ResampleAudio(int sample_rate, int channels);
285 
286  /// @brief Set audio export options
287  /// @param has_audio Does this file need an audio stream?
288  /// @param codec The codec used to encode the audio for this file
289  /// @param sample_rate The number of audio samples needed in this file
290  /// @param channels The number of audio channels needed in this file
291  /// @param channel_layout The 'layout' of audio channels (i.e. mono, stereo, surround, etc...)
292  /// @param bit_rate The audio bit rate used during encoding
293  ///
294  /// \note This is an overloaded function.
295  void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate);
296 
297  /// @brief Set audio export options.
298  ///
299  /// Enables the stream and configures a default 2-channel stereo layout.
300  ///
301  /// @param codec The codec used to encode the audio for this file
302  /// @param sample_rate The number of audio samples needed in this file
303  /// @param bit_rate The audio bit rate used during encoding
304  ///
305  /// \note This is an overloaded function.
306  void SetAudioOptions(std::string codec, int sample_rate, int bit_rate);
307 
308  /// @brief Set the cache size
309  /// @param new_size The number of frames to queue before writing to the file
310  void SetCacheSize(int new_size) { cache_size = new_size; };
311 
312  /// @brief Set video export options
313  /// @param has_video Does this file need a video stream
314  /// @param codec The codec used to encode the images in this video
315  /// @param fps The number of frames per second
316  /// @param width The width in pixels of this video
317  /// @param height The height in pixels of this video
318  /// @param pixel_ratio The shape of the pixels represented as a openshot::Fraction (1x1 is most common / square pixels)
319  /// @param interlaced Does this video need to be interlaced?
320  /// @param top_field_first Which frame should be used as the top field?
321  /// @param bit_rate The video bit rate used during encoding
322  ///
323  /// \note This is an overloaded function.
324  void SetVideoOptions(bool has_video, std::string codec, openshot::Fraction fps, int width, int height, openshot::Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate);
325 
326  /// @brief Set video export options.
327  ///
328  /// Enables the stream and configures non-interlaced video with a 1:1 pixel aspect ratio.
329  ///
330  /// @param codec The codec used to encode the images in this video
331  /// @param width The width in pixels of this video
332  /// @param height The height in pixels of this video
333  /// @param fps The number of frames per second
334  /// @param bit_rate The video bit rate used during encoding
335  ///
336  /// \note This is an overloaded function.
337  /// \warning Observe the argument order, which is consistent with the openshot::Timeline constructor, but differs from the other signature.
338  void SetVideoOptions(std::string codec, int width, int height, openshot::Fraction fps, int bit_rate);
339 
340  /// @brief Set custom options (some codecs accept additional params). This must be called after the
341  /// PrepareStreams() method, otherwise the streams have not been initialized yet.
342  ///
343  /// @param stream The stream (openshot::StreamType) this option should apply to
344  /// @param name The name of the option you want to set (i.e. qmin, qmax, etc...)
345  /// @param value The new value of this option
346  void SetOption(openshot::StreamType stream, std::string name, std::string value);
347 
348  /// @brief Write the file header (after the options are set). This method is called automatically
349  /// by the Open() method if this method has not yet been called.
350  void WriteHeader();
351 
352  /// @brief Add a frame to the stack waiting to be encoded.
353  /// @param frame The openshot::Frame object to write to this image
354  ///
355  /// \note This is an overloaded function.
356  void WriteFrame(std::shared_ptr<openshot::Frame> frame);
357 
358  /// @brief Write a block of frames from a reader
359  /// @param reader A openshot::ReaderBase object which will provide frames to be written
360  /// @param start The starting frame number of the reader
361  /// @param length The number of frames to write
362  ///
363  /// \note This is an overloaded function.
364  void WriteFrame(openshot::ReaderBase *reader, int64_t start, int64_t length);
365 
366  /// @brief Write the file trailer (after all frames are written). This is called automatically
367  /// by the Close() method if this method has not yet been called.
368  void WriteTrailer();
369 
370  };
371 
372 }
373 
374 #endif
Header file for CacheMemory class.
Header file for FFmpegUtilities.
#define PixelFormat
#define SWRCONTEXT
Header file for OpenMPUtilities (set some common macros)
Header file for ReaderBase class.
Header file for global Settings class.
Header file for WriterBase class.
Header file for ZeroMQ-based Logger class.
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition: FFmpegWriter.h:148
void Close()
Close the writer.
void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate)
Set audio export options.
void SetOption(openshot::StreamType stream, std::string name, std::string value)
Set custom options (some codecs accept additional params). This must be called after the PrepareStrea...
void PrepareStreams()
Prepare & initialize streams and open codecs. This method is called automatically by the Open() metho...
void SetVideoOptions(bool has_video, std::string codec, openshot::Fraction fps, int width, int height, openshot::Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate)
Set video export options.
bool IsOpen()
Determine if writer is open or closed.
Definition: FFmpegWriter.h:263
void SetCacheSize(int new_size)
Set the cache size.
Definition: FFmpegWriter.h:310
void ResampleAudio(int sample_rate, int channels)
Set audio resample options.
int GetCacheSize()
Get the cache size (number of frames to queue before writing)
Definition: FFmpegWriter.h:260
void Open()
Open writer.
void WriteHeader()
Write the file header (after the options are set). This method is called automatically by the Open() ...
FFmpegWriter(const std::string &path)
Constructor for FFmpegWriter. Throws an exception on failure to open path.
static bool IsValidCodec(std::string codec_name)
Determine if codec name is valid.
void OutputStreamInfo()
Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
void WriteTrailer()
Write the file trailer (after all frames are written). This is called automatically by the Close() me...
void RemoveScalers()
Remove & deallocate all software scalers.
This class represents a fraction.
Definition: Fraction.h:48
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:98
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition: WriterBase.h:88
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
StreamType
This enumeration designates the type of stream when encoding (video or audio)
Definition: FFmpegWriter.h:60
@ AUDIO_STREAM
An audio stream (used to determine which type of stream)
Definition: FFmpegWriter.h:62
@ VIDEO_STREAM
A video stream (used to determine which type of stream)
Definition: FFmpegWriter.h:61