audio: Fix regression of libfaad. [1/1]

PD#SWPL-172603

Problem:
Fix regression issues with coverity changes: sending main and ad data
to the faad decoder at the same time, which causes decoding confusion.

Solution:
Static variables make functions non-reentrant,
delete static variables and use heap variables instead;
This is a supplement to the following cl:
https://scgit.amlogic.com/#/c/453144.

Verify:
t5w.

Change-Id: Iabb446e06aee41825cf60151c3b8862e28e29eda
Signed-off-by: yuliang.hu <yuliang.hu@amlogic.com>
diff --git a/libfaad/decoder.c b/libfaad/decoder.c
index 08f1e3d..16c7346 100644
--- a/libfaad/decoder.c
+++ b/libfaad/decoder.c
@@ -823,6 +823,13 @@
     hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
     hDecoder->last_ch_configure = -1;
     hDecoder->last_sf_index = -1;
+    hDecoder->process_buf = calloc(1, sizeof(element));
+    if (hDecoder->process_buf) {
+        hDecoder->process_buf_size = sizeof(element);
+    } else {
+        hDecoder->process_buf_size = 0;
+    }
+
     return hDecoder;
 }
 
@@ -1428,6 +1435,10 @@
         }
     }
 #endif
+    if (hDecoder->process_buf) {
+        free(hDecoder->process_buf);
+        hDecoder->process_buf = NULL;
+    }
     //why not free before?
     if (hDecoder) {
         faad_free(hDecoder);
diff --git a/libfaad/filtbank.c b/libfaad/filtbank.c
index 7b03794..74524a5 100644
--- a/libfaad/filtbank.c
+++ b/libfaad/filtbank.c
@@ -86,6 +86,18 @@
 #endif
     }
 #endif
+    fb->process_buf = calloc(2 * 1024, sizeof(real_t));
+    if (fb->process_buf) {
+        fb->process_buf_size = 2 * 1024 * sizeof(real_t);
+    } else {
+        fb->process_buf_size = 0;
+    }
+    fb->process_buf2 = calloc(2 * 1024, sizeof(real_t));
+    if (fb->process_buf2) {
+        fb->process_buf_size2 = 2 * 1024 * sizeof(real_t);
+    } else {
+        fb->process_buf_size2 = 0;
+    }
 
     return fb;
 }
@@ -102,6 +114,14 @@
 #ifdef LD_DEC
         faad_mdct_end(fb->mdct1024);
 #endif
+        if (fb->process_buf) {
+            free(fb->process_buf);
+            fb->process_buf = NULL;
+        }
+        if (fb->process_buf2) {
+            free(fb->process_buf2);
+            fb->process_buf2 = NULL;
+        }
 
         faad_free(fb);
     }
@@ -123,6 +143,7 @@
         break;
     default:
         mdct = fb->mdct2048;
+        break;
     }
 
     faad_imdct(mdct, in_data, out_data);
@@ -154,6 +175,7 @@
 #endif
     default:
         mdct = fb->mdct2048;
+        break;
     }
 
     faad_mdct(mdct, in_data, out_data);
@@ -166,7 +188,7 @@
                   uint8_t object_type, uint16_t frame_len)
 {
     int16_t i;
-    ALIGN static real_t transf_buf[2 * 1024] = {0};
+    ALIGN real_t *transf_buf = NULL;
 
     const real_t *window_long = NULL;
     const real_t *window_long_prev = NULL;
@@ -178,7 +200,8 @@
     uint16_t trans = nshort / 2;
 
     uint16_t nflat_ls = (nlong - nshort) / 2;
-    memset(transf_buf, 0, sizeof(transf_buf));
+    memset(fb->process_buf, 0, fb->process_buf_size);
+    transf_buf = (real_t *)fb->process_buf;
 
 #ifdef PROFILE
     int64_t count = faad_get_ts();
@@ -344,7 +367,7 @@
                      uint8_t object_type, uint16_t frame_len)
 {
     int16_t i;
-    ALIGN static real_t windowed_buf[2 * 1024] = {0};
+    ALIGN real_t *windowed_buf = NULL;
 
     const real_t *window_long = NULL;
     const real_t *window_long_prev = NULL;
@@ -354,8 +377,9 @@
     uint16_t nlong = frame_len;
     uint16_t nshort = frame_len / 8;
     uint16_t nflat_ls = (nlong - nshort) / 2;
+    memset(fb->process_buf, 0, fb->process_buf_size);
+    windowed_buf = (real_t *)fb->process_buf;
 
-    memset(windowed_buf, 0, sizeof(windowed_buf));
     assert(window_sequence != EIGHT_SHORT_SEQUENCE);
 
 #ifdef LD_DEC
diff --git a/libfaad/lt_predict.c b/libfaad/lt_predict.c
index b4bbd65..b8cc9e8 100644
--- a/libfaad/lt_predict.c
+++ b/libfaad/lt_predict.c
@@ -81,10 +81,12 @@
 {
     uint8_t sfb;
     uint16_t bin, i, num_samples;
-    ALIGN static real_t x_est[2048];
-    ALIGN static real_t X_est[2048];
-    memset(x_est, 0, sizeof(x_est));
-    memset(X_est, 0, sizeof(X_est));
+    ALIGN real_t *x_est = NULL;
+    ALIGN real_t *X_est = NULL;
+    memset(fb->process_buf, 0, fb->process_buf_size);
+    memset(fb->process_buf2, 0, fb->process_buf_size2);
+    x_est = (real_t *)fb->process_buf;
+    X_est = (real_t *)fb->process_buf2;
 
     if (ics->window_sequence != EIGHT_SHORT_SEQUENCE) {
         if (ltp->data_present) {
diff --git a/libfaad/ps_dec.c b/libfaad/ps_dec.c
index 98979e4..142a5ae 100644
--- a/libfaad/ps_dec.c
+++ b/libfaad/ps_dec.c
@@ -1768,7 +1768,14 @@
 {
     /* free hybrid filterbank structures */
     hybrid_free(ps->hyb);
-
+    if (ps->process_buf) {
+            free(ps->process_buf);
+            ps->process_buf = NULL;
+        }
+    if (ps->process_buf2) {
+        free(ps->process_buf2);
+        ps->process_buf2 = NULL;
+    }
     faad_free(ps);
 }
 
@@ -1855,16 +1862,30 @@
         IM(ps->opd_prev[i][1]) = 0;
     }
 
+    ps->process_buf = calloc(32 * 32, sizeof(qmf_t));
+    if (ps->process_buf) {
+        ps->process_buf_size = 32 * 32 * sizeof(qmf_t);
+    } else {
+        ps->process_buf_size = 0;
+    }
+    ps->process_buf2 = calloc(32 * 32, sizeof(qmf_t));
+    if (ps->process_buf2) {
+        ps->process_buf_size2 = 32 * 32 * sizeof(qmf_t);
+    } else {
+        ps->process_buf_size2 = 0;
+    }
     return ps;
 }
 
 /* main Parametric Stereo decoding function */
 uint8_t ps_decode(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64])
 {
-    static qmf_t X_hybrid_left[32][32] = {{0}};
-    static qmf_t X_hybrid_right[32][32] = {{0}};
-    memset(X_hybrid_left, 0, sizeof(X_hybrid_left));
-    memset(X_hybrid_right, 0, sizeof(X_hybrid_right));
+    qmf_t (*X_hybrid_left)[32] = NULL;
+    qmf_t (*X_hybrid_right)[32] = NULL;
+    memset(ps->process_buf, 0, sizeof(ps->process_buf_size));
+    memset(ps->process_buf2, 0, sizeof(ps->process_buf_size2));
+    X_hybrid_left = (qmf_t (*)[32])ps->process_buf;
+    X_hybrid_right = (qmf_t (*)[32])ps->process_buf2;
 
     /* delta decoding of the bitstream data */
     ps_data_decode(ps);
diff --git a/libfaad/ps_dec.h b/libfaad/ps_dec.h
index 9b112f3..f609291 100644
--- a/libfaad/ps_dec.h
+++ b/libfaad/ps_dec.h
@@ -132,6 +132,10 @@
         complex_t ipd_prev[20][2];
         complex_t opd_prev[20][2];
 
+        void *process_buf;
+        int process_buf_size;
+        void *process_buf2;
+        int process_buf_size2;
     } ps_info;
 
     /* ps_syntax.c */
diff --git a/libfaad/sbr_dec.c b/libfaad/sbr_dec.c
index edecdd7..ffc8405 100644
--- a/libfaad/sbr_dec.c
+++ b/libfaad/sbr_dec.c
@@ -138,6 +138,19 @@
         memset(sbr->Xsbr[0], 0, (sbr->numTimeSlotsRate + sbr->tHFGen) * 64 * sizeof(qmf_t));
     }
 
+    sbr->process_buf = calloc(38 * 64, sizeof(qmf_t));
+    if (sbr->process_buf) {
+        sbr->process_buf_size = 38 * 64 * sizeof(qmf_t);
+    } else {
+        sbr->process_buf_size = 0;
+    }
+    sbr->process_buf2 = calloc(38 * 64, sizeof(qmf_t));
+    if (sbr->process_buf2) {
+        sbr->process_buf_size2 = 38 * 64 * sizeof(qmf_t);
+    } else {
+        sbr->process_buf_size2 = 0;
+    }
+
     return sbr;
 }
 
@@ -442,12 +455,13 @@
 {
     uint8_t dont_process = 0;
     uint8_t ret = 0;
-    ALIGN static qmf_t X[MAX_NTSR][64];
-    memset(X, 0, sizeof(X));
+    ALIGN qmf_t (*X)[64] = NULL;
 
     if (sbr == NULL) {
         return 20;
     }
+    memset(sbr->process_buf, 0, sizeof(sbr->process_buf_size));
+    X = (qmf_t (*)[64])sbr->process_buf;
 
     /* case can occur due to bit errors */
     if (sbr->id_aac != ID_CPE) {
@@ -527,12 +541,13 @@
 {
     uint8_t dont_process = 0;
     uint8_t ret = 0;
-    ALIGN static qmf_t X[MAX_NTSR][64];
-    memset(X, 0, sizeof(X));
+    ALIGN qmf_t (*X)[64] = NULL;
 
     if (sbr == NULL) {
         return 20;
     }
+    memset(sbr->process_buf, 0, sizeof(sbr->process_buf_size));
+    X = (qmf_t (*)[64])sbr->process_buf;
 
     /* case can occur due to bit errors */
     if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE) {
@@ -598,14 +613,17 @@
     uint8_t l, k;
     uint8_t dont_process = 0;
     uint8_t ret = 0;
-    ALIGN static qmf_t X_left[38][64] = {{0}};
-    ALIGN static qmf_t X_right[38][64] = {{0}}; /* must set this to 0 */
-    memset(X_left, 0, sizeof(X_left));
-    memset(X_right, 0, sizeof(X_right));
+    ALIGN qmf_t (*X_left)[64] = NULL;
+    ALIGN qmf_t (*X_right)[64] = NULL;
 
     if (sbr == NULL) {
         return 20;
     }
+    memset(sbr->process_buf, 0, sizeof(sbr->process_buf_size));/* must set this to 0 */
+    memset(sbr->process_buf2, 0, sizeof(sbr->process_buf_size2));
+    X_left = (qmf_t (*)[64])sbr->process_buf;
+    X_right = (qmf_t (*)[64])sbr->process_buf2;
+
 
     /* case can occur due to bit errors */
     if (sbr->id_aac != ID_SCE && sbr->id_aac != ID_LFE) {
diff --git a/libfaad/sbr_dec.h b/libfaad/sbr_dec.h
index ec6dd2b..3950435 100644
--- a/libfaad/sbr_dec.h
+++ b/libfaad/sbr_dec.h
@@ -225,6 +225,10 @@
         uint8_t bs_num_rel_1[2];
         uint8_t bs_df_env[2][9];
         uint8_t bs_df_noise[2][3];
+        void *process_buf;
+        int process_buf_size;
+        void *process_buf2;
+        int process_buf_size2;
     } sbr_info;
 
     sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac,
diff --git a/libfaad/ssr_fb.c b/libfaad/ssr_fb.c
index 15e7b92..23e5432 100644
--- a/libfaad/ssr_fb.c
+++ b/libfaad/ssr_fb.c
@@ -57,6 +57,19 @@
     fb->long_window[1]  = kbd_long_256;
     fb->short_window[1] = kbd_short_32;
 
+    fb->process_buf = calloc(2 * 1024, sizeof(real_t));
+    if (fb->process_buf) {
+        fb->process_buf_size = 2 * 1024 * sizeof(real_t);
+    } else {
+        fb->process_buf_size = 0;
+    }
+    fb->process_buf2 = calloc(2 * 1024, sizeof(real_t));
+    if (fb->process_buf2) {
+        fb->process_buf_size2 = 2 * 1024 * sizeof(real_t);
+    } else {
+        fb->process_buf_size2 = 0;
+    }
+
     return fb;
 }
 
@@ -65,6 +78,15 @@
     faad_mdct_end(fb->mdct256);
     faad_mdct_end(fb->mdct2048);
 
+    if (fb->process_buf) {
+        free(fb->process_buf);
+        fb->process_buf = NULL;
+    }
+    if (fb->process_buf2) {
+        free(fb->process_buf2);
+        fb->process_buf2 = NULL;
+    }
+
     if (fb) {
         faad_free(fb);
     }
diff --git a/libfaad/structs.h b/libfaad/structs.h
index 4d99267..0bc4a1b 100644
--- a/libfaad/structs.h
+++ b/libfaad/structs.h
@@ -82,6 +82,10 @@
 #ifdef PROFILE
         int64_t cycles;
 #endif
+        void *process_buf;
+        int process_buf_size;
+        void *process_buf2;
+        int process_buf_size2;
     } fb_info;
 
     typedef struct {
@@ -509,6 +513,8 @@
         short  dec_buffer[DEC_BUF_SIZE];
         short output_buffer[OUT_BUF_SIZE];
         unsigned char temp_bufer[TMP_BUF_SIZE];
+        element *process_buf;
+        int process_buf_size;
     } NeAACDecStruct;
 
 
diff --git a/libfaad/syntax.c b/libfaad/syntax.c
index 02453f4..84a56cc 100644
--- a/libfaad/syntax.c
+++ b/libfaad/syntax.c
@@ -588,19 +588,21 @@
         uint8_t channel, uint8_t *tag)
 {
     uint8_t retval = 0;
-    static element sce = {0};
-    ic_stream *ics = &(sce.ics1);
+    element *sce = NULL;
+    ic_stream *ics = NULL;
     ALIGN int16_t spec_data[1024] = {0};
-    memset(&sce, 0, sizeof(sce));
+    memset(hDecoder->process_buf, 0, hDecoder->process_buf_size);
+    sce = (element *)hDecoder->process_buf;
+    ics = &(sce->ics1);
 
-    sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
+    sce->element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
                                DEBUGVAR(1, 38, "single_lfe_channel_element(): element_instance_tag"));
 
-    *tag = sce.element_instance_tag;
-    sce.channel = channel;
-    sce.paired_channel = -1;
+    *tag = sce->element_instance_tag;
+    sce->channel = channel;
+    sce->paired_channel = -1;
 
-    retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
+    retval = individual_channel_stream(hDecoder, sce, ld, ics, 0, spec_data);
     if (retval > 0) {
         return retval;
     }
@@ -624,7 +626,7 @@
 #endif
 
     /* noiseless coding is done, spectral reconstruction is done now */
-    retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
+    retval = reconstruct_single_channel(hDecoder, ics, sce, spec_data);
     if (retval > 0) {
         return retval;
     }
@@ -638,23 +640,26 @@
 {
     ALIGN int16_t spec_data1[1024] = {0};
     ALIGN int16_t spec_data2[1024] = {0};
-    static element cpe = {0};
-    ic_stream *ics1 = &(cpe.ics1);
-    ic_stream *ics2 = &(cpe.ics2);
+    element *cpe = NULL;
+    ic_stream *ics1 = NULL;
+    ic_stream *ics2 = NULL;
     uint8_t result;
 
-    memset(&cpe, 0, sizeof(cpe));
-    cpe.channel        = channels;
-    cpe.paired_channel = channels + 1;
+    memset(hDecoder->process_buf, 0, hDecoder->process_buf_size);
+    cpe = (element *)hDecoder->process_buf;
+    ics1 = &(cpe->ics1);
+    ics2 = &(cpe->ics2);
+    cpe->channel        = channels;
+    cpe->paired_channel = channels + 1;
 
-    cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
+    cpe->element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
                                DEBUGVAR(1, 39, "channel_pair_element(): element_instance_tag"));
-    *tag = cpe.element_instance_tag;
+    *tag = cpe->element_instance_tag;
 
-    if ((cpe.common_window = faad_get1bit(ld
+    if ((cpe->common_window = faad_get1bit(ld
                                           DEBUGVAR(1, 40, "channel_pair_element(): common_window"))) & 1) {
         /* both channels have common ics information */
-        if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0) {
+        if ((result = ics_info(hDecoder, ics1, ld, cpe->common_window)) > 0) {
             return result;
         }
 
@@ -697,13 +702,13 @@
         ics1->ms_mask_present = 0;
     }
 
-    if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
+    if ((result = individual_channel_stream(hDecoder, cpe, ld, ics1,
                                             0, spec_data1)) > 0) {
         return result;
     }
 
 #ifdef ERROR_RESILIENCE
-    if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
+    if (cpe->common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
         (ics1->predictor_data_present)) {
         if ((
 #ifdef LTP_DEC
@@ -721,7 +726,7 @@
     }
 #endif
 
-    if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
+    if ((result = individual_channel_stream(hDecoder, cpe, ld, ics2,
                                             0, spec_data2)) > 0) {
         return result;
     }
@@ -740,7 +745,7 @@
 #endif
 
     /* noiseless coding is done, spectral reconstruction is done now */
-    if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe,
+    if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, cpe,
                                            spec_data1, spec_data2)) > 0) {
         return result;
     }
@@ -845,7 +850,8 @@
                 if (!common_window && (hDecoder->object_type >= ER_OBJECT_START)) {
                     if ((ics->ltp.data_present = faad_get1bit(ld
                                                  DEBUGVAR(1, 50, "ics_info(): ltp.data_present"))) & 1) {
-                        retval = ltp_data(hDecoder, ics, &(ics->ltp), ld);
+                        /* coverity[CHECKED_RETURN] */
+                        ltp_data(hDecoder, ics, &(ics->ltp), ld);
                     }
                 }
 #endif