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