Esempio n. 1
0
 public TsFile(string content, string fileName, IReadOnlyCollection <string> filePath, TsFileType fileType)
 {
     Content  = content;
     FileName = fileName;
     FilePath = filePath;
     FileType = fileType;
 }
Esempio n. 2
0
 public BlueMux(string fileName, TsFileType fileType, List <StreamInfo> StreamsToKeep, bool fAsync, bool fProcessAudio, bool fMlpToAc3)
 {
     fsw                = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None, Constants.DISK_BUFFER, fAsync);
     this.tsiow         = new TsIo(null, fsw, Constants.DISK_BUFFER);
     this.fileType      = fileType;
     this.StreamsToKeep = StreamsToKeep;
     this.buffer        = new List <TsPacket>();
     this.currentPcr    = -1;
     this.lastPcr       = -1;
     this.lastDelta     = -1;
     this.pcrOffset     = -1;
     this.lastPts       = -1;
     this.currentPts    = -1;
     this.ptsDelta      = -1;
     this.ptsOffset     = 0;
     this.ptsCount      = -1;
     this.pcrPacket     = null;
     this.header        = new byte[4];
     this.supHeader     = new byte[10];
     this.supHeader[0]  = (byte)'P';
     this.supHeader[1]  = (byte)'G';
     this.VideoType     = 0;
     this.lastPacket    = null;
     this.sitCount      = 0;
     this.packetCount   = 0;
     this.epData        = new List <EpElement>();
     this.pmtStreams    = null;
     this.lastPtsList   = new Dictionary <ushort, long>();
     this.soundFrames   = new Dictionary <ushort, List <byte> >();
     this.processAudio  = fProcessAudio;
     this.MlpToAc3      = fMlpToAc3;
     CreatePsi();
 }
Esempio n. 3
0
        private static string FileTypeString(TsFileType tsFileType)
        {
            switch (tsFileType)
            {
            case TsFileType.TypeScript:
                return("ts");

            case TsFileType.Definition:
                return("d.ts");

            default:
                throw new ArgumentOutOfRangeException(nameof(tsFileType), tsFileType, null);
            }
        }
Esempio n. 4
0
 public BlueMux(string fileName, TsFileType fileType, List<StreamInfo> StreamsToKeep, bool fAsync, bool fProcessAudio, bool fMlpToAc3)
 {
     fsw = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None, Constants.DISK_BUFFER, fAsync);
     this.tsiow = new TsIo(null, fsw, Constants.DISK_BUFFER);
     this.fileType = fileType;
     this.StreamsToKeep = StreamsToKeep;
     this.buffer = new List<TsPacket>();
     this.currentPcr = -1;
     this.lastPcr = -1;
     this.lastDelta = -1;
     this.pcrOffset = -1;
     this.lastPts = -1;
     this.currentPts = -1;
     this.ptsDelta = -1;
     this.ptsOffset = 0;
     this.ptsCount = -1;
     this.pcrPacket = null;
     this.header = new byte[4];
     this.supHeader = new byte[10];
     this.supHeader[0] = (byte)'P';
     this.supHeader[1] = (byte)'G';
     this.VideoType = 0;
     this.lastPacket = null;
     this.sitCount = 0;
     this.packetCount = 0;
     this.epData = new List<EpElement>();
     this.pmtStreams = null;
     this.lastPtsList = new Dictionary<ushort, long>();
     this.soundFrames = new Dictionary<ushort, List<byte>>();
     this.processAudio = fProcessAudio;
     this.MlpToAc3 = fMlpToAc3;
     CreatePsi();
 }
Esempio n. 5
0
        public void StartMuxing(string outPath, BackgroundWorker worker, TsFileType outType, List<ushort> pidsToKeep, TimeSpan ts, TimeSpan te, bool useAsync, bool processAudio, bool MlpToAc3, PesFile input, PesFile secondary, TimeSpan offset, TimeSpan chapterLen)
        {
            inFile = input;
            bw = worker;
            List<StreamInfo> sis = new List<StreamInfo>();
            PesPacket[] ppa = null;
            PesPacket[] spa = null;
            BluRayOutput bo = null;

            Int64 startTrim = inFile.StartPcr + ((Int64)ts.TotalSeconds * (Int64)Constants.MPEG2TS_CLOCK_RATE);
            if (startTrim > Constants.MAX_MPEG2TS_CLOCK)
                startTrim -= Constants.MAX_MPEG2TS_CLOCK;
            foreach (StreamInfo si in inFile.StreamInfos)
                if (pidsToKeep.Contains(si.ElementaryPID))
                    sis.Add(si);
            if(null != secondary && pidsToKeep.Contains(secondary.StreamInfos[0].ElementaryPID))
                sis.Add(secondary.StreamInfos[0]);

            switch(outType)
            {
                case TsFileType.BLU_RAY:
                    bo = new BluRayOutput(outPath, chapterLen);
                    outFile = new BlueMux(Path.Combine(outPath, @"BDMV\STREAM\00001.m2ts"), outType, sis, useAsync, processAudio, MlpToAc3);
                    break;

                case TsFileType.MKV:
                    outFile = new MkvMux(outPath, sis, useAsync, processAudio);
                    break;

                case TsFileType.DEMUX:
                    outFile = new DeMux(outPath, sis, useAsync, processAudio);
                    break;

                default:
                    outFile = new BlueMux(outPath, outType, sis, useAsync, processAudio, MlpToAc3);
                    break;
            }

            if (ts.TotalSeconds == 0)
                inFile.Seek(-1);
            else
                inFile.Seek(startTrim);

            if (te.TotalSeconds > 0)
            {
                endTrim = inFile.EndPcr - ((Int64)te.TotalSeconds * (Int64)Constants.MPEG2TS_CLOCK_RATE);
                if (endTrim < 0)
                    endTrim += Constants.MAX_MPEG2TS_CLOCK;
            }
            inFile.SetPcrDelegate(new PcrChanged(UpdatePcr));
            inFile.SetPtsDelegate(new PtsChanged(UpdatePts));
            startOffset = startTrim + ((Int64)offset.TotalSeconds * (Int64)Constants.MPEG2TS_CLOCK_RATE);
            if (startOffset > Constants.MAX_MPEG2TS_CLOCK)
                startOffset -= Constants.MAX_MPEG2TS_CLOCK;
            for (ppa = inFile.GetNextPesPackets(); ppa != null && ppa.Length > 0 && subtitles == false; ppa = inFile.GetNextPesPackets())
            {
                if (worker.CancellationPending || done)
                    goto leave_routine;
                foreach (PesPacket pp in ppa)
                    if (null != pp)
                        outFile.MuxPacket(pp);
                    else
                        goto leave_routine;
            }
            if (subtitles)
            {
                if (null != secondary && ppa != null && ppa.Length > 0)
                {
                    secondary.Seek(-1);
                    spa = secondary.GetNextPesPackets();
                    PesPacket sp = spa[0];
                    PesHeader sh = sp.GetHeader();
                    PesHeader ph = ppa[0].GetHeader();
                    while (ph == null || ph.HasPts == false)
                    {
                        if (worker.CancellationPending || done)
                            goto leave_routine;
                        foreach (PesPacket pp in ppa)
                            if (null != pp)
                                outFile.MuxPacket(pp);
                        ppa = inFile.GetNextPesPackets();
                        if (ppa == null || ppa.Length == 0 || ppa[0] == null)
                            goto leave_routine;
                        ph = ppa[0].GetHeader();
                    }
                    Int64 ptsOffset = ph.Pts - sh.Pts;
                    bool clock = true;
                    for (; ppa != null && ppa.Length > 0 && ppa[0] != null; ppa = inFile.GetNextPesPackets())
                    {
                        foreach (PesPacket pp in ppa)
                        {
                            ph = pp.GetHeader();
                            if (sh != null && ph != null && ph.HasPts)
                            {
                                if (clock)
                                {
                                    Int64 time = sh.Pts + ptsOffset;
                                    if (time < 0)
                                        time += Constants.MAX_PTS_CLOCK;
                                    else if (time > Constants.MAX_PTS_CLOCK)
                                        time -= Constants.MAX_PTS_CLOCK;
                                    sh.Pts = time;
                                    for (int i = 9; i < 14; i++)
                                        sp[i] = sh[i]; // copy PTS
                                    if (sh.HasDts)
                                    {
                                        time = sh.Dts + ptsOffset;
                                        if (time < 0)
                                            time += Constants.MAX_PTS_CLOCK;
                                        else if (time > Constants.MAX_PTS_CLOCK)
                                            time -= Constants.MAX_PTS_CLOCK;
                                        sh.Dts = time;
                                        for (int i = 14; i < 19; i++)
                                            sp[i] = sh[i]; // copy DTS
                                    }
                                    clock = false;
                                }
                                Int64 delta = sh.Pts - ph.Pts;
                                if (delta > (0 - Constants.PTS_CLOCK_RATE) && delta < Constants.PTS_CLOCK_RATE)
                                {
                                    outFile.MuxPacket(sp);
                                    spa = secondary.GetNextPesPackets();
                                    if (spa != null && spa.Length > 0 && spa[0] != null)
                                    {
                                        sp = spa[0];
                                        sh = sp.GetHeader();
                                        clock = true;
                                    }
                                }
                            }
                            outFile.MuxPacket(pp);
                        }
                    }
                }
                else
                {
                    for (; ppa != null && ppa.Length > 0; ppa = inFile.GetNextPesPackets())
                    {
                        if (worker.CancellationPending || done)
                            goto leave_routine;
                        foreach (PesPacket pp in ppa)
                            if (null != pp)
                                outFile.MuxPacket(pp);
                            else
                                goto leave_routine;
                    }
                }
            }
            leave_routine:
            outFile.Close();
            if (outType == TsFileType.BLU_RAY)
            {
                bo.Author(outFile.EpData, outFile.Psi, outFile.CurrentPacketNumber);
            }
        }
Esempio n. 6
0
 public void StartMuxing(string outPath, BackgroundWorker worker, TsFileType outType, List<ushort> pidsToKeep, TimeSpan ts, TimeSpan te, bool useAsync, PesFile input)
 {
     StartMuxing(outPath, worker, outType, pidsToKeep, ts, te, useAsync, false, false, input, null, TimeSpan.Zero, TimeSpan.Zero);
 }
Esempio n. 7
0
 protected PesFile(BackgroundWorker openWorker)
 {
     fs = null;
     tsior = null;
     fileType = TsFileType.UNKNOWN;
     startPcr = -1;
     endPcr = -1;
     sis = null;
     pcrDelegate = null;
     ptsDelegate = null;
     this.openWorker = openWorker;
     lastPercent = 0;
 }
Esempio n. 8
0
        public void StartMuxing(string outPath, BackgroundWorker worker, TsFileType outType, List <ushort> pidsToKeep, TimeSpan ts, TimeSpan te, bool useAsync, bool processAudio, bool MlpToAc3, PesFile input, PesFile secondary, TimeSpan offset, TimeSpan chapterLen)
        {
            inFile = input;
            bw     = worker;
            List <StreamInfo> sis = new List <StreamInfo>();

            PesPacket[]  ppa = null;
            PesPacket[]  spa = null;
            BluRayOutput bo  = null;

            Int64 startTrim = inFile.StartPcr + ((Int64)ts.TotalSeconds * (Int64)Constants.MPEG2TS_CLOCK_RATE);

            if (startTrim > Constants.MAX_MPEG2TS_CLOCK)
            {
                startTrim -= Constants.MAX_MPEG2TS_CLOCK;
            }
            foreach (StreamInfo si in inFile.StreamInfos)
            {
                if (pidsToKeep.Contains(si.ElementaryPID))
                {
                    sis.Add(si);
                }
            }
            if (null != secondary && pidsToKeep.Contains(secondary.StreamInfos[0].ElementaryPID))
            {
                sis.Add(secondary.StreamInfos[0]);
            }

            switch (outType)
            {
            case TsFileType.BLU_RAY:
                bo      = new BluRayOutput(outPath, chapterLen);
                outFile = new BlueMux(Path.Combine(outPath, @"BDMV\STREAM\00001.m2ts"), outType, sis, useAsync, processAudio, MlpToAc3);
                break;

            case TsFileType.MKV:
                outFile = new MkvMux(outPath, sis, useAsync, processAudio);
                break;

            case TsFileType.DEMUX:
                outFile = new DeMux(outPath, sis, useAsync, processAudio);
                break;

            default:
                outFile = new BlueMux(outPath, outType, sis, useAsync, processAudio, MlpToAc3);
                break;
            }

            if (ts.TotalSeconds == 0)
            {
                inFile.Seek(-1);
            }
            else
            {
                inFile.Seek(startTrim);
            }

            if (te.TotalSeconds > 0)
            {
                endTrim = inFile.EndPcr - ((Int64)te.TotalSeconds * (Int64)Constants.MPEG2TS_CLOCK_RATE);
                if (endTrim < 0)
                {
                    endTrim += Constants.MAX_MPEG2TS_CLOCK;
                }
            }
            inFile.SetPcrDelegate(new PcrChanged(UpdatePcr));
            inFile.SetPtsDelegate(new PtsChanged(UpdatePts));
            startOffset = startTrim + ((Int64)offset.TotalSeconds * (Int64)Constants.MPEG2TS_CLOCK_RATE);
            if (startOffset > Constants.MAX_MPEG2TS_CLOCK)
            {
                startOffset -= Constants.MAX_MPEG2TS_CLOCK;
            }
            for (ppa = inFile.GetNextPesPackets(); ppa != null && ppa.Length > 0 && subtitles == false; ppa = inFile.GetNextPesPackets())
            {
                if (worker.CancellationPending || done)
                {
                    goto leave_routine;
                }
                foreach (PesPacket pp in ppa)
                {
                    if (null != pp)
                    {
                        outFile.MuxPacket(pp);
                    }
                    else
                    {
                        goto leave_routine;
                    }
                }
            }
            if (subtitles)
            {
                if (null != secondary && ppa != null && ppa.Length > 0)
                {
                    secondary.Seek(-1);
                    spa = secondary.GetNextPesPackets();
                    PesPacket sp = spa[0];
                    PesHeader sh = sp.GetHeader();
                    PesHeader ph = ppa[0].GetHeader();
                    while (ph == null || ph.HasPts == false)
                    {
                        if (worker.CancellationPending || done)
                        {
                            goto leave_routine;
                        }
                        foreach (PesPacket pp in ppa)
                        {
                            if (null != pp)
                            {
                                outFile.MuxPacket(pp);
                            }
                        }
                        ppa = inFile.GetNextPesPackets();
                        if (ppa == null || ppa.Length == 0 || ppa[0] == null)
                        {
                            goto leave_routine;
                        }
                        ph = ppa[0].GetHeader();
                    }
                    Int64 ptsOffset = ph.Pts - sh.Pts;
                    bool  clock     = true;
                    for (; ppa != null && ppa.Length > 0 && ppa[0] != null; ppa = inFile.GetNextPesPackets())
                    {
                        foreach (PesPacket pp in ppa)
                        {
                            ph = pp.GetHeader();
                            if (sh != null && ph != null && ph.HasPts)
                            {
                                if (clock)
                                {
                                    Int64 time = sh.Pts + ptsOffset;
                                    if (time < 0)
                                    {
                                        time += Constants.MAX_PTS_CLOCK;
                                    }
                                    else if (time > Constants.MAX_PTS_CLOCK)
                                    {
                                        time -= Constants.MAX_PTS_CLOCK;
                                    }
                                    sh.Pts = time;
                                    for (int i = 9; i < 14; i++)
                                    {
                                        sp[i] = sh[i]; // copy PTS
                                    }
                                    if (sh.HasDts)
                                    {
                                        time = sh.Dts + ptsOffset;
                                        if (time < 0)
                                        {
                                            time += Constants.MAX_PTS_CLOCK;
                                        }
                                        else if (time > Constants.MAX_PTS_CLOCK)
                                        {
                                            time -= Constants.MAX_PTS_CLOCK;
                                        }
                                        sh.Dts = time;
                                        for (int i = 14; i < 19; i++)
                                        {
                                            sp[i] = sh[i]; // copy DTS
                                        }
                                    }
                                    clock = false;
                                }
                                Int64 delta = sh.Pts - ph.Pts;
                                if (delta > (0 - Constants.PTS_CLOCK_RATE) && delta < Constants.PTS_CLOCK_RATE)
                                {
                                    outFile.MuxPacket(sp);
                                    spa = secondary.GetNextPesPackets();
                                    if (spa != null && spa.Length > 0 && spa[0] != null)
                                    {
                                        sp    = spa[0];
                                        sh    = sp.GetHeader();
                                        clock = true;
                                    }
                                }
                            }
                            outFile.MuxPacket(pp);
                        }
                    }
                }
                else
                {
                    for (; ppa != null && ppa.Length > 0; ppa = inFile.GetNextPesPackets())
                    {
                        if (worker.CancellationPending || done)
                        {
                            goto leave_routine;
                        }
                        foreach (PesPacket pp in ppa)
                        {
                            if (null != pp)
                            {
                                outFile.MuxPacket(pp);
                            }
                            else
                            {
                                goto leave_routine;
                            }
                        }
                    }
                }
            }
leave_routine:
            outFile.Close();
            if (outType == TsFileType.BLU_RAY)
            {
                bo.Author(outFile.EpData, outFile.Psi, outFile.CurrentPacketNumber);
            }
        }
Esempio n. 9
0
 public void StartMuxing(string outPath, BackgroundWorker worker, TsFileType outType, List <ushort> pidsToKeep, TimeSpan ts, TimeSpan te, bool useAsync, PesFile input)
 {
     StartMuxing(outPath, worker, outType, pidsToKeep, ts, te, useAsync, false, false, input, null, TimeSpan.Zero, TimeSpan.Zero);
 }