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.
11 #ifndef VPX_DSP_BITWRITER_H_
12 #define VPX_DSP_BITWRITER_H_
14 #include "vpx_ports/mem.h"
16 #include "vpx_dsp/prob.h"
22 typedef struct vpx_writer
{
23 unsigned int lowvalue
;
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
) {
35 int count
= br
->count
;
36 unsigned int range
= br
->range
;
37 unsigned int lowvalue
= br
->lowvalue
;
40 split
= 1 + (((range
- 1) * probability
) >> 8);
46 range
= br
->range
- split
;
49 shift
= vpx_norm
[range
];
55 int offset
= shift
- count
;
57 if ((lowvalue
<< (offset
- 1)) & 0x80000000) {
60 while (x
>= 0 && br
->buffer
[x
] == 0xff) {
68 br
->buffer
[br
->pos
++] = (lowvalue
>> (24 - offset
));
77 br
->lowvalue
= lowvalue
;
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
) {
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)
98 #endif // VPX_DSP_BITWRITER_H_