Merge branch 'master' into 1.0
[dcpomatic.git] / src / lib / video_content.cc
1 /*
2     Copyright (C) 2013 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 <libcxml/cxml.h>
21 #include "video_content.h"
22 #include "video_decoder.h"
23
24 #include "i18n.h"
25
26 int const VideoContentProperty::VIDEO_LENGTH = 0;
27 int const VideoContentProperty::VIDEO_SIZE = 1;
28 int const VideoContentProperty::VIDEO_FRAME_RATE = 2;
29
30 using std::string;
31 using std::stringstream;
32 using std::setprecision;
33 using boost::shared_ptr;
34 using boost::lexical_cast;
35
36 VideoContent::VideoContent (boost::filesystem::path f)
37         : Content (f)
38         , _video_length (0)
39 {
40
41 }
42
43 VideoContent::VideoContent (shared_ptr<const cxml::Node> node)
44         : Content (node)
45 {
46         _video_length = node->number_child<ContentVideoFrame> ("VideoLength");
47         _video_size.width = node->number_child<int> ("VideoWidth");
48         _video_size.height = node->number_child<int> ("VideoHeight");
49         _video_frame_rate = node->number_child<float> ("VideoFrameRate");
50 }
51
52 VideoContent::VideoContent (VideoContent const & o)
53         : Content (o)
54         , _video_length (o._video_length)
55         , _video_size (o._video_size)
56         , _video_frame_rate (o._video_frame_rate)
57 {
58
59 }
60
61 void
62 VideoContent::as_xml (xmlpp::Node* node) const
63 {
64         boost::mutex::scoped_lock lm (_mutex);
65         node->add_child("VideoLength")->add_child_text (lexical_cast<string> (_video_length));
66         node->add_child("VideoWidth")->add_child_text (lexical_cast<string> (_video_size.width));
67         node->add_child("VideoHeight")->add_child_text (lexical_cast<string> (_video_size.height));
68         node->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
69 }
70
71 void
72 VideoContent::take_from_video_decoder (shared_ptr<VideoDecoder> d)
73 {
74         /* These decoder calls could call other content methods which take a lock on the mutex */
75         libdcp::Size const vs = d->native_size ();
76         float const vfr = d->video_frame_rate ();
77         
78         {
79                 boost::mutex::scoped_lock lm (_mutex);
80                 _video_size = vs;
81                 _video_frame_rate = vfr;
82         }
83         
84         signal_changed (VideoContentProperty::VIDEO_SIZE);
85         signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
86 }
87
88
89 string
90 VideoContent::information () const
91 {
92         if (video_size().width == 0 || video_size().height == 0) {
93                 return "";
94         }
95         
96         stringstream s;
97
98         s << String::compose (
99                 _("%1x%2 pixels (%3:1)"),
100                 video_size().width,
101                 video_size().height,
102                 setprecision (3), float (video_size().width) / video_size().height
103                 );
104         
105         return s.str ();
106 }