int load_GSM(media_entry *p) { unsigned char byte1; int ret; if (!(p->description.flags & MED_PKT_LEN)) { p->description.pkt_len=20; /* By default for GSM */ p->description.delta_mtime=p->description.pkt_len; p->description.flags|=MED_PKT_LEN; } if ( (ret=mediaopen(p)) < 0) return ret; if (read(p->fd,&byte1,1) != 1) return ERR_PARSE; mediaclose(p); switch (byte1 & 0x07) { case 0: p->description.bitrate = 4750; break; case 1: p->description.bitrate = 5150; break; case 2: p->description.bitrate = 5900; break; case 3: p->description.bitrate = 6700; break; case 4: p->description.bitrate = 7400; break; case 5: p->description.bitrate = 7950; break; case 6: p->description.bitrate = 10200; break; case 7: p->description.bitrate = 12200; break; } p->description.flags|=MED_BITRATE; return ERR_NOERROR; }
int read_MP3( media_entry * me, uint8 * data, uint32 * data_size, double *mtime, int *recallme, uint8 * marker) { // char thefile[255]; int ret; unsigned char *buff = me->buff_data; int N = 0, res; *marker = 0; *recallme = 0; if (!(me->flags & ME_FD)) { if ((ret = mediaopen(me)) < 0) return ret; //me->prev_mstart_offset=0; /* TODO support Id3 TAG v2, this doesn't work */ //if ((me->description.flags & MED_ID3) && (me->description).msource!=live) // lseek(me->fd,(me->description.tag_dim)+10,SEEK_SET); } if (me->play_offset != -1 && (me->description).msource != live) { //random access N = (float) me->description.bitrate / 8 * me->play_offset / 1000; /*me->prev_mstart_offset= */ me->play_offset = -1; // pkt_len is pkt lenght in milliseconds, lseek(me->fd, N, SEEK_SET); } if ((read(me->fd, &(buff[me->buff_size]), 4)) != 4) { return ERR_EOF; } me->buff_size = 0; if ((buff[0] == 0xff) && ((buff[1] & 0xe0) == 0xe0)) { N = (int) (me->description.frame_len * (float) me->description.bitrate / (float) me->description.sample_rate / 8); if (buff[2] & 0x02) N++; } else { // Sync not found, not Mpeg-1/2 // id3 TAG v1 is suppressed, id3 TAG v2 is not supported and //fprintf(stderr,"ERROR: Sync not found, not Mpeg-1/2\n"); N = (int) (me->description.frame_len * (float) me->description.bitrate / (float) me->description.sample_rate / 8); //return ERR_EOF; } *data_size = N + 4; // 4 bytes are needed for mp3 in RTP encapsulation data[0] = 0; data[1] = 0; data[2] = 0; data[3] = 0; // These first 4 bytes are for mp3 in RTP encapsulation data[4] = buff[0]; data[5] = buff[1]; data[6] = buff[2]; data[7] = buff[3]; if ((res = read(me->fd, &(data[8]), N - 4)) < (N - 4)) { if ((res <= 0)) { return ERR_EOF; } else { *data_size = res + 8; } } return ERR_NOERROR; }
int load_MPA(media_entry *p) { int ret; long int tag_dim; int n,RowIndex, ColIndex; int BitrateMatrix[16][5] = { {0, 0, 0, 0, 0 }, {32000, 32000, 32000, 32000, 8000 }, {64000, 48000, 40000, 48000, 16000 }, {96000, 56000, 48000, 56000, 24000 }, {128000,64000, 56000, 64000, 32000 }, {160000,80000, 64000, 80000, 40000 }, {192000,96000, 80000, 96000, 48000 }, {224000,112000,96000, 112000,56000 }, {256000,128000,112000,128000,64000 }, {288000,160000,128000,144000,80000 }, {320000,192000,160000,160000,96000 }, {352000,224000,192000,176000,112000}, {384000,256000,224000,192000,128000}, {416000,320000,256000,224000,144000}, {448000,384000,320000,256000,160000}, {0, 0, 0, 0, 0 } }; fnc_log(FNC_LOG_DEBUG, "loading MPA...\n"); if ( (ret=mediaopen(p)) < 0) return ret; p->buff_size=0; if ( (p->buff_size = read(p->fd, p->buff_data, 4)) != 4) return ERR_PARSE; // shawill: look if ID3 tag is present if (!memcmp(p->buff_data, "ID3", 3)) { // ID3 tag present n = read_dim(p->fd, &tag_dim); // shawill: one day we will look also at this function printf("%ld\n",tag_dim); p->description.flags|=MED_ID3; p->description.tag_dim=tag_dim; lseek(p->fd,tag_dim,SEEK_CUR); if ( (p->buff_size = read(p->fd, p->buff_data, 4)) != 4) return ERR_PARSE; } //fstat(p->fd, &fdstat); //if ( !S_ISFIFO(fdstat.st_mode) ) { mediaclose(p); // close(p->fd); p->buff_size = 4; //} if (! ((p->buff_data[0]==0xff) && ((p->buff_data[1] & 0xe0)==0xe0))) return ERR_PARSE; switch (p->buff_data[1] & 0x1e) { /* Mpeg version and Level */ case 18: ColIndex = 4; break; /* Mpeg-2 L3 */ case 20: ColIndex = 4; break; /* Mpeg-2 L2 */ case 22: ColIndex = 3; break; /* Mpeg-2 L1 */ case 26: ColIndex = 2; break; /* Mpeg-1 L3 */ case 28: ColIndex = 1; break; /* Mpeg-1 L2 */ case 30: ColIndex = 0; break; /* Mpeg-1 L1 */ default: { return ERR_PARSE; } } RowIndex = (p->buff_data[2] & 0xf0) / 16; p->description.bitrate = BitrateMatrix[RowIndex][ColIndex]; p->description.flags|=MED_BITRATE; if (p->buff_data[1] & 0x08) { /* Mpeg-1 */ switch (p->buff_data[2] & 0x0c) { case 0x00: p->description.sample_rate=44100; break; case 0x04: p->description.sample_rate=48000; break; case 0x08: p->description.sample_rate=32000; break; default: { return ERR_PARSE; } } } else { /* Mpeg-2 */ // switch (sync3 & 0x0c) { switch (p->buff_data[2] & 0x0c) { case 0x00: p->description.sample_rate=22050; break; case 0x04: p->description.sample_rate=24000; break; case 0x08: p->description.sample_rate=16000; break; default: { return ERR_PARSE; } } } p->description.flags|=MED_SAMPLE_RATE; if ((p->buff_data[1] & 0x06) == 6) p->description.frame_len = 384; else p->description.frame_len = 1152; p->description.flags|=MED_FRAME_LEN; p->description.pkt_len=(double)p->description.frame_len/(double)p->description.sample_rate*1000; p->description.delta_mtime=p->description.pkt_len; p->description.flags|=MED_PKT_LEN; return ERR_NOERROR; }
int read_MPEG_video (media_entry *me, uint8 *data, uint32 *data_size, double *mtime, int *recallme, uint8 *marker) /* reads MPEG-1,2 Video */ { int ret; uint32 num_bytes; char *vsh1_1; #ifdef MPEG2VSHE char *vsh2_1; #endif uint32 count,flag=0,seq_head_pres=0; unsigned char *data_tmp; static_MPEG_video *s=NULL; uint32 wasSeeking=0; if (!(me->flags & ME_FD)) { if ( (ret=mediaopen(me)) < 0 ) return ret; s = (static_MPEG_video *) calloc (1, sizeof(static_MPEG_video)); me->stat = (void *) s; s->final_byte=0x00; s->std=TO_PROBE; s->fragmented=0; } else s = (static_MPEG_video *) me->stat; num_bytes = ((me->description).byte_per_pckt>261)?(me->description).byte_per_pckt:DEFAULT_BYTE_X_PKT; #if HAVE_ALLOCA data_tmp=(unsigned char *)alloca(65000); #else data_tmp=(unsigned char *)calloc(1,65000); #endif if (data_tmp==NULL) return ERR_ALLOC; if (s->std == MPEG_2) {/*the first time is TO_PROBE*/ #ifdef MPEG2VSHE *data_size = 8; #else *data_size = 4; #endif /* bytes for the video specific header */ } else { *data_size = 4; } if (s->std == TO_PROBE) { probe_standard(me,data_tmp,data_size,me->fd,&s->std); flag=1; seq_head_pres=1; } /*---- Random Access ----*/ if((me->description).msource!=live && me->play_offset!=me->prev_mstart_offset){ if(!me->description.bitrate){ //Bit Rate unavaible in sequence header, so i calculate it from scratch me->description.bitrate=(int)(s->data_total * 8 / (*mtime))*1000; } else{ s->fragmented=0; wasSeeking=1; if(!flag){ lseek(me->fd,0,SEEK_SET); probe_standard(me,data_tmp,data_size,me->fd,&s->std); seq_head_pres=1; } } count=random_access(me); lseek(me->fd,count,SEEK_SET); me->prev_mstart_offset=me->play_offset; } /*---- end Random Access ----*/ #if 0 if(num_bytes==0){ /*TODO:case 1 slice for pkt*/ do{ if(!flag){ if ( next_start_code(data_tmp,data_size,me->fd) == -1) { close(me->fd); #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } read(me->fd,&s->final_byte,1); data_tmp[*data_size]=s->final_byte; *data_size+=1; }else{ s->final_byte=data_tmp[*data_size-1]; } if (s->final_byte == 0xb3) { read_seq_head(me,data_tmp,data_size,me->fd,&s->final_byte,s->std); seq_head_pres=1; } if (s->final_byte == 0xb8) { read_gop_head(data_tmp,data_size,me->fd,&s->final_byte,&s->hours,&s->minutes,&s->seconds,&s->picture,s->std); } if (s->final_byte == 0x00) { read_picture_head(data_tmp,data_size,me->fd,&s->final_byte,&s->temp_ref,&s->vsh1,s->std); if (s->std == MPEG_2) { read_picture_coding_ext(data_tmp,data_size,me->fd,&s->final_byte,&s->vsh2); } } }while(s->final_byte>0xAF); read_slice(data_tmp,data_size,me->fd,&s->final_byte); s->vsh1.b=1; }/*end if(num_bytes==0)*/ else #endif if(!s->fragmented)/*num_bytes !=0 and slice was not fragmented*/{ char buf_aux[3]; int i; if(flag && wasSeeking==0) s->final_byte=data_tmp[*data_size-1]; else{ data_tmp[*data_size]=0x00; *data_size+=1; data_tmp[*data_size]=0x00; *data_size+=1; data_tmp[*data_size]=0x01; *data_size+=1; data_tmp[*data_size]=s->final_byte; *data_size+=1; } if(s->final_byte==0xb7){ if (next_start_code(data_tmp,data_size,me->fd)==-1){ /* If there aren't 3 more bytes we are at EOF */ #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } if(read(me->fd,&s->final_byte,1)<1) { #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } data_tmp[*data_size]=s->final_byte; *data_size+=1; } *recallme=1; while((s->final_byte > 0xAF || s->final_byte==0x00) && *recallme){ if (s->final_byte == 0xb3) { read_seq_head(me, data_tmp, data_size, me->fd, &s->final_byte, s->std); seq_head_pres=1; } if (s->final_byte == 0xb8) { read_gop_head(data_tmp, data_size, me->fd, &s->final_byte, &s->hours, &s->minutes, &s->seconds, &s->picture, s->std); } if (s->final_byte == 0x00) { read_picture_head(data_tmp, data_size, me->fd, &s->final_byte, &s->temp_ref, &s->vsh1, s->std); if (s->std == MPEG_2 && *data_size<num_bytes) { read_picture_coding_ext(data_tmp, data_size, me->fd, &s->final_byte, &s->vsh2); } } if (s->final_byte == 0xb7) {/*sequence end code*/ *recallme=0; s->fragmented=0; } while(s->final_byte==0xb2||s->final_byte==0xb5){ if(next_start_code(data_tmp,data_size,me->fd)<0) { #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } if(read(me->fd,&s->final_byte,1)<1) { #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } data_tmp[*data_size]=s->final_byte; *data_size+=1; } } // *data_size-=4; while(num_bytes > *data_size && *recallme){ // *data_size+=4; if ( read(me->fd,&buf_aux,3) <3){ /* If there aren't 3 more bytes we are at EOF */ // close(me->fd); #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } while ( !((buf_aux[0] == 0x00) && (buf_aux[1]==0x00) && (buf_aux[2]==0x01)) && *data_size <num_bytes) { data_tmp[*data_size]=buf_aux[0]; *data_size+=1; buf_aux[0]=buf_aux[1]; buf_aux[1]=buf_aux[2]; if ( read(me->fd,&buf_aux[2],1) <1){ // close(me->fd); #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } } for (i=0;i<3;i++) { data_tmp[*data_size]=buf_aux[i]; *data_size+=1; } if(read(me->fd,&s->final_byte,1)<1) { #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } data_tmp[*data_size]=s->final_byte; *data_size+=1; if ( ((buf_aux[0] == 0x00) && (buf_aux[1]==0x00) && (buf_aux[2]==0x01)) ) { if(((s->final_byte > 0xAF)||(s->final_byte==0x00)) ){/*TODO: 0xb7*/ *recallme = 0; // *data_size-=4; } else *recallme=1; s->fragmented=0; } else{ *recallme=1; s->fragmented=1; } }/*end while *data_size < num_bytes && *recallme*/ s->vsh1.b=1; if ( ((buf_aux[0] == 0x00) && (buf_aux[1]==0x00) && (buf_aux[2]==0x01)) ) *data_size-=4; }/*end else num_bytes!=0 and slice was not fragmented*/ else{/*num_bytes!=0 and slice was fragmented*/ char buf_aux[3]; int i; if ( read(me->fd,&buf_aux,3) <3){ /* If there aren't 3 more bytes we are at EOF */ // close(me->fd); #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } while ( !((buf_aux[0] == 0x00) && (buf_aux[1]==0x00) && (buf_aux[2]==0x01)) && *data_size <num_bytes) { data_tmp[*data_size]=buf_aux[0]; *data_size+=1; buf_aux[0]=buf_aux[1]; buf_aux[1]=buf_aux[2]; if ( read(me->fd,&buf_aux[2],1) <1){ // close(me->fd); #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } } for (i=0;i<3;i++) { data_tmp[*data_size]=buf_aux[i]; *data_size+=1; } if(read(me->fd,&s->final_byte,1)<1) { #if !HAVE_ALLOCA free(data_tmp); #endif return ERR_EOF; } data_tmp[*data_size]=s->final_byte; *data_size+=1; if ( ((buf_aux[0] == 0x00) && (buf_aux[1]==0x00) && (buf_aux[2]==0x01)) ) { if(((s->final_byte > 0xAF)||(s->final_byte==0x00)) ){/*TODO: 0xb7*/ *recallme = 0; } else *recallme=1; s->fragmented=0; *data_size-=4; } else{ *recallme=1; s->fragmented=1; } s->vsh1.b=0; } if ( me->description.msource != live ) *mtime = (s->hours * 3.6e6) + (s->minutes * 6e4) + (s->seconds * 1000) + (s->temp_ref*40) + (s->picture*40); s->data_total+=*data_size; if (s->std==MPEG_2 && !flag) { #ifdef MPEG2VSHE s->data_total-=8; s->vsh1.t=1; #else s->data_total-=4; s->vsh1.t=0; #endif } else { s->data_total-=4; s->vsh1.t=0; } s->vsh1.mbz=0; s->vsh1.an=0; s->vsh1.n=0; if (seq_head_pres) { s->vsh1.s=1; } else { s->vsh1.s=0; } // s->vsh1.b=1; if (!s->fragmented) { s->vsh1.e=1; } else { s->vsh1.e=0; } vsh1_1 = (char *)(&s->vsh1); /* to see the struct as a set of bytes */ data_tmp[0] = vsh1_1[3]; data_tmp[1] = vsh1_1[2]; data_tmp[2] = vsh1_1[1]; data_tmp[3] = vsh1_1[0]; #ifdef MPEG2VSHE if (s->std == MPEG_2 && !flag) { vsh2_1 = (char *)(&s->vsh2); data_tmp[4] = vsh2_1[3]; data_tmp[5] = vsh2_1[2]; data_tmp[6] = vsh2_1[1]; data_tmp[7] = vsh2_1[0]; } #endif flag=0; /*so it is just probed*/ memcpy(data, data_tmp, *data_size); #if !HAVE_ALLOCA free(data_tmp); #endif *marker=!(*recallme); return ERR_NOERROR; }
int read_MPEG_system(media_entry *me, uint8 *data,uint32 *data_size, double *mtime, int *recallme, uint8 *marker) { int ret; uint32 num_bytes; int count,count1,flag,packet_done=0,not_remove=0,time_set=0,pts_diff,clock,dts_present=0,audio_flag=0; float pkt_len; static_MPEG_system *s=NULL; if (!(me->flags & ME_FD)) { if ( (ret=mediaopen(me)) < 0 ) return ret; s = (static_MPEG_system *) calloc (1, sizeof(static_MPEG_system)); me->stat = (void *) s; s->final_byte=0x00; s->offset_flag=0; s->offset=0; s->new_dts=0; } else s = (static_MPEG_system *) me->stat; num_bytes = (me->description).byte_per_pckt; *data_size=0; count=count1=0; flag = 1; lseek(me->fd,s->data_total,SEEK_SET); /* At this point it should be right to find the nearest lower frame */ /* computing it from the value of mtime */ //*data=(unsigned char *)calloc(1,65000); /* and starting the reading from this */ //if (*data==NULL) { /* feature not yet implemented */ // return ERR_ALLOC; //} if ( next_start_code(data,data_size,me->fd) == -1) { close(me->fd); return ERR_EOF; } read(me->fd,&s->final_byte,1); data[*data_size]=s->final_byte; *data_size+=1; if (s->final_byte == 0xba) { read_pack_head(data,data_size,me->fd,&s->final_byte,&s->scr); } if (s->final_byte >= 0xbc) { if (s->final_byte == 0xc0) { audio_flag = 1; } read_packet_head(data,data_size,me->fd,&s->final_byte,&time_set,&s->pts,&s->dts,&dts_present,&s->pts_audio); if (s->new_dts != 1){ s->new_dts = dts_present; } if ( (!s->offset_flag) && (s->pts.pts!=0) ) { s->offset=s->pts.pts; s->offset_flag=1; } } if (num_bytes != 0) { time_set=0; while ( ((*data_size <= num_bytes) && (*recallme == 1)) || (!packet_done) ) { count = read_packet(data,data_size,me->fd,&s->final_byte); if (!packet_done) { not_remove = 1; } else { not_remove = 0; } packet_done = 1; next_start_code(data,data_size,me->fd); read(me->fd,&s->final_byte,1); data[*data_size]=s->final_byte; *data_size+=1; if ( (s->final_byte < 0xbc) && (*data_size <= num_bytes) ) { *recallme = 0; flag = 0; // next packet coudn't stay in the same rtp packet } } if (flag && !not_remove) { count+=4; lseek(me->fd,-count,SEEK_CUR); *data_size-=count; not_remove = 0; } else { lseek(me->fd,-4,SEEK_CUR); *data_size-=4; } s->data_total+=*data_size; clock = (me->description).clock_rate; if (!audio_flag){ *mtime = ((float)(s->pts.pts-s->offset)*1000)/(float)clock; } else { *mtime = ((float)(s->pts_audio.pts-s->offset)*1000)/(float)clock; } count=0; do { /* finds next packet */ count1=next_start_code(data,data_size,me->fd); count+=count1; count+=3; read(me->fd,&s->final_byte,1); data[*data_size]=s->final_byte; *data_size+=1; count+=1; } while ((s->final_byte < 0xbc) && (s->final_byte != 0xb9) ); if (s->final_byte == 0xb9) { s->data_total+=4; } else { count1=read_packet_head(data,data_size,me->fd,&s->final_byte,&time_set,&s->next_pts,&s->next_dts,&dts_present,&s->pts_audio); /* reads next packet head */ count += count1; *data_size-=count; if ( (s->pts.pts == s->next_pts.pts) || (s->final_byte == 0xbe) || (s->final_byte == 0xc0) || (s->offset==0)) { *recallme=1; } else { if (!dts_present){ /* dts_present now is referring to the next packet */ if (!s->new_dts){ pts_diff = s->next_pts.pts - s->pts.pts; } else { pts_diff = s->next_pts.pts - s->dts.pts; } } else { pts_diff = s->next_dts.pts - s->pts.pts; } pkt_len = (((float)pts_diff*1000)/(float)clock); changePacketLength(pkt_len,me); *recallme=0; s->new_dts = 0; } /* compute the delta_mtime */ me->description.delta_mtime = (s->next_pts.pts - s->pts.pts)*1000/(float)clock; *mtime=(s->scr.scr*1000)/(double)me->description.clock_rate; /* adjust SRC value to be passed as argument to the msec2tick and do not */ } *marker=!(*recallme); return ERR_NOERROR; } else { read_packet(data,data_size,me->fd,&s->final_byte); s->data_total+=*data_size; clock = (me->description).clock_rate; if (!audio_flag){ *mtime = ((float)(s->pts.pts-s->offset)*1000)/(float)clock; } else { *mtime = ((float)(s->pts_audio.pts-s->offset)*1000)/(float)clock; } count=0; time_set=0; do { count1=next_start_code(data,data_size,me->fd); count+=count1; count+=3; read(me->fd,&s->final_byte,1); data[*data_size]=s->final_byte; *data_size+=1; count+=1; } while ((s->final_byte < 0xbc) && (s->final_byte != 0xb9) ); if (s->final_byte == 0xb9) { s->data_total+=4; } else { count1=read_packet_head(data,data_size,me->fd,&s->final_byte,&time_set,&s->next_pts,&s->next_dts,&dts_present,&s->pts_audio); count += count1; *data_size-=count; if ( (s->pts.pts == s->next_pts.pts) || (s->final_byte == 0xbe) || (s->final_byte == 0xc0) || (s->offset==0)) { *recallme=1; } else { if (!dts_present){ /* dts_present now is referring to the next packet */ if (!s->new_dts){ pts_diff = s->next_pts.pts - s->pts.pts; } else { pts_diff = s->next_pts.pts - s->dts.pts; } } else { pts_diff = s->next_dts.pts - s->pts.pts; } pkt_len = (((float)pts_diff*1000)/(float)clock); changePacketLength(pkt_len,me); *recallme=0; s->new_dts = 0; } /* compute the delta_mtime */ me->description.delta_mtime = (s->next_pts.pts - s->pts.pts)*1000/(float)clock; *mtime=(s->scr.scr*1000)/(double)me->description.clock_rate; /* adjust SRC value to be passed as argument to the msec2tick and do not */ } *marker=!(*recallme); return ERR_NOERROR; } }