aboutsummaryrefslogtreecommitdiff
path: root/libvpx/vp8/encoder/boolhuff.h
blob: a8c536b99c0171b1a19b8c81b5a6fa405fb797d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

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

#include "vpx_ports/mem.h"
#include "vpx/internal/vpx_codec_internal.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
  unsigned int lowvalue;
  unsigned int range;
  int count;
  unsigned int pos;
  unsigned char *buffer;
  unsigned char *buffer_end;
  struct vpx_internal_error_info *error;
} BOOL_CODER;

void vp8_start_encode(BOOL_CODER *bc, unsigned char *source,
                      unsigned char *source_end);

void vp8_encode_value(BOOL_CODER *bc, int data, int bits);
void vp8_stop_encode(BOOL_CODER *bc);
extern const unsigned int vp8_prob_cost[256];

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

static int validate_buffer(const unsigned char *start, size_t len,
                           const unsigned char *end,
                           struct vpx_internal_error_info *error) {
  if (start + len > start && start + len < end) {
    return 1;
  } else {
    vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME,
                       "Truncated packet or corrupt partition ");
  }

  return 0;
}
static void vp8_encode_bool(BOOL_CODER *bc, int bit, int probability) {
  unsigned int split;
  int count = bc->count;
  unsigned int range = bc->range;
  unsigned int lowvalue = bc->lowvalue;
  int shift;

  split = 1 + (((range - 1) * probability) >> 8);

  range = split;

  if (bit) {
    lowvalue += split;
    range = bc->range - split;
  }

  shift = vp8_norm[range];

  range <<= shift;
  count += shift;

  if (count >= 0) {
    int offset = shift - count;

    if ((lowvalue << (offset - 1)) & 0x80000000) {
      int x = bc->pos - 1;

      while (x >= 0 && bc->buffer[x] == 0xff) {
        bc->buffer[x] = (unsigned char)0;
        x--;
      }

      bc->buffer[x] += 1;
    }

    validate_buffer(bc->buffer + bc->pos, 1, bc->buffer_end, bc->error);
    bc->buffer[bc->pos++] = (lowvalue >> (24 - offset) & 0xff);

    shift = count;
    lowvalue = (int)(((uint64_t)lowvalue << offset) & 0xffffff);
    count -= 8;
  }

  lowvalue <<= shift;
  bc->count = count;
  bc->lowvalue = lowvalue;
  bc->range = range;
}

#ifdef __cplusplus
}  // extern "C"
#endif

#endif  // VPX_VP8_ENCODER_BOOLHUFF_H_