Merge master.
[dcpomatic.git] / test / frame_rate_test.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  test/frame_rate_test.cc
21  *  @brief Tests for FrameRateChange and the computation of the best
22  *  frame rate for the DCP.
23  */
24
25 #include <boost/test/unit_test.hpp>
26 #include "lib/film.h"
27 #include "lib/config.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/playlist.h"
30 #include "lib/ffmpeg_audio_stream.h"
31 #include "test.h"
32
33 using boost::shared_ptr;
34
35 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
36    with a single piece of content.
37 */
38 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_single)
39 {
40         shared_ptr<Film> film = new_test_film ("best_dcp_frame_rate_test_single");
41         /* Get any piece of content, it doesn't matter what */
42         shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/test.mp4"));
43         film->add_content (content);
44         wait_for_jobs ();
45         
46         /* Run some tests with a limited range of allowed rates */
47         
48         std::list<int> afr;
49         afr.push_back (24);
50         afr.push_back (25);
51         afr.push_back (30);
52         Config::instance()->set_allowed_dcp_frame_rates (afr);
53
54         content->_video_frame_rate = 60;
55         int best = film->playlist()->best_dcp_frame_rate ();
56         FrameRateChange frc = FrameRateChange (60, best);
57         BOOST_CHECK_EQUAL (best, 30);
58         BOOST_CHECK_EQUAL (frc.skip, true);
59         BOOST_CHECK_EQUAL (frc.repeat, 1);
60         BOOST_CHECK_EQUAL (frc.change_speed, false);
61         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
62         
63         content->_video_frame_rate = 50;
64         best = film->playlist()->best_dcp_frame_rate ();
65         frc = FrameRateChange (50, best);
66         BOOST_CHECK_EQUAL (best, 25);
67         BOOST_CHECK_EQUAL (frc.skip, true);
68         BOOST_CHECK_EQUAL (frc.repeat, 1);
69         BOOST_CHECK_EQUAL (frc.change_speed, false);
70         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
71
72         content->_video_frame_rate = 48;
73         best = film->playlist()->best_dcp_frame_rate ();
74         frc = FrameRateChange (48, best);
75         BOOST_CHECK_EQUAL (best, 24);
76         BOOST_CHECK_EQUAL (frc.skip, true);
77         BOOST_CHECK_EQUAL (frc.repeat, 1);
78         BOOST_CHECK_EQUAL (frc.change_speed, false);
79         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
80
81         content->_video_frame_rate = 30;
82         best = film->playlist()->best_dcp_frame_rate ();
83         frc = FrameRateChange (30, 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, false);
88         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
89
90         content->_video_frame_rate = 29.97;
91         best = film->playlist()->best_dcp_frame_rate ();
92         frc = FrameRateChange (29.97, best);
93         BOOST_CHECK_EQUAL (best, 30);
94         BOOST_CHECK_EQUAL (frc.skip, false);
95         BOOST_CHECK_EQUAL (frc.repeat, 1);
96         BOOST_CHECK_EQUAL (frc.change_speed, true);
97         BOOST_CHECK_CLOSE (frc.speed_up, 30 / 29.97, 0.1);
98         
99         content->_video_frame_rate = 25;
100         best = film->playlist()->best_dcp_frame_rate ();
101         frc = FrameRateChange (25, best);
102         BOOST_CHECK_EQUAL (best, 25);
103         BOOST_CHECK_EQUAL (frc.skip, false);
104         BOOST_CHECK_EQUAL (frc.repeat, 1);
105         BOOST_CHECK_EQUAL (frc.change_speed, false);
106         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
107
108         content->_video_frame_rate = 24;
109         best = film->playlist()->best_dcp_frame_rate ();
110         frc = FrameRateChange (24, best);
111         BOOST_CHECK_EQUAL (best, 24);
112         BOOST_CHECK_EQUAL (frc.skip, false);
113         BOOST_CHECK_EQUAL (frc.repeat, 1);
114         BOOST_CHECK_EQUAL (frc.change_speed, false);
115         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
116
117         content->_video_frame_rate = 14.5;
118         best = film->playlist()->best_dcp_frame_rate ();
119         frc = FrameRateChange (14.5, best);
120         BOOST_CHECK_EQUAL (best, 30);
121         BOOST_CHECK_EQUAL (frc.skip, false);
122         BOOST_CHECK_EQUAL (frc.repeat, 2);
123         BOOST_CHECK_EQUAL (frc.change_speed, true);
124         BOOST_CHECK_CLOSE (frc.speed_up, 15 / 14.5, 0.1);
125
126         content->_video_frame_rate = 12.6;
127         best = film->playlist()->best_dcp_frame_rate ();
128         frc = FrameRateChange (12.6, best);
129         BOOST_CHECK_EQUAL (best, 25);
130         BOOST_CHECK_EQUAL (frc.skip, false);
131         BOOST_CHECK_EQUAL (frc.repeat, 2);
132         BOOST_CHECK_EQUAL (frc.change_speed, true);
133         BOOST_CHECK_CLOSE (frc.speed_up, 25 / 25.2, 0.1);
134
135         content->_video_frame_rate = 12.4;
136         best = film->playlist()->best_dcp_frame_rate ();
137         frc = FrameRateChange (12.4, best);
138         BOOST_CHECK_EQUAL (best, 25);
139         BOOST_CHECK_EQUAL (frc.skip, false);
140         BOOST_CHECK_EQUAL (frc.repeat, 2);
141         BOOST_CHECK_EQUAL (frc.change_speed, true);
142         BOOST_CHECK_CLOSE (frc.speed_up, 25 / 24.8, 0.1);
143
144         content->_video_frame_rate = 12;
145         best = film->playlist()->best_dcp_frame_rate ();
146         frc = FrameRateChange (12, best);
147         BOOST_CHECK_EQUAL (best, 24);
148         BOOST_CHECK_EQUAL (frc.skip, false);
149         BOOST_CHECK_EQUAL (frc.repeat, 2);
150         BOOST_CHECK_EQUAL (frc.change_speed, false);
151         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
152
153         /* Now add some more rates and see if it will use them
154            in preference to skip/repeat.
155         */
156
157         afr.push_back (48);
158         afr.push_back (50);
159         afr.push_back (60);
160         Config::instance()->set_allowed_dcp_frame_rates (afr);
161
162         content->_video_frame_rate = 60;
163         best = film->playlist()->best_dcp_frame_rate ();
164         frc = FrameRateChange (60, best);
165         BOOST_CHECK_EQUAL (best, 60);
166         BOOST_CHECK_EQUAL (frc.skip, false);
167         BOOST_CHECK_EQUAL (frc.repeat, 1);
168         BOOST_CHECK_EQUAL (frc.change_speed, false);
169         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
170         
171         content->_video_frame_rate = 50;
172         best = film->playlist()->best_dcp_frame_rate ();
173         frc = FrameRateChange (50, best);
174         BOOST_CHECK_EQUAL (best, 50);
175         BOOST_CHECK_EQUAL (frc.skip, false);
176         BOOST_CHECK_EQUAL (frc.repeat, 1);
177         BOOST_CHECK_EQUAL (frc.change_speed, false);
178         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
179
180         content->_video_frame_rate = 48;
181         best = film->playlist()->best_dcp_frame_rate ();
182         frc = FrameRateChange (48, best);
183         BOOST_CHECK_EQUAL (best, 48);
184         BOOST_CHECK_EQUAL (frc.skip, false);
185         BOOST_CHECK_EQUAL (frc.repeat, 1);
186         BOOST_CHECK_EQUAL (frc.change_speed, false);
187         BOOST_CHECK_CLOSE (frc.speed_up, 1, 0.1);
188
189         /* Check some out-there conversions (not the best) */
190         
191         frc = FrameRateChange (14.99, 24);
192         BOOST_CHECK_EQUAL (frc.skip, false);
193         BOOST_CHECK_EQUAL (frc.repeat, 2);
194         BOOST_CHECK_EQUAL (frc.change_speed, true);
195         BOOST_CHECK_CLOSE (frc.speed_up, 24 / (2 * 14.99), 0.1);
196
197         /* Check some conversions with limited DCP targets */
198
199         afr.clear ();
200         afr.push_back (24);
201         Config::instance()->set_allowed_dcp_frame_rates (afr);
202
203         content->_video_frame_rate = 25;
204         best = film->playlist()->best_dcp_frame_rate ();
205         frc = FrameRateChange (25, best);
206         BOOST_CHECK_EQUAL (best, 24);
207         BOOST_CHECK_EQUAL (frc.skip, false);
208         BOOST_CHECK_EQUAL (frc.repeat, 1);
209         BOOST_CHECK_EQUAL (frc.change_speed, true);
210         BOOST_CHECK_CLOSE (frc.speed_up, 24.0 / 25, 0.1);
211 }
212
213 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
214    with two pieces of content.
215 */
216 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_double)
217 {
218         shared_ptr<Film> film = new_test_film ("best_dcp_frame_rate_test_double");
219         /* Get any old content, it doesn't matter what */
220         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
221         film->add_content (A);
222         shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
223         film->add_content (B);
224         wait_for_jobs ();
225
226         /* Run some tests with a limited range of allowed rates */
227         
228         std::list<int> afr;
229         afr.push_back (24);
230         afr.push_back (25);
231         afr.push_back (30);
232         Config::instance()->set_allowed_dcp_frame_rates (afr);
233
234         A->_video_frame_rate = 30;
235         B->_video_frame_rate = 24;
236         BOOST_CHECK_EQUAL (film->playlist()->best_dcp_frame_rate(), 25);
237 }
238
239
240 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
241 {
242         shared_ptr<Film> film = new_test_film ("audio_sampling_rate_test");
243         /* Get any piece of content, it doesn't matter what */
244         shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/test.mp4"));
245         film->examine_and_add_content (content);
246         wait_for_jobs ();
247         
248         std::list<int> afr;
249         afr.push_back (24);
250         afr.push_back (25);
251         afr.push_back (30);
252         Config::instance()->set_allowed_dcp_frame_rates (afr);
253
254         content->_video_frame_rate = 24;
255         film->set_video_frame_rate (24);
256         content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
257         BOOST_CHECK_EQUAL (content->resampled_audio_frame_rate(), 48000);
258
259         content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
260         BOOST_CHECK_EQUAL (content->resampled_audio_frame_rate(), 48000);
261
262         content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 80000, 0)));
263         BOOST_CHECK_EQUAL (content->resampled_audio_frame_rate(), 96000);
264
265         content->_video_frame_rate = 23.976;
266         film->set_video_frame_rate (24);
267         content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
268         BOOST_CHECK_EQUAL (content->resampled_audio_frame_rate(), 47952);
269
270         content->_video_frame_rate = 29.97;
271         film->set_video_frame_rate (30);
272         BOOST_CHECK_EQUAL (film->video_frame_rate (), 30);
273         content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
274         BOOST_CHECK_EQUAL (content->resampled_audio_frame_rate(), 47952);
275
276         content->_video_frame_rate = 25;
277         film->set_video_frame_rate (24);
278         content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 48000, 0)));
279         BOOST_CHECK_EQUAL (content->resampled_audio_frame_rate(), 50000);
280
281         content->_video_frame_rate = 25;
282         film->set_video_frame_rate (24);
283         content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 44100, 0)));
284         BOOST_CHECK_EQUAL (content->resampled_audio_frame_rate(), 50000);
285
286         /* Check some out-there conversions (not the best) */
287         
288         content->_video_frame_rate = 14.99;
289         film->set_video_frame_rate (25);
290         content->set_audio_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream ("a", 42, 16000, 0)));
291         /* The FrameRateChange within resampled_audio_frame_rate should choose to double-up
292            the 14.99 fps video to 30 and then run it slow at 25.
293         */
294         BOOST_CHECK_EQUAL (content->resampled_audio_frame_rate(), rint (48000 * 2 * 14.99 / 25));
295 }
296