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_VPX_DSP_BITWRITER_H_
12 #define VPX_VPX_DSP_BITWRITER_H_
13
14 #include <stdio.h>
15
16 #include "vpx_ports/compiler_attributes.h"
17 #include "vpx_ports/mem.h"
18
19 #include "vpx_dsp/prob.h"
20 #if CONFIG_BITSTREAM_DEBUG
21 #include "vpx_util/vpx_debug_util.h"
22 #endif // CONFIG_BITSTREAM_DEBUG
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 typedef struct vpx_writer {
29 unsigned int lowvalue;
30 unsigned int range;
31 int count;
32 unsigned int pos;
33 uint8_t *buffer;
34 } vpx_writer;
35
36 void vpx_start_encode(vpx_writer *br, uint8_t *source);
37 void vpx_stop_encode(vpx_writer *br);
38
vpx_write(vpx_writer * br,int bit,int probability)39 static INLINE VPX_NO_UNSIGNED_SHIFT_CHECK void vpx_write(vpx_writer *br,
40 int bit,
41 int probability) {
42 unsigned int split;
43 int count = br->count;
44 unsigned int range = br->range;
45 unsigned int lowvalue = br->lowvalue;
46 int shift;
47
48 #if CONFIG_BITSTREAM_DEBUG
49 /*
50 int queue_r = 0;
51 int frame_idx_r = 0;
52 int queue_w = bitstream_queue_get_write();
53 int frame_idx_w = bitstream_queue_get_frame_write();
54 if (frame_idx_w == frame_idx_r && queue_w == queue_r) {
55 fprintf(stderr, "\n *** bitstream queue at frame_idx_w %d queue_w %d\n",
56 frame_idx_w, queue_w);
57 assert(0);
58 }
59 */
60 bitstream_queue_push(bit, probability);
61 #endif
62
63 split = 1 + (((range - 1) * probability) >> 8);
64
65 range = split;
66
67 if (bit) {
68 lowvalue += split;
69 range = br->range - split;
70 }
71
72 shift = vpx_norm[range];
73
74 range <<= shift;
75 count += shift;
76
77 if (count >= 0) {
78 int offset = shift - count;
79
80 if ((lowvalue << (offset - 1)) & 0x80000000) {
81 int x = br->pos - 1;
82
83 while (x >= 0 && br->buffer[x] == 0xff) {
84 br->buffer[x] = 0;
85 x--;
86 }
87
88 br->buffer[x] += 1;
89 }
90
91 br->buffer[br->pos++] = (lowvalue >> (24 - offset)) & 0xff;
92 lowvalue <<= offset;
93 shift = count;
94 lowvalue &= 0xffffff;
95 count -= 8;
96 }
97
98 lowvalue <<= shift;
99 br->count = count;
100 br->lowvalue = lowvalue;
101 br->range = range;
102 }
103
vpx_write_bit(vpx_writer * w,int bit)104 static INLINE void vpx_write_bit(vpx_writer *w, int bit) {
105 vpx_write(w, bit, 128); // vpx_prob_half
106 }
107
vpx_write_literal(vpx_writer * w,int data,int bits)108 static INLINE void vpx_write_literal(vpx_writer *w, int data, int bits) {
109 int bit;
110
111 for (bit = bits - 1; bit >= 0; bit--) vpx_write_bit(w, 1 & (data >> bit));
112 }
113
114 #define vpx_write_prob(w, v) vpx_write_literal((w), (v), 8)
115
116 #ifdef __cplusplus
117 } // extern "C"
118 #endif
119
120 #endif // VPX_VPX_DSP_BITWRITER_H_
121