[x265] [PATCH 3/3] SEI Memory Leak Fix

Pavan Tarun Chakka Venkata pavan.tarun at multicorewareinc.com
Tue Nov 12 13:20:00 UTC 2024


>From 52d1f689c5bcc8a6b913e55094d0dcc03a7ff809 Mon Sep 17 00:00:00 2001
From: PavanTarun <pavan.tarun at multicorewareinc.com>
Date: Sat, 9 Nov 2024 13:18:18 +0530
Subject: [PATCH 3/3] SEI Memory Leak Fix

---
 source/encoder/encoder.cpp |   5 +-
 source/encoder/sei.cpp     | 102 +++++++++++++++++--------------------
 source/encoder/sei.h       |   2 +-
 3 files changed, 53 insertions(+), 56 deletions(-)

diff --git a/source/encoder/encoder.cpp b/source/encoder/encoder.cpp
index eef7519a1..db6d746e3 100644
--- a/source/encoder/encoder.cpp
+++ b/source/encoder/encoder.cpp
@@ -6127,7 +6127,9 @@ void Encoder::readUserSeiFile(x265_sei_payload&
seiMsg, int curPoc)
         int payloadType = atoi(strtok(NULL, " "));
         char *base64Encode = strtok(NULL, "\n");
         int base64EncodeLength = (int)strlen(base64Encode);
-        char *base64Decode = SEI::base64Decode(base64Encode,
base64EncodeLength);
+        char* decodedString;
+    decodedString = (char*)malloc(sizeof(char) * (base64EncodeLength));
+        char *base64Decode = SEI::base64Decode(base64Encode,
base64EncodeLength, decodedString);
         if (nalType == NAL_UNIT_PREFIX_SEI && (!strcmp(prefix, "PREFIX")))
         {
             int currentPOC = curPoc;
@@ -6150,6 +6152,7 @@ void Encoder::readUserSeiFile(x265_sei_payload&
seiMsg, int curPoc)
                     break;
                 }
                 memcpy(seiMsg.payload, base64Decode, seiMsg.payloadSize);
+                free(decodedString);
                 break;
             }
         }
diff --git a/source/encoder/sei.cpp b/source/encoder/sei.cpp
index 6e4b805e5..85cd7894a 100644
--- a/source/encoder/sei.cpp
+++ b/source/encoder/sei.cpp
@@ -92,60 +92,54 @@ void SEI::setSize(uint32_t size)

 /* charSet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */

-char* SEI::base64Decode(char encodedString[], int base64EncodeLength)
+char* SEI::base64Decode(char encodedString[], int base64EncodeLength,
char* decodedString)
 {
-    char* decodedString;
-    decodedString = (char*)malloc(sizeof(char) * ((base64EncodeLength / 4)
* 3));
-    int i, j, k = 0;
-    // stores the bitstream
-    int bitstream = 0;
-    // countBits stores current number of bits in bitstream
-    int countBits = 0;
-    // selects 4 characters from encodedString at a time. Find the
position of each encoded character in charSet and stores in bitstream
-    for (i = 0; i < base64EncodeLength; i += 4)
-    {
-        bitstream = 0, countBits = 0;
-        for (j = 0; j < 4; j++)
-        {
-            // make space for 6 bits
-            if (encodedString[i + j] != '=')
-            {
-                bitstream = bitstream << 6;
-                countBits += 6;
-            }
-            // Finding the position of each encoded character in charSet
and storing in bitstream, use OR '|' operator to store bits
-
-            if (encodedString[i + j] >= 'A' && encodedString[i + j] <= 'Z')
-                bitstream = bitstream | (encodedString[i + j] - 'A');
-
-            else if (encodedString[i + j] >= 'a' && encodedString[i + j]
<= 'z')
-                bitstream = bitstream | (encodedString[i + j] - 'a' + 26);
-
-            else if (encodedString[i + j] >= '0' && encodedString[i + j]
<= '9')
-                bitstream = bitstream | (encodedString[i + j] - '0' + 52);
-
-            // '+' occurs in 62nd position in charSet
-            else if (encodedString[i + j] == '+')
-                bitstream = bitstream | 62;
-
-            // '/' occurs in 63rd position in charSet
-            else if (encodedString[i + j] == '/')
-                bitstream = bitstream | 63;
-
-            // to delete appended bits during encoding
-            else
-            {
-                bitstream = bitstream >> 2;
-                countBits -= 2;
-            }
-        }
-
-        while (countBits != 0)
-        {
-            countBits -= 8;
-            decodedString[k++] = (bitstream >> countBits) & 255;
-        }
-    }
-    return decodedString;
+ int i, j, k = 0;
+ // stores the bitstream
+ int bitstream = 0;
+ // countBits stores the current number of bits in bitstream
+ int countBits = 0;
+
+ for (i = 0; i < base64EncodeLength; i += 4)
+ {
+ bitstream = 0;
+ countBits = 0;
+
+ for (j = 0; j < 4; j++)
+ {
+ if (encodedString[i + j] != '=')
+ {
+ int value = 0;
+ if (encodedString[i + j] >= 'A' && encodedString[i + j] <= 'Z')
+ value = encodedString[i + j] - 'A';
+ else if (encodedString[i + j] >= 'a' && encodedString[i + j] <= 'z')
+ value = encodedString[i + j] - 'a' + 26;
+ else if (encodedString[i + j] >= '0' && encodedString[i + j] <= '9')
+ value = encodedString[i + j] - '0' + 52;
+ else if (encodedString[i + j] == '+')
+ value = 62;
+ else if (encodedString[i + j] == '/')
+ value = 63;
+ else
+ value = 0;
+
+ bitstream = (bitstream << 6) | value;
+ countBits += 6;
+ }
+ }
+
+ while (countBits >= 8)
+ {
+ countBits -= 8;
+ decodedString[k++] = (bitstream >> countBits) & 0xFF;
+ }
+ }
+
+ if (k < base64EncodeLength)
+ {
+ decodedString[k] = '\0';
+ }
+
+ return decodedString;
 }

diff --git a/source/encoder/sei.h b/source/encoder/sei.h
index a33cb94e9..f02e0c50d 100644
--- a/source/encoder/sei.h
+++ b/source/encoder/sei.h
@@ -40,7 +40,7 @@ public:
     * The writeSEImessages() method calls writeSEI() which encodes the
header */
     void writeSEImessages(Bitstream& bs, const SPS& sps, NalUnitType
nalUnitType, NALList& list, int isNested, int layerId = 0);
     void setSize(uint32_t size);
-    static char* base64Decode(char encodedString[], int
base64EncodeLength);
+    static char* base64Decode(char encodedString[], int
base64EncodeLength, char* base64Decode);
     virtual ~SEI() {}
 protected:
     SEIPayloadType  m_payloadType;
-- 
2.41.0.windows.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20241112/53d1edc0/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0003-SEI-Memory-Leak-Fix.patch
Type: application/octet-stream
Size: 5895 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20241112/53d1edc0/attachment.obj>


More information about the x265-devel mailing list