Esempio n. 1
0
 public static void Scan(
     TSVideoStream stream,
     TSStreamBuffer buffer,
     ref string tag)
 {
     stream.IsVBR = true;
     stream.IsInitialized = true;
 }
Esempio n. 2
0
        public static void Scan(
            TSAudioStream stream,
            TSStreamBuffer buffer,
            long bitrate,
            ref string tag)
        {
            if (stream.IsInitialized) return;

            bool syncFound = false;
            uint sync = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                sync = (sync << 8) + buffer.ReadByte();
                if (sync == 0x7FFE8001)
                {
                    syncFound = true;
                    break;
                }
            }
            if (!syncFound) return;

            int frame_type = buffer.ReadBits(1);
            int samples_deficit = buffer.ReadBits(5);
            int crc_present = buffer.ReadBits(1);
            int sample_blocks = buffer.ReadBits(7);
            int frame_size = buffer.ReadBits(14);
            if (frame_size < 95)
            {
                return;
            }
            int amode = buffer.ReadBits(6);
            int sample_rate = buffer.ReadBits(4);
            if (sample_rate < 0 || sample_rate >= dca_sample_rates.Length)
            {
                return;
            }
            int bit_rate = buffer.ReadBits(5);
            if (bit_rate < 0 || bit_rate >= dca_bit_rates.Length)
            {
                return;
            }
            int downmix = buffer.ReadBits(1);
            int dynrange = buffer.ReadBits(1);
            int timestamp = buffer.ReadBits(1);
            int aux_data = buffer.ReadBits(1);
            int hdcd = buffer.ReadBits(1);
            int ext_descr = buffer.ReadBits(3);
            int ext_coding = buffer.ReadBits(1);
            int aspf = buffer.ReadBits(1);
            int lfe = buffer.ReadBits(2);
            int predictor_history = buffer.ReadBits(1);
            if (crc_present == 1)
            {
                int crc = buffer.ReadBits(16);
            }
            int multirate_inter = buffer.ReadBits(1);
            int version = buffer.ReadBits(4);
            int copy_history = buffer.ReadBits(2);
            int source_pcm_res = buffer.ReadBits(3);
            int front_sum = buffer.ReadBits(1);
            int surround_sum = buffer.ReadBits(1);
            int dialog_norm = buffer.ReadBits(4);
            if (source_pcm_res < 0 || source_pcm_res >= dca_bits_per_sample.Length)
            {
                return;
            }
            int subframes = buffer.ReadBits(4);
            int total_channels = buffer.ReadBits(3) + 1 + ext_coding;

            stream.SampleRate = dca_sample_rates[sample_rate];
            stream.ChannelCount = total_channels;
            stream.LFE = (lfe > 0 ? 1 : 0);
            stream.BitDepth = dca_bits_per_sample[source_pcm_res];
            stream.DialNorm = -dialog_norm;
            if ((source_pcm_res & 0x1) == 0x1)
            {
                stream.AudioMode = TSAudioMode.Extended;
            }

            stream.BitRate = (uint)dca_bit_rates[bit_rate];
            switch (stream.BitRate)
            {
                case 1:
                    if (bitrate > 0)
                    {
                        stream.BitRate = bitrate;
                        stream.IsVBR = false;
                        stream.IsInitialized = true;
                    }
                    else
                    {
                        stream.BitRate = 0;
                    }
                    break;

                case 2:
                case 3:
                    stream.IsVBR = true;
                    stream.IsInitialized = true;
                    break;

                default:
                    stream.IsVBR = false;
                    stream.IsInitialized = true;
                    break;
            }
        }
Esempio n. 3
0
        public static void Scan(
            TSVideoStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            uint parse = 0;
            byte accessUnitDelimiterParse = 0;
            byte sequenceParameterSetParse = 0;
            string profile = null;
            string level = null;
            byte constraintSet0Flag = 0;
            byte constraintSet1Flag = 0;
            byte constraintSet2Flag = 0;
            byte constraintSet3Flag = 0;

            for (int i = 0; i < buffer.Length; i++)
            {
                parse = (parse << 8) + buffer.ReadByte();

                if (parse == 0x00000109)
                {
                    accessUnitDelimiterParse = 1;
                }
                else if (accessUnitDelimiterParse > 0)
                {
                    --accessUnitDelimiterParse;
                    if (accessUnitDelimiterParse == 0)
                    {
                        switch ((parse & 0xFF) >> 5)
                        {
                            case 0: // I
                            case 3: // SI
                            case 5: // I, SI
                                tag = "I";
                                break;

                            case 1: // I, P
                            case 4: // SI, SP
                            case 6: // I, SI, P, SP
                                tag = "P";
                                break;

                            case 2: // I, P, B
                            case 7: // I, SI, P, SP, B
                                tag = "B";
                                break;
                        }
                        if (stream.IsInitialized) return;
                    }
                }
                else if (parse == 0x00000127 || parse == 0x00000167)
                {
                    sequenceParameterSetParse = 3;
                }
                else if (sequenceParameterSetParse > 0)
                {
                    --sequenceParameterSetParse;
                    switch (sequenceParameterSetParse)
                    {
                        case 2:
                            switch (parse & 0xFF)
                            {
                                case 66:
                                    profile = "Baseline Profile";
                                    break;
                                case 77:
                                    profile = "Main Profile";
                                    break;
                                case 88:
                                    profile = "Extended Profile";
                                    break;
                                case 100:
                                    profile = "High Profile";
                                    break;
                                case 110:
                                    profile = "High 10 Profile";
                                    break;
                                case 122:
                                    profile = "High 4:2:2 Profile";
                                    break;
                                case 144:
                                    profile = "High 4:4:4 Profile";
                                    break;
                                default:
                                    profile = "Unknown Profile";
                                    break;
                            }
                            break;

                        case 1:
                            constraintSet0Flag = (byte)
                                ((parse & 0x80) >> 7);
                            constraintSet1Flag = (byte)
                                ((parse & 0x40) >> 6);
                            constraintSet2Flag = (byte)
                                ((parse & 0x20) >> 5);
                            constraintSet3Flag = (byte)
                                ((parse & 0x10) >> 4);
                            break;

                        case 0:
                            byte b = (byte)(parse & 0xFF);
                            if (b == 11 && constraintSet3Flag == 1)
                            {
                                level = "1b";
                            }
                            else
                            {
                                level = string.Format(
                                    "{0:D}.{1:D}",
                                    b / 10, (b - ((b / 10) * 10)));
                            }
                            stream.EncodingProfile = string.Format(
                                "{0} {1}", profile, level);
                            stream.IsVBR = true;
                            stream.IsInitialized = true;
                            break;
                    }
                }
            }
            return;
        }
Esempio n. 4
0
        public static void Scan(
            TSVideoStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            int parse = 0;
            byte frameHeaderParse = 0;
            byte sequenceHeaderParse = 0;
            bool isInterlaced = false;

            for (int i = 0; i < buffer.Length; i++)
            {
                parse = (parse << 8) + buffer.ReadByte();

                if (parse == 0x0000010D)
                {
                    frameHeaderParse = 4;
                }
                else if (frameHeaderParse > 0)
                {
                    --frameHeaderParse;
                    if (frameHeaderParse == 0)
                    {
                        uint pictureType = 0;
                        if (isInterlaced)
                        {
                            if ((parse & 0x80000000) == 0)
                            {
                                pictureType =
                                    (uint)((parse & 0x78000000) >> 13);
                            }
                            else
                            {
                                pictureType =
                                    (uint)((parse & 0x3c000000) >> 12);
                            }
                        }
                        else
                        {
                            pictureType =
                                (uint)((parse & 0xf0000000) >> 14);
                        }

                        if ((pictureType & 0x20000) == 0)
                        {
                            tag = "P";
                        }
                        else if ((pictureType & 0x10000) == 0)
                        {
                            tag = "B";
                        }
                        else if ((pictureType & 0x8000) == 0)
                        {
                            tag = "I";
                        }
                        else if ((pictureType & 0x4000) == 0)
                        {
                            tag = "B"; // TODO: "BI"
                        }
                        else
                        {
                            tag = null;
                        }
                        if (stream.IsInitialized) return;
                    }
                }
                else if (parse == 0x0000010F)
                {
                    sequenceHeaderParse = 6;
                }
                else if (sequenceHeaderParse > 0)
                {
                    --sequenceHeaderParse;
                    switch (sequenceHeaderParse)
                    {
                        case 5:
                            int profileLevel = ((parse & 0x38) >> 3);
                            if (((parse & 0xC0) >> 6) == 3)
                            {
                                stream.EncodingProfile = string.Format(
                                    "Advanced Profile {0}", profileLevel);
                            }
                            else
                            {
                                stream.EncodingProfile = string.Format(
                                    "Main Profile {0}", profileLevel);
                            }
                            break;

                        case 0:
                            if (((parse & 0x40) >> 6) > 0)
                            {
                                isInterlaced = true;
                            }
                            else
                            {
                                isInterlaced = false;
                            }
                            break;
                    }
                    stream.IsVBR = true;
                    stream.IsInitialized = true;
                }
            }
        }
Esempio n. 5
0
        public static void Scan(
            TSAudioStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            if (stream.IsInitialized) return;

            byte[] sync = buffer.ReadBytes(2);
            if (sync == null ||
                sync[0] != 0x0B ||
                sync[1] != 0x77)
            {
                return;
            }

            int sr_code = 0;
            int frame_size = 0;
            int frame_size_code = 0;
            int channel_mode = 0;
            int lfe_on = 0;
            int dial_norm = 0;
            int num_blocks = 0;

            byte[] hdr = buffer.ReadBytes(4);
            int bsid = (hdr[3] & 0xF8) >> 3;
            buffer.Seek(-4, SeekOrigin.Current);
            if (bsid <= 10)
            {
                byte[] crc = buffer.ReadBytes(2);
                sr_code = buffer.ReadBits(2);
                frame_size_code = buffer.ReadBits(6);
                bsid = buffer.ReadBits(5);
                int bsmod = buffer.ReadBits(3);

                channel_mode = buffer.ReadBits(3);
                int cmixlev = 0;
                if (((channel_mode & 0x1) > 0) && (channel_mode != 0x1))
                {
                    cmixlev = buffer.ReadBits(2);
                }
                int surmixlev = 0;
                if ((channel_mode & 0x4) > 0)
                {
                    surmixlev = buffer.ReadBits(2);
                }
                int dsurmod = 0;
                if (channel_mode == 0x2)
                {
                    dsurmod = buffer.ReadBits(2);
                    if (dsurmod == 0x2)
                    {
                        stream.AudioMode = TSAudioMode.Surround;
                    }
                }
                lfe_on = buffer.ReadBits(1);
                dial_norm = buffer.ReadBits(5);
                int compr = 0;
                if (1 == buffer.ReadBits(1))
                {
                    compr = buffer.ReadBits(8);
                }
                int langcod = 0;
                if (1 == buffer.ReadBits(1))
                {
                    langcod = buffer.ReadBits(8);
                }
                int mixlevel = 0;
                int roomtyp = 0;
                if (1 == buffer.ReadBits(1))
                {
                    mixlevel = buffer.ReadBits(5);
                    roomtyp = buffer.ReadBits(2);
                }
                if (channel_mode == 0)
                {
                    int dialnorm2 = buffer.ReadBits(5);
                    int compr2 = 0;
                    if (1 == buffer.ReadBits(1))
                    {
                        compr2 = buffer.ReadBits(8);
                    }
                    int langcod2 = 0;
                    if (1 == buffer.ReadBits(1))
                    {
                        langcod2 = buffer.ReadBits(8);
                    }
                    int mixlevel2 = 0;
                    int roomtyp2 = 0;
                    if (1 == buffer.ReadBits(1))
                    {
                        mixlevel2 = buffer.ReadBits(5);
                        roomtyp2 = buffer.ReadBits(2);
                    }
                }
                int copyrightb = buffer.ReadBits(1);
                int origbs = buffer.ReadBits(1);
                if (bsid == 6)
                {
                    if (1 == buffer.ReadBits(1))
                    {
                        int dmixmod = buffer.ReadBits(2);
                        int ltrtcmixlev = buffer.ReadBits(3);
                        int ltrtsurmixlev = buffer.ReadBits(3);
                        int lorocmixlev = buffer.ReadBits(3);
                        int lorosurmixlev = buffer.ReadBits(3);
                    }
                    if (1 == buffer.ReadBits(1))
                    {
                        int dsurexmod = buffer.ReadBits(2);
                        int dheadphonmod = buffer.ReadBits(2);
                        if (dheadphonmod == 0x2)
                        {
                            // TODO
                        }
                        int adconvtyp = buffer.ReadBits(1);
                        int xbsi2 = buffer.ReadBits(8);
                        int encinfo = buffer.ReadBits(1);
                        if (dsurexmod == 2)
                        {
                            stream.AudioMode = TSAudioMode.Extended;
                        }
                    }
                }
            }
            else
            {
                int frame_type = buffer.ReadBits(2);
                int substreamid = buffer.ReadBits(3);
                frame_size = (buffer.ReadBits(11) + 1) << 1;

                sr_code = buffer.ReadBits(2);
                if (sr_code == 3)
                {
                    sr_code = buffer.ReadBits(2);
                }
                else
                {
                    num_blocks = buffer.ReadBits(2);
                }
                channel_mode = buffer.ReadBits(3);
                lfe_on = buffer.ReadBits(1);
            }

            switch (channel_mode)
            {
                case 0: // 1+1
                    stream.ChannelCount = 2;
                    if (stream.AudioMode == TSAudioMode.Unknown)
                    {
                        stream.AudioMode = TSAudioMode.DualMono;
                    }
                    break;
                case 1: // 1/0
                    stream.ChannelCount = 1;
                    break;
                case 2: // 2/0
                    stream.ChannelCount = 2;
                    if (stream.AudioMode == TSAudioMode.Unknown)
                    {
                        stream.AudioMode = TSAudioMode.Stereo;
                    }
                    break;
                case 3: // 3/0
                    stream.ChannelCount = 3;
                    break;
                case 4: // 2/1
                    stream.ChannelCount = 3;
                    break;
                case 5: // 3/1
                    stream.ChannelCount = 4;
                    break;
                case 6: // 2/2
                    stream.ChannelCount = 4;
                    break;
                case 7: // 3/2
                    stream.ChannelCount = 5;
                    break;
                default:
                    stream.ChannelCount = 0;
                    break;
            }

            switch (sr_code)
            {
                case 0:
                    stream.SampleRate = 48000;
                    break;
                case 1:
                    stream.SampleRate = 44100;
                    break;
                case 2:
                    stream.SampleRate = 32000;
                    break;
                default:
                    stream.SampleRate = 0;
                    break;
            }

            if (bsid <= 10)
            {
                switch (frame_size_code >> 1)
                {
                    case 18:
                        stream.BitRate = 640000;
                        break;
                    case 17:
                        stream.BitRate = 576000;
                        break;
                    case 16:
                        stream.BitRate = 512000;
                        break;
                    case 15:
                        stream.BitRate = 448000;
                        break;
                    case 14:
                        stream.BitRate = 384000;
                        break;
                    case 13:
                        stream.BitRate = 320000;
                        break;
                    case 12:
                        stream.BitRate = 256000;
                        break;
                    case 11:
                        stream.BitRate = 224000;
                        break;
                    case 10:
                        stream.BitRate = 192000;
                        break;
                    case 9:
                        stream.BitRate = 160000;
                        break;
                    case 8:
                        stream.BitRate = 128000;
                        break;
                    case 7:
                        stream.BitRate = 112000;
                        break;
                    case 6:
                        stream.BitRate = 96000;
                        break;
                    case 5:
                        stream.BitRate = 80000;
                        break;
                    case 4:
                        stream.BitRate = 64000;
                        break;
                    case 3:
                        stream.BitRate = 56000;
                        break;
                    case 2:
                        stream.BitRate = 48000;
                        break;
                    case 1:
                        stream.BitRate = 40000;
                        break;
                    case 0:
                        stream.BitRate = 32000;
                        break;
                    default:
                        stream.BitRate = 0;
                        break;
                }
            }
            else
            {
                stream.BitRate = (long)
                    (4.0 * frame_size * stream.SampleRate / (num_blocks * 256));
            }

            stream.LFE = lfe_on;
            if (stream.StreamType != TSStreamType.AC3_PLUS_AUDIO &&
                stream.StreamType != TSStreamType.AC3_PLUS_SECONDARY_AUDIO)
            {
                stream.DialNorm = dial_norm - 31;
            }
            stream.IsVBR = false;
            stream.IsInitialized = true;
        }
Esempio n. 6
0
        public static void Scan(
            TSVideoStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            int parse = 0;
            int pictureParse = 0;
            int sequenceHeaderParse = 0;
            int extensionParse = 0;
            int sequenceExtensionParse = 0;            

            for (int i = 0; i < buffer.Length; i++)
            {
                parse = (parse << 8) + buffer.ReadByte();

                if (parse == 0x00000100)
                {
                    pictureParse = 2;
                }
                else if (parse == 0x000001B3)
                {
                    sequenceHeaderParse = 7;
                }
                else if (sequenceHeaderParse > 0)
                {
                    --sequenceHeaderParse;
                    switch (sequenceHeaderParse)
                    {
#if DEBUG
                        case 6:
                            break;

                        case 5:
                            break;

                        case 4:
                            stream.Width =
                                (int)((parse & 0xFFF000) >> 12);
                            stream.Height =
                                (int)(parse & 0xFFF);
                            break;

                        case 3:
                            stream.AspectRatio =
                                (TSAspectRatio)((parse & 0xF0) >> 4);

                            switch ((parse & 0xF0) >> 4)
                            {
                                case 0: // Forbidden
                                    break;
                                case 1: // Square
                                    break;
                                case 2: // 4:3
                                    break;
                                case 3: // 16:9
                                    break;
                                case 4: // 2.21:1
                                    break;
                                default: // Reserved
                                    break;
                            }

                            switch (parse & 0xF)
                            {
                                case 0: // Forbidden
                                    break;
                                case 1: // 23.976
                                    stream.FrameRateEnumerator = 24000;
                                    stream.FrameRateDenominator = 1001;
                                    break;
                                case 2: // 24
                                    stream.FrameRateEnumerator = 24000;
                                    stream.FrameRateDenominator = 1000;
                                    break;
                                case 3: // 25
                                    stream.FrameRateEnumerator = 25000;
                                    stream.FrameRateDenominator = 1000;
                                    break;
                                case 4: // 29.97
                                    stream.FrameRateEnumerator = 30000;
                                    stream.FrameRateDenominator = 1001;
                                    break;
                                case 5: // 30
                                    stream.FrameRateEnumerator = 30000;
                                    stream.FrameRateDenominator = 1000;
                                    break;
                                case 6: // 50
                                    stream.FrameRateEnumerator = 50000;
                                    stream.FrameRateDenominator = 1000;
                                    break;
                                case 7: // 59.94
                                    stream.FrameRateEnumerator = 60000;
                                    stream.FrameRateDenominator = 1001;
                                    break;
                                case 8: // 60
                                    stream.FrameRateEnumerator = 60000;
                                    stream.FrameRateDenominator = 1000;
                                    break;
                                default: // Reserved
                                    stream.FrameRateEnumerator = 0;
                                    stream.FrameRateDenominator = 0;
                                    break;
                            }
                            break;

                        case 2:
                            break;

                        case 1:
                            break;
#endif

                        case 0:
#if DEBUG
                            stream.BitRate =
                                (((parse & 0xFFFFC0) >> 6) * 200);
#endif
                            stream.IsVBR = true;
                            stream.IsInitialized = true;
                            break;
                    }
                }
                else if (pictureParse > 0)
                {
                    --pictureParse;
                    if (pictureParse == 0)
                    {
                        switch ((parse & 0x38) >> 3)
                        {
                            case 1:
                                tag = "I";
                                break;
                            case 2:
                                tag = "P";
                                break;
                            case 3:
                                tag = "B";
                                break;
                            default:
                                break;
                        }
                        if (stream.IsInitialized) return;
                    }
                }
                else if (parse == 0x000001B5)
                {
                    extensionParse = 1;
                }
                else if (extensionParse > 0)
                {
                    --extensionParse;
                    if (extensionParse == 0)
                    {
                        if ((parse & 0xF0) == 0x10)
                        {
                            sequenceExtensionParse = 1;
                        }
                    }
                }
                else if (sequenceExtensionParse > 0)
                {
                    --sequenceExtensionParse;
#if DEBUG
                    if (sequenceExtensionParse == 0)
                    {
                        uint sequenceExtension = 
                            ((parse & 0x8) >> 3);
                        if (sequenceExtension == 0)
                        {
                            stream.IsInterlaced = true;
                        }
                        else
                        {
                            stream.IsInterlaced = false;
                        }
                    }
#endif
                }
            }
        }
Esempio n. 7
0
        public static void Scan(
            TSAudioStream stream,
            TSStreamBuffer buffer,
            long bitrate,
            ref string tag)
        {
            if (stream.IsInitialized &&
                (stream.StreamType == TSStreamType.DTS_HD_SECONDARY_AUDIO ||
                (stream.CoreStream != null &&
                 stream.CoreStream.IsInitialized))) return;

            bool syncFound = false;
            uint sync = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                sync = (sync << 8) + buffer.ReadByte();
                if (sync == 0x64582025)
                {
                    syncFound = true;
                    break;
                }
            }

            if (!syncFound)
            {
                tag = "CORE";
                if (stream.CoreStream == null)
                {
                    stream.CoreStream = new TSAudioStream();
                    stream.CoreStream.StreamType = TSStreamType.DTS_AUDIO;
                }
                if (!stream.CoreStream.IsInitialized)
                {
                    buffer.BeginRead();
                    TSCodecDTS.Scan(stream.CoreStream, buffer, bitrate, ref tag);
                }
                return;
            }

            tag = "HD";
            int temp1 = buffer.ReadBits(8);
            int nuSubStreamIndex = buffer.ReadBits(2);
            int nuExtSSHeaderSize = 0;
            int nuExtSSFSize = 0;
            int bBlownUpHeader = buffer.ReadBits(1);
            if (1 == bBlownUpHeader)
            {
                nuExtSSHeaderSize = buffer.ReadBits(12) + 1;
                nuExtSSFSize = buffer.ReadBits(20) + 1;
            }
            else
            {
                nuExtSSHeaderSize = buffer.ReadBits(8) + 1;
                nuExtSSFSize = buffer.ReadBits(16) + 1;
            }
            int nuNumAudioPresent = 1;
            int nuNumAssets = 1;
            int bStaticFieldsPresent = buffer.ReadBits(1);
            if (1 == bStaticFieldsPresent)
            {
                int nuRefClockCode = buffer.ReadBits(2);
                int nuExSSFrameDurationCode = buffer.ReadBits(3) + 1;
                long nuTimeStamp = 0;
                if (1 == buffer.ReadBits(1))
                {
                    nuTimeStamp = (buffer.ReadBits(18) << 18) + buffer.ReadBits(18);
                }
                nuNumAudioPresent = buffer.ReadBits(3) + 1;
                nuNumAssets = buffer.ReadBits(3) + 1;
                int[] nuActiveExSSMask = new int[nuNumAudioPresent];
                for (int i = 0; i < nuNumAudioPresent; i++)
                {
                    nuActiveExSSMask[i] = buffer.ReadBits(nuSubStreamIndex + 1); //?
                }
                for (int i = 0; i < nuNumAudioPresent; i++)
                {
                    for (int j = 0; j < nuSubStreamIndex + 1; j++)
                    {
                        if (((j + 1) % 2) == 1)
                        {
                            int mask = buffer.ReadBits(8);
                        }
                    }
                }
                if (1 == buffer.ReadBits(1))
                {
                    int nuMixMetadataAdjLevel = buffer.ReadBits(2);
                    int nuBits4MixOutMask = buffer.ReadBits(2) * 4 + 4;
                    int nuNumMixOutConfigs = buffer.ReadBits(2) + 1;
                    int[] nuMixOutChMask = new int[nuNumMixOutConfigs];
                    for (int i = 0; i < nuNumMixOutConfigs; i++)
                    {
                        nuMixOutChMask[i] = buffer.ReadBits(nuBits4MixOutMask);
                    }
                }
            }
            int[] AssetSizes = new int[nuNumAssets];
            for (int i = 0; i < nuNumAssets; i++)
            {
                if (1 == bBlownUpHeader)
                {
                    AssetSizes[i] = buffer.ReadBits(20) + 1;
                }
                else
                {
                    AssetSizes[i] = buffer.ReadBits(16) + 1;
                }                
            }
            for (int i = 0; i < nuNumAssets; i++)
            {
                long bufferPosition = buffer.Position;
                int nuAssetDescriptorFSIZE = buffer.ReadBits(9) + 1;
                int DescriptorDataForAssetIndex = buffer.ReadBits(3);
                if (1 == bStaticFieldsPresent)
                {
                    int AssetTypeDescrPresent = buffer.ReadBits(1);
                    if (1 == AssetTypeDescrPresent)
                    {
                        int AssetTypeDescriptor = buffer.ReadBits(4);
                    }
                    int LanguageDescrPresent = buffer.ReadBits(1);
                    if (1 == LanguageDescrPresent)
                    {
                        int LanguageDescriptor = buffer.ReadBits(24);
                    }
                    int bInfoTextPresent = buffer.ReadBits(1);
                    if (1 == bInfoTextPresent)
                    {
                        int nuInfoTextByteSize = buffer.ReadBits(10) + 1;
                        int[] InfoText = new int[nuInfoTextByteSize];
                        for (int j = 0; j < nuInfoTextByteSize; j++)
                        {
                            InfoText[j] = buffer.ReadBits(8);
                        }
                    }
                    int nuBitResolution = buffer.ReadBits(5) + 1;
                    int nuMaxSampleRate = buffer.ReadBits(4);
                    int nuTotalNumChs = buffer.ReadBits(8) + 1;
                    int bOne2OneMapChannels2Speakers = buffer.ReadBits(1);
                    int nuSpkrActivityMask = 0;
                    if (1 == bOne2OneMapChannels2Speakers)
                    {
                        int bEmbeddedStereoFlag = 0;
                        if (nuTotalNumChs > 2)
                        {
                            bEmbeddedStereoFlag = buffer.ReadBits(1);
                        }
                        int bEmbeddedSixChFlag = 0;
                        if (nuTotalNumChs > 6)
                        {
                            bEmbeddedSixChFlag = buffer.ReadBits(1);
                        }
                        int bSpkrMaskEnabled = buffer.ReadBits(1);
                        int nuNumBits4SAMask = 0;
                        if (1 == bSpkrMaskEnabled)
                        {
                            nuNumBits4SAMask = buffer.ReadBits(2);
                            nuNumBits4SAMask = nuNumBits4SAMask * 4 + 4;
                            nuSpkrActivityMask = buffer.ReadBits(nuNumBits4SAMask);
                        }
                        // TODO...
                    }
                    stream.SampleRate = SampleRates[nuMaxSampleRate];
                    stream.BitDepth = nuBitResolution;
                    
                    stream.LFE = 0;
                    if ((nuSpkrActivityMask & 0x8) == 0x8)
                    {
                        ++stream.LFE;
                    }
                    if ((nuSpkrActivityMask & 0x1000) == 0x1000)
                    {
                        ++stream.LFE;
                    }
                    stream.ChannelCount = nuTotalNumChs - stream.LFE;
                }
                if (nuNumAssets > 1)
                {
                    // TODO...
                    break;
                }
            }

            // TODO
            if (stream.CoreStream != null)
            {
                TSAudioStream coreStream = (TSAudioStream)stream.CoreStream;
                if (coreStream.AudioMode == TSAudioMode.Extended &&
                    stream.ChannelCount == 5)
                {
                    stream.AudioMode = TSAudioMode.Extended;
                }
                /*
                if (coreStream.DialNorm != 0)
                {
                    stream.DialNorm = coreStream.DialNorm;
                }
                */
            }

            if (stream.StreamType == TSStreamType.DTS_HD_MASTER_AUDIO)
            {
                stream.IsVBR = true;
                stream.IsInitialized = true;
            }
            else if (bitrate > 0)
            {
                stream.IsVBR = false;
                stream.BitRate = bitrate;
                if (stream.CoreStream != null)
                {
                    stream.BitRate += stream.CoreStream.BitRate;
                    stream.IsInitialized = true;
                }
                stream.IsInitialized = (stream.BitRate > 0 ? true : false);
            }            
        }
Esempio n. 8
0
        public static void Scan(
            TSAudioStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            if (stream.IsInitialized &&
                stream.CoreStream != null &&
                stream.CoreStream.IsInitialized) return;

            bool syncFound = false;
            uint sync = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                sync = (sync << 8) + buffer.ReadByte();
                if (sync == 0xF8726FBA)
                {
                    syncFound = true;
                    break;
                }
            }

            if (!syncFound)
            {
                tag = "CORE";
                if (stream.CoreStream == null)
                {
                    stream.CoreStream = new TSAudioStream();
                    stream.CoreStream.StreamType = TSStreamType.AC3_AUDIO;
                }
                if (!stream.CoreStream.IsInitialized)
                {
                    buffer.BeginRead();
                    TSCodecAC3.Scan(stream.CoreStream, buffer, ref tag);
                }
                return;
            }

            tag = "HD";
            int ratebits = buffer.ReadBits(4);
            if (ratebits != 0xF)
            {
                stream.SampleRate =
                    (((ratebits & 8) > 0 ? 44100 : 48000) << (ratebits & 7));
            }
            int temp1 = buffer.ReadBits(8);
            int channels_thd_stream1 = buffer.ReadBits(5);
            int temp2 = buffer.ReadBits(2);

            stream.ChannelCount = 0;
            stream.LFE = 0;
            int c_LFE2 = buffer.ReadBits(1);
            if (c_LFE2 == 1)
            {
                stream.LFE += 1;
            }
            int c_Cvh = buffer.ReadBits(1);
            if (c_Cvh == 1)
            {
                stream.ChannelCount += 1;
            }
            int c_LRw = buffer.ReadBits(1);
            if (c_LRw == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LRsd = buffer.ReadBits(1);
            if (c_LRsd == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_Ts = buffer.ReadBits(1);
            if (c_Ts == 1)
            {
                stream.ChannelCount += 1;
            }
            int c_Cs = buffer.ReadBits(1);
            if (c_Cs == 1)
            {
                stream.ChannelCount += 1;
            }
            int c_LRrs = buffer.ReadBits(1);
            if (c_LRrs == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LRc = buffer.ReadBits(1);
            if (c_LRc == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LRvh = buffer.ReadBits(1);
            if (c_LRvh == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LRs = buffer.ReadBits(1);
            if (c_LRs == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LFE = buffer.ReadBits(1);
            if (c_LFE == 1)
            {
                stream.LFE += 1;
            }
            int c_C = buffer.ReadBits(1);
            if (c_C == 1)
            {
                stream.ChannelCount += 1;
            }
            int c_LR = buffer.ReadBits(1);
            if (c_LR == 1)
            {
                stream.ChannelCount += 2;
            }

            int access_unit_size = 40 << (ratebits & 7);
            int access_unit_size_pow2 = 64 << (ratebits & 7);

            buffer.ReadBits(24);
            buffer.ReadBits(24);

            int is_vbr = buffer.ReadBits(1);
            int peak_bitrate = buffer.ReadBits(15);
            peak_bitrate = (peak_bitrate * stream.SampleRate) >> 4;

            double peak_bitdepth =
                (double)peak_bitrate /
                (stream.ChannelCount + stream.LFE) /
                stream.SampleRate;
            if (peak_bitdepth > 14)
            {
                stream.BitDepth = 24;
            }
            else
            {
                stream.BitDepth = 16;
            }

            #if DEBUG
            System.Diagnostics.Debug.WriteLine(string.Format(
                "{0}\t{1}\t{2:F2}",
                stream.PID, peak_bitrate, peak_bitdepth));
            #endif
            stream.IsVBR = true;
            stream.IsInitialized = true;
        }
Esempio n. 9
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;
        }
Esempio n. 10
0
        public static void Scan(
            TSAudioStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            if (stream.IsInitialized) return;

            byte[] header = buffer.ReadBytes(4);
            int flags = (header[2] << 8) + header[3];

            switch ((flags & 0xF000) >> 12)
            {
                case 1: // 1/0/0
                    stream.ChannelCount = 1;
                    stream.LFE = 0;
                    break;
                case 3: // 2/0/0
                    stream.ChannelCount = 2;
                    stream.LFE = 0;
                    break;
                case 4: // 3/0/0
                    stream.ChannelCount = 3;
                    stream.LFE = 0;
                    break;
                case 5: // 2/1/0
                    stream.ChannelCount = 3;
                    stream.LFE = 0;
                    break;
                case 6: // 3/1/0
                    stream.ChannelCount = 4;
                    stream.LFE = 0;
                    break;
                case 7: // 2/2/0
                    stream.ChannelCount = 4;
                    stream.LFE = 0;
                    break;
                case 8: // 3/2/0
                    stream.ChannelCount = 5;
                    stream.LFE = 0;
                    break;
                case 9: // 3/2/1
                    stream.ChannelCount = 5;
                    stream.LFE = 1;
                    break;
                case 10: // 3/4/0
                    stream.ChannelCount = 7;
                    stream.LFE = 0;
                    break;
                case 11: // 3/4/1
                    stream.ChannelCount = 7;
                    stream.LFE = 1;
                    break;
                default:
                    stream.ChannelCount = 0;
                    stream.LFE = 0;
                    break;
            }

            switch ((flags & 0xC0) >> 6)
            {
                case 1:
                    stream.BitDepth = 16;
                    break;
                case 2:
                    stream.BitDepth = 20;
                    break;
                case 3:
                    stream.BitDepth = 24;
                    break;
                default:
                    stream.BitDepth = 0;
                    break;
            }

            switch ((flags & 0xF00) >> 8)
            {
                case 1:
                    stream.SampleRate = 48000;
                    break;
                case 4:
                    stream.SampleRate = 96000;
                    break;
                case 5:
                    stream.SampleRate = 192000;
                    break;
                default:
                    stream.SampleRate = 0;
                    break;
            }

            stream.BitRate = (uint)
                (stream.SampleRate * stream.BitDepth *
                 (stream.ChannelCount + stream.LFE));

            stream.IsVBR = false;
            stream.IsInitialized = true;
        }
Esempio n. 11
0
        public void Scan(TSAudioStream stream, TSStreamBuffer buffer, ref string tag, long?bitrate)
        {
            if (stream.IsInitialized && stream.CoreStream != null && stream.CoreStream.IsInitialized)
            {
                return;
            }

            var  syncFound = false;
            uint sync      = 0;

            for (var i = 0; i < buffer.Length; i++)
            {
                sync = (sync << 8) + buffer.ReadByte();
                if (sync == 0xF8726FBA)
                {
                    syncFound = true;
                    break;
                }
            }

            if (!syncFound)
            {
                tag = "CORE";
                stream.CoreStream ??= new TSAudioStream {
                    StreamType = TSStreamType.AC3_AUDIO
                };
                if (stream.CoreStream.IsInitialized)
                {
                    return;
                }
                buffer.BeginRead();
                (new TSCodecAC3()).Scan(stream.CoreStream, buffer, ref tag, bitrate);

                return;
            }

            tag = "HD";
            int ratebits = buffer.ReadBits2(4);

            if (ratebits != 0xF)
            {
                stream.SampleRate = ((ratebits & 8) > 0 ? 44100 : 48000) << (ratebits & 7);
            }
            buffer.BSSkipBits(15);

            stream.ChannelCount = 0;
            stream.LFE          = 0;
            if (buffer.ReadBool())
            {
                stream.LFE += 1;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 1;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 2;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 2;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 1;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 1;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 2;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 2;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 2;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 2;
            }
            if (buffer.ReadBool())
            {
                stream.LFE += 1;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 1;
            }
            if (buffer.ReadBool())
            {
                stream.ChannelCount += 2;
            }

            buffer.BSSkipBits(49);

            var peakBitrate = buffer.ReadBits4(15);

            peakBitrate = (uint)((peakBitrate * stream.SampleRate) >> 4);

            var peakBitdepth = (double)peakBitrate / (stream.ChannelCount + stream.LFE) / stream.SampleRate;

            stream.BitDepth = peakBitdepth > 14 ? 24 : 16;

            buffer.BSSkipBits(79);

            var hasExtensions = buffer.ReadBool();
            var numExtensions = buffer.ReadBits2(4) * 2 + 1;
            var hasContent    = Convert.ToBoolean(buffer.ReadBits4(4));

            if (hasExtensions)
            {
                for (var idx = 0; idx < numExtensions; ++idx)
                {
                    if (Convert.ToBoolean(buffer.ReadBits2(8)))
                    {
                        hasContent = true;
                    }
                }

                if (hasContent)
                {
                    stream.HasExtensions = true;
                }
            }

#if DEBUG
            Debug.WriteLine($"{stream.PID}\t{peakBitrate}\t{peakBitdepth:F2}");
#endif

            /*
             * // TODO: Get THD dialnorm from metadata
             * if (stream.CoreStream != null)
             * {
             *  TSAudioStream coreStream = (TSAudioStream)stream.CoreStream;
             *  if (coreStream.DialNorm != 0)
             *  {
             *      stream.DialNorm = coreStream.DialNorm;
             *  }
             * }
             */

            stream.IsVBR = true;
            if (stream.CoreStream != null && stream.CoreStream.IsInitialized)
            {
                stream.IsInitialized = true;
            }
        }
Esempio n. 12
0
        public static void Scan(
            TSVideoStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            uint   parse = 0;
            byte   accessUnitDelimiterParse  = 0;
            byte   sequenceParameterSetParse = 0;
            string profile            = null;
            string level              = null;
            byte   constraintSet0Flag = 0;
            byte   constraintSet1Flag = 0;
            byte   constraintSet2Flag = 0;
            byte   constraintSet3Flag = 0;

            for (int i = 0; i < buffer.Length; i++)
            {
                parse = (parse << 8) + buffer.ReadByte();

                if (parse == 0x00000109)
                {
                    accessUnitDelimiterParse = 1;
                }
                else if (accessUnitDelimiterParse > 0)
                {
                    --accessUnitDelimiterParse;
                    if (accessUnitDelimiterParse == 0)
                    {
                        switch ((parse & 0xFF) >> 5)
                        {
                        case 0:     // I
                        case 3:     // SI
                        case 5:     // I, SI
                            tag = "I";
                            break;

                        case 1:     // I, P
                        case 4:     // SI, SP
                        case 6:     // I, SI, P, SP
                            tag = "P";
                            break;

                        case 2:     // I, P, B
                        case 7:     // I, SI, P, SP, B
                            tag = "B";
                            break;
                        }
                        if (stream.IsInitialized)
                        {
                            return;
                        }
                    }
                }
                else if (parse == 0x00000127 || parse == 0x00000167)
                {
                    sequenceParameterSetParse = 3;
                }
                else if (sequenceParameterSetParse > 0)
                {
                    --sequenceParameterSetParse;
                    switch (sequenceParameterSetParse)
                    {
                    case 2:
                        switch (parse & 0xFF)
                        {
                        case 66:
                            profile = "Baseline Profile";
                            break;

                        case 77:
                            profile = "Main Profile";
                            break;

                        case 88:
                            profile = "Extended Profile";
                            break;

                        case 100:
                            profile = "High Profile";
                            break;

                        case 110:
                            profile = "High 10 Profile";
                            break;

                        case 122:
                            profile = "High 4:2:2 Profile";
                            break;

                        case 144:
                            profile = "High 4:4:4 Profile";
                            break;

                        default:
                            profile = "Unknown Profile";
                            break;
                        }
                        break;

                    case 1:
                        constraintSet0Flag = (byte)
                                             ((parse & 0x80) >> 7);
                        constraintSet1Flag = (byte)
                                             ((parse & 0x40) >> 6);
                        constraintSet2Flag = (byte)
                                             ((parse & 0x20) >> 5);
                        constraintSet3Flag = (byte)
                                             ((parse & 0x10) >> 4);
                        break;

                    case 0:
                        byte b = (byte)(parse & 0xFF);
                        if (b == 11 && constraintSet3Flag == 1)
                        {
                            level = "1b";
                        }
                        else
                        {
                            level = string.Format(
                                "{0:D}.{1:D}",
                                b / 10, (b - ((b / 10) * 10)));
                        }
                        stream.EncodingProfile = string.Format(
                            "{0} {1}", profile, level);
                        stream.IsVBR         = true;
                        stream.IsInitialized = true;
                        break;
                    }
                }
            }
            return;
        }
Esempio n. 13
0
        public static void Scan(TSAudioStream stream, TSStreamBuffer buffer, ref string tag)
        {
            if (stream.IsInitialized)
            {
                return;
            }

            byte[] sync = buffer.ReadBytes(2);
            if (sync == null || sync[0] != 0x0B || sync[1] != 0x77)
            {
                return;
            }

            var secondFrame = stream.ChannelCount > 0;

            uint srCode;
            uint frameSize     = 0;
            uint frameSizeCode = 0;
            uint channelMode;
            uint lfeOn;
            uint dialNorm;
            uint numBlocks = 0;

            byte[] hdr  = buffer.ReadBytes(4);
            uint   bsid = (uint)((hdr[3] & 0xF8) >> 3);

            buffer.Seek(-4, SeekOrigin.Current);
            if (bsid <= 10)
            {
                buffer.BSSkipBytes(2);
                srCode        = buffer.ReadBits2(2);
                frameSizeCode = buffer.ReadBits2(6);
                bsid          = buffer.ReadBits2(5);
                buffer.BSSkipBits(3);

                channelMode = buffer.ReadBits2(3);
                if (((channelMode & 0x1) > 0) && (channelMode != 0x1))
                {
                    buffer.BSSkipBits(2);
                }
                if ((channelMode & 0x4) > 0)
                {
                    buffer.BSSkipBits(2);
                }
                if (channelMode == 0x2)
                {
                    var dsurmod = buffer.ReadBits2(2);
                    if (dsurmod == 0x2)
                    {
                        stream.AudioMode = TSAudioMode.Surround;
                    }
                }
                lfeOn    = buffer.ReadBits2(1);
                dialNorm = buffer.ReadBits2(5);
                if (buffer.ReadBool())
                {
                    buffer.BSSkipBits(8);
                }
                if (buffer.ReadBool())
                {
                    buffer.BSSkipBits(8);
                }
                if (buffer.ReadBool())
                {
                    buffer.BSSkipBits(7);
                }
                if (channelMode == 0)
                {
                    buffer.BSSkipBits(5);
                    if (buffer.ReadBool())
                    {
                        buffer.BSSkipBits(8);
                    }
                    if (buffer.ReadBool())
                    {
                        buffer.BSSkipBits(8);
                    }
                    if (buffer.ReadBool())
                    {
                        buffer.BSSkipBits(7);
                    }
                }
                buffer.BSSkipBits(2);
                if (bsid == 6)
                {
                    if (buffer.ReadBool())
                    {
                        buffer.BSSkipBits(14);
                    }
                    if (buffer.ReadBool())
                    {
                        uint dsurexmod    = buffer.ReadBits2(2);
                        uint dheadphonmod = buffer.ReadBits2(2);
                        if (dheadphonmod == 0x2)
                        {
                            // TODO
                        }
                        buffer.BSSkipBits(10);
                        if (dsurexmod == 2)
                        {
                            stream.AudioMode = TSAudioMode.Extended;
                        }
                    }
                }
            }
            else
            {
                uint frameType = buffer.ReadBits2(2);
                buffer.BSSkipBits(3);

                frameSize = (buffer.ReadBits4(11) + 1) << 1;

                srCode = buffer.ReadBits2(2);
                if (srCode == 3)
                {
                    srCode    = buffer.ReadBits2(2);
                    numBlocks = 3;
                }
                else
                {
                    numBlocks = buffer.ReadBits2(2);
                }
                channelMode = buffer.ReadBits2(3);
                lfeOn       = buffer.ReadBits2(1);
                bsid        = buffer.ReadBits2(5);
                dialNorm    = buffer.ReadBits2(5);

                if (buffer.ReadBool())
                {
                    buffer.BSSkipBits(8);
                }
                if (channelMode == 0) // 1+1
                {
                    buffer.BSSkipBits(5);
                    if (buffer.ReadBool())
                    {
                        buffer.BSSkipBits(8);
                    }
                }
                if (frameType == 1) //dependent stream
                {
                    stream.CoreStream = (TSAudioStream)stream.Clone();

                    if (buffer.ReadBool()) //channel remapping
                    {
                        uint chanmap = buffer.ReadBits4(16);

                        stream.ChannelCount  = stream.CoreStream.ChannelCount;
                        stream.ChannelCount += AC3ChanMap((int)chanmap);
                        lfeOn = (uint)stream.CoreStream.LFE;
                    }
                }

                bool emdfFound = false;

                do
                {
                    uint emdfSync = (buffer.ReadBits4(16));
                    if ((emdfSync) == 0x5838)
                    {
                        emdfFound = true;
                        break;
                    }
                    buffer.Seek(-2, SeekOrigin.Current);
                    buffer.BSSkipBits(1); // skip 1 bit
                } while (buffer.Position < buffer.Length);

                if (emdfFound)
                {
                    uint emdfContainerSize = buffer.ReadBits4(16);
                    var  remainAfterEmdf   = buffer.DataBitStreamRemain() - emdfContainerSize * 8;

                    uint emdfVersion = buffer.ReadBits2(2); //emdf_version
                    if (emdfVersion == 3)
                    {
                        emdfVersion += buffer.ReadBits2(2);
                    }

                    if (emdfVersion > 0)
                    {
                        buffer.BSSkipBits((int)(buffer.DataBitStreamRemain() - remainAfterEmdf));
                    }
                    else
                    {
                        var temp = buffer.ReadBits2(3);
                        if (temp == 0x7)
                        {
                            buffer.BSSkipBits(2); //skip 3 bits
                        }
                        var emdfPayloadID = buffer.ReadBits2(5);

                        if (emdfPayloadID > 0 && emdfPayloadID < 16)
                        {
                            if (emdfPayloadID == 0x1F)
                            {
                                buffer.BSSkipBits(5); //skip 5 bits
                            }
                            EmdfPayloadConfig(buffer);

                            int emdfPayloadSize = buffer.ReadBits2(8) * 8;
                            buffer.BSSkipBits(emdfPayloadSize + 1);
                        }

                        while ((emdfPayloadID = buffer.ReadBits2(5)) != 14 && buffer.Position < buffer.Length)
                        {
                            if (emdfPayloadID == 0x1F)
                            {
                                buffer.BSSkipBits(5); //skip 5 bits
                            }
                            EmdfPayloadConfig(buffer);

                            int emdfPayloadSize = buffer.ReadBits2(8) * 8;
                            buffer.ReadBits4(emdfPayloadSize + 1);
                        }

                        if (buffer.Position < buffer.Length && emdfPayloadID == 14)
                        {
                            EmdfPayloadConfig(buffer);

                            buffer.BSSkipBits(12);

                            uint jocNumObjectsBits = buffer.ReadBits2(6);

                            if (jocNumObjectsBits > 0)
                            {
                                stream.HasExtensions = true;
                            }
                        }
                    }
                }
            }

            if ((channelMode < 8) && stream.ChannelCount == 0)
            {
                stream.ChannelCount = AC3Channels[channelMode];
            }

            if (stream.AudioMode == TSAudioMode.Unknown)
            {
                switch (channelMode)
                {
                case 0:     // 1+1
                    stream.AudioMode = TSAudioMode.DualMono;
                    break;

                case 2:     // 2/0
                    stream.AudioMode = TSAudioMode.Stereo;
                    break;

                default:
                    stream.AudioMode = TSAudioMode.Unknown;
                    break;
                }
            }

            switch (srCode)
            {
            case 0:
                stream.SampleRate = 48000;
                break;

            case 1:
                stream.SampleRate = 44100;
                break;

            case 2:
                stream.SampleRate = 32000;
                break;

            default:
                stream.SampleRate = 0;
                break;
            }

            if (bsid <= 10)
            {
                uint fSize = frameSizeCode >> 1;
                if (fSize < 19)
                {
                    stream.BitRate = AC3Bitrate[fSize] * 1000;
                }
            }
            else
            {
                stream.BitRate = (long)
                                 (4.0 * frameSize * stream.SampleRate / (numBlocks * 256));
                if (stream.CoreStream != null)
                {
                    stream.BitRate += stream.CoreStream.BitRate;
                }
            }

            stream.LFE = (int)lfeOn;
            if (stream.StreamType != TSStreamType.AC3_PLUS_AUDIO &&
                stream.StreamType != TSStreamType.AC3_PLUS_SECONDARY_AUDIO)
            {
                stream.DialNorm = (int)(dialNorm - 31);
            }
            stream.IsVBR = false;
            if (stream.StreamType == TSStreamType.AC3_PLUS_AUDIO && bsid == 6 && !secondFrame)
            {
                stream.IsInitialized = false;
            }
            else
            {
                stream.IsInitialized = true;
            }
        }
Esempio n. 14
0
        public static void Scan(
            TSVideoStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            int  parse               = 0;
            byte frameHeaderParse    = 0;
            byte sequenceHeaderParse = 0;
            bool isInterlaced        = false;

            for (int i = 0; i < buffer.Length; i++)
            {
                parse = (parse << 8) + buffer.ReadByte();

                if (parse == 0x0000010D)
                {
                    frameHeaderParse = 4;
                }
                else if (frameHeaderParse > 0)
                {
                    --frameHeaderParse;
                    if (frameHeaderParse == 0)
                    {
                        uint pictureType = 0;
                        if (isInterlaced)
                        {
                            if ((parse & 0x80000000) == 0)
                            {
                                pictureType =
                                    (uint)((parse & 0x78000000) >> 13);
                            }
                            else
                            {
                                pictureType =
                                    (uint)((parse & 0x3c000000) >> 12);
                            }
                        }
                        else
                        {
                            pictureType =
                                (uint)((parse & 0xf0000000) >> 14);
                        }

                        if ((pictureType & 0x20000) == 0)
                        {
                            tag = "P";
                        }
                        else if ((pictureType & 0x10000) == 0)
                        {
                            tag = "B";
                        }
                        else if ((pictureType & 0x8000) == 0)
                        {
                            tag = "I";
                        }
                        else if ((pictureType & 0x4000) == 0)
                        {
                            tag = "BI";
                        }
                        else
                        {
                            tag = null;
                        }
                        if (stream.IsInitialized)
                        {
                            return;
                        }
                    }
                }
                else if (parse == 0x0000010F)
                {
                    sequenceHeaderParse = 6;
                }
                else if (sequenceHeaderParse > 0)
                {
                    --sequenceHeaderParse;
                    switch (sequenceHeaderParse)
                    {
                    case 5:
                        int profileLevel = ((parse & 0x38) >> 3);
                        if (((parse & 0xC0) >> 6) == 3)
                        {
                            stream.EncodingProfile = string.Format(
                                "Advanced Profile {0}", profileLevel);
                        }
                        else
                        {
                            stream.EncodingProfile = string.Format(
                                "Main Profile {0}", profileLevel);
                        }
                        break;

                    case 0:
                        if (((parse & 0x40) >> 6) > 0)
                        {
                            isInterlaced = true;
                        }
                        else
                        {
                            isInterlaced = false;
                        }
                        break;
                    }
                    stream.IsVBR         = true;
                    stream.IsInitialized = true;
                }
            }
        }
Esempio n. 15
0
        public static void Scan(
            TSAudioStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            if (stream.IsInitialized &&
                stream.CoreStream != null &&
                stream.CoreStream.IsInitialized)
            {
                return;
            }

            bool syncFound = false;
            uint sync      = 0;

            for (int i = 0; i < buffer.Length; i++)
            {
                sync = (sync << 8) + buffer.ReadByte();
                if (sync == 0xF8726FBA)
                {
                    syncFound = true;
                    break;
                }
            }

            if (!syncFound)
            {
                tag = "CORE";
                if (stream.CoreStream == null)
                {
                    stream.CoreStream            = new TSAudioStream();
                    stream.CoreStream.StreamType = TSStreamType.AC3_AUDIO;
                }
                if (!stream.CoreStream.IsInitialized)
                {
                    buffer.BeginRead();
                    TSCodecAC3.Scan(stream.CoreStream, buffer, ref tag);
                }
                return;
            }

            tag = "HD";
            int ratebits = buffer.ReadBits(4);

            if (ratebits != 0xF)
            {
                stream.SampleRate =
                    (((ratebits & 8) > 0 ? 44100 : 48000) << (ratebits & 7));
            }
            int temp1 = buffer.ReadBits(8);
            int channels_thd_stream1 = buffer.ReadBits(5);
            int temp2 = buffer.ReadBits(2);

            stream.ChannelCount = 0;
            stream.LFE          = 0;
            int c_LFE2 = buffer.ReadBits(1);

            if (c_LFE2 == 1)
            {
                stream.LFE += 1;
            }
            int c_Cvh = buffer.ReadBits(1);

            if (c_Cvh == 1)
            {
                stream.ChannelCount += 1;
            }
            int c_LRw = buffer.ReadBits(1);

            if (c_LRw == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LRsd = buffer.ReadBits(1);

            if (c_LRsd == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_Ts = buffer.ReadBits(1);

            if (c_Ts == 1)
            {
                stream.ChannelCount += 1;
            }
            int c_Cs = buffer.ReadBits(1);

            if (c_Cs == 1)
            {
                stream.ChannelCount += 1;
            }
            int c_LRrs = buffer.ReadBits(1);

            if (c_LRrs == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LRc = buffer.ReadBits(1);

            if (c_LRc == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LRvh = buffer.ReadBits(1);

            if (c_LRvh == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LRs = buffer.ReadBits(1);

            if (c_LRs == 1)
            {
                stream.ChannelCount += 2;
            }
            int c_LFE = buffer.ReadBits(1);

            if (c_LFE == 1)
            {
                stream.LFE += 1;
            }
            int c_C = buffer.ReadBits(1);

            if (c_C == 1)
            {
                stream.ChannelCount += 1;
            }
            int c_LR = buffer.ReadBits(1);

            if (c_LR == 1)
            {
                stream.ChannelCount += 2;
            }

            int access_unit_size      = 40 << (ratebits & 7);
            int access_unit_size_pow2 = 64 << (ratebits & 7);

            int a1 = buffer.ReadBits(16);
            int a2 = buffer.ReadBits(16);
            int a3 = buffer.ReadBits(16);

            int is_vbr       = buffer.ReadBits(1);
            int peak_bitrate = buffer.ReadBits(15);

            peak_bitrate = (peak_bitrate * stream.SampleRate) >> 4;

            double peak_bitdepth =
                (double)peak_bitrate /
                (stream.ChannelCount + stream.LFE) /
                stream.SampleRate;

            if (peak_bitdepth > 14)
            {
                stream.BitDepth = 24;
            }
            else
            {
                stream.BitDepth = 16;
            }

#if DEBUG
            System.Diagnostics.Debug.WriteLine(string.Format(
                                                   "{0}\t{1}\t{2:F2}",
                                                   stream.PID, peak_bitrate, peak_bitdepth));
#endif

            /*
             * // TODO: Get THD dialnorm from metadata
             * if (stream.CoreStream != null)
             * {
             *  TSAudioStream coreStream = (TSAudioStream)stream.CoreStream;
             *  if (coreStream.DialNorm != 0)
             *  {
             *      stream.DialNorm = coreStream.DialNorm;
             *  }
             * }
             */

            stream.IsVBR         = true;
            stream.IsInitialized = true;
        }
Esempio n. 16
0
        public static void Scan(
            TSAudioStream stream,
            TSStreamBuffer buffer,
            ref string tag)
        {
            if (stream.IsInitialized)
            {
                return;
            }

            byte[] sync = buffer.ReadBytes(2);
            if (sync == null ||
                sync[0] != 0x0B ||
                sync[1] != 0x77)
            {
                return;
            }

            int sr_code         = 0;
            int frame_size      = 0;
            int frame_size_code = 0;
            int channel_mode    = 0;
            int lfe_on          = 0;
            int dial_norm       = 0;
            int num_blocks      = 0;

            byte[] hdr  = buffer.ReadBytes(4);
            int    bsid = (hdr[3] & 0xF8) >> 3;

            buffer.Seek(-4, SeekOrigin.Current);
            if (bsid <= 10)
            {
                byte[] crc = buffer.ReadBytes(2);
                sr_code         = buffer.ReadBits(2);
                frame_size_code = buffer.ReadBits(6);
                bsid            = buffer.ReadBits(5);
                int bsmod = buffer.ReadBits(3);

                channel_mode = buffer.ReadBits(3);
                int cmixlev = 0;
                if (((channel_mode & 0x1) > 0) && (channel_mode != 0x1))
                {
                    cmixlev = buffer.ReadBits(2);
                }
                int surmixlev = 0;
                if ((channel_mode & 0x4) > 0)
                {
                    surmixlev = buffer.ReadBits(2);
                }
                int dsurmod = 0;
                if (channel_mode == 0x2)
                {
                    dsurmod = buffer.ReadBits(2);
                    if (dsurmod == 0x2)
                    {
                        stream.AudioMode = TSAudioMode.Surround;
                    }
                }
                lfe_on    = buffer.ReadBits(1);
                dial_norm = buffer.ReadBits(5);
                int compr = 0;
                if (1 == buffer.ReadBits(1))
                {
                    compr = buffer.ReadBits(8);
                }
                int langcod = 0;
                if (1 == buffer.ReadBits(1))
                {
                    langcod = buffer.ReadBits(8);
                }
                int mixlevel = 0;
                int roomtyp  = 0;
                if (1 == buffer.ReadBits(1))
                {
                    mixlevel = buffer.ReadBits(5);
                    roomtyp  = buffer.ReadBits(2);
                }
                if (channel_mode == 0)
                {
                    int dialnorm2 = buffer.ReadBits(5);
                    int compr2    = 0;
                    if (1 == buffer.ReadBits(1))
                    {
                        compr2 = buffer.ReadBits(8);
                    }
                    int langcod2 = 0;
                    if (1 == buffer.ReadBits(1))
                    {
                        langcod2 = buffer.ReadBits(8);
                    }
                    int mixlevel2 = 0;
                    int roomtyp2  = 0;
                    if (1 == buffer.ReadBits(1))
                    {
                        mixlevel2 = buffer.ReadBits(5);
                        roomtyp2  = buffer.ReadBits(2);
                    }
                }
                int copyrightb = buffer.ReadBits(1);
                int origbs     = buffer.ReadBits(1);
                if (bsid == 6)
                {
                    if (1 == buffer.ReadBits(1))
                    {
                        int dmixmod       = buffer.ReadBits(2);
                        int ltrtcmixlev   = buffer.ReadBits(3);
                        int ltrtsurmixlev = buffer.ReadBits(3);
                        int lorocmixlev   = buffer.ReadBits(3);
                        int lorosurmixlev = buffer.ReadBits(3);
                    }
                    if (1 == buffer.ReadBits(1))
                    {
                        int dsurexmod    = buffer.ReadBits(2);
                        int dheadphonmod = buffer.ReadBits(2);
                        if (dheadphonmod == 0x2)
                        {
                            // TODO
                        }
                        int adconvtyp = buffer.ReadBits(1);
                        int xbsi2     = buffer.ReadBits(8);
                        int encinfo   = buffer.ReadBits(1);
                        if (dsurexmod == 2)
                        {
                            stream.AudioMode = TSAudioMode.Extended;
                        }
                    }
                }
            }
            else
            {
                int frame_type  = buffer.ReadBits(2);
                int substreamid = buffer.ReadBits(3);
                frame_size = (buffer.ReadBits(11) + 1) << 1;

                sr_code = buffer.ReadBits(2);
                if (sr_code == 3)
                {
                    sr_code = buffer.ReadBits(2);
                }
                else
                {
                    num_blocks = buffer.ReadBits(2);
                }
                channel_mode = buffer.ReadBits(3);
                lfe_on       = buffer.ReadBits(1);
            }

            switch (channel_mode)
            {
            case 0:     // 1+1
                stream.ChannelCount = 2;
                if (stream.AudioMode == TSAudioMode.Unknown)
                {
                    stream.AudioMode = TSAudioMode.DualMono;
                }
                break;

            case 1:     // 1/0
                stream.ChannelCount = 1;
                break;

            case 2:     // 2/0
                stream.ChannelCount = 2;
                if (stream.AudioMode == TSAudioMode.Unknown)
                {
                    stream.AudioMode = TSAudioMode.Stereo;
                }
                break;

            case 3:     // 3/0
                stream.ChannelCount = 3;
                break;

            case 4:     // 2/1
                stream.ChannelCount = 3;
                break;

            case 5:     // 3/1
                stream.ChannelCount = 4;
                break;

            case 6:     // 2/2
                stream.ChannelCount = 4;
                break;

            case 7:     // 3/2
                stream.ChannelCount = 5;
                break;

            default:
                stream.ChannelCount = 0;
                break;
            }

            switch (sr_code)
            {
            case 0:
                stream.SampleRate = 48000;
                break;

            case 1:
                stream.SampleRate = 44100;
                break;

            case 2:
                stream.SampleRate = 32000;
                break;

            default:
                stream.SampleRate = 0;
                break;
            }

            if (bsid <= 10)
            {
                switch (frame_size_code >> 1)
                {
                case 18:
                    stream.BitRate = 640000;
                    break;

                case 17:
                    stream.BitRate = 576000;
                    break;

                case 16:
                    stream.BitRate = 512000;
                    break;

                case 15:
                    stream.BitRate = 448000;
                    break;

                case 14:
                    stream.BitRate = 384000;
                    break;

                case 13:
                    stream.BitRate = 320000;
                    break;

                case 12:
                    stream.BitRate = 256000;
                    break;

                case 11:
                    stream.BitRate = 224000;
                    break;

                case 10:
                    stream.BitRate = 192000;
                    break;

                case 9:
                    stream.BitRate = 160000;
                    break;

                case 8:
                    stream.BitRate = 128000;
                    break;

                case 7:
                    stream.BitRate = 112000;
                    break;

                case 6:
                    stream.BitRate = 96000;
                    break;

                case 5:
                    stream.BitRate = 80000;
                    break;

                case 4:
                    stream.BitRate = 64000;
                    break;

                case 3:
                    stream.BitRate = 56000;
                    break;

                case 2:
                    stream.BitRate = 48000;
                    break;

                case 1:
                    stream.BitRate = 40000;
                    break;

                case 0:
                    stream.BitRate = 32000;
                    break;

                default:
                    stream.BitRate = 0;
                    break;
                }
            }
            else
            {
                stream.BitRate = (long)
                                 (4.0 * frame_size * stream.SampleRate / (num_blocks * 256));
            }

            stream.LFE = lfe_on;
            if (stream.StreamType != TSStreamType.AC3_PLUS_AUDIO &&
                stream.StreamType != TSStreamType.AC3_PLUS_SECONDARY_AUDIO)
            {
                stream.DialNorm = dial_norm - 31;
            }
            stream.IsVBR         = false;
            stream.IsInitialized = true;
        }