| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2006, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: biobits.c $ |
|---|
| 11 | * $brcm_Revision: 2 $ |
|---|
| 12 | * $brcm_Date: 8/31/06 9:45a $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Bit stream utility |
|---|
| 17 | * |
|---|
| 18 | * Revision History: |
|---|
| 19 | * |
|---|
| 20 | * $brcm_Log: /BSEAV/lib/asf/biobits.c $ |
|---|
| 21 | * |
|---|
| 22 | * 2 8/31/06 9:45a vsilyaev |
|---|
| 23 | * PR 23826: Fixed bub in end of vector condition |
|---|
| 24 | * |
|---|
| 25 | * 1 7/28/06 4:09p vsilyaev |
|---|
| 26 | * PR 22578: Bitstream utility |
|---|
| 27 | * |
|---|
| 28 | * |
|---|
| 29 | *******************************************************************************/ |
|---|
| 30 | #include "bstd.h" |
|---|
| 31 | #include "biobits.h" |
|---|
| 32 | |
|---|
| 33 | BDBG_MODULE(biobits); |
|---|
| 34 | |
|---|
| 35 | void |
|---|
| 36 | bio_bitstream_init(bio_bitstream *bs, bio_cursor *cursor) |
|---|
| 37 | { |
|---|
| 38 | bs->cursor = cursor; |
|---|
| 39 | bs->cache = 0; |
|---|
| 40 | bs->cache_pos = -1; /* invalid */ |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | void |
|---|
| 44 | bio_bitstream_dump(bio_bitstream *bs) |
|---|
| 45 | { |
|---|
| 46 | BSTD_UNUSED(bs); |
|---|
| 47 | BDBG_WRN(("bio_bitstream: cache %#x cache_pos %d", bs->cache, bs->cache_pos)); |
|---|
| 48 | return; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | bool |
|---|
| 52 | bio_bitstream_eof(bio_bitstream *bs) |
|---|
| 53 | { |
|---|
| 54 | return bs->cache_pos == -1 && bio_cursor_eof(bs->cursor); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | static int |
|---|
| 58 | bio_bitstream_refill(bio_bitstream *bs) |
|---|
| 59 | { |
|---|
| 60 | bio_cursor *c = bs->cursor; |
|---|
| 61 | |
|---|
| 62 | BDBG_ASSERT(bs->cache_pos==-1); |
|---|
| 63 | #if B_IOVEC_FAST |
|---|
| 64 | if (c->left>=4) { |
|---|
| 65 | bs->cache = (uint32_t)c->cursor[3] | |
|---|
| 66 | ((uint32_t)(c->cursor[2])<<8) | |
|---|
| 67 | ((uint32_t)(c->cursor[1])<<16) | |
|---|
| 68 | ((uint32_t)(c->cursor[0])<<24); |
|---|
| 69 | bs->cache_pos=31; |
|---|
| 70 | c->cursor += 4; |
|---|
| 71 | c->left -= 4; |
|---|
| 72 | return 0; |
|---|
| 73 | } |
|---|
| 74 | #endif |
|---|
| 75 | { |
|---|
| 76 | int next; |
|---|
| 77 | uint32_t cache; |
|---|
| 78 | |
|---|
| 79 | next = bio_cursor_next(c); |
|---|
| 80 | if (next==BIO_EOF) { |
|---|
| 81 | return BIO_EOF; |
|---|
| 82 | } |
|---|
| 83 | cache = (uint32_t)next; |
|---|
| 84 | next = bio_cursor_next(c); |
|---|
| 85 | if (next==BIO_EOF) { |
|---|
| 86 | bs->cache = cache; |
|---|
| 87 | bs->cache_pos = 7; |
|---|
| 88 | return 0; |
|---|
| 89 | } |
|---|
| 90 | cache = (cache << 8) | (uint32_t)next; |
|---|
| 91 | |
|---|
| 92 | next = bio_cursor_next(c); |
|---|
| 93 | if (next==BIO_EOF) { |
|---|
| 94 | bs->cache = cache; |
|---|
| 95 | bs->cache_pos = 15; |
|---|
| 96 | return 0; |
|---|
| 97 | } |
|---|
| 98 | cache = (cache << 8) | (uint32_t)next; |
|---|
| 99 | |
|---|
| 100 | next = bio_cursor_next(c); |
|---|
| 101 | if (next==BIO_EOF) { |
|---|
| 102 | bs->cache = cache; |
|---|
| 103 | bs->cache_pos = 23; |
|---|
| 104 | return 0; |
|---|
| 105 | } |
|---|
| 106 | bs->cache = (cache << 8) | (uint32_t)next; |
|---|
| 107 | bs->cache_pos = 31; |
|---|
| 108 | } |
|---|
| 109 | return 0; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | int |
|---|
| 113 | bio_bitstream_show(bio_bitstream *bs) |
|---|
| 114 | { |
|---|
| 115 | int bit; |
|---|
| 116 | |
|---|
| 117 | if(bs->cache_pos==-1) { |
|---|
| 118 | if (bio_bitstream_refill(bs)==BIO_EOF) { |
|---|
| 119 | return BIO_EOF; |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | bit = (bs->cache >> bs->cache_pos)&1; |
|---|
| 123 | return bit; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | int |
|---|
| 127 | bio_bitstream_bit(bio_bitstream *bs) |
|---|
| 128 | { |
|---|
| 129 | int bit; |
|---|
| 130 | |
|---|
| 131 | bit = bio_bitstream_show(bs); |
|---|
| 132 | if (bit!=BIO_EOF) { |
|---|
| 133 | bs->cache_pos--; |
|---|
| 134 | } |
|---|
| 135 | return bit; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | int |
|---|
| 139 | bio_bitstream_drop(bio_bitstream *bs) |
|---|
| 140 | { |
|---|
| 141 | return bio_bitstream_bit(bs); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | unsigned |
|---|
| 145 | bio_bitstream_bits(bio_bitstream *bs, unsigned nbits) |
|---|
| 146 | { |
|---|
| 147 | uint32_t bits; |
|---|
| 148 | int pos; |
|---|
| 149 | |
|---|
| 150 | BDBG_ASSERT(nbits<=31); |
|---|
| 151 | BDBG_ASSERT(nbits>0); |
|---|
| 152 | pos = bs->cache_pos - nbits; |
|---|
| 153 | nbits--; |
|---|
| 154 | if( bs->cache_pos>=(int)nbits) { |
|---|
| 155 | bits = bs->cache >> (bs->cache_pos-nbits) & ((uint32_t)(-1)>>(31-nbits)); |
|---|
| 156 | bs->cache_pos = pos; |
|---|
| 157 | } else { |
|---|
| 158 | for(bits=0;;) { |
|---|
| 159 | bits |= (unsigned) bio_bitstream_bit(bs); |
|---|
| 160 | if (nbits==0) { |
|---|
| 161 | break; |
|---|
| 162 | } |
|---|
| 163 | nbits--; |
|---|
| 164 | bits <<=1; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | return bits; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void |
|---|
| 171 | bio_bitstream_drop_bits(bio_bitstream *bs, unsigned nbits) |
|---|
| 172 | { |
|---|
| 173 | bio_bitstream_bits(bs, nbits); |
|---|
| 174 | return; |
|---|
| 175 | } |
|---|
| 176 | |
|---|