NHMLFixup v10
[jpcrr.git] / streamtools / dumpconvert.cpp
blob57627fb7e80b93c7f3a7a82a97a5f11eb6d286e7
1 #include "newpacket.hpp"
2 #include "misc.hpp"
3 #include "outputs/public.hpp"
4 #include "packet-processor.hpp"
5 #include "temporal-antialias.hpp"
6 #include <string>
7 #include <stdexcept>
8 #include "timeparse.hpp"
9 #include <iostream>
11 int real_main(int argc, char** argv)
13 int dropmode = 0;
14 double antialias_factor = 0;
15 bool sep = false;
16 bool seen_filenames = false;
17 struct packet_processor_parameters params;
19 //Initialize parameters.
20 params.audio_delay = 0;
21 params.subtitle_delay = 0;
22 params.audio_rate = 44100;
23 params.demux = NULL;
24 params.width = 0;
25 params.height = 0;
26 params.rate_num = 0;
27 params.rate_denum = 0;
28 params.dedup_max = 0;
29 params.frame_dropper = NULL;
30 params.outgroup = NULL;
33 for(int i = 1; i < argc; i++) {
34 std::string arg = argv[i];
35 if(arg == "--") {
36 sep = true;
37 if(i + 1 < argc)
38 seen_filenames = true; //There will be filenames.
39 break;
41 if(!isstringprefix(arg, "--")) {
42 seen_filenames = true; //This is a filename.
43 continue;
45 try {
46 if(isstringprefix(arg, "--video-width=")) {
47 std::string value = settingvalue(arg);
48 char* x;
49 params.width = strtoul(value.c_str(), &x, 10);
50 if(*x || !params.width) {
51 std::cerr << "--video-width: Bad width" << std::endl;
52 return 1;
54 } else if(isstringprefix(arg, "--video-height=")) {
55 std::string value = settingvalue(arg);
56 char* x;
57 params.height = strtoul(value.c_str(), &x, 10);
58 if(*x || !params.height) {
59 std::cerr << "--video-height: Bad height" << std::endl;
60 return 1;
62 } else if(isstringprefix(arg, "--video-scale-algo=")) {
63 //Processed later.
64 } else if(arg == "--video-framerate=auto") {
65 if(params.rate_denum) {
66 std::cerr << "Conflicts with earlier explicit fps: " << arg << "." << std::endl;
67 return 1;
69 params.rate_num = 1;
70 } else if(isstringprefix(arg, "--video-framerate=")) {
71 std::string value = settingvalue(arg);
72 char* x;
73 if(params.rate_num || params.rate_denum) {
74 std::cerr << "Conflicts with earlier fps: " << arg << "." << std::endl;
75 return 1;
77 params.rate_num = strtoul(value.c_str(), &x, 10);
78 if((*x != '\0' && *x != '/') || !params.rate_num) {
79 std::cerr << "--video-framerate: Bad value (n)" << std::endl;
80 return 1;
82 if(*x) {
83 x++;
84 params.rate_denum = strtoul(x, &x, 10);
85 if(*x || !params.rate_denum) {
86 std::cerr << "--video-framerate: Bad value (d)" << std::endl;
87 return 1;
89 } else
90 params.rate_denum = 1;
92 } else if(isstringprefix(arg, "--video-max-dedup=")) {
93 std::string value = settingvalue(arg);
94 char* x;
95 params.dedup_max = strtoul(value.c_str(), &x, 10);
96 if(*x) {
97 std::cerr << "--video-dedup-max: Bad value" << std::endl;
98 return 1;
100 if(params.rate_denum) {
101 std::cerr << "Conflicts with earlier explicit fps: " << arg << "." << std::endl;
102 return 1;
104 params.rate_num = 1;
105 } else if(isstringprefix(arg, "--audio-delay=")) {
106 std::string value = settingvalue(arg);
107 if(value.length() && value[0] == '-')
108 params.audio_delay = -(int64_t)parse_timespec(value.substr(1));
109 else
110 params.audio_delay = -(int64_t)parse_timespec(value);
111 } else if(isstringprefix(arg, "--subtitle-delay=")) {
112 std::string value = settingvalue(arg);
113 if(value.length() && value[0] == '-')
114 params.subtitle_delay = -(int64_t)parse_timespec(value.substr(1));
115 else
116 params.subtitle_delay = -(int64_t)parse_timespec(value);
117 } else if(isstringprefix(arg, "--video-temporalantialias=")) {
118 std::string value = settingvalue(arg);
119 char* x;
120 antialias_factor = strtod(value.c_str(), &x);
121 if(*x)
122 throw std::runtime_error("Bad blur factor");
123 dropmode = 1;
124 } else if(isstringprefix(arg, "--audio-mixer-")) {
125 //We process these later.
126 } else if(isstringprefix(arg, "--video-hardsub-")) {
127 //We process these later.
128 } else if(isstringprefix(arg, "--output-")) {
129 //We process these later.
130 } else {
131 std::cerr << "Bad option: " << arg << "." << std::endl;
132 return 1;
134 } catch(std::exception& e) {
135 std::cerr << "Error processing option: " << arg << ":" << e.what() << std::endl;
136 return 1;
141 if(!seen_filenames) {
142 std::cout << "usage: " << argv[0] << " [<options>] [--] <filename>..." << std::endl;
143 std::cout << "Convert <filename> to variety of raw formats." << std::endl;
144 std::cout << "--output-<type>=<file>[,<parameters>]" << std::endl;
145 std::cout << "\tSend <type> output to <file>." << std::endl;
146 std::cout << "\tSupported types: " << get_output_driver_string() << std::endl;
147 std::cout << "--audio-delay=<delay>" << std::endl;
148 std::cout << "\tSet audio delay to <delay> (may be negative). Default 0." << std::endl;
149 std::cout << "--subtitle-delay=<delay>" << std::endl;
150 std::cout << "\tSet subtitle delay to <delay> (may be negative). Default 0." << std::endl;
151 std::cout << "--video-width=<width>" << std::endl;
152 std::cout << "\tSet video width to <width>." << std::endl;
153 std::cout << "--video-height=<height>" << std::endl;
154 std::cout << "\tSet video width to <height>." << std::endl;
155 std::cout << "--video-scale-algo=<algo>" << std::endl;
156 std::cout << "\tSet video scaling algo to <algo>." << std::endl;
157 std::cout << "--video-scale-algo=<algo> <w> <h>" << std::endl;
158 std::cout << "\tSet video scaling algo for <w>x<h> frames to <algo>." << std::endl;
159 std::cout << "\tSupported algorithms: " << get_rescaler_string() << std::endl;
160 std::cout << "--video-scale-framerate=<n>[/<d>]" << std::endl;
161 std::cout << "\tSet video framerate to <n>/<d>." << std::endl;
162 std::cout << "--video-scale-framerate=auto" << std::endl;
163 std::cout << "\tSet video framerate to variable." << std::endl;
164 std::cout << "--video-max-dedup=<frames>" << std::endl;
165 std::cout << "\tSet maximum consequtive frames to elide to <frames>." << std::endl;
166 std::cout << "--video-temporalantialias=<factor>" << std::endl;
167 std::cout << "\tEnable temporal antialiasing with specified blur factor." << std::endl;
168 print_hardsubs_help("--video-hardsub-");
169 print_audio_resampler_help("--audio-mixer-");
170 return 1;
173 subtitle_set_resolution(params.width, params.height);
175 if(!params.rate_num && !params.rate_denum) {
176 params.rate_num = 60;
177 params.rate_denum = 1;
180 if(dropmode == 0)
181 params.frame_dropper = new framerate_reducer_dropframes();
182 else
183 params.frame_dropper = new framerate_reducer_temporalantialias(antialias_factor, params.rate_num,
184 params.rate_denum);
186 audio_settings asettings(params.audio_rate);
187 video_settings vsettings(params.width, params.height, params.rate_num, params.rate_denum);
188 subtitle_settings ssettings;
190 params.outgroup = new output_driver_group();
192 params.outgroup->set_audio_settings(asettings);
193 params.outgroup->set_video_settings(vsettings);
194 params.outgroup->set_subtitle_settings(ssettings);
196 sep = false;
197 for(int i = 1; i < argc; i++) {
198 std::string arg = argv[i];
199 if(arg == "--") {
200 sep = true;
201 break;
203 if(!isstringprefix(arg, "--"))
204 continue;
205 if(isstringprefix(arg, "--output-")) {
206 std::string type;
207 std::string file;
208 std::string parameters;
209 size_t x = arg.find_first_of("=");
210 if(x > arg.length()) {
211 std::cerr << "Bad output specification: " << arg << "." << std::endl;
212 return 1;
214 type = arg.substr(9, x - 9);
215 file = arg.substr(x + 1);
216 x = file.find_first_of(",");
217 if(x < file.length()) {
218 parameters = file.substr(x + 1);
219 file = file.substr(0, x);
221 params.outgroup->add_driver(type, file, parameters);
225 packet_processor& p = create_packet_processor(&params, argc, argv);
226 sep = false;
227 uint64_t timebase = 0;
228 for(int i = 1; i < argc; i++) {
229 std::string arg = argv[i];
230 if(arg == "--") {
231 sep = true;
232 continue;
234 if(sep || !isstringprefix(arg, "--")) {
235 read_channel rc(arg);
236 timebase = send_stream(p, rc, timebase);
239 p.send_end_of_stream();
240 params.outgroup->do_audio_end_callback();
241 delete params.outgroup;
242 delete &p;
243 delete params.frame_dropper;
244 return 0;