Fix integer overflow in ft_rendered_size_line
[ilaris-y4m-tools.git] / rawtoy4m.cpp
blob454d2468de9d353e3cb728063c009187828b1d72
1 #include "yuv4mpeg.hpp"
2 #include "parseval.hpp"
3 #include <iostream>
5 int main(int argc, char** argv)
7 const char* interlace_types = "bpt";
8 size_t width = 0, height = 0;
9 uint32_t fps_n = 0, fps_d = 0;
10 uint32_t sar_n = 0, sar_d = 0;
11 int interlace = 0;
12 for(int i = 1; i < argc; i++) {
13 std::string arg = argv[i];
14 regex_results r;
15 if(r = regex("--resolution=([1-9][0-9]*)x([1-9][0-9]*)", arg)) {
16 width = parse_value<uint32_t>(r[1]);
17 height = parse_value<uint32_t>(r[2]);
18 } else if(r = regex("--fps=([1-9][0-9]*)/([1-9][0-9]*)", arg)) {
19 fps_n = parse_value<uint32_t>(r[1]);
20 fps_d = parse_value<uint32_t>(r[2]);
21 } else if(r = regex("--fps=([1-9][0-9]*)", arg)) {
22 fps_n = parse_value<uint32_t>(r[1]);
23 fps_d = 1;
24 } else if(r = regex("--sar=([1-9][0-9]*)/([1-9][0-9]*)", arg)) {
25 sar_n = parse_value<uint32_t>(r[1]);
26 sar_d = parse_value<uint32_t>(r[2]);
27 } else if(r = regex("--progressive", arg)) {
28 interlace = 0;
29 } else if(r = regex("--interlace=top", arg)) {
30 interlace = 1;
31 } else if(r = regex("--interlace=bottom", arg)) {
32 interlace = -1;
33 } else {
34 std::cerr << "rawtoy4m: Unrecognized option '" << arg << "'" << std::endl;
35 return 1;
38 if(!width || !height) {
39 std::cerr << "rawtoy4m: --resolution=<w>x<h> required" << std::endl;
40 return 1;
43 //Open files.
44 FILE* in = stdin;
45 FILE* out = stdout;
46 mark_pipe_as_binary(in);
47 mark_pipe_as_binary(out);
49 //Write header.
50 struct yuv4mpeg_stream_header strmh;
51 strmh.width = width;
52 strmh.height = height;
53 strmh.chroma = "rgb";
54 strmh.interlace = interlace_types[interlace + 1];
55 strmh.fps_n = fps_n;
56 strmh.fps_d = fps_d;
57 strmh.sar_n = sar_n;
58 strmh.sar_d = sar_d;
59 std::string _strmh = std::string(strmh);
60 write_or_die(out, _strmh);
62 std::vector<char> buffer;
63 buffer.resize(3 * width * height);
64 //Write frames.
65 while(true) {
66 if(!read_or_die2(in, &buffer[0], buffer.size()))
67 break;
68 struct yuv4mpeg_frame_header framh;
69 std::string _framh = std::string(framh);
70 write_or_die(out, _framh);
71 write_or_die(out, &buffer[0], buffer.size());
73 return 0;