Rename SafeStringStream -> locked_stringstream. Bump deps for removal of stringstream.
[dcpomatic.git] / src / lib / frame_rate_change.cc
1 /*
2     Copyright (C) 2012-2016 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 #include "frame_rate_change.h"
22 #include "types.h"
23 #include "compose.hpp"
24 #include <cmath>
25
26 #include "i18n.h"
27
28 using std::string;
29
30 static bool
31 about_equal (double a, double b)
32 {
33         return (fabs (a - b) < VIDEO_FRAME_RATE_EPSILON);
34 }
35
36
37 FrameRateChange::FrameRateChange (double source_, int dcp_)
38         : source (source_)
39         , dcp (dcp_)
40         , skip (false)
41         , repeat (1)
42         , change_speed (false)
43 {
44         if (fabs (source / 2.0 - dcp) < fabs (source - dcp)) {
45                 /* The difference between source and DCP frame rate will be lower
46                    (i.e. better) if we skip.
47                 */
48                 skip = true;
49         } else if (fabs (source * 2 - dcp) < fabs (source - dcp)) {
50                 /* The difference between source and DCP frame rate would be better
51                    if we repeated each frame once; it may be better still if we
52                    repeated more than once.  Work out the required repeat.
53                 */
54                 repeat = round (dcp / source);
55         }
56
57         speed_up = dcp / (source * factor());
58         change_speed = !about_equal (speed_up, 1.0);
59 }
60
61 string
62 FrameRateChange::description () const
63 {
64         string description;
65
66         if (!skip && repeat == 1 && !change_speed) {
67                 description = _("Content and DCP have the same rate.\n");
68         } else {
69                 if (skip) {
70                         description = _("DCP will use every other frame of the content.\n");
71                 } else if (repeat == 2) {
72                         description = _("Each content frame will be doubled in the DCP.\n");
73                 } else if (repeat > 2) {
74                         description = String::compose (_("Each content frame will be repeated %1 more times in the DCP.\n"), repeat - 1);
75                 }
76
77                 if (change_speed) {
78                         double const pc = dcp * 100 / (source * factor());
79                         description += String::compose (_("DCP will run at %1%% of the content speed.\n"), pc);
80                 }
81         }
82
83         return description;
84 }