Support Dolby-style WAV back surround names when guessing mappings (#2427).
[dcpomatic.git] / src / lib / image_content.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "compose.hpp"
23 #include "exceptions.h"
24 #include "film.h"
25 #include "frame_rate_change.h"
26 #include "image_content.h"
27 #include "image_examiner.h"
28 #include "image_filename_sorter.h"
29 #include "job.h"
30 #include "util.h"
31 #include "video_content.h"
32 #include <libcxml/cxml.h>
33 #include <libxml++/libxml++.h>
34 #include <iostream>
35
36 #include "i18n.h"
37
38
39 using std::cout;
40 using std::list;
41 using std::make_shared;
42 using std::shared_ptr;
43 using std::string;
44 using std::vector;
45 using namespace dcpomatic;
46
47
48 ImageContent::ImageContent (boost::filesystem::path p)
49 {
50         video = make_shared<VideoContent>(this);
51
52         if (boost::filesystem::is_regular_file (p) && valid_image_file (p)) {
53                 add_path (p);
54         } else {
55                 _path_to_scan = p;
56         }
57
58         set_default_colour_conversion ();
59 }
60
61
62 ImageContent::ImageContent (cxml::ConstNodePtr node, int version)
63         : Content (node)
64 {
65         video = VideoContent::from_xml (this, node, version, VideoRange::FULL);
66 }
67
68
69 string
70 ImageContent::summary () const
71 {
72         string s = path_summary () + " ";
73         /* Get the string() here so that the name does not have quotes around it */
74         if (still ()) {
75                 s += _("[still]");
76         } else {
77                 s += _("[moving images]");
78         }
79
80         return s;
81 }
82
83
84 string
85 ImageContent::technical_summary () const
86 {
87         string s = Content::technical_summary() + " - "
88                 + video->technical_summary() + " - ";
89
90         if (still ()) {
91                 s += _("still");
92         } else {
93                 s += _("moving");
94         }
95
96         return s;
97 }
98
99
100 void
101 ImageContent::as_xml (xmlpp::Node* node, bool with_paths) const
102 {
103         node->add_child("Type")->add_child_text ("Image");
104         Content::as_xml (node, with_paths);
105
106         if (video) {
107                 video->as_xml (node);
108         }
109 }
110
111
112 void
113 ImageContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
114 {
115         if (_path_to_scan) {
116                 job->sub (_("Scanning image files"));
117                 vector<boost::filesystem::path> paths;
118                 int n = 0;
119                 for (auto i: boost::filesystem::directory_iterator(*_path_to_scan)) {
120                         if (boost::filesystem::is_regular_file(i.path()) && valid_image_file (i.path())) {
121                                 paths.push_back (i.path());
122                         }
123                         ++n;
124                         if ((n % 1000) == 0) {
125                                 job->set_progress_unknown ();
126                         }
127                 }
128
129                 if (paths.empty()) {
130                         throw FileError (_("No valid image files were found in the folder."), *_path_to_scan);
131                 }
132
133                 sort (paths.begin(), paths.end(), ImageFilenameSorter());
134                 set_paths (paths);
135         }
136
137         Content::examine (film, job);
138
139         auto examiner = make_shared<ImageExaminer>(film, shared_from_this(), job);
140         video->take_from_examiner(film, examiner);
141         set_default_colour_conversion ();
142 }
143
144
145 DCPTime
146 ImageContent::full_length (shared_ptr<const Film> film) const
147 {
148         FrameRateChange const frc (film, shared_from_this());
149         return DCPTime::from_frames (llrint(video->length_after_3d_combine() * frc.factor()), film->video_frame_rate());
150 }
151
152
153 DCPTime
154 ImageContent::approximate_length () const
155 {
156         return DCPTime::from_frames (video->length_after_3d_combine(), 24);
157 }
158
159
160 string
161 ImageContent::identifier () const
162 {
163         char buffer[256];
164         snprintf (buffer, sizeof(buffer), "%s_%s_%" PRId64, Content::identifier().c_str(), video->identifier().c_str(), video->length());
165         return buffer;
166 }
167
168
169 bool
170 ImageContent::still () const
171 {
172         return number_of_paths() == 1;
173 }
174
175
176 void
177 ImageContent::set_default_colour_conversion ()
178 {
179         for (auto i: paths()) {
180                 if (valid_j2k_file (i)) {
181                         /* We default to no colour conversion if we have JPEG2000 files */
182                         video->unset_colour_conversion ();
183                         return;
184                 }
185         }
186
187         bool const s = still ();
188
189         boost::mutex::scoped_lock lm (_mutex);
190
191         if (s) {
192                 video->set_colour_conversion (PresetColourConversion::from_id ("srgb").conversion);
193         } else {
194                 video->set_colour_conversion (PresetColourConversion::from_id ("rec709").conversion);
195         }
196 }
197
198
199 void
200 ImageContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
201 {
202         Content::add_properties (film, p);
203         video->add_properties (p);
204 }