Basic detach of FFmpegContent, ImageContent, DCPContent
[dcpomatic.git] / src / lib / image_content.cc
1 /*
2     Copyright (C) 2013-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 "image_content.h"
21 #include "video_content.h"
22 #include "image_examiner.h"
23 #include "compose.hpp"
24 #include "film.h"
25 #include "job.h"
26 #include "frame_rate_change.h"
27 #include "exceptions.h"
28 #include "safe_stringstream.h"
29 #include "image_filename_sorter.h"
30 #include <libcxml/cxml.h>
31 #include <libxml++/libxml++.h>
32 #include <boost/foreach.hpp>
33 #include <iostream>
34
35 #include "i18n.h"
36
37 using std::string;
38 using std::cout;
39 using boost::shared_ptr;
40
41 ImageContent::ImageContent (shared_ptr<const Film> film, boost::filesystem::path p)
42         : Content (film)
43         , video (new VideoContent (film))
44 {
45         if (boost::filesystem::is_regular_file (p) && valid_image_file (p)) {
46                 _paths.push_back (p);
47         } else {
48                 for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
49                         if (boost::filesystem::is_regular_file (i->path()) && valid_image_file (i->path())) {
50                                 _paths.push_back (i->path ());
51                         }
52                 }
53
54                 if (_paths.empty()) {
55                         throw FileError (_("No valid image files were found in the folder."), p);
56                 }
57
58                 sort (_paths.begin(), _paths.end(), ImageFilenameSorter ());
59         }
60
61         set_default_colour_conversion ();
62 }
63
64
65 ImageContent::ImageContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
66         : Content (film, node)
67         , video (new VideoContent (film, node, version))
68 {
69
70 }
71
72 string
73 ImageContent::summary () const
74 {
75         string s = path_summary () + " ";
76         /* Get the string() here so that the name does not have quotes around it */
77         if (still ()) {
78                 s += _("[still]");
79         } else {
80                 s += _("[moving images]");
81         }
82
83         return s;
84 }
85
86 string
87 ImageContent::technical_summary () const
88 {
89         string s = Content::technical_summary() + " - "
90                 + video->technical_summary() + " - ";
91
92         if (still ()) {
93                 s += _("still");
94         } else {
95                 s += _("moving");
96         }
97
98         return s;
99 }
100
101 void
102 ImageContent::as_xml (xmlpp::Node* node) const
103 {
104         node->add_child("Type")->add_child_text ("Image");
105         Content::as_xml (node);
106         video->as_xml (node);
107 }
108
109 void
110 ImageContent::examine (shared_ptr<Job> job)
111 {
112         Content::examine (job);
113
114         shared_ptr<const Film> film = _film.lock ();
115         DCPOMATIC_ASSERT (film);
116
117         shared_ptr<ImageExaminer> examiner (new ImageExaminer (film, shared_from_this(), job));
118         take_from_video_examiner (examiner);
119         set_default_colour_conversion ();
120 }
121
122 void
123 ImageContent::set_video_length (Frame len)
124 {
125         {
126                 boost::mutex::scoped_lock lm (_mutex);
127                 _video_length = len;
128         }
129
130         signal_changed (ContentProperty::LENGTH);
131 }
132
133 DCPTime
134 ImageContent::full_length () const
135 {
136         shared_ptr<const Film> film = _film.lock ();
137         DCPOMATIC_ASSERT (film);
138         FrameRateChange const frc (video_frame_rate(), film->video_frame_rate());
139         return DCPTime::from_frames (llrint (video_length_after_3d_combine() * frc.factor ()), film->video_frame_rate ());
140 }
141
142 string
143 ImageContent::identifier () const
144 {
145         SafeStringStream s;
146         s << Content::identifier();
147         s << "_" << video->identifier ();
148         s << "_" << video_length();
149         return s.str ();
150 }
151
152 bool
153 ImageContent::still () const
154 {
155         return number_of_paths() == 1;
156 }
157
158 void
159 ImageContent::set_default_colour_conversion ()
160 {
161         BOOST_FOREACH (boost::filesystem::path i, _paths) {
162                 if (valid_j2k_file (i)) {
163                         /* We default to no colour conversion if we have JPEG2000 files */
164                         unset_colour_conversion ();
165                         return;
166                 }
167         }
168
169         bool const s = still ();
170
171         boost::mutex::scoped_lock lm (_mutex);
172
173         if (s) {
174                 _colour_conversion = PresetColourConversion::from_id ("srgb").conversion;
175         } else {
176                 _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
177         }
178 }