bitwriter.h 2.22 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"
15
#include "vpx_dsp/prob.h"
Angie Chiang's avatar
Angie Chiang committed
16
#include "vpx_util/debug_util.h"
17

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

Yaowu Xu's avatar
Yaowu Xu committed
22
typedef struct vpx_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;
Yaowu Xu's avatar
Yaowu Xu committed
28
} vpx_writer;
29

Yaowu Xu's avatar
Yaowu Xu committed
30
31
void vpx_start_encode(vpx_writer *bc, uint8_t *buffer);
void vpx_stop_encode(vpx_writer *bc);
32

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

Angie Chiang's avatar
Angie Chiang committed
40
41
42
43
#if CONFIG_BITSTREAM_DEBUG
  bitstream_queue_push(bit, probability);
#endif  // CONFIG_BITSTREAM_DEBUG

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

John Koleszar's avatar
John Koleszar committed
46
  range = split;
47

John Koleszar's avatar
John Koleszar committed
48
49
50
51
  if (bit) {
    lowvalue += split;
    range = br->range - split;
  }
52

Yaowu Xu's avatar
Yaowu Xu committed
53
  shift = vpx_norm[range];
54

John Koleszar's avatar
John Koleszar committed
55
56
  range <<= shift;
  count += shift;
57

John Koleszar's avatar
John Koleszar committed
58
59
  if (count >= 0) {
    int offset = shift - count;
60

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

John Koleszar's avatar
John Koleszar committed
64
      while (x >= 0 && br->buffer[x] == 0xff) {
65
        br->buffer[x] = 0;
John Koleszar's avatar
John Koleszar committed
66
67
        x--;
      }
68

John Koleszar's avatar
John Koleszar committed
69
      br->buffer[x] += 1;
70
71
    }

John Koleszar's avatar
John Koleszar committed
72
73
74
75
76
77
78
79
80
81
82
    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;
83
84
}

Yaowu Xu's avatar
Yaowu Xu committed
85
86
static INLINE void vpx_write_bit(vpx_writer *w, int bit) {
  vpx_write(w, bit, 128);  // vpx_prob_half
87
88
}

Yaowu Xu's avatar
Yaowu Xu committed
89
static INLINE void vpx_write_literal(vpx_writer *w, int data, int bits) {
90
91
  int bit;

clang-format's avatar
clang-format committed
92
  for (bit = bits - 1; bit >= 0; bit--) vpx_write_bit(w, 1 & (data >> bit));
93
94
}

Yaowu Xu's avatar
Yaowu Xu committed
95
#define vpx_write_prob(w, v) vpx_write_literal((w), (v), 8)
96

97
98
99
100
#ifdef __cplusplus
}  // extern "C"
#endif

101
#endif  // VPX_DSP_BITWRITER_H_