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 VP9_ENCODER_VP9_WRITER_H_
12 #define VP9_ENCODER_VP9_WRITER_H_
14 #include "vpx_ports/mem.h"
16 #include "vp9/common/vp9_prob.h"
23 unsigned int lowvalue;
30 // Variables used to track bit costs without outputing to the bitstream
31 unsigned int measure_cost;
35 extern const unsigned int vp9_prob_cost[256];
37 void vp9_start_encode(vp9_writer *bc, uint8_t *buffer);
38 void vp9_stop_encode(vp9_writer *bc);
40 static void vp9_write(vp9_writer *br, int bit, int probability) {
42 int count = br->count;
43 unsigned int range = br->range;
44 unsigned int lowvalue = br->lowvalue;
45 register unsigned int shift;
47 split = 1 + (((range - 1) * probability) >> 8);
53 range = br->range - split;
56 shift = vp9_norm[range];
62 int offset = shift - count;
64 if ((lowvalue << (offset - 1)) & 0x80000000) {
67 while (x >= 0 && br->buffer[x] == 0xff) {
75 br->buffer[br->pos++] = (lowvalue >> (24 - offset));
84 br->lowvalue = lowvalue;
88 static void vp9_write_bit(vp9_writer *w, int bit) {
89 vp9_write(w, bit, 128); // vp9_prob_half
92 static void vp9_write_literal(vp9_writer *w, int data, int bits) {
95 for (bit = bits - 1; bit >= 0; bit--)
96 vp9_write_bit(w, 1 & (data >> bit));
99 #define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8)
105 #endif // VP9_ENCODER_VP9_WRITER_H_