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