John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 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 |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
| 12 | #include "dboolhuff.h" |
Joey Parrish | 18c0860 | 2014-04-15 14:10:58 -0700 | [diff] [blame] | 13 | #include "vp8/common/common.h" |
James Zern | 820302a | 2015-08-27 14:46:08 -0700 | [diff] [blame] | 14 | #include "vpx_dsp/vpx_dsp_common.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 15 | |
Johann | bb9c95e | 2011-02-04 16:00:00 -0500 | [diff] [blame] | 16 | int vp8dx_start_decode(BOOL_DECODER *br, |
| 17 | const unsigned char *source, |
Dmitry Kovalev | 26cec5c | 2013-03-15 18:21:55 -0700 | [diff] [blame] | 18 | unsigned int source_sz, |
Joey Parrish | 18c0860 | 2014-04-15 14:10:58 -0700 | [diff] [blame] | 19 | vpx_decrypt_cb decrypt_cb, |
Jeff Petkau | 368c723 | 2013-06-13 12:16:58 -0700 | [diff] [blame] | 20 | void *decrypt_state) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 21 | { |
Timothy B. Terriberry | c17b62e | 2010-05-05 17:58:19 -0400 | [diff] [blame] | 22 | br->user_buffer_end = source+source_sz; |
| 23 | br->user_buffer = source; |
| 24 | br->value = 0; |
| 25 | br->count = -8; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 26 | br->range = 255; |
Jeff Petkau | 368c723 | 2013-06-13 12:16:58 -0700 | [diff] [blame] | 27 | br->decrypt_cb = decrypt_cb; |
| 28 | br->decrypt_state = decrypt_state; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 29 | |
| 30 | if (source_sz && !source) |
| 31 | return 1; |
| 32 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 33 | /* Populate the buffer */ |
Johann | bb9c95e | 2011-02-04 16:00:00 -0500 | [diff] [blame] | 34 | vp8dx_bool_decoder_fill(br); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 35 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
Johann | bb9c95e | 2011-02-04 16:00:00 -0500 | [diff] [blame] | 39 | void vp8dx_bool_decoder_fill(BOOL_DECODER *br) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 40 | { |
Dmitry Kovalev | 33efdfe | 2013-03-04 16:53:00 -0800 | [diff] [blame] | 41 | const unsigned char *bufptr = br->user_buffer; |
Dmitry Kovalev | 33efdfe | 2013-03-04 16:53:00 -0800 | [diff] [blame] | 42 | VP8_BD_VALUE value = br->value; |
| 43 | int count = br->count; |
Joey Parrish | 18c0860 | 2014-04-15 14:10:58 -0700 | [diff] [blame] | 44 | int shift = VP8_BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT); |
Jeff Petkau | 368c723 | 2013-06-13 12:16:58 -0700 | [diff] [blame] | 45 | size_t bytes_left = br->user_buffer_end - bufptr; |
| 46 | size_t bits_left = bytes_left * CHAR_BIT; |
Dmitry Kovalev | 33efdfe | 2013-03-04 16:53:00 -0800 | [diff] [blame] | 47 | int x = (int)(shift + CHAR_BIT - bits_left); |
| 48 | int loop_end = 0; |
Jeff Petkau | 368c723 | 2013-06-13 12:16:58 -0700 | [diff] [blame] | 49 | unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1]; |
| 50 | |
| 51 | if (br->decrypt_cb) { |
James Zern | 820302a | 2015-08-27 14:46:08 -0700 | [diff] [blame] | 52 | size_t n = VPXMIN(sizeof(decrypted), bytes_left); |
Johann | d94aee6 | 2013-06-20 09:52:08 -0700 | [diff] [blame] | 53 | br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n); |
Jeff Petkau | 368c723 | 2013-06-13 12:16:58 -0700 | [diff] [blame] | 54 | bufptr = decrypted; |
| 55 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 56 | |
Dmitry Kovalev | 33efdfe | 2013-03-04 16:53:00 -0800 | [diff] [blame] | 57 | 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 Petkau | 368c723 | 2013-06-13 12:16:58 -0700 | [diff] [blame] | 68 | value |= (VP8_BD_VALUE)*bufptr << shift; |
Dmitry Kovalev | 26cec5c | 2013-03-15 18:21:55 -0700 | [diff] [blame] | 69 | ++bufptr; |
Jeff Petkau | 368c723 | 2013-06-13 12:16:58 -0700 | [diff] [blame] | 70 | ++br->user_buffer; |
Dmitry Kovalev | 33efdfe | 2013-03-04 16:53:00 -0800 | [diff] [blame] | 71 | shift -= CHAR_BIT; |
| 72 | } |
| 73 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 74 | |
Timothy B. Terriberry | c17b62e | 2010-05-05 17:58:19 -0400 | [diff] [blame] | 75 | br->value = value; |
| 76 | br->count = count; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 77 | } |