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