📄 dvb_cademux.c
字号:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <cyg/io/io.h>#include <cyg/hal/types.h>#include <cyg/infra/diag.h>#include <cyg/kernel/kapi.h>#include "custom.h"#include "kinf.h"#include "sp_demux.h"#include "DVB_CADemux.h"static int DVB_CADemuxReadBody(ST_CA_DEMUX *pstDemux);static intDVB_CaDemuxReadHeader(ST_CA_DEMUX *pstDemux);extern unsigned long crc32(char *data, int len);void DVB_CAOpenChannel(ST_CA_DEMUX *pstDemux){ dmx_channel_config_t stConfig; dmx_filter_t stFilter; int i = 0; if(pstDemux == NULL) return; pstDemux->ucPollingState = MAX_POLLING_STATE; if(pstDemux->iChannelIndex >= 0) { DVB_CACloseChannel(pstDemux); } pstDemux->iChannelIndex = SP_Demux_OpenChannel(); if(pstDemux->iChannelIndex < 0) goto ERROR; if(SP_Demux_SetBufferSize(pstDemux->iChannelIndex, pstDemux->usMaxBufferLen*2) < 0) goto ERROR; stConfig.type = pstDemux->ucType; stConfig.pid = pstDemux->usPid; if(SP_Demux_SetChannelConfig(pstDemux->iChannelIndex, &stConfig) != 0) goto ERROR; for(i = 0; i < pstDemux->ucFilterNum; i++) { memset(&stFilter, 0, sizeof(dmx_filter_t)); if(pstDemux->ucFilterLen[i] > 0) { stFilter.length = pstDemux->ucFilterLen[i]; memcpy(stFilter.match, pstDemux->ucMatch[i], pstDemux->ucFilterLen[i]); memcpy(stFilter.mask, pstDemux->ucMask[i], pstDemux->ucFilterLen[i]); memcpy(stFilter.negate, pstDemux->ucNegate[i], pstDemux->ucFilterLen[i]); } if(SP_Demux_AddFilter(pstDemux->iChannelIndex, &stFilter) != 0) goto ERROR; } if(pstDemux->iChannelIndex >= 0) { pstDemux->usBufferLen = 0; pstDemux->usSectionLen = 0; memset(pstDemux->pucDemuxBuffer, 0x0, pstDemux->usMaxSectionLen); SP_Demux_StartChannel(pstDemux->iChannelIndex); pstDemux->ucPollingState = POLLING_HEADER; } else { ERRF(" channel startup failed\n"); } return;ERROR: DVB_CACloseChannel(pstDemux); pstDemux->iChannelIndex = -1;}void DVB_CACloseChannel(ST_CA_DEMUX *pstDemux){ if(pstDemux && pstDemux->iChannelIndex >= 0) { pstDemux->usBufferLen = 0; pstDemux->usSectionLen = 0; SP_Demux_StopChannel(pstDemux->iChannelIndex); SP_Demux_CloseChannel(pstDemux->iChannelIndex); pstDemux->iChannelIndex = -1; pstDemux->ucPollingState = MAX_POLLING_STATE; } }intDVB_CADemuxReadSection(ST_CA_DEMUX *pstDemux){ int iResult = DEMUX_SUCCESS; if(pstDemux == NULL) return DEMUX_ERROR; switch(pstDemux->ucPollingState) { case POLLING_HEADER: iResult = DVB_CaDemuxReadHeader(pstDemux); if(iResult != DEMUX_SUCCESS) break; case POLLING_BODY: iResult = DVB_CADemuxReadBody(pstDemux); if(iResult != DEMUX_SUCCESS) break; pstDemux->ucPollingState = POLLING_HEADER; break; default: break; } return iResult;}static int DVB_CADemuxReadBody(ST_CA_DEMUX *pstDemux){ UINT16 CurLen = 0; CurLen = SP_Demux_GetDataLength(pstDemux->iChannelIndex); if (CurLen == 0) return DEMUX_WAIT; if(CurLen > (pstDemux->usSectionLen + 3 - pstDemux->usBufferLen)) CurLen = (pstDemux->usSectionLen + 3 - pstDemux->usBufferLen); SP_Demux_ReadBuffer(pstDemux->iChannelIndex, pstDemux->pucDemuxBuffer + pstDemux->usBufferLen, CurLen, DMX_RD_NONBLOCK, 0); pstDemux->usBufferLen += CurLen; if(pstDemux->usBufferLen < (pstDemux->usSectionLen + 3)) { return DEMUX_WAIT; } if(pstDemux->ucNoCrc == 0) { if (crc32(pstDemux->pucDemuxBuffer, pstDemux->usSectionLen + 3)) { // ecos_printf("crc error!\n"); return DEMUX_ERROR; } } pstDemux->usBufferLen = 0; return DEMUX_SUCCESS;}static intDVB_CaDemuxReadHeader(ST_CA_DEMUX *pstDemux){ int iReadNum = 0; if(pstDemux == NULL) return DEMUX_ERROR; pstDemux->usBufferLen = 0; pstDemux->usSectionLen = 0; if((iReadNum = SP_Demux_ReadBuffer( pstDemux->iChannelIndex, pstDemux->pucDemuxBuffer, 3, DMX_RD_NONBLOCK, 0)) != 3) return DEMUX_WAIT; pstDemux->usSectionLen = (pstDemux->pucDemuxBuffer[1] & 0xf) << 8; pstDemux->usSectionLen += pstDemux->pucDemuxBuffer[2] & 0xff; if(pstDemux->usSectionLen + 3 > pstDemux->usMaxSectionLen) { return DEMUX_ERROR; } pstDemux->usBufferLen = 3; pstDemux->ucPollingState = POLLING_BODY; return DEMUX_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -