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