Fix alpha blending with with offset; should help with #1155.
[dcpomatic.git] / test / ffmpeg_encoder_test.cc
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 #include "lib/ffmpeg_encoder.h"
22 #include "lib/film.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/audio_content.h"
25 #include "lib/text_subtitle_content.h"
26 #include "lib/ratio.h"
27 #include "lib/transcode_job.h"
28 #include "lib/dcp_content.h"
29 #include "lib/subtitle_content.h"
30 #include "test.h"
31 #include <boost/test/unit_test.hpp>
32
33 using boost::shared_ptr;
34
35 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_basic_test_mov)
36 {
37         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_basic_test_mov");
38         film->set_name ("ffmpeg_transcoder_basic_test");
39         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
40         film->set_container (Ratio::from_id ("185"));
41         film->set_audio_channels (6);
42
43         film->examine_and_add_content (c);
44         BOOST_REQUIRE (!wait_for_jobs ());
45
46         shared_ptr<Job> job (new TranscodeJob (film));
47         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_basic_test.mov", FFmpegEncoder::FORMAT_PRORES, false);
48         encoder.go ();
49 }
50
51 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_basic_test_mp4)
52 {
53         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_basic_test_mp4");
54         film->set_name ("ffmpeg_transcoder_basic_test");
55         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
56         film->set_container (Ratio::from_id ("185"));
57         film->set_audio_channels (6);
58
59         film->examine_and_add_content (c);
60         BOOST_REQUIRE (!wait_for_jobs ());
61
62         shared_ptr<Job> job (new TranscodeJob (film));
63         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_basic_test.mp4", FFmpegEncoder::FORMAT_H264, false);
64         encoder.go ();
65 }
66
67 /** Simplest possible export subtitle case: just the subtitles */
68 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_test_subs_h264_1)
69 {
70         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_test_subs_h264_1");
71         film->set_name ("ffmpeg_encoder_test_subs_h264_1");
72         film->set_container (Ratio::from_id ("185"));
73         film->set_audio_channels (6);
74
75         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip2.srt"));
76         film->examine_and_add_content (s);
77         BOOST_REQUIRE (!wait_for_jobs ());
78         s->subtitle->set_colour (dcp::Colour (255, 255, 0));
79         s->subtitle->set_shadow (true);
80         s->subtitle->set_effect_colour (dcp::Colour (0, 255, 255));
81         film->write_metadata();
82
83         shared_ptr<Job> job (new TranscodeJob (film));
84         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_test_subs_h264_1.mp4", FFmpegEncoder::FORMAT_H264, false);
85         encoder.go ();
86 }
87
88 /** Slightly more complicated example with longer subs and a video to overlay */
89 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_test_subs_h264_2)
90 {
91         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_test_subs_h264_2");
92         film->set_name ("ffmpeg_encoder_test_subs_h264_2");
93         film->set_container (Ratio::from_id ("185"));
94         film->set_audio_channels (6);
95
96         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
97         film->examine_and_add_content (c);
98         BOOST_REQUIRE (!wait_for_jobs ());
99
100         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip.srt"));
101         film->examine_and_add_content (s);
102         BOOST_REQUIRE (!wait_for_jobs ());
103         s->subtitle->set_colour (dcp::Colour (255, 255, 0));
104         s->subtitle->set_shadow (true);
105         s->subtitle->set_effect_colour (dcp::Colour (0, 255, 255));
106         film->write_metadata();
107
108         shared_ptr<Job> job (new TranscodeJob (film));
109         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_test_subs_h264_2.mp4", FFmpegEncoder::FORMAT_H264, false);
110         encoder.go ();
111 }
112
113 /** Simplest possible export subtitle case: just the subtitles */
114 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_test_subs_prores_1)
115 {
116         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_test_subs_prores_1");
117         film->set_name ("ffmpeg_encoder_test_subs_prores_1");
118         film->set_container (Ratio::from_id ("185"));
119         film->set_audio_channels (6);
120
121         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip2.srt"));
122         film->examine_and_add_content (s);
123         BOOST_REQUIRE (!wait_for_jobs ());
124         s->subtitle->set_colour (dcp::Colour (255, 255, 0));
125         s->subtitle->set_shadow (true);
126         s->subtitle->set_effect_colour (dcp::Colour (0, 255, 255));
127         film->write_metadata();
128
129         shared_ptr<Job> job (new TranscodeJob (film));
130         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_test_subs_prores_1.mov", FFmpegEncoder::FORMAT_PRORES, false);
131         encoder.go ();
132 }
133
134 /** Slightly more complicated example with longer subs and a video to overlay */
135 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_test_subs_prores_2)
136 {
137         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_test_subs_prores_2");
138         film->set_name ("ffmpeg_encoder_test_subs_prores_2");
139         film->set_container (Ratio::from_id ("185"));
140         film->set_audio_channels (6);
141
142         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
143         film->examine_and_add_content (c);
144         BOOST_REQUIRE (!wait_for_jobs ());
145
146         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip.srt"));
147         film->examine_and_add_content (s);
148         BOOST_REQUIRE (!wait_for_jobs ());
149         s->subtitle->set_colour (dcp::Colour (255, 255, 0));
150         s->subtitle->set_shadow (true);
151         s->subtitle->set_effect_colour (dcp::Colour (0, 255, 255));
152
153         shared_ptr<Job> job (new TranscodeJob (film));
154         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_test_subs_prores_2.mov", FFmpegEncoder::FORMAT_PRORES, false);
155         encoder.go ();
156 }
157
158 /** Test a bug with export of scope-in-flat DCP content */
159 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_bug_test_scope)
160 {
161         shared_ptr<Film> film = new_test_film2("ffmpeg_encoder_bug_test_scope");
162         film->examine_and_add_content(shared_ptr<DCPContent>(new DCPContent(film, "test/data/scope_dcp")));
163         BOOST_REQUIRE(!wait_for_jobs());
164
165         film->set_container(Ratio::from_id("185"));
166
167         shared_ptr<Job> job(new TranscodeJob(film));
168         FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_bug_test_scope.mp4", FFmpegEncoder::FORMAT_H264, false);
169         encoder.go();
170 }
171
172 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_basic_test_mixdown)
173 {
174         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_basic_test_mixdown");
175         film->set_name ("ffmpeg_transcoder_basic_test");
176         film->set_container (Ratio::from_id ("185"));
177         film->set_audio_channels (6);
178
179         shared_ptr<FFmpegContent> L (new FFmpegContent (film, "test/data/L.wav"));
180         film->examine_and_add_content (L);
181         shared_ptr<FFmpegContent> R (new FFmpegContent (film, "test/data/R.wav"));
182         film->examine_and_add_content (R);
183         shared_ptr<FFmpegContent> C (new FFmpegContent (film, "test/data/C.wav"));
184         film->examine_and_add_content (C);
185         shared_ptr<FFmpegContent> Ls (new FFmpegContent (film, "test/data/Ls.wav"));
186         film->examine_and_add_content (Ls);
187         shared_ptr<FFmpegContent> Rs (new FFmpegContent (film, "test/data/Rs.wav"));
188         film->examine_and_add_content (Rs);
189         shared_ptr<FFmpegContent> Lfe (new FFmpegContent (film, "test/data/Lfe.wav"));
190         film->examine_and_add_content (Lfe);
191         BOOST_REQUIRE (!wait_for_jobs ());
192
193         AudioMapping map (1, MAX_DCP_AUDIO_CHANNELS);
194
195         L->set_position (DCPTime::from_seconds (0));
196         map.make_zero ();
197         map.set (0, 0, 1);
198         L->audio->set_mapping (map);
199         R->set_position (DCPTime::from_seconds (1));
200         map.make_zero ();
201         map.set (0, 1, 1);
202         R->audio->set_mapping (map);
203         C->set_position (DCPTime::from_seconds (2));
204         map.make_zero ();
205         map.set (0, 2, 1);
206         C->audio->set_mapping (map);
207         Lfe->set_position (DCPTime::from_seconds (3));
208         map.make_zero ();
209         map.set (0, 3, 1);
210         Lfe->audio->set_mapping (map);
211         Ls->set_position (DCPTime::from_seconds (4));
212         map.make_zero ();
213         map.set (0, 4, 1);
214         Ls->audio->set_mapping (map);
215         Rs->set_position (DCPTime::from_seconds (5));
216         map.make_zero ();
217         map.set (0, 5, 1);
218         Rs->audio->set_mapping (map);
219
220         shared_ptr<Job> job (new TranscodeJob (film));
221         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_basic_test_mixdown.mp4", FFmpegEncoder::FORMAT_H264, true);
222         encoder.go ();
223
224         /* Skip the first video packet when checking as it contains x264 options which can vary between machines
225            (e.g. number of threads used for encoding).
226         */
227         check_ffmpeg ("build/test/ffmpeg_encoder_basic_test_mixdown.mp4", "test/data/ffmpeg_encoder_basic_test_mixdown.mp4");
228 }