Coverage Report

Created: 2026-01-09 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/ixheaace_ps_bitenc.c
Line
Count
Source
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 "ixheaac_type_def.h"
22
23
#include "ixheaace_sbr_header.h"
24
#include "ixheaace_sbr_def.h"
25
#include "ixheaace_resampler.h"
26
#include "ixheaace_sbr_rom.h"
27
#include "ixheaace_common_rom.h"
28
#include "ixheaace_bitbuffer.h"
29
#include "ixheaace_sbr_main.h"
30
31
#include "ixheaace_sbr_hybrid.h"
32
#include "ixheaace_sbr_ps_enc.h"
33
#include "ixheaace_sbr_ps_bitenc.h"
34
#include "ixheaace_env_bit.h"
35
36
static WORD32 iexheaax_append_bitstream(ixheaace_bit_buf_handle hdl_bitbuf_write,
37
                                        ixheaace_bit_buf_handle hdl_bitbuf_read,
38
27.7k
                                        UWORD8 num_bits) {
39
27.7k
  WORD32 idx;
40
27.7k
  UWORD32 value;
41
42
27.7k
  if (num_bits > 16) {
43
4.85k
    WORD32 cnt;
44
4.85k
    UWORD8 rem;
45
4.85k
    cnt = num_bits >> 4;
46
4.85k
    rem = num_bits % 16;
47
48
26.3k
    for (idx = 0; idx < cnt; idx++) {
49
21.4k
      value = ixheaace_readbits(hdl_bitbuf_read, 16);
50
21.4k
      ixheaace_write_bits(hdl_bitbuf_write, value, 16);
51
21.4k
    }
52
4.85k
    if (rem) {
53
4.50k
      value = ixheaace_readbits(hdl_bitbuf_read, rem);
54
4.50k
      ixheaace_write_bits(hdl_bitbuf_write, value, rem);
55
4.50k
    }
56
22.9k
  } else {
57
22.9k
    value = ixheaace_readbits(hdl_bitbuf_read, num_bits);
58
22.9k
    ixheaace_write_bits(hdl_bitbuf_write, value, num_bits);
59
22.9k
  }
60
61
27.7k
  return num_bits;
62
27.7k
}
63
64
WORD32
65
ixheaace_enc_write_ps_data(ixheaace_pstr_ps_enc pstr_ps_handle, WORD32 b_header_active,
66
27.7k
                           ixheaace_str_ps_tab *ps_tables) {
67
27.7k
  WORD32 tmp_var, gr;
68
27.7k
  const WORD32 *aa_huff_book_iid_c;
69
27.7k
  const WORD16 *aa_huff_book_icc_c;
70
27.7k
  const WORD8 *aa_huff_book_iid_l;
71
27.7k
  const WORD8 *aa_huff_book_icc_l;
72
27.7k
  WORD32 *aa_delta_iid;
73
27.7k
  WORD32 *aa_delta_icc;
74
75
27.7k
  WORD8 index, last_index;
76
27.7k
  WORD32 no_bits_f = 0;
77
27.7k
  WORD32 no_bits_t = 0;
78
79
27.7k
  WORD32 aa_delta_iid_t[NUMBER_OF_IID_BINS] = {0};
80
27.7k
  WORD32 aa_delta_icc_t[NUMBER_OF_ICC_BINS] = {0};
81
82
27.7k
  WORD32 aa_delta_iid_f[NUMBER_OF_IID_BINS] = {0};
83
27.7k
  WORD32 aa_delta_icc_f[NUMBER_OF_ICC_BINS] = {0};
84
85
27.7k
  WORD32 ab_dt_flag_iid;
86
27.7k
  WORD32 ab_dt_flag_icc;
87
88
27.7k
  WORD32 b_send_header;
89
90
27.7k
  UWORD32 b_zero_iid = 1;
91
27.7k
  UWORD32 b_zero_icc = 1;
92
27.7k
  UWORD32 b_keep_params = 1;
93
94
27.7k
  ixheaace_bit_buf_handle bb = &pstr_ps_handle->ps_bit_buf;
95
96
27.7k
  tmp_var = ia_enhaacplus_enc_get_bits_available(bb);
97
98
  /* bit buffer shall be empty */
99
27.7k
  if (tmp_var != 0) {
100
0
    return -1;
101
0
  }
102
103
27.7k
  if (b_header_active) {
104
3.27k
    b_keep_params = 0;
105
3.27k
  }
106
107
27.7k
  last_index = 0;
108
109
498k
  for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
110
470k
    WORD IID_flag;
111
470k
    FLOAT32 pan_value = pstr_ps_handle->aaa_IID_data_buf[gr][SYSTEMLOOKAHEAD];
112
470k
    IID_flag = 0;
113
470k
    if ((pan_value >= -ps_tables->pan_class[0]) && (pan_value <= ps_tables->pan_class[0])) {
114
435k
      IID_flag = 1;
115
435k
    }
116
117
470k
    if (IID_flag) {
118
435k
      index = 0;
119
435k
    } else {
120
35.2k
      if (pan_value < 0) {
121
93.0k
        for (index = NO_IID_STEPS - 1; pan_value > -ps_tables->pan_class[index]; index--) {
122
75.5k
        }
123
17.4k
        index = -index - 1;
124
17.7k
      } else {
125
93.7k
        for (index = NO_IID_STEPS - 1; pan_value < ps_tables->pan_class[index]; index--) {
126
75.9k
        }
127
17.7k
        index++;
128
17.7k
      }
129
130
35.2k
      b_zero_iid = 0;
131
35.2k
    }
132
133
470k
    if (gr == 0) {
134
27.7k
      aa_delta_iid_f[gr] = index;
135
27.7k
      no_bits_t = 0;
136
137
27.7k
      no_bits_f = ps_tables->a_book_ps_iid_freq_length[index + CODE_BCK_LAV_IID];
138
443k
    } else {
139
443k
      aa_delta_iid_f[gr] = index - last_index;
140
141
443k
      no_bits_f += ps_tables->a_book_ps_iid_freq_length[aa_delta_iid_f[gr] + CODE_BCK_LAV_IID];
142
443k
    }
143
144
470k
    last_index = index;
145
146
470k
    aa_delta_iid_t[gr] = index - pstr_ps_handle->a_last_iid_index[gr];
147
148
470k
    pstr_ps_handle->a_last_iid_index[gr] = index;
149
150
470k
    no_bits_t += ps_tables->a_book_ps_iid_time_length[aa_delta_iid_t[gr] + CODE_BCK_LAV_IID];
151
152
470k
    if (aa_delta_iid_t[gr] != 0) {
153
50.7k
      b_keep_params = 0;
154
50.7k
    }
155
470k
  }
156
157
27.7k
  if (no_bits_t < no_bits_f && !b_header_active) {
158
1.23k
    ab_dt_flag_iid = 1;
159
1.23k
    aa_delta_iid = aa_delta_iid_t;
160
1.23k
    aa_huff_book_iid_c = ps_tables->a_book_ps_iid_time_code;
161
1.23k
    aa_huff_book_iid_l = ps_tables->a_book_ps_iid_time_length;
162
26.5k
  } else {
163
26.5k
    ab_dt_flag_iid = 0;
164
26.5k
    aa_delta_iid = aa_delta_iid_f;
165
26.5k
    aa_huff_book_iid_c = ps_tables->a_book_ps_iid_freq_code;
166
26.5k
    aa_huff_book_iid_l = ps_tables->a_book_ps_iid_freq_length;
167
26.5k
  }
168
169
27.7k
  last_index = 0;
170
171
498k
  for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
172
470k
    WORD ICC_flag;
173
470k
    FLOAT32 sa_value = pstr_ps_handle->aaa_ICC_data_buf[gr][SYSTEMLOOKAHEAD];
174
470k
    ICC_flag = 0;
175
470k
    if (sa_value <= ps_tables->sa_class[0]) {
176
414k
      ICC_flag = 1;
177
414k
    }
178
470k
    if (ICC_flag) {
179
414k
      index = 0;
180
414k
    } else {
181
248k
      for (index = NO_ICC_STEPS - 2; sa_value < ps_tables->sa_class[index]; index--) {
182
192k
      }
183
56.2k
      index++;
184
185
56.2k
      b_zero_icc = 0;
186
56.2k
    }
187
188
470k
    if (gr == 0) {
189
27.7k
      aa_delta_icc_f[gr] = index;
190
191
27.7k
      no_bits_f = ps_tables->a_book_ps_icc_freq_length[index + CODE_BCK_LAV_ICC];
192
193
27.7k
      no_bits_t = 0;
194
443k
    } else {
195
443k
      aa_delta_icc_f[gr] = index - last_index;
196
197
443k
      no_bits_f += ps_tables->a_book_ps_icc_freq_length[aa_delta_icc_f[gr] + CODE_BCK_LAV_ICC];
198
443k
    }
199
200
470k
    last_index = index;
201
202
470k
    aa_delta_icc_t[gr] = index - pstr_ps_handle->a_last_icc_index[gr];
203
204
470k
    pstr_ps_handle->a_last_icc_index[gr] = index;
205
206
470k
    no_bits_t += ps_tables->a_book_ps_icc_time_length[aa_delta_icc_t[gr] + CODE_BCK_LAV_ICC];
207
208
470k
    if (aa_delta_icc_t[gr] != 0) {
209
66.4k
      b_keep_params = 0;
210
66.4k
    }
211
470k
  }
212
213
27.7k
  if (no_bits_t < no_bits_f && !b_header_active) {
214
945
    ab_dt_flag_icc = 1;
215
945
    aa_delta_icc = aa_delta_icc_t;
216
945
    aa_huff_book_icc_c = ps_tables->a_book_ps_icc_time_code;
217
945
    aa_huff_book_icc_l = ps_tables->a_book_ps_icc_time_length;
218
26.8k
  } else {
219
26.8k
    ab_dt_flag_icc = 0;
220
26.8k
    aa_delta_icc = aa_delta_icc_f;
221
26.8k
    aa_huff_book_icc_c = ps_tables->a_book_ps_icc_freq_code;
222
26.8k
    aa_huff_book_icc_l = ps_tables->a_book_ps_icc_freq_length;
223
26.8k
  }
224
225
27.7k
  {
226
27.7k
    static WORD32 initheader = 0;
227
228
27.7k
    if (!initheader || b_header_active) {
229
3.27k
      initheader = 1;
230
3.27k
      pstr_ps_handle->b_enable_header = 1;
231
24.4k
    } else {
232
24.4k
      pstr_ps_handle->b_enable_header = 0;
233
24.4k
    }
234
27.7k
  }
235
236
27.7k
  b_send_header = pstr_ps_handle->b_enable_header ||
237
24.4k
                  pstr_ps_handle->b_prev_zero_iid != b_zero_iid ||
238
21.7k
                  pstr_ps_handle->b_prev_zero_icc != b_zero_icc;
239
240
27.7k
  ixheaace_write_bits(bb, b_send_header, 1);
241
242
27.7k
  if (b_send_header) {
243
6.18k
    ixheaace_write_bits(bb, !b_zero_iid, 1);
244
245
6.18k
    if (!b_zero_iid) {
246
1.92k
      ixheaace_write_bits(bb, (pstr_ps_handle->b_hi_freq_res_iid_icc) ? 1 : 0, 3);
247
1.92k
    }
248
249
6.18k
    ixheaace_write_bits(bb, !b_zero_icc, 1);
250
251
6.18k
    if (!b_zero_icc) {
252
2.12k
      ixheaace_write_bits(bb, (pstr_ps_handle->b_hi_freq_res_iid_icc) ? 1 : 0, 3);
253
2.12k
    }
254
255
6.18k
    ixheaace_write_bits(bb, 0, 1);
256
6.18k
  }
257
258
27.7k
  ixheaace_write_bits(bb, 0, 1);
259
260
27.7k
  ixheaace_write_bits(bb, 1 - b_keep_params, 2);
261
262
27.7k
  if (!b_keep_params) {
263
8.87k
    if (!b_zero_iid) {
264
4.58k
      ixheaace_write_bits(bb, ab_dt_flag_iid, 1);
265
266
85.0k
      for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
267
80.4k
        ixheaace_write_bits(bb, aa_huff_book_iid_c[aa_delta_iid[gr] + CODE_BCK_LAV_IID],
268
80.4k
                            aa_huff_book_iid_l[aa_delta_iid[gr] + CODE_BCK_LAV_IID]);
269
80.4k
      }
270
4.58k
    }
271
8.87k
  }
272
273
27.7k
  if (!b_keep_params) {
274
8.87k
    if (!b_zero_icc) {
275
4.81k
      ixheaace_write_bits(bb, ab_dt_flag_icc, 1);
276
277
88.8k
      for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
278
84.0k
        ixheaace_write_bits(bb, aa_huff_book_icc_c[aa_delta_icc[gr] + CODE_BCK_LAV_ICC],
279
84.0k
                            aa_huff_book_icc_l[aa_delta_icc[gr] + CODE_BCK_LAV_ICC]);
280
84.0k
      }
281
4.81k
    }
282
8.87k
  }
283
284
27.7k
  pstr_ps_handle->b_prev_zero_iid = b_zero_iid;
285
27.7k
  pstr_ps_handle->b_prev_zero_icc = b_zero_icc;
286
287
27.7k
  return ia_enhaacplus_enc_get_bits_available(bb);
288
27.7k
}
289
290
WORD32
291
ixheaace_append_ps_bitstream(ixheaace_pstr_ps_enc pstr_ps_handle, ixheaace_bit_buf_handle hdl_bs,
292
55.5k
                             WORD32 *sbr_hdr_bits) {
293
55.5k
  if (!pstr_ps_handle) {
294
0
    return 0;
295
0
  }
296
55.5k
  if (!hdl_bs) {
297
27.7k
    return ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
298
27.7k
  } else {
299
27.7k
    UWORD8 num_bits = (UWORD8)ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
300
301
27.7k
    ixheaace_write_bits(hdl_bs, EXTENSION_ID_PS_CODING, SI_SBR_EXTENSION_ID_BITS);
302
27.7k
    iexheaax_append_bitstream(hdl_bs, &pstr_ps_handle->ps_bit_buf, num_bits);
303
304
27.7k
    pstr_ps_handle->bit_buf_read_offset = (WORD32)(pstr_ps_handle->ps_bit_buf.ptr_read_next -
305
27.7k
                                                   pstr_ps_handle->ps_bit_buf.ptr_bit_buf_base);
306
27.7k
    pstr_ps_handle->bit_buf_write_offset = (WORD32)(pstr_ps_handle->ps_bit_buf.ptr_write_next -
307
27.7k
                                                    pstr_ps_handle->ps_bit_buf.ptr_bit_buf_base);
308
309
27.7k
    return ia_enhaacplus_enc_get_bits_available(hdl_bs) - (*sbr_hdr_bits) -
310
27.7k
           SI_FILL_EXTENTION_BITS;
311
27.7k
  }
312
55.5k
}