summaryrefslogtreecommitdiff
path: root/src/lib/analyse_subtitles_job.cc
blob: 92fb9657c143019486a6ff22b992926ca56f3570 (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
110
111
112
113
114
115
116
117
118
119
120
121
/*
    Copyright (C) 2020 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 "analyse_subtitles_job.h"
#include "playlist.h"
#include "player.h"
#include "subtitle_analysis.h"
#include "bitmap_text.h"
#include "render_text.h"
#include "text_content.h"
#include "image.h"
#include <iostream>

#include "i18n.h"

using std::string;
using boost::shared_ptr;
using boost::weak_ptr;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif

AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content)
	: Job (film)
	, _content (content)
	, _path (_film->subtitle_analysis_path(content))
{
}


string
AnalyseSubtitlesJob::name () const
{
	return _("Analysing subtitles");
}


string
AnalyseSubtitlesJob::json_name () const
{
	return N_("analyse_subtitles");
}


void
AnalyseSubtitlesJob::run ()
{
	shared_ptr<Playlist> playlist (new Playlist());
	shared_ptr<Content> content = _content.lock ();
	DCPOMATIC_ASSERT (content);
	playlist->add (_film, content);

	shared_ptr<Player> player (new Player(_film, playlist));
	player->set_ignore_audio ();
	player->set_fast ();
	player->set_play_referenced ();
	player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2));

	set_progress_unknown ();

	if (!content->text.empty()) {
		while (!player->pass ()) {}
	}

	SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset());
	analysis.write (_path);

	set_progress (1);
	set_state (FINISHED_OK);
}


void
AnalyseSubtitlesJob::analyse (PlayerText text, TextType type)
{
	if (type != TEXT_OPEN_SUBTITLE) {
		return;
	}

	BOOST_FOREACH (BitmapText const& i, text.bitmap) {
		if (!_bounding_box) {
			_bounding_box = i.rectangle;
		} else {
			_bounding_box->extend (i.rectangle);
		}
	}

	if (!text.string.empty()) {
		/* We can provide dummy values for time and frame rate here as they are only used to calculate fades */
		dcp::Size const frame = _film->frame_size();
		BOOST_FOREACH (PositionImage i, render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) {
			dcpomatic::Rect<double> rect (
					double(i.position.x) / frame.width, double(i.position.y) / frame.height,
					double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height
					);
			if (!_bounding_box) {
				_bounding_box = rect;
			} else {
				_bounding_box->extend (rect);
			}
		}
	}
}