No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / test / frame_rate_test.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  test/frame_rate_test.cc
22  *  @brief Tests for FrameRateChange and the computation of the best
23  *  frame rate for the DCP.
24  */
25
26 #include <boost/test/unit_test.hpp>
27 #include "lib/film.h"
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"
35 #include "test.h"
36
37 using boost::shared_ptr;
38
39 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
40    with a single piece of content.
41 */
42 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_single)
43 {
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);
48         wait_for_jobs ();
49
50         /* Run some tests with a limited range of allowed rates */
51
52         std::list<int> afr;
53         afr.push_back (24);
54         afr.push_back (25);
55         afr.push_back (30);
56         Config::instance()->set_allowed_dcp_frame_rates (afr);
57
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);
66
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);
75
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);
84
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);
93
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);
102
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);
111
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);
120
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);
129
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);
138
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);
147
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);
156
157         /* Now add some more rates and see if it will use them
158            in preference to skip/repeat.
159         */
160
161         afr.push_back (48);
162         afr.push_back (50);
163         afr.push_back (60);
164         Config::instance()->set_allowed_dcp_frame_rates (afr);
165
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);
174
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);
183
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);
192
193         /* Check some out-there conversions (not the best) */
194
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);
200
201         /* Check some conversions with limited DCP targets */
202
203         afr.clear ();
204         afr.push_back (24);
205         Config::instance()->set_allowed_dcp_frame_rates (afr);
206
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);
215 }
216
217 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
218    with two pieces of content.
219 */
220 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_double)
221 {
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);
228         wait_for_jobs ();
229
230         /* Run some tests with a limited range of allowed rates */
231
232         std::list<int> afr;
233         afr.push_back (24);
234         afr.push_back (25);
235         afr.push_back (30);
236         Config::instance()->set_allowed_dcp_frame_rates (afr);
237
238         A->_video_frame_rate = 30;
239         B->_video_frame_rate = 24;
240         BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 25);
241
242         A->_video_frame_rate = 24;
243         B->_video_frame_rate = 24;
244         BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 24);
245
246         A->_video_frame_rate = 24;
247         B->_video_frame_rate = 48;
248         BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 24);
249 }
250
251 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
252 {
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);
257         wait_for_jobs ();
258
259         std::list<int> afr;
260         afr.push_back (24);
261         afr.push_back (25);
262         afr.push_back (30);
263         Config::instance()->set_allowed_dcp_frame_rates (afr);
264
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);
272
273         stream->_frame_rate = 44100;
274         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 48000);
275
276         stream->_frame_rate = 80000;
277         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), 96000);
278
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);
283
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);
289
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);
294
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);
299
300         /* Check some out-there conversions (not the best) */
301
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.
307         */
308         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(), lrint (48000 * 2 * 14.99 / 25));
309 }