#include "stdafx.h"
#include "Demux.h"
#include "crc.h"
int i=0, j=0, k=0, s = 0;
int pat_total,sdt_total,nit_total,pmt_total[500]={0};
int Pat_count = 0, Sdt_count = 0,Nit_count = 0, Pmt_count[500]={0};
int pat_Sec_length_buf[256],sdt_Sec_length_buf[256],nit_Sec_length_buf[256],pmt_Sec_length_buf[500][256];
void *handle = NULL;
void *pPatFilter = NULL, *pSdtFilter = NULL, *pNitFilter = NULL,*pPmtFilter[500] = {NULL};
double ts_buf[500][1024]={0.0};
unsigned char *p1, *p2, *p3, *p4;
unsigned char pat_buf[256*1024], sdt_buf[256*1024],nit_buf[256*1024],pmt_buf[500][1024*256],pmt_number_buf[500];
void PmtFilter(void *handle, void* filter, unsigned char *buf, int32_t size, uint32_t lParam)
{
int t4= 0;
double program_number;
uint32_t crc32, ival;
unsigned int p =0;
unsigned short program_info_length;
unsigned short stream_type;
unsigned short ES_info_length;
unsigned char section_number= buf[6];
unsigned char last_section_number= buf[7];
unsigned char section_length=(buf[1]&0x0f<<8)|buf[2];
static unsigned int flag=0;
static unsigned char k4;
static unsigned char pmt_flag[500][256]={0};
for(i = 0;i<s;i++)
{
if( filter == pPmtFilter[i])
{
p = i;
break;
}
}
/* pat/pmt的尺寸不可能超过1024字节*/
if( 1024 < size || filter != pPmtFilter[p])
{
return ;
}
/*rcr校验*/
crc32 = GetCrc32( buf, size-4 );
memcpy( &ival, buf+size-4, 4 );
if( ival != htonl(crc32))
{
return ;
}
if(pmt_flag[p][section_number]==0)
{
if((k4 == section_number) && k4 <= last_section_number)
{
memcpy( &pmt_buf[p][section_number*1024], buf, size);
pmt_total[p] += size;
pmt_flag[p][section_number]=1;
Pmt_count[p]++;
pmt_Sec_length_buf[p][section_number] = size;
}
}
for(j = 0 ;j<s;j++)
{
for(i=0;i<s;i++)
{
for(k=0;k<Pmt_count[j];k++)
{
t4 = 0;
section_length = ((pmt_buf[i][k*1024+1]&0x0f)<<8) | pmt_buf[i][k*1024+2];
program_number = ((pmt_buf[i][k*1024+3]<<8) | pmt_buf[i][k*1024+4]);
if(fabs((ts_buf[j][8] - program_number)) <0.0001)
{
program_info_length = ((pmt_buf[i][k*1024+10]&0x0f)<<8) | pmt_buf[i][k*1024+11];
t4 = k*1024 + t4 + 12 + program_info_length;
/*获得音视频type and ID*/
while(t4 < section_length -1)
{
stream_type = pmt_buf[i][t4];
ES_info_length = ((pmt_buf[i][t4+3]&0x0f<<8)) | pmt_buf[i][t4+4];
/*向链表中对于相同service_id的节点添加video、audio信息*/
if(stream_type == 0x02)
{
ts_buf[j][2] = ((pmt_buf[i][t4+1]&0x1f)<<8) | pmt_buf[i][t4+2];
}
else if(stream_type == 0x04)
{
ts_buf[j][3] = ((pmt_buf[i][t4+1]&0x1f)<<8) | pmt_buf[i][t4+2];
}
/*回到节目流信息开始*/
t4 = t4 + 5 + ES_info_length;
}
}
}
}
}
}
/*解析PAT表*/
void PatFilter(void *handle, void *filter, unsigned char *buf, int32_t size, uint32_t lParam)
{
uint16_t Pmt_pid;
uint32_t crc32, ival;
unsigned short s1;
unsigned short program_number;
unsigned short transport_stream_id;
unsigned short section_number=buf[6];
unsigned short last_section_number= buf[7];
unsigned char *p,*q;
unsigned char section_length=(buf[1]&0x0f<<8)|buf[2];
static unsigned int flag=0;
static unsigned short k1 =0;
static unsigned char pat_flag[256]={0};
j=0;
p = pat_buf;
/* pat/pmt的尺寸不可能超过1024字节 */
if( 1024 < size || filter != pPatFilter )
{
return ;
}
/* CRC校验 */
crc32 = GetCrc32( buf, size-4 );
memcpy( &ival, buf+size-4, 4 );
if( ival != htonl(crc32))
{
return ;
}
/*transport_stream_id*/
transport_stream_id = (buf[3]<<8) | buf[4];
if(flag == 0)
{
p1= pat_buf;
}
if(pat_flag[section_number]==0)
{
if((k1 == section_number) && k1 <= last_section_number)
{
/* 保存PAT-SECTION */
memcpy( p1, buf, section_length+3);
p1 += section_length + 3;
flag = 1;
pat_total += section_length + 3;
pat_flag[section_number] = 1;
Pat_count++;
pat_Sec_length_buf[section_number] = section_length + 3;
k1++;
p = pat_buf;
q = p;
p += 8;
s1 = 12;
while(s1 < section_length + 3)
{
/*高低位转换,即program_number获取service_id*/
program_number = (p[0]<<8) | p[1];
p += 2;
if(program_number != 0x0)
{
/*由于reserved中有3位需要过滤,获取pid*/
Pmt_pid = ((p[0]&0x1f)<<8) | p[1];
/* 开始过滤PMT */
pPmtFilter[s] = Demux_CreateFilterEx(handle, Pmt_pid, 0x02, 0xff, 0, 0, 1024, PmtFilter, (uint32_t)0);
/*循环次数,即有多少个PMT表 */
s++;
}
p += 2;
s1 = s1+4;
}
}
}
for(k=0;k<Pat_count;k++)
{
p = p+8+k*1024;
for(i= 0;i<=s;i++)
{
if(0 != ((p[0]<<8) | p[1]))
{
ts_buf[j][0] = (pat_buf[3]<<8) | pat_buf[4];
ts_buf[j][8] = (p[0]<<8) | p[1];
ts_buf[j][1] = ((p[2]&0x1f)<<8) | p[3];
j++;
}
p = p+4;
}
}
}
void SdtFilter(void *handle, void* filter, unsigned char *buf, int32_t size, uint32_t lParam)
{
int t3=0;
int flag[500];
double transport_stream_id;
double service_id;
uint32_t crc32, ival;
unsigned short descriptors_loop_length;
unsigned short descriptors_tag;
unsigned short service_provider_name_length;
unsigned short service_name_length;
unsigned char section_number= buf[6];
unsigned char last_section_number= buf[7];
unsigned char section_length=(buf[1]&0x0f<<8)|buf[2];
static unsigned char k2;
static unsigned int flag1=0;
static unsigned char sdt_flag[256]={0};
/* pat/pmt的尺寸不可能超过1024字节 */
if( 1024 < size || filter != pSdtFilter )
{
return ;
}
/* CRC校验 */
crc32 = GetCrc32( buf, size-4 );
memcpy( &ival, buf+size-4, 4 );
if( ival != htonl(crc32))
{
return ;
}
if(flag1 == 0)
{
p2 = sdt_buf;
}
if(sdt_flag[section_number]==0)
{
if((k2 == section_number) && k2 <= last_section_number)
{
memcpy( p2, buf, section_length+3);
p2 += section_length + 3;
flag1 = 1;
sdt_total += section_length+3;
sdt_flag[section_number]=1;
Sdt_count++;
sdt_Sec_length_buf[section_number] = section_length + 3;
k2++;
}
}
for(j=0;j<s;j++)
{
flag[j] = 0;
for(i=0;i<s;i++)
{
for(k=0;k<Sdt_count;k++)
{
t3=0;
transport_stream_id = (sdt_buf[k*1024+3]<<8) | sdt_buf[k*1024+4];
section_length = (((sdt_buf[k*1024+1]&0x0f)<<8) | sdt_buf[k*1024+2])+3;
if(flag[j]==0)
{
if(fabs(ts_buf[j][0] - transport_stream_id)<0.0001)
{
t3 = k*1024+t3 +11;
while(t3<section_length+3)
{
/* service_id */
service_id = sdt_buf[t3]<<8 | sdt_buf[t3+1];
/* descriptor_loop_length */
descriptors_loop_length = ((sdt_buf[t3+3]&0x0f)<<8) | sdt_buf[t3+4];
if(fabs(ts_buf[j][8] - service_id)<0.0001)
{
descriptors_tag = sdt_buf[t3+5];
if(descriptors_tag == 0x48)
{
service_provider_name_length = sdt_buf[t3+8];
service_name_length = sdt_buf[t3+9+service_provider_name_length];
memcpy(&ts_buf[j][9],&sdt_buf[t3+10+service_provider_name_length],service_name_length);
*(((unsigned char*)&ts_buf[i][9])+service_name_length) = '\0';
}
flag[j] = 1;
break;
}
t3 = t3 + 5 + descriptors_loop_length;
}
}
}
}
}
}
}
/*解析NIT表*/
void NitFilter(void *handle, void* filter, unsigned char *buf, int32_t size, uint32_t lParam)
{ int t2;
double transport_stream_id;
uint32_t crc32, ival;
unsigned short descriptors_tag;
unsigned short modulation;
unsigned short transport_stream_loop_length;
unsigned short network_descriptors_length;
unsigned short transport_descriptors_length;
unsigned short section_length= ((buf[1]&0x0f)<<8) | buf[2];
unsigned char section_number= bu
- 1
- 2
- 3
- 4
- 5
前往页