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