Streamtools: Refactor rescaling code
[jpcrr.git] / streamtools / guessresolution.cpp
blob22dad8d55ff6cb03a5148a5448666ee2180e22b2
1 #include <map>
2 #include <iostream>
3 #include <stdint.h>
4 #include "newpacket.hpp"
6 int real_main(int argc, char** argv)
8 uint64_t na_time = 0;
9 std::map<std::pair<uint32_t, uint32_t>, uint64_t> res_time;
10 uint32_t current_width = 0, current_height = 0;
11 uint64_t last_timestamp = 0;
12 uint64_t last_timestamp2 = 0;
13 uint64_t time_correction = 0;
15 if(argc == 1) {
16 std::cerr << "Syntax: guessresolution.exe <files>..." << std::endl;
17 return 1;
19 for(int i = 1; i < argc; i++) {
20 read_channel in(argv[i]);
21 time_correction = last_timestamp;
22 packet* p;
23 while((p = in.read())) {
24 p->rp_timestamp += time_correction;
25 last_timestamp = p->rp_timestamp;
26 if(current_width != 0 && current_height != 0) {
27 std::pair<uint32_t, uint32_t> res = std::make_pair(current_width, current_height);
28 if(!res_time.count(res))
29 res_time[res] = 0;
30 res_time[res] = res_time[res] + (p->rp_timestamp - last_timestamp2);
31 last_timestamp2 = p->rp_timestamp;
32 } else {
33 na_time = na_time + (p->rp_timestamp - last_timestamp2);
34 last_timestamp2 = p->rp_timestamp;
36 if(p->rp_major == 0 && p->rp_payload.size() >= 4) {
37 current_width = ((uint32_t)p->rp_payload[0] << 8) | p->rp_payload[1];
38 current_height = ((uint32_t)p->rp_payload[2] << 8) | p->rp_payload[3];
43 uint32_t gwidth = 0, gheight = 0;
44 uint64_t gtime = 0;
45 std::cout << "<no video data> for " << (na_time + 500000) / 1000000 << "ms." << std::endl;
46 for(std::map<std::pair<uint32_t, uint32_t>, uint64_t>::iterator j = res_time.begin(); j != res_time.end(); ++j) {
47 std::cout << j->first.first << "*" << j->first.second << " for " << (j->second + 500000) / 1000000 << "ms."
48 << std::endl;
49 if(j->second > gtime) {
50 gwidth = j->first.first;
51 gheight = j->first.second;
52 gtime = j->second;
55 if(gwidth)
56 std::cout << "Guessed resolution is " << gwidth << "*" << gheight << "." << std::endl;
57 else
58 std::cout << "Warning: No video data available." << std::endl;
59 return 0;