summaryrefslogtreecommitdiff
path: root/src/lib/frame_rate_change.cc
blob: d0b302bb402a84d76f7c448d4d0405594a32b1e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <cmath>
#include "frame_rate_change.h"
#include "compose.hpp"

#include "i18n.h"

using std::string;

static bool
about_equal (double a, double b)
{
	/* A film of F seconds at f FPS will be Ff frames;
	   Consider some delta FPS d, so if we run the same
	   film at (f + d) FPS it will last F(f + d) seconds.

	   Hence the difference in length over the length of the film will
	   be F(f + d) - Ff frames
	    = Ff + Fd - Ff frames
	    = Fd frames
	    = Fd/f seconds

	   So if we accept a difference of 1 frame, ie 1/f seconds, we can
	   say that

	   1/f = Fd/f
	ie 1 = Fd
	ie d = 1/F

	   So for a 3hr film, ie F = 3 * 60 * 60 = 10800, the acceptable
	   FPS error is 1/F ~= 0.0001 ~= 10-e4
	*/

	return (fabs (a - b) < 1e-4);
}


FrameRateChange::FrameRateChange (double source_, int dcp_)
	: source (source_)
	, dcp (dcp_)
	, skip (false)
	, repeat (1)
	, change_speed (false)
{
	if (fabs (source / 2.0 - dcp) < fabs (source - dcp)) {
		/* The difference between source and DCP frame rate will be lower
		   (i.e. better) if we skip.
		*/
		skip = true;
	} else if (fabs (source * 2 - dcp) < fabs (source - dcp)) {
		/* The difference between source and DCP frame rate would be better
		   if we repeated each frame once; it may be better still if we
		   repeated more than once.  Work out the required repeat.
		*/
		repeat = round (dcp / source);
	}

	speed_up = dcp / (source * factor());
	change_speed = !about_equal (speed_up, 1.0);
}

string
FrameRateChange::description () const
{
	string description;

	if (!skip && repeat == 1 && !change_speed) {
		description = _("Content and DCP have the same rate.\n");
	} else {
		if (skip) {
			description = _("DCP will use every other frame of the content.\n");
		} else if (repeat == 2) {
			description = _("Each content frame will be doubled in the DCP.\n");
		} else if (repeat > 2) {
			description = String::compose (_("Each content frame will be repeated %1 more times in the DCP.\n"), repeat - 1);
		}

		if (change_speed) {
			double const pc = dcp * 100 / (source * factor());
			description += String::compose (_("DCP will run at %1%% of the content speed.\n"), pc);
		}
	}

	return description;
}