Coverage Report

Created: 2026-06-13 07:04

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
23.2k
                                        UWORD8 num_bits) {
39
23.2k
  WORD32 idx;
40
23.2k
  UWORD32 value;
41
42
23.2k
  if (num_bits > 16) {
43
3.30k
    WORD32 cnt;
44
3.30k
    UWORD8 rem;
45
3.30k
    cnt = num_bits >> 4;
46
3.30k
    rem = num_bits % 16;
47
48
16.8k
    for (idx = 0; idx < cnt; idx++) {
49
13.5k
      value = ixheaace_readbits(hdl_bitbuf_read, 16);
50
13.5k
      ixheaace_write_bits(hdl_bitbuf_write, value, 16);
51
13.5k
    }
52
3.30k
    if (rem) {
53
3.06k
      value = ixheaace_readbits(hdl_bitbuf_read, rem);
54
3.06k
      ixheaace_write_bits(hdl_bitbuf_write, value, rem);
55
3.06k
    }
56
19.9k
  } else {
57
19.9k
    value = ixheaace_readbits(hdl_bitbuf_read, num_bits);
58
19.9k
    ixheaace_write_bits(hdl_bitbuf_write, value, num_bits);
59
19.9k
  }
60
61
23.2k
  return num_bits;
62
23.2k
}
63
64
WORD32
65
ixheaace_enc_write_ps_data(ixheaace_pstr_ps_enc pstr_ps_handle, WORD32 b_header_active,
66
23.2k
                           ixheaace_str_ps_tab *ps_tables) {
67
23.2k
  WORD32 tmp_var, gr;
68
23.2k
  const WORD32 *aa_huff_book_iid_c;
69
23.2k
  const WORD16 *aa_huff_book_icc_c;
70
23.2k
  const WORD8 *aa_huff_book_iid_l;
71
23.2k
  const WORD8 *aa_huff_book_icc_l;
72
23.2k
  WORD32 *aa_delta_iid;
73
23.2k
  WORD32 *aa_delta_icc;
74
75
23.2k
  WORD8 index, last_index;
76
23.2k
  WORD32 no_bits_f = 0;
77
23.2k
  WORD32 no_bits_t = 0;
78
79
23.2k
  WORD32 aa_delta_iid_t[NUMBER_OF_IID_BINS] = {0};
80
23.2k
  WORD32 aa_delta_icc_t[NUMBER_OF_ICC_BINS] = {0};
81
82
23.2k
  WORD32 aa_delta_iid_f[NUMBER_OF_IID_BINS] = {0};
83
23.2k
  WORD32 aa_delta_icc_f[NUMBER_OF_ICC_BINS] = {0};
84
85
23.2k
  WORD32 ab_dt_flag_iid;
86
23.2k
  WORD32 ab_dt_flag_icc;
87
88
23.2k
  WORD32 b_send_header;
89
90
23.2k
  UWORD32 b_zero_iid = 1;
91
23.2k
  UWORD32 b_zero_icc = 1;
92
23.2k
  UWORD32 b_keep_params = 1;
93
94
23.2k
  ixheaace_bit_buf_handle bb = &pstr_ps_handle->ps_bit_buf;
95
96
23.2k
  tmp_var = ia_enhaacplus_enc_get_bits_available(bb);
97
98
  /* bit buffer shall be empty */
99
23.2k
  if (tmp_var != 0) {
100
0
    return -1;
101
0
  }
102
103
23.2k
  if (b_header_active) {
104
2.91k
    b_keep_params = 0;
105
2.91k
  }
106
107
23.2k
  last_index = 0;
108
109
429k
  for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
110
406k
    WORD IID_flag;
111
406k
    FLOAT32 pan_value = pstr_ps_handle->aaa_IID_data_buf[gr][SYSTEMLOOKAHEAD];
112
406k
    IID_flag = 0;
113
406k
    if ((pan_value >= -ps_tables->pan_class[0]) && (pan_value <= ps_tables->pan_class[0])) {
114
385k
      IID_flag = 1;
115
385k
    }
116
117
406k
    if (IID_flag) {
118
385k
      index = 0;
119
385k
    } else {
120
21.2k
      if (pan_value < 0) {
121
57.6k
        for (index = NO_IID_STEPS - 1; pan_value > -ps_tables->pan_class[index]; index--) {
122
47.1k
        }
123
10.5k
        index = -index - 1;
124
10.6k
      } else {
125
59.9k
        for (index = NO_IID_STEPS - 1; pan_value < ps_tables->pan_class[index]; index--) {
126
49.2k
        }
127
10.6k
        index++;
128
10.6k
      }
129
130
21.2k
      b_zero_iid = 0;
131
21.2k
    }
132
133
406k
    if (gr == 0) {
134
23.2k
      aa_delta_iid_f[gr] = index;
135
23.2k
      no_bits_t = 0;
136
137
23.2k
      no_bits_f = ps_tables->a_book_ps_iid_freq_length[index + CODE_BCK_LAV_IID];
138
382k
    } else {
139
382k
      aa_delta_iid_f[gr] = index - last_index;
140
141
382k
      no_bits_f += ps_tables->a_book_ps_iid_freq_length[aa_delta_iid_f[gr] + CODE_BCK_LAV_IID];
142
382k
    }
143
144
406k
    last_index = index;
145
146
406k
    aa_delta_iid_t[gr] = index - pstr_ps_handle->a_last_iid_index[gr];
147
148
406k
    pstr_ps_handle->a_last_iid_index[gr] = index;
149
150
406k
    no_bits_t += ps_tables->a_book_ps_iid_time_length[aa_delta_iid_t[gr] + CODE_BCK_LAV_IID];
151
152
406k
    if (aa_delta_iid_t[gr] != 0) {
153
31.0k
      b_keep_params = 0;
154
31.0k
    }
155
406k
  }
156
157
23.2k
  if (no_bits_t < no_bits_f && !b_header_active) {
158
913
    ab_dt_flag_iid = 1;
159
913
    aa_delta_iid = aa_delta_iid_t;
160
913
    aa_huff_book_iid_c = ps_tables->a_book_ps_iid_time_code;
161
913
    aa_huff_book_iid_l = ps_tables->a_book_ps_iid_time_length;
162
22.3k
  } else {
163
22.3k
    ab_dt_flag_iid = 0;
164
22.3k
    aa_delta_iid = aa_delta_iid_f;
165
22.3k
    aa_huff_book_iid_c = ps_tables->a_book_ps_iid_freq_code;
166
22.3k
    aa_huff_book_iid_l = ps_tables->a_book_ps_iid_freq_length;
167
22.3k
  }
168
169
23.2k
  last_index = 0;
170
171
429k
  for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
172
406k
    WORD ICC_flag;
173
406k
    FLOAT32 sa_value = pstr_ps_handle->aaa_ICC_data_buf[gr][SYSTEMLOOKAHEAD];
174
406k
    ICC_flag = 0;
175
406k
    if (sa_value <= ps_tables->sa_class[0]) {
176
370k
      ICC_flag = 1;
177
370k
    }
178
406k
    if (ICC_flag) {
179
370k
      index = 0;
180
370k
    } else {
181
159k
      for (index = NO_ICC_STEPS - 2; sa_value < ps_tables->sa_class[index]; index--) {
182
123k
      }
183
35.7k
      index++;
184
185
35.7k
      b_zero_icc = 0;
186
35.7k
    }
187
188
406k
    if (gr == 0) {
189
23.2k
      aa_delta_icc_f[gr] = index;
190
191
23.2k
      no_bits_f = ps_tables->a_book_ps_icc_freq_length[index + CODE_BCK_LAV_ICC];
192
193
23.2k
      no_bits_t = 0;
194
382k
    } else {
195
382k
      aa_delta_icc_f[gr] = index - last_index;
196
197
382k
      no_bits_f += ps_tables->a_book_ps_icc_freq_length[aa_delta_icc_f[gr] + CODE_BCK_LAV_ICC];
198
382k
    }
199
200
406k
    last_index = index;
201
202
406k
    aa_delta_icc_t[gr] = index - pstr_ps_handle->a_last_icc_index[gr];
203
204
406k
    pstr_ps_handle->a_last_icc_index[gr] = index;
205
206
406k
    no_bits_t += ps_tables->a_book_ps_icc_time_length[aa_delta_icc_t[gr] + CODE_BCK_LAV_ICC];
207
208
406k
    if (aa_delta_icc_t[gr] != 0) {
209
41.8k
      b_keep_params = 0;
210
41.8k
    }
211
406k
  }
212
213
23.2k
  if (no_bits_t < no_bits_f && !b_header_active) {
214
679
    ab_dt_flag_icc = 1;
215
679
    aa_delta_icc = aa_delta_icc_t;
216
679
    aa_huff_book_icc_c = ps_tables->a_book_ps_icc_time_code;
217
679
    aa_huff_book_icc_l = ps_tables->a_book_ps_icc_time_length;
218
22.5k
  } else {
219
22.5k
    ab_dt_flag_icc = 0;
220
22.5k
    aa_delta_icc = aa_delta_icc_f;
221
22.5k
    aa_huff_book_icc_c = ps_tables->a_book_ps_icc_freq_code;
222
22.5k
    aa_huff_book_icc_l = ps_tables->a_book_ps_icc_freq_length;
223
22.5k
  }
224
225
23.2k
  {
226
23.2k
    static WORD32 initheader = 0;
227
228
23.2k
    if (!initheader || b_header_active) {
229
2.91k
      initheader = 1;
230
2.91k
      pstr_ps_handle->b_enable_header = 1;
231
20.3k
    } else {
232
20.3k
      pstr_ps_handle->b_enable_header = 0;
233
20.3k
    }
234
23.2k
  }
235
236
23.2k
  b_send_header = pstr_ps_handle->b_enable_header ||
237
20.3k
                  pstr_ps_handle->b_prev_zero_iid != b_zero_iid ||
238
18.4k
                  pstr_ps_handle->b_prev_zero_icc != b_zero_icc;
239
240
23.2k
  ixheaace_write_bits(bb, b_send_header, 1);
241
242
23.2k
  if (b_send_header) {
243
4.91k
    ixheaace_write_bits(bb, !b_zero_iid, 1);
244
245
4.91k
    if (!b_zero_iid) {
246
1.32k
      ixheaace_write_bits(bb, (pstr_ps_handle->b_hi_freq_res_iid_icc) ? 1 : 0, 3);
247
1.32k
    }
248
249
4.91k
    ixheaace_write_bits(bb, !b_zero_icc, 1);
250
251
4.91k
    if (!b_zero_icc) {
252
1.48k
      ixheaace_write_bits(bb, (pstr_ps_handle->b_hi_freq_res_iid_icc) ? 1 : 0, 3);
253
1.48k
    }
254
255
4.91k
    ixheaace_write_bits(bb, 0, 1);
256
4.91k
  }
257
258
23.2k
  ixheaace_write_bits(bb, 0, 1);
259
260
23.2k
  ixheaace_write_bits(bb, 1 - b_keep_params, 2);
261
262
23.2k
  if (!b_keep_params) {
263
6.69k
    if (!b_zero_iid) {
264
3.08k
      ixheaace_write_bits(bb, ab_dt_flag_iid, 1);
265
266
53.3k
      for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
267
50.2k
        ixheaace_write_bits(bb, aa_huff_book_iid_c[aa_delta_iid[gr] + CODE_BCK_LAV_IID],
268
50.2k
                            aa_huff_book_iid_l[aa_delta_iid[gr] + CODE_BCK_LAV_IID]);
269
50.2k
      }
270
3.08k
    }
271
6.69k
  }
272
273
23.2k
  if (!b_keep_params) {
274
6.69k
    if (!b_zero_icc) {
275
3.26k
      ixheaace_write_bits(bb, ab_dt_flag_icc, 1);
276
277
55.8k
      for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
278
52.6k
        ixheaace_write_bits(bb, aa_huff_book_icc_c[aa_delta_icc[gr] + CODE_BCK_LAV_ICC],
279
52.6k
                            aa_huff_book_icc_l[aa_delta_icc[gr] + CODE_BCK_LAV_ICC]);
280
52.6k
      }
281
3.26k
    }
282
6.69k
  }
283
284
23.2k
  pstr_ps_handle->b_prev_zero_iid = b_zero_iid;
285
23.2k
  pstr_ps_handle->b_prev_zero_icc = b_zero_icc;
286
287
23.2k
  return ia_enhaacplus_enc_get_bits_available(bb);
288
23.2k
}
289
290
WORD32
291
ixheaace_append_ps_bitstream(ixheaace_pstr_ps_enc pstr_ps_handle, ixheaace_bit_buf_handle hdl_bs,
292
46.4k
                             WORD32 *sbr_hdr_bits) {
293
46.4k
  if (!pstr_ps_handle) {
294
0
    return 0;
295
0
  }
296
46.4k
  if (!hdl_bs) {
297
23.2k
    return ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
298
23.2k
  } else {
299
23.2k
    UWORD8 num_bits = (UWORD8)ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
300
301
23.2k
    ixheaace_write_bits(hdl_bs, EXTENSION_ID_PS_CODING, SI_SBR_EXTENSION_ID_BITS);
302
23.2k
    iexheaax_append_bitstream(hdl_bs, &pstr_ps_handle->ps_bit_buf, num_bits);
303
304
23.2k
    pstr_ps_handle->bit_buf_read_offset = (WORD32)(pstr_ps_handle->ps_bit_buf.ptr_read_next -
305
23.2k
                                                   pstr_ps_handle->ps_bit_buf.ptr_bit_buf_base);
306
23.2k
    pstr_ps_handle->bit_buf_write_offset = (WORD32)(pstr_ps_handle->ps_bit_buf.ptr_write_next -
307
23.2k
                                                    pstr_ps_handle->ps_bit_buf.ptr_bit_buf_base);
308
309
23.2k
    return ia_enhaacplus_enc_get_bits_available(hdl_bs) - (*sbr_hdr_bits) -
310
23.2k
           SI_FILL_EXTENTION_BITS;
311
23.2k
  }
312
46.4k
}