示例#1
0
        bool DecodeSoundPCM8(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            uint nSampleCount = datahdr.SampleCount;

            if (nSampleCount > m_nBufLength)
            {
                m_ptrBuffer3 = new sbyte [nSampleCount * m_mioih.ChannelCount];
                m_nBufLength = nSampleCount;
            }
            if (0 != (datahdr.Flags & MIO_LEAD_BLOCK))
            {
                (context as HuffmanDecodeContext).PrepareToDecodeERINACode();
            }
            uint nBytes = nSampleCount * (uint)m_mioih.ChannelCount;

            if (context.DecodeBytes(m_ptrBuffer3, nBytes) < nBytes)
            {
                return(false);
            }
            int ptrSrcBuf = 0; // (PBYTE) m_ptrBuffer3;
            int nStep     = m_mioih.ChannelCount;

            for (int i = 0; i < m_mioih.ChannelCount; i++)
            {
                int   ptrDstBuf = wave_pos + i;
                sbyte bytValue  = 0;
                for (uint j = 0; j < nSampleCount; j++)
                {
                    bytValue += m_ptrBuffer3[ptrSrcBuf++];
                    ptrWaveBuf[ptrDstBuf] = (byte)bytValue;
                    ptrDstBuf            += nStep;
                }
            }
            return(true);
        }
示例#2
0
        public bool DecodeSound(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            context.FlushBuffer();

            if (m_mioih.Transformation == CvType.Lossless_ERI)
            {
                if (m_mioih.BitsPerSample == 8)
                {
                    return(DecodeSoundPCM8(context, datahdr, ptrWaveBuf, wave_pos));
                }
                else if (m_mioih.BitsPerSample == 16)
                {
                    return(DecodeSoundPCM16(context, datahdr, ptrWaveBuf, wave_pos));
                }
            }
            else if ((m_mioih.Transformation == CvType.LOT_ERI) ||
                     (m_mioih.Transformation == CvType.LOT_ERI_MSS))
            {
                if ((m_mioih.ChannelCount != 2) || (m_mioih.Transformation == CvType.LOT_ERI))
                {
                    return(DecodeSoundDCT(context, datahdr, ptrWaveBuf, wave_pos));
                }
                else
                {
                    return(DecodeSoundDCT_MSS(context, datahdr, ptrWaveBuf, wave_pos));
                }
            }
            return(false);
        }
示例#3
0
        public bool DecodeSound(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            context.FlushBuffer();

            if (m_mioih.Transformation == CvType.Lossless_ERI)
            {
                if (m_mioih.BitsPerSample == 8)
                {
                    return DecodeSoundPCM8 (context, datahdr, ptrWaveBuf, wave_pos);
                }
                else if (m_mioih.BitsPerSample == 16)
                {
                    return DecodeSoundPCM16 (context, datahdr, ptrWaveBuf, wave_pos);
                }
            }
            else if ((m_mioih.Transformation == CvType.LOT_ERI)
                     || (m_mioih.Transformation == CvType.LOT_ERI_MSS))
            {
                if ((m_mioih.ChannelCount != 2) || (m_mioih.Transformation == CvType.LOT_ERI))
                {
                    return DecodeSoundDCT (context, datahdr, ptrWaveBuf, wave_pos);
                }
                else
                {
                    return DecodeSoundDCT_MSS (context, datahdr, ptrWaveBuf, wave_pos);
                }
            }
            return false;
        }
示例#4
0
 bool DecodeSoundPCM8(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
 {
     uint nSampleCount = datahdr.SampleCount;
     if (nSampleCount > m_nBufLength)
     {
         m_ptrBuffer3 = new sbyte [nSampleCount * m_mioih.ChannelCount];
         m_nBufLength = nSampleCount;
     }
     if (0 != (datahdr.Flags & MIO_LEAD_BLOCK))
     {
         (context as HuffmanDecodeContext).PrepareToDecodeERINACode();
     }
     uint nBytes = nSampleCount * (uint)m_mioih.ChannelCount;
     if (context.DecodeBytes (m_ptrBuffer3, nBytes) < nBytes)
     {
         return false;
     }
     int ptrSrcBuf = 0; // (PBYTE) m_ptrBuffer3;
     int nStep = m_mioih.ChannelCount;
     for (int i = 0; i < m_mioih.ChannelCount; i ++ )
     {
         int ptrDstBuf = wave_pos + i;
         sbyte bytValue = 0;
         for (uint j = 0; j < nSampleCount; j++)
         {
             bytValue += m_ptrBuffer3[ptrSrcBuf++];
             ptrWaveBuf[ptrDstBuf] = (byte)bytValue;
             ptrDstBuf += nStep;
         }
     }
     return true;
 }
示例#5
0
        bool DecodeSoundPCM16(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            uint nSampleCount = datahdr.SampleCount;
            uint nChannelCount = (uint)m_mioih.ChannelCount;
            uint nAllSampleCount = nSampleCount * nChannelCount;
            uint nBytes = nAllSampleCount * sizeof(short);

            if (ptrWaveBuf.Length < wave_pos + (int)nBytes)
                return false;

            if (nSampleCount > m_nBufLength)
            {
                m_ptrBuffer3 = new sbyte[nBytes];
                m_ptrBuffer4 = new byte[nBytes];
                m_nBufLength = nSampleCount;
            }
            if (0 != (datahdr.Flags & MIO_LEAD_BLOCK))
            {
                (context as HuffmanDecodeContext).PrepareToDecodeERINACode();
            }
            if (context.DecodeBytes (m_ptrBuffer3, nBytes) < nBytes)
            {
                return false;
            }
            int pbytSrcBuf1, pbytSrcBuf2, pbytDstBuf;
            for (int i = 0; i < m_mioih.ChannelCount; i++)
            {
                int nOffset = i * (int)nSampleCount * sizeof(short);
                pbytSrcBuf1 = nOffset; // ((PBYTE) m_ptrBuffer3) + nOffset;
                pbytSrcBuf2 = pbytSrcBuf1 + (int)nSampleCount; // pbytSrcBuf1 + nSampleCount;
                pbytDstBuf = nOffset; // ((PBYTE) m_ptrBuffer4) + nOffset;

                for (uint j = 0; j < nSampleCount; j ++)
                {
                    sbyte bytLow  = m_ptrBuffer3[pbytSrcBuf2 + j];
                    sbyte bytHigh = m_ptrBuffer3[pbytSrcBuf1 + j];
                    m_ptrBuffer4[pbytDstBuf + j * sizeof(short) + 0] = (byte)bytLow;
                    m_ptrBuffer4[pbytDstBuf + j * sizeof(short) + 1] = (byte)(bytHigh ^ (bytLow >> 7));
                }
            }
            if (m_ptrBuffer4.Length < nBytes)
                return false;
            unsafe
            {
                fixed (byte* rawBuffer4 = m_ptrBuffer4, rawWaveBuf = &ptrWaveBuf[wave_pos])
                {
                    int nStep = m_mioih.ChannelCount;
                    short* ptrSrcBuf = (short*)rawBuffer4;
                    for (int i = 0; i < m_mioih.ChannelCount; i++)
                    {
                        short* ptrDstBuf = (short*)rawWaveBuf + i; // (SWORD*) ptrWaveBuf;
                        short wValue = 0;
                        short wDelta = 0;
                        for (uint j = 0; j < nSampleCount; j++)
                        {
                            wDelta += *ptrSrcBuf++;
                            wValue += wDelta;
                            *ptrDstBuf = wValue;
                            ptrDstBuf += nStep;
                        }
                    }
                }
            }
            return true;
        }
示例#6
0
        bool DecodeSoundDCT_MSS(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            uint nDegreeWidth = 1u << m_mioih.SubbandDegree;
            uint nSampleCount = (datahdr.SampleCount + nDegreeWidth - 1) & ~(nDegreeWidth - 1);
            uint nSubbandCount = (nSampleCount >> m_mioih.SubbandDegree);
            uint nChannelCount = (uint)m_mioih.ChannelCount;
            uint nAllSampleCount = nSampleCount * nChannelCount;
            uint nAllSubbandCount = nSubbandCount;

            if (nSampleCount > m_nBufLength)
            {
                m_ptrBuffer2 = new int[nAllSampleCount];
                m_ptrBuffer3 = new sbyte[nAllSampleCount * sizeof(short)];
                m_ptrDivisionTable = new byte[nAllSubbandCount];
                m_ptrRevolveCode = new byte[nAllSubbandCount * 10];
                m_ptrWeightCode = new int[nAllSubbandCount * 10];
                m_ptrCoefficient = new int[nAllSubbandCount * 10];
                m_nBufLength = nSampleCount;
            }
            if (context.GetABit() != 0)
            {
                return false;
            }

            int nLastDivision = -1;
            m_ptrNextDivision = 0; // within m_ptrDivisionTable;
            m_ptrNextRevCode = 0; // within m_ptrRevolveCode;
            m_ptrNextWeight = 0; // within m_ptrWeightCode;
            m_ptrNextCoefficient = 0; // within m_ptrCoefficient;

            uint i, j, k;
            for (i = 0; i < nSubbandCount; i ++)
            {
                int nDivisionCode = (int)context.GetNBits (2);
                m_ptrDivisionTable[m_ptrNextDivision++] = (byte)nDivisionCode;

                bool fLeadBlock = false;
                if (nDivisionCode != nLastDivision)
                {
                    if (i != 0)
                    {
                        m_ptrRevolveCode[m_ptrNextRevCode++] = (byte)context.GetNBits (2);
                        m_ptrWeightCode[m_ptrNextWeight++] = (int)context.GetNBits (32);
                        m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits (16);
                    }
                    fLeadBlock = true;
                    nLastDivision = nDivisionCode;
                }
                uint nDivisionCount = 1u << nDivisionCode;
                for (k = 0; k < nDivisionCount; k++)
                {
                    if (fLeadBlock)
                    {
                        m_ptrRevolveCode[m_ptrNextRevCode++] = (byte)context.GetNBits (2);
                        fLeadBlock = false;
                    }
                    else
                    {
                        m_ptrRevolveCode[m_ptrNextRevCode++] = (byte)context.GetNBits (4);
                    }
                    m_ptrWeightCode[m_ptrNextWeight++] = (int)context.GetNBits (32);
                    m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits (16);
                }
            }
            if (nSubbandCount > 0)
            {
                m_ptrRevolveCode[m_ptrNextRevCode++] = (byte)context.GetNBits (2);
                m_ptrWeightCode[m_ptrNextWeight++] = (int)context.GetNBits (32);
                m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits (16);
            }
            if (context.GetABit() != 0)
            {
                return false;
            }
            if (0 != (datahdr.Flags & MIO_LEAD_BLOCK))
            {
                if (m_mioih.Architecture != EriCode.Nemesis)
                {
                    (context as HuffmanDecodeContext).PrepareToDecodeERINACode();
                }
                else
                {
                    throw new NotImplementedException ("Nemesis encoding not implemented");
            //                    context.PrepareToDecodeERISACode( );
                }
            }
            else if (m_mioih.Architecture == EriCode.Nemesis)
            {
                throw new NotImplementedException ("Nemesis encoding not implemented");
            //                context.InitializeERISACode( );
            }
            if (m_mioih.Architecture != EriCode.Nemesis)
            {
                if (context.DecodeBytes (m_ptrBuffer3, nAllSampleCount * 2) < nAllSampleCount * 2)
                {
                    return false;
                }
                int ptrHBuf = 0; // within m_ptrBuffer3;
                int ptrLBuf = (int)nAllSampleCount; // within m_ptrBuffer3

                for (i = 0; i < nDegreeWidth * 2; i++)
                {
                    int ptrQuantumized = (int)i; // within (PINT) m_ptrBuffer2
                    for (j = 0; j < nAllSubbandCount; j++)
                    {
                        int nLow  = m_ptrBuffer3[ptrLBuf++];
                        int nHigh = m_ptrBuffer3[ptrHBuf++] ^ (nLow >> 8);
                        m_ptrBuffer2[ptrQuantumized] = (nLow & 0xFF) | (nHigh << 8);
                        ptrQuantumized += (int)nDegreeWidth * 2;
                    }
                }
            }
            else
            {
                throw new NotImplementedException ("Nemesis encoding not implemented");
                /*
                if ( context.DecodeERISACodeWords
                        ( (SWORD*) m_ptrBuffer3, nAllSampleCount ) < nAllSampleCount )
                {
                    return false;
                }
                for ( i = 0; i < nAllSampleCount; i ++ )
                {
                    ((PINT)m_ptrBuffer2)[i] = ((SWORD*)m_ptrBuffer3)[i];
                }
                */
            }
            uint nSamples;
            uint nRestSamples = datahdr.SampleCount;
            //            int ptrDstBuf = wave_pos; // within (SWORD*) ptrWaveBuf;

            nLastDivision = -1;
            m_ptrNextDivision = 0; // m_ptrDivisionTable;
            m_ptrNextRevCode = 0; // m_ptrRevolveCode;
            m_ptrNextWeight = 0; // m_ptrWeightCode;
            m_ptrNextCoefficient = 0; // m_ptrCoefficient;
            m_ptrNextSource = 0; // (PINT) m_ptrBuffer2;

            for (i = 0; i < nSubbandCount; i++)
            {
                int nDivisionCode = m_ptrDivisionTable[m_ptrNextDivision++];
                uint nDivisionCount = 1u << nDivisionCode;

                bool fLeadBlock = false;
                if (nLastDivision != nDivisionCode)
                {
                    if (i != 0)
                    {
                        nSamples = Math.Min (nRestSamples, (uint)m_nDegreeNum);
                        DecodePostBlock_MSS (ptrWaveBuf, wave_pos, nSamples);
                        nRestSamples -= nSamples;
                        wave_pos += (int)(nSamples * nChannelCount * sizeof(short));
                    }
                    InitializeWithDegree (m_mioih.SubbandDegree - nDivisionCode);
                    nLastDivision = nDivisionCode;
                    fLeadBlock = true;
                }
                for (k = 0; k < nDivisionCount; k++)
                {
                    if (fLeadBlock)
                    {
                        DecodeLeadBlock_MSS();
                        fLeadBlock = false;
                    }
                    else
                    {
                        nSamples = nRestSamples;
                        if (nSamples > m_nDegreeNum)
                        {
                            nSamples = (uint)m_nDegreeNum;
                        }
                        DecodeInternalBlock_MSS (ptrWaveBuf, wave_pos, nSamples);
                        nRestSamples -= nSamples;
                        wave_pos += (int)(nSamples * nChannelCount * sizeof(short));
                    }
                }
            }
            if (nSubbandCount > 0)
            {
                nSamples = nRestSamples;
                if (nSamples > m_nDegreeNum)
                {
                    nSamples = (uint)m_nDegreeNum;
                }
                DecodePostBlock_MSS (ptrWaveBuf, wave_pos, nSamples);
                nRestSamples -= nSamples;
                wave_pos += (int)(nSamples * nChannelCount) * sizeof(short);
            }
            return true;
        }
示例#7
0
        bool DecodeSoundDCT(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            uint i, j, k;
            uint nDegreeWidth = 1u << m_mioih.SubbandDegree;
            uint nSampleCount = (datahdr.SampleCount + nDegreeWidth - 1) & ~(nDegreeWidth - 1);
            uint nSubbandCount = (nSampleCount >> m_mioih.SubbandDegree);
            uint nChannelCount = (uint)m_mioih.ChannelCount;
            uint nAllSampleCount = nSampleCount * nChannelCount;
            uint nAllSubbandCount = nSubbandCount * nChannelCount;

            if (nSampleCount > m_nBufLength)
            {
                m_ptrBuffer2 = new int[nAllSampleCount];
                m_ptrBuffer3 = new sbyte[nAllSampleCount * sizeof(short)];
                m_ptrDivisionTable = new byte[nAllSubbandCount];
                m_ptrWeightCode = new int[nAllSubbandCount * 5];
                m_ptrCoefficient = new int[nAllSubbandCount * 5];
                m_nBufLength = nSampleCount;
            }
            if (context.GetABit() != 0)
            {
                return  false;
            }
            int[] pLastDivision = new int [nChannelCount];
            m_ptrNextDivision = 0; // within m_ptrDivisionTable;
            m_ptrNextWeight = 0; // within m_ptrWeightCode;
            m_ptrNextCoefficient = 0; // within m_ptrCoefficient;

            for (i = 0; i < nChannelCount; i++)
            {
                pLastDivision[i] = -1;
            }
            for (i = 0; i < nSubbandCount; i++)
            {
                for (j = 0; j < nChannelCount; j++)
                {
                    int nDivisionCode = (int)context.GetNBits(2);
                    m_ptrDivisionTable[m_ptrNextDivision++] = (byte)nDivisionCode;

                    if (nDivisionCode != pLastDivision[j])
                    {
                        if (i != 0)
                        {
                            m_ptrWeightCode[m_ptrNextWeight++] = (int)context.GetNBits (32);
                            m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits (16);
                        }
                        pLastDivision[j] = nDivisionCode;
                    }

                    uint nDivisionCount = 1u << nDivisionCode;
                    for (k = 0; k < nDivisionCount; k ++)
                    {
                        m_ptrWeightCode[m_ptrNextWeight++] = (int)context.GetNBits (32);
                        m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits (16);
                    }
                }
            }
            if (nSubbandCount > 0)
            {
                for (i = 0; i < nChannelCount; i++)
                {
                    m_ptrWeightCode[m_ptrNextWeight++] = (int)context.GetNBits (32);
                    m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits (16);
                }
            }

            if (context.GetABit() != 0)
            {
                return  false;
            }
            if (0 != (datahdr.Flags & MIO_LEAD_BLOCK))
            {
                if (m_mioih.Architecture != EriCode.Nemesis)
                {
                    (context as HuffmanDecodeContext).PrepareToDecodeERINACode();
                }
                else
                {
                    throw new NotImplementedException ("Nemesis encoding not implemented");
            //                    context.PrepareToDecodeERISACode();
                }
            }
            else if (m_mioih.Architecture == EriCode.Nemesis)
            {
                throw new NotImplementedException ("Nemesis encoding not implemented");
            //                context.InitializeERISACode();
            }
            if (m_mioih.Architecture != EriCode.Nemesis)
            {
                if (context.DecodeBytes (m_ptrBuffer3, nAllSampleCount * 2 ) < nAllSampleCount * 2)
                {
                    return false;
                }
                int ptrHBuf = 0; // within m_ptrBuffer3;
                int ptrLBuf = (int)nAllSampleCount; // within m_ptrBuffer3

                for (i = 0; i < nDegreeWidth; i++)
                {
                    int ptrQuantumized = (int)i; // within (PINT) m_ptrBuffer2
                    for (j = 0; j < nAllSubbandCount; j++)
                    {
                        int nLow  = m_ptrBuffer3[ptrLBuf++];
                        int nHigh = m_ptrBuffer3[ptrHBuf++] ^ (nLow >> 8);
                        m_ptrBuffer2[ptrQuantumized] = (nLow & 0xFF) | (nHigh << 8);
                        ptrQuantumized += (int)nDegreeWidth;
                    }
                }
            }
            else
            {
                throw new NotImplementedException ("Nemesis encoding not implemented");
                /*
                if (context.DecodeERISACodeWords (m_ptrBuffer3, nAllSampleCount) < nAllSampleCount)
                {
                    return false;
                }
                for (i = 0; i < nAllSampleCount; i++)
                {
                    ((PINT)m_ptrBuffer2)[i] = ((SWORD*)m_ptrBuffer3)[i];
                }
                */
            }
            uint nSamples;
            uint[] pRestSamples = new uint [nChannelCount];
            int[] ptrDstBuf = new int [nChannelCount]; // indices within ptrWaveBuf

            m_ptrNextDivision = 0; // within m_ptrDivisionTable;
            m_ptrNextWeight = 0; // within m_ptrWeightCode;
            m_ptrNextCoefficient = 0; // within m_ptrCoefficient;
            m_ptrNextSource = 0; // within (PINT) m_ptrBuffer2;

            for (i = 0; i < nChannelCount; i++)
            {
                pLastDivision[i] = -1;
                pRestSamples[i] = datahdr.SampleCount;
                ptrDstBuf[i] = wave_pos + (int)i*sizeof(short);
            }
            int nCurrentDivision = -1;

            for (i = 0; i < nSubbandCount; i++)
            {
                for (j = 0; j < nChannelCount; j++)
                {
                    int nDivisionCode = m_ptrDivisionTable[m_ptrNextDivision++];
                    int nDivisionCount = 1 << nDivisionCode;
                    int nChannelStep = (int)(nDegreeWidth * m_mioih.LappedDegree * j);
                    m_ptrLastDCTBuf = nChannelStep; // within m_ptrLastDCT

                    bool fLeadBlock = false;
                    if (pLastDivision[j] != nDivisionCode)
                    {
                        if (i != 0)
                        {
                            if (nCurrentDivision != pLastDivision[j])
                            {
                                InitializeWithDegree (m_mioih.SubbandDegree - pLastDivision[j]);
                                nCurrentDivision = pLastDivision[j];
                            }
                            nSamples = pRestSamples[j];
                            if (nSamples > m_nDegreeNum)
                            {
                                nSamples = (uint)m_nDegreeNum;
                            }
                            DecodePostBlock (ptrWaveBuf, ptrDstBuf[j], nSamples);
                            pRestSamples[j] -= nSamples;
                            ptrDstBuf[j] += (int)(nSamples * nChannelCount * sizeof(short));
                        }
                        pLastDivision[j] = (int)nDivisionCode;
                        fLeadBlock = true;
                    }
                    if (nCurrentDivision != nDivisionCode)
                    {
                        InitializeWithDegree (m_mioih.SubbandDegree - nDivisionCode);
                        nCurrentDivision = nDivisionCode;
                    }
                    for (k = 0; k < nDivisionCount; k++)
                    {
                        if (fLeadBlock)
                        {
                            DecodeLeadBlock();
                            fLeadBlock = false;
                        }
                        else
                        {
                            nSamples = pRestSamples[j];
                            if (nSamples > m_nDegreeNum)
                            {
                                nSamples = (uint)m_nDegreeNum;
                            }
                            DecodeInternalBlock (ptrWaveBuf, ptrDstBuf[j], nSamples);
                            pRestSamples[j] -= nSamples;
                            ptrDstBuf[j] += (int)(nSamples * nChannelCount * sizeof(short));
                        }
                    }
                }
            }
            if (nSubbandCount > 0)
            {
                for (i = 0; i < nChannelCount; i ++)
                {
                    int nChannelStep = (int)(nDegreeWidth * m_mioih.LappedDegree * i);
                    m_ptrLastDCTBuf = nChannelStep; // within m_ptrLastDCT

                    if (nCurrentDivision != pLastDivision[i])
                    {
                        InitializeWithDegree (m_mioih.SubbandDegree - pLastDivision[i]);
                        nCurrentDivision = pLastDivision[i];
                    }
                    nSamples = pRestSamples[i];
                    if (nSamples > m_nDegreeNum)
                    {
                        nSamples = (uint)m_nDegreeNum;
                    }
                    DecodePostBlock (ptrWaveBuf, ptrDstBuf[i], nSamples);
                    pRestSamples[i] -= nSamples;
                    ptrDstBuf[i] += (int)(nSamples * nChannelCount * sizeof(short));
                }
            }
            return true;
        }
示例#8
0
        bool DecodeSoundDCT_MSS(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            uint nDegreeWidth     = 1u << m_mioih.SubbandDegree;
            uint nSampleCount     = (datahdr.SampleCount + nDegreeWidth - 1) & ~(nDegreeWidth - 1);
            uint nSubbandCount    = (nSampleCount >> m_mioih.SubbandDegree);
            uint nChannelCount    = (uint)m_mioih.ChannelCount;
            uint nAllSampleCount  = nSampleCount * nChannelCount;
            uint nAllSubbandCount = nSubbandCount;

            if (nSampleCount > m_nBufLength)
            {
                m_ptrBuffer2       = new int[nAllSampleCount];
                m_ptrBuffer3       = new sbyte[nAllSampleCount * sizeof(short)];
                m_ptrDivisionTable = new byte[nAllSubbandCount];
                m_ptrRevolveCode   = new byte[nAllSubbandCount * 10];
                m_ptrWeightCode    = new int[nAllSubbandCount * 10];
                m_ptrCoefficient   = new int[nAllSubbandCount * 10];
                m_nBufLength       = nSampleCount;
            }
            if (context.GetABit() != 0)
            {
                return(false);
            }

            int nLastDivision = -1;

            m_ptrNextDivision    = 0; // within m_ptrDivisionTable;
            m_ptrNextRevCode     = 0; // within m_ptrRevolveCode;
            m_ptrNextWeight      = 0; // within m_ptrWeightCode;
            m_ptrNextCoefficient = 0; // within m_ptrCoefficient;

            uint i, j, k;

            for (i = 0; i < nSubbandCount; i++)
            {
                int nDivisionCode = (int)context.GetNBits(2);
                m_ptrDivisionTable[m_ptrNextDivision++] = (byte)nDivisionCode;

                bool fLeadBlock = false;
                if (nDivisionCode != nLastDivision)
                {
                    if (i != 0)
                    {
                        m_ptrRevolveCode[m_ptrNextRevCode++]     = (byte)context.GetNBits(2);
                        m_ptrWeightCode[m_ptrNextWeight++]       = (int)context.GetNBits(32);
                        m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits(16);
                    }
                    fLeadBlock    = true;
                    nLastDivision = nDivisionCode;
                }
                uint nDivisionCount = 1u << nDivisionCode;
                for (k = 0; k < nDivisionCount; k++)
                {
                    if (fLeadBlock)
                    {
                        m_ptrRevolveCode[m_ptrNextRevCode++] = (byte)context.GetNBits(2);
                        fLeadBlock = false;
                    }
                    else
                    {
                        m_ptrRevolveCode[m_ptrNextRevCode++] = (byte)context.GetNBits(4);
                    }
                    m_ptrWeightCode[m_ptrNextWeight++]       = (int)context.GetNBits(32);
                    m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits(16);
                }
            }
            if (nSubbandCount > 0)
            {
                m_ptrRevolveCode[m_ptrNextRevCode++]     = (byte)context.GetNBits(2);
                m_ptrWeightCode[m_ptrNextWeight++]       = (int)context.GetNBits(32);
                m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits(16);
            }
            if (context.GetABit() != 0)
            {
                return(false);
            }
            if (0 != (datahdr.Flags & MIO_LEAD_BLOCK))
            {
                if (m_mioih.Architecture != EriCode.Nemesis)
                {
                    (context as HuffmanDecodeContext).PrepareToDecodeERINACode();
                }
                else
                {
                    throw new NotImplementedException("Nemesis encoding not implemented");
//                    context.PrepareToDecodeERISACode( );
                }
            }
            else if (m_mioih.Architecture == EriCode.Nemesis)
            {
                throw new NotImplementedException("Nemesis encoding not implemented");
//                context.InitializeERISACode( );
            }
            if (m_mioih.Architecture != EriCode.Nemesis)
            {
                if (context.DecodeBytes(m_ptrBuffer3, nAllSampleCount * 2) < nAllSampleCount * 2)
                {
                    return(false);
                }
                int ptrHBuf = 0;                    // within m_ptrBuffer3;
                int ptrLBuf = (int)nAllSampleCount; // within m_ptrBuffer3

                for (i = 0; i < nDegreeWidth * 2; i++)
                {
                    int ptrQuantumized = (int)i; // within (PINT) m_ptrBuffer2
                    for (j = 0; j < nAllSubbandCount; j++)
                    {
                        int nLow  = m_ptrBuffer3[ptrLBuf++];
                        int nHigh = m_ptrBuffer3[ptrHBuf++] ^ (nLow >> 8);
                        m_ptrBuffer2[ptrQuantumized] = (nLow & 0xFF) | (nHigh << 8);
                        ptrQuantumized += (int)nDegreeWidth * 2;
                    }
                }
            }
            else
            {
                throw new NotImplementedException("Nemesis encoding not implemented");

                /*
                 * if ( context.DecodeERISACodeWords
                 *      ( (SWORD*) m_ptrBuffer3, nAllSampleCount ) < nAllSampleCount )
                 * {
                 *  return false;
                 * }
                 * for ( i = 0; i < nAllSampleCount; i ++ )
                 * {
                 *  ((PINT)m_ptrBuffer2)[i] = ((SWORD*)m_ptrBuffer3)[i];
                 * }
                 */
            }
            uint nSamples;
            uint nRestSamples = datahdr.SampleCount;

//            int ptrDstBuf = wave_pos; // within (SWORD*) ptrWaveBuf;

            nLastDivision        = -1;
            m_ptrNextDivision    = 0; // m_ptrDivisionTable;
            m_ptrNextRevCode     = 0; // m_ptrRevolveCode;
            m_ptrNextWeight      = 0; // m_ptrWeightCode;
            m_ptrNextCoefficient = 0; // m_ptrCoefficient;
            m_ptrNextSource      = 0; // (PINT) m_ptrBuffer2;

            for (i = 0; i < nSubbandCount; i++)
            {
                int  nDivisionCode  = m_ptrDivisionTable[m_ptrNextDivision++];
                uint nDivisionCount = 1u << nDivisionCode;

                bool fLeadBlock = false;
                if (nLastDivision != nDivisionCode)
                {
                    if (i != 0)
                    {
                        nSamples = Math.Min(nRestSamples, (uint)m_nDegreeNum);
                        DecodePostBlock_MSS(ptrWaveBuf, wave_pos, nSamples);
                        nRestSamples -= nSamples;
                        wave_pos     += (int)(nSamples * nChannelCount * sizeof(short));
                    }
                    InitializeWithDegree(m_mioih.SubbandDegree - nDivisionCode);
                    nLastDivision = nDivisionCode;
                    fLeadBlock    = true;
                }
                for (k = 0; k < nDivisionCount; k++)
                {
                    if (fLeadBlock)
                    {
                        DecodeLeadBlock_MSS();
                        fLeadBlock = false;
                    }
                    else
                    {
                        nSamples = nRestSamples;
                        if (nSamples > m_nDegreeNum)
                        {
                            nSamples = (uint)m_nDegreeNum;
                        }
                        DecodeInternalBlock_MSS(ptrWaveBuf, wave_pos, nSamples);
                        nRestSamples -= nSamples;
                        wave_pos     += (int)(nSamples * nChannelCount * sizeof(short));
                    }
                }
            }
            if (nSubbandCount > 0)
            {
                nSamples = nRestSamples;
                if (nSamples > m_nDegreeNum)
                {
                    nSamples = (uint)m_nDegreeNum;
                }
                DecodePostBlock_MSS(ptrWaveBuf, wave_pos, nSamples);
                nRestSamples -= nSamples;
                wave_pos     += (int)(nSamples * nChannelCount) * sizeof(short);
            }
            return(true);
        }
示例#9
0
        bool DecodeSoundDCT(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            uint i, j, k;
            uint nDegreeWidth     = 1u << m_mioih.SubbandDegree;
            uint nSampleCount     = (datahdr.SampleCount + nDegreeWidth - 1) & ~(nDegreeWidth - 1);
            uint nSubbandCount    = (nSampleCount >> m_mioih.SubbandDegree);
            uint nChannelCount    = (uint)m_mioih.ChannelCount;
            uint nAllSampleCount  = nSampleCount * nChannelCount;
            uint nAllSubbandCount = nSubbandCount * nChannelCount;

            if (nSampleCount > m_nBufLength)
            {
                m_ptrBuffer2       = new int[nAllSampleCount];
                m_ptrBuffer3       = new sbyte[nAllSampleCount * sizeof(short)];
                m_ptrDivisionTable = new byte[nAllSubbandCount];
                m_ptrWeightCode    = new int[nAllSubbandCount * 5];
                m_ptrCoefficient   = new int[nAllSubbandCount * 5];
                m_nBufLength       = nSampleCount;
            }
            if (context.GetABit() != 0)
            {
                return(false);
            }
            int[] pLastDivision = new int [nChannelCount];
            m_ptrNextDivision    = 0; // within m_ptrDivisionTable;
            m_ptrNextWeight      = 0; // within m_ptrWeightCode;
            m_ptrNextCoefficient = 0; // within m_ptrCoefficient;

            for (i = 0; i < nChannelCount; i++)
            {
                pLastDivision[i] = -1;
            }
            for (i = 0; i < nSubbandCount; i++)
            {
                for (j = 0; j < nChannelCount; j++)
                {
                    int nDivisionCode = (int)context.GetNBits(2);
                    m_ptrDivisionTable[m_ptrNextDivision++] = (byte)nDivisionCode;

                    if (nDivisionCode != pLastDivision[j])
                    {
                        if (i != 0)
                        {
                            m_ptrWeightCode[m_ptrNextWeight++]       = (int)context.GetNBits(32);
                            m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits(16);
                        }
                        pLastDivision[j] = nDivisionCode;
                    }

                    uint nDivisionCount = 1u << nDivisionCode;
                    for (k = 0; k < nDivisionCount; k++)
                    {
                        m_ptrWeightCode[m_ptrNextWeight++]       = (int)context.GetNBits(32);
                        m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits(16);
                    }
                }
            }
            if (nSubbandCount > 0)
            {
                for (i = 0; i < nChannelCount; i++)
                {
                    m_ptrWeightCode[m_ptrNextWeight++]       = (int)context.GetNBits(32);
                    m_ptrCoefficient[m_ptrNextCoefficient++] = (int)context.GetNBits(16);
                }
            }

            if (context.GetABit() != 0)
            {
                return(false);
            }
            if (0 != (datahdr.Flags & MIO_LEAD_BLOCK))
            {
                if (m_mioih.Architecture != EriCode.Nemesis)
                {
                    (context as HuffmanDecodeContext).PrepareToDecodeERINACode();
                }
                else
                {
                    throw new NotImplementedException("Nemesis encoding not implemented");
//                    context.PrepareToDecodeERISACode();
                }
            }
            else if (m_mioih.Architecture == EriCode.Nemesis)
            {
                throw new NotImplementedException("Nemesis encoding not implemented");
//                context.InitializeERISACode();
            }
            if (m_mioih.Architecture != EriCode.Nemesis)
            {
                if (context.DecodeBytes(m_ptrBuffer3, nAllSampleCount * 2) < nAllSampleCount * 2)
                {
                    return(false);
                }
                int ptrHBuf = 0;                    // within m_ptrBuffer3;
                int ptrLBuf = (int)nAllSampleCount; // within m_ptrBuffer3

                for (i = 0; i < nDegreeWidth; i++)
                {
                    int ptrQuantumized = (int)i; // within (PINT) m_ptrBuffer2
                    for (j = 0; j < nAllSubbandCount; j++)
                    {
                        int nLow  = m_ptrBuffer3[ptrLBuf++];
                        int nHigh = m_ptrBuffer3[ptrHBuf++] ^ (nLow >> 8);
                        m_ptrBuffer2[ptrQuantumized] = (nLow & 0xFF) | (nHigh << 8);
                        ptrQuantumized += (int)nDegreeWidth;
                    }
                }
            }
            else
            {
                throw new NotImplementedException("Nemesis encoding not implemented");

                /*
                 * if (context.DecodeERISACodeWords (m_ptrBuffer3, nAllSampleCount) < nAllSampleCount)
                 * {
                 *  return false;
                 * }
                 * for (i = 0; i < nAllSampleCount; i++)
                 * {
                 *  ((PINT)m_ptrBuffer2)[i] = ((SWORD*)m_ptrBuffer3)[i];
                 * }
                 */
            }
            uint nSamples;

            uint[] pRestSamples = new uint [nChannelCount];
            int[]  ptrDstBuf    = new int [nChannelCount]; // indices within ptrWaveBuf

            m_ptrNextDivision    = 0;                      // within m_ptrDivisionTable;
            m_ptrNextWeight      = 0;                      // within m_ptrWeightCode;
            m_ptrNextCoefficient = 0;                      // within m_ptrCoefficient;
            m_ptrNextSource      = 0;                      // within (PINT) m_ptrBuffer2;

            for (i = 0; i < nChannelCount; i++)
            {
                pLastDivision[i] = -1;
                pRestSamples[i]  = datahdr.SampleCount;
                ptrDstBuf[i]     = wave_pos + (int)i * sizeof(short);
            }
            int nCurrentDivision = -1;

            for (i = 0; i < nSubbandCount; i++)
            {
                for (j = 0; j < nChannelCount; j++)
                {
                    int nDivisionCode  = m_ptrDivisionTable[m_ptrNextDivision++];
                    int nDivisionCount = 1 << nDivisionCode;
                    int nChannelStep   = (int)(nDegreeWidth * m_mioih.LappedDegree * j);
                    m_ptrLastDCTBuf = nChannelStep; // within m_ptrLastDCT

                    bool fLeadBlock = false;
                    if (pLastDivision[j] != nDivisionCode)
                    {
                        if (i != 0)
                        {
                            if (nCurrentDivision != pLastDivision[j])
                            {
                                InitializeWithDegree(m_mioih.SubbandDegree - pLastDivision[j]);
                                nCurrentDivision = pLastDivision[j];
                            }
                            nSamples = pRestSamples[j];
                            if (nSamples > m_nDegreeNum)
                            {
                                nSamples = (uint)m_nDegreeNum;
                            }
                            DecodePostBlock(ptrWaveBuf, ptrDstBuf[j], nSamples);
                            pRestSamples[j] -= nSamples;
                            ptrDstBuf[j]    += (int)(nSamples * nChannelCount * sizeof(short));
                        }
                        pLastDivision[j] = (int)nDivisionCode;
                        fLeadBlock       = true;
                    }
                    if (nCurrentDivision != nDivisionCode)
                    {
                        InitializeWithDegree(m_mioih.SubbandDegree - nDivisionCode);
                        nCurrentDivision = nDivisionCode;
                    }
                    for (k = 0; k < nDivisionCount; k++)
                    {
                        if (fLeadBlock)
                        {
                            DecodeLeadBlock();
                            fLeadBlock = false;
                        }
                        else
                        {
                            nSamples = pRestSamples[j];
                            if (nSamples > m_nDegreeNum)
                            {
                                nSamples = (uint)m_nDegreeNum;
                            }
                            DecodeInternalBlock(ptrWaveBuf, ptrDstBuf[j], nSamples);
                            pRestSamples[j] -= nSamples;
                            ptrDstBuf[j]    += (int)(nSamples * nChannelCount * sizeof(short));
                        }
                    }
                }
            }
            if (nSubbandCount > 0)
            {
                for (i = 0; i < nChannelCount; i++)
                {
                    int nChannelStep = (int)(nDegreeWidth * m_mioih.LappedDegree * i);
                    m_ptrLastDCTBuf = nChannelStep; // within m_ptrLastDCT

                    if (nCurrentDivision != pLastDivision[i])
                    {
                        InitializeWithDegree(m_mioih.SubbandDegree - pLastDivision[i]);
                        nCurrentDivision = pLastDivision[i];
                    }
                    nSamples = pRestSamples[i];
                    if (nSamples > m_nDegreeNum)
                    {
                        nSamples = (uint)m_nDegreeNum;
                    }
                    DecodePostBlock(ptrWaveBuf, ptrDstBuf[i], nSamples);
                    pRestSamples[i] -= nSamples;
                    ptrDstBuf[i]    += (int)(nSamples * nChannelCount * sizeof(short));
                }
            }
            return(true);
        }
示例#10
0
        bool DecodeSoundPCM16(ERISADecodeContext context, MioDataHeader datahdr, byte[] ptrWaveBuf, int wave_pos)
        {
            uint nSampleCount    = datahdr.SampleCount;
            uint nChannelCount   = (uint)m_mioih.ChannelCount;
            uint nAllSampleCount = nSampleCount * nChannelCount;
            uint nBytes          = nAllSampleCount * sizeof(short);

            if (ptrWaveBuf.Length < wave_pos + (int)nBytes)
            {
                return(false);
            }

            if (nSampleCount > m_nBufLength)
            {
                m_ptrBuffer3 = new sbyte[nBytes];
                m_ptrBuffer4 = new byte[nBytes];
                m_nBufLength = nSampleCount;
            }
            if (0 != (datahdr.Flags & MIO_LEAD_BLOCK))
            {
                (context as HuffmanDecodeContext).PrepareToDecodeERINACode();
            }
            if (context.DecodeBytes(m_ptrBuffer3, nBytes) < nBytes)
            {
                return(false);
            }
            int pbytSrcBuf1, pbytSrcBuf2, pbytDstBuf;

            for (int i = 0; i < m_mioih.ChannelCount; i++)
            {
                int nOffset = i * (int)nSampleCount * sizeof(short);
                pbytSrcBuf1 = nOffset;                         // ((PBYTE) m_ptrBuffer3) + nOffset;
                pbytSrcBuf2 = pbytSrcBuf1 + (int)nSampleCount; // pbytSrcBuf1 + nSampleCount;
                pbytDstBuf  = nOffset;                         // ((PBYTE) m_ptrBuffer4) + nOffset;

                for (uint j = 0; j < nSampleCount; j++)
                {
                    sbyte bytLow  = m_ptrBuffer3[pbytSrcBuf2 + j];
                    sbyte bytHigh = m_ptrBuffer3[pbytSrcBuf1 + j];
                    m_ptrBuffer4[pbytDstBuf + j * sizeof(short) + 0] = (byte)bytLow;
                    m_ptrBuffer4[pbytDstBuf + j * sizeof(short) + 1] = (byte)(bytHigh ^ (bytLow >> 7));
                }
            }
            if (m_ptrBuffer4.Length < nBytes)
            {
                return(false);
            }
            unsafe
            {
                fixed(byte *rawBuffer4 = m_ptrBuffer4, rawWaveBuf = &ptrWaveBuf[wave_pos])
                {
                    int    nStep     = m_mioih.ChannelCount;
                    short *ptrSrcBuf = (short *)rawBuffer4;

                    for (int i = 0; i < m_mioih.ChannelCount; i++)
                    {
                        short *ptrDstBuf = (short *)rawWaveBuf + i; // (SWORD*) ptrWaveBuf;
                        short  wValue    = 0;
                        short  wDelta    = 0;
                        for (uint j = 0; j < nSampleCount; j++)
                        {
                            wDelta += *ptrSrcBuf++;
                            wValue += wDelta;
                            *ptrDstBuf = wValue;
                            ptrDstBuf += nStep;
                        }
                    }
                }
            }
            return(true);
        }