xref: /rockchip-linux_mpp/mpp/base/test/mpp_bit_read_test.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  * Copyright 2022 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_read_test"
18 
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include "mpp_log.h"
23 #include "mpp_common.h"
24 #include "mpp_bitread.h"
25 
26 #define BIT_READ_BUFFER_SIZE        (1024)
27 
28 typedef enum BitOpsType_e {
29     BIT_GET,
30     BIT_GET_UE,
31     BIT_GET_SE,
32     BIT_SKIP,
33 } BitOpsType;
34 
35 static RK_U8 bitOpsStr[][10] = {
36     "read_bit",
37     "read_ue",
38     "read_se",
39     "skip_bit"
40 };
41 
42 typedef struct BitOps_t {
43     BitOpsType type;
44     RK_S32 len;
45     RK_S32 val;
46     char syntax[50];
47 } BitOps;
48 
49 // AVS2 I-frame data containing "00 00 02"
50 static RK_U8 test_data_1[] = {
51     0x00, 0x00, 0x01, 0xB3, 0x00, 0x00, 0xFF, 0xFF,
52     0x80, 0x00, 0x00, 0x02, 0x04, 0x40, 0xCA, 0xB3,
53     0xD2, 0x2B, 0x33, 0x4C, 0x91, 0x88, 0xAC, 0xCD,
54     0x14, 0xD8, 0xA1, 0x86, 0x14, 0x30, 0x8A, 0x58,
55     0xE1, 0x46, 0x8C, 0x1D, 0x80, 0x00, 0x00, 0x01
56 };
57 
58 static BitOps bit_ops_1 [] = {
59     {BIT_SKIP, 32, 0, "Skip start code of I Pic"},
60     {BIT_GET, 32, 65535, "bbv_delay"},
61     {BIT_GET, 1, 1, "time_code_flag"},
62     {BIT_GET, 24, 0, "time_code"},
63     {BIT_GET, 8, 0, "coding_order"},
64     {BIT_GET_UE, 0, 3, "picture_output_delay"},
65     {BIT_GET, 1, 0, "use RCS in SPS"},
66     {BIT_GET, 1, 1, "refered by others"},
67     {BIT_GET, 3, 0, "number of reference picture"},
68     {BIT_GET, 3, 0, "number of removed picture"},
69     {BIT_GET, 1, 1, "marker bit"},
70     {BIT_GET, 1, 1, "progressive_frame"},
71     {BIT_GET, 1, 0, "top_field_first"},
72     {BIT_GET, 1, 0, "repeat_first_field"},
73     {BIT_GET, 1, 1, "fixed_picture_qp"},
74     {BIT_GET, 7, 43, "picture_qp"},
75     {BIT_GET, 1, 0, "loop_filter_disable"},
76     {BIT_GET, 1, 0, "loop_filter_parameter_flag"},
77     {BIT_GET, 1, 1, "chroma_quant_param_disable"}
78 };
79 
80 // AVS2 Sequence header data without "00 00 02"
81 static RK_U8 test_data_2[] = {
82     0x00, 0x00, 0x01, 0xB0, 0x20, 0x42, 0x81, 0xA0,
83     0x03, 0xC1, 0x23, 0x0F, 0x42, 0x44, 0x00, 0x10,
84     0x00, 0x01, 0x9F, 0xFE, 0x46, 0x10, 0x18, 0xE8,
85     0x0D, 0x02, 0x4B, 0x10, 0xA4, 0x74, 0x09, 0x44,
86     0x99, 0x02, 0x58, 0x90, 0x61, 0x0D, 0x0A, 0x20,
87     0x90, 0x22, 0x91, 0x24, 0x38, 0x42, 0x39, 0x00,
88     0x00, 0x01, 0xB5, 0x23, 0x0F, 0x01, 0x14, 0x99
89 };
90 
91 static BitOps bit_ops_2 [] = {
92     {BIT_SKIP, 32, 0, "Skip start code of Seq header"},
93     {BIT_GET, 8, 32, "profile_id"},
94     {BIT_GET, 8, 66, "level_id"},
95     {BIT_GET, 1, 1, "progressive_sequence"},
96     {BIT_GET, 1, 0, "field_coded_sequence"},
97     {BIT_GET, 14, 416, "horizontal_size"},
98     {BIT_GET, 14, 240, "vertical_size"},
99     {BIT_GET, 2, 1, "chroma_format"},
100     {BIT_GET, 3, 1, "sample_precision"},
101     {BIT_GET, 4, 1, "aspect_ratio"},
102     {BIT_GET, 4, 8, "frame_rate_code"},
103     {BIT_GET, 18, 125000, "bit_rate_lower_18"},
104     {BIT_GET, 1, 1, "marker_bit"},
105     {BIT_GET, 12, 0, "bit_rate_upper_12"},
106     {BIT_GET, 1, 0, "low_delay"},
107     {BIT_GET, 1, 1, "marker_bit"},
108     {BIT_GET, 1, 0, "enable_temporal_id"},
109     {BIT_GET, 18, 0, "bbv_uffer_size"},
110     {BIT_GET, 3, 6, "lcu_size"},
111     {BIT_GET, 1, 0, "enable_weighted_quant"},
112     // weighted quant matrix
113     {BIT_GET, 1, 1, "disable_background_picture"},
114     {BIT_GET, 1, 1, "enable_mhp_skip"},
115     {BIT_GET, 1, 1, "enable_dhp"},
116     {BIT_GET, 1, 1, "enable_wsm"},
117     {BIT_GET, 1, 1, "enable_amp"},
118     {BIT_GET, 1, 1, "enable_nsqt"},
119     {BIT_GET, 1, 1, "enable_nsip"},
120     {BIT_GET, 1, 1, "enable_2nd_transform"},
121     {BIT_GET, 1, 1, "enable_sao"},
122     {BIT_GET, 1, 1, "enable_alf"},
123     {BIT_GET, 1, 1, "enable_pmvr"},
124     {BIT_GET, 1, 1, "marker_bit"},
125     {BIT_GET, 6, 8, "num_of_rps"},
126     {BIT_GET, 1, 1, "refered_by_others[0]"},
127     {BIT_GET, 3, 4, "num_of_ref[0]"},
128     {BIT_GET, 6, 8, "ref_pic[0][0]"},
129     {BIT_GET, 6, 3, "ref_pic[0][1]"},
130     {BIT_GET, 6, 7, "ref_pic[0][2]"},
131     {BIT_GET, 6, 16, "ref_pic[0][3]"},
132     {BIT_GET, 3, 0, "num_to_remove[0]"},
133     {BIT_GET, 1, 1, "marker_bit"},
134     {BIT_GET, 1, 1, "refered_by_others[1]"},
135     {BIT_GET, 3, 2, "num_of_ref[1]"},
136     {BIT_GET, 6, 1, "ref_pic[1][0]"},
137     {BIT_GET, 6, 9, "ref_pic[1][1]"},
138     {BIT_GET, 3, 3, "num_to_remove[1]"},
139     {BIT_GET, 6, 4, "remove_pic[1][0]"},
140     {BIT_GET, 6, 10, "remove_pic[1][0]"},
141     {BIT_GET, 6, 17, "remove_pic[1][0]"},
142     {BIT_GET, 1, 1, "marker_bit"},
143     {BIT_GET, 1, 1, "refered_by_others[2]"},
144     {BIT_GET, 3, 2, "num_of_ref[2]"},
145     {BIT_GET, 6, 1, "ref_pic[2][0]"},
146     {BIT_GET, 6, 10, "ref_pic[2][1]"},
147     {BIT_GET, 3, 1, "num_to_remove[2]"},
148     {BIT_GET, 6, 9, "remove_pic[2][0]"},
149     {BIT_GET, 1, 1, "marker_bit"},
150     {BIT_GET, 1, 0, "refered_by_others[3]"},
151     {BIT_GET, 3, 2, "num_of_ref[3]"},
152     {BIT_GET, 6, 1, "ref_pic[3][0]"},
153     {BIT_GET, 6, 11, "ref_pic[3][1]"},
154     {BIT_GET, 3, 0, "num_to_remove[3]"},
155     {BIT_GET, 1, 1, "marker_bit"},
156     {BIT_GET, 1, 0, "refered_by_others[4]"},
157     {BIT_GET, 3, 2, "num_of_ref[4]"},
158     {BIT_GET, 6, 3, "ref_pic[4][0]"},
159     {BIT_GET, 6, 2, "ref_pic[4][1]"},
160     {BIT_GET, 3, 0, "num_to_remove[4]"},
161     {BIT_GET, 1, 1, "marker_bit"},
162     {BIT_GET, 1, 1, "refered_by_others[5]"},
163     {BIT_GET, 3, 2, "num_of_ref[5]"},
164     {BIT_GET, 6, 5, "ref_pic[5][0]"},
165     {BIT_GET, 6, 4, "ref_pic[5][1]"},
166     {BIT_GET, 3, 0, "num_to_remove[5]"},
167     {BIT_GET, 1, 1, "marker_bit"},
168     {BIT_GET, 1, 0, "refered_by_6thers[6]"},
169     {BIT_GET, 3, 2, "num_of_ref[6]"},
170     {BIT_GET, 6, 1, "ref_pic[6][0]"},
171     {BIT_GET, 6, 5, "ref_pic[6][1]"},
172     {BIT_GET, 3, 1, "num_to_remove[6]"},
173     {BIT_GET, 6, 4, "remove_pic[6][0]"},
174     {BIT_GET, 1, 1, "marker_bit"},
175     {BIT_GET, 1, 0, "refered_by_others[7]"},
176     {BIT_GET, 3, 2, "num_of_ref[7]"},
177     {BIT_GET, 6, 7, "ref_pic[7][0]"},
178     {BIT_GET, 6, 2, "ref_pic[7][1]"},
179     {BIT_GET, 3, 0, "num_to_remove[7]"},
180     {BIT_GET, 1, 1, "marker_bit"},
181     {BIT_GET, 5, 3, "picture_reorder_delay"},
182     {BIT_GET, 1, 1, "enable_clf"},
183     {BIT_GET, 2, 0, "reserved 2bits 00"}
184 };
185 
186 // H264 SPS data with "00 00 03 00"
187 static RK_U8 test_data_3[] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x64, 0x00, 0x28, 0xAD, 0x00,
188                               0x2C, 0xA4, 0x01, 0xE0, 0x11, 0x1F, 0x78, 0x0B, 0x50, 0x10,
189                               0x10, 0x14, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03,
190                               0x00, 0xCB, 0x9B, 0x80, 0x03, 0xD0, 0x90, 0x03, 0xA9, 0xEF,
191                               0x7B, 0xE0, 0xA0
192                              };
193 
194 // H264 PPS and SEI data without "00 00 03 00"
195 static RK_U8 test_data_4[] = {0x00, 0x00, 0x00, 0x01, 0x68, 0xFE, 0x3C, 0xB0};
196 
197 static BitOps bit_ops_3[] = {
198     {BIT_SKIP,      40, 0,      "start code of SPS"},
199     {BIT_GET,       8,  100,    "profile_idc"},
200     {BIT_GET,       1,  0,      "constraint_set0_flag"},
201     {BIT_GET,       1,  0,      "constraint_set1_flag"},
202     {BIT_GET,       1,  0,      "constraint_set2_flag"},
203     {BIT_GET,       1,  0,      "constraint_set3_flag"},
204     {BIT_GET,       1,  0,      "constraint_set4_flag"},
205     {BIT_GET,       1,  0,      "constraint_set5_flag"},
206     {BIT_GET,       2,  0,      "reserved_zero_2bits"},
207     {BIT_GET,       8,  40,     "level_idc"},
208     {BIT_GET_UE,    0,  0,      "seq_parameter_set_id"},
209     {BIT_GET_UE,    0,  1,      "chroma_format_idc"},
210     {BIT_GET_UE,    0,  0,      "bit_depth_luma_minus8"},
211     {BIT_GET_UE,    0,  0,      "bit_depth_chroma_minus8"},
212     {BIT_GET,       1,  0,      "qp_prime_y_zero_transform_bypass"},
213     {BIT_GET,       1,  1,      "seq_scaling_matrix_present_flag"},
214     {BIT_GET,       1,  0,      "seq_sacling_list_present_flag[0]"},
215     {BIT_GET,       1,  0,      "seq_sacling_list_present_flag[1]"},
216     {BIT_GET,       1,  0,      "seq_sacling_list_present_flag[2]"},
217     {BIT_GET,       1,  0,      "seq_sacling_list_present_flag[3]"},
218     {BIT_GET,       1,  0,      "seq_sacling_list_present_flag[4]"},
219     {BIT_GET,       1,  0,      "seq_sacling_list_present_flag[5]"},
220     {BIT_GET,       1,  0,      "seq_sacling_list_present_flag[6]"},
221     {BIT_GET,       1,  0,      "seq_sacling_list_present_flag[7]"},
222     {BIT_GET_UE,    0,  4,      "log2_max_frame_num_minus4"},
223     {BIT_GET_UE,    0,  0,      "pic_order_cnt_type"},
224     {BIT_GET_UE,    0,  4,      "log2_max_pic_order_cnt_lsb_minus"},
225     {BIT_GET_UE,    0,  3,      "max_num_ref_frames"},
226     {BIT_GET,       1,  0,      "gaps_in_frame_num_value_allowed_flag"},
227     {BIT_GET_UE,    0,  119,     "pic_width_in_mbs_minus1"},
228     {BIT_GET_UE,    0,  33,      "pic_height_in_map_units_minus1"},
229     {BIT_GET,       1,  0,       "frame_mbs_only_flag"},
230     {BIT_GET,       1,  0,       "mb_adaptive_frame_field_flag"},
231     {BIT_GET,       1,  1,       "direct_8x8_inference_flag"},
232     {BIT_GET,       1,  1,       "frame_cropping_flag"},
233     {BIT_GET_UE,    0,  0,      "frame_crop_left_offset"},
234     {BIT_GET_UE,    0,  0,      "frame_crop_right_offset"},
235     {BIT_GET_UE,    0,  0,      "frame_crop_top_offset"},
236     {BIT_GET_UE,    0,  2,      "frame_crop_bottom_offset"},
237     {BIT_GET,       1,  1,      "vui_parameters_present_flag"},
238     {BIT_GET,       1,  1,      "aspect_ratio_info_present_flag"},
239     {BIT_GET,       8,  1,      "aspect_ratio_idc"},
240     {BIT_GET,       1,  0,      "overscan_info_ipresent_flag"},
241     {BIT_GET,       1,  1,      "video_signal_type_present_flag"},
242     {BIT_GET,       3,  5,      "video_format"},
243     {BIT_GET,       1,  0,      "video_full_range_flag"},
244     {BIT_GET,       1,  1,      "colour_description_present_flag"},
245     {BIT_GET,       8,  1,      "colour_primaries"},
246     {BIT_GET,       8,  1,      "transfer_characteristics"},
247     {BIT_GET,       8,  1,      "matrix_coefficient"},
248     {BIT_GET,       1,  0,      "chroma_loc_info_present_flag"},
249     {BIT_GET,       1,  1,      "timing_info_presetn_flag"},
250     {BIT_GET,       32, 1,      "num_units_in_tick"},
251     {BIT_GET,       32, 50,     "time_scale"},
252     {BIT_GET,       1,  1,      "fixed_frame_rate_flag"},
253     {BIT_GET,       1,  1,      "nal_hrd_parameters_present_flag"},
254     {BIT_GET_UE,    0,  0,      "cpb_cnt_minus1"},
255     {BIT_GET,       4,  3,      "bit_rate_scale"},
256     {BIT_GET,       4,  7,      "cpb_size_scale"},
257     {BIT_GET_UE,    0,  15624,  "bit_rate_value_minus1[0]"},
258     {BIT_GET_UE,    0,  1874,   "cpb_size_value_minus1[0]"},
259     {BIT_GET,       1,  1,      "cbr_flag[0]"},
260     {BIT_GET,       5,  23,     "initial_cpb_removal_delay_length_minus1"},
261     {BIT_GET,       5,  23,     "cpb_removal_delay_length_minus1"},
262     {BIT_GET,       5,  23,     "dpb_output_delay_length_minus1"},
263     {BIT_GET,       5,  24,     "time_offset_length"},
264     {BIT_GET,       1,  0,      "vcl_hrd_parameters_present_flag"},
265     {BIT_GET,       1,  0,      "low_delay_hrd_flag"},
266     {BIT_GET,       1,  1,      "picture_sturct_present_flag"},
267     {BIT_GET,       1,  0,      "bitstream_restriction_flag"},
268 };
269 
270 static BitOps bit_ops_4[] = {
271     {BIT_SKIP,      40, 0,      "Skip start code of PPS"},
272     {BIT_GET_UE,    0,  0,      "pic_parameter_set_id"},
273     {BIT_GET_UE,    0,  0,      "seq_parameter_set_id"},
274     {BIT_GET,       1,  1,      "entropy_coding_mode_flag"},
275     {BIT_GET,       1,  1,      "bottom_field_pic_order_in_frame_present_flag"},
276     {BIT_GET_UE,    0,  0,      "num_slice_groups_minus1"},
277     {BIT_GET_UE,    0,  0,      "num_ref_idx_l0_default_active_minus1"},
278     {BIT_GET_UE,    0,  0,      "num_ref_idx_l1_default_active_minus1"},
279     {BIT_GET,       1,  0,      "weighted_pred_flag"},
280     {BIT_GET,       2,  0,      "weighted_bipred_idc"},
281     {BIT_GET_SE,    0,  0,      "pic_init_qp_minus26"},
282     {BIT_GET_SE,    0,  0,      "pic_init_qs_minus26"},
283     {BIT_GET_SE,    0,  0,      "chroma_qp_index_offset"},
284     {BIT_GET,       1,  1,      "debloking_filter_control_present_flag"},
285     {BIT_GET,       1,  0,      "constrained_intra_pred_flag"},
286     {BIT_GET,       1,  0,      "redundant_pic_cnt_present_flag"},
287     {BIT_GET,       1,  1,      "transform_8x8_mode_flag"},
288     {BIT_GET,       1,  0,      "pic_scaling_matrix_present_flag"},
289     {BIT_GET_SE,    0,  0,      "second_chroma_qp_index_offset"}
290 };
291 
proc_bit_ops(BitReadCtx_t * ctx,BitOps * ops,RK_S32 * ret_val)292 MPP_RET proc_bit_ops(BitReadCtx_t *ctx, BitOps *ops, RK_S32 *ret_val)
293 {
294     MPP_RET ret = MPP_OK;
295     switch (ops->type) {
296     case BIT_GET :
297         if (ops->len >= 32) {
298             READ_BITS_LONG(ctx, ops->len, ret_val);
299         } else {
300             READ_BITS(ctx, ops->len, ret_val);
301         }
302         break;
303     case BIT_GET_UE:
304         READ_UE(ctx, ret_val);
305         break;
306     case BIT_GET_SE:
307         READ_SE(ctx, ret_val);
308         break;
309     case BIT_SKIP:
310         if (ops->len >= 32) {
311             SKIP_BITS_LONG(ctx, ops->len);
312         } else {
313             SKIP_BITS(ctx, ops->len);
314         }
315         break;
316     }
317     goto __NORMAL;
318 __BITREAD_ERR:
319     mpp_err("Read failed: syntax %s, %s, %d bits\n", ops->syntax, bitOpsStr[ops->type], ops->len);
320     return ret = MPP_ERR_VALUE;
321 __NORMAL:
322     if (ops->val != *ret_val) {
323         mpp_err("Read error: syntax %s, expect %d but %d\n", ops->syntax, ops->val, *ret_val);
324         ret = MPP_ERR_VALUE;
325     } else {
326         mpp_log("Read OK, syntax %s, %d\n", ops->syntax, *ret_val);
327     }
328     return ret;
329 }
330 
main()331 int main()
332 {
333     BitReadCtx_t reader;
334     RK_U32 i;
335     RK_S32 tmp = 0;
336 
337     mpp_log("mpp bit read test start\n");
338     mpp_log("Reading H264 data with 00 00 03 00...");
339     memset(&reader, 0, sizeof(BitReadCtx_t));
340     mpp_set_bitread_ctx(&reader, test_data_3, sizeof(test_data_3));
341     // mpp_set_pre_detection(&reader);
342     mpp_set_bitread_pseudo_code_type(&reader, PSEUDO_CODE_H264_H265);
343     for (i = 0; i < MPP_ARRAY_ELEMS(bit_ops_3); i++) {
344 
345         if (proc_bit_ops(&reader, &bit_ops_3[i], &tmp))
346             goto __READ_FAILED;
347 
348         tmp = 0;
349     }
350 
351     mpp_log("Reading H264 data without 00 00 03 00...");
352     memset(&reader, 0, sizeof(BitReadCtx_t));
353     mpp_set_bitread_ctx(&reader, test_data_4, sizeof(test_data_4));
354 
355     for (i = 0; i < MPP_ARRAY_ELEMS(bit_ops_4); i++) {
356         if (proc_bit_ops(&reader, &bit_ops_4[i], &tmp))
357             goto __READ_FAILED;
358 
359         tmp = 0;
360     }
361 
362     mpp_log("Reading AVS2 data without 00 00 02...");
363     memset(&reader, 0, sizeof(BitReadCtx_t));
364     mpp_set_bitread_ctx(&reader, test_data_2, sizeof(test_data_2));
365 
366     for (i = 0; i < MPP_ARRAY_ELEMS(bit_ops_2); i++) {
367         if (proc_bit_ops(&reader, &bit_ops_2[i], &tmp))
368             goto __READ_FAILED;
369 
370         tmp = 0;
371     }
372 
373     mpp_log("Reading AVS2 data with 00 00 02...");
374     memset(&reader, 0, sizeof(BitReadCtx_t));
375     mpp_set_bitread_ctx(&reader, test_data_1, sizeof(test_data_1));
376     mpp_set_bitread_pseudo_code_type(&reader, PSEUDO_CODE_AVS2);
377 
378     for (i = 0; i < MPP_ARRAY_ELEMS(bit_ops_1); i++) {
379         if (proc_bit_ops(&reader, &bit_ops_1[i], &tmp))
380             goto __READ_FAILED;
381 
382         tmp = 0;
383     }
384     mpp_log("mpp bit read test end\n");
385     return 0;
386 __READ_FAILED:
387     return -1;
388 }