1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
This file is part of libdcp.
libdcp is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
libdcp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libdcp. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the
OpenSSL library under certain conditions as described in each
individual source file, and distribute linked combinations
including the two.
You must obey the GNU General Public License in all respects
for all of the code used other than OpenSSL. If you modify
file(s) with this exception, you may extend this exception to your
version of the file(s), but you are not obligated to do so. If you
do not wish to do so, delete this exception statement from your
version. If you delete this exception statement from all source
files in the program, then also delete it here.
*/
#include "asset.h"
#include "combine.h"
#include "cpl.h"
#include "dcp.h"
#include "dcp_assert.h"
#include "exceptions.h"
#include "font_asset.h"
#include "interop_subtitle_asset.h"
#include "raw_convert.h"
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <set>
#include <string>
#include <vector>
using std::list;
using std::map;
using std::set;
using std::string;
using std::vector;
using std::dynamic_pointer_cast;
using boost::optional;
using std::shared_ptr;
boost::filesystem::path
make_unique (boost::filesystem::path path)
{
if (!boost::filesystem::exists(path)) {
return path;
}
for (int i = 0; i < 10000; ++i) {
boost::filesystem::path p = path.parent_path() / (path.stem().string() + dcp::raw_convert<string>(i) + path.extension().string());
if (!boost::filesystem::exists(p)) {
return p;
}
}
DCP_ASSERT (false);
return path;
}
static
void
create_hard_link_or_copy (boost::filesystem::path from, boost::filesystem::path to)
{
try {
create_hard_link (from, to);
} catch (boost::filesystem::filesystem_error& e) {
if (e.code() == boost::system::errc::cross_device_link) {
copy_file (from, to);
} else {
throw;
}
}
}
void
dcp::combine (
vector<boost::filesystem::path> inputs,
boost::filesystem::path output,
string issuer,
string creator,
string issue_date,
string annotation_text,
shared_ptr<const CertificateChain> signer
)
{
using namespace boost::filesystem;
DCP_ASSERT (!inputs.empty());
DCP output_dcp (output);
optional<dcp::Standard> standard;
BOOST_FOREACH (path i, inputs) {
DCP dcp (i);
dcp.read ();
if (!standard) {
standard = *dcp.standard();
} else if (standard != dcp.standard()) {
throw CombineError ("Cannot combine Interop and SMPTE DCPs.");
}
}
list<path> paths;
list<shared_ptr<dcp::Asset> > assets;
BOOST_FOREACH (path i, inputs) {
DCP dcp (i);
dcp.read ();
BOOST_FOREACH (shared_ptr<dcp::CPL> j, dcp.cpls()) {
output_dcp.add (j);
}
BOOST_FOREACH (shared_ptr<dcp::Asset> j, dcp.assets(true)) {
if (dynamic_pointer_cast<dcp::CPL>(j)) {
continue;
}
optional<path> file = j->file();
DCP_ASSERT (file);
path new_path = make_unique(output / file->filename());
shared_ptr<dcp::InteropSubtitleAsset> sub = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(j);
if (sub) {
/* Interop fonts are really fiddly. The font files are assets (in the ASSETMAP)
* and also linked from the font XML by filename. We have to fix both these things,
* and re-write the font XML file since the font URI might have changed if it's a duplicate
* with another DCP.
*/
map<string, path> fonts = sub->font_filenames ();
for (map<string, path>::const_iterator k = fonts.begin(); k != fonts.end(); ++k) {
sub->set_font_file (k->first, make_unique(output / k->second.filename()));
}
sub->write (new_path);
} else if (!dynamic_pointer_cast<dcp::FontAsset>(j)) {
/* Take care of everything else that's not a Interop subtitle asset, Interop font file
* or CPL.
*/
optional<path> file = j->file();
DCP_ASSERT (file);
path new_path = make_unique(output / file->filename());
create_hard_link_or_copy (*file, new_path);
j->set_file (new_path);
}
assets.push_back (j);
}
}
output_dcp.resolve_refs (assets);
output_dcp.write_xml (*standard, issuer, creator, issue_date, annotation_text, signer);
}
|