Audio-Play-MPG123
view release on metacpan or search on metacpan
mpg123/system.c view on Meta::CPAN
static int system_raw_read_head(int f,unsigned long *head)
{
unsigned char buf[4];
if(my_read(f,buf,4) != 4) {
perror("read_head");
return -1;
}
*head = (buf[0]<<24) + (buf[1]<<16) + (buf[2]<<8) + buf[3];
if(verbose > 1)
fprintf(stderr,"head: %08lx\n",*head);
return 0;
}
static int system_raw_read_word(int f,int *word)
{
unsigned char buf[2];
int ret;
if(my_read(f,buf,2) != 2) {
perror("read_word");
return -1;
}
*word = (buf[0]<<8) + buf[1];
return 0;
}
static int system_raw_read(int f,int len,unsigned char *buf)
{
if(my_read(f,buf,len) != len)
return -1;
return 0;
}
static int system_raw_skip(int f,int len)
{
int ret;
int cnt = 0;
ret = lseek(f,len,SEEK_CUR);
if(ret < 0 && errno == ESPIPE) {
cnt = len;
while(cnt) {
char buf[1024];
if(cnt > 1024)
ret = read(f,buf,1024);
else
ret = read(f,buf,cnt);
if(ret < 0)
return -1;
cnt -= ret;
}
ret = len;
}
return ret;
}
static unsigned long system_raw_timer_value(unsigned char *buf)
{
unsigned long val;
if(!(buf[0] & 0x1) || !(buf[2] & 0x1) || !(buf[4] & 0x1)) {
if(verbose)
fprintf(stderr,"Warning: missing marker in time stamp!\n");
}
val = (buf[0] & 0xe) << (29-1);
val |= buf[1] << 21;
val |= (buf[2] & 0xfe) << (14-1);
val |= buf[3] << 7;
val |= buf[4] >> 1;
return val;
}
static int system_raw_read_packet_data(int fd,struct packet_info *pi)
{
static unsigned char buf[16384];
int len;
int pos = 0;
int i;
if(system_raw_read_word(filept,&len) < 0)
return -1;
if(verbose > 1)
fprintf(stderr,"Stream video/audio len: %d\n",len);
if(system_raw_read(fd,len,buf) < 0)
return -1;
for(i=0;i<16;i++,pos++) {
if(buf[pos] != 0xff)
break;
}
if(i == 16) {
fprintf(stderr,"Ouch ... too much stuffing bytes!\n");
return -1;
}
if( (buf[pos] & 0xc0) == 0x40 ) {
pi->scale = (buf[pos] >> 5) & 0x1;
pi->size = (buf[pos] & 0x1f) << 8;
pi->size |= buf[pos+1];
pos += 2;
}
switch( buf[pos] & 0xf0) {
case 0x00:
if(buf[pos] != 0x0f) {
fprintf(stderr,"Ouch ... illegal timer code!\n");
return -1;
}
pos++;
break;
case 0x20:
pi->pts = system_raw_timer_value(buf+pos);
pos += 5;
break;
case 0x30:
pi->pts = system_raw_timer_value(buf+pos);
pos += 5;
if( (buf[pos] & 0xf) != 0x10) {
if(verbose)
fprintf(stderr,"DTS should start with 0x1x!\n");
}
pi->dts = system_raw_timer_value(buf+pos);
pos += 5;
break;
default:
if(verbose)
fprintf(stderr,"Ouch ... illegal timer code!\n");
return -1;
}
#if 1
write(1,buf+pos,len-pos);
#endif
return 0;
}
static int system_raw_read_packet_info(int f,double *clock,unsigned long *rate)
{
unsigned char buf[8];
int i;
if(my_read(f,buf,8) != 8) {
perror("read_packet_info");
return -1;
}
*clock = 0.0;
for(i=0;i<5;i++) {
*clock *= 256.0;
*clock += (double) buf[4-i];
}
*rate = (buf[5]<<16) + (buf[6]<<8) + buf[7];
return 0;
}
static int system_raw_read_system_header(int f,struct system_info *ssi)
{
int rlen,len;
unsigned char buf[6+48*3];
int i,cnt;
if(system_raw_read_word(filept,&len) < 0)
return -1;
if(verbose > 1)
fprintf(stderr,"system len: %d\n",len);
rlen = len;
if(len > 6 + 48 * 3) {
if(verbose)
fprintf(stderr,"Oops .. large System header!\n");
rlen = 6+48*3;
}
if(my_read(f,buf,rlen) != rlen) {
perror("raw_read_system_header");
return -1;
}
if(len - rlen) {
if(system_raw_skip(filept,len-rlen) < 0)
return -1;
}
( run in 1.221 second using v1.01-cache-2.11-cpan-817d5f8af8b )