/* read ahead and find the next MPEG header, to guess framesize return value: success code 1: found a valid frame size (stored in the handle). <0: error codes, possibly from feeder buffer (NEED_MORE) 0: cannot get the framesize for some reason and shall silentry try the next possible header (if this is no free format stream after all...)  */
static int guess_freeformat_framesize(mpg123_handle *fr)
/* read ahead and find the next MPEG header, to guess framesize return value: success code 1: found a valid frame size (stored in the handle). <0: error codes, possibly from feeder buffer (NEED_MORE) 0: cannot get the framesize for some reason and shall silentry try the next possible header (if this is no free format stream after all...)  */
static int guess_freeformat_framesize(mpg123_handle *fr)
{
	long i;
	int ret;
	unsigned long head;
	if(!(fr->rdat.flags & (READER_SEEKABLE|READER_BUFFERED)))
	{
		if(NOQUIET) error("Cannot look for freeformat frame size with non-seekable and non-buffered stream!");
		return 0;
	}
	if((ret=fr->rd->head_read(fr,&head))<=0)
	return ret;
	for(i=4;i<65536;i++)
	{
		if((ret=fr->rd->head_shift(fr,&head))<=0) return ret;
		if((head & HDR_SAMEMASK) == (fr->oldhead & HDR_SAMEMASK))
		{
			fr->rd->back_bytes(fr,i+1);
			fr->framesize = i-3;
			return 1;
		}
	}
	fr->rd->back_bytes(fr,i);
	return 0;
}