11b362b15af34006e6a11974088a46d42b903418eJohann/*
21b362b15af34006e6a11974088a46d42b903418eJohann *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
31b362b15af34006e6a11974088a46d42b903418eJohann *
41b362b15af34006e6a11974088a46d42b903418eJohann *  Use of this source code is governed by a BSD-style license
51b362b15af34006e6a11974088a46d42b903418eJohann *  that can be found in the LICENSE file in the root of the source
61b362b15af34006e6a11974088a46d42b903418eJohann *  tree. An additional intellectual property rights grant can be found
71b362b15af34006e6a11974088a46d42b903418eJohann *  in the file PATENTS.  All contributing project authors may
81b362b15af34006e6a11974088a46d42b903418eJohann *  be found in the AUTHORS file in the root of the source tree.
91b362b15af34006e6a11974088a46d42b903418eJohann */
101b362b15af34006e6a11974088a46d42b903418eJohann
111b362b15af34006e6a11974088a46d42b903418eJohann
121b362b15af34006e6a11974088a46d42b903418eJohann#include "dboolhuff.h"
131b362b15af34006e6a11974088a46d42b903418eJohann
141b362b15af34006e6a11974088a46d42b903418eJohannint vp8dx_start_decode(BOOL_DECODER *br,
151b362b15af34006e6a11974088a46d42b903418eJohann                       const unsigned char *source,
16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang                       unsigned int source_sz,
17ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang                       vp8_decrypt_cb *decrypt_cb,
18ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang                       void *decrypt_state)
191b362b15af34006e6a11974088a46d42b903418eJohann{
201b362b15af34006e6a11974088a46d42b903418eJohann    br->user_buffer_end = source+source_sz;
211b362b15af34006e6a11974088a46d42b903418eJohann    br->user_buffer     = source;
221b362b15af34006e6a11974088a46d42b903418eJohann    br->value    = 0;
231b362b15af34006e6a11974088a46d42b903418eJohann    br->count    = -8;
241b362b15af34006e6a11974088a46d42b903418eJohann    br->range    = 255;
25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    br->decrypt_cb = decrypt_cb;
26ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    br->decrypt_state = decrypt_state;
271b362b15af34006e6a11974088a46d42b903418eJohann
281b362b15af34006e6a11974088a46d42b903418eJohann    if (source_sz && !source)
291b362b15af34006e6a11974088a46d42b903418eJohann        return 1;
301b362b15af34006e6a11974088a46d42b903418eJohann
311b362b15af34006e6a11974088a46d42b903418eJohann    /* Populate the buffer */
321b362b15af34006e6a11974088a46d42b903418eJohann    vp8dx_bool_decoder_fill(br);
331b362b15af34006e6a11974088a46d42b903418eJohann
341b362b15af34006e6a11974088a46d42b903418eJohann    return 0;
351b362b15af34006e6a11974088a46d42b903418eJohann}
361b362b15af34006e6a11974088a46d42b903418eJohann
371b362b15af34006e6a11974088a46d42b903418eJohannvoid vp8dx_bool_decoder_fill(BOOL_DECODER *br)
381b362b15af34006e6a11974088a46d42b903418eJohann{
39ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    const unsigned char *bufptr = br->user_buffer;
40ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    VP8_BD_VALUE value = br->value;
41ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    int count = br->count;
42ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    int shift = VP8_BD_VALUE_SIZE - 8 - (count + 8);
43ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    size_t bytes_left = br->user_buffer_end - bufptr;
44ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    size_t bits_left = bytes_left * CHAR_BIT;
45ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    int x = (int)(shift + CHAR_BIT - bits_left);
46ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    int loop_end = 0;
47ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1];
48ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
49ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    if (br->decrypt_cb) {
5091037db265ecdd914a26e056cf69207b4f50924ehkuang        size_t n = bytes_left > sizeof(decrypted) ? sizeof(decrypted) : bytes_left;
5191037db265ecdd914a26e056cf69207b4f50924ehkuang        br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n);
52ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        bufptr = decrypted;
53ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    }
54ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
55ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    if(x >= 0)
56ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    {
57ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        count += VP8_LOTS_OF_BITS;
58ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        loop_end = x;
59ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    }
60ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
61ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    if (x < 0 || bits_left)
62ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    {
63ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        while(shift >= loop_end)
64ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        {
65ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang            count += CHAR_BIT;
66ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang            value |= (VP8_BD_VALUE)*bufptr << shift;
67ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang            ++bufptr;
68ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang            ++br->user_buffer;
69ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang            shift -= CHAR_BIT;
70ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        }
71ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    }
72ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
731b362b15af34006e6a11974088a46d42b903418eJohann    br->value = value;
741b362b15af34006e6a11974088a46d42b903418eJohann    br->count = count;
751b362b15af34006e6a11974088a46d42b903418eJohann}
76