[x265] [PATCH] Cleanup SEI code

ashok at multicorewareinc.com ashok at multicorewareinc.com
Tue Sep 18 13:05:27 CEST 2018


# HG changeset patch
# User Ashok Kumar Mishra <ashok at multicorewareinc.com>
# Date 1537268644 -19800
#      Tue Sep 18 16:34:04 2018 +0530
# Node ID f3c175d92ec248fc6b3388c6f70c1f3ea365d539
# Parent  6426e22f479f10a07d606119dede0da844fb5015
Cleanup SEI code

diff -r 6426e22f479f -r f3c175d92ec2 source/common/param.cpp
--- a/source/common/param.cpp	Tue Sep 18 16:34:04 2018 +0530
+++ b/source/common/param.cpp	Tue Sep 18 16:34:04 2018 +0530
@@ -1415,9 +1415,16 @@
     if (param->masteringDisplayColorVolume || param->maxFALL || param->maxCLL)
         param->bEmitHDRSEI = 1;
 
-    bool isSingleSEI = ((param->bEmitHRDSEI || param->bEmitInfoSEI || param->decodedPictureHashSEI ||
-                         param->masteringDisplayColorVolume || param->maxCLL || param->maxFALL || 
-                         param->bEmitHDRSEI || param->bEmitIDRRecoverySEI));
+    bool isSingleSEI = (param->bRepeatHeaders
+                     || param->bEmitHRDSEI
+                     || param->bEmitInfoSEI
+                     || param->bEmitHDRSEI
+                     || param->bEmitIDRRecoverySEI
+                   || !!param->interlaceMode
+                     || param->preferredTransferCharacteristics > 1
+                     || param->toneMapFile
+                     || param->naluFile);
+
     if (!isSingleSEI && param->bSingleSeiNal)
     {
         param->bSingleSeiNal = 0;
diff -r 6426e22f479f -r f3c175d92ec2 source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp	Tue Sep 18 16:34:04 2018 +0530
+++ b/source/encoder/encoder.cpp	Tue Sep 18 16:34:04 2018 +0530
@@ -2374,27 +2374,22 @@
     sbacCoder.codePPS(m_pps, (m_param->maxSlices <= 1), m_iPPSQpMinus26);
     bs.writeByteAlignment();
     list.serialize(NAL_UNIT_PPS, bs);
+
     if (m_param->bSingleSeiNal)
         bs.resetBits();
+
     if (m_param->bEmitHDRSEI)
     {
         SEIContentLightLevel cllsei;
         cllsei.max_content_light_level = m_param->maxCLL;
         cllsei.max_pic_average_light_level = m_param->maxFALL;
-        if (!m_param->bSingleSeiNal)
-            bs.resetBits();
-        cllsei.write(bs, m_sps);
-        cllsei.alignAndSerialize(bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, list);
+        cllsei.writeSEImessages(bs, m_sps, NAL_UNIT_PREFIX_SEI, list, m_param->bSingleSeiNal);
+
         if (m_param->masteringDisplayColorVolume)
         {
             SEIMasteringDisplayColorVolume mdsei;
             if (mdsei.parse(m_param->masteringDisplayColorVolume))
-            {
-                if (!m_param->bSingleSeiNal)
-                    bs.resetBits();
-                mdsei.write(bs, m_sps);
-                mdsei.alignAndSerialize(bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, list);
-            }
+                mdsei.writeSEImessages(bs, m_sps, NAL_UNIT_PREFIX_SEI, list, m_param->bSingleSeiNal);
             else
                 x265_log(m_param, X265_LOG_WARNING, "unable to parse mastering display color volume info\n");
         }
@@ -2413,13 +2408,12 @@
                     "Copyright 2013-2018 (c) Multicoreware, Inc - "
                     "http://x265.org - options: %s",
                     X265_BUILD, PFX(version_str), PFX(build_info_str), opts);
-                if (!m_param->bSingleSeiNal)
-                    bs.resetBits();
+
                 SEIuserDataUnregistered idsei;
                 idsei.m_userData = (uint8_t*)buffer;
                 idsei.setSize((uint32_t)strlen(buffer));
-                idsei.write(bs, m_sps);
-                idsei.alignAndSerialize(bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, list);
+                idsei.writeSEImessages(bs, m_sps, NAL_UNIT_PREFIX_SEI, list, m_param->bSingleSeiNal);
+
                 X265_FREE(buffer);
             }
 
@@ -2433,12 +2427,7 @@
         SEIActiveParameterSets sei;
         sei.m_selfContainedCvsFlag = true;
         sei.m_noParamSetUpdateFlag = true;
-        if (!m_param->bSingleSeiNal)
-            bs.resetBits();
-        int payloadSize = sei.countPayloadSize(m_sps);
-        sei.setSize(payloadSize);
-        sei.write(bs, m_sps);
-        sei.alignAndSerialize(bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, list);
+        sei.writeSEImessages(bs, m_sps, NAL_UNIT_PREFIX_SEI, list, m_param->bSingleSeiNal);
     }
 }
 
@@ -2521,7 +2510,7 @@
     vui.defaultDisplayWindow.bottomOffset = m_param->vui.defDispWinBottomOffset;
     vui.defaultDisplayWindow.leftOffset = m_param->vui.defDispWinLeftOffset;
 
-	vui.frameFieldInfoPresentFlag = !!m_param->interlaceMode || (m_param->pictureStructure >= 0);
+    vui.frameFieldInfoPresentFlag = !!m_param->interlaceMode || (m_param->pictureStructure >= 0);
     vui.fieldSeqFlag = !!m_param->interlaceMode;
 
     vui.hrdParametersPresentFlag = m_param->bEmitHRDSEI;
diff -r 6426e22f479f -r f3c175d92ec2 source/encoder/frameencoder.cpp
--- a/source/encoder/frameencoder.cpp	Tue Sep 18 16:34:04 2018 +0530
+++ b/source/encoder/frameencoder.cpp	Tue Sep 18 16:34:04 2018 +0530
@@ -365,6 +365,33 @@
     return length;
 }
 
+bool FrameEncoder::writeToneMapInfo(x265_sei_payload *payload)
+{
+    bool payloadChange = false;
+    if (m_top->m_prevTonemapPayload.payload != NULL && payload->payloadSize == m_top->m_prevTonemapPayload.payloadSize)
+    {
+        if (memcmp(m_top->m_prevTonemapPayload.payload, payload->payload, payload->payloadSize) != 0)
+            payloadChange = true;
+    }
+    else
+    {
+        payloadChange = true;
+        if (m_top->m_prevTonemapPayload.payload != NULL)
+            x265_free(m_top->m_prevTonemapPayload.payload);
+        m_top->m_prevTonemapPayload.payload = (uint8_t*)x265_malloc(sizeof(uint8_t)* payload->payloadSize);
+    }
+
+    if (payloadChange)
+    {
+        m_top->m_prevTonemapPayload.payloadType = payload->payloadType;
+        m_top->m_prevTonemapPayload.payloadSize = payload->payloadSize;
+        memcpy(m_top->m_prevTonemapPayload.payload, payload->payload, payload->payloadSize);
+    }
+
+    bool isIDR = m_frame->m_lowres.sliceType == X265_TYPE_IDR;
+    return (payloadChange || isIDR);
+}
+
 void FrameEncoder::writeTrailingSEIMessages()
 {
     Slice* slice = m_frame->m_encData->m_slice;
@@ -393,10 +420,8 @@
         payloadSize = 1 + 4 * planes;
     }
 
-    m_bs.resetBits();
     m_seiReconPictureDigest.setSize(payloadSize);
-    m_seiReconPictureDigest.write(m_bs, *slice->m_sps);
-    m_seiReconPictureDigest.alignAndSerialize(m_bs, true, m_param->bSingleSeiNal, NAL_UNIT_SUFFIX_SEI, m_nalList);
+    m_seiReconPictureDigest.writeSEImessages(m_bs, *slice->m_sps, NAL_UNIT_SUFFIX_SEI, m_nalList, false);
 }
 
 void FrameEncoder::compressFrame()
@@ -496,9 +521,7 @@
                 wa.waitForExit();
             else
                 weightAnalyse(*slice, *m_frame, *m_param);
-
         }
-
     }
     else
         slice->disableWeights();
@@ -670,12 +693,7 @@
             bpSei->m_dpbDelayOffset = 0;
             // hrdFullness() calculates the initial CPB removal delay and offset
             m_top->m_rateControl->hrdFullness(bpSei);
-            if (!m_param->bSingleSeiNal)
-                m_bs.resetBits();
-            int payloadSize = bpSei->countPayloadSize(*slice->m_sps);
-            bpSei->setSize(payloadSize);
-            bpSei->write(m_bs, *slice->m_sps);
-            bpSei->alignAndSerialize(m_bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, m_nalList);
+            bpSei->writeSEImessages(m_bs, *slice->m_sps, NAL_UNIT_PREFIX_SEI, m_nalList, m_param->bSingleSeiNal);
 
             m_top->m_lastBPSEI = m_rce.encodeOrder;
         }
@@ -687,11 +705,7 @@
             sei.m_recoveryPocCnt = 0;
             sei.m_exactMatchingFlag = true;
             sei.m_brokenLinkFlag = false;
-            if (!m_param->bSingleSeiNal)
-                m_bs.resetBits();
-            sei.setSize(sei.countPayloadSize(*slice->m_sps));
-            sei.write(m_bs, *slice->m_sps);
-            sei.alignAndSerialize(m_bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, m_nalList);
+            sei.writeSEImessages(m_bs, *slice->m_sps, NAL_UNIT_PREFIX_SEI, m_nalList, m_param->bSingleSeiNal);
         }
     }
 
@@ -711,10 +725,7 @@
             else
                 sei->m_picStruct = m_param->pictureStructure;
 
-            if (m_param->interlaceMode)
-                sei->m_sourceScanType = 0;
-            else
-                sei->m_sourceScanType = 1;
+            sei->m_sourceScanType = m_param->interlaceMode ? 0 : 1;
 
             sei->m_duplicateFlag = false;
         }
@@ -728,26 +739,17 @@
             sei->m_auCpbRemovalDelay = X265_MIN(X265_MAX(1, m_rce.encodeOrder - prevBPSEI), (1 << hrd->cpbRemovalDelayLength));
             sei->m_picDpbOutputDelay = slice->m_sps->numReorderPics + poc - m_rce.encodeOrder;
         }
-        if (!m_param->bSingleSeiNal)
-            m_bs.resetBits();
-        int payloadSize = sei->countPayloadSize(*slice->m_sps);
-        sei->setSize(payloadSize);
-        sei->write(m_bs, *slice->m_sps);
-        sei->alignAndSerialize(m_bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, m_nalList);
+
+        sei->writeSEImessages(m_bs, *slice->m_sps, NAL_UNIT_PREFIX_SEI, m_nalList, m_param->bSingleSeiNal);
     }
 
     if (m_param->preferredTransferCharacteristics > -1 && slice->isIRAP())
     {
         SEIAlternativeTC m_seiAlternativeTC;
         m_seiAlternativeTC.m_preferredTransferCharacteristics = m_param->preferredTransferCharacteristics;
-        m_bs.resetBits();
-        int payloadSize = m_seiAlternativeTC.countPayloadSize(*slice->m_sps);
-        m_seiAlternativeTC.setSize(payloadSize);
-        m_seiAlternativeTC.write(m_bs, *slice->m_sps);
-        m_seiAlternativeTC.alignAndSerialize(m_bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, m_nalList);
+        m_seiAlternativeTC.writeSEImessages(m_bs, *slice->m_sps, NAL_UNIT_PREFIX_SEI, m_nalList, m_param->bSingleSeiNal);
     }
 
-    bool isSei = false;
     /* Write user SEI */
     for (int i = 0; i < m_frame->m_userSEI.numPayloads; i++)
     {
@@ -756,33 +758,27 @@
         {
             SEIuserDataUnregistered sei;
             sei.m_userData = payload->payload;
-            if (!m_param->bSingleSeiNal)
-                m_bs.resetBits();
             sei.setSize(payload->payloadSize);
-            sei.write(m_bs, *slice->m_sps);
-            sei.alignAndSerialize(m_bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, m_nalList);
-            isSei = true;
+            sei.writeSEImessages(m_bs, *slice->m_sps, NAL_UNIT_PREFIX_SEI, m_nalList, m_param->bSingleSeiNal);
         }
         else if (payload->payloadType == USER_DATA_REGISTERED_ITU_T_T35)
         {
+            bool writeSei = m_param->bDhdr10opt ? writeToneMapInfo(payload) : true;
             if (writeSei)
             {
-                SEICreativeIntentMeta sei;
-                sei.m_payload = payload->payload;
-                if (!m_param->bSingleSeiNal)
-                    m_bs.resetBits();
+                SEIuserDataRegistered sei;
+                sei.m_userData = payload->payload;
                 sei.setSize(payload->payloadSize);
-                sei.write(m_bs, *slice->m_sps);
-                sei.alignAndSerialize(m_bs, false, m_param->bSingleSeiNal, NAL_UNIT_PREFIX_SEI, m_nalList);
-                isSei = true;
+                sei.writeSEImessages(m_bs, *slice->m_sps, NAL_UNIT_PREFIX_SEI, m_nalList, m_param->bSingleSeiNal);
             }
         }
         else
             x265_log(m_param, X265_LOG_ERROR, "Unrecognized SEI type\n");
     }
 
-    isSei |= ((m_frame->m_lowres.bKeyframe && m_param->bRepeatHeaders) || m_param->bEmitHRDSEI ||
-             !!m_param->interlaceMode || (m_frame->m_lowres.sliceType == X265_TYPE_IDR && m_param->bEmitIDRRecoverySEI));
+    bool isSei = ((m_frame->m_lowres.bKeyframe && m_param->bRepeatHeaders) || m_param->bEmitHRDSEI ||
+                 !!m_param->interlaceMode || (m_frame->m_lowres.sliceType == X265_TYPE_IDR && m_param->bEmitIDRRecoverySEI) ||
+                   m_frame->m_userSEI.numPayloads);
 
     if (isSei && m_param->bSingleSeiNal)
     {
@@ -1061,10 +1057,8 @@
 
         m_nalList.serialize(slice->m_nalUnitType, m_bs);
     }
-    if (isSei && m_param->bSingleSeiNal)
-        m_bs.resetBits();
 
-    if (m_param->decodedPictureHashSEI)    
+    if (m_param->decodedPictureHashSEI)
         writeTrailingSEIMessages();
 
     uint64_t bytes = 0;
diff -r 6426e22f479f -r f3c175d92ec2 source/encoder/frameencoder.h
--- a/source/encoder/frameencoder.h	Tue Sep 18 16:34:04 2018 +0530
+++ b/source/encoder/frameencoder.h	Tue Sep 18 16:34:04 2018 +0530
@@ -230,6 +230,7 @@
     int  collectCTUStatistics(const CUData& ctu, FrameStats* frameLog);
     void noiseReductionUpdate();
     void writeTrailingSEIMessages();
+    bool writeToneMapInfo(x265_sei_payload *payload);
 
     /* Called by WaveFront::findJob() */
     virtual void processRow(int row, int threadId);
diff -r 6426e22f479f -r f3c175d92ec2 source/encoder/sei.cpp
--- a/source/encoder/sei.cpp	Tue Sep 18 16:34:04 2018 +0530
+++ b/source/encoder/sei.cpp	Tue Sep 18 16:34:04 2018 +0530
@@ -33,46 +33,44 @@
     0x2C, 0xA2, 0xDE, 0x09, 0xB5, 0x17, 0x47, 0xDB,
     0xBB, 0x55, 0xA4, 0xFE, 0x7F, 0xC2, 0xFC, 0x4E
 };
-/* count the size of the payload and return the size in bits */
-int SEI::countPayloadSize(const SPS& sps)
+
+/* marshal a single SEI message sei, storing the marshalled representation
+* in bitstream bs */
+void SEI::writeSEImessages(Bitstream& bs, const SPS& sps, NalUnitType nalUnitType, NALList& list, int isNested)
 {
+    if (!isNested)
+        bs.resetBits();
+
     BitCounter counter;
     m_bitIf = &counter;
     writeSEI(sps);
+    /* count the size of the payload and return the size in bits */
     X265_CHECK(0 == (counter.getNumberOfWrittenBits() & 7), "payload unaligned\n");
-    int count = counter.getNumberOfWrittenBits() >> 3;
-    return count;
-}
+    uint32_t payloadData = counter.getNumberOfWrittenBits() >> 3;
 
-void SEI::alignAndSerialize(Bitstream& bs, int lastSei, int isSingleSei, NalUnitType nalUnitType, NALList& list)
-{
-    if (lastSei || !isSingleSei)
+    // set bitstream
+    m_bitIf = &bs;
+
+    uint32_t payloadType = m_payloadType;
+    for (; payloadType >= 0xff; payloadType -= 0xff)
+        WRITE_CODE(0xff, 8, "payload_type");
+    WRITE_CODE(payloadType, 8, "payload_type");
+
+    uint32_t payloadSize = payloadData;
+    for (; payloadSize >= 0xff; payloadSize -= 0xff)
+        WRITE_CODE(0xff, 8, "payload_size");
+    WRITE_CODE(payloadSize, 8, "payload_size");
+
+    // virtual writeSEI method, write to bs 
+    writeSEI(sps);
+
+    if (!isNested)
     {
         bs.writeByteAlignment();
         list.serialize(nalUnitType, bs);
     }
 }
 
-/* marshal a single SEI message sei, storing the marshalled representation
- * in bitstream bs */
-void SEI::write(Bitstream& bs, const SPS& sps)
-{
-    uint32_t type = m_payloadType;
-    m_bitIf = &bs;
-    uint32_t payloadSize = m_payloadSize;
-    if (m_payloadType == USER_DATA_UNREGISTERED)
-        payloadSize = m_payloadSize + 16;
-    uint32_t payloadType = m_payloadType;
-    for (; payloadType >= 0xff; payloadType -= 0xff)
-        WRITE_CODE(0xff, 8, "payload_type");
-    WRITE_CODE(type, 8, "payload_type");
-    for (; payloadSize >= 0xff; payloadSize -= 0xff)
-        WRITE_CODE(0xff, 8, "payload_size");
-    WRITE_CODE(payloadSize, 8, "payload_size");
-    /* virtual writeSEI method, write to bs */
-    writeSEI(sps);
-}
-
 void SEI::writeByteAlign()
 {
     // TODO: expose bs.writeByteAlignment() as virtual function
diff -r 6426e22f479f -r f3c175d92ec2 source/encoder/sei.h
--- a/source/encoder/sei.h	Tue Sep 18 16:34:04 2018 +0530
+++ b/source/encoder/sei.h	Tue Sep 18 16:34:04 2018 +0530
@@ -36,11 +36,9 @@
 class SEI : public SyntaxElementWriter
 {
 public:
-    /* SEI users call write() to marshal an SEI to a bitstream.
-     * The write() method calls writeSEI() which encodes the header */
-    void write(Bitstream& bs, const SPS& sps);
-    void alignAndSerialize(Bitstream& bs, int lastSei, int isSingleSei, NalUnitType nalUnitType, NALList& list);
-    int countPayloadSize(const SPS& sps);
+    /* SEI users call writeSEImessages() to marshal an SEI to a bitstream.
+    * The writeSEImessages() method calls writeSEI() which encodes the header */
+    void writeSEImessages(Bitstream& bs, const SPS& sps, NalUnitType nalUnitType, NALList& list, int isNested);
     void setSize(uint32_t size);
     static char* base64Decode(char encodedString[], int base64EncodeLength);
     virtual ~SEI() {}
@@ -51,6 +49,32 @@
     void writeByteAlign();
 };
 
+//seongnam.oh at samsung.com :: for the Creative Intent Meta Data Encoding
+class SEIuserDataRegistered : public SEI
+{
+public:
+    SEIuserDataRegistered()
+    {
+        m_payloadType = USER_DATA_REGISTERED_ITU_T_T35;
+        m_payloadSize = 0;
+    }
+
+    uint8_t *m_userData;
+
+    // daniel.vt at samsung.com :: for the Creative Intent Meta Data Encoding ( seongnam.oh at samsung.com )
+    void writeSEI(const SPS&)
+    {
+        if (!m_userData)
+            return;
+
+        uint32_t i = 0;
+        for (; i < m_payloadSize; ++i)
+            WRITE_CODE(m_userData[i], 8, "creative_intent_metadata");
+    }
+};
+
+static const uint32_t ISO_IEC_11578_LEN = 16;
+
 class SEIuserDataUnregistered : public SEI
 {
 public:
@@ -59,11 +83,11 @@
         m_payloadType = USER_DATA_UNREGISTERED;
         m_payloadSize = 0;
     }
-    static const uint8_t m_uuid_iso_iec_11578[16];
+    static const uint8_t m_uuid_iso_iec_11578[ISO_IEC_11578_LEN];
     uint8_t *m_userData;
     void writeSEI(const SPS&)
     {
-        for (uint32_t i = 0; i < 16; i++)
+        for (uint32_t i = 0; i < ISO_IEC_11578_LEN; i++)
             WRITE_CODE(m_uuid_iso_iec_11578[i], 8, "sei.uuid_iso_iec_11578[i]");
         for (uint32_t i = 0; i < m_payloadSize; i++)
             WRITE_CODE(m_userData[i], 8, "user_data");
@@ -280,45 +304,21 @@
     }
 };
 
-//seongnam.oh at samsung.com :: for the Creative Intent Meta Data Encoding
-class SEICreativeIntentMeta : public SEI
-{
-public:
-    SEICreativeIntentMeta()
-    {
-        m_payloadType = USER_DATA_REGISTERED_ITU_T_T35;
-        m_payloadSize = 0;
-    }
-
-    uint8_t *m_payload;
-
-    // daniel.vt at samsung.com :: for the Creative Intent Meta Data Encoding ( seongnam.oh at samsung.com )
-    void writeSEI(const SPS&)
-    {
-        if (!m_payload)
-            return;
-
-        uint32_t i = 0;
-        for (; i < m_payloadSize; ++i)
-            WRITE_CODE(m_payload[i], 8, "creative_intent_metadata");
-    }
-};
-
 class SEIAlternativeTC : public SEI
 {
 public:
     int m_preferredTransferCharacteristics;
     SEIAlternativeTC()
     {
-	    m_payloadType = ALTERNATIVE_TRANSFER_CHARACTERISTICS;
-		m_payloadSize = 0;
-		m_preferredTransferCharacteristics = -1;
-	}	
-	
-	void writeSEI(const SPS&)
-	{
-	    WRITE_CODE(m_preferredTransferCharacteristics, 8, "Preferred transfer characteristics");
-	}
+        m_payloadType = ALTERNATIVE_TRANSFER_CHARACTERISTICS;
+        m_payloadSize = 0;
+        m_preferredTransferCharacteristics = -1;
+    }
+
+    void writeSEI(const SPS&)
+    {
+        WRITE_CODE(m_preferredTransferCharacteristics, 8, "Preferred transfer characteristics");
+    }
 };
 
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2018-09-17_18-43-27_r12418+.diff
Type: text/x-patch
Size: 19294 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20180918/279330fe/attachment-0001.bin>


More information about the x265-devel mailing list