slow bitshifting 24 to 32 bit

Hello,

I have here a working way of shifting a 24 bit audio file to a 32 bit buffer. The whole (!) file should be read to this big buffer. For every 3 bytes a fread is used in the loop. That is too slow. Any idea how to make this fast?

Thanks a lot!
Regards
Thomas

1
2
3
4
5
6
7
8
9
	unsigned char *buf = new unsigned char[3]; // 3 bytes - 24 bit
	unsigned int *buffer32 = new unsigned int[size32]; // big buffer with the whole file
	for (i=0; i < size32/4; i++)
	{
	fread(buf, 3, 1, fd); // 3 bytes - 24 bit , fread for every 3 bytes is too slow !!!
	//24-bit data being packed into the lower 3 bytes of a 32-bit word:
	buffer32[i] = null[0] | buf[2] << 24 | buf[1] << 16 | buf[0] << 8 ;
	}
	delete[] buf;
Last edited on
Use a memory mapped file, letting operating system handle reading from the file behind the scenes. You will "see" a contiguous chunk of memory as a result.
I prob. wouldn't use a memory mapped file here. But it looks like you could read the i/p file in one go into a big enough buffer and then walk through it 3 bytes at a time.

You are already allocating a single buffer for the result, so I presume the file you're dealing with is not that big?

An inbetween solution would be to use two loops. The outer loop reads the file in big chunks, and the inner one does the byte-wise stuff. But as you are already creating a single buffer for the result...

By the way, I see you're using new and delete, so this is "C++ code". So why not use std::ifstream and std::vector<>?

And in your code above, I think buf is too tiny to allocate on the heap.

1
2
3
4
5
6
7
8
9
    unsigned char buf[3]; // 3 bytes - 24 bit - on the stack
    unsigned int *buffer32 = new unsigned int[size32]; // big buffer with the whole file
    for (i=0; i < size32/4; i++)
    {
        fread(buf, 3, 1, fd); // 3 bytes - 24 bit , fread for every 3 bytes is too slow !!!
        //24-bit data being packed into the lower 3 bytes of a 32-bit word:
        buffer32[i] = null[0] | buf[2] << 24 | buf[1] << 16 | buf[0] << 8 ;
    }
    // no need to delete buf! :-) 


Andy

PS I would use a memory mapped file when dealing with a big enough file. Or if I needed random access. But it would be to solve an overly ore complicated problem.
Last edited on
The file could be very big! So using a separate big 24 bit buffer would implicate two very big buffers. This is very fast, but too big... May be a bigger "buf" could be the solution?

And I need to have this in the code

 
buffer32[i] = null[0] | buf[2] << 24 | buf[1] << 16 | buf[0] << 8 ;


because there are more versions, where I must use other kind of bitshiftings.

Regards
Thomas
Topic archived. No new replies allowed.