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