Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_writer.h
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 VP9_ENCODER_VP9_WRITER_H_
12 #define VP9_ENCODER_VP9_WRITER_H_
13
14 #include "vpx_ports/mem.h"
15
16 #include "vp9/common/vp9_prob.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 typedef struct {
23   unsigned int lowvalue;
24   unsigned int range;
25   unsigned int value;
26   int count;
27   unsigned int pos;
28   uint8_t *buffer;
29
30   // Variables used to track bit costs without outputing to the bitstream
31   unsigned int  measure_cost;
32   uint64_t bit_counter;
33 } vp9_writer;
34
35 extern const unsigned int vp9_prob_cost[256];
36
37 void vp9_start_encode(vp9_writer *bc, uint8_t *buffer);
38 void vp9_stop_encode(vp9_writer *bc);
39
40 static void vp9_write(vp9_writer *br, int bit, int probability) {
41   unsigned int split;
42   int count = br->count;
43   unsigned int range = br->range;
44   unsigned int lowvalue = br->lowvalue;
45   register unsigned int shift;
46
47   split = 1 + (((range - 1) * probability) >> 8);
48
49   range = split;
50
51   if (bit) {
52     lowvalue += split;
53     range = br->range - split;
54   }
55
56   shift = vp9_norm[range];
57
58   range <<= shift;
59   count += shift;
60
61   if (count >= 0) {
62     int offset = shift - count;
63
64     if ((lowvalue << (offset - 1)) & 0x80000000) {
65       int x = br->pos - 1;
66
67       while (x >= 0 && br->buffer[x] == 0xff) {
68         br->buffer[x] = 0;
69         x--;
70       }
71
72       br->buffer[x] += 1;
73     }
74
75     br->buffer[br->pos++] = (lowvalue >> (24 - offset));
76     lowvalue <<= offset;
77     shift = count;
78     lowvalue &= 0xffffff;
79     count -= 8;
80   }
81
82   lowvalue <<= shift;
83   br->count = count;
84   br->lowvalue = lowvalue;
85   br->range = range;
86 }
87
88 static void vp9_write_bit(vp9_writer *w, int bit) {
89   vp9_write(w, bit, 128);  // vp9_prob_half
90 }
91
92 static void vp9_write_literal(vp9_writer *w, int data, int bits) {
93   int bit;
94
95   for (bit = bits - 1; bit >= 0; bit--)
96     vp9_write_bit(w, 1 & (data >> bit));
97 }
98
99 #define vp9_write_prob(w, v) vp9_write_literal((w), (v), 8)
100
101 #ifdef __cplusplus
102 }  // extern "C"
103 #endif
104
105 #endif  // VP9_ENCODER_VP9_WRITER_H_