filmstro_ffmpeg
 All Classes Files Functions Friends Macros
filmstro_ffmpeg_FFmpegVideoWriter.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3  Copyright (c) 2017, Filmstro Ltd. - Daniel Walz
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification,
7  are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13  3. Neither the name of the copyright holder nor the names of its contributors
14  may be used to endorse or promote products derived from this software without
15  specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26  OF THE POSSIBILITY OF SUCH DAMAGE.
27  ==============================================================================
28  \class FFmpegVideoWriter
29  \file filmstro_ffmpeg_FFmpegVideoWriter.h
30  \brief A class to write a video stream using FFmpeg
31 
32  \author Daniel Walz @ filmstro.com
33  \date September 19th 2016
34 
35  \description Use this writer to write audio frames and video frames in a
36  video file using ffmpeg
37  ==============================================================================
38  */
39 
40 
41 #ifndef FILMSTRO_FFMPEG_FFMPEGVIDEOWRITER_H_INCLUDED
42 #define FILMSTRO_FFMPEG_FFMPEGVIDEOWRITER_H_INCLUDED
43 
44 class FFmpegVideoSource;
45 
46 
48 {
49 public:
50 
51  FFmpegVideoWriter (const juce::String& format = juce::String());
53 
55  void setVideoCodec (AVCodecID codec = AV_CODEC_ID_PROBE);
57  void setAudioCodec (AVCodecID codec = AV_CODEC_ID_PROBE);
59  void setSubtitleCodec (AVCodecID codec = AV_CODEC_ID_PROBE);
60 
62  void setSampleRate (const int newSampleRate);
63 
65  void setVideoSize (const int width, const int height);
66 
68  void setPixelFormat (const AVPixelFormat format);
69 
71  void setPixelAspect (const int num, const int den);
72 
75  void setTimeBase (AVMediaType type, AVRational timebase);
76 
78  void copySettingsFromContext (const AVCodecContext* context);
79 
82  bool openMovieFile (const juce::File& outputFile, const juce::String& format=juce::String());
83 
85  void closeMovieFile ();
86 
88  void writeNextAudioBlock (juce::AudioSourceChannelInfo& info);
89 
91  void writeNextVideoFrame (const juce::Image& image, const juce::int64 timestamp);
92 
93  void videoSizeChanged (const int width, const int height, const AVPixelFormat) override;
94 
97  void displayNewFrame (const AVFrame*) override;
98 
100  static juce::StringArray getOutputFormatNames ();
101 
102  // ==============================================================================
103 private:
104 
105  void closeContexts ();
106 
107  void finishWriting ();
108 
110  bool writeAudioFrame (const bool flush=false);
111 
112  int encodeWriteFrame (AVFrame *frame, AVMediaType type);
113 
114  // ==============================================================================
115 
117  juce::int64 audioWritePosition;
118 
119  AVFormatContext* formatContext;
120 
121  AVCodecContext* videoContext;
122  AVCodecContext* audioContext;
123  AVCodecContext* subtitleContext;
124 
125  AVCodecID videoCodec;
126  AVCodecID audioCodec;
127  AVCodecID subtitleCodec;
128 
129  int videoStreamIdx;
130  int audioStreamIdx;
131  int subtitleStreamIdx;
132 
133  AVRational videoTimeBase;
134  AVRational audioTimeBase;
135  AVRational subtitleTimeBase;
136 
137  int sampleRate;
138  int64_t channelLayout;
139 
140  int videoWidth;
141  int videoHeight;
142  AVPixelFormat pixelFormat;
143  AVRational pixelAspect;
144 
145  // buffer audio to match the video's audio frame size
146  AudioBufferFIFO<float> audioFifo;
147 
148  juce::ScopedPointer<FFmpegVideoScaler> outVideoScaler;
149  juce::ScopedPointer<FFmpegVideoScaler> inVideoScaler;
150 
151  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FFmpegVideoWriter)
152 };
153 
154 #endif /* FILMSTRO_FFMPEG_FFMPEGVIDEOWRITER_H_INCLUDED */
155 
void setVideoSize(const int width, const int height)
Set the video size before opening a file.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:113
void setSampleRate(const int newSampleRate)
Set the audio sample rate before opening a file.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:107
void copySettingsFromContext(const AVCodecContext *context)
copies settings from a context (e.g.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:146
bool openMovieFile(const juce::File &outputFile, const juce::String &format=juce::String())
Opens a file for writing audio, video and subtitles.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:176
void writeNextVideoFrame(const juce::Image &image, const juce::int64 timestamp)
Write the next video frame from juce image.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:406
void videoSizeChanged(const int width, const int height, const AVPixelFormat) override
This is called whenever the size changed, so a framebuffer can be resized.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:543
Definition: filmstro_ffmpeg_FFmpegVideoWriter.h:47
void setPixelAspect(const int num, const int den)
Set the pixel aspect ratio as fraction before opening a file.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:124
FFmpegVideoWriter(const juce::String &format=juce::String())
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:43
void setTimeBase(AVMediaType type, AVRational timebase)
Set the timebase for the stream, provide either AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:129
static juce::StringArray getOutputFormatNames()
Returns the names of available output formats.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:81
Definition: filmstro_ffmpeg_FFmpegVideoListener.h:44
~FFmpegVideoWriter()
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:77
void setVideoCodec(AVCodecID codec=AV_CODEC_ID_PROBE)
Set the requested video codec before opening a file.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:92
void setAudioCodec(AVCodecID codec=AV_CODEC_ID_PROBE)
Set the requested audio codec before opening a file.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:97
void displayNewFrame(const AVFrame *) override
This callback receives frames from e.g.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:549
void setPixelFormat(const AVPixelFormat format)
Set the pixel format before opening a file.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:119
void closeMovieFile()
Closes the movie file.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:347
void writeNextAudioBlock(juce::AudioSourceChannelInfo &info)
Append a chunk of audio data.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:400
void setSubtitleCodec(AVCodecID codec=AV_CODEC_ID_PROBE)
Set the requested subtitle codec before opening a file.
Definition: filmstro_ffmpeg_FFmpegVideoWriter.cpp:102