Exemplo n.º 1
0
 public bool IsCompatible(TSStreamClip clip)
 {
     foreach (TSStream stream1 in StreamFile.Streams.Values)
     {
         if (clip.StreamFile.Streams.ContainsKey(stream1.PID))
         {
             TSStream stream2 = clip.StreamFile.Streams[stream1.PID];
             if (stream1.StreamType != stream2.StreamType)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Exemplo n.º 2
0
 protected void CopyTo(TSStream stream)
 {
     stream.PID           = PID;
     stream.StreamType    = StreamType;
     stream.IsVBR         = IsVBR;
     stream.BitRate       = BitRate;
     stream.IsInitialized = IsInitialized;
     stream.LanguageCode  = _LanguageCode;
     if (Descriptors != null)
     {
         stream.Descriptors = new List <TSDescriptor>();
         foreach (TSDescriptor descriptor in Descriptors)
         {
             stream.Descriptors.Add(descriptor.Clone());
         }
     }
 }
Exemplo n.º 3
0
 protected void CopyTo(TSStream stream)
 {
     stream.PID = PID;
     stream.StreamType = StreamType;
     stream.IsVBR = IsVBR;
     stream.BitRate = BitRate;
     stream.IsInitialized = IsInitialized;
     stream.LanguageCode = _LanguageCode;
     if (Descriptors != null)
     {
         stream.Descriptors = new List<TSDescriptor>();
         foreach (TSDescriptor descriptor in Descriptors)
         {
             stream.Descriptors.Add(descriptor.Clone());
         }
     }
 }
Exemplo n.º 4
0
        private void LoadStreamClips()
        {
            AngleClips.Clear();
            if (AngleCount > 0)
            {
                for (int angleIndex = 0; angleIndex < AngleCount; angleIndex++)
                {
                    AngleClips.Add(new Dictionary <double, TSStreamClip>());
                }
            }

            TSStreamClip referenceClip = null;

            if (StreamClips.Count > 0)
            {
                referenceClip = StreamClips[0];
            }
            foreach (TSStreamClip clip in StreamClips)
            {
                if (clip.StreamClipFile.Streams.Count > referenceClip.StreamClipFile.Streams.Count)
                {
                    referenceClip = clip;
                }
                else if (clip.Length > referenceClip.Length)
                {
                    referenceClip = clip;
                }
                if (AngleCount > 0)
                {
                    if (clip.AngleIndex == 0)
                    {
                        for (int angleIndex = 0; angleIndex < AngleCount; angleIndex++)
                        {
                            AngleClips[angleIndex][clip.RelativeTimeIn] = clip;
                        }
                    }
                    else
                    {
                        AngleClips[clip.AngleIndex - 1][clip.RelativeTimeIn] = clip;
                    }
                }
            }

            foreach (TSStream clipStream
                     in referenceClip.StreamClipFile.Streams.Values)
            {
                if (!Streams.ContainsKey(clipStream.PID))
                {
                    TSStream stream = clipStream.Clone();
                    Streams[clipStream.PID] = stream;

                    if (!IsCustom && !PlaylistStreams.ContainsKey(stream.PID))
                    {
                        stream.IsHidden = true;
                        HasHiddenTracks = true;
                    }

                    if (stream.IsVideoStream)
                    {
                        VideoStreams.Add((TSVideoStream)stream);
                    }
                    else if (stream.IsAudioStream)
                    {
                        AudioStreams.Add((TSAudioStream)stream);
                    }
                    else if (stream.IsGraphicsStream)
                    {
                        GraphicsStreams.Add((TSGraphicsStream)stream);
                    }
                    else if (stream.IsTextStream)
                    {
                        TextStreams.Add((TSTextStream)stream);
                    }
                }
            }

            if (referenceClip.StreamFile != null)
            {
                // TODO: Better way to add this in?
                if (BDInfoSettings.EnableSSIF &&
                    referenceClip.StreamFile.InterleavedFile != null &&
                    referenceClip.StreamFile.Streams.ContainsKey(4114) &&
                    !Streams.ContainsKey(4114))
                {
                    TSStream stream = referenceClip.StreamFile.Streams[4114].Clone();
                    Streams[4114] = stream;
                    if (stream.IsVideoStream)
                    {
                        VideoStreams.Add((TSVideoStream)stream);
                    }
                }

                foreach (TSStream clipStream
                         in referenceClip.StreamFile.Streams.Values)
                {
                    if (Streams.ContainsKey(clipStream.PID))
                    {
                        TSStream stream = Streams[clipStream.PID];

                        if (stream.StreamType != clipStream.StreamType)
                        {
                            continue;
                        }

                        if (clipStream.BitRate > stream.BitRate)
                        {
                            stream.BitRate = clipStream.BitRate;
                        }
                        stream.IsVBR = clipStream.IsVBR;

                        if (stream.IsVideoStream &&
                            clipStream.IsVideoStream)
                        {
                            ((TSVideoStream)stream).EncodingProfile =
                                ((TSVideoStream)clipStream).EncodingProfile;
                        }
                        else if (stream.IsAudioStream &&
                                 clipStream.IsAudioStream)
                        {
                            TSAudioStream audioStream     = (TSAudioStream)stream;
                            TSAudioStream clipAudioStream = (TSAudioStream)clipStream;

                            if (clipAudioStream.ChannelCount > audioStream.ChannelCount)
                            {
                                audioStream.ChannelCount = clipAudioStream.ChannelCount;
                            }
                            if (clipAudioStream.LFE > audioStream.LFE)
                            {
                                audioStream.LFE = clipAudioStream.LFE;
                            }
                            if (clipAudioStream.SampleRate > audioStream.SampleRate)
                            {
                                audioStream.SampleRate = clipAudioStream.SampleRate;
                            }
                            if (clipAudioStream.BitDepth > audioStream.BitDepth)
                            {
                                audioStream.BitDepth = clipAudioStream.BitDepth;
                            }
                            if (clipAudioStream.DialNorm < audioStream.DialNorm)
                            {
                                audioStream.DialNorm = clipAudioStream.DialNorm;
                            }
                            if (clipAudioStream.AudioMode != TSAudioMode.Unknown)
                            {
                                audioStream.AudioMode = clipAudioStream.AudioMode;
                            }
                            if (clipAudioStream.CoreStream != null &&
                                audioStream.CoreStream == null)
                            {
                                audioStream.CoreStream = (TSAudioStream)
                                                         clipAudioStream.CoreStream.Clone();
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < AngleCount; i++)
            {
                AngleStreams.Add(new Dictionary <ushort, TSStream>());
            }

            if (!BDInfoSettings.KeepStreamOrder)
            {
                VideoStreams.Sort(CompareVideoStreams);
            }
            foreach (TSStream stream in VideoStreams)
            {
                SortedStreams.Add(stream);
                for (int i = 0; i < AngleCount; i++)
                {
                    TSStream angleStream = stream.Clone();
                    angleStream.AngleIndex           = i + 1;
                    AngleStreams[i][angleStream.PID] = angleStream;
                    SortedStreams.Add(angleStream);
                }
            }

            if (!BDInfoSettings.KeepStreamOrder)
            {
                AudioStreams.Sort(CompareAudioStreams);
            }
            foreach (TSStream stream in AudioStreams)
            {
                SortedStreams.Add(stream);
            }

            if (!BDInfoSettings.KeepStreamOrder)
            {
                GraphicsStreams.Sort(CompareGraphicsStreams);
            }
            foreach (TSStream stream in GraphicsStreams)
            {
                SortedStreams.Add(stream);
            }

            if (!BDInfoSettings.KeepStreamOrder)
            {
                TextStreams.Sort(CompareTextStreams);
            }
            foreach (TSStream stream in TextStreams)
            {
                SortedStreams.Add(stream);
            }
        }
Exemplo n.º 5
0
        protected TSStream CreatePlaylistStream(byte[] data, ref int pos)
        {
            TSStream stream = null;

            int start = pos;

            int headerLength = data[pos++];
            int headerPos    = pos;
            int headerType   = data[pos++];

            int pid       = 0;
            int subpathid = 0;
            int subclipid = 0;

            switch (headerType)
            {
            case 1:
                pid = ReadInt16(data, ref pos);
                break;

            case 2:
                subpathid = data[pos++];
                subclipid = data[pos++];
                pid       = ReadInt16(data, ref pos);
                break;

            case 3:
                subpathid = data[pos++];
                pid       = ReadInt16(data, ref pos);
                break;

            case 4:
                subpathid = data[pos++];
                subclipid = data[pos++];
                pid       = ReadInt16(data, ref pos);
                break;

            default:
                break;
            }

            pos = headerPos + headerLength;

            int streamLength = data[pos++];
            int streamPos    = pos;

            TSStreamType streamType = (TSStreamType)data[pos++];

            switch (streamType)
            {
            case TSStreamType.MVC_VIDEO:
                // TODO
                break;

            case TSStreamType.AVC_VIDEO:
            case TSStreamType.MPEG1_VIDEO:
            case TSStreamType.MPEG2_VIDEO:
            case TSStreamType.VC1_VIDEO:

                TSVideoFormat videoFormat = (TSVideoFormat)
                                            (data[pos] >> 4);
                TSFrameRate frameRate = (TSFrameRate)
                                        (data[pos] & 0xF);
                TSAspectRatio aspectRatio = (TSAspectRatio)
                                            (data[pos + 1] >> 4);

                stream = new TSVideoStream();
                ((TSVideoStream)stream).VideoFormat = videoFormat;
                ((TSVideoStream)stream).AspectRatio = aspectRatio;
                ((TSVideoStream)stream).FrameRate   = frameRate;

#if DEBUG
                Debug.WriteLine(string.Format(
                                    "\t{0} {1} {2} {3} {4}",
                                    pid,
                                    streamType,
                                    videoFormat,
                                    frameRate,
                                    aspectRatio));
#endif

                break;

            case TSStreamType.AC3_AUDIO:
            case TSStreamType.AC3_PLUS_AUDIO:
            case TSStreamType.AC3_PLUS_SECONDARY_AUDIO:
            case TSStreamType.AC3_TRUE_HD_AUDIO:
            case TSStreamType.DTS_AUDIO:
            case TSStreamType.DTS_HD_AUDIO:
            case TSStreamType.DTS_HD_MASTER_AUDIO:
            case TSStreamType.DTS_HD_SECONDARY_AUDIO:
            case TSStreamType.LPCM_AUDIO:
            case TSStreamType.MPEG1_AUDIO:
            case TSStreamType.MPEG2_AUDIO:

                int audioFormat = ReadByte(data, ref pos);

                TSChannelLayout channelLayout = (TSChannelLayout)
                                                (audioFormat >> 4);
                TSSampleRate sampleRate = (TSSampleRate)
                                          (audioFormat & 0xF);

                string audioLanguage = ReadString(data, 3, ref pos);

                stream = new TSAudioStream();
                ((TSAudioStream)stream).ChannelLayout = channelLayout;
                ((TSAudioStream)stream).SampleRate    = TSAudioStream.ConvertSampleRate(sampleRate);
                ((TSAudioStream)stream).LanguageCode  = audioLanguage;

#if DEBUG
                Debug.WriteLine(string.Format(
                                    "\t{0} {1} {2} {3} {4}",
                                    pid,
                                    streamType,
                                    audioLanguage,
                                    channelLayout,
                                    sampleRate));
#endif

                break;

            case TSStreamType.INTERACTIVE_GRAPHICS:
            case TSStreamType.PRESENTATION_GRAPHICS:

                string graphicsLanguage = ReadString(data, 3, ref pos);

                stream = new TSGraphicsStream();
                ((TSGraphicsStream)stream).LanguageCode = graphicsLanguage;

                if (data[pos] != 0)
                {
                }

#if DEBUG
                Debug.WriteLine(string.Format(
                                    "\t{0} {1} {2}",
                                    pid,
                                    streamType,
                                    graphicsLanguage));
#endif

                break;

            case TSStreamType.SUBTITLE:

                int    code         = ReadByte(data, ref pos); // TODO
                string textLanguage = ReadString(data, 3, ref pos);

                stream = new TSTextStream();
                ((TSTextStream)stream).LanguageCode = textLanguage;

#if DEBUG
                Debug.WriteLine(string.Format(
                                    "\t{0} {1} {2}",
                                    pid,
                                    streamType,
                                    textLanguage));
#endif

                break;

            default:
                break;
            }

            pos = streamPos + streamLength;

            if (stream != null)
            {
                stream.PID        = (ushort)pid;
                stream.StreamType = streamType;
            }

            return(stream);
        }
Exemplo n.º 6
0
        public void Scan(
            Dictionary <string, TSStreamFile> streamFiles,
            Dictionary <string, TSStreamClipFile> streamClipFiles)
        {
            FileStream   fileStream = null;
            BinaryReader fileReader = null;

            try
            {
                Streams.Clear();
                StreamClips.Clear();

                fileStream = File.OpenRead(FileInfo.FullName);
                fileReader = new BinaryReader(fileStream);

                byte[] data       = new byte[fileStream.Length];
                int    dataLength = fileReader.Read(data, 0, data.Length);

                int pos = 0;

                FileType = ReadString(data, 8, ref pos);
                if (FileType != "MPLS0100" && FileType != "MPLS0200")
                {
                    throw new Exception(string.Format(
                                            "Playlist {0} has an unknown file type {1}.",
                                            FileInfo.Name, FileType));
                }

                int playlistOffset   = ReadInt32(data, ref pos);
                int chaptersOffset   = ReadInt32(data, ref pos);
                int extensionsOffset = ReadInt32(data, ref pos);

                // misc flags
                pos = 0x38;
                byte miscFlags = ReadByte(data, ref pos);

                // MVC_Base_view_R_flag is stored in 4th bit
                MVCBaseViewR = (miscFlags & 0x10) != 0;

                pos = playlistOffset;

                int playlistLength   = ReadInt32(data, ref pos);
                int playlistReserved = ReadInt16(data, ref pos);
                int itemCount        = ReadInt16(data, ref pos);
                int subitemCount     = ReadInt16(data, ref pos);

                List <TSStreamClip> chapterClips = new List <TSStreamClip>();
                for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
                {
                    int    itemStart  = pos;
                    int    itemLength = ReadInt16(data, ref pos);
                    string itemName   = ReadString(data, 5, ref pos);
                    string itemType   = ReadString(data, 4, ref pos);

                    TSStreamFile streamFile     = null;
                    string       streamFileName = string.Format(
                        "{0}.M2TS", itemName);
                    if (streamFiles.ContainsKey(streamFileName))
                    {
                        streamFile = streamFiles[streamFileName];
                    }
                    if (streamFile == null)
                    {
                        Debug.WriteLine(string.Format(
                                            "Playlist {0} referenced missing file {1}.",
                                            FileInfo.Name, streamFileName));
                    }

                    TSStreamClipFile streamClipFile     = null;
                    string           streamClipFileName = string.Format(
                        "{0}.CLPI", itemName);
                    if (streamClipFiles.ContainsKey(streamClipFileName))
                    {
                        streamClipFile = streamClipFiles[streamClipFileName];
                    }
                    if (streamClipFile == null)
                    {
                        throw new Exception(string.Format(
                                                "Playlist {0} referenced missing file {1}.",
                                                FileInfo.Name, streamFileName));
                    }

                    pos += 1;
                    int multiangle = (data[pos] >> 4) & 0x01;
                    int condition  = data[pos] & 0x0F;
                    pos += 2;

                    int inTime = ReadInt32(data, ref pos);
                    if (inTime < 0)
                    {
                        inTime &= 0x7FFFFFFF;
                    }
                    double timeIn = (double)inTime / 45000;

                    int outTime = ReadInt32(data, ref pos);
                    if (outTime < 0)
                    {
                        outTime &= 0x7FFFFFFF;
                    }
                    double timeOut = (double)outTime / 45000;

                    TSStreamClip streamClip = new TSStreamClip(
                        streamFile, streamClipFile);

                    streamClip.Name            = streamFileName; //TODO
                    streamClip.TimeIn          = timeIn;
                    streamClip.TimeOut         = timeOut;
                    streamClip.Length          = streamClip.TimeOut - streamClip.TimeIn;
                    streamClip.RelativeTimeIn  = TotalLength;
                    streamClip.RelativeTimeOut = streamClip.RelativeTimeIn + streamClip.Length;
                    StreamClips.Add(streamClip);
                    chapterClips.Add(streamClip);

                    pos += 12;
                    if (multiangle > 0)
                    {
                        int angles = data[pos];
                        pos += 2;
                        for (int angle = 0; angle < angles - 1; angle++)
                        {
                            string angleName = ReadString(data, 5, ref pos);
                            string angleType = ReadString(data, 4, ref pos);
                            pos += 1;

                            TSStreamFile angleFile     = null;
                            string       angleFileName = string.Format(
                                "{0}.M2TS", angleName);
                            if (streamFiles.ContainsKey(angleFileName))
                            {
                                angleFile = streamFiles[angleFileName];
                            }
                            if (angleFile == null)
                            {
                                throw new Exception(string.Format(
                                                        "Playlist {0} referenced missing angle file {1}.",
                                                        FileInfo.Name, angleFileName));
                            }

                            TSStreamClipFile angleClipFile     = null;
                            string           angleClipFileName = string.Format(
                                "{0}.CLPI", angleName);
                            if (streamClipFiles.ContainsKey(angleClipFileName))
                            {
                                angleClipFile = streamClipFiles[angleClipFileName];
                            }
                            if (angleClipFile == null)
                            {
                                throw new Exception(string.Format(
                                                        "Playlist {0} referenced missing angle file {1}.",
                                                        FileInfo.Name, angleClipFileName));
                            }

                            TSStreamClip angleClip =
                                new TSStreamClip(angleFile, angleClipFile);
                            angleClip.AngleIndex      = angle + 1;
                            angleClip.TimeIn          = streamClip.TimeIn;
                            angleClip.TimeOut         = streamClip.TimeOut;
                            angleClip.RelativeTimeIn  = streamClip.RelativeTimeIn;
                            angleClip.RelativeTimeOut = streamClip.RelativeTimeOut;
                            angleClip.Length          = streamClip.Length;
                            StreamClips.Add(angleClip);
                        }
                        if (angles - 1 > AngleCount)
                        {
                            AngleCount = angles - 1;
                        }
                    }

                    int streamInfoLength = ReadInt16(data, ref pos);
                    pos += 2;
                    int streamCountVideo          = data[pos++];
                    int streamCountAudio          = data[pos++];
                    int streamCountPG             = data[pos++];
                    int streamCountIG             = data[pos++];
                    int streamCountSecondaryAudio = data[pos++];
                    int streamCountSecondaryVideo = data[pos++];
                    int streamCountPIP            = data[pos++];
                    pos += 5;

#if DEBUG
                    Debug.WriteLine(string.Format(
                                        "{0} : {1} -> V:{2} A:{3} PG:{4} IG:{5} 2A:{6} 2V:{7} PIP:{8}",
                                        Name, streamFileName, streamCountVideo, streamCountAudio, streamCountPG, streamCountIG,
                                        streamCountSecondaryAudio, streamCountSecondaryVideo, streamCountPIP));
#endif

                    for (int i = 0; i < streamCountVideo; i++)
                    {
                        TSStream stream = CreatePlaylistStream(data, ref pos);
                        if (stream != null)
                        {
                            PlaylistStreams[stream.PID] = stream;
                        }
                    }
                    for (int i = 0; i < streamCountAudio; i++)
                    {
                        TSStream stream = CreatePlaylistStream(data, ref pos);
                        if (stream != null)
                        {
                            PlaylistStreams[stream.PID] = stream;
                        }
                    }
                    for (int i = 0; i < streamCountPG; i++)
                    {
                        TSStream stream = CreatePlaylistStream(data, ref pos);
                        if (stream != null)
                        {
                            PlaylistStreams[stream.PID] = stream;
                        }
                    }
                    for (int i = 0; i < streamCountIG; i++)
                    {
                        TSStream stream = CreatePlaylistStream(data, ref pos);
                        if (stream != null)
                        {
                            PlaylistStreams[stream.PID] = stream;
                        }
                    }
                    for (int i = 0; i < streamCountSecondaryAudio; i++)
                    {
                        TSStream stream = CreatePlaylistStream(data, ref pos);
                        if (stream != null)
                        {
                            PlaylistStreams[stream.PID] = stream;
                        }
                        pos += 2;
                    }
                    for (int i = 0; i < streamCountSecondaryVideo; i++)
                    {
                        TSStream stream = CreatePlaylistStream(data, ref pos);
                        if (stream != null)
                        {
                            PlaylistStreams[stream.PID] = stream;
                        }
                        pos += 6;
                    }

                    /*
                     * TODO
                     *
                     * for (int i = 0; i < streamCountPIP; i++)
                     * {
                     *  TSStream stream = CreatePlaylistStream(data, ref pos);
                     *  if (stream != null) PlaylistStreams[stream.PID] = stream;
                     * }
                     */

                    pos += itemLength - (pos - itemStart) + 2;
                }

                pos = chaptersOffset + 4;

                int chapterCount = ReadInt16(data, ref pos);

                for (int chapterIndex = 0;
                     chapterIndex < chapterCount;
                     chapterIndex++)
                {
                    int chapterType = data[pos + 1];

                    if (chapterType == 1)
                    {
                        int streamFileIndex =
                            ((int)data[pos + 2] << 8) + data[pos + 3];

                        long chapterTime =
                            ((long)data[pos + 4] << 24) +
                            ((long)data[pos + 5] << 16) +
                            ((long)data[pos + 6] << 8) +
                            ((long)data[pos + 7]);

                        TSStreamClip streamClip = chapterClips[streamFileIndex];

                        double chapterSeconds = (double)chapterTime / 45000;

                        double relativeSeconds =
                            chapterSeconds -
                            streamClip.TimeIn +
                            streamClip.RelativeTimeIn;

                        // TODO: Ignore short last chapter?
                        if (TotalLength - relativeSeconds > 1.0)
                        {
                            streamClip.Chapters.Add(chapterSeconds);
                            this.Chapters.Add(relativeSeconds);
                        }
                    }
                    else
                    {
                        // TODO: Handle other chapter types?
                    }
                    pos += 14;
                }
            }
            finally
            {
                if (fileReader != null)
                {
                    fileReader.Close();
                }
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }
Exemplo n.º 7
0
        private bool ScanStream(
            TSStream stream,
            TSStreamState streamState,
            TSStreamBuffer buffer)
        {
            streamState.StreamTag = null;

            long bitrate = 0;
            if (stream.IsAudioStream &&
                streamState.PTSTransfer > 0)
            {
                bitrate = (long)Math.Round(
                    (buffer.TransferLength * 8.0) /
                    ((double)streamState.PTSTransfer / 90000));

                if (bitrate > streamState.PeakTransferRate)
                {
                    streamState.PeakTransferRate = bitrate;
                }
            }
            if (buffer.TransferLength > streamState.PeakTransferLength)
            {
                streamState.PeakTransferLength = buffer.TransferLength;
            }

            buffer.BeginRead();
            switch (stream.StreamType)
            {
                case TSStreamType.MPEG2_VIDEO:
                    TSCodecMPEG2.Scan(
                        (TSVideoStream)stream, buffer, ref streamState.StreamTag);
                    break;

                case TSStreamType.AVC_VIDEO:
                    TSCodecAVC.Scan(
                        (TSVideoStream)stream, buffer, ref streamState.StreamTag);
                    break;

                case TSStreamType.MVC_VIDEO:
                    TSCodecMVC.Scan(
                        (TSVideoStream)stream, buffer, ref streamState.StreamTag);
                    break;

                case TSStreamType.VC1_VIDEO:
                    TSCodecVC1.Scan(
                        (TSVideoStream)stream, buffer, ref streamState.StreamTag);
                    break;

                case TSStreamType.AC3_AUDIO:
                    TSCodecAC3.Scan(
                        (TSAudioStream)stream, buffer, ref streamState.StreamTag);
                    break;

                case TSStreamType.AC3_PLUS_AUDIO:
                case TSStreamType.AC3_PLUS_SECONDARY_AUDIO:
                    TSCodecAC3.Scan(
                        (TSAudioStream)stream, buffer, ref streamState.StreamTag);
                    break;

                case TSStreamType.AC3_TRUE_HD_AUDIO:
                    TSCodecTrueHD.Scan(
                        (TSAudioStream)stream, buffer, ref streamState.StreamTag);
                    break;

                case TSStreamType.LPCM_AUDIO:
                    TSCodecLPCM.Scan(
                        (TSAudioStream)stream, buffer, ref streamState.StreamTag);
                    break;

                case TSStreamType.DTS_AUDIO:
                    TSCodecDTS.Scan(
                        (TSAudioStream)stream, buffer, bitrate, ref streamState.StreamTag);
                    break;

                case TSStreamType.DTS_HD_AUDIO:
                case TSStreamType.DTS_HD_MASTER_AUDIO:
                case TSStreamType.DTS_HD_SECONDARY_AUDIO:
                    TSCodecDTSHD.Scan(
                        (TSAudioStream)stream, buffer, bitrate, ref streamState.StreamTag);
                    break;

                default:
                    stream.IsInitialized = true;
                    break;
            }
            buffer.EndRead();
            streamState.StreamBuffer.Reset();

            bool isAVC = false;
            bool isMVC = false;
            foreach (TSStream finishedStream in Streams.Values)
            {
                if (!finishedStream.IsInitialized)
                {
                    return false;
                }
                if (finishedStream.StreamType == TSStreamType.AVC_VIDEO)
                {
                    isAVC = true;
                }
                if (finishedStream.StreamType == TSStreamType.MVC_VIDEO)
                {
                    isMVC = true;
                }
            }
            if (isMVC && !isAVC)
            {
                return false;
            }
            return true;
        }
Exemplo n.º 8
0
        public void Scan()
        {
            FileStream   fileStream = null;
            BinaryReader fileReader = null;

            try
            {
#if DEBUG
                Debug.WriteLine(string.Format(
                                    "Scanning {0}...", Name));
#endif
                Streams.Clear();

                fileStream = File.OpenRead(FileInfo.FullName);
                fileReader = new BinaryReader(fileStream);

                byte[] data = new byte[fileStream.Length];
                fileReader.Read(data, 0, data.Length);

                byte[] fileType = new byte[8];
                Array.Copy(data, 0, fileType, 0, fileType.Length);

                FileType = ASCIIEncoding.ASCII.GetString(fileType);
                if (FileType != "HDMV0100" &&
                    FileType != "HDMV0200")
                {
                    throw new Exception(string.Format(
                                            "Clip info file {0} has an unknown file type {1}.",
                                            FileInfo.Name, FileType));
                }
#if DEBUG
                Debug.WriteLine(string.Format(
                                    "\tFileType: {0}", FileType));
#endif
                int clipIndex =
                    ((int)data[12] << 24) +
                    ((int)data[13] << 16) +
                    ((int)data[14] << 8) +
                    ((int)data[15]);

                int clipLength =
                    ((int)data[clipIndex] << 24) +
                    ((int)data[clipIndex + 1] << 16) +
                    ((int)data[clipIndex + 2] << 8) +
                    ((int)data[clipIndex + 3]);

                byte[] clipData = new byte[clipLength];
                Array.Copy(data, clipIndex + 4, clipData, 0, clipData.Length);

                int streamCount = clipData[8];
#if DEBUG
                Debug.WriteLine(string.Format(
                                    "\tStreamCount: {0}", streamCount));
#endif
                int streamOffset = 10;
                for (int streamIndex = 0;
                     streamIndex < streamCount;
                     streamIndex++)
                {
                    TSStream stream = null;

                    ushort PID = (ushort)
                                 ((clipData[streamOffset] << 8) +
                                  clipData[streamOffset + 1]);

                    streamOffset += 2;

                    TSStreamType streamType = (TSStreamType)
                                              clipData[streamOffset + 1];
                    switch (streamType)
                    {
                    case TSStreamType.MVC_VIDEO:
                        // TODO
                        break;

                    case TSStreamType.AVC_VIDEO:
                    case TSStreamType.MPEG1_VIDEO:
                    case TSStreamType.MPEG2_VIDEO:
                    case TSStreamType.VC1_VIDEO:
                    {
                        TSVideoFormat videoFormat = (TSVideoFormat)
                                                    (clipData[streamOffset + 2] >> 4);
                        TSFrameRate frameRate = (TSFrameRate)
                                                (clipData[streamOffset + 2] & 0xF);
                        TSAspectRatio aspectRatio = (TSAspectRatio)
                                                    (clipData[streamOffset + 3] >> 4);

                        stream = new TSVideoStream();
                        ((TSVideoStream)stream).VideoFormat = videoFormat;
                        ((TSVideoStream)stream).AspectRatio = aspectRatio;
                        ((TSVideoStream)stream).FrameRate   = frameRate;
#if DEBUG
                        Debug.WriteLine(string.Format(
                                            "\t{0} {1} {2} {3} {4}",
                                            PID,
                                            streamType,
                                            videoFormat,
                                            frameRate,
                                            aspectRatio));
#endif
                    }
                    break;

                    case TSStreamType.AC3_AUDIO:
                    case TSStreamType.AC3_PLUS_AUDIO:
                    case TSStreamType.AC3_PLUS_SECONDARY_AUDIO:
                    case TSStreamType.AC3_TRUE_HD_AUDIO:
                    case TSStreamType.DTS_AUDIO:
                    case TSStreamType.DTS_HD_AUDIO:
                    case TSStreamType.DTS_HD_MASTER_AUDIO:
                    case TSStreamType.DTS_HD_SECONDARY_AUDIO:
                    case TSStreamType.LPCM_AUDIO:
                    case TSStreamType.MPEG1_AUDIO:
                    case TSStreamType.MPEG2_AUDIO:
                    {
                        byte[] languageBytes = new byte[3];
                        Array.Copy(clipData, streamOffset + 3,
                                   languageBytes, 0, languageBytes.Length);
                        string languageCode =
                            ASCIIEncoding.ASCII.GetString(languageBytes);

                        TSChannelLayout channelLayout = (TSChannelLayout)
                                                        (clipData[streamOffset + 2] >> 4);
                        TSSampleRate sampleRate = (TSSampleRate)
                                                  (clipData[streamOffset + 2] & 0xF);

                        stream = new TSAudioStream();
                        ((TSAudioStream)stream).LanguageCode  = languageCode;
                        ((TSAudioStream)stream).ChannelLayout = channelLayout;
                        ((TSAudioStream)stream).SampleRate    = TSAudioStream.ConvertSampleRate(sampleRate);
                        ((TSAudioStream)stream).LanguageCode  = languageCode;
#if DEBUG
                        Debug.WriteLine(string.Format(
                                            "\t{0} {1} {2} {3} {4}",
                                            PID,
                                            streamType,
                                            languageCode,
                                            channelLayout,
                                            sampleRate));
#endif
                    }
                    break;

                    case TSStreamType.INTERACTIVE_GRAPHICS:
                    case TSStreamType.PRESENTATION_GRAPHICS:
                    {
                        byte[] languageBytes = new byte[3];
                        Array.Copy(clipData, streamOffset + 2,
                                   languageBytes, 0, languageBytes.Length);
                        string languageCode =
                            ASCIIEncoding.ASCII.GetString(languageBytes);

                        stream = new TSGraphicsStream();
                        stream.LanguageCode = languageCode;
#if DEBUG
                        Debug.WriteLine(string.Format(
                                            "\t{0} {1} {2}",
                                            PID,
                                            streamType,
                                            languageCode));
#endif
                    }
                    break;

                    case TSStreamType.SUBTITLE:
                    {
                        byte[] languageBytes = new byte[3];
                        Array.Copy(clipData, streamOffset + 3,
                                   languageBytes, 0, languageBytes.Length);
                        string languageCode =
                            ASCIIEncoding.ASCII.GetString(languageBytes);
#if DEBUG
                        Debug.WriteLine(string.Format(
                                            "\t{0} {1} {2}",
                                            PID,
                                            streamType,
                                            languageCode));
#endif
                        stream = new TSTextStream();
                        stream.LanguageCode = languageCode;
                    }
                    break;
                    }

                    if (stream != null)
                    {
                        stream.PID        = PID;
                        stream.StreamType = streamType;
                        Streams.Add(PID, stream);
                    }

                    streamOffset += clipData[streamOffset] + 1;
                }
                IsValid = true;
            }
            finally
            {
                if (fileReader != null)
                {
                    fileReader.Close();
                }
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }