summaryrefslogtreecommitdiff
path: root/src/lib/frame_rate_change.cc
blob: 6057f9ca3a9641c2f4284d1ecd382fc19257c85f (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
104
105
106
107
108
109
/*
    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic 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.

    DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "content.h"
#include "film.h"
#include "frame_rate_change.h"
#include <cmath>

#include "i18n.h"


using std::shared_ptr;
using std::string;


FrameRateChange::FrameRateChange()
{

}


FrameRateChange::FrameRateChange(double source, int dcp)
{
	_source = source;
	_dcp = dcp;

	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 = round(source / dcp) - 1;
	} 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());

	auto about_equal = [](double a, double b) {
		return fabs(a - b) < VIDEO_FRAME_RATE_EPSILON;
	};

	_change_speed = !about_equal(_speed_up, 1.0);
}


FrameRateChange::FrameRateChange(shared_ptr<const Film> film, shared_ptr<const Content> content)
	: FrameRateChange(content->active_video_frame_rate(film), film->video_frame_rate())
{

}


FrameRateChange::FrameRateChange(shared_ptr<const Film> film, Content const * content)
	: FrameRateChange(content->active_video_frame_rate(film), film->video_frame_rate())
{

}


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

	if (_skip == 0 && _repeat == 1 && !_change_speed) {
		description = _("Content and DCP have the same rate.\n");
	} else {
		if (_skip == 1) {
			description = _("DCP will use every other frame of the content.\n");
		} else if (_skip >= 2) {
			description = fmt::format(_("DCP will contain 1 out of every {} frames of the content.\n"), _skip + 1);
		} else if (_repeat == 2) {
			description = _("Each content frame will be doubled in the DCP.\n");
		} else if (_repeat > 2) {
			description = fmt::format(_("Each content frame will be repeated {} more times in the DCP.\n"), _repeat - 1);
		}

		if (_change_speed) {
			double const pc = _dcp * 100 / (_source * factor());
			char buffer[256];
			snprintf(buffer, sizeof(buffer), _("DCP will run at %.1f%% of the content speed.\n"), pc);
			description += buffer;
		}
	}

	return description;
}