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