a6eb0762927cd908eeb728a167d4d0a546345c0d
[dcpomatic.git] / src / lib / subrip_content.cc
1 /*
2     Copyright (C) 2014-2015 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 "subrip_content.h"
21 #include "util.h"
22 #include "subrip.h"
23 #include "film.h"
24 #include "font.h"
25 #include "raw_convert.h"
26 #include <libxml++/libxml++.h>
27 #include <iostream>
28
29 #include "i18n.h"
30
31 using std::string;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::lexical_cast;
35
36 std::string const SubRipContent::font_id = "font";
37
38 int const SubRipContentProperty::SUBTITLE_COLOUR = 300;
39 int const SubRipContentProperty::SUBTITLE_OUTLINE = 301;
40 int const SubRipContentProperty::SUBTITLE_OUTLINE_COLOUR = 302;
41
42 SubRipContent::SubRipContent (shared_ptr<const Film> film, boost::filesystem::path path)
43         : Content (film, path)
44         , SubtitleContent (film, path)
45         , _colour (255, 255, 255)
46         , _outline (false)
47         , _outline_colour (0, 0, 0)
48 {
49
50 }
51
52 SubRipContent::SubRipContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
53         : Content (film, node)
54         , SubtitleContent (film, node, version)
55         , _length (node->number_child<ContentTime::Type> ("Length"))
56         , _frame_rate (node->optional_number_child<double>("SubtitleFrameRate"))
57         , _colour (
58                 node->optional_number_child<int>("Red").get_value_or(255),
59                 node->optional_number_child<int>("Green").get_value_or(255),
60                 node->optional_number_child<int>("Blue").get_value_or(255)
61                 )
62         , _outline (node->optional_bool_child("Outline").get_value_or(false))
63         , _outline_colour (
64                 node->optional_number_child<int>("OutlineRed").get_value_or(255),
65                 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
66                 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
67                 )
68 {
69
70 }
71
72 void
73 SubRipContent::examine (boost::shared_ptr<Job> job)
74 {
75         Content::examine (job);
76         SubRip s (shared_from_this ());
77
78         /* Default to turning these subtitles on */
79         set_use_subtitles (true);
80
81         boost::mutex::scoped_lock lm (_mutex);
82         _length = s.length ();
83         add_font (shared_ptr<Font> (new Font (font_id)));
84 }
85
86 string
87 SubRipContent::summary () const
88 {
89         return path_summary() + " " + _("[subtitles]");
90 }
91
92 string
93 SubRipContent::technical_summary () const
94 {
95         return Content::technical_summary() + " - " + _("SubRip subtitles");
96 }
97
98 void
99 SubRipContent::as_xml (xmlpp::Node* node) const
100 {
101         node->add_child("Type")->add_child_text ("SubRip");
102         Content::as_xml (node);
103         SubtitleContent::as_xml (node);
104         node->add_child("Length")->add_child_text (raw_convert<string> (_length.get ()));
105         node->add_child("Red")->add_child_text (raw_convert<string> (_colour.r));
106         node->add_child("Green")->add_child_text (raw_convert<string> (_colour.g));
107         node->add_child("Blue")->add_child_text (raw_convert<string> (_colour.b));
108         node->add_child("Outline")->add_child_text (raw_convert<string> (_outline));
109         node->add_child("OutlineRed")->add_child_text (raw_convert<string> (_outline_colour.r));
110         node->add_child("OutlineGreen")->add_child_text (raw_convert<string> (_outline_colour.g));
111         node->add_child("OutlineBlue")->add_child_text (raw_convert<string> (_outline_colour.b));
112 }
113
114 DCPTime
115 SubRipContent::full_length () const
116 {
117         FrameRateChange const frc (subtitle_video_frame_rate(), film()->video_frame_rate ());
118         return DCPTime (_length, frc);
119 }
120
121 void
122 SubRipContent::set_subtitle_video_frame_rate (int r)
123 {
124         {
125                 boost::mutex::scoped_lock lm (_mutex);
126                 _frame_rate = r;
127         }
128
129         signal_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
130 }
131
132 double
133 SubRipContent::subtitle_video_frame_rate () const
134 {
135         {
136                 boost::mutex::scoped_lock lm (_mutex);
137                 if (_frame_rate) {
138                         return _frame_rate.get ();
139                 }
140         }
141
142         /* No frame rate specified, so assume this content has been
143            prepared for any concurrent video content.
144         */
145         return film()->active_frame_rate_change(position()).source;
146 }
147
148 void
149 SubRipContent::set_colour (dcp::Colour colour)
150 {
151         {
152                 boost::mutex::scoped_lock lm (_mutex);
153                 if (_colour == colour) {
154                         return;
155                 }
156
157                 _colour = colour;
158         }
159
160         signal_changed (SubRipContentProperty::SUBTITLE_COLOUR);
161 }
162
163 void
164 SubRipContent::set_outline (bool o)
165 {
166         {
167                 boost::mutex::scoped_lock lm (_mutex);
168                 if (_outline == o) {
169                         return;
170                 }
171
172                 _outline = o;
173         }
174
175         signal_changed (SubRipContentProperty::SUBTITLE_OUTLINE);
176 }
177
178 void
179 SubRipContent::set_outline_colour (dcp::Colour colour)
180 {
181         {
182                 boost::mutex::scoped_lock lm (_mutex);
183                 if (_outline_colour == colour) {
184                         return;
185                 }
186
187                 _outline_colour = colour;
188         }
189
190         signal_changed (SubRipContentProperty::SUBTITLE_OUTLINE_COLOUR);
191 }