bcd0907263311999869c0d9913c8fe69a42f2e57
[dcpomatic.git] / src / tools / dcpomatic_ecinema.cc
1 /*
2     Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "lib/version.h"
22 #include "lib/decrypted_ecinema_kdm.h"
23 #include "lib/config.h"
24 #include <dcp/key.h>
25 extern "C" {
26 #include <libavformat/avformat.h>
27 #include <libavutil/aes_ctr.h>
28 }
29 #include <boost/filesystem.hpp>
30 #include <boost/optional.hpp>
31 #include <openssl/rand.h>
32 #include <getopt.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <string>
36 #include <iostream>
37
38 using std::string;
39 using std::cerr;
40 using std::cout;
41 using boost::optional;
42
43 static void
44 help (string n)
45 {
46         cerr << "Syntax: " << n << " [OPTION] <FILE>\n"
47              << "  -v, --version        show DCP-o-matic version\n"
48              << "  -h, --help           show this help\n"
49              << "  -o, --output         output directory\n"
50              << "\n"
51              << "<FILE> is the unencrypted .mp4 file.\n";
52 }
53
54 int
55 main (int argc, char* argv[])
56 {
57         optional<boost::filesystem::path> output;
58
59         int option_index = 0;
60         while (true) {
61                 static struct option long_options[] = {
62                         { "version", no_argument, 0, 'v'},
63                         { "help", no_argument, 0, 'h'},
64                         { "output", required_argument, 0, 'o'},
65                 };
66
67                 int c = getopt_long (argc, argv, "vho:", long_options, &option_index);
68
69                 if (c == -1) {
70                         break;
71                 }
72
73                 switch (c) {
74                 case 'v':
75                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
76                         exit (EXIT_SUCCESS);
77                 case 'h':
78                         help (argv[0]);
79                         exit (EXIT_SUCCESS);
80                 case 'o':
81                         output = optarg;
82                         break;
83                 }
84         }
85
86         if (optind >= argc) {
87                 help (argv[0]);
88                 exit (EXIT_FAILURE);
89         }
90
91         if (!output) {
92                 cerr << "You must specify --output-media or -o\n";
93                 exit (EXIT_FAILURE);
94         }
95
96         boost::filesystem::path input = argv[optind];
97         boost::filesystem::path output_mp4 = *output / (input.filename().string() + ".ecinema");
98
99         if (!boost::filesystem::is_directory(*output)) {
100                 boost::filesystem::create_directory (*output);
101         }
102
103         av_register_all ();
104
105         AVFormatContext* input_fc = avformat_alloc_context ();
106         if (avformat_open_input(&input_fc, input.string().c_str(), 0, 0) < 0) {
107                 cerr << "Could not open input file\n";
108                 exit (EXIT_FAILURE);
109         }
110
111         if (avformat_find_stream_info (input_fc, 0) < 0) {
112                 cerr << "Could not read stream information\n";
113                 exit (EXIT_FAILURE);
114         }
115
116         AVFormatContext* output_fc;
117         avformat_alloc_output_context2 (&output_fc, av_guess_format("mp4", 0, 0), 0, 0);
118
119         for (uint32_t i = 0; i < input_fc->nb_streams; ++i) {
120                 AVStream* is = input_fc->streams[i];
121                 AVStream* os = avformat_new_stream (output_fc, is->codec->codec);
122                 if (avcodec_copy_context (os->codec, is->codec) < 0) {
123                         cerr << "Could not set up output stream.\n";
124                         exit (EXIT_FAILURE);
125                 }
126
127                 switch (os->codec->codec_type) {
128                 case AVMEDIA_TYPE_VIDEO:
129                         os->time_base = is->time_base;
130                         os->avg_frame_rate = is->avg_frame_rate;
131                         os->r_frame_rate = is->r_frame_rate;
132                         os->sample_aspect_ratio = is->sample_aspect_ratio;
133                         os->codec->time_base = is->codec->time_base;
134                         os->codec->framerate = is->codec->framerate;
135                         os->codec->pix_fmt = is->codec->pix_fmt;
136                         break;
137                 case AVMEDIA_TYPE_AUDIO:
138                         os->codec->sample_fmt = is->codec->sample_fmt;
139                         os->codec->bits_per_raw_sample = is->codec->bits_per_raw_sample;
140                         os->codec->sample_rate = is->codec->sample_rate;
141                         os->codec->channel_layout = is->codec->channel_layout;
142                         os->codec->channels = is->codec->channels;
143                         break;
144                 default:
145                         /* XXX */
146                         break;
147                 }
148         }
149
150         if (avio_open2 (&output_fc->pb, output_mp4.string().c_str(), AVIO_FLAG_WRITE, 0, 0) < 0) {
151                 cerr << "Could not open output file `" << output_mp4.string() << "'\n";
152                 exit (EXIT_FAILURE);
153         }
154
155         dcp::Key key (AES_CTR_KEY_SIZE);
156         AVDictionary* options = 0;
157         av_dict_set (&options, "encryption_key", key.hex().c_str(), 0);
158
159         if (avformat_write_header (output_fc, &options) < 0) {
160                 cerr << "Could not write header to output.\n";
161                 exit (EXIT_FAILURE);
162         }
163
164         AVPacket packet;
165         while (av_read_frame(input_fc, &packet) >= 0) {
166                 AVStream* is = input_fc->streams[packet.stream_index];
167                 AVStream* os = output_fc->streams[packet.stream_index];
168                 packet.pts = av_rescale_q_rnd(packet.pts, is->time_base, os->time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
169                 packet.dts = av_rescale_q_rnd(packet.dts, is->time_base, os->time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
170                 packet.duration = av_rescale_q(packet.duration, is->time_base, os->time_base);
171                 packet.pos = -1;
172                 if (av_interleaved_write_frame (output_fc, &packet) < 0) {
173                         cerr << "Could not write frame to output.\n";
174                         exit (EXIT_FAILURE);
175                 }
176         }
177
178         av_write_trailer (output_fc);
179
180         avformat_free_context (input_fc);
181         avformat_free_context (output_fc);
182
183         DecryptedECinemaKDM decrypted_kdm (key);
184         EncryptedECinemaKDM encrypted_kdm = decrypted_kdm.encrypt (Config::instance()->decryption_chain());
185         cout << encrypted_kdm.as_xml() << "\n";
186 }