Allow size-specific resizers
[jpcrr.git] / streamtools / dumpconvert.cpp
blob91b85a0d5a6861454763c220087f612940334eda
1 #include "newpacket.hpp"
2 #include "misc.hpp"
3 #include "output-drv.hpp"
4 #include "packet-processor.hpp"
5 #include <string>
6 #include <stdexcept>
7 #include "timeparse.hpp"
8 #include <iostream>
10 #define DEFAULT_RESIZE_TYPE "lanczos2"
12 int main(int argc, char** argv)
14 int64_t _audio_delay = 0;
15 int64_t _subtitle_delay = 0;
16 uint32_t _audio_rate = 44100;
17 uint32_t _width = 0;
18 uint32_t _height = 0;
19 uint32_t _rate_num = 0;
20 uint32_t _rate_denum = 0;
21 uint32_t _dedup_max = 0;
22 std::string resize_type = DEFAULT_RESIZE_TYPE;
23 std::map<std::pair<uint32_t, uint32_t>, std::string> special_resizers;
24 bool sep = false;
26 for(int i = 1; i < argc; i++) {
27 std::string arg = argv[i];
28 if(arg == "--") {
29 sep = true;
30 break;
32 if(!isstringprefix(arg, "--"))
33 continue;
34 try {
35 if(isstringprefix(arg, "--video-width=")) {
36 std::string value = settingvalue(arg);
37 char* x;
38 _width = strtoul(value.c_str(), &x, 10);
39 if(*x || !_width) {
40 std::cerr << "--video-width: Bad width" << std::endl;
41 return 1;
43 } else if(isstringprefix(arg, "--video-height=")) {
44 std::string value = settingvalue(arg);
45 char* x;
46 _height = strtoul(value.c_str(), &x, 10);
47 if(*x || !_height) {
48 std::cerr << "--video-height: Bad height" << std::endl;
49 return 1;
51 } else if(isstringprefix(arg, "--video-scale-algo=")) {
52 std::string tmptype = settingvalue(arg);
53 uint32_t width = 0;
54 uint32_t height = 0;
55 size_t p = tmptype.find_first_of(" ");
56 if(p > tmptype.length())
57 resize_type = settingvalue(arg);
58 else {
59 std::string tmptype_tail = tmptype.substr(p + 1);
60 tmptype = tmptype.substr(0, p);
61 const char* x = tmptype_tail.c_str();
62 char* end;
63 width = (uint32_t)strtoul(x, &end, 10);
64 if(*end != ' ')
65 throw std::runtime_error("Bad resolution for resolution-dependent scaler (width).");
66 x = end + 1;
67 height = (uint32_t)strtoul(x, &end, 10);
68 if(*end != '\0')
69 throw std::runtime_error("Bad resolution for resolution-dependent scaler (height).");
70 special_resizers[std::make_pair(width, height)] = tmptype;
72 } else if(isstringprefix(arg, "--video-scale-algo-")) {
73 std::string tmptype = settingvalue(arg);
74 uint32_t width = 0;
75 uint32_t height = 0;
77 special_resizers[std::make_pair(width, height)] = tmptype;
78 } else if(arg == "--video-framerate=auto") {
79 if(_rate_denum) {
80 std::cerr << "Conflicts with earlier explicit fps: " << arg << "." << std::endl;
81 return 1;
83 _rate_num = 1;
84 } else if(isstringprefix(arg, "--video-framerate=")) {
85 std::string value = settingvalue(arg);
86 char* x;
87 if(_rate_num || _rate_denum) {
88 std::cerr << "Conflicts with earlier fps: " << arg << "." << std::endl;
89 return 1;
91 _rate_num = strtoul(value.c_str(), &x, 10);
92 if((*x != '\0' && *x != '/') || !_rate_num) {
93 std::cerr << "--video-framerate: Bad value (n)" << std::endl;
94 return 1;
96 if(*x) {
97 _rate_denum = strtoul(x, &x, 10);
98 if(*x || !_rate_denum) {
99 std::cerr << "--video-framerate: Bad value (d)" << std::endl;
100 return 1;
102 } else
103 _rate_denum = 1;
105 } else if(isstringprefix(arg, "--video-max-dedup=")) {
106 std::string value = settingvalue(arg);
107 char* x;
108 _dedup_max = strtoul(value.c_str(), &x, 10);
109 if(*x) {
110 std::cerr << "--video-dedup-max: Bad value" << std::endl;
111 return 1;
113 if(_rate_denum) {
114 std::cerr << "Conflicts with earlier explicit fps: " << arg << "." << std::endl;
115 return 1;
117 _rate_num = 1;
118 } else if(isstringprefix(arg, "--audio-delay=")) {
119 std::string value = settingvalue(arg);
120 if(value.length() && value[0] == '-')
121 _audio_delay = -(int64_t)parse_timespec(value.substr(1));
122 else
123 _audio_delay = -(int64_t)parse_timespec(value);
124 } else if(isstringprefix(arg, "--subtitle-delay=")) {
125 std::string value = settingvalue(arg);
126 if(value.length() && value[0] == '-')
127 _subtitle_delay = -(int64_t)parse_timespec(value.substr(1));
128 else
129 _subtitle_delay = -(int64_t)parse_timespec(value);
130 } else if(isstringprefix(arg, "--audio-mixer-")) {
131 //We process these later.
132 } else if(isstringprefix(arg, "--video-hardsub-")) {
133 //We process these later.
134 } else if(isstringprefix(arg, "--output-")) {
135 //We process these later.
136 } else {
137 std::cerr << "Bad option: " << arg << "." << std::endl;
138 return 1;
140 } catch(std::exception& e) {
141 std::cerr << "Error processing option: " << arg << ":" << e.what() << std::endl;
142 return 1;
147 if(!_width || !_height) {
148 std::cout << "usage: " << argv[0] << " [<options>] [--] <filename>..." << std::endl;
149 std::cout << "Convert <filename> to variety of raw formats." << std::endl;
150 std::cout << "--output-<type>=<file>[,<parameters>]" << std::endl;
151 std::cout << "\tSend <type> output to <file>." << std::endl;
152 std::cout << "\tSupported types: " << get_output_driver_list() << std::endl;
153 std::cout << "--audio-delay=<delay>" << std::endl;
154 std::cout << "\tSet audio delay to <delay> (may be negative). Default 0." << std::endl;
155 std::cout << "--subtitle-delay=<delay>" << std::endl;
156 std::cout << "\tSet subtitle delay to <delay> (may be negative). Default 0." << std::endl;
157 std::cout << "--video-width=<width>" << std::endl;
158 std::cout << "\tSet video width to <width>." << std::endl;
159 std::cout << "--video-height=<height>" << std::endl;
160 std::cout << "\tSet video width to <height>." << std::endl;
161 std::cout << "--video-scale-algo=<algo>" << std::endl;
162 std::cout << "\tSet video scaling algo to <algo>." << std::endl;
163 std::cout << "--video-scale-algo=<algo> <w> <h>" << std::endl;
164 std::cout << "\tSet video scaling algo for <w>x<h> frames to <algo>." << std::endl;
165 std::cout << "\tSupported algorithms: " << get_resizer_list() << std::endl;
166 std::cout << "--video-scale-framerate=<n>[/<d>]" << std::endl;
167 std::cout << "\tSet video framerate to <n>/<d>." << std::endl;
168 std::cout << "--video-scale-framerate=auto" << std::endl;
169 std::cout << "\tSet video framerate to variable." << std::endl;
170 std::cout << "--video-max-dedup=<frames>" << std::endl;
171 std::cout << "\tSet maximum consequtive frames to elide to <frames>." << std::endl;
172 print_hardsubs_help("--video-hardsub-");
173 print_audio_resampler_help("--audio-mixer-");
174 return 1;
178 if(!_rate_num && !_rate_denum) {
179 _rate_num = 60;
180 _rate_denum = 1;
183 audio_settings asettings(_audio_rate);
184 video_settings vsettings(_width, _height, _rate_num, _rate_denum);
185 subtitle_settings ssettings;
186 set_audio_parameters(asettings);
187 set_video_parameters(vsettings);
188 set_subtitle_parameters(ssettings);
191 sep = false;
192 for(int i = 1; i < argc; i++) {
193 std::string arg = argv[i];
194 if(arg == "--") {
195 sep = true;
196 break;
198 if(!isstringprefix(arg, "--"))
199 continue;
200 if(isstringprefix(arg, "--output-")) {
201 std::string type;
202 std::string file;
203 std::string parameters;
204 size_t x = arg.find_first_of("=");
205 if(x > arg.length()) {
206 std::cerr << "Bad output specification: " << arg << "." << std::endl;
207 return 1;
209 type = arg.substr(9, x - 9);
210 file = arg.substr(x + 1);
211 x = file.find_first_of(",");
212 if(x < file.length()) {
213 parameters = file.substr(x + 1);
214 file = file.substr(0, x);
216 add_output_driver(type, file, parameters);
220 packet_processor& p = create_packet_processor(_audio_delay, _subtitle_delay, _audio_rate, _width, _height,
221 _rate_num, _rate_denum, _dedup_max, resize_type, special_resizers, argc, argv);
222 sep = false;
223 uint64_t timebase = 0;
224 for(int i = 1; i < argc; i++) {
225 std::string arg = argv[i];
226 if(arg == "--") {
227 sep = true;
228 continue;
230 if(sep || !isstringprefix(arg, "--")) {
231 read_channel rc(arg);
232 timebase = send_stream(p, rc, timebase);
235 p.send_end_of_stream();
236 close_output_drivers();