Supporters update.
[dcpomatic.git] / src / lib / ffmpeg_file_encoder.h
1 /*
2     Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #ifndef DCPOMATIC_FFMPEG_FILE_ENCODER_H
23 #define DCPOMATIC_FFMPEG_FILE_ENCODER_H
24
25
26 #include "audio_mapping.h"
27 #include "dcpomatic_time.h"
28 #include "encoder.h"
29 #include "event_history.h"
30 #include "image_store.h"
31 #include "log.h"
32 #include <dcp/key.h>
33 #include <dcp/warnings.h>
34 LIBDCP_DISABLE_WARNINGS
35 extern "C" {
36 #include <libavcodec/avcodec.h>
37 #include <libavformat/avformat.h>
38 }
39 LIBDCP_ENABLE_WARNINGS
40
41
42 class ExportAudioStream;
43
44
45 enum class ExportFormat
46 {
47         PRORES_4444,
48         PRORES_HQ,
49         H264_AAC,
50         SUBTITLES_DCP
51 };
52
53
54 class FFmpegFileEncoder
55 {
56 public:
57         FFmpegFileEncoder (
58                 dcp::Size video_frame_size,
59                 int video_frame_rate,
60                 int audio_frame_rate,
61                 int channels,
62                 ExportFormat,
63                 bool audio_stream_per_channel,
64                 int x264_crf,
65                 boost::filesystem::path output
66                 );
67
68         ~FFmpegFileEncoder ();
69
70         void video (std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime);
71         void audio (std::shared_ptr<AudioBuffers>);
72         void subtitle (PlayerText, dcpomatic::DCPTimePeriod);
73
74         void flush ();
75
76         static AVPixelFormat pixel_format (ExportFormat format);
77
78 private:
79         void setup_video ();
80         void setup_audio ();
81
82         void audio_frame (int size);
83
84         AVCodec const * _video_codec = nullptr;
85         AVCodecContext* _video_codec_context = nullptr;
86         std::vector<std::shared_ptr<ExportAudioStream>> _audio_streams;
87         bool _audio_stream_per_channel;
88         AVFormatContext* _format_context = nullptr;
89         AVStream* _video_stream = nullptr;
90         AVPixelFormat _pixel_format;
91         AVSampleFormat _sample_format;
92         AVDictionary* _video_options = nullptr;
93         std::string _video_codec_name;
94         std::string _audio_codec_name;
95         int _audio_channels;
96
97         boost::filesystem::path _output;
98         dcp::Size _video_frame_size;
99         int _video_frame_rate;
100         int _audio_frame_rate;
101
102         int64_t _audio_frames = 0;
103
104         std::shared_ptr<AudioBuffers> _pending_audio;
105
106         ImageStore _pending_images;
107
108         static int _video_stream_index;
109         static int _audio_stream_index_base;
110 };
111
112 #endif