Line data Source code
1 : /*
2 : * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 : *
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 : */
10 :
11 : #ifndef VPX_DSP_BITWRITER_H_
12 : #define VPX_DSP_BITWRITER_H_
13 :
14 : #include "vpx_ports/mem.h"
15 :
16 : #include "vpx_dsp/prob.h"
17 :
18 : #ifdef __cplusplus
19 : extern "C" {
20 : #endif
21 :
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;
29 :
30 : void vpx_start_encode(vpx_writer *bc, uint8_t *buffer);
31 : void vpx_stop_encode(vpx_writer *bc);
32 :
33 0 : static INLINE void vpx_write(vpx_writer *br, int bit, int probability) {
34 : unsigned int split;
35 0 : int count = br->count;
36 0 : unsigned int range = br->range;
37 0 : unsigned int lowvalue = br->lowvalue;
38 : register int shift;
39 :
40 0 : split = 1 + (((range - 1) * probability) >> 8);
41 :
42 0 : range = split;
43 :
44 0 : if (bit) {
45 0 : lowvalue += split;
46 0 : range = br->range - split;
47 : }
48 :
49 0 : shift = vpx_norm[range];
50 :
51 0 : range <<= shift;
52 0 : count += shift;
53 :
54 0 : if (count >= 0) {
55 0 : int offset = shift - count;
56 :
57 0 : if ((lowvalue << (offset - 1)) & 0x80000000) {
58 0 : int x = br->pos - 1;
59 :
60 0 : while (x >= 0 && br->buffer[x] == 0xff) {
61 0 : br->buffer[x] = 0;
62 0 : x--;
63 : }
64 :
65 0 : br->buffer[x] += 1;
66 : }
67 :
68 0 : br->buffer[br->pos++] = (lowvalue >> (24 - offset));
69 0 : lowvalue <<= offset;
70 0 : shift = count;
71 0 : lowvalue &= 0xffffff;
72 0 : count -= 8;
73 : }
74 :
75 0 : lowvalue <<= shift;
76 0 : br->count = count;
77 0 : br->lowvalue = lowvalue;
78 0 : br->range = range;
79 0 : }
80 :
81 0 : static INLINE void vpx_write_bit(vpx_writer *w, int bit) {
82 0 : vpx_write(w, bit, 128); // vpx_prob_half
83 0 : }
84 :
85 0 : static INLINE void vpx_write_literal(vpx_writer *w, int data, int bits) {
86 : int bit;
87 :
88 0 : for (bit = bits - 1; bit >= 0; bit--) vpx_write_bit(w, 1 & (data >> bit));
89 0 : }
90 :
91 : #define vpx_write_prob(w, v) vpx_write_literal((w), (v), 8)
92 :
93 : #ifdef __cplusplus
94 : } // extern "C"
95 : #endif
96 :
97 : #endif // VPX_DSP_BITWRITER_H_
|