Пример #1
0
        public override TSStream Clone()
        {
            TSGraphicsStream stream = new TSGraphicsStream();

            CopyTo(stream);
            return(stream);
        }
Пример #2
0
        private static int CompareGraphicsStreams(
            TSGraphicsStream x,
            TSGraphicsStream y)
        {
            if (x == y)
            {
                return(0);
            }
            else if (x == null && y == null)
            {
                return(0);
            }
            else if (x == null && y != null)
            {
                return(-1);
            }
            else if (x != null && y == null)
            {
                return(1);
            }
            else
            {
                int sortX = GetStreamTypeSortIndex(x.StreamType);
                int sortY = GetStreamTypeSortIndex(y.StreamType);

                if (sortX > sortY)
                {
                    return(-1);
                }
                else if (sortY > sortX)
                {
                    return(1);
                }
                else if (x.LanguageCode == "eng")
                {
                    return(-1);
                }
                else if (y.LanguageCode == "eng")
                {
                    return(1);
                }
                else
                {
                    if (x.LanguageCode == y.LanguageCode)
                    {
                        if (x.PID > y.PID)
                        {
                            return(1);
                        }
                        else if (y.PID > x.PID)
                        {
                            return(-1);
                        }
                        else
                        {
                            return(0);
                        }
                    }
                    else
                    {
                        return(string.Compare(x.LanguageName, y.LanguageName));
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Adds the subtitle stream.
 /// </summary>
 /// <param name="streams">The streams.</param>
 /// <param name="textStream">The text stream.</param>
 private void AddSubtitleStream(List<MediaStream> streams, TSGraphicsStream textStream)
 {
     streams.Add(new MediaStream
         {
             Language = textStream.LanguageCode,
             Codec = textStream.CodecShortName,
             Type = MediaStreamType.Subtitle,
             Index = streams.Count
         });
 }
        private static int CompareGraphicsStreams(
            TSGraphicsStream x,
            TSGraphicsStream y)
        {
            if (x == y)
            {
                return 0;
            }
            else if (x == null && y == null)
            {
                return 0;
            }
            else if (x == null && y != null)
            {
                return -1;
            }
            else if (x != null && y == null)
            {
                return 1;
            }
            else
            {
                int sortX = GetStreamTypeSortIndex(x.StreamType);
                int sortY = GetStreamTypeSortIndex(y.StreamType);

                if (sortX > sortY)
                {
                    return -1;
                }
                else if (sortY > sortX)
                {
                    return 1;
                }
                else if (x.LanguageCode == "eng")
                {
                    return -1;
                }
                else if (y.LanguageCode == "eng")
                {
                    return 1;
                }
                else
                {
                    if (x.LanguageCode == y.LanguageCode)
                    {
                        if (x.PID > y.PID)
                        {
                            return 1;
                        }
                        else if (y.PID > x.PID)
                        {
                            return -1;
                        }
                        else
                        {
                            return 0;
                        }
                    }
                    else
                    {
                        return string.Compare(x.LanguageName, y.LanguageName);
                    }
                }
            }
        }
Пример #5
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();
                }
            }
        }
Пример #6
0
 public override TSStream Clone()
 {
     TSGraphicsStream stream = new TSGraphicsStream();
     CopyTo(stream);
     return stream;
 }
Пример #7
0
        public void Scan()
        {
            FileStream fileStream = null;
            BinaryReader fileReader = null;

            try
            {

                Debug.WriteLine(string.Format(
                    "Scanning {0}...", Name));

                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));
                }

                Debug.WriteLine(string.Format(
                    "\tFileType: {0}", FileType));

                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];

                Debug.WriteLine(string.Format(
                    "\tStreamCount: {0}", streamCount));

                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.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;

                            Debug.WriteLine(string.Format(
                                "\t{0} {1} {2} {3} {4}",
                                PID,
                                streamType,
                                videoFormat,
                                frameRate,
                                aspectRatio));

                        }
                        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;

                            Debug.WriteLine(string.Format(
                                "\t{0} {1} {2} {3} {4}",
                                PID,
                                streamType,
                                languageCode,
                                channelLayout,
                                sampleRate));

                        }
                        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;

                            Debug.WriteLine(string.Format(
                                "\t{0} {1} {2}",
                                PID,
                                streamType,
                                languageCode));

                        }
                        break;

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

                            Debug.WriteLine(string.Format(
                                "\t{0} {1} {2}",
                                PID,
                                streamType,
                                languageCode));

                            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();
            }
        }
Пример #8
0
        private TSStream CreateStream(
            ushort streamPID, 
            byte streamType, 
            List<TSDescriptor> streamDescriptors)
        {
            TSStream stream = null;

            switch ((TSStreamType)streamType)
            {
                case TSStreamType.MVC_VIDEO:
                case TSStreamType.AVC_VIDEO:
                case TSStreamType.MPEG1_VIDEO:
                case TSStreamType.MPEG2_VIDEO:
                case TSStreamType.VC1_VIDEO:
                {
                    stream = new TSVideoStream();
                }
                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:
                {
                    stream = new TSAudioStream();
                }
                break;

                case TSStreamType.INTERACTIVE_GRAPHICS:
                case TSStreamType.PRESENTATION_GRAPHICS:
                {
                    stream = new TSGraphicsStream();
                }
                break;

                case TSStreamType.SUBTITLE:
                {
                    stream = new TSTextStream();
                }
                break;

                default:
                    break;
            }

            if (stream != null &&
                !Streams.ContainsKey(streamPID))
            {
                stream.PID = streamPID;
                stream.StreamType = (TSStreamType)streamType;
                stream.Descriptors = streamDescriptors;
                Streams[stream.PID] = stream;
            }
            if (!StreamDiagnostics.ContainsKey(streamPID))
            {
                StreamDiagnostics[streamPID] =
                    new List<TSStreamDiagnostics>();
            }

            return stream;
        } 
Пример #9
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;

            var 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:

                var videoFormat = (TSVideoFormat)
                                  (data[pos] >> 4);
                var frameRate = (TSFrameRate)
                                (data[pos] & 0xF);
                var 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);

                var channelLayout = (TSChannelLayout)
                                    (audioFormat >> 4);
                var 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);
        }
Пример #10
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;
        }