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