Merge "Resolve a couple of TODOs in firstpass.c"
[aom.git] / vpx_dsp / bitwriter.h
blobd904997af309ffcd36c0295c2d21c551a7cc35cb
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * 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.
9 */
11 #ifndef VPX_DSP_BITWRITER_H_
12 #define VPX_DSP_BITWRITER_H_
14 #include "vpx_ports/mem.h"
16 #include "vpx_dsp/prob.h"
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
22 typedef struct vpx_writer {
23 unsigned int lowvalue;
24 unsigned int range;
25 int count;
26 unsigned int pos;
27 uint8_t *buffer;
28 } vpx_writer;
30 void vpx_start_encode(vpx_writer *bc, uint8_t *buffer);
31 void vpx_stop_encode(vpx_writer *bc);
33 static INLINE void vpx_write(vpx_writer *br, int bit, int probability) {
34 unsigned int split;
35 int count = br->count;
36 unsigned int range = br->range;
37 unsigned int lowvalue = br->lowvalue;
38 register int shift;
40 split = 1 + (((range - 1) * probability) >> 8);
42 range = split;
44 if (bit) {
45 lowvalue += split;
46 range = br->range - split;
49 shift = vpx_norm[range];
51 range <<= shift;
52 count += shift;
54 if (count >= 0) {
55 int offset = shift - count;
57 if ((lowvalue << (offset - 1)) & 0x80000000) {
58 int x = br->pos - 1;
60 while (x >= 0 && br->buffer[x] == 0xff) {
61 br->buffer[x] = 0;
62 x--;
65 br->buffer[x] += 1;
68 br->buffer[br->pos++] = (lowvalue >> (24 - offset));
69 lowvalue <<= offset;
70 shift = count;
71 lowvalue &= 0xffffff;
72 count -= 8;
75 lowvalue <<= shift;
76 br->count = count;
77 br->lowvalue = lowvalue;
78 br->range = range;
81 static INLINE void vpx_write_bit(vpx_writer *w, int bit) {
82 vpx_write(w, bit, 128); // vpx_prob_half
85 static INLINE void vpx_write_literal(vpx_writer *w, int data, int bits) {
86 int bit;
88 for (bit = bits - 1; bit >= 0; bit--)
89 vpx_write_bit(w, 1 & (data >> bit));
92 #define vpx_write_prob(w, v) vpx_write_literal((w), (v), 8)
94 #ifdef __cplusplus
95 } // extern "C"
96 #endif
98 #endif // VPX_DSP_BITWRITER_H_