Allow omitting video size when only processing audio
[jpcrr.git] / streamtools / fmtopcm.cpp
blob31c051cc7de0d7fdcf93dea63d94e9282980d08a
1 #include "newpacket.hpp"
2 #include "timeparse.hpp"
3 #include "timecounter.hpp"
4 #include "resampler.hpp"
5 #include <iostream>
6 #include <sstream>
7 #include <list>
8 #include <map>
9 #include <vector>
10 #include <cstdio>
11 #include <cstring>
12 #include <stdexcept>
13 #include <algorithm>
15 int real_main(int argc, char** argv)
17 std::map<std::string, uint16_t> channel_assignments;
18 std::vector<struct channel> chans;
19 uint32_t converted_channel = 0;
20 uint16_t converted_channel_out;
21 if(argc != 5) {
22 std::cerr << "syntax: fmtopcm.exe <input> <channel> <rate> <output>" << std::endl;
23 exit(1);
26 read_channel rchan(argv[1]);
27 write_channel wchan(argv[4]);
28 converted_channel = rchan.number_for_channel(argv[2]);
29 timecounter counter(argv[3]);
30 resampler_fm resampler(atoi(argv[3]));
32 //Create output channel.
33 channel_assignments[argv[2]] = converted_channel_out = (uint16_t)chans.size();
34 struct channel c;
35 c.c_channel = (uint16_t)chans.size();
36 c.c_type = 1;
37 c.c_channel_name = argv[2];
38 chans.push_back(c);
39 wchan.start_segment(chans);
41 packet* p;
42 packet p2;
43 while((p = rchan.read())) {
44 //Extract output...
45 while((uint64_t)counter <= p->rp_timestamp) {
46 sample_number_t out = resampler.nextsample();
47 p2.rp_timestamp = counter;
48 p2.rp_channel = converted_channel_out;
49 p2.rp_minor = 1;
50 p2.rp_payload.resize(4);
51 unsigned short x = (unsigned short)out.get_x();
52 unsigned short y = (unsigned short)out.get_y();
53 p2.rp_payload[0] = (x >> 8) & 0xFF;
54 p2.rp_payload[1] = x & 0xFF;
55 p2.rp_payload[2] = (y >> 8) & 0xFF;
56 p2.rp_payload[3] = y & 0xFF;
57 wchan.write(p2);
58 counter++;
60 //Send input...
61 if(p->rp_channel_perm == converted_channel && p->rp_minor > 0) {
62 resampler.sendpacket(*p);
63 delete p;
64 continue;
67 if(!channel_assignments.count(p->rp_channel_name)) {
68 //No channel yet, create.
69 channel_assignments[p->rp_channel_name] = (uint16_t)chans.size();
70 struct channel c;
71 c.c_channel = (uint16_t)chans.size();
72 c.c_type = p->rp_major;
73 c.c_channel_name = p->rp_channel_name;
74 chans.push_back(c);
75 wchan.start_segment(chans);
77 uint16_t chan = channel_assignments[p->rp_channel_name];
78 if(chans[chan].c_type != p->rp_major && p->rp_channel_perm != converted_channel) {
79 //Change type.
80 chans[chan].c_type = p->rp_major;
81 wchan.start_segment(chans);
83 p->rp_channel = chan;
84 wchan.write(*p);
85 delete p;
88 return 0;