• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  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.
9  */
10 
11 
12 #include "dboolhuff.h"
13 
vp8dx_start_decode(BOOL_DECODER * br,const unsigned char * source,unsigned int source_sz,vp8_decrypt_cb * decrypt_cb,void * decrypt_state)14 int vp8dx_start_decode(BOOL_DECODER *br,
15                        const unsigned char *source,
16                        unsigned int source_sz,
17                        vp8_decrypt_cb *decrypt_cb,
18                        void *decrypt_state)
19 {
20     br->user_buffer_end = source+source_sz;
21     br->user_buffer     = source;
22     br->value    = 0;
23     br->count    = -8;
24     br->range    = 255;
25     br->decrypt_cb = decrypt_cb;
26     br->decrypt_state = decrypt_state;
27 
28     if (source_sz && !source)
29         return 1;
30 
31     /* Populate the buffer */
32     vp8dx_bool_decoder_fill(br);
33 
34     return 0;
35 }
36 
vp8dx_bool_decoder_fill(BOOL_DECODER * br)37 void vp8dx_bool_decoder_fill(BOOL_DECODER *br)
38 {
39     const unsigned char *bufptr = br->user_buffer;
40     VP8_BD_VALUE value = br->value;
41     int count = br->count;
42     int shift = VP8_BD_VALUE_SIZE - 8 - (count + 8);
43     size_t bytes_left = br->user_buffer_end - bufptr;
44     size_t bits_left = bytes_left * CHAR_BIT;
45     int x = (int)(shift + CHAR_BIT - bits_left);
46     int loop_end = 0;
47     unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1];
48 
49     if (br->decrypt_cb) {
50         size_t n = bytes_left > sizeof(decrypted) ? sizeof(decrypted) : bytes_left;
51         br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n);
52         bufptr = decrypted;
53     }
54 
55     if(x >= 0)
56     {
57         count += VP8_LOTS_OF_BITS;
58         loop_end = x;
59     }
60 
61     if (x < 0 || bits_left)
62     {
63         while(shift >= loop_end)
64         {
65             count += CHAR_BIT;
66             value |= (VP8_BD_VALUE)*bufptr << shift;
67             ++bufptr;
68             ++br->user_buffer;
69             shift -= CHAR_BIT;
70         }
71     }
72 
73     br->value = value;
74     br->count = count;
75 }
76