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