Fix integer overflow in ft_rendered_size_line
[ilaris-y4m-tools.git] / timeshow.cpp
bloba7a549e03b0ed001344fec13d78fb51dd5ca38a6
1 #include "yuv4mpeg.hpp"
2 #include "parseval.hpp"
3 #include <iostream>
5 int main(int argc, char** argv)
7 uint64_t last_interval = 0;
8 unsigned interval = 5;
9 size_t framesize = 0;
10 for(int i = 1; i < argc; i++) {
11 std::string arg = argv[i];
12 regex_results r;
13 if(r = regex("--interval=([1-9][0-9]*)", arg)) {
14 try {
15 interval = parse_value<unsigned>(r[1]);
16 } catch(std::exception& e) {
17 std::cerr << "timeshow: Bad interval '" << r[1] << "'" << std::endl;
18 return 1;
20 } else {
21 std::cerr << "timeshow: Unrecognized option '" << arg << "'" << std::endl;
22 return 1;
26 //Open files.
27 FILE* in = stdin;
28 mark_pipe_as_binary(in);
30 //Fixup header.
31 try {
32 struct yuv4mpeg_stream_header strmh(in);
33 if(strmh.chroma == "rgb")
34 framesize = 3 * strmh.width * strmh.height;
35 else if(strmh.chroma == "420")
36 framesize = 3 * strmh.width * strmh.height / 2;
37 else if(strmh.chroma == "420p16")
38 framesize = 6 * strmh.width * strmh.height / 2;
39 else if(strmh.chroma == "422")
40 framesize = 2 * strmh.width * strmh.height;
41 else if(strmh.chroma == "422p16")
42 framesize = 4 * strmh.width * strmh.height;
43 else if(strmh.chroma == "444")
44 framesize = 3 * strmh.width * strmh.height;
45 else if(strmh.chroma == "444p16")
46 framesize = 6 * strmh.width * strmh.height;
47 else
48 throw std::runtime_error("Unsupported input chroma type '" + strmh.chroma + "'");
49 bool vfr = strmh.find_extension("VFR");
51 uint64_t duration = 0;
52 std::string _framh, _framh2;
53 std::vector<char> buffer;
54 buffer.resize(framesize);
55 double framerate = (strmh.fps_n && strmh.fps_d) ? (1.0 * strmh.fps_n / strmh.fps_d) : 25;
56 //Loop frames.
57 while(true) {
58 if(!read_line2(in, _framh))
59 break;
60 read_or_die(in, &buffer[0], buffer.size());
61 struct yuv4mpeg_frame_header framh(_framh);
62 duration += (vfr ? framh.duration : 1);
63 uint64_t seconds = duration / framerate;
64 uint64_t cur_interval = seconds / interval;
65 if(cur_interval > last_interval) {
66 last_interval = cur_interval;
67 std::cerr << "timeshow: " << seconds << "s done.\e[K" << std::endl;
70 } catch(std::exception& e) {
71 std::cerr << "timeshow: Error: " << e.what() << std::endl;
72 return 1;
74 return 0;