More float -> double.
[dcpomatic.git] / src / lib / frame_rate_change.cc
1 /*
2     Copyright (C) 2012-2015 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 <cmath>
21 #include "frame_rate_change.h"
22 #include "compose.hpp"
23
24 #include "i18n.h"
25
26 using std::string;
27
28 static bool
29 about_equal (double a, double b)
30 {
31         /* A film of F seconds at f FPS will be Ff frames;
32            Consider some delta FPS d, so if we run the same
33            film at (f + d) FPS it will last F(f + d) seconds.
34
35            Hence the difference in length over the length of the film will
36            be F(f + d) - Ff frames
37             = Ff + Fd - Ff frames
38             = Fd frames
39             = Fd/f seconds
40
41            So if we accept a difference of 1 frame, ie 1/f seconds, we can
42            say that
43
44            1/f = Fd/f
45         ie 1 = Fd
46         ie d = 1/F
47
48            So for a 3hr film, ie F = 3 * 60 * 60 = 10800, the acceptable
49            FPS error is 1/F ~= 0.0001 ~= 10-e4
50         */
51
52         return (fabs (a - b) < 1e-4);
53 }
54
55
56 FrameRateChange::FrameRateChange (double source_, int dcp_)
57         : source (source_)
58         , dcp (dcp_)
59         , skip (false)
60         , repeat (1)
61         , change_speed (false)
62 {
63         if (fabs (source / 2.0 - dcp) < fabs (source - dcp)) {
64                 /* The difference between source and DCP frame rate will be lower
65                    (i.e. better) if we skip.
66                 */
67                 skip = true;
68         } else if (fabs (source * 2 - dcp) < fabs (source - dcp)) {
69                 /* The difference between source and DCP frame rate would be better
70                    if we repeated each frame once; it may be better still if we
71                    repeated more than once.  Work out the required repeat.
72                 */
73                 repeat = round (dcp / source);
74         }
75
76         speed_up = dcp / (source * factor());
77         change_speed = !about_equal (speed_up, 1.0);
78 }
79
80 string
81 FrameRateChange::description () const
82 {
83         string description;
84
85         if (!skip && repeat == 1 && !change_speed) {
86                 description = _("Content and DCP have the same rate.\n");
87         } else {
88                 if (skip) {
89                         description = _("DCP will use every other frame of the content.\n");
90                 } else if (repeat == 2) {
91                         description = _("Each content frame will be doubled in the DCP.\n");
92                 } else if (repeat > 2) {
93                         description = String::compose (_("Each content frame will be repeated %1 more times in the DCP.\n"), repeat - 1);
94                 }
95
96                 if (change_speed) {
97                         double const pc = dcp * 100 / (source * factor());
98                         description += String::compose (_("DCP will run at %1%% of the content speed.\n"), pc);
99                 }
100         }
101
102         return description;
103 }