1 /******************************************************************************
2 * *
3 * Copyright (C) 2023 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
21 #include <string.h>
22
23 #include "ixheaac_type_def.h"
24 #include "ixheaac_constants.h"
25 #include "ixheaace_aac_constants.h"
26 #include "ixheaac_basic_ops32.h"
27 #include "ixheaac_basic_ops16.h"
28 #include "ixheaac_basic_ops40.h"
29
30 #include "ixheaace_bitbuffer.h"
31 #include "ixheaace_sbr_def.h"
32 #include "ixheaace_sbr_cmondata.h"
33 #include "ixheaace_sbr_crc.h"
34 #include "ixheaace_common_utils.h"
35
ixheaace_crc_advance(UWORD16 crc_poly,UWORD16 crc_mask,UWORD16 * ptr_crc,UWORD32 b_value,WORD32 b_bits)36 static VOID ixheaace_crc_advance(UWORD16 crc_poly, UWORD16 crc_mask, UWORD16 *ptr_crc,
37 UWORD32 b_value, WORD32 b_bits) {
38 WORD32 i = b_bits - 1;
39
40 while (i >= 0) {
41 UWORD16 flag = (*ptr_crc) & crc_mask ? 1 : 0;
42
43 flag ^= (b_value & (1 << i) ? 1 : 0);
44
45 (*ptr_crc) <<= 1;
46
47 if (flag) {
48 (*ptr_crc) ^= crc_poly;
49 }
50 i--;
51 }
52 }
53
ixheaace_init_sbr_bitstream(ixheaace_pstr_common_data pstr_cmon_data,UWORD8 * ptr_memory_base,WORD32 memory_size,WORD32 crc_active,ixheaace_sbr_codec_type sbr_codec)54 VOID ixheaace_init_sbr_bitstream(ixheaace_pstr_common_data pstr_cmon_data,
55 UWORD8 *ptr_memory_base, WORD32 memory_size, WORD32 crc_active,
56 ixheaace_sbr_codec_type sbr_codec) {
57 ixheaace_reset_bitbuf(&pstr_cmon_data->str_sbr_bit_buf, ptr_memory_base, memory_size);
58
59 pstr_cmon_data->str_tmp_write_bit_buf = pstr_cmon_data->str_sbr_bit_buf;
60 if (HEAAC_SBR == sbr_codec) {
61 ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0, SI_FILL_EXTENTION_BITS);
62
63 if (crc_active) {
64 ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0, SI_CRC_BITS_SBR);
65 }
66 }
67 }
68
ixheaace_assemble_sbr_bitstream(ixheaace_pstr_common_data pstr_cmon_data,ixheaace_sbr_codec_type sbr_codec)69 VOID ixheaace_assemble_sbr_bitstream(ixheaace_pstr_common_data pstr_cmon_data,
70 ixheaace_sbr_codec_type sbr_codec) {
71 UWORD16 crc_reg = CRCINIT_SBR;
72 WORD32 num_crc_bits, i;
73 WORD32 sbr_load = 0;
74
75 sbr_load = pstr_cmon_data->sbr_hdr_bits + pstr_cmon_data->sbr_data_bits;
76
77 if (HEAAC_SBR == sbr_codec) {
78 sbr_load += SI_FILL_EXTENTION_BITS;
79 }
80
81 if (pstr_cmon_data->sbr_crc_len) {
82 sbr_load += SI_CRC_BITS_SBR;
83 }
84
85 if (USAC_SBR != sbr_codec) {
86 pstr_cmon_data->sbr_fill_bits = (8 - (sbr_load) % 8) % 8;
87
88 ixheaace_write_bits(&pstr_cmon_data->str_sbr_bit_buf, 0,
89 (UWORD8)pstr_cmon_data->sbr_fill_bits);
90 }
91 if (pstr_cmon_data->sbr_crc_len) {
92 ixheaace_bit_buf tmp_crc_buf = pstr_cmon_data->str_sbr_bit_buf;
93
94 ixheaace_readbits(&tmp_crc_buf, SI_FILL_EXTENTION_BITS);
95
96 ixheaace_readbits(&tmp_crc_buf, SI_CRC_BITS_SBR);
97
98 num_crc_bits = pstr_cmon_data->sbr_hdr_bits + pstr_cmon_data->sbr_data_bits +
99 pstr_cmon_data->sbr_fill_bits;
100
101 i = 0;
102 while (i < num_crc_bits) {
103 UWORD32 bit;
104
105 bit = ixheaace_readbits(&tmp_crc_buf, 1);
106
107 ixheaace_crc_advance(CRC_POLYNOMIAL_SBR, CRC_MASK_SBR, &crc_reg, bit, 1);
108
109 i++;
110 }
111
112 crc_reg &= (CRC_RANGE_SBR);
113 }
114
115 if (pstr_cmon_data->sbr_crc_len) {
116 ixheaace_write_bits(&pstr_cmon_data->str_tmp_write_bit_buf, SI_FIL_CRC_SBR,
117 SI_FILL_EXTENTION_BITS);
118
119 ixheaace_write_bits(&pstr_cmon_data->str_tmp_write_bit_buf, crc_reg, SI_CRC_BITS_SBR);
120 } else {
121 if (HEAAC_SBR == sbr_codec) {
122 ixheaace_write_bits(&pstr_cmon_data->str_tmp_write_bit_buf, SI_FIL_SBR,
123 SI_FILL_EXTENTION_BITS);
124 }
125 }
126 }
127