bitwriter.h 2.09 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

Yaowu Xu's avatar
Yaowu Xu committed
14
#include "aom_ports/mem.h"
John Koleszar's avatar
John Koleszar committed
15

Yaowu Xu's avatar
Yaowu Xu committed
16
#include "aom_dsp/prob.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

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
}

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

Yaowu Xu's avatar
Yaowu Xu committed
85
static INLINE void vpx_write_literal(vpx_writer *w, int data, int bits) {
86 87
  int bit;

clang-format's avatar
clang-format committed
88
  for (bit = bits - 1; bit >= 0; bit--) vpx_write_bit(w, 1 & (data >> bit));
89 90
}

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

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

97
#endif  // VPX_DSP_BITWRITER_H_