📄 umc_mpeg2ts_spl.cpp
字号:
/*////////////////////////////////////////////////////////////////////////////////// INTEL CORPORATION PROPRIETARY INFORMATION// This software is supplied under the terms of a license agreement or// nondisclosure agreement with Intel Corporation and may not be copied// or disclosed except in accordance with the terms of that agreement.// Copyright(c) 2003-2005 Intel Corporation. All Rights Reserved.//*/#include "umc_mpeg2ts_spl.h"#include <ipps.h>#pragma warning(disable: 4244)using namespace UMC;Status MPEG2TSSplitter::InitProcessing(){ Status err = UMC_OK; unsigned char byteCode; ResetInfoFromPrevPES(); //splitter supports only 188 bytes length m_uiRegularPackSize = 188; if(m_systemStreamInfo.stream_type == MPEG2_TRANSPORT_STREAM) { m_uiNextSyncBytePos = 188; } else if(m_systemStreamInfo.stream_type == MPEG2_TRANSPORT_STREAM_TTS) { m_uiNextSyncBytePos = 192; } else VM_ASSERT(0); m_uiCurrentPackSize = 0; //size of packet can be 188 or 192 bytes //more allocated for sure in case corrupted sync byte if(!m_bIgnoreVideo) { err = InitVideoHelperBuffer(512); } if(UMC_OK == err) { CheckEndian(true); err = CheckTSSyncByte(byteCode); } return err;}Status MPEG2TSSplitter::CloseProcessing(){ if(m_lockedVideoBuffer.GetDataSize() > 16 && !m_bStop) { unsigned char* ptr = (unsigned char*)m_lockedVideoBuffer.GetBufferPointer(); int pos = m_lockedVideoBuffer.GetDataSize(); ptr[pos] = 0; ptr[pos+1] = 0; ptr[pos+2] = 1; ptr[pos+3] = 0xb7; m_lockedVideoBuffer.SetDataSize(m_lockedVideoBuffer.GetDataSize() + 4); if(m_uiEndian == 2) { m_lockedVideoBuffer.SetDataSize((m_lockedVideoBuffer.GetDataSize() + 15)&~15); //if start code was find make buffer size 16 bytes aligned //and swap data if needed Swap4Bytes((unsigned char*)m_lockedVideoBuffer.GetBufferPointer(), m_lockedVideoBuffer.GetDataSize()); } m_pVideoBuffer->UnLockInputBuffer(&m_lockedVideoBuffer); m_uiVideoFrameCount++; m_lockedVideoBuffer.SetTime(m_dCurrVideoPts, m_dCurrVideoDts); StoreLastPositionAndPTS(m_lockedVideoBuffer.GetTime(), m_pDataReader->GetPosition() - m_readVideoStream.GetDataSize()); } if(m_pTmpDataBuffAct) { ippsFree(m_pTmpDataBuffAct); m_pTmpDataBuffAct = NULL; } return UMC_OK;}Status MPEG2TSSplitter::ProcessOnePacket(int get_size){ Status err; unsigned char byteCode; byteCode = 0; err = MPEG2TSPack(); if(err != UMC_OK) return err; err = CheckTSSyncByte(byteCode); if (byteCode != ID_TS_SYNC_BYTE) { err = MoveToNextSyncWord(); if(err != UMC_OK) return err; err = CheckTSSyncByte(byteCode); if(err != UMC_OK) return err; } return err;}Status MPEG2TSSplitter::ProcessPackets(int count){ Status err; unsigned char byteCode; int packCount = 0; if(count == -1) err = InitProcessing(); if(err == UMC_OK) { do { //m_uiPosCurrPacket=m_pDataReader->GetPosition(); err = ProcessOnePacket(); packCount++; } while(!m_bStop && (err == UMC_OK) && ((count == -1) || (packCount < count))); } if((count == -1) || (err == UMC_END_OF_STREAM)) CloseProcessing(); return err;}Status MPEG2TSSplitter::CheckTSSyncByte(unsigned char &byteCode){ Status sts = UMC_BAD_STREAM; if(m_systemStreamInfo.stream_type == MPEG2_TRANSPORT_STREAM) { sts = m_pDataReader->CheckByte(&byteCode, 0); } else if(m_systemStreamInfo.stream_type == MPEG2_TRANSPORT_STREAM_TTS) { sts = m_pDataReader->MovePosition(4); if(sts != UMC_OK) return sts; sts = m_pDataReader->CheckByte(&byteCode, 0); } return sts;}Status MPEG2TSSplitter::MoveToNextSyncWord(){ //need to find first sync byte = 0x47 Status err; unsigned char byteCode; err = m_pDataReader->CheckByte(&byteCode, 0); if((err == UMC_OK) && (byteCode != ID_TS_SYNC_BYTE)) { do { err = m_pDataReader->GetByte(&byteCode); if(err != UMC_OK) break; err = m_pDataReader->CheckByte(&byteCode, 0); } while(!m_bStop && (err == UMC_OK) && (byteCode != ID_TS_SYNC_BYTE)); } if((byteCode == ID_TS_SYNC_BYTE) && (err == UMC_OK) && (m_systemStreamInfo.stream_type == MPEG2_TRANSPORT_STREAM_TTS)&& !m_bStop ) { err = m_pDataReader->MovePosition(-4); } return err;}Status MPEG2TSSplitter::ProcessPackets_back(unsigned int pid, vm_sizet p_packege_start, vm_sizet p_start, vm_sizet p_end){ Status err; unsigned int id=0; unsigned short len=0; vm_sizet curr_position; double timeStamp; int gotBytes = 0; bool isLoop=true; double dts; ElementaryStream* pCurrES = NULL; Status retBuff; do { retBuff = m_pVideoBuffer->LockInputBuffer(&m_lockedVideoBuffer); if(retBuff == UMC_NOT_ENOUGH_BUFFER) vm_time_sleep(5); } while(!m_bStop && retBuff == UMC_NOT_ENOUGH_BUFFER); if(UMC_OK == retBuff && m_pVideoAuxData) do { retBuff = m_pVideoAuxData->LockInputBuffer(&m_startCodesData); if(retBuff == UMC_NOT_ENOUGH_BUFFER) vm_time_sleep(5); } while(!m_bStop && retBuff == UMC_NOT_ENOUGH_BUFFER); /* m_dCurrVideoPts and m_dCurrVideoDts have been saved in GetPrevGOP function*/ m_lockedVideoBuffer.SetTime(m_dCurrVideoPts, m_dCurrVideoDts); ResetInfoFromPrevPES(); m_dPrevVideoPts = -1; m_uiStartCodePos = 0; m_uiStartCodeOff = 0; err =m_pDataReader->SetPosition(p_packege_start); if(err != UMC_OK) return err; while (isLoop&&(m_bStop == 0) ) { unsigned char byte_code=0; unsigned short short_code=0; unsigned short payload_unit_start_indicator, id, adaptation_field_control; int CurrentPackSize = m_uiRegularPackSize; unsigned int int_code; err = CheckTSSyncByte(byte_code); if(err != UMC_OK) return err; err = m_pDataReader->GetByte(&byte_code); if(err != UMC_OK) return err; VM_ASSERT(byte_code == ID_TS_SYNC_BYTE); CurrentPackSize --; err = m_pDataReader->GetShort(&short_code); if(err != UMC_OK) return err; CurrentPackSize -= 2; payload_unit_start_indicator = ((short_code >> 14) & 1); id = (short_code & ((1 << 13) - 1)); err = m_pDataReader->GetByte(&byte_code); if(err != UMC_OK) return err; CurrentPackSize --; adaptation_field_control = ((byte_code >> 4) & ((1 << 2) - 1)); if (id == pid ) { if (adaptation_field_control==3) { unsigned int hlen; err = m_pDataReader->GetByte(&byte_code); if(err != UMC_OK) return err; hlen = (unsigned int)byte_code; err = m_pDataReader->MovePosition(hlen); if(err != UMC_OK) return err; CurrentPackSize -= (hlen+1); } else if(adaptation_field_control==2) { err = m_pDataReader->MovePosition(CurrentPackSize); if(err != UMC_OK) return err; CurrentPackSize = 0; } gotBytes=0; curr_position = m_pDataReader->GetPosition(); if (CurrentPackSize>0) { if(payload_unit_start_indicator) { err = m_pDataReader->GetUInt(&int_code); if(err != UMC_OK) return err; VM_ASSERT ((int_code>>8)==0x000001); err = m_pDataReader->GetShort(&short_code); CurrentPackSize -= 6; if(err != UMC_OK) return err; err = MPEG2PESPacketHeader(&timeStamp, &dts, gotBytes); if(err != UMC_OK) return err; CurrentPackSize -=gotBytes; curr_position = m_pDataReader->GetPosition(); gotBytes = 0; GetVideoTime(&m_dCurrVideoPts, timeStamp); m_dCurrVideoDts = dts; } if (p_start>curr_position) { err =m_pDataReader->SetPosition(p_start); if(err != UMC_OK) return err; gotBytes = (int)(p_start - curr_position); } if (p_end<curr_position+CurrentPackSize) { gotBytes += curr_position+CurrentPackSize - p_end; isLoop = false; } int i = CheckIfNewES(false, id, -1); pCurrES = &m_pESArray[i]; if (CurrentPackSize>0) { switch (m_pESArray[0].m_videoStreamInfo.stream_type) { case MPEG1_VIDEO: case MPEG2_VIDEO: err = FillBufferMPEG2Video(pCurrES, CurrentPackSize - gotBytes, m_uiEndian); break; default: return UMC::UMC_NOT_IMPLEMENTED; } switch (err) { case UMC_OK: case UMC_NOT_ENOUGH_DATA: err = UMC_OK; break; default: return err; } } } } else { // this packege is missed err = m_pDataReader->MovePosition(CurrentPackSize); if(err != UMC_OK) return err; } if (!isLoop) break; err = MoveToNextSyncWord(); if(err != UMC_OK) return err; } return err;}Status MPEG2TSSplitter::GetPrevGop( unsigned int pid, vm_sizet p_start,vm_sizet &posIFrame,vm_sizet &posPackege){ vm_sizet PrevPESEndPosition=0; vm_sizet PrevPacketPosition=0; vm_sizet CurrPacketPosition=0; vm_sizet PacketPosition=0; vm_sizet start; vm_sizet end; vm_sizet saved_start; vm_sizet video_start; int CurrentPackSize=0; bool bFirstFrame = false; bool bSyncWord = true; unsigned char LastBytes[]={0xff,0xff,0xff,0xff,0xff,0xff}; Status err; int i=5; double dts; ElementaryStream* pCurrES = NULL; unsigned short payload_unit_start_indicator,adaptation_field_control; double currPTS=-1.0, currDTS=-1.0; double prevPTS=-1.0, prevDTS=-1.0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -