MP3:DataFrameHeader

小站开通了:http://noteba.com/

 

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace WindowsFormsApplication1
{
     
    /// <summary>
    /// 数据帧 帧头
    /// </summary>
    public class DataFrameHeader
    {
        private static int[][] bitrates;
        private static int[][] rates;

        private byte[] _header = new byte[3];
        public byte[] Headr
        {
            get { return _header; }
        }

        /// <summary>
        /// 4 bytes
        /// </summary>
        /// <param name="stream"></param>
        public DataFrameHeader(byte[] header)
        {
            _header = header;

            if (bitrates == null || rates==null)
            {

                #region bitrates init
                bitrates = new int[15][];
                bitrates[0] = new int[] { 32, 32, 32, 32, 32, 8 };//(8)
                bitrates[1] = new int[] { 64, 48, 40, 64, 48, 16 };//(16)
                bitrates[2] = new int[] { 96, 56, 48, 96, 56, 24 };//(24)
                bitrates[3] = new int[] { 128, 64, 56, 128, 64, 32 };//(32)
                bitrates[4] = new int[] { 160, 80, 64, 160, 80, 64 };//(40)
                bitrates[5] = new int[] { 192, 96, 80, 192, 96, 80 };//(48)
                bitrates[6] = new int[] { 224, 112, 96, 224, 112, 56 };//(56)
                bitrates[7] = new int[] { 256, 128, 112, 256, 128, 64 };//(64)
                bitrates[8] = new int[] { 288, 160, 128, 288, 160, 128 };//(80)
                bitrates[9] = new int[] { 320, 192, 160, 320, 192, 160 };//(96)
                bitrates[10] = new int[] { 352, 224, 192, 352, 224, 112 };//(112)
                bitrates[11] = new int[] { 384, 256, 224, 384, 256, 128 };//(128)
                bitrates[12] = new int[] { 416, 320, 256, 416, 320, 256 };//(144)
                bitrates[13] = new int[] { 448, 384, 320, 448, 384, 320 };//(160)
                #endregion

                #region rates init
                rates = new int[3][];
                rates[0] = new int[] { 44100, 22050, 11025 };
                rates[1] = new int[] { 48000, 24000, 12000 };
                rates[2] = new int[] { 32000, 16000, 8000 };
                #endregion
            }

                       
            #region get _version
            int ret = _header[1] & 0x18;//0001 1000
            if (ret == 0x18)
                _version = "1";
            else if (ret == 0x10)
                _version = "2";
            else if (ret == 0)
                _version = "2.5";//2.5
            else
                _version = "0";
            #endregion

            #region get _layer
            ret = _header[1] & 0x06;// 0000 0110
            if (ret == 0x06)
                _layer = "1";
            else if (ret == 0x04)
                _layer = "2";
            else if (ret == 0x02)
                _layer = "3";
            else if (ret == 0)
                _layer = "0";
            else _layer = "0";
            #endregion

            #region get _crc
            _crc = (_header[1] & 0x01) == 0;// 0000 0001
            #endregion

            #region get _bitrate
            int idx = 0;
            ret = _header[2] & 0Xf0;// 1111 0000
            ret = ret >> 4;

            if (ret == 0)
                _bitrate = 0;//free index
            else if (ret == 0xf0)
                _bitrate = -1;//bad index
            else
            {              
                ret--;
                // ret -> Version*Layer
                idx = int.Parse(Version=="2.5"?"2":Version) * int.Parse(Layer);
                idx--;
                _bitrate = bitrates[ret][idx];
            }
            #endregion

            #region get _rate
            idx = -1;
            ret = _header[2] & 0x0c;//0000 1100
            if (ret == 0x08)
                idx = 2;
            else if (ret == 0x04)
                idx = 1;
            else if (ret == 0)
                idx = 0;

            if (ret == 0x0c)
                _rate = 0;
            else
            {
                int v = int.Parse(Version == "2.5" ? "3" : Version);              
                v--;
                _rate = rates[idx][(int)v];
            }
            #endregion

            #region get _paddingBit
            _paddingBit =((_header[2] & 0x02) == 0);//0000 0010
            #endregion

            #region get _fetchSize
            if (_version != "1")
            {
                _fetchSize = (_layer == "1" ? 24 : 72);
            }
            else {
                _fetchSize = (_layer == "1" ? 48 : 144);
            }
            #endregion

            #region get _frameTime
            _frameTime = _fetchSize * 1000 * 8 / Rate;
            #endregion

            #region get _size
            //_size = ( (_fetchSize  *  _bitrate * 1000 / 8) / _rate) + (_paddingBit ? 1 : 0) * (_layer == "1" ? 4 : 1);
            _size = (_fetchSize * _bitrate*1000) / (_rate) + (_paddingBit ? 1 : 0) * (_layer == "1" ? 4 : 1);
            #endregion
        }


        private int _fetchSize;
        /// <summary>
        /// 每帧采样数
        /// </summary>
        public int FetchSize
        {
            get { return _fetchSize; }
        }


        private int _frameTime;
        /// <summary>
        /// 毫秒(MS)
        /// </summary>
        public int FrameTime
        {
            get { return _frameTime; }
        }


        private string _version;
        /// <summary>
        /// Version 19~20 bit :B
        /// AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
        /// </summary>
        public string Version
        {
            get { return _version; }
        }

        private string _layer;
        /// <summary>
        /// Layer 17~18 bit:C
        /// AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
        /// </summary>
        public string Layer
        {
            get { return _layer; }
        }


        private bool _crc;
        /// <summary>
        /// 是否包含  2 byte 的 CRC 取决于 header 的第16bit: D
        /// AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
        /// </summary>
        public bool CRC
        {
            get{ return _crc; }
        }


        private int _bitrate;
        /// <summary>
        /// 12~15 bit :E
        /// AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
        /// </summary>
        public int Bitrate
        {
            get { return _bitrate; }
        }


        private int _rate;
        /// <summary>
        /// 10 ~ 11 bit:F
        /// AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
        /// </summary>
        public int Rate
        {
            get { return _rate; }
        }

        private bool _paddingBit;
        /// <summary>
        /// 9 bit,:G
        /// AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
        /// </summary>
        public bool PaddingBit
        {
            get { return _paddingBit; }
        }

        private int _size;
        /// <summary>
        /// 4 byte; 帧内容大小,不包括帧头
        /// 计算时 取4 byte 的 8位 得到 一个 32位 的2进制数 就是 帧内容大小
        /// </summary>
        public int Size
        {
            get { return _size; }
        }

    }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值