xref: /rockchip-linux_mpp/mpp/hal/vpu/vp8e/hal_vp8e_putbit.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  * Copyright 2017 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define MODULE_TAG "hal_vp8e_putbit"
18 
19 #include "hal_vp8e_base.h"
20 #include "hal_vp8e_putbit.h"
21 
vp8e_set_buffer(Vp8ePutBitBuf * bitbuf,RK_U8 * data,RK_S32 size)22 MPP_RET vp8e_set_buffer(Vp8ePutBitBuf *bitbuf, RK_U8 *data, RK_S32 size)
23 {
24     if ((bitbuf == NULL) || (data == NULL) || (size < 1))
25         return MPP_NOK;
26 
27     bitbuf->data = data;
28     bitbuf->p_data = data;
29     bitbuf->size = size;
30 
31     bitbuf->range = 255;
32     bitbuf->bottom = 0;
33     bitbuf->bits_left = 24;
34 
35     bitbuf->byte_cnt = 0;
36 
37     return MPP_OK;
38 }
39 
vp8e_put_bool(Vp8ePutBitBuf * bitbuf,RK_S32 prob,RK_S32 bool_value)40 MPP_RET vp8e_put_bool(Vp8ePutBitBuf *bitbuf, RK_S32 prob, RK_S32 bool_value)
41 {
42     RK_S32 split = 1 + ((bitbuf->range - 1) * prob >> 8);
43 
44     if (bool_value) {
45         bitbuf->bottom += split;
46         bitbuf->range -= split;
47     } else {
48         bitbuf->range = split;
49     }
50 
51     while (bitbuf->range < 128) {
52         if (bitbuf->bottom < 0) {
53             RK_U8 *data = bitbuf->data;
54             while (*--data == 255) {
55                 *data = 0;
56             }
57             (*data)++;
58         }
59         bitbuf->range <<= 1;
60         bitbuf->bottom <<= 1;
61 
62         if (!--bitbuf->bits_left) {
63             *bitbuf->data++ = (bitbuf->bottom >> 24) & 0xff;
64             bitbuf->byte_cnt++;
65             bitbuf->bottom &= 0xffffff;     /* Keep 3 bytes */
66             bitbuf->bits_left = 8;
67         }
68     }
69     return MPP_OK;
70 }
71 
vp8e_put_lit(Vp8ePutBitBuf * bitbuf,RK_S32 value,RK_S32 number)72 MPP_RET vp8e_put_lit(Vp8ePutBitBuf *bitbuf, RK_S32 value,
73                      RK_S32 number)
74 {
75     while (number--) {
76         vp8e_put_bool(bitbuf, 128, (value >> number) & 0x1);
77     }
78     return MPP_OK;
79 }
80 
vp8e_put_byte(Vp8ePutBitBuf * bitbuf,RK_S32 byte)81 MPP_RET vp8e_put_byte(Vp8ePutBitBuf *bitbuf, RK_S32 byte)
82 {
83     *bitbuf->data++ = byte;
84     bitbuf->byte_cnt++;
85     return MPP_OK;
86 }
87 
vp8e_buffer_gap(Vp8ePutBitBuf * bitbuf,RK_S32 gap)88 MPP_RET vp8e_buffer_gap(Vp8ePutBitBuf *bitbuf, RK_S32 gap)
89 {
90     if ((bitbuf->data - bitbuf->p_data) + gap > bitbuf->size) {
91         bitbuf->size = 0;
92         return MPP_NOK;
93     }
94 
95     return MPP_OK;
96 }
97 
vp8e_buffer_overflow(Vp8ePutBitBuf * bitbuf)98 MPP_RET vp8e_buffer_overflow(Vp8ePutBitBuf *bitbuf)
99 {
100     if (bitbuf->size > 0)
101         return MPP_OK;
102 
103     return MPP_NOK;
104 }
105