Rearrange some includes of dcpomatic_time.h
[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 "log.h"
31 #include <dcp/key.h>
32 #include <dcp/warnings.h>
33 LIBDCP_DISABLE_WARNINGS
34 extern "C" {
35 #include <libavcodec/avcodec.h>
36 #include <libavformat/avformat.h>
37 }
38 LIBDCP_ENABLE_WARNINGS
39
40
41 class ExportAudioStream;
42
43
44 class FFmpegFileEncoder
45 {
46 public:
47         FFmpegFileEncoder (
48                 dcp::Size video_frame_size,
49                 int video_frame_rate,
50                 int audio_frame_rate,
51                 int channels,
52                 ExportFormat,
53                 bool audio_stream_per_channel,
54                 int x264_crf,
55                 boost::filesystem::path output
56                 );
57
58         ~FFmpegFileEncoder ();
59
60         void video (std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime);
61         void audio (std::shared_ptr<AudioBuffers>);
62         void subtitle (PlayerText, dcpomatic::DCPTimePeriod);
63
64         void flush ();
65
66         static AVPixelFormat pixel_format (ExportFormat format);
67
68 private:
69         void setup_video ();
70         void setup_audio ();
71
72         void audio_frame (int size);
73
74         static void buffer_free(void* opaque, uint8_t* data);
75         void buffer_free2(uint8_t* data);
76
77         AVCodec const * _video_codec = nullptr;
78         AVCodecContext* _video_codec_context = nullptr;
79         std::vector<std::shared_ptr<ExportAudioStream>> _audio_streams;
80         bool _audio_stream_per_channel;
81         AVFormatContext* _format_context = nullptr;
82         AVStream* _video_stream = nullptr;
83         AVPixelFormat _pixel_format;
84         AVSampleFormat _sample_format;
85         AVDictionary* _video_options = nullptr;
86         std::string _video_codec_name;
87         std::string _audio_codec_name;
88         int _audio_channels;
89
90         boost::filesystem::path _output;
91         dcp::Size _video_frame_size;
92         int _video_frame_rate;
93         int _audio_frame_rate;
94
95         int64_t _audio_frames = 0;
96
97         std::shared_ptr<AudioBuffers> _pending_audio;
98
99         /** Store of shared_ptr<Image> to keep them alive whilst raw pointers into
100             their data have been passed to FFmpeg.
101         */
102         std::map<uint8_t*, std::shared_ptr<const Image>> _pending_images;
103         boost::mutex _pending_images_mutex;
104
105         static int _video_stream_index;
106         static int _audio_stream_index_base;
107 };
108
109 #endif