X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Ftools%2Fdcpomatic_ecinema.cc;h=152194c482f415560320a4feaae9fb0816770431;hb=8276ed82237c30935962874c974e5462737fb0fb;hp=bcd0907263311999869c0d9913c8fe69a42f2e57;hpb=cb0a41355327f0308e5b56c21bff1d1ac004a9cb;p=dcpomatic.git diff --git a/src/tools/dcpomatic_ecinema.cc b/src/tools/dcpomatic_ecinema.cc index bcd090726..152194c48 100644 --- a/src/tools/dcpomatic_ecinema.cc +++ b/src/tools/dcpomatic_ecinema.cc @@ -21,6 +21,16 @@ #include "lib/version.h" #include "lib/decrypted_ecinema_kdm.h" #include "lib/config.h" +#include "lib/util.h" +#include "lib/film.h" +#include "lib/dcp_content.h" +#include "lib/job_manager.h" +#include "lib/cross.h" +#include "lib/transcode_job.h" +#include "lib/ffmpeg_encoder.h" +#include "lib/signal_manager.h" +#include "lib/video_content.h" +#include "lib/ratio.h" #include extern "C" { #include @@ -38,33 +48,53 @@ extern "C" { using std::string; using std::cerr; using std::cout; +using std::ofstream; using boost::optional; +using boost::shared_ptr; + +static void convert_dcp ( + boost::filesystem::path input, + boost::filesystem::path output_file, + boost::optional kdm, + int crf + ); +static void convert_ffmpeg (boost::filesystem::path input, boost::filesystem::path output_file, string format); +static void write_kdm (string id, boost::filesystem::path name, dcp::Key key); static void help (string n) { - cerr << "Syntax: " << n << " [OPTION] \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" + << " -k, --kdm DCP KDM filename (if required)\n" + << " -c, --crf quality (CRF) when transcoding from DCP (0 is best, 51 is worst, defaults to 23)\n" << "\n" - << " is the unencrypted .mp4 file.\n"; + << " is an unencrypted .mp4 file; is a DCP directory.\n"; } int main (int argc, char* argv[]) { optional output; + optional format; + optional kdm; + int crf = 23; 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'}, + { "version", no_argument, 0, 'v' }, + { "help", no_argument, 0, 'h' }, + { "output", required_argument, 0, 'o' }, + { "format", required_argument, 0, 'f' }, + { "kdm", required_argument, 0, 'k' }, + { "crf", required_argument, 0, 'c' }, }; - int c = getopt_long (argc, argv, "vho:", long_options, &option_index); + int c = getopt_long (argc, argv, "vho:f:k:c:", long_options, &option_index); if (c == -1) { break; @@ -80,6 +110,15 @@ main (int argc, char* argv[]) case 'o': output = optarg; break; + case 'f': + format = optarg; + break; + case 'k': + kdm = optarg; + break; + case 'c': + crf = atoi(optarg); + break; } } @@ -93,8 +132,26 @@ main (int argc, char* argv[]) 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); + } + + dcpomatic_setup_path_encoding (); + dcpomatic_setup (); + signal_manager = new SignalManager (); + boost::filesystem::path input = argv[optind]; - boost::filesystem::path output_mp4 = *output / (input.filename().string() + ".ecinema"); + boost::filesystem::path output_file; + if (boost::filesystem::is_directory(input)) { + output_file = *output / (input.parent_path().filename().string() + ".ecinema"); + } else { + output_file = *output / (input.filename().string() + ".ecinema"); + } if (!boost::filesystem::is_directory(*output)) { boost::filesystem::create_directory (*output); @@ -102,6 +159,17 @@ main (int argc, char* argv[]) av_register_all (); + if (boost::filesystem::is_directory(input)) { + /* Assume input is a DCP */ + convert_dcp (input, output_file, kdm, crf); + } else { + convert_ffmpeg (input, output_file, *format); + } +} + +static void +convert_ffmpeg (boost::filesystem::path input, boost::filesystem::path output_file, string format) +{ 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"; @@ -114,20 +182,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; @@ -140,6 +209,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 */ @@ -147,14 +220,23 @@ 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, &options) < 0) { cerr << "Could not write header to output.\n"; @@ -180,7 +262,51 @@ main (int argc, char* argv[]) avformat_free_context (input_fc); avformat_free_context (output_fc); - DecryptedECinemaKDM decrypted_kdm (key); - EncryptedECinemaKDM encrypted_kdm = decrypted_kdm.encrypt (Config::instance()->decryption_chain()); - cout << encrypted_kdm.as_xml() << "\n"; + write_kdm (id, output_file, key); +} + +static void +write_kdm (string id, boost::filesystem::path name, dcp::Key key) +{ + DecryptedECinemaKDM decrypted_kdm (id, name.filename().string(), key, optional(), optional()); + EncryptedECinemaKDM encrypted_kdm = decrypted_kdm.encrypt (Config::instance()->decryption_chain()->leaf()); + + ofstream f(string(name.string() + ".xml").c_str()); + f << encrypted_kdm.as_xml() << "\n"; +} + +static void +convert_dcp ( + boost::filesystem::path input, boost::filesystem::path output_file, optional kdm, int crf + ) +{ + shared_ptr film (new Film(boost::optional())); + shared_ptr dcp (new DCPContent(input)); + film->examine_and_add_content (dcp); + if (kdm) { + dcp->add_kdm (dcp::EncryptedKDM(dcp::file_to_string(*kdm))); + } + + JobManager* jm = JobManager::instance (); + while (jm->work_to_do ()) { + while (signal_manager->ui_idle ()) {} + dcpomatic_sleep_seconds (1); + } + DCPOMATIC_ASSERT (!jm->errors()); + + film->set_container (Ratio::nearest_from_ratio(dcp->video->size().ratio())); + + string id = dcp::make_uuid (); + dcp::Key key (AES_CTR_KEY_SIZE); + + shared_ptr job (new TranscodeJob(film)); + job->set_encoder ( + shared_ptr( + new FFmpegEncoder(film, job, output_file, EXPORT_FORMAT_H264_PCM, false, false, crf, key, id) + ) + ); + jm->add (job); + show_jobs_on_console (true); + + write_kdm (id, output_file, key); }