/* Copyright (C) 2018-2019 Carl Hetherington 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 . */ #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 #include #include using std::string; using std::cerr; using std::cout; using boost::optional; static void 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"; } int main (int argc, char* argv[]) { optional output; optional 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, optional(), optional()); EncryptedECinemaKDM encrypted_kdm = decrypted_kdm.encrypt (Config::instance()->decryption_chain()->leaf()); cout << encrypted_kdm.as_xml() << "\n"; }