aboutsummaryrefslogtreecommitdiff
path: root/encoder/ixheaace_fd_enc.c
blob: 34556e90d262533cfcbb17ef25274a379b18bdf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/******************************************************************************
 *                                                                            *
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
 */

#include <string.h>
#include "iusace_type_def.h"
#include "iusace_bitbuffer.h"
#include "iusace_cnst.h"
#include "impd_drc_common_enc.h"
#include "impd_drc_uni_drc.h"
#include "impd_drc_api.h"
#include "impd_drc_uni_drc_eq.h"
#include "impd_drc_uni_drc_filter_bank.h"
#include "impd_drc_gain_enc.h"
#include "impd_drc_struct_def.h"

#include "ixheaace_memory_standards.h"
#include "iusace_tns_usac.h"
#include "iusace_psy_mod.h"
#include "iusace_config.h"
#include "ixheaace_adjust_threshold_data.h"
#include "iusace_fd_qc_util.h"
#include "iusace_config.h"
#include "iusace_arith_enc.h"
#include "iusace_fd_quant.h"
#include "iusace_ms.h"
#include "iusace_signal_classifier.h"
#include "iusace_block_switch_const.h"
#include "iusace_block_switch_struct_def.h"
#include "ixheaace_sbr_header.h"
#include "ixheaace_config.h"
#include "ixheaace_asc_write.h"
#include "iusace_main.h"
#include "iusace_write_bitstream.h"
#include "iusace_lpd.h"
#include "ixheaace_cplx_pred.h"
#include "iusace_func_prototypes.h"

IA_ERRORCODE iusace_fd_encode(ia_sfb_params_struct *pstr_sfb_prms, WORD32 usac_independancy_flag,
                              ia_usac_data_struct *pstr_usac_data,
                              ia_usac_encoder_config_struct *pstr_usac_config,
                              ia_bit_buf_struct *pstr_it_bit_buff, WORD32 nr_core_coder_ch,
                              WORD32 chn, WORD32 ele_id, WORD32 *bit_written) {
  iusace_scratch_mem *pstr_scratch = &pstr_usac_data->str_scratch;
  IA_ERRORCODE err_code = 0;
  WORD32 i_ch, idx = 0;
  WORD32 *ptr_num_fac_bits = pstr_scratch->ptr_num_fac_bits;
  WORD32 tns_data_present[2] = {0};
  WORD32 *ptr_core_mode_next = pstr_usac_data->core_mode_next;
  WORD32 *ptr_core_mode_prev = pstr_usac_data->core_mode_prev;
  *bit_written = 0;
  memset(pstr_scratch->ptr_num_fac_bits, 0,
         MAX_TIME_CHANNELS * sizeof(pstr_scratch->ptr_num_fac_bits[0]));
  for (i_ch = chn; i_ch < chn + nr_core_coder_ch; i_ch++) {
    tns_data_present[idx] = pstr_usac_data->pstr_tns_info[i_ch] != NULL;

    if (tns_data_present[idx]) {
      tns_data_present[idx] = pstr_usac_data->pstr_tns_info[i_ch]->tns_data_present;
    }
    idx++;
  }

  idx = 0;
  for (i_ch = chn; i_ch < chn + nr_core_coder_ch; i_ch++) {
    memset(pstr_scratch->p_reconstructed_time_signal[idx], 0, 4096 * sizeof(FLOAT64));
    err_code = iusace_fd_fac(
        pstr_sfb_prms->grouped_sfb_offset[i_ch], pstr_sfb_prms->max_sfb[i_ch],
        pstr_usac_data->ptr_2frame_time_data[i_ch], pstr_sfb_prms->window_sequence[i_ch],
        pstr_scratch->p_reconstructed_time_signal[idx], pstr_usac_data->td_encoder[i_ch],
        ((pstr_usac_data->td_encoder[i_ch]->prev_mode == 0) && ptr_core_mode_prev[i_ch]) ==
            CORE_MODE_TD,
        ptr_core_mode_next[i_ch] == CORE_MODE_TD, pstr_usac_data->fac_out_stream[i_ch],
        &ptr_num_fac_bits[i_ch], pstr_scratch);
    if (err_code) {
      return err_code;
    }
    idx++;
  }

  err_code = iusace_quantize_spec(pstr_sfb_prms, usac_independancy_flag, nr_core_coder_ch,
                                  pstr_usac_data, pstr_usac_config, chn, ele_id);
  if (err_code) return err_code;

  for (i_ch = chn; i_ch < chn + nr_core_coder_ch; i_ch++) {
    pstr_sfb_prms->window_shape[i_ch] =
        pstr_usac_data->str_psy_mod.str_psy_out_data[i_ch].window_shape;
  }

  if (nr_core_coder_ch == 1) {
    iusace_write_bits_buf(pstr_it_bit_buff, tns_data_present[0], 1);
    *bit_written = *bit_written + 1;
  }
  if (nr_core_coder_ch == 2) {
    *bit_written = *bit_written + iusace_write_cpe(pstr_sfb_prms, pstr_it_bit_buff,
                                                   tns_data_present, usac_independancy_flag,
                                                   pstr_usac_config, pstr_usac_data, chn);
  }

  idx = 0;
  for (i_ch = chn; i_ch < chn + nr_core_coder_ch; i_ch++) {
    *bit_written =
        *bit_written + iusace_write_fd_data(pstr_it_bit_buff, pstr_sfb_prms,
                                            ptr_num_fac_bits[i_ch], usac_independancy_flag,
                                            pstr_usac_data, pstr_usac_config, i_ch, ele_id, idx);
    idx++;
  }

  return err_code;
}