changeset 12494:2abd2a1909a4 draft

zone: Fix memory handling for zones This patch does the following: Allocate and free memory for zones Copy of zone and zone params
author Pooja Venkatesan <pooja@multicorewareinc.com>
date Tue, 05 Mar 2019 14:18:21 +0530
parents 31ab7e09a3b5
children 74a8672ea4f7
files source/CMakeLists.txt source/common/param.cpp source/encoder/api.cpp source/encoder/encoder.cpp source/x265.h
diffstat 5 files changed, 60 insertions(+-), 7 deletions(-) [+]
line wrap: on
line diff
--- a/source/CMakeLists.txt	Tue Feb 26 13:52:24 2019 +0530
+++ b/source/CMakeLists.txt	Tue Mar 05 14:18:21 2019 +0530
@@ -29,7 +29,7 @@ option(NATIVE_BUILD "Target the build CP
 option(STATIC_LINK_CRT "Statically link C runtime for release builds" OFF)
 mark_as_advanced(FPROFILE_USE FPROFILE_GENERATE NATIVE_BUILD)
 # X265_BUILD must be incremented each time the public API is changed
-set(X265_BUILD 170)
+set(X265_BUILD 171)
 configure_file("${PROJECT_SOURCE_DIR}/x265.def.in"
                "${PROJECT_BINARY_DIR}/x265.def")
 configure_file("${PROJECT_SOURCE_DIR}/x265_config.h.in"
--- a/source/common/param.cpp	Tue Feb 26 13:52:24 2019 +0530
+++ b/source/common/param.cpp	Tue Mar 05 14:18:21 2019 +0530
@@ -102,6 +102,7 @@ x265_param *x265_param_alloc()
 
 void x265_param_free(x265_param* p)
 {
+    x265_zone_free(p);
 #ifdef SVT_HEVC
      x265_free(p->svtHevcParam);
 #endif
@@ -2244,13 +2245,24 @@ void x265_copy_params(x265_param* dst, x
     dst->rc.zoneCount = src->rc.zoneCount;
     dst->rc.zonefileCount = src->rc.zonefileCount;
 
-    if (src->rc.zones)
+    if (src->rc.zonefileCount && src->rc.zones)
     {
-        dst->rc.zones->startFrame = src->rc.zones->startFrame;
-        dst->rc.zones->endFrame = src->rc.zones->endFrame;
-        dst->rc.zones->bForceQp = src->rc.zones->bForceQp;
-        dst->rc.zones->qp = src->rc.zones->qp;
-        dst->rc.zones->bitrateFactor = src->rc.zones->bitrateFactor;
+        for (int i = 0; i < src->rc.zonefileCount; i++)
+        {
+            dst->rc.zones[i].startFrame = src->rc.zones[i].startFrame;
+            memcpy(dst->rc.zones[i].zoneParam, src->rc.zones[i].zoneParam, sizeof(x265_param));
+        }
+    }
+    else if (src->rc.zoneCount && src->rc.zones)
+    {
+        for (int i = 0; i < src->rc.zoneCount; i++)
+        {
+            dst->rc.zones[i].startFrame = src->rc.zones[i].startFrame;
+            dst->rc.zones[i].endFrame = src->rc.zones[i].endFrame;
+            dst->rc.zones[i].bForceQp = src->rc.zones[i].bForceQp;
+            dst->rc.zones[i].qp = src->rc.zones[i].qp;
+            dst->rc.zones[i].bitrateFactor = src->rc.zones[i].bitrateFactor;
+        }
     }
     else
         dst->rc.zones = NULL;
--- a/source/encoder/api.cpp	Tue Feb 26 13:52:24 2019 +0530
+++ b/source/encoder/api.cpp	Tue Mar 05 14:18:21 2019 +0530
@@ -98,6 +98,12 @@ x265_encoder *x265_encoder_open(x265_par
     x265_param* zoneParam = PARAM_NS::x265_param_alloc();
     if (!param || !latestParam)
         goto fail;
+    if (p->rc.zoneCount || p->rc.zonefileCount)
+    {
+        int zoneCount = p->rc.zonefileCount ? p->rc.zonefileCount : p->rc.zoneCount;
+        param->rc.zones = x265_zone_alloc(zoneCount, p->rc.zonefileCount ? true : false);
+        latestParam->rc.zones = x265_zone_alloc(zoneCount, p->rc.zonefileCount ? true : false);
+    }
 
     x265_copy_params(param, p);
     x265_log(param, X265_LOG_INFO, "HEVC encoder version %s\n", PFX(version_str));
@@ -287,6 +293,11 @@ int x265_encoder_reconfig(x265_encoder* 
     bool isReconfigureRc = encoder->isReconfigureRc(encoder->m_latestParam, param_in);
     if ((encoder->m_reconfigure && !isReconfigureRc) || (encoder->m_reconfigureRc && isReconfigureRc)) /* Reconfigure in progress */
         return 1;
+    if (encoder->m_latestParam->rc.zoneCount || encoder->m_latestParam->rc.zonefileCount)
+    {
+        int zoneCount = encoder->m_latestParam->rc.zonefileCount ? encoder->m_latestParam->rc.zonefileCount : encoder->m_latestParam->rc.zoneCount;
+        save.rc.zones = x265_zone_alloc(zoneCount, encoder->m_latestParam->rc.zonefileCount ? true : false);
+    }
     x265_copy_params(&save, encoder->m_latestParam);
     int ret = encoder->reconfigureParam(encoder->m_latestParam, param_in);
     if (ret)
@@ -922,6 +933,26 @@ void x265_picture_free(x265_picture *p)
     return x265_free(p);
 }
 
+x265_zone *x265_zone_alloc(int zoneCount, bool isZoneFile)
+{
+    x265_zone* zone = (x265_zone*)x265_malloc(sizeof(x265_zone) * zoneCount);
+    if (isZoneFile) {
+        for (int i = 0; i < zoneCount; i++)
+            zone[i].zoneParam = (x265_param*)x265_malloc(sizeof(x265_param));
+    }
+    return zone;
+}
+
+void x265_zone_free(x265_param *param)
+{
+    if (param->rc.zonefileCount) {
+        for (int i = 0; i < param->rc.zonefileCount; i++)
+            x265_free(param->rc.zones[i].zoneParam);
+    }
+    if (param->rc.zoneCount || param->rc.zonefileCount)
+        x265_free(param->rc.zones);
+}
+
 static const x265_api libapi =
 {
     X265_MAJOR_VERSION,
--- a/source/encoder/encoder.cpp	Tue Feb 26 13:52:24 2019 +0530
+++ b/source/encoder/encoder.cpp	Tue Mar 05 14:18:21 2019 +0530
@@ -3356,6 +3356,12 @@ void Encoder::configure(x265_param *p)
     if (p->dolbyProfile)     // Default disabled.
         configureDolbyVisionParams(p);
 
+    if (p->rc.zonefileCount && p->rc.zoneCount)
+    {
+        p->rc.zoneCount = 0;
+        x265_log(p, X265_LOG_WARNING, "Only zone or zonefile can be used. Enabling only zonefile\n");
+    }
+
     if (m_param->rc.zonefileCount && p->bOpenGOP)
     {
         p->bOpenGOP = 0;
--- a/source/x265.h	Tue Feb 26 13:52:24 2019 +0530
+++ b/source/x265.h	Tue Mar 05 14:18:21 2019 +0530
@@ -1798,6 +1798,10 @@ void x265_param_default(x265_param *para
 #define X265_PARAM_BAD_VALUE (-2)
 int x265_param_parse(x265_param *p, const char *name, const char *value);
 
+x265_zone *x265_zone_alloc(int zoneCount, bool isZoneFile);
+
+void x265_zone_free(x265_param *param);
+
 int x265_zone_param_parse(x265_param* p, const char* name, const char* value);
 
 static const char * const x265_profile_names[] = {