35fd850640a795ec0b22881a8ab107172558eacf
[dcpomatic.git] / src / lib / ffmpeg_encoder.h
1 /*
2     Copyright (C) 2017 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_ENCODER_H
22 #define DCPOMATIC_FFMPEG_ENCODER_H
23
24 #include "encoder.h"
25 #include "event_history.h"
26 #include "audio_mapping.h"
27 extern "C" {
28 #include <libavcodec/avcodec.h>
29 #include <libavformat/avformat.h>
30 }
31 #include <boost/thread/condition.hpp>
32
33 class Butler;
34
35 class FFmpegEncoder : public Encoder
36 {
37 public:
38         enum Format
39         {
40                 FORMAT_PRORES,
41                 FORMAT_H264
42         };
43
44         FFmpegEncoder (boost::shared_ptr<const Film> film, boost::weak_ptr<Job> job, boost::filesystem::path output, Format format, bool mixdown_to_stereo);
45
46         void go ();
47
48         float current_rate () const;
49         Frame frames_done () const;
50         bool finishing () const {
51                 return false;
52         }
53
54 private:
55         void video (boost::shared_ptr<PlayerVideo>, DCPTime);
56         void audio (boost::shared_ptr<AudioBuffers>);
57         void subtitle (PlayerCaption, DCPTimePeriod);
58
59         void setup_video ();
60         void setup_audio ();
61
62         void audio_frame (int size);
63
64         static void buffer_free(void* opaque, uint8_t* data);
65         void buffer_free2(uint8_t* data);
66
67         AVCodec* _video_codec;
68         AVCodecContext* _video_codec_context;
69         AVCodec* _audio_codec;
70         AVCodecContext* _audio_codec_context;
71         AVFormatContext* _format_context;
72         AVStream* _video_stream;
73         AVStream* _audio_stream;
74         AVPixelFormat _pixel_format;
75         AVSampleFormat _sample_format;
76         AVDictionary* _video_options;
77         std::string _video_codec_name;
78         std::string _audio_codec_name;
79         int _output_audio_channels;
80
81         mutable boost::mutex _mutex;
82         DCPTime _last_time;
83
84         EventHistory _history;
85
86         boost::filesystem::path _output;
87
88         boost::shared_ptr<AudioBuffers> _pending_audio;
89
90         mutable boost::mutex _queue_mutex;
91         boost::condition _queue_full;
92         std::list<std::pair<boost::shared_ptr<PlayerVideo>, DCPTime> > _queue;
93
94         boost::shared_ptr<Butler> _butler;
95
96         /** Store of shared_ptr<Image> to keep them alive whilst raw pointers into
97             their data have been passed to FFmpeg.
98         */
99         std::map<uint8_t*, boost::shared_ptr<const Image> > _pending_images;
100
101         static int _video_stream_index;
102         static int _audio_stream_index;
103 };
104
105 #endif