2d7d419979358dc31c00ccfa1e3ccf540566427e
[dcpomatic.git] / src / lib / ffmpeg_encoder.cc
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 #include "ffmpeg_encoder.h"
22 #include "film.h"
23 #include "job.h"
24 #include "player.h"
25 #include "player_video.h"
26 #include "log.h"
27 #include "image.h"
28 #include "cross.h"
29 #include "butler.h"
30 #include "compose.hpp"
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::runtime_error;
37 using std::cout;
38 using std::pair;
39 using boost::shared_ptr;
40 using boost::bind;
41 using boost::weak_ptr;
42
43
44 FFmpegEncoder::FFmpegEncoder (shared_ptr<const Film> film, weak_ptr<Job> job, boost::filesystem::path output, ExportFormat format, bool mixdown_to_stereo, int x264_crf)
45         : Encoder (film, job)
46         , _file_encoder (
47                 _film->frame_size(),
48                 _film->video_frame_rate(),
49                 _film->audio_frame_rate(),
50                 mixdown_to_stereo ? 2 : film->audio_channels(),
51                 _film->log(),
52                 format,
53                 x264_crf,
54                 output
55                 )
56         , _history (1000)
57 {
58         _player->set_always_burn_open_subtitles ();
59         _player->set_play_referenced ();
60
61         int const ch = film->audio_channels ();
62
63         AudioMapping map;
64         if (mixdown_to_stereo) {
65                 _output_audio_channels = 2;
66                 map = AudioMapping (ch, 2);
67                 float const overall_gain = 2 / (4 + sqrt(2));
68                 float const minus_3dB = 1 / sqrt(2);
69                 map.set (dcp::LEFT,   0, overall_gain);
70                 map.set (dcp::RIGHT,  1, overall_gain);
71                 map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
72                 map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
73                 map.set (dcp::LS,     0, overall_gain);
74                 map.set (dcp::RS,     1, overall_gain);
75         } else {
76                 _output_audio_channels = ch;
77                 map = AudioMapping (ch, ch);
78                 for (int i = 0; i < ch; ++i) {
79                         map.set (i, i, 1);
80                 }
81         }
82
83         _butler.reset (new Butler (_player, film->log(), map, _output_audio_channels));
84 }
85
86 void
87 FFmpegEncoder::go ()
88 {
89         {
90                 shared_ptr<Job> job = _job.lock ();
91                 DCPOMATIC_ASSERT (job);
92                 job->sub (_("Encoding"));
93         }
94
95         DCPTime const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
96         int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
97         float* interleaved = new float[_output_audio_channels * audio_frames];
98         shared_ptr<AudioBuffers> deinterleaved (new AudioBuffers (_output_audio_channels, audio_frames));
99         for (DCPTime i; i < _film->length(); i += video_frame) {
100                 pair<shared_ptr<PlayerVideo>, DCPTime> v = _butler->get_video ();
101                 _file_encoder.video (v.first, v.second);
102
103                 _history.event ();
104
105                 {
106                         boost::mutex::scoped_lock lm (_mutex);
107                         _last_time = i;
108                 }
109
110                 shared_ptr<Job> job = _job.lock ();
111                 if (job) {
112                         job->set_progress (float(i.get()) / _film->length().get());
113                 }
114
115                 _butler->get_audio (interleaved, audio_frames);
116                 /* XXX: inefficient; butler interleaves and we deinterleave again */
117                 float* p = interleaved;
118                 for (int j = 0; j < audio_frames; ++j) {
119                         for (int k = 0; k < _output_audio_channels; ++k) {
120                                 deinterleaved->data(k)[j] = *p++;
121                         }
122                 }
123                 _file_encoder.audio (deinterleaved);
124         }
125         delete[] interleaved;
126
127         _file_encoder.flush ();
128 }
129
130 float
131 FFmpegEncoder::current_rate () const
132 {
133         return _history.rate ();
134 }
135
136 Frame
137 FFmpegEncoder::frames_done () const
138 {
139         boost::mutex::scoped_lock lm (_mutex);
140         return _last_time.frames_round (_film->video_frame_rate ());
141 }