Rename Encoder -> FilmEncoder, and subclasses.
[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_buffers.h"
27 #include "audio_mapping.h"
28 #include "dcpomatic_time.h"
29 #include "event_history.h"
30 #include "image_store.h"
31 #include "log.h"
32 #include "player_text.h"
33 #include "player_video.h"
34 #include <dcp/key.h>
35 #include <dcp/warnings.h>
36 LIBDCP_DISABLE_WARNINGS
37 extern "C" {
38 #include <libavcodec/avcodec.h>
39 #include <libavformat/avformat.h>
40 }
41 LIBDCP_ENABLE_WARNINGS
42
43
44 class ExportAudioStream;
45
46
47 enum class ExportFormat
48 {
49         PRORES_4444,
50         PRORES_HQ,
51         H264_AAC,
52         SUBTITLES_DCP
53 };
54
55
56 class FFmpegFileEncoder
57 {
58 public:
59         FFmpegFileEncoder (
60                 dcp::Size video_frame_size,
61                 int video_frame_rate,
62                 int audio_frame_rate,
63                 int channels,
64                 ExportFormat,
65                 bool audio_stream_per_channel,
66                 int x264_crf,
67                 boost::filesystem::path output
68                 );
69
70         ~FFmpegFileEncoder ();
71
72         void video (std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime);
73         void audio (std::shared_ptr<AudioBuffers>);
74         void subtitle (PlayerText, dcpomatic::DCPTimePeriod);
75
76         void flush ();
77
78         static AVPixelFormat pixel_format (ExportFormat format);
79
80 private:
81         void setup_video ();
82         void setup_audio ();
83
84         void audio_frame (int size);
85
86         AVCodec const * _video_codec = nullptr;
87         AVCodecContext* _video_codec_context = nullptr;
88         std::vector<std::shared_ptr<ExportAudioStream>> _audio_streams;
89         bool _audio_stream_per_channel;
90         AVFormatContext* _format_context = nullptr;
91         AVStream* _video_stream = nullptr;
92         AVPixelFormat _pixel_format;
93         AVSampleFormat _sample_format;
94         AVDictionary* _video_options = nullptr;
95         std::string _video_codec_name;
96         std::string _audio_codec_name;
97         int _audio_channels;
98
99         boost::filesystem::path _output;
100         dcp::Size _video_frame_size;
101         int _video_frame_rate;
102         int _audio_frame_rate;
103
104         int64_t _audio_frames = 0;
105
106         std::shared_ptr<AudioBuffers> _pending_audio;
107
108         ImageStore _pending_images;
109
110         static int _video_stream_index;
111         static int _audio_stream_index_base;
112 };
113
114 #endif