When I read a binary mkv the id of a cluster is E7 byte and the timestamp has an unsigned int value but when I read it id doesn't give me the correct timestamp.
double mkVSParser::get_clusters_timestamps(char *&package,unsigned long &size)
{
uint8_t *data_to_find = new uint8_t;
*data_to_find=0xE7;//the id
char * buffer = new char[sizeof (uint8_t)];
uint8_t current_data[sizeof (uint8_t)];
for(int i=0;i<size;i++)//finde the first 0xE7 in an cluster
{
memcpy(&buffer[0],&package[i],sizeof (uint8_t));
memcpy(¤t_data[0],buffer,sizeof (uint8_t));
if (memcmp(data_to_find, current_data, sizeof (uint8_t)) == 0)
{
unsigned int timestemp;
std::cout<<"position of byte =="<<i<<"and id =="<<(unsigned int)package[i]<<std::endl;
memcpy(×temp,&package[i+1],sizeof(unsigned int));
std::cout<<"cluster timestemp ="<<timestemp<<std::endl;
return 0;
}
}
return 0;
}
Is there something that I missed?
MKV binary data is in EBML format and unsigned integer may be variable in size. Variable size int's may consist of variable number of octets (may have different size in bytes).
Position of first '1' bit in first byte of variable size integer denotes size in bytes. If it's on the first position
then the variable is one byte long and the rest of the bits after first '1' bit (7 lower bits in this case) are the binary representation of the number. Variable size int that starts with
is seven bytes long as first '1' bit here is on the seventh position.
So first you need to read first byte of the number and find the position N of the first '1' bit and then read the whole number N bytes long ignoring that first '1' bit (like it's a zero bit).