<div dir="ltr">From 22a33a4d48d5f2134e21ac1cae4e97e282b18f5d Mon Sep 17 00:00:00 2001<br>From: Niranjan <<a href="mailto:niranjan@multicorewareinc.com">niranjan@multicorewareinc.com</a>><br>Date: Tue, 16 Mar 2021 23:41:35 +0530<br>Subject: [PATCH] Add: End Of Bitstream and End Of Sequence NAL units<br><br>---<br> doc/reST/cli.rst                 |  9 +++++++++<br> source/CMakeLists.txt            |  2 +-<br> source/abrEncApp.cpp             | 12 ++++++++++++<br> source/common/param.cpp          |  8 ++++++++<br> source/encoder/api.cpp           | 11 +++++++++++<br> source/encoder/encoder.cpp       | 13 +++++++++++++<br> source/encoder/encoder.h         |  2 ++<br> source/encoder/frameencoder.cpp  |  6 ++++++<br> source/test/regression-tests.txt |  1 +<br> source/x265.h                    |  9 +++++++++<br> source/x265cli.cpp               |  2 ++<br> source/x265cli.h                 |  4 ++++<br> 12 files changed, 78 insertions(+), 1 deletion(-)<br><br>diff --git a/doc/reST/cli.rst b/doc/reST/cli.rst<br>index 1bb90175b..fe5dd6a7b 100755<br>--- a/doc/reST/cli.rst<br>+++ b/doc/reST/cli.rst<br>@@ -2488,6 +2488,15 @@ Bitstream options<br>        the very first AUD will be skipped since it cannot be placed at the<br>  start of the access unit, where it belongs. Default disabled<br> <br>+.. option:: --eob, --no-eob<br>+<br>+    Emit an end of bitstream NAL unit at the end of the bitstream.<br>+<br>+.. option:: --eos, --no-eos<br>+<br>+   Emit an end of sequence NAL unit at the end of every coded <br>+  video sequence.<br>+<br> .. option:: --hrd, --no-hrd<br> <br>        Enable the signaling of HRD parameters to the decoder. The HRD<br>diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt<br>index a407271b4..b4e57b592 100755<br>--- a/source/CMakeLists.txt<br>+++ b/source/CMakeLists.txt<br>@@ -29,7 +29,7 @@ option(NATIVE_BUILD "Target the build CPU" OFF)<br> option(STATIC_LINK_CRT "Statically link C runtime for release builds" OFF)<br> mark_as_advanced(FPROFILE_USE FPROFILE_GENERATE NATIVE_BUILD)<br> # X265_BUILD must be incremented each time the public API is changed<br>-set(X265_BUILD 199)<br>+set(X265_BUILD 200)<br> configure_file("${PROJECT_SOURCE_DIR}/<a href="http://x265.def.in">x265.def.in</a>"<br>                "${PROJECT_BINARY_DIR}/x265.def")<br> configure_file("${PROJECT_SOURCE_DIR}/<a href="http://x265_config.h.in">x265_config.h.in</a>"<br>diff --git a/source/abrEncApp.cpp b/source/abrEncApp.cpp<br>index fa62ebf63..b637908b9 100644<br>--- a/source/abrEncApp.cpp<br>+++ b/source/abrEncApp.cpp<br>@@ -795,6 +795,18 @@ ret:<br>                     break;<br>             }<br> <br>+            if ((m_param->bEnableEndOfBitstream || m_param->bEnableEndOfSequence) && outFrameCount == (uint32_t)m_param->totalFrames)<br>+            {<br>+                if (api->encoder_end_nal_units(m_encoder, &p_nal, &nal) < 0)<br>+                {<br>+                    x265_log(NULL, X265_LOG_ERROR, "Failure generating end nal units\n");<br>+                    m_ret = 3;<br>+                    goto fail;<br>+                }<br>+                else<br>+                    m_cliopt.totalbytes += m_cliopt.output->writeHeaders(p_nal, nal);<br>+            }<br>+<br>             if (bDolbyVisionRPU)<br>             {<br>                 if (fgetc(m_cliopt.dolbyVisionRpu) != EOF)<br>diff --git a/source/common/param.cpp b/source/common/param.cpp<br>index 90b80c8a4..11a3ddc96 100755<br>--- a/source/common/param.cpp<br>+++ b/source/common/param.cpp<br>@@ -145,6 +145,8 @@ void x265_param_default(x265_param* param)<br>     param->bAnnexB = 1;<br>     param->bRepeatHeaders = 0;<br>     param->bEnableAccessUnitDelimiters = 0;<br>+    param->bEnableEndOfBitstream = 0;<br>+    param->bEnableEndOfSequence = 0;<br>     param->bEmitHRDSEI = 0;<br>     param->bEmitInfoSEI = 1;<br>     param->bEmitHDRSEI = 0; /*Deprecated*/<br>@@ -1383,6 +1385,8 @@ int x265_param_parse(x265_param* p, const char* name, const char* value)<br>         OPT("min-vbv-fullness") p->minVbvFullness = atof(value);<br>         OPT("max-vbv-fullness") p->maxVbvFullness = atof(value);<br>         OPT("video-signal-type-preset") p->videoSignalTypePreset = strdup(value);<br>+        OPT("eob") p->bEnableEndOfBitstream = atobool(value);<br>+        OPT("eos") p->bEnableEndOfSequence = atobool(value);<br>         else<br>             return X265_PARAM_BAD_NAME;<br>     }<br>@@ -2056,6 +2060,8 @@ char *x265_param2string(x265_param* p, int padx, int pady)<br>     BOOL(p->bRepeatHeaders, "repeat-headers");<br>     BOOL(p->bAnnexB, "annexb");<br>     BOOL(p->bEnableAccessUnitDelimiters, "aud");<br>+    BOOL(p->bEnableEndOfBitstream, "eob");<br>+    BOOL(p->bEnableEndOfSequence, "eos");<br>     BOOL(p->bEmitHRDSEI, "hrd");<br>     BOOL(p->bEmitInfoSEI, "info");<br>     s += sprintf(s, " hash=%d", p->decodedPictureHashSEI);<br>@@ -2370,6 +2376,8 @@ void x265_copy_params(x265_param* dst, x265_param* src)<br>     dst->bRepeatHeaders = src->bRepeatHeaders;<br>     dst->bAnnexB = src->bAnnexB;<br>     dst->bEnableAccessUnitDelimiters = src->bEnableAccessUnitDelimiters;<br>+    dst->bEnableEndOfBitstream = src->bEnableEndOfBitstream;<br>+    dst->bEnableEndOfSequence = src->bEnableEndOfSequence;<br>     dst->bEmitInfoSEI = src->bEmitInfoSEI;<br>     dst->decodedPictureHashSEI = src->decodedPictureHashSEI;<br>     dst->bEnableTemporalSubLayers = src->bEnableTemporalSubLayers;<br>diff --git a/source/encoder/api.cpp b/source/encoder/api.cpp<br>index a986355e0..e4eba7139 100644<br>--- a/source/encoder/api.cpp<br>+++ b/source/encoder/api.cpp<br>@@ -295,6 +295,16 @@ int x265_encoder_headers(x265_encoder *enc, x265_nal **pp_nal, uint32_t *pi_nal)<br>     return -1;<br> }<br> <br>+int x265_encoder_end_nal_units(x265_encoder *enc, x265_nal **pp_nal, uint32_t *pi_nal)<br>+{<br>+    Encoder *encoder = static_cast<Encoder*>(enc);<br>+    Bitstream bs;<br>+    encoder->getEndNalUnits(encoder->m_nalList, bs);<br>+    *pp_nal = &encoder->m_nalList.m_nal[0];<br>+    if (pi_nal) *pi_nal = encoder->m_nalList.m_numNal;<br>+    return encoder->m_nalList.m_occupancy;<br>+}<br>+<br> void x265_encoder_parameters(x265_encoder *enc, x265_param *out)<br> {<br>     if (enc && out)<br>@@ -1052,6 +1062,7 @@ static const x265_api libapi =<br>     &x265_encoder_reconfig,<br>     &x265_encoder_reconfig_zone,<br>     &x265_encoder_headers,<br>+    &x265_encoder_end_nal_units,<br>     &x265_encoder_encode,<br>     &x265_encoder_get_stats,<br>     &x265_encoder_log,<br>diff --git a/source/encoder/encoder.cpp b/source/encoder/encoder.cpp<br>index 1dd247124..74f69d250 100644<br>--- a/source/encoder/encoder.cpp<br>+++ b/source/encoder/encoder.cpp<br>@@ -3374,6 +3374,19 @@ void Encoder::getStreamHeaders(NALList& list, Entropy& sbacCoder, Bitstream& bs)<br>     }<br> }<br> <br>+void Encoder::getEndNalUnits(NALList& list, Bitstream& bs)<br>+{<br>+    NALList nalList;<br>+    bs.resetBits();<br>+<br>+    if (m_param->bEnableEndOfSequence)<br>+        nalList.serialize(NAL_UNIT_EOS, bs);<br>+    if (m_param->bEnableEndOfBitstream)<br>+        nalList.serialize(NAL_UNIT_EOB, bs);<br>+<br>+    list.takeContents(nalList);<br>+}<br>+<br> void Encoder::initVPS(VPS *vps)<br> {<br>     /* Note that much of the VPS is initialized by determineLevel() */<br>diff --git a/source/encoder/encoder.h b/source/encoder/encoder.h<br>index 22a886ef4..2ee5bdaee 100644<br>--- a/source/encoder/encoder.h<br>+++ b/source/encoder/encoder.h<br>@@ -327,6 +327,8 @@ public:<br> <br>     void getStreamHeaders(NALList& list, Entropy& sbacCoder, Bitstream& bs);<br> <br>+    void getEndNalUnits(NALList& list, Bitstream& bs);<br>+<br>     void fetchStats(x265_stats* stats, size_t statsSizeBytes);<br> <br>     void printSummary();<br>diff --git a/source/encoder/frameencoder.cpp b/source/encoder/frameencoder.cpp<br>index 2086a15a5..1c0554c2d 100644<br>--- a/source/encoder/frameencoder.cpp<br>+++ b/source/encoder/frameencoder.cpp<br>@@ -465,6 +465,12 @@ void FrameEncoder::compressFrame()<br>      * unit) */<br>     Slice* slice = m_frame->m_encData->m_slice;<br> <br>+    if (m_param->bEnableEndOfSequence && m_frame->m_lowres.sliceType == X265_TYPE_IDR && m_frame->m_poc)<br>+    {<br>+        m_bs.resetBits();<br>+        m_nalList.serialize(NAL_UNIT_EOS, m_bs);<br>+    }<br>+<br>     if (m_param->bEnableAccessUnitDelimiters && (m_frame->m_poc || m_param->bRepeatHeaders))<br>     {<br>         m_bs.resetBits();<br>diff --git a/source/test/regression-tests.txt b/source/test/regression-tests.txt<br>index a4fcab87f..971c854df 100644<br>--- a/source/test/regression-tests.txt<br>+++ b/source/test/regression-tests.txt<br>@@ -166,6 +166,7 @@ crowd_run_1920x1080_50.yuv, --preset fast --ctu 64 --rskip 2 --rskip-edge-thresh<br> crowd_run_1920x1080_50.yuv, --preset slow --ctu 32 --rskip 2 --rskip-edge-threshold 5 --hist-scenecut --hist-threshold 0.1<br> crowd_run_1920x1080_50.yuv, --preset slower --ctu 16 --rskip 2 --rskip-edge-threshold 5 --hist-scenecut --hist-threshold 0.1 --aq-mode 4<br> crowd_run_1920x1080_50.yuv, --preset ultrafast --video-signal-type-preset BT2100_PQ_YCC:BT2100x108n0005<br>+crowd_run_1920x1080_50.yuv, --preset ultrafast --eob --eos<br>  <br> # Main12 intraCost overflow bug test<br> 720p50_parkrun_ter.y4m,--preset medium<br>diff --git a/source/x265.h b/source/x265.h<br>index ad02072bf..796b134a9 100644<br>--- a/source/x265.h<br>+++ b/source/x265.h<br>@@ -1939,6 +1939,14 @@ typedef struct x265_param<br>      * together with this, which changes the values set corresponding to the system-id <br>      * or color-volume, it will be discarded. */<br>     const char* videoSignalTypePreset;<br>+<br>+    /* Flag indicating whether the encoder should emit an End of Bitstream<br>+     * NAL at the end of bitstream. Default false */<br>+    int      bEnableEndOfBitstream;<br>+<br>+    /* Flag indicating whether the encoder should emit an End of Sequence<br>+     * NAL at the end of every Coded Video Sequence. Default false */<br>+    int      bEnableEndOfSequence;<br> } x265_param;<br> <br> /* x265_param_alloc:<br>@@ -2252,6 +2260,7 @@ typedef struct x265_api<br>     int           (*encoder_reconfig)(x265_encoder*, x265_param*);<br>     int           (*encoder_reconfig_zone)(x265_encoder*, x265_zone*);<br>     int           (*encoder_headers)(x265_encoder*, x265_nal**, uint32_t*);<br>+    int           (*encoder_end_nal_units)(x265_encoder*, x265_nal**, uint32_t*);<br>     int           (*encoder_encode)(x265_encoder*, x265_nal**, uint32_t*, x265_picture*, x265_picture*);<br>     void          (*encoder_get_stats)(x265_encoder*, x265_stats*, uint32_t);<br>     void          (*encoder_log)(x265_encoder*, int, char**);<br>diff --git a/source/x265cli.cpp b/source/x265cli.cpp<br>index f2b73f7db..334396e53 100755<br>--- a/source/x265cli.cpp<br>+++ b/source/x265cli.cpp<br>@@ -350,6 +350,8 @@ namespace X265_NS {<br>         H0("   --[no-]idr-recovery-sei      Emit recovery point infor SEI at each IDR frame \n");<br>         H0("   --[no-]temporal-layers        Enable a temporal sublayer for unreferenced B frames. Default %s\n", OPT(param->bEnableTemporalSubLayers));<br>         H0("   --[no-]aud                    Emit access unit delimiters at the start of each access unit. Default %s\n", OPT(param->bEnableAccessUnitDelimiters));<br>+        H0("   --[no-]eob                    Emit end of bitstream nal unit at the end of the bitstream. Default %s\n", OPT(param->bEnableEndOfBitstream));<br>+        H0("   --[no-]eos                    Emit end of sequence nal unit at the end of every coded video sequence. Default %s\n", OPT(param->bEnableEndOfSequence));<br>         H1("   --hash <integer>              Decoded Picture Hash SEI 0: disabled, 1: MD5, 2: CRC, 3: Checksum. Default %d\n", param->decodedPictureHashSEI);<br>         H0("   --atc-sei <integer>           Emit the alternative transfer characteristics SEI message where the integer is the preferred transfer characteristics. Default disabled\n");<br>         H0("   --pic-struct <integer>        Set the picture structure and emits it in the picture timing SEI message. Values in the range 0..12. See D.3.3 of the HEVC spec. for a detailed explanation.\n");<br>diff --git a/source/x265cli.h b/source/x265cli.h<br>index 3d2480374..2b0518d1f 100644<br>--- a/source/x265cli.h<br>+++ b/source/x265cli.h<br>@@ -269,6 +269,10 @@ static const struct option long_options[] =<br>     { "repeat-headers",       no_argument, NULL, 0 },<br>     { "aud",                  no_argument, NULL, 0 },<br>     { "no-aud",               no_argument, NULL, 0 },<br>+    { "eob",                  no_argument, NULL, 0 },<br>+    { "no-eob",               no_argument, NULL, 0 },<br>+    { "eos",                  no_argument, NULL, 0 },<br>+    { "no-eos",               no_argument, NULL, 0 },<br>     { "info",                 no_argument, NULL, 0 },<br>     { "no-info",              no_argument, NULL, 0 },<br>     { "zones",          required_argument, NULL, 0 },<br>-- <br>2.18.0.windows.1<br><br><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><font color="#0c343d" face="verdana, sans-serif"><br></font></div><div dir="ltr"><font color="#0c343d" face="verdana, sans-serif">Thanks & Regards</font><div><font color="#0c343d" face="verdana, sans-serif"><b>Niranjan Kumar B</b></font></div><div><font size="1" color="#0c343d" face="verdana, sans-serif">Video Codec Engineer </font></div><div><font size="1" color="#0c343d" face="verdana, sans-serif">Media & AI Analytics</font></div><div><font face="trebuchet ms, sans-serif" color="#0c343d">+91 958 511 1449</font></div><div><a href="https://multicorewareinc.com/" style="color:rgb(17,85,204)" target="_blank"><img src="https://docs.google.com/uc?export=download&id=1kc3RJu9M8bnIf6Xa5rUw2d-eEVUsPBE5&revid=0B7tw9XJBmynaemR1VUpQUi9DVytRVW5SVkRwVTFjb1hBMUcwPQ"></a></div></div></div></div></div></div></div></div>