boolhuff.h 2.72 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
15
16
17
18
19
20
21
 */


/****************************************************************************
*
*   Module Title :     boolhuff.h
*
*   Description  :     Bool Coder header file.
*
****************************************************************************/
#ifndef __INC_BOOLHUFF_H
#define __INC_BOOLHUFF_H

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
37
} BOOL_CODER;

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

John Koleszar's avatar
John Koleszar committed
39
40
41
42
extern void vp8_encode_value(BOOL_CODER *br, int data, int bits);
extern void vp8_stop_encode(BOOL_CODER *bc);
extern const unsigned int vp8_prob_cost[256];

43
44
45
46
extern void vp8_encode_uniform(BOOL_CODER *bc, int v, int n);
extern void vp8_encode_term_subexp(BOOL_CODER *bc, int v, int k, int n);
extern int vp8_count_uniform(int v, int n);
extern int vp8_count_term_subexp(int v, int k, int n);
47
extern int vp9_recenter_nonneg(int v, int m);
48
49
50
51

DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);


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

#ifdef ENTROPY_STATS
#if defined(SECTIONBITS_OUTPUT)

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

#endif
#endif

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

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

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

John Koleszar's avatar
John Koleszar committed
79
  shift = vp8_norm[range];
80

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

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

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

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

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

John Koleszar's avatar
John Koleszar committed
98
99
100
101
102
103
104
105
106
107
108
    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;
109
110
}

John Koleszar's avatar
John Koleszar committed
111
#endif