Coverage Report

Created: 2025-11-24 06:48

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