/*****************************************************************************
* dvbpsi_PushPacket 数字电视中TS包解码函数注解
*****************************************************************************
* Injection of a TS packet into a PSI decoder.
*****************************************************************************/
void dvbpsi_PushPacket(dvbpsi_handle h_dvbpsi, uint8_t* p_data)
{
uint8_t i_expected_counter; /* 存储下一个数据包的连续计数器数字 */
dvbpsi_psi_section_t* p_section; /* 当前段指针 */
uint8_t* p_payload_pos; /* 当前位置指针 */
uint8_t* p_new_pos = NULL; /* Beginning of the new section,updated to NULL when the new section is handled */
int i_available; /* 包中可用字节数 */
/* TS start code */
if(p_data[0] != 0x47) //同步字节(sync byte:1B),应为0x47
{
DVBPSI_ERROR("PSI decoder", "not a TS packet");
return;
}
/* 连续检查 */
i_expected_counter = (h_dvbpsi->i_continuity_counter + 1) & 0xf; /*连续计数器累加1*/
h_dvbpsi->i_continuity_counter = p_data[3] & 0xf; //TS头部最后4比特为连续计数器
if(i_expected_counter != h_dvbpsi->i_continuity_counter) /*判断是否是连续传送的包*/
{
DVBPSI_ERROR_ARG("PSI decoder",
"TS discontinuity (received %d, expected %d)",
h_dvbpsi->i_continuity_counter, i_expected_counter);
h_dvbpsi->b_discontinuity = 1; //将不连续标志置1
if(h_dvbpsi->p_current_section) //删除当前段
{
dvbpsi_DeletePSISections(h_dvbpsi->p_current_section); //释放空间
h_dvbpsi->p_current_section = NULL;
}
}
/* 判断是否有有效负载,没有就返回*/
if(!(p_data[3] & 0x10)) //第28bit标识有无调整段
{
return;
}
if(p_data[3] & 0x20) //第27bit标识有无自适应区(adaption field)
p_payload_pos = p_data + 5 + p_data[4]; //5为包头4字节及1字节的自适应长度 p_data[4]自适应区长度
else
p_payload_pos = p_data + 4;
/* Unit start -> skip the pointer_field and a new section begins */
if(p_data[1] & 0x40) //即"payload unit start indicator"bit位 有效荷载单元起始批示符位,值为1表示存在确定的起始信息
{
p_new_pos = p_payload_pos + *p_payload_pos + 1; //指向包数据起始位置
p_payload_pos += 1; //跳过长度指示的1个字节
}
p_section = h_dvbpsi->p_current_section;
/* If the psi decoder needs a begginning of section and a new section
begins in the packet then initialize the dvbpsi_psi_section_t structure */
if(p_section == NULL)
{
if(p_new_pos)
{
/* 为结构分配内存空间 */
h_dvbpsi->p_current_section
= p_section
= dvbpsi_NewPSISection(h_dvbpsi->i_section_max_size);
/* 更新当前位置指针 */
p_payload_pos = p_new_pos;
/* 有指针指向新段 */
p_new_pos = NULL;
/* Just need the header to know how long is the section */
h_dvbpsi->i_need = 3; //在前3个字节中包含了表ID、section syntax indicator和长度批示,能确定是那种表,有多长
h_dvbpsi->b_complete_header = 0;
}
else
{
/* No new section => return */
return;
}
}
/* Remaining bytes in the payload */
i_available = 188 + p_data - p_