buf_ans.h 3.43 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
11
 */

Alex Converse's avatar
Alex Converse committed
12
13
#ifndef AOM_DSP_BUF_ANS_H_
#define AOM_DSP_BUF_ANS_H_
14
// Buffered forward ANS writer.
15
// Symbols are written to the writer in forward (decode) order and serialized
16
17
18
// backwards due to ANS's stack like behavior.

#include <assert.h>
Yaowu Xu's avatar
Yaowu Xu committed
19
20
#include "./aom_config.h"
#include "aom/aom_integer.h"
Alex Converse's avatar
Alex Converse committed
21
#include "aom_dsp/ans.h"
22
#include "aom_dsp/answriter.h"
23
24
25
26
27
28
29
30
31

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus

#define ANS_METHOD_UABS 0
#define ANS_METHOD_RANS 1

struct buffered_ans_symbol {
32
33
34
35
36
  unsigned int method : 1;  // one of ANS_METHOD_UABS or ANS_METHOD_RANS
  // TODO(aconverse): Should be possible to write this in terms of start for ABS
  unsigned int val_start : RANS_PROB_BITS;  // Boolean value for ABS
                                            // start in symbol cycle for Rans
  unsigned int prob : RANS_PROB_BITS;       // Probability of this symbol
37
38
39
};

struct BufAnsCoder {
Alex Converse's avatar
Alex Converse committed
40
  struct aom_internal_error_info *error;
41
42
43
44
45
  struct buffered_ans_symbol *buf;
  int size;
  int offset;
};

Alex Converse's avatar
Alex Converse committed
46
47
void aom_buf_ans_alloc(struct BufAnsCoder *c,
                       struct aom_internal_error_info *error, int size_hint);
48

Alex Converse's avatar
Alex Converse committed
49
void aom_buf_ans_free(struct BufAnsCoder *c);
50

Alex Converse's avatar
Alex Converse committed
51
void aom_buf_ans_grow(struct BufAnsCoder *c);
52
53

static INLINE void buf_ans_write_reset(struct BufAnsCoder *const c) {
54
55
56
  c->offset = 0;
}

57
58
static INLINE void buf_uabs_write(struct BufAnsCoder *const c, uint8_t val,
                                  AnsP8 prob) {
59
60
  assert(c->offset <= c->size);
  if (c->offset == c->size) {
Alex Converse's avatar
Alex Converse committed
61
    aom_buf_ans_grow(c);
62
  }
63
64
65
66
67
68
69
70
  c->buf[c->offset].method = ANS_METHOD_UABS;
  c->buf[c->offset].val_start = val;
  c->buf[c->offset].prob = prob;
  ++c->offset;
}

static INLINE void buf_rans_write(struct BufAnsCoder *const c,
                                  const struct rans_sym *const sym) {
71
72
  assert(c->offset <= c->size);
  if (c->offset == c->size) {
Alex Converse's avatar
Alex Converse committed
73
    aom_buf_ans_grow(c);
74
  }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  c->buf[c->offset].method = ANS_METHOD_RANS;
  c->buf[c->offset].val_start = sym->cum_prob;
  c->buf[c->offset].prob = sym->prob;
  ++c->offset;
}

static INLINE void buf_ans_flush(const struct BufAnsCoder *const c,
                                 struct AnsCoder *ans) {
  int offset;
  for (offset = c->offset - 1; offset >= 0; --offset) {
    if (c->buf[offset].method == ANS_METHOD_RANS) {
      struct rans_sym sym;
      sym.prob = c->buf[offset].prob;
      sym.cum_prob = c->buf[offset].val_start;
      rans_write(ans, &sym);
    } else {
Alex Converse's avatar
Alex Converse committed
91
92
      uabs_write(ans, (uint8_t)c->buf[offset].val_start,
                 (AnsP8)c->buf[offset].prob);
93
94
95
96
    }
  }
}

97
98
99
100
static INLINE void buf_uabs_write_bit(struct BufAnsCoder *c, int bit) {
  buf_uabs_write(c, bit, 128);
}

101
102
static INLINE void buf_uabs_write_literal(struct BufAnsCoder *c, int literal,
                                          int bits) {
103
104
105
106
107
108
  int bit;

  assert(bits < 31);
  for (bit = bits - 1; bit >= 0; bit--)
    buf_uabs_write_bit(c, 1 & (literal >> bit));
}
109
110
111
#ifdef __cplusplus
}  // extern "C"
#endif  // __cplusplus
Alex Converse's avatar
Alex Converse committed
112
#endif  // AOM_DSP_BUF_ANS_H_