Merge master.
[dcpomatic.git] / test / trimmer_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 using boost::shared_ptr;
21
22 shared_ptr<const Image> trimmer_test_last_video;
23 int trimmer_test_video_frames = 0;
24 shared_ptr<const AudioBuffers> trimmer_test_last_audio;
25
26 void
27 trimmer_test_video_helper (shared_ptr<const Image> image, bool, shared_ptr<Subtitle>)
28 {
29         trimmer_test_last_video = image;
30         ++trimmer_test_video_frames;
31 }
32
33 void
34 trimmer_test_audio_helper (shared_ptr<const AudioBuffers> audio)
35 {
36         trimmer_test_last_audio = audio;
37 }
38
39 BOOST_AUTO_TEST_CASE (trimmer_passthrough_test)
40 {
41         Trimmer trimmer (shared_ptr<Log> (), 0, 0, 200, 48000, 25, 25);
42         trimmer.Video.connect (bind (&trimmer_test_video_helper, _1, _2, _3));
43         trimmer.Audio.connect (bind (&trimmer_test_audio_helper, _1));
44
45         shared_ptr<SimpleImage> video (new SimpleImage (PIX_FMT_RGB24, libdcp::Size (1998, 1080), true));
46         shared_ptr<AudioBuffers> audio (new AudioBuffers (6, 42 * 1920));
47
48         trimmer.process_video (video, false, shared_ptr<Subtitle> ());
49         trimmer.process_audio (audio);
50
51         BOOST_CHECK_EQUAL (video.get(), trimmer_test_last_video.get());
52         BOOST_CHECK_EQUAL (audio.get(), trimmer_test_last_audio.get());
53         BOOST_CHECK_EQUAL (audio->frames(), trimmer_test_last_audio->frames());
54 }
55
56
57 /** Test the audio handling of the Trimmer */
58 BOOST_AUTO_TEST_CASE (trimmer_audio_test)
59 {
60         Trimmer trimmer (shared_ptr<Log> (), 25, 75, 200, 48000, 25, 25);
61
62         trimmer.Audio.connect (bind (&trimmer_test_audio_helper, _1));
63
64         /* 21 video frames-worth of audio frames; should be completely stripped */
65         trimmer_test_last_audio.reset ();
66         shared_ptr<AudioBuffers> audio (new AudioBuffers (6, 21 * 1920));
67         trimmer.process_audio (audio);
68         BOOST_CHECK (trimmer_test_last_audio == 0);
69
70         /* 42 more video frames-worth, 4 should be stripped from the start */
71         audio.reset (new AudioBuffers (6, 42 * 1920));
72         trimmer.process_audio (audio);
73         BOOST_CHECK (trimmer_test_last_audio);
74         BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 38 * 1920);
75
76         /* 42 more video frames-worth, should be kept as-is */
77         trimmer_test_last_audio.reset ();
78         audio.reset (new AudioBuffers (6, 42 * 1920));
79         trimmer.process_audio (audio);
80         BOOST_CHECK (trimmer_test_last_audio);
81         BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 42 * 1920);
82
83         /* 25 more video frames-worth, 5 should be trimmed from the end */
84         trimmer_test_last_audio.reset ();
85         audio.reset (new AudioBuffers (6, 25 * 1920));
86         trimmer.process_audio (audio);
87         BOOST_CHECK (trimmer_test_last_audio);
88         BOOST_CHECK_EQUAL (trimmer_test_last_audio->frames(), 20 * 1920);
89
90         /* Now some more; all should be trimmed */
91         trimmer_test_last_audio.reset ();
92         audio.reset (new AudioBuffers (6, 100 * 1920));
93         trimmer.process_audio (audio);
94         BOOST_CHECK (trimmer_test_last_audio == 0);
95 }
96
97 BOOST_AUTO_TEST_CASE (trim_end_test)
98 {
99         Trimmer trimmer (shared_ptr<Log> (), 0, 75, 200, 48000, 25, 25);
100
101         shared_ptr<SimpleImage> image (new SimpleImage (PIX_FMT_RGB24, libdcp::Size (256, 256), true));
102
103         trimmer.Video.connect (bind (&trimmer_test_video_helper, _1, _2, _3));
104         trimmer_test_video_frames = 0;
105         for (int i = 0; i < 200; ++i) {
106                 trimmer.process_video (image, false, shared_ptr<Subtitle> ());
107         }
108
109         BOOST_CHECK_EQUAL (trimmer_test_video_frames, 125);
110 }