Exemplo n.º 1
0
        /**
         * Read a 32-bit header from the bitstream.
         */
        public void read_header(Bitstream stream, Crc16[] crcp)
        {
            uint headerstring; // made to a uint for unsigned right-shift used in this function
            int  channel_bitrate;
            bool sync = false;

            do
            {
                headerstring  = (uint)stream.syncHeader(syncmode);
                _headerstring = (int)headerstring; // E.B
                if (syncmode == Bitstream.INITIAL_SYNC)
                {
                    h_version = (int)((headerstring >> 19) & 1);
                    if (((headerstring >> 20) & 1) == 0) // SZD: MPEG2.5 detection
                    {
                        if (h_version == MPEG2_LSF)
                        {
                            h_version = MPEG25_LSF;
                        }
                        else
                        {
                            throw new BitstreamException(Errors.UNKNOWN_ERROR);
                        }
                    }
                    if ((h_sample_frequency = (int)((headerstring >> 10) & 3)) == 3)
                    {
                        throw new BitstreamException(Errors.UNKNOWN_ERROR);
                    }
                }
                h_layer          = (int)(4 - (headerstring >> 17) & 3);
                h_protection_bit = (int)((headerstring >> 16) & 1);
                h_bitrate_index  = (int)((headerstring >> 12) & 0xF);
                h_padding_bit    = (int)((headerstring >> 9) & 1);
                h_mode           = (int)((headerstring >> 6) & 3);
                h_mode_extension = (int)((headerstring >> 4) & 3);
                if (h_mode == JOINT_STEREO)
                {
                    h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
                }
                else
                {
                    h_intensity_stereo_bound = 0; // should never be used
                }
                if (((headerstring >> 3) & 1) == 1)
                {
                    h_copyright = true;
                }
                if (((headerstring >> 2) & 1) == 1)
                {
                    h_original = true;
                }
                // calculate number of subbands:
                if (h_layer == 1)
                {
                    h_number_of_subbands = 32;
                }
                else
                {
                    channel_bitrate = h_bitrate_index;
                    // calculate bitrate per channel:
                    if (h_mode != SINGLE_CHANNEL)
                    {
                        if (channel_bitrate == 4)
                        {
                            channel_bitrate = 1;
                        }
                        else
                        {
                            channel_bitrate -= 4;
                        }
                    }
                    if ((channel_bitrate == 1) || (channel_bitrate == 2))
                    {
                        if (h_sample_frequency == THIRTYTWO)
                        {
                            h_number_of_subbands = 12;
                        }
                        else
                        {
                            h_number_of_subbands = 8;
                        }
                    }
                    else if ((h_sample_frequency == FOURTYEIGHT) || ((channel_bitrate >= 3) && (channel_bitrate <= 5)))
                    {
                        h_number_of_subbands = 27;
                    }
                    else
                    {
                        h_number_of_subbands = 30;
                    }
                }
                if (h_intensity_stereo_bound > h_number_of_subbands)
                {
                    h_intensity_stereo_bound = h_number_of_subbands;
                }
                // calculate framesize and nSlots
                calculate_framesize();
                // read framedata:
                int framesizeloaded = stream.read_frame_data(framesize);
                if ((framesize >= 0) && (framesizeloaded != framesize))
                {
                    // Data loaded does not match to expected framesize,
                    // it might be an ID3v1 TAG. (Fix 11/17/04).
                    throw new BitstreamException(Errors.INVALIDFRAME);
                }
                if (stream.isSyncCurrentPosition(syncmode))
                {
                    if (syncmode == Bitstream.INITIAL_SYNC)
                    {
                        syncmode = Bitstream.STRICT_SYNC;
                        stream.set_syncword(headerstring & 0xFFF80CC0);
                    }
                    sync = true;
                }
                else
                {
                    stream.unreadFrame();
                }
            }while (!sync);
            stream.parse_frame();
            if (h_protection_bit == 0)
            {
                // frame contains a crc checksum
                checksum = (short)stream.get_bits(16);
                if (crc == null)
                {
                    crc = new Crc16();
                }
                crc.add_bits((int)headerstring, 16);
                crcp[0] = crc;
            }
            else
            {
                crcp[0] = null;
            }
            if (h_sample_frequency == FOURTYFOUR_POINT_ONE)
            {
                /*
                 *  if (offset == null)
                 * {
                 *    int max = max_number_of_frames(stream);
                 *    offset = new int[max];
                 *   for(int i=0; i<max; i++) offset[i] = 0;
                 * }
                 * // E.B : Investigate more
                 * int cf = stream.current_frame();
                 * int lf = stream.last_frame();
                 * if ((cf > 0) && (cf == lf))
                 * {
                 *     offset[cf] = offset[cf-1] + h_padding_bit;
                 * }
                 * else
                 * {
                 *         offset[0] = h_padding_bit;
                 * }
                 */
            }
        }