X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Ftools%2Fdcpomatic_ecinema.cc;h=740893fa9fdc78fa0aec6110734b0cfa5a403c5b;hb=422d7ae041812af3587db6fdbead159672ff2772;hp=6a19e5c42724f3f9ca9a62d0c844ecb96939168b;hpb=d2cf99dbfda417caae0d47c004a3262ca75321ca;p=dcpomatic.git diff --git a/src/tools/dcpomatic_ecinema.cc b/src/tools/dcpomatic_ecinema.cc index 6a19e5c42..740893fa9 100644 --- a/src/tools/dcpomatic_ecinema.cc +++ b/src/tools/dcpomatic_ecinema.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2018 Carl Hetherington + Copyright (C) 2018-2019 Carl Hetherington This file is part of DCP-o-matic. @@ -19,11 +19,17 @@ */ #include "lib/version.h" +#include "lib/decrypted_ecinema_kdm.h" +#include "lib/config.h" +#include "lib/util.h" +#include extern "C" { #include +#include } #include #include +#include #include #include #include @@ -41,6 +47,8 @@ help (string n) cerr << "Syntax: " << n << " [OPTION] \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" << " is the unencrypted .mp4 file.\n"; } @@ -49,6 +57,7 @@ int main (int argc, char* argv[]) { optional output; + optional format; int option_index = 0; while (true) { @@ -56,9 +65,10 @@ main (int argc, char* argv[]) { "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:", long_options, &option_index); + int c = getopt_long (argc, argv, "vho:f:", long_options, &option_index); if (c == -1) { break; @@ -74,6 +84,9 @@ main (int argc, char* argv[]) case 'o': output = optarg; break; + case 'f': + format = optarg; + break; } } @@ -83,18 +96,26 @@ main (int argc, char* argv[]) } if (!output) { - cerr << "You must specify --output or -o\n"; + cerr << "You must specify --output-media or -o\n"; exit (EXIT_FAILURE); } - boost::filesystem::path input = argv[optind]; - boost::filesystem::path output_mp4 = *output / (input.filename().string() + ".ecinema"); + if (!format) { + format = "mov"; + } - if (mkdir(output->c_str(), 0777) < 0) { - cerr << "Could not create output directory `" << output->string() << "'\n"; + 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 (); @@ -109,20 +130,21 @@ main (int argc, char* argv[]) } AVFormatContext* output_fc; - avformat_alloc_output_context2 (&output_fc, av_guess_format("mp4", 0, 0), 0, 0); + 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_copy_context (os->codec, is->codec) < 0) { + if (avcodec_parameters_copy (os->codecpar, is->codecpar) < 0) { cerr << "Could not set up output stream.\n"; exit (EXIT_FAILURE); } - switch (os->codec->codec_type) { + os->avg_frame_rate = is->avg_frame_rate; + + switch (is->codec->codec_type) { case AVMEDIA_TYPE_VIDEO: os->time_base = is->time_base; - os->avg_frame_rate = is->avg_frame_rate; os->r_frame_rate = is->r_frame_rate; os->sample_aspect_ratio = is->sample_aspect_ratio; os->codec->time_base = is->codec->time_base; @@ -135,6 +157,10 @@ main (int argc, char* argv[]) 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 */ @@ -142,12 +168,25 @@ main (int argc, char* argv[]) } } - if (avio_open2 (&output_fc->pb, output_mp4.string().c_str(), AVIO_FLAG_WRITE, 0, 0) < 0) { - cerr << "Could not open output file `" << output_mp4.string() << "'\n"; + 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, 0) < 0) { + if (avformat_write_header (output_fc, &options) < 0) { cerr << "Could not write header to output.\n"; exit (EXIT_FAILURE); } @@ -170,4 +209,8 @@ main (int argc, char* argv[]) 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"; }