Tidy and fix logging.
[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 #ifndef DCPOMATIC_FFMPEG_FILE_ENCODER_H
22 #define DCPOMATIC_FFMPEG_FILE_ENCODER_H
23
24 #include "encoder.h"
25 #include "event_history.h"
26 #include "audio_mapping.h"
27 #include "log.h"
28 extern "C" {
29 #include <libavcodec/avcodec.h>
30 #include <libavformat/avformat.h>
31 }
32
33 class FFmpegFileEncoder
34 {
35 public:
36         FFmpegFileEncoder (
37                 dcp::Size video_frame_size,
38                 int video_frame_rate,
39                 int audio_frame_rate,
40                 int channels,
41                 ExportFormat,
42                 int x264_crf,
43                 boost::filesystem::path output
44                 );
45
46         void video (boost::shared_ptr<PlayerVideo>, DCPTime);
47         void audio (boost::shared_ptr<AudioBuffers>);
48         void subtitle (PlayerText, DCPTimePeriod);
49
50         void flush ();
51
52         static AVPixelFormat pixel_format (ExportFormat format);
53
54 private:
55         void setup_video ();
56         void setup_audio ();
57
58         void audio_frame (int size);
59
60         static void buffer_free(void* opaque, uint8_t* data);
61         void buffer_free2(uint8_t* data);
62
63         AVCodec* _video_codec;
64         AVCodecContext* _video_codec_context;
65         AVCodec* _audio_codec;
66         AVCodecContext* _audio_codec_context;
67         AVFormatContext* _format_context;
68         AVStream* _video_stream;
69         AVStream* _audio_stream;
70         AVPixelFormat _pixel_format;
71         AVSampleFormat _sample_format;
72         AVDictionary* _video_options;
73         std::string _video_codec_name;
74         std::string _audio_codec_name;
75         int _audio_channels;
76
77         boost::filesystem::path _output;
78         dcp::Size _video_frame_size;
79         int _video_frame_rate;
80         int _audio_frame_rate;
81
82         boost::shared_ptr<AudioBuffers> _pending_audio;
83
84         /** Store of shared_ptr<Image> to keep them alive whilst raw pointers into
85             their data have been passed to FFmpeg.
86         */
87         std::map<uint8_t*, boost::shared_ptr<const Image> > _pending_images;
88
89         static int _video_stream_index;
90         static int _audio_stream_index;
91 };
92
93 #endif