1 /*
2 * Copyright 2015 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 "mpp_bit_test"
18
19 #include <stdlib.h>
20
21 #include "mpp_log.h"
22 #include "mpp_common.h"
23 #include "mpp_bitwrite.h"
24
25 #define BIT_WRITER_BUFFER_SIZE 1024
26
27 /*
28 * type is for operation type
29 * 0 - plain bit put
30 * 1 - bit put with detection
31 * 2 - write ue
32 * 3 - write se
33 * 4 - align byte
34 */
35 typedef enum BitOpsType_e {
36 BIT_PUT_NO03,
37 BIT_PUT,
38 BIT_PUT_UE,
39 BIT_PUT_SE,
40 BIT_ALIGN_BYTE,
41 } BitOpsType;
42
43 typedef struct BitOps_t {
44 BitOpsType type;
45 RK_S32 val;
46 RK_S32 len;
47 } BitOps;
48
49 static BitOps bit_ops[] = {
50 { BIT_PUT_NO03, 0, 8, },
51 { BIT_PUT, 0, 3, },
52 { BIT_PUT, 0, 15, },
53 { BIT_PUT, 0, 23, },
54 { BIT_PUT_UE, 17, 0, },
55 { BIT_PUT_SE, 9, 0, },
56 { BIT_ALIGN_BYTE, 0, 0, },
57 };
58
proc_bit_ops(MppWriteCtx * writer,BitOps * ops)59 void proc_bit_ops(MppWriteCtx *writer, BitOps *ops)
60 {
61 switch (ops->type) {
62 case BIT_PUT_NO03 : {
63 mpp_writer_put_raw_bits(writer, ops->val, ops->len);
64 } break;
65 case BIT_PUT : {
66 mpp_writer_put_bits(writer, ops->val, ops->len);
67 } break;
68 case BIT_PUT_UE : {
69 mpp_writer_put_ue(writer, ops->val);
70 } break;
71 case BIT_PUT_SE : {
72 mpp_writer_put_ue(writer, ops->val);
73 } break;
74 case BIT_ALIGN_BYTE : {
75 mpp_writer_trailing(writer);
76 } break;
77 default : {
78 mpp_err("invalid ops type %d\n", ops->type);
79 } break;
80 }
81 }
82
main()83 int main()
84 {
85 MPP_RET ret = MPP_ERR_UNKNOW;
86 void *data = NULL;
87 size_t size = BIT_WRITER_BUFFER_SIZE;
88 MppWriteCtx writer;
89 RK_U32 len_byte;
90 RK_U32 i;
91 RK_U32 buf_len = 0;
92 char buf[BIT_WRITER_BUFFER_SIZE];
93
94 mpp_log("mpp_bit_test start\n");
95
96 data = malloc(size);
97 if (NULL == data) {
98 mpp_err("mpp_bit_test malloc failed\n");
99 goto TEST_FAILED;
100 }
101
102 mpp_writer_init(&writer, data, size);
103
104 for (i = 0; i < MPP_ARRAY_ELEMS(bit_ops); i++)
105 proc_bit_ops(&writer, &bit_ops[i]);
106
107 len_byte = writer.byte_cnt;
108
109 for (i = 0; i < len_byte; i++) {
110 buf_len += snprintf(buf + buf_len, sizeof(buf) - buf_len,
111 "%02x ", writer.buffer[i]);
112 }
113
114 mpp_log("stream %s\n", buf);
115
116 ret = MPP_OK;
117 TEST_FAILED:
118 if (data)
119 free(data);
120
121 if (ret)
122 mpp_log("mpp_bit_test failed\n");
123 else
124 mpp_log("mpp_bit_test success\n");
125
126 return ret;
127 }
128
129