Tidy up and add a couple more tests.
[dcpomatic.git] / test / ffmpeg_encoder_test.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 "lib/ffmpeg_encoder.h"
22 #include "lib/film.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/video_content.h"
25 #include "lib/audio_content.h"
26 #include "lib/text_subtitle_content.h"
27 #include "lib/ratio.h"
28 #include "lib/transcode_job.h"
29 #include "lib/dcp_content.h"
30 #include "lib/subtitle_content.h"
31 #include "lib/compose.hpp"
32 #include "test.h"
33 #include <boost/test/unit_test.hpp>
34
35 using std::string;
36 using boost::shared_ptr;
37
38 static void
39 ffmpeg_content_test (int number, boost::filesystem::path content, FFmpegEncoder::Format format)
40 {
41         string name = "ffmpeg_encoder_";
42         string extension;
43         switch (format) {
44         case FFmpegEncoder::FORMAT_H264:
45                 name += "h264";
46                 extension = "mp4";
47                 break;
48         case FFmpegEncoder::FORMAT_PRORES:
49                 name += "prores";
50                 extension = "mov";
51                 break;
52         }
53
54         name = String::compose("%1_test%2", name, number);
55
56         shared_ptr<Film> film = new_test_film (name);
57         film->set_name (name);
58         shared_ptr<FFmpegContent> c (new FFmpegContent (film, content));
59         film->set_container (Ratio::from_id ("185"));
60         film->set_audio_channels (6);
61
62         film->examine_and_add_content (c);
63         BOOST_REQUIRE (!wait_for_jobs ());
64
65         film->set_video_frame_rate (24);
66
67         shared_ptr<Job> job (new TranscodeJob (film));
68         FFmpegEncoder encoder (film, job, String::compose("build/test/%1.%2", name, extension), format, false);
69         encoder.go ();
70 }
71
72 /** Red / green / blue MP4 -> Prores */
73 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test1)
74 {
75         ffmpeg_content_test (1, "test/data/test.mp4", FFmpegEncoder::FORMAT_PRORES);
76 }
77
78 /** Dolby Aurora trailer VOB -> Prores */
79 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test2)
80 {
81         ffmpeg_content_test (2, private_data / "dolby_aurora.vob", FFmpegEncoder::FORMAT_PRORES);
82 }
83
84 /** Sintel trailer -> Prores */
85 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test3)
86 {
87         ffmpeg_content_test (3, private_data / "Sintel_Trailer1.480p.DivX_Plus_HD.mkv", FFmpegEncoder::FORMAT_PRORES);
88 }
89
90 /** Big Buck Bunny trailer -> Prores */
91 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test4)
92 {
93         ffmpeg_content_test (4, private_data / "big_buck_bunny_trailer_480p.mov", FFmpegEncoder::FORMAT_PRORES);
94 }
95
96 /** Still image -> Prores */
97 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test5)
98 {
99         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test5");
100         film->set_name ("ffmpeg_encoder_prores_test5");
101         shared_ptr<FFmpegContent> c (new FFmpegContent (film, private_data / "bbc405.png"));
102         film->set_container (Ratio::from_id ("185"));
103         film->set_audio_channels (6);
104
105         film->examine_and_add_content (c);
106         BOOST_REQUIRE (!wait_for_jobs ());
107
108         c->video->set_length (240);
109
110         shared_ptr<Job> job (new TranscodeJob (film));
111         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test5.mov", FFmpegEncoder::FORMAT_PRORES, false);
112         encoder.go ();
113 }
114
115 /** Subs -> Prores */
116 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test6)
117 {
118         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test6");
119         film->set_name ("ffmpeg_encoder_prores_test6");
120         film->set_container (Ratio::from_id ("185"));
121         film->set_audio_channels (6);
122
123         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip2.srt"));
124         film->examine_and_add_content (s);
125         BOOST_REQUIRE (!wait_for_jobs ());
126         s->subtitle->set_colour (dcp::Colour (255, 255, 0));
127         s->subtitle->set_effect (dcp::SHADOW);
128         s->subtitle->set_effect_colour (dcp::Colour (0, 255, 255));
129         film->write_metadata();
130
131         shared_ptr<Job> job (new TranscodeJob (film));
132         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test6.mov", FFmpegEncoder::FORMAT_PRORES, false);
133         encoder.go ();
134 }
135
136 /** Video + subs -> Prores */
137 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_prores_test7)
138 {
139         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_prores_test7");
140         film->set_name ("ffmpeg_encoder_prores_test7");
141         film->set_container (Ratio::from_id ("185"));
142         film->set_audio_channels (6);
143
144         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
145         film->examine_and_add_content (c);
146         BOOST_REQUIRE (!wait_for_jobs ());
147
148         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip.srt"));
149         film->examine_and_add_content (s);
150         BOOST_REQUIRE (!wait_for_jobs ());
151         s->subtitle->set_colour (dcp::Colour (255, 255, 0));
152         s->subtitle->set_effect (dcp::SHADOW);
153         s->subtitle->set_effect_colour (dcp::Colour (0, 255, 255));
154
155         shared_ptr<Job> job (new TranscodeJob (film));
156         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_prores_test7.mov", FFmpegEncoder::FORMAT_PRORES, false);
157         encoder.go ();
158 }
159
160 /** Red / green / blue MP4 -> H264 */
161 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test1)
162 {
163         ffmpeg_content_test(1, "test/data/test.mp4", FFmpegEncoder::FORMAT_H264);
164 }
165
166 /** Just subtitles -> H264 */
167 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test2)
168 {
169         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_h264_test2");
170         film->set_name ("ffmpeg_encoder_h264_test2");
171         film->set_container (Ratio::from_id ("185"));
172         film->set_audio_channels (6);
173
174         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip2.srt"));
175         film->examine_and_add_content (s);
176         BOOST_REQUIRE (!wait_for_jobs ());
177         s->subtitle->set_colour (dcp::Colour (255, 255, 0));
178         s->subtitle->set_effect (dcp::SHADOW);
179         s->subtitle->set_effect_colour (dcp::Colour (0, 255, 255));
180         film->write_metadata();
181
182         shared_ptr<Job> job (new TranscodeJob (film));
183         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test2.mp4", FFmpegEncoder::FORMAT_H264, false);
184         encoder.go ();
185 }
186
187 /** Video + subs -> H264 */
188 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test3)
189 {
190         shared_ptr<Film> film = new_test_film ("ffmpeg_encoder_h264_test3");
191         film->set_name ("ffmpeg_encoder_h264_test3");
192         film->set_container (Ratio::from_id ("185"));
193         film->set_audio_channels (6);
194
195         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/test.mp4"));
196         film->examine_and_add_content (c);
197         BOOST_REQUIRE (!wait_for_jobs ());
198
199         shared_ptr<TextSubtitleContent> s (new TextSubtitleContent (film, "test/data/subrip.srt"));
200         film->examine_and_add_content (s);
201         BOOST_REQUIRE (!wait_for_jobs ());
202         s->subtitle->set_colour (dcp::Colour (255, 255, 0));
203         s->subtitle->set_effect (dcp::SHADOW);
204         s->subtitle->set_effect_colour (dcp::Colour (0, 255, 255));
205         film->write_metadata();
206
207         shared_ptr<Job> job (new TranscodeJob (film));
208         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test3.mp4", FFmpegEncoder::FORMAT_H264, false);
209         encoder.go ();
210 }
211
212 /** Scope-in-flat DCP -> H264 */
213 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test4)
214 {
215         shared_ptr<Film> film = new_test_film2("ffmpeg_encoder_h264_test4");
216         film->examine_and_add_content(shared_ptr<DCPContent>(new DCPContent(film, "test/data/scope_dcp")));
217         BOOST_REQUIRE(!wait_for_jobs());
218
219         film->set_container(Ratio::from_id("185"));
220
221         shared_ptr<Job> job(new TranscodeJob(film));
222         FFmpegEncoder encoder(film, job, "build/test/ffmpeg_encoder_h264_test4.mp4", FFmpegEncoder::FORMAT_H264, false);
223         encoder.go();
224 }
225
226 /** Test mixdown from 5.1 to stereo */
227 BOOST_AUTO_TEST_CASE (ffmpeg_encoder_h264_test5)
228 {
229         shared_ptr<Film> film = new_test_film ("ffmpeg_transcoder_h264_test5");
230         film->set_name ("ffmpeg_transcoder_h264_test5");
231         film->set_container (Ratio::from_id ("185"));
232         film->set_audio_channels (6);
233
234         shared_ptr<FFmpegContent> L (new FFmpegContent (film, "test/data/L.wav"));
235         film->examine_and_add_content (L);
236         shared_ptr<FFmpegContent> R (new FFmpegContent (film, "test/data/R.wav"));
237         film->examine_and_add_content (R);
238         shared_ptr<FFmpegContent> C (new FFmpegContent (film, "test/data/C.wav"));
239         film->examine_and_add_content (C);
240         shared_ptr<FFmpegContent> Ls (new FFmpegContent (film, "test/data/Ls.wav"));
241         film->examine_and_add_content (Ls);
242         shared_ptr<FFmpegContent> Rs (new FFmpegContent (film, "test/data/Rs.wav"));
243         film->examine_and_add_content (Rs);
244         shared_ptr<FFmpegContent> Lfe (new FFmpegContent (film, "test/data/Lfe.wav"));
245         film->examine_and_add_content (Lfe);
246         BOOST_REQUIRE (!wait_for_jobs ());
247
248         AudioMapping map (1, MAX_DCP_AUDIO_CHANNELS);
249
250         L->set_position (DCPTime::from_seconds (0));
251         map.make_zero ();
252         map.set (0, 0, 1);
253         L->audio->set_mapping (map);
254         R->set_position (DCPTime::from_seconds (1));
255         map.make_zero ();
256         map.set (0, 1, 1);
257         R->audio->set_mapping (map);
258         C->set_position (DCPTime::from_seconds (2));
259         map.make_zero ();
260         map.set (0, 2, 1);
261         C->audio->set_mapping (map);
262         Lfe->set_position (DCPTime::from_seconds (3));
263         map.make_zero ();
264         map.set (0, 3, 1);
265         Lfe->audio->set_mapping (map);
266         Ls->set_position (DCPTime::from_seconds (4));
267         map.make_zero ();
268         map.set (0, 4, 1);
269         Ls->audio->set_mapping (map);
270         Rs->set_position (DCPTime::from_seconds (5));
271         map.make_zero ();
272         map.set (0, 5, 1);
273         Rs->audio->set_mapping (map);
274
275         shared_ptr<Job> job (new TranscodeJob (film));
276         FFmpegEncoder encoder (film, job, "build/test/ffmpeg_encoder_h264_test5.mp4", FFmpegEncoder::FORMAT_H264, true);
277         encoder.go ();
278
279         check_ffmpeg ("build/test/ffmpeg_encoder_h264_test5.mp4", "test/data/ffmpeg_encoder_h264_test5.mp4", 1);
280 }