Ejemplo n.º 1
0
        public void WriteChunk(byte[] chunk, uint timeStamp)
        {
            if (chunk.Length < 1)
            {
                return;
            }

            if (chunk[0] == 0)
            {             // Header
                if (chunk.Length < 3)
                {
                    return;
                }

                var bits = (ulong)BitConverterBE.ToUInt16(chunk, 1) << 48;

                _aacProfile      = BitHelper.Read(ref bits, 5) - 1;
                _sampleRateIndex = BitHelper.Read(ref bits, 4);
                _channelConfig   = BitHelper.Read(ref bits, 4);

                if (_aacProfile == 4)                 // HE-AAC
                {
                    _aacProfile = 1;                  // Uses LC profile + SBR
                }
                if (_aacProfile < 0 || _aacProfile > 3)
                {
                    throw new Exception("Unsupported AAC profile.");
                }
                if (_sampleRateIndex > 12)
                {
                    throw new Exception("Invalid AAC sample rate index.");
                }
                if (_channelConfig > 6)
                {
                    throw new Exception("Invalid AAC channel configuration.");
                }
            }
            else
            {             // Audio data
                var   dataSize = chunk.Length - 1;
                ulong bits     = 0;

                // Reference: WriteADTSHeader from FAAC's bitstream.c

                BitHelper.Write(ref bits, 12, 0xFFF);
                BitHelper.Write(ref bits, 1, 0);
                BitHelper.Write(ref bits, 2, 0);
                BitHelper.Write(ref bits, 1, 1);
                BitHelper.Write(ref bits, 2, _aacProfile);
                BitHelper.Write(ref bits, 4, _sampleRateIndex);
                BitHelper.Write(ref bits, 1, 0);
                BitHelper.Write(ref bits, 3, _channelConfig);
                BitHelper.Write(ref bits, 1, 0);
                BitHelper.Write(ref bits, 1, 0);
                BitHelper.Write(ref bits, 1, 0);
                BitHelper.Write(ref bits, 1, 0);
                BitHelper.Write(ref bits, 13, 7 + dataSize);
                BitHelper.Write(ref bits, 11, 0x7FF);
                BitHelper.Write(ref bits, 2, 0);

                _fs.Write(BitConverterBE.GetBytes(bits), 1, 7);
                _fs.Write(chunk, 1, dataSize);
            }
        }
Ejemplo n.º 2
0
        private void GetFrameSize(byte[] chunk)
        {
            if (_codecID == 2)
            {
                // Reference: flv_h263_decode_picture_header from libavcodec's h263.c

                if (chunk.Length < 10)
                {
                    return;
                }

                if (chunk[0] != 0 || chunk[1] != 0)
                {
                    return;
                }

                var x = BitConverterBE.ToUInt64(chunk, 2);
                int format;

                if (BitHelper.Read(ref x, 1) != 1)
                {
                    return;
                }
                BitHelper.Read(ref x, 5);
                BitHelper.Read(ref x, 8);

                format = BitHelper.Read(ref x, 3);
                switch (format)
                {
                case 0:
                    _width  = BitHelper.Read(ref x, 8);
                    _height = BitHelper.Read(ref x, 8);
                    break;

                case 1:
                    _width  = BitHelper.Read(ref x, 16);
                    _height = BitHelper.Read(ref x, 16);
                    break;

                case 2:
                    _width  = 352;
                    _height = 288;
                    break;

                case 3:
                    _width  = 176;
                    _height = 144;
                    break;

                case 4:
                    _width  = 128;
                    _height = 96;
                    break;

                case 5:
                    _width  = 320;
                    _height = 240;
                    break;

                case 6:
                    _width  = 160;
                    _height = 120;
                    break;

                default:
                    return;
                }
            }
            else if (_codecID == 4 || _codecID == 5)
            {
                // Reference: vp6_parse_header from libavcodec's vp6.c

                var skip = _codecID == 4 ? 1 : 4;
                if (chunk.Length < skip + 8)
                {
                    return;
                }
                var x = BitConverterBE.ToUInt64(chunk, skip);

                var deltaFrameFlag     = BitHelper.Read(ref x, 1);
                var quant              = BitHelper.Read(ref x, 6);
                var separatedCoeffFlag = BitHelper.Read(ref x, 1);
                var subVersion         = BitHelper.Read(ref x, 5);
                var filterHeader       = BitHelper.Read(ref x, 2);
                var interlacedFlag     = BitHelper.Read(ref x, 1);

                if (deltaFrameFlag != 0)
                {
                    return;
                }
                if (separatedCoeffFlag != 0 || filterHeader == 0)
                {
                    BitHelper.Read(ref x, 16);
                }

                _height = BitHelper.Read(ref x, 8) * 16;
                _width  = BitHelper.Read(ref x, 8) * 16;

                // chunk[0] contains the width and height (4 bits each, respectively) that should
                // be cropped off during playback, which will be non-zero if the encoder padded
                // the frames to a macroblock boundary.  But if you use this adjusted size in the
                // AVI header, DirectShow seems to ignore it, and it can cause stride or chroma
                // alignment problems with VFW if the width/height aren't multiples of 4.
                if (!_isAlphaWriter)
                {
                    var cropX = chunk[0] >> 4;
                    var cropY = chunk[0] & 0x0F;
                    if ((cropX != 0 || cropY != 0) && !_isAlphaWriter)
                    {
                        _warnings.Add(String.Format("Suggested cropping: {0} pixels from right, {1} pixels from bottom.", cropX, cropY));
                    }
                }
            }
        }