Coverage Report

Created: 2026-01-09 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/encoder/iusace_enc_fac.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 <string.h>
22
#include "ixheaac_type_def.h"
23
#include "ixheaace_adjust_threshold_data.h"
24
#include "iusace_bitbuffer.h"
25
26
/* DRC */
27
#include "impd_drc_common_enc.h"
28
#include "impd_drc_uni_drc.h"
29
#include "impd_drc_tables.h"
30
#include "impd_drc_api.h"
31
#include "impd_drc_uni_drc_eq.h"
32
#include "impd_drc_uni_drc_filter_bank.h"
33
#include "impd_drc_gain_enc.h"
34
#include "impd_drc_struct_def.h"
35
36
#include "iusace_cnst.h"
37
#include "iusace_tns_usac.h"
38
#include "iusace_psy_mod.h"
39
#include "iusace_config.h"
40
#include "iusace_arith_enc.h"
41
#include "iusace_block_switch_const.h"
42
#include "iusace_block_switch_struct_def.h"
43
#include "iusace_fd_qc_util.h"
44
#include "iusace_fd_quant.h"
45
#include "iusace_ms.h"
46
#include "iusace_signal_classifier.h"
47
#include "ixheaace_sbr_header.h"
48
#include "ixheaace_config.h"
49
#include "ixheaace_asc_write.h"
50
#include "iusace_main.h"
51
#include "iusace_write_bitstream.h"
52
#include "iusace_func_prototypes.h"
53
#include "iusace_avq_enc.h"
54
#include "iusace_lpd_rom.h"
55
56
1.64M
static WORD32 iusace_unary_code(WORD32 idx, WORD16 *ptr_bit_buf) {
57
1.64M
  WORD32 num_bits;
58
59
1.64M
  num_bits = 1;
60
61
1.64M
  idx -= 1;
62
4.02M
  while (idx-- > 0) {
63
2.37M
    *ptr_bit_buf++ = 1;
64
2.37M
    num_bits++;
65
2.37M
  }
66
67
1.64M
  *ptr_bit_buf = 0;
68
69
1.64M
  return (num_bits);
70
1.64M
}
71
72
static VOID iusace_get_nk_mode(WORD32 mode_lpc, ia_bit_buf_struct *pstr_it_bit_buff,
73
454k
                               WORD32 *nk_mode, WORD32 lpc_set) {
74
454k
  switch (lpc_set) {
75
150k
    case 4:
76
150k
      break;
77
4.29k
    case 0:
78
116k
    case 2:
79
116k
      *nk_mode = 3;
80
116k
      iusace_write_bits_buf(pstr_it_bit_buff, mode_lpc, 1);
81
116k
      break;
82
94.3k
    case 1:
83
94.3k
      *nk_mode = mode_lpc;
84
94.3k
      if (mode_lpc == 2) {
85
44.7k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
86
49.5k
      } else if (mode_lpc == 1) {
87
28.5k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
88
28.5k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
89
28.5k
      } else if (mode_lpc == 0) {
90
21.0k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
91
21.0k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
92
21.0k
      }
93
94.3k
      break;
94
93.4k
    case 3:
95
93.4k
      if (mode_lpc == 0) {
96
1.41k
        *nk_mode = 0;
97
1.41k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
98
1.41k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
99
92.0k
      } else if (mode_lpc == 1) {
100
53.3k
        *nk_mode = 1;
101
53.3k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
102
53.3k
      } else if (mode_lpc == 2) {
103
3.49k
        *nk_mode = 2;
104
3.49k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
105
3.49k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
106
3.49k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
107
35.1k
      } else {
108
35.1k
        *nk_mode = 2;
109
35.1k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
110
35.1k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
111
35.1k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
112
35.1k
      }
113
93.4k
      break;
114
454k
  }
115
454k
  return;
116
454k
}
117
118
static VOID iusace_write_qn_data(WORD32 *qn, ia_bit_buf_struct *pstr_it_bit_buff, WORD32 nk_mode,
119
426k
                                 WORD32 num_frames) {
120
426k
  WORD32 k, i;
121
426k
  switch (nk_mode) {
122
53.3k
    case 1:
123
160k
      for (k = 0; k < 2; k++) {
124
178k
        for (i = 0; i < qn[k] - 1; i++) {
125
71.6k
          iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
126
71.6k
        }
127
106k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
128
106k
      }
129
53.3k
      break;
130
172k
    case 0:
131
256k
    case 2:
132
372k
    case 3:
133
1.11M
      for (k = 0; k < 2; k++) {
134
745k
        WORD32 qn1 = qn[k] - 2;
135
745k
        if (qn1 < 0 || qn1 > 3) {
136
363k
          qn1 = 3;
137
363k
        }
138
745k
        iusace_write_bits_buf(pstr_it_bit_buff, qn1, 2);
139
745k
      }
140
372k
      if ((nk_mode == 2) && num_frames != 2) {
141
250k
        for (k = 0; k < 2; k++) {
142
166k
          if (qn[k] > 4) {
143
104k
            for (i = 0; i < qn[k] - 4; i++) {
144
68.4k
              iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
145
68.4k
            }
146
35.8k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
147
35.8k
          }
148
166k
          if (qn[k] == 0) {
149
23.8k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
150
23.8k
          }
151
166k
        }
152
289k
      } else {
153
868k
        for (k = 0; k < 2; k++) {
154
578k
          if (qn[k] == 5) {
155
45.8k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
156
533k
          } else if (qn[k] == 6) {
157
133k
            iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
158
133k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
159
400k
          } else if (qn[k] == 0) {
160
134k
            iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
161
134k
            iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
162
134k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
163
265k
          } else {
164
265k
            WORD32 qn_ext = qn[k] - 4;
165
265k
            if (qn_ext > 0) {
166
236k
              for (i = 0; i < qn_ext; i++) {
167
184k
                iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
168
184k
              }
169
51.6k
              iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
170
51.6k
            }
171
265k
          }
172
578k
        }
173
289k
      }
174
372k
      break;
175
426k
  }
176
426k
  return;
177
426k
}
178
179
static VOID iusace_write_cb_indices(WORD32 *qn, WORD32 *ptr_params, WORD32 *idx,
180
                                    ia_bit_buf_struct *pstr_it_bit_buff, WORD32 nk_mode,
181
426k
                                    WORD32 num_frames) {
182
426k
  WORD32 k;
183
426k
  WORD32 j = *idx;
184
185
426k
  iusace_write_qn_data(qn, pstr_it_bit_buff, nk_mode, num_frames);
186
187
1.27M
  for (k = 0; k < 2; k++) {
188
852k
    if (qn[k] > 0) {
189
630k
      WORD32 n, nk, i;
190
630k
      if (qn[k] > 4) {
191
268k
        nk = (qn[k] - 3) >> 1;
192
268k
        n = qn[k] - nk * 2;
193
361k
      } else {
194
361k
        nk = 0;
195
361k
        n = qn[k];
196
361k
      }
197
198
630k
      iusace_write_bits_buf(pstr_it_bit_buff, ptr_params[j++], (UWORD8)(4 * n));
199
200
5.67M
      for (i = 0; i < 8; i++) {
201
5.04M
        iusace_write_bits_buf(pstr_it_bit_buff, ptr_params[j++], (UWORD8)nk);
202
5.04M
      }
203
630k
    }
204
852k
  }
205
206
426k
  *idx = j;
207
208
426k
  return;
209
426k
}
210
211
static VOID iusace_write_lpc_data(ia_bit_buf_struct *pstr_it_bit_buff, WORD32 *param_lpc,
212
150k
                                  WORD32 first_lpd_flag, WORD32 *mod, WORD32 num_frames) {
213
150k
  WORD32 nk_mode = 0;
214
150k
  WORD32 j = 0, k;
215
150k
  WORD32 mode_lpc = 0;
216
150k
  WORD32 qn[2] = {0};
217
218
150k
  iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 4);
219
220
150k
  iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
221
222
450k
  for (k = 0; k < 2; k++) {
223
300k
    qn[k] = param_lpc[j++];
224
300k
  }
225
226
150k
  iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
227
228
150k
  if (first_lpd_flag) {
229
4.29k
    mode_lpc = param_lpc[j++];
230
4.29k
    iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 0);
231
232
4.29k
    if (mode_lpc == 0) {
233
221
      iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
234
221
    }
235
236
12.8k
    for (k = 0; k < 2; k++) {
237
8.59k
      qn[k] = param_lpc[j++];
238
8.59k
    }
239
240
4.29k
    iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
241
4.29k
  }
242
243
150k
  mode_lpc = param_lpc[j++];
244
245
150k
  if (num_frames == 4 && mod[0] < 3) {
246
112k
    iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 2);
247
248
112k
    if (mode_lpc == 0) {
249
39.9k
      iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
250
39.9k
    }
251
252
337k
    for (k = 0; k < 2; k++) {
253
225k
      qn[k] = param_lpc[j++];
254
225k
    }
255
256
112k
    iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
257
112k
  }
258
150k
  mode_lpc = param_lpc[j++];
259
150k
  if (mod[0] < 2) {
260
94.3k
    iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 1);
261
262
94.3k
    if (mode_lpc != 1) {
263
65.8k
      if (mode_lpc == 0) {
264
21.0k
        iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
265
21.0k
      }
266
267
197k
      for (k = 0; k < 2; k++) {
268
131k
        qn[k] = param_lpc[j++];
269
131k
      }
270
271
65.8k
      iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
272
65.8k
    }
273
94.3k
  } else if (mode_lpc != 1) {
274
43.2k
    if (mode_lpc == 0) {
275
22.2k
      j++;
276
22.2k
    }
277
129k
    for (k = 0; k < 2; k++) {
278
86.5k
      qn[k] = param_lpc[j++];
279
86.5k
    }
280
43.2k
    j += ((qn[0] > 0) ? 9 : 0) + ((qn[1] > 0) ? 9 : 0);
281
43.2k
  }
282
283
150k
  mode_lpc = param_lpc[j++];
284
150k
  if (num_frames != 2 && mod[2] < 2) {
285
93.4k
    iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 3);
286
93.4k
    if (mode_lpc == 0) {
287
1.41k
      iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
288
1.41k
    }
289
290
280k
    for (k = 0; k < 2; k++) {
291
186k
      qn[k] = param_lpc[j++];
292
186k
    }
293
93.4k
    iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
294
93.4k
  }
295
150k
  return;
296
150k
}
297
298
VOID iusace_encode_fac_params(WORD32 *mod, WORD32 *n_param_tcx, ia_usac_data_struct *usac_data,
299
                              WORD32 const usac_independency_flag,
300
150k
                              ia_bit_buf_struct *pstr_it_bit_buff, WORD32 ch_idx) {
301
150k
  WORD32 *total_nbbits = &usac_data->total_nbbits[ch_idx];
302
150k
  ia_usac_td_encoder_struct *pstr_td = usac_data->td_encoder[ch_idx];
303
150k
  WORD32 codec_mode = pstr_td->acelp_core_mode;
304
150k
  WORD16 *bit_buf = usac_data->td_serial_out[ch_idx];
305
150k
  WORD32 is_bass_post_filter = 1;
306
150k
  WORD32 first_lpd_flag = (usac_data->core_mode_prev[ch_idx] == CORE_MODE_FD);
307
150k
  WORD32 *param_lpc = usac_data->param_buf + (NUM_FRAMES * MAX_NUM_TCX_PRM_PER_DIV);
308
150k
  WORD32 *param = usac_data->param_buf;
309
150k
  WORD32 j, k, n, sfr, lpd_mode, num_bits, sq_bits, *prm;
310
150k
  WORD16 first_tcx_flag = 1;
311
150k
  WORD32 nbits_fac, nb_bits_lpc;
312
150k
  WORD32 core_mode_last = (first_lpd_flag) ? 0 : 1;
313
150k
  WORD32 fac_data_present;
314
150k
  WORD32 num_frames = NUM_FRAMES;
315
150k
  WORD16 *ptr_bit_buf = bit_buf;
316
317
150k
  pstr_td->num_bits_per_supfrm = 0;
318
150k
  *total_nbbits = 0;
319
320
150k
  iusace_write_bits_buf(pstr_it_bit_buff, pstr_td->acelp_core_mode, 3);
321
322
150k
  if (mod[0] == 3) {
323
37.5k
    lpd_mode = 25;
324
112k
  } else if ((mod[0] == 2) && (mod[2] == 2)) {
325
11.7k
    lpd_mode = 24;
326
100k
  } else {
327
100k
    if (mod[0] == 2) {
328
6.45k
      lpd_mode = 16 + mod[2] + 2 * mod[3];
329
94.3k
    } else if (mod[2] == 2) {
330
7.33k
      lpd_mode = 20 + mod[0] + 2 * mod[1];
331
87.0k
    } else {
332
87.0k
      lpd_mode = mod[0] + 2 * mod[1] + 4 * mod[2] + 8 * mod[3];
333
87.0k
    }
334
100k
  }
335
150k
  iusace_write_bits_buf(pstr_it_bit_buff, lpd_mode, 5);
336
150k
  pstr_td->num_bits_per_supfrm = 5;
337
150k
  *total_nbbits += 5;
338
339
150k
  iusace_write_bits_buf(pstr_it_bit_buff, is_bass_post_filter, 1);
340
150k
  *total_nbbits += 1;
341
342
150k
  iusace_write_bits_buf(pstr_it_bit_buff, core_mode_last, 1);
343
150k
  *total_nbbits += 1;
344
345
150k
  if (((mod[0] == 0) && (mod[-1] != 0)) || ((mod[0] > 0) && (mod[-1] == 0))) {
346
24.2k
    fac_data_present = 1;
347
125k
  } else {
348
125k
    fac_data_present = 0;
349
125k
  }
350
351
150k
  iusace_write_bits_buf(pstr_it_bit_buff, fac_data_present, 1);
352
150k
  *total_nbbits += 1;
353
354
150k
  num_bits = (iusace_acelp_core_numbits_1024[codec_mode] / 4) - 2;
355
356
150k
  k = 0;
357
600k
  while (k < num_frames) {
358
450k
    lpd_mode = mod[k];
359
450k
    prm = param + (k * MAX_NUM_TCX_PRM_PER_DIV);
360
450k
    j = 0;
361
362
450k
    if (((mod[k - 1] == 0) && (mod[k] > 0)) || ((mod[k - 1] > 0) && (mod[k] == 0))) {
363
104k
      nbits_fac = iusace_fd_encode_fac(&prm[j], ptr_bit_buf, (pstr_td->len_subfrm) / 2);
364
104k
      j += (pstr_td->len_subfrm) / 2;
365
104k
      *total_nbbits += nbits_fac;
366
16.7M
      for (WORD32 i = 0; i < nbits_fac; i++) {
367
16.6M
        iusace_write_bits_buf(pstr_it_bit_buff, ptr_bit_buf[i], 1);
368
16.6M
      }
369
104k
    }
370
371
450k
    if (lpd_mode == 0) {
372
246k
      iusace_write_bits_buf(pstr_it_bit_buff, prm[j++], 2);
373
374
1.15M
      for (sfr = 0; sfr < (pstr_td->num_subfrm); sfr++) {
375
912k
        n = 6;
376
912k
        if ((sfr == 0) || (((pstr_td->len_subfrm) == 256) && (sfr == 2))) n = 9;
377
912k
        iusace_write_bits_buf(pstr_it_bit_buff, prm[j], (UWORD8)n);
378
912k
        j++;
379
912k
        iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 1);
380
912k
        j++;
381
912k
        if (codec_mode == ACELP_CORE_MODE_9k6) {
382
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 5);
383
0
          j++;
384
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 5);
385
0
          j++;
386
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 5);
387
0
          j++;
388
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 5);
389
0
          j++;
390
912k
        } else if (codec_mode == ACELP_CORE_MODE_11k2) {
391
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
392
0
          j++;
393
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
394
0
          j++;
395
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 5);
396
0
          j++;
397
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 5);
398
0
          j++;
399
912k
        } else if (codec_mode == ACELP_CORE_MODE_12k8) {
400
91.3k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
401
91.3k
          j++;
402
91.3k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
403
91.3k
          j++;
404
91.3k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
405
91.3k
          j++;
406
91.3k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
407
91.3k
          j++;
408
820k
        } else if (codec_mode == ACELP_CORE_MODE_14k4) {
409
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
410
0
          j++;
411
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
412
0
          j++;
413
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
414
0
          j++;
415
0
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
416
0
          j++;
417
820k
        } else if (codec_mode == ACELP_CORE_MODE_16k) {
418
9.68k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
419
9.68k
          j++;
420
9.68k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
421
9.68k
          j++;
422
9.68k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
423
9.68k
          j++;
424
9.68k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
425
9.68k
          j++;
426
811k
        } else if (codec_mode == ACELP_CORE_MODE_18k4) {
427
811k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 2);
428
811k
          j++;
429
811k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 2);
430
811k
          j++;
431
811k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 2);
432
811k
          j++;
433
811k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 2);
434
811k
          j++;
435
811k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 14);
436
811k
          j++;
437
811k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 14);
438
811k
          j++;
439
811k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 14);
440
811k
          j++;
441
811k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 14);
442
811k
          j++;
443
811k
        }
444
912k
        iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 7);
445
912k
        j++;
446
912k
      }
447
246k
      *total_nbbits += (num_bits - NBITS_LPC);
448
246k
      pstr_td->num_bits_per_supfrm += (num_bits - NBITS_LPC);
449
246k
      k++;
450
246k
    } else {
451
204k
      iusace_write_bits_buf(pstr_it_bit_buff, prm[j++], 3);
452
204k
      *total_nbbits += 3;
453
204k
      pstr_td->num_bits_per_supfrm += 3;
454
204k
      iusace_write_bits_buf(pstr_it_bit_buff, prm[j++], 7);
455
204k
      *total_nbbits += 7;
456
204k
      pstr_td->num_bits_per_supfrm += 7;
457
458
204k
      if (first_tcx_flag) {
459
118k
        first_tcx_flag = 0;
460
118k
        if (usac_independency_flag) {
461
4.80k
          pstr_td->arith_reset_flag = 1;
462
4.80k
          memset(pstr_td->c_pres, 0, 516 * sizeof(WORD32));
463
4.80k
          memset(pstr_td->c_prev, 0, 516 * sizeof(WORD32));
464
114k
        } else {
465
114k
          if (pstr_td->arith_reset_flag) {
466
114k
            memset(pstr_td->c_pres, 0, 516 * sizeof(WORD32));
467
114k
            memset(pstr_td->c_prev, 0, 516 * sizeof(WORD32));
468
114k
          }
469
114k
          iusace_write_bits_buf(pstr_it_bit_buff, pstr_td->arith_reset_flag, 1);
470
114k
          *total_nbbits += 1;
471
114k
          pstr_td->num_bits_per_supfrm += 1;
472
114k
        }
473
118k
      }
474
475
204k
      sq_bits = iusace_tcx_coding(pstr_it_bit_buff, n_param_tcx[k], pstr_td->len_frame, prm + j,
476
204k
                                  pstr_td->c_pres, pstr_td->c_prev);
477
478
204k
      *total_nbbits += sq_bits;
479
204k
      pstr_td->num_bits_per_supfrm += sq_bits;
480
481
204k
      k += (1 << (lpd_mode - 1));
482
204k
    }
483
450k
  }
484
485
150k
  nb_bits_lpc = pstr_it_bit_buff->cnt_bits;
486
487
150k
  iusace_write_lpc_data(pstr_it_bit_buff, param_lpc, first_lpd_flag, mod, num_frames);
488
489
150k
  nb_bits_lpc = pstr_it_bit_buff->cnt_bits - nb_bits_lpc;
490
150k
  *total_nbbits += nb_bits_lpc;
491
150k
  pstr_td->num_bits_per_supfrm += nb_bits_lpc;
492
493
150k
  if ((core_mode_last == 0) && (fac_data_present == 1)) {
494
3.34k
    WORD32 short_fac_flag = (mod[-1] == -2) ? 1 : 0;
495
3.34k
    iusace_write_bits_buf(pstr_it_bit_buff, short_fac_flag, 1);
496
3.34k
    *total_nbbits += 1;
497
3.34k
  }
498
499
150k
  return;
500
150k
}
501
502
109k
WORD32 iusace_fd_encode_fac(WORD32 *prm, WORD16 *ptr_bit_buf, WORD32 fac_length) {
503
109k
  WORD32 i, j, n, nb, qn, kv[8], nk, fac_bits;
504
109k
  WORD32 I;
505
506
109k
  fac_bits = 0;
507
508
1.75M
  for (i = 0; i < fac_length; i += 8) {
509
1.64M
    iusace_apply_voronoi_ext(&prm[i], &qn, &I, kv);
510
511
1.64M
    nb = iusace_unary_code(qn, ptr_bit_buf);
512
1.64M
    ptr_bit_buf += nb;
513
514
1.64M
    fac_bits += nb;
515
516
1.64M
    nk = 0;
517
1.64M
    n = qn;
518
1.64M
    if (qn > 4) {
519
195k
      nk = (qn - 3) >> 1;
520
195k
      n = qn - nk * 2;
521
195k
    }
522
523
1.64M
    if (qn != 0) {
524
884k
      iusace_write_bits2buf(I, 4 * n, ptr_bit_buf);
525
884k
      ptr_bit_buf += 4 * n;
526
884k
    }
527
1.64M
    if (qn > 4) {
528
1.75M
      for (j = 0; j < 8; j++) {
529
1.56M
        iusace_write_bits2buf(kv[j], nk, ptr_bit_buf);
530
1.56M
        ptr_bit_buf += nk;
531
1.56M
      }
532
195k
    }
533
1.64M
    fac_bits += 4 * qn;
534
1.64M
  }
535
536
109k
  return fac_bits;
537
109k
}