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