boolhuff.h 3.22 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
 */


/****************************************************************************
*
*   Module Title :     boolhuff.h
*
*   Description  :     Bool Coder header file.
*
****************************************************************************/
19 20
#ifndef VP8_ENCODER_BOOLHUFF_H_
#define VP8_ENCODER_BOOLHUFF_H_
John Koleszar's avatar
John Koleszar committed
21

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

25 26 27 28
#ifdef __cplusplus
extern "C" {
#endif

John Koleszar's avatar
John Koleszar committed
29 30 31 32 33 34 35
typedef struct
{
    unsigned int lowvalue;
    unsigned int range;
    int count;
    unsigned int pos;
    unsigned char *buffer;
36 37
    unsigned char *buffer_end;
    struct vpx_internal_error_info *error;
John Koleszar's avatar
John Koleszar committed
38 39
} BOOL_CODER;

40
extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer, unsigned char *buffer_end);
41

John Koleszar's avatar
John Koleszar committed
42 43 44 45
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];

46 47 48

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

49 50 51 52 53 54 55 56 57 58
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 ");
59

60 61
    return 0;
}
62 63 64 65 66 67 68 69
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;

70
#ifdef VP8_ENTROPY_STATS
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
#if defined(SECTIONBITS_OUTPUT)

    if (bit)
        Sectionbits[active_section] += vp8_prob_cost[255-probability];
    else
        Sectionbits[active_section] += vp8_prob_cost[probability];

#endif
#endif

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

    range = split;

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

    shift = vp8_norm[range];

    range <<= shift;
    count += shift;

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

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

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

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

113
        validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error);
114
        br->buffer[br->pos++] = (lowvalue >> (24 - offset));
115

116 117 118 119 120 121 122 123 124 125 126 127
        lowvalue <<= offset;
        shift = count;
        lowvalue &= 0xffffff;
        count -= 8 ;
    }

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

128 129 130 131
#ifdef __cplusplus
}  // extern "C"
#endif

132
#endif  // VP8_ENCODER_BOOLHUFF_H_