Standardized coding style for TS-related code.

This commit is contained in:
Thulinma 2012-12-31 16:33:45 +01:00
parent 439d5bf98c
commit 05416da269
4 changed files with 246 additions and 245 deletions

View file

@ -1,14 +1,14 @@
#include "nal.h" #include "nal.h"
NAL_Unit::NAL_Unit( ) { NAL_Unit::NAL_Unit(){
} }
NAL_Unit::NAL_Unit( std::string & InputData ) { NAL_Unit::NAL_Unit(std::string & InputData){
ReadData( InputData ); ReadData(InputData);
} }
bool NAL_Unit::ReadData( std::string & InputData ) { bool NAL_Unit::ReadData(std::string & InputData){
std::string FullAnnexB; std::string FullAnnexB;
FullAnnexB += (char)0x00; FullAnnexB += (char)0x00;
FullAnnexB += (char)0x00; FullAnnexB += (char)0x00;
@ -19,53 +19,66 @@ bool NAL_Unit::ReadData( std::string & InputData ) {
ShortAnnexB += (char)0x00; ShortAnnexB += (char)0x00;
ShortAnnexB += (char)0x01; ShortAnnexB += (char)0x01;
// fprintf( stderr, "NAL_Unit::ReadData --- DataSize: %d\n", InputData.size() ); // 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; bool AnnexB = false;
if( InputData.substr(0,3) == ShortAnnexB ) { AnnexB = true; } if (InputData.substr(0, 3) == ShortAnnexB){
if( InputData.substr(0,4) == FullAnnexB ) { InputData.erase(0,1); AnnexB = true; } AnnexB = true;
if( AnnexB ) { }
if (InputData.substr(0, 4) == FullAnnexB){
InputData.erase(0, 1);
AnnexB = true;
}
if (AnnexB){
MyData = ""; MyData = "";
InputData.erase(0,3);//Intro Bytes InputData.erase(0, 3); //Intro Bytes
bool FinalByteRead = false; bool FinalByteRead = false;
int Location = std::min( InputData.find( ShortAnnexB ), InputData.find( FullAnnexB ) ); int Location = std::min(InputData.find(ShortAnnexB), InputData.find(FullAnnexB));
MyData = InputData.substr(0,Location); MyData = InputData.substr(0, Location);
InputData.erase(0,Location); InputData.erase(0, Location);
} else { }else{
if( InputData.size() < 4 ) { return false; } if (InputData.size() < 4){
return false;
}
int UnitLen = (InputData[0] << 24) + (InputData[1] << 16) + (InputData[2] << 8) + InputData[3]; int UnitLen = (InputData[0] << 24) + (InputData[1] << 16) + (InputData[2] << 8) + InputData[3];
if( InputData.size() < 4+UnitLen ) { return false; } if (InputData.size() < 4 + UnitLen){
InputData.erase(0,4);//Remove Length return false;
MyData = InputData.substr(0,UnitLen); }
InputData.erase(0,UnitLen);//Remove this unit from the string InputData.erase(0, 4); //Remove Length
MyData = InputData.substr(0, UnitLen);
InputData.erase(0, UnitLen); //Remove this unit from the string
} }
return true; return true;
} }
std::string NAL_Unit::AnnexB( bool LongIntro ) { std::string NAL_Unit::AnnexB(bool LongIntro){
std::string Result; std::string Result;
if( MyData.size() ) { if (MyData.size()){
if( LongIntro ) { Result += (char)0x00; } if (LongIntro){
Result += (char)0x00;
}
Result += (char)0x00; 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; Result += MyData;
} }
return Result; return Result;
} }
std::string NAL_Unit::SizePrepended( ) { std::string NAL_Unit::SizePrepended(){
std::string Result; std::string Result;
if( MyData.size() ) { if (MyData.size()){
int DataSize = MyData.size(); int DataSize = MyData.size();
Result += (char)( ( DataSize & 0xFF000000 ) >> 24 ); Result += (char)((DataSize & 0xFF000000) >> 24);
Result += (char)( ( DataSize & 0x00FF0000 ) >> 16 ); Result += (char)((DataSize & 0x00FF0000) >> 16);
Result += (char)( ( DataSize & 0x0000FF00 ) >> 8 ); Result += (char)((DataSize & 0x0000FF00) >> 8);
Result += (char)( DataSize & 0x000000FF );//Size Lead-In Result += (char)(DataSize & 0x000000FF); //Size Lead-In
Result += MyData; Result += MyData;
} }
return Result; return Result;
} }
int NAL_Unit::Type( ) { int NAL_Unit::Type(){
return ( MyData[0] & 0x1F ); return (MyData[0] & 0x1F);
} }

View file

@ -1,14 +1,15 @@
#include <string> #include <string>
#include <cstdio> #include <cstdio>
class NAL_Unit { class NAL_Unit{
public: public:
NAL_Unit( ); NAL_Unit();
NAL_Unit( std::string & InputData ); NAL_Unit(std::string & InputData);
bool ReadData( std::string & InputData ); bool ReadData(std::string & InputData);
std::string AnnexB( bool LongIntro = false ); std::string AnnexB(bool LongIntro = false);
std::string SizePrepended( ); std::string SizePrepended();
int Type( ); int Type();
private: private:
std::string MyData; std::string MyData;
};//NAL_Unit class };
//NAL_Unit class

View file

@ -5,61 +5,62 @@
/// This constructor creates an empty TS::Packet, ready for use for either reading or writing. /// This constructor creates an empty TS::Packet, ready for use for either reading or writing.
/// All this constructor does is call TS::Packet::Clear(). /// All this constructor does is call TS::Packet::Clear().
TS::Packet::Packet() { TS::Packet::Packet(){
strBuf.reserve( 188 ); strBuf.reserve(188);
Clear( ); Clear();
} }
/// This function fills a TS::Packet from provided Data. /// This function fills a TS::Packet from provided Data.
/// It fills the content with the first 188 bytes of Data. /// It fills the content with the first 188 bytes of Data.
/// \param Data The data to be read into the packet. /// \param Data The data to be read into the packet.
/// \return true if it was possible to read in a full packet, false otherwise. /// \return true if it was possible to read in a full packet, false otherwise.
bool TS::Packet::FromString( std::string & Data ) { bool TS::Packet::FromString(std::string & Data){
if( Data.size() < 188 ) { if (Data.size() < 188){
return false; return false;
} else { }else{
strBuf = Data.substr(0,188); strBuf = Data.substr(0, 188);
Data.erase(0,188); Data.erase(0, 188);
} }
return true; return true;
} }
/// The deconstructor deletes all space that may be occupied by a TS::Packet. /// 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. /// Sets the PID of a single TS::Packet.
/// \param NewPID The new PID of the packet. /// \param NewPID The new PID of the packet.
void TS::Packet::PID( int NewPID ) { void TS::Packet::PID(int NewPID){
strBuf[1] = (strBuf[1] & 0xE0) + ((NewPID & 0x1F00) >> 8 ); strBuf[1] = (strBuf[1] & 0xE0) + ((NewPID & 0x1F00) >> 8);
strBuf[2] = (NewPID & 0x00FF); strBuf[2] = (NewPID & 0x00FF);
} }
/// Gets the PID of a single TS::Packet. /// Gets the PID of a single TS::Packet.
/// \return The value of the PID. /// \return The value of the PID.
int TS::Packet::PID() { int TS::Packet::PID(){
return (( strBuf[1] & 0x1F ) << 8 ) + strBuf[2]; return ((strBuf[1] & 0x1F) << 8) + strBuf[2];
} }
/// Sets the Continuity Counter of a single TS::Packet. /// Sets the Continuity Counter of a single TS::Packet.
/// \param NewContinuity The new Continuity Counter of the packet. /// \param NewContinuity The new Continuity Counter of the packet.
void TS::Packet::ContinuityCounter( int NewContinuity ) { void TS::Packet::ContinuityCounter(int NewContinuity){
strBuf[3] = ( strBuf[3] & 0xF0 ) + ( NewContinuity & 0x0F ); strBuf[3] = (strBuf[3] & 0xF0) + (NewContinuity & 0x0F);
} }
/// Gets the Continuity Counter of a single TS::Packet. /// Gets the Continuity Counter of a single TS::Packet.
/// \return The value of the Continuity Counter. /// \return The value of the Continuity Counter.
int TS::Packet::ContinuityCounter() { int TS::Packet::ContinuityCounter(){
return ( strBuf[3] & 0x0F ); return (strBuf[3] & 0x0F);
} }
/// Gets the amount of bytes that are not written yet in a TS::Packet. /// 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. /// \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(); return 188 - strBuf.size();
} }
/// Clears a TS::Packet. /// Clears a TS::Packet.
void TS::Packet::Clear( ) { void TS::Packet::Clear(){
strBuf.resize(4); strBuf.resize(4);
strBuf[0] = 0x47; strBuf[0] = 0x47;
strBuf[1] = 0x00; strBuf[1] = 0x00;
@ -72,11 +73,11 @@ void TS::Packet::Clear( ) {
/// - 1: No AdaptationField. /// - 1: No AdaptationField.
/// - 2: AdaptationField Only. /// - 2: AdaptationField Only.
/// - 3: AdaptationField followed by Data. /// - 3: AdaptationField followed by Data.
void TS::Packet::AdaptationField( int NewSelector ) { void TS::Packet::AdaptationField(int NewSelector){
strBuf[3] = ( strBuf[3] & 0xCF ) + ((NewSelector & 0x03) << 4); strBuf[3] = (strBuf[3] & 0xCF) + ((NewSelector & 0x03) << 4);
if( NewSelector & 0x02 ) { if (NewSelector & 0x02){
strBuf[4] = 0x00; strBuf[4] = 0x00;
} else { }else{
strBuf.resize(4); strBuf.resize(4);
} }
} }
@ -85,40 +86,40 @@ void TS::Packet::AdaptationField( int NewSelector ) {
/// \return The existence of an adaptationfield. /// \return The existence of an adaptationfield.
/// - 0: No adaptationfield present. /// - 0: No adaptationfield present.
/// - 1: Adaptationfield is present. /// - 1: Adaptationfield is present.
int TS::Packet::AdaptationField( ) { int TS::Packet::AdaptationField(){
return ((strBuf[3] & 0x30) >> 4 ); return ((strBuf[3] & 0x30) >> 4);
} }
/// Sets the PCR (Program Clock Reference) of a TS::Packet. /// Sets the PCR (Program Clock Reference) of a TS::Packet.
/// \param NewVal The new PCR Value. /// \param NewVal The new PCR Value.
void TS::Packet::PCR( int64_t NewVal ) { void TS::Packet::PCR(int64_t NewVal){
if( strBuf.size() < 12 ) { if (strBuf.size() < 12){
strBuf.resize( 12 ); strBuf.resize(12);
} }
AdaptationField( 3 ); AdaptationField(3);
strBuf[4] = 0x07; strBuf[4] = 0x07;
strBuf[5] = (strBuf[5] | 0x10 ); strBuf[5] = (strBuf[5] | 0x10);
int64_t TmpVal = NewVal / 300; int64_t TmpVal = NewVal / 300;
strBuf[6] = (((TmpVal>>1)>>24) & 0xFF); strBuf[6] = (((TmpVal >> 1) >> 24) & 0xFF);
strBuf[7] = (((TmpVal>>1)>>16) & 0xFF); strBuf[7] = (((TmpVal >> 1) >> 16) & 0xFF);
strBuf[8] = (((TmpVal>>1)>>8) & 0xFF); strBuf[8] = (((TmpVal >> 1) >> 8) & 0xFF);
strBuf[9] = ((TmpVal>>1) & 0xFF); strBuf[9] = ((TmpVal >> 1) & 0xFF);
int Remainder = NewVal % 300; 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); strBuf[11] = (Remainder & 0x00FF);
} }
/// Gets the PCR (Program Clock Reference) of a TS::Packet. /// Gets the PCR (Program Clock Reference) of a TS::Packet.
/// \return The value of the PCR. /// \return The value of the PCR.
int64_t TS::Packet::PCR( ) { int64_t TS::Packet::PCR(){
if( !AdaptationField() ) { if ( !AdaptationField()){
return -1; return -1;
} }
if( !(strBuf[5] & 0x10 ) ) { if ( !(strBuf[5] & 0x10)){
return -1; return -1;
} }
int64_t Result = 0; 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 = Result * 300;
Result += ((strBuf[10] & 0x01) << 8 + strBuf[11]); Result += ((strBuf[10] & 0x01) << 8 + strBuf[11]);
return Result; return Result;
@ -126,26 +127,23 @@ int64_t TS::Packet::PCR( ) {
/// Gets the current length of the adaptationfield. /// Gets the current length of the adaptationfield.
/// \return The length of the adaptationfield. /// \return The length of the adaptationfield.
int TS::Packet::AdaptationFieldLen( ) { int TS::Packet::AdaptationFieldLen(){
if( !AdaptationField() ) { if ( !AdaptationField()){
return -1; return -1;
} }
return (int)strBuf[4]; return (int)strBuf[4];
} }
/// Prints a packet to stdout, for analyser purposes. /// Prints a packet to stdout, for analyser purposes.
void TS::Packet::Print( ) { void TS::Packet::Print(){
std::cout << "TS Packet: " << (strBuf[0] == 0x47) std::cout << "TS Packet: " << (strBuf[0] == 0x47) << "\n\tNewUnit: " << UnitStart() << "\n\tPID: " << PID() << "\n\tContinuity Counter: "
<< "\n\tNewUnit: " << UnitStart() << ContinuityCounter() << "\n\tAdaption Field: " << AdaptationField() << "\n";
<< "\n\tPID: " << PID() if (AdaptationField()){
<< "\n\tContinuity Counter: " << ContinuityCounter()
<< "\n\tAdaption Field: " << AdaptationField() << "\n";
if( AdaptationField() ) {
std::cout << "\t\tAdaption Field Length: " << AdaptationFieldLen() << "\n"; std::cout << "\t\tAdaption Field Length: " << AdaptationFieldLen() << "\n";
if( AdaptationFieldLen() ) { if (AdaptationFieldLen()){
std::cout << "\t\tRandom Access: " << RandomAccess() << "\n"; std::cout << "\t\tRandom Access: " << RandomAccess() << "\n";
} }
if( PCR() != -1 ) { if (PCR() != -1){
std::cout << "\t\tPCR: " << PCR() << "( " << (double)PCR() / 27000000 << " s )\n"; 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. /// Gets whether a new unit starts in this TS::Packet.
/// \return The start of a new unit. /// \return The start of a new unit.
int TS::Packet::UnitStart( ) { int TS::Packet::UnitStart(){
return ( strBuf[1] & 0x40) >> 6; return (strBuf[1] & 0x40) >> 6;
} }
/// Sets the start of a new unit in this TS::Packet. /// Sets the start of a new unit in this TS::Packet.
/// \param NewVal The new value for the start of a unit. /// \param NewVal The new value for the start of a unit.
void TS::Packet::UnitStart( int NewVal ) { void TS::Packet::UnitStart(int NewVal){
if( NewVal ) { if (NewVal){
strBuf[1] |= 0x40; strBuf[1] |= 0x40;
} else { }else{
strBuf[1] &= 0xBF; strBuf[1] &= 0xBF;
} }
} }
/// Gets whether this TS::Packet can be accessed at random (indicates keyframe). /// Gets whether this TS::Packet can be accessed at random (indicates keyframe).
/// \return Whether or not this TS::Packet contains a keyframe. /// \return Whether or not this TS::Packet contains a keyframe.
int TS::Packet::RandomAccess( ) { int TS::Packet::RandomAccess(){
if( AdaptationField() < 2 ) { if (AdaptationField() < 2){
return -1; return -1;
} }
return ( strBuf[5] & 0x40) >> 6; return (strBuf[5] & 0x40) >> 6;
} }
/// Sets whether this TS::Packet contains a keyframe /// Sets whether this TS::Packet contains a keyframe
/// \param NewVal Whether or not this TS::Packet contains a keyframe. /// \param NewVal Whether or not this TS::Packet contains a keyframe.
void TS::Packet::RandomAccess( int NewVal ) { void TS::Packet::RandomAccess(int NewVal){
if( AdaptationField() == 3 ) { if (AdaptationField() == 3){
if( strBuf.size() < 6 ) { if (strBuf.size() < 6){
strBuf.resize(6); strBuf.resize(6);
} }
if( !strBuf[4] ) { if ( !strBuf[4]){
strBuf[4] = 1; strBuf[4] = 1;
} }
if( NewVal ) { if (NewVal){
strBuf[5] |= 0x40; strBuf[5] |= 0x40;
} else { }else{
strBuf[5] &= 0xBF; strBuf[5] &= 0xBF;
} }
} else { }else{
if( strBuf.size() < 6 ) { if (strBuf.size() < 6){
strBuf.resize(6); strBuf.resize(6);
} }
AdaptationField( 3 ); AdaptationField(3);
strBuf[4] = 1; strBuf[4] = 1;
if( NewVal ) { if (NewVal){
strBuf[5] = 0x40; strBuf[5] = 0x40;
} else { }else{
strBuf[5] = 0x00; strBuf[5] = 0x00;
} }
} }
} }
/// Transforms the TS::Packet into a standard Program Association Table /// Transforms the TS::Packet into a standard Program Association Table
void TS::Packet::DefaultPAT( ) { void TS::Packet::DefaultPAT(){
static int MyCntr = 0; static int MyCntr = 0;
strBuf = std::string( TS::PAT, 188 ); strBuf = std::string(TS::PAT, 188);
ContinuityCounter( MyCntr ); ContinuityCounter(MyCntr);
MyCntr = ( (MyCntr + 1) % 0x10); MyCntr = ((MyCntr + 1) % 0x10);
} }
/// Transforms the TS::Packet into a standard Program Mapping Table /// Transforms the TS::Packet into a standard Program Mapping Table
void TS::Packet::DefaultPMT( ) { void TS::Packet::DefaultPMT(){
static int MyCntr = 0; static int MyCntr = 0;
strBuf = std::string( TS::PMT, 188 ); strBuf = std::string(TS::PMT, 188);
ContinuityCounter( MyCntr ); ContinuityCounter(MyCntr);
MyCntr = ( (MyCntr + 1) % 0x10); MyCntr = ((MyCntr + 1) % 0x10);
} }
/// Generates a string from the contents of the TS::Packet /// Generates a string from the contents of the TS::Packet
/// \return A string representation of the packet. /// \return A string representation of the packet.
const char* TS::Packet::ToString( ) { const char* TS::Packet::ToString(){
if( strBuf.size() != 188 ) { if (strBuf.size() != 188){
std::cerr << "Error: Size invalid (" << strBuf.size() << ") Invalid data from this point on." << std::endl; 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. /// Generates a PES Lead-in for a video frame.
/// Starts at the first Free byte. /// Starts at the first Free byte.
/// \param NewLen The length of this video frame. /// \param NewLen The length of this video frame.
void TS::Packet::PESVideoLeadIn( int NewLen, long long unsigned int PTS ) { void TS::Packet::PESVideoLeadIn(int NewLen, long long unsigned int PTS){
NewLen += ( PTS == 1 ? 9 : 14 ); NewLen += (PTS == 1 ? 9 : 14);
strBuf += (char)0x00;//PacketStartCodePrefix strBuf += (char)0x00; //PacketStartCodePrefix
strBuf += (char)0x00;//PacketStartCodePrefix (Cont) strBuf += (char)0x00; //PacketStartCodePrefix (Cont)
strBuf += (char)0x01;//PacketStartCodePrefix (Cont) strBuf += (char)0x01; //PacketStartCodePrefix (Cont)
strBuf += (char)0xe0;//StreamType Video strBuf += (char)0xe0; //StreamType Video
strBuf += (char)((NewLen & 0xFF00) >> 8);//PES PacketLength strBuf += (char)((NewLen & 0xFF00) >> 8); //PES PacketLength
strBuf += (char)(NewLen & 0x00FF);//PES PacketLength (Cont) strBuf += (char)(NewLen & 0x00FF); //PES PacketLength (Cont)
strBuf += (char)0x80;//Reserved + Flags strBuf += (char)0x80; //Reserved + Flags
if( PTS != 1 ) { if (PTS != 1){
strBuf += (char)0x80;//PTSOnlyFlag + Flags strBuf += (char)0x80; //PTSOnlyFlag + Flags
strBuf += (char)0x05;//PESHeaderDataLength strBuf += (char)0x05; //PESHeaderDataLength
strBuf += (char)(0x20 + ((PTS & 0x1C0000000) >> 29 ) + 1);//PTS strBuf += (char)(0x20 + ((PTS & 0x1C0000000) >> 29) + 1); //PTS
strBuf += (char)((PTS & 0x03FC00000) >> 22 );//PTS (Cont) strBuf += (char)((PTS & 0x03FC00000) >> 22); //PTS (Cont)
strBuf += (char)(((PTS & 0x0003F8000) >> 14 ) + 1);//PTS (Cont) strBuf += (char)(((PTS & 0x0003F8000) >> 14) + 1); //PTS (Cont)
strBuf += (char)((PTS & 0x000007F80) >> 7 );//PTS (Cont) strBuf += (char)((PTS & 0x000007F80) >> 7); //PTS (Cont)
strBuf += (char)(((PTS & 0x00000007F) << 1) + 1);//PTS (Cont) strBuf += (char)(((PTS & 0x00000007F) << 1) + 1); //PTS (Cont)
} else { }else{
strBuf += (char)0x00;//PTSOnlyFlag + Flags strBuf += (char)0x00; //PTSOnlyFlag + Flags
strBuf += (char)0x00;//PESHeaderDataLength strBuf += (char)0x00; //PESHeaderDataLength
} }
//PesPacket-Wise Prepended Data //PesPacket-Wise Prepended Data
strBuf += (char)0x00;//NALU StartCode strBuf += (char)0x00; //NALU StartCode
strBuf += (char)0x00;//NALU StartCode (Cont) strBuf += (char)0x00; //NALU StartCode (Cont)
strBuf += (char)0x00;//NALU StartCode (Cont) strBuf += (char)0x00; //NALU StartCode (Cont)
strBuf += (char)0x01;//NALU StartCode (Cont) strBuf += (char)0x01; //NALU StartCode (Cont)
strBuf += (char)0x09;//NALU EndOfPacket (Einde Vorige Packet) strBuf += (char)0x09; //NALU EndOfPacket (Einde Vorige Packet)
strBuf += (char)0xF0;//NALU EndOfPacket (Cont) strBuf += (char)0xF0; //NALU EndOfPacket (Cont)
} }
/// Generates a PES Lead-in for an audio frame. /// Generates a PES Lead-in for an audio frame.
/// Starts at the first Free byte. /// Starts at the first Free byte.
/// \param NewLen The length of this audio frame. /// \param NewLen The length of this audio frame.
/// \param PTS The timestamp of the 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; NewLen += 8;
strBuf += (char)0x00;//PacketStartCodePrefix strBuf += (char)0x00; //PacketStartCodePrefix
strBuf += (char)0x00;//PacketStartCodePrefix (Cont) strBuf += (char)0x00; //PacketStartCodePrefix (Cont)
strBuf += (char)0x01;//PacketStartCodePrefix (Cont) strBuf += (char)0x01; //PacketStartCodePrefix (Cont)
strBuf += (char)0xc0;//StreamType Audio strBuf += (char)0xc0; //StreamType Audio
strBuf += (char)((NewLen & 0xFF00) >> 8);//PES PacketLength strBuf += (char)((NewLen & 0xFF00) >> 8); //PES PacketLength
strBuf += (char)(NewLen & 0x00FF);//PES PacketLength (Cont) strBuf += (char)(NewLen & 0x00FF); //PES PacketLength (Cont)
strBuf += (char)0x80;//Reserved + Flags strBuf += (char)0x80; //Reserved + Flags
strBuf += (char)0x80;//PTSOnlyFlag + Flags strBuf += (char)0x80; //PTSOnlyFlag + Flags
strBuf += (char)0x05;//PESHeaderDataLength strBuf += (char)0x05; //PESHeaderDataLength
strBuf += (char)(0x20 + ((PTS & 0x1C0000000) >> 29 ) + 1);//PTS strBuf += (char)(0x20 + ((PTS & 0x1C0000000) >> 29) + 1); //PTS
strBuf += (char)((PTS & 0x03FC00000) >> 22 );//PTS (Cont) strBuf += (char)((PTS & 0x03FC00000) >> 22); //PTS (Cont)
strBuf += (char)(((PTS & 0x0003F8000) >> 14 ) + 1);//PTS (Cont) strBuf += (char)(((PTS & 0x0003F8000) >> 14) + 1); //PTS (Cont)
strBuf += (char)((PTS & 0x000007F80) >> 7 );//PTS (Cont) strBuf += (char)((PTS & 0x000007F80) >> 7); //PTS (Cont)
strBuf += (char)(((PTS & 0x00000007F) << 1) + 1);//PTS (Cont) strBuf += (char)(((PTS & 0x00000007F) << 1) + 1); //PTS (Cont)
} }
/// Fills the free bytes of the TS::Packet. /// Fills the free bytes of the TS::Packet.
/// Stores as many bytes from NewVal as possible in the packet. /// Stores as many bytes from NewVal as possible in the packet.
/// \param NewVal The data to store in the packet. /// \param NewVal The data to store in the packet.
void TS::Packet::FillFree( std::string & NewVal ) { void TS::Packet::FillFree(std::string & NewVal){
int toWrite = 188-strBuf.size(); int toWrite = 188 - strBuf.size();
strBuf += NewVal.substr(0,toWrite); strBuf += NewVal.substr(0, toWrite);
NewVal.erase(0,toWrite); NewVal.erase(0, toWrite);
} }
/// Adds NumBytes of stuffing to the TS::Packet. /// Adds NumBytes of stuffing to the TS::Packet.
/// \param NumBytes the amount of stuffing bytes. /// \param NumBytes the amount of stuffing bytes.
void TS::Packet::AddStuffing( int NumBytes ) { void TS::Packet::AddStuffing(int NumBytes){
if( NumBytes <= 0 ) { return; } if (NumBytes <= 0){
if( AdaptationField( ) == 3 ) { return;
}
if (AdaptationField() == 3){
int Offset = strBuf[4]; int Offset = strBuf[4];
strBuf[4] = Offset + NumBytes - 1; strBuf[4] = Offset + NumBytes - 1;
strBuf.resize(5+Offset+NumBytes-2); strBuf.resize(5 + Offset + NumBytes - 2);
for( int i = 0; i < ( NumBytes -2 ); i ++ ) { for (int i = 0; i < (NumBytes - 2); i++){
strBuf[5+Offset+i] = 0xFF; strBuf[5 + Offset + i] = 0xFF;
} }
} else { }else{
AdaptationField( 3 ); AdaptationField(3);
strBuf.resize(6); strBuf.resize(6);
strBuf[4] = (char)(NumBytes - 1); strBuf[4] = (char)(NumBytes - 1);
strBuf[5] = (char)0x00; strBuf[5] = (char)0x00;
for( int i = 0; i < ( NumBytes - 2 ); i ++ ) { for (int i = 0; i < (NumBytes - 2); i++){
strBuf += (char)0xFF; strBuf += (char)0xFF;
} }
} }

View file

@ -15,99 +15,86 @@
/// Holds all TS processing related code. /// Holds all TS processing related code.
namespace TS { namespace TS {
/// Class for reading and writing TS Streams /// Class for reading and writing TS Streams
class Packet { class Packet{
public: public:
Packet(); Packet();
~Packet(); ~Packet();
bool FromString( std::string & Data ); bool FromString(std::string & Data);
void PID( int NewPID ); void PID(int NewPID);
int PID(); int PID();
void ContinuityCounter( int NewContinuity ); void ContinuityCounter(int NewContinuity);
int ContinuityCounter(); int ContinuityCounter();
void Clear(); void Clear();
void PCR( int64_t NewVal ); void PCR(int64_t NewVal);
int64_t PCR(); int64_t PCR();
void AdaptationField( int NewVal ); void AdaptationField(int NewVal);
int AdaptationField( ); int AdaptationField();
int AdaptationFieldLen( ); int AdaptationFieldLen();
void DefaultPAT(); void DefaultPAT();
void DefaultPMT(); void DefaultPMT();
int UnitStart( ); int UnitStart();
void UnitStart( int NewVal ); void UnitStart(int NewVal);
int RandomAccess( ); int RandomAccess();
void RandomAccess( int NewVal ); void RandomAccess(int NewVal);
int BytesFree(); int BytesFree();
void Print(); void Print();
const char* ToString(); const char* ToString();
void PESVideoLeadIn( int NewLen, long long unsigned int PTS = 1 ); void PESVideoLeadIn(int NewLen, long long unsigned int PTS = 1);
void PESAudioLeadIn( int NewLen, uint64_t PTS = 0 ); void PESAudioLeadIn(int NewLen, uint64_t PTS = 0);
void FillFree( std::string & PackageData ); void FillFree(std::string & PackageData);
void AddStuffing( int NumBytes ); void AddStuffing(int NumBytes);
private: private:
//int Free; //int Free;
std::string strBuf; std::string strBuf;
//char Buffer[188];///< The actual data //char Buffer[188];///< The actual data
};//TS::Packet class };
//TS::Packet class
/// Constructs an audio header to be used on each audio frame. /// 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 /// The length of this header will ALWAYS be 7 bytes, and has to be
/// prepended on each audio frame. /// prepended on each audio frame.
/// \param FrameLen the length of the current audio frame. /// \param FrameLen the length of the current audio frame.
static inline std::string GetAudioHeader( int FrameLen, std::string initData ) { static inline std::string GetAudioHeader(int FrameLen, std::string initData){
char StandardHeader[7] = {0xFF,0xF1,0x00,0x00,0x00,0x1F,0xFC}; char StandardHeader[7] = {0xFF, 0xF1, 0x00, 0x00, 0x00, 0x1F, 0xFC};
FrameLen += 7; FrameLen += 7;
StandardHeader[2] = ((((initData[0] >> 3) - 1) << 6 ) & 0xC0);//AAC Profile - 1 ( First two bits ) 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[0] & 0x07) << 1) | ((initData[1] >> 7) & 0x01)) << 2); //AAC Frequency Index
StandardHeader[2] |= ((initData[1] & 0x20) >> 5);//AAC Channel Config StandardHeader[2] |= ((initData[1] & 0x20) >> 5); //AAC Channel Config
StandardHeader[3] = ((initData[1] & 0x18 ) << 3 );//AAC CHannel Config (cont.) StandardHeader[3] = ((initData[1] & 0x18) << 3); //AAC CHannel Config (cont.)
StandardHeader[3] |= ( ( FrameLen & 0x00001800 ) >> 11 ); StandardHeader[3] |= ((FrameLen & 0x00001800) >> 11);
StandardHeader[4] = ( ( FrameLen & 0x000007F8 ) >> 3 ); StandardHeader[4] = ((FrameLen & 0x000007F8) >> 3);
StandardHeader[5] |= ( ( FrameLen & 0x00000007 ) << 5 ); StandardHeader[5] |= ((FrameLen & 0x00000007) << 5);
return std::string(StandardHeader,7); return std::string(StandardHeader, 7);
} }
/// A standard Program Association Table, as generated by FFMPEG. /// A standard Program Association Table, as generated by FFMPEG.
/// Seems to be independent of the stream. /// Seems to be independent of the stream.
static char PAT[188] = { static char PAT[188] = {0x47, 0x40, 0x00, 0x10, 0x00, 0x00, 0xB0, 0x0D, 0x00, 0x01, 0xC1, 0x00, 0x00, 0x00, 0x01, 0xF0, 0x00, 0x2A, 0xB1, 0x04,
0x47,0x40,0x00,0x10, 0x00,0x00,0xB0,0x0D, 0x00,0x01,0xC1,0x00, 0x00,0x00,0x01,0xF0, 0xB2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0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xFF, 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
};
/// A standard Program Mapping Table, as generated by FFMPEG. /// A standard Program Mapping Table, as generated by FFMPEG.
/// Contains both Audio and Video mappings, works also on video- or audio-only streams. /// Contains both Audio and Video mappings, works also on video- or audio-only streams.
static char PMT[188] = { static char PMT[188] = {0x47, 0x50, 0x00, 0x10, 0x00, 0x02, 0xB0, 0x17, 0x00, 0x01, 0xC1, 0x00, 0x00, 0xE1, 0x00, 0xF0, 0x00, 0x1B, 0xE1, 0x00,
0x47,0x50,0x00,0x10, 0x00,0x02,0xB0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Bytesteam Nal-Header. /// The shortened Bytesteam Nal-Header.
static char ShortNalHeader[3] = { static char ShortNalHeader[3] = {0x00, 0x00, 0x01};
0x00,0x00,0x01 }
}; ;
};//TS namespace //TS namespace