No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / test / video_content_scale_test.cc
1 /*
2     Copyright (C) 2015 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 <boost/test/unit_test.hpp>
22 #include "lib/ffmpeg_content.h"
23 #include "lib/ratio.h"
24 #include "lib/video_content.h"
25
26 using std::list;
27 using std::string;
28 using std::cerr;
29 using boost::shared_ptr;
30 using boost::optional;
31
32 static
33 void
34 test (dcp::Size content_size, dcp::Size display_size, dcp::Size film_size, Crop crop, Ratio const * ratio, bool scale, dcp::Size correct)
35 {
36         shared_ptr<Film> film;
37         SafeStringStream s;
38         s << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
39                 "<Content>"
40                 "<Type>FFmpeg</Type>"
41                 "<Path>/home/c.hetherington/DCP/prophet_clip.mkv</Path>"
42                 "<Digest>f3f23663da5bef6d2cbaa0db066f3351314142710</Digest>"
43                 "<Position>0</Position>"
44                 "<TrimStart>0</TrimStart>"
45                 "<TrimEnd>0</TrimEnd>"
46                 "<VideoLength>2879</VideoLength>"
47                 "<VideoWidth>" << content_size.width << "</VideoWidth>"
48                 "<VideoHeight>" << content_size.height << "</VideoHeight>"
49                 "<VideoFrameRate>23.97602462768555</VideoFrameRate>"
50                 "<OriginalVideoFrameRate>23.97602462768555</OriginalVideoFrameRate>"
51                 "<VideoFrameType>0</VideoFrameType>"
52                 "<SampleAspectRatio>1</SampleAspectRatio>"
53                 "<BitsPerPixel>12</BitsPerPixel>"
54                 "<LeftCrop>" << crop.left << "</LeftCrop>"
55                 "<RightCrop>" << crop.right << "</RightCrop>"
56                 "<TopCrop>" << crop.top << "</TopCrop>"
57                 "<BottomCrop>" << crop.bottom << "</BottomCrop>"
58                 "<Scale>";
59
60         if (ratio) {
61                 s << "<Ratio>" << ratio->id() << "</Ratio>";
62         } else {
63                 s << "<Scale>" << scale << "</Scale>";
64         }
65
66         s << "</Scale>"
67                 "<ColourConversion>"
68                 "<InputGamma>2.4</InputGamma>"
69                 "<InputGammaLinearised>1</InputGammaLinearised>"
70                 "<Matrix i=\"0\" j=\"0\">0.4124564</Matrix>"
71                 "<Matrix i=\"0\" j=\"1\">0.3575761</Matrix>"
72                 "<Matrix i=\"0\" j=\"2\">0.1804375</Matrix>"
73                 "<Matrix i=\"1\" j=\"0\">0.2126729</Matrix>"
74                 "<Matrix i=\"1\" j=\"1\">0.7151522</Matrix>"
75                 "<Matrix i=\"1\" j=\"2\">0.072175</Matrix>"
76                 "<Matrix i=\"2\" j=\"0\">0.0193339</Matrix>"
77                 "<Matrix i=\"2\" j=\"1\">0.119192</Matrix>"
78                 "<Matrix i=\"2\" j=\"2\">0.9503041</Matrix>"
79                 "<OutputGamma>2.6</OutputGamma>"
80                 "</ColourConversion>"
81                 "<AudioGain>0</AudioGain>"
82                 "<AudioDelay>0</AudioDelay>"
83                 "<SubtitleXOffset>0</SubtitleXOffset>"
84                 "<SubtitleYOffset>0</SubtitleYOffset>"
85                 "<SubtitleXScale>0</SubtitleXScale>"
86                 "<SubtitleYScale>0</SubtitleYScale>"
87                 "</Content>";
88
89         shared_ptr<cxml::Document> doc (new cxml::Document ());
90         doc->read_string(s.str ());
91
92         list<string> notes;
93         shared_ptr<FFmpegContent> vc (new FFmpegContent (film, doc, 10, notes));
94
95         optional<VideoContentScale> sc;
96         if (ratio) {
97                 sc = VideoContentScale (ratio);
98         } else {
99                 sc = VideoContentScale (scale);
100         }
101
102         dcp::Size answer = sc.get().size (vc->video, display_size, film_size);
103         if (answer != correct) {
104                 cerr << "Testing " << vc->video->size().width << "x" << vc->video->size().height << "\n";
105                 cerr << "Testing " << display_size.width << "x" << display_size.height << "\n";
106                 cerr << answer.width << "x" << answer.height << " instead of " << correct.width << "x" << correct.height << "\n";
107         }
108         BOOST_CHECK (answer == correct);
109 }
110
111 /* Test scale and stretch to specified ratio */
112 BOOST_AUTO_TEST_CASE (video_content_scale_test_to_ratio)
113 {
114         /* To DCP */
115
116         // Flat in flat container
117         test (
118                 dcp::Size (400, 200),
119                 dcp::Size (1998, 1080),
120                 dcp::Size (1998, 1080),
121                 Crop (0, 0, 0, 0),
122                 Ratio::from_id ("185"),
123                 true,
124                 dcp::Size (1998, 1080)
125                 );
126
127         // Scope in flat container
128         test (
129                 dcp::Size (400, 200),
130                 dcp::Size (1998, 1080),
131                 dcp::Size (1998, 1080),
132                 Crop (0, 0, 0, 0),
133                 Ratio::from_id ("239"),
134                 true,
135                 dcp::Size (1998, 837)
136                 );
137
138         // Flat in scope container
139         test (
140                 dcp::Size (400, 200),
141                 dcp::Size (2048, 858),
142                 dcp::Size (2048, 858),
143                 Crop (0, 0, 0, 0),
144                 Ratio::from_id ("185"),
145                 true,
146                 dcp::Size (1587, 858)
147                 );
148
149
150         /* To player */
151
152         // Flat in flat container
153         test (
154                 dcp::Size (400, 200),
155                 dcp::Size (185, 100),
156                 dcp::Size (1998, 1080),
157                 Crop (0, 0, 0, 0),
158                 Ratio::from_id ("185"),
159                 true,
160                 dcp::Size (185, 100)
161                 );
162
163         // Scope in flat container
164         test (
165                 dcp::Size (400, 200),
166                 dcp::Size (185, 100),
167                 dcp::Size (1998, 1080),
168                 Crop (0, 0, 0, 0),
169                 Ratio::from_id ("239"),
170                 true,
171                 dcp::Size (185, 78)
172                 );
173
174         // Flat in scope container
175         test (
176                 dcp::Size (400, 200),
177                 dcp::Size (239, 100),
178                 dcp::Size (2048, 858),
179                 Crop (0, 0, 0, 0),
180                 Ratio::from_id ("185"),
181                 true,
182                 dcp::Size (185, 100)
183                 );
184 }
185
186 /* Test no scale */
187 BOOST_AUTO_TEST_CASE (video_content_scale_no_scale)
188 {
189        /* No scale where the content is bigger than even the film container */
190        test (
191                dcp::Size (1920, 1080),
192                dcp::Size (887, 371),
193                dcp::Size (2048, 858),
194                Crop (),
195                0,
196                false,
197                dcp::Size (659, 371)
198                );
199 }