Move some stuff into FilmViewer::dcp().
[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
22 /** @file  test/frame_rate_test.cc
23  *  @brief Tests for FrameRateChange and the computation of the best
24  *  frame rate for the DCP.
25  *  @ingroup feature
26  */
27
28
29 #include "lib/audio_content.h"
30 #include "lib/config.h"
31 #include "lib/ffmpeg_audio_stream.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/film.h"
34 #include "lib/frame_rate_change.h"
35 #include "lib/playlist.h"
36 #include "lib/video_content.h"
37 #include "test.h"
38 #include <boost/test/unit_test.hpp>
39
40
41
42 /* Test Playlist::best_dcp_frame_rate and FrameRateChange
43    with a single piece of content.
44 */
45 BOOST_AUTO_TEST_CASE (best_dcp_frame_rate_test_single)
46 {
47         auto content = std::make_shared<FFmpegContent>("test/data/test.mp4");
48         auto film = new_test_film("best_dcp_frame_rate_test_single", { content });
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         auto 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->playlist()->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->playlist()->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->playlist()->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         auto A = std::make_shared<FFmpegContent>("test/data/test.mp4");
223         auto B = std::make_shared<FFmpegContent>("test/data/test.mp4");
224         auto film = new_test_film("best_dcp_frame_rate_test_double", { A, B });
225
226         /* Run some tests with a limited range of allowed rates */
227
228         std::list<int> afr = { 24, 25, 30 };
229         Config::instance()->set_allowed_dcp_frame_rates (afr);
230
231         A->_video_frame_rate = 30;
232         B->_video_frame_rate = 24;
233         BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 25);
234
235         A->_video_frame_rate = 24;
236         B->_video_frame_rate = 24;
237         BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 24);
238
239         A->_video_frame_rate = 24;
240         B->_video_frame_rate = 48;
241         BOOST_CHECK_EQUAL (film->best_video_frame_rate(), 24);
242 }
243
244 BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
245 {
246         auto content = std::make_shared<FFmpegContent>("test/data/test.mp4");
247         auto film = new_test_film("audio_sampling_rate_test", { content });
248
249         std::list<int> afr = { 24, 25, 30 };
250         Config::instance()->set_allowed_dcp_frame_rates (afr);
251
252         auto stream = std::make_shared<FFmpegAudioStream>("foo", 0, 0, 0, 0, 0);
253         content->audio.reset (new AudioContent (content.get()));
254         content->audio->add_stream (stream);
255         content->_video_frame_rate = 24;
256         film->set_video_frame_rate (24);
257         stream->_frame_rate = 48000;
258         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(film), 48000);
259
260         stream->_frame_rate = 44100;
261         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(film), 48000);
262
263         stream->_frame_rate = 80000;
264         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(film), 48000);
265
266         content->_video_frame_rate = 23.976;
267         film->set_video_frame_rate (24);
268         stream->_frame_rate = 48000;
269         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(film), 47952);
270
271         content->_video_frame_rate = 29.97;
272         film->set_video_frame_rate (30);
273         BOOST_CHECK_EQUAL (film->video_frame_rate (), 30);
274         stream->_frame_rate = 48000;
275         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(film), 47952);
276
277         content->_video_frame_rate = 25;
278         film->set_video_frame_rate (24);
279         stream->_frame_rate = 48000;
280         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(film), 50000);
281
282         content->_video_frame_rate = 25;
283         film->set_video_frame_rate (24);
284         stream->_frame_rate = 44100;
285         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(film), 50000);
286
287         /* Check some out-there conversions (not the best) */
288
289         content->_video_frame_rate = 14.99;
290         film->set_video_frame_rate (25);
291         stream->_frame_rate = 16000;
292         /* The FrameRateChange within resampled_frame_rate should choose to double-up
293            the 14.99 fps video to 30 and then run it slow at 25.
294         */
295         BOOST_CHECK_EQUAL (content->audio->resampled_frame_rate(film), lrint (48000 * 2 * 14.99 / 25));
296 }