b939d29149fd95c28c20da419ce37cf49d7d514d
[dcpomatic.git] / test / optimise_stills_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/dcp_encoder.h"
22 #include "lib/writer.h"
23 #include "lib/transcode_job.h"
24 #include "lib/job_manager.h"
25 #include "lib/film.h"
26 #include "lib/ratio.h"
27 #include "lib/content_factory.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/content.h"
30 #include "lib/video_content.h"
31 #include "test.h"
32 #include <boost/test/unit_test.hpp>
33 #include <boost/algorithm/string.hpp>
34
35 using std::getline;
36 using std::ifstream;
37 using std::string;
38 using std::vector;
39 using boost::starts_with;
40 using boost::split;
41 using boost::dynamic_pointer_cast;
42 using boost::shared_ptr;
43
44 static
45 void
46 check (string name, int check_full, int check_repeat)
47 {
48         /* The encoder will have been destroyed so parse the logs */
49         string log_file = "build/test/" + name + "/log";
50         ifstream log (log_file.c_str());
51         string line;
52         int repeat = 0;
53         int full = 0;
54         while (getline (log, line)) {
55                 vector<string> bits;
56                 split (bits, line, boost::is_any_of (":"));
57                 if (bits.size() >= 4 && starts_with (bits[3], " Wrote")) {
58                         split (bits, bits[3], boost::is_any_of (" "));
59                         if (bits.size() >= 7) {
60                                 full = atoi (bits[2].c_str());
61                                 repeat = atoi (bits[6].c_str());
62                         }
63                 }
64
65         }
66
67         BOOST_CHECK_EQUAL (full, check_full);
68         BOOST_CHECK_EQUAL (repeat, check_repeat);
69 }
70
71 /** Make a 2D DCP out of a 2D still and check that the J2K encoding is only done once for each frame */
72 BOOST_AUTO_TEST_CASE (optimise_stills_test1)
73 {
74         shared_ptr<Film> film = new_test_film ("optimise_stills_test1");
75         film->set_container (Ratio::from_id ("185"));
76         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
77         film->set_name ("frobozz");
78         shared_ptr<Content> content = content_factory(film, "test/data/flat_red.png").front ();
79         film->examine_and_add_content (content);
80         BOOST_REQUIRE (!wait_for_jobs ());
81         film->make_dcp ();
82         BOOST_REQUIRE (!wait_for_jobs ());
83
84         check ("optimise_stills_test1", 1, 10 * 24 - 1);
85 }
86
87 /** Make a 3D DCP out of a 3D L/R still and check that the J2K encoding is only done once for L and R */
88 BOOST_AUTO_TEST_CASE (optimise_stills_test2)
89 {
90         shared_ptr<Film> film = new_test_film ("optimise_stills_test2");
91         film->set_container (Ratio::from_id ("185"));
92         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
93         film->set_name ("frobozz");
94         shared_ptr<Content> content = content_factory(film, "test/data/flat_red.png").front ();
95         film->examine_and_add_content (content);
96         BOOST_REQUIRE (!wait_for_jobs ());
97         content->video->set_frame_type (VIDEO_FRAME_TYPE_3D_LEFT_RIGHT);
98         film->set_three_d (true);
99         film->make_dcp ();
100         BOOST_REQUIRE (!wait_for_jobs ());
101
102         check ("optimise_stills_test2", 2, 10 * 48 - 2);
103 }