diff --git a/lib/nal.cpp b/lib/nal.cpp index dfbc34b4..41a7034d 100644 --- a/lib/nal.cpp +++ b/lib/nal.cpp @@ -1,14 +1,14 @@ #include "nal.h" -NAL_Unit::NAL_Unit( ) { - +NAL_Unit::NAL_Unit(){ + } -NAL_Unit::NAL_Unit( std::string & InputData ) { - ReadData( InputData ); +NAL_Unit::NAL_Unit(std::string & InputData){ + ReadData(InputData); } -bool NAL_Unit::ReadData( std::string & InputData ) { +bool NAL_Unit::ReadData(std::string & InputData){ std::string FullAnnexB; FullAnnexB += (char)0x00; FullAnnexB += (char)0x00; @@ -19,53 +19,66 @@ bool NAL_Unit::ReadData( std::string & InputData ) { ShortAnnexB += (char)0x00; ShortAnnexB += (char)0x01; // fprintf( stderr, "NAL_Unit::ReadData --- DataSize: %d\n", InputData.size() ); - if( InputData.size() < 3 ) { return false; } + if (InputData.size() < 3){ + return false; + } bool AnnexB = false; - if( InputData.substr(0,3) == ShortAnnexB ) { AnnexB = true; } - if( InputData.substr(0,4) == FullAnnexB ) { InputData.erase(0,1); AnnexB = true; } - if( AnnexB ) { + if (InputData.substr(0, 3) == ShortAnnexB){ + AnnexB = true; + } + if (InputData.substr(0, 4) == FullAnnexB){ + InputData.erase(0, 1); + AnnexB = true; + } + if (AnnexB){ MyData = ""; - InputData.erase(0,3);//Intro Bytes + InputData.erase(0, 3); //Intro Bytes bool FinalByteRead = false; - int Location = std::min( InputData.find( ShortAnnexB ), InputData.find( FullAnnexB ) ); - MyData = InputData.substr(0,Location); - InputData.erase(0,Location); - } else { - if( InputData.size() < 4 ) { return false; } + int Location = std::min(InputData.find(ShortAnnexB), InputData.find(FullAnnexB)); + MyData = InputData.substr(0, Location); + InputData.erase(0, Location); + }else{ + if (InputData.size() < 4){ + return false; + } int UnitLen = (InputData[0] << 24) + (InputData[1] << 16) + (InputData[2] << 8) + InputData[3]; - if( InputData.size() < 4+UnitLen ) { return false; } - InputData.erase(0,4);//Remove Length - MyData = InputData.substr(0,UnitLen); - InputData.erase(0,UnitLen);//Remove this unit from the string + if (InputData.size() < 4 + UnitLen){ + return false; + } + InputData.erase(0, 4); //Remove Length + MyData = InputData.substr(0, UnitLen); + InputData.erase(0, UnitLen); //Remove this unit from the string } return true; } -std::string NAL_Unit::AnnexB( bool LongIntro ) { +std::string NAL_Unit::AnnexB(bool LongIntro){ std::string Result; - if( MyData.size() ) { - if( LongIntro ) { Result += (char)0x00; } + if (MyData.size()){ + if (LongIntro){ + Result += (char)0x00; + } Result += (char)0x00; Result += (char)0x00; - Result += (char)0x01;//Annex B Lead-In + Result += (char)0x01; //Annex B Lead-In Result += MyData; } return Result; } -std::string NAL_Unit::SizePrepended( ) { +std::string NAL_Unit::SizePrepended(){ std::string Result; - if( MyData.size() ) { + if (MyData.size()){ int DataSize = MyData.size(); - Result += (char)( ( DataSize & 0xFF000000 ) >> 24 ); - Result += (char)( ( DataSize & 0x00FF0000 ) >> 16 ); - Result += (char)( ( DataSize & 0x0000FF00 ) >> 8 ); - Result += (char)( DataSize & 0x000000FF );//Size Lead-In + Result += (char)((DataSize & 0xFF000000) >> 24); + Result += (char)((DataSize & 0x00FF0000) >> 16); + Result += (char)((DataSize & 0x0000FF00) >> 8); + Result += (char)(DataSize & 0x000000FF); //Size Lead-In Result += MyData; } return Result; } -int NAL_Unit::Type( ) { - return ( MyData[0] & 0x1F ); +int NAL_Unit::Type(){ + return (MyData[0] & 0x1F); } diff --git a/lib/nal.h b/lib/nal.h index b129f479..c8307eaf 100644 --- a/lib/nal.h +++ b/lib/nal.h @@ -1,14 +1,15 @@ #include #include -class NAL_Unit { +class NAL_Unit{ public: - NAL_Unit( ); - NAL_Unit( std::string & InputData ); - bool ReadData( std::string & InputData ); - std::string AnnexB( bool LongIntro = false ); - std::string SizePrepended( ); - int Type( ); + NAL_Unit(); + NAL_Unit(std::string & InputData); + bool ReadData(std::string & InputData); + std::string AnnexB(bool LongIntro = false); + std::string SizePrepended(); + int Type(); private: std::string MyData; -};//NAL_Unit class +}; +//NAL_Unit class diff --git a/lib/ts_packet.cpp b/lib/ts_packet.cpp index 41040fa7..f8d580cb 100644 --- a/lib/ts_packet.cpp +++ b/lib/ts_packet.cpp @@ -5,61 +5,62 @@ /// This constructor creates an empty TS::Packet, ready for use for either reading or writing. /// All this constructor does is call TS::Packet::Clear(). -TS::Packet::Packet() { - strBuf.reserve( 188 ); - Clear( ); +TS::Packet::Packet(){ + strBuf.reserve(188); + Clear(); } /// This function fills a TS::Packet from provided Data. /// It fills the content with the first 188 bytes of Data. /// \param Data The data to be read into the packet. /// \return true if it was possible to read in a full packet, false otherwise. -bool TS::Packet::FromString( std::string & Data ) { - if( Data.size() < 188 ) { - return false; - } else { - strBuf = Data.substr(0,188); - Data.erase(0,188); +bool TS::Packet::FromString(std::string & Data){ + if (Data.size() < 188){ + return false; + }else{ + strBuf = Data.substr(0, 188); + Data.erase(0, 188); } return true; } /// The deconstructor deletes all space that may be occupied by a TS::Packet. -TS::Packet::~Packet() { } +TS::Packet::~Packet(){ +} /// Sets the PID of a single TS::Packet. /// \param NewPID The new PID of the packet. -void TS::Packet::PID( int NewPID ) { - strBuf[1] = (strBuf[1] & 0xE0) + ((NewPID & 0x1F00) >> 8 ); +void TS::Packet::PID(int NewPID){ + strBuf[1] = (strBuf[1] & 0xE0) + ((NewPID & 0x1F00) >> 8); strBuf[2] = (NewPID & 0x00FF); } /// Gets the PID of a single TS::Packet. /// \return The value of the PID. -int TS::Packet::PID() { - return (( strBuf[1] & 0x1F ) << 8 ) + strBuf[2]; +int TS::Packet::PID(){ + return ((strBuf[1] & 0x1F) << 8) + strBuf[2]; } /// Sets the Continuity Counter of a single TS::Packet. /// \param NewContinuity The new Continuity Counter of the packet. -void TS::Packet::ContinuityCounter( int NewContinuity ) { - strBuf[3] = ( strBuf[3] & 0xF0 ) + ( NewContinuity & 0x0F ); +void TS::Packet::ContinuityCounter(int NewContinuity){ + strBuf[3] = (strBuf[3] & 0xF0) + (NewContinuity & 0x0F); } /// Gets the Continuity Counter of a single TS::Packet. /// \return The value of the Continuity Counter. -int TS::Packet::ContinuityCounter() { - return ( strBuf[3] & 0x0F ); +int TS::Packet::ContinuityCounter(){ + return (strBuf[3] & 0x0F); } /// Gets the amount of bytes that are not written yet in a TS::Packet. /// \return The amount of bytes that can still be written to this packet. -int TS::Packet::BytesFree( ) { +int TS::Packet::BytesFree(){ return 188 - strBuf.size(); } /// Clears a TS::Packet. -void TS::Packet::Clear( ) { +void TS::Packet::Clear(){ strBuf.resize(4); strBuf[0] = 0x47; strBuf[1] = 0x00; @@ -72,11 +73,11 @@ void TS::Packet::Clear( ) { /// - 1: No AdaptationField. /// - 2: AdaptationField Only. /// - 3: AdaptationField followed by Data. -void TS::Packet::AdaptationField( int NewSelector ) { - strBuf[3] = ( strBuf[3] & 0xCF ) + ((NewSelector & 0x03) << 4); - if( NewSelector & 0x02 ) { +void TS::Packet::AdaptationField(int NewSelector){ + strBuf[3] = (strBuf[3] & 0xCF) + ((NewSelector & 0x03) << 4); + if (NewSelector & 0x02){ strBuf[4] = 0x00; - } else { + }else{ strBuf.resize(4); } } @@ -85,40 +86,40 @@ void TS::Packet::AdaptationField( int NewSelector ) { /// \return The existence of an adaptationfield. /// - 0: No adaptationfield present. /// - 1: Adaptationfield is present. -int TS::Packet::AdaptationField( ) { - return ((strBuf[3] & 0x30) >> 4 ); +int TS::Packet::AdaptationField(){ + return ((strBuf[3] & 0x30) >> 4); } /// Sets the PCR (Program Clock Reference) of a TS::Packet. /// \param NewVal The new PCR Value. -void TS::Packet::PCR( int64_t NewVal ) { - if( strBuf.size() < 12 ) { - strBuf.resize( 12 ); +void TS::Packet::PCR(int64_t NewVal){ + if (strBuf.size() < 12){ + strBuf.resize(12); } - AdaptationField( 3 ); + AdaptationField(3); strBuf[4] = 0x07; - strBuf[5] = (strBuf[5] | 0x10 ); + strBuf[5] = (strBuf[5] | 0x10); int64_t TmpVal = NewVal / 300; - strBuf[6] = (((TmpVal>>1)>>24) & 0xFF); - strBuf[7] = (((TmpVal>>1)>>16) & 0xFF); - strBuf[8] = (((TmpVal>>1)>>8) & 0xFF); - strBuf[9] = ((TmpVal>>1) & 0xFF); + strBuf[6] = (((TmpVal >> 1) >> 24) & 0xFF); + strBuf[7] = (((TmpVal >> 1) >> 16) & 0xFF); + strBuf[8] = (((TmpVal >> 1) >> 8) & 0xFF); + strBuf[9] = ((TmpVal >> 1) & 0xFF); int Remainder = NewVal % 300; - strBuf[10] = 0x7E + ((TmpVal & 0x01)<<7) + ((Remainder & 0x0100) >> 8 ); + strBuf[10] = 0x7E + ((TmpVal & 0x01) << 7) + ((Remainder & 0x0100) >> 8); strBuf[11] = (Remainder & 0x00FF); } /// Gets the PCR (Program Clock Reference) of a TS::Packet. /// \return The value of the PCR. -int64_t TS::Packet::PCR( ) { - if( !AdaptationField() ) { +int64_t TS::Packet::PCR(){ + if ( !AdaptationField()){ return -1; } - if( !(strBuf[5] & 0x10 ) ) { + if ( !(strBuf[5] & 0x10)){ return -1; } int64_t Result = 0; - Result = (((strBuf[6] << 24) + (strBuf[7] << 16) + (strBuf[8] << 8) + strBuf[9]) << 1) + ( strBuf[10] & 0x80 >> 7 ); + Result = (((strBuf[6] << 24) + (strBuf[7] << 16) + (strBuf[8] << 8) + strBuf[9]) << 1) + (strBuf[10] & 0x80 >> 7); Result = Result * 300; Result += ((strBuf[10] & 0x01) << 8 + strBuf[11]); return Result; @@ -126,26 +127,23 @@ int64_t TS::Packet::PCR( ) { /// Gets the current length of the adaptationfield. /// \return The length of the adaptationfield. -int TS::Packet::AdaptationFieldLen( ) { - if( !AdaptationField() ) { +int TS::Packet::AdaptationFieldLen(){ + if ( !AdaptationField()){ return -1; } return (int)strBuf[4]; } /// Prints a packet to stdout, for analyser purposes. -void TS::Packet::Print( ) { - std::cout << "TS Packet: " << (strBuf[0] == 0x47) - << "\n\tNewUnit: " << UnitStart() - << "\n\tPID: " << PID() - << "\n\tContinuity Counter: " << ContinuityCounter() - << "\n\tAdaption Field: " << AdaptationField() << "\n"; - if( AdaptationField() ) { +void TS::Packet::Print(){ + std::cout << "TS Packet: " << (strBuf[0] == 0x47) << "\n\tNewUnit: " << UnitStart() << "\n\tPID: " << PID() << "\n\tContinuity Counter: " + << ContinuityCounter() << "\n\tAdaption Field: " << AdaptationField() << "\n"; + if (AdaptationField()){ std::cout << "\t\tAdaption Field Length: " << AdaptationFieldLen() << "\n"; - if( AdaptationFieldLen() ) { + if (AdaptationFieldLen()){ std::cout << "\t\tRandom Access: " << RandomAccess() << "\n"; } - if( PCR() != -1 ) { + if (PCR() != -1){ std::cout << "\t\tPCR: " << PCR() << "( " << (double)PCR() / 27000000 << " s )\n"; } } @@ -153,166 +151,168 @@ void TS::Packet::Print( ) { /// Gets whether a new unit starts in this TS::Packet. /// \return The start of a new unit. -int TS::Packet::UnitStart( ) { - return ( strBuf[1] & 0x40) >> 6; +int TS::Packet::UnitStart(){ + return (strBuf[1] & 0x40) >> 6; } /// Sets the start of a new unit in this TS::Packet. /// \param NewVal The new value for the start of a unit. -void TS::Packet::UnitStart( int NewVal ) { - if( NewVal ) { +void TS::Packet::UnitStart(int NewVal){ + if (NewVal){ strBuf[1] |= 0x40; - } else { + }else{ strBuf[1] &= 0xBF; } } /// Gets whether this TS::Packet can be accessed at random (indicates keyframe). /// \return Whether or not this TS::Packet contains a keyframe. -int TS::Packet::RandomAccess( ) { - if( AdaptationField() < 2 ) { +int TS::Packet::RandomAccess(){ + if (AdaptationField() < 2){ return -1; } - return ( strBuf[5] & 0x40) >> 6; + return (strBuf[5] & 0x40) >> 6; } /// Sets whether this TS::Packet contains a keyframe /// \param NewVal Whether or not this TS::Packet contains a keyframe. -void TS::Packet::RandomAccess( int NewVal ) { - if( AdaptationField() == 3 ) { - if( strBuf.size() < 6 ) { +void TS::Packet::RandomAccess(int NewVal){ + if (AdaptationField() == 3){ + if (strBuf.size() < 6){ strBuf.resize(6); } - if( !strBuf[4] ) { - strBuf[4] = 1; + if ( !strBuf[4]){ + strBuf[4] = 1; } - if( NewVal ) { + if (NewVal){ strBuf[5] |= 0x40; - } else { + }else{ strBuf[5] &= 0xBF; } - } else { - if( strBuf.size() < 6 ) { + }else{ + if (strBuf.size() < 6){ strBuf.resize(6); } - AdaptationField( 3 ); + AdaptationField(3); strBuf[4] = 1; - if( NewVal ) { + if (NewVal){ strBuf[5] = 0x40; - } else { + }else{ strBuf[5] = 0x00; } } } /// Transforms the TS::Packet into a standard Program Association Table -void TS::Packet::DefaultPAT( ) { +void TS::Packet::DefaultPAT(){ static int MyCntr = 0; - strBuf = std::string( TS::PAT, 188 ); - ContinuityCounter( MyCntr ); - MyCntr = ( (MyCntr + 1) % 0x10); + strBuf = std::string(TS::PAT, 188); + ContinuityCounter(MyCntr); + MyCntr = ((MyCntr + 1) % 0x10); } /// Transforms the TS::Packet into a standard Program Mapping Table -void TS::Packet::DefaultPMT( ) { +void TS::Packet::DefaultPMT(){ static int MyCntr = 0; - strBuf = std::string( TS::PMT, 188 ); - ContinuityCounter( MyCntr ); - MyCntr = ( (MyCntr + 1) % 0x10); + strBuf = std::string(TS::PMT, 188); + ContinuityCounter(MyCntr); + MyCntr = ((MyCntr + 1) % 0x10); } /// Generates a string from the contents of the TS::Packet /// \return A string representation of the packet. -const char* TS::Packet::ToString( ) { - if( strBuf.size() != 188 ) { +const char* TS::Packet::ToString(){ + if (strBuf.size() != 188){ std::cerr << "Error: Size invalid (" << strBuf.size() << ") Invalid data from this point on." << std::endl; } - return strBuf.c_str( ); + return strBuf.c_str(); } /// Generates a PES Lead-in for a video frame. /// Starts at the first Free byte. /// \param NewLen The length of this video frame. -void TS::Packet::PESVideoLeadIn( int NewLen, long long unsigned int PTS ) { - NewLen += ( PTS == 1 ? 9 : 14 ); - strBuf += (char)0x00;//PacketStartCodePrefix - strBuf += (char)0x00;//PacketStartCodePrefix (Cont) - strBuf += (char)0x01;//PacketStartCodePrefix (Cont) - strBuf += (char)0xe0;//StreamType Video - strBuf += (char)((NewLen & 0xFF00) >> 8);//PES PacketLength - strBuf += (char)(NewLen & 0x00FF);//PES PacketLength (Cont) - strBuf += (char)0x80;//Reserved + Flags - if( PTS != 1 ) { - strBuf += (char)0x80;//PTSOnlyFlag + Flags - strBuf += (char)0x05;//PESHeaderDataLength - strBuf += (char)(0x20 + ((PTS & 0x1C0000000) >> 29 ) + 1);//PTS - strBuf += (char)((PTS & 0x03FC00000) >> 22 );//PTS (Cont) - strBuf += (char)(((PTS & 0x0003F8000) >> 14 ) + 1);//PTS (Cont) - strBuf += (char)((PTS & 0x000007F80) >> 7 );//PTS (Cont) - strBuf += (char)(((PTS & 0x00000007F) << 1) + 1);//PTS (Cont) - } else { - strBuf += (char)0x00;//PTSOnlyFlag + Flags - strBuf += (char)0x00;//PESHeaderDataLength +void TS::Packet::PESVideoLeadIn(int NewLen, long long unsigned int PTS){ + NewLen += (PTS == 1 ? 9 : 14); + strBuf += (char)0x00; //PacketStartCodePrefix + strBuf += (char)0x00; //PacketStartCodePrefix (Cont) + strBuf += (char)0x01; //PacketStartCodePrefix (Cont) + strBuf += (char)0xe0; //StreamType Video + strBuf += (char)((NewLen & 0xFF00) >> 8); //PES PacketLength + strBuf += (char)(NewLen & 0x00FF); //PES PacketLength (Cont) + strBuf += (char)0x80; //Reserved + Flags + if (PTS != 1){ + strBuf += (char)0x80; //PTSOnlyFlag + Flags + strBuf += (char)0x05; //PESHeaderDataLength + strBuf += (char)(0x20 + ((PTS & 0x1C0000000) >> 29) + 1); //PTS + strBuf += (char)((PTS & 0x03FC00000) >> 22); //PTS (Cont) + strBuf += (char)(((PTS & 0x0003F8000) >> 14) + 1); //PTS (Cont) + strBuf += (char)((PTS & 0x000007F80) >> 7); //PTS (Cont) + strBuf += (char)(((PTS & 0x00000007F) << 1) + 1); //PTS (Cont) + }else{ + strBuf += (char)0x00; //PTSOnlyFlag + Flags + strBuf += (char)0x00; //PESHeaderDataLength } //PesPacket-Wise Prepended Data - - strBuf += (char)0x00;//NALU StartCode - strBuf += (char)0x00;//NALU StartCode (Cont) - strBuf += (char)0x00;//NALU StartCode (Cont) - strBuf += (char)0x01;//NALU StartCode (Cont) - strBuf += (char)0x09;//NALU EndOfPacket (Einde Vorige Packet) - strBuf += (char)0xF0;//NALU EndOfPacket (Cont) + + strBuf += (char)0x00; //NALU StartCode + strBuf += (char)0x00; //NALU StartCode (Cont) + strBuf += (char)0x00; //NALU StartCode (Cont) + strBuf += (char)0x01; //NALU StartCode (Cont) + strBuf += (char)0x09; //NALU EndOfPacket (Einde Vorige Packet) + strBuf += (char)0xF0; //NALU EndOfPacket (Cont) } /// Generates a PES Lead-in for an audio frame. /// Starts at the first Free byte. /// \param NewLen The length of this audio frame. /// \param PTS The timestamp of the audio frame. -void TS::Packet::PESAudioLeadIn( int NewLen, uint64_t PTS ) { +void TS::Packet::PESAudioLeadIn(int NewLen, uint64_t PTS){ NewLen += 8; - strBuf += (char)0x00;//PacketStartCodePrefix - strBuf += (char)0x00;//PacketStartCodePrefix (Cont) - strBuf += (char)0x01;//PacketStartCodePrefix (Cont) - strBuf += (char)0xc0;//StreamType Audio - - strBuf += (char)((NewLen & 0xFF00) >> 8);//PES PacketLength - strBuf += (char)(NewLen & 0x00FF);//PES PacketLength (Cont) - strBuf += (char)0x80;//Reserved + Flags - strBuf += (char)0x80;//PTSOnlyFlag + Flags - strBuf += (char)0x05;//PESHeaderDataLength - strBuf += (char)(0x20 + ((PTS & 0x1C0000000) >> 29 ) + 1);//PTS - strBuf += (char)((PTS & 0x03FC00000) >> 22 );//PTS (Cont) - strBuf += (char)(((PTS & 0x0003F8000) >> 14 ) + 1);//PTS (Cont) - strBuf += (char)((PTS & 0x000007F80) >> 7 );//PTS (Cont) - strBuf += (char)(((PTS & 0x00000007F) << 1) + 1);//PTS (Cont) + strBuf += (char)0x00; //PacketStartCodePrefix + strBuf += (char)0x00; //PacketStartCodePrefix (Cont) + strBuf += (char)0x01; //PacketStartCodePrefix (Cont) + strBuf += (char)0xc0; //StreamType Audio + + strBuf += (char)((NewLen & 0xFF00) >> 8); //PES PacketLength + strBuf += (char)(NewLen & 0x00FF); //PES PacketLength (Cont) + strBuf += (char)0x80; //Reserved + Flags + strBuf += (char)0x80; //PTSOnlyFlag + Flags + strBuf += (char)0x05; //PESHeaderDataLength + strBuf += (char)(0x20 + ((PTS & 0x1C0000000) >> 29) + 1); //PTS + strBuf += (char)((PTS & 0x03FC00000) >> 22); //PTS (Cont) + strBuf += (char)(((PTS & 0x0003F8000) >> 14) + 1); //PTS (Cont) + strBuf += (char)((PTS & 0x000007F80) >> 7); //PTS (Cont) + strBuf += (char)(((PTS & 0x00000007F) << 1) + 1); //PTS (Cont) } /// Fills the free bytes of the TS::Packet. /// Stores as many bytes from NewVal as possible in the packet. /// \param NewVal The data to store in the packet. -void TS::Packet::FillFree( std::string & NewVal ) { - int toWrite = 188-strBuf.size(); - strBuf += NewVal.substr(0,toWrite); - NewVal.erase(0,toWrite); +void TS::Packet::FillFree(std::string & NewVal){ + int toWrite = 188 - strBuf.size(); + strBuf += NewVal.substr(0, toWrite); + NewVal.erase(0, toWrite); } /// Adds NumBytes of stuffing to the TS::Packet. /// \param NumBytes the amount of stuffing bytes. -void TS::Packet::AddStuffing( int NumBytes ) { - if( NumBytes <= 0 ) { return; } - if( AdaptationField( ) == 3 ) { +void TS::Packet::AddStuffing(int NumBytes){ + if (NumBytes <= 0){ + return; + } + if (AdaptationField() == 3){ int Offset = strBuf[4]; strBuf[4] = Offset + NumBytes - 1; - strBuf.resize(5+Offset+NumBytes-2); - for( int i = 0; i < ( NumBytes -2 ); i ++ ) { - strBuf[5+Offset+i] = 0xFF; + strBuf.resize(5 + Offset + NumBytes - 2); + for (int i = 0; i < (NumBytes - 2); i++){ + strBuf[5 + Offset + i] = 0xFF; } - } else { - AdaptationField( 3 ); + }else{ + AdaptationField(3); strBuf.resize(6); strBuf[4] = (char)(NumBytes - 1); strBuf[5] = (char)0x00; - for( int i = 0; i < ( NumBytes - 2 ); i ++ ) { + for (int i = 0; i < (NumBytes - 2); i++){ strBuf += (char)0xFF; } } diff --git a/lib/ts_packet.h b/lib/ts_packet.h index 4fb6a129..5525958b 100644 --- a/lib/ts_packet.h +++ b/lib/ts_packet.h @@ -15,99 +15,86 @@ /// Holds all TS processing related code. namespace TS { /// Class for reading and writing TS Streams - class Packet { + class Packet{ public: Packet(); ~Packet(); - bool FromString( std::string & Data ); - void PID( int NewPID ); + bool FromString(std::string & Data); + void PID(int NewPID); int PID(); - void ContinuityCounter( int NewContinuity ); + void ContinuityCounter(int NewContinuity); int ContinuityCounter(); void Clear(); - void PCR( int64_t NewVal ); + void PCR(int64_t NewVal); int64_t PCR(); - void AdaptationField( int NewVal ); - int AdaptationField( ); - int AdaptationFieldLen( ); + void AdaptationField(int NewVal); + int AdaptationField(); + int AdaptationFieldLen(); void DefaultPAT(); void DefaultPMT(); - int UnitStart( ); - void UnitStart( int NewVal ); - int RandomAccess( ); - void RandomAccess( int NewVal ); + int UnitStart(); + void UnitStart(int NewVal); + int RandomAccess(); + void RandomAccess(int NewVal); int BytesFree(); - + void Print(); const char* ToString(); - void PESVideoLeadIn( int NewLen, long long unsigned int PTS = 1 ); - void PESAudioLeadIn( int NewLen, uint64_t PTS = 0 ); - void FillFree( std::string & PackageData ); - void AddStuffing( int NumBytes ); + void PESVideoLeadIn(int NewLen, long long unsigned int PTS = 1); + void PESAudioLeadIn(int NewLen, uint64_t PTS = 0); + void FillFree(std::string & PackageData); + void AddStuffing(int NumBytes); private: //int Free; std::string strBuf; //char Buffer[188];///< The actual data - };//TS::Packet class - + }; + //TS::Packet class + /// Constructs an audio header to be used on each audio frame. /// The length of this header will ALWAYS be 7 bytes, and has to be /// prepended on each audio frame. /// \param FrameLen the length of the current audio frame. - static inline std::string GetAudioHeader( int FrameLen, std::string initData ) { - char StandardHeader[7] = {0xFF,0xF1,0x00,0x00,0x00,0x1F,0xFC}; + static inline std::string GetAudioHeader(int FrameLen, std::string initData){ + char StandardHeader[7] = {0xFF, 0xF1, 0x00, 0x00, 0x00, 0x1F, 0xFC}; FrameLen += 7; - StandardHeader[2] = ((((initData[0] >> 3) - 1) << 6 ) & 0xC0);//AAC Profile - 1 ( First two bits ) - StandardHeader[2] |= (( ((initData[0] & 0x07) << 1) | ((initData[1] >> 7) & 0x01) ) << 2 );//AAC Frequency Index - StandardHeader[2] |= ((initData[1] & 0x20) >> 5);//AAC Channel Config - StandardHeader[3] = ((initData[1] & 0x18 ) << 3 );//AAC CHannel Config (cont.) - StandardHeader[3] |= ( ( FrameLen & 0x00001800 ) >> 11 ); - StandardHeader[4] = ( ( FrameLen & 0x000007F8 ) >> 3 ); - StandardHeader[5] |= ( ( FrameLen & 0x00000007 ) << 5 ); - return std::string(StandardHeader,7); + StandardHeader[2] = ((((initData[0] >> 3) - 1) << 6) & 0xC0); //AAC Profile - 1 ( First two bits ) + StandardHeader[2] |= ((((initData[0] & 0x07) << 1) | ((initData[1] >> 7) & 0x01)) << 2); //AAC Frequency Index + StandardHeader[2] |= ((initData[1] & 0x20) >> 5); //AAC Channel Config + StandardHeader[3] = ((initData[1] & 0x18) << 3); //AAC CHannel Config (cont.) + StandardHeader[3] |= ((FrameLen & 0x00001800) >> 11); + StandardHeader[4] = ((FrameLen & 0x000007F8) >> 3); + StandardHeader[5] |= ((FrameLen & 0x00000007) << 5); + return std::string(StandardHeader, 7); } - + /// A standard Program Association Table, as generated by FFMPEG. /// Seems to be independent of the stream. - static char PAT[188] = { - 0x47,0x40,0x00,0x10, 0x00,0x00,0xB0,0x0D, 0x00,0x01,0xC1,0x00, 0x00,0x00,0x01,0xF0, - 0x00,0x2A,0xB1,0x04, 0xB2,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, - 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, - 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF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F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + /// A standard Program Mapping Table, as generated by FFMPEG. /// Contains both Audio and Video mappings, works also on video- or audio-only streams. - static char PMT[188] = { - 0x47,0x50,0x00,0x10, 0x00,0x02,0xB0,0x17, 0x00,0x01,0xC1,0x00, 0x00,0xE1,0x00,0xF0, - 0x00,0x1B,0xE1,0x00, 0xF0,0x00,0x0F,0xE1, 0x01,0xF0,0x00,0x2F, 0x44,0xB9,0x9B,0xFF, - 0xFF,0xFF,0xFF,0xFF, 0xFF,0x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0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + /// The full Bytesteam Nal-Header. - static char NalHeader[4] = { - 0x00,0x00,0x00,0x01 - }; - + static char NalHeader[4] = {0x00, 0x00, 0x00, 0x01}; + /// The shortened Bytesteam Nal-Header. - static char ShortNalHeader[3] = { - 0x00,0x00,0x01 - }; -};//TS namespace + static char ShortNalHeader[3] = {0x00, 0x00, 0x01}; +} +; +//TS namespace