Upgrade libhevc to v1.4.0

This project was upgraded with external_updater.
Usage: tools/external_updater/updater.sh update external/libhevc
For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md

Bug: 319333298
Test: atest CtsMediaV2TestCases
Change-Id: I0f74377ff76b752c4794abf4397725ae54665f97
diff --git a/METADATA b/METADATA
index 2617cad..1cdacb9 100644
--- a/METADATA
+++ b/METADATA
@@ -1,19 +1,19 @@
 # This project was upgraded with external_updater.
-# Usage: tools/external_updater/updater.sh update libhevc
+# Usage: tools/external_updater/updater.sh update external/libhevc
 # For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md
 
 name: "libhevc"
 description: "Android fork of the libhevc library."
 third_party {
-  url {
-    type: GIT
-    value: "https://github.com/ittiam-systems/libhevc.git"
-  }
-  version: "v1.3.0"
   license_type: NOTICE
   last_upgrade_date {
-    year: 2023
-    month: 10
-    day: 23
+    year: 2024
+    month: 4
+    day: 24
+  }
+  identifier {
+    type: "Git"
+    value: "https://github.com/ittiam-systems/libhevc.git"
+    version: "v1.4.0"
   }
 }
diff --git a/decoder/ihevcd_iquant_itrans_recon_ctb.c b/decoder/ihevcd_iquant_itrans_recon_ctb.c
index 6af3001..c526231 100644
--- a/decoder/ihevcd_iquant_itrans_recon_ctb.c
+++ b/decoder/ihevcd_iquant_itrans_recon_ctb.c
@@ -559,20 +559,22 @@
     UWORD8 *pu1_pic_intra_flag;
     /*************************************************************************/
     /* Contanis scaling matrix offset in the following order in a 1D buffer  */
+    /* Entries that are listed as UNUSED are invalid combinations where      */
+    /* scaling matrix is not used. eg: 64x64 SKIP CU, 64x64 PCM CU           */
     /* Intra 4 x 4 Y, 4 x 4 U, 4 x 4 V                                       */
     /* Inter 4 x 4 Y, 4 x 4 U, 4 x 4 V                                       */
     /* Intra 8 x 8 Y, 8 x 8 U, 8 x 8 V                                       */
     /* Inter 8 x 8 Y, 8 x 8 U, 8 x 8 V                                       */
     /* Intra 16x16 Y, 16x16 U, 16x16 V                                       */
     /* Inter 16x16 Y, 16x16 U, 16x16 V                                       */
-    /* Intra 32x32 Y                                                         */
-    /* Inter 32x32 Y                                                         */
+    /* Intra 32x32 Y, UNUSED,  UNUSED                                        */
+    /* Inter 32x32 Y, UNUSED,  UNUSED                                        */
+    /* UNUSED,        UNUSED,  UNUSED                                        */
+    /* UNUSED,        UNUSED,  UNUSED                                        */
     /*************************************************************************/
-    /* Only first 20 entries are used. Array is extended to avoid out of bound
-       reads. Skip CUs (64x64) read this table, but don't really use the value */
     static const WORD32 scaling_mat_offset[] =
       { 0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992,
-        1248, 1504, 1760, 2016, 3040, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+        1248, 1504, 1760, 2016, 0, 0, 3040, 0, 0, 0, 0, 0, 0, 0, 0};
 
     PROFILE_DISABLE_IQ_IT_RECON_INTRA_PRED();
 
@@ -806,10 +808,7 @@
 
                     /* Calculating scaling matrix offset */
                     offset = log2_y_trans_size_minus_2 * 6
-                                    + (!intra_flag)
-                                    * ((log2_y_trans_size_minus_2
-                                                    == 3) ? 1 : 3)
-                                    + c_idx;
+                                    + (!intra_flag) * 3 + c_idx;
                     pi2_dequant_matrix = pi2_scaling_mat
                                     + scaling_mat_offset[offset];
 
diff --git a/decoder/ihevcd_parse_headers.c b/decoder/ihevcd_parse_headers.c
index 0ce1b27..e50272c 100644
--- a/decoder/ihevcd_parse_headers.c
+++ b/decoder/ihevcd_parse_headers.c
@@ -1844,6 +1844,21 @@
                                           &ps_sps->s_vui_parameters,
                                           ps_sps->i1_sps_max_sub_layers - 1);
         RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
+
+        if (0 != ps_codec->u4_allocate_dynamic_done) {
+
+            vui_t *ps_vui = &ps_sps->s_vui_parameters;
+            sps_t *ps_sps_old = ps_codec->s_parse.ps_sps;
+            vui_t *ps_vui_old = &ps_sps_old->s_vui_parameters;
+
+            if (ps_vui->u1_video_full_range_flag != ps_vui_old->u1_video_full_range_flag ||
+                ps_vui->u1_colour_primaries != ps_vui_old->u1_colour_primaries ||
+                ps_vui->u1_transfer_characteristics != ps_vui_old->u1_transfer_characteristics ||
+                ps_vui->u1_matrix_coefficients != ps_vui_old->u1_matrix_coefficients) {
+                ps_codec->i4_reset_flag = 1;
+                return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
+            }
+        }
     }
 
     BITS_PARSE("sps_extension_flag", value, ps_bitstrm, 1);
@@ -1893,7 +1908,10 @@
         return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
     }
 
+    // Ensure both i2_pic_width_in_luma_samples and i2_pic_height_in_luma_samples do
+    // not exceed MAX_WD and their product doesn't exceed MAX_WD * MAX_HT
     if((ps_sps->i2_pic_width_in_luma_samples > MAX_WD) ||
+                    (ps_sps->i2_pic_height_in_luma_samples > MAX_WD) ||
                     ((ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples) >
                     (MAX_WD * MAX_HT)))
     {
diff --git a/decoder/ihevcd_parse_slice_header.c b/decoder/ihevcd_parse_slice_header.c
index 9742456..0fedeb0 100644
--- a/decoder/ihevcd_parse_slice_header.c
+++ b/decoder/ihevcd_parse_slice_header.c
@@ -808,11 +808,12 @@
                 if(ps_codec->i4_pic_present)
                 {
                     prev_slice_incomplete_flag = 1;
+                    ps_codec->i4_slice_error = 1;
+                    ps_codec->s_parse.i4_cur_slice_idx--;
+                    if(ps_codec->s_parse.i4_cur_slice_idx < 0)
+                        ps_codec->s_parse.i4_cur_slice_idx = 0;
                 }
-                else
-                {
-                    return IHEVCD_IGNORE_SLICE;
-                }
+                return IHEVCD_IGNORE_SLICE;
             }
             /* If the slice address is less than the next CTB's index,
              * extra CTBs have been decoded in the previous slice.
diff --git a/encoder/ihevce_decomp_pre_intra_pass.c b/encoder/ihevce_decomp_pre_intra_pass.c
index 065fcbb..e272f82 100644
--- a/encoder/ihevce_decomp_pre_intra_pass.c
+++ b/encoder/ihevce_decomp_pre_intra_pass.c
@@ -859,7 +859,7 @@
     *pi4_act_factor = (1 << QP_LEVEL_MOD_ACT_FACTOR);
     if(cu_satd != -1 && (WORD32)frm_avg_activity != 0)
     {
-        ULWORD64 sq_cur_satd = (cu_satd * cu_satd);
+        ULWORD64 sq_cur_satd = ((ULWORD64)cu_satd * (ULWORD64)cu_satd);
         float log2_sq_cur_satd = fast_log2(1 + sq_cur_satd);
         WORD32 qp_offset = f_mod_strength * (log2_sq_cur_satd - frm_avg_activity);