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