Fix some problems reported by Valgrind
[jpcrr.git] / streamtools / dumpconvert.cpp
bloba03822dac340a4957bc6ca4c91c81e656ad48949
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 real_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 framerate_reducer* dropper;
23 std::string resize_type = DEFAULT_RESIZE_TYPE;
24 std::map<std::pair<uint32_t, uint32_t>, std::string> special_resizers;
25 bool sep = false;
27 dropper = new framerate_reducer_dropframes();
29 for(int i = 1; i < argc; i++) {
30 std::string arg = argv[i];
31 if(arg == "--") {
32 sep = true;
33 break;
35 if(!isstringprefix(arg, "--"))
36 continue;
37 try {
38 if(isstringprefix(arg, "--video-width=")) {
39 std::string value = settingvalue(arg);
40 char* x;
41 _width = strtoul(value.c_str(), &x, 10);
42 if(*x || !_width) {
43 std::cerr << "--video-width: Bad width" << std::endl;
44 return 1;
46 } else if(isstringprefix(arg, "--video-height=")) {
47 std::string value = settingvalue(arg);
48 char* x;
49 _height = strtoul(value.c_str(), &x, 10);
50 if(*x || !_height) {
51 std::cerr << "--video-height: Bad height" << std::endl;
52 return 1;
54 } else if(isstringprefix(arg, "--video-scale-algo=")) {
55 std::string tmptype = settingvalue(arg);
56 uint32_t width = 0;
57 uint32_t height = 0;
58 size_t p = tmptype.find_first_of(" ");
59 if(p > tmptype.length())
60 resize_type = settingvalue(arg);
61 else {
62 std::string tmptype_tail = tmptype.substr(p + 1);
63 tmptype = tmptype.substr(0, p);
64 const char* x = tmptype_tail.c_str();
65 char* end;
66 width = (uint32_t)strtoul(x, &end, 10);
67 if(*end != ' ')
68 throw std::runtime_error("Bad resolution for resolution-dependent scaler (width).");
69 x = end + 1;
70 height = (uint32_t)strtoul(x, &end, 10);
71 if(*end != '\0')
72 throw std::runtime_error("Bad resolution for resolution-dependent scaler (height).");
73 special_resizers[std::make_pair(width, height)] = tmptype;
75 } else if(isstringprefix(arg, "--video-scale-algo-")) {
76 std::string tmptype = settingvalue(arg);
77 uint32_t width = 0;
78 uint32_t height = 0;
80 special_resizers[std::make_pair(width, height)] = tmptype;
81 } else if(arg == "--video-framerate=auto") {
82 if(_rate_denum) {
83 std::cerr << "Conflicts with earlier explicit fps: " << arg << "." << std::endl;
84 return 1;
86 _rate_num = 1;
87 } else if(isstringprefix(arg, "--video-framerate=")) {
88 std::string value = settingvalue(arg);
89 char* x;
90 if(_rate_num || _rate_denum) {
91 std::cerr << "Conflicts with earlier fps: " << arg << "." << std::endl;
92 return 1;
94 _rate_num = strtoul(value.c_str(), &x, 10);
95 if((*x != '\0' && *x != '/') || !_rate_num) {
96 std::cerr << "--video-framerate: Bad value (n)" << std::endl;
97 return 1;
99 if(*x) {
100 x++;
101 _rate_denum = strtoul(x, &x, 10);
102 if(*x || !_rate_denum) {
103 std::cerr << "--video-framerate: Bad value (d)" << std::endl;
104 return 1;
106 } else
107 _rate_denum = 1;
109 } else if(isstringprefix(arg, "--video-max-dedup=")) {
110 std::string value = settingvalue(arg);
111 char* x;
112 _dedup_max = strtoul(value.c_str(), &x, 10);
113 if(*x) {
114 std::cerr << "--video-dedup-max: Bad value" << std::endl;
115 return 1;
117 if(_rate_denum) {
118 std::cerr << "Conflicts with earlier explicit fps: " << arg << "." << std::endl;
119 return 1;
121 _rate_num = 1;
122 } else if(isstringprefix(arg, "--audio-delay=")) {
123 std::string value = settingvalue(arg);
124 if(value.length() && value[0] == '-')
125 _audio_delay = -(int64_t)parse_timespec(value.substr(1));
126 else
127 _audio_delay = -(int64_t)parse_timespec(value);
128 } else if(isstringprefix(arg, "--subtitle-delay=")) {
129 std::string value = settingvalue(arg);
130 if(value.length() && value[0] == '-')
131 _subtitle_delay = -(int64_t)parse_timespec(value.substr(1));
132 else
133 _subtitle_delay = -(int64_t)parse_timespec(value);
134 } else if(isstringprefix(arg, "--audio-mixer-")) {
135 //We process these later.
136 } else if(isstringprefix(arg, "--video-hardsub-")) {
137 //We process these later.
138 } else if(isstringprefix(arg, "--output-")) {
139 //We process these later.
140 } else {
141 std::cerr << "Bad option: " << arg << "." << std::endl;
142 return 1;
144 } catch(std::exception& e) {
145 std::cerr << "Error processing option: " << arg << ":" << e.what() << std::endl;
146 return 1;
151 if(!_width || !_height) {
152 std::cout << "usage: " << argv[0] << " [<options>] [--] <filename>..." << std::endl;
153 std::cout << "Convert <filename> to variety of raw formats." << std::endl;
154 std::cout << "--output-<type>=<file>[,<parameters>]" << std::endl;
155 std::cout << "\tSend <type> output to <file>." << std::endl;
156 std::cout << "\tSupported types: " << get_output_driver_list() << std::endl;
157 std::cout << "--audio-delay=<delay>" << std::endl;
158 std::cout << "\tSet audio delay to <delay> (may be negative). Default 0." << std::endl;
159 std::cout << "--subtitle-delay=<delay>" << std::endl;
160 std::cout << "\tSet subtitle delay to <delay> (may be negative). Default 0." << std::endl;
161 std::cout << "--video-width=<width>" << std::endl;
162 std::cout << "\tSet video width to <width>." << std::endl;
163 std::cout << "--video-height=<height>" << std::endl;
164 std::cout << "\tSet video width to <height>." << std::endl;
165 std::cout << "--video-scale-algo=<algo>" << std::endl;
166 std::cout << "\tSet video scaling algo to <algo>." << std::endl;
167 std::cout << "--video-scale-algo=<algo> <w> <h>" << std::endl;
168 std::cout << "\tSet video scaling algo for <w>x<h> frames to <algo>." << std::endl;
169 std::cout << "\tSupported algorithms: " << get_resizer_list() << std::endl;
170 std::cout << "--video-scale-framerate=<n>[/<d>]" << std::endl;
171 std::cout << "\tSet video framerate to <n>/<d>." << std::endl;
172 std::cout << "--video-scale-framerate=auto" << std::endl;
173 std::cout << "\tSet video framerate to variable." << std::endl;
174 std::cout << "--video-max-dedup=<frames>" << std::endl;
175 std::cout << "\tSet maximum consequtive frames to elide to <frames>." << std::endl;
176 print_hardsubs_help("--video-hardsub-");
177 print_audio_resampler_help("--audio-mixer-");
178 return 1;
182 if(!_rate_num && !_rate_denum) {
183 _rate_num = 60;
184 _rate_denum = 1;
187 audio_settings asettings(_audio_rate);
188 video_settings vsettings(_width, _height, _rate_num, _rate_denum);
189 subtitle_settings ssettings;
190 set_audio_parameters(asettings);
191 set_video_parameters(vsettings);
192 set_subtitle_parameters(ssettings);
195 sep = false;
196 for(int i = 1; i < argc; i++) {
197 std::string arg = argv[i];
198 if(arg == "--") {
199 sep = true;
200 break;
202 if(!isstringprefix(arg, "--"))
203 continue;
204 if(isstringprefix(arg, "--output-")) {
205 std::string type;
206 std::string file;
207 std::string parameters;
208 size_t x = arg.find_first_of("=");
209 if(x > arg.length()) {
210 std::cerr << "Bad output specification: " << arg << "." << std::endl;
211 return 1;
213 type = arg.substr(9, x - 9);
214 file = arg.substr(x + 1);
215 x = file.find_first_of(",");
216 if(x < file.length()) {
217 parameters = file.substr(x + 1);
218 file = file.substr(0, x);
220 add_output_driver(type, file, parameters);
224 packet_processor& p = create_packet_processor(_audio_delay, _subtitle_delay, _audio_rate, _width, _height,
225 _rate_num, _rate_denum, _dedup_max, resize_type, special_resizers, argc, argv, dropper);
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 close_output_drivers();
241 delete &p;
242 delete dropper;
243 return 0;