Esempio n. 1
0
int	inflate_seek(int pos, void* appdata)
// Try to go to pos.  Return actual pos.
{
    inflater_impl*	inf = (inflater_impl*) appdata;
    if (inf->m_error)
    {
        return inf->m_logical_stream_pos;
    }

    // If we're seeking backwards, then restart from the beginning.
    if (pos < inf->m_logical_stream_pos)
    {
        inf->reset();
    }

    unsigned char	temp[ZBUF_SIZE];

    // Now seek forwards, by just reading data in blocks.
    while (inf->m_logical_stream_pos < pos)
    {
        int	to_read = pos - inf->m_logical_stream_pos;
        int	to_read_this_time = imin_swf(to_read, ZBUF_SIZE);
        assert(to_read_this_time > 0);

        int	bytes_read = inf->inflate_from_stream(temp, to_read_this_time);
        assert(bytes_read <= to_read_this_time);
        if (bytes_read == 0)
        {
            // Trouble; can't seek any further.
            break;
        }
    }

    assert(inf->m_logical_stream_pos <= pos);

    return inf->m_logical_stream_pos;
}
	void	adpcm_expand(
		void* out_data_void,
		stream* in,
		int sample_count,	// in stereo, this is number of *pairs* of samples
		bool stereo)
	// Utility function: uncompress ADPCM data from in stream to
	// out_data[].	The output buffer must have (sample_count*2)
	// bytes for mono, or (sample_count*4) bytes for stereo.
	{
		Sint16*	out_data = (Sint16*) out_data_void;

		// Read header.
		int	n_bits = in->read_uint(2) + 2;	// 2 to 5 bits

		while (sample_count)
		{
			// Read initial sample & index values.
			int	sample = in->read_sint(16);

			int	stepsize_index = in->read_uint(6);
			assert(STEPSIZE_CT >= (1 << 6));	// ensure we don't need to clamp.

			int	samples_this_block = imin_swf(sample_count, 4096);
			sample_count -= samples_this_block;

			if (stereo == false)
			{
#define DO_MONO(n) DO_MONO_BLOCK(&out_data, n, samples_this_block, in, sample, stepsize_index)

				switch (n_bits)
				{
				default: assert(0); break;
				case 2: DO_MONO(2); break;
				case 3: DO_MONO(3); break;
				case 4: DO_MONO(4); break;
				case 5: DO_MONO(5); break;
				}
			}
			else
			{
				// Stereo.

				// Got values for left channel; now get initial sample
				// & index for right channel.
				int	right_sample = in->read_sint(16);

				int	right_stepsize_index = in->read_uint(6);
				assert(STEPSIZE_CT >= (1 << 6));	// ensure we don't need to clamp.

#define DO_STEREO(n)					\
	DO_STEREO_BLOCK(				\
		&out_data, n, samples_this_block,	\
		in, sample, stepsize_index,		\
		right_sample, right_stepsize_index)
			
				switch (n_bits)
				{
				default: assert(0); break;
				case 2: DO_STEREO(2); break;
				case 3: DO_STEREO(3); break;
				case 4: DO_STEREO(4); break;
				case 5: DO_STEREO(5); break;
				}
			}
		}
	}