int nalu_read()

in ftl_app/nalu.c [719:849]


int nalu_read(FILE *fp, unsigned char *buf)
{
	/*buffer in 100 bytes then start parsing*/
	unsigned int psc = 0, byte = 0;
	int psc_found = 0;
	int i;
	int nalu_len;
	int bytes_in_buffer;

	struct bitstream_elmt_t bs;
	int leading_zero_byte_cnt = 0;

#if 1
	while( (fread(&byte, 1, 1, fp)) == 1)
	{
		if(byte == 1)
		{
			if(leading_zero_byte_cnt >= 2)
			{
				psc_found = 1;
				break;
			}
		}
		
		if(byte == 0)
			leading_zero_byte_cnt++;
		else
			leading_zero_byte_cnt = 0;
	}

	if(!psc_found)
		return -1;

	leading_zero_byte_cnt = 0;
	psc_found = 0;
	nalu_len = 0;
	bytes_in_buffer = 0;
	while( (fread(&byte, 1, 1, fp)) == 1)
	{
		if(byte == 1)
		{
			if(leading_zero_byte_cnt >= 2)
			{
				/*backup 3 bytes so next run sees the start code*/
				psc_found = 1;
				fseek(fp, -3, SEEK_CUR);
				bytes_in_buffer -= 3;
				break;
			}
		}
		
		if(byte == 0)
			leading_zero_byte_cnt++;
		else
			leading_zero_byte_cnt = 0;

		buf[bytes_in_buffer] = byte;
		bytes_in_buffer++;
	}

	if(leading_zero_byte_cnt > 3)
	{
		printf("Found packet with too many leading zero's in start code");
	}

	if(!psc_found)
	{
		printf("End of stream\n");
	}

	nalu_len = bytes_in_buffer;
#endif

#if 0

	fread(&psc, 1, 3, fp);

	if(psc != 0x000001)
	{
		psc = psc << 8;

		fread(&byte, 1, 1, fp);

		psc = (psc & 0xFFFFFF00) | (byte & 0xFF);

		if(psc != 0x00000001)
		{
			/*rewind stream*/
			fseek(fp, -4, SEEK_CUR);
			
			return -1;
		}
	}

	bitstream_init(&bs, buf);
	nalu_len = 0;
	bytes_in_buffer = 0;
	do
	{
		bytes_in_buffer += fread(buf, 1, NALU_READ_PRELOAD, fp);

		for(i=0; i < bytes_in_buffer-3; i++)
		{
			if(bitstream_peak(&bs, 24) == 1 || bitstream_peak(&bs, 32) == 1)
			{
				psc_found = 1;
				break;
			}

			bitstream_u(&bs, 8);
			nalu_len++;
		}

		bytes_in_buffer -= i;
		buf += NALU_READ_PRELOAD;
	}while(!psc_found);

	fseek(fp, -bytes_in_buffer, SEEK_CUR);
#endif

#if 0

	fread(&psc, 1, 4, fp);

	fread(buf, 1, 8, fp);

	nalu_len = 8;
#endif

	return nalu_len;
}