TS流解析的第二个步骤是解析TS的关键也是难点,需要熟读文档ISO13181-1.pdf。下面我也会就我的解析代码做详尽的解析。
接着上一篇TS流解析之一解包长点击打开链接讲解了TS流解析的第一个步骤,我们已经解出了TS流的包长以及第一个有效包的位置信息。下面我们在之前的基础上增加代码:
1.在原函数ParseTs的基础上添加<span style="color: rgb(204, 0, 0); font-family: Arial, Helvetica, sans-serif;">ParseTable解析TS文件中各个表的信息:
<span style="font-family: Arial, Helvetica, sans-serif;">int ParseTs(FILE *pfTsFile)</span>
{
int iTsLength = 0;
int iTsPosition = 0;
int iReturn = 0;
//LIST_HEAD(ListDisplayHead);
iTsLength = ParseTsLength(pfTsFile, &iTsPosition);
printf("\nThe transport stream packet length is: %d\n", iTsLength);
printf("and the first packet position is: %d\n\n", iTsPosition);
iReturn = ParseTable(pfTsFile, iTsPosition, iTsLength);
return iReturn;
}
int ParseTable(FILE *pfTsFile, int iTsPosition, int iTsLength)
{
int iReturn = 0;
unsigned int uiUserPID = 0;
//Get PAT table
uiUserPID = 0x0000;
iReturn = GetTable(pfTsFile, iTsPosition, iTsLength, uiUserPID, PAT_TABLE_ID);
return 0;
}
讲解:
由文档ISO13181-1.pdf 32页PID table可知PAT的PID固定是0x0000,其中#define PAT_TABLE_ID 0x00,由56页table_id表可知:
3.获取某table的所有section函数GetTable:
int GetTable(FILE *pfTsFile, int iTsPosition, int iTsLength, unsigned int uiUserPID, unsigned int uiTableId)
{
int iReturn = 0;
unsigned int uiVersionNumber = 0xff;
unsigned int uiLastSectionNumber = 0;
unsigned int uiSectionNumberRecord[257] = { 0 };
unsigned char *ucSectionBuffer = (unsigned char *)malloc(sizeof(char) * 4096);
if (-1 == fseek(pfTsFile, iTsPosition, SEEK_SET))
{
printf("fseek in %d error(GetSection)\n", iTsPosition);
goto Ret;
}
while (feof(pfTsFile) == 0)
{
memset(ucSectionBuffer, 0, iTsLength);
iReturn = GetSection(pfTsFile, iTsLength, uiUserPID, uiTableId, &uiVersionNumber, ucSectionBuffer, uiSectionNumberRecord);
if (iReturn == -1)
{
break;
}
uiLastSectionNumber = ucSectionBuffer[7];
//find function to parse section
//FindFuncParseSection(ucSectionBuffer, uiUserPID, uiTableId);
if (uiSectionNumberRecord[256] > uiLastSectionNumber)
{
printf("find one of the table. Please wait to find all the table.\n");
break;
}
}
Ret:
free(ucSectionBuffer);
return iReturn;
}
文档原文: