2 Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "lib/version.h"
22 #include "lib/decrypted_ecinema_kdm.h"
23 #include "lib/config.h"
26 #include "lib/dcp_content.h"
27 #include "lib/job_manager.h"
28 #include "lib/cross.h"
29 #include "lib/transcode_job.h"
30 #include "lib/ffmpeg_encoder.h"
31 #include "lib/signal_manager.h"
32 #include "lib/video_content.h"
33 #include "lib/ratio.h"
36 #include <libavformat/avformat.h>
37 #include <libavutil/aes_ctr.h>
39 #include <boost/filesystem.hpp>
40 #include <boost/optional.hpp>
41 #include <openssl/rand.h>
44 #include <sys/types.h>
52 using boost::optional;
53 using boost::shared_ptr;
55 static void convert_dcp (
56 boost::filesystem::path input,
57 boost::filesystem::path output_file,
58 boost::optional<boost::filesystem::path> kdm,
61 static void convert_ffmpeg (boost::filesystem::path input, boost::filesystem::path output_file, string format);
62 static void write_kdm (string id, boost::filesystem::path name, dcp::Key key);
67 cerr << "Syntax: " << n << " [OPTION] <FILE>|<DIRECTORY>\n"
68 << " -v, --version show DCP-o-matic version\n"
69 << " -h, --help show this help\n"
70 << " -o, --output output directory\n"
71 << " -f, --format output format (mov or mp4; defaults to mov)\n"
72 << " -k, --kdm DCP KDM filename (if required)\n"
73 << " -c, --crf quality (CRF) when transcoding from DCP (0 is best, 51 is worst, defaults to 23)\n"
75 << "<FILE> is an unencrypted .mp4 file; <DIRECTORY> is a DCP directory.\n";
79 main (int argc, char* argv[])
81 optional<boost::filesystem::path> output;
82 optional<string> format;
83 optional<boost::filesystem::path> kdm;
88 static struct option long_options[] = {
89 { "version", no_argument, 0, 'v' },
90 { "help", no_argument, 0, 'h' },
91 { "output", required_argument, 0, 'o' },
92 { "format", required_argument, 0, 'f' },
93 { "kdm", required_argument, 0, 'k' },
94 { "crf", required_argument, 0, 'c' },
97 int c = getopt_long (argc, argv, "vho:f:k:c:", long_options, &option_index);
105 cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
125 if (optind >= argc) {
131 cerr << "You must specify --output-media or -o\n";
139 if (*format != "mov" && *format != "mp4") {
140 cerr << "Invalid format specified: must be mov or mp4\n";
144 dcpomatic_setup_path_encoding ();
146 signal_manager = new SignalManager ();
148 boost::filesystem::path input = argv[optind];
149 boost::filesystem::path output_file;
150 if (boost::filesystem::is_directory(input)) {
151 output_file = *output / (input.parent_path().filename().string() + ".ecinema");
153 output_file = *output / (input.filename().string() + ".ecinema");
156 if (!boost::filesystem::is_directory(*output)) {
157 boost::filesystem::create_directory (*output);
162 if (boost::filesystem::is_directory(input)) {
163 /* Assume input is a DCP */
164 convert_dcp (input, output_file, kdm, crf);
166 convert_ffmpeg (input, output_file, *format);
171 convert_ffmpeg (boost::filesystem::path input, boost::filesystem::path output_file, string format)
173 AVFormatContext* input_fc = avformat_alloc_context ();
174 if (avformat_open_input(&input_fc, input.string().c_str(), 0, 0) < 0) {
175 cerr << "Could not open input file\n";
179 if (avformat_find_stream_info (input_fc, 0) < 0) {
180 cerr << "Could not read stream information\n";
184 AVFormatContext* output_fc;
185 avformat_alloc_output_context2 (&output_fc, av_guess_format(format.c_str(), 0, 0), 0, 0);
187 for (uint32_t i = 0; i < input_fc->nb_streams; ++i) {
188 AVStream* is = input_fc->streams[i];
189 AVStream* os = avformat_new_stream (output_fc, is->codec->codec);
190 if (avcodec_parameters_copy (os->codecpar, is->codecpar) < 0) {
191 cerr << "Could not set up output stream.\n";
195 os->avg_frame_rate = is->avg_frame_rate;
197 switch (is->codec->codec_type) {
198 case AVMEDIA_TYPE_VIDEO:
199 os->time_base = is->time_base;
200 os->r_frame_rate = is->r_frame_rate;
201 os->sample_aspect_ratio = is->sample_aspect_ratio;
202 os->codec->time_base = is->codec->time_base;
203 os->codec->framerate = is->codec->framerate;
204 os->codec->pix_fmt = is->codec->pix_fmt;
206 case AVMEDIA_TYPE_AUDIO:
207 os->codec->sample_fmt = is->codec->sample_fmt;
208 os->codec->bits_per_raw_sample = is->codec->bits_per_raw_sample;
209 os->codec->sample_rate = is->codec->sample_rate;
210 os->codec->channel_layout = is->codec->channel_layout;
211 os->codec->channels = is->codec->channels;
212 if (is->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
213 /* XXX: fix incoming 24-bit files labelled lpcm, which apparently isn't allowed */
214 os->codecpar->codec_tag = MKTAG('i', 'n', '2', '4');
223 if (avio_open2 (&output_fc->pb, output_file.string().c_str(), AVIO_FLAG_WRITE, 0, 0) < 0) {
224 cerr << "Could not open output file `" << output_file.string() << "'\n";
228 dcp::Key key (AES_CTR_KEY_SIZE);
229 AVDictionary* options = 0;
230 av_dict_set (&options, "encryption_key", key.hex().c_str(), 0);
231 /* XXX: is this OK? */
232 av_dict_set (&options, "encryption_kid", "00000000000000000000000000000000", 0);
233 av_dict_set (&options, "encryption_scheme", "cenc-aes-ctr", 0);
235 string id = dcp::make_uuid ();
236 if (av_dict_set(&output_fc->metadata, SWAROOP_ID_TAG, id.c_str(), 0) < 0) {
237 cerr << "Could not write ID to output.\n";
241 if (avformat_write_header (output_fc, &options) < 0) {
242 cerr << "Could not write header to output.\n";
247 while (av_read_frame(input_fc, &packet) >= 0) {
248 AVStream* is = input_fc->streams[packet.stream_index];
249 AVStream* os = output_fc->streams[packet.stream_index];
250 packet.pts = av_rescale_q_rnd(packet.pts, is->time_base, os->time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
251 packet.dts = av_rescale_q_rnd(packet.dts, is->time_base, os->time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
252 packet.duration = av_rescale_q(packet.duration, is->time_base, os->time_base);
254 if (av_interleaved_write_frame (output_fc, &packet) < 0) {
255 cerr << "Could not write frame to output.\n";
260 av_write_trailer (output_fc);
262 avformat_free_context (input_fc);
263 avformat_free_context (output_fc);
265 write_kdm (id, output_file, key);
269 write_kdm (string id, boost::filesystem::path name, dcp::Key key)
271 DecryptedECinemaKDM decrypted_kdm (id, name.filename().string(), key, optional<dcp::LocalTime>(), optional<dcp::LocalTime>());
272 EncryptedECinemaKDM encrypted_kdm = decrypted_kdm.encrypt (Config::instance()->decryption_chain()->leaf());
274 ofstream f(string(name.string() + ".xml").c_str());
275 f << encrypted_kdm.as_xml() << "\n";
280 boost::filesystem::path input, boost::filesystem::path output_file, optional<boost::filesystem::path> kdm, int crf
283 shared_ptr<Film> film (new Film(boost::optional<boost::filesystem::path>()));
284 shared_ptr<DCPContent> dcp (new DCPContent(input));
285 film->examine_and_add_content (dcp);
287 dcp->add_kdm (dcp::EncryptedKDM(dcp::file_to_string(*kdm)));
290 JobManager* jm = JobManager::instance ();
291 while (jm->work_to_do ()) {
292 while (signal_manager->ui_idle ()) {}
293 dcpomatic_sleep_seconds (1);
295 DCPOMATIC_ASSERT (!jm->errors());
297 film->set_container (Ratio::nearest_from_ratio(dcp->video->size().ratio()));
299 string id = dcp::make_uuid ();
300 dcp::Key key (AES_CTR_KEY_SIZE);
302 shared_ptr<TranscodeJob> job (new TranscodeJob(film));
304 shared_ptr<FFmpegEncoder>(
305 new FFmpegEncoder(film, job, output_file, EXPORT_FORMAT_H264_PCM, false, false, crf, key, id)
309 show_jobs_on_console (true);
311 write_kdm (id, output_file, key);