<div dir="ltr">From 069e70e0448b2de42dcbd85386b4abbd40d5221f Mon Sep 17 00:00:00 2001<br>From: Mahesh <<a href="mailto:mahesh@multicorewareinc.com">mahesh@multicorewareinc.com</a>><br>Date: Fri, 19 Jan 2024 10:13:07 +0530<br>Subject: [PATCH] Multilib recon fix<br><br>---<br> source/output/output.cpp |  6 ++--<br> source/output/output.h   |  2 +-<br> source/output/y4m.cpp    | 58 +++++++++++++++++-----------------<br> source/output/y4m.h      |  4 ++-<br> source/output/yuv.cpp    | 81 +++++++++++++++++++++++++-----------------------<br> source/output/yuv.h      |  4 ++-<br> source/x265cli.cpp       |  2 +-<br> 7 files changed, 82 insertions(+), 75 deletions(-)<br><br>diff --git a/source/output/output.cpp b/source/output/output.cpp<br>index 35ed845..c2327b6 100644<br>--- a/source/output/output.cpp<br>+++ b/source/output/output.cpp<br>@@ -30,14 +30,14 @@<br> <br> using namespace X265_NS;<br> <br>-ReconFile* ReconFile::open(const char *fname, int width, int height, uint32_t bitdepth, uint32_t fpsNum, uint32_t fpsDenom, int csp)<br>+ReconFile* ReconFile::open(const char *fname, int width, int height, uint32_t bitdepth, uint32_t fpsNum, uint32_t fpsDenom, int csp, int sourceBitDepth)<br> {<br>     const char * s = strrchr(fname, '.');<br> <br>     if (s && !strcmp(s, ".y4m"))<br>-        return new Y4MOutput(fname, width, height, fpsNum, fpsDenom, csp);<br>+        return new Y4MOutput(fname, width, height, fpsNum, fpsDenom, csp, sourceBitDepth);<br>     else<br>-        return new YUVOutput(fname, width, height, bitdepth, csp);<br>+        return new YUVOutput(fname, width, height, bitdepth, csp, sourceBitDepth);<br> }<br> <br> OutputFile* OutputFile::open(const char *fname, InputFileInfo& inputInfo)<br>diff --git a/source/output/output.h b/source/output/output.h<br>index ac61d8a..6fee4f2 100644<br>--- a/source/output/output.h<br>+++ b/source/output/output.h<br>@@ -42,7 +42,7 @@ public:<br>     ReconFile()           {}<br> <br>     static ReconFile* open(const char *fname, int width, int height, uint32_t bitdepth,<br>-                           uint32_t fpsNum, uint32_t fpsDenom, int csp);<br>+                           uint32_t fpsNum, uint32_t fpsDenom, int csp, int sourceBitDepth);<br> <br>     virtual bool isFail() const = 0;<br> <br>diff --git a/source/output/y4m.cpp b/source/output/y4m.cpp<br>index c1fd3a6..0ebd91c 100644<br>--- a/source/output/y4m.cpp<br>+++ b/source/output/y4m.cpp<br>@@ -28,11 +28,12 @@<br> using namespace X265_NS;<br> using namespace std;<br> <br>-Y4MOutput::Y4MOutput(const char *filename, int w, int h, uint32_t fpsNum, uint32_t fpsDenom, int csp)<br>+Y4MOutput::Y4MOutput(const char *filename, int w, int h, uint32_t fpsNum, uint32_t fpsDenom, int csp, int inputdepth)<br>     : width(w)<br>     , height(h)<br>     , colorSpace(csp)<br>     , frameSize(0)<br>+    , inputDepth(inputdepth)<br> {<br>     ofs.open(filename, ios::binary | ios::out);<br>     buf = new char[width];<br>@@ -72,38 +73,37 @@ bool Y4MOutput::writePicture(const x265_picture& pic)<br> <br>     X265_CHECK(pic.colorSpace == colorSpace, "invalid chroma subsampling\n");<br> <br>-#if HIGH_BIT_DEPTH<br>-<br>-    // encoder gave us short pixels, downshift, then write<br>-    X265_CHECK(pic.bitDepth > 8, "invalid bit depth\n");<br>-    int shift = pic.bitDepth - 8;<br>-    for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>+    if (inputDepth > 8)<br>     {<br>-        uint16_t *src = (uint16_t*)pic.planes[i];<br>-        for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>-        {<br>-            for (int w = 0; w < width >> x265_cli_csps[colorSpace].width[i]; w++)<br>-                buf[w] = (char)(src[w] >> shift);<br>-<br>-            ofs.write(buf, width >> x265_cli_csps[colorSpace].width[i]);<br>-            src += pic.stride[i] / sizeof(*src);<br>-        }<br>+      // encoder gave us short pixels, downshift, then write<br>+       X265_CHECK(pic.bitDepth > 8, "invalid bit depth\n");<br>+    int shift = pic.bitDepth - 8;<br>+        for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>+        {<br>+            uint16_t *src = (uint16_t*)pic.planes[i];<br>+            for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>+             {<br>+                    for (int w = 0; w < width >> x265_cli_csps[colorSpace].width[i]; w++)<br>+                               buf[w] = (char)(src[w] >> shift);<br>+<br>+                   ofs.write(buf, width >> x265_cli_csps[colorSpace].width[i]);<br>+                   src += pic.stride[i] / sizeof(*src);<br>+         }<br>+    }       <br>     }<br>-<br>-#else // if HIGH_BIT_DEPTH<br>-<br>-    X265_CHECK(pic.bitDepth == 8, "invalid bit depth\n");<br>-    for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>+    else<br>     {<br>-        char *src = (char*)pic.planes[i];<br>-        for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>-        {<br>-            ofs.write(src, width >> x265_cli_csps[colorSpace].width[i]);<br>-            src += pic.stride[i] / sizeof(*src);<br>-        }<br>+  X265_CHECK(pic.bitDepth == 8, "invalid bit depth\n");<br>+      for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>+        {<br>+            char *src = (char*)pic.planes[i];<br>+            for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>+             {<br>+                    ofs.write(src, width >> x265_cli_csps[colorSpace].width[i]);<br>+                   src += pic.stride[i] / sizeof(*src);<br>+         }<br>+    }<br>     }<br> <br>-#endif // if HIGH_BIT_DEPTH<br>-<br>     return true;<br> }<br>diff --git a/source/output/y4m.h b/source/output/y4m.h<br>index f939a9b..f00175b 100644<br>--- a/source/output/y4m.h<br>+++ b/source/output/y4m.h<br>@@ -42,6 +42,8 @@ protected:<br> <br>     uint32_t frameSize;<br> <br>+    int inputDepth;<br>+<br>     std::ofstream ofs;<br> <br>     std::ofstream::pos_type header;<br>@@ -52,7 +54,7 @@ protected:<br> <br> public:<br> <br>-    Y4MOutput(const char *filename, int width, int height, uint32_t fpsNum, uint32_t fpsDenom, int csp);<br>+    Y4MOutput(const char *filename, int width, int height, uint32_t fpsNum, uint32_t fpsDenom, int csp, int inputDepth);<br> <br>     virtual ~Y4MOutput();<br> <br>diff --git a/source/output/yuv.cpp b/source/output/yuv.cpp<br>index 366d5ce..b078ddf 100644<br>--- a/source/output/yuv.cpp<br>+++ b/source/output/yuv.cpp<br>@@ -28,12 +28,13 @@<br> using namespace X265_NS;<br> using namespace std;<br> <br>-YUVOutput::YUVOutput(const char *filename, int w, int h, uint32_t d, int csp)<br>+YUVOutput::YUVOutput(const char *filename, int w, int h, uint32_t d, int csp, int inputdepth)<br>     : width(w)<br>     , height(h)<br>     , depth(d)<br>     , colorSpace(csp)<br>     , frameSize(0)<br>+    , inputDepth(inputdepth)<br> {<br>     ofs.open(filename, ios::binary | ios::out);<br>     buf = new char[width];<br>@@ -56,50 +57,52 @@ bool YUVOutput::writePicture(const x265_picture& pic)<br>     X265_CHECK(pic.colorSpace == colorSpace, "invalid chroma subsampling\n");<br>     X265_CHECK(pic.bitDepth == (int)depth, "invalid bit depth\n");<br> <br>-#if HIGH_BIT_DEPTH<br>-    if (depth == 8)<br>+    if (inputDepth > 8)<br>     {<br>-        int shift = pic.bitDepth - 8;<br>-        ofs.seekp((std::streamoff)fileOffset);<br>-        for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>-        {<br>-            uint16_t *src = (uint16_t*)pic.planes[i];<br>-            for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>-            {<br>-                for (int w = 0; w < width >> x265_cli_csps[colorSpace].width[i]; w++)<br>-                    buf[w] = (char)(src[w] >> shift);<br>+      if (depth == 8)<br>+      {<br>+            int shift = pic.bitDepth - 8;<br>+                ofs.seekp((std::streamoff)fileOffset);<br>+               for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>+                {<br>+                    uint16_t *src = (uint16_t*)pic.planes[i];<br>+                    for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>+                     {<br>+                            for (int w = 0; w < width >> x265_cli_csps[colorSpace].width[i]; w++)<br>+                                       buf[w] = (char)(src[w] >> shift);<br> <br>-                ofs.write(buf, width >> x265_cli_csps[colorSpace].width[i]);<br>-                src += pic.stride[i] / sizeof(*src);<br>-            }<br>-        }<br>+                                ofs.write(buf, width >> x265_cli_csps[colorSpace].width[i]);<br>+                           src += pic.stride[i] / sizeof(*src);<br>+                 }<br>+            }<br>+    }<br>+    else<br>+ {<br>+            ofs.seekp((std::streamoff)(fileOffset * 2));<br>+         for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>+                {<br>+                    uint16_t *src = (uint16_t*)pic.planes[i];<br>+                    for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>+                     {<br>+                            ofs.write((const char*)src, (width * 2) >> x265_cli_csps[colorSpace].width[i]);<br>+                                src += pic.stride[i] / sizeof(*src);<br>+                 }<br>+            }<br>+    }<br>     }<br>     else<br>     {<br>-        ofs.seekp((std::streamoff)(fileOffset * 2));<br>-        for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>-        {<br>-            uint16_t *src = (uint16_t*)pic.planes[i];<br>-            for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>-            {<br>-                ofs.write((const char*)src, (width * 2) >> x265_cli_csps[colorSpace].width[i]);<br>-                src += pic.stride[i] / sizeof(*src);<br>-            }<br>-        }<br>+   ofs.seekp((std::streamoff)fileOffset);<br>+       for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>+        {<br>+            char *src = (char*)pic.planes[i];<br>+            for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>+             {<br>+                    ofs.write(src, width >> x265_cli_csps[colorSpace].width[i]);<br>+                   src += pic.stride[i] / sizeof(*src);<br>+         }<br>+    }<br>     }<br>-#else // if HIGH_BIT_DEPTH<br>-    ofs.seekp((std::streamoff)fileOffset);<br>-    for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)<br>-    {<br>-        char *src = (char*)pic.planes[i];<br>-        for (int h = 0; h < height >> x265_cli_csps[colorSpace].height[i]; h++)<br>-        {<br>-            ofs.write(src, width >> x265_cli_csps[colorSpace].width[i]);<br>-            src += pic.stride[i] / sizeof(*src);<br>-        }<br>-    }<br>-<br>-#endif // if HIGH_BIT_DEPTH<br> <br>     return true;<br> }<br>diff --git a/source/output/yuv.h b/source/output/yuv.h<br>index 6b01b4e..c70c480 100644<br>--- a/source/output/yuv.h<br>+++ b/source/output/yuv.h<br>@@ -46,13 +46,15 @@ protected:<br> <br>     uint32_t frameSize;<br> <br>+    int inputDepth;<br>+<br>     char *buf;<br> <br>     std::ofstream ofs;<br> <br> public:<br> <br>-    YUVOutput(const char *filename, int width, int height, uint32_t bitdepth, int csp);<br>+    YUVOutput(const char *filename, int width, int height, uint32_t bitdepth, int csp, int inputDepth);<br> <br>     virtual ~YUVOutput();<br> <br>diff --git a/source/x265cli.cpp b/source/x265cli.cpp<br>index cbb3be5..e6ac9bb 100755<br>--- a/source/x265cli.cpp<br>+++ b/source/x265cli.cpp<br>@@ -917,7 +917,7 @@ namespace X265_NS {<br>             if (reconFileBitDepth == 0)<br>                 reconFileBitDepth = param->internalBitDepth;<br>             this->recon = ReconFile::open(reconfn, param->sourceWidth, param->sourceHeight, reconFileBitDepth,<br>-                param->fpsNum, param->fpsDenom, param->internalCsp);<br>+                param->fpsNum, param->fpsDenom, param->internalCsp, param->sourceBitDepth);<br>             if (this->recon->isFail())<br>             {<br>                 x265_log(param, X265_LOG_WARNING, "unable to write reconstructed outputs file\n");<br>-- <br>1.8.3.1<br><br></div>