File: vpx_bool_writer.hh

package info (click to toggle)
lepton 1.2.1+20170405-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,248 kB
  • ctags: 3,431
  • sloc: cpp: 24,345; ansic: 7,872; python: 330; makefile: 246; sh: 92; perl: 30
file content (68 lines) | stat: -rw-r--r-- 2,122 bytes parent folder | download | duplicates (2)
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
#include "../util/options.hh"
#include "boolwriter.hh"
#include "../../io/MuxReader.hh"
class VPXBoolWriter
{
private:
    vpx_writer boolwriter;
    Sirikata::MuxReader::ResizableByteBuffer output_;
#ifdef DEBUG_ARICODER
    bool any_written;
#endif
    enum {
         MIN_SIZE = 1024 * 1024
    };
    enum {
        SIZE_CHECK  = 0xfff00000
    };
public:
  VPXBoolWriter() {
#ifdef DEBUG_ARICODER
        any_written = false;
#endif
        static_assert(MIN_SIZE & SIZE_CHECK,
                      "min size must be caught by the size check, so allocations happen after");
        static_assert(((MIN_SIZE - 1) & SIZE_CHECK) == 0,
                      "min size -1 must not be caught by the size check");
        boolwriter.lowvalue = 0;
        boolwriter.range = 0;
        boolwriter.count = 0;
        boolwriter.pos = 0;
        boolwriter.buffer = NULL;
    }
    void init () {
#ifdef DEBUG_ARICODER
        always_assert(!any_written);
#endif
	output_.resize((std::max((unsigned int)MIN_SIZE,
                                 std::min((unsigned int)4096 * 1024,
                                          (unsigned int)(5120 * 1024 / NUM_THREADS))))
		       + 1024);
        vpx_start_encode(&boolwriter, output_.data());
    }
    void put( const bool value, Branch & branch, Billing bill) {
#ifdef DEBUG_ARICODER
        if (!any_written) {
               any_written = true;
               static int count=0;
               w_bitcount = count * 500000000;
	       ++count;
        }
#endif
        vpx_write(&boolwriter, value, branch.prob(), bill);
        if (__builtin_expect(boolwriter.pos & SIZE_CHECK, false)) {
            // check if we're out of buffer space
            if (boolwriter.pos + 128 > output_.size()) {
                output_.resize(output_.size() * 2);
                boolwriter.buffer = &output_[0]; //reset buffer
            }
        }
        branch.record_obs_and_update(value);
    }
    void finish(Sirikata::MuxReader::ResizableByteBuffer &finish) {
        vpx_stop_encode(&boolwriter);
        output_.resize(boolwriter.pos);
        finish.swap(output_);
    }
};