<commit_before>package com.kaltura.hls.m2ts
{
    import flash.utils.ByteArray;
    import flash.net.ObjectEncoding;
    import flash.utils.ByteArray;
    import flash.utils.Endian;    
    import flash.utils.IDataInput;
    import flash.utils.IDataOutput;
    import com.hurlant.util.Hex;

    CONFIG::LOGGING
    {
        import org.osmf.logging.Logger;
        import org.osmf.logging.Log;
    }

    /**
     * Process packetized elementary streams and extract NALUs and other data.
     */
    public class PESProcessor
    {
        CONFIG::LOGGING
        {
            private static const logger:Logger = Log.getLogger("com.kaltura.hls.m2ts.PESProcessor");
        }

        public var types:Object = {};
        public var streams:Object = {};

        public var lastVideoNALU:NALU = null;

		public var lastID3NALU:NALU = null;
		public var lastID3Point:uint;
		
        public var transcoder:FLVTranscoder = new FLVTranscoder();

        public var headerSent:Boolean = false;

        public var pmtStreamId:int = -1;

        public var hasVideo:Boolean = false;

        protected var pendingBuffers:Vector.<Object> = new Vector.<Object>();
        protected var pendingLastConvertedIndex:int = 0;

        public var lastPTS:Number = NaN, lastDTS:Number = NaN;

        /**
         * Given a MPEG timestamp we've seen previously, determine if the new timestamp
         * has wrapped and correct it to follow the old timestamp.
         */
        public static function handleMpegTimestampWrap(newTime:Number, oldTime:Number):Number
        {
            while (!isNaN(oldTime) && (Math.abs(newTime - oldTime) > 4294967296))
                newTime += (oldTime < newTime) ? -8589934592 : 8589934592;

            return newTime;
        }

        public function logStreams():void
        {
            CONFIG::LOGGING
            {
                logger.debug("----- PES state -----");
                for(var k:* in streams)
                {
                    logger.debug("   " + k + " has " + streams[k].buffer.length + " bytes, type=" + types[k]);
                }                
            }
        }

        public function clear(clearAACConfig:Boolean = true):void
        {
            hasVideo = false;
            streams = {};
            lastVideoNALU = null;
            transcoder.clear(clearAACConfig);

            // Reset PTS/DTS reference.
            lastPTS = lastDTS = NaN;
        }

        private function parseProgramAssociationTable(bytes:ByteArray, cursor:uint):Boolean
        {
            // Get the section length.
            var sectionLen:uint = ((bytes[cursor+2] & 0x03) << 8) | bytes[cursor+3];

            // Check the section length for a single PMT.
            CONFIG::LOGGING
            {
                if (sectionLen > 13)
                {
                    logger.debug("Saw multiple PMT entries in the PAT; blindly choosing first one.");
                }
            }

            // Grab the PMT ID.
            pmtStreamId = ((bytes[cursor+10] << 8) | bytes[cursor+11]) & 0x1FFF;

            CONFIG::LOGGING
            {
                logger.debug("Saw PMT ID of " + pmtStreamId);
            }
<commit_msg>Fix for failure to unwrap MPEG2 timestamps.<commit_after>package com.kaltura.hls.m2ts
{
    import flash.utils.ByteArray;
    import flash.net.ObjectEncoding;
    import flash.utils.ByteArray;
    import flash.utils.Endian;    
    import flash.utils.IDataInput;
    import flash.utils.IDataOutput;
    import com.hurlant.util.Hex;

    CONFIG::LOGGING
    {
        import org.osmf.logging.Logger;
        import org.osmf.logging.Log;
    }

    /**
     * Process packetized elementary streams and extract NALUs and other data.
     */
    public class PESProcessor
    {
        CONFIG::LOGGING
        {
            private static const logger:Logger = Log.getLogger("com.kaltura.hls.m2ts.PESProcessor");
        }

        public var types:Object = {};
        public var streams:Object = {};

        public var lastVideoNALU:NALU = null;

		public var lastID3NALU:NALU = null;
		public var lastID3Point:uint;
		
        public var transcoder:FLVTranscoder = new FLVTranscoder();

        public var headerSent:Boolean = false;

        public var pmtStreamId:int = -1;

        public var hasVideo:Boolean = false;

        protected var pendingBuffers:Vector.<Object> = new Vector.<Object>();
        protected var pendingLastConvertedIndex:int = 0;

        public var lastPTS:Number = NaN, lastDTS:Number = NaN;

        /**
         * Given a MPEG timestamp we've seen previously, determine if the new timestamp
         * has wrapped and correct it to follow the old timestamp.
         */
        public static function handleMpegTimestampWrap(newTime:Number, oldTime:Number):Number
        {
            while (!isNaN(oldTime) && (Math.abs(newTime - oldTime) > 4294967296))
                newTime += (oldTime < newTime) ? -8589934592 : 8589934592;

            return newTime;
        }

        public function logStreams():void
        {
            CONFIG::LOGGING
            {
                logger.debug("----- PES state -----");
                for(var k:* in streams)
                {
                    logger.debug("   " + k + " has " + streams[k].buffer.length + " bytes, type=" + types[k]);
                }                
            }
        }

        public function clear(clearAACConfig:Boolean = true):void
        {
            hasVideo = false;
            streams = {};
            lastVideoNALU = null;
            transcoder.clear(clearAACConfig);

            // Don't reset PTS/DTS reference. This implies the
            // total stream length is < ~27 hours or else you'll
            // get erroneous timecodes.
        }

        private function parseProgramAssociationTable(bytes:ByteArray, cursor:uint):Boolean
        {
            // Get the section length.
            var sectionLen:uint = ((bytes[cursor+2] & 0x03) << 8) | bytes[cursor+3];

            // Check the section length for a single PMT.
            CONFIG::LOGGING
            {
                if (sectionLen > 13)
                {
                    logger.debug("Saw multiple PMT entries in the PAT; blindly choosing first one.");
                }
            }

            // Grab the PMT ID.
            pmtStreamId = ((bytes[cursor+10] << 8) | bytes[cursor+11]) & 0x1FFF;

            CONFIG::LOGGING
            {
                logger.debug("Saw PMT ID of " + pmtStreamId);
            }

{'fraction_non_alphanumeric': 0.09829780708480294, 'fraction_numerical': 0.016101824873485663, 'mean_word_length': 1.711850311850312, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '248551', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1765, 'n_words': 447}