• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* File includes                                                             */
22 /*****************************************************************************/
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "ixheaac_type_def.h"
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_user_config.h"
32 #include "iusace_cnst.h"
33 #include "ixheaace_api.h"
34 #include "ixheaac_error_standards.h"
35 #include "ixheaace_error_handler.h"
36 #include "ixheaace_loudness_measurement.h"
37 
38 VOID ia_enhaacplus_enc_error_handler_init();
39 VOID ia_testbench_error_handler_init();
40 
41 extern ia_error_info_struct ia_testbench_error_info;
42 extern ia_error_info_struct ia_enhaacplus_enc_error_info;
43 
44 /*****************************************************************************/
45 /* Constant hash defines                                                     */
46 /*****************************************************************************/
47 #define IA_MAX_CMD_LINE_LENGTH 300
48 #define IA_MAX_ARGS 20
49 #define IA_SCREEN_WIDTH 80
50 #define APP_BITRES_SIZE_CONFIG_PARAM_DEF_VALUE_LC (768)
51 #define APP_BITRES_SIZE_CONFIG_PARAM_DEF_VALUE_LD (384)
52 
53 #define PARAMFILE "paramfilesimple.txt"
54 
55 /*****************************************************************************/
56 /* Error codes for the testbench                                             */
57 /*****************************************************************************/
58 #define IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED 0xFFFFA001
59 #define DRC_CONFIG_FILE "impd_drc_config_params.txt"
60 /*****************************************************************************/
61 /* Application Context structure                                                          */
62 /*****************************************************************************/
63 typedef struct {
64   FILE *pf_inp;
65   FILE *pf_out;
66   FILE *pf_meta;
67   WORD32 use_ga_hdr;
68 } ixheaace_app_context;
69 
ia_enhaacplus_enc_fread(void * buf,int size,int bytes,FILE * fp)70 int ia_enhaacplus_enc_fread(void *buf, int size, int bytes, FILE *fp) {
71   return (int)fread(buf, size, bytes, fp);
72 }
73 
ia_enhaacplus_enc_pcm_data_read(FILE * in_file,UWORD32 num_samples,WORD32 num_channels,WORD16 ** data)74 IA_ERRORCODE ia_enhaacplus_enc_pcm_data_read(FILE *in_file, UWORD32 num_samples,
75                                              WORD32 num_channels, WORD16 **data) {
76   UWORD32 count = 0;
77   WORD16 temp;
78   WORD16 *buf = &temp;
79   UWORD8 channel_no;
80   UWORD32 sample_no = 0;
81 
82   while (count < num_samples) {
83     sample_no = count / num_channels;
84     channel_no = (UWORD8)(count % num_channels);
85     if (fread(buf, sizeof(WORD16), 1, in_file) != 1) {
86       if (feof(in_file)) {
87         printf("End of file reached.\n");
88       } else {
89         printf("Error reading a file.\n");
90         return -1;
91       }
92     }
93     data[channel_no][sample_no] = temp;
94     count++;
95   }
96   return IA_NO_ERROR;
97 }
98 
ia_enhaacplus_enc_fwrite(void * pb_buf,FILE * pf_out,WORD32 i_out_bytes)99 int ia_enhaacplus_enc_fwrite(void *pb_buf, FILE *pf_out, WORD32 i_out_bytes) {
100   fwrite(pb_buf, sizeof(char), i_out_bytes, pf_out);
101   return 1;
102 }
103 
ia_enhaacplus_enc_wav_header_decode(FILE * in_file,UWORD32 * n_channels,UWORD32 * i_channel_mask,UWORD32 * sample_rate,UWORD32 * pcm_sz,WORD32 * length)104 IA_ERRORCODE ia_enhaacplus_enc_wav_header_decode(FILE *in_file, UWORD32 *n_channels,
105                                                  UWORD32 *i_channel_mask, UWORD32 *sample_rate,
106                                                  UWORD32 *pcm_sz, WORD32 *length) {
107   WORD8 wav_hdr[40 + 36];
108   WORD8 data_start[4];
109   WORD16 num_ch;
110   UWORD32 f_samp;
111   WORD16 output_format;
112   WORD32 check, count = 0;
113   FLAG wav_format_pcm = 0, wav_format_extensible = 0;
114   UWORD16 cb_size = 0;
115   WORD32 curr_pos, size;
116 
117   *i_channel_mask = 0;
118 
119   if (fread(wav_hdr, 1, 40, in_file) != 40) return 1;
120 
121   if (wav_hdr[0] != 'R' && wav_hdr[1] != 'I' && wav_hdr[2] != 'F' && wav_hdr[3] != 'F') {
122     return 1;
123   }
124 
125   if (wav_hdr[20] == 01 && wav_hdr[21] == 00) {
126     wav_format_pcm = 1;
127   } else if (wav_hdr[20] == ((WORD8)-2) && wav_hdr[21] == ((WORD8)-1)) {
128     wav_format_extensible = 1;
129   } else {
130     return 1;
131   }
132 
133   num_ch = (WORD16)((UWORD8)wav_hdr[23] * 256 + (UWORD8)wav_hdr[22]);
134   f_samp = ((UWORD8)wav_hdr[27] * 256 * 256 * 256);
135   f_samp += ((UWORD8)wav_hdr[26] * 256 * 256);
136   f_samp += ((UWORD8)wav_hdr[25] * 256);
137   f_samp += ((UWORD8)wav_hdr[24]);
138 
139   output_format = ((UWORD8)wav_hdr[35] * 256);
140   output_format += ((UWORD8)wav_hdr[34]);
141 
142   *n_channels = num_ch;
143   *sample_rate = f_samp;
144   *pcm_sz = output_format;
145 
146   if (wav_format_pcm) {
147     data_start[0] = wav_hdr[36];
148     data_start[1] = wav_hdr[37];
149     data_start[2] = wav_hdr[38];
150     data_start[3] = wav_hdr[39];
151   } else if (wav_format_extensible) {
152     cb_size |= ((UWORD8)wav_hdr[37] << 8);
153     cb_size |= ((UWORD8)wav_hdr[36]);
154 
155     if (fread(&(wav_hdr[40]), 1, (UWORD16)(cb_size - 2 + 4), in_file) !=
156         (UWORD16)(cb_size - 2 + 4))
157       return 1;
158 
159     if (cb_size > 34) {
160       return 1;
161     }
162 
163     *i_channel_mask = 0;
164     *i_channel_mask |= (UWORD8)wav_hdr[43] << 24;
165     *i_channel_mask |= (UWORD8)wav_hdr[42] << 16;
166     *i_channel_mask |= (UWORD8)wav_hdr[41] << 8;
167     *i_channel_mask |= (UWORD8)wav_hdr[40];
168 
169     data_start[0] = wav_hdr[40 + cb_size - 2 + 0];
170     data_start[1] = wav_hdr[40 + cb_size - 2 + 1];
171     data_start[2] = wav_hdr[40 + cb_size - 2 + 2];
172     data_start[3] = wav_hdr[40 + cb_size - 2 + 3];
173   }
174 
175   check = 1;
176   while (check) {
177     if (data_start[0] == 'd' && data_start[1] == 'a' && data_start[2] == 't' &&
178         data_start[3] == 'a') {
179       if (1 != fread(length, 4, 1, in_file)) return 1;
180       check = 0;
181 
182       curr_pos = ftell(in_file);
183       fseek(in_file, 0L, SEEK_END);
184       size = ftell(in_file) - curr_pos;
185       if (*length > size) {
186         printf("\n Inconsitent file size \n");
187         *length = size;
188       }
189       fseek(in_file, curr_pos, SEEK_SET);
190     } else {
191       data_start[0] = data_start[1];
192       data_start[1] = data_start[2];
193       data_start[2] = data_start[3];
194       if (1 != fread(&data_start[3], 1, 1, in_file)) return 1;
195     }
196     count++;
197     if (count > 40) {
198       *length = 0xffffffff;
199       return (1);
200     }
201   }
202   return IA_NO_ERROR;
203 }
204 
ia_enhaacplus_enc_print_usage()205 void ia_enhaacplus_enc_print_usage() {
206   printf("\nUsage:\n");
207   printf("\n<executable> -ifile:<inputfile> -ofile:<outputfile> [options]\n");
208   printf("\nor\n");
209   printf("\n<executable> -paramfile:<paramfile>\n");
210   printf("\n[options] can be,");
211   printf("\n[-br:<bitrate>]");
212   printf("\n[-mps:<use_mps>]");
213   printf("\n[-adts:<use_adts_flag>]");
214   printf("\n[-tns:<use_tns_flag>]");
215   printf("\n[-nf:<use_noise_filling>]");
216   printf("\n[-cmpx_pred:<use_complex_prediction>]");
217   printf("\n[-framesize:<framesize_to_be_used>]");
218   printf("\n[-aot:<audio_object_type>]");
219   printf("\n[-esbr:<esbr_flag>]");
220   printf("\n[-full_bandwidth:<enable_full_bandwidth>]");
221   printf("\n[-max_out_buffer_per_ch:<bitreservoir_size>]");
222   printf("\n[-tree_cfg:<tree_config>]");
223   printf("\n[-usac:<usac_encoding_mode>]");
224   printf("\n[-ccfl_idx:<corecoder_framelength_index>]");
225   printf("\n[-pvc_enc:<pvc_enc_flag>]");
226   printf("\n[-harmonic_sbr:<harmonic_sbr_flag>]");
227   printf("\n[-esbr_hq:<esbr_hq_flag>]");
228   printf("\n[-drc:<drc_flag>]");
229   printf("\n[-inter_tes_enc:<inter_tes_enc_flag>]");
230   printf("\n[-rap:<random access interval in ms>]");
231   printf("\n[-stream_id:<stream identifier>]");
232   printf("\n[-delay_adjust:<delay adjustment>]");
233   printf("\n\nwhere, \n  <paramfile> is the parameter file with multiple commands");
234   printf("\n  <inputfile> is the input 16-bit WAV or PCM file name");
235   printf("\n  <outputfile> is the output ADTS/ES file name");
236   printf("\n  <bitrate> is the bit-rate in bits per second. Default value is 48000. ");
237   printf("\n  <use_mps> Valid values are 0 (disable MPS) and 1 (enable MPS). Default is 0.");
238   printf(
239       "\n  <use_adts_flag> Valid values are 0 ( No ADTS header) and 1 ( generate ADTS header). "
240       "Default is 0.");
241   printf("\n  <use_tns_flag> Valid values are 0 (disable TNS) and 1 (enable TNS). Default is 1.");
242   printf("\n  <use_noise_filling> controls usage of noise filling in encoding. Default 0.");
243   printf(
244       "\n  <use_complex_prediction> controls usage of complex prediction in encoding. Default "
245       "0.");
246   printf("\n  <framesize_to_be_used> is the framesize to be used.");
247   printf(
248       "\n        For AOT 23, 39 (LD core coder profiles) valid values are 480 and 512. Default "
249       "is "
250       "512.");
251   printf(
252       "\n        For AOT 2, 5, 29 (LC core coder profiles) valid values are 960 and 1024. "
253       "Default "
254       "is 1024.");
255   printf(
256       "\n        For AOT 42 (USAC profile) valid values are 768 and 1024. "
257       "Default "
258       "is 1024.");
259   printf("\n  <audio_object_type> is the Audio object type");
260   printf("\n        2 for AAC-LC");
261   printf("\n        5 for HE-AACv1(Legacy SBR)");
262   printf("\n        23 for AAC-LD");
263   printf("\n        29 for HE-AACv2");
264   printf("\n        39 for AAC-ELD");
265   printf("\n        42 for USAC");
266   printf("\n        Default is 2 for AAC-LC.");
267   printf("\n  <esbr_flag> Valid values are 0 (disable eSBR) and 1 (enable eSBR).");
268   printf("\n      Default is 0 for HE-AACv1 profile (legacy SBR) and 1 for USAC profile.");
269   printf(
270       "\n  <enable_full_bandwidth> Enable use of full bandwidth of input. Valid values are "
271       "0(disable full bandwidth) and 1(enable full bandwidth). Default is 0.");
272   printf("\n  <bitreservoir_size> is the maximum size of bit reservoir to be used.");
273   printf(
274       "\n        Valid values are from -1 to 6144. -1 will omit use of bit reservoir. Default is "
275       "384.");
276   printf(
277       "\n  <tree_config> MPS tree config"
278       "\n        0 for '212'"
279       "\n        1 for '5151'"
280       "\n        2 for '5152'"
281       "\n        3 for '525'"
282       "\n        Default '212' for stereo input and '5151' for 6ch input.");
283   printf(
284       "\n  <usac_encoding_mode> USAC encoding mode to be chose"
285       "0 for 'usac_switched'"
286       "1 for 'usac_fd'"
287       "2 for 'usac_td'"
288       "Default 'usac_fd'");
289   printf(
290       "\n  <corecoder_framelength_index> is the core coder framelength index for USAC encoder");
291   printf("\n    Valid values are 0, 1, 2, 3, 4. eSBR enabling is implicit");
292   printf("\n    0 - Core coder framelength of USAC is  768 and eSBR is disabled");
293   printf("\n    1 - Core coder framelength of USAC is 1024 and eSBR is disabled");
294   printf("\n    2 - Core coder framelength of USAC is  768 and eSBR ratio 8:3");
295   printf("\n    3 - Core coder framelength of USAC is 1024 and eSBR ratio 2:1");
296   printf("\n    4 - Core coder framelength of USAC is 1024 and eSBR ratio 4:1");
297   printf(
298       "\n  <pvc_enc_flag> Valid values are 0 (disable PVC encoding) and "
299       "1 (enable PVC encoding). Default is 0.");
300   printf(
301       "\n  <harmonic_sbr_flag> Valid values are 0 (disable harmonic SBR) and "
302       "1 (enable harmonic SBR). Default is 0.");
303   printf(
304       "\n  <esbr_hq_flag> Valid values are 0 (disable high quality eSBR) and "
305       "1 (enable high quality eSBR). Default is 0.");
306   printf(
307       "\n  <drc_flag> Valid values are 0 (disable DRC encoding) and "
308       "1 (enable DRC encoding). Default is 0.");
309   printf(
310       "\n  <inter_tes_enc_flag> Valid values are 0 (disable inter - TES encoding) and "
311       "1 (enable inter - TES encoding). Default is 0.");
312   printf(
313       "\n  <random access interval in ms> is the time interval between audio preroll frames in "
314       "ms. It is applicable only for AOT 42."
315       "\n        Valid values are -1 (Audio preroll sent only at beginning of file) and "
316       "greater than 1000 ms. Default is -1.");
317   printf(
318       "\n <stream identifier> is the stream id used to uniquely identify configuration of a "
319       "stream within a set of associated streams."
320       "\n        It is applicable only for AOT 42. Valid values are 0 to 65535. Default is 0.");
321   printf(
322     "\n <delay adjustment> is used to discard delay on the decoded file using pre-roll frames"
323     "on encoder."
324     "\n        It is applicable only for AOT 42. Valid values are 0 and 1. Default is 0.");
325   exit(1);
326 }
327 
ixheaace_parse_config_param(WORD32 argc,pWORD8 argv[],pVOID ptr_enc_api)328 static VOID ixheaace_parse_config_param(WORD32 argc, pWORD8 argv[], pVOID ptr_enc_api) {
329   LOOPIDX i;
330   ixheaace_user_config_struct *pstr_enc_api = (ixheaace_user_config_struct *)ptr_enc_api;
331 
332   for (i = 0; i < argc; i++) {
333     /* Stream bit rate */
334     if (!strncmp((const char *)argv[i], "-br:", 4)) {
335       char *pb_arg_val = (char *)argv[i] + 4;
336       pstr_enc_api->input_config.i_bitrate = atoi(pb_arg_val);
337       fprintf(stdout, "Stream bit rate = %d\n", pstr_enc_api->input_config.i_bitrate);
338     }
339     /* MPS */
340     if (!strncmp((const char *)argv[i], "-mps:", 5)) {
341       char *pb_arg_val = (char *)argv[i] + 5;
342       pstr_enc_api->input_config.i_use_mps = atoi(pb_arg_val);
343     }
344     /* Use TNS */
345     if (!strncmp((const char *)argv[i], "-tns:", 5)) {
346       char *pb_arg_val = (char *)argv[i] + 5;
347       pstr_enc_api->input_config.aac_config.use_tns = atoi(pb_arg_val);
348       pstr_enc_api->input_config.user_tns_flag = 1;
349     }
350     /*noise filling*/
351     if (!strncmp((pCHAR8)argv[i], "-nf:", 4)) {
352       pCHAR8 pb_arg_val = (pCHAR8)argv[i] + 4;
353       pstr_enc_api->input_config.aac_config.noise_filling = atoi(pb_arg_val);
354     }
355     /* Complex Prediction */
356     if (!strncmp((pCHAR8)argv[i], "-cmpx_pred:", 11)) {
357       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 11);
358       pstr_enc_api->input_config.cplx_pred = atoi(pb_arg_val);
359     }
360     /*Use full bandwidth*/
361     if (!strncmp((const char *)argv[i], "-full_bandwidth:", 16)) {
362       char *pb_arg_val = (char *)argv[i] + 16;
363       pstr_enc_api->input_config.aac_config.full_bandwidth = atoi(pb_arg_val);
364     }
365     /* frame size */
366     if (!strncmp((const char *)argv[i], "-framesize:", 11)) {
367       char *pb_arg_val = (char *)argv[i] + 11;
368       pstr_enc_api->input_config.frame_length = atoi(pb_arg_val);
369       pstr_enc_api->input_config.frame_cmd_flag = 1;
370     }
371     if (!strncmp((const char *)argv[i], "-aot:", 5)) {
372       char *pb_arg_val = (char *)argv[i] + 5;
373       pstr_enc_api->input_config.aot = atoi(pb_arg_val);
374     }
375     if (!strncmp((const char *)argv[i], "-esbr:", 6)) {
376       char *pb_arg_val = (char *)argv[i] + 6;
377       pstr_enc_api->input_config.esbr_flag = atoi(pb_arg_val);
378       pstr_enc_api->input_config.user_esbr_flag = 1;
379     }
380 
381     if (!strncmp((const char *)argv[i], "-max_out_buffer_per_ch:", 23)) {
382       char *pb_arg_val = (char *)argv[i] + 23;
383       pstr_enc_api->input_config.aac_config.bitreservoir_size = atoi(pb_arg_val);
384       pstr_enc_api->input_config.out_bytes_flag = 1;
385     }
386     if (!strncmp((const char *)argv[i], "-tree_cfg:", 10)) {
387       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 10);
388       pstr_enc_api->input_config.i_mps_tree_config = atoi(pb_arg_val);
389     }
390     if (!strncmp((const char *)argv[i], "-adts:", 6)) {
391       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 6);
392       pstr_enc_api->input_config.i_use_adts = atoi(pb_arg_val);
393     }
394     if (!strncmp((const char *)argv[i], "-usac:", 6)) {
395       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 6);
396       pstr_enc_api->input_config.codec_mode = atoi(pb_arg_val);
397       pstr_enc_api->input_config.usac_en = 1;
398       pstr_enc_api->input_config.aot = AOT_USAC;
399     }
400     if (!strncmp((const char *)argv[i], "-ccfl_idx:", 10)) {
401       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 10);
402       pstr_enc_api->input_config.ccfl_idx = atoi(pb_arg_val);
403     }
404     if (!strncmp((const char *)argv[i], "-pvc_enc:", 9)) {
405       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 9);
406       pstr_enc_api->input_config.pvc_active = atoi(pb_arg_val);
407     }
408     if (!strncmp((const char *)argv[i], "-harmonic_sbr:", 14)) {
409       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 14);
410       pstr_enc_api->input_config.harmonic_sbr = atoi(pb_arg_val);
411     }
412     if (!strncmp((const char *)argv[i], "-esbr_hq:", 9)) {
413       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 9);
414       pstr_enc_api->input_config.hq_esbr = atoi(pb_arg_val);
415     }
416     /* DRC */
417     if (!strncmp((pCHAR8)argv[i], "-drc:", 5)) {
418       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 5);
419       pstr_enc_api->input_config.use_drc_element = atoi(pb_arg_val);
420     }
421     if (!strncmp((const char *)argv[i], "-inter_tes_enc:", 15)) {
422       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 15);
423       pstr_enc_api->input_config.inter_tes_active = atoi(pb_arg_val);
424     }
425     if (!strncmp((const char *)argv[i], "-rap:", 5)) {
426       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 5);
427       pstr_enc_api->input_config.random_access_interval = atoi(pb_arg_val);
428     }
429     if (!strncmp((const char *)argv[i], "-stream_id:", 11)) {
430       pCHAR8 pb_arg_val = (pCHAR8)(argv[i] + 11);
431       pstr_enc_api->input_config.stream_id = atoi(pb_arg_val);
432     }
433     if (!strncmp((const char *)argv[i], "-delay_adjust:", 14)) {
434       pWORD8 pb_arg_val = argv[i] + 14;
435       pstr_enc_api->input_config.use_delay_adjustment = atoi((const char *)pb_arg_val);
436     }
437   }
438 
439   return;
440 }
441 
ia_enhaacplus_enc_display_id_message(WORD8 lib_name[],WORD8 lib_version[])442 VOID ia_enhaacplus_enc_display_id_message(WORD8 lib_name[], WORD8 lib_version[]) {
443   WORD8 str[4][IA_SCREEN_WIDTH] = {"ITTIAM SYSTEMS PVT LTD, BANGALORE\n",
444                                    "http:\\\\www.ittiam.com\n", "", ""};
445   WORD8 spaces[IA_SCREEN_WIDTH / 2 + 1];
446   WORD32 i, spclen;
447 
448   strcpy((pCHAR8)str[2], (pCHAR8)lib_name);
449   strcat((pCHAR8)str[2], (pCHAR8)lib_version);
450   strcat((pCHAR8)str[2], "\n");
451   strcat((pCHAR8)str[4 - 1], "\n");
452 
453   for (i = 0; i < IA_SCREEN_WIDTH / 2 + 1; i++) {
454     spaces[i] = ' ';
455   }
456 
457   for (i = 0; i < 4; i++) {
458     spclen = IA_SCREEN_WIDTH / 2 - (WORD32)(strlen((const char *)str[i]) / 2);
459     spaces[spclen] = '\0';
460     printf("%s", (const char *)spaces);
461     spaces[spclen] = ' ';
462     printf("%s", (const char *)str[i]);
463   }
464 }
465 
malloc_global(UWORD32 size,UWORD32 alignment)466 pVOID malloc_global(UWORD32 size, UWORD32 alignment) {
467 #ifdef WIN32
468   return _aligned_malloc(size, alignment);
469 #else
470   pVOID ptr = NULL;
471   if (posix_memalign((VOID **)&ptr, alignment, size)) {
472     ptr = NULL;
473   }
474   return ptr;
475 #endif
476 }
477 
free_global(pVOID ptr)478 VOID free_global(pVOID ptr) {
479 #ifdef WIN32
480   _aligned_free(ptr);
481 #else
482   free(ptr);
483 #endif
484   ptr = NULL;
485 }
486 
iaace_aac_set_default_config(ixheaace_aac_enc_config * config)487 static VOID iaace_aac_set_default_config(ixheaace_aac_enc_config *config) {
488   /* make the pre initialization of the structs flexible */
489   memset(config, 0, sizeof(*config));
490 
491   /* default configurations */
492   config->bitrate = 48000;
493   config->bandwidth = 0;
494   config->inv_quant = 2;
495   config->use_tns = 0;
496   config->noise_filling = 0;
497   config->bitreservoir_size = APP_BITRES_SIZE_CONFIG_PARAM_DEF_VALUE_LC;
498 }
499 
ixheaace_print_drc_config_params(ixheaace_input_config * pstr_input_config,ixheaace_input_config * pstr_input_config_user)500 static VOID ixheaace_print_drc_config_params(ixheaace_input_config *pstr_input_config,
501                                              ixheaace_input_config *pstr_input_config_user) {
502   WORD32 flag = 0, i, j, k;
503   ia_drc_input_config *drc_cfg = (ia_drc_input_config *)(pstr_input_config->pv_drc_cfg);
504   ia_drc_input_config *drc_cfg_user = (ia_drc_input_config *)(pstr_input_config_user->pv_drc_cfg);
505 
506   ia_drc_uni_drc_config_struct *pstr_uni_drc_config = &drc_cfg->str_uni_drc_config;
507   ia_drc_uni_drc_config_struct *pstr_uni_drc_config_user = &drc_cfg_user->str_uni_drc_config;
508 
509   ia_drc_loudness_info_set_struct *pstr_enc_loudness_info_set =
510       &drc_cfg->str_enc_loudness_info_set;
511   ia_drc_loudness_info_set_struct *pstr_enc_loudness_info_set_user =
512       &drc_cfg_user->str_enc_loudness_info_set;
513 
514   for (i = 0; i < pstr_uni_drc_config->drc_instructions_uni_drc_count; i++) {
515     if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i].additional_downmix_id_count !=
516         pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i].additional_downmix_id_count) {
517       flag = 1;
518     }
519     if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i].drc_location !=
520         pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i].drc_location) {
521       flag = 1;
522     }
523     if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i]
524             .drc_set_target_loudness_value_upper !=
525         pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i]
526             .drc_set_target_loudness_value_upper) {
527       flag = 1;
528     }
529     if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i]
530             .drc_set_target_loudness_value_lower !=
531         pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i]
532             .drc_set_target_loudness_value_lower) {
533       flag = 1;
534     }
535     if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i]
536             .drc_set_target_loudness_value_lower !=
537         pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i]
538             .drc_set_target_loudness_value_lower) {
539       flag = 1;
540     }
541     for (j = 0; j < MAX_CHANNEL_COUNT; j++) {
542       if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i].gain_set_index[j] !=
543           pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i].gain_set_index[j]) {
544         flag = 1;
545       }
546     }
547     if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i].num_drc_channel_groups !=
548         pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i].num_drc_channel_groups) {
549       flag = 1;
550     }
551     for (j = 0; j < pstr_uni_drc_config->str_drc_instructions_uni_drc[i].num_drc_channel_groups;
552          j++) {
553       if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i]
554               .str_gain_modifiers[j]
555               .attenuation_scaling[0] != pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i]
556                                              .str_gain_modifiers[j]
557                                              .attenuation_scaling[0]) {
558         flag = 1;
559       }
560       if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i]
561               .str_gain_modifiers[j]
562               .amplification_scaling[0] !=
563           pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i]
564               .str_gain_modifiers[j]
565               .amplification_scaling[0]) {
566         flag = 1;
567       }
568       if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i]
569               .str_gain_modifiers[j]
570               .gain_offset[0] != pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i]
571                                      .str_gain_modifiers[j]
572                                      .gain_offset[0]) {
573         flag = 1;
574       }
575     }
576     if (pstr_uni_drc_config->str_drc_instructions_uni_drc[i].limiter_peak_target !=
577         pstr_uni_drc_config_user->str_drc_instructions_uni_drc[i].limiter_peak_target) {
578       flag = 1;
579     }
580   }
581   if (flag == 1) {
582     printf("\nDRC : Invalid config str_drc_instructions_uni_drc");
583     flag = 0;
584   }
585   for (i = 0; i < pstr_uni_drc_config->drc_coefficients_uni_drc_count; i++) {
586     if (pstr_uni_drc_config->str_drc_coefficients_uni_drc[i].drc_location !=
587         pstr_uni_drc_config_user->str_drc_coefficients_uni_drc[i].drc_location) {
588       flag = 1;
589     }
590     if (pstr_uni_drc_config->str_drc_coefficients_uni_drc[i].gain_set_count !=
591         pstr_uni_drc_config_user->str_drc_coefficients_uni_drc[i].gain_set_count) {
592       flag = 1;
593     }
594     for (j = 0; j < pstr_uni_drc_config->str_drc_coefficients_uni_drc[i].gain_set_count; j++) {
595       if (pstr_uni_drc_config->str_drc_coefficients_uni_drc[i]
596               .str_gain_set_params[j]
597               .gain_coding_profile != pstr_uni_drc_config_user->str_drc_coefficients_uni_drc[i]
598                                           .str_gain_set_params[j]
599                                           .gain_coding_profile) {
600         flag = 1;
601       }
602       if (pstr_uni_drc_config->str_drc_coefficients_uni_drc[i]
603               .str_gain_set_params[j]
604               .band_count != pstr_uni_drc_config_user->str_drc_coefficients_uni_drc[i]
605                                  .str_gain_set_params[j]
606                                  .band_count) {
607         flag = 1;
608       }
609       for (k = 0;
610            k <
611            pstr_uni_drc_config->str_drc_coefficients_uni_drc[i].str_gain_set_params[j].band_count;
612            k++) {
613         if (pstr_uni_drc_config->str_drc_coefficients_uni_drc[i]
614                 .str_gain_set_params[j]
615                 .gain_params[k]
616                 .nb_points != pstr_uni_drc_config_user->str_drc_coefficients_uni_drc[i]
617                                   .str_gain_set_params[j]
618                                   .gain_params[k]
619                                   .nb_points) {
620           flag = 1;
621         }
622         if (pstr_uni_drc_config->str_drc_coefficients_uni_drc[i]
623                 .str_gain_set_params[j]
624                 .gain_params[k]
625                 .drc_characteristic != pstr_uni_drc_config_user->str_drc_coefficients_uni_drc[i]
626                                            .str_gain_set_params[j]
627                                            .gain_params[k]
628                                            .drc_characteristic) {
629           flag = 1;
630         }
631         if (pstr_uni_drc_config->str_drc_coefficients_uni_drc[i]
632                 .str_gain_set_params[j]
633                 .gain_params[k]
634                 .crossover_freq_index != pstr_uni_drc_config_user->str_drc_coefficients_uni_drc[i]
635                                              .str_gain_set_params[j]
636                                              .gain_params[k]
637                                              .crossover_freq_index) {
638           flag = 1;
639         }
640         if (pstr_uni_drc_config->str_drc_coefficients_uni_drc[i]
641                 .str_gain_set_params[j]
642                 .gain_params[k]
643                 .start_sub_band_index != pstr_uni_drc_config_user->str_drc_coefficients_uni_drc[i]
644                                              .str_gain_set_params[j]
645                                              .gain_params[k]
646                                              .start_sub_band_index) {
647           flag = 1;
648         }
649       }
650     }
651   }
652   if (flag == 1) {
653     printf("\nDRC : Invalid config: str_drc_coefficients_uni_drc");
654     flag = 0;
655   }
656   for (i = 0; i < pstr_enc_loudness_info_set->loudness_info_count; i++) {
657     if (pstr_enc_loudness_info_set->str_loudness_info[i].sample_peak_level !=
658         pstr_enc_loudness_info_set_user->str_loudness_info[i].sample_peak_level) {
659       flag = 1;
660     }
661     if (pstr_enc_loudness_info_set->str_loudness_info[i].true_peak_level !=
662         pstr_enc_loudness_info_set_user->str_loudness_info[i].true_peak_level) {
663       flag = 1;
664     }
665     if (pstr_enc_loudness_info_set->str_loudness_info[i].true_peak_level_measurement_system !=
666         pstr_enc_loudness_info_set_user->str_loudness_info[i]
667             .true_peak_level_measurement_system) {
668       flag = 1;
669     }
670     if (pstr_enc_loudness_info_set->str_loudness_info[i].true_peak_level_reliability !=
671         pstr_enc_loudness_info_set_user->str_loudness_info[i].true_peak_level_reliability) {
672       flag = 1;
673     }
674     if (pstr_enc_loudness_info_set->str_loudness_info[i].measurement_count !=
675         pstr_enc_loudness_info_set_user->str_loudness_info[i].measurement_count) {
676       flag = 1;
677     }
678     for (j = 0; j < pstr_enc_loudness_info_set->str_loudness_info[i].measurement_count; j++) {
679       if (pstr_enc_loudness_info_set->str_loudness_info[i]
680               .str_loudness_measure[j]
681               .method_definition != pstr_enc_loudness_info_set_user->str_loudness_info[i]
682                                         .str_loudness_measure[j]
683                                         .method_definition) {
684         flag = 1;
685       }
686       if (pstr_enc_loudness_info_set->str_loudness_info[i].str_loudness_measure[j].method_value !=
687           pstr_enc_loudness_info_set_user->str_loudness_info[i]
688               .str_loudness_measure[j]
689               .method_value) {
690         flag = 1;
691       }
692       if (pstr_enc_loudness_info_set->str_loudness_info[i]
693               .str_loudness_measure[j]
694               .measurement_system != pstr_enc_loudness_info_set_user->str_loudness_info[i]
695                                          .str_loudness_measure[j]
696                                          .measurement_system) {
697         flag = 1;
698       }
699       if (pstr_enc_loudness_info_set->str_loudness_info[i].str_loudness_measure[j].reliability !=
700           pstr_enc_loudness_info_set_user->str_loudness_info[i]
701               .str_loudness_measure[j]
702               .reliability) {
703         flag = 1;
704       }
705     }
706   }
707   if (flag == 1) {
708     printf("\nDRC : Invalid config str_loudness_info");
709     flag = 0;
710   }
711   for (i = 0; i < pstr_enc_loudness_info_set->loudness_info_album_count; i++) {
712     if (pstr_enc_loudness_info_set->str_loudness_info_album[i].sample_peak_level !=
713         pstr_enc_loudness_info_set_user->str_loudness_info_album[i].sample_peak_level) {
714       flag = 1;
715     }
716     if (pstr_enc_loudness_info_set->str_loudness_info_album[i].true_peak_level !=
717         pstr_enc_loudness_info_set_user->str_loudness_info_album[i].true_peak_level) {
718       flag = 1;
719     }
720     if (pstr_enc_loudness_info_set->str_loudness_info_album[i]
721             .true_peak_level_measurement_system !=
722         pstr_enc_loudness_info_set_user->str_loudness_info_album[i]
723             .true_peak_level_measurement_system) {
724       flag = 1;
725     }
726     if (pstr_enc_loudness_info_set->str_loudness_info_album[i].true_peak_level_reliability !=
727         pstr_enc_loudness_info_set_user->str_loudness_info_album[i].true_peak_level_reliability) {
728       flag = 1;
729     }
730     if (pstr_enc_loudness_info_set->str_loudness_info_album[i].measurement_count !=
731         pstr_enc_loudness_info_set_user->str_loudness_info_album[i].measurement_count) {
732       flag = 1;
733     }
734     for (j = 0; j < pstr_enc_loudness_info_set->str_loudness_info_album[i].measurement_count;
735          j++) {
736       if (pstr_enc_loudness_info_set->str_loudness_info_album[i]
737               .str_loudness_measure[j]
738               .method_definition != pstr_enc_loudness_info_set_user->str_loudness_info_album[i]
739                                         .str_loudness_measure[j]
740                                         .method_definition) {
741         flag = 1;
742       }
743       if (pstr_enc_loudness_info_set->str_loudness_info_album[i]
744               .str_loudness_measure[j]
745               .method_value != pstr_enc_loudness_info_set_user->str_loudness_info_album[i]
746                                    .str_loudness_measure[j]
747                                    .method_value) {
748         flag = 1;
749       }
750       if (pstr_enc_loudness_info_set->str_loudness_info_album[i]
751               .str_loudness_measure[j]
752               .measurement_system != pstr_enc_loudness_info_set_user->str_loudness_info_album[i]
753                                          .str_loudness_measure[j]
754                                          .measurement_system) {
755         flag = 1;
756       }
757       if (pstr_enc_loudness_info_set->str_loudness_info_album[i]
758               .str_loudness_measure[j]
759               .reliability != pstr_enc_loudness_info_set_user->str_loudness_info_album[i]
760                                   .str_loudness_measure[j]
761                                   .reliability) {
762         flag = 1;
763       }
764     }
765   }
766   if (flag == 1) {
767     printf("\nDRC : Invalid config str_loudness_info_album");
768   }
769 }
770 
ixheaace_print_config_params(ixheaace_input_config * pstr_input_config,ixheaace_input_config * pstr_input_config_user)771 static VOID ixheaace_print_config_params(ixheaace_input_config *pstr_input_config,
772                                          ixheaace_input_config *pstr_input_config_user) {
773   printf(
774       "\n*************************************************************************************"
775       "***********\n");
776   printf("\nParameters Taken:\n");
777   if (pstr_input_config_user->aot == pstr_input_config->aot) {
778     printf("\nAOT : %d", pstr_input_config->aot);
779   } else {
780     printf("\nAOT (Invalid config value, setting to default) : %d", pstr_input_config->aot);
781   }
782   if (pstr_input_config->aot == AOT_AAC_LC) {
783     printf(" - AAC LC ");
784   } else if (pstr_input_config->aot == AOT_SBR) {
785     if (pstr_input_config->esbr_flag == 1) {
786       printf(" - HEAACv1 (eSBR) ");
787     } else {
788       printf(" - HEAACv1 (Legacy SBR) ");
789     }
790   } else if (pstr_input_config->aot == AOT_AAC_ELD) {
791     printf(" - AAC ELD");
792     if (pstr_input_config->i_use_mps == 1) {
793       printf("v2");
794     }
795   } else if (pstr_input_config->aot == AOT_AAC_LD) {
796     printf(" - AAC LD ");
797   } else if (pstr_input_config->aot == AOT_USAC) {
798     printf(" - USAC ");
799     if (pstr_input_config->i_use_mps == 1) {
800       printf(" MPS ");
801     }
802   } else if (pstr_input_config->aot == AOT_PS) {
803     printf(" - HEAACv2 ");
804   }
805   if (pstr_input_config->aot == AOT_PS || pstr_input_config->aot == AOT_SBR) {
806     if (pstr_input_config_user->esbr_flag == pstr_input_config->esbr_flag) {
807       printf("\nESBR Flag : %d", pstr_input_config->esbr_flag);
808     } else {
809       printf("\nESBR Flag (Invalid config value, setting to default) : %d",
810              pstr_input_config->esbr_flag);
811     }
812   }
813   if (pstr_input_config->aot == AOT_USAC) {
814     if (pstr_input_config_user->codec_mode == pstr_input_config->codec_mode) {
815       printf("\nUSAC Codec Mode : ");
816     } else {
817       printf("\nUSAC Codec Mode (Invalid config value, setting to default) : ");
818     }
819 
820     if (pstr_input_config->usac_en) {
821       if (pstr_input_config->codec_mode == USAC_SWITCHED) {
822         printf("Switched Mode");
823       } else if (pstr_input_config->codec_mode == USAC_ONLY_FD) {
824         printf("FD Mode");
825       } else if (pstr_input_config->codec_mode == USAC_ONLY_TD) {
826         printf("TD Mode");
827       }
828     } else {
829       printf("Not Enabled");
830     }
831   }
832 
833   if (pstr_input_config->aot == AOT_USAC) {
834     if (pstr_input_config_user->harmonic_sbr == pstr_input_config->harmonic_sbr) {
835       printf("\nHarmonic SBR : %d", pstr_input_config->harmonic_sbr);
836     } else {
837       printf("\nHarmonic SBR (Invalid config value, setting to default) : %d",
838              pstr_input_config->harmonic_sbr);
839     }
840     if (pstr_input_config_user->hq_esbr == pstr_input_config->hq_esbr) {
841       printf("\nHigh quality esbr : %d", pstr_input_config->hq_esbr);
842     } else {
843       printf("\nHigh quality esbr Flag (Invalid config value, setting to default) : %d",
844              pstr_input_config->hq_esbr);
845     }
846     if (pstr_input_config_user->cplx_pred == pstr_input_config->cplx_pred) {
847       printf("\nComplex Prediction Flag : %d", pstr_input_config->cplx_pred);
848     } else {
849       printf("\nComplex Prediction Flag (Invalid config value, setting to default) : %d",
850              pstr_input_config->cplx_pred);
851     }
852     if (pstr_input_config_user->aac_config.use_tns == pstr_input_config->aac_config.use_tns) {
853       printf("\nTNS Flag : %d", pstr_input_config->aac_config.use_tns);
854     } else {
855       printf("\nTNS Flag (Invalid config value, setting to default) : %d",
856              pstr_input_config->aac_config.use_tns);
857     }
858 
859     if (pstr_input_config_user->ccfl_idx == pstr_input_config->ccfl_idx) {
860       printf("\nCore-coder framelength index : %d", pstr_input_config->ccfl_idx);
861     } else {
862       if (pstr_input_config_user->ccfl_idx >= NO_SBR_CCFL_768 &&
863           pstr_input_config_user->ccfl_idx <= SBR_4_1) {
864         if (pstr_input_config_user->ccfl_idx == NO_SBR_CCFL_768 &&
865             pstr_input_config->ccfl_idx == SBR_8_3) {
866           printf(
867               "\nCore-coder framelength index (Unsupported configuration, enabling 8:3 eSBR) : "
868               "%d",
869               pstr_input_config->ccfl_idx);
870         }
871         else if (pstr_input_config_user->ccfl_idx == NO_SBR_CCFL_1024 &&
872             pstr_input_config->ccfl_idx == SBR_2_1) {
873           printf(
874               "\nCore-coder framelength index (Unsupported configuration, enabling 2:1 eSBR) : "
875               "%d",
876               pstr_input_config->ccfl_idx);
877         }
878         else if (pstr_input_config_user->ccfl_idx != pstr_input_config->ccfl_idx)
879         {
880           printf(
881             "\nCore-coder framelength index changed from %d to %d ",
882             pstr_input_config_user->ccfl_idx, pstr_input_config->ccfl_idx);
883         }
884       } else {
885           printf(
886             "\nCore-coder framelength index (Invalid input config value, setting to default): %d"
887             , pstr_input_config->ccfl_idx);
888       }
889     }
890   }
891   if (pstr_input_config_user->aac_config.noise_filling ==
892       pstr_input_config->aac_config.noise_filling) {
893     printf("\nNoise Filling Flag : %d", pstr_input_config->aac_config.noise_filling);
894   } else {
895     printf("\nNoise Filling Flag (Invalid config value, setting to default) : %d",
896            pstr_input_config->aac_config.noise_filling);
897   }
898   if (pstr_input_config_user->i_use_adts == pstr_input_config->i_use_adts) {
899     printf("\nUse ADTS Flag : %d", pstr_input_config->i_use_adts);
900   } else {
901     printf("\nUse ADTS Flag (Invalid config value, setting to default) : %d",
902            pstr_input_config->i_use_adts);
903   }
904 
905   if (pstr_input_config->aot != AOT_USAC) {
906     if (pstr_input_config_user->aac_config.full_bandwidth ==
907         pstr_input_config->aac_config.full_bandwidth) {
908       printf("\nFull Bandwidth Flag : %d", pstr_input_config->aac_config.full_bandwidth);
909     } else {
910       printf("\nFull Bandwidth Flag (Invalid config value, setting to default) : %d",
911              pstr_input_config->aac_config.full_bandwidth);
912     }
913   }
914   if (pstr_input_config_user->aac_config.use_tns == pstr_input_config->aac_config.use_tns) {
915     printf("\nUse TNS Flag : %d", pstr_input_config->aac_config.use_tns);
916   } else {
917     printf("\nUse TNS Flag (Invalid config value, setting to default) : %d",
918            pstr_input_config->aac_config.use_tns);
919   }
920   if (pstr_input_config->aot == AOT_AAC_LD || pstr_input_config->aot == AOT_AAC_ELD) {
921     if (pstr_input_config_user->aac_config.bitreservoir_size ==
922         pstr_input_config->aac_config.bitreservoir_size) {
923       printf("\nBitreservoir Size : %d", pstr_input_config->aac_config.bitreservoir_size);
924     } else {
925       printf("\nBitreservoir Size (Invalid config value, setting to default) : %d",
926              pstr_input_config->aac_config.bitreservoir_size);
927     }
928   }
929 
930   printf("\nBitrate : %d bps", pstr_input_config->i_bitrate);
931 
932   if (pstr_input_config_user->i_use_mps != pstr_input_config->i_use_mps) {
933     printf("\nMPS (Invalid config value, setting to default) : %d ",
934            pstr_input_config->i_use_mps);
935   }
936   if (pstr_input_config->i_use_mps) {
937     if (pstr_input_config_user->i_mps_tree_config == pstr_input_config->i_mps_tree_config) {
938       printf("\nTree config : %d ", pstr_input_config->i_mps_tree_config);
939     } else {
940       printf("\nTree config (Invalid tree config value, setting to default) : %d ",
941              pstr_input_config->i_mps_tree_config);
942     }
943   } else if (!(pstr_input_config->i_use_mps) && (pstr_input_config_user->i_mps_tree_config !=
944                                                  pstr_input_config->i_mps_tree_config)) {
945     printf("\nTree config (Invalid tree config value ) : %d ",
946            pstr_input_config->i_mps_tree_config);
947   }
948   if (pstr_input_config->frame_cmd_flag) {
949     if (pstr_input_config_user->frame_length == pstr_input_config->frame_length) {
950       printf("\nFrame Length : %d", pstr_input_config->frame_length);
951     } else {
952       printf("\nFrame Length (Invalid config value, setting to default) : %d",
953              pstr_input_config->frame_length);
954     }
955   } else {
956     printf("\nFrame Length : %d", pstr_input_config->frame_length);
957   }
958   if ((pstr_input_config_user->i_samp_freq != pstr_input_config->i_samp_freq) &&
959       (pstr_input_config->i_use_adts == 1)) {
960     printf(
961         "\nSampling Frequency (With adts:1, Setting non-standard Sampling Frequency to mapped "
962         "standard Sampling Frequency) : %d Hz",
963         pstr_input_config->i_samp_freq);
964   } else {
965     printf("\nSampling Frequency : %d Hz", pstr_input_config_user->i_samp_freq);
966   }
967 
968   // DRC validation
969   if (pstr_input_config->aot == AOT_USAC) {
970     if (pstr_input_config->use_drc_element != pstr_input_config_user->use_drc_element) {
971       printf("\nDRC (Invalid config value, setting to default) : %d",
972              pstr_input_config->use_drc_element);
973     }
974     if (pstr_input_config->use_drc_element) {
975       printf("\nDRC : 1");
976       ixheaace_print_drc_config_params(pstr_input_config, pstr_input_config_user);
977     }
978 
979     if (pstr_input_config->random_access_interval !=
980         pstr_input_config_user->random_access_interval) {
981       printf("\nRandom access interval (Invalid config value, setting to default) : %d",
982              pstr_input_config->random_access_interval);
983     }
984 
985     if (pstr_input_config->use_delay_adjustment !=
986       pstr_input_config_user->use_delay_adjustment) {
987       printf("\nDelay compensation (Invalid config value, setting to default) : %d",
988         pstr_input_config->use_delay_adjustment);
989     }
990   }
991 
992   printf(
993       "\n*************************************************************************************"
994       "***********\n\n");
995 }
996 
ixheaace_calculate_loudness_measure(ixheaace_input_config * pstr_in_cfg,ixheaace_output_config * pstr_out_cfg,FILE * in_file)997 static IA_ERRORCODE ixheaace_calculate_loudness_measure(ixheaace_input_config *pstr_in_cfg,
998                                                         ixheaace_output_config *pstr_out_cfg,
999                                                         FILE *in_file) {
1000   WORD32 temp_pos, input_size;
1001   WORD32 count = 0;
1002   IA_ERRORCODE err_code = IA_NO_ERROR;
1003   temp_pos = ftell(in_file);
1004   VOID *loudness_handle =
1005       malloc_global(ixheaace_loudness_info_get_handle_size(), DEFAULT_MEM_ALIGN_8);
1006   if (loudness_handle == NULL) {
1007     printf("fatal error: libxaac encoder: Memory allocation failed");
1008     return -1;
1009   }
1010   input_size = (pstr_in_cfg->i_samp_freq / 10) * (pstr_in_cfg->i_channels);
1011   err_code = ixheaace_loudness_init_params(loudness_handle, pstr_in_cfg, pstr_out_cfg);
1012   if (err_code) {
1013     free_global(loudness_handle);
1014     return -1;
1015   }
1016   WORD16 **samples = 0;
1017   samples =
1018       (WORD16 **)malloc_global(pstr_in_cfg->i_channels * sizeof(*samples), DEFAULT_MEM_ALIGN_8);
1019   if (samples == NULL) {
1020     printf("fatal error: libxaac encoder: Memory allocation failed");
1021     free_global(loudness_handle);
1022     return -1;
1023   }
1024   for (count = 0; count < pstr_in_cfg->i_channels; count++) {
1025     samples[count] = (WORD16 *)malloc_global(
1026         (pstr_out_cfg->samp_freq / 10) * sizeof(*samples[count]), DEFAULT_MEM_ALIGN_8);
1027     if (samples[count] == NULL) {
1028       printf("fatal error: libxaac encoder: Memory allocation failed");
1029       while (count) {
1030         count--;
1031         free_global(samples[count]);
1032       }
1033       free_global(samples);
1034       free_global(loudness_handle);
1035       return -1;
1036     }
1037     memset(samples[count], 0, (pstr_out_cfg->samp_freq / 10) * sizeof(*samples[count]));
1038   }
1039   count = 0;
1040   WORD32 no_samples_per_frame = (WORD32)(pstr_out_cfg->samp_freq * 0.1 * pstr_in_cfg->i_channels);
1041   while (count <= ((pstr_in_cfg->aac_config.length / 2) - no_samples_per_frame)) {
1042     err_code =
1043         ia_enhaacplus_enc_pcm_data_read(in_file, input_size, pstr_in_cfg->i_channels, samples);
1044     if (err_code) {
1045       printf("fatal error: libxaac encoder: Reading PCM data failed");
1046       for (count = 0; count < pstr_in_cfg->i_channels; count++) {
1047         free_global(samples[count]);
1048       }
1049       free_global(samples);
1050       free_global(loudness_handle);
1051       return -1;
1052     }
1053     pstr_in_cfg->measured_loudness = ixheaace_measure_loudness(loudness_handle, samples);
1054     count += no_samples_per_frame;
1055   }
1056   if (pstr_in_cfg->method_def == METHOD_DEFINITION_PROGRAM_LOUDNESS) {
1057     pstr_in_cfg->measured_loudness = ixheaace_measure_integrated_loudness(loudness_handle);
1058     pstr_in_cfg->sample_peak_level = ixheaace_measure_sample_peak_value(loudness_handle);
1059   }
1060   fseek(in_file, temp_pos, SEEK_SET);
1061   for (count = 0; count < pstr_in_cfg->i_channels; count++) {
1062     free_global(samples[count]);
1063   }
1064   free_global(samples);
1065   free_global(loudness_handle);
1066   return err_code;
1067 }
1068 
ia_enhaacplus_enc_main_process(ixheaace_app_context * pstr_context,WORD32 argc,pWORD8 argv[])1069 IA_ERRORCODE ia_enhaacplus_enc_main_process(ixheaace_app_context *pstr_context, WORD32 argc,
1070                                             pWORD8 argv[]) {
1071   LOOPIDX frame_count = 0;
1072 
1073   /* Error code */
1074   IA_ERRORCODE err_code = IA_NO_ERROR;
1075 
1076   /* API obj */
1077   pVOID pv_ia_process_api_obj;
1078   /* First part                                        */
1079   /* Error Handler Init                                */
1080   /* Get Library Name, Library Version and API Version */
1081   /* Initialize API structure + Default config set     */
1082   /* Set config params from user                       */
1083   /* Initialize memory tables                          */
1084   /* Get memory information and allocate memory        */
1085 
1086   pWORD8 pb_inp_buf = NULL, pb_out_buf = NULL;
1087   WORD32 i_bytes_read = 0;
1088   WORD32 input_size = 0;
1089   WORD32 samp_freq;
1090   FLOAT32 down_sampling_ratio = 1;
1091   WORD32 i_out_bytes = 0;
1092   WORD32 i_total_length = 0;
1093   WORD32 start_offset_samples = 0, i_dec_len = 0;
1094   UWORD32 *ia_stsz_size = NULL;
1095   UWORD32 ui_samp_freq, ui_num_chan, ui_pcm_wd_sz, ui_pcm = 0, ui_channel_mask,
1096                                                    ui_num_coupling_chans = 0;
1097   WORD32 max_frame_size = 0;
1098   WORD32 expected_frame_count = 0;
1099   FILE *pf_drc_inp = NULL;
1100   /* The error init function */
1101   VOID (*p_error_init)();
1102 
1103   /* The process error info structure */
1104   ia_error_info_struct *p_proc_err_info;
1105 
1106   /* ******************************************************************/
1107   /* The API config structure                                         */
1108   /* ******************************************************************/
1109   ixheaace_user_config_struct str_enc_api = {{0}, {0}};
1110   ixheaace_input_config *pstr_in_cfg = &str_enc_api.input_config;
1111   ixheaace_output_config *pstr_out_cfg = &str_enc_api.output_config;
1112   pstr_in_cfg->pv_drc_cfg = malloc_global(sizeof(ia_drc_input_config), DEFAULT_MEM_ALIGN_8);
1113   if (pstr_in_cfg->pv_drc_cfg == NULL) {
1114     printf("fatal error: libxaac encoder: Memory allocation failed");
1115     return -1;
1116   }
1117   ia_drc_input_config *pstr_drc_cfg = (ia_drc_input_config *)pstr_in_cfg->pv_drc_cfg;
1118 
1119   /* Stack process struct initing */
1120   p_error_init = ia_enhaacplus_enc_error_handler_init;
1121   p_proc_err_info = &ia_enhaacplus_enc_error_info;
1122   /* Stack process struct initing end */
1123 
1124   /* ******************************************************************/
1125   /* Initialize the error handler                                     */
1126   /* ******************************************************************/
1127   (*p_error_init)();
1128 
1129   /* ******************************************************************/
1130   /* Parse input configuration parameters                             */
1131   /* ******************************************************************/
1132 
1133   iaace_aac_set_default_config(&pstr_in_cfg->aac_config);
1134 
1135   pstr_in_cfg->i_bitrate = pstr_in_cfg->aac_config.bitrate;
1136   pstr_in_cfg->aot = AOT_AAC_LC;
1137   pstr_in_cfg->codec_mode = USAC_ONLY_FD;
1138   pstr_in_cfg->i_channels = 2;
1139   pstr_in_cfg->i_samp_freq = 44100;
1140   pstr_in_cfg->i_use_mps = 0;
1141   pstr_in_cfg->i_mps_tree_config = -1;
1142   pstr_in_cfg->i_use_adts = 0;
1143   pstr_in_cfg->esbr_flag = 0;
1144   pstr_in_cfg->i_use_es = 1;
1145   pstr_in_cfg->cplx_pred = 0;
1146   pstr_in_cfg->ccfl_idx = NO_SBR_CCFL_1024;
1147   pstr_in_cfg->pvc_active = 0;
1148   pstr_in_cfg->harmonic_sbr = 0;
1149   pstr_in_cfg->inter_tes_active = 0;
1150   pstr_in_cfg->use_drc_element = 0;
1151   pstr_in_cfg->i_channels_mask = 0;
1152   pstr_in_cfg->i_num_coupling_chan = 0;
1153   pstr_in_cfg->aac_config.full_bandwidth = 0;
1154   pstr_out_cfg->malloc_xheaace = &malloc_global;
1155   pstr_out_cfg->free_xheaace = &free_global;
1156   pstr_in_cfg->frame_cmd_flag = 0;
1157   pstr_in_cfg->out_bytes_flag = 0;
1158   pstr_in_cfg->user_tns_flag = 0;
1159   pstr_in_cfg->user_esbr_flag = 0;
1160   pstr_in_cfg->i_use_adts = !pstr_context->use_ga_hdr;
1161   pstr_in_cfg->random_access_interval = DEFAULT_RAP_INTERVAL_IN_MS;
1162   pstr_in_cfg->method_def = METHOD_DEFINITION_PROGRAM_LOUDNESS;
1163   pstr_in_cfg->measurement_system = MEASUREMENT_SYSTEM_BS_1770_3;
1164   pstr_in_cfg->use_delay_adjustment = USAC_DEFAULT_DELAY_ADJUSTMENT_VALUE;
1165 
1166   /* ******************************************************************/
1167   /* Parse input configuration parameters                             */
1168   /* ******************************************************************/
1169   ixheaace_parse_config_param(argc, argv, &str_enc_api);
1170 
1171   {
1172     if (pstr_in_cfg->aot == AOT_AAC_LC || pstr_in_cfg->aot == AOT_SBR ||
1173         pstr_in_cfg->aot == AOT_PS) {
1174       if (pstr_in_cfg->frame_cmd_flag == 0) {
1175         pstr_in_cfg->frame_length = 1024;
1176       }
1177       if (pstr_in_cfg->out_bytes_flag == 0) {
1178         pstr_in_cfg->aac_config.bitreservoir_size = APP_BITRES_SIZE_CONFIG_PARAM_DEF_VALUE_LC;
1179       }
1180       if (pstr_in_cfg->user_tns_flag == 0) {
1181         pstr_in_cfg->aac_config.use_tns = 1;
1182       }
1183     } else if (pstr_in_cfg->aot == AOT_AAC_LD || pstr_in_cfg->aot == AOT_AAC_ELD) {
1184       if (pstr_in_cfg->frame_cmd_flag == 0) {
1185         pstr_in_cfg->frame_length = 512;
1186       }
1187       if (pstr_in_cfg->out_bytes_flag == 0) {
1188         pstr_in_cfg->aac_config.bitreservoir_size = APP_BITRES_SIZE_CONFIG_PARAM_DEF_VALUE_LD;
1189       }
1190 
1191       if (pstr_in_cfg->user_tns_flag == 0) {
1192         pstr_in_cfg->aac_config.use_tns = 1;
1193       }
1194     } else if (pstr_in_cfg->aot == AOT_USAC) {
1195       if (pstr_in_cfg->user_esbr_flag == 0) {
1196         pstr_in_cfg->esbr_flag = 1;
1197       }
1198       if (pstr_in_cfg->user_tns_flag == 0) {
1199         pstr_in_cfg->aac_config.use_tns = 1;
1200       }
1201     }
1202   }
1203 
1204   ui_samp_freq = pstr_in_cfg->i_samp_freq;
1205   ui_num_chan = pstr_in_cfg->i_channels;
1206   ui_channel_mask = pstr_in_cfg->i_channels_mask;
1207   ui_num_coupling_chans = pstr_in_cfg->i_num_coupling_chan;
1208   ui_pcm_wd_sz = pstr_in_cfg->ui_pcm_wd_sz;
1209   if (!(pstr_in_cfg->i_use_adts)) {
1210     pstr_in_cfg->i_use_es = 1;
1211   } else {
1212     pstr_in_cfg->i_use_es = 0;
1213   }
1214 
1215   if (!ui_pcm) {
1216     /* Decode WAV header */
1217     if (ia_enhaacplus_enc_wav_header_decode(pstr_context->pf_inp, &ui_num_chan, &ui_channel_mask,
1218                                             &ui_samp_freq, &ui_pcm_wd_sz, &i_total_length) == 1) {
1219       fprintf(stdout, "Unable to Read Input WAV File\n");
1220       exit(1);
1221     }
1222 
1223     /* PCM Word Size (For single input file) */
1224     pstr_in_cfg->ui_pcm_wd_sz = ui_pcm_wd_sz;
1225     /* Sampling Frequency */
1226     pstr_in_cfg->i_samp_freq = ui_samp_freq;
1227     /* Total Number of Channels */
1228     pstr_in_cfg->i_channels = ui_num_chan;
1229     /* Number of coupling channels*/
1230     pstr_in_cfg->i_num_coupling_chan = ui_num_coupling_chans;
1231     /* Channels Mask */
1232     pstr_in_cfg->i_channels_mask = ui_channel_mask;
1233 
1234     pstr_in_cfg->aac_config.length = i_total_length;
1235   }
1236 
1237   /*1st pass -> Loudness Measurement */
1238   if (pstr_in_cfg->aot == AOT_USAC || pstr_in_cfg->usac_en) {
1239     err_code =
1240         ixheaace_calculate_loudness_measure(pstr_in_cfg, pstr_out_cfg, pstr_context->pf_inp);
1241     if (err_code) {
1242       printf("\n Error in calculating loudness.\n");
1243       exit(1);
1244     } else {
1245       printf("\n loudness level : %lf", pstr_in_cfg->measured_loudness);
1246       printf("\n sample_peak_level : %lf \n", pstr_in_cfg->sample_peak_level);
1247     }
1248   }
1249 
1250   ixheaace_input_config pstr_in_cfg_user = *pstr_in_cfg;
1251 
1252   ia_drc_input_config *pstr_drc_cfg_user = NULL;
1253 
1254   /* Get library id and version number and display it */
1255   ixheaace_get_lib_id_strings((pVOID)&pstr_out_cfg->version);
1256   ia_enhaacplus_enc_display_id_message(pstr_out_cfg->version.p_lib_name,
1257                                        pstr_out_cfg->version.p_version_num);
1258 
1259   /* ******************************************************************/
1260   /* Initialize API structure and set config params to default        */
1261   /* ******************************************************************/
1262   /* DRC */
1263   if (pstr_in_cfg->use_drc_element == 1 && pstr_in_cfg->aot == AOT_USAC) {
1264     LOOPIDX k;
1265     CHAR8 drc_config_file_name[IA_MAX_CMD_LINE_LENGTH];
1266     strcpy(drc_config_file_name, DRC_CONFIG_FILE);
1267 
1268     pf_drc_inp = fopen(drc_config_file_name, "rt");
1269 
1270     if (!pf_drc_inp) {
1271       printf("\nError in opening DRC configuration file\n\n");
1272       pstr_in_cfg->use_drc_element = 0;
1273     }
1274 
1275     if (pf_drc_inp != 0) {
1276       memset(pstr_drc_cfg, 0, sizeof(ia_drc_input_config));
1277       ixheaace_read_drc_config_params(
1278           pf_drc_inp, &pstr_drc_cfg->str_enc_params, &pstr_drc_cfg->str_uni_drc_config,
1279           &pstr_drc_cfg->str_enc_loudness_info_set, &pstr_drc_cfg->str_enc_gain_extension,
1280           pstr_in_cfg->i_channels);
1281 
1282       pstr_drc_cfg->str_enc_params.gain_sequence_present = FALSE;
1283       for (k = 0; k < pstr_drc_cfg->str_uni_drc_config.drc_coefficients_uni_drc_count; k++) {
1284         if (pstr_drc_cfg->str_uni_drc_config.str_drc_coefficients_uni_drc[k].drc_location == 1) {
1285           if (pstr_drc_cfg->str_uni_drc_config.str_drc_coefficients_uni_drc[k].gain_set_count >
1286               0) {
1287             pstr_drc_cfg->str_enc_params.gain_sequence_present = TRUE;
1288             break;
1289           }
1290         }
1291       }
1292 
1293       if (pstr_drc_cfg->str_enc_params.gain_sequence_present == FALSE) {
1294         for (k = 0; k < pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1295                             .drc_coefficients_uni_drc_v1_count;
1296              k++) {
1297           if (pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1298                   .str_drc_coefficients_uni_drc_v1[k]
1299                   .drc_location == 1) {
1300             if (pstr_drc_cfg->str_uni_drc_config.str_uni_drc_config_ext
1301                     .str_drc_coefficients_uni_drc_v1[k]
1302                     .gain_sequence_count > 0) {
1303               pstr_drc_cfg->str_enc_params.gain_sequence_present = TRUE;
1304               break;
1305             }
1306           }
1307         }
1308       }
1309 
1310       pstr_drc_cfg_user =
1311           (ia_drc_input_config *)malloc_global(sizeof(ia_drc_input_config), DEFAULT_MEM_ALIGN_8);
1312       if (pstr_drc_cfg_user == NULL) {
1313         printf("fatal error: libxaac encoder: Memory allocation failed");
1314         free_global(pstr_in_cfg->pv_drc_cfg);
1315         return -1;
1316       }
1317       // Copy DRC config to user DRC config
1318       memcpy(pstr_drc_cfg_user, pstr_drc_cfg, sizeof(ia_drc_input_config));
1319 
1320       pstr_in_cfg_user.pv_drc_cfg = pstr_drc_cfg_user;
1321     }
1322   }
1323 
1324   err_code = ixheaace_create((pVOID)pstr_in_cfg, (pVOID)pstr_out_cfg);
1325   _IA_HANDLE_ERROR(p_proc_err_info, (pWORD8) "", err_code, pstr_out_cfg);
1326 
1327   pv_ia_process_api_obj = pstr_out_cfg->pv_ia_process_api_obj;
1328 
1329   pb_inp_buf = (pWORD8)pstr_out_cfg->mem_info_table[IA_MEMTYPE_INPUT].mem_ptr;
1330   pb_out_buf = (pWORD8)pstr_out_cfg->mem_info_table[IA_MEMTYPE_OUTPUT].mem_ptr;
1331 
1332   ixheaace_print_config_params(pstr_in_cfg, &pstr_in_cfg_user);
1333 
1334   if (pstr_drc_cfg_user) {
1335     free_global(pstr_drc_cfg_user);
1336     pstr_drc_cfg_user = NULL;
1337   }
1338 
1339   start_offset_samples = 0;
1340   input_size = pstr_out_cfg->input_size;
1341   expected_frame_count = pstr_out_cfg->expected_frame_count;
1342 
1343   if (NULL == ia_stsz_size) {
1344     ia_stsz_size = (UWORD32 *)malloc_global((expected_frame_count + 2) * sizeof(*ia_stsz_size),
1345                                             DEFAULT_MEM_ALIGN_8);
1346     if (ia_stsz_size == NULL) {
1347       if (pstr_in_cfg->pv_drc_cfg) {
1348         free_global(pstr_in_cfg->pv_drc_cfg);
1349       }
1350       printf("fatal error: libxaac encoder: Memory allocation failed");
1351       return -1;
1352     }
1353     memset(ia_stsz_size, 0, (expected_frame_count + 2) * sizeof(*ia_stsz_size));
1354   }
1355   down_sampling_ratio = pstr_out_cfg->down_sampling_ratio;
1356   samp_freq = (WORD32)(pstr_out_cfg->samp_freq / down_sampling_ratio);
1357 
1358   { ia_enhaacplus_enc_fwrite(pb_out_buf, pstr_context->pf_out, 0); }
1359 
1360   if ((pstr_in_cfg->usac_en || pstr_in_cfg->i_use_es)) {
1361     i_dec_len = pstr_out_cfg->i_out_bytes;
1362     ia_enhaacplus_enc_fwrite(pb_out_buf, pstr_context->pf_out, pstr_out_cfg->i_out_bytes);
1363     fflush(pstr_context->pf_out);
1364   }
1365 
1366   i_bytes_read =
1367       ia_enhaacplus_enc_fread((pVOID)pb_inp_buf, sizeof(WORD8), input_size, pstr_context->pf_inp);
1368 
1369   while (i_bytes_read) {
1370     /*****************************************************************************/
1371     /* Print frame number */
1372     /*****************************************************************************/
1373     fprintf(stdout, "Frames Processed [%d]\r", frame_count);
1374     fflush(stdout);
1375 
1376     if (i_bytes_read != input_size) {
1377       memset((pb_inp_buf + i_bytes_read), 0, (input_size - i_bytes_read));
1378     }
1379 
1380     /*****************************************************************************/
1381     /* Perform Encoding of frame data */
1382     /*****************************************************************************/
1383 
1384     err_code = ixheaace_process(pv_ia_process_api_obj, (pVOID)pstr_in_cfg, (pVOID)pstr_out_cfg);
1385 
1386     _IA_HANDLE_ERROR(p_proc_err_info, (pWORD8) "", err_code, pstr_out_cfg);
1387 
1388     /* Get the output bytes */
1389     i_out_bytes = pstr_out_cfg->i_out_bytes;
1390 
1391     if (max_frame_size < i_out_bytes) max_frame_size = i_out_bytes;
1392     if (i_out_bytes) {
1393       frame_count++;
1394       ia_stsz_size[frame_count - 1] = pstr_out_cfg->i_out_bytes;
1395 
1396       ia_enhaacplus_enc_fwrite(pb_out_buf, pstr_context->pf_out, i_out_bytes);
1397       fflush(pstr_context->pf_out);
1398       if (!pstr_in_cfg->use_delay_adjustment) {
1399          i_bytes_read = ia_enhaacplus_enc_fread((pVOID)pb_inp_buf, sizeof(WORD8), input_size,
1400            pstr_context->pf_inp);
1401       }
1402     }
1403     if (pstr_in_cfg->use_delay_adjustment) {
1404       i_bytes_read = ia_enhaacplus_enc_fread((pVOID)pb_inp_buf, sizeof(WORD8), input_size,
1405         pstr_context->pf_inp);
1406     }
1407 
1408     if (frame_count == expected_frame_count) break;
1409   }
1410 
1411   fprintf(stdout, "\n");
1412   fflush(stdout);
1413 
1414   // Error handler is not invoked here to avoid invoking ixheaace_delete() twice.
1415   err_code = ixheaace_delete((pVOID)pstr_out_cfg);
1416   if ((err_code)&IA_FATAL_ERROR) {
1417     if (pstr_in_cfg->pv_drc_cfg) {
1418       free_global(pstr_in_cfg->pv_drc_cfg);
1419     }
1420     if (ia_stsz_size != NULL) {
1421       free_global(ia_stsz_size);
1422     }
1423     return (err_code);
1424   }
1425 
1426   if ((pstr_in_cfg->usac_en || pstr_in_cfg->i_use_es) && (pstr_context->pf_meta)) {
1427     fprintf(pstr_context->pf_meta, "-dec_info_init:%d\n", i_dec_len);
1428     fprintf(pstr_context->pf_meta, "-g_track_count:%d\n", 1);
1429     fprintf(pstr_context->pf_meta, "-ia_mp4_stsz_entries:%d\n", frame_count);
1430     fprintf(pstr_context->pf_meta, "-movie_time_scale:%d\n", samp_freq);
1431     fprintf(pstr_context->pf_meta, "-media_time_scale:%d\n", samp_freq);
1432     fprintf(pstr_context->pf_meta, "-playTimeInSamples:%d\n",
1433             i_total_length / ((ui_pcm_wd_sz >> 3) * ui_num_chan));
1434     fprintf(pstr_context->pf_meta, "-startOffsetInSamples:%d\n-useEditlist:%d\n",
1435             start_offset_samples, 1);
1436     for (WORD32 i = 0; i < frame_count; i++)
1437       fprintf(pstr_context->pf_meta, "-ia_mp4_stsz_size:%d\n", ia_stsz_size[i]);
1438   }
1439   if (pstr_in_cfg->pv_drc_cfg) {
1440     free_global(pstr_in_cfg->pv_drc_cfg);
1441   }
1442   if (ia_stsz_size != NULL) {
1443     free_global(ia_stsz_size);
1444   }
1445   return IA_NO_ERROR;
1446 }
1447 
main(WORD32 argc,pCHAR8 argv[])1448 int main(WORD32 argc, pCHAR8 argv[]) {
1449   FILE *param_file_id = NULL;
1450   WORD32 usac_en = 0;
1451   WORD8 curr_cmd[IA_MAX_CMD_LINE_LENGTH];
1452   WORD32 fargc, curpos;
1453   WORD32 processcmd = 0;
1454   WORD8 fargv[IA_MAX_ARGS][IA_MAX_CMD_LINE_LENGTH];
1455 
1456   pWORD8 pargv[IA_MAX_ARGS];
1457 
1458   WORD8 pb_input_file_path[IA_MAX_CMD_LINE_LENGTH] = "";
1459   WORD8 pb_output_file_path[IA_MAX_CMD_LINE_LENGTH] = "";
1460   WORD8 pb_drc_file_path[IA_MAX_CMD_LINE_LENGTH] = "";
1461   ixheaace_app_context str_context;
1462   memset(&str_context, 0, sizeof(ixheaace_app_context));
1463   str_context.use_ga_hdr = 1;
1464   ia_testbench_error_handler_init();
1465 
1466   ixheaace_version instance = {0};
1467   if (argc == 1 || argc == 2) {
1468     if (argc == 2 && (!strncmp((const char *)argv[1], "-paramfile:", 11))) {
1469       pWORD8 paramfile = (pWORD8)argv[1] + 11;
1470 
1471       param_file_id = fopen((const char *)paramfile, "r");
1472       if (param_file_id == NULL) {
1473         ixheaace_get_lib_id_strings(&instance);
1474         ia_enhaacplus_enc_display_id_message(instance.p_lib_name, instance.p_version_num);
1475         ia_enhaacplus_enc_print_usage();
1476         return IA_NO_ERROR;
1477       }
1478     } else {
1479       param_file_id = fopen(PARAMFILE, "r");
1480       if (param_file_id == NULL) {
1481         ixheaace_get_lib_id_strings(&instance);
1482         ia_enhaacplus_enc_display_id_message(instance.p_lib_name, instance.p_version_num);
1483         ia_enhaacplus_enc_print_usage();
1484         return IA_NO_ERROR;
1485       }
1486     }
1487 
1488     /* Process one line at a time */
1489     while (fgets((char *)curr_cmd, IA_MAX_CMD_LINE_LENGTH, param_file_id)) {
1490       WORD8 pb_meta_file_name[IA_MAX_CMD_LINE_LENGTH] = "";
1491       curpos = 0;
1492       fargc = 0;
1493       usac_en = 0;
1494       /* if it is not a param_file command and if */
1495       /* CLP processing is not enabled */
1496       if (curr_cmd[0] != '@' && !processcmd) { /* skip it */
1497         continue;
1498       }
1499 
1500       while (sscanf((char *)curr_cmd + curpos, "%s", fargv[fargc]) != EOF) {
1501         if (fargv[0][0] == '/' && fargv[0][1] == '/') break;
1502         if (strcmp((const char *)fargv[0], "@echo") == 0) break;
1503         if (strcmp((const char *)fargv[fargc], "@New_line") == 0) {
1504           if (NULL == fgets((char *)curr_cmd + curpos, IA_MAX_CMD_LINE_LENGTH, param_file_id))
1505             break;
1506           continue;
1507         }
1508         curpos += (WORD32)strlen((const char *)fargv[fargc]);
1509         while (*(curr_cmd + curpos) == ' ' || *(curr_cmd + curpos) == '\t') curpos++;
1510         fargc++;
1511       }
1512 
1513       if (fargc < 1) /* for blank lines etc. */
1514         continue;
1515 
1516       if (strcmp((const char *)fargv[0], "@Output_path") == 0) {
1517         if (fargc > 1)
1518           strcpy((char *)pb_output_file_path, (const char *)fargv[1]);
1519         else
1520           strcpy((char *)pb_output_file_path, "");
1521         continue;
1522       }
1523 
1524       if (strcmp((const char *)fargv[0], "@Input_path") == 0) {
1525         if (fargc > 1)
1526           strcpy((char *)pb_input_file_path, (const char *)fargv[1]);
1527         else
1528           strcpy((char *)pb_input_file_path, "");
1529         strcpy((char *)pb_drc_file_path, (const char *)pb_input_file_path);
1530         continue;
1531       }
1532 
1533       if (strcmp((const char *)fargv[0], "@Start") == 0) {
1534         processcmd = 1;
1535         continue;
1536       }
1537 
1538       if (strcmp((const char *)fargv[0], "@Stop") == 0) {
1539         processcmd = 0;
1540         continue;
1541       }
1542 
1543       /* otherwise if this a normal command and its enabled for execution */
1544       if (processcmd) {
1545         int i;
1546         int err_code = IA_NO_ERROR;
1547         WORD8 pb_input_file_name[IA_MAX_CMD_LINE_LENGTH] = "";
1548         WORD8 pb_output_file_name[IA_MAX_CMD_LINE_LENGTH] = "";
1549         WORD32 aot_value = 0;
1550         WORD32 is_ld_eld = 0;  // If set to 1, it denotes AOT 23 or AOT 39
1551 
1552         int file_count = 0;
1553         for (i = 0; i < fargc; i++) {
1554           printf("%s ", fargv[i]);
1555           pargv[i] = fargv[i];
1556 
1557           if (!strncmp((const char *)fargv[i], "-ifile:", 7)) {
1558             pWORD8 pb_arg_val = fargv[i] + 7;
1559 
1560             strcat((char *)pb_input_file_name, (const char *)pb_input_file_path);
1561             strcat((char *)pb_input_file_name, (const char *)pb_arg_val);
1562 
1563             str_context.pf_inp = NULL;
1564             str_context.pf_inp = fopen((const char *)pb_input_file_name, "rb");
1565             if (str_context.pf_inp == NULL) {
1566               err_code = IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED;
1567               ia_error_handler(&ia_testbench_error_info, (pWORD8) "Input File", err_code);
1568             }
1569             file_count++;
1570           }
1571 
1572           if (!strncmp((const char *)fargv[i], "-ofile:", 7)) {
1573             pWORD8 pb_arg_val = fargv[i] + 7;
1574 
1575             strcat((char *)pb_output_file_name, (const char *)pb_output_file_path);
1576             strcat((char *)pb_output_file_name, (const char *)pb_arg_val);
1577 
1578             str_context.pf_out = NULL;
1579             str_context.pf_out = fopen((const char *)pb_output_file_name, "wb");
1580             if (str_context.pf_out == NULL) {
1581               err_code = IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED;
1582               ia_error_handler(&ia_testbench_error_info, (pWORD8) "Output File", err_code);
1583             }
1584             file_count++;
1585           }
1586           if (!strncmp((const char *)fargv[i], "-usac:", 6)) {
1587             usac_en = 1;
1588           }
1589           if (!strncmp((const char *)fargv[i], "-aot:", 5)) {
1590             pWORD8 pb_arg_val = fargv[i] + 5;
1591             aot_value = atoi((const char *)(pb_arg_val));
1592             if (aot_value == 23 || aot_value == 39) {
1593               is_ld_eld = 1;
1594             }
1595           }
1596           if (!strncmp((const char *)fargv[i], "-adts:", 6)) {
1597             pWORD8 pb_arg_val = fargv[i] + 6;
1598 
1599             if ((atoi((const char *)pb_arg_val))) str_context.use_ga_hdr = 0;
1600           }
1601         }
1602 
1603         printf("\n");
1604 
1605         if (file_count != 2) {
1606           err_code = IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED;
1607           ia_error_handler(&ia_testbench_error_info, (pWORD8) "Input or Output File", err_code);
1608         }
1609         if (is_ld_eld) {
1610           str_context.use_ga_hdr = 1;
1611         }
1612 
1613         if ((strcmp((const char *)pb_output_file_name, "")) &&
1614             (usac_en || str_context.use_ga_hdr)) {
1615           char *file_name = strrchr((const char *)pb_output_file_name, '.');
1616           SIZE_T idx = file_name - (char *)pb_output_file_name;
1617           memcpy(pb_meta_file_name, pb_output_file_name, idx);
1618           strcat((char *)pb_meta_file_name, ".txt");
1619           str_context.pf_meta = NULL;
1620           str_context.pf_meta = fopen((const char *)pb_meta_file_name, "wt");
1621           if (str_context.pf_meta == NULL) {
1622             err_code = IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED;
1623             ia_error_handler(&ia_testbench_error_info, (pWORD8) "Meta File", err_code);
1624           }
1625         }
1626         if (err_code == IA_NO_ERROR) ia_enhaacplus_enc_main_process(&str_context, fargc, pargv);
1627 
1628         str_context.use_ga_hdr = 1;
1629 
1630         if (str_context.pf_inp) fclose(str_context.pf_inp);
1631         if (str_context.pf_out) fclose(str_context.pf_out);
1632         if (str_context.pf_meta != NULL) {
1633           fclose(str_context.pf_meta);
1634           str_context.pf_meta = NULL;
1635         }
1636       }
1637     }
1638   } else {
1639     int i;
1640     int err_code = IA_NO_ERROR;
1641     int file_count = 0;
1642     WORD32 aot_value = 0;
1643     WORD32 is_ld_eld = 0;  // If set to 1, it denotes AOT 23 or AOT 39
1644 
1645     WORD8 pb_input_file_name[IA_MAX_CMD_LINE_LENGTH] = "";
1646     WORD8 pb_output_file_name[IA_MAX_CMD_LINE_LENGTH] = "";
1647     WORD8 pb_meta_file_name[IA_MAX_CMD_LINE_LENGTH] = "";
1648     for (i = 1; i < argc; i++) {
1649       printf("%s ", argv[i]);
1650 
1651       if (!strncmp((const char *)argv[i], "-ifile:", 7)) {
1652         pWORD8 pb_arg_val = (pWORD8)argv[i] + 7;
1653         strcat((char *)pb_input_file_name, (const char *)pb_input_file_path);
1654         strcat((char *)pb_input_file_name, (const char *)pb_arg_val);
1655 
1656         str_context.pf_inp = NULL;
1657         str_context.pf_inp = fopen((const char *)pb_input_file_name, "rb");
1658         if (str_context.pf_inp == NULL) {
1659           err_code = IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED;
1660           ia_error_handler(&ia_testbench_error_info, (pWORD8) "Input File", err_code);
1661         }
1662         file_count++;
1663       }
1664 
1665       if (!strncmp((const char *)argv[i], "-ofile:", 7)) {
1666         pWORD8 pb_arg_val = (pWORD8)argv[i] + 7;
1667 
1668         strcat((char *)pb_output_file_name, (const char *)pb_output_file_path);
1669         strcat((char *)pb_output_file_name, (const char *)pb_arg_val);
1670 
1671         str_context.pf_out = NULL;
1672         str_context.pf_out = fopen((const char *)pb_output_file_name, "wb");
1673         if (str_context.pf_out == NULL) {
1674           err_code = IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED;
1675           ia_error_handler(&ia_testbench_error_info, (pWORD8) "Output File", err_code);
1676         }
1677         file_count++;
1678       }
1679 
1680       if (!strncmp((const char *)argv[i], "-usac:", 6)) {
1681         usac_en = 1;
1682       }
1683       if (!strncmp((const char *)argv[i], "-aot:", 5)) {
1684         pCHAR8 pb_arg_val = argv[i] + 5;
1685         aot_value = atoi((const char *)(pb_arg_val));
1686         if (aot_value == 23 || aot_value == 39) {
1687           is_ld_eld = 1;
1688         }
1689       }
1690 
1691       if (!strncmp((const char *)argv[i], "-adts:", 6)) {
1692         pCHAR8 pb_arg_val = argv[i] + 6;
1693         if (atoi((const char *)pb_arg_val)) str_context.use_ga_hdr = 0;
1694       }
1695 
1696       if (!strncmp((const char *)argv[i], "-help", 5)) {
1697         ia_enhaacplus_enc_print_usage();
1698       }
1699     }
1700 
1701     printf("\n");
1702     if (file_count != 2) {
1703       err_code = IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED;
1704       ia_error_handler(&ia_testbench_error_info, (pWORD8) "Input or Output File", err_code);
1705     }
1706     if (is_ld_eld) {
1707       str_context.use_ga_hdr = 1;
1708     }
1709 #ifdef _WIN32
1710 #pragma warning(suppress : 6001)
1711 #endif
1712 
1713     if ((strcmp((const char *)pb_output_file_name, "")) && (usac_en || str_context.use_ga_hdr)) {
1714       char *file_name = strrchr((const char *)pb_output_file_name, '.');
1715       SIZE_T idx = file_name - (char *)pb_output_file_name;
1716       memcpy(pb_meta_file_name, pb_output_file_name, idx);
1717       strcat((char *)pb_meta_file_name, ".txt");
1718       str_context.pf_meta = NULL;
1719       str_context.pf_meta = fopen((const char *)pb_meta_file_name, "wt");
1720       if (str_context.pf_meta == NULL) {
1721         err_code = IA_TESTBENCH_MFMAN_FATAL_FILE_OPEN_FAILED;
1722         ia_error_handler(&ia_testbench_error_info, (pWORD8) "Meta File", err_code);
1723       }
1724     }
1725     if (err_code == IA_NO_ERROR)
1726       ia_enhaacplus_enc_main_process(&str_context, argc - 1, (pWORD8 *)&argv[1]);
1727 
1728     str_context.use_ga_hdr = 1;
1729     if (str_context.pf_inp) fclose(str_context.pf_inp);
1730     if (str_context.pf_out) fclose(str_context.pf_out);
1731     if (str_context.pf_meta != NULL) {
1732       fclose(str_context.pf_meta);
1733       str_context.pf_meta = NULL;
1734     }
1735   }
1736   if (param_file_id != NULL) {
1737     fclose(param_file_id);
1738   }
1739 
1740   return IA_NO_ERROR;
1741 }
1742