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