OpenShot Library | libopenshot  0.1.9
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2013 OpenShot Studios, LLC, Fabrice Bellard
9  * (http://www.openshotstudios.com). This file is part of
10  * OpenShot Library (http://www.openshot.org), an open-source project
11  * dedicated to delivering high quality video editing and animation solutions
12  * to the world.
13  *
14  * This file is originally based on the Libavformat API example, and then modified
15  * by the libopenshot project.
16  *
17  * OpenShot Library is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  * * OpenShot Library (libopenshot) is free software: you can redistribute it
22  * and/or modify it under the terms of the GNU Lesser General Public License
23  * as published by the Free Software Foundation, either version 3 of the
24  * License, or (at your option) any later version.
25  *
26  * OpenShot Library (libopenshot) is distributed in the hope that it will be
27  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29  * GNU Lesser General Public License for more details.
30  *
31  * You should have received a copy of the GNU Lesser General Public License
32  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
33  */
34 
35 
36 #ifndef OPENSHOT_FFMPEG_WRITER_H
37 #define OPENSHOT_FFMPEG_WRITER_H
38 
39 #include "ReaderBase.h"
40 #include "WriterBase.h"
41 
42 // Include FFmpeg headers and macros
43 #include "FFmpegUtilities.h"
44 
45 #include <cmath>
46 #include <ctime>
47 #include <iostream>
48 #include <stdio.h>
49 #include <unistd.h>
50 #include "CacheMemory.h"
51 #include "Exceptions.h"
52 #include "OpenMPUtilities.h"
53 #include "ZmqLogger.h"
54 
55 
56 using namespace std;
57 
58 namespace openshot
59 {
60 
61  /// This enumeration designates the type of stream when encoding (video or audio)
63  {
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, 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, 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  {
145  private:
146  string path;
147  int cache_size;
148  bool is_writing;
149  bool is_open;
150  int64_t write_video_count;
151  int64_t write_audio_count;
152 
153  bool prepare_streams;
154  bool write_header;
155  bool write_trailer;
156 
157  AVOutputFormat *fmt;
158  AVFormatContext *oc;
159  AVStream *audio_st, *video_st;
160  AVCodecContext *video_codec;
161  AVCodecContext *audio_codec;
162  SwsContext *img_convert_ctx;
163  double audio_pts, video_pts;
164  int16_t *samples;
165  uint8_t *audio_outbuf;
166  uint8_t *audio_encoder_buffer;
167 
168  int num_of_rescalers;
169  int rescaler_position;
170  vector<SwsContext*> image_rescalers;
171 
172  int audio_outbuf_size;
173  int audio_input_frame_size;
174  int initial_audio_input_frame_size;
175  int audio_input_position;
176  int audio_encoder_buffer_size;
177  AVAudioResampleContext *avr;
178  AVAudioResampleContext *avr_planar;
179 
180  /* Resample options */
181  int original_sample_rate;
182  int original_channels;
183 
184  std::shared_ptr<Frame> last_frame;
185  deque<std::shared_ptr<Frame> > spooled_audio_frames;
186  deque<std::shared_ptr<Frame> > spooled_video_frames;
187 
188  deque<std::shared_ptr<Frame> > queued_audio_frames;
189  deque<std::shared_ptr<Frame> > queued_video_frames;
190 
191  deque<std::shared_ptr<Frame> > processed_frames;
192  deque<std::shared_ptr<Frame> > deallocate_frames;
193 
194  map<std::shared_ptr<Frame>, AVFrame*> av_frames;
195 
196  /// Add an AVFrame to the cache
197  void add_avframe(std::shared_ptr<Frame> frame, AVFrame* av_frame);
198 
199  /// Add an audio output stream
200  AVStream* add_audio_stream();
201 
202  /// Add a video output stream
203  AVStream* add_video_stream();
204 
205  /// Allocate an AVFrame object
206  AVFrame* allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer);
207 
208  /// Auto detect format (from path)
209  void auto_detect_format();
210 
211  /// Close the audio codec
212  void close_audio(AVFormatContext *oc, AVStream *st);
213 
214  /// Close the video codec
215  void close_video(AVFormatContext *oc, AVStream *st);
216 
217  /// Flush encoders
218  void flush_encoders();
219 
220  /// initialize streams
221  void initialize_streams();
222 
223  /// @brief Init a collection of software rescalers (thread safe)
224  /// @param source_width The source width of the image scalers (used to cache a bunch of scalers)
225  /// @param source_height The source height of the image scalers (used to cache a bunch of scalers)
226  void InitScalers(int source_width, int source_height);
227 
228  /// open audio codec
229  void open_audio(AVFormatContext *oc, AVStream *st);
230 
231  /// open video codec
232  void open_video(AVFormatContext *oc, AVStream *st);
233 
234  /// process video frame
235  void process_video_packet(std::shared_ptr<Frame> frame);
236 
237  /// write all queued frames' audio to the video file
238  void write_audio_packets(bool final);
239 
240  /// write video frame
241  bool write_video_packet(std::shared_ptr<Frame> frame, AVFrame* frame_final);
242 
243  /// write all queued frames
244  void write_queued_frames();
245 
246  public:
247 
248  /// @brief Constructor for FFmpegWriter. Throws one of the following exceptions.
249  /// @param path The file path of the video file you want to open and read
250  FFmpegWriter(string path);
251 
252  /// Close the writer
253  void Close();
254 
255  /// Get the cache size (number of frames to queue before writing)
256  int GetCacheSize() { return cache_size; };
257 
258  /// Determine if writer is open or closed
259  bool IsOpen() { return is_open; };
260 
261  /// Open writer
262  void Open();
263 
264  /// Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
265  void OutputStreamInfo();
266 
267  /// @brief Prepare & initialize streams and open codecs. This method is called automatically
268  /// by the Open() method if this method has not yet been called.
269  void PrepareStreams();
270 
271  /// Remove & deallocate all software scalers
272  void RemoveScalers();
273 
274  /// @brief Set audio resample options
275  /// @param sample_rate The number of samples per second of the audio
276  /// @param channels The number of audio channels
277  void ResampleAudio(int sample_rate, int channels);
278 
279  /// @brief Set audio export options
280  /// @param has_audio Does this file need an audio stream?
281  /// @param codec The codec used to encode the audio for this file
282  /// @param sample_rate The number of audio samples needed in this file
283  /// @param channels The number of audio channels needed in this file
284  /// @param channel_layout The 'layout' of audio channels (i.e. mono, stereo, surround, etc...)
285  /// @param bit_rate The audio bit rate used during encoding
286  void SetAudioOptions(bool has_audio, string codec, int sample_rate, int channels, ChannelLayout channel_layout, int bit_rate);
287 
288  /// @brief Set the cache size
289  /// @param new_size The number of frames to queue before writing to the file
290  void SetCacheSize(int new_size) { cache_size = new_size; };
291 
292  /// @brief Set video export options
293  /// @param has_video Does this file need a video stream
294  /// @param codec The codec used to encode the images in this video
295  /// @param fps The number of frames per second
296  /// @param width The width in pixels of this video
297  /// @param height The height in pixels of this video
298  /// @param pixel_ratio The shape of the pixels represented as a openshot::Fraction (1x1 is most common / square pixels)
299  /// @param interlaced Does this video need to be interlaced?
300  /// @param top_field_first Which frame should be used as the top field?
301  /// @param bit_rate The video bit rate used during encoding
302  void SetVideoOptions(bool has_video, string codec, Fraction fps, int width, int height,Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate);
303 
304  /// @brief Set custom options (some codecs accept additional params). This must be called after the
305  /// PrepareStreams() method, otherwise the streams have not been initialized yet.
306  /// @param stream The stream (openshot::StreamType) this option should apply to
307  /// @param name The name of the option you want to set (i.e. qmin, qmax, etc...)
308  /// @param value The new value of this option
309  void SetOption(StreamType stream, string name, string value);
310 
311  /// @brief Write the file header (after the options are set). This method is called automatically
312  /// by the Open() method if this method has not yet been called.
313  void WriteHeader();
314 
315  /// @brief Add a frame to the stack waiting to be encoded.
316  /// @param frame The openshot::Frame object to write to this image
317  void WriteFrame(std::shared_ptr<Frame> frame);
318 
319  /// @brief Write a block of frames from a reader
320  /// @param reader A openshot::ReaderBase object which will provide frames to be written
321  /// @param start The starting frame number of the reader
322  /// @param length The number of frames to write
323  void WriteFrame(ReaderBase* reader, int64_t start, int64_t length);
324 
325  /// @brief Write the file trailer (after all frames are written). This is called automatically
326  /// by the Close() method if this method has not yet been called.
327  void WriteTrailer();
328 
329  };
330 
331 }
332 
333 #endif
A video stream (used to determine which type of stream)
Definition: FFmpegWriter.h:64
int GetCacheSize()
Get the cache size (number of frames to queue before writing)
Definition: FFmpegWriter.h:256
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
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:95
Header file for CacheMemory class.
bool IsOpen()
Determine if writer is open or closed.
Definition: FFmpegWriter.h:259
Header file for all Exception classes.
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:85
This class represents a fraction.
Definition: Fraction.h:42
Header file for ZeroMQ-based Logger class.
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:290
#define PixelFormat
This namespace is the default namespace for all code in the openshot library.
Header file for FFmpegUtilities.
StreamType
This enumeration designates the type of stream when encoding (video or audio)
Definition: FFmpegWriter.h:62