1 /******************************************************************************
2 * *
3 * Copyright (C) 2018 The Android Open Source Project
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 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #include "ixheaacd_type_def.h"
21
22 #include "ixheaacd_bitbuffer.h"
23 #include "ixheaacd_sbr_crc.h"
24 #include "ixheaacd_sbr_const.h"
25
ixheaacd_calc_chk_sum(WORD16 * crc_state,WORD32 stream_data,WORD32 num_bits)26 static VOID ixheaacd_calc_chk_sum(WORD16* crc_state, WORD32 stream_data,
27 WORD32 num_bits) {
28 WORD32 i;
29 WORD32 data_bit_mask = (1 << (num_bits - 1));
30 WORD16 crc10 = SBR_CRC_POLY;
31 WORD16 crc_mask = (1 << 9);
32 WORD16 crc_state_local = *crc_state;
33
34 for (i = 0; i < num_bits; i++) {
35 WORD32 bit0, bit1;
36 bit0 = ((crc_state_local & crc_mask) ? 1 : 0);
37 bit1 = ((data_bit_mask & stream_data) ? 1 : 0);
38 bit0 ^= bit1;
39 crc_state_local = (WORD16)((WORD32)(crc_state_local & 0x0000FFFF) << 1);
40 if (bit0) {
41 crc_state_local ^= crc10;
42 }
43 data_bit_mask = (data_bit_mask >> 1);
44 }
45 *crc_state = crc_state_local;
46 return;
47 }
48
ixheaacd_sbr_crc(ia_bit_buf_struct * it_bit_buff,WORD32 num_crc_bits)49 static PLATFORM_INLINE WORD32 ixheaacd_sbr_crc(ia_bit_buf_struct* it_bit_buff,
50 WORD32 num_crc_bits) {
51 WORD32 i;
52 WORD32 num_full_bytes, rem_bits;
53 WORD32 byte_value;
54 WORD16 crc_state = 0;
55
56 num_full_bytes = (num_crc_bits >> 3);
57 rem_bits = (num_crc_bits & 0x7);
58
59 for (i = 0; i < num_full_bytes; i++) {
60 byte_value = ixheaacd_read_bits_buf(it_bit_buff, 8);
61 ixheaacd_calc_chk_sum(&crc_state, byte_value, 8);
62 }
63
64 byte_value = ixheaacd_read_bits_buf(it_bit_buff, rem_bits);
65 ixheaacd_calc_chk_sum(&crc_state, byte_value, rem_bits);
66
67 return (crc_state & 0x03FF);
68 }
69
ixheaacd_sbr_crccheck(ia_bit_buf_struct * it_bit_buff,WORD32 crc_bits_len)70 FLAG ixheaacd_sbr_crccheck(ia_bit_buf_struct* it_bit_buff,
71 WORD32 crc_bits_len) {
72 struct ia_bit_buf_struct it_bit_buff_local = {0};
73 WORD32 num_crc_bits;
74 WORD32 calc_crc_sum;
75 WORD32 bits_available;
76 WORD32 crc_check_sum;
77
78 crc_check_sum = ixheaacd_read_bits_buf(it_bit_buff, SBR_CYC_REDCY_CHK_BITS);
79
80 it_bit_buff_local = *it_bit_buff;
81
82 bits_available = it_bit_buff->cnt_bits;
83
84 if (bits_available <= 0) {
85 return 0;
86 }
87
88 num_crc_bits =
89 (crc_bits_len > bits_available) ? bits_available : crc_bits_len;
90
91 calc_crc_sum = ixheaacd_sbr_crc(&it_bit_buff_local, num_crc_bits);
92
93 if (calc_crc_sum != crc_check_sum) {
94 return 0;
95 }
96 return 1;
97 }
98