Exemplo n.º 1
0
 private void CreatePsi()
 {
     pidMappings = new Dictionary<ushort, ushort>();
     Continuities = new Dictionary<ushort, byte>();
     ushort currentVideoPid = Constants.DEFAULT_VIDEO_PID;
     ushort currentAudioPid = Constants.DEFAULT_AUDIO_PID;
     ushort currentSubPid = Constants.DEFAULT_SUBTITLE_PID;
     ushort currentPresPid = Constants.DEFAULT_PRESENTATION_GRAPHICS_PID;
     ushort currentIntPid = Constants.DEFAULT_INTERACTIVE_GRAPHICS_PID;
     pmtStreams = new List<StreamInfo>();
     StreamInfo sitemp = null;
     foreach (StreamInfo si in StreamsToKeep)
     {
         if (pidMappings.ContainsKey(si.ElementaryPID))
             throw new ArgumentException(String.Format("Invalid/Duplicate StreamInfo with pid {0}", si.ElementaryPID));
         switch (si.StreamType)
         {
             case ElementaryStreamTypes.AUDIO_STREAM_AC3:
             case ElementaryStreamTypes.AUDIO_STREAM_AC3_PLUS:
                 pidMappings.Add(si.ElementaryPID, currentAudioPid);
                 if(this.fileType == TsFileType.BLU_RAY)
                     sitemp = new StreamInfo(si.StreamType, currentAudioPid);
                 else
                     sitemp = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3, currentAudioPid);
                 Continuities.Add(currentAudioPid, 0);
                 currentAudioPid++;
                 break;
             case ElementaryStreamTypes.AUDIO_STREAM_AC3_TRUE_HD:
                 pidMappings.Add(si.ElementaryPID, currentAudioPid);
                 if(MlpToAc3)
                     sitemp = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3, currentAudioPid);
                 else
                     sitemp = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3_TRUE_HD, currentAudioPid);
                 Continuities.Add(currentAudioPid, 0);
                 currentAudioPid++;
                 break;
             case ElementaryStreamTypes.AUDIO_STREAM_DTS_HD:
             case ElementaryStreamTypes.AUDIO_STREAM_DTS_HD_MASTER_AUDIO:
                 pidMappings.Add(si.ElementaryPID, currentAudioPid);
                 if (MlpToAc3)
                     sitemp = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_DTS, currentAudioPid);
                 else
                     sitemp = new StreamInfo(si.StreamType, currentAudioPid);
                 Continuities.Add(currentAudioPid, 0);
                 currentAudioPid++;
                 break;
             case ElementaryStreamTypes.AUDIO_STREAM_DTS:
             case ElementaryStreamTypes.AUDIO_STREAM_LPCM:
             case ElementaryStreamTypes.AUDIO_STREAM_MPEG1:
             case ElementaryStreamTypes.AUDIO_STREAM_MPEG2:
                 pidMappings.Add(si.ElementaryPID, currentAudioPid);
                 sitemp = new StreamInfo(si.StreamType, currentAudioPid);
                 Continuities.Add(currentAudioPid, 0);
                 currentAudioPid++;
                 break;
             case ElementaryStreamTypes.SUBTITLE_STREAM:
                 pidMappings.Add(si.ElementaryPID, currentSubPid);
                 sitemp = new StreamInfo(si.StreamType, currentSubPid);
                 Continuities.Add(currentSubPid, 0);
                 currentSubPid++;
                 break;
             case ElementaryStreamTypes.PRESENTATION_GRAPHICS_STREAM:
                 pidMappings.Add(si.ElementaryPID, currentPresPid);
                 sitemp = new StreamInfo(si.StreamType, currentPresPid);
                 Continuities.Add(currentPresPid, 0);
                 currentPresPid++;
                 break;
             case ElementaryStreamTypes.INTERACTIVE_GRAPHICS_STREAM:
                 pidMappings.Add(si.ElementaryPID, currentIntPid);
                 sitemp = new StreamInfo(si.StreamType, currentIntPid);
                 Continuities.Add(currentIntPid, 0);
                 currentIntPid++;
                 break;
             case ElementaryStreamTypes.VIDEO_STREAM_H264:
             case ElementaryStreamTypes.VIDEO_STREAM_MPEG1:
             case ElementaryStreamTypes.VIDEO_STREAM_MPEG2:
             case ElementaryStreamTypes.VIDEO_STREAM_VC1:
                 pidMappings.Add(si.ElementaryPID, currentVideoPid);
                 sitemp = new StreamInfo(si.StreamType, currentVideoPid);
                 Continuities.Add(currentVideoPid, 0);
                 currentVideoPid++;
                 if (0 == VideoType)
                     VideoType = (byte)si.StreamType;
                 break;
         }
         sitemp.ElementaryDescriptors = si.ElementaryDescriptors;
         sitemp.AspectRatio = si.AspectRatio;
         sitemp.AudioPresentationType = si.AudioPresentationType;
         sitemp.FrameRate = si.FrameRate;
         sitemp.SamplingFrequency = si.SamplingFrequency;
         sitemp.VideoFormat = si.VideoFormat;
         pmtStreams.Add(sitemp);
     }
     pat = new PatPacket();
     ProgramInfo[] pi = new ProgramInfo[2];
     pi[0] = new ProgramInfo(0, Constants.SIT_PID);
     pi[1] = new ProgramInfo(1, Constants.DEFAULT_PMT_PID);
     pat.Programs = pi;
     pmt = new PmtPacket();
     pmt.PID = Constants.DEFAULT_PMT_PID;
     pmt.ElementaryStreams = pmtStreams.ToArray();
     List<byte> programDescriptors = new List<byte>();
     programDescriptors.AddRange(Constants.copy_control_descriptor);
     programDescriptors.AddRange(Constants.hdmv_registration_descriptor);
     pmt.ProgramDescriptorsData = programDescriptors.ToArray();
     sit = new SitPacket();
     buffer.Add(pat);
     pat.IncrementContinuityCounter();
     buffer.Add(pmt);
     pmt.IncrementContinuityCounter();
     buffer.Add(sit);
     sit.IncrementContinuityCounter();
 }
Exemplo n.º 2
0
        protected override void GetInitialValues()
        {
            GetTimeStamps();

            Dictionary<byte, StreamInfo> evoStreams = new Dictionary<byte, StreamInfo>();
            fs.Seek(0, SeekOrigin.Begin);
            byte[] inBuff = new byte[Constants.DISK_BUFFER]; // 72MB IS TOO BIG! CAUSES FAILURE: << 2]; note this was line 463
            int Length = fs.Read(inBuff, 0, inBuff.Length);
            UInt32 marker = 0xffffffff;
            int stuffing = 0;
            for (int i = 0; i < Length; i++)
            {
                ReportProgress(50 * i / Length);
                marker = marker << 8;
                marker &= 0xffffff00;
                marker += inBuff[i];
                if ((marker & 0xffffff00) == 0x00000100)
                {
                    switch (marker & 0xff)
                    {
                        case 0xba:
                            // pack start code
                            if (Length <= i + 10)
                            {
                                i = Length;
                                break;
                            }
                            i += 10;
                            stuffing = inBuff[i] & 0x7;
                            if (Length <= i + stuffing)
                            {
                                i = Length;
                                break;
                            }
                            i += stuffing;
                            stuffing = 0;
                            break;
                        default:
                            if (inBuff[i] == Constants.PES_PRIVATE1)
                            {
                                // skip to the end of the header
                                int endOfHeader = i + 6 + inBuff[i + 5];
                                if (inBuff.Length > endOfHeader + 4)
                                {
                                    if ((inBuff[endOfHeader] & 0xe0) == Constants.PES_PRIVATE_SUBTITLE)
                                    {
                                        byte subid = inBuff[endOfHeader];
                                        if (evoStreams.ContainsKey(subid) == false)
                                        {
                                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.PRESENTATION_GRAPHICS_STREAM, subid);
                                            evoStreams.Add(subid, si);
                                        }
                                    }
                                    else if ((inBuff[endOfHeader] & 0xf8) == Constants.PES_PRIVATE_AC3)
                                    {
                                        byte subid = inBuff[endOfHeader];
                                        if (evoStreams.ContainsKey(subid) == false)
                                        {
                                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3, subid);
                                            evoStreams.Add(subid, si);
                                        }
                                    }
                                    else if ((inBuff[endOfHeader] & 0xf8) == Constants.PES_PRIVATE_DTS_HD)
                                    {
                                        byte subid = inBuff[endOfHeader];
                                        if (evoStreams.ContainsKey(subid) == false)
                                        {
                                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_DTS_HD, subid);
                                            evoStreams.Add(subid, si);
                                        }
                                    }
                                    else if ((inBuff[endOfHeader] & 0xf0) == Constants.PES_PRIVATE_AC3_PLUS)
                                    {
                                        byte subid = inBuff[endOfHeader];
                                        if (evoStreams.ContainsKey(subid) == false)
                                        {
                                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3_PLUS, subid);
                                            evoStreams.Add(subid, si);
                                        }
                                    }
                                    else if ((inBuff[endOfHeader] & 0xf8) == Constants.PES_PRIVATE_LPCM)
                                    {
                                        byte subid = inBuff[endOfHeader];
                                        if (evoStreams.ContainsKey(subid) == false)
                                        {
                                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_LPCM, subid);
                                            evoStreams.Add(subid, si);
                                        }
                                    }
                                    else if ((inBuff[endOfHeader] & 0xf8) == Constants.PES_PRIVATE_AC3_TRUE_HD)
                                    {
                                        byte subid = inBuff[endOfHeader];
                                        if (evoStreams.ContainsKey(subid) == false)
                                        {
                                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3_TRUE_HD, subid);
                                            evoStreams.Add(subid, si);
                                        }
                                    }
                                }
                            }
                            else if ((inBuff[i] & 0xe0) == Constants.PES_AUDIO_MPEG)
                            {
                                byte subid = inBuff[i];
                                if (evoStreams.ContainsKey(subid) == false)
                                {
                                    StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_MPEG2, subid);
                                    evoStreams.Add(subid, si);
                                }
                            }
                            else if ((inBuff[i] & 0xf0) == Constants.PES_VIDEO)
                            {
                                byte subid = inBuff[i];
                                if (evoStreams.ContainsKey(subid) == false)
                                {
                                    // need to differenciate between H.264 and MPEG2
                                    if (Length <= i + 5)
                                    {
                                        i = Length;
                                        break;
                                    }
                                    ElementaryStreamTypes videoFormat = ElementaryStreamTypes.INVALID;
                                    int startIndex = i + 6 + inBuff[i + 5];
                                    if (Length <= startIndex + 3)
                                    {
                                        i = Length;
                                        break;
                                    }
                                    UInt32 format = (UInt32)inBuff[startIndex] << 24;
                                    format += (UInt32)inBuff[startIndex + 1] << 16;
                                    format += (UInt32)inBuff[startIndex + 2] << 8;
                                    format += inBuff[startIndex + 3];
                                    if ((format & 0xffff) == 0x1)
                                    {
                                        videoFormat = ElementaryStreamTypes.VIDEO_STREAM_H264;
                                        StreamInfo si = new StreamInfo(videoFormat, subid);
                                        evoStreams.Add(subid, si);
                                    }
                                    else if ((format & 0xffffff00) == 0x100)
                                    {
                                        videoFormat = ElementaryStreamTypes.VIDEO_STREAM_MPEG2;
                                        StreamInfo si = new StreamInfo(videoFormat, subid);
                                        evoStreams.Add(subid, si);
                                    }
                                }
                            }
                            else if (inBuff[i] == Constants.PES_VIDEO_VC1)
                            {
                                byte subid = GetVC1SubstreamId(inBuff, i + 3);
                                if (subid != 0 && evoStreams.ContainsKey(subid) == false)
                                {
                                    StreamInfo si = new StreamInfo(ElementaryStreamTypes.VIDEO_STREAM_VC1, subid);
                                    si.ElementaryDescriptors = Constants.vc1_descriptor;
                                    evoStreams.Add(subid, si);
                                }
                            }

                            // other PES packet
                            if (Length < i + 3)
                            {
                                i = Length;
                                break;
                            }
                            stuffing = (inBuff[i + 1] << 8) + inBuff[i + 2];
                            i += 2;
                            if (Length < i + stuffing)
                            {
                                i = Length;
                                break;
                            }
                            i += stuffing;
                            stuffing = 0;
                            break;
                    }
                    marker = 0xffffffff;
                }
            }
            if (evoStreams.Count > 0)
            {
                sis = new StreamInfo[evoStreams.Values.Count];
                evoStreams.Values.CopyTo(sis, 0);
            }
        }
Exemplo n.º 3
0
        protected override void GetInitialValues()
        {
            GetTimestamps();
            Seek(-1);
            PatPacket pp = null;
            PmtPacket pm = null;
            bool bluray = false;
            bool validPmt = false;
            ushort pmtPid = 0;
            int i = 0;
            Dictionary<ushort, StreamInfo> streams = new Dictionary<ushort, StreamInfo>();
            while (true)
            {
                for (i = tsior.Read(tsPack, 0, 1 + stride); (i > 0) && (tsPack[stride] != Constants.SYNC_BYTE); i = tsior.Read(tsPack, stride, 1))
                    ; // ensure a good sync byte
                if (i == 0)
                    break; // end of stream
                i = tsior.Read(tsPack, stride + 1, tsPack.Length - (stride + 1));
                if (i < tsPack.Length - (stride + 1))
                    break; // end of stream
                ts.SetData(tsPack, stride);
                if (ts.PID == Constants.PAT_PID)
                {
                    pp = new PatPacket(ts.GetData());
                    ProgramInfo[] pi = pp.Programs;
                    if (null != pi && pi.Length == 2)
                    {
                        if ((pi[0].ProgramNumber == 0 && pi[0].ProgramPID == 0x001f && pi[1].ProgramNumber == 1 && pi[1].ProgramPID == 0x0100)
                            || (pi[1].ProgramNumber == 0 && pi[1].ProgramPID == 0x001f && pi[0].ProgramNumber == 1 && pi[0].ProgramPID == 0x0100))
                        {
                            bluray = true;
                        }
                    }
                    else if (null != pi && pi[0] != null && pi[0].ProgramNumber == 1)
                        pmtPid = pi[0].ProgramPID;
                }
                else if (bluray && ts.PID == 0x0100)
                {
                    if (pm == null)
                        pm = new PmtPacket(ts.GetData());
                    else if (pm.Complete == false)
                        pm.AddData(ts.Payload, 0, ts.Payload.Length);
                    if (pm.Complete)
                    {
                        sis = pm.ElementaryStreams;
                        dt = pm.DtcpInfo;
                        return;
                    }
                }
                else if (ts.PID == pmtPid)
                {
                    pm = new PmtPacket(ts.GetData());
                    sis = pm.ElementaryStreams;
                    dt = pm.DtcpInfo;
                }
                else if (streams.ContainsKey(ts.PID) == false && ts.HasPesHeader)
                {
                    byte[] payload = ts.Payload;
                    PesHeader ph = new PesHeader(ts.Payload);
                    PesPacket pes = new PesPacket(payload, 0, payload.Length, ts.PID);
                    if (ph.StreamId == Constants.PES_PRIVATE1)
                    {
                        byte[] audio = pes.GetPayload();
                        AC3Info ac3 = new AC3Info(audio, 0);
                        DtsInfo dts = new DtsInfo(audio, 0);
                        MlpInfo mlp = new MlpInfo(audio, 0);
                        if (ac3.Valid)
                        {
                            if (ac3.SyntaxType == Ac3SyntaxType.Standard || ac3.SyntaxType == Ac3SyntaxType.Alternative)
                            {
                                StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3, ts.PID);
                                streams.Add(ts.PID, si);
                            }
                            else if (ac3.SyntaxType == Ac3SyntaxType.Enhanced)
                            {
                                StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3_PLUS, ts.PID);
                                streams.Add(ts.PID, si);
                            }
                        }
                        else if (dts.Valid)
                        {
                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_DTS_HD, ts.PID);
                            streams.Add(ts.PID, si);
                        }
                        else if (mlp.Valid)
                        {
                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3_TRUE_HD, ts.PID);
                            streams.Add(ts.PID, si);
                        }
                        else if ((pes.ExtendedId & 0xf8) == Constants.PES_PRIVATE_LPCM)
                        {
                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_LPCM, ts.PID);
                            streams.Add(ts.PID, si);
                        }
                    }
                    else if ((ph.StreamId & 0xe0) == Constants.PES_AUDIO_MPEG)
                    {
                        StreamInfo si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_MPEG2, ts.PID);
                        streams.Add(ts.PID, si);
                    }
                    else if ((ph.StreamId & 0xf0) == Constants.PES_VIDEO)
                    {
                        UInt32 format = pes.ExtendedType;
                        if ((format & 0xffff) == 0x1)
                        {
                        }
                        else if ((format & 0xffffff00) == 0x100)
                        {
                        }

                        H264Info h264 = new H264Info(payload, 0);
                        Mpeg2Info mpg2 = new Mpeg2Info(payload, 0);
                        if (h264.Valid)
                        {
                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.VIDEO_STREAM_H264, ts.PID);
                            streams.Add(ts.PID, si);
                        }
                        else if (mpg2.Valid)
                        {
                            StreamInfo si = new StreamInfo(ElementaryStreamTypes.VIDEO_STREAM_MPEG2, ts.PID);
                            streams.Add(ts.PID, si);
                        }
                    }
                    else if (ph.StreamId == Constants.PES_VIDEO_VC1)
                    {
                        StreamInfo si = new StreamInfo(ElementaryStreamTypes.VIDEO_STREAM_VC1, ts.PID);
                        streams.Add(ts.PID, si);
                    }
                }
                if (sis != null)
                {
                    validPmt = true;
                    foreach (StreamInfo si in sis)
                    {
                        if (streams.ContainsKey(si.ElementaryPID) == false)
                        {
                            validPmt = false;
                            break;
                        }
                    }
                    if (validPmt)
                        return;
                }
                if (fs.Position > (Constants.DISK_BUFFER << 1))
                    break;
            }
            sis = new StreamInfo[streams.Values.Count];
            streams.Values.CopyTo(sis, 0);
        }
Exemplo n.º 4
0
 private void ParseStream(StreamInfo si, byte[] payload)
 {
     switch (si.StreamType)
     {
         case ElementaryStreamTypes.VIDEO_STREAM_VC1:
             if ((si.ElementaryDescriptors == null) ||
                 si.FrameRate == FrameRate.Reserved ||
                 si.VideoFormat == VideoFormat.Reserved ||
                 si.AspectRatio == AspectRatio.Reserved)
             {
                 VC1SequenceInfo sq = new VC1SequenceInfo(payload, 0);
                 if (sq.Valid)
                 {
                     if (sq.AspectRatio != AspectRatio.Reserved)
                         si.AspectRatio = sq.AspectRatio;
                     if (sq.FrameRate != FrameRate.Reserved)
                         si.FrameRate = sq.FrameRate;
                     if (sq.VideoFormat != VideoFormat.Reserved)
                         si.VideoFormat = sq.VideoFormat;
                     if (si.ElementaryDescriptors == null)
                         si.ElementaryDescriptors = Constants.vc1_descriptor;
                 }
             }
             break;
         case ElementaryStreamTypes.AUDIO_STREAM_AC3:
             if ((si.ElementaryDescriptors == null) ||
                 si.AudioPresentationType == AudioPresentationType.Reserved ||
                 si.SamplingFrequency == SamplingFrequency.Reserved)
             {
                 AC3Info ac3 = new AC3Info(payload, 0);
                 if (ac3.Valid)
                 {
                     if (ac3.AudioPresentationType != AudioPresentationType.Reserved)
                         si.AudioPresentationType = ac3.AudioPresentationType;
                     if (ac3.SamplingFrequency != SamplingFrequency.Reserved)
                         si.SamplingFrequency = ac3.SamplingFrequency;
                     if (si.ElementaryDescriptors == null)
                         si.ElementaryDescriptors = ac3.ElementaryDescriptors;
                     if (ac3.SyntaxType == Ac3SyntaxType.Enhanced)
                         si.StreamType = ElementaryStreamTypes.AUDIO_STREAM_AC3_PLUS;
                 }
             }
             break;
         case ElementaryStreamTypes.AUDIO_STREAM_AC3_PLUS:
             if (si.AudioPresentationType == AudioPresentationType.Reserved ||
                 si.SamplingFrequency == SamplingFrequency.Reserved)
             {
                 AC3Info ac3 = new AC3Info(payload, 0);
                 if (ac3.Valid)
                 {
                     if (ac3.AudioPresentationType != AudioPresentationType.Reserved)
                         si.AudioPresentationType = ac3.AudioPresentationType;
                     if (ac3.SamplingFrequency != SamplingFrequency.Reserved)
                         si.SamplingFrequency = ac3.SamplingFrequency;
                     if (si.ElementaryDescriptors == null)
                         si.ElementaryDescriptors = ac3.ElementaryDescriptors;
                 }
             }
             break;
         case ElementaryStreamTypes.AUDIO_STREAM_AC3_TRUE_HD:
             if (si.AudioPresentationType == AudioPresentationType.Reserved ||
                 si.SamplingFrequency == SamplingFrequency.Reserved)
             {
                 MlpInfo ac3 = new MlpInfo(payload, 0);
                 if (ac3.Valid)
                 {
                     if (ac3.AudioPresentationType != AudioPresentationType.Reserved)
                         si.AudioPresentationType = ac3.AudioPresentationType;
                     if (ac3.SamplingFrequency != SamplingFrequency.Reserved)
                         si.SamplingFrequency = ac3.SamplingFrequency;
                     if (si.ElementaryDescriptors == null)
                         si.ElementaryDescriptors = ac3.ElementaryDescriptors;
                 }
             }
             break;
         case ElementaryStreamTypes.VIDEO_STREAM_H264:
             if ((si.ElementaryDescriptors == null) ||
                 si.FrameRate == FrameRate.Reserved ||
                 si.VideoFormat == VideoFormat.Reserved ||
                 si.AspectRatio == AspectRatio.Reserved)
             {
                 H264Info h264 = new H264Info(payload, 0);
                 if(h264.Valid)
                 {
                     if (h264.AspectRatio != AspectRatio.Reserved)
                         si.AspectRatio = h264.AspectRatio;
                     if (h264.FrameRate != FrameRate.Reserved)
                         si.FrameRate = h264.FrameRate;
                     if (h264.VideoFormat != VideoFormat.Reserved)
                         si.VideoFormat = h264.VideoFormat;
                     if (si.ElementaryDescriptors == null)
                         si.ElementaryDescriptors = h264.ElementaryDescriptors;
                 }
             }
             break;
         case ElementaryStreamTypes.AUDIO_STREAM_DTS:
         case ElementaryStreamTypes.AUDIO_STREAM_DTS_HD:
         case ElementaryStreamTypes.AUDIO_STREAM_DTS_HD_MASTER_AUDIO:
             if (si.AudioPresentationType == AudioPresentationType.Reserved ||
                 si.SamplingFrequency == SamplingFrequency.Reserved)
             {
                 DtsInfo dts = new DtsInfo(payload, 0);
                 if (dts.Valid)
                 {
                     if (dts.AudioPresentationType != AudioPresentationType.Reserved)
                         si.AudioPresentationType = dts.AudioPresentationType;
                     if (dts.SamplingFrequency != SamplingFrequency.Reserved)
                         si.SamplingFrequency = dts.SamplingFrequency;
                 }
             }
             break;
         case ElementaryStreamTypes.VIDEO_STREAM_MPEG2:
             if ((si.ElementaryDescriptors == null) ||
                 si.FrameRate == FrameRate.Reserved ||
                 si.VideoFormat == VideoFormat.Reserved ||
                 si.AspectRatio == AspectRatio.Reserved)
             {
                 Mpeg2Info mpeg2 = new Mpeg2Info(payload, 0);
                 if (mpeg2.Valid)
                 {
                     if (mpeg2.AspectRatio != AspectRatio.Reserved)
                         si.AspectRatio = mpeg2.AspectRatio;
                     if (mpeg2.FrameRate != FrameRate.Reserved)
                         si.FrameRate = mpeg2.FrameRate;
                     if (mpeg2.VideoFormat != VideoFormat.Reserved)
                         si.VideoFormat = mpeg2.VideoFormat;
                     if (si.ElementaryDescriptors == null)
                         si.ElementaryDescriptors = mpeg2.ElementaryDescriptors;
                 }
             }
             break;
     }
 }
Exemplo n.º 5
0
 protected override void GetInitialValues()
 {
     fs.Seek(0, SeekOrigin.Begin);
     UInt32 header = 0xffffffff;
     EbmlElement mkv = null;
     byte b = 0;
     for (int i = 0; i < Constants.DISK_BUFFER; i++)
     {
         int ib = fs.ReadByte();
         if (ib == -1)
             throw new FormatException("The specified file is too short");
         b = (byte)(ib & 0xff);
         header <<= 8;
         header |= b;
         if (header == Constants.MKVSEGMENT_START)
         {
             fs.Seek(-4, SeekOrigin.Current);
             mkv = EbmlElement.ParseEbml(fs);
             if (null == mkv)
                 throw new FormatException("Invalid/Corrupted MKV file");
             break;
         }
     }
     EbmlElement[] clusts = mkv.Children;
     foreach (EbmlElement clust in clusts)
     {
         ReportProgress((int)(50 * clust.DataStream.Position / clust.DataStream.Length));
         if (clust.Id == Constants.MKVCLUSTER_START)
         {
             Clusters.Add(GetClusterClock(clust), clust);
         }
         else if (clust.Id == Constants.MKVTRACKINFO_START)
         {
             EbmlElement[] tracks = clust.Children;
             ushort id = 0;
             List<StreamInfo> mkvStreams = new List<StreamInfo>();
             foreach (EbmlElement track in tracks)
             {
                 if (track.Id == 0xae)
                 {
                     EbmlElement[] trackAttributes = track.Children;
                     string codec = null;
                     ushort pid = 0;
                     UInt64 uid = 0;
                     byte[] data = null;
                     EbmlElement elementaryInfo = null;
                     foreach (EbmlElement trackAtt in trackAttributes)
                     {
                         switch (trackAtt.Id)
                         {
                             case 0xd7: // Track number
                                 for (Int64 i = 0; i < trackAtt.Size; i++)
                                 {
                                     pid <<= 8;
                                     pid |= (byte)fs.ReadByte();
                                 }
                                 break;
                             case 0x86: // Codec ID
                                 byte[] cid = new byte[(int)trackAtt.Size];
                                 trackAtt.DataStream.Read(cid, 0, (int)trackAtt.Size);
                                 codec = Encoding.ASCII.GetString(cid);
                                 break;
                             case 0x63a2: // Coded Private
                                 data = new byte[(int)trackAtt.Size];
                                 trackAtt.DataStream.Read(data, 0, (int)trackAtt.Size);
                                 break;
                             case 0x73c5: // Track UID
                                 for (Int64 i = 0; i < trackAtt.Size; i++)
                                 {
                                     uid <<= 8;
                                     uid |= (byte)fs.ReadByte();
                                 }
                                 break;
                             case 0xe0: // Video elementary stream info
                                 elementaryInfo = trackAtt;
                                 break;
                             case 0xe1: // Audio elementary stream info
                                 elementaryInfo = trackAtt;
                                 break;
                         }
                     }
                     if (codec == null || pid == 0)
                         throw new FormatException("Track info is invalid");
                     if (0 == string.Compare(codec, "V_MPEG2", true, CultureInfo.InvariantCulture) ||
                         0 == string.Compare(codec, "V_MPEG4/ISO/AVC", true, CultureInfo.InvariantCulture) ||
                         0 == string.Compare(codec, "V_MS/VFW/FOURCC", true, CultureInfo.InvariantCulture) ||
                         0 == string.Compare(codec, "A_AC3", true, CultureInfo.InvariantCulture) ||
                         0 == string.Compare(codec, "A_DTS", true, CultureInfo.InvariantCulture))
                     {
                         id++;
                         TrackInfo ti = new TrackInfo(id, codec, data, elementaryInfo);
                         TrackList.Add(id, ti);
                         StreamInfo si = null;
                         if (0 == string.Compare(codec, "V_MPEG2", true, CultureInfo.InvariantCulture))
                             si = new StreamInfo(ElementaryStreamTypes.VIDEO_STREAM_MPEG2, id);
                         else if (0 == string.Compare(codec, "V_MPEG4/ISO/AVC", true, CultureInfo.InvariantCulture))
                             si = new StreamInfo(ElementaryStreamTypes.VIDEO_STREAM_H264, id);
                         else if (0 == string.Compare(codec, "V_MS/VFW/FOURCC", true, CultureInfo.InvariantCulture))
                             si = new StreamInfo(ElementaryStreamTypes.VIDEO_STREAM_VC1, id);
                         else if (0 == string.Compare(codec, "A_AC3", true, CultureInfo.InvariantCulture))
                             si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_AC3, id);
                         else if (0 == string.Compare(codec, "A_DTS", true, CultureInfo.InvariantCulture))
                             si = new StreamInfo(ElementaryStreamTypes.AUDIO_STREAM_DTS, id);
                         mkvStreams.Add(si);
                     }
                 }
             }
             if (mkvStreams.Count > 0)
                 sis = mkvStreams.ToArray();
         }
     }
     GetTimeStamps();
     CurrentIndex = 0;
 }
Exemplo n.º 6
0
        public void Author(EpElement[] EpInfo, StreamInfo[] sis, UInt32 numOfSourcePackets)
        {
            List<ushort> Pids = new List<ushort>();
            List<byte[]> StreamCodingInfos = new List<byte[]>();
            List<byte[]> AudioEntries = new List<byte[]>();
            List<byte[]> AudioAttributes = new List<byte[]>();
            List<byte[]> PgEntries = new List<byte[]>();
            List<byte[]> PgAttributes = new List<byte[]>();
            byte[] VideoEntry = null;
            byte[] VideoAttribute = null;
            ElementaryStreamTypes VideoType = ElementaryStreamTypes.INVALID;
            foreach(StreamInfo si in sis)
            {
                switch (si.StreamType)
                {
                    case ElementaryStreamTypes.AUDIO_STREAM_AC3:
                    case ElementaryStreamTypes.AUDIO_STREAM_AC3_PLUS:
                    case ElementaryStreamTypes.AUDIO_STREAM_AC3_TRUE_HD:
                    case ElementaryStreamTypes.AUDIO_STREAM_DTS:
                    case ElementaryStreamTypes.AUDIO_STREAM_DTS_HD:
                    case ElementaryStreamTypes.AUDIO_STREAM_DTS_HD_MASTER_AUDIO:
                    case ElementaryStreamTypes.AUDIO_STREAM_LPCM:
                    case ElementaryStreamTypes.AUDIO_STREAM_MPEG1:
                    case ElementaryStreamTypes.AUDIO_STREAM_MPEG2:
                        byte[] AudioEntry = BuildStreamEntry(si.ElementaryPID);
                        byte[] AudioAttribute = BuildAudioStreamAttributes((byte)si.StreamType,si.AudioPresentationType,si.SamplingFrequency);
                        AudioEntries.Add(AudioEntry);
                        AudioAttributes.Add(AudioAttribute);
                        byte[] AudioCodingInfo = BuildAudioStreamCodingInfo(si.StreamType, si.AudioPresentationType, si.SamplingFrequency);
                        Pids.Add(si.ElementaryPID);
                        StreamCodingInfos.Add(AudioCodingInfo);
                        break;
                    case ElementaryStreamTypes.VIDEO_STREAM_H264:
                    case ElementaryStreamTypes.VIDEO_STREAM_MPEG1:
                    case ElementaryStreamTypes.VIDEO_STREAM_MPEG2:
                    case ElementaryStreamTypes.VIDEO_STREAM_VC1:
                        VideoType = si.StreamType;
                        VideoEntry = BuildStreamEntry(si.ElementaryPID);
                        VideoAttribute = BuildVideoStreamAttributes((byte)si.StreamType,si.VideoFormat,si.FrameRate);
                        byte[] VideoCodingInfo = BuildVideoStreamCodingInfo(si.StreamType, si.VideoFormat, si.FrameRate, si.AspectRatio);
                        Pids.Add(si.ElementaryPID);
                        StreamCodingInfos.Add(VideoCodingInfo);
                        break;
                    case ElementaryStreamTypes.PRESENTATION_GRAPHICS_STREAM:
                        byte[] PgEntry = BuildStreamEntry(si.ElementaryPID);
                        PgEntries.Add(PgEntry);
                        PgAttributes.Add(PgStreamAttributes);
                        byte[] PgCodingInfo = BuildPgStreamCodingInfo();
                        Pids.Add(si.ElementaryPID);
                        StreamCodingInfos.Add(PgCodingInfo);
                        break;
                }
            }
            byte[][] PlayItems = new byte[1][];
            UInt32 Start = (UInt32)((EpInfo[0].PTS >> 1) & 0xffffffff);
            UInt32 End = (UInt32)((EpInfo[EpInfo.Length - 1].PTS >> 1) & 0xffffffff);
            UInt32 Interval = ((UInt32)(chapterLen.TotalMinutes)) * 2700000;
            byte[] StnTable = BuildStnTable(VideoEntry, VideoAttribute, AudioEntries.ToArray(), AudioAttributes.ToArray(), PgEntries.ToArray(), PgAttributes.ToArray());
            PlayItems[0] = BuildFirstPlayItem(0, Start, End, StnTable);
            byte[] PlayList = BuildPlayList(PlayItems);
            byte[] PlayListMark = BuildFirstPlayMarks(Start, End, Interval);
            byte[] mlps = Build_mlps(PlayList, PlayListMark);
            File.WriteAllBytes(Path.Combine(path, @"BDMV\PLAYLIST\00000.mpls"), mlps);
            File.Copy(Path.Combine(path, @"BDMV\PLAYLIST\00000.mpls"), Path.Combine(path, @"BDMV\BACKUP\PLAYLIST\00000.mpls"),true);

            byte[] ClipInfo = BuildClipInfo(numOfSourcePackets,EpInfo);
            byte[] SequenceInfo = BuildSequenceInfo(Start, End);
            byte[] ProgramInf = BuildProgramInfo(Pids.ToArray(), StreamCodingInfos.ToArray());
            byte[] EpMap = BuildEpMap(EpInfo);
            byte[] CPI = BuildCpi(EpMap);
            byte[] clpi = Build_clpi(ClipInfo, SequenceInfo, ProgramInf, CPI);
            File.WriteAllBytes(Path.Combine(path, @"BDMV\CLIPINF\00001.clpi"), clpi);
            File.Copy(Path.Combine(path, @"BDMV\CLIPINF\00001.clpi"), Path.Combine(path, @"BDMV\BACKUP\CLIPINF\00001.clpi"),true);
        }