Coverage Report

Created: 2026-02-26 06:35

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