OpenShot Library | libopenshot  0.2.4
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 <iostream>
51 #include <stdio.h>
52 #include <unistd.h>
53 #include "CacheMemory.h"
54 #include "Exceptions.h"
55 #include "OpenMPUtilities.h"
56 #include "ZmqLogger.h"
57 #include "Settings.h"
58 
59 
60 namespace openshot {
61 
62  /// This enumeration designates the type of stream when encoding (video or audio)
63  enum StreamType {
64  VIDEO_STREAM, ///< A video stream (used to determine which type of stream)
65  AUDIO_STREAM ///< An audio stream (used to determine which type of stream)
66  };
67 
68  /**
69  * @brief This class uses the FFmpeg libraries, to write and encode video files and audio files.
70  *
71  * All FFmpeg options can be set using the SetOption() method, and any Reader may be used
72  * to generate openshot::Frame objects needed for writing. Be sure to use valid bit rates, frame
73  * rates, and sample rates (each format / codec has a limited # of valid options).
74  *
75  * @code SIMPLE EXAMPLE
76  *
77  * // Create a reader for a video
78  * FFmpegReader r("MyAwesomeVideo.webm");
79  * r.Open(); // Open thetarget_ reader
80  *
81  * // Create a writer (which will create a WebM video)
82  * FFmpegWriter w("/home/jonathan/NewVideo.webm");
83  *
84  * // Set options
85  * w.SetAudioOptions(true, "libvorbis", 44100, 2, ChannelLayout::LAYOUT_STEREO, 128000); // Sample Rate: 44100, Channels: 2, Bitrate: 128000
86  * w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000); // FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
87  *
88  * // Open the writer
89  * w.Open();
90  *
91  * // Write all frames from the reader
92  * w.WriteFrame(&r, 1, r.info.video_length);
93  *
94  * // Close the reader & writer
95  * w.Close();
96  * r.Close();
97  * @endcode
98  *
99  * Here is a more advanced example, which sets some additional (and optional) encoding
100  * options.
101  *
102  * @code ADVANCED WRITER EXAMPLE
103  *
104  * // Create a reader for a video
105  * FFmpegReader r("MyAwesomeVideo.webm");
106  * r.Open(); // Open the reader
107  *
108  * // Create a writer (which will create a WebM video)
109  * FFmpegWriter w("/home/jonathan/NewVideo.webm");
110  *
111  * // Set options
112  * w.SetAudioOptions(true, "libvorbis", 44100, 2, ChannelLayout::LAYOUT_STEREO, 128000); // Sample Rate: 44100, Channels: 2, Bitrate: 128000
113  * w.SetVideoOptions(true, "libvpx", openshot::Fraction(24,1), 720, 480, openshot::Fraction(1,1), false, false, 300000); // FPS: 24, Size: 720x480, Pixel Ratio: 1/1, Bitrate: 300000
114  *
115  * // Prepare Streams (Optional method that must be called before any SetOption calls)
116  * w.PrepareStreams();
117  *
118  * // Set some specific encoding options (Optional methods)
119  * w.SetOption(VIDEO_STREAM, "qmin", "2" );
120  * w.SetOption(VIDEO_STREAM, "qmax", "30" );
121  * w.SetOption(VIDEO_STREAM, "crf", "10" );
122  * w.SetOption(VIDEO_STREAM, "rc_min_rate", "2000000" );
123  * w.SetOption(VIDEO_STREAM, "rc_max_rate", "4000000" );
124  * w.SetOption(VIDEO_STREAM, "max_b_frames", "10" );
125  *
126  * // Write the header of the video file
127  * w.WriteHeader();
128  *
129  * // Open the writer
130  * w.Open();
131  *
132  * // Write all frames from the reader
133  * w.WriteFrame(&r, 1, r.info.video_length);
134  *
135  * // Write the trailer of the video file
136  * w.WriteTrailer();
137  *
138  * // Close the reader & writer
139  * w.Close();
140  * r.Close();
141  * @endcode
142  */
143  class FFmpegWriter : public WriterBase {
144  private:
145  std::string path;
146  int cache_size;
147  bool is_writing;
148  bool is_open;
149  int64_t write_video_count;
150  int64_t write_audio_count;
151 
152  bool prepare_streams;
153  bool write_header;
154  bool write_trailer;
155 
156  AVOutputFormat *fmt;
157  AVFormatContext *oc;
158  AVStream *audio_st, *video_st;
159  AVCodecContext *video_codec;
160  AVCodecContext *audio_codec;
161  SwsContext *img_convert_ctx;
162  double audio_pts, video_pts;
163  int16_t *samples;
164  uint8_t *audio_outbuf;
165  uint8_t *audio_encoder_buffer;
166 
167  int num_of_rescalers;
168  int rescaler_position;
169  std::vector<SwsContext *> image_rescalers;
170 
171  int audio_outbuf_size;
172  int audio_input_frame_size;
173  int initial_audio_input_frame_size;
174  int audio_input_position;
175  int audio_encoder_buffer_size;
176  SWRCONTEXT *avr;
177  SWRCONTEXT *avr_planar;
178 
179  /* Resample options */
180  int original_sample_rate;
181  int original_channels;
182 
183  std::shared_ptr<openshot::Frame> last_frame;
184  std::deque<std::shared_ptr<openshot::Frame> > spooled_audio_frames;
185  std::deque<std::shared_ptr<openshot::Frame> > spooled_video_frames;
186 
187  std::deque<std::shared_ptr<openshot::Frame> > queued_audio_frames;
188  std::deque<std::shared_ptr<openshot::Frame> > queued_video_frames;
189 
190  std::deque<std::shared_ptr<openshot::Frame> > processed_frames;
191  std::deque<std::shared_ptr<openshot::Frame> > deallocate_frames;
192 
193  std::map<std::shared_ptr<openshot::Frame>, AVFrame *> av_frames;
194 
195  /// Add an AVFrame to the cache
196  void add_avframe(std::shared_ptr<openshot::Frame> frame, AVFrame *av_frame);
197 
198  /// Add an audio output stream
199  AVStream *add_audio_stream();
200 
201  /// Add a video output stream
202  AVStream *add_video_stream();
203 
204  /// Allocate an AVFrame object
205  AVFrame *allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer);
206 
207  /// Auto detect format (from path)
208  void auto_detect_format();
209 
210  /// Close the audio codec
211  void close_audio(AVFormatContext *oc, AVStream *st);
212 
213  /// Close the video codec
214  void close_video(AVFormatContext *oc, AVStream *st);
215 
216  /// Flush encoders
217  void flush_encoders();
218 
219  /// initialize streams
220  void initialize_streams();
221 
222  /// @brief Init a collection of software rescalers (thread safe)
223  /// @param source_width The source width of the image scalers (used to cache a bunch of scalers)
224  /// @param source_height The source height of the image scalers (used to cache a bunch of scalers)
225  void InitScalers(int source_width, int source_height);
226 
227  /// open audio codec
228  void open_audio(AVFormatContext *oc, AVStream *st);
229 
230  /// open video codec
231  void open_video(AVFormatContext *oc, AVStream *st);
232 
233  /// process video frame
234  void process_video_packet(std::shared_ptr<openshot::Frame> frame);
235 
236  /// write all queued frames' audio to the video file
237  void write_audio_packets(bool is_final);
238 
239  /// write video frame
240  bool write_video_packet(std::shared_ptr<openshot::Frame> frame, AVFrame *frame_final);
241 
242  /// write all queued frames
243  void write_queued_frames();
244 
245  public:
246 
247  /// @brief Constructor for FFmpegWriter. Throws one of the following exceptions.
248  /// @param path The file path of the video file you want to open and read
249  FFmpegWriter(std::string path);
250 
251  /// Close the writer
252  void Close();
253 
254  /// Get the cache size (number of frames to queue before writing)
255  int GetCacheSize() { return cache_size; };
256 
257  /// Determine if writer is open or closed
258  bool IsOpen() { return is_open; };
259 
260  /// Determine if codec name is valid
261  static bool IsValidCodec(std::string codec_name);
262 
263  /// Open writer
264  void Open();
265 
266  /// Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
267  void OutputStreamInfo();
268 
269  /// @brief Prepare & initialize streams and open codecs. This method is called automatically
270  /// by the Open() method if this method has not yet been called.
271  void PrepareStreams();
272 
273  /// Remove & deallocate all software scalers
274  void RemoveScalers();
275 
276  /// @brief Set audio resample options
277  /// @param sample_rate The number of samples per second of the audio
278  /// @param channels The number of audio channels
279  void ResampleAudio(int sample_rate, int channels);
280 
281  /// @brief Set audio export options
282  /// @param has_audio Does this file need an audio stream?
283  /// @param codec The codec used to encode the audio for this file
284  /// @param sample_rate The number of audio samples needed in this file
285  /// @param channels The number of audio channels needed in this file
286  /// @param channel_layout The 'layout' of audio channels (i.e. mono, stereo, surround, etc...)
287  /// @param bit_rate The audio bit rate used during encoding
288  void SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, openshot::ChannelLayout channel_layout, int bit_rate);
289 
290  /// @brief Set the cache size
291  /// @param new_size The number of frames to queue before writing to the file
292  void SetCacheSize(int new_size) { cache_size = new_size; };
293 
294  /// @brief Set video export options
295  /// @param has_video Does this file need a video stream
296  /// @param codec The codec used to encode the images in this video
297  /// @param fps The number of frames per second
298  /// @param width The width in pixels of this video
299  /// @param height The height in pixels of this video
300  /// @param pixel_ratio The shape of the pixels represented as a openshot::Fraction (1x1 is most common / square pixels)
301  /// @param interlaced Does this video need to be interlaced?
302  /// @param top_field_first Which frame should be used as the top field?
303  /// @param bit_rate The video bit rate used during encoding
304  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);
305 
306  /// @brief Set custom options (some codecs accept additional params). This must be called after the
307  /// PrepareStreams() method, otherwise the streams have not been initialized yet.
308  /// @param stream The stream (openshot::StreamType) this option should apply to
309  /// @param name The name of the option you want to set (i.e. qmin, qmax, etc...)
310  /// @param value The new value of this option
311  void SetOption(openshot::StreamType stream, std::string name, std::string value);
312 
313  /// @brief Write the file header (after the options are set). This method is called automatically
314  /// by the Open() method if this method has not yet been called.
315  void WriteHeader();
316 
317  /// @brief Add a frame to the stack waiting to be encoded.
318  /// @param frame The openshot::Frame object to write to this image
319  void WriteFrame(std::shared_ptr<openshot::Frame> frame);
320 
321  /// @brief Write a block of frames from a reader
322  /// @param reader A openshot::ReaderBase object which will provide frames to be written
323  /// @param start The starting frame number of the reader
324  /// @param length The number of frames to write
325  void WriteFrame(openshot::ReaderBase *reader, int64_t start, int64_t length);
326 
327  /// @brief Write the file trailer (after all frames are written). This is called automatically
328  /// by the Close() method if this method has not yet been called.
329  void WriteTrailer();
330 
331  };
332 
333 }
334 
335 #endif
A video stream (used to determine which type of stream)
Definition: FFmpegWriter.h:64
#define SWRCONTEXT
int GetCacheSize()
Get the cache size (number of frames to queue before writing)
Definition: FFmpegWriter.h:255
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:65
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:143
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:258
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Add a frame to the stack waiting to be encoded.
void Open()
Open writer.
Header file for all Exception classes.
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:45
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:292
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.
void PrepareStreams()
Prepare & initialize streams and open codecs. This method is called automatically by the Open() metho...
FFmpegWriter(std::string path)
Constructor for FFmpegWriter. Throws one of the following exceptions.
Header file for FFmpegUtilities.
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:63