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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
This file is part of DCP-o-matic.
DCP-o-matic 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.
DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/version.h"
#include "lib/decrypted_ecinema_kdm.h"
#include "lib/config.h"
#include "lib/util.h"
#include <dcp/key.h>
extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/aes_ctr.h>
}
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#include <openssl/rand.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <iostream>
using std::string;
using std::cerr;
using std::cout;
using boost::optional;
static void
help (string n)
{
cerr << "Syntax: " << n << " [OPTION] <FILE>\n"
<< " -v, --version show DCP-o-matic version\n"
<< " -h, --help show this help\n"
<< " -o, --output output directory\n"
<< " -f, --format output format (mov or mp4; defaults to mov)\n"
<< "\n"
<< "<FILE> is the unencrypted .mp4 file.\n";
}
int
main (int argc, char* argv[])
{
optional<boost::filesystem::path> output;
optional<boost::filesystem::path> format;
int option_index = 0;
while (true) {
static struct option long_options[] = {
{ "version", no_argument, 0, 'v'},
{ "help", no_argument, 0, 'h'},
{ "output", required_argument, 0, 'o'},
{ "format", required_argument, 0, 'f'},
};
int c = getopt_long (argc, argv, "vho:f:", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 'v':
cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
exit (EXIT_SUCCESS);
case 'h':
help (argv[0]);
exit (EXIT_SUCCESS);
case 'o':
output = optarg;
break;
case 'f':
format = optarg;
break;
}
}
if (optind >= argc) {
help (argv[0]);
exit (EXIT_FAILURE);
}
if (!output) {
cerr << "You must specify --output-media or -o\n";
exit (EXIT_FAILURE);
}
if (!format) {
format = "mov";
}
if (*format != "mov" && *format != "mp4") {
cerr << "Invalid format specified: must be mov or mp4\n";
exit (EXIT_FAILURE);
}
boost::filesystem::path input = argv[optind];
boost::filesystem::path output_file = *output / (input.filename().string() + ".ecinema");
if (!boost::filesystem::is_directory(*output)) {
boost::filesystem::create_directory (*output);
}
av_register_all ();
AVFormatContext* input_fc = avformat_alloc_context ();
if (avformat_open_input(&input_fc, input.string().c_str(), 0, 0) < 0) {
cerr << "Could not open input file\n";
exit (EXIT_FAILURE);
}
if (avformat_find_stream_info (input_fc, 0) < 0) {
cerr << "Could not read stream information\n";
exit (EXIT_FAILURE);
}
AVFormatContext* output_fc;
avformat_alloc_output_context2 (&output_fc, av_guess_format(format->c_str(), 0, 0), 0, 0);
for (uint32_t i = 0; i < input_fc->nb_streams; ++i) {
AVStream* is = input_fc->streams[i];
AVStream* os = avformat_new_stream (output_fc, is->codec->codec);
if (avcodec_parameters_copy (os->codecpar, is->codecpar) < 0) {
cerr << "Could not set up output stream.\n";
exit (EXIT_FAILURE);
}
os->avg_frame_rate = is->avg_frame_rate;
switch (is->codec->codec_type) {
case AVMEDIA_TYPE_VIDEO:
os->time_base = is->time_base;
os->r_frame_rate = is->r_frame_rate;
os->sample_aspect_ratio = is->sample_aspect_ratio;
os->codec->time_base = is->codec->time_base;
os->codec->framerate = is->codec->framerate;
os->codec->pix_fmt = is->codec->pix_fmt;
break;
case AVMEDIA_TYPE_AUDIO:
os->codec->sample_fmt = is->codec->sample_fmt;
os->codec->bits_per_raw_sample = is->codec->bits_per_raw_sample;
os->codec->sample_rate = is->codec->sample_rate;
os->codec->channel_layout = is->codec->channel_layout;
os->codec->channels = is->codec->channels;
if (is->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
/* XXX: fix incoming 24-bit files labelled lpcm, which apparently isn't allowed */
os->codecpar->codec_tag = MKTAG('i', 'n', '2', '4');
}
break;
default:
/* XXX */
break;
}
}
if (avio_open2 (&output_fc->pb, output_file.string().c_str(), AVIO_FLAG_WRITE, 0, 0) < 0) {
cerr << "Could not open output file `" << output_file.string() << "'\n";
exit (EXIT_FAILURE);
}
dcp::Key key (AES_CTR_KEY_SIZE);
AVDictionary* options = 0;
av_dict_set (&options, "encryption_key", key.hex().c_str(), 0);
/* XXX: is this OK? */
av_dict_set (&options, "encryption_kid", "00000000000000000000000000000000", 0);
av_dict_set (&options, "encryption_scheme", "cenc-aes-ctr", 0);
string id = dcp::make_uuid ();
if (av_dict_set(&output_fc->metadata, SWAROOP_ID_TAG, id.c_str(), 0) < 0) {
cerr << "Could not write ID to output.\n";
exit (EXIT_FAILURE);
}
if (avformat_write_header (output_fc, &options) < 0) {
cerr << "Could not write header to output.\n";
exit (EXIT_FAILURE);
}
AVPacket packet;
while (av_read_frame(input_fc, &packet) >= 0) {
AVStream* is = input_fc->streams[packet.stream_index];
AVStream* os = output_fc->streams[packet.stream_index];
packet.pts = av_rescale_q_rnd(packet.pts, is->time_base, os->time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
packet.dts = av_rescale_q_rnd(packet.dts, is->time_base, os->time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
packet.duration = av_rescale_q(packet.duration, is->time_base, os->time_base);
packet.pos = -1;
if (av_interleaved_write_frame (output_fc, &packet) < 0) {
cerr << "Could not write frame to output.\n";
exit (EXIT_FAILURE);
}
}
av_write_trailer (output_fc);
avformat_free_context (input_fc);
avformat_free_context (output_fc);
DecryptedECinemaKDM decrypted_kdm (id, output_file.filename().string(), key);
EncryptedECinemaKDM encrypted_kdm = decrypted_kdm.encrypt (Config::instance()->decryption_chain()->leaf());
cout << encrypted_kdm.as_xml() << "\n";
}
|