Remove approximate size stuff where playback viewer would round
[dcpomatic.git] / test / video_content_scale_test.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/test/unit_test.hpp>
21 #include "lib/ffmpeg_content.h"
22 #include "lib/ratio.h"
23
24 using std::list;
25 using std::string;
26 using std::cerr;
27 using std::stringstream;
28 using boost::shared_ptr;
29 using boost::optional;
30
31 static
32 void
33 test (dcp::Size content_size, dcp::Size display_size, dcp::Size film_size, Crop crop, Ratio const * ratio, bool scale, dcp::Size correct)
34 {
35         shared_ptr<Film> film;
36         stringstream s;
37         s << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
38                 "<Content>"
39                 "<Type>FFmpeg</Type>"
40                 "<Path>/home/c.hetherington/DCP/prophet_clip.mkv</Path>"
41                 "<Digest>f3f23663da5bef6d2cbaa0db066f3351314142710</Digest>"
42                 "<Position>0</Position>"
43                 "<TrimStart>0</TrimStart>"
44                 "<TrimEnd>0</TrimEnd>"
45                 "<VideoLength>2879</VideoLength>"
46                 "<VideoWidth>" << content_size.width << "</VideoWidth>"
47                 "<VideoHeight>" << content_size.height << "</VideoHeight>"
48                 "<VideoFrameRate>23.97602462768555</VideoFrameRate>"
49                 "<OriginalVideoFrameRate>23.97602462768555</OriginalVideoFrameRate>"
50                 "<VideoFrameType>0</VideoFrameType>"
51                 "<SampleAspectRatio>1</SampleAspectRatio>"
52                 "<LeftCrop>" << crop.left << "</LeftCrop>"
53                 "<RightCrop>" << crop.right << "</RightCrop>"
54                 "<TopCrop>" << crop.top << "</TopCrop>"
55                 "<BottomCrop>" << crop.bottom << "</BottomCrop>"
56                 "<Scale>";
57
58         if (ratio) {
59                 s << "<Ratio>" << ratio->id() << "</Ratio>";
60         } else {
61                 s << "<Scale>" << scale << "</Scale>";
62         }
63
64         s << "</Scale>"
65                 "<ColourConversion>"
66                 "<InputGamma>2.4</InputGamma>"
67                 "<InputGammaLinearised>1</InputGammaLinearised>"
68                 "<Matrix i=\"0\" j=\"0\">0.4124564</Matrix>"
69                 "<Matrix i=\"0\" j=\"1\">0.3575761</Matrix>"
70                 "<Matrix i=\"0\" j=\"2\">0.1804375</Matrix>"
71                 "<Matrix i=\"1\" j=\"0\">0.2126729</Matrix>"
72                 "<Matrix i=\"1\" j=\"1\">0.7151522</Matrix>"
73                 "<Matrix i=\"1\" j=\"2\">0.072175</Matrix>"
74                 "<Matrix i=\"2\" j=\"0\">0.0193339</Matrix>"
75                 "<Matrix i=\"2\" j=\"1\">0.119192</Matrix>"
76                 "<Matrix i=\"2\" j=\"2\">0.9503041</Matrix>"
77                 "<OutputGamma>2.6</OutputGamma>"
78                 "</ColourConversion>"
79                 "<AudioGain>0</AudioGain>"
80                 "<AudioDelay>0</AudioDelay>"
81                 "<SubtitleXOffset>0</SubtitleXOffset>"
82                 "<SubtitleYOffset>0</SubtitleYOffset>"
83                 "<SubtitleXScale>0</SubtitleXScale>"
84                 "<SubtitleYScale>0</SubtitleYScale>"
85                 "</Content>";
86
87         shared_ptr<cxml::Document> doc (new cxml::Document ());
88         doc->read_string(s.str ());
89
90         list<string> notes;
91         shared_ptr<VideoContent> vc (new FFmpegContent (film, doc, 10, notes));
92
93         optional<VideoContentScale> sc;
94         if (ratio) {
95                 sc = VideoContentScale (ratio);
96         } else {
97                 sc = VideoContentScale (scale);
98         }
99
100         dcp::Size answer = sc.get().size (vc, display_size, film_size);
101         if (answer != correct) {
102                 cerr << "Testing " << vc->video_size().width << "x" << vc->video_size().height << "\n";
103                 cerr << "Testing " << display_size.width << "x" << display_size.height << "\n";
104                 cerr << answer.width << "x" << answer.height << " instead of " << correct.width << "x" << correct.height << "\n";
105         }
106         BOOST_CHECK (answer == correct);
107 }
108       
109 /* Test scale and stretch to specified ratio */
110 BOOST_AUTO_TEST_CASE (video_content_scale_test_to_ratio)
111 {
112         /* To DCP */
113
114         // Flat in flat container
115         test (
116                 dcp::Size (400, 200),
117                 dcp::Size (1998, 1080),
118                 dcp::Size (1998, 1080),
119                 Crop (0, 0, 0, 0),
120                 Ratio::from_id ("185"),
121                 true,
122                 dcp::Size (1998, 1080)
123                 );
124
125         // Scope in flat container
126         test (
127                 dcp::Size (400, 200),
128                 dcp::Size (1998, 1080),
129                 dcp::Size (1998, 1080),
130                 Crop (0, 0, 0, 0),
131                 Ratio::from_id ("239"),
132                 true,
133                 dcp::Size (1998, 837)
134                 );
135         
136         // Flat in scope container
137         test (
138                 dcp::Size (400, 200),
139                 dcp::Size (2048, 858),
140                 dcp::Size (2048, 858),
141                 Crop (0, 0, 0, 0),
142                 Ratio::from_id ("185"),
143                 true,
144                 dcp::Size (1587, 858)
145                 );
146
147         
148         /* To player */
149
150         // Flat in flat container
151         test (
152                 dcp::Size (400, 200),
153                 dcp::Size (185, 100),
154                 dcp::Size (1998, 1080),
155                 Crop (0, 0, 0, 0),
156                 Ratio::from_id ("185"),
157                 true,
158                 dcp::Size (185, 100)
159                 );
160
161         // Scope in flat container
162         test (
163                 dcp::Size (400, 200),
164                 dcp::Size (185, 100),
165                 dcp::Size (1998, 1080),
166                 Crop (0, 0, 0, 0),
167                 Ratio::from_id ("239"),
168                 true,
169                 dcp::Size (185, 78)
170                 );
171         
172         // Flat in scope container
173         test (
174                 dcp::Size (400, 200),
175                 dcp::Size (239, 100),
176                 dcp::Size (2048, 858),
177                 Crop (0, 0, 0, 0),
178                 Ratio::from_id ("185"),
179                 true,
180                 dcp::Size (185, 100)
181                 );
182 }
183
184 /* Test no scale */
185 BOOST_AUTO_TEST_CASE (video_content_scale_no_scale)
186 {
187        /* No scale where the content is bigger than even the film container */
188        test (
189                dcp::Size (1920, 1080),
190                dcp::Size (887, 371),
191                dcp::Size (2048, 858),
192                Crop (),
193                0,
194                false,
195                dcp::Size (659, 371)
196                );
197 }