vp9_boolhuff.h 2.81 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
11
12
13
 */


/****************************************************************************
*
14
*   Module Title :     vp9_boolhuff.h
John Koleszar's avatar
John Koleszar committed
15
16
17
18
*
*   Description  :     Bool Coder header file.
*
****************************************************************************/
19
20
#ifndef VP9_ENCODER_VP9_BOOLHUFF_H_
#define VP9_ENCODER_VP9_BOOLHUFF_H_
John Koleszar's avatar
John Koleszar committed
21

22
#include "vpx_ports/mem.h"
John Koleszar's avatar
John Koleszar committed
23

John Koleszar's avatar
John Koleszar committed
24
25
26
27
28
29
30
31
32
33
34
typedef struct {
  unsigned int lowvalue;
  unsigned int range;
  unsigned int value;
  int count;
  unsigned int pos;
  unsigned char *buffer;

  // Variables used to track bit costs without outputing to the bitstream
  unsigned int  measure_cost;
  unsigned long bit_counter;
John Koleszar's avatar
John Koleszar committed
35
36
} BOOL_CODER;

37
extern void vp9_start_encode(BOOL_CODER *bc, unsigned char *buffer);
38

39
extern void vp9_encode_value(BOOL_CODER *br, int data, int bits);
John Koleszar's avatar
John Koleszar committed
40
extern void vp9_encode_unsigned_max(BOOL_CODER *br, int data, int max);
41
extern void vp9_stop_encode(BOOL_CODER *bc);
42
extern const unsigned int vp9_prob_cost[256];
John Koleszar's avatar
John Koleszar committed
43

44
45
46
47
extern void vp9_encode_uniform(BOOL_CODER *bc, int v, int n);
extern void vp9_encode_term_subexp(BOOL_CODER *bc, int v, int k, int n);
extern int vp9_count_uniform(int v, int n);
extern int vp9_count_term_subexp(int v, int k, int n);
48
extern int vp9_recenter_nonneg(int v, int m);
49

50
DECLARE_ALIGNED(16, extern const unsigned char, vp9_norm[256]);
51
52


53
static void encode_bool(BOOL_CODER *br, int bit, int probability) {
John Koleszar's avatar
John Koleszar committed
54
55
56
57
58
  unsigned int split;
  int count = br->count;
  unsigned int range = br->range;
  unsigned int lowvalue = br->lowvalue;
  register unsigned int shift;
59
60
61
62

#ifdef ENTROPY_STATS
#if defined(SECTIONBITS_OUTPUT)

John Koleszar's avatar
John Koleszar committed
63
  if (bit)
64
    Sectionbits[active_section] += vp9_prob_cost[255 - probability];
John Koleszar's avatar
John Koleszar committed
65
  else
66
    Sectionbits[active_section] += vp9_prob_cost[probability];
67
68
69
70

#endif
#endif

John Koleszar's avatar
John Koleszar committed
71
  split = 1 + (((range - 1) * probability) >> 8);
72

John Koleszar's avatar
John Koleszar committed
73
  range = split;
74

John Koleszar's avatar
John Koleszar committed
75
76
77
78
  if (bit) {
    lowvalue += split;
    range = br->range - split;
  }
79

80
  shift = vp9_norm[range];
81

John Koleszar's avatar
John Koleszar committed
82
83
  range <<= shift;
  count += shift;
84

John Koleszar's avatar
John Koleszar committed
85
86
  if (count >= 0) {
    int offset = shift - count;
87

John Koleszar's avatar
John Koleszar committed
88
89
    if ((lowvalue << (offset - 1)) & 0x80000000) {
      int x = br->pos - 1;
90

John Koleszar's avatar
John Koleszar committed
91
92
93
94
      while (x >= 0 && br->buffer[x] == 0xff) {
        br->buffer[x] = (unsigned char)0;
        x--;
      }
95

John Koleszar's avatar
John Koleszar committed
96
      br->buffer[x] += 1;
97
98
    }

John Koleszar's avatar
John Koleszar committed
99
100
101
102
103
104
105
106
107
108
109
    br->buffer[br->pos++] = (lowvalue >> (24 - offset));
    lowvalue <<= offset;
    shift = count;
    lowvalue &= 0xffffff;
    count -= 8;
  }

  lowvalue <<= shift;
  br->count = count;
  br->lowvalue = lowvalue;
  br->range = range;
110
111
}

John Koleszar's avatar
John Koleszar committed
112
#endif