Exemplo n.º 1
0
        unsafe void decode_subframes(BitReader bitreader, FlacFrame frame)
        {
            fixed(int *r = residualBuffer, s = samplesBuffer)
            for (int ch = 0; ch < PCM.ChannelCount; ch++)
            {
                // subframe header
                uint t1 = bitreader.readbit();                 // ?????? == 0

                if (t1 != 0)
                {
                    throw new Exception("unsupported subframe coding (ch == " + ch.ToString() + ")");
                }
                int type_code = (int)bitreader.readbits(6);

                frame.subframes[ch].wbits = (int)bitreader.readbit();
                if (frame.subframes[ch].wbits != 0)
                {
                    frame.subframes[ch].wbits += (int)bitreader.read_unary();
                }

                frame.subframes[ch].obits = PCM.BitsPerSample - frame.subframes[ch].wbits;
                switch (frame.ch_mode)
                {
                case ChannelMode.MidSide: frame.subframes[ch].obits += ch; break;

                case ChannelMode.LeftSide: frame.subframes[ch].obits += ch; break;

                case ChannelMode.RightSide: frame.subframes[ch].obits += 1 - ch; break;
                }

                frame.subframes[ch].best.type  = (SubframeType)type_code;
                frame.subframes[ch].best.order = 0;

                if ((type_code & (uint)SubframeType.LPC) != 0)
                {
                    frame.subframes[ch].best.order = (type_code - (int)SubframeType.LPC) + 1;
                    frame.subframes[ch].best.type  = SubframeType.LPC;
                }
                else if ((type_code & (uint)SubframeType.Fixed) != 0)
                {
                    frame.subframes[ch].best.order = (type_code - (int)SubframeType.Fixed);
                    frame.subframes[ch].best.type  = SubframeType.Fixed;
                }

                frame.subframes[ch].best.residual = r + ch * Flake.MAX_BLOCKSIZE;
                frame.subframes[ch].samples       = s + ch * Flake.MAX_BLOCKSIZE;

                // subframe
                switch (frame.subframes[ch].best.type)
                {
                case SubframeType.Constant:
                    decode_subframe_constant(bitreader, frame, ch);
                    break;

                case SubframeType.Verbatim:
                    decode_subframe_verbatim(bitreader, frame, ch);
                    break;

                case SubframeType.Fixed:
                    decode_subframe_fixed(bitreader, frame, ch);
                    break;

                case SubframeType.LPC:
                    decode_subframe_lpc(bitreader, frame, ch);
                    break;

                default:
                    throw new Exception("invalid subframe type");
                }
            }
        }
Exemplo n.º 2
0
        unsafe void decode_metadata()
        {
            byte x;
            int  i, id;

            //bool first = true;
            byte[] FLAC__STREAM_SYNC_STRING = new byte[] { (byte)'f', (byte)'L', (byte)'a', (byte)'C' };
            byte[] ID3V2_TAG_ = new byte[] { (byte)'I', (byte)'D', (byte)'3' };

            for (i = id = 0; i < 4;)
            {
                if (_IO.Read(_framesBuffer, 0, 1) == 0)
                {
                    throw new Exception("FLAC stream not found");
                }
                x = _framesBuffer[0];
                if (x == FLAC__STREAM_SYNC_STRING[i])
                {
                    //first = true;
                    i++;
                    id = 0;
                    continue;
                }
                if (id < 3 && x == ID3V2_TAG_[id])
                {
                    id++;
                    i = 0;
                    if (id == 3)
                    {
                        if (!skip_bytes(3))
                        {
                            throw new Exception("FLAC stream not found");
                        }
                        int skip = 0;
                        for (int j = 0; j < 4; j++)
                        {
                            if (0 == _IO.Read(_framesBuffer, 0, 1))
                            {
                                throw new Exception("FLAC stream not found");
                            }
                            skip <<= 7;
                            skip  |= ((int)_framesBuffer[0] & 0x7f);
                        }
                        if (!skip_bytes(skip))
                        {
                            throw new Exception("FLAC stream not found");
                        }
                    }
                    continue;
                }
                id = 0;
                if (x == 0xff)                 /* MAGIC NUMBER for the first 8 frame sync bits */
                {
                    do
                    {
                        if (_IO.Read(_framesBuffer, 0, 1) == 0)
                        {
                            throw new Exception("FLAC stream not found");
                        }
                        x = _framesBuffer[0];
                    } while (x == 0xff);
                    if (x >> 2 == 0x3e)                     /* MAGIC NUMBER for the last 6 sync bits */
                    {
                        //_IO.Position -= 2;
                        // state = frame
                        throw new Exception("headerless file unsupported");
                    }
                }
                throw new Exception("FLAC stream not found");
            }

            do
            {
                fill_frames_buffer();
                fixed(byte *buf = _framesBuffer)
                {
                    BitReader    bitreader = new BitReader(buf, _framesBufferOffset, _framesBufferLength - _framesBufferOffset);
                    bool         is_last   = bitreader.readbit() != 0;
                    MetadataType type      = (MetadataType)bitreader.readbits(7);
                    int          len       = (int)bitreader.readbits(24);

                    if (type == MetadataType.StreamInfo)
                    {
                        const int FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN  = 16;                 /* bits */
                        const int FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN  = 16;                 /* bits */
                        const int FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN  = 24;                 /* bits */
                        const int FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN  = 24;                 /* bits */
                        const int FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN     = 20;                 /* bits */
                        const int FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN        = 3;                  /* bits */
                        const int FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN = 5;                  /* bits */
                        const int FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN   = 36;                 /* bits */
                        const int FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN          = 128;                /* bits */

                        min_block_size = bitreader.readbits(FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN);
                        max_block_size = bitreader.readbits(FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN);
                        min_frame_size = bitreader.readbits(FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
                        max_frame_size = bitreader.readbits(FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
                        int sample_rate     = (int)bitreader.readbits(FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN);
                        int channels        = 1 + (int)bitreader.readbits(FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN);
                        int bits_per_sample = 1 + (int)bitreader.readbits(FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN);
                        pcm          = new AudioPCMConfig(bits_per_sample, channels, sample_rate);
                        _sampleCount = (long)bitreader.readbits64(FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
                        bitreader.skipbits(FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN);
                    }
                    else if (type == MetadataType.Seektable)
                    {
                        int num_entries = len / 18;
                        seek_table = new SeekPoint[num_entries];
                        for (int e = 0; e < num_entries; e++)
                        {
                            seek_table[e].number    = bitreader.read_long();
                            seek_table[e].offset    = bitreader.read_long();
                            seek_table[e].framesize = (int)bitreader.read_ushort();
                        }
                    }
                    if (_framesBufferLength < 4 + len)
                    {
                        _IO.Position       += 4 + len - _framesBufferLength;
                        _framesBufferLength = 0;
                    }
                    else
                    {
                        _framesBufferLength -= 4 + len;
                        _framesBufferOffset += 4 + len;
                    }
                    if (is_last)
                    {
                        break;
                    }
                }
            } while (true);
            first_frame_offset = _IO.Position - _framesBufferLength;
        }
Exemplo n.º 3
0
        unsafe void decode_frame_header(BitReader bitreader, FlacFrame frame)
        {
            int header_start = bitreader.Position;

            if (bitreader.readbits(15) != 0x7FFC)
            {
                throw new Exception("invalid frame");
            }
            uint vbs = bitreader.readbit();

            frame.bs_code0 = (int)bitreader.readbits(4);
            uint sr_code0 = bitreader.readbits(4);

            frame.ch_mode = (ChannelMode)bitreader.readbits(4);
            uint bps_code = bitreader.readbits(3);

            if (Flake.flac_bitdepths[bps_code] != PCM.BitsPerSample)
            {
                throw new Exception("unsupported bps coding");
            }
            uint t1 = bitreader.readbit();             // == 0?????

            if (t1 != 0)
            {
                throw new Exception("unsupported frame coding");
            }
            frame.frame_number = (int)bitreader.read_utf8();

            // custom block size
            if (frame.bs_code0 == 6)
            {
                frame.bs_code1  = (int)bitreader.readbits(8);
                frame.blocksize = frame.bs_code1 + 1;
            }
            else if (frame.bs_code0 == 7)
            {
                frame.bs_code1  = (int)bitreader.readbits(16);
                frame.blocksize = frame.bs_code1 + 1;
            }
            else
            {
                frame.blocksize = Flake.flac_blocksizes[frame.bs_code0];
            }

            // custom sample rate
            if (sr_code0 < 1 || sr_code0 > 11)
            {
                // sr_code0 == 12 -> sr == bitreader.readbits(8) * 1000;
                // sr_code0 == 13 -> sr == bitreader.readbits(16);
                // sr_code0 == 14 -> sr == bitreader.readbits(16) * 10;
                throw new Exception("invalid sample rate mode");
            }

            int frame_channels = (int)frame.ch_mode + 1;

            if (frame_channels > 11)
            {
                throw new Exception("invalid channel mode");
            }
            if (frame_channels == 2 || frame_channels > 8)             // Mid/Left/Right Side Stereo
            {
                frame_channels = 2;
            }
            else
            {
                frame.ch_mode = ChannelMode.NotStereo;
            }
            if (frame_channels != PCM.ChannelCount)
            {
                throw new Exception("invalid channel mode");
            }

            // CRC-8 of frame header
            byte crc = do_crc ? crc8.ComputeChecksum(bitreader.Buffer, header_start, bitreader.Position - header_start) : (byte)0;

            frame.crc8 = (byte)bitreader.readbits(8);
            if (do_crc && frame.crc8 != crc)
            {
                throw new Exception("header crc mismatch");
            }
        }