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