1 /*
2 *
3 * Copyright 2015 Rockchip Electronics Co. LTD
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <string.h>
19 #include "mpp_bitput.h"
20
mpp_set_bitput_ctx(BitputCtx_t * bp,RK_U64 * data,RK_U32 len)21 RK_S32 mpp_set_bitput_ctx(BitputCtx_t *bp, RK_U64 *data, RK_U32 len)
22 {
23 memset(bp, 0, sizeof(BitputCtx_t));
24 bp->index = 0;
25 bp->bitpos = 0;
26 bp->bvalue = 0;
27 bp->size = len;
28 bp->buflen = len; // align 64bit
29 bp->pbuf = data;
30 return 0;
31 }
32
mpp_put_bits(BitputCtx_t * bp,RK_U64 invalue,RK_S32 lbits)33 void mpp_put_bits(BitputCtx_t *bp, RK_U64 invalue, RK_S32 lbits)
34 {
35 RK_U8 hbits = 0;
36
37 if (!lbits) return;
38
39 if (bp->index >= bp->buflen) return;
40
41 hbits = 64 - lbits;
42 invalue = (invalue << hbits) >> hbits;
43 bp->bvalue |= invalue << bp->bitpos; // high bits value
44 if ((bp->bitpos + lbits) >= 64) {
45 bp->pbuf[bp->index] = bp->bvalue;
46 bp->bvalue = invalue >> (64 - bp->bitpos); // low bits value
47 bp->index++;
48 }
49
50 if (bp->index >= bp->buflen)
51 return;
52
53 bp->pbuf[bp->index] = bp->bvalue;
54 bp->bitpos = (bp->bitpos + lbits) & 63;
55 // mpp_log("bp->index = %d bp->bitpos = %d lbits = %d invalue 0x%x bp->hvalue 0x%x bp->lvalue 0x%x",bp->index,bp->bitpos,lbits, (RK_U32)invalue,(RK_U32)(bp->bvalue >> 32),(RK_U32)bp->bvalue);
56 }
57
mpp_put_align(BitputCtx_t * bp,RK_S32 align_bits,int flag)58 void mpp_put_align(BitputCtx_t *bp, RK_S32 align_bits, int flag)
59 {
60 RK_U32 word_offset = 0, len = 0;
61
62 word_offset = (align_bits >= 64) ? ((bp->index & (((align_bits & 0xfe0) >> 6) - 1)) << 6) : 0;
63 len = (align_bits - (word_offset + (bp->bitpos % align_bits))) % align_bits;
64 while (len > 0) {
65 if (len >= 8) {
66 if (flag == 0)
67 mpp_put_bits(bp, ((RK_U64)0 << (64 - 8)) >> (64 - 8), 8);
68 else
69 mpp_put_bits(bp, (0xffffffffffffffff << (64 - 8)) >> (64 - 8), 8);
70 len -= 8;
71 } else {
72 if (flag == 0)
73 mpp_put_bits(bp, ((RK_U64)0 << (64 - len)) >> (64 - len), len);
74 else
75 mpp_put_bits(bp, (0xffffffffffffffff << (64 - len)) >> (64 - len), len);
76 len -= len;
77 }
78 }
79 }
80
81