2 Copyright (C) 2012 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 #include <boost/test/unit_test.hpp>
22 #include "lib/config.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/playlist.h"
25 #include "lib/frame_rate_change.h"
28 using boost::shared_ptr;
30 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
31 with a single piece of content.
33 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_single)
35 shared_ptr<Film> film = new_test_film ("best_dcp_frame_rate_test_single");
36 /* Get any piece of content, it doesn't matter what */
37 shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/test.mp4"));
38 film->add_content (content);
41 /* Run some tests with a limited range of allowed rates */
47 Config::instance()->set_allowed_dcp_frame_rates (afr);
49 content->_video_frame_rate = 60;
50 int best = film->playlist()->best_dcp_frame_rate ();
51 FrameRateChange frc = FrameRateChange (60, best);
52 BOOST_CHECK_EQUAL (best, 30);
53 BOOST_CHECK_EQUAL (frc.skip, true);
54 BOOST_CHECK_EQUAL (frc.repeat, 1);
55 BOOST_CHECK_EQUAL (frc.change_speed, false);
57 content->_video_frame_rate = 50;
58 best = film->playlist()->best_dcp_frame_rate ();
59 frc = FrameRateChange (50, best);
60 BOOST_CHECK_EQUAL (best, 25);
61 BOOST_CHECK_EQUAL (frc.skip, true);
62 BOOST_CHECK_EQUAL (frc.repeat, 1);
63 BOOST_CHECK_EQUAL (frc.change_speed, false);
65 content->_video_frame_rate = 48;
66 best = film->playlist()->best_dcp_frame_rate ();
67 frc = FrameRateChange (48, best);
68 BOOST_CHECK_EQUAL (best, 24);
69 BOOST_CHECK_EQUAL (frc.skip, true);
70 BOOST_CHECK_EQUAL (frc.repeat, 1);
71 BOOST_CHECK_EQUAL (frc.change_speed, false);
73 content->_video_frame_rate = 30;
74 best = film->playlist()->best_dcp_frame_rate ();
75 frc = FrameRateChange (30, best);
76 BOOST_CHECK_EQUAL (best, 30);
77 BOOST_CHECK_EQUAL (frc.skip, false);
78 BOOST_CHECK_EQUAL (frc.repeat, 1);
79 BOOST_CHECK_EQUAL (frc.change_speed, false);
81 content->_video_frame_rate = 29.97;
82 best = film->playlist()->best_dcp_frame_rate ();
83 frc = FrameRateChange (29.97, best);
84 BOOST_CHECK_EQUAL (best, 30);
85 BOOST_CHECK_EQUAL (frc.skip, false);
86 BOOST_CHECK_EQUAL (frc.repeat, 1);
87 BOOST_CHECK_EQUAL (frc.change_speed, true);
89 content->_video_frame_rate = 25;
90 best = film->playlist()->best_dcp_frame_rate ();
91 frc = FrameRateChange (25, best);
92 BOOST_CHECK_EQUAL (best, 25);
93 BOOST_CHECK_EQUAL (frc.skip, false);
94 BOOST_CHECK_EQUAL (frc.repeat, 1);
95 BOOST_CHECK_EQUAL (frc.change_speed, false);
97 content->_video_frame_rate = 24;
98 best = film->playlist()->best_dcp_frame_rate ();
99 frc = FrameRateChange (24, best);
100 BOOST_CHECK_EQUAL (best, 24);
101 BOOST_CHECK_EQUAL (frc.skip, false);
102 BOOST_CHECK_EQUAL (frc.repeat, 1);
103 BOOST_CHECK_EQUAL (frc.change_speed, false);
105 content->_video_frame_rate = 14.5;
106 best = film->playlist()->best_dcp_frame_rate ();
107 frc = FrameRateChange (14.5, best);
108 BOOST_CHECK_EQUAL (best, 30);
109 BOOST_CHECK_EQUAL (frc.skip, false);
110 BOOST_CHECK_EQUAL (frc.repeat, 2);
111 BOOST_CHECK_EQUAL (frc.change_speed, true);
113 content->_video_frame_rate = 12.6;
114 best = film->playlist()->best_dcp_frame_rate ();
115 frc = FrameRateChange (12.6, best);
116 BOOST_CHECK_EQUAL (best, 25);
117 BOOST_CHECK_EQUAL (frc.skip, false);
118 BOOST_CHECK_EQUAL (frc.repeat, 2);
119 BOOST_CHECK_EQUAL (frc.change_speed, true);
121 content->_video_frame_rate = 12.4;
122 best = film->playlist()->best_dcp_frame_rate ();
123 frc = FrameRateChange (12.4, best);
124 BOOST_CHECK_EQUAL (best, 25);
125 BOOST_CHECK_EQUAL (frc.skip, false);
126 BOOST_CHECK_EQUAL (frc.repeat, 2);
127 BOOST_CHECK_EQUAL (frc.change_speed, true);
129 content->_video_frame_rate = 12;
130 best = film->playlist()->best_dcp_frame_rate ();
131 frc = FrameRateChange (12, best);
132 BOOST_CHECK_EQUAL (best, 24);
133 BOOST_CHECK_EQUAL (frc.skip, false);
134 BOOST_CHECK_EQUAL (frc.repeat, 2);
135 BOOST_CHECK_EQUAL (frc.change_speed, false);
137 /* Now add some more rates and see if it will use them
138 in preference to skip/repeat.
144 Config::instance()->set_allowed_dcp_frame_rates (afr);
146 content->_video_frame_rate = 60;
147 best = film->playlist()->best_dcp_frame_rate ();
148 frc = FrameRateChange (60, best);
149 BOOST_CHECK_EQUAL (best, 60);
150 BOOST_CHECK_EQUAL (frc.skip, false);
151 BOOST_CHECK_EQUAL (frc.repeat, 1);
152 BOOST_CHECK_EQUAL (frc.change_speed, false);
154 content->_video_frame_rate = 50;
155 best = film->playlist()->best_dcp_frame_rate ();
156 frc = FrameRateChange (50, best);
157 BOOST_CHECK_EQUAL (best, 50);
158 BOOST_CHECK_EQUAL (frc.skip, false);
159 BOOST_CHECK_EQUAL (frc.repeat, 1);
160 BOOST_CHECK_EQUAL (frc.change_speed, false);
162 content->_video_frame_rate = 48;
163 best = film->playlist()->best_dcp_frame_rate ();
164 frc = FrameRateChange (48, best);
165 BOOST_CHECK_EQUAL (best, 48);
166 BOOST_CHECK_EQUAL (frc.skip, false);
167 BOOST_CHECK_EQUAL (frc.repeat, 1);
168 BOOST_CHECK_EQUAL (frc.change_speed, false);
170 /* Check some out-there conversions (not the best) */
172 frc = FrameRateChange (14.99, 24);
173 BOOST_CHECK_EQUAL (frc.skip, false);
174 BOOST_CHECK_EQUAL (frc.repeat, 2);
175 BOOST_CHECK_EQUAL (frc.change_speed, true);
177 /* Check some conversions with limited DCP targets */
181 Config::instance()->set_allowed_dcp_frame_rates (afr);
183 content->_video_frame_rate = 25;
184 best = film->playlist()->best_dcp_frame_rate ();
185 frc = FrameRateChange (25, best);
186 BOOST_CHECK_EQUAL (best, 24);
187 BOOST_CHECK_EQUAL (frc.skip, false);
188 BOOST_CHECK_EQUAL (frc.repeat, 1);
189 BOOST_CHECK_EQUAL (frc.change_speed, true);
192 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
193 with two pieces of content.
195 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_double)
197 shared_ptr<Film> film = new_test_film ("best_dcp_frame_rate_test_double");
198 /* Get any old content, it doesn't matter what */
199 shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
200 film->add_content (A);
201 shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
202 film->add_content (B);
205 /* Run some tests with a limited range of allowed rates */
211 Config::instance()->set_allowed_dcp_frame_rates (afr);
213 A->_video_frame_rate = 30;
214 B->_video_frame_rate = 24;
215 BOOST_CHECK_EQUAL (film->playlist()->best_dcp_frame_rate(), 25);
219 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
221 shared_ptr<Film> film = new_test_film ("audio_sampling_rate_test");
222 /* Get any piece of content, it doesn't matter what */
223 shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/test.mp4"));
224 film->examine_and_add_content (content);
231 Config::instance()->set_allowed_dcp_frame_rates (afr);
233 content->_video_frame_rate = 24;
234 film->set_video_frame_rate (24);
235 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
236 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 48000);
238 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
239 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 48000);
241 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 80000, 0)));
242 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 96000);
244 content->_video_frame_rate = 23.976;
245 film->set_video_frame_rate (24);
246 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
247 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 47952);
249 content->_video_frame_rate = 29.97;
250 film->set_video_frame_rate (30);
251 BOOST_CHECK_EQUAL (film->video_frame_rate (), 30);
252 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
253 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 47952);
255 content->_video_frame_rate = 25;
256 film->set_video_frame_rate (24);
257 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
258 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 50000);
260 content->_video_frame_rate = 25;
261 film->set_video_frame_rate (24);
262 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
263 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), 50000);
265 /* Check some out-there conversions (not the best) */
267 content->_video_frame_rate = 14.99;
268 film->set_video_frame_rate (25);
269 content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 16000, 0)));
270 /* The FrameRateChange within output_audio_frame_rate should choose to double-up
271 the 14.99 fps video to 30 and then run it slow at 25.
273 BOOST_CHECK_EQUAL (content->output_audio_frame_rate(), rint (48000 * 2 * 14.99 / 25));