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