blob: 8a7e33205b9dddf6bcef418e859df5359770d0f1 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
12#include "dboolhuff.h"
Joey Parrish18c08602014-04-15 14:10:58 -070013#include "vp8/common/common.h"
James Zern820302a2015-08-27 14:46:08 -070014#include "vpx_dsp/vpx_dsp_common.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040015
Johannbb9c95e2011-02-04 16:00:00 -050016int vp8dx_start_decode(BOOL_DECODER *br,
17 const unsigned char *source,
Dmitry Kovalev26cec5c2013-03-15 18:21:55 -070018 unsigned int source_sz,
Joey Parrish18c08602014-04-15 14:10:58 -070019 vpx_decrypt_cb decrypt_cb,
Jeff Petkau368c7232013-06-13 12:16:58 -070020 void *decrypt_state)
John Koleszar0ea50ce2010-05-18 11:58:33 -040021{
Timothy B. Terriberryc17b62e2010-05-05 17:58:19 -040022 br->user_buffer_end = source+source_sz;
23 br->user_buffer = source;
24 br->value = 0;
25 br->count = -8;
John Koleszar0ea50ce2010-05-18 11:58:33 -040026 br->range = 255;
Jeff Petkau368c7232013-06-13 12:16:58 -070027 br->decrypt_cb = decrypt_cb;
28 br->decrypt_state = decrypt_state;
John Koleszar0ea50ce2010-05-18 11:58:33 -040029
30 if (source_sz && !source)
31 return 1;
32
John Koleszar0ea50ce2010-05-18 11:58:33 -040033 /* Populate the buffer */
Johannbb9c95e2011-02-04 16:00:00 -050034 vp8dx_bool_decoder_fill(br);
John Koleszar0ea50ce2010-05-18 11:58:33 -040035
John Koleszar0ea50ce2010-05-18 11:58:33 -040036 return 0;
37}
38
Johannbb9c95e2011-02-04 16:00:00 -050039void vp8dx_bool_decoder_fill(BOOL_DECODER *br)
John Koleszar0ea50ce2010-05-18 11:58:33 -040040{
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080041 const unsigned char *bufptr = br->user_buffer;
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080042 VP8_BD_VALUE value = br->value;
43 int count = br->count;
Joey Parrish18c08602014-04-15 14:10:58 -070044 int shift = VP8_BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
Jeff Petkau368c7232013-06-13 12:16:58 -070045 size_t bytes_left = br->user_buffer_end - bufptr;
46 size_t bits_left = bytes_left * CHAR_BIT;
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080047 int x = (int)(shift + CHAR_BIT - bits_left);
48 int loop_end = 0;
Jeff Petkau368c7232013-06-13 12:16:58 -070049 unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1];
50
51 if (br->decrypt_cb) {
James Zern820302a2015-08-27 14:46:08 -070052 size_t n = VPXMIN(sizeof(decrypted), bytes_left);
Johannd94aee62013-06-20 09:52:08 -070053 br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n);
Jeff Petkau368c7232013-06-13 12:16:58 -070054 bufptr = decrypted;
55 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040056
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080057 if(x >= 0)
58 {
59 count += VP8_LOTS_OF_BITS;
60 loop_end = x;
61 }
62
63 if (x < 0 || bits_left)
64 {
65 while(shift >= loop_end)
66 {
67 count += CHAR_BIT;
Jeff Petkau368c7232013-06-13 12:16:58 -070068 value |= (VP8_BD_VALUE)*bufptr << shift;
Dmitry Kovalev26cec5c2013-03-15 18:21:55 -070069 ++bufptr;
Jeff Petkau368c7232013-06-13 12:16:58 -070070 ++br->user_buffer;
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080071 shift -= CHAR_BIT;
72 }
73 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040074
Timothy B. Terriberryc17b62e2010-05-05 17:58:19 -040075 br->value = value;
76 br->count = count;
John Koleszar0ea50ce2010-05-18 11:58:33 -040077}