8c473b7bc27e524fb0ca9f6134fe1bd3dec3522c
[dcpomatic.git] / src / lib / content_factory.cc
1 /*
2     Copyright (C) 2013-2016 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 /** @file  src/lib/content_factory.cc
22  *  @brief Methods to create content objects.
23  */
24
25 #include "ffmpeg_content.h"
26 #include "audio_content.h"
27 #include "image_content.h"
28 #include "atmos_mxf_content.h"
29 #include "text_caption_file_content.h"
30 #include "dcp_content.h"
31 #include "dcp_subtitle_content.h"
32 #include "util.h"
33 #include "ffmpeg_audio_stream.h"
34 #include "video_mxf_content.h"
35 #include "film.h"
36 #include "log_entry.h"
37 #include "log.h"
38 #include "compose.hpp"
39 #include <libcxml/cxml.h>
40 #include <dcp/smpte_subtitle_asset.h>
41 #include <boost/algorithm/string.hpp>
42
43 #include "i18n.h"
44
45 using std::string;
46 using std::list;
47 using boost::shared_ptr;
48 using boost::optional;
49
50 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
51
52 /** Create a Content object from an XML node.
53  *  @param film Film that the content will be in.
54  *  @param node XML description.
55  *  @param version XML state version.
56  *  @param notes A list to which is added descriptions of any non-critial warnings / messages.
57  *  @return Content object, or 0 if no content was recognised in the XML.
58  */
59 shared_ptr<Content>
60 content_factory (shared_ptr<const Film> film, cxml::NodePtr node, int version, list<string>& notes)
61 {
62         string const type = node->string_child ("Type");
63
64         boost::shared_ptr<Content> content;
65
66         if (type == "FFmpeg") {
67                 /* SndfileContent is now handled by the FFmpeg code rather than by
68                    separate libsndfile-based code.
69                 */
70                 content.reset (new FFmpegContent (film, node, version, notes));
71         } else if (type == "Image") {
72                 content.reset (new ImageContent (film, node, version));
73         } else if (type == "Sndfile") {
74                 /* SndfileContent is now handled by the FFmpeg code rather than by
75                    separate libsndfile-based code.
76                 */
77                 content.reset (new FFmpegContent (film, node, version, notes));
78
79                 content->audio->set_stream (
80                         AudioStreamPtr (
81                                 new FFmpegAudioStream (
82                                         "Stream", 0,
83                                         node->number_child<int> ("AudioFrameRate"),
84                                         node->number_child<Frame> ("AudioLength"),
85                                         AudioMapping (node->node_child ("AudioMapping"), version)
86                                         )
87                                 )
88                         );
89
90         } else if (type == "SubRip" || type == "TextSubtitle") {
91                 content.reset (new TextCaptionFileContent (film, node, version));
92         } else if (type == "DCP") {
93                 content.reset (new DCPContent (film, node, version));
94         } else if (type == "DCPSubtitle") {
95                 content.reset (new DCPSubtitleContent (film, node, version));
96         } else if (type == "VideoMXF") {
97                 content.reset (new VideoMXFContent (film, node, version));
98         } else if (type == "AtmosMXF") {
99                 content.reset (new AtmosMXFContent (film, node, version));
100         }
101
102         /* See if this content should be nudged to start on a video frame */
103         DCPTime const old_pos = content->position();
104         content->set_position(old_pos);
105         if (old_pos != content->position()) {
106                 string note = _("Your project contains video content that was not aligned to a frame boundary.");
107                 note += "  ";
108                 if (old_pos < content->position()) {
109                         note += String::compose(
110                                 _("The file %1 has been moved %2 milliseconds later."),
111                                 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
112                                 );
113                 } else {
114                         note += String::compose(
115                                 _("The file %1 has been moved %2 milliseconds earlier."),
116                                 content->path_summary(), DCPTime(content->position() - old_pos).seconds() * 1000
117                                 );
118                 }
119                 notes.push_back (note);
120         }
121
122         /* ...or have a start trim which is an integer number of frames */
123         ContentTime const old_trim = content->trim_start();
124         content->set_trim_start(old_trim);
125         if (old_trim != content->trim_start()) {
126                 string note = _("Your project contains video content whose trim was not aligned to a frame boundary.");
127                 note += "  ";
128                 if (old_trim < content->trim_start()) {
129                         note += String::compose(
130                                 _("The file %1 has been trimmed by %2 milliseconds more."),
131                                 content->path_summary(), ContentTime(content->trim_start() - old_trim).seconds() * 1000
132                                 );
133                 } else {
134                         note += String::compose(
135                                 _("The file %1 has been trimmed by %2 milliseconds less."),
136                                 content->path_summary(), ContentTime(old_trim - content->trim_start()).seconds() * 1000
137                                 );
138                 }
139                 notes.push_back (note);
140         }
141
142         return content;
143 }
144
145 /** Create some Content objects from a file or directory.
146  *  @param film Film that the content will be in.
147  *  @param path File or directory.
148  *  @return Content objects.
149  */
150 list<shared_ptr<Content> >
151 content_factory (shared_ptr<const Film> film, boost::filesystem::path path)
152 {
153         list<shared_ptr<Content> > content;
154
155         if (boost::filesystem::is_directory (path)) {
156
157                 LOG_GENERAL ("Look in directory %1", path);
158
159                 if (boost::filesystem::is_empty (path)) {
160                         return content;
161                 }
162
163                 /* See if this is a set of images or a set of sound files */
164
165                 int image_files = 0;
166                 int sound_files = 0;
167                 int read = 0;
168                 for (boost::filesystem::directory_iterator i(path); i != boost::filesystem::directory_iterator() && read < 10; ++i) {
169
170                         LOG_GENERAL ("Checking file %1", i->path());
171
172                         if (boost::starts_with (i->path().leaf().string(), ".")) {
173                                 /* We ignore hidden files */
174                                 LOG_GENERAL ("Ignored %1 (starts with .)", i->path());
175                                 continue;
176                         }
177
178                         if (!boost::filesystem::is_regular_file(i->path())) {
179                                 /* Ignore things which aren't files (probably directories) */
180                                 LOG_GENERAL ("Ignored %1 (not a regular file)", i->path());
181                                 continue;
182                         }
183
184                         if (valid_image_file (i->path ())) {
185                                 ++image_files;
186                         }
187
188                         if (valid_sound_file (i->path ())) {
189                                 ++sound_files;
190                         }
191
192                         ++read;
193                 }
194
195                 if (image_files > 0 && sound_files == 0)  {
196                         content.push_back (shared_ptr<Content> (new ImageContent (film, path)));
197                 } else if (image_files == 0 && sound_files > 0) {
198                         for (boost::filesystem::directory_iterator i(path); i != boost::filesystem::directory_iterator(); ++i) {
199                                 content.push_back (shared_ptr<FFmpegContent> (new FFmpegContent (film, i->path())));
200                         }
201                 }
202
203         } else {
204
205                 shared_ptr<Content> single;
206
207                 string ext = path.extension().string ();
208                 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
209
210                 if (valid_image_file (path)) {
211                         single.reset (new ImageContent (film, path));
212                 } else if (ext == ".srt" || ext == ".ssa" || ext == ".ass") {
213                         single.reset (new TextCaptionFileContent (film, path));
214                 } else if (ext == ".xml") {
215                         cxml::Document doc;
216                         doc.read_file (path);
217                         if (doc.root_name() == "DCinemaSecurityMessage") {
218                                 throw KDMAsContentError ();
219                         }
220                         single.reset (new DCPSubtitleContent (film, path));
221                 } else if (ext == ".mxf" && dcp::SMPTESubtitleAsset::valid_mxf (path)) {
222                         single.reset (new DCPSubtitleContent (film, path));
223                 } else if (ext == ".mxf" && VideoMXFContent::valid_mxf (path)) {
224                         single.reset (new VideoMXFContent (film, path));
225                 } else if (ext == ".mxf" && AtmosMXFContent::valid_mxf (path)) {
226                         single.reset (new AtmosMXFContent (film, path));
227                 }
228
229                 if (!single) {
230                         single.reset (new FFmpegContent (film, path));
231                 }
232
233                 content.push_back (single);
234         }
235
236         return content;
237 }