huangcm
2024-08-23 d76fb8c8c6d079a3cee81da7072347dcb8bbbc70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/******************************************************************************
 *                                                                            *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#include <ixheaacd_type_def.h>
 
#include "ixheaacd_bitbuffer.h"
#include "ixheaacd_sbr_crc.h"
#include "ixheaacd_sbr_const.h"
 
static VOID ixheaacd_calc_chk_sum(WORD16* crc_state, WORD32 stream_data,
                                  WORD32 num_bits) {
  WORD32 i;
  WORD32 data_bit_mask = (1 << (num_bits - 1));
  WORD16 crc10 = SBR_CRC_POLY;
  WORD16 crc_mask = (1 << 9);
  WORD16 crc_state_local = *crc_state;
 
  for (i = 0; i < num_bits; i++) {
    WORD32 bit0, bit1;
    bit0 = ((crc_state_local & crc_mask) ? 1 : 0);
    bit1 = ((data_bit_mask & stream_data) ? 1 : 0);
    bit0 ^= bit1;
    crc_state_local = (WORD16)((WORD32)(crc_state_local & 0x0000FFFF) << 1);
    if (bit0) {
      crc_state_local ^= crc10;
    }
    data_bit_mask = (data_bit_mask >> 1);
  }
  *crc_state = crc_state_local;
  return;
}
 
static PLATFORM_INLINE WORD32 ixheaacd_sbr_crc(ia_bit_buf_struct* it_bit_buff,
                                               WORD32 num_crc_bits) {
  WORD32 i;
  WORD32 num_full_bytes, rem_bits;
  WORD32 byte_value;
  WORD16 crc_state = 0;
 
  num_full_bytes = (num_crc_bits >> 3);
  rem_bits = (num_crc_bits & 0x7);
 
  for (i = 0; i < num_full_bytes; i++) {
    byte_value = ixheaacd_read_bits_buf(it_bit_buff, 8);
    ixheaacd_calc_chk_sum(&crc_state, byte_value, 8);
  }
 
  byte_value = ixheaacd_read_bits_buf(it_bit_buff, rem_bits);
  ixheaacd_calc_chk_sum(&crc_state, byte_value, rem_bits);
 
  return (crc_state & 0x03FF);
}
 
FLAG ixheaacd_sbr_crccheck(ia_bit_buf_struct* it_bit_buff,
                           WORD32 crc_bits_len) {
  struct ia_bit_buf_struct it_bit_buff_local = {0};
  WORD32 num_crc_bits;
  WORD32 calc_crc_sum;
  WORD32 bits_available;
  WORD32 crc_check_sum;
 
  crc_check_sum = ixheaacd_read_bits_buf(it_bit_buff, SBR_CYC_REDCY_CHK_BITS);
 
  it_bit_buff_local = *it_bit_buff;
 
  bits_available = it_bit_buff->cnt_bits;
 
  if (bits_available <= 0) {
    return 0;
  }
 
  num_crc_bits =
      (crc_bits_len > bits_available) ? bits_available : crc_bits_len;
 
  calc_crc_sum = ixheaacd_sbr_crc(&it_bit_buff_local, num_crc_bits);
 
  if (calc_crc_sum != crc_check_sum) {
    return 0;
  }
  return 1;
}