2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file test/frame_rate_test.cc
21 * @brief Tests for FrameRateChange and the computation of the best
22 * frame rate for the DCP.
25 #include <boost/test/unit_test.hpp>
27 #include "lib/config.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/playlist.h"
32 using boost::shared_ptr;
34 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
35 with a single piece of content.
37 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_single)
39 shared_ptr<Film> film = new_test_film ("best_dcp_frame_rate_test_single");
40 /* Get any piece of content, it doesn't matter what */
41 shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/test.mp4"));
42 film->add_content (content);
45 /* Run some tests with a limited range of allowed rates */
51 Config::instance()->set_allowed_dcp_frame_rates (afr);
53 content->_video_frame_rate = 60;
54 int best = film->playlist()->best_dcp_frame_rate ();
55 FrameRateChange frc = FrameRateChange (60, best);
56 BOOST_CHECK_EQUAL (best, 30);
57 BOOST_CHECK_EQUAL (frc.skip, true);
58 BOOST_CHECK_EQUAL (frc.repeat, 1);
59 BOOST_CHECK_EQUAL (frc.change_speed, false);
60 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
62 content->_video_frame_rate = 50;
63 best = film->playlist()->best_dcp_frame_rate ();
64 frc = FrameRateChange (50, best);
65 BOOST_CHECK_EQUAL (best, 25);
66 BOOST_CHECK_EQUAL (frc.skip, true);
67 BOOST_CHECK_EQUAL (frc.repeat, 1);
68 BOOST_CHECK_EQUAL (frc.change_speed, false);
69 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
71 content->_video_frame_rate = 48;
72 best = film->playlist()->best_dcp_frame_rate ();
73 frc = FrameRateChange (48, best);
74 BOOST_CHECK_EQUAL (best, 24);
75 BOOST_CHECK_EQUAL (frc.skip, true);
76 BOOST_CHECK_EQUAL (frc.repeat, 1);
77 BOOST_CHECK_EQUAL (frc.change_speed, false);
78 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
80 content->_video_frame_rate = 30;
81 best = film->playlist()->best_dcp_frame_rate ();
82 frc = FrameRateChange (30, best);
83 BOOST_CHECK_EQUAL (best, 30);
84 BOOST_CHECK_EQUAL (frc.skip, false);
85 BOOST_CHECK_EQUAL (frc.repeat, 1);
86 BOOST_CHECK_EQUAL (frc.change_speed, false);
87 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
89 content->_video_frame_rate = 29.97;
90 best = film->playlist()->best_dcp_frame_rate ();
91 frc = FrameRateChange (29.97, best);
92 BOOST_CHECK_EQUAL (best, 30);
93 BOOST_CHECK_EQUAL (frc.skip, false);
94 BOOST_CHECK_EQUAL (frc.repeat, 1);
95 BOOST_CHECK_EQUAL (frc.change_speed, true);
96 BOOST_CHECK_CLOSE (frc.speed_up, 30 / 29.97, 0.1);
98 content->_video_frame_rate = 25;
99 best = film->playlist()->best_dcp_frame_rate ();
100 frc = FrameRateChange (25, best);
101 BOOST_CHECK_EQUAL (best, 25);
102 BOOST_CHECK_EQUAL (frc.skip, false);
103 BOOST_CHECK_EQUAL (frc.repeat, 1);
104 BOOST_CHECK_EQUAL (frc.change_speed, false);
105 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
107 content->_video_frame_rate = 24;
108 best = film->playlist()->best_dcp_frame_rate ();
109 frc = FrameRateChange (24, best);
110 BOOST_CHECK_EQUAL (best, 24);
111 BOOST_CHECK_EQUAL (frc.skip, false);
112 BOOST_CHECK_EQUAL (frc.repeat, 1);
113 BOOST_CHECK_EQUAL (frc.change_speed, false);
114 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
116 content->_video_frame_rate = 14.5;
117 best = film->playlist()->best_dcp_frame_rate ();
118 frc = FrameRateChange (14.5, best);
119 BOOST_CHECK_EQUAL (best, 30);
120 BOOST_CHECK_EQUAL (frc.skip, false);
121 BOOST_CHECK_EQUAL (frc.repeat, 2);
122 BOOST_CHECK_EQUAL (frc.change_speed, true);
123 BOOST_CHECK_CLOSE (frc.speed_up, 15 / 14.5, 0.1);
125 content->_video_frame_rate = 12.6;
126 best = film->playlist()->best_dcp_frame_rate ();
127 frc = FrameRateChange (12.6, best);
128 BOOST_CHECK_EQUAL (best, 25);
129 BOOST_CHECK_EQUAL (frc.skip, false);
130 BOOST_CHECK_EQUAL (frc.repeat, 2);
131 BOOST_CHECK_EQUAL (frc.change_speed, true);
132 BOOST_CHECK_CLOSE (frc.speed_up, 25 / 25.2, 0.1);
134 content->_video_frame_rate = 12.4;
135 best = film->playlist()->best_dcp_frame_rate ();
136 frc = FrameRateChange (12.4, best);
137 BOOST_CHECK_EQUAL (best, 25);
138 BOOST_CHECK_EQUAL (frc.skip, false);
139 BOOST_CHECK_EQUAL (frc.repeat, 2);
140 BOOST_CHECK_EQUAL (frc.change_speed, true);
141 BOOST_CHECK_CLOSE (frc.speed_up, 25 / 24.8, 0.1);
143 content->_video_frame_rate = 12;
144 best = film->playlist()->best_dcp_frame_rate ();
145 frc = FrameRateChange (12, best);
146 BOOST_CHECK_EQUAL (best, 24);
147 BOOST_CHECK_EQUAL (frc.skip, false);
148 BOOST_CHECK_EQUAL (frc.repeat, 2);
149 BOOST_CHECK_EQUAL (frc.change_speed, false);
150 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
152 /* Now add some more rates and see if it will use them
153 in preference to skip/repeat.
159 Config::instance()->set_allowed_dcp_frame_rates (afr);
161 content->_video_frame_rate = 60;
162 best = film->playlist()->best_dcp_frame_rate ();
163 frc = FrameRateChange (60, best);
164 BOOST_CHECK_EQUAL (best, 60);
165 BOOST_CHECK_EQUAL (frc.skip, false);
166 BOOST_CHECK_EQUAL (frc.repeat, 1);
167 BOOST_CHECK_EQUAL (frc.change_speed, false);
168 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
170 content->_video_frame_rate = 50;
171 best = film->playlist()->best_dcp_frame_rate ();
172 frc = FrameRateChange (50, best);
173 BOOST_CHECK_EQUAL (best, 50);
174 BOOST_CHECK_EQUAL (frc.skip, false);
175 BOOST_CHECK_EQUAL (frc.repeat, 1);
176 BOOST_CHECK_EQUAL (frc.change_speed, false);
177 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
179 content->_video_frame_rate = 48;
180 best = film->playlist()->best_dcp_frame_rate ();
181 frc = FrameRateChange (48, best);
182 BOOST_CHECK_EQUAL (best, 48);
183 BOOST_CHECK_EQUAL (frc.skip, false);
184 BOOST_CHECK_EQUAL (frc.repeat, 1);
185 BOOST_CHECK_EQUAL (frc.change_speed, false);
186 BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
188 /* Check some out-there conversions (not the best) */
190 frc = FrameRateChange (14.99, 24);
191 BOOST_CHECK_EQUAL (frc.skip, false);
192 BOOST_CHECK_EQUAL (frc.repeat, 2);
193 BOOST_CHECK_EQUAL (frc.change_speed, true);
194 BOOST_CHECK_CLOSE (frc.speed_up, 24 / (2 * 14.99), 0.1);
196 /* Check some conversions with limited DCP targets */
200 Config::instance()->set_allowed_dcp_frame_rates (afr);
202 content->_video_frame_rate = 25;
203 best = film->playlist()->best_dcp_frame_rate ();
204 frc = FrameRateChange (25, best);
205 BOOST_CHECK_EQUAL (best, 24);
206 BOOST_CHECK_EQUAL (frc.skip, false);
207 BOOST_CHECK_EQUAL (frc.repeat, 1);
208 BOOST_CHECK_EQUAL (frc.change_speed, true);
209 BOOST_CHECK_CLOSE (frc.speed_up, 24.0 / 25, 0.1);
212 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
213 with two pieces of content.
215 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_double)
217 shared_ptr<Film> film = new_test_film ("best_dcp_frame_rate_test_double");
218 /* Get any old content, it doesn't matter what */
219 shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
220 film->add_content (A);
221 shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
222 film->add_content (B);
225 /* Run some tests with a limited range of allowed rates */
231 Config::instance()->set_allowed_dcp_frame_rates (afr);
233 A->_video_frame_rate = 30;
234 B->_video_frame_rate = 24;
235 BOOST_CHECK_EQUAL (film->playlist()->best_dcp_frame_rate(), 25);
239 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
241 shared_ptr<Film> film = new_test_film ("audio_sampling_rate_test");
242 /* Get any piece of content, it doesn't matter what */
243 shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/test.mp4"));
244 film->add_content (content);
251 Config::instance()->set_allowed_dcp_frame_rates (afr);
253 content->_video_frame_rate = 24;
254 film->set_video_frame_rate (24);
255 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
256 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 48000);
258 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
259 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 48000);
261 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 80000, 0)));
262 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 96000);
264 content->_video_frame_rate = 23.976;
265 film->set_video_frame_rate (24);
266 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
267 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 47952);
269 content->_video_frame_rate = 29.97;
270 film->set_video_frame_rate (30);
271 BOOST_CHECK_EQUAL (film->video_frame_rate (), 30);
272 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
273 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 47952);
275 content->_video_frame_rate = 25;
276 film->set_video_frame_rate (24);
277 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
278 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 50000);
280 content->_video_frame_rate = 25;
281 film->set_video_frame_rate (24);
282 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
283 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 50000);
285 /* Check some out-there conversions (not the best) */
287 content->_video_frame_rate = 14.99;
288 film->set_video_frame_rate (25);
289 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 16000, 0)));
290 /* The FrameRateChange within output_audio_frame_rate should choose to double-up
291 the 14.99 fps video to 30 and then run it slow at 25.
293 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), rint (48000 * 2 * 14.99 / 25));