TS流解析之二获取完整section

本文详细介绍了TS流解析过程中获取完整section的步骤和策略,包括解析TS包,理解section_number和last_section_number的含义,以及如何判断是否处理完所有section。通过实例分析了section可能出现的不连续情况,并讨论了优化处理方法,特别是针对PAT、CAT等table的特殊情况。
摘要由CSDN通过智能技术生成

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;
}


2. ParseTable 函数定义(为简化起见这里带入PAT的PID进行解PAT table)
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;
}


文档原文:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值