2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file test/frame_rate_test.cc
22 * @brief Tests for FrameRateChange and the computation of the best
23 * frame rate for the DCP.
26 #include <boost/test/unit_test.hpp>
28 #include "lib/config.h"
29 #include "lib/ffmpeg_content.h"
30 #include "lib/playlist.h"
31 #include "lib/ffmpeg_audio_stream.h"
32 #include "lib/frame_rate_change.h"
33 #include "lib/video_content.h"
34 #include "lib/audio_content.h"
37 using boost::shared_ptr;
39 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
40 with a single piece of content.
42 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_single)
44 shared_ptr<Film> film = new_test_film ("best_dcp_frame_rate_test_single");
45 /* Get any piece of content, it doesn't matter what */
46 shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/test.mp4"));
47 film->examine_and_add_content (content);
50 /* Run some tests with a limited range of allowed rates */
56 Config::instance()->set_allowed_dcp_frame_rates (afr);
58 content->_video_frame_rate = 60;
59 int best = film->best_video_frame_rate ();
60 FrameRateChange frc = FrameRateChange (60, best);
61 BOOST_CHECK_EQUAL (best, 30);
62 BOOST_CHECK_EQUAL (frc.skip, true);
63 BOOST_CHECK_EQUAL (frc.repeat, 1);
64 BOOST_CHECK_EQUAL (frc.change_speed, false);
65 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
67 content->_video_frame_rate = 50;
68 best = film->best_video_frame_rate ();
69 frc = FrameRateChange (50, best);
70 BOOST_CHECK_EQUAL (best, 25);
71 BOOST_CHECK_EQUAL (frc.skip, true);
72 BOOST_CHECK_EQUAL (frc.repeat, 1);
73 BOOST_CHECK_EQUAL (frc.change_speed, false);
74 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
76 content->_video_frame_rate = 48;
77 best = film->best_video_frame_rate ();
78 frc = FrameRateChange (48, best);
79 BOOST_CHECK_EQUAL (best, 24);
80 BOOST_CHECK_EQUAL (frc.skip, true);
81 BOOST_CHECK_EQUAL (frc.repeat, 1);
82 BOOST_CHECK_EQUAL (frc.change_speed, false);
83 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
85 content->_video_frame_rate = 30;
86 best = film->best_video_frame_rate ();
87 frc = FrameRateChange (30, best);
88 BOOST_CHECK_EQUAL (best, 30);
89 BOOST_CHECK_EQUAL (frc.skip, false);
90 BOOST_CHECK_EQUAL (frc.repeat, 1);
91 BOOST_CHECK_EQUAL (frc.change_speed, false);
92 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
94 content->_video_frame_rate = 29.97;
95 best = film->best_video_frame_rate ();
96 frc = FrameRateChange (29.97, best);
97 BOOST_CHECK_EQUAL (best, 30);
98 BOOST_CHECK_EQUAL (frc.skip, false);
99 BOOST_CHECK_EQUAL (frc.repeat, 1);
100 BOOST_CHECK_EQUAL (frc.change_speed, true);
101 BOOST_CHECK_CLOSE (frc.speed_up, 30 / 29.97, 0.1);
103 content->_video_frame_rate = 25;
104 best = film->best_video_frame_rate ();
105 frc = FrameRateChange (25, best);
106 BOOST_CHECK_EQUAL (best, 25);
107 BOOST_CHECK_EQUAL (frc.skip, false);
108 BOOST_CHECK_EQUAL (frc.repeat, 1);
109 BOOST_CHECK_EQUAL (frc.change_speed, false);
110 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
112 content->_video_frame_rate = 24;
113 best = film->best_video_frame_rate ();
114 frc = FrameRateChange (24, best);
115 BOOST_CHECK_EQUAL (best, 24);
116 BOOST_CHECK_EQUAL (frc.skip, false);
117 BOOST_CHECK_EQUAL (frc.repeat, 1);
118 BOOST_CHECK_EQUAL (frc.change_speed, false);
119 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
121 content->_video_frame_rate = 14.5;
122 best = film->best_video_frame_rate ();
123 frc = FrameRateChange (14.5, best);
124 BOOST_CHECK_EQUAL (best, 30);
125 BOOST_CHECK_EQUAL (frc.skip, false);
126 BOOST_CHECK_EQUAL (frc.repeat, 2);
127 BOOST_CHECK_EQUAL (frc.change_speed, true);
128 BOOST_CHECK_CLOSE (frc.speed_up, 15 / 14.5, 0.1);
130 content->_video_frame_rate = 12.6;
131 best = film->best_video_frame_rate ();
132 frc = FrameRateChange (12.6, best);
133 BOOST_CHECK_EQUAL (best, 25);
134 BOOST_CHECK_EQUAL (frc.skip, false);
135 BOOST_CHECK_EQUAL (frc.repeat, 2);
136 BOOST_CHECK_EQUAL (frc.change_speed, true);
137 BOOST_CHECK_CLOSE (frc.speed_up, 25 / 25.2, 0.1);
139 content->_video_frame_rate = 12.4;
140 best = film->best_video_frame_rate ();
141 frc = FrameRateChange (12.4, best);
142 BOOST_CHECK_EQUAL (best, 25);
143 BOOST_CHECK_EQUAL (frc.skip, false);
144 BOOST_CHECK_EQUAL (frc.repeat, 2);
145 BOOST_CHECK_EQUAL (frc.change_speed, true);
146 BOOST_CHECK_CLOSE (frc.speed_up, 25 / 24.8, 0.1);
148 content->_video_frame_rate = 12;
149 best = film->best_video_frame_rate ();
150 frc = FrameRateChange (12, best);
151 BOOST_CHECK_EQUAL (best, 24);
152 BOOST_CHECK_EQUAL (frc.skip, false);
153 BOOST_CHECK_EQUAL (frc.repeat, 2);
154 BOOST_CHECK_EQUAL (frc.change_speed, false);
155 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
157 /* Now add some more rates and see if it will use them
158 in preference to skip/repeat.
164 Config::instance()->set_allowed_dcp_frame_rates (afr);
166 content->_video_frame_rate = 60;
167 best = film->best_video_frame_rate ();
168 frc = FrameRateChange (60, best);
169 BOOST_CHECK_EQUAL (best, 60);
170 BOOST_CHECK_EQUAL (frc.skip, false);
171 BOOST_CHECK_EQUAL (frc.repeat, 1);
172 BOOST_CHECK_EQUAL (frc.change_speed, false);
173 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
175 content->_video_frame_rate = 50;
176 best = film->best_video_frame_rate ();
177 frc = FrameRateChange (50, best);
178 BOOST_CHECK_EQUAL (best, 50);
179 BOOST_CHECK_EQUAL (frc.skip, false);
180 BOOST_CHECK_EQUAL (frc.repeat, 1);
181 BOOST_CHECK_EQUAL (frc.change_speed, false);
182 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
184 content->_video_frame_rate = 48;
185 best = film->best_video_frame_rate ();
186 frc = FrameRateChange (48, best);
187 BOOST_CHECK_EQUAL (best, 48);
188 BOOST_CHECK_EQUAL (frc.skip, false);
189 BOOST_CHECK_EQUAL (frc.repeat, 1);
190 BOOST_CHECK_EQUAL (frc.change_speed, false);
191 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
193 /* Check some out-there conversions (not the best) */
195 frc = FrameRateChange (14.99, 24);
196 BOOST_CHECK_EQUAL (frc.skip, false);
197 BOOST_CHECK_EQUAL (frc.repeat, 2);
198 BOOST_CHECK_EQUAL (frc.change_speed, true);
199 BOOST_CHECK_CLOSE (frc.speed_up, 24 / (2 * 14.99), 0.1);
201 /* Check some conversions with limited DCP targets */
205 Config::instance()->set_allowed_dcp_frame_rates (afr);
207 content->_video_frame_rate = 25;
208 best = film->best_video_frame_rate ();
209 frc = FrameRateChange (25, best);
210 BOOST_CHECK_EQUAL (best, 24);
211 BOOST_CHECK_EQUAL (frc.skip, false);
212 BOOST_CHECK_EQUAL (frc.repeat, 1);
213 BOOST_CHECK_EQUAL (frc.change_speed, true);
214 BOOST_CHECK_CLOSE (frc.speed_up, 24.0 / 25, 0.1);
217 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
218 with two pieces of content.
220 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_double)
222 shared_ptr<Film> film = new_test_film ("best_dcp_frame_rate_test_double");
223 /* Get any old content, it doesn't matter what */
224 shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
225 film->examine_and_add_content (A);
226 shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
227 film->examine_and_add_content (B);
230 /* Run some tests with a limited range of allowed rates */
236 Config::instance()->set_allowed_dcp_frame_rates (afr);
238 A->_video_frame_rate = 30;
239 B->_video_frame_rate = 24;
240 BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 25);
242 A->_video_frame_rate = 24;
243 B->_video_frame_rate = 24;
244 BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 24);
246 A->_video_frame_rate = 24;
247 B->_video_frame_rate = 48;
248 BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 24);
251 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
253 shared_ptr<Film> film = new_test_film ("audio_sampling_rate_test");
254 /* Get any piece of content, it doesn't matter what */
255 shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/test.mp4"));
256 film->examine_and_add_content (content);
263 Config::instance()->set_allowed_dcp_frame_rates (afr);
265 shared_ptr<FFmpegAudioStream> stream (new FFmpegAudioStream ("foo", 0, 0, 0, 0));
266 content->audio.reset (new AudioContent (content.get()));
267 content->audio->add_stream (stream);
268 content->_video_frame_rate = 24;
269 film->set_video_frame_rate (24);
270 stream->_frame_rate = 48000;
271 BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 48000);
273 stream->_frame_rate = 44100;
274 BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 48000);
276 stream->_frame_rate = 80000;
277 BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 96000);
279 content->_video_frame_rate = 23.976;
280 film->set_video_frame_rate (24);
281 stream->_frame_rate = 48000;
282 BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 47952);
284 content->_video_frame_rate = 29.97;
285 film->set_video_frame_rate (30);
286 BOOST_CHECK_EQUAL (film->video_frame_rate (), 30);
287 stream->_frame_rate = 48000;
288 BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 47952);
290 content->_video_frame_rate = 25;
291 film->set_video_frame_rate (24);
292 stream->_frame_rate = 48000;
293 BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 50000);
295 content->_video_frame_rate = 25;
296 film->set_video_frame_rate (24);
297 stream->_frame_rate = 44100;
298 BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 50000);
300 /* Check some out-there conversions (not the best) */
302 content->_video_frame_rate = 14.99;
303 film->set_video_frame_rate (25);
304 stream->_frame_rate = 16000;
305 /* The FrameRateChange within resampled_frame_rate should choose to double-up
306 the 14.99 fps video to 30 and then run it slow at 25.
308 BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), lrint (48000 * 2 * 14.99 / 25));