Rename TYPE_DEBUG_PLAYER to TYPE_DEBUG_VIDEO_VIEW.
[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 "content.h"
24 #include "film.h"
25 #include "compose.hpp"
26 #include <cmath>
27
28 #include "i18n.h"
29
30 using std::string;
31 using boost::shared_ptr;
32
33 static bool
34 about_equal (double a, double b)
35 {
36         return (fabs (a - b) < VIDEO_FRAME_RATE_EPSILON);
37 }
38
39 FrameRateChange::FrameRateChange ()
40         : source (24)
41         , dcp (24)
42         , skip (false)
43         , repeat (1)
44         , change_speed (false)
45         , speed_up (1)
46 {
47
48 }
49
50 FrameRateChange::FrameRateChange (double source_, int dcp_)
51         : skip (false)
52         , repeat (1)
53         , change_speed (false)
54 {
55         construct (source_, dcp_);
56 }
57
58 void
59 FrameRateChange::construct (double source_, int dcp_)
60 {
61         source = source_;
62         dcp = dcp_;
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 FrameRateChange::FrameRateChange (shared_ptr<const Film> film, shared_ptr<const Content> content)
82         : skip (false)
83         , repeat (1)
84 {
85         construct (content->active_video_frame_rate(film), film->video_frame_rate());
86 }
87
88 FrameRateChange::FrameRateChange (shared_ptr<const Film> film, Content const * content)
89         : skip (false)
90         , repeat (1)
91 {
92         construct (content->active_video_frame_rate(film), film->video_frame_rate());
93 }
94
95 string
96 FrameRateChange::description () const
97 {
98         string description;
99
100         if (!skip && repeat == 1 && !change_speed) {
101                 description = _("Content and DCP have the same rate.\n");
102         } else {
103                 if (skip) {
104                         description = _("DCP will use every other frame of the content.\n");
105                 } else if (repeat == 2) {
106                         description = _("Each content frame will be doubled in the DCP.\n");
107                 } else if (repeat > 2) {
108                         description = String::compose (_("Each content frame will be repeated %1 more times in the DCP.\n"), repeat - 1);
109                 }
110
111                 if (change_speed) {
112                         double const pc = dcp * 100 / (source * factor());
113                         char buffer[256];
114                         snprintf (buffer, sizeof(buffer), _("DCP will run at %.1f%% of the content speed.\n"), pc);
115                         description += buffer;
116                 }
117         }
118
119         return description;
120 }