summaryrefslogtreecommitdiff
path: root/decompressor.hpp
blob: 63f3cb4d1f46715653e0e4ca98edb48a33826a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Copyright 2014-2016 Jan Huwald, Stephan Richter

   This file is part of HRTC.

   HRTC is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   HRTC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with this program (see file LICENSE).  If not, see
   <http://www.gnu.org/licenses/>. */

#pragma once 

#include "common.hpp"
#include "num_util.hpp"

struct DecompTrajState {
  Time t0, dt;
  int x0, dx;

  template<typename Real>
  Real get(Time t1, Real quantum) {
    return dt 
      ?                   quant2real<Real>(x0, quantum)
        + Real(t1 - t0) * quant2real<Real>(dx, quantum) / dt
      : quant2real<Real>(x0, quantum);
  }
};

template<typename Real>
struct DecompressorState {
  TId numTraj;
  Real quantum;

  DecompTrajState *trajState;
  priority_queue<STP, priority_queue<STP>::container_type, std::greater<STP>> expectedSegment;
  Time curTime;

	SplitSVIBuffer buf;
  uint64_t chunkSz, chunkCur;

  function<ChunkSize(char*)> chunkSrc;
  EncodingPtr decoder;

  // statistic helpers
#ifdef HACKY_STATS
  map<int, uint> stat_key_x, stat_dx, stat_dt;
#endif

  DecompressorState(TId numTraj, Real quantum,
		    uint64_t maxChunkSize, EncodingPtr decoder,
		    function<ChunkSize(char*)> chunkSrc)
  : numTraj(numTraj),
    quantum(quantum),
    trajState(new DecompTrajState[numTraj]),
    curTime(0),
    buf(decoder, maxChunkSize),
    chunkSz(0),
    chunkCur(0),
    chunkSrc(chunkSrc),
    decoder(decoder)
  {}

  bool readFrame(Real *trajDst) {
    if (!curTime)
      if (!readKeyFrame()) return false;
    while ((curTime == expectedSegment.top().time) && (chunkCur < chunkSz))
      readSegment();
    if (expectedSegment.top().time <= curTime)
      return false;
    // push data to trajDst
    if (trajDst) {
      for (int i=0; i<numTraj; i++)
	trajDst[i] = trajState[i].get<Real>(curTime, quantum);
    }
    curTime++;
    return true;
  }

  bool readKeyFrame() {
    // init expected segements
    uint8_t *raw_iv = new uint8_t[numTraj * sizeof(Real)];
    ChunkSize sz = chunkSrc((char*) raw_iv);
    if (!sz.raw) return false;
    uint bit_count = sz.raw / numTraj;
    assert(bit_count * numTraj == sz.raw);
    dynamic_bitset<uint8_t> iv(raw_iv, raw_iv + sz.compressed);
    delete[] raw_iv;

    for (int i=0; i<numTraj; i++) {
      uint32_t x_quant = 0;
      for (uint j=0; j<bit_count; j++)
	    x_quant |= decltype(x_quant)(iv[i * bit_count + j]) << j;

      STP stp;
      stp.id = i;
      stp.time = 1;
      expectedSegment.push(stp);
      
      DecompTrajState &traj = trajState[i];
      traj.t0 = 0;
      traj.x0 = unsigned2signed(x_quant);
      traj.dt = 0;
      traj.dx = 0;

#ifdef HACKY_STATS
      stat_key_x[traj.x0]++;
#endif
    }
    loadNextChunk();
    return true;
  }
  
  void readSegment() {
    assert(chunkCur < chunkSz);
    
    // update mentioned traj
    TId id = expectedSegment.top().id;
    SVI svi = buf.get(chunkCur);
    DecompTrajState &traj = trajState[id];
    traj.x0 += traj.dx;
    traj.t0 += traj.dt; assert(traj.t0 == curTime-1);
    traj.dt  = svi.dt + 1;
    traj.dx  = unsigned2signed(svi.v);

    // gather histogram data
#ifdef HACKY_STATS
    stat_dx[traj.dx]++;
    stat_dt[traj.dt]++;
#endif
    
    // add next expected point
    STP stp;
    stp.id = id;
    stp.time = curTime + traj.dt;
    expectedSegment.pop();
    expectedSegment.push(stp);
    
    chunkCur++;
    
    // fetch new trajectory data
    if (chunkCur == chunkSz)
      loadNextChunk();
  }

  void loadNextChunk() {
	  ChunkSize sz = chunkSrc((char*) buf.compressed); 
    chunkCur = 0;
    chunkSz = sz.raw / 2 / 4;
    if (chunkSz) {
	    buf.decode(chunkSz, sz.compressed / 4);
    }
    // NOTE: chunkCur == chunkSz is used to signal a failed load
  }

  ~DecompressorState() {
    delete[] trajState;

#ifdef HACKY_STATS
		for (auto stat : {make_tuple(stat_key_x, "stat_key_x"),
					make_tuple(stat_dx, "stat_dx"),
					make_tuple(stat_dt, "stat_dt")})
			for (auto i : get<0>(stat))
				cout << get<1>(stat) << " " << i.first << " " << i.second << endl;
#endif
  }
};
contact: Jan Huwald // Impressum