| 1 | #include <cvd/byte.h> |
| 2 | #include <cvd/colourspaces.h> |
| 3 | #include <cvd/videosource.h> |
| 4 | |
| 5 | namespace CVD |
| 6 | { |
| 7 | using namespace std; |
| 8 | |
| 9 | void tolower(std::string& s) |
| 10 | { |
| 11 | for(size_t i = 0; i < s.length(); ++i) |
| 12 | s[i] = static_cast<char>(::tolower(s[i])); |
| 13 | } |
| 14 | |
| 15 | void tolower(char* s) |
| 16 | { |
| 17 | for(size_t i = 0; s[i]; ++i) |
| 18 | s[i] = static_cast<char>(::tolower(s[i])); |
| 19 | } |
| 20 | |
| 21 | using std::tolower; |
| 22 | |
| 23 | //If the flag is specified at all, but no parameter is |
| 24 | //given, this implies true. |
| 25 | bool parseBoolFlag(std::string s) |
| 26 | { |
| 27 | tolower(s); |
| 28 | if(s == "" || s == "yes" || s == "true" || s == "1") |
| 29 | return 1; |
| 30 | else if(s == "no" || s == "false" || s == "0") |
| 31 | return 0; |
| 32 | else |
| 33 | throw ParseException("invalid interlaced/fields setting '" + s + "' (must be true/false or yes/no or 1/0)"); |
| 34 | } |
| 35 | |
| 36 | ImageRef parseImageRef(string s, bool is_size) |
| 37 | { |
| 38 | ImageRef size; |
| 39 | tolower(s); |
| 40 | if(is_size && s == "vga") |
| 41 | size = ImageRef(640, 480); |
| 42 | else if(is_size && s == "qvga") |
| 43 | size = ImageRef(320, 240); |
| 44 | else if(is_size && s == "pal") |
| 45 | size = ImageRef(720, 576); |
| 46 | else if(is_size && s == "ntsc") |
| 47 | size = ImageRef(720, 480); |
| 48 | else if(is_size && s == "xga") |
| 49 | size = ImageRef(1024, 768); |
| 50 | else |
| 51 | { |
| 52 | std::istringstream size_in(s); |
| 53 | char x; |
| 54 | if(!(size_in >> size.x >> x >> size.y)) |
| 55 | { |
| 56 | if(is_size) |
| 57 | throw ParseException("invalid image size specification: '" + s + "'\n\t valid specs: qvga, vga, pal, ntsc, xga, <width>x<height>"); |
| 58 | else |
| 59 | throw ParseException("invalid image position specification: '" + s + "'\n\t valid specs: <width>x<height>"); |
| 60 | } |
| 61 | } |
| 62 | return size; |
| 63 | } |
| 64 | |
| 65 | std::string escape(char c) |
| 66 | { |
| 67 | static std::string escaped[256]; |
| 68 | static bool init = false; |
| 69 | if(!init) |
| 70 | { |
| 71 | for(int i = 0; i < 256; ++i) |
| 72 | escaped[i] = std::string(1, static_cast<char>(i)); |
| 73 | escaped[(unsigned char)'\a'] = "\\a"; |
| 74 | escaped[(unsigned char)'\b'] = "\\b"; |
| 75 | escaped[(unsigned char)'\f'] = "\\f"; |
| 76 | escaped[(unsigned char)'\n'] = "\\n"; |
| 77 | escaped[(unsigned char)'\r'] = "\\r"; |
| 78 | escaped[(unsigned char)'\t'] = "\\t"; |
| 79 | escaped[(unsigned char)'\v'] = "\\v"; |
| 80 | escaped[(unsigned char)'\\'] = "\\\\"; |
| 81 | escaped[(unsigned char)'\''] = "\\'"; |
| 82 | escaped[(unsigned char)'\"'] = "\\\""; |
| 83 | init = true; |
| 84 | } |
| 85 | return escaped[(unsigned char)c]; |
| 86 | } |
| 87 | |
| 88 | std::string unescape(const std::string& s) |
| 89 | { |
| 90 | static const char mapping[][2] = { { 'a', '\a' }, |
| 91 | { 'b', '\b' }, |
| 92 | { 'f', '\f' }, |
| 93 | { 'n', '\n' }, |
| 94 | { 'r', '\r' }, |
| 95 | { 't', '\t' }, |
| 96 | { 'v', '\v' }, |
| 97 | { '\\', '\\' }, |
| 98 | { '\'', '\'' }, |
| 99 | { '"', '"' }, |
| 100 | { 0, 0 } }; |
| 101 | std::string ret; |
| 102 | ret.reserve(s.length()); |
| 103 | for(size_t i = 0; i < s.length(); ++i) |
| 104 | { |
| 105 | if(s[i] == '\\') |
| 106 | { |
| 107 | if(i + 1 == s.length()) |
| 108 | throw ParseException("illegal '\\' terminating literal"); |
| 109 | char escape = s[i + 1]; |
| 110 | if(isdigit(escape)) |
| 111 | { |
| 112 | if(i + 3 >= s.length() || (s[i + 2] < '0' || s[i + 2] > '7') || (s[i + 3] < '0' || s[i + 3] > '7')) |
| 113 | throw ParseException("partial octal character code; need three digits"); |
| 114 | int code = (escape - '0') * 64 + (s[i + 2] - '0') * 8 + s[i + 3] - '0'; |
| 115 | if(code > 255) |
| 116 | throw ParseException("invalid octal character code; must be in [000,377]"); |
| 117 | ret += static_cast<char>(code); |
| 118 | i += 3; |
| 119 | } |
| 120 | else if(escape == 'h') |
| 121 | { |
| 122 | if(i + 3 >= s.length() || !isxdigit(s[i + 2]) || !isxdigit(s[i + 3])) |
| 123 | throw ParseException("partial hex character code; need two hex digits"); |
| 124 | int code = (isdigit(s[i + 2]) ? (s[i + 2] - '0') : (tolower(s[i + 2]) - 'a' + 10)) * 16 + (isdigit(s[i + 3]) ? (s[i + 3] - '0') : (tolower(s[i + 3]) - 'a' + 10)); |
| 125 | ret += static_cast<char>(code); |
| 126 | i += 3; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | int j = 0; |
| 131 | while(mapping[j][0] && mapping[j][0] != s[i + 1]) |
| 132 | ++j; |
| 133 | if(!mapping[j][0]) |
| 134 | throw ParseException("unknown escape sequence"); |
| 135 | ret += mapping[j][1]; |
| 136 | i += 1; |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | ret += s[i]; |
| 141 | } |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | std::ostream& operator<<(std::ostream& out, const VideoSource& vs) |
| 146 | { |
| 147 | out << vs.protocol << ":"; |
| 148 | if(!vs.options.empty()) |
| 149 | { |
| 150 | out << "[ "; |
| 151 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 152 | { |
| 153 | out << it->first << "=" << it->second << ", "; |
| 154 | } |
| 155 | out << "]"; |
| 156 | } |
| 157 | out << "//" << vs.identifier; |
| 158 | return out; |
| 159 | } |
| 160 | |
| 161 | void match(std::istream& in, char c) |
| 162 | { |
| 163 | if(in.peek() != c) |
| 164 | { |
| 165 | std::ostringstream oss; |
| 166 | std::string x; |
| 167 | |
| 168 | if(in.peek() == -1) |
| 169 | x = "<EOF>"; |
| 170 | else |
| 171 | x = escape(static_cast<char>(in.peek())); |
| 172 | |
| 173 | oss << "expected '" << c << "', got '" << x << "'"; |
| 174 | throw ParseException(oss.str()); |
| 175 | } |
| 176 | in.ignore(); |
| 177 | } |
| 178 | |
| 179 | void skip_ws(std::istream& in) |
| 180 | { |
| 181 | while(isspace(in.peek())) |
| 182 | in.ignore(); |
| 183 | } |
| 184 | |
| 185 | std::string read_quoted_literal(std::istream& in) |
| 186 | { |
| 187 | std::string value; |
| 188 | match(in, '"'); |
| 189 | while(in.peek() != '"') |
| 190 | { |
| 191 | int cc = in.get(); |
| 192 | if(cc == -1) |
| 193 | throw ParseException("unmatched open quote"); |
| 194 | char c = static_cast<char>(cc); |
| 195 | value += c; |
| 196 | if(c == '\\') |
| 197 | { |
| 198 | int c2 = in.get(); |
| 199 | if(c2 == -1) |
| 200 | throw ParseException("unmatched open quote"); |
| 201 | value += static_cast<char>(c2); |
| 202 | } |
| 203 | } |
| 204 | match(in, '"'); |
| 205 | return value; |
| 206 | } |
| 207 | |
| 208 | std::string read_word(std::istream& in) |
| 209 | { |
| 210 | std::string word; |
| 211 | while(isalnum(in.peek()) || in.peek() == '_') |
| 212 | word += static_cast<char>(in.get()); |
| 213 | return word; |
| 214 | } |
| 215 | |
| 216 | std::string read_path(std::istream& in) |
| 217 | { |
| 218 | std::string word; |
| 219 | int c; |
| 220 | while((c = in.get()) != -1) |
| 221 | word += (char)c; |
| 222 | return word; |
| 223 | } |
| 224 | |
| 225 | void parse(std::istream& in, VideoSource& vs) |
| 226 | { |
| 227 | std::string prot; |
| 228 | skip_ws(in); |
| 229 | while(isalnum(in.peek())) |
| 230 | prot += static_cast<char>(in.get()); |
| 231 | if(prot.length() == 0) |
| 232 | throw ParseException("protocol must not be empty"); |
| 233 | vs.protocol = prot; |
| 234 | //std::cerr << "prot" << std::endl; |
| 235 | if(in.peek() != ':') |
| 236 | throw ParseException("expected ':' after protocol"); |
| 237 | in.ignore(); |
| 238 | if(in.peek() == '[') |
| 239 | { |
| 240 | in.ignore(); |
| 241 | while(true) |
| 242 | { |
| 243 | skip_ws(in); |
| 244 | std::string name = read_word(in); |
| 245 | tolower(name); |
| 246 | //std::cerr << "opt: " << name << std::endl; |
| 247 | skip_ws(in); |
| 248 | |
| 249 | if(in.peek() != '=') |
| 250 | vs.options.push_back(std::make_pair(name, "")); |
| 251 | else |
| 252 | { |
| 253 | in.ignore(); |
| 254 | skip_ws(in); |
| 255 | if(in.peek() == '"') |
| 256 | { |
| 257 | vs.options.push_back(std::make_pair(name, unescape(read_quoted_literal(in)))); |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | vs.options.push_back(std::make_pair(name, read_word(in))); |
| 262 | } |
| 263 | } |
| 264 | skip_ws(in); |
| 265 | if(in.peek() != ',') |
| 266 | break; |
| 267 | in.ignore(); |
| 268 | } |
| 269 | skip_ws(in); |
| 270 | match(in, ']'); |
| 271 | //std::cerr << "options" << std::endl; |
| 272 | } |
| 273 | match(in, '/'); |
| 274 | match(in, '/'); |
| 275 | //std::cerr << "slashes" << std::endl; |
| 276 | if(in.peek() == '"') |
| 277 | { |
| 278 | std::string id = read_quoted_literal(in); |
| 279 | vs.identifier = unescape(id); |
| 280 | //std::cerr << "quoted id" << std::endl; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | std::string id = read_path(in); |
| 285 | vs.identifier = id; |
| 286 | //std::cerr << "id" << std::endl; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void get_deinterlace_options(const VideoSource& vs, DeinterlaceBufferFields::Fields& fields, bool& line_double) |
| 291 | { |
| 292 | |
| 293 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 294 | { |
| 295 | if(it->first == "oddonly" && parseBoolFlag(it->second) == true) |
| 296 | fields = DeinterlaceBufferFields::OddOnly; |
| 297 | else if(it->first == "evenonly" && parseBoolFlag(it->second) == true) |
| 298 | fields = DeinterlaceBufferFields::EvenOnly; |
| 299 | else if(it->first == "oddeven" && parseBoolFlag(it->second) == true) |
| 300 | fields = DeinterlaceBufferFields::OddEven; |
| 301 | else if(it->first == "evenodd" && parseBoolFlag(it->second) == true) |
| 302 | fields = DeinterlaceBufferFields::EvenOdd; |
| 303 | else if(it->first == "double" && parseBoolFlag(it->second) == true) |
| 304 | line_double = true; |
| 305 | else |
| 306 | throw VideoSourceException("invalid option for files protocol: " + it->first + "\n\t valid options: oddonly, evenonly, oddeven, evenodd, double"); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void get_skip_options(const VideoSource& vs, bool& do_seek, double& seek, int& drop) |
| 311 | { |
| 312 | |
| 313 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 314 | { |
| 315 | if(it->first == "seek") |
| 316 | { |
| 317 | do_seek = true; |
| 318 | seek = atof(it->second.c_str()); |
| 319 | } |
| 320 | else if(it->first == "drop") |
| 321 | { |
| 322 | drop = atoi(it->second.c_str()); |
| 323 | } |
| 324 | else |
| 325 | throw VideoSourceException("invalid option for files protocol: " + it->first + "\n\t valid options: seek=<time>, drop=<n>"); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void get_colourspace_options(const VideoSource& vs, string& colourspace) |
| 330 | { |
| 331 | colourspace = "mono"; |
| 332 | |
| 333 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 334 | { |
| 335 | if(it->first == "from") |
| 336 | { |
| 337 | colourspace = it->second; |
| 338 | } |
| 339 | else |
| 340 | throw VideoSourceException("invalid option for files protocol: " + it->first + "\n\t valid options: from"); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void get_files_options(const VideoSource& vs, int& fps, VideoBufferFlags::OnEndOfBuffer& eob) |
| 345 | { |
| 346 | fps = 30; |
| 347 | eob = VideoBufferFlags::RepeatLastFrame; |
| 348 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 349 | { |
| 350 | if(it->first == "fps") |
| 351 | { |
| 352 | fps = atoi(it->second.c_str()); |
| 353 | } |
| 354 | else if(it->first == "on_end") |
| 355 | { |
| 356 | if(it->second == "loop") |
| 357 | eob = VideoBufferFlags::Loop; |
| 358 | else if(it->second == "unset_pending") |
| 359 | eob = VideoBufferFlags::UnsetPending; |
| 360 | else if(it->second == "repeat_last") |
| 361 | eob = VideoBufferFlags::RepeatLastFrame; |
| 362 | else |
| 363 | throw VideoSourceException("invalid end-of-buffer behaviour: " + it->second + "\n\t valid options are repeat_last, unset_pending, loop"); |
| 364 | } |
| 365 | else |
| 366 | throw VideoSourceException("invalid option for files protocol: " + it->first + "\n\t valid options: read_ahead, on_end, fps"); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void get_v4l1_options(const VideoSource& vs, ImageRef& size) |
| 371 | { |
| 372 | size = ImageRef(0, 0); |
| 373 | |
| 374 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 375 | { |
| 376 | if(it->first == "size") |
| 377 | size = parseImageRef(it->second, true); |
| 378 | else |
| 379 | throw VideoSourceException("invalid option for 'v4l2' protocol: " + it->first + "\n\t valid options: size, input, interlaced, fields"); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void get_uvc_options(const VideoSource& vs, ImageRef& size, double& fps, bool& jpeg, bool& verbose) |
| 384 | { |
| 385 | size = ImageRef(640, 480); |
| 386 | fps = 30; |
| 387 | jpeg = 0; |
| 388 | verbose = 0; |
| 389 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 390 | { |
| 391 | if(it->first == "size") |
| 392 | size = parseImageRef(it->second, true); |
| 393 | else if(it->first == "fps") |
| 394 | { |
| 395 | fps = stod(it->second.c_str()); |
| 396 | } |
| 397 | else if(it->first == "verbose") |
| 398 | { |
| 399 | verbose = parseBoolFlag(it->second); |
| 400 | } |
| 401 | else if(it->first == "mjpeg" || it->first == "jpeg") |
| 402 | { |
| 403 | jpeg = parseBoolFlag(it->second); |
| 404 | } |
| 405 | else |
| 406 | throw VideoSourceException("invalid option for 'uvc' protocol: " + it->first + "\n\t valid options: size, fps, mjpeg, verbose"); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void get_v4l2_options(const VideoSource& vs, ImageRef& size, int& input, bool& interlaced, bool& verbose) |
| 411 | { |
| 412 | size = ImageRef(640, 480); |
| 413 | input = -1; |
| 414 | interlaced = false; |
| 415 | verbose = 0; |
| 416 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 417 | { |
| 418 | if(it->first == "size") |
| 419 | size = parseImageRef(it->second, true); |
| 420 | else if(it->first == "input") |
| 421 | { |
| 422 | input = atoi(it->second.c_str()); |
| 423 | } |
| 424 | else if(it->first == "verbose") |
| 425 | { |
| 426 | verbose = parseBoolFlag(it->second); |
| 427 | } |
| 428 | else if(it->first == "interlaced" || it->first == "fields") |
| 429 | { |
| 430 | interlaced = parseBoolFlag(it->second); |
| 431 | } |
| 432 | else |
| 433 | throw VideoSourceException("invalid option for 'v4l2' protocol: " + it->first + "\n\t valid options: size, input, interlaced, fields"); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void get_file_options(const VideoSource& vs, VideoBufferFlags::OnEndOfBuffer& eob, bool& verbose, string& fmtname, map<string, string>& opts) |
| 438 | { |
| 439 | eob = VideoBufferFlags::RepeatLastFrame; |
| 440 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 441 | { |
| 442 | if(it->first == "on_end") |
| 443 | { |
| 444 | if(it->second == "loop") |
| 445 | eob = VideoBufferFlags::Loop; |
| 446 | else if(it->second == "unset_pending") |
| 447 | eob = VideoBufferFlags::UnsetPending; |
| 448 | else if(it->second == "repeat_last") |
| 449 | eob = VideoBufferFlags::RepeatLastFrame; |
| 450 | else |
| 451 | throw VideoSourceException("invalid end-of-buffer behaviour: " + it->second + "\n\t valid options are repeat_last, unset_pending, loop"); |
| 452 | } |
| 453 | else if(it->first == "verbose") |
| 454 | verbose = true; |
| 455 | else if(it->first == "format" || it->first == "fmt" || it->first == "f") |
| 456 | fmtname = it->second; |
| 457 | else |
| 458 | opts[it->first] = it->second; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void get_dc1394_options(const VideoSource& vs, ImageRef& size, float& fps, ImageRef& offset, bool& verbose, bool& bus_reset, int& format7_mode) |
| 463 | { |
| 464 | size = offset = ImageRef(-1, -1); |
| 465 | fps = -1; |
| 466 | verbose = 0; |
| 467 | bus_reset = 0; |
| 468 | format7_mode = -1; |
| 469 | |
| 470 | for(VideoSource::option_list::const_iterator it = vs.options.begin(); it != vs.options.end(); ++it) |
| 471 | { |
| 472 | if(it->first == "fps") |
| 473 | fps = stof(it->second); |
| 474 | else if(it->first == "size") |
| 475 | size = parseImageRef(it->second, true); |
| 476 | else if(it->first == "offset") |
| 477 | offset = parseImageRef(it->second, false); |
| 478 | else if(it->first == "verbose") |
| 479 | verbose = parseBoolFlag(it->second); |
| 480 | else if(it->first == "reset") |
| 481 | bus_reset = parseBoolFlag(it->second); |
| 482 | else if(it->first == "mode" || it->first == "format7_mode" || it->first == "format7") |
| 483 | format7_mode = atoi(it->second.c_str()); |
| 484 | else |
| 485 | throw VideoSourceException("invalid option for dc1394 protocol: " + it->first + "\n\t valid options: fps, size, offset, verbose"); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | } |