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