bitwriter.h 2.1 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
 */

11
12
#ifndef VPX_DSP_BITWRITER_H_
#define VPX_DSP_BITWRITER_H_
John Koleszar's avatar
John Koleszar committed
13

14
#include "vpx_ports/mem.h"
John Koleszar's avatar
John Koleszar committed
15

16
#include "vpx_dsp/prob.h"
17

18
19
20
21
#ifdef __cplusplus
extern "C" {
#endif

22
typedef struct vp9_writer {
John Koleszar's avatar
John Koleszar committed
23
24
25
26
  unsigned int lowvalue;
  unsigned int range;
  int count;
  unsigned int pos;
27
  uint8_t *buffer;
28
} vp9_writer;
29

30
31
32
void vp9_start_encode(vp9_writer *bc, uint8_t *buffer);
void vp9_stop_encode(vp9_writer *bc);

33
static INLINE void vp9_write(vp9_writer *br, int bit, int probability) {
John Koleszar's avatar
John Koleszar committed
34
35
36
37
38
  unsigned int split;
  int count = br->count;
  unsigned int range = br->range;
  unsigned int lowvalue = br->lowvalue;
  register unsigned int shift;
39

John Koleszar's avatar
John Koleszar committed
40
  split = 1 + (((range - 1) * probability) >> 8);
41

John Koleszar's avatar
John Koleszar committed
42
  range = split;
43

John Koleszar's avatar
John Koleszar committed
44
45
46
47
  if (bit) {
    lowvalue += split;
    range = br->range - split;
  }
48

Yaowu Xu's avatar
Yaowu Xu committed
49
  shift = vpx_norm[range];
50

John Koleszar's avatar
John Koleszar committed
51
52
  range <<= shift;
  count += shift;
53

John Koleszar's avatar
John Koleszar committed
54
55
  if (count >= 0) {
    int offset = shift - count;
56

John Koleszar's avatar
John Koleszar committed
57
58
    if ((lowvalue << (offset - 1)) & 0x80000000) {
      int x = br->pos - 1;
59

John Koleszar's avatar
John Koleszar committed
60
      while (x >= 0 && br->buffer[x] == 0xff) {
61
        br->buffer[x] = 0;
John Koleszar's avatar
John Koleszar committed
62
63
        x--;
      }
64

John Koleszar's avatar
John Koleszar committed
65
      br->buffer[x] += 1;
66
67
    }

John Koleszar's avatar
John Koleszar committed
68
69
70
71
72
73
74
75
76
77
78
    br->buffer[br->pos++] = (lowvalue >> (24 - offset));
    lowvalue <<= offset;
    shift = count;
    lowvalue &= 0xffffff;
    count -= 8;
  }

  lowvalue <<= shift;
  br->count = count;
  br->lowvalue = lowvalue;
  br->range = range;
79
80
}

81
static INLINE void vp9_write_bit(vp9_writer *w, int bit) {
Yaowu Xu's avatar
Yaowu Xu committed
82
  vp9_write(w, bit, 128);  // vpx_prob_half
83
84
}

85
static INLINE void vp9_write_literal(vp9_writer *w, int data, int bits) {
86
87
88
89
90
91
  int bit;

  for (bit = bits - 1; bit >= 0; bit--)
    vp9_write_bit(w, 1 & (data >> bit));
}

92
#define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8)
93

94
95
96
97
#ifdef __cplusplus
}  // extern "C"
#endif

98
#endif  // VPX_DSP_BITWRITER_H_