blob: b874d4c46c0789854410b9d311211a75b5480f5f [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"
John Koleszar0ea50ce2010-05-18 11:58:33 -040014
Johannbb9c95e2011-02-04 16:00:00 -050015int vp8dx_start_decode(BOOL_DECODER *br,
16 const unsigned char *source,
Dmitry Kovalev26cec5c2013-03-15 18:21:55 -070017 unsigned int source_sz,
Joey Parrish18c08602014-04-15 14:10:58 -070018 vpx_decrypt_cb decrypt_cb,
Jeff Petkau368c7232013-06-13 12:16:58 -070019 void *decrypt_state)
John Koleszar0ea50ce2010-05-18 11:58:33 -040020{
Timothy B. Terriberryc17b62e2010-05-05 17:58:19 -040021 br->user_buffer_end = source+source_sz;
22 br->user_buffer = source;
23 br->value = 0;
24 br->count = -8;
John Koleszar0ea50ce2010-05-18 11:58:33 -040025 br->range = 255;
Jeff Petkau368c7232013-06-13 12:16:58 -070026 br->decrypt_cb = decrypt_cb;
27 br->decrypt_state = decrypt_state;
John Koleszar0ea50ce2010-05-18 11:58:33 -040028
29 if (source_sz && !source)
30 return 1;
31
John Koleszar0ea50ce2010-05-18 11:58:33 -040032 /* Populate the buffer */
Johannbb9c95e2011-02-04 16:00:00 -050033 vp8dx_bool_decoder_fill(br);
John Koleszar0ea50ce2010-05-18 11:58:33 -040034
John Koleszar0ea50ce2010-05-18 11:58:33 -040035 return 0;
36}
37
Johannbb9c95e2011-02-04 16:00:00 -050038void vp8dx_bool_decoder_fill(BOOL_DECODER *br)
John Koleszar0ea50ce2010-05-18 11:58:33 -040039{
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080040 const unsigned char *bufptr = br->user_buffer;
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080041 VP8_BD_VALUE value = br->value;
42 int count = br->count;
Joey Parrish18c08602014-04-15 14:10:58 -070043 int shift = VP8_BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
Jeff Petkau368c7232013-06-13 12:16:58 -070044 size_t bytes_left = br->user_buffer_end - bufptr;
45 size_t bits_left = bytes_left * CHAR_BIT;
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080046 int x = (int)(shift + CHAR_BIT - bits_left);
47 int loop_end = 0;
Jeff Petkau368c7232013-06-13 12:16:58 -070048 unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1];
49
50 if (br->decrypt_cb) {
Joey Parrish18c08602014-04-15 14:10:58 -070051 size_t n = MIN(sizeof(decrypted), bytes_left);
Johannd94aee62013-06-20 09:52:08 -070052 br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n);
Jeff Petkau368c7232013-06-13 12:16:58 -070053 bufptr = decrypted;
54 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040055
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080056 if(x >= 0)
57 {
58 count += VP8_LOTS_OF_BITS;
59 loop_end = x;
60 }
61
62 if (x < 0 || bits_left)
63 {
64 while(shift >= loop_end)
65 {
66 count += CHAR_BIT;
Jeff Petkau368c7232013-06-13 12:16:58 -070067 value |= (VP8_BD_VALUE)*bufptr << shift;
Dmitry Kovalev26cec5c2013-03-15 18:21:55 -070068 ++bufptr;
Jeff Petkau368c7232013-06-13 12:16:58 -070069 ++br->user_buffer;
Dmitry Kovalev33efdfe2013-03-04 16:53:00 -080070 shift -= CHAR_BIT;
71 }
72 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040073
Timothy B. Terriberryc17b62e2010-05-05 17:58:19 -040074 br->value = value;
75 br->count = count;
John Koleszar0ea50ce2010-05-18 11:58:33 -040076}