OpenShot Library | libopenshot  0.2.6
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  AVOutputFormat *fmt;
162  AVFormatContext *oc;
163  AVStream *audio_st, *video_st;
164  AVCodecContext *video_codec_ctx;
165  AVCodecContext *audio_codec_ctx;
166  SwsContext *img_convert_ctx;
167  int16_t *samples;
168  uint8_t *audio_outbuf;
169  uint8_t *audio_encoder_buffer;
170 
171  int num_of_rescalers;
172  int rescaler_position;
173  std::vector<SwsContext *> image_rescalers;
174 
175  int audio_outbuf_size;
176  int audio_input_frame_size;
177  int initial_audio_input_frame_size;
178  int audio_input_position;
179  int audio_encoder_buffer_size;
180  SWRCONTEXT *avr;
181  SWRCONTEXT *avr_planar;
182 
183  /* Resample options */
184  int original_sample_rate;
185  int original_channels;
186 
187  std::shared_ptr<openshot::Frame> last_frame;
188  std::deque<std::shared_ptr<openshot::Frame> > spooled_audio_frames;
189  std::deque<std::shared_ptr<openshot::Frame> > spooled_video_frames;
190 
191  std::deque<std::shared_ptr<openshot::Frame> > queued_audio_frames;
192  std::deque<std::shared_ptr<openshot::Frame> > queued_video_frames;
193 
194  std::deque<std::shared_ptr<openshot::Frame> > processed_frames;
195  std::deque<std::shared_ptr<openshot::Frame> > deallocate_frames;
196 
197  std::map<std::shared_ptr<openshot::Frame>, AVFrame *> av_frames;
198 
199  /// Add an AVFrame to the cache
200  void add_avframe(std::shared_ptr<openshot::Frame> frame, AVFrame *av_frame);
201 
202  /// Add an audio output stream
203  AVStream *add_audio_stream();
204 
205  /// Add a video output stream
206  AVStream *add_video_stream();
207 
208  /// Allocate an AVFrame object
209  AVFrame *allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer);
210 
211  /// Auto detect format (from path)
212  void auto_detect_format();
213 
214  /// Close the audio codec
215  void close_audio(AVFormatContext *oc, AVStream *st);
216 
217  /// Close the video codec
218  void close_video(AVFormatContext *oc, AVStream *st);
219 
220  /// Flush encoders
221  void flush_encoders();
222 
223  /// initialize streams
224  void initialize_streams();
225 
226  /// @brief Init a collection of software rescalers (thread safe)
227  /// @param source_width The source width of the image scalers (used to cache a bunch of scalers)
228  /// @param source_height The source height of the image scalers (used to cache a bunch of scalers)
229  void InitScalers(int source_width, int source_height);
230 
231  /// open audio codec
232  void open_audio(AVFormatContext *oc, AVStream *st);
233 
234  /// open video codec
235  void open_video(AVFormatContext *oc, AVStream *st);
236 
237  /// process video frame
238  void process_video_packet(std::shared_ptr<openshot::Frame> frame);
239 
240  /// write all queued frames' audio to the video file
241  void write_audio_packets(bool is_final);
242 
243  /// write video frame
244  bool write_video_packet(std::shared_ptr<openshot::Frame> frame, AVFrame *frame_final);
245 
246  /// write all queued frames
247  void write_queued_frames();
248 
249  public:
250 
251  /// @brief Constructor for FFmpegWriter.
252  /// Throws an exception on failure to open path.
253  ///
254  /// @param path The file path of the video file you want to open and read
255  FFmpegWriter(const std::string& path);
256 
257  /// Close the writer
258  void Close();
259 
260  /// Get the cache size (number of frames to queue before writing)
261  int GetCacheSize() { return cache_size; };
262 
263  /// Determine if writer is open or closed
264  bool IsOpen() { return is_open; };
265 
266  /// Determine if codec name is valid
267  static bool IsValidCodec(std::string codec_name);
268 
269  /// Open writer
270  void Open();
271 
272  /// Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
273  void OutputStreamInfo();
274 
275  /// @brief Prepare & initialize streams and open codecs. This method is called automatically
276  /// by the Open() method if this method has not yet been called.
277  void PrepareStreams();
278 
279  /// Remove & deallocate all software scalers
280  void RemoveScalers();
281 
282  /// @brief Set audio resample options
283  /// @param sample_rate The number of samples per second of the audio
284  /// @param channels The number of audio channels
285  void ResampleAudio(int sample_rate, int channels);
286 
287  /// @brief Set audio export options
288  /// @param has_audio Does this file need an audio stream?
289  /// @param codec The codec used to encode the audio for this file
290  /// @param sample_rate The number of audio samples needed in this file
291  /// @param channels The number of audio channels needed in this file
292  /// @param channel_layout The 'layout' of audio channels (i.e. mono, stereo, surround, etc...)
293  /// @param bit_rate The audio bit rate used during encoding
294  ///
295  /// \note This is an overloaded function.
296  void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate);
297 
298  /// @brief Set audio export options.
299  ///
300  /// Enables the stream and configures a default 2-channel stereo layout.
301  ///
302  /// @param codec The codec used to encode the audio for this file
303  /// @param sample_rate The number of audio samples needed in this file
304  /// @param bit_rate The audio bit rate used during encoding
305  ///
306  /// \note This is an overloaded function.
307  void SetAudioOptions(std::string codec, int sample_rate, int bit_rate);
308 
309  /// @brief Set the cache size
310  /// @param new_size The number of frames to queue before writing to the file
311  void SetCacheSize(int new_size) { cache_size = new_size; };
312 
313  /// @brief Set video export options
314  /// @param has_video Does this file need a video stream
315  /// @param codec The codec used to encode the images in this video
316  /// @param fps The number of frames per second
317  /// @param width The width in pixels of this video
318  /// @param height The height in pixels of this video
319  /// @param pixel_ratio The shape of the pixels represented as a openshot::Fraction (1x1 is most common / square pixels)
320  /// @param interlaced Does this video need to be interlaced?
321  /// @param top_field_first Which frame should be used as the top field?
322  /// @param bit_rate The video bit rate used during encoding
323  ///
324  /// \note This is an overloaded function.
325  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);
326 
327  /// @brief Set video export options.
328  ///
329  /// Enables the stream and configures non-interlaced video with a 1:1 pixel aspect ratio.
330  ///
331  /// @param codec The codec used to encode the images in this video
332  /// @param width The width in pixels of this video
333  /// @param height The height in pixels of this video
334  /// @param fps The number of frames per second
335  /// @param bit_rate The video bit rate used during encoding
336  ///
337  /// \note This is an overloaded function.
338  /// \warning Observe the argument order, which is consistent with the openshot::Timeline constructor, but differs from the other signature.
339  void SetVideoOptions(std::string codec, int width, int height, openshot::Fraction fps, int bit_rate);
340 
341  /// @brief Set custom options (some codecs accept additional params). This must be called after the
342  /// PrepareStreams() method, otherwise the streams have not been initialized yet.
343  ///
344  /// @param stream The stream (openshot::StreamType) this option should apply to
345  /// @param name The name of the option you want to set (i.e. qmin, qmax, etc...)
346  /// @param value The new value of this option
347  void SetOption(openshot::StreamType stream, std::string name, std::string value);
348 
349  /// @brief Write the file header (after the options are set). This method is called automatically
350  /// by the Open() method if this method has not yet been called.
351  void WriteHeader();
352 
353  /// @brief Add a frame to the stack waiting to be encoded.
354  /// @param frame The openshot::Frame object to write to this image
355  ///
356  /// \note This is an overloaded function.
357  void WriteFrame(std::shared_ptr<openshot::Frame> frame);
358 
359  /// @brief Write a block of frames from a reader
360  /// @param reader A openshot::ReaderBase object which will provide frames to be written
361  /// @param start The starting frame number of the reader
362  /// @param length The number of frames to write
363  ///
364  /// \note This is an overloaded function.
365  void WriteFrame(openshot::ReaderBase *reader, int64_t start, int64_t length);
366 
367  /// @brief Write the file trailer (after all frames are written). This is called automatically
368  /// by the Close() method if this method has not yet been called.
369  void WriteTrailer();
370 
371  };
372 
373 }
374 
375 #endif
A video stream (used to determine which type of stream)
Definition: FFmpegWriter.h:61
#define SWRCONTEXT
int GetCacheSize()
Get the cache size (number of frames to queue before writing)
Definition: FFmpegWriter.h:261
void OutputStreamInfo()
Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
An audio stream (used to determine which type of stream)
Definition: FFmpegWriter.h:62
Header file for ReaderBase class.
Header file for OpenMPUtilities (set some common macros)
This class uses the FFmpeg libraries, to write and encode video files and audio files.
Definition: FFmpegWriter.h:148
static bool IsValidCodec(std::string codec_name)
Determine if codec name is valid.
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
Header file for CacheMemory class.
bool IsOpen()
Determine if writer is open or closed.
Definition: FFmpegWriter.h:264
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
void Open()
Open writer.
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.
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 RemoveScalers()
Remove & deallocate all software scalers.
Header file for WriterBase class.
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition: WriterBase.h:87
This class represents a fraction.
Definition: Fraction.h:48
Header file for ZeroMQ-based Logger class.
void ResampleAudio(int sample_rate, int channels)
Set audio resample options.
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround...
void SetCacheSize(int new_size)
Set the cache size.
Definition: FFmpegWriter.h:311
Header file for global Settings class.
#define PixelFormat
void WriteTrailer()
Write the file trailer (after all frames are written). This is called automatically by the Close() me...
void WriteHeader()
Write the file header (after the options are set). This method is called automatically by the Open() ...
void Close()
Close the writer.
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
void PrepareStreams()
Prepare & initialize streams and open codecs. This method is called automatically by the Open() metho...
Header file for FFmpegUtilities.
FFmpegWriter(const std::string &path)
Constructor for FFmpegWriter. Throws an exception on failure to open path.
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...
StreamType
This enumeration designates the type of stream when encoding (video or audio)
Definition: FFmpegWriter.h:60