C++11 tidying.
[dcpomatic.git] / test / ffmpeg_pts_offset_test.cc
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file  test/ffmpeg_pts_offset_test.cc
23  *  @brief Check the computation of _pts_offset in FFmpegDecoder.
24  *  @ingroup selfcontained
25  */
26
27
28 #include <boost/test/unit_test.hpp>
29 #include "lib/film.h"
30 #include "lib/ffmpeg_decoder.h"
31 #include "lib/ffmpeg_content.h"
32 #include "lib/ffmpeg_audio_stream.h"
33 #include "lib/audio_content.h"
34 #include "test.h"
35
36
37 using std::make_shared;
38 using std::shared_ptr;
39 using namespace dcpomatic;
40
41
42 BOOST_AUTO_TEST_CASE (ffmpeg_pts_offset_test)
43 {
44         auto film = new_test_film ("ffmpeg_pts_offset_test");
45         auto content = make_shared<FFmpegContent>("test/data/test.mp4");
46         film->examine_and_add_content (content);
47         BOOST_REQUIRE (!wait_for_jobs());
48
49         content->audio = make_shared<AudioContent>(content.get());
50         content->audio->add_stream (shared_ptr<FFmpegAudioStream> (new FFmpegAudioStream));
51         content->_video_frame_rate = 24;
52
53         {
54                 /* Sound == video so no offset required */
55                 content->_first_video = ContentTime ();
56                 content->ffmpeg_audio_streams().front()->first_audio = ContentTime ();
57                 FFmpegDecoder decoder (film, content, false);
58                 BOOST_CHECK_EQUAL (decoder._pts_offset.get(), 0);
59         }
60
61         {
62                 /* Common offset should be removed */
63                 content->_first_video = ContentTime::from_seconds (600);
64                 content->ffmpeg_audio_streams().front()->first_audio = ContentTime::from_seconds (600);
65                 FFmpegDecoder decoder (film, content, false);
66                 BOOST_CHECK_EQUAL (decoder._pts_offset.get(), ContentTime::from_seconds(-600).get());
67         }
68
69         {
70                 /* Video is on a frame boundary */
71                 content->_first_video = ContentTime::from_frames (1, 24);
72                 content->ffmpeg_audio_streams().front()->first_audio = ContentTime ();
73                 FFmpegDecoder decoder (film, content, false);
74                 BOOST_CHECK_EQUAL (decoder._pts_offset.get(), 0);
75         }
76
77         {
78                 /* Video is off a frame boundary */
79                 double const frame = 1.0 / 24.0;
80                 content->_first_video = ContentTime::from_seconds (frame + 0.0215);
81                 content->ffmpeg_audio_streams().front()->first_audio = ContentTime ();
82                 FFmpegDecoder decoder (film, content, false);
83                 BOOST_CHECK_CLOSE (decoder._pts_offset.seconds(), (frame - 0.0215), 0.00001);
84         }
85
86         {
87                 /* Video is off a frame boundary and both have a common offset */
88                 double const frame = 1.0 / 24.0;
89                 content->_first_video = ContentTime::from_seconds (frame + 0.0215 + 4.1);
90                 content->ffmpeg_audio_streams().front()->first_audio = ContentTime::from_seconds (4.1);
91                 FFmpegDecoder decoder (film, content, false);
92                 BOOST_CHECK_CLOSE (decoder._pts_offset.seconds(), (frame - 0.0215) - 4.1, 0.1);
93         }
94 }