No-op; fix GPL address and use the explicit-program-name version.
[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_subtitle_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 using std::string;
44 using std::list;
45 using boost::shared_ptr;
46
47 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
48
49 /** Create a Content object from an XML node.
50  *  @param film Film that the content will be in.
51  *  @param node XML description.
52  *  @param version XML state version.
53  *  @param notes A list to which is added descriptions of any non-critial warnings / messages.
54  *  @return Content object, or 0 if no content was recognised in the XML.
55  */
56 shared_ptr<Content>
57 content_factory (shared_ptr<const Film> film, cxml::NodePtr node, int version, list<string>& notes)
58 {
59         string const type = node->string_child ("Type");
60
61         boost::shared_ptr<Content> content;
62
63         if (type == "FFmpeg") {
64                 /* SndfileContent is now handled by the FFmpeg code rather than by
65                    separate libsndfile-based code.
66                 */
67                 content.reset (new FFmpegContent (film, node, version, notes));
68         } else if (type == "Image") {
69                 content.reset (new ImageContent (film, node, version));
70         } else if (type == "Sndfile") {
71                 /* SndfileContent is now handled by the FFmpeg code rather than by
72                    separate libsndfile-based code.
73                 */
74                 content.reset (new FFmpegContent (film, node, version, notes));
75
76                 content->audio->set_stream (
77                         AudioStreamPtr (
78                                 new FFmpegAudioStream (
79                                         "Stream", 0,
80                                         node->number_child<int> ("AudioFrameRate"),
81                                         node->number_child<Frame> ("AudioLength"),
82                                         AudioMapping (node->node_child ("AudioMapping"), version)
83                                         )
84                                 )
85                         );
86
87         } else if (type == "SubRip" || type == "TextSubtitle") {
88                 content.reset (new TextSubtitleContent (film, node, version));
89         } else if (type == "DCP") {
90                 content.reset (new DCPContent (film, node, version));
91         } else if (type == "DCPSubtitle") {
92                 content.reset (new DCPSubtitleContent (film, node, version));
93         } else if (type == "VideoMXF") {
94                 content.reset (new VideoMXFContent (film, node, version));
95         } else if (type == "AtmosMXF") {
96                 content.reset (new AtmosMXFContent (film, node, version));
97         }
98
99         return content;
100 }
101
102 /** Create a Content object from a file or directory.
103  *  @param film Film that the content will be in.
104  *  @param path File or directory.
105  *  @return Content object.
106  */
107 shared_ptr<Content>
108 content_factory (shared_ptr<const Film> film, boost::filesystem::path path)
109 {
110         shared_ptr<Content> content;
111
112         if (boost::filesystem::is_directory (path)) {
113
114                 LOG_GENERAL ("Look in directory %1", path);
115
116                 if (boost::filesystem::is_empty (path)) {
117                         return shared_ptr<Content> ();
118                 }
119
120                 /* Guess if this is a DCP or a set of images: read the first ten filenames and if they
121                    are all valid image files we assume it is a set of images.
122                 */
123
124                 bool is_dcp = false;
125                 int read = 0;
126                 for (boost::filesystem::directory_iterator i(path); i != boost::filesystem::directory_iterator() && read < 10; ++i) {
127
128                         LOG_GENERAL ("Checking file %1", i->path());
129
130                         if (boost::starts_with (i->path().leaf().string(), "._") || i->path().leaf().string() == ".DS_Store") {
131                                 /* We ignore these files */
132                                 LOG_GENERAL ("Ignored %1 (starts with {._}, or .DS_Store)", i->path());
133                                 continue;
134                         }
135
136                         if (!boost::filesystem::is_regular_file(i->path())) {
137                                 /* Ignore things which aren't files (probably directories) */
138                                 LOG_GENERAL ("Ignored %1 (not a regular file)", i->path());
139                                 continue;
140                         }
141
142                         if (!valid_image_file (i->path())) {
143                                 /* We have a normal file which isn't an image; assume we are looking
144                                    at a DCP.
145                                 */
146                                 LOG_GENERAL ("It's a DCP because of %1", i->path());
147                                 is_dcp = true;
148                         }
149
150                         ++read;
151                 }
152
153                 if (is_dcp) {
154                         content.reset (new DCPContent (film, path));
155                 } else {
156                         content.reset (new ImageContent (film, path));
157                 }
158
159         } else {
160
161                 string ext = path.extension().string ();
162                 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
163
164                 if (valid_image_file (path)) {
165                         content.reset (new ImageContent (film, path));
166                 } else if (ext == ".srt" || ext == ".ssa") {
167                         content.reset (new TextSubtitleContent (film, path));
168                 } else if (ext == ".xml") {
169                         content.reset (new DCPSubtitleContent (film, path));
170                 } else if (ext == ".mxf" && dcp::SMPTESubtitleAsset::valid_mxf (path)) {
171                         content.reset (new DCPSubtitleContent (film, path));
172                 } else if (ext == ".mxf" && VideoMXFContent::valid_mxf (path)) {
173                         content.reset (new VideoMXFContent (film, path));
174                 } else if (ext == ".mxf" && AtmosMXFContent::valid_mxf (path)) {
175                         content.reset (new AtmosMXFContent (film, path));
176                 }
177
178                 if (!content) {
179                         content.reset (new FFmpegContent (film, path));
180                 }
181         }
182
183         return content;
184 }