<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif">From 9a17fa8147c89324633ae9746db2ae0a51babd5e Mon Sep 17 00:00:00 2001<br>From: PavanTarun <<a href="mailto:pavan.tarun@multicorewareinc.com">pavan.tarun@multicorewareinc.com</a>><br>Date: Sat, 9 Nov 2024 13:18:18 +0530<br>Subject: [PATCH 3/3] SEI Memory Leak Fix<br><br>---<br> source/encoder/encoder.cpp |   5 +-<br> source/encoder/sei.cpp     | 102 +++++++++++++++++--------------------<br> source/encoder/sei.h       |   2 +-<br> 3 files changed, 53 insertions(+), 56 deletions(-)<br><br>diff --git a/source/encoder/encoder.cpp b/source/encoder/encoder.cpp<br>index d1d12e91c..69f51cda9 100644<br>--- a/source/encoder/encoder.cpp<br>+++ b/source/encoder/encoder.cpp<br>@@ -6235,7 +6235,9 @@ void Encoder::readUserSeiFile(x265_sei_payload& seiMsg, int curPoc)<br>         int payloadType = atoi(strtok(NULL, " "));<br>         char *base64Encode = strtok(NULL, "\n");<br>         int base64EncodeLength = (int)strlen(base64Encode);<br>-        char *base64Decode = SEI::base64Decode(base64Encode, base64EncodeLength);<br>+        char* decodedString;<br>+            decodedString = (char*)malloc(sizeof(char) * (base64EncodeLength));<br>+        char *base64Decode = SEI::base64Decode(base64Encode, base64EncodeLength, decodedString);<br>         if (nalType == NAL_UNIT_PREFIX_SEI && (!strcmp(prefix, "PREFIX")))<br>         {<br>             int currentPOC = curPoc;<br>@@ -6258,6 +6260,7 @@ void Encoder::readUserSeiFile(x265_sei_payload& seiMsg, int curPoc)<br>                     break;<br>                 }<br>                 memcpy(seiMsg.payload, base64Decode, seiMsg.payloadSize);<br>+                free(decodedString);<br>                 break;<br>             }<br>         }<br>diff --git a/source/encoder/sei.cpp b/source/encoder/sei.cpp<br>index 6e4b805e5..85cd7894a 100644<br>--- a/source/encoder/sei.cpp<br>+++ b/source/encoder/sei.cpp<br>@@ -92,60 +92,54 @@ void SEI::setSize(uint32_t size)<br> <br> /* charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */<br> <br>-char* SEI::base64Decode(char encodedString[], int base64EncodeLength)<br>+char* SEI::base64Decode(char encodedString[], int base64EncodeLength, char* decodedString)<br> {<br>-    char* decodedString;<br>-    decodedString = (char*)malloc(sizeof(char) * ((base64EncodeLength / 4) * 3));<br>-    int i, j, k = 0;<br>-    // stores the bitstream<br>-    int bitstream = 0;<br>-    // countBits stores current number of bits in bitstream<br>-    int countBits = 0;<br>-    // selects 4 characters from encodedString at a time. Find the position of each encoded character in charSet and stores in bitstream<br>-    for (i = 0; i < base64EncodeLength; i += 4)<br>-    {<br>-        bitstream = 0, countBits = 0;<br>-        for (j = 0; j < 4; j++)<br>-        {<br>-            // make space for 6 bits<br>-            if (encodedString[i + j] != '=')<br>-            {<br>-                bitstream = bitstream << 6;<br>-                countBits += 6;<br>-            }<br>-            // Finding the position of each encoded character in charSet and storing in bitstream, use OR '|' operator to store bits<br>-<br>-            if (encodedString[i + j] >= 'A' && encodedString[i + j] <= 'Z')<br>-                bitstream = bitstream | (encodedString[i + j] - 'A');<br>-<br>-            else if (encodedString[i + j] >= 'a' && encodedString[i + j] <= 'z')<br>-                bitstream = bitstream | (encodedString[i + j] - 'a' + 26);<br>-            <br>-            else if (encodedString[i + j] >= '0' && encodedString[i + j] <= '9')<br>-                bitstream = bitstream | (encodedString[i + j] - '0' + 52);<br>-            <br>-            // '+' occurs in 62nd position in charSet<br>-            else if (encodedString[i + j] == '+')<br>-                bitstream = bitstream | 62;<br>-            <br>-            // '/' occurs in 63rd position in charSet<br>-            else if (encodedString[i + j] == '/')<br>-                bitstream = bitstream | 63;<br>-            <br>-            // to delete appended bits during encoding<br>-            else<br>-            {<br>-                bitstream = bitstream >> 2;<br>-                countBits -= 2;<br>-            }<br>-        }<br>-    <br>-        while (countBits != 0)<br>-        {<br>-            countBits -= 8;<br>-            decodedString[k++] = (bitstream >> countBits) & 255;<br>-        }<br>-    }<br>-    return decodedString;<br>+      int i, j, k = 0;<br>+     // stores the bitstream<br>+      int bitstream = 0;<br>+   // countBits stores the current number of bits in bitstream<br>+  int countBits = 0;<br>+<br>+        for (i = 0; i < base64EncodeLength; i += 4)<br>+       {<br>+            bitstream = 0;<br>+               countBits = 0;<br>+<br>+            for (j = 0; j < 4; j++)<br>+           {<br>+                    if (encodedString[i + j] != '=')<br>+                     {<br>+                            int value = 0;<br>+                               if (encodedString[i + j] >= 'A' && encodedString[i + j] <= 'Z')<br>+                                        value = encodedString[i + j] - 'A';<br>+                          else if (encodedString[i + j] >= 'a' && encodedString[i + j] <= 'z')<br>+                                   value = encodedString[i + j] - 'a' + 26;<br>+                             else if (encodedString[i + j] >= '0' && encodedString[i + j] <= '9')<br>+                                   value = encodedString[i + j] - '0' + 52;<br>+                             else if (encodedString[i + j] == '+')<br>+                                        value = 62;<br>+                          else if (encodedString[i + j] == '/')<br>+                                        value = 63;<br>+                          else<br>+                                 value = 0;<br>+<br>+                                bitstream = (bitstream << 6) | value;<br>+                          countBits += 6;<br>+                      }<br>+            }<br>+<br>+         while (countBits >= 8)<br>+            {<br>+                    countBits -= 8;<br>+                      decodedString[k++] = (bitstream >> countBits) & 0xFF;<br>+              }<br>+    }<br>+<br>+ if (k < base64EncodeLength)<br>+       {<br>+            decodedString[k] = '\0';<br>+     }<br>+<br>+ return decodedString;<br> }<br> <br>diff --git a/source/encoder/sei.h b/source/encoder/sei.h<br>index a33cb94e9..f02e0c50d 100644<br>--- a/source/encoder/sei.h<br>+++ b/source/encoder/sei.h<br>@@ -40,7 +40,7 @@ public:<br>     * The writeSEImessages() method calls writeSEI() which encodes the header */<br>     void writeSEImessages(Bitstream& bs, const SPS& sps, NalUnitType nalUnitType, NALList& list, int isNested, int layerId = 0);<br>     void setSize(uint32_t size);<br>-    static char* base64Decode(char encodedString[], int base64EncodeLength);<br>+    static char* base64Decode(char encodedString[], int base64EncodeLength, char* base64Decode);<br>     virtual ~SEI() {}<br> protected:<br>     SEIPayloadType  m_payloadType;<br>-- <br>2.41.0.windows.1<br><br></div></div>