Exemplo n.º 1
0
        /*
         * public UInt32 CurrentPacketNumber
         * {
         *  get { return packetCount + (UInt32)buffer.Count; }
         * }
         *
         * public EpElement[] EpData
         * {
         *  get
         *  {
         *      return epData.ToArray();
         *  }
         * }
         *
         * public StreamInfo[] Psi
         * {
         *  get { return pmtStreams.ToArray(); }
         * }
         */

        public override void MuxPacket(PesPacket pp)
        {
            if (fileType == TsFileType.TS || fileType == TsFileType.M2TS || fileType == TsFileType.BLU_RAY)
            {
                MuxTsPacket(pp);
            }
            else if (fileType == TsFileType.SUP_ELEMENTARY && pidMappings.ContainsKey(pp.PID))
            {
                byte[]    data = pp.GetData();
                PesHeader ph   = pp.GetHeader();
                if (ph.HasPts)
                {
                    Int64 Pts = ph.Pts;
                    Int64 Dts = 0;
                    if (ph.HasDts)
                    {
                        Dts = ph.Dts;
                    }
                    supHeader[2] = (byte)((Pts >> 24) & 0xff);
                    supHeader[3] = (byte)((Pts >> 16) & 0xff);
                    supHeader[4] = (byte)((Pts >> 8) & 0xff);
                    supHeader[5] = (byte)(Pts & 0xff);
                    supHeader[6] = (byte)((Dts >> 24) & 0xff);
                    supHeader[7] = (byte)((Dts >> 16) & 0xff);
                    supHeader[8] = (byte)((Dts >> 8) & 0xff);
                    supHeader[9] = (byte)(Dts & 0xff);
                    tsiow.Write(supHeader, 0, supHeader.Length);
                }
                tsiow.Write(data, ph.HeaderLength + 9, data.Length - (ph.HeaderLength + 9));
            }
            else if (fileType == TsFileType.PES_ELEMENTARY && pidMappings.ContainsKey(pp.PID))
            {
                byte[] data = pp.GetData();
                tsiow.Write(data, 0, data.Length);
            }
            else if (fileType == TsFileType.ELEMENTARY && pidMappings.ContainsKey(pp.PID))
            {
                byte[]    data = pp.GetData();
                PesHeader ph   = pp.GetHeader();
                tsiow.Write(data, ph.HeaderLength + 9, data.Length - (ph.HeaderLength + 9));
            }
        }
Exemplo n.º 2
0
 // If PesPacket contains valid stream data, store it
 // in the respective output stream.
 public override void MuxPacket(PesPacket pp)
 {
     foreach (DeMuxStream S in _Streams)
     {
         if (pp.PID == S.PID)
         {
             byte[] data = pp.GetData();
             S.FileStream.Write(data, 0, data.Length);
         }
     }
 }
Exemplo n.º 3
0
        public override void MuxPacket(PesPacket pp)
        {
            // TODO: Fix, just QnD ...
            bool bKeep = false;

            foreach (StreamInfo si in _StreamsToKeep)
            {
                if (si.ElementaryPID == pp.PID)
                {
                    bKeep = true;
                }
            }

            if (bKeep)
            {
                byte[] data = pp.GetData();
                _FileStream.Write(data, 0, data.Length);
            }
        }
Exemplo n.º 4
0
        private void MuxPesPacketToTs(PesPacket pp, bool priority)
        {
            byte[] data = new byte[Constants.TS_SIZE];
            int    j    = 0;
            int    i    = 0;

            byte[] pes = pp.GetData();

            // take care of the first packet
            data[0] = Constants.SYNC_BYTE;
            data[1] = 0x40; // payload start
            data[2] = 0;
            if (pes.Length < Constants.TS_PAYLOAD_SIZE)
            {
                data[3] = 0x30; // adaptation and payload
                int stufLength = Constants.TS_PAYLOAD_SIZE - pes.Length - 1;
                data[4] = (byte)stufLength;
                i       = 5;
                if (stufLength > 0)
                {
                    data[i] = 0;
                    i++;
                    stufLength--;
                }
                for (; i < (6 + stufLength); i++)
                {
                    data[i] = 0xff;
                }
                for (; i < Constants.TS_SIZE; i++)
                {
                    data[i] = pes[j];
                    j++;
                }
            }
            else
            {
                data[3] = 0x10; // no adaptation, payload only
                for (i = 4; i < data.Length; i++)
                {
                    data[i] = pes[j];
                    j++;
                }
            }
            TsPacket ts = new TsPacket();

            ts.SetData(data, 0);
            ushort pid = pidMappings[pp.PID];

            ts.PID               = pid;
            ts.Priority          = priority;
            ts.ContinuityCounter = Continuities[pid];
            ts.IncrementContinuityCounter();
            Continuities[pid] = ts.ContinuityCounter;
            buffer.Add(ts);
            while (j < ((pes.Length / Constants.TS_PAYLOAD_SIZE) * Constants.TS_PAYLOAD_SIZE))
            {
                // take care of the other packets
                data[0] = Constants.SYNC_BYTE;
                data[1] = 0x00; // no payload start
                data[2] = 0;
                data[3] = 0x10; // no adaptation, payload only
                for (i = 4; i < data.Length; i++)
                {
                    data[i] = pes[j];
                    j++;
                }
                ts = new TsPacket();
                ts.SetData(data, 0);
                ts.PID               = pid;
                ts.Priority          = priority;
                ts.ContinuityCounter = Continuities[pid];
                ts.IncrementContinuityCounter();
                Continuities[pid] = ts.ContinuityCounter;
                buffer.Add(ts);
            }
            // take care of the last packet
            if (j < pes.Length)
            {
                data[0] = Constants.SYNC_BYTE;
                data[1] = 0x00; // no payload start
                data[2] = 0;
                data[3] = 0x30; // adaptation and payload
                int stufLength = Constants.TS_PAYLOAD_SIZE - (pes.Length - j) - 1;
                data[4] = (byte)stufLength;
                i       = 5;
                if (stufLength > 0)
                {
                    data[i] = 0;
                    i++;
                    stufLength--;
                    for (; i < (6 + stufLength); i++)
                    {
                        data[i] = 0xff;
                    }
                }
                for (; i < Constants.TS_SIZE; i++)
                {
                    data[i] = pes[j];
                    j++;
                }
                ts = new TsPacket();
                ts.SetData(data, 0);
                ts.PID               = pid;
                ts.Priority          = priority;
                ts.ContinuityCounter = Continuities[pid];
                ts.IncrementContinuityCounter();
                Continuities[pid] = ts.ContinuityCounter;
                buffer.Add(ts);
            }
        }
Exemplo n.º 5
0
        private PesPacket CheckAndFixDiscontinuities(PesPacket pp)
        {
            // checks for PTS/DTS discontinuities
            PesHeader ph = pp.GetHeader();

            byte[] data   = pp.GetData();
            int    len    = ph.TotalHeaderLength;
            int    i      = len;
            UInt32 marker = 0xffffffff;

            if (Constants.DEFAULT_VIDEO_PID == pidMappings[pp.PID])
            {
                // checks for end of stream headers
                switch (VideoType)
                {
                case (byte)ElementaryStreamTypes.VIDEO_STREAM_VC1:
                    for (; i < data.Length; i++)
                    {
                        marker <<= 8;
                        marker  |= data[i];
                        if (marker == Constants.VC1_END_OF_STREAM)
                        {
                            break;
                        }
                    }
                    if (i < data.Length)
                    {
                        // we have an end of stream marker
                        i -= 3;
                        PesPacket pnew = new PesPacket(data, 0, i, pp.PID);
                        i += 4;
                        pnew.AddData(data, i, data.Length - i);
                        pp   = pnew;
                        ph   = pp.GetHeader();
                        data = pp.GetData();
                    }
                    break;

                case (byte)ElementaryStreamTypes.VIDEO_STREAM_MPEG2:
                    for (; i < data.Length; i++)
                    {
                        marker <<= 8;
                        marker  |= data[i];
                        if (marker == Constants.MPEG2_SEQ_END)
                        {
                            break;
                        }
                    }
                    if (i < data.Length)
                    {
                        // we have an end of stream marker
                        i -= 3;
                        PesPacket pnew = new PesPacket(data, 0, i, pp.PID);
                        i += 4;
                        pnew.AddData(data, i, data.Length - i);
                        pp   = pnew;
                        ph   = pp.GetHeader();
                        data = pp.GetData();
                    }
                    break;

                case (byte)ElementaryStreamTypes.VIDEO_STREAM_H264:
                    for (; i < data.Length; i++)
                    {
                        marker <<= 8;
                        marker  |= data[i];
                        if ((marker & 0xffffff9f) == Constants.H264_END_OF_STREAM)
                        {
                            break;
                        }
                    }
                    if (i < data.Length)
                    {
                        // we have an end of stream marker
                        i -= 3;
                        PesPacket pnew = new PesPacket(data, 0, i, pp.PID);
                        i += 4;
                        pnew.AddData(data, i, data.Length - i);
                        pp   = pnew;
                        ph   = pp.GetHeader();
                        data = pp.GetData();
                    }
                    break;
                }

                if (ph.HasPts)
                {
                    lastPts    = currentPts;
                    currentPts = ph.Pts;
                    ptsCount   = ptsDelta;
                    ptsDelta   = currentPts - lastPts;
                    if (lastPts != -1)
                    {
                        if (ptsDelta < (0 - (Constants.PTS_CLOCK_RATE << 2)) ||
                            ptsDelta > (Constants.PTS_CLOCK_RATE << 2))
                        {
                            ptsOffset += (lastPts + ptsCount - currentPts);
                        }
                    }
                }

                // build EP Map info
                marker = 0xffffffff;
                switch (VideoType)
                {
                case (byte)ElementaryStreamTypes.VIDEO_STREAM_VC1:
                    for (i = ph.TotalHeaderLength; i < data.Length; i++)
                    {
                        marker <<= 8;
                        marker  |= data[i];
                        if (marker == Constants.VC1_SEQ_SC && ph.HasPts)
                        {
                            EpElement ep = new EpElement(ph.Pts, this.CurrentPacketNumber);
                            epData.Add(ep);
                            break;
                        }
                    }
                    break;

                case (byte)ElementaryStreamTypes.VIDEO_STREAM_MPEG2:
                    for (i = ph.TotalHeaderLength; i < data.Length; i++)
                    {
                        marker <<= 8;
                        marker  |= data[i];
                        if (marker == Constants.MPEG2_SEQ_CODE && ph.HasPts)
                        {
                            EpElement ep = new EpElement(ph.Pts, this.CurrentPacketNumber);
                            epData.Add(ep);
                            break;
                        }
                    }
                    break;

                case (byte)ElementaryStreamTypes.VIDEO_STREAM_H264:
                    for (i = ph.TotalHeaderLength; i < data.Length; i++)
                    {
                        marker <<= 8;
                        marker  |= data[i];
                        if ((marker & 0xffffff9f) == Constants.H264_PREFIX && ph.HasPts)
                        {
                            EpElement ep = new EpElement(ph.Pts, this.CurrentPacketNumber);
                            epData.Add(ep);
                            break;
                        }
                    }
                    break;
                }
            }
            if (ph.HasPts && ptsOffset != 0)
            {
                Int64 time = ph.Pts + ptsOffset;
                if (time < 0)
                {
                    time += Constants.MAX_PTS_CLOCK;
                }
                else if (time > Constants.MAX_PTS_CLOCK)
                {
                    time -= Constants.MAX_PTS_CLOCK;
                }
                ph.Pts = time;
                for (i = 9; i < 14; i++)
                {
                    pp[i] = ph[i]; // copy PTS
                }
                if (ph.HasDts)
                {
                    time = ph.Dts + ptsOffset;
                    if (time < 0)
                    {
                        time += Constants.MAX_PTS_CLOCK;
                    }
                    else if (time > Constants.MAX_PTS_CLOCK)
                    {
                        time -= Constants.MAX_PTS_CLOCK;
                    }
                    ph.Dts = time;
                    for (i = 14; i < 19; i++)
                    {
                        pp[i] = ph[i]; // copy DTS
                    }
                }
            }

            lastPacket = pp;
            return(pp);
        }