dboolhuff.c 2.04 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
 */


#include "dboolhuff.h"
13
#include "vp8/common/common.h"
John Koleszar's avatar
John Koleszar committed
14

Johann's avatar
Johann committed
15 16
int vp8dx_start_decode(BOOL_DECODER *br,
                       const unsigned char *source,
17
                       unsigned int source_sz,
18
                       vpx_decrypt_cb decrypt_cb,
19
                       void *decrypt_state)
John Koleszar's avatar
John Koleszar committed
20
{
21 22 23 24
    br->user_buffer_end = source+source_sz;
    br->user_buffer     = source;
    br->value    = 0;
    br->count    = -8;
John Koleszar's avatar
John Koleszar committed
25
    br->range    = 255;
26 27
    br->decrypt_cb = decrypt_cb;
    br->decrypt_state = decrypt_state;
John Koleszar's avatar
John Koleszar committed
28 29 30 31 32

    if (source_sz && !source)
        return 1;

    /* Populate the buffer */
Johann's avatar
Johann committed
33
    vp8dx_bool_decoder_fill(br);
John Koleszar's avatar
John Koleszar committed
34 35 36 37

    return 0;
}

Johann's avatar
Johann committed
38
void vp8dx_bool_decoder_fill(BOOL_DECODER *br)
John Koleszar's avatar
John Koleszar committed
39
{
Dmitry Kovalev's avatar
Dmitry Kovalev committed
40 41 42
    const unsigned char *bufptr = br->user_buffer;
    VP8_BD_VALUE value = br->value;
    int count = br->count;
43
    int shift = VP8_BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
44 45
    size_t bytes_left = br->user_buffer_end - bufptr;
    size_t bits_left = bytes_left * CHAR_BIT;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
46 47
    int x = (int)(shift + CHAR_BIT - bits_left);
    int loop_end = 0;
48 49 50
    unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1];

    if (br->decrypt_cb) {
51
        size_t n = MIN(sizeof(decrypted), bytes_left);
52
        br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n);
53 54
        bufptr = decrypted;
    }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
55 56 57 58 59 60 61 62 63 64 65 66

    if(x >= 0)
    {
        count += VP8_LOTS_OF_BITS;
        loop_end = x;
    }

    if (x < 0 || bits_left)
    {
        while(shift >= loop_end)
        {
            count += CHAR_BIT;
67
            value |= (VP8_BD_VALUE)*bufptr << shift;
68
            ++bufptr;
69
            ++br->user_buffer;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
70 71 72
            shift -= CHAR_BIT;
        }
    }
73 74 75

    br->value = value;
    br->count = count;
John Koleszar's avatar
John Koleszar committed
76
}