Coverage Report

Created: 2026-08-31 07:12

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.52M
static WORD32 iusace_unary_code(WORD32 idx, WORD16 *ptr_bit_buf) {
57
1.52M
  WORD32 num_bits;
58
59
1.52M
  num_bits = 1;
60
61
1.52M
  idx -= 1;
62
3.87M
  while (idx-- > 0) {
63
2.35M
    *ptr_bit_buf++ = 1;
64
2.35M
    num_bits++;
65
2.35M
  }
66
67
1.52M
  *ptr_bit_buf = 0;
68
69
1.52M
  return (num_bits);
70
1.52M
}
71
72
static VOID iusace_get_nk_mode(WORD32 mode_lpc, ia_bit_buf_struct *pstr_it_bit_buff,
73
421k
                               WORD32 *nk_mode, WORD32 lpc_set) {
74
421k
  switch (lpc_set) {
75
136k
    case 4:
76
136k
      break;
77
4.14k
    case 0:
78
108k
    case 2:
79
108k
      *nk_mode = 3;
80
108k
      iusace_write_bits_buf(pstr_it_bit_buff, mode_lpc, 1);
81
108k
      break;
82
88.9k
    case 1:
83
88.9k
      *nk_mode = mode_lpc;
84
88.9k
      if (mode_lpc == 2) {
85
42.6k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
86
46.3k
      } else if (mode_lpc == 1) {
87
28.2k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
88
28.2k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
89
28.2k
      } else if (mode_lpc == 0) {
90
18.0k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
91
18.0k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
92
18.0k
      }
93
88.9k
      break;
94
88.2k
    case 3:
95
88.2k
      if (mode_lpc == 0) {
96
1.28k
        *nk_mode = 0;
97
1.28k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
98
1.28k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
99
86.9k
      } else if (mode_lpc == 1) {
100
51.9k
        *nk_mode = 1;
101
51.9k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
102
51.9k
      } else if (mode_lpc == 2) {
103
3.24k
        *nk_mode = 2;
104
3.24k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
105
3.24k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
106
3.24k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
107
31.7k
      } else {
108
31.7k
        *nk_mode = 2;
109
31.7k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
110
31.7k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
111
31.7k
        iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
112
31.7k
      }
113
88.2k
      break;
114
421k
  }
115
421k
  return;
116
421k
}
117
118
static VOID iusace_write_qn_data(WORD32 *qn, ia_bit_buf_struct *pstr_it_bit_buff, WORD32 nk_mode,
119
393k
                                 WORD32 num_frames) {
120
393k
  WORD32 k, i;
121
393k
  switch (nk_mode) {
122
51.9k
    case 1:
123
155k
      for (k = 0; k < 2; k++) {
124
170k
        for (i = 0; i < qn[k] - 1; i++) {
125
67.0k
          iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
126
67.0k
        }
127
103k
        iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
128
103k
      }
129
51.9k
      break;
130
155k
    case 0:
131
233k
    case 2:
132
341k
    case 3:
133
1.02M
      for (k = 0; k < 2; k++) {
134
683k
        WORD32 qn1 = qn[k] - 2;
135
683k
        if (qn1 < 0 || qn1 > 3) {
136
332k
          qn1 = 3;
137
332k
        }
138
683k
        iusace_write_bits_buf(pstr_it_bit_buff, qn1, 2);
139
683k
      }
140
341k
      if ((nk_mode == 2) && num_frames != 2) {
141
232k
        for (k = 0; k < 2; k++) {
142
155k
          if (qn[k] > 4) {
143
85.4k
            for (i = 0; i < qn[k] - 4; i++) {
144
55.7k
              iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
145
55.7k
            }
146
29.6k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
147
29.6k
          }
148
155k
          if (qn[k] == 0) {
149
25.8k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
150
25.8k
          }
151
155k
        }
152
264k
      } else {
153
792k
        for (k = 0; k < 2; k++) {
154
528k
          if (qn[k] == 5) {
155
41.2k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
156
486k
          } else if (qn[k] == 6) {
157
119k
            iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
158
119k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
159
367k
          } else if (qn[k] == 0) {
160
126k
            iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
161
126k
            iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
162
126k
            iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
163
240k
          } else {
164
240k
            WORD32 qn_ext = qn[k] - 4;
165
240k
            if (qn_ext > 0) {
166
196k
              for (i = 0; i < qn_ext; i++) {
167
153k
                iusace_write_bits_buf(pstr_it_bit_buff, 1, 1);
168
153k
              }
169
43.1k
              iusace_write_bits_buf(pstr_it_bit_buff, 0, 1);
170
43.1k
            }
171
240k
          }
172
528k
        }
173
264k
      }
174
341k
      break;
175
393k
  }
176
393k
  return;
177
393k
}
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
393k
                                    WORD32 num_frames) {
182
393k
  WORD32 k;
183
393k
  WORD32 j = *idx;
184
185
393k
  iusace_write_qn_data(qn, pstr_it_bit_buff, nk_mode, num_frames);
186
187
1.18M
  for (k = 0; k < 2; k++) {
188
787k
    if (qn[k] > 0) {
189
571k
      WORD32 n, nk, i;
190
571k
      if (qn[k] > 4) {
191
235k
        nk = (qn[k] - 3) >> 1;
192
235k
        n = qn[k] - nk * 2;
193
335k
      } else {
194
335k
        nk = 0;
195
335k
        n = qn[k];
196
335k
      }
197
198
571k
      iusace_write_bits_buf(pstr_it_bit_buff, ptr_params[j++], (UWORD8)(4 * n));
199
200
5.14M
      for (i = 0; i < 8; i++) {
201
4.57M
        iusace_write_bits_buf(pstr_it_bit_buff, ptr_params[j++], (UWORD8)nk);
202
4.57M
      }
203
571k
    }
204
787k
  }
205
206
393k
  *idx = j;
207
208
393k
  return;
209
393k
}
210
211
static VOID iusace_write_lpc_data(ia_bit_buf_struct *pstr_it_bit_buff, WORD32 *param_lpc,
212
136k
                                  WORD32 first_lpd_flag, WORD32 *mod, WORD32 num_frames) {
213
136k
  WORD32 nk_mode = 0;
214
136k
  WORD32 j = 0, k;
215
136k
  WORD32 mode_lpc = 0;
216
136k
  WORD32 qn[2] = {0};
217
218
136k
  iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 4);
219
220
136k
  iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
221
222
409k
  for (k = 0; k < 2; k++) {
223
272k
    qn[k] = param_lpc[j++];
224
272k
  }
225
226
136k
  iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
227
228
136k
  if (first_lpd_flag) {
229
4.14k
    mode_lpc = param_lpc[j++];
230
4.14k
    iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 0);
231
232
4.14k
    if (mode_lpc == 0) {
233
249
      iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
234
249
    }
235
236
12.4k
    for (k = 0; k < 2; k++) {
237
8.28k
      qn[k] = param_lpc[j++];
238
8.28k
    }
239
240
4.14k
    iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
241
4.14k
  }
242
243
136k
  mode_lpc = param_lpc[j++];
244
245
136k
  if (num_frames == 4 && mod[0] < 3) {
246
104k
    iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 2);
247
248
104k
    if (mode_lpc == 0) {
249
33.3k
      iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
250
33.3k
    }
251
252
312k
    for (k = 0; k < 2; k++) {
253
208k
      qn[k] = param_lpc[j++];
254
208k
    }
255
256
104k
    iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
257
104k
  }
258
136k
  mode_lpc = param_lpc[j++];
259
136k
  if (mod[0] < 2) {
260
88.9k
    iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 1);
261
262
88.9k
    if (mode_lpc != 1) {
263
60.6k
      if (mode_lpc == 0) {
264
18.0k
        iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
265
18.0k
      }
266
267
181k
      for (k = 0; k < 2; k++) {
268
121k
        qn[k] = param_lpc[j++];
269
121k
      }
270
271
60.6k
      iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
272
60.6k
    }
273
88.9k
  } else if (mode_lpc != 1) {
274
36.9k
    if (mode_lpc == 0) {
275
20.6k
      j++;
276
20.6k
    }
277
110k
    for (k = 0; k < 2; k++) {
278
73.9k
      qn[k] = param_lpc[j++];
279
73.9k
    }
280
36.9k
    j += ((qn[0] > 0) ? 9 : 0) + ((qn[1] > 0) ? 9 : 0);
281
36.9k
  }
282
283
136k
  mode_lpc = param_lpc[j++];
284
136k
  if (num_frames != 2 && mod[2] < 2) {
285
88.2k
    iusace_get_nk_mode(mode_lpc, pstr_it_bit_buff, &nk_mode, 3);
286
88.2k
    if (mode_lpc == 0) {
287
1.28k
      iusace_write_bits_buf(pstr_it_bit_buff, param_lpc[j++], 8);
288
1.28k
    }
289
290
264k
    for (k = 0; k < 2; k++) {
291
176k
      qn[k] = param_lpc[j++];
292
176k
    }
293
88.2k
    iusace_write_cb_indices(qn, param_lpc, &j, pstr_it_bit_buff, nk_mode, num_frames);
294
88.2k
  }
295
136k
  return;
296
136k
}
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
136k
                              ia_bit_buf_struct *pstr_it_bit_buff, WORD32 ch_idx) {
301
136k
  WORD32 *total_nbbits = &usac_data->total_nbbits[ch_idx];
302
136k
  ia_usac_td_encoder_struct *pstr_td = usac_data->td_encoder[ch_idx];
303
136k
  WORD32 codec_mode = pstr_td->acelp_core_mode;
304
136k
  WORD16 *bit_buf = usac_data->td_serial_out[ch_idx];
305
136k
  WORD32 is_bass_post_filter = 1;
306
136k
  WORD32 first_lpd_flag = (usac_data->core_mode_prev[ch_idx] == CORE_MODE_FD);
307
136k
  WORD32 *param_lpc = usac_data->param_buf + (NUM_FRAMES * MAX_NUM_TCX_PRM_PER_DIV);
308
136k
  WORD32 *param = usac_data->param_buf;
309
136k
  WORD32 j, k, n, sfr, lpd_mode, num_bits, sq_bits, *prm;
310
136k
  WORD16 first_tcx_flag = 1;
311
136k
  WORD32 nbits_fac, nb_bits_lpc;
312
136k
  WORD32 core_mode_last = (first_lpd_flag) ? 0 : 1;
313
136k
  WORD32 fac_data_present;
314
136k
  WORD32 num_frames = NUM_FRAMES;
315
136k
  WORD16 *ptr_bit_buf = bit_buf;
316
317
136k
  pstr_td->num_bits_per_supfrm = 0;
318
136k
  *total_nbbits = 0;
319
320
136k
  iusace_write_bits_buf(pstr_it_bit_buff, pstr_td->acelp_core_mode, 3);
321
322
136k
  if (mod[0] == 3) {
323
32.2k
    lpd_mode = 25;
324
104k
  } else if ((mod[0] == 2) && (mod[2] == 2)) {
325
9.27k
    lpd_mode = 24;
326
94.8k
  } else {
327
94.8k
    if (mod[0] == 2) {
328
5.95k
      lpd_mode = 16 + mod[2] + 2 * mod[3];
329
88.9k
    } else if (mod[2] == 2) {
330
6.67k
      lpd_mode = 20 + mod[0] + 2 * mod[1];
331
82.2k
    } else {
332
82.2k
      lpd_mode = mod[0] + 2 * mod[1] + 4 * mod[2] + 8 * mod[3];
333
82.2k
    }
334
94.8k
  }
335
136k
  iusace_write_bits_buf(pstr_it_bit_buff, lpd_mode, 5);
336
136k
  pstr_td->num_bits_per_supfrm = 5;
337
136k
  *total_nbbits += 5;
338
339
136k
  iusace_write_bits_buf(pstr_it_bit_buff, is_bass_post_filter, 1);
340
136k
  *total_nbbits += 1;
341
342
136k
  iusace_write_bits_buf(pstr_it_bit_buff, core_mode_last, 1);
343
136k
  *total_nbbits += 1;
344
345
136k
  if (((mod[0] == 0) && (mod[-1] != 0)) || ((mod[0] > 0) && (mod[-1] == 0))) {
346
22.3k
    fac_data_present = 1;
347
114k
  } else {
348
114k
    fac_data_present = 0;
349
114k
  }
350
351
136k
  iusace_write_bits_buf(pstr_it_bit_buff, fac_data_present, 1);
352
136k
  *total_nbbits += 1;
353
354
136k
  num_bits = (iusace_acelp_core_numbits_1024[codec_mode] / 4) - 2;
355
356
136k
  k = 0;
357
554k
  while (k < num_frames) {
358
417k
    lpd_mode = mod[k];
359
417k
    prm = param + (k * MAX_NUM_TCX_PRM_PER_DIV);
360
417k
    j = 0;
361
362
417k
    if (((mod[k - 1] == 0) && (mod[k] > 0)) || ((mod[k - 1] > 0) && (mod[k] == 0))) {
363
95.5k
      nbits_fac = iusace_fd_encode_fac(&prm[j], ptr_bit_buf, (pstr_td->len_subfrm) / 2);
364
95.5k
      j += (pstr_td->len_subfrm) / 2;
365
95.5k
      *total_nbbits += nbits_fac;
366
16.1M
      for (WORD32 i = 0; i < nbits_fac; i++) {
367
16.0M
        iusace_write_bits_buf(pstr_it_bit_buff, ptr_bit_buf[i], 1);
368
16.0M
      }
369
95.5k
    }
370
371
417k
    if (lpd_mode == 0) {
372
226k
      iusace_write_bits_buf(pstr_it_bit_buff, prm[j++], 2);
373
374
1.06M
      for (sfr = 0; sfr < (pstr_td->num_subfrm); sfr++) {
375
841k
        n = 6;
376
841k
        if ((sfr == 0) || (((pstr_td->len_subfrm) == 256) && (sfr == 2))) n = 9;
377
841k
        iusace_write_bits_buf(pstr_it_bit_buff, prm[j], (UWORD8)n);
378
841k
        j++;
379
841k
        iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 1);
380
841k
        j++;
381
841k
        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
841k
        } 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
841k
        } else if (codec_mode == ACELP_CORE_MODE_12k8) {
400
46.8k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
401
46.8k
          j++;
402
46.8k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
403
46.8k
          j++;
404
46.8k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
405
46.8k
          j++;
406
46.8k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 9);
407
46.8k
          j++;
408
794k
        } 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
794k
        } else if (codec_mode == ACELP_CORE_MODE_16k) {
418
6.29k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
419
6.29k
          j++;
420
6.29k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
421
6.29k
          j++;
422
6.29k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
423
6.29k
          j++;
424
6.29k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 13);
425
6.29k
          j++;
426
788k
        } else if (codec_mode == ACELP_CORE_MODE_18k4) {
427
788k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 2);
428
788k
          j++;
429
788k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 2);
430
788k
          j++;
431
788k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 2);
432
788k
          j++;
433
788k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 2);
434
788k
          j++;
435
788k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 14);
436
788k
          j++;
437
788k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 14);
438
788k
          j++;
439
788k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 14);
440
788k
          j++;
441
788k
          iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 14);
442
788k
          j++;
443
788k
        }
444
841k
        iusace_write_bits_buf(pstr_it_bit_buff, prm[j], 7);
445
841k
        j++;
446
841k
      }
447
226k
      *total_nbbits += (num_bits - NBITS_LPC);
448
226k
      pstr_td->num_bits_per_supfrm += (num_bits - NBITS_LPC);
449
226k
      k++;
450
226k
    } else {
451
191k
      iusace_write_bits_buf(pstr_it_bit_buff, prm[j++], 3);
452
191k
      *total_nbbits += 3;
453
191k
      pstr_td->num_bits_per_supfrm += 3;
454
191k
      iusace_write_bits_buf(pstr_it_bit_buff, prm[j++], 7);
455
191k
      *total_nbbits += 7;
456
191k
      pstr_td->num_bits_per_supfrm += 7;
457
458
191k
      if (first_tcx_flag) {
459
107k
        first_tcx_flag = 0;
460
107k
        if (usac_independency_flag) {
461
4.28k
          pstr_td->arith_reset_flag = 1;
462
4.28k
          memset(pstr_td->c_pres, 0, 516 * sizeof(WORD32));
463
4.28k
          memset(pstr_td->c_prev, 0, 516 * sizeof(WORD32));
464
103k
        } else {
465
103k
          if (pstr_td->arith_reset_flag) {
466
103k
            memset(pstr_td->c_pres, 0, 516 * sizeof(WORD32));
467
103k
            memset(pstr_td->c_prev, 0, 516 * sizeof(WORD32));
468
103k
          }
469
103k
          iusace_write_bits_buf(pstr_it_bit_buff, pstr_td->arith_reset_flag, 1);
470
103k
          *total_nbbits += 1;
471
103k
          pstr_td->num_bits_per_supfrm += 1;
472
103k
        }
473
107k
      }
474
475
191k
      sq_bits = iusace_tcx_coding(pstr_it_bit_buff, n_param_tcx[k], pstr_td->len_frame, prm + j,
476
191k
                                  pstr_td->c_pres, pstr_td->c_prev);
477
478
191k
      *total_nbbits += sq_bits;
479
191k
      pstr_td->num_bits_per_supfrm += sq_bits;
480
481
191k
      k += (1 << (lpd_mode - 1));
482
191k
    }
483
417k
  }
484
485
136k
  nb_bits_lpc = pstr_it_bit_buff->cnt_bits;
486
487
136k
  iusace_write_lpc_data(pstr_it_bit_buff, param_lpc, first_lpd_flag, mod, num_frames);
488
489
136k
  nb_bits_lpc = pstr_it_bit_buff->cnt_bits - nb_bits_lpc;
490
136k
  *total_nbbits += nb_bits_lpc;
491
136k
  pstr_td->num_bits_per_supfrm += nb_bits_lpc;
492
493
136k
  if ((core_mode_last == 0) && (fac_data_present == 1)) {
494
3.37k
    WORD32 short_fac_flag = (mod[-1] == -2) ? 1 : 0;
495
3.37k
    iusace_write_bits_buf(pstr_it_bit_buff, short_fac_flag, 1);
496
3.37k
    *total_nbbits += 1;
497
3.37k
  }
498
499
136k
  return;
500
136k
}
501
502
101k
WORD32 iusace_fd_encode_fac(WORD32 *prm, WORD16 *ptr_bit_buf, WORD32 fac_length) {
503
101k
  WORD32 i, j, n, nb, qn, kv[8], nk, fac_bits;
504
101k
  WORD32 I;
505
506
101k
  fac_bits = 0;
507
508
1.62M
  for (i = 0; i < fac_length; i += 8) {
509
1.52M
    iusace_apply_voronoi_ext(&prm[i], &qn, &I, kv);
510
511
1.52M
    nb = iusace_unary_code(qn, ptr_bit_buf);
512
1.52M
    ptr_bit_buf += nb;
513
514
1.52M
    fac_bits += nb;
515
516
1.52M
    nk = 0;
517
1.52M
    n = qn;
518
1.52M
    if (qn > 4) {
519
195k
      nk = (qn - 3) >> 1;
520
195k
      n = qn - nk * 2;
521
195k
    }
522
523
1.52M
    if (qn != 0) {
524
814k
      iusace_write_bits2buf(I, 4 * n, ptr_bit_buf);
525
814k
      ptr_bit_buf += 4 * n;
526
814k
    }
527
1.52M
    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.52M
    fac_bits += 4 * qn;
534
1.52M
  }
535
536
101k
  return fac_bits;
537
101k
}